ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * drivers/video/sunxi/bootGUI/dev_manage.c
 *
 * Copyright (c) 2007-2019 Allwinnertech Co., Ltd.
 * Author: zhengxiaobin <zhengxiaobin@allwinnertech.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
#include <common.h>
#include <boot_gui.h>
#include "boot_gui_config.h"
#include "dev_manage.h"
#include "hdmi_manage.h"
#include "video_hal.h"
#include "video_misc_hal.h"
 
#define DISPLAY_PARTITION_NAME "Reserve0"
#define DISPLAY_RSL_FILENAME "disp_rsl.fex"
 
enum {
   LCD_NOT_SKIP_OPEN = 0,
   LCD_SKIP_OPEN,
};
 
/**
 * @name       :get_display_config
 * @param[IN]  :num:number of byte of buf
 * @param[IN]  :buf:pointer of content in disp_rsi.fex
 * @param[OUT] :disp_dev:boot gui display device
 * @return     :0 if find one
 */
static int get_display_config(disp_device_t *disp_dev, char *buf, int num)
{
   int format = 0, i = 0;
   char *p = buf;
   char *needle = NULL;
   char *tok = NULL;
   unsigned char line = 0;
   unsigned int temp[5] = {0};
 
   if (!disp_dev || !buf) {
       printf("Null pointer!\n");
       goto OUT;
   }
 
   while (num > 0 && line < 6) {
       while ((*p != '\n') && (*p != '\0'))
           p++;
       *p++ = '\0';
       if (line < 3) {
           format = simple_strtoul(buf, NULL, 16);
           if (disp_dev->type == ((format >> 8) & 0xFF)) {
               pr_msg("get format[%x] for type[%d]\n", format,
                      disp_dev->type);
               disp_dev->mode = format & 0xff;
               if (disp_dev->type == DISP_OUTPUT_TYPE_HDMI &&
                   disp_dev->mode < DISP_TV_MOD_480P) {
                   disp_dev->mode = -1;
                   return -1;
               }
           }
       } else {
           /*get device config*/
           if (disp_dev->type == DISP_OUTPUT_TYPE_HDMI) {
               needle = strstr(buf, "hdmi_config=");
               if (needle != NULL) {
                   i = 0;
                   needle = strstr(buf, "=");
                   ++needle;
                   tok = strtok(needle, ",");
                   while (tok != NULL && i < 5) {
                       temp[i] = simple_strtoul(
                           tok, NULL, 10);
                       tok = strtok(NULL, ",");
                       ++i;
                   }
                   disp_dev->format = temp[0];
                   disp_dev->bits = temp[1];
                   disp_dev->eotf = temp[2];
                   disp_dev->cs = temp[3];
                   disp_dev->dvi_hdmi = temp[4];
                   return 0;
               } else {
                   disp_dev->format = DISP_CSC_TYPE_YUV444;
                   disp_dev->bits = DISP_DATA_8BITS;
                   disp_dev->eotf = DISP_EOTF_GAMMA22;
                   disp_dev->cs =
                       disp_dev->mode >
                           DISP_TV_MOD_720P_50HZ
                       ? DISP_BT709
                       : DISP_BT601;
                   disp_dev->dvi_hdmi = DISP_HDMI;
               }
           } else {
               disp_dev->format =
                   (disp_dev->type == DISP_OUTPUT_TYPE_LCD)
                   ? DISP_CSC_TYPE_RGB
                   : DISP_CSC_TYPE_YUV444;
               disp_dev->bits = DISP_DATA_8BITS;
               disp_dev->eotf = DISP_EOTF_GAMMA22;
               disp_dev->cs =
                   disp_dev->mode > DISP_TV_MOD_720P_50HZ
                   ? DISP_BT709
                   : DISP_BT601;
           }
       }
       num -= (p - buf);
       buf = p;
       ++line;
   }
 
   if (line == 6) {
       disp_dev->format = (disp_dev->type == DISP_OUTPUT_TYPE_LCD)
                      ? DISP_CSC_TYPE_RGB
                      : DISP_CSC_TYPE_YUV444;
       disp_dev->bits = DISP_DATA_8BITS;
       disp_dev->eotf = DISP_EOTF_GAMMA22;
       disp_dev->cs = disp_dev->mode > DISP_TV_MOD_720P_50HZ
                  ? DISP_BT709
                  : DISP_BT601;
   }
OUT:
   return -1;
}
 
static int get_device_configs(disp_device_t *disp_dev_list, int *dev_num)
{
   int node = 0;
   char prop[32] = {'\n'};
 
   int read_bytes = 0;
   char buf[256] = {0};
 
   disp_device_t *disp_dev = NULL;
   int id = 0;
   int fsload = 0;
 
   node = get_disp_fdt_node();
 
   for (id = 0; id < *dev_num; ++id) {
       uint32_t value = 0;
       sprintf(prop, "dev%d_output_type", id);
       disp_getprop_by_name(node, prop, &value, 0);
 
       /* For LCD products, it is redundant to do this, so we skip it here */
       if (id == 0  && value != 1 && fsload == 0) {
           read_bytes = hal_fat_fsload(DISPLAY_PARTITION_NAME,
               DISPLAY_RSL_FILENAME, buf, sizeof(buf));
           fsload = 1;
       }
       if (0 == value)
           break;
 
       disp_dev = &(disp_dev_list[id]);
       memset((void *)disp_dev, 0, sizeof(*disp_dev));
       disp_dev->type = value;
       sprintf(prop, "dev%d_screen_id", id);
       disp_getprop_by_name(node, prop, (uint32_t *)&(disp_dev->screen_id), 0);
       sprintf(prop, "dev%d_do_hpd", id);
       disp_getprop_by_name(node, prop, (uint32_t *)&(disp_dev->do_hpd), 0);
       if (0 < read_bytes)
           get_display_config(disp_dev, buf, read_bytes);
       if ((0 >= read_bytes) || (-1 == disp_dev->mode)) {
           sprintf(prop, "dev%d_output_mode", id);
           disp_getprop_by_name(node, prop, (uint32_t *)&(disp_dev->mode), 0);
           disp_dev->format =
               (disp_dev->type == DISP_OUTPUT_TYPE_LCD)
               ? DISP_CSC_TYPE_RGB
               : DISP_CSC_TYPE_YUV444;
           disp_dev->bits = DISP_DATA_8BITS;
           disp_dev->eotf = DISP_EOTF_GAMMA22;
           disp_dev->cs =
               disp_dev->mode > DISP_TV_MOD_720P_50HZ
               ? DISP_BT709
               : DISP_BT601;
       }
   }
 
   if (0 < id) {
       *dev_num = id;
   } else {
       /* cannot allow that dev_num is no larger than 0 */
       printf("no cfs of display devices.\n");
       memset((void *)disp_dev_list, 0, sizeof(*disp_dev_list));
       disp_dev_list->type = DISP_OUTPUT_TYPE_LCD;
       *dev_num = 1;
   }
 
   if (1 < *dev_num)
       disp_getprop_by_name(node, "def_output_dev", (uint32_t *)&id, 0);
   else if (1 == *dev_num)
       id = 0;
   return id; /* the def_output_dev for multi devices policy */
}
 
static disp_device_t *do_hpd_detect(disp_device_t *disp_dev, int dev_num)
{
   disp_device_t *devices[DISP_DEV_NUM];
   int hpd_num, i;
   unsigned int count;
 
   for (hpd_num = 0, i = 0; i < dev_num; ++i) {
       if (disp_dev[i].do_hpd) {
           devices[hpd_num] = &(disp_dev[i]);
           ++hpd_num;
       }
   }
 
   if (0 < hpd_num) {
 
       disp_dev = devices[0];
       count = HPD_DETECT_COUNT0;
       while (count--) {
           disp_dev->hpd_state = hal_get_hpd_state(
               devices[0]->screen_id, disp_dev->type);
           if (disp_dev->hpd_state) {
               /*printf("main-hpd:count=%u,sel=%d,type=%d\n",*/
                   /*HPD_DETECT_COUNT0 - count,*/
                   /*disp_dev->screen_id, disp_dev->type);*/
               return disp_dev;
           }
       }
 
       count = HPD_DETECT_COUNT1;
       while (count--) {
           for (i = 0; i < hpd_num; ++i) {
               disp_dev = devices[i];
               disp_dev->hpd_state = hal_get_hpd_state(
                   disp_dev->screen_id, disp_dev->type);
               if (disp_dev->hpd_state) {
                   /*printf("ext-hpd:count=%u,sel=%d,type=%d\n",*/
                       /*HPD_DETECT_COUNT1 - count,*/
                       /*disp_dev->screen_id, disp_dev->type);*/
                   return disp_dev;
               }
           }
       }
   }
 
   return NULL;
}
 
static unsigned char lcd_is_skip_open(int channel)
{
#ifdef CONFIG_LCD_CHECK_SKIP_OPEN
   /* this is for skiping lcd opening for carlet produce */
   char data_buf[8] = {0};
   int read_bytes = hal_fat_fsload("Reserve0", "ban_bl.fex",
       data_buf, sizeof(data_buf));
   return (read_bytes >= 0) ? LCD_SKIP_OPEN : LCD_NOT_SKIP_OPEN;
#else
   return LCD_NOT_SKIP_OPEN;
#endif
}
 
int disp_devices_open(void)
{
   disp_device_t devices[DISP_DEV_NUM];
   int actual_dev_num = DISP_DEV_NUM;
   int def_output_dev;
   int verify_mode;
   disp_device_t *output_dev = NULL;
 
   /* 1.get display devices list */
   memset((void *)devices, 0, sizeof(devices));
   def_output_dev = get_device_configs(devices, &actual_dev_num);
 
   /* 2.chose one as output by doing hpd */
   if (devices[0].type == DISP_OUTPUT_TYPE_LCD) {
       output_dev = &devices[0];
   } else {
       output_dev = do_hpd_detect(devices, actual_dev_num);
       if (NULL == output_dev)
           output_dev = &(devices[def_output_dev]);
   }
 
   /* 3.do open one device */
   switch (output_dev->type) {
   case DISP_OUTPUT_TYPE_HDMI:
       if (0 == output_dev->hpd_state) {
           printf("hdmi hpd out, force open?\n");
           /* TODO: force open hdmi device */
       } else {
           int vendor_id;
 
           verify_mode = hdmi_verify_mode(
               output_dev->screen_id, output_dev->mode, &vendor_id);
           if (verify_mode != output_dev->mode) {
               /* If the mode is change, need to reset the
                * other configs (format/bits/cs).
                * TODO: select format and bits according to edid*/
               output_dev->mode = verify_mode;
               if (verify_mode == DISP_TV_MOD_3840_2160P_50HZ
                       || verify_mode == DISP_TV_MOD_3840_2160P_60HZ) {
                   output_dev->bits = DISP_DATA_8BITS;
                   output_dev->format = DISP_CSC_TYPE_YUV420;
                   output_dev->cs = DISP_BT709;
               } else {
                   output_dev->bits = DISP_DATA_8BITS;
                   output_dev->format = DISP_CSC_TYPE_YUV444;
                   output_dev->cs = verify_mode > DISP_TV_MOD_720P_50HZ ?
                       DISP_BT709 : DISP_BT601;
               }
           }
           hal_switch_device(output_dev, FB_ID_0); /* fixme */
       }
       break;
   case DISP_OUTPUT_TYPE_LCD:
       if (LCD_SKIP_OPEN == lcd_is_skip_open(output_dev->screen_id)) {
           printf("lcd skip open\n");
           hal_save_int_to_kernel("ban_bl", 1);
           return 0;
       }
   case DISP_OUTPUT_TYPE_TV:
   case DISP_OUTPUT_TYPE_VGA:
       hal_switch_device(output_dev, FB_ID_0); /* fixme */
       break;
   default:
       printf("open device(type=%d) fail!\n", output_dev->type);
       return -1;
   }
 
#ifdef CONFIG_UPDATE_DISPLAY_MODE
   int init_disp = 0;
   int i = 0;
   for (i = 0; i < actual_dev_num; ++i) {
       if (0 == devices[i].opened) {
           init_disp |= (((devices[i].type << 8) | devices[i].mode)
               << (devices[i].screen_id * 16));
       }
   }
   if (0 != init_disp)
       hal_save_int_to_kernel("init_disp", init_disp);
 
   /* update display device config to kerner */
   if (hal_save_disp_device_config_to_kernel(0, 0))
       printf("save disp device failed\n");
#endif
 
   return 0;
}
 
#if defined(CONFIG_BOOT_GUI_TEST)
/* for test */
int disp_device_open_ex(int dev_id, int fb_id, int flag)
{
   disp_device_t devices[DISP_DEV_NUM];
   int actual_dev_num = DISP_DEV_NUM;
   int def_output_dev;
   disp_device_t *output_dev = NULL;
 
   /* 1.get display devices list */
   memset((void *)devices, 0, sizeof(devices));
   def_output_dev = get_device_configs(devices, &actual_dev_num);
   if (dev_id >= actual_dev_num) {
       printf("invalid para: dev_id=%d, actual_dev_num=%d\n",
           dev_id, actual_dev_num);
       return -1;
   }
   output_dev = &(devices[dev_id]);
   return hal_switch_device(output_dev, fb_id);
}
#endif