1 /*
2  * Copyright 2017-2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_MIPI_DSI_CMD_H_
10 #define _FSL_MIPI_DSI_CMD_H_
11 
12 #include "fsl_common.h"
13 #include "fsl_mipi_dsi.h"
14 
15 /*
16  * Change log:
17  *
18  *   1.0.2
19  *     - Fix MISRA-C 2012 issues.
20  *
21  *   1.0.1
22  *     - Add more functions for panel works in command mode.
23  *
24  *   1.0.0
25  *     - Initial version
26  */
27 
28 /*******************************************************************************
29  * Definitions
30  ******************************************************************************/
31 
32 enum _mipi_dcs
33 {
34     kMIPI_DCS_Nop                  = 0x00,
35     kMIPI_DCS_SoftReset            = 0x01,
36     kMIPI_DCS_GetRedChannel        = 0x06,
37     kMIPI_DCS_GetGreenChannel      = 0x07,
38     kMIPI_DCS_GetBlueChannel       = 0x08,
39     kMIPI_DCS_GetPowerMode         = 0x0A,
40     kMIPI_DCS_GetAddressMode       = 0x0B,
41     kMIPI_DCS_GetPixelFormat       = 0x0C,
42     kMIPI_DCS_GetDisplayMode       = 0x0D,
43     kMIPI_DCS_GetSignalMode        = 0x0E,
44     kMIPI_DCS_GetDiagnosticResult  = 0x0F,
45     kMIPI_DCS_EnterSleepMode       = 0x10,
46     kMIPI_DCS_ExitSleepMode        = 0x11,
47     kMIPI_DCS_EnterPartialMode     = 0x12,
48     kMIPI_DCS_EnterNormalMode      = 0x13,
49     kMIPI_DCS_ExitInvertMode       = 0x20,
50     kMIPI_DCS_EnterInvertMode      = 0x21,
51     kMIPI_DCS_SetGammaCurve        = 0x26,
52     kMIPI_DCS_SetDisplayOff        = 0x28,
53     kMIPI_DCS_SetDisplayOn         = 0x29,
54     kMIPI_DCS_SetColumnAddress     = 0x2a,
55     kMIPI_DCS_SetPageAddress       = 0x2b,
56     kMIPI_DCS_WriteMemoryStart     = 0x2C,
57     kMIPI_DCS_WriteLUT             = 0x2D,
58     kMIPI_DCS_ReadMemoryStart      = 0x2E,
59     kMIPI_DCS_SetPartialRows       = 0x30,
60     kMIPI_DCS_SetPartialColumns    = 0x31,
61     kMIPI_DCS_SetScrollArea        = 0x33,
62     kMIPI_DCS_SetTearOff           = 0x34,
63     kMIPI_DCS_SetTearOn            = 0x35,
64     kMIPI_DCS_SetAddressMode       = 0x36,
65     kMIPI_DCS_SetScrollStart       = 0x37,
66     kMIPI_DCS_ExitIdleMode         = 0x38,
67     kMIPI_DCS_EnterIdleMode        = 0x39,
68     kMIPI_DCS_SetPixelFormat       = 0x3A,
69     kMIPI_DCS_WriteMemoryContinue  = 0x3C,
70     kMIPI_DCS_Set3DControl         = 0x3D,
71     kMIPI_DCS_ReadMemoryContinue   = 0x3E,
72     kMIPI_DCS_Get3DControl         = 0x3F,
73     kMIPI_DCS_SetVsyncTiming       = 0x40,
74     kMIPI_DCS_SetTearScanline      = 0x44,
75     kMIPI_DCS_GetScanline          = 0x45,
76     kMIPI_DCS_SetDisplayBrightness = 0x51,
77     kMIPI_DCS_GetDisplayBrightness = 0x52,
78     kMIPI_DCS_WriteControlDisplay  = 0x53,
79     kMIPI_DCS_GetControlDisplay    = 0x54,
80     kMIPI_DCS_WritePowerSave       = 0x55,
81     kMIPI_DCS_GetPowerSave         = 0x56,
82     kMIPI_DCS_SetCABCMinBrightness = 0x5E,
83     kMIPI_DCS_GetCABCMinBrightness = 0x5F,
84     kMIPI_DCS_ReadDDBStart         = 0xA1,
85     kMIPI_DCS_ReadDDBContinue      = 0xA8,
86 };
87 
88 /*!
89  * @brief Pixel format used by DSC command.
90  */
91 typedef enum _mipi_dsc_pixel_format
92 {
93     kMIPI_DCS_Pixel3Bits  = 1U, /*!< 3-bit per pixel. */
94     kMIPI_DCS_Pixel8Bits  = 2U, /*!< 8-bit per pixel. */
95     kMIPI_DCS_Pixel12Bits = 3U, /*!< 12-bit per pixel. */
96     kMIPI_DCS_Pixel16Bits = 5U, /*!< 16-bit per pixel. */
97     kMIPI_DCS_Pixel18Bits = 6U, /*!< 18-bit per pixel. */
98     kMIPI_DCS_Pixel24Bits = 7U, /*!< 24-bit per pixel. */
99 } mipi_dsc_pixel_format_t;
100 
101 /*!
102  * @brief Callback function when the write memory finished.
103  *
104  * If transfer done successfully, the @p status is kStatus_Success.
105  */
106 typedef void (*mipi_dsi_mem_done_callback_t)(status_t status, void *userData);
107 
108 /*! @brief MIPI DSI transfer function. */
109 typedef status_t (*mipi_dsi_transfer_func_t)(dsi_transfer_t *xfer);
110 
111 /*! @brief MIPI DSI memory write function. */
112 typedef status_t (*mipi_dsi_mem_write_func_t)(uint8_t virtualChannel, const uint8_t *data, uint32_t length);
113 
114 /*! @brief MIPI DSI memory write function using 2-dimensional way. */
115 typedef status_t (*mipi_dsi_mem_write_func_2D_t)(
116     uint8_t virtualChannel, const uint8_t *data, uint32_t minorLoop, uint32_t minorLoopOffset, uint32_t majorLoop);
117 
118 /*! @brief MIPI DSI device. */
119 typedef struct _mipi_dsi_device
120 {
121     uint8_t virtualChannel;
122     mipi_dsi_transfer_func_t xferFunc;
123     mipi_dsi_mem_write_func_t memWriteFunc;      /*!< Function to write display memory,
124                                                    it should be non-blocking function and
125                                                    notify upper layer using callback when finished.
126                                                    Not used when panel works in video mode. */
127     mipi_dsi_mem_write_func_2D_t memWriteFunc2D; /*!< Function to write display memory using 2-dimensional way,
128                                               it should be non-blocking function and
129                                               notify upper layer using callback when finished.
130                                               Not used when panel works in video mode. */
131     mipi_dsi_mem_done_callback_t callback;       /*!< The callback function to notify upper layer
132                                                     that memory write done. Not used when panel
133                                                     works in video mode. */
134     void *userData;                              /*!< Parameter for the memory write done callback.
135                                                  not used when panel works in video mode. */
136 } mipi_dsi_device_t;
137 
138 /*******************************************************************************
139  * API
140  ******************************************************************************/
141 
142 #if defined(__cplusplus)
143 extern "C" {
144 #endif
145 
146 /*!
147  * @brief Send software reset to MIPI DSI device.
148  *
149  * @param device The MIPI DSI device.
150  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
151  */
152 status_t MIPI_DSI_DCS_SoftReset(mipi_dsi_device_t *device);
153 
154 /*!
155  * @brief Set display on or off.
156  *
157  * @param device The MIPI DSI device.
158  * @param on Set true to turn on, false to turn off.
159  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
160  */
161 status_t MIPI_DSI_DCS_SetDisplayOn(mipi_dsi_device_t *device, bool on);
162 
163 /*!
164  * @brief Enter or exit sleep mode.
165  *
166  * @param device The MIPI DSI device.
167  * @param enter Set true to enter sleep mode, false to exit.
168  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
169  */
170 status_t MIPI_DSI_DCS_EnterSleepMode(mipi_dsi_device_t *device, bool enter);
171 
172 /*!
173  * @brief Enter or exit partial mode.
174  *
175  * @param device The MIPI DSI device.
176  * @param enter Set true to enter partial mode, false to exit.
177  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
178  */
179 status_t MIPI_DSI_DCS_EnterPartialMode(mipi_dsi_device_t *device, bool enter);
180 
181 /*!
182  * @brief Enter or exit invert mode.
183  *
184  * @param device The MIPI DSI device.
185  * @param enter Set true to enter invert mode, false to exit.
186  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
187  */
188 status_t MIPI_DSI_DCS_EnterInvertMode(mipi_dsi_device_t *device, bool enter);
189 
190 /*!
191  * @brief Enter or exit idle mode.
192  *
193  * @param device The MIPI DSI device.
194  * @param enter Set true to enter idle mode, false to exit.
195  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
196  */
197 status_t MIPI_DSI_DCS_EnterIdleMode(mipi_dsi_device_t *device, bool enter);
198 
199 /*!
200  * @brief Send DCS command.
201  *
202  * @param device The MIPI DSI device.
203  * @param txData The data to send.
204  * @param txDataSize Size of the data to send (in bytes).
205  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
206  */
207 status_t MIPI_DSI_DCS_Write(mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize);
208 
209 /*!
210  * @brief Send generic data.
211  *
212  * @param device The MIPI DSI device.
213  * @param txData The data to send.
214  * @param txDataSize Size of the data to send (in bytes).
215  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
216  */
217 status_t MIPI_DSI_GenericWrite(mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize);
218 
219 /*!
220  * @brief Set the maximum return data length.
221  *
222  * @param device The MIPI DSI device.
223  * @param sizeBytes Maximum return data length.
224  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
225  */
226 status_t MIPI_DSI_DCS_SetMaxReturnPktSize(mipi_dsi_device_t *device, uint16_t sizeBytes);
227 
228 /*!
229  * @brief Generic read.
230  *
231  * @param device The MIPI DSI device.
232  * @param txData The data to send before read.
233  * @param txDataSize Size of the data to send (in bytes).
234  * @param rxData The data to read.
235  * @param rxDataSize Size of the data to read (in bytes), after this function returns,
236  * it is the actual read length.
237  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
238  */
239 status_t MIPI_DSI_GenericRead(
240     mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize, uint8_t *rxData, int32_t *rxDataSize);
241 
242 /*!
243  * @brief Read DCS command(read type command, such as: Get Display ID).
244  *
245  * @param device The MIPI DSI device.
246  * @param dcsCmd The command to send before read.
247  * @param rxData The data to read.
248  * @param rxDataSize Size of the data to read (in bytes), after this function returns,
249  * it is the actual read length.
250  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
251  */
252 status_t MIPI_DSI_ReadCMD(mipi_dsi_device_t *device, enum _mipi_dcs dcsCmd, uint8_t *rxData, int32_t *rxDataSize);
253 
254 /*!
255  * @brief Set the panel pixel format.
256  *
257  * @param device The MIPI DSI device.
258  * @param dbiFormat The DBI interface pixel format.
259  * @param dpiFormat The DPI interface pixel format.
260  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
261  */
262 status_t MIPI_DSI_DCS_SetPixelFormat(mipi_dsi_device_t *device,
263                                      mipi_dsc_pixel_format_t dbiFormat,
264                                      mipi_dsc_pixel_format_t dpiFormat);
265 
266 /*!
267  * @brief Select area to write or read pixels.
268  *
269  * @param device The MIPI DSI device.
270  * @param startX Start point X coordination.
271  * @param startY Start point Y coordination.
272  * @param endX End point X coordination.
273  * @param endY End point Y coordination.
274  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
275  */
276 status_t MIPI_DSI_SelectArea(mipi_dsi_device_t *device, uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY);
277 
278 /*!
279  * @brief Send pixel data to the display controller's frame memory.
280  *
281  * The pixels will be shown in the region selected by @ref MIPI_DSI_SelectArea.
282  * This function is non-blocking function, user should install callback function
283  * using @ref MIPI_DSI_SetMemoryDoneCallback to get informed when write finished.
284  *
285  * @param device The MIPI DSI device.
286  * @param data The pixel data to send.
287  * @param length Length of the data in byte.
288  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
289  */
290 status_t MIPI_DSI_WriteMemory(mipi_dsi_device_t *device, const uint8_t *data, uint32_t length);
291 
292 /*!
293  * @brief Send pixel data to the display controller's frame memory in 2-dinmensional way.
294  *
295  * The pixels will be shown in the region selected by @ref MIPI_DSI_SelectArea.
296  * This function is non-blocking function, user should install callback function
297  * using @ref MIPI_DSI_SetMemoryDoneCallback to get informed when write finished.
298  *
299  * @verbatim
300  * +---------------------------------------------------+
301  * |                                                   |
302  * |         data                                      |
303  * |           +-------------------+                   |
304  * |           |    minorLoop     |                   |
305  * |           |                   |                   |
306  * |           |                   | majorLoop        |
307  * |           |                   |                   |
308  * |           |                   |                   |
309  * |           +-------------------+                   |
310  * |                                                   |
311  * |     minorLoop + minorLoopOffset                |
312  * +---------------------------------------------------+
313  * @endverbatim
314  *
315  * @param device The MIPI DSI device.
316  * @param data The pixel data to send.
317  * @param minorLoop Count of the data in one line in byte.
318  * @param minorLoopOffset The offset between line stride and the count of one line in byte.
319  * @param majorLoop Count of the lines in byte.
320  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
321  */
322 status_t MIPI_DSI_WriteMemory2D(
323     mipi_dsi_device_t *device, const uint8_t *data, uint32_t minorLoop, uint32_t minorLoopOffset, uint32_t majorLoop);
324 
325 /*!
326  * @brief Install the callback called when write memory finished.
327  *
328  * Upper layer should install callback function using this function to
329  * get memory write done notification.
330  *
331  * @param device The MIPI DSI device.
332  * @param callback The callback function to inform upper layer that memory write done.
333  * @param userData Parameter used by the callback.
334  * @return Returns @ref kStatus_Success if success, otherwise returns error code.
335  */
336 void MIPI_DSI_SetMemoryDoneCallback(mipi_dsi_device_t *device, mipi_dsi_mem_done_callback_t callback, void *userData);
337 
338 /*!
339  * @brief The callback function lower layer should call when write memory finished.
340  *
341  * When implement the @ref mipi_dsi_device_t, this function should be called when
342  * the memory writing finished. The parameter @p userData should be pointer to the
343  * @ref mipi_dsi_device_t.
344  *
345  * @param status The memory writing result. @ref kStatus_Success if success.
346  * @param userData Must be pointer to the @ref mipi_dsi_device_t instance.
347  */
348 void MIPI_DSI_MemoryDoneDriverCallback(status_t status, void *userData);
349 
350 #if defined(__cplusplus)
351 }
352 #endif
353 
354 #endif /* _FSL_MIPI_DSI_CMD_H_ */
355