Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 2 | /* |
| 3 | * MIPI Display Bus Interface (DBI) LCD controller support |
| 4 | * |
| 5 | * Copyright 2016 Noralf Trønnes |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef __LINUX_MIPI_DBI_H |
| 9 | #define __LINUX_MIPI_DBI_H |
| 10 | |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 11 | #include <linux/mutex.h> |
| 12 | #include <drm/drm_device.h> |
| 13 | #include <drm/drm_simple_kms_helper.h> |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 14 | |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 15 | struct drm_rect; |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 16 | struct spi_device; |
| 17 | struct gpio_desc; |
| 18 | struct regulator; |
| 19 | |
| 20 | /** |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 21 | * struct mipi_dbi - MIPI DBI interface |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 22 | */ |
| 23 | struct mipi_dbi { |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 24 | /** |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 25 | * @cmdlock: Command lock |
| 26 | */ |
| 27 | struct mutex cmdlock; |
| 28 | |
| 29 | /** |
| 30 | * @command: Bus specific callback executing commands. |
| 31 | */ |
| 32 | int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num); |
| 33 | |
| 34 | /** |
| 35 | * @read_commands: Array of read commands terminated by a zero entry. |
| 36 | * Reading is disabled if this is NULL. |
| 37 | */ |
| 38 | const u8 *read_commands; |
| 39 | |
| 40 | /** |
| 41 | * @swap_bytes: Swap bytes in buffer before transfer |
| 42 | */ |
| 43 | bool swap_bytes; |
| 44 | |
| 45 | /** |
| 46 | * @reset: Optional reset gpio |
| 47 | */ |
| 48 | struct gpio_desc *reset; |
| 49 | |
| 50 | /* Type C specific */ |
| 51 | |
| 52 | /** |
| 53 | * @spi: SPI device |
| 54 | */ |
| 55 | struct spi_device *spi; |
| 56 | |
| 57 | /** |
| 58 | * @dc: Optional D/C gpio. |
| 59 | */ |
| 60 | struct gpio_desc *dc; |
| 61 | |
| 62 | /** |
| 63 | * @tx_buf9: Buffer used for Option 1 9-bit conversion |
| 64 | */ |
| 65 | void *tx_buf9; |
| 66 | |
| 67 | /** |
| 68 | * @tx_buf9_len: Size of tx_buf9. |
| 69 | */ |
| 70 | size_t tx_buf9_len; |
| 71 | }; |
| 72 | |
| 73 | /** |
| 74 | * struct mipi_dbi_dev - MIPI DBI device |
| 75 | */ |
| 76 | struct mipi_dbi_dev { |
| 77 | /** |
Noralf Trønnes | 3eba392 | 2019-02-25 15:42:30 +0100 | [diff] [blame] | 78 | * @drm: DRM device |
| 79 | */ |
| 80 | struct drm_device drm; |
| 81 | |
| 82 | /** |
| 83 | * @pipe: Display pipe structure |
| 84 | */ |
| 85 | struct drm_simple_display_pipe pipe; |
| 86 | |
Noralf Trønnes | 710ae47 | 2019-07-19 17:59:16 +0200 | [diff] [blame] | 87 | /** |
| 88 | * @connector: Connector |
| 89 | */ |
| 90 | struct drm_connector connector; |
| 91 | |
| 92 | /** |
| 93 | * @mode: Fixed display mode |
| 94 | */ |
| 95 | struct drm_display_mode mode; |
| 96 | |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 97 | /** |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 98 | * @tx_buf: Buffer used for transfer (copy clip rect area) |
| 99 | */ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 100 | u16 *tx_buf; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * @rotation: initial rotation in degrees Counter Clock Wise |
| 104 | */ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 105 | unsigned int rotation; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 106 | |
| 107 | /** |
Geert Uytterhoeven | f41a8a6 | 2020-01-15 13:45:46 +0100 | [diff] [blame] | 108 | * @left_offset: Horizontal offset of the display relative to the |
| 109 | * controller's driver array |
| 110 | */ |
| 111 | unsigned int left_offset; |
| 112 | |
| 113 | /** |
| 114 | * @top_offset: Vertical offset of the display relative to the |
| 115 | * controller's driver array |
| 116 | */ |
| 117 | unsigned int top_offset; |
| 118 | |
| 119 | /** |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 120 | * @backlight: backlight device (optional) |
| 121 | */ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 122 | struct backlight_device *backlight; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * @regulator: power regulator (optional) |
| 126 | */ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 127 | struct regulator *regulator; |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * @dbi: MIPI DBI interface |
| 131 | */ |
| 132 | struct mipi_dbi dbi; |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 135 | static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm) |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 136 | { |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 137 | return container_of(drm, struct mipi_dbi_dev, drm); |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 140 | int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi, |
David Lechner | ace9881 | 2017-08-03 17:33:45 -0500 | [diff] [blame] | 141 | struct gpio_desc *dc); |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 142 | int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev, |
| 143 | const struct drm_simple_display_pipe_funcs *funcs, |
| 144 | const uint32_t *formats, unsigned int format_count, |
| 145 | const struct drm_display_mode *mode, |
| 146 | unsigned int rotation, size_t tx_buf_size); |
| 147 | int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev, |
| 148 | const struct drm_simple_display_pipe_funcs *funcs, |
| 149 | const struct drm_display_mode *mode, unsigned int rotation); |
Noralf Trønnes | af74138 | 2019-01-15 05:36:42 +0100 | [diff] [blame] | 150 | void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe, |
| 151 | struct drm_plane_state *old_state); |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 152 | void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev, |
Ville Syrjälä | e85d300 | 2018-03-23 17:35:09 +0200 | [diff] [blame] | 153 | struct drm_crtc_state *crtc_state, |
| 154 | struct drm_plane_state *plan_state); |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 155 | void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe); |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 156 | void mipi_dbi_hw_reset(struct mipi_dbi *dbi); |
| 157 | bool mipi_dbi_display_is_on(struct mipi_dbi *dbi); |
Noralf Trønnes | 84137b8 | 2019-07-22 12:43:07 +0200 | [diff] [blame] | 158 | int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev); |
| 159 | int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev); |
Noralf Trønnes | d23d4d4 | 2019-07-19 17:59:12 +0200 | [diff] [blame] | 160 | |
David Lechner | 13deee8 | 2017-11-19 14:12:07 -0600 | [diff] [blame] | 161 | u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len); |
Noralf Trønnes | d23d4d4 | 2019-07-19 17:59:12 +0200 | [diff] [blame] | 162 | int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz, |
| 163 | u8 bpw, const void *buf, size_t len); |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 164 | |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 165 | int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val); |
| 166 | int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len); |
Geert Uytterhoeven | f019190 | 2020-03-16 17:42:49 +0100 | [diff] [blame] | 167 | int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, |
| 168 | size_t len); |
David Lechner | 13deee8 | 2017-11-19 14:12:07 -0600 | [diff] [blame] | 169 | int mipi_dbi_buf_copy(void *dst, struct drm_framebuffer *fb, |
Noralf Trønnes | b051b34 | 2019-01-15 05:36:41 +0100 | [diff] [blame] | 170 | struct drm_rect *clip, bool swap); |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 171 | /** |
| 172 | * mipi_dbi_command - MIPI DCS command with optional parameter(s) |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 173 | * @dbi: MIPI DBI structure |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 174 | * @cmd: Command |
Jonathan Neuschäfer | 2b405ec | 2021-01-01 22:18:17 +0100 | [diff] [blame] | 175 | * @seq: Optional parameter(s) |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 176 | * |
| 177 | * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for |
| 178 | * get/read. |
| 179 | * |
| 180 | * Returns: |
| 181 | * Zero on success, negative error code on failure. |
| 182 | */ |
Noralf Trønnes | 36b5057 | 2019-07-22 12:43:05 +0200 | [diff] [blame] | 183 | #define mipi_dbi_command(dbi, cmd, seq...) \ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 184 | ({ \ |
Geert Uytterhoeven | f019190 | 2020-03-16 17:42:49 +0100 | [diff] [blame] | 185 | const u8 d[] = { seq }; \ |
Linus Walleij | 3f5aa5a | 2021-07-02 15:56:01 +0200 | [diff] [blame] | 186 | struct device *dev = &(dbi)->spi->dev; \ |
| 187 | int ret; \ |
| 188 | ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \ |
| 189 | if (ret) \ |
| 190 | dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \ |
| 191 | ret; \ |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 192 | }) |
| 193 | |
| 194 | #ifdef CONFIG_DEBUG_FS |
Wambui Karuga | 7ce84471 | 2020-03-10 16:31:21 +0300 | [diff] [blame] | 195 | void mipi_dbi_debugfs_init(struct drm_minor *minor); |
Noralf Trønnes | 02dd95f | 2017-01-22 00:30:47 +0100 | [diff] [blame] | 196 | #else |
| 197 | #define mipi_dbi_debugfs_init NULL |
| 198 | #endif |
| 199 | |
| 200 | #endif /* __LINUX_MIPI_DBI_H */ |