Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
02dd95fe NT |
2 | /* |
3 | * MIPI Display Bus Interface (DBI) LCD controller support | |
4 | * | |
5 | * Copyright 2016 Noralf Trønnes | |
02dd95fe NT |
6 | */ |
7 | ||
8 | #ifndef __LINUX_MIPI_DBI_H | |
9 | #define __LINUX_MIPI_DBI_H | |
10 | ||
3eba3922 NT |
11 | #include <linux/mutex.h> |
12 | #include <drm/drm_device.h> | |
13 | #include <drm/drm_simple_kms_helper.h> | |
02dd95fe | 14 | |
b051b345 | 15 | struct drm_rect; |
02dd95fe | 16 | struct gpio_desc; |
b5f636e6 | 17 | struct iosys_map; |
02dd95fe | 18 | struct regulator; |
b5f636e6 | 19 | struct spi_device; |
02dd95fe NT |
20 | |
21 | /** | |
84137b86 | 22 | * struct mipi_dbi - MIPI DBI interface |
02dd95fe NT |
23 | */ |
24 | struct mipi_dbi { | |
84137b86 NT |
25 | /** |
26 | * @cmdlock: Command lock | |
27 | */ | |
28 | struct mutex cmdlock; | |
29 | ||
30 | /** | |
31 | * @command: Bus specific callback executing commands. | |
32 | */ | |
33 | int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num); | |
34 | ||
35 | /** | |
36 | * @read_commands: Array of read commands terminated by a zero entry. | |
37 | * Reading is disabled if this is NULL. | |
38 | */ | |
39 | const u8 *read_commands; | |
40 | ||
41 | /** | |
42 | * @swap_bytes: Swap bytes in buffer before transfer | |
43 | */ | |
44 | bool swap_bytes; | |
45 | ||
46 | /** | |
47 | * @reset: Optional reset gpio | |
48 | */ | |
49 | struct gpio_desc *reset; | |
50 | ||
51 | /* Type C specific */ | |
52 | ||
53 | /** | |
54 | * @spi: SPI device | |
55 | */ | |
56 | struct spi_device *spi; | |
57 | ||
58 | /** | |
59 | * @dc: Optional D/C gpio. | |
60 | */ | |
61 | struct gpio_desc *dc; | |
62 | ||
63 | /** | |
64 | * @tx_buf9: Buffer used for Option 1 9-bit conversion | |
65 | */ | |
66 | void *tx_buf9; | |
67 | ||
68 | /** | |
69 | * @tx_buf9_len: Size of tx_buf9. | |
70 | */ | |
71 | size_t tx_buf9_len; | |
72 | }; | |
73 | ||
74 | /** | |
75 | * struct mipi_dbi_dev - MIPI DBI device | |
76 | */ | |
77 | struct mipi_dbi_dev { | |
3eba3922 NT |
78 | /** |
79 | * @drm: DRM device | |
80 | */ | |
81 | struct drm_device drm; | |
82 | ||
83 | /** | |
84 | * @pipe: Display pipe structure | |
85 | */ | |
86 | struct drm_simple_display_pipe pipe; | |
87 | ||
710ae47d NT |
88 | /** |
89 | * @connector: Connector | |
90 | */ | |
91 | struct drm_connector connector; | |
92 | ||
93 | /** | |
94 | * @mode: Fixed display mode | |
95 | */ | |
96 | struct drm_display_mode mode; | |
97 | ||
84137b86 | 98 | /** |
84137b86 NT |
99 | * @tx_buf: Buffer used for transfer (copy clip rect area) |
100 | */ | |
02dd95fe | 101 | u16 *tx_buf; |
84137b86 NT |
102 | |
103 | /** | |
104 | * @rotation: initial rotation in degrees Counter Clock Wise | |
105 | */ | |
02dd95fe | 106 | unsigned int rotation; |
84137b86 | 107 | |
f41a8a69 GU |
108 | /** |
109 | * @left_offset: Horizontal offset of the display relative to the | |
110 | * controller's driver array | |
111 | */ | |
112 | unsigned int left_offset; | |
113 | ||
114 | /** | |
115 | * @top_offset: Vertical offset of the display relative to the | |
116 | * controller's driver array | |
117 | */ | |
118 | unsigned int top_offset; | |
119 | ||
84137b86 NT |
120 | /** |
121 | * @backlight: backlight device (optional) | |
122 | */ | |
02dd95fe | 123 | struct backlight_device *backlight; |
84137b86 NT |
124 | |
125 | /** | |
3b1fb8b3 | 126 | * @regulator: power regulator (Vdd) (optional) |
84137b86 | 127 | */ |
02dd95fe | 128 | struct regulator *regulator; |
84137b86 | 129 | |
3b1fb8b3 OP |
130 | /** |
131 | * @io_regulator: I/O power regulator (Vddi) (optional) | |
132 | */ | |
133 | struct regulator *io_regulator; | |
134 | ||
84137b86 NT |
135 | /** |
136 | * @dbi: MIPI DBI interface | |
137 | */ | |
138 | struct mipi_dbi dbi; | |
1e7e8e18 NT |
139 | |
140 | /** | |
141 | * @driver_private: Driver private data. | |
142 | * Necessary for drivers with private data since devm_drm_dev_alloc() | |
143 | * can't allocate structures that embed a structure which then again | |
144 | * embeds drm_device. | |
145 | */ | |
146 | void *driver_private; | |
02dd95fe NT |
147 | }; |
148 | ||
84137b86 | 149 | static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm) |
02dd95fe | 150 | { |
84137b86 | 151 | return container_of(drm, struct mipi_dbi_dev, drm); |
02dd95fe NT |
152 | } |
153 | ||
36b50572 | 154 | int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi, |
ace98812 | 155 | struct gpio_desc *dc); |
84137b86 NT |
156 | int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev, |
157 | const struct drm_simple_display_pipe_funcs *funcs, | |
158 | const uint32_t *formats, unsigned int format_count, | |
159 | const struct drm_display_mode *mode, | |
160 | unsigned int rotation, size_t tx_buf_size); | |
161 | int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev, | |
162 | const struct drm_simple_display_pipe_funcs *funcs, | |
163 | const struct drm_display_mode *mode, unsigned int rotation); | |
216b9bba TZ |
164 | enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe, |
165 | const struct drm_display_mode *mode); | |
af741381 NT |
166 | void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe, |
167 | struct drm_plane_state *old_state); | |
84137b86 | 168 | void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev, |
e85d3006 VS |
169 | struct drm_crtc_state *crtc_state, |
170 | struct drm_plane_state *plan_state); | |
02dd95fe | 171 | void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe); |
e7caf04d TZ |
172 | int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe, |
173 | struct drm_plane_state *plane_state); | |
174 | void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe, | |
175 | struct drm_plane_state *plane_state); | |
176 | void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe); | |
177 | struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe); | |
178 | void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe, | |
179 | struct drm_plane_state *plane_state); | |
180 | ||
36b50572 NT |
181 | void mipi_dbi_hw_reset(struct mipi_dbi *dbi); |
182 | bool mipi_dbi_display_is_on(struct mipi_dbi *dbi); | |
84137b86 NT |
183 | int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev); |
184 | int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev); | |
d23d4d4d | 185 | |
13deee81 | 186 | u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len); |
d23d4d4d NT |
187 | int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz, |
188 | u8 bpw, const void *buf, size_t len); | |
02dd95fe | 189 | |
36b50572 NT |
190 | int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val); |
191 | int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len); | |
f019190b GU |
192 | int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data, |
193 | size_t len); | |
b5f636e6 | 194 | int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb, |
b051b345 | 195 | struct drm_rect *clip, bool swap); |
b5f636e6 | 196 | |
02dd95fe NT |
197 | /** |
198 | * mipi_dbi_command - MIPI DCS command with optional parameter(s) | |
36b50572 | 199 | * @dbi: MIPI DBI structure |
02dd95fe | 200 | * @cmd: Command |
2b405ec0 | 201 | * @seq: Optional parameter(s) |
02dd95fe NT |
202 | * |
203 | * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for | |
204 | * get/read. | |
205 | * | |
206 | * Returns: | |
207 | * Zero on success, negative error code on failure. | |
208 | */ | |
36b50572 | 209 | #define mipi_dbi_command(dbi, cmd, seq...) \ |
02dd95fe | 210 | ({ \ |
f019190b | 211 | const u8 d[] = { seq }; \ |
3f5aa5ac LW |
212 | struct device *dev = &(dbi)->spi->dev; \ |
213 | int ret; \ | |
214 | ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \ | |
215 | if (ret) \ | |
216 | dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \ | |
217 | ret; \ | |
02dd95fe NT |
218 | }) |
219 | ||
220 | #ifdef CONFIG_DEBUG_FS | |
7ce84471 | 221 | void mipi_dbi_debugfs_init(struct drm_minor *minor); |
02dd95fe | 222 | #else |
6844a288 | 223 | static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {} |
02dd95fe NT |
224 | #endif |
225 | ||
63aa5ec6 TZ |
226 | /** |
227 | * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs | |
228 | * for MIPI-DBI devices | |
229 | * @enable_: Enable-callback implementation | |
230 | * | |
231 | * This macro initializes struct drm_simple_display_pipe_funcs with default | |
232 | * values for MIPI-DBI-based devices. The only callback that depends on the | |
233 | * hardware is @enable, for which the driver has to provide an implementation. | |
234 | * MIPI-based drivers are encouraged to use this macro for initialization. | |
235 | */ | |
236 | #define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \ | |
237 | .mode_valid = mipi_dbi_pipe_mode_valid, \ | |
238 | .enable = (enable_), \ | |
239 | .disable = mipi_dbi_pipe_disable, \ | |
e7caf04d TZ |
240 | .update = mipi_dbi_pipe_update, \ |
241 | .begin_fb_access = mipi_dbi_pipe_begin_fb_access, \ | |
242 | .end_fb_access = mipi_dbi_pipe_end_fb_access, \ | |
243 | .reset_plane = mipi_dbi_pipe_reset_plane, \ | |
244 | .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, \ | |
245 | .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state | |
63aa5ec6 | 246 | |
02dd95fe | 247 | #endif /* __LINUX_MIPI_DBI_H */ |