xref: /linux/include/drm/drm_mipi_dbi.h (revision 4cd24d4b)
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 
15*4cd24d4bSThomas Zimmermann struct drm_format_conv_state;
16174102f4SNoralf Trønnes struct drm_rect;
17174102f4SNoralf Trønnes struct gpio_desc;
18b5f636e6SThomas Zimmermann struct iosys_map;
19174102f4SNoralf Trønnes struct regulator;
20b5f636e6SThomas Zimmermann struct spi_device;
21174102f4SNoralf Trønnes 
22174102f4SNoralf Trønnes /**
23174102f4SNoralf Trønnes  * struct mipi_dbi - MIPI DBI interface
24174102f4SNoralf Trønnes  */
25174102f4SNoralf Trønnes struct mipi_dbi {
26174102f4SNoralf Trønnes 	/**
27174102f4SNoralf Trønnes 	 * @cmdlock: Command lock
28174102f4SNoralf Trønnes 	 */
29174102f4SNoralf Trønnes 	struct mutex cmdlock;
30174102f4SNoralf Trønnes 
31174102f4SNoralf Trønnes 	/**
32174102f4SNoralf Trønnes 	 * @command: Bus specific callback executing commands.
33174102f4SNoralf Trønnes 	 */
34174102f4SNoralf Trønnes 	int (*command)(struct mipi_dbi *dbi, u8 *cmd, u8 *param, size_t num);
35174102f4SNoralf Trønnes 
36174102f4SNoralf Trønnes 	/**
37174102f4SNoralf Trønnes 	 * @read_commands: Array of read commands terminated by a zero entry.
38174102f4SNoralf Trønnes 	 *                 Reading is disabled if this is NULL.
39174102f4SNoralf Trønnes 	 */
40174102f4SNoralf Trønnes 	const u8 *read_commands;
41174102f4SNoralf Trønnes 
42174102f4SNoralf Trønnes 	/**
43174102f4SNoralf Trønnes 	 * @swap_bytes: Swap bytes in buffer before transfer
44174102f4SNoralf Trønnes 	 */
45174102f4SNoralf Trønnes 	bool swap_bytes;
46174102f4SNoralf Trønnes 
47174102f4SNoralf Trønnes 	/**
48174102f4SNoralf Trønnes 	 * @reset: Optional reset gpio
49174102f4SNoralf Trønnes 	 */
50174102f4SNoralf Trønnes 	struct gpio_desc *reset;
51174102f4SNoralf Trønnes 
52174102f4SNoralf Trønnes 	/* Type C specific */
53174102f4SNoralf Trønnes 
54174102f4SNoralf Trønnes 	/**
55174102f4SNoralf Trønnes 	 * @spi: SPI device
56174102f4SNoralf Trønnes 	 */
57174102f4SNoralf Trønnes 	struct spi_device *spi;
58174102f4SNoralf Trønnes 
59174102f4SNoralf Trønnes 	/**
60174102f4SNoralf Trønnes 	 * @dc: Optional D/C gpio.
61174102f4SNoralf Trønnes 	 */
62174102f4SNoralf Trønnes 	struct gpio_desc *dc;
63174102f4SNoralf Trønnes 
64174102f4SNoralf Trønnes 	/**
65174102f4SNoralf Trønnes 	 * @tx_buf9: Buffer used for Option 1 9-bit conversion
66174102f4SNoralf Trønnes 	 */
67174102f4SNoralf Trønnes 	void *tx_buf9;
68174102f4SNoralf Trønnes 
69174102f4SNoralf Trønnes 	/**
70174102f4SNoralf Trønnes 	 * @tx_buf9_len: Size of tx_buf9.
71174102f4SNoralf Trønnes 	 */
72174102f4SNoralf Trønnes 	size_t tx_buf9_len;
73174102f4SNoralf Trønnes };
74174102f4SNoralf Trønnes 
75174102f4SNoralf Trønnes /**
76174102f4SNoralf Trønnes  * struct mipi_dbi_dev - MIPI DBI device
77174102f4SNoralf Trønnes  */
78174102f4SNoralf Trønnes struct mipi_dbi_dev {
79174102f4SNoralf Trønnes 	/**
80174102f4SNoralf Trønnes 	 * @drm: DRM device
81174102f4SNoralf Trønnes 	 */
82174102f4SNoralf Trønnes 	struct drm_device drm;
83174102f4SNoralf Trønnes 
84174102f4SNoralf Trønnes 	/**
85174102f4SNoralf Trønnes 	 * @pipe: Display pipe structure
86174102f4SNoralf Trønnes 	 */
87174102f4SNoralf Trønnes 	struct drm_simple_display_pipe pipe;
88174102f4SNoralf Trønnes 
89174102f4SNoralf Trønnes 	/**
90174102f4SNoralf Trønnes 	 * @connector: Connector
91174102f4SNoralf Trønnes 	 */
92174102f4SNoralf Trønnes 	struct drm_connector connector;
93174102f4SNoralf Trønnes 
94174102f4SNoralf Trønnes 	/**
95174102f4SNoralf Trønnes 	 * @mode: Fixed display mode
96174102f4SNoralf Trønnes 	 */
97174102f4SNoralf Trønnes 	struct drm_display_mode mode;
98174102f4SNoralf Trønnes 
99174102f4SNoralf Trønnes 	/**
100174102f4SNoralf Trønnes 	 * @tx_buf: Buffer used for transfer (copy clip rect area)
101174102f4SNoralf Trønnes 	 */
102174102f4SNoralf Trønnes 	u16 *tx_buf;
103174102f4SNoralf Trønnes 
104174102f4SNoralf Trønnes 	/**
105174102f4SNoralf Trønnes 	 * @rotation: initial rotation in degrees Counter Clock Wise
106174102f4SNoralf Trønnes 	 */
107174102f4SNoralf Trønnes 	unsigned int rotation;
108174102f4SNoralf Trønnes 
109174102f4SNoralf Trønnes 	/**
110f41a8a69SGeert Uytterhoeven 	 * @left_offset: Horizontal offset of the display relative to the
111f41a8a69SGeert Uytterhoeven 	 *               controller's driver array
112f41a8a69SGeert Uytterhoeven 	 */
113f41a8a69SGeert Uytterhoeven 	unsigned int left_offset;
114f41a8a69SGeert Uytterhoeven 
115f41a8a69SGeert Uytterhoeven 	/**
116f41a8a69SGeert Uytterhoeven 	 * @top_offset: Vertical offset of the display relative to the
117f41a8a69SGeert Uytterhoeven 	 *              controller's driver array
118f41a8a69SGeert Uytterhoeven 	 */
119f41a8a69SGeert Uytterhoeven 	unsigned int top_offset;
120f41a8a69SGeert Uytterhoeven 
121f41a8a69SGeert Uytterhoeven 	/**
122174102f4SNoralf Trønnes 	 * @backlight: backlight device (optional)
123174102f4SNoralf Trønnes 	 */
124174102f4SNoralf Trønnes 	struct backlight_device *backlight;
125174102f4SNoralf Trønnes 
126174102f4SNoralf Trønnes 	/**
1273b1fb8b3SOtto Pflüger 	 * @regulator: power regulator (Vdd) (optional)
128174102f4SNoralf Trønnes 	 */
129174102f4SNoralf Trønnes 	struct regulator *regulator;
130174102f4SNoralf Trønnes 
131174102f4SNoralf Trønnes 	/**
1323b1fb8b3SOtto Pflüger 	 * @io_regulator: I/O power regulator (Vddi) (optional)
1333b1fb8b3SOtto Pflüger 	 */
1343b1fb8b3SOtto Pflüger 	struct regulator *io_regulator;
1353b1fb8b3SOtto Pflüger 
1363b1fb8b3SOtto Pflüger 	/**
137174102f4SNoralf Trønnes 	 * @dbi: MIPI DBI interface
138174102f4SNoralf Trønnes 	 */
139174102f4SNoralf Trønnes 	struct mipi_dbi dbi;
1401e7e8e18SNoralf Trønnes 
1411e7e8e18SNoralf Trønnes 	/**
1421e7e8e18SNoralf Trønnes 	 * @driver_private: Driver private data.
1431e7e8e18SNoralf Trønnes 	 *                  Necessary for drivers with private data since devm_drm_dev_alloc()
1441e7e8e18SNoralf Trønnes 	 *                  can't allocate structures that embed a structure which then again
1451e7e8e18SNoralf Trønnes 	 *                  embeds drm_device.
1461e7e8e18SNoralf Trønnes 	 */
1471e7e8e18SNoralf Trønnes 	void *driver_private;
148174102f4SNoralf Trønnes };
149174102f4SNoralf Trønnes 
drm_to_mipi_dbi_dev(struct drm_device * drm)150174102f4SNoralf Trønnes static inline struct mipi_dbi_dev *drm_to_mipi_dbi_dev(struct drm_device *drm)
151174102f4SNoralf Trønnes {
152174102f4SNoralf Trønnes 	return container_of(drm, struct mipi_dbi_dev, drm);
153174102f4SNoralf Trønnes }
154174102f4SNoralf Trønnes 
155174102f4SNoralf Trønnes int mipi_dbi_spi_init(struct spi_device *spi, struct mipi_dbi *dbi,
156174102f4SNoralf Trønnes 		      struct gpio_desc *dc);
157174102f4SNoralf Trønnes int mipi_dbi_dev_init_with_formats(struct mipi_dbi_dev *dbidev,
158174102f4SNoralf Trønnes 				   const struct drm_simple_display_pipe_funcs *funcs,
159174102f4SNoralf Trønnes 				   const uint32_t *formats, unsigned int format_count,
160174102f4SNoralf Trønnes 				   const struct drm_display_mode *mode,
161174102f4SNoralf Trønnes 				   unsigned int rotation, size_t tx_buf_size);
162174102f4SNoralf Trønnes int mipi_dbi_dev_init(struct mipi_dbi_dev *dbidev,
163174102f4SNoralf Trønnes 		      const struct drm_simple_display_pipe_funcs *funcs,
164174102f4SNoralf Trønnes 		      const struct drm_display_mode *mode, unsigned int rotation);
165216b9bbaSThomas Zimmermann enum drm_mode_status mipi_dbi_pipe_mode_valid(struct drm_simple_display_pipe *pipe,
166216b9bbaSThomas Zimmermann 					      const struct drm_display_mode *mode);
167174102f4SNoralf Trønnes void mipi_dbi_pipe_update(struct drm_simple_display_pipe *pipe,
168174102f4SNoralf Trønnes 			  struct drm_plane_state *old_state);
169174102f4SNoralf Trønnes void mipi_dbi_enable_flush(struct mipi_dbi_dev *dbidev,
170174102f4SNoralf Trønnes 			   struct drm_crtc_state *crtc_state,
171174102f4SNoralf Trønnes 			   struct drm_plane_state *plan_state);
172174102f4SNoralf Trønnes void mipi_dbi_pipe_disable(struct drm_simple_display_pipe *pipe);
173e7caf04dSThomas Zimmermann int mipi_dbi_pipe_begin_fb_access(struct drm_simple_display_pipe *pipe,
174e7caf04dSThomas Zimmermann 				  struct drm_plane_state *plane_state);
175e7caf04dSThomas Zimmermann void mipi_dbi_pipe_end_fb_access(struct drm_simple_display_pipe *pipe,
176e7caf04dSThomas Zimmermann 				 struct drm_plane_state *plane_state);
177e7caf04dSThomas Zimmermann void mipi_dbi_pipe_reset_plane(struct drm_simple_display_pipe *pipe);
178e7caf04dSThomas Zimmermann struct drm_plane_state *mipi_dbi_pipe_duplicate_plane_state(struct drm_simple_display_pipe *pipe);
179e7caf04dSThomas Zimmermann void mipi_dbi_pipe_destroy_plane_state(struct drm_simple_display_pipe *pipe,
180e7caf04dSThomas Zimmermann 				       struct drm_plane_state *plane_state);
181e7caf04dSThomas Zimmermann 
182174102f4SNoralf Trønnes void mipi_dbi_hw_reset(struct mipi_dbi *dbi);
183174102f4SNoralf Trønnes bool mipi_dbi_display_is_on(struct mipi_dbi *dbi);
184174102f4SNoralf Trønnes int mipi_dbi_poweron_reset(struct mipi_dbi_dev *dbidev);
185174102f4SNoralf Trønnes int mipi_dbi_poweron_conditional_reset(struct mipi_dbi_dev *dbidev);
186174102f4SNoralf Trønnes 
187174102f4SNoralf Trønnes u32 mipi_dbi_spi_cmd_max_speed(struct spi_device *spi, size_t len);
188174102f4SNoralf Trønnes int mipi_dbi_spi_transfer(struct spi_device *spi, u32 speed_hz,
189174102f4SNoralf Trønnes 			  u8 bpw, const void *buf, size_t len);
190174102f4SNoralf Trønnes 
191174102f4SNoralf Trønnes int mipi_dbi_command_read(struct mipi_dbi *dbi, u8 cmd, u8 *val);
192174102f4SNoralf Trønnes int mipi_dbi_command_buf(struct mipi_dbi *dbi, u8 cmd, u8 *data, size_t len);
193f019190bSGeert Uytterhoeven int mipi_dbi_command_stackbuf(struct mipi_dbi *dbi, u8 cmd, const u8 *data,
194f019190bSGeert Uytterhoeven 			      size_t len);
195b5f636e6SThomas Zimmermann int mipi_dbi_buf_copy(void *dst, struct iosys_map *src, struct drm_framebuffer *fb,
196*4cd24d4bSThomas Zimmermann 		      struct drm_rect *clip, bool swap,
197*4cd24d4bSThomas Zimmermann 		      struct drm_format_conv_state *fmtcnv_state);
198b5f636e6SThomas Zimmermann 
199174102f4SNoralf Trønnes /**
200174102f4SNoralf Trønnes  * mipi_dbi_command - MIPI DCS command with optional parameter(s)
201174102f4SNoralf Trønnes  * @dbi: MIPI DBI structure
202174102f4SNoralf Trønnes  * @cmd: Command
2032b405ec0SJonathan Neuschäfer  * @seq: Optional parameter(s)
204174102f4SNoralf Trønnes  *
205174102f4SNoralf Trønnes  * Send MIPI DCS command to the controller. Use mipi_dbi_command_read() for
206174102f4SNoralf Trønnes  * get/read.
207174102f4SNoralf Trønnes  *
208174102f4SNoralf Trønnes  * Returns:
209174102f4SNoralf Trønnes  * Zero on success, negative error code on failure.
210174102f4SNoralf Trønnes  */
211174102f4SNoralf Trønnes #define mipi_dbi_command(dbi, cmd, seq...) \
212174102f4SNoralf Trønnes ({ \
213f019190bSGeert Uytterhoeven 	const u8 d[] = { seq }; \
2143f5aa5acSLinus Walleij 	struct device *dev = &(dbi)->spi->dev;	\
2153f5aa5acSLinus Walleij 	int ret; \
2163f5aa5acSLinus Walleij 	ret = mipi_dbi_command_stackbuf(dbi, cmd, d, ARRAY_SIZE(d)); \
2173f5aa5acSLinus Walleij 	if (ret) \
2183f5aa5acSLinus Walleij 		dev_err_ratelimited(dev, "error %d when sending command %#02x\n", ret, cmd); \
2193f5aa5acSLinus Walleij 	ret; \
220174102f4SNoralf Trønnes })
221174102f4SNoralf Trønnes 
222174102f4SNoralf Trønnes #ifdef CONFIG_DEBUG_FS
2237ce84471SWambui Karuga void mipi_dbi_debugfs_init(struct drm_minor *minor);
224174102f4SNoralf Trønnes #else
mipi_dbi_debugfs_init(struct drm_minor * minor)2256844a288SVille Syrjälä static inline void mipi_dbi_debugfs_init(struct drm_minor *minor) {}
226174102f4SNoralf Trønnes #endif
227174102f4SNoralf Trønnes 
22863aa5ec6SThomas Zimmermann /**
22963aa5ec6SThomas Zimmermann  * DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS - Initializes struct drm_simple_display_pipe_funcs
23063aa5ec6SThomas Zimmermann  *                                          for MIPI-DBI devices
23163aa5ec6SThomas Zimmermann  * @enable_: Enable-callback implementation
23263aa5ec6SThomas Zimmermann  *
23363aa5ec6SThomas Zimmermann  * This macro initializes struct drm_simple_display_pipe_funcs with default
23463aa5ec6SThomas Zimmermann  * values for MIPI-DBI-based devices. The only callback that depends on the
23563aa5ec6SThomas Zimmermann  * hardware is @enable, for which the driver has to provide an implementation.
23663aa5ec6SThomas Zimmermann  * MIPI-based drivers are encouraged to use this macro for initialization.
23763aa5ec6SThomas Zimmermann  */
23863aa5ec6SThomas Zimmermann #define DRM_MIPI_DBI_SIMPLE_DISPLAY_PIPE_FUNCS(enable_) \
23963aa5ec6SThomas Zimmermann 	.mode_valid = mipi_dbi_pipe_mode_valid, \
24063aa5ec6SThomas Zimmermann 	.enable = (enable_), \
24163aa5ec6SThomas Zimmermann 	.disable = mipi_dbi_pipe_disable, \
242e7caf04dSThomas Zimmermann 	.update = mipi_dbi_pipe_update, \
243e7caf04dSThomas Zimmermann 	.begin_fb_access = mipi_dbi_pipe_begin_fb_access, \
244e7caf04dSThomas Zimmermann 	.end_fb_access = mipi_dbi_pipe_end_fb_access, \
245e7caf04dSThomas Zimmermann 	.reset_plane = mipi_dbi_pipe_reset_plane, \
246e7caf04dSThomas Zimmermann 	.duplicate_plane_state = mipi_dbi_pipe_duplicate_plane_state, \
247e7caf04dSThomas Zimmermann 	.destroy_plane_state = mipi_dbi_pipe_destroy_plane_state
24863aa5ec6SThomas Zimmermann 
249174102f4SNoralf Trønnes #endif /* __LINUX_MIPI_DBI_H */
250