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 gpio_desc; 17*b5f636e6SThomas Zimmermann struct iosys_map; 18174102f4SNoralf Trønnes struct regulator; 19*b5f636e6SThomas Zimmermann struct spi_device; 20174102f4SNoralf Trønnes 21174102f4SNoralf Trønnes /** 22174102f4SNoralf Trønnes * struct mipi_dbi - MIPI DBI interface 23174102f4SNoralf Trønnes */ 24174102f4SNoralf Trønnes struct mipi_dbi { 25174102f4SNoralf Trønnes /** 26174102f4SNoralf Trønnes * @cmdlock: Command lock 27174102f4SNoralf Trønnes */ 28174102f4SNoralf Trønnes struct mutex cmdlock; 29174102f4SNoralf Trønnes 30174102f4SNoralf Trønnes /** 31174102f4SNoralf Trønnes * @command: Bus specific callback executing commands. 32174102f4SNoralf Trønnes */ 33174102f4SNoralf Trønnes int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num); 34174102f4SNoralf Trønnes 35174102f4SNoralf Trønnes /** 36174102f4SNoralf Trønnes * @read_commands: Array of read commands terminated by a zero entry. 37174102f4SNoralf Trønnes * Reading is disabled if this is NULL. 38174102f4SNoralf Trønnes */ 39174102f4SNoralf Trønnes const u8 *read_commands; 40174102f4SNoralf Trønnes 41174102f4SNoralf Trønnes /** 42174102f4SNoralf Trønnes * @swap_bytes: Swap bytes in buffer before transfer 43174102f4SNoralf Trønnes */ 44174102f4SNoralf Trønnes bool swap_bytes; 45174102f4SNoralf Trønnes 46174102f4SNoralf Trønnes /** 47174102f4SNoralf Trønnes * @reset: Optional reset gpio 48174102f4SNoralf Trønnes */ 49174102f4SNoralf Trønnes struct gpio_desc *reset; 50174102f4SNoralf Trønnes 51174102f4SNoralf Trønnes /* Type C specific */ 52174102f4SNoralf Trønnes 53174102f4SNoralf Trønnes /** 54174102f4SNoralf Trønnes * @spi: SPI device 55174102f4SNoralf Trønnes */ 56174102f4SNoralf Trønnes struct spi_device *spi; 57174102f4SNoralf Trønnes 58174102f4SNoralf Trønnes /** 59174102f4SNoralf Trønnes * @dc: Optional D/C gpio. 60174102f4SNoralf Trønnes */ 61174102f4SNoralf Trønnes struct gpio_desc *dc; 62174102f4SNoralf Trønnes 63174102f4SNoralf Trønnes /** 64174102f4SNoralf Trønnes * @tx_buf9: Buffer used for Option 1 9-bit conversion 65174102f4SNoralf Trønnes */ 66174102f4SNoralf Trønnes void *tx_buf9; 67174102f4SNoralf Trønnes 68174102f4SNoralf Trønnes /** 69174102f4SNoralf Trønnes * @tx_buf9_len: Size of tx_buf9. 70174102f4SNoralf Trønnes */ 71174102f4SNoralf Trønnes size_t tx_buf9_len; 72174102f4SNoralf Trønnes }; 73174102f4SNoralf Trønnes 74174102f4SNoralf Trønnes /** 75174102f4SNoralf Trønnes * struct mipi_dbi_dev - MIPI DBI device 76174102f4SNoralf Trønnes */ 77174102f4SNoralf Trønnes struct mipi_dbi_dev { 78174102f4SNoralf Trønnes /** 79174102f4SNoralf Trønnes * @drm: DRM device 80174102f4SNoralf Trønnes */ 81174102f4SNoralf Trønnes struct drm_device drm; 82174102f4SNoralf Trønnes 83174102f4SNoralf Trønnes /** 84174102f4SNoralf Trønnes * @pipe: Display pipe structure 85174102f4SNoralf Trønnes */ 86174102f4SNoralf Trønnes struct drm_simple_display_pipe pipe; 87174102f4SNoralf Trønnes 88174102f4SNoralf Trønnes /** 89174102f4SNoralf Trønnes * @connector: Connector 90174102f4SNoralf Trønnes */ 91174102f4SNoralf Trønnes struct drm_connector connector; 92174102f4SNoralf Trønnes 93174102f4SNoralf Trønnes /** 94174102f4SNoralf Trønnes * @mode: Fixed display mode 95174102f4SNoralf Trønnes */ 96174102f4SNoralf Trønnes struct drm_display_mode mode; 97174102f4SNoralf Trønnes 98174102f4SNoralf Trønnes /** 99174102f4SNoralf Trønnes * @tx_buf: Buffer used for transfer (copy clip rect area) 100174102f4SNoralf Trønnes */ 101174102f4SNoralf Trønnes u16 *tx_buf; 102174102f4SNoralf Trønnes 103174102f4SNoralf Trønnes /** 104174102f4SNoralf Trønnes * @rotation: initial rotation in degrees Counter Clock Wise 105174102f4SNoralf Trønnes */ 106174102f4SNoralf Trønnes unsigned int rotation; 107174102f4SNoralf Trønnes 108174102f4SNoralf Trønnes /** 109f41a8a69SGeert Uytterhoeven * @left_offset: Horizontal offset of the display relative to the 110f41a8a69SGeert Uytterhoeven * controller's driver array 111f41a8a69SGeert Uytterhoeven */ 112f41a8a69SGeert Uytterhoeven unsigned int left_offset; 113f41a8a69SGeert Uytterhoeven 114f41a8a69SGeert Uytterhoeven /** 115f41a8a69SGeert Uytterhoeven * @top_offset: Vertical offset of the display relative to the 116f41a8a69SGeert Uytterhoeven * controller's driver array 117f41a8a69SGeert Uytterhoeven */ 118f41a8a69SGeert Uytterhoeven unsigned int top_offset; 119f41a8a69SGeert Uytterhoeven 120f41a8a69SGeert Uytterhoeven /** 121174102f4SNoralf Trønnes * @backlight: backlight device (optional) 122174102f4SNoralf Trønnes */ 123174102f4SNoralf Trønnes struct backlight_device *backlight; 124174102f4SNoralf Trønnes 125174102f4SNoralf Trønnes /** 126174102f4SNoralf Trønnes * @regulator: power regulator (optional) 127174102f4SNoralf Trønnes */ 128174102f4SNoralf Trønnes struct regulator *regulator; 129174102f4SNoralf Trønnes 130174102f4SNoralf Trønnes /** 131174102f4SNoralf Trønnes * @dbi: MIPI DBI interface 132174102f4SNoralf Trønnes */ 133174102f4SNoralf Trønnes struct mipi_dbi dbi; 1341e7e8e18SNoralf Trønnes 1351e7e8e18SNoralf Trønnes /** 1361e7e8e18SNoralf Trønnes * @driver_private: Driver private data. 1371e7e8e18SNoralf Trønnes * Necessary for drivers with private data since devm_drm_dev_alloc() 1381e7e8e18SNoralf Trønnes * can't allocate structures that embed a structure which then again 1391e7e8e18SNoralf Trønnes * embeds drm_device. 1401e7e8e18SNoralf Trønnes */ 1411e7e8e18SNoralf Trønnes void *driver_private; 142174102f4SNoralf Trønnes }; 143174102f4SNoralf Trønnes 144174102f4SNoralf Trønnes static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm) 145174102f4SNoralf Trønnes { 146174102f4SNoralf Trønnes return container_of(drm, struct mipi_dbi_dev, drm); 147174102f4SNoralf Trønnes } 148174102f4SNoralf Trønnes 149174102f4SNoralf Trønnes int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi, 150174102f4SNoralf Trønnes struct gpio_desc *dc); 151174102f4SNoralf Trønnes int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev, 152174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs, 153174102f4SNoralf Trønnes const uint32_t *formats, unsigned int format_count, 154174102f4SNoralf Trønnes const struct drm_display_mode *mode, 155174102f4SNoralf Trønnes unsigned int rotation, size_t tx_buf_size); 156174102f4SNoralf Trønnes int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev, 157174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs, 158174102f4SNoralf Trønnes const struct drm_display_mode *mode, unsigned int rotation); 159216b9bbaSThomas Zimmermann enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe, 160216b9bbaSThomas Zimmermann const struct drm_display_mode *mode); 161174102f4SNoralf Trønnes void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe, 162174102f4SNoralf Trønnes struct drm_plane_state *old_state); 163174102f4SNoralf Trønnes void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev, 164174102f4SNoralf Trønnes struct drm_crtc_state *crtc_state, 165174102f4SNoralf Trønnes struct drm_plane_state *plan_state); 166174102f4SNoralf Trønnes void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe); 167174102f4SNoralf Trønnes void mipi_dbi_hw_reset(struct mipi_dbi *dbi); 168174102f4SNoralf Trønnes bool mipi_dbi_display_is_on(struct mipi_dbi *dbi); 169174102f4SNoralf Trønnes int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev); 170174102f4SNoralf Trønnes int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev); 171174102f4SNoralf Trønnes 172174102f4SNoralf Trønnes u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len); 173174102f4SNoralf Trønnes int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz, 174174102f4SNoralf Trønnes u8 bpw, const void *buf, size_t len); 175174102f4SNoralf Trønnes 176174102f4SNoralf Trønnes int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val); 177174102f4SNoralf Trønnes int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len); 178f019190bSGeert Uytterhoeven int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, 179f019190bSGeert Uytterhoeven size_t len); 180*b5f636e6SThomas Zimmermann int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb, 181174102f4SNoralf Trønnes struct drm_rect *clip, bool swap); 182*b5f636e6SThomas Zimmermann 183174102f4SNoralf Trønnes /** 184174102f4SNoralf Trønnes * mipi_dbi_command - MIPI DCS command with optional parameter(s) 185174102f4SNoralf Trønnes * @dbi: MIPI DBI structure 186174102f4SNoralf Trønnes * @cmd: Command 1872b405ec0SJonathan Neuschäfer * @seq: Optional parameter(s) 188174102f4SNoralf Trønnes * 189174102f4SNoralf Trønnes * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for 190174102f4SNoralf Trønnes * get/read. 191174102f4SNoralf Trønnes * 192174102f4SNoralf Trønnes * Returns: 193174102f4SNoralf Trønnes * Zero on success, negative error code on failure. 194174102f4SNoralf Trønnes */ 195174102f4SNoralf Trønnes #define mipi_dbi_command(dbi, cmd, seq...) \ 196174102f4SNoralf Trønnes ({ \ 197f019190bSGeert Uytterhoeven const u8 d[] = { seq }; \ 1983f5aa5acSLinus Walleij struct device *dev = &(dbi)->spi->dev; \ 1993f5aa5acSLinus Walleij int ret; \ 2003f5aa5acSLinus Walleij ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \ 2013f5aa5acSLinus Walleij if (ret) \ 2023f5aa5acSLinus Walleij dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \ 2033f5aa5acSLinus Walleij ret; \ 204174102f4SNoralf Trønnes }) 205174102f4SNoralf Trønnes 206174102f4SNoralf Trønnes #ifdef CONFIG_DEBUG_FS 2077ce84471SWambui Karuga void mipi_dbi_debugfs_init(struct drm_minor *minor); 208174102f4SNoralf Trønnes #else 2096844a288SVille Syrjälä static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {} 210174102f4SNoralf Trønnes #endif 211174102f4SNoralf Trønnes 21263aa5ec6SThomas Zimmermann /** 21363aa5ec6SThomas Zimmermann * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs 21463aa5ec6SThomas Zimmermann * for MIPI-DBI devices 21563aa5ec6SThomas Zimmermann * @enable_: Enable-callback implementation 21663aa5ec6SThomas Zimmermann * 21763aa5ec6SThomas Zimmermann * This macro initializes struct drm_simple_display_pipe_funcs with default 21863aa5ec6SThomas Zimmermann * values for MIPI-DBI-based devices. The only callback that depends on the 21963aa5ec6SThomas Zimmermann * hardware is @enable, for which the driver has to provide an implementation. 22063aa5ec6SThomas Zimmermann * MIPI-based drivers are encouraged to use this macro for initialization. 22163aa5ec6SThomas Zimmermann */ 22263aa5ec6SThomas Zimmermann #define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \ 22363aa5ec6SThomas Zimmermann .mode_valid = mipi_dbi_pipe_mode_valid, \ 22463aa5ec6SThomas Zimmermann .enable = (enable_), \ 22563aa5ec6SThomas Zimmermann .disable = mipi_dbi_pipe_disable, \ 22663aa5ec6SThomas Zimmermann .update = mipi_dbi_pipe_update 22763aa5ec6SThomas Zimmermann 228174102f4SNoralf Trønnes #endif /* __LINUX_MIPI_DBI_H */ 229