video: dw-mipi-dsi: driver-specific configuration of phy timings
[platform/kernel/u-boot.git] / include / mipi_dsi.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * MIPI DSI Bus
4  *
5  * Copyright (C) 2012-2013, Samsung Electronics, Co., Ltd.
6  * Copyright (C) 2018 STMicroelectronics - All Rights Reserved
7  * Author(s): Andrzej Hajda <a.hajda@samsung.com>
8  *            Yannick Fertre <yannick.fertre@st.com>
9  *            Philippe Cornu <philippe.cornu@st.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
15 #ifndef MIPI_DSI_H
16 #define MIPI_DSI_H
17
18 #include <mipi_display.h>
19 #include <linux/bitops.h>
20
21 struct mipi_dsi_host;
22 struct mipi_dsi_device;
23
24 /* request ACK from peripheral */
25 #define MIPI_DSI_MSG_REQ_ACK    BIT(0)
26 /* use Low Power Mode to transmit message */
27 #define MIPI_DSI_MSG_USE_LPM    BIT(1)
28
29 /**
30  * struct mipi_dsi_msg - read/write DSI buffer
31  * @channel: virtual channel id
32  * @type: payload data type
33  * @flags: flags controlling this 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
44         size_t tx_len;
45         const void *tx_buf;
46
47         size_t rx_len;
48         void *rx_buf;
49 };
50
51 bool mipi_dsi_packet_format_is_short(u8 type);
52 bool mipi_dsi_packet_format_is_long(u8 type);
53
54 /**
55  * struct mipi_dsi_packet - represents a MIPI DSI packet in protocol format
56  * @size: size (in bytes) of the packet
57  * @header: the four bytes that make up the header (Data ID, Word Count or
58  *     Packet Data, and ECC)
59  * @payload_length: number of bytes in the payload
60  * @payload: a pointer to a buffer containing the payload, if any
61  */
62 struct mipi_dsi_packet {
63         size_t size;
64         u8 header[4];
65         size_t payload_length;
66         const u8 *payload;
67 };
68
69 int mipi_dsi_create_packet(struct mipi_dsi_packet *packet,
70                            const struct mipi_dsi_msg *msg);
71
72 /**
73  * struct mipi_dsi_host_ops - DSI bus operations
74  * @attach: attach DSI device to DSI host
75  * @detach: detach DSI device from DSI host
76  * @transfer: transmit a DSI packet
77  *
78  * DSI packets transmitted by .transfer() are passed in as mipi_dsi_msg
79  * structures. This structure contains information about the type of packet
80  * being transmitted as well as the transmit and receive buffers. When an
81  * error is encountered during transmission, this function will return a
82  * negative error code. On success it shall return the number of bytes
83  * transmitted for write packets or the number of bytes received for read
84  * packets.
85  *
86  * Note that typically DSI packet transmission is atomic, so the .transfer()
87  * function will seldomly return anything other than the number of bytes
88  * contained in the transmit buffer on success.
89  */
90 struct mipi_dsi_host_ops {
91         int (*attach)(struct mipi_dsi_host *host,
92                       struct mipi_dsi_device *dsi);
93         int (*detach)(struct mipi_dsi_host *host,
94                       struct mipi_dsi_device *dsi);
95         ssize_t (*transfer)(struct mipi_dsi_host *host,
96                             const struct mipi_dsi_msg *msg);
97 };
98
99 /**
100  * struct mipi_dsi_phy_timing - DSI host phy timings
101  * @data_hs2lp: High Speed to Low Speed Data Transition Time
102  * @data_lp2hs: Low Speed to High Speed Data Transition Time
103  * @clk_hs2lp: High Speed to Low Speed Clock Transition Time
104  * @clk_lp2hs: Low Speed to High Speed Clock Transition Time
105  */
106 struct mipi_dsi_phy_timing {
107         u16 data_hs2lp;
108         u16 data_lp2hs;
109         u16 clk_hs2lp;
110         u16 clk_lp2hs;
111 };
112
113 /**
114  * struct mipi_dsi_phy_ops - DSI host physical operations
115  * @init: initialized host physical part
116  * @get_lane_mbps: get lane bitrate per lane (mbps)
117  * @post_set_mode: operation that should after set mode
118  */
119 struct mipi_dsi_phy_ops {
120         int (*init)(void *priv_data);
121         int (*get_lane_mbps)(void *priv_data, struct display_timing *timings,
122                              u32 lanes, u32 format, unsigned int *lane_mbps);
123         void (*post_set_mode)(void *priv_data,  unsigned long mode_flags);
124         int (*get_timing)(void *priv_data, unsigned int lane_mbps,
125                           struct mipi_dsi_phy_timing *timing);
126 };
127
128 /**
129  * struct mipi_dsi_host - DSI host device
130  * @dev: driver model device node for this DSI host
131  * @ops: DSI host operations
132  * @list: list management
133  */
134 struct mipi_dsi_host {
135         struct device *dev;
136         const struct mipi_dsi_host_ops *ops;
137         struct list_head list;
138 };
139
140 /* DSI mode flags */
141
142 /* video mode */
143 #define MIPI_DSI_MODE_VIDEO             BIT(0)
144 /* video burst mode */
145 #define MIPI_DSI_MODE_VIDEO_BURST       BIT(1)
146 /* video pulse mode */
147 #define MIPI_DSI_MODE_VIDEO_SYNC_PULSE  BIT(2)
148 /* enable auto vertical count mode */
149 #define MIPI_DSI_MODE_VIDEO_AUTO_VERT   BIT(3)
150 /* enable hsync-end packets in vsync-pulse and v-porch area */
151 #define MIPI_DSI_MODE_VIDEO_HSE         BIT(4)
152 /* disable hfront-porch area */
153 #define MIPI_DSI_MODE_VIDEO_HFP         BIT(5)
154 /* disable hback-porch area */
155 #define MIPI_DSI_MODE_VIDEO_HBP         BIT(6)
156 /* disable hsync-active area */
157 #define MIPI_DSI_MODE_VIDEO_HSA         BIT(7)
158 /* flush display FIFO on vsync pulse */
159 #define MIPI_DSI_MODE_VSYNC_FLUSH       BIT(8)
160 /* disable EoT packets in HS mode */
161 #define MIPI_DSI_MODE_EOT_PACKET        BIT(9)
162 /* device supports non-continuous clock behavior (DSI spec 5.6.1) */
163 #define MIPI_DSI_CLOCK_NON_CONTINUOUS   BIT(10)
164 /* transmit data in low power */
165 #define MIPI_DSI_MODE_LPM               BIT(11)
166
167 enum mipi_dsi_pixel_format {
168         MIPI_DSI_FMT_RGB888,
169         MIPI_DSI_FMT_RGB666,
170         MIPI_DSI_FMT_RGB666_PACKED,
171         MIPI_DSI_FMT_RGB565,
172 };
173
174 #define DSI_DEV_NAME_SIZE               20
175
176 /**
177  * struct mipi_dsi_device_info - template for creating a mipi_dsi_device
178  * @type: DSI peripheral chip type
179  * @channel: DSI virtual channel assigned to peripheral
180  * @node: pointer to OF device node or NULL
181  *
182  * This is populated and passed to mipi_dsi_device_new to create a new
183  * DSI device
184  */
185 struct mipi_dsi_device_info {
186         char type[DSI_DEV_NAME_SIZE];
187         u32 channel;
188         struct device_node *node;
189 };
190
191 /**
192  * struct mipi_dsi_device - DSI peripheral device
193  * @host: DSI host for this peripheral
194  * @dev: driver model device node for this peripheral
195  * @name: DSI peripheral chip type
196  * @channel: virtual channel assigned to the peripheral
197  * @format: pixel format for video mode
198  * @lanes: number of active data lanes
199  * @mode_flags: DSI operation mode related flags
200  */
201 struct mipi_dsi_device {
202         struct mipi_dsi_host *host;
203         struct udevice *dev;
204
205         char name[DSI_DEV_NAME_SIZE];
206         unsigned int channel;
207         unsigned int lanes;
208         enum mipi_dsi_pixel_format format;
209         unsigned long mode_flags;
210 };
211
212 /**
213  * mipi_dsi_pixel_format_to_bpp - obtain the number of bits per pixel for any
214  *                                given pixel format defined by the MIPI DSI
215  *                                specification
216  * @fmt: MIPI DSI pixel format
217  *
218  * Returns: The number of bits per pixel of the given pixel format.
219  */
220 static inline int mipi_dsi_pixel_format_to_bpp(enum mipi_dsi_pixel_format fmt)
221 {
222         switch (fmt) {
223         case MIPI_DSI_FMT_RGB888:
224         case MIPI_DSI_FMT_RGB666:
225                 return 24;
226
227         case MIPI_DSI_FMT_RGB666_PACKED:
228                 return 18;
229
230         case MIPI_DSI_FMT_RGB565:
231                 return 16;
232         }
233
234         return -EINVAL;
235 }
236
237 /**
238  * struct mipi_dsi_panel_plat - DSI panel platform data
239  * @device: DSI peripheral device
240  * @lanes: number of active data lanes
241  * @format: pixel format for video mode
242  * @mode_flags: DSI operation mode related flags
243  */
244 struct mipi_dsi_panel_plat {
245         struct mipi_dsi_device *device;
246         unsigned int lanes;
247         enum mipi_dsi_pixel_format format;
248         unsigned long mode_flags;
249 };
250
251 /**
252  * mipi_dsi_attach - attach a DSI device to its DSI host
253  * @dsi: DSI peripheral
254  */
255 int mipi_dsi_attach(struct mipi_dsi_device *dsi);
256
257 /**
258  * mipi_dsi_detach - detach a DSI device from its DSI host
259  * @dsi: DSI peripheral
260  */
261 int mipi_dsi_detach(struct mipi_dsi_device *dsi);
262 int mipi_dsi_shutdown_peripheral(struct mipi_dsi_device *dsi);
263 int mipi_dsi_turn_on_peripheral(struct mipi_dsi_device *dsi);
264 int mipi_dsi_set_maximum_return_packet_size(struct mipi_dsi_device *dsi,
265                                             u16 value);
266
267 ssize_t mipi_dsi_generic_write(struct mipi_dsi_device *dsi, const void *payload,
268                                size_t size);
269 ssize_t mipi_dsi_generic_read(struct mipi_dsi_device *dsi, const void *params,
270                               size_t num_params, void *data, size_t size);
271
272 /**
273  * enum mipi_dsi_dcs_tear_mode - Tearing Effect Output Line mode
274  * @MIPI_DSI_DCS_TEAR_MODE_VBLANK: the TE output line consists of V-Blanking
275  *    information only
276  * @MIPI_DSI_DCS_TEAR_MODE_VHBLANK : the TE output line consists of both
277  *    V-Blanking and H-Blanking information
278  */
279 enum mipi_dsi_dcs_tear_mode {
280         MIPI_DSI_DCS_TEAR_MODE_VBLANK,
281         MIPI_DSI_DCS_TEAR_MODE_VHBLANK,
282 };
283
284 #define MIPI_DSI_DCS_POWER_MODE_DISPLAY BIT(2)
285 #define MIPI_DSI_DCS_POWER_MODE_NORMAL  BIT(3)
286 #define MIPI_DSI_DCS_POWER_MODE_SLEEP   BIT(4)
287 #define MIPI_DSI_DCS_POWER_MODE_PARTIAL BIT(5)
288 #define MIPI_DSI_DCS_POWER_MODE_IDLE    BIT(6)
289
290 /**
291  * mipi_dsi_dcs_write_buffer() - transmit a DCS command with payload
292  * @dsi: DSI peripheral device
293  * @data: buffer containing data to be transmitted
294  * @len: size of transmission buffer
295  *
296  * This function will automatically choose the right data type depending on
297  * the command payload length.
298  *
299  * Return: The number of bytes successfully transmitted or a negative error
300  * code on failure.
301  */
302 ssize_t mipi_dsi_dcs_write_buffer(struct mipi_dsi_device *dsi,
303                                   const void *data, size_t len);
304
305 /**
306  * mipi_dsi_dcs_write() - send DCS write command
307  * @dsi: DSI peripheral device
308  * @cmd: DCS command
309  * @data: buffer containing the command payload
310  * @len: command payload length
311  *
312  * This function will automatically choose the right data type depending on
313  * the command payload length.
314
315  * code on failure.
316  */
317 ssize_t mipi_dsi_dcs_write(struct mipi_dsi_device *dsi, u8 cmd,
318                            const void *data, size_t len);
319
320 /**
321  * mipi_dsi_dcs_read() - send DCS read request command
322  * @dsi: DSI peripheral device
323  * @cmd: DCS command
324  * @data: buffer in which to receive data
325  * @len: size of receive buffer
326  *
327  * Return: The number of bytes read or a negative error code on failure.
328  */
329 ssize_t mipi_dsi_dcs_read(struct mipi_dsi_device *dsi, u8 cmd, void *data,
330                           size_t len);
331
332 /**
333  * mipi_dsi_dcs_nop() - send DCS nop packet
334  * @dsi: DSI peripheral device
335  *
336  * Return: 0 on success or a negative error code on failure.
337  */
338 int mipi_dsi_dcs_nop(struct mipi_dsi_device *dsi);
339
340 /**
341  * mipi_dsi_dcs_soft_reset() - perform a software reset of the display module
342  * @dsi: DSI peripheral device
343  *
344  * Return: 0 on success or a negative error code on failure.
345  */
346 int mipi_dsi_dcs_soft_reset(struct mipi_dsi_device *dsi);
347
348 /**
349  * mipi_dsi_dcs_get_power_mode() - query the display module's current power
350  *    mode
351  * @dsi: DSI peripheral device
352  * @mode: return location for the current power mode
353  *
354  * Return: 0 on success or a negative error code on failure.
355  */
356 int mipi_dsi_dcs_get_power_mode(struct mipi_dsi_device *dsi, u8 *mode);
357
358 /**
359  * mipi_dsi_dcs_get_pixel_format() - gets the pixel format for the RGB image
360  *    data used by the interface
361  * @dsi: DSI peripheral device
362  * @format: return location for the pixel format
363  *
364  * Return: 0 on success or a negative error code on failure.
365  */
366 int mipi_dsi_dcs_get_pixel_format(struct mipi_dsi_device *dsi, u8 *format);
367
368 /**
369  * mipi_dsi_dcs_enter_sleep_mode() - disable all unnecessary blocks inside the
370  *    display module except interface communication
371  * @dsi: DSI peripheral device
372  *
373  * Return: 0 on success or a negative error code on failure.
374  */
375 int mipi_dsi_dcs_enter_sleep_mode(struct mipi_dsi_device *dsi);
376
377 /**
378  * mipi_dsi_dcs_exit_sleep_mode() - enable all blocks inside the display
379  *    module
380  * @dsi: DSI peripheral device
381  *
382  * Return: 0 on success or a negative error code on failure.
383  */
384 int mipi_dsi_dcs_exit_sleep_mode(struct mipi_dsi_device *dsi);
385
386 /**
387  * mipi_dsi_dcs_set_display_off() - stop displaying the image data on the
388  *    display device
389  * @dsi: DSI peripheral device
390  *
391  * Return: 0 on success or a negative error code on failure.
392  */
393 int mipi_dsi_dcs_set_display_off(struct mipi_dsi_device *dsi);
394
395 /**
396  * mipi_dsi_dcs_set_display_on() - start displaying the image data on the
397  *    display device
398  * @dsi: DSI peripheral device
399  *
400  * Return: 0 on success or a negative error code on failure
401  */
402 int mipi_dsi_dcs_set_display_on(struct mipi_dsi_device *dsi);
403
404 /**
405  * mipi_dsi_dcs_set_column_address() - define the column extent of the frame
406  *    memory accessed by the host processor
407  * @dsi: DSI peripheral device
408  * @start: first column of frame memory
409  * @end: last column of frame memory
410  *
411  * Return: 0 on success or a negative error code on failure.
412  */
413 int mipi_dsi_dcs_set_column_address(struct mipi_dsi_device *dsi, u16 start,
414                                     u16 end);
415 /**
416  * mipi_dsi_dcs_set_page_address() - define the page extent of the frame
417  *    memory accessed by the host processor
418  * @dsi: DSI peripheral device
419  * @start: first page of frame memory
420  * @end: last page of frame memory
421  *
422  * Return: 0 on success or a negative error code on failure.
423  */
424 int mipi_dsi_dcs_set_page_address(struct mipi_dsi_device *dsi, u16 start,
425                                   u16 end);
426
427 /**
428  * mipi_dsi_dcs_set_tear_off() - turn off the display module's Tearing Effect
429  *    output signal on the TE signal line
430  * @dsi: DSI peripheral device
431  *
432  * Return: 0 on success or a negative error code on failure
433  */
434 int mipi_dsi_dcs_set_tear_off(struct mipi_dsi_device *dsi);
435
436 /**
437  * mipi_dsi_dcs_set_tear_on() - turn on the display module's Tearing Effect
438  *    output signal on the TE signal line.
439  * @dsi: DSI peripheral device
440  * @mode: the Tearing Effect Output Line mode
441  *
442  * Return: 0 on success or a negative error code on failure
443  */
444 int mipi_dsi_dcs_set_tear_on(struct mipi_dsi_device *dsi,
445                              enum mipi_dsi_dcs_tear_mode mode);
446
447 /**
448  * mipi_dsi_dcs_set_pixel_format() - sets the pixel format for the RGB image
449  *    data used by the interface
450  * @dsi: DSI peripheral device
451  * @format: pixel format
452  *
453  * Return: 0 on success or a negative error code on failure.
454  */
455 int mipi_dsi_dcs_set_pixel_format(struct mipi_dsi_device *dsi, u8 format);
456
457 /**
458  * mipi_dsi_dcs_set_tear_scanline() - set the scanline to use as trigger for
459  *    the Tearing Effect output signal of the display module
460  * @dsi: DSI peripheral device
461  * @scanline: scanline to use as trigger
462  *
463  * Return: 0 on success or a negative error code on failure
464  */
465 int mipi_dsi_dcs_set_tear_scanline(struct mipi_dsi_device *dsi, u16 scanline);
466
467 /**
468  * mipi_dsi_dcs_set_display_brightness() - sets the brightness value of the
469  *    display
470  * @dsi: DSI peripheral device
471  * @brightness: brightness value
472  *
473  * Return: 0 on success or a negative error code on failure.
474  */
475 int mipi_dsi_dcs_set_display_brightness(struct mipi_dsi_device *dsi,
476                                         u16 brightness);
477
478 /**
479  * mipi_dsi_dcs_get_display_brightness() - gets the current brightness value
480  *    of the display
481  * @dsi: DSI peripheral device
482  * @brightness: brightness value
483  *
484  * Return: 0 on success or a negative error code on failure.
485  */
486 int mipi_dsi_dcs_get_display_brightness(struct mipi_dsi_device *dsi,
487                                         u16 *brightness);
488
489 #endif /* MIPI_DSI_H */