1 /*
2  * Copyright 2017-2020 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #include "fsl_mipi_dsi_cmd.h"
10 
11 /*******************************************************************************
12  * Definitions
13  ******************************************************************************/
14 
15 /*******************************************************************************
16  * Variables
17  ******************************************************************************/
18 
19 /*******************************************************************************
20  * Code
21  ******************************************************************************/
MIPI_DSI_DCS_SoftReset(mipi_dsi_device_t * device)22 status_t MIPI_DSI_DCS_SoftReset(mipi_dsi_device_t *device)
23 {
24     dsi_transfer_t dsiXfer = {0};
25     uint8_t txData         = (uint8_t)kMIPI_DCS_SoftReset;
26 
27     dsiXfer.virtualChannel = device->virtualChannel;
28     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
29     dsiXfer.txDataSize     = 1;
30     dsiXfer.txData         = &txData;
31 
32     return device->xferFunc(&dsiXfer);
33 }
34 
MIPI_DSI_DCS_SetDisplayOn(mipi_dsi_device_t * device,bool on)35 status_t MIPI_DSI_DCS_SetDisplayOn(mipi_dsi_device_t *device, bool on)
36 {
37     dsi_transfer_t dsiXfer = {0};
38     uint8_t txData;
39 
40     dsiXfer.virtualChannel = device->virtualChannel;
41     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
42     dsiXfer.txDataSize     = 1;
43     dsiXfer.txData         = &txData;
44 
45     if (on)
46     {
47         txData = (uint8_t)kMIPI_DCS_SetDisplayOn;
48     }
49     else
50     {
51         txData = (uint8_t)kMIPI_DCS_SetDisplayOff;
52     }
53 
54     return device->xferFunc(&dsiXfer);
55 }
56 
MIPI_DSI_DCS_SetPixelFormat(mipi_dsi_device_t * device,mipi_dsc_pixel_format_t dbiFormat,mipi_dsc_pixel_format_t dpiFormat)57 status_t MIPI_DSI_DCS_SetPixelFormat(mipi_dsi_device_t *device,
58                                      mipi_dsc_pixel_format_t dbiFormat,
59                                      mipi_dsc_pixel_format_t dpiFormat)
60 {
61     dsi_transfer_t dsiXfer = {0};
62     uint8_t txData[2];
63 
64     dsiXfer.virtualChannel = device->virtualChannel;
65     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrOneParam;
66     dsiXfer.txDataSize     = 2;
67     dsiXfer.txData         = txData;
68 
69     txData[0] = (uint8_t)kMIPI_DCS_SetPixelFormat;
70     txData[1] = ((uint8_t)dbiFormat << 0U) | ((uint8_t)dpiFormat << 4U);
71 
72     return device->xferFunc(&dsiXfer);
73 }
74 
MIPI_DSI_DCS_EnterSleepMode(mipi_dsi_device_t * device,bool enter)75 status_t MIPI_DSI_DCS_EnterSleepMode(mipi_dsi_device_t *device, bool enter)
76 {
77     dsi_transfer_t dsiXfer = {0};
78     uint8_t txData;
79 
80     dsiXfer.virtualChannel = device->virtualChannel;
81     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
82     dsiXfer.txDataSize     = 1;
83     dsiXfer.txData         = &txData;
84 
85     if (enter)
86     {
87         txData = (uint8_t)kMIPI_DCS_EnterSleepMode;
88     }
89     else
90     {
91         txData = (uint8_t)kMIPI_DCS_ExitSleepMode;
92     }
93 
94     return device->xferFunc(&dsiXfer);
95 }
96 
MIPI_DSI_DCS_EnterPartialMode(mipi_dsi_device_t * device,bool enter)97 status_t MIPI_DSI_DCS_EnterPartialMode(mipi_dsi_device_t *device, bool enter)
98 {
99     dsi_transfer_t dsiXfer = {0};
100     uint8_t txData;
101 
102     dsiXfer.virtualChannel = device->virtualChannel;
103     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
104     dsiXfer.txDataSize     = 1;
105     dsiXfer.txData         = &txData;
106 
107     if (enter)
108     {
109         txData = (uint8_t)kMIPI_DCS_EnterPartialMode;
110     }
111     else
112     {
113         txData = (uint8_t)kMIPI_DCS_EnterNormalMode;
114     }
115 
116     return device->xferFunc(&dsiXfer);
117 }
118 
MIPI_DSI_DCS_EnterInvertMode(mipi_dsi_device_t * device,bool enter)119 status_t MIPI_DSI_DCS_EnterInvertMode(mipi_dsi_device_t *device, bool enter)
120 {
121     dsi_transfer_t dsiXfer = {0};
122     uint8_t txData;
123 
124     dsiXfer.virtualChannel = device->virtualChannel;
125     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
126     dsiXfer.txDataSize     = 1;
127     dsiXfer.txData         = &txData;
128 
129     if (enter)
130     {
131         txData = (uint8_t)kMIPI_DCS_EnterInvertMode;
132     }
133     else
134     {
135         txData = (uint8_t)kMIPI_DCS_ExitInvertMode;
136     }
137 
138     return device->xferFunc(&dsiXfer);
139 }
140 
MIPI_DSI_DCS_EnterIdleMode(mipi_dsi_device_t * device,bool enter)141 status_t MIPI_DSI_DCS_EnterIdleMode(mipi_dsi_device_t *device, bool enter)
142 {
143     dsi_transfer_t dsiXfer = {0};
144     uint8_t txData;
145 
146     dsiXfer.virtualChannel = device->virtualChannel;
147     dsiXfer.txDataType     = kDSI_TxDataDcsShortWrNoParam;
148     dsiXfer.txDataSize     = 1;
149     dsiXfer.txData         = &txData;
150 
151     if (enter)
152     {
153         txData = (uint8_t)kMIPI_DCS_EnterIdleMode;
154     }
155     else
156     {
157         txData = (uint8_t)kMIPI_DCS_ExitIdleMode;
158     }
159 
160     return device->xferFunc(&dsiXfer);
161 }
162 
MIPI_DSI_DCS_Write(mipi_dsi_device_t * device,const uint8_t * txData,int32_t txDataSize)163 status_t MIPI_DSI_DCS_Write(mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize)
164 {
165     dsi_transfer_t dsiXfer = {0};
166 
167     dsiXfer.virtualChannel = device->virtualChannel;
168     dsiXfer.txDataSize     = (uint16_t)txDataSize;
169     dsiXfer.txData         = txData;
170 
171     if (0 == txDataSize)
172     {
173         /* For DSC command, the data size should not be 0. */
174         return kStatus_InvalidArgument;
175     }
176     else if (1 == txDataSize)
177     {
178         dsiXfer.txDataType = kDSI_TxDataDcsShortWrNoParam;
179     }
180     else if (2 == txDataSize)
181     {
182         dsiXfer.txDataType = kDSI_TxDataDcsShortWrOneParam;
183     }
184     else
185     {
186         dsiXfer.txDataType = kDSI_TxDataDcsLongWr;
187     }
188 
189     return device->xferFunc(&dsiXfer);
190 }
191 
MIPI_DSI_GenericWrite(mipi_dsi_device_t * device,const uint8_t * txData,int32_t txDataSize)192 status_t MIPI_DSI_GenericWrite(mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize)
193 {
194     dsi_transfer_t dsiXfer = {0};
195 
196     dsiXfer.virtualChannel = device->virtualChannel;
197     dsiXfer.txDataSize     = (uint16_t)txDataSize;
198     dsiXfer.txData         = txData;
199 
200     if (0 == txDataSize)
201     {
202         dsiXfer.txDataType = kDSI_TxDataGenShortWrNoParam;
203     }
204     else if (1 == txDataSize)
205     {
206         dsiXfer.txDataType = kDSI_TxDataGenShortWrOneParam;
207     }
208     else if (2 == txDataSize)
209     {
210         dsiXfer.txDataType = kDSI_TxDataGenShortWrTwoParam;
211     }
212     else
213     {
214         dsiXfer.txDataType = kDSI_TxDataGenLongWr;
215     }
216 
217     return device->xferFunc(&dsiXfer);
218 }
219 
MIPI_DSI_DCS_SetMaxReturnPktSize(mipi_dsi_device_t * device,uint16_t sizeBytes)220 status_t MIPI_DSI_DCS_SetMaxReturnPktSize(mipi_dsi_device_t *device, uint16_t sizeBytes)
221 {
222     dsi_transfer_t dsiXfer = {0};
223 
224     dsiXfer.virtualChannel = device->virtualChannel;
225     dsiXfer.txDataType     = kDSI_TxDataSetMaxReturnPktSize;
226     dsiXfer.txDataSize     = 2;
227     dsiXfer.txData         = (uint8_t *)&sizeBytes;
228 
229     return device->xferFunc(&dsiXfer);
230 }
231 
MIPI_DSI_GenericRead(mipi_dsi_device_t * device,const uint8_t * txData,int32_t txDataSize,uint8_t * rxData,int32_t * rxDataSize)232 status_t MIPI_DSI_GenericRead(
233     mipi_dsi_device_t *device, const uint8_t *txData, int32_t txDataSize, uint8_t *rxData, int32_t *rxDataSize)
234 {
235     status_t status;
236     dsi_transfer_t dsiXfer = {0};
237 
238     dsiXfer.virtualChannel = device->virtualChannel;
239     dsiXfer.txDataSize     = (uint16_t)txDataSize;
240     dsiXfer.txData         = txData;
241     dsiXfer.rxDataSize     = (uint16_t)*rxDataSize;
242     dsiXfer.rxData         = rxData;
243 
244     *rxDataSize = 0;
245 
246     if (0 == txDataSize)
247     {
248         dsiXfer.txDataType = kDSI_TxDataGenShortRdNoParam;
249     }
250     else if (1 == txDataSize)
251     {
252         dsiXfer.txDataType = kDSI_TxDataGenShortRdOneParam;
253     }
254     else if (2 == txDataSize)
255     {
256         dsiXfer.txDataType = kDSI_TxDataGenShortRdTwoParam;
257     }
258     else
259     {
260         return kStatus_InvalidArgument;
261     }
262 
263     status = device->xferFunc(&dsiXfer);
264 
265     /* Return actual received size. */
266     *rxDataSize = (int32_t)dsiXfer.rxDataSize;
267 
268     return status;
269 }
270 
MIPI_DSI_ReadCMD(mipi_dsi_device_t * device,enum _mipi_dcs dcsCmd,uint8_t * rxData,int32_t * rxDataSize)271 status_t MIPI_DSI_ReadCMD(mipi_dsi_device_t *device, enum _mipi_dcs dcsCmd, uint8_t *rxData, int32_t *rxDataSize)
272 {
273     uint8_t txData[2];
274     status_t status = kStatus_Fail;
275 
276     txData[0] = (uint8_t)dcsCmd;
277     if (kStatus_Success == MIPI_DSI_DCS_SetMaxReturnPktSize(device, (uint16_t)*rxDataSize))
278     {
279         status = MIPI_DSI_GenericRead(device, txData, 1, rxData, rxDataSize);
280     }
281 
282     return status;
283 }
284 
MIPI_DSI_SelectArea(mipi_dsi_device_t * device,uint16_t startX,uint16_t startY,uint16_t endX,uint16_t endY)285 status_t MIPI_DSI_SelectArea(mipi_dsi_device_t *device, uint16_t startX, uint16_t startY, uint16_t endX, uint16_t endY)
286 {
287     status_t status;
288     dsi_transfer_t dsiXfer = {0};
289     uint8_t txData[4];
290 
291     dsiXfer.virtualChannel = device->virtualChannel;
292     dsiXfer.txDataType     = kDSI_TxDataDcsLongWr;
293     dsiXfer.txDataSize     = 4;
294     dsiXfer.txData         = txData;
295     dsiXfer.sendDscCmd     = true;
296     dsiXfer.dscCmd         = (uint8_t)kMIPI_DCS_SetColumnAddress;
297 
298     txData[0] = (uint8_t)((startX >> 8U) & 0xFFU);
299     txData[1] = (uint8_t)(startX & 0xFFU);
300     txData[2] = (uint8_t)((endX >> 8U) & 0xFFU);
301     txData[3] = (uint8_t)(endX & 0xFFU);
302 
303     status = device->xferFunc(&dsiXfer);
304 
305     if (kStatus_Success != status)
306     {
307         return status;
308     }
309 
310     dsiXfer.dscCmd = (uint8_t)kMIPI_DCS_SetPageAddress;
311     txData[0]      = (uint8_t)((startY >> 8U) & 0xFFU);
312     txData[1]      = (uint8_t)(startY & 0xFFU);
313     txData[2]      = (uint8_t)((endY >> 8U) & 0xFFU);
314     txData[3]      = (uint8_t)(endY & 0xFFU);
315 
316     return device->xferFunc(&dsiXfer);
317 }
318 
MIPI_DSI_WriteMemory(mipi_dsi_device_t * device,const uint8_t * data,uint32_t length)319 status_t MIPI_DSI_WriteMemory(mipi_dsi_device_t *device, const uint8_t *data, uint32_t length)
320 {
321     return device->memWriteFunc(device->virtualChannel, data, length);
322 }
323 
MIPI_DSI_WriteMemory2D(mipi_dsi_device_t * device,const uint8_t * data,uint32_t minorLoop,uint32_t minorLoopOffset,uint32_t majorLoop)324 status_t MIPI_DSI_WriteMemory2D(
325     mipi_dsi_device_t *device, const uint8_t *data, uint32_t minorLoop, uint32_t minorLoopOffset, uint32_t majorLoop)
326 {
327     if (device->memWriteFunc2D != NULL)
328     {
329         return device->memWriteFunc2D(device->virtualChannel, data, minorLoop, minorLoopOffset, majorLoop);
330     }
331     else
332     {
333         return kStatus_Fail;
334     }
335 }
336 
MIPI_DSI_SetMemoryDoneCallback(mipi_dsi_device_t * device,mipi_dsi_mem_done_callback_t callback,void * userData)337 void MIPI_DSI_SetMemoryDoneCallback(mipi_dsi_device_t *device, mipi_dsi_mem_done_callback_t callback, void *userData)
338 {
339     device->callback = callback;
340     device->userData = userData;
341 }
342 
MIPI_DSI_MemoryDoneDriverCallback(status_t status,void * userData)343 void MIPI_DSI_MemoryDoneDriverCallback(status_t status, void *userData)
344 {
345     mipi_dsi_device_t *device = (mipi_dsi_device_t *)userData;
346 
347     if (NULL != device->callback)
348     {
349         device->callback(status, device->userData);
350     }
351 }
352