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