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;
17b5f636e6SThomas Zimmermann struct iosys_map;
18174102f4SNoralf Trønnes struct regulator;
19b5f636e6SThomas 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 /**
126*3b1fb8b3SOtto Pflüger * @regulator: power regulator (Vdd) (optional)
127174102f4SNoralf Trønnes */
128174102f4SNoralf Trønnes struct regulator *regulator;
129174102f4SNoralf Trønnes
130174102f4SNoralf Trønnes /**
131*3b1fb8b3SOtto Pflüger * @io_regulator: I/O power regulator (Vddi) (optional)
132*3b1fb8b3SOtto Pflüger */
133*3b1fb8b3SOtto Pflüger struct regulator *io_regulator;
134*3b1fb8b3SOtto Pflüger
135*3b1fb8b3SOtto Pflüger /**
136174102f4SNoralf Trønnes * @dbi: MIPI DBI interface
137174102f4SNoralf Trønnes */
138174102f4SNoralf Trønnes struct mipi_dbi dbi;
1391e7e8e18SNoralf Trønnes
1401e7e8e18SNoralf Trønnes /**
1411e7e8e18SNoralf Trønnes * @driver_private: Driver private data.
1421e7e8e18SNoralf Trønnes * Necessary for drivers with private data since devm_drm_dev_alloc()
1431e7e8e18SNoralf Trønnes * can't allocate structures that embed a structure which then again
1441e7e8e18SNoralf Trønnes * embeds drm_device.
1451e7e8e18SNoralf Trønnes */
1461e7e8e18SNoralf Trønnes void *driver_private;
147174102f4SNoralf Trønnes };
148174102f4SNoralf Trønnes
drm_to_mipi_dbi_dev(struct drm_device * drm)149174102f4SNoralf Trønnes static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm)
150174102f4SNoralf Trønnes {
151174102f4SNoralf Trønnes return container_of(drm, struct mipi_dbi_dev, drm);
152174102f4SNoralf Trønnes }
153174102f4SNoralf Trønnes
154174102f4SNoralf Trønnes int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
155174102f4SNoralf Trønnes struct gpio_desc *dc);
156174102f4SNoralf Trønnes int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
157174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs,
158174102f4SNoralf Trønnes const uint32_t *formats, unsigned int format_count,
159174102f4SNoralf Trønnes const struct drm_display_mode *mode,
160174102f4SNoralf Trønnes unsigned int rotation, size_t tx_buf_size);
161174102f4SNoralf Trønnes int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
162174102f4SNoralf Trønnes const struct drm_simple_display_pipe_funcs *funcs,
163174102f4SNoralf Trønnes const struct drm_display_mode *mode, unsigned int rotation);
164216b9bbaSThomas Zimmermann enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
165216b9bbaSThomas Zimmermann const struct drm_display_mode *mode);
166174102f4SNoralf Trønnes void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
167174102f4SNoralf Trønnes struct drm_plane_state *old_state);
168174102f4SNoralf Trønnes void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
169174102f4SNoralf Trønnes struct drm_crtc_state *crtc_state,
170174102f4SNoralf Trønnes struct drm_plane_state *plan_state);
171174102f4SNoralf Trønnes void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe);
172e7caf04dSThomas Zimmermann int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe,
173e7caf04dSThomas Zimmermann struct drm_plane_state *plane_state);
174e7caf04dSThomas Zimmermann void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe,
175e7caf04dSThomas Zimmermann struct drm_plane_state *plane_state);
176e7caf04dSThomas Zimmermann void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe);
177e7caf04dSThomas Zimmermann struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe);
178e7caf04dSThomas Zimmermann void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe,
179e7caf04dSThomas Zimmermann struct drm_plane_state *plane_state);
180e7caf04dSThomas Zimmermann
181174102f4SNoralf Trønnes void mipi_dbi_hw_reset(struct mipi_dbi *dbi);
182174102f4SNoralf Trønnes bool mipi_dbi_display_is_on(struct mipi_dbi *dbi);
183174102f4SNoralf Trønnes int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev);
184174102f4SNoralf Trønnes int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev);
185174102f4SNoralf Trønnes
186174102f4SNoralf Trønnes u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
187174102f4SNoralf Trønnes int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
188174102f4SNoralf Trønnes u8 bpw, const void *buf, size_t len);
189174102f4SNoralf Trønnes
190174102f4SNoralf Trønnes int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val);
191174102f4SNoralf Trønnes int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len);
192f019190bSGeert Uytterhoeven int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
193f019190bSGeert Uytterhoeven size_t len);
194b5f636e6SThomas Zimmermann int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
195174102f4SNoralf Trønnes struct drm_rect *clip, bool swap);
196b5f636e6SThomas Zimmermann
197174102f4SNoralf Trønnes /**
198174102f4SNoralf Trønnes * mipi_dbi_command - MIPI DCS command with optional parameter(s)
199174102f4SNoralf Trønnes * @dbi: MIPI DBI structure
200174102f4SNoralf Trønnes * @cmd: Command
2012b405ec0SJonathan Neuschäfer * @seq: Optional parameter(s)
202174102f4SNoralf Trønnes *
203174102f4SNoralf Trønnes * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for
204174102f4SNoralf Trønnes * get/read.
205174102f4SNoralf Trønnes *
206174102f4SNoralf Trønnes * Returns:
207174102f4SNoralf Trønnes * Zero on success, negative error code on failure.
208174102f4SNoralf Trønnes */
209174102f4SNoralf Trønnes #define mipi_dbi_command(dbi, cmd, seq...) \
210174102f4SNoralf Trønnes ({ \
211f019190bSGeert Uytterhoeven const u8 d[] = { seq }; \
2123f5aa5acSLinus Walleij struct device *dev = &(dbi)->spi->dev; \
2133f5aa5acSLinus Walleij int ret; \
2143f5aa5acSLinus Walleij ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \
2153f5aa5acSLinus Walleij if (ret) \
2163f5aa5acSLinus Walleij dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \
2173f5aa5acSLinus Walleij ret; \
218174102f4SNoralf Trønnes })
219174102f4SNoralf Trønnes
220174102f4SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
2217ce84471SWambui Karuga void mipi_dbi_debugfs_init(struct drm_minor *minor);
222174102f4SNoralf Trønnes #else
mipi_dbi_debugfs_init(struct drm_minor * minor)2236844a288SVille Syrjälä static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {}
224174102f4SNoralf Trønnes #endif
225174102f4SNoralf Trønnes
22663aa5ec6SThomas Zimmermann /**
22763aa5ec6SThomas Zimmermann * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs
22863aa5ec6SThomas Zimmermann * for MIPI-DBI devices
22963aa5ec6SThomas Zimmermann * @enable_: Enable-callback implementation
23063aa5ec6SThomas Zimmermann *
23163aa5ec6SThomas Zimmermann * This macro initializes struct drm_simple_display_pipe_funcs with default
23263aa5ec6SThomas Zimmermann * values for MIPI-DBI-based devices. The only callback that depends on the
23363aa5ec6SThomas Zimmermann * hardware is @enable, for which the driver has to provide an implementation.
23463aa5ec6SThomas Zimmermann * MIPI-based drivers are encouraged to use this macro for initialization.
23563aa5ec6SThomas Zimmermann */
23663aa5ec6SThomas Zimmermann #define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \
23763aa5ec6SThomas Zimmermann .mode_valid = mipi_dbi_pipe_mode_valid, \
23863aa5ec6SThomas Zimmermann .enable = (enable_), \
23963aa5ec6SThomas Zimmermann .disable = mipi_dbi_pipe_disable, \
240e7caf04dSThomas Zimmermann .update = mipi_dbi_pipe_update, \
241e7caf04dSThomas Zimmermann .begin_fb_access = mipi_dbi_pipe_begin_fb_access, \
242e7caf04dSThomas Zimmermann .end_fb_access = mipi_dbi_pipe_end_fb_access, \
243e7caf04dSThomas Zimmermann .reset_plane = mipi_dbi_pipe_reset_plane, \
244e7caf04dSThomas Zimmermann .duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, \
245e7caf04dSThomas Zimmermann .destroy_plane_state = mipi_dbi_pipe_destroy_plane_state
24663aa5ec6SThomas Zimmermann
247174102f4SNoralf Trønnes #endif /* __LINUX_MIPI_DBI_H */
248