1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "hdf_disp.h"
10 #include <securec.h>
11 #include "hdf_base.h"
12 #include "hdf_bl.h"
13 #include "hdf_log.h"
14 #include "osal.h"
15 
16 #define OFFSET_TWO_BYTE    16
17 static struct DispManager *g_dispManager = NULL;
18 static struct PanelManager g_panelManager;
RegisterPanel(struct PanelData * panel)19 int32_t RegisterPanel(struct PanelData *panel)
20 {
21     uint32_t panelNum;
22 
23     if (panel == NULL) {
24         HDF_LOGE("%s: panel data is null", __func__);
25         return HDF_ERR_INVALID_PARAM;
26     }
27     if (panel->info == NULL) {
28         HDF_LOGE("%s panel info is null", __func__);
29         return HDF_FAILURE;
30     }
31     if ((panel->on == NULL) || (panel->off == NULL)) {
32         HDF_LOGE("%s on or off is null", __func__);
33         return HDF_FAILURE;
34     }
35     panel->powerStatus = POWER_STATUS_OFF;
36     panelNum = g_panelManager.panelNum;
37     if (panelNum >= PANEL_MAX) {
38         HDF_LOGE("%s registered panel up PANEL_MAX", __func__);
39         return HDF_FAILURE;
40     }
41     g_panelManager.panel[panelNum] = panel;
42     g_panelManager.panelNum++;
43     HDF_LOGI("%s: register success", __func__);
44     return HDF_SUCCESS;
45 }
46 
GetPanelManager(void)47 struct PanelManager *GetPanelManager(void)
48 {
49     if (g_panelManager.panelNum == 0) {
50         return NULL;
51     } else {
52         return &g_panelManager;
53     }
54 }
55 
GetDispManager(void)56 struct DispManager *GetDispManager(void)
57 {
58     if (g_dispManager != NULL && g_dispManager->initialzed) {
59         return g_dispManager;
60     }
61     return NULL;
62 }
63 
64 static int32_t InitDisp(uint32_t devId);
65 int32_t DispOn(uint32_t devId);
66 int32_t DispOff(uint32_t devId);
67 int32_t SetDispBacklight(uint32_t devId, uint32_t level);
68 static int32_t GetDispInfo(uint32_t devId, struct DispInfo *info);
69 static int32_t SetDispPower(uint32_t devId, uint32_t powerStatus);
70 static void EsdCheckStartUp(struct DispEsd *esd, uint32_t devId);
71 static void EsdCheckEnd(struct DispEsd *esd, uint32_t devId);
72 
GetDispOps(void)73 struct DispOperations *GetDispOps(void)
74 {
75     static struct DispOperations dispOps = {
76         .init = InitDisp,
77         .on = DispOn,
78         .off = DispOff,
79         .setBacklight = SetDispBacklight,
80         .getDispInfo = GetDispInfo,
81     };
82     return &dispOps;
83 }
84 
85 #ifdef __KERNEL__
86 EXPORT_SYMBOL(GetDispOps);
87 EXPORT_SYMBOL(DispOn);
88 EXPORT_SYMBOL(DispOff);
89 EXPORT_SYMBOL(SetDispBacklight);
90 #endif
91 
InitDisp(uint32_t devId)92 static int32_t InitDisp(uint32_t devId)
93 {
94     return 0;
95 }
96 
DispOn(uint32_t devId)97 int32_t DispOn(uint32_t devId)
98 {
99     return SetDispPower(devId, POWER_STATUS_ON);
100 }
101 
DispOff(uint32_t devId)102 int32_t DispOff(uint32_t devId)
103 {
104     return SetDispPower(devId, POWER_STATUS_OFF);
105 }
106 
SetDispBacklight(uint32_t devId,uint32_t level)107 int32_t SetDispBacklight(uint32_t devId, uint32_t level)
108 {
109     struct DispManager *disp = NULL;
110     struct PanelData *panel = NULL;
111 
112     disp = GetDispManager();
113     if (disp && disp->panelManager && devId < disp->panelManager->panelNum) {
114         panel = disp->panelManager->panel[devId];
115     }
116     if ((panel == NULL) || (UpdateBrightness(panel->blDev, level) != HDF_SUCCESS)) {
117         HDF_LOGE("%s:panel is null or UpdateBrightness failed", __func__);
118         return HDF_FAILURE;
119     }
120     HDF_LOGI("%s:level = %u", __func__, level);
121     return HDF_SUCCESS;
122 }
123 
GetDispInfo(uint32_t devId,struct DispInfo * info)124 static int32_t GetDispInfo(uint32_t devId, struct DispInfo *info)
125 {
126     struct DispManager *disp = NULL;
127     struct PanelData *panel = NULL;
128 
129     if (info == NULL) {
130         HDF_LOGE("%s:info is null", __func__);
131         return HDF_FAILURE;
132     }
133     disp = GetDispManager();
134     if (disp == NULL) {
135         HDF_LOGE("%s: disp is null", __func__);
136         return HDF_FAILURE;
137     }
138     if (devId >= disp->panelManager->panelNum) {
139         HDF_LOGE("%s: devId exceed registered panelNum", __func__);
140         return HDF_FAILURE;
141     }
142     panel = disp->panelManager->panel[devId];
143     info->width = panel->info->width;
144     info->height = panel->info->height;
145     info->hbp = panel->info->hbp;
146     info->hfp = panel->info->hfp;
147     info->hsw = panel->info->hsw;
148     info->vbp = panel->info->vbp;
149     info->vfp = panel->info->vfp;
150     info->vsw = panel->info->vsw;
151     info->intfType = panel->info->intfType;
152     info->intfSync = panel->info->intfSync;
153     info->frameRate = panel->info->frameRate;
154     info->minLevel = panel->info->blk.minLevel;
155     info->maxLevel = panel->info->blk.maxLevel;
156     info->defLevel = panel->info->blk.defLevel;
157     return HDF_SUCCESS;
158 }
159 
SetDispPower(uint32_t devId,uint32_t powerStatus)160 static int32_t SetDispPower(uint32_t devId, uint32_t powerStatus)
161 {
162     int32_t ret = HDF_FAILURE;
163     struct DispManager *disp = NULL;
164     struct PanelData *panel = NULL;
165 
166     disp = GetDispManager();
167     if (disp == NULL) {
168         HDF_LOGE("%s:disp is null", __func__);
169         return HDF_FAILURE;
170     }
171     if (devId >= disp->panelManager->panelNum) {
172         HDF_LOGE("%s:devId exceed registered panelNum", __func__);
173         return HDF_FAILURE;
174     }
175     panel = disp->panelManager->panel[devId];
176     OsalMutexLock(&disp->dispMutex);
177     if (panel->powerStatus == powerStatus) {
178         OsalMutexUnlock(&disp->dispMutex);
179         HDF_LOGE("%s: panel already in mode = %d", __func__, powerStatus);
180         return HDF_SUCCESS;
181     }
182     switch (powerStatus) {
183         case POWER_STATUS_ON:
184             ret = panel->on(panel);
185             if (ret == HDF_SUCCESS) {
186                 panel->powerStatus = POWER_STATUS_ON;
187                 ret = UpdateBacklightState(panel->blDev, FB_POWER_ON);
188                 EsdCheckStartUp(disp->esd, devId);
189             }
190             break;
191         case POWER_STATUS_OFF:
192             ret = panel->off(panel);
193             if (ret == HDF_SUCCESS) {
194                 panel->powerStatus = POWER_STATUS_OFF;
195                 ret = UpdateBacklightState(panel->blDev, FB_POWER_OFF);
196                 EsdCheckEnd(disp->esd, devId);
197             }
198             break;
199         default:
200             HDF_LOGE("%s: not support powerStatus: %d", __func__, powerStatus);
201             break;
202     }
203     OsalMutexUnlock(&disp->dispMutex);
204     return ret;
205 }
206 
SetPowerStatus(struct HdfDeviceObject * device,struct HdfSBuf * reqData,struct HdfSBuf * rspData)207 static int32_t SetPowerStatus(struct HdfDeviceObject *device, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
208 {
209     uint32_t para = 0;
210 
211     (void)device;
212     (void)rspData;
213     if (reqData == NULL) {
214         return HDF_ERR_INVALID_PARAM;
215     }
216     if (!HdfSbufReadUint32(reqData, &para)) {
217         HDF_LOGE("%s: HdfSbufReadBuffer failed", __func__);
218         return HDF_FAILURE;
219     }
220     uint32_t devId = (para >> OFFSET_TWO_BYTE) & 0xffff;
221     uint32_t powerStatus = para & 0xffff;
222     return SetDispPower(devId, powerStatus);
223 }
224 
GetPowerStatus(struct HdfDeviceObject * device,struct HdfSBuf * reqData,struct HdfSBuf * rspData)225 static int32_t GetPowerStatus(struct HdfDeviceObject *device, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
226 {
227     uint32_t devId = 0;
228     enum PowerStatus powerStatus;
229     struct DispManager *disp = NULL;
230     struct PanelData *panel = NULL;
231 
232     (void)device;
233     if (reqData == NULL) {
234         return HDF_ERR_INVALID_PARAM;
235     }
236     if (!HdfSbufReadUint32(reqData, &devId)) {
237         HDF_LOGE("%s: HdfSbufReadBuffer failed", __func__);
238         return HDF_FAILURE;
239     }
240     disp = GetDispManager();
241     if (disp == NULL || disp->panelManager == NULL || (devId >= disp->panelManager->panelNum)) {
242         HDF_LOGE("%s: get panel failed", __func__);
243         return HDF_FAILURE;
244     }
245     OsalMutexLock(&disp->dispMutex);
246     panel = disp->panelManager->panel[devId];
247     powerStatus = panel->powerStatus;
248     OsalMutexUnlock(&disp->dispMutex);
249     if (!HdfSbufWriteUint32(rspData, powerStatus)) {
250         HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
251         return HDF_FAILURE;
252     }
253     return HDF_SUCCESS;
254 }
255 
SetBacklight(struct HdfDeviceObject * device,struct HdfSBuf * reqData,struct HdfSBuf * rspData)256 static int32_t SetBacklight(struct HdfDeviceObject *device, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
257 {
258     int32_t ret;
259 
260     (void)device;
261     (void)rspData;
262     if (reqData == NULL) {
263         return HDF_ERR_INVALID_PARAM;
264     }
265     uint32_t para = 0;
266     if (!HdfSbufReadUint32(reqData, &para)) {
267         HDF_LOGE("%s: HdfSbufReadBuffer failed", __func__);
268         return HDF_FAILURE;
269     }
270     uint32_t devId = (para >> OFFSET_TWO_BYTE) & 0xffff;
271     uint32_t level = para & 0xffff;
272     ret = SetDispBacklight(devId, level);
273     return ret;
274 }
275 
GetBacklight(struct HdfDeviceObject * device,struct HdfSBuf * reqData,struct HdfSBuf * rspData)276 static int32_t GetBacklight(struct HdfDeviceObject *device, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
277 {
278     uint32_t devId = 0;
279     uint32_t currLevel;
280     struct DispManager *disp = NULL;
281     struct PanelData *panel = NULL;
282 
283     (void)device;
284     if (reqData == NULL) {
285         return HDF_ERR_INVALID_PARAM;
286     }
287     if (!HdfSbufReadUint32(reqData, &devId)) {
288         HDF_LOGE("%s: HdfSbufReadBuffer failed", __func__);
289         return HDF_FAILURE;
290     }
291     disp = GetDispManager();
292     if (disp == NULL || disp->panelManager == NULL || (devId >= disp->panelManager->panelNum)) {
293         HDF_LOGE("%s: get panel failed", __func__);
294         return HDF_FAILURE;
295     }
296     panel = disp->panelManager->panel[devId];
297     if (GetCurrBrightness(panel->blDev, &currLevel) != HDF_SUCCESS) {
298         HDF_LOGE("%s: GetCurrBrightness failed", __func__);
299         return HDF_FAILURE;
300     }
301     if (!HdfSbufWriteUint32(rspData, currLevel)) {
302         HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
303         return HDF_FAILURE;
304     }
305     return HDF_SUCCESS;
306 }
307 
GetInfo(struct HdfDeviceObject * device,struct HdfSBuf * reqData,struct HdfSBuf * rspData)308 static int32_t GetInfo(struct HdfDeviceObject *device, struct HdfSBuf *reqData, struct HdfSBuf *rspData)
309 {
310     (void)device;
311     (void)rspData;
312     uint32_t devId = 0;
313     struct DispInfo info;
314 
315     if (reqData == NULL) {
316         return HDF_ERR_INVALID_PARAM;
317     }
318     if (!HdfSbufReadUint32(reqData, &devId)) {
319         HDF_LOGE("%s: HdfSbufReadBuffer failed", __func__);
320         return HDF_FAILURE;
321     }
322     (void)memset_s(&info, sizeof(struct DispInfo), 0, sizeof(struct DispInfo));
323     if (GetDispInfo(devId, &info) != HDF_SUCCESS) {
324         HDF_LOGE("%s: GetDispInfo failed", __func__);
325         return HDF_FAILURE;
326     }
327     if (!HdfSbufWriteBuffer(rspData, &info, sizeof(struct DispInfo)) != 0) {
328         HDF_LOGE("%s: copy info failed", __func__);
329         return HDF_FAILURE;
330     }
331     return HDF_SUCCESS;
332 }
333 
334 DispCmdHandle g_dispCmdHandle[] = {
335     GetPowerStatus,
336     GetInfo,
337     SetPowerStatus,
338     SetBacklight,
339     GetBacklight,
340 };
341 
DispCmdProcess(struct HdfDeviceObject * device,int32_t cmd,struct HdfSBuf * reqData,struct HdfSBuf * rspData)342 static int32_t DispCmdProcess(struct HdfDeviceObject *device, int32_t cmd, struct HdfSBuf *reqData,
343     struct HdfSBuf *rspData)
344 {
345     int32_t cmdNum = sizeof(g_dispCmdHandle) / sizeof(g_dispCmdHandle[0]);
346     if (device == NULL || reqData == NULL || rspData == NULL) {
347         return HDF_ERR_INVALID_PARAM;
348     }
349     if (cmd >= cmdNum || cmd < 0) {
350         HDF_LOGE("%s: invalid cmd = %d", __func__, cmd);
351         return HDF_FAILURE;
352     }
353     HDF_LOGD("%s: cmd = %d", __func__, cmd);
354     if (g_dispCmdHandle[cmd] == NULL) {
355         return HDF_FAILURE;
356     }
357 
358     return g_dispCmdHandle[cmd](device, reqData, rspData);
359 }
360 
HdfDispDispatch(struct HdfDeviceIoClient * client,int id,struct HdfSBuf * data,struct HdfSBuf * reply)361 static int32_t HdfDispDispatch(struct HdfDeviceIoClient *client, int id, struct HdfSBuf *data,
362     struct HdfSBuf *reply)
363 {
364     if (client == NULL) {
365         return HDF_ERR_INVALID_PARAM;
366     }
367     return DispCmdProcess(client->device, id, data, reply);
368 }
369 
HdfDispBind(struct HdfDeviceObject * dev)370 static int HdfDispBind(struct HdfDeviceObject *dev)
371 {
372     if (dev == NULL) {
373         return HDF_FAILURE;
374     }
375     static struct IDeviceIoService dispService = {
376         .object.objectId = 1,
377         .Dispatch = HdfDispDispatch,
378     };
379     dev->service = &dispService;
380     return HDF_SUCCESS;
381 }
382 
PanelRecovery(struct PanelData * panel)383 static void PanelRecovery(struct PanelData *panel)
384 {
385     HDF_LOGI("%s enter", __func__);
386     if (panel->off) {
387         panel->off(panel);
388     }
389     OsalMSleep(150); // delay 150ms
390     if (panel->on) {
391         panel->on(panel);
392     }
393 }
394 
EsdTimerHandler(uintptr_t arg)395 static void EsdTimerHandler(uintptr_t arg)
396 {
397     uint32_t devId = (uint32_t)arg;
398     struct DispManager *disp = NULL;
399 
400     disp = GetDispManager();
401     if ((disp == NULL) || (disp->esd == NULL)) {
402         HDF_LOGE("%s: disp or esd is null", __func__);
403         return;
404     }
405     if (devId >= disp->esd->panelNum) {
406         HDF_LOGE("%s: esd is null", __func__);
407         return;
408     }
409     HdfAddWork(&disp->dispWorkQueue, disp->esd->work[devId]);
410     HDF_LOGD("%s devId[%d] add work to wq", __func__, devId);
411 }
412 
EsdWorkHandler(void * arg)413 static void EsdWorkHandler(void *arg)
414 {
415     int32_t ret = HDF_SUCCESS;
416     uint32_t devId = (uint32_t)arg;
417     struct PanelData *panel = NULL;
418     struct DispManager *disp = NULL;
419 
420     disp = GetDispManager();
421     if ((disp == NULL) || (disp->panelManager == NULL)) {
422         HDF_LOGE("%s: disp or panelManager is null", __func__);
423         return;
424     }
425     if (devId >= disp->panelManager->panelNum) {
426         HDF_LOGE("%s: dispCtrl is null or panel is null", __func__);
427         return;
428     }
429     panel = disp->panelManager->panel[devId];
430     if ((panel->esd == NULL) || (panel->esd->checkFunc == NULL)) {
431         HDF_LOGE("%s: esd or checkFunc is null", __func__);
432         return;
433     }
434     ret = panel->esd->checkFunc(panel);
435     if (ret != HDF_SUCCESS) {
436         OsalMutexLock(&disp->dispMutex);
437         if (panel->esd->state == ESD_RUNNING) {
438             PanelRecovery(panel);
439         } else {
440             HDF_LOGI("%s: esd check has disabled", __func__);
441             OsalMutexUnlock(&disp->dispMutex);
442             return;
443         }
444         OsalMutexUnlock(&disp->dispMutex);
445         panel->esd->recoveryNum++;
446     }
447     HDF_LOGD("%s recoveryNum = %d", __func__, panel->esd->recoveryNum);
448     if (panel->esd->recoveryNum >= ESD_MAX_RECOVERY) {
449         panel->esd->recoveryNum = 0;
450         OsalMutexLock(&disp->dispMutex);
451         if (panel->esd->state == ESD_RUNNING) {
452             OsalTimerDelete(disp->esd->timer[devId]);
453             panel->esd->state = ESD_READY;
454             HDF_LOGI("%s disable esd check", __func__);
455         } else {
456             HDF_LOGI("%s: esd check has disabled", __func__);
457             OsalMutexUnlock(&disp->dispMutex);
458             return;
459         }
460         OsalMutexUnlock(&disp->dispMutex);
461     }
462 }
463 
EsdCheckStartUp(struct DispEsd * esd,uint32_t devId)464 static void EsdCheckStartUp(struct DispEsd *esd, uint32_t devId)
465 {
466     if (esd == NULL) {
467         HDF_LOGE("%s esd is null", __func__);
468         return;
469     }
470     HDF_LOGD("%s enter", __func__);
471     if ((esd->panelEsd[devId] != NULL) && esd->panelEsd[devId]->support) {
472         if (esd->panelEsd[devId]->state == ESD_READY) {
473             OsalTimerCreate(esd->timer[devId], esd->panelEsd[devId]->interval,
474                 EsdTimerHandler, (uintptr_t)devId);
475             OsalTimerStartLoop(esd->timer[devId]);
476             esd->panelEsd[devId]->state = ESD_RUNNING;
477             HDF_LOGI("%s panel enable esd check", __func__);
478         }
479     }
480 }
481 
EsdCheckEnd(struct DispEsd * esd,uint32_t devId)482 static void EsdCheckEnd(struct DispEsd *esd, uint32_t devId)
483 {
484     if (esd == NULL) {
485         HDF_LOGE("%s esd is null", __func__);
486         return;
487     }
488     HDF_LOGD("%s enter", __func__);
489     if ((esd->panelEsd[devId] != NULL) && (esd->panelEsd[devId]->support)) {
490         esd->panelEsd[devId]->recoveryNum = 0;
491         if (esd->panelEsd[devId]->state == ESD_RUNNING) {
492             OsalTimerDelete(esd->timer[devId]);
493             esd->panelEsd[devId]->state = ESD_READY;
494             HDF_LOGI("%s panel disable esd check", __func__);
495         }
496     }
497 }
498 
EsdResMalloc(int32_t panelNum)499 static struct DispEsd *EsdResMalloc(int32_t panelNum)
500 {
501     struct DispEsd *esd = NULL;
502 
503     esd = (struct DispEsd *)OsalMemCalloc(sizeof(struct DispEsd));
504     if (esd == NULL) {
505         HDF_LOGE("%s esd malloc fail", __func__);
506         return NULL;
507     }
508     esd->panelEsd = (struct PanelEsd **)OsalMemCalloc(sizeof(struct PanelEsd *) * panelNum);
509     if (esd->panelEsd == NULL) {
510         HDF_LOGE("%s panelEsd malloc fail", __func__);
511         goto PANEL_ESD_EXIT;
512     }
513     esd->work = (HdfWork **)OsalMemCalloc(sizeof(HdfWork *) * panelNum);
514     if (esd->work == NULL) {
515         HDF_LOGE("%s work malloc fail", __func__);
516         goto WORK_EXIT;
517     }
518     esd->timer = (OsalTimer **)OsalMemCalloc(sizeof(OsalTimer *) * panelNum);
519     if (esd->timer == NULL) {
520         HDF_LOGE("%s timer malloc fail", __func__);
521         goto TIMER_EXIT;
522     }
523     esd->workInit = (bool *)OsalMemCalloc(sizeof(bool) * panelNum);
524     if (esd->workInit == NULL) {
525         HDF_LOGE("%s workInit malloc fail", __func__);
526         goto WORK_INIT_EXIT;
527     }
528     return esd;
529 
530 WORK_INIT_EXIT:
531     OsalMemFree(esd->timer);
532 TIMER_EXIT:
533     OsalMemFree(esd->work);
534 WORK_EXIT:
535     OsalMemFree(esd->panelEsd);
536 PANEL_ESD_EXIT:
537     OsalMemFree(esd);
538 
539     return NULL;
540 }
541 
EsdMemFree(struct DispEsd * esd)542 static void EsdMemFree(struct DispEsd *esd)
543 {
544     uint32_t i;
545 
546     for (i = 0; i < esd->panelNum; i++) {
547         if (esd->workInit[i] == true) {
548             HdfWorkDestroy(esd->work[i]);
549         }
550         OsalMemFree(esd->work[i]);
551         OsalMemFree(esd->timer[i]);
552     }
553     OsalMemFree(esd->panelEsd);
554     OsalMemFree(esd->work);
555     OsalMemFree(esd->timer);
556     OsalMemFree(esd);
557 }
558 
EsdResInit(struct DispEsd * esd,struct PanelData ** panel)559 static int32_t EsdResInit(struct DispEsd *esd, struct PanelData **panel)
560 {
561     uint32_t i;
562     int32_t ret;
563 
564     for (i = 0; i < esd->panelNum; i++) {
565         if ((panel[i]->esd != NULL) && panel[i]->esd->support) {
566             esd->panelEsd[i] = panel[i]->esd;
567             esd->work[i] = (HdfWork *)OsalMemCalloc(sizeof(HdfWork));
568             if (esd->work[i] == NULL) {
569                 HDF_LOGE("%s work malloc fail", __func__);
570                 EsdMemFree(esd);
571                 return HDF_FAILURE;
572             }
573             esd->timer[i] = (OsalTimer *)OsalMemCalloc(sizeof(OsalTimer));
574             if (esd->timer[i] == NULL) {
575                 EsdMemFree(esd);
576                 HDF_LOGE("%s timer malloc fail", __func__);
577                 return HDF_FAILURE;
578             }
579             if (esd->panelEsd[i]->interval < ESD_DEFAULT_INTERVAL) {
580                 esd->panelEsd[i]->interval = ESD_DEFAULT_INTERVAL;
581             }
582             ret = HdfWorkInit(esd->work[i], EsdWorkHandler, (void *)i);
583             if (ret != HDF_SUCCESS) {
584                 EsdMemFree(esd);
585                 HDF_LOGE("%s HdfWorkInit fail", __func__);
586                 return HDF_FAILURE;
587             }
588             esd->workInit[i] = true;
589             esd->panelEsd[i]->state = ESD_READY;
590         }
591     }
592     return HDF_SUCCESS;
593 }
594 
EsdCheckInit(struct DispManager * disp)595 static int32_t EsdCheckInit(struct DispManager *disp)
596 {
597     uint32_t i;
598     int32_t ret;
599     int32_t count = 0;
600     uint32_t panelNum;
601     struct DispEsd *esd = NULL;
602     struct PanelData **panel = NULL;
603 
604     if (disp->panelManager == NULL) {
605         HDF_LOGE("%s panelManager is null", __func__);
606         return HDF_FAILURE;
607     }
608     panel = disp->panelManager->panel;
609     panelNum = disp->panelManager->panelNum;
610     for (i = 0; i < panelNum; i++) {
611         if ((panel[i]->esd != NULL) && panel[i]->esd->support) {
612             count++;
613             break;
614         }
615     }
616     if (count == 0) {
617         HDF_LOGI("%s none of panels support esd", __func__);
618         return HDF_SUCCESS;
619     }
620     esd = EsdResMalloc(panelNum);
621     if (esd == NULL) {
622         HDF_LOGE("%s EsdResInit fail", __func__);
623         return HDF_FAILURE;
624     }
625     esd->panelNum = panelNum;
626     ret = EsdResInit(esd, panel);
627     if (ret != HDF_SUCCESS) {
628         HDF_LOGE("%s EsdResInit fail", __func__);
629         EsdMemFree(esd);
630         return HDF_FAILURE;
631     }
632     disp->esd = esd;
633     return HDF_SUCCESS;
634 }
635 
DispManagerInit(struct PanelManager * panelManager)636 static int32_t DispManagerInit(struct PanelManager *panelManager)
637 {
638     int32_t ret;
639 
640     g_dispManager = (struct DispManager *)OsalMemCalloc(sizeof(struct DispManager));
641     if (g_dispManager == NULL) {
642         HDF_LOGE("%s g_dispManager malloc fail", __func__);
643         return HDF_FAILURE;
644     }
645     g_dispManager->panelManager = panelManager;
646     ret = HdfWorkQueueInit(&g_dispManager->dispWorkQueue, "dispWQ");
647     if (ret != HDF_SUCCESS) {
648         HDF_LOGE("%s: HdfWorkQueueInit fail", __func__);
649         return HDF_FAILURE;
650     }
651     ret = EsdCheckInit(g_dispManager);
652     if (ret != HDF_SUCCESS) {
653         HdfWorkQueueDestroy(&g_dispManager->dispWorkQueue);
654         HDF_LOGE("%s: EsdCheckInit fail", __func__);
655         return HDF_FAILURE;
656     }
657     OsalMutexInit(&g_dispManager->dispMutex);
658     g_dispManager->initialzed = true;
659     return HDF_SUCCESS;
660 }
661 
HdfDispEntryInit(struct HdfDeviceObject * object)662 static int32_t HdfDispEntryInit(struct HdfDeviceObject *object)
663 {
664     int32_t ret;
665     struct PanelManager *panelManager = NULL;
666 
667     if (object == NULL) {
668         HDF_LOGE("%s: object is null!", __func__);
669         return HDF_FAILURE;
670     }
671     panelManager = GetPanelManager();
672     if (panelManager == NULL) {
673         HDF_LOGE("%s: panelManager is null!", __func__);
674         return HDF_FAILURE;
675     }
676     ret = DispManagerInit(panelManager);
677     if (ret != HDF_SUCCESS) {
678         HDF_LOGE("%s: DispManagerInit fail", __func__);
679         return HDF_FAILURE;
680     }
681     HDF_LOGI("%s success", __func__);
682     return HDF_SUCCESS;
683 }
684 
685 struct HdfDriverEntry g_dispDevEntry = {
686     .moduleVersion = 1,
687     .moduleName = "HDF_DISP",
688     .Init = HdfDispEntryInit,
689     .Bind = HdfDispBind,
690 };
691 
692 HDF_INIT(g_dispDevEntry);
693