huangcm
2025-07-03 c26084b3642f262f858535ab4e46c1e9b520d3a1
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
 * Allwinner SoCs display driver.
 *
 * Copyright (C) 2016 Allwinner.
 *
 * This file is licensed under the terms of the GNU General Public
 * License version 2.  This program is licensed "as is" without any
 * warranty of any kind, whether express or implied.
 */
 
/******************************************************************************
 *  All Winner Tech, All Right Reserved. 2014-2015 Copyright (c)
 *
 *  File name   :       de_enhance.c
 *
 *  Description :       display engine 2.0 enhance basic function definition
 *
 *  History     :       2014/04/29  vito cheng  v0.1  Initial version
 *
 *****************************************************************************/
#include "de_rtmx.h"
#include "de_enhance.h"
#include "de_feat.h"
#include "de_vep_table.h"
 
#define ONE_SCREEN_ONE_PARA
/*
 * only ONE parameters for one screen,
 * and all VEPs in this screen use SAME parameters
 */
/* DEBUG */
#define ASE_EXIST
#define LTI_EXIST
#define FCC_EXIST
 
static int device_num;
static int vep_num[DE_NUM];
static unsigned int frame_cnt[DE_NUM] = { 0 };
 
static unsigned int g_demo_enable[DE_NUM] = { 0 };
static struct disp_enhance_config g_config[DE_NUM];
unsigned int g_format[DE_NUM] = { 0xff };
unsigned int g_size_bypass[DE_NUM] = { 0 };
/* size too small to enable vep */
static struct disp_rect g_size[DE_NUM];
static bool g_size_change[DE_NUM];
 
/* global histogram, use for bws and ce */
unsigned int *g_hist[DE_NUM][CHN_NUM];
unsigned int *g_hist_p[DE_NUM][CHN_NUM];
unsigned int g_sum[DE_NUM][CHN_NUM];
 
static unsigned int
enhance_mode_to_para[ENHANCE_MODE_NUM][MODE_NUM][FORMAT_NUM][PARA_NUM] = {
   /* SHARP    AUTO_CONTRAST  AUTO_COLOR  FC_R    FC_G    FC_B */
   {
       /* lcd normal */
       {
           /* rgb */
           {0x00, 0x00000, 0x00, 0, 0, 0},
           /* yuv */
           {0x00, 0x00000, 0x00, 0, 0, 0},
       },
       /* hdmi normal */
       {
           /* rgb */
           {0x00, 0x00000, 0x00, 0, 0, 0},
           /* yuv disable all */
           {0x00, 0x00000, 0x00, 0, 0, 0},
       },
   },
   /* lcd vivid */
   {
       {
           /* rgb */
           {0x22, 0x10001, 0x21, 1, 1, 1},
           /* yuv */
           {0x11, 0x10010, 0x21, 1, 2, 1},
       },
       /* hdmi vivid */
       {
           /* rgb */
           {0x00, 0x00000, 0x21, 1, 1, 1},
           /* yuv disable all */
           {0x20, 0x01020, 0x21, 1, 1, 1},
       },
   },
   {
       /* lcd soft */
       {
           /* rgb */
           {0x00, 0x00000, 0x00, 0, 0, 0},
           /* yuv */
           {0x00, 0x00000, 0x00, 0, 0, 0},
       },
       /* hdmi soft */
       {
           /* rgb */
           {0x00, 0x00000, 0x00, 0, 0, 0},
           /* yuv disable all */
           {0x00, 0x00000, 0x00, 0, 0, 0},
       }
   },
};
 
int de_enhance_info2data(struct disp_enhance_config *config,
            struct vep_config_data *data, unsigned int bypass)
{
   struct de_rect tmp_win;
   struct disp_enhance_config tmp_config;
 
   memcpy(&tmp_config.info, config, sizeof(struct disp_enhance_config));
   memset(data, 0, sizeof(struct vep_config_data));
 
   /* FIXME : should be deleted */
   tmp_win.x = config->info.window.x;
   tmp_win.y = config->info.window.y;
   tmp_win.w = config->info.window.width;
   tmp_win.h = config->info.window.height;
   /* FIXME end: should be deleted */
 
   tmp_config.info.sharp = (tmp_config.info.enable == 0
                || bypass == 1) ? 0 : tmp_config.info.sharp;
   tmp_config.info.auto_color = (tmp_config.info.enable == 0
                     || bypass ==
                     1) ? 0 : tmp_config.info.auto_color;
   tmp_config.info.auto_contrast = (tmp_config.info.enable == 0
                    || bypass ==
                    1) ? 0 : tmp_config.info.auto_contrast;
   tmp_config.info.fancycolor_red = (tmp_config.info.enable == 0
                     || bypass ==
                     1) ? 0 : tmp_config.info.
       fancycolor_red;
   tmp_config.info.fancycolor_green = (tmp_config.info.enable == 0
                       || bypass ==
                       1) ? 0 : tmp_config.info.
       fancycolor_green;
   tmp_config.info.fancycolor_blue = (tmp_config.info.enable == 0
                      || bypass ==
                      1) ? 0 : tmp_config.info.
       fancycolor_blue;
 
   /* FIXME : tmp_win should be deleted */
   /* fce */
   de_fce_info2para(tmp_config.info.sharp, tmp_config.info.auto_contrast,
            tmp_config.info.auto_color, tmp_win, &data->fce_para);
 
   /* FIXME */
   /* peak */
   de_peak_info2para(tmp_config.info.sharp, tmp_win,
       &data->peak_para);
#ifdef LTI_EXIST
   /* lti */
   de_lti_info2para(tmp_config.info.sharp, tmp_win,
       &data->lti_para);
#endif
#ifdef ASE_EXIST
   /* ase */
   de_ase_info2para(tmp_config.info.auto_color, tmp_win, &data->ase_para);
#endif
#ifdef FCC_EXIST
   /* fcc */
   de_fcc_info2para(tmp_config.info.fancycolor_red,
            tmp_config.info.fancycolor_green,
            tmp_config.info.fancycolor_blue, 0, 0, 0, tmp_win,
            &data->fcc_para);
#endif
 
   return 0;
}
 
int de_enhance_set_format(unsigned int screen_id, unsigned int format)
{
   int format_change = 0;
 
   if (g_format[screen_id] != format) {
       g_format[screen_id] = format;
       format_change = 1;
       g_config[screen_id].flags |= ENH_FORMAT_DIRTY;
   }
 
   if (format_change == 1 || g_size_change[screen_id])
       de_enhance_apply(screen_id, &g_config[screen_id]);
 
   return 0;
}
 
int de_enhance_apply(unsigned int screen_id,
               struct disp_enhance_config *config)
{
   int ch_id, chno;
   struct vep_config_data *data;
   int auto_contrast_dirty;
 
   data = kmalloc(sizeof(struct vep_config_data), GFP_KERNEL | __GFP_ZERO);
   if (!data) {
       __wrn("kmalloc fail!\n");
       goto err;
   }
   chno = vep_num[screen_id];
 
   __inf("disp %d, en=%d, sharp=%d\n", screen_id, config[0].info.enable,
         config[0].info.sharp);
   memcpy(&g_config[screen_id], config,
          sizeof(struct disp_enhance_config));
   de_enhance_set_mode(g_format[screen_id], config);
   for (ch_id = 0; ch_id < chno; ch_id++) {
       auto_contrast_dirty = (config[0].flags & (ENH_ENABLE_DIRTY
                              | ENH_SIZE_DIRTY
                              | ENH_FORMAT_DIRTY
                              | ENH_MODE_DIRTY)) ?
                           1 : 0;
 
       /* disp_enhance_info -> vep_config_data */
       de_enhance_info2data(&config[0], data,
                    g_size_bypass[screen_id]);
       /* FIXME: Update according to dirty flag */
       /* fce */
       de_hist_apply(screen_id, ch_id, data->fce_para.hist_en,
                 auto_contrast_dirty);
       de_ce_apply(screen_id, ch_id, data->fce_para.ce_en,
               data->fce_para.b_automode,
               data->fce_para.up_precent_thr,
               data->fce_para.down_precent_thr,
               data->fce_para.update_diff_thr,
               data->fce_para.slope_black_lmt,
               data->fce_para.slope_white_lmt,
               auto_contrast_dirty);
       de_fce_enable(screen_id, ch_id, data->fce_para.fce_en);
       de_fce_set_para(screen_id, ch_id, data->fce_para);
 
       /* peak */
       de_peak_enable(screen_id, ch_id, data->peak_para.peak_en);
       de_peak_set_para(screen_id, ch_id, data->peak_para.gain,
                data->peak_para.hp_ratio,
                data->peak_para.bp0_ratio);
 
       /* lti */
       de_lti_enable(screen_id, ch_id, data->lti_para.lti_en);
       de_lti_set_para(screen_id, ch_id, data->lti_para.lti_en);
 
       /* ase */
       de_ase_enable(screen_id, ch_id, data->ase_para.ase_en);
       de_ase_set_para(screen_id, ch_id, data->ase_para.gain);
 
       /* fcc */
       de_fcc_enable(screen_id, ch_id, data->fcc_para.fcc_en);
       de_fcc_set_para(screen_id, ch_id, data->fcc_para.sgain);
 
   }
   de_enhance_demo_enable(screen_id, config[0].info.demo_enable);
   g_config[screen_id].flags = 0;
 
   kfree(data);
err:
   return 0;
}
 
int de_enhance_set_size(unsigned int screen_id, struct disp_rect *size)
{
   int ch_id, chno;
   struct de_rect tmp_win;
   unsigned int demo_enable;
 
   chno = vep_num[screen_id];
   demo_enable = g_demo_enable[screen_id] ? 1 : 0;
 
   for (ch_id = 0; ch_id < chno; ch_id++) {
       tmp_win.x = 0;
       tmp_win.y = 0;
       if (demo_enable) {
           if (size[ch_id].width > size[ch_id].height) {
               tmp_win.w = size[ch_id].width >> 1;
               tmp_win.h = size[ch_id].height;
           } else {
               tmp_win.w = size[ch_id].width;
               tmp_win.h = size[ch_id].height >> 1;
           }
       } else {
           tmp_win.w = size[ch_id].width;
           tmp_win.h = size[ch_id].height;
       }
 
       /* fce */
       de_fce_set_size(screen_id, ch_id, size[ch_id].width,
               size[ch_id].height);
       de_fce_set_window(screen_id, ch_id, demo_enable, tmp_win);
       g_ce_status[screen_id][ch_id]->width = size[ch_id].width;
       g_ce_status[screen_id][ch_id]->height = size[ch_id].height;
 
       /* peak */
       de_peak_set_size(screen_id, ch_id, size[ch_id].width,
                size[ch_id].height);
       de_peak_set_window(screen_id, ch_id, demo_enable, tmp_win);
 
       /* lti */
       de_lti_set_size(screen_id, ch_id, size[ch_id].width,
               size[ch_id].height);
       de_lti_set_window(screen_id, ch_id, demo_enable, tmp_win);
 
       /* ase */
       de_ase_set_size(screen_id, ch_id, size[ch_id].width,
               size[ch_id].height);
       de_ase_set_window(screen_id, ch_id, demo_enable, tmp_win);
 
       /* fcc */
       de_fcc_set_size(screen_id, ch_id, size[ch_id].width,
               size[ch_id].height);
       de_fcc_set_window(screen_id, ch_id, demo_enable, tmp_win);
   }
 
   if ((size->width !=  g_size[screen_id].width)
       || (size->height != g_size[screen_id].height)) {
       g_size_change[screen_id] = true;
       g_config[screen_id].flags |= ENH_SIZE_DIRTY;
   } else
       g_size_change[screen_id] = false;
   memcpy(&g_size[screen_id], size, sizeof(struct disp_rect));
 
   if (size[0].width < ENAHNCE_MIN_WIDTH
       || size[0].height < ENAHNCE_MIN_HEIGHT) {
       g_size_bypass[screen_id] = 1;
   } else {
       g_size_bypass[screen_id] = 0;
   }
 
   return 0;
}
 
int de_enhance_demo_enable(unsigned int screen_id, unsigned int enable)
{
   if (enable)
       g_demo_enable[screen_id] = 1;
   else
       g_demo_enable[screen_id] = 0;
   return 0;
}
 
int de_enhance_sync(unsigned int screen_id)
{
   return 0;
}
 
int de_enhance_tasklet(unsigned int screen_id)
{
   int ch_id, chno;
 
   chno = vep_num[screen_id];
 
   for (ch_id = 0; ch_id < chno; ch_id++) {
       /* hist */
       de_hist_tasklet(screen_id, ch_id, frame_cnt[screen_id]);
 
       /* ce */
       de_ce_tasklet(screen_id, ch_id, frame_cnt[screen_id]);
       frame_cnt[screen_id]++;
   }
   return 0;
}
 
int de_enhance_update_regs(unsigned int screen_id)
{
   int chno, ch_id;
 
   chno = vep_num[screen_id];
   for (ch_id = 0; ch_id < chno; ch_id++) {
       de_fce_update_regs(screen_id, ch_id);
#ifdef LTI_EXIST
       de_lti_update_regs(screen_id, ch_id);
#endif
       de_peak_update_regs(screen_id, ch_id);
#ifdef ASE_EXIST
       de_ase_update_regs(screen_id, ch_id);
#endif
#ifdef FCC_EXIST
       de_fcc_update_regs(screen_id, ch_id);
#endif
   }
   return 0;
}
 
int de_enhance_init(struct disp_bsp_init_para *para)
{
   int screen_id, ch_id;
 
   device_num = de_feat_get_num_screens();
 
   for (screen_id = 0; screen_id < device_num; screen_id++)
       vep_num[screen_id] = de_feat_is_support_vep(screen_id);
 
   for (screen_id = 0; screen_id < device_num; screen_id++)
       for (ch_id = 0; ch_id < vep_num[screen_id]; ch_id++) {
#if defined(CONFIG_INDEPENDENT_DE)
           de_fce_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE + screen_id]);
#ifdef LTI_EXIST
           de_lti_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE + screen_id]);
#endif
           de_peak_init(screen_id, ch_id,
                    para->reg_base[DISP_MOD_DE + screen_id]);
#ifdef ASE_EXIST
           de_ase_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE + screen_id]);
#endif
#ifdef FCC_EXIST
           de_fcc_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE + screen_id]);
#endif
#else/*for CONFIG_INDEPENDENT_DE*/
           de_fce_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE]);
#ifdef LTI_EXIST
           de_lti_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE]);
#endif
           de_peak_init(screen_id, ch_id,
                    para->reg_base[DISP_MOD_DE]);
#ifdef ASE_EXIST
           de_ase_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE]);
#endif
#ifdef FCC_EXIST
           de_fcc_init(screen_id, ch_id,
                   para->reg_base[DISP_MOD_DE]);
#endif
#endif
       }
 
   /* initial */
   for (screen_id = 0; screen_id < device_num; screen_id++)
       frame_cnt[screen_id] = 0;
 
   return 0;
}
 
int de_enhance_exit(void)
{
   int screen_id, ch_id;
 
   device_num = de_feat_get_num_screens();
 
   for (screen_id = 0; screen_id < device_num; screen_id++)
       vep_num[screen_id] = de_feat_is_support_vep(screen_id);
 
   for (screen_id = 0; screen_id < device_num; screen_id++)
       for (ch_id = 0; ch_id < vep_num[screen_id]; ch_id++) {
           de_fce_exit(screen_id, ch_id);
#ifdef LTI_EXIST
           de_lti_exit(screen_id, ch_id);
#endif
           de_peak_exit(screen_id, ch_id);
#ifdef ASE_EXIST
           de_ase_exit(screen_id, ch_id);
#endif
#ifdef FCC_EXIST
           de_fcc_exit(screen_id, ch_id);
#endif
       }
 
   return 0;
}
 
int de_enhance_set_mode(unsigned int format, struct disp_enhance_config *config)
{
   unsigned int mode, enhance_mode;
 
   mode = config->info.mode & 0xffff;
   enhance_mode = (config->info.mode & 0xffff0000) >> 16;
 
   if ((mode >= MODE_NUM) || (format >= 2)
       || (enhance_mode >= ENHANCE_MODE_NUM)) {
       __wrn("para excess! mode = %d, enhance_mode = %d, format=%d\n",
           mode, enhance_mode, format);
       return 1;
   }
 
   config->info.sharp =
       enhance_mode_to_para[enhance_mode][mode][format][0];
   config->info.auto_contrast =
       enhance_mode_to_para[enhance_mode][mode][format][1];
   config->info.auto_color =
       enhance_mode_to_para[enhance_mode][mode][format][2];
   config->info.fancycolor_red =
       enhance_mode_to_para[enhance_mode][mode][format][3];
   config->info.fancycolor_green =
       enhance_mode_to_para[enhance_mode][mode][format][4];
   config->info.fancycolor_blue =
       enhance_mode_to_para[enhance_mode][mode][format][5];
 
   return 0;
}