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