1174102f4SNoralf Trønnes /* SPDX-License-Identifier: GPL-2.0-or-later */ 2174102f4SNoralf Trønnes /* 3174102f4SNoralf Trønnes * MIPI Display Bus Interface (DBI) LCD controller support 4174102f4SNoralf Trønnes * 5174102f4SNoralf Trønnes * Copyright 2016 Noralf Trønnes 6174102f4SNoralf Trønnes */ 7174102f4SNoralf Trønnes 8174102f4SNoralf Trønnes #ifndef __LINUX_MIPI_DBI_H 9174102f4SNoralf Trønnes #define __LINUX_MIPI_DBI_H 10174102f4SNoralf Trønnes 11174102f4SNoralf Trønnes #include <linux/mutex.h> 12174102f4SNoralf Trønnes #include <drm/drm_device.h> 13174102f4SNoralf Trønnes #include <drm/drm_simple_kms_helper.h> 14174102f4SNoralf Trønnes 15174102f4SNoralf Trønnes struct drm_rect; 16174102f4SNoralf Trønnes struct spi_device; 17174102f4SNoralf Trønnes struct gpio_desc; 18174102f4SNoralf Trønnes struct regulator; 19174102f4SNoralf Trønnes 20174102f4SNoralf Trønnes /** 21174102f4SNoralf Trønnes * struct mipi_dbi - MIPI DBI interface 22174102f4SNoralf Trønnes */ 23174102f4SNoralf Trønnes struct mipi_dbi { 24174102f4SNoralf Trønnes /** 25174102f4SNoralf Trønnes * @cmdlock: Command lock 26174102f4SNoralf Trønnes */ 27174102f4SNoralf Trønnes struct mutex cmdlock; 28174102f4SNoralf Trønnes 29174102f4SNoralf Trønnes /** 30174102f4SNoralf Trønnes * @command: Bus specific callback executing commands. 31174102f4SNoralf Trønnes */ 32174102f4SNoralf Trønnes int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num); 33174102f4SNoralf Trønnes 34174102f4SNoralf Trønnes /** 35174102f4SNoralf Trønnes * @read_commands: Array of read commands terminated by a zero entry. 36174102f4SNoralf Trønnes * Reading is disabled if this is NULL. 37174102f4SNoralf Trønnes */ 38174102f4SNoralf Trønnes const u8 *read_commands; 39174102f4SNoralf Trønnes 40174102f4SNoralf Trønnes /** 41174102f4SNoralf Trønnes * @swap_bytes: Swap bytes in buffer before transfer 42174102f4SNoralf Trønnes */ 43174102f4SNoralf Trønnes bool swap_bytes; 44174102f4SNoralf Trønnes 45174102f4SNoralf Trønnes /** 46174102f4SNoralf Trønnes * @reset: Optional reset gpio 47174102f4SNoralf Trønnes */ 48174102f4SNoralf Trønnes struct gpio_desc *reset; 49174102f4SNoralf Trønnes 50174102f4SNoralf Trønnes /* Type C specific */ 51174102f4SNoralf Trønnes 52174102f4SNoralf Trønnes /** 53174102f4SNoralf Trønnes * @spi: SPI device 54174102f4SNoralf Trønnes */ 55174102f4SNoralf Trønnes struct spi_device *spi; 56174102f4SNoralf Trønnes 57174102f4SNoralf Trønnes /** 58174102f4SNoralf Trønnes * @dc: Optional D/C gpio. 59174102f4SNoralf Trønnes */ 60174102f4SNoralf Trønnes struct gpio_desc *dc; 61174102f4SNoralf Trønnes 62174102f4SNoralf Trønnes /** 63174102f4SNoralf Trønnes * @tx_buf9: Buffer used for Option 1 9-bit conversion 64174102f4SNoralf Trønnes */ 65174102f4SNoralf Trønnes void *tx_buf9; 66174102f4SNoralf Trønnes 67174102f4SNoralf Trønnes /** 68174102f4SNoralf Trønnes * @tx_buf9_len: Size of tx_buf9. 69174102f4SNoralf Trønnes */ 70174102f4SNoralf Trønnes size_t tx_buf9_len; 71174102f4SNoralf Trønnes }; 72174102f4SNoralf Trønnes 73174102f4SNoralf Trønnes /** 74174102f4SNoralf Trønnes * struct mipi_dbi_dev - MIPI DBI device 75174102f4SNoralf Trønnes */ 76174102f4SNoralf Trønnes struct mipi_dbi_dev { 77174102f4SNoralf Trønnes /** 78174102f4SNoralf Trønnes * @drm: DRM device 79174102f4SNoralf Trønnes */ 80174102f4SNoralf Trønnes struct drm_device drm; 81174102f4SNoralf Trønnes 82174102f4SNoralf Trønnes /** 83174102f4SNoralf Trønnes * @pipe: Display pipe structure 84174102f4SNoralf Trønnes */ 85174102f4SNoralf Trønnes struct drm_simple_display_pipe pipe; 86174102f4SNoralf Trønnes 87174102f4SNoralf Trønnes /** 88174102f4SNoralf Trønnes * @connector: Connector 89174102f4SNoralf Trønnes */ 90174102f4SNoralf Trønnes struct drm_connector connector; 91174102f4SNoralf Trønnes 92174102f4SNoralf Trønnes /** 93174102f4SNoralf Trønnes * @mode: Fixed display mode 94174102f4SNoralf Trønnes */ 95174102f4SNoralf Trønnes struct drm_display_mode mode; 96174102f4SNoralf Trønnes 97174102f4SNoralf Trønnes /** 98174102f4SNoralf Trønnes * @tx_buf: Buffer used for transfer (copy clip rect area) 99174102f4SNoralf Trønnes */ 100174102f4SNoralf Trønnes u16 *tx_buf; 101174102f4SNoralf Trønnes 102174102f4SNoralf Trønnes /** 103174102f4SNoralf Trønnes * @rotation: initial rotation in degrees Counter Clock Wise 104174102f4SNoralf Trønnes */ 105174102f4SNoralf Trønnes unsigned int rotation; 106174102f4SNoralf Trønnes 107174102f4SNoralf Trønnes /** 108f41a8a69SGeert Uytterhoeven * @left_offset: Horizontal offset of the display relative to the 109f41a8a69SGeert Uytterhoeven * controller's driver array 110f41a8a69SGeert Uytterhoeven */ 111f41a8a69SGeert Uytterhoeven unsigned int left_offset; 112f41a8a69SGeert Uytterhoeven 113f41a8a69SGeert Uytterhoeven /** 114f41a8a69SGeert Uytterhoeven * @top_offset: Vertical offset of the display relative to the 115f41a8a69SGeert Uytterhoeven * controller's driver array 116f41a8a69SGeert Uytterhoeven */ 117f41a8a69SGeert Uytterhoeven unsigned int top_offset; 118f41a8a69SGeert Uytterhoeven 119f41a8a69SGeert Uytterhoeven /** 120174102f4SNoralf Trønnes * @backlight: backlight device (optional) 121174102f4SNoralf Trønnes */ 122174102f4SNoralf Trønnes struct backlight_device *backlight; 123174102f4SNoralf Trønnes 124174102f4SNoralf Trønnes /** 125174102f4SNoralf Trønnes * @regulator: power regulator (optional) 126174102f4SNoralf Trønnes */ 127174102f4SNoralf Trønnes struct regulator *regulator; 128174102f4SNoralf Trønnes 129174102f4SNoralf Trønnes /** 130174102f4SNoralf Trønnes * @dbi: MIPI DBI interface 131174102f4SNoralf Trønnes */ 132174102f4SNoralf Trønnes struct mipi_dbi dbi; 1331e7e8e18SNoralf Trønnes 1341e7e8e18SNoralf Trønnes /** 1351e7e8e18SNoralf Trønnes * @driver_private: Driver private data. 1361e7e8e18SNoralf Trønnes * Necessary for drivers with private data since devm_drm_dev_alloc() 1371e7e8e18SNoralf Trønnes * can't allocate structures that embed a structure which then again 1381e7e8e18SNoralf Trønnes * embeds drm_device. 1391e7e8e18SNoralf Trønnes */ 1401e7e8e18SNoralf Trønnes void *driver_private; 141174102f4SNoralf Trønnes }; 142174102f4SNoralf Trønnes 143174102f4SNoralf Trønnes static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm) 144174102f4SNoralf Trønnes { 145174102f4SNoralf Trønnes return container_of(drm, struct mipi_dbi_dev, drm); 146174102f4SNoralf Trønnes } 147174102f4SNoralf Trønnes 148174102f4SNoralf Trønnes int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi, 149174102f4SNoralf Trønnes struct gpio_desc *dc); 150174102f4SNoralf Trønnes int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev, 151174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs, 152174102f4SNoralf Trønnes const uint32_t *formats, unsigned int format_count, 153174102f4SNoralf Trønnes const struct drm_display_mode *mode, 154174102f4SNoralf Trønnes unsigned int rotation, size_t tx_buf_size); 155174102f4SNoralf Trønnes int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev, 156174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs, 157174102f4SNoralf Trønnes const struct drm_display_mode *mode, unsigned int rotation); 158*216b9bbaSThomas Zimmermann enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe, 159*216b9bbaSThomas Zimmermann const struct drm_display_mode *mode); 160174102f4SNoralf Trønnes void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe, 161174102f4SNoralf Trønnes struct drm_plane_state *old_state); 162174102f4SNoralf Trønnes void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev, 163174102f4SNoralf Trønnes struct drm_crtc_state *crtc_state, 164174102f4SNoralf Trønnes struct drm_plane_state *plan_state); 165174102f4SNoralf Trønnes void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe); 166174102f4SNoralf Trønnes void mipi_dbi_hw_reset(struct mipi_dbi *dbi); 167174102f4SNoralf Trønnes bool mipi_dbi_display_is_on(struct mipi_dbi *dbi); 168174102f4SNoralf Trønnes int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev); 169174102f4SNoralf Trønnes int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev); 170174102f4SNoralf Trønnes 171174102f4SNoralf Trønnes u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len); 172174102f4SNoralf Trønnes int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz, 173174102f4SNoralf Trønnes u8 bpw, const void *buf, size_t len); 174174102f4SNoralf Trønnes 175174102f4SNoralf Trønnes int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val); 176174102f4SNoralf Trønnes int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len); 177f019190bSGeert Uytterhoeven int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, 178f019190bSGeert Uytterhoeven size_t len); 179174102f4SNoralf Trønnes int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb, 180174102f4SNoralf Trønnes struct drm_rect *clip, bool swap); 181174102f4SNoralf Trønnes /** 182174102f4SNoralf Trønnes * mipi_dbi_command - MIPI DCS command with optional parameter(s) 183174102f4SNoralf Trønnes * @dbi: MIPI DBI structure 184174102f4SNoralf Trønnes * @cmd: Command 1852b405ec0SJonathan Neuschäfer * @seq: Optional parameter(s) 186174102f4SNoralf Trønnes * 187174102f4SNoralf Trønnes * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for 188174102f4SNoralf Trønnes * get/read. 189174102f4SNoralf Trønnes * 190174102f4SNoralf Trønnes * Returns: 191174102f4SNoralf Trønnes * Zero on success, negative error code on failure. 192174102f4SNoralf Trønnes */ 193174102f4SNoralf Trønnes #define mipi_dbi_command(dbi, cmd, seq...) \ 194174102f4SNoralf Trønnes ({ \ 195f019190bSGeert Uytterhoeven const u8 d[] = { seq }; \ 1963f5aa5acSLinus Walleij struct device *dev = &(dbi)->spi->dev; \ 1973f5aa5acSLinus Walleij int ret; \ 1983f5aa5acSLinus Walleij ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \ 1993f5aa5acSLinus Walleij if (ret) \ 2003f5aa5acSLinus Walleij dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \ 2013f5aa5acSLinus Walleij ret; \ 202174102f4SNoralf Trønnes }) 203174102f4SNoralf Trønnes 204174102f4SNoralf Trønnes #ifdef CONFIG_DEBUG_FS 2057ce84471SWambui Karuga void mipi_dbi_debugfs_init(struct drm_minor *minor); 206174102f4SNoralf Trønnes #else 2076844a288SVille Syrjälä static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {} 208174102f4SNoralf Trønnes #endif 209174102f4SNoralf Trønnes 210174102f4SNoralf Trønnes #endif /* __LINUX_MIPI_DBI_H */ 211