• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * MIPI DSI Bus
4  *
5  * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
6  * Andrzej Hajda <a.hajda@samsung.com>
7  */
8 
9 #ifndef __DRM_MIPI_DSI_H__
10 #define __DRM_MIPI_DSI_H__
11 
12 #include <linux/device.h>
13 
14 struct mipi_dsi_host;
15 struct mipi_dsi_device;
16 struct drm_dsc_picture_parameter_set;
17 
18 /* request ACK from peripheral */
19 #define MIPI_DSI_MSG_REQ_ACK	BIT(0)
20 /* use Low Power Mode to transmit message */
21 #define MIPI_DSI_MSG_USE_LPM	BIT(1)
22 /* read mipi_dsi_msg.ctrl and unicast to only that ctrls */
23 #define MIPI_DSI_MSG_UNICAST	BIT(2)
24 /* Stack all commands until lastcommand bit and trigger all in one go */
25 #define MIPI_DSI_MSG_LASTCOMMAND BIT(3)
26 
27 /**
28  * struct mipi_dsi_msg - read/write DSI buffer
29  * @channel: virtual channel id
30  * @type: payload data type
31  * @flags: flags controlling this message transmission
32  * @ctrl: ctrl index to transmit on
33  * @wait_ms: duration in ms to wait after message transmission
34  * @tx_len: length of @tx_buf
35  * @tx_buf: data to be written
36  * @rx_len: length of @rx_buf
37  * @rx_buf: data to be read, or NULL
38  */
39 struct mipi_dsi_msg {
40 	u8 channel;
41 	u8 type;
42 	u16 flags;
43 	u32 ctrl;
44 	u32 wait_ms;
45 
46 	size_t tx_len;
47 	const void *tx_buf;
48 
49 	size_t rx_len;
50 	void *rx_buf;
51 };
52 
53 bool mipi_dsi_packet_format_is_short(u8 type);
54 bool mipi_dsi_packet_format_is_long(u8 type);
55 
56 /**
57  * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
58  * @size: size (in bytes) of the packet
59  * @header: the four bytes that make up the header (Data ID, Word Count or
60  *     Packet Data, and ECC)
61  * @payload_length: number of bytes in the payload
62  * @payload: a pointer to a buffer containing the payload, if any
63  */
64 struct mipi_dsi_packet {
65 	size_t size;
66 	u8 header[4];
67 	size_t payload_length;
68 	const u8 *payload;
69 };
70 
71 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
72 			   const struct mipi_dsi_msg *msg);
73 
74 /**
75  * struct mipi_dsi_host_ops - DSI bus operations
76  * @attach: attach DSI device to DSI host
77  * @detach: detach DSI device from DSI host
78  * @transfer: transmit a DSI packet
79  *
80  * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
81  * structures. This structure contains information about the type of packet
82  * being transmitted as well as the transmit and receive buffers. When an
83  * error is encountered during transmission, this function will return a
84  * negative error code. On success it shall return the number of bytes
85  * transmitted for write packets or the number of bytes received for read
86  * packets.
87  *
88  * Note that typically DSI packet transmission is atomic, so the .transfer()
89  * function will seldomly return anything other than the number of bytes
90  * contained in the transmit buffer on success.
91  */
92 struct mipi_dsi_host_ops {
93 	int (*attach)(struct mipi_dsi_host *host,
94 		      struct mipi_dsi_device *dsi);
95 	int (*detach)(struct mipi_dsi_host *host,
96 		      struct mipi_dsi_device *dsi);
97 	ssize_t (*transfer)(struct mipi_dsi_host *host,
98 			    const struct mipi_dsi_msg *msg);
99 };
100 
101 /**
102  * struct mipi_dsi_host - DSI host device
103  * @dev: driver model device node for this DSI host
104  * @ops: DSI host operations
105  * @list: list management
106  */
107 struct mipi_dsi_host {
108 	struct device *dev;
109 	const struct mipi_dsi_host_ops *ops;
110 	struct list_head list;
111 };
112 
113 int mipi_dsi_host_register(struct mipi_dsi_host *host);
114 void mipi_dsi_host_unregister(struct mipi_dsi_host *host);
115 struct mipi_dsi_host *of_find_mipi_dsi_host_by_node(struct device_node *node);
116 
117 /* DSI mode flags */
118 
119 /* video mode */
120 #define MIPI_DSI_MODE_VIDEO		BIT(0)
121 /* video burst mode */
122 #define MIPI_DSI_MODE_VIDEO_BURST	BIT(1)
123 /* video pulse mode */
124 #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE	BIT(2)
125 /* enable auto vertical count mode */
126 #define MIPI_DSI_MODE_VIDEO_AUTO_VERT	BIT(3)
127 /* enable hsync-end packets in vsync-pulse and v-porch area */
128 #define MIPI_DSI_MODE_VIDEO_HSE		BIT(4)
129 /* disable hfront-porch area */
130 #define MIPI_DSI_MODE_VIDEO_HFP		BIT(5)
131 /* disable hback-porch area */
132 #define MIPI_DSI_MODE_VIDEO_HBP		BIT(6)
133 /* disable hsync-active area */
134 #define MIPI_DSI_MODE_VIDEO_HSA		BIT(7)
135 /* flush display FIFO on vsync pulse */
136 #define MIPI_DSI_MODE_VSYNC_FLUSH	BIT(8)
137 /* disable EoT packets in HS mode */
138 #define MIPI_DSI_MODE_EOT_PACKET	BIT(9)
139 /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
140 #define MIPI_DSI_CLOCK_NON_CONTINUOUS	BIT(10)
141 /* transmit data in low power */
142 #define MIPI_DSI_MODE_LPM		BIT(11)
143 /* disable BLLP area */
144 #define MIPI_DSI_MODE_VIDEO_BLLP	BIT(12)
145 /* disable EOF BLLP area */
146 #define MIPI_DSI_MODE_VIDEO_EOF_BLLP	BIT(13)
147 
148 enum mipi_dsi_pixel_format {
149 	MIPI_DSI_FMT_RGB888,
150 	MIPI_DSI_FMT_RGB666,
151 	MIPI_DSI_FMT_RGB666_PACKED,
152 	MIPI_DSI_FMT_RGB565,
153 };
154 
155 #define DSI_DEV_NAME_SIZE		20
156 
157 /**
158  * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
159  * @type: DSI peripheral chip type
160  * @channel: DSI virtual channel assigned to peripheral
161  * @node: pointer to OF device node or NULL
162  *
163  * This is populated and passed to mipi_dsi_device_new to create a new
164  * DSI device
165  */
166 struct mipi_dsi_device_info {
167 	char type[DSI_DEV_NAME_SIZE];
168 	u32 channel;
169 	struct device_node *node;
170 };
171 
172 /**
173  * struct mipi_dsi_device - DSI peripheral device
174  * @host: DSI host for this peripheral
175  * @dev: driver model device node for this peripheral
176  * @name: DSI peripheral chip type
177  * @channel: virtual channel assigned to the peripheral
178  * @format: pixel format for video mode
179  * @lanes: number of active data lanes
180  * @mode_flags: DSI operation mode related flags
181  * @hs_rate: maximum lane frequency for high speed mode in hertz, this should
182  * be set to the real limits of the hardware, zero is only accepted for
183  * legacy drivers
184  * @lp_rate: maximum lane frequency for low power mode in hertz, this should
185  * be set to the real limits of the hardware, zero is only accepted for
186  * legacy drivers
187  */
188 struct mipi_dsi_device {
189 	struct mipi_dsi_host *host;
190 	struct device dev;
191 
192 	char name[DSI_DEV_NAME_SIZE];
193 	unsigned int channel;
194 	unsigned int lanes;
195 	enum mipi_dsi_pixel_format format;
196 	unsigned long mode_flags;
197 	unsigned long hs_rate;
198 	unsigned long lp_rate;
199 };
200 
201 #define MIPI_DSI_MODULE_PREFIX "mipi-dsi:"
202 
to_mipi_dsi_device(struct device * dev)203 static inline struct mipi_dsi_device *to_mipi_dsi_device(struct device *dev)
204 {
205 	return container_of(dev, struct mipi_dsi_device, dev);
206 }
207 
208 /**
209  * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
210  *                                given pixel format defined by the MIPI DSI
211  *                                specification
212  * @fmt: MIPI DSI pixel format
213  *
214  * Returns: The number of bits per pixel of the given pixel format.
215  */
mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)216 static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
217 {
218 	switch (fmt) {
219 	case MIPI_DSI_FMT_RGB888:
220 	case MIPI_DSI_FMT_RGB666:
221 		return 24;
222 
223 	case MIPI_DSI_FMT_RGB666_PACKED:
224 		return 18;
225 
226 	case MIPI_DSI_FMT_RGB565:
227 		return 16;
228 	}
229 
230 	return -EINVAL;
231 }
232 
233 struct mipi_dsi_device *
234 mipi_dsi_device_register_full(struct mipi_dsi_host *host,
235 			      const struct mipi_dsi_device_info *info);
236 void mipi_dsi_device_unregister(struct mipi_dsi_device *dsi);
237 struct mipi_dsi_device *of_find_mipi_dsi_device_by_node(struct device_node *np);
238 int mipi_dsi_attach(struct mipi_dsi_device *dsi);
239 int mipi_dsi_detach(struct mipi_dsi_device *dsi);
240 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
241 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
242 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
243 					    u16 value);
244 ssize_t mipi_dsi_compression_mode(struct mipi_dsi_device *dsi, bool enable);
245 ssize_t mipi_dsi_picture_parameter_set(struct mipi_dsi_device *dsi,
246 				       const struct drm_dsc_picture_parameter_set *pps);
247 
248 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
249 			       size_t size);
250 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
251 			      size_t num_params, void *data, size_t size);
252 
253 /**
254  * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
255  * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
256  *    information only
257  * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
258  *    V-Blanking and H-Blanking information
259  */
260 enum mipi_dsi_dcs_tear_mode {
261 	MIPI_DSI_DCS_TEAR_MODE_VBLANK,
262 	MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
263 };
264 
265 #define MIPI_DSI_DCS_POWER_MODE_DISPLAY (1 << 2)
266 #define MIPI_DSI_DCS_POWER_MODE_NORMAL  (1 << 3)
267 #define MIPI_DSI_DCS_POWER_MODE_SLEEP   (1 << 4)
268 #define MIPI_DSI_DCS_POWER_MODE_PARTIAL (1 << 5)
269 #define MIPI_DSI_DCS_POWER_MODE_IDLE    (1 << 6)
270 
271 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
272 				  const void *data, size_t len);
273 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
274 			   const void *data, size_t len);
275 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
276 			  size_t len);
277 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
278 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
279 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
280 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
281 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
282 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
283 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
284 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
285 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
286 				    u16 end);
287 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
288 				  u16 end);
289 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
290 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
291 			     enum mipi_dsi_dcs_tear_mode mode);
292 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
293 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
294 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
295 					u16 brightness);
296 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
297 					u16 *brightness);
298 int mipi_dsi_dcs_set_display_brightness_large(struct mipi_dsi_device *dsi,
299 					     u16 brightness);
300 int mipi_dsi_dcs_get_display_brightness_large(struct mipi_dsi_device *dsi,
301 					     u16 *brightness);
302 
303 /**
304  * struct mipi_dsi_driver - DSI driver
305  * @driver: device driver model driver
306  * @probe: callback for device binding
307  * @remove: callback for device unbinding
308  * @shutdown: called at shutdown time to quiesce the device
309  */
310 struct mipi_dsi_driver {
311 	struct device_driver driver;
312 	int(*probe)(struct mipi_dsi_device *dsi);
313 	int(*remove)(struct mipi_dsi_device *dsi);
314 	void (*shutdown)(struct mipi_dsi_device *dsi);
315 };
316 
317 static inline struct mipi_dsi_driver *
to_mipi_dsi_driver(struct device_driver * driver)318 to_mipi_dsi_driver(struct device_driver *driver)
319 {
320 	return container_of(driver, struct mipi_dsi_driver, driver);
321 }
322 
mipi_dsi_get_drvdata(const struct mipi_dsi_device * dsi)323 static inline void *mipi_dsi_get_drvdata(const struct mipi_dsi_device *dsi)
324 {
325 	return dev_get_drvdata(&dsi->dev);
326 }
327 
mipi_dsi_set_drvdata(struct mipi_dsi_device * dsi,void * data)328 static inline void mipi_dsi_set_drvdata(struct mipi_dsi_device *dsi, void *data)
329 {
330 	dev_set_drvdata(&dsi->dev, data);
331 }
332 
333 int mipi_dsi_driver_register_full(struct mipi_dsi_driver *driver,
334 				  struct module *owner);
335 void mipi_dsi_driver_unregister(struct mipi_dsi_driver *driver);
336 
337 #define mipi_dsi_driver_register(driver) \
338 	mipi_dsi_driver_register_full(driver, THIS_MODULE)
339 
340 #define module_mipi_dsi_driver(__mipi_dsi_driver) \
341 	module_driver(__mipi_dsi_driver, mipi_dsi_driver_register, \
342 			mipi_dsi_driver_unregister)
343 
344 #endif /* __DRM_MIPI_DSI__ */
345