xref: /utopia/UTPA2-700.0.x/modules/ldm/drv/ldm/drvLDM.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 ////////////////////////////////////////////////////////////////////////////////
79 //
80 // Copyright (c) 2008-2009 MStar Semiconductor, Inc.
81 // All rights reserved.
82 //
83 // Unless otherwise stipulated in writing, any and all information contained
84 // herein regardless in any format shall remain the sole proprietary of
85 // MStar Semiconductor Inc. and be kept in strict confidence
86 // ("MStar Confidential Information") by the recipient.
87 // Any unauthorized act including without limitation unauthorized disclosure,
88 // copying, use, reproduction, sale, distribution, modification, disassembling,
89 // reverse engineering and compiling of the contents of MStar Confidential
90 // Information is unlawful and strictly prohibited. MStar hereby reserves the
91 // rights to any and all damages, losses, costs and expenses resulting therefrom.
92 //
93 ////////////////////////////////////////////////////////////////////////////////
94 
95 ///////////////////////////////////////////////////////////////////////////////////////////////////
96 ///
97 /// file    drvLDM.c
98 /// @brief  LDM Driver Interface
99 /// @author MStar Semiconductor Inc.
100 ///////////////////////////////////////////////////////////////////////////////////////////////////
101 
102 //-------------------------------------------------------------------------------------------------
103 //  Include Files
104 //-------------------------------------------------------------------------------------------------
105 #include <string.h>
106 #include "MsCommon.h"
107 #include "MsOS.h"
108 #include "drvLDM.h"
109 #include "ULog.h"
110 
111 #include <unistd.h>
112 #include <fcntl.h>
113 #include <stdlib.h>
114 #include <stdio.h>
115 #include <errno.h>
116 #include <sys/time.h>
117 
118 #include "regLDM.h"
119 #include "halLDM.h"
120 #include "drvMMIO.h"
121 #include "utopia.h"
122 
123 #if defined(MSOS_TYPE_LINUX)
124 #include <sys/ioctl.h>
125 #endif
126 
127 //-------------------------------------------------------------------------------------------------
128 //  Driver Compiler Options
129 //-------------------------------------------------------------------------------------------------
130 
131 //-------------------------------------------------------------------------------------------------
132 //  Local Defines
133 //-------------------------------------------------------------------------------------------------
134 
135 #define LDM_MODULE_NAME       "/dev/localdimming"
136 #define TAG_LDM "LDM"
137 
138 //-------------------------------------------------------------------------------------------------
139 //  Local Structurs
140 //-------------------------------------------------------------------------------------------------
141 static int LDM_fd = 0;
142 
143 //-------------------------------------------------------------------------------------------------
144 //  Global Variables
145 //-------------------------------------------------------------------------------------------------
146 
147 //-------------------------------------------------------------------------------------------------
148 //  Local Variables
149 //-------------------------------------------------------------------------------------------------
150 static EN_LDM_DEBUG_LEVEL gLDMDbgLevel = E_LDM_DEBUG_LEVEL_ERROR;
151 static MS_BOOL gbLDMInitialized = FALSE;
152 static MS_U8 gu8LDMStatus;
153 static MS_U8 gLDMTemp;
154 
155 static MS_BOOL bLdm_Inited = FALSE;
156 static MS_S32 _gs32LDM_Mutex;
157 //LDM mutex wait time
158 #define LDM_MUTEX_WAIT_TIME    3000
159 
160 //-------------------------------------------------------------------------------------------------
161 //  Debug Functions
162 //-------------------------------------------------------------------------------------------------
163 #define LDM_DBG_FUNC()           if (gLDMDbgLevel >= E_LDM_DEBUG_LEVEL_ALL) \
164                                      printf("[UTOPIA]:[LDM]: %s: %d: \n",__FUNCTION__,__LINE__);
165 #define LDM_DBG_INFO(msg...)     if (gLDMDbgLevel >= E_LDM_DEBUG_LEVEL_INFO ) \
166                                      printf("[UTOPIA]:[LDM INFO]: %s: %d: ",__FUNCTION__,__LINE__);\
167                                      printf(msg);
168 #define LDM_DBG_ERR(msg...)      if (gLDMDbgLevel >= E_LDM_DEBUG_LEVEL_ERROR) \
169                                     printf("[UTOPIA]:[LDM ERROR]: %s: %d: ",__FUNCTION__,__LINE__);\
170                                     printf(msg);
171 #define LDM_DBG_WARN(msg...)     if (gLDMDbgLevel >= E_LDM_DEBUG_LEVEL_WARNING) \
172                                     printf("[UTOPIA]:[LDM WARNING]: %s: %d: ",__FUNCTION__,__LINE__);\
173                                     printf(msg);
174 
175 
176 
177 //-------------------------------------------------------------------------------------------------
178 //  Local Functions
179 //-------------------------------------------------------------------------------------------------
180 
181 
182 //-------------------------------------------------------------------------------------------------
183 //  Global Functions
184 //-------------------------------------------------------------------------------------------------
185 
186 //----------------------------------------------------------------
187 // MDrv_LDM_SetDbgLevel - Set debug level
188 /// @ingroup G_LDM_CONTROL
189 // @param: eLevel: debug level
190 // @return: E_LDM_RET_SUCCESS is setted
191 //----------------------------------------------------------------
MDrv_LDM_SetDbgLevel(EN_LDM_DEBUG_LEVEL eLevel)192 MS_U8 MDrv_LDM_SetDbgLevel(EN_LDM_DEBUG_LEVEL eLevel)
193 {
194     gLDMDbgLevel = eLevel;
195 
196 #if defined(MSOS_TYPE_LINUX)
197     int iResult = 0;
198 
199     //LDM_DBG_INFO("Debug level: %d \n", eLevel);
200     if ((E_LDM_DEBUG_LEVEL_MAX <= eLevel)||(1 > eLevel))
201     {
202         printf("elevel is out or range: %d \n", eLevel);
203         //LDM_DBG_ERR("elevel is out or range: %d \n", eLevel);
204         return E_LDM_RET_PARAMETER_ERROR;
205     }
206     if (0 == LDM_fd)   //First time open
207     {
208         LDM_fd = open(LDM_MODULE_NAME, O_RDWR);
209         if (LDM_fd < 0)
210         {
211             LDM_fd = 0;
212             printf("Fail to open LDM Kernal Module\n");
213             //LDM_DBG_ERR("Fail to open LDM Kernal Module\n");
214             return E_LDM_RET_FAIL;
215         }
216     }
217 
218     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_DEBUG,&gLDMDbgLevel);
219     if(iResult != 0)
220     {
221         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
222         return E_LDM_RET_FAIL;
223     }
224 #endif
225     LDM_DBG_INFO("OK\n");
226     return E_LDM_RET_SUCCESS;
227 }
228 
229 //----------------------------------------------------------------
230 // MDrv_LDM_Enable - start local dimming algorithm
231 /// @ingroup G_LDM_CONTROL
232 // @param: na
233 // @return: E_LDM_RET_SUCCESS is enable
234 //----------------------------------------------------------------
MDrv_LDM_Enable(void)235 MS_U8 MDrv_LDM_Enable(void)
236 {
237 #if defined(MSOS_TYPE_LINUX)
238     int iResult = 0;
239 
240     LDM_DBG_FUNC();
241 
242     if (0 == LDM_fd)
243     {
244         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
245         return E_LDM_RET_FAIL;
246     }
247     if(E_LDM_STATUS_ENABLE == gu8LDMStatus)
248     {
249         LDM_DBG_WARN(" reenter \n");
250         return E_LDM_RET_SUCCESS;
251     }
252 
253     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_ENABLE);
254     if(iResult != 0)
255     {
256         LDM_DBG_ERR("LDM enable Fails iResult: %d \n", iResult);
257         return E_LDM_RET_FAIL;
258     }
259 
260     gu8LDMStatus = E_LDM_STATUS_ENABLE;
261     LDM_DBG_INFO("OK\n");
262     return E_LDM_RET_SUCCESS;
263 #else
264     if(bLdm_Inited ==0)
265     {
266         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
267         return E_LDM_RET_FAIL;
268     }
269     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
270     {
271         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
272         return E_LDM_RET_FAIL;
273     }
274     HAL_LDM_Enable(1);
275 
276     MsOS_ReleaseMutex(_gs32LDM_Mutex);
277     return E_LDM_RET_SUCCESS;
278 #endif
279 }
280 //----------------------------------------------------------------
281 // MDrv_LDM_Disable - stop local dimming algorithm, send constant luminance  to led
282 /// @ingroup G_LDM_CONTROL
283 // @param: u8Lumma: constant luminance range from 00 to 255
284 // @return: E_LDM_RET_SUCCESS is disable
285 //----------------------------------------------------------------
MDrv_LDM_Disable(MS_U8 u8Lumma)286 MS_U8 MDrv_LDM_Disable(MS_U8 u8Lumma)
287 {
288 #if defined(MSOS_TYPE_LINUX)
289     int iResult = 0;
290 
291     LDM_DBG_INFO(" u8Lumma: %d \n", u8Lumma);
292 
293     if (0 == LDM_fd)
294     {
295         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
296         return E_LDM_RET_FAIL;
297     }
298     if(E_LDM_STATUS_DISNABLE == gu8LDMStatus)
299     {
300         LDM_DBG_WARN(" reenter \n");
301         return E_LDM_RET_SUCCESS;
302     }
303 
304     gLDMTemp = u8Lumma;
305     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_DISABLE,&gLDMTemp);
306     if(iResult != 0)
307     {
308         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
309         return E_LDM_RET_FAIL;
310     }
311 
312     gu8LDMStatus = E_LDM_STATUS_DISNABLE;
313     LDM_DBG_INFO("OK\n");
314     return E_LDM_RET_SUCCESS;
315 #else
316     if(bLdm_Inited ==0)
317     {
318         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
319         return E_LDM_RET_FAIL;
320     }
321     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
322     {
323         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
324         return E_LDM_RET_FAIL;
325     }
326     HAL_LDM_Enable(0);
327 
328     MsOS_ReleaseMutex(_gs32LDM_Mutex);
329     return E_LDM_RET_SUCCESS;
330 #endif
331 }
332 
333 #if defined(MSOS_TYPE_LINUX)
334 //----------------------------------------------------------------
335 // MDrv_LDM_GetData - get local dimming status
336 /// @ingroup G_LDM_CONTROL
337 // @param: void
338 // @return: EN_LDM_STATUS
339 //----------------------------------------------------------------
MDrv_LDM_GetStatus(void)340 MS_U8 MDrv_LDM_GetStatus(void)
341 {
342     LDM_DBG_FUNC();
343     LDM_DBG_INFO("status: %d \n", gu8LDMStatus);
344 
345     return gu8LDMStatus;
346 }
347 
348 //----------------------------------------------------------------
349 // MDrv_LDM_SetConfPath - Set config files path
350 /// @ingroup G_LDM_CONTROL
351 // @param: stPath: config files path in board or u-disk
352 // @return: E_LDM_RET_SUCCESS is setted
353 //----------------------------------------------------------------
MDrv_LDM_SetConfPath(ST_LDM_CONF_PATH stPath)354 MS_U8 MDrv_LDM_SetConfPath(ST_LDM_CONF_PATH stPath)
355 {
356     int iResult = 0;
357     ST_LDM_CONF_PATH stCusPath;
358 
359     if (!(stPath.aCusPath) || !(stPath.aCusPathU))
360     {
361         LDM_DBG_ERR("path id null \n");
362         return E_LDM_RET_PARAMETER_ERROR;
363     }
364     LDM_DBG_INFO("set config path: %s, u-disk path:%s \n", stPath.aCusPath, stPath.aCusPathU);
365 
366     if (0 == LDM_fd)   //First time open
367     {
368         LDM_fd = open(LDM_MODULE_NAME, O_RDWR);
369         if (LDM_fd < 0)
370         {
371             LDM_fd = 0;
372             LDM_DBG_ERR("Fail to open LDM Kernal Module\n");
373             return E_LDM_RET_FAIL;
374         }
375     }
376 
377     memset(&stCusPath,0,sizeof(ST_LDM_CONF_PATH));
378     strcpy((char *)(stCusPath.aCusPath), (const char *)(stPath.aCusPath));
379     strcpy((char *)(stCusPath.aCusPathU), (const char *)(stPath.aCusPathU));
380     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_CUS_PATH,&stCusPath);
381     if(iResult != 0)
382     {
383         LDM_DBG_ERR("LDM path Configuration Fails iResult: %d \n", iResult);
384         return E_LDM_RET_FAIL;
385     }
386 
387     LDM_DBG_INFO("OK\n");
388     return E_LDM_RET_SUCCESS;
389 }
390 
391 //----------------------------------------------------------------
392 // MDrv_LDM_Init - Set  mmap address to register base
393 /// @ingroup G_LDM_CONTROL
394 // @param: phyAddr: local dimming mmap address in mmap.ini
395 // @return: E_LDM_RET_SUCCESS is inital
396 //----------------------------------------------------------------
MDrv_LDM_Init(MS_PHY phyAddr)397 MS_U8 MDrv_LDM_Init(MS_PHY phyAddr)
398 {
399     int iResult = 0;
400     MS_PHY phyMMAPAddr = 0;
401 
402     LDM_DBG_INFO("init mmap address: 0x%llx \n", phyAddr);
403 
404     if(TRUE == gbLDMInitialized)
405     {
406         LDM_DBG_WARN(" reenter \n");
407         return E_LDM_RET_SUCCESS;
408     }
409 
410     if (0 == LDM_fd)   //First time open
411     {
412         LDM_fd = open(LDM_MODULE_NAME, O_RDWR);
413         if (LDM_fd < 0)
414         {
415             LDM_fd = 0;
416             LDM_DBG_ERR("Fail to open LDM Kernal Module\n");
417             return E_LDM_RET_FAIL;
418         }
419     }
420 
421     phyMMAPAddr = phyAddr;
422     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_INIT,&phyMMAPAddr);
423     if(iResult != 0)
424     {
425         LDM_DBG_ERR("LDM Initial Fails iResult: %d \n", iResult);
426         return E_LDM_RET_FAIL;
427     }
428 
429     gbLDMInitialized = TRUE;
430     gu8LDMStatus = E_LDM_STATUS_INIT;
431     LDM_DBG_INFO("OK\n");
432     return E_LDM_RET_SUCCESS;
433 }
434 
435 //----------------------------------------------------------------
436 // MDrv_LDM_GetData - get LDF/LDB/SPI data pre frame in buffer
437 /// @ingroup G_LDM_CONTROL
438 // @param: stData:  data type and mmap address filled with the requied type
439 // @return: E_LDM_RET_SUCCESS is getted
440 //----------------------------------------------------------------
MDrv_LDM_GetData(ST_LDM_GET_DATA * stData)441 MS_U8 MDrv_LDM_GetData(ST_LDM_GET_DATA *stData)
442 {
443     int iResult = 0;
444     ST_LDM_GET_DATA stGetData;
445 
446     if (0 == LDM_fd)
447     {
448         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
449         return E_LDM_RET_FAIL;
450     }
451     if (!(stData))
452     {
453         LDM_DBG_ERR("poiter is null! \n");
454         return E_LDM_RET_PARAMETER_ERROR;
455     }
456     LDM_DBG_INFO("get enDataType :%d \n", stData->enDataType);
457 
458     memset(&stGetData,0,sizeof(ST_LDM_GET_DATA));
459     stGetData.enDataType = stData->enDataType;
460     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_GET_DATA,&stGetData);
461     if(iResult != 0)
462     {
463         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
464         return E_LDM_RET_FAIL;
465     }
466 
467     stData->phyAddr = stGetData.phyAddr;
468     LDM_DBG_INFO("OK\n");
469     return E_LDM_RET_SUCCESS;
470 }
471 
472 
473 //----------------------------------------------------------------
474 // MDrv_LDM_SetStrength - Set back light percent
475 /// @ingroup G_LDM_CONTROL
476 // @param: u8Percent: the percent ranged from 0 to 100
477 // @return: E_LDM_RET_SUCCESS is setted
478 //----------------------------------------------------------------
MDrv_LDM_SetStrength(MS_U8 u8Percent)479 MS_U8 MDrv_LDM_SetStrength(MS_U8 u8Percent)
480 {
481     int iResult = 0;
482 
483     LDM_DBG_INFO("strength u8Percent: %d \n", u8Percent);
484 
485     if ((100 <= u8Percent)||(0 > u8Percent))
486     {
487         LDM_DBG_ERR("Percent is out or range: %d \n", u8Percent);
488         return E_LDM_RET_PARAMETER_ERROR;
489     }
490     if (0 == LDM_fd)
491     {
492         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
493         return E_LDM_RET_FAIL;
494     }
495 
496     gLDMTemp = (u8Percent*255)/100;
497     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_SET_STRENGTH,&gLDMTemp);
498     if(iResult != 0)
499     {
500         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
501         return E_LDM_RET_FAIL;
502     }
503 
504     LDM_DBG_INFO("OK\n");
505     return E_LDM_RET_SUCCESS;
506 }
507 
508 
509 //----------------------------------------------------------------
510 // MDrv_LDM_Suspend -go to sleep
511 /// @ingroup G_LDM_CONTROL
512 // @param: na
513 // @return: E_LDM_RET_SUCCESS is suspend
514 //----------------------------------------------------------------
MDrv_LDM_Suspend(void)515 MS_U8 MDrv_LDM_Suspend(void)
516 {
517     int iResult = 0;
518 
519     LDM_DBG_FUNC();
520 
521     if (0 == LDM_fd)
522     {
523         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
524         return E_LDM_RET_FAIL;
525     }
526     if(E_LDM_STATUS_SUSPEND == gu8LDMStatus)
527     {
528         LDM_DBG_WARN(" reenter \n");
529         return E_LDM_RET_SUCCESS;
530     }
531 
532     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_SUSPEND);
533     if(iResult != 0)
534     {
535         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
536         return E_LDM_RET_FAIL;
537     }
538 
539     gu8LDMStatus = E_LDM_STATUS_SUSPEND;
540     LDM_DBG_INFO("OK\n");
541     return E_LDM_RET_SUCCESS;
542 }
543 
544 
545 //----------------------------------------------------------------
546 // MDrv_LDM_Resume - wake up from sleeping
547 /// @ingroup G_LDM_CONTROL
548 // @param: na
549 // @return: E_LDM_RET_SUCCESS is resumed
550 //----------------------------------------------------------------
MDrv_LDM_Resume(void)551 MS_U8 MDrv_LDM_Resume(void)
552 {
553     //int iResult = 0;
554 
555     LDM_DBG_FUNC();
556 
557     if (0 == LDM_fd)
558     {
559         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
560         return E_LDM_RET_FAIL;
561     }
562 
563     if(E_LDM_STATUS_RESUME == gu8LDMStatus)
564     {
565         LDM_DBG_WARN(" reenter \n");
566         return E_LDM_RET_SUCCESS;
567     }
568 
569     /*  resume in kernel directly, since the booting time
570     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_RESUME);
571     if(iResult != 0)
572     {
573         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
574         return E_LDM_RET_FAIL;
575     }
576     */
577 
578    gu8LDMStatus = E_LDM_STATUS_RESUME;
579    LDM_DBG_INFO("OK\n");
580    return E_LDM_RET_SUCCESS;
581 }
582 
583 
584 //----------------------------------------------------------------
585 // MDrv_LDM_DemoPattern - demo pattern from customer
586 /// @ingroup G_LDM_CONTROL
587 // @param: stPattern: demo type: turn on led, left-right half show
588 // @return: E_LDM_RET_SUCCESS is demonstrative
589 //----------------------------------------------------------------
MDrv_LDM_DemoPattern(ST_LDM_DEMO_PATTERN stPattern)590 MS_U8 MDrv_LDM_DemoPattern(ST_LDM_DEMO_PATTERN stPattern)
591 {
592     int iResult = 0;
593     ST_LDM_DEMO_PATTERN stDemoPattern;
594 
595     LDM_DBG_INFO("bOn: %d, type: %d, led number: %d \n", \
596         stPattern.bOn, stPattern.enDemoPattern, stPattern.u16LEDNum);
597 
598     if (0 == LDM_fd)
599     {
600         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
601         return E_LDM_RET_FAIL;
602     }
603 
604     memset(&stDemoPattern,0,sizeof(ST_LDM_DEMO_PATTERN));
605     stDemoPattern.bOn = stPattern.bOn;
606     stDemoPattern.enDemoPattern = stPattern.enDemoPattern;
607     stDemoPattern.u16LEDNum = stPattern.u16LEDNum;
608     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_DEMO_PATTERN,&stDemoPattern);
609     if(iResult != 0)
610     {
611         LDM_DBG_ERR("LDM Initial Configuration Fails iResult: %d \n", iResult);
612         return E_LDM_RET_FAIL;
613     }
614 
615     LDM_DBG_INFO("OK\n");
616     return E_LDM_RET_SUCCESS;
617 }
618 
619 
620 //----------------------------------------------------------------
621 // MDrv_LDM_DumpDRAM - Print LDF/LDB/SPI data
622 /// @ingroup G_LDM_CONTROL
623 // @param: eType: LDF/LDB/SPI
624 // @return: E_LDM_RET_SUCCESS is dumped
625 //----------------------------------------------------------------
MDrv_LDM_DumpDRAM(EN_LDM_GET_DATA_TYPE eType)626 MS_U8 MDrv_LDM_DumpDRAM(EN_LDM_GET_DATA_TYPE eType)
627 {
628     int iResult = 0;
629     EN_LDM_GET_DATA_TYPE eGetType;
630 
631     LDM_DBG_INFO("dump eType: %d \n", eType);
632 
633     if ((E_LDM_DATA_TYPE_MAX <= eType)||(E_LDM_DATA_TYPE_LDF > eType))
634     {
635         LDM_DBG_ERR("type is out or range: %d \n", eType);
636         return E_LDM_RET_PARAMETER_ERROR;
637     }
638     if (0 == LDM_fd)
639     {
640         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
641         return E_LDM_RET_FAIL;
642     }
643 
644     eGetType = eType;
645     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_DUMP_DRAM,&eGetType);
646     if(iResult != 0)
647     {
648         LDM_DBG_ERR("LDM dump dram Fails iResult: %d \n", iResult);
649         return E_LDM_RET_FAIL;
650     }
651 
652     LDM_DBG_INFO("OK\n");
653     return E_LDM_RET_SUCCESS;
654 }
655 
656 
657 //----------------------------------------------------------------
658 // MDrv_LDM_Deinit - stop local dimmg, free resource owned
659 /// @ingroup G_LDM_CONTROL
660 // @param: na
661 // @return: E_LDM_RET_SUCCESS is deinit
662 //----------------------------------------------------------------
MDrv_LDM_Deinit(void)663 MS_U8 MDrv_LDM_Deinit(void)
664 {
665     int iResult = 0;
666     LDM_DBG_FUNC();
667 
668     if (0 == LDM_fd)
669     {
670         LDM_DBG_ERR("LDM file descriptor is not been opened\n");
671         return E_LDM_RET_FAIL;
672     }
673     if(E_LDM_STATUS_DEINIT == gu8LDMStatus)
674     {
675         LDM_DBG_WARN(" reenter \n");
676         return E_LDM_RET_SUCCESS;
677     }
678 
679     iResult = ioctl(LDM_fd, MDRV_LDM_IOC_DEINIT);
680     if(iResult != 0)
681     {
682         LDM_DBG_ERR("LDM deinit Fails iResult: %d \n", iResult);
683         return E_LDM_RET_FAIL;
684     }
685 
686     close(LDM_fd);
687     LDM_fd = 0;
688     gLDMDbgLevel = E_LDM_DEBUG_LEVEL_ERROR;
689     gbLDMInitialized = FALSE;
690     gu8LDMStatus = E_LDM_STATUS_DEINIT;
691     LDM_DBG_INFO("OK\n");
692     return E_LDM_RET_SUCCESS;
693 }
694 
695 
696 //----------------------------------------------------------------
697 // MDrv_LDM_SendCommads - the api to reserve
698 /// @ingroup G_LDM_CONTROL
699 // @param: pstCMD: pointer to command
700 // @param: u8Number: number of command
701 // @return: E_LDM_RET_SUCCESS is sent
702 //----------------------------------------------------------------
MDrv_LDM_SendCommads(ST_LDM_CMD_INFO * pstCMD,MS_U8 u8Number)703 MS_U8 MDrv_LDM_SendCommads(ST_LDM_CMD_INFO* pstCMD,MS_U8 u8Number)
704 {
705     int iResult = 0;
706     MS_U8 u8Counter = 0;
707 
708     LDM_DBG_INFO("number: %d \n", u8Number);
709 
710     if (0 == LDM_fd)   //First time open
711     {
712         LDM_fd = open(LDM_MODULE_NAME, O_RDWR);
713         if (LDM_fd < 0)
714         {
715             LDM_fd = 0;
716             LDM_DBG_ERR("Fail to open LDM Kernal Module\n");
717             return E_LDM_RET_FAIL;
718         }
719     }
720 
721     if (!pstCMD)
722     {
723         LDM_DBG_ERR("poiter is null! \n");
724         return E_LDM_RET_PARAMETER_ERROR;
725     }
726 
727     while(u8Number > u8Counter)
728     {
729         if(!((pstCMD[u8Counter]).pContent))
730         {
731             LDM_DBG_ERR("pstCMD[%d] poniter is null \n", u8Counter);
732             u8Counter++;
733             continue;
734             //return E_LDM_RET_NOT_SUPPORTED;
735         }
736 
737         //MS_S8 temp = -1;   //commands without any parameter
738         //sCommandArray[i].u32CommandIndex = MDRV_LDM_IOC_ENABLE;
739         //sCommandArray[i].pContent = &temp;
740         iResult = *(MS_S8 *)((pstCMD[u8Counter]).pContent);
741         LDM_DBG_INFO("send command u8Counter: %d, content:%d\n", u8Counter, iResult);
742         if(-1 != *(MS_S8 *)((pstCMD[u8Counter]).pContent))
743         {
744             iResult = ioctl(LDM_fd, (pstCMD[u8Counter]).u32CommandIndex, (pstCMD[u8Counter]).pContent);
745         }
746         else
747         {
748             iResult = ioctl(LDM_fd, (pstCMD[u8Counter]).u32CommandIndex);
749         }
750 
751         if(iResult != 0)
752         {
753             LDM_DBG_ERR("LDM send command Fails iResult: %d, u8Counter:%d \n", iResult, u8Counter);
754             return E_LDM_RET_FAIL;
755         }
756         u8Counter++;
757         usleep(900);
758     }
759 
760     LDM_DBG_INFO("OK\n");
761     return E_LDM_RET_SUCCESS;
762 }
763 
764 #endif
765 //-------------------------------------------------------------------------------------------------
766 /// Description : set LDM bank base address
767 /// @param u8ClkHz \b IN:  back light period 60HZ or 120HZ
768 /// @return TRUE : Success
769 /// @return Others : Fail
770 //-------------------------------------------------------------------------------------------------
MDrv_LDM_SetIOMapBase(MS_U8 u8ClkHz)771 MS_BOOL MDrv_LDM_SetIOMapBase(MS_U8 u8ClkHz)
772 {
773     MS_VIRT VirtNONPMBank = 0;
774     MS_PHY  u32NONPMBankSize = 0;
775 
776     if(bLdm_Inited)
777     {
778          LDM_DBG_ERR("Fun:%s @@error: LDM Channel %d has been initiated \n",__FUNCTION__,u8ClkHz);
779         return FALSE;
780     }
781     //create mutex for multi thread
782     _gs32LDM_Mutex = MsOS_CreateMutex(E_MSOS_FIFO, "Mutex LDM", MSOS_PROCESS_SHARED);
783     MS_ASSERT(_gs32LDM_Mutex >= 0);
784 
785     //get nonPM base bank addr
786     if (!MDrv_MMIO_GetBASE( &VirtNONPMBank, &u32NONPMBankSize, MS_MODULE_HW))
787     {
788          LDM_DBG_ERR("Fun:%s @@error:IOMap failure to get DRV_MMIO_NONPM_BANK\n",__FUNCTION__);
789         return FALSE;
790     }
791 
792     HAL_LDM_MMIOConfig(VirtNONPMBank,u8ClkHz);
793 
794     bLdm_Inited = TRUE;
795     return TRUE;
796 }
797 //-------------------------------------------------------------------------------------------------
798 /// Description : set LDF/ LDB/ SPI/ Edge2D base address
799 /// @param stLdmCfgAddr \b IN:  doublebuffer for  LDF index
800 /// @return TRUE : Success
801 /// @return Others : Fail
802 //-------------------------------------------------------------------------------------------------
MDrv_LDM_Config_MmapAddr(ST_LDM_CFG_MMAP_ADDR * stLdmCfgAddr)803 MS_BOOL MDrv_LDM_Config_MmapAddr(ST_LDM_CFG_MMAP_ADDR *stLdmCfgAddr)
804 {
805     if((stLdmCfgAddr == NULL) || (bLdm_Inited ==0))
806     {
807         LDM_DBG_ERR("Fun:%s @@error:param_in stLdmCfgAddr is NULL or LDM not inited!!!\n",__FUNCTION__);
808         return FALSE;
809     }
810     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
811     {
812         LDM_DBG_ERR("Fun:%s @@error: ENTRY fails!\n", __FUNCTION__);
813         return FALSE;
814     }
815     #if 0
816     LDM_DBG_INFO("u32LDFAddr0_l = %lx \n",stLdmCfgAddr->u32LDFAddr0_l);
817     LDM_DBG_INFO("u32LDFAddr0_r = %lx \n",stLdmCfgAddr->u32LDFAddr0_r);
818     LDM_DBG_INFO("u32LDFAddr1_l = %lx \n",stLdmCfgAddr->u32LDFAddr1_l);
819     LDM_DBG_INFO("u32LDFAddr1_r = %lx \n",stLdmCfgAddr->u32LDFAddr1_r);
820     LDM_DBG_INFO("u32LDBAddr0_l = %lx \n",stLdmCfgAddr->u32LDBAddr0_l);
821     LDM_DBG_INFO("u32LDBAddr0_r = %lx \n",stLdmCfgAddr->u32LDBAddr0_r);
822     LDM_DBG_INFO("u32LDBAddr1_l = %lx \n",stLdmCfgAddr->u32LDBAddr1_l);
823     LDM_DBG_INFO("u32LDBAddr1_r = %lx \n",stLdmCfgAddr->u32LDBAddr1_r);
824     LDM_DBG_INFO("u32Edge2DAddr = %lx \n",stLdmCfgAddr->u32Edge2DAddr);
825     LDM_DBG_INFO("u32DataOffset = %lx \n",stLdmCfgAddr->u32DataOffset);
826     #endif
827     //step 1: set LDF base mem address
828     HAL_LDM_SetLDFAddr(0,stLdmCfgAddr->u32LDFAddr0_l,stLdmCfgAddr->u32LDFAddr0_r);
829     HAL_LDM_SetLDFAddr(1,stLdmCfgAddr->u32LDFAddr1_l,stLdmCfgAddr->u32LDFAddr1_r);
830 
831     //step2: set LDB base mem address
832     HAL_LDM_SetLDBAddr(0,stLdmCfgAddr->u32LDBAddr0_l,stLdmCfgAddr->u32LDBAddr0_r);
833     HAL_LDM_SetLDBAddr(1,stLdmCfgAddr->u32LDBAddr1_l,stLdmCfgAddr->u32LDBAddr1_r);
834 
835     //step3: set Edge2D base mem address
836     HAL_LDM_SetEdge2DAddr(stLdmCfgAddr->u32Edge2DAddr);
837 
838     //step4:set LEDBuf BaseOffset [spi data addr = LDB addr +LEDBuf BaseOffset]
839     HAL_LDM_SetLEDBufBaseOffset(stLdmCfgAddr->u32DataOffset);
840 
841     MsOS_ReleaseMutex(_gs32LDM_Mutex);
842     return TRUE;
843 }
844 
845 //-------------------------------------------------------------------------------------------------
846 /// Description : Set MIU PackOffset & Pack length [DMA load mem offset data ,ps:always offset=0]
847 /// @param u8HWNum \b IN: LDMA Hw number index
848 /// @param u8PackOffset \b IN: u8PackOffset
849 /// @param u8PackLength \b IN: DMA load mem data pack format size
850 /// @return TRUE : Success
851 /// @return Others : Fail
852 //-------------------------------------------------------------------------------------------------
MDrv_LDM_SetMIUPackFormat(MS_U8 u8HWNum,MS_U8 u8PackOffset,MS_U8 u8PackLength)853 MS_BOOL MDrv_LDM_SetMIUPackFormat(MS_U8 u8HWNum,MS_U8 u8PackOffset,MS_U8 u8PackLength)
854 {
855     if(bLdm_Inited ==0)
856     {
857         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
858         return FALSE;
859     }
860     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
861     {
862         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
863         return FALSE;
864     }
865 
866     HAL_LDM_SetMIUPackOffset(u8HWNum,u8PackOffset);
867     HAL_LDM_SetMIUPackLength(u8HWNum,u8PackLength);
868 
869     MsOS_ReleaseMutex(_gs32LDM_Mutex);
870     return TRUE;
871 }
872 //-------------------------------------------------------------------------------------------------
873 /// Description : Set YoffEnd
874 /// @param u8HWNum \b IN: LDMA Hw number index
875 /// @param u8YoffEnd \b IN: u8YoffEnd always =led height
876 /// @return DRVAESDMA_OK : Success
877 /// @return Others : Fail
878 //-------------------------------------------------------------------------------------------------
MDrv_LDM_SetYoffEnd(MS_U8 u8HWNum,MS_U8 u8YoffEnd)879 MS_BOOL MDrv_LDM_SetYoffEnd(MS_U8 u8HWNum,MS_U8 u8YoffEnd)
880 {
881     if(bLdm_Inited ==0)
882     {
883         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
884         return FALSE;
885     }
886     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
887     {
888         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
889         return FALSE;
890     }
891     HAL_LDM_SetYoffEnd(u8HWNum,u8YoffEnd);
892 
893     MsOS_ReleaseMutex(_gs32LDM_Mutex);
894     return TRUE;
895 }
896 //-------------------------------------------------------------------------------------------------
897 /// Description : Set Back light height & width number
898 /// @param u8Height \b IN: LDMA Height = led Height number
899 /// @param u8Height \b IN: LDMA Width = led Width number
900 /// @return DRVAESDMA_OK : Success
901 /// @return Others : Fail
902 //-------------------------------------------------------------------------------------------------
MDrv_LDM_SetBlLdmaSize(MS_U8 u8Height,MS_U8 u8Width)903 MS_BOOL MDrv_LDM_SetBlLdmaSize(MS_U8 u8Height,MS_U8 u8Width)
904 {
905     if(bLdm_Inited ==0)
906     {
907         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
908         return FALSE;
909     }
910     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
911     {
912         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
913         return FALSE;
914     }
915     HAL_LDM_SetBlHeightDMA(u8Height);
916     HAL_LDM_SetBlWidthDMA(u8Width);
917 
918     MsOS_ReleaseMutex(_gs32LDM_Mutex);
919     return TRUE;
920 }
921 
922 //-------------------------------------------------------------------------------------------------
923 /// Description : Enable ldma for LD
924 /// @param u8HWNum \b IN: LDMA Hw number index
925 /// @param bEnable \b IN: set DMA for local dimming enable or disable
926 /// @return DRVAESDMA_OK : Success
927 /// @return Others : Fail
928 //-------------------------------------------------------------------------------------------------
MDrv_LDM_SetDmaEnable(MS_U8 u8HWNum,MS_BOOL bEnable)929 MS_BOOL MDrv_LDM_SetDmaEnable(MS_U8 u8HWNum, MS_BOOL bEnable)
930 {
931     if(bLdm_Inited ==0)
932     {
933         LDM_DBG_ERR("Fun:%s @@error:LDM Should init !!!\n",__FUNCTION__);
934         return FALSE;
935     }
936     if (FALSE == MsOS_ObtainMutex(_gs32LDM_Mutex, LDM_MUTEX_WAIT_TIME))
937     {
938         LDM_DBG_ERR("Fun:%s @@error:ENTRY fails!\n", __FUNCTION__);
939         return FALSE;
940     }
941 
942     HAL_LDM_SetDmaEnable(u8HWNum,bEnable);
943 
944     MsOS_ReleaseMutex(_gs32LDM_Mutex);
945     return TRUE;
946 
947 }
948