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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/of_gpio.h>
#include "panel.h"
#include "panel_drv.h"
#include "copr.h"
static int panel_do_copr_seqtbl_by_index(struct copr_info *copr, int index)
{
struct panel_device *panel = to_panel_device(copr);
struct seqinfo *tbl;
int ret;
if (panel == NULL) {
panel_err("ERR:PANEL:%s:panel is null\n", __func__);
return -EINVAL;
}
if (!IS_PANEL_ACTIVE(panel)) {
panel_warn("WARN:PANEL:%s:panel inactive state\n", __func__);
return -EINVAL;
}
tbl = panel->copr.seqtbl;
mutex_lock(&panel->op_lock);
if (unlikely(index < 0 || index >= MAX_COPR_SEQ)) {
panel_err("%s, invalid paramter (panel %p, index %d)\n",
__func__, panel, index);
ret = -EINVAL;
goto do_exit;
}
#ifdef DEBUG_PANEL
pr_info("%s, %s start\n", __func__, tbl[index].name);
#endif
ret = panel_do_seqtbl(panel, &tbl[index]);
if (unlikely(ret < 0)) {
pr_err("%s, failed to excute seqtbl %s\n",
__func__, tbl->name);
ret = -EIO;
goto do_exit;
}
do_exit:
mutex_unlock(&panel->op_lock);
#ifdef DEBUG_PANEL
pr_info("%s, %s end\n", __func__, tbl[index].name);
#endif
return 0;
}
static int panel_set_copr(struct copr_info *copr)
{
int ret;
if (unlikely(!copr->props.support))
return -ENODEV;
ret = panel_do_copr_seqtbl_by_index(copr, COPR_SET_SEQ);
if (unlikely(ret < 0)) {
pr_err("%s, failed to do seqtbl\n", __func__);
return -EIO;
}
msleep(34);
if (copr->props.reg.copr_en)
copr->props.state = COPR_REG_ON;
else
copr->props.state = COPR_REG_OFF;
pr_debug("%s copr en %d gamma %d er 0x%02X eg 0x%02X eb 0x%02X roi on:%d xs:%d ys:%d xe:%d ye:%d\n",
__func__, copr->props.reg.copr_en, copr->props.reg.copr_gamma,
copr->props.reg.copr_er, copr->props.reg.copr_eg, copr->props.reg.copr_eb,
copr->props.reg.roi_on, copr->props.reg.roi_xs, copr->props.reg.roi_ys,
copr->props.reg.roi_xe, copr->props.reg.roi_ye);
return 0;
}
int panel_set_roi(struct copr_info *copr, struct copr_roi *roi, struct copr_ergb *ergb)
{
struct copr_properties *props = &copr->props;
int ret = 0;
if (unlikely(!copr->props.support))
return -ENODEV;
if (roi == NULL) {
props->reg.roi_xs = 0;
props->reg.roi_ys = 0;
props->reg.roi_xe = 0;
props->reg.roi_ye = 0;
props->reg.roi_on = false;
} else {
props->reg.roi_xs = roi->roi_xs;
props->reg.roi_ys = roi->roi_ys;
props->reg.roi_xe = roi->roi_xe;
props->reg.roi_ye = roi->roi_ye;
props->reg.roi_on = true;
}
ret = panel_set_copr(copr);
if (ret < 0) {
pr_err("%s, failed to set_copr\n", __func__);
goto error;
}
error:
return ret;
}
#ifdef CONFIG_SUPPORT_COPR_AVG
static int panel_clear_copr(struct copr_info *copr)
{
int ret = 0;
ret = panel_do_copr_seqtbl_by_index(copr, COPR_CLR_CNT_ON_SEQ);
if (unlikely(ret < 0))
pr_err("%s, failed to do seqtbl\n", __func__);
msleep(34);
ret = panel_do_copr_seqtbl_by_index(copr, COPR_CLR_CNT_OFF_SEQ);
if (unlikely(ret < 0))
pr_err("%s, failed to do seqtbl\n", __func__);
msleep(34);
pr_debug("%s copr clear seq\n", __func__);
return ret;
}
#endif
#ifdef CONFIG_SUPPORT_COPR_DSI
static int panel_read_copr_dsi(struct copr_info *copr)
{
u8 *buf = NULL;
int ret, size;
struct panel_device *panel = to_panel_device(copr);
struct panel_info *panel_data;
struct copr_properties *props = &copr->props;
if (unlikely(!panel)) {
panel_err("PANEL:ERR:%s:panel is null\n", __func__);
return -ENODEV;
}
panel_data = &panel->panel_data;
ret = panel_do_copr_seqtbl_by_index(copr, COPR_DSI_GET_SEQ);
if (unlikely(ret < 0)) {
pr_err("%s, failed to do seqtbl\n", __func__);
ret = -EIO;
goto get_copr_error;
}
size = get_resource_size_by_name(panel_data, "copr_dsi");
if (size < 0) {
pr_err("%s, failed to get copr size (ret %d)\n", __func__, size);
ret = -EINVAL;
goto get_copr_error;
}
buf = kzalloc(size, GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto get_copr_error;
}
ret = resource_copy_by_name(panel_data, (u8 *)buf, "copr_dsi");
if (ret < 0) {
pr_err("%s, failed to get copr (ret %d)\n", __func__, ret);
ret = -EIO;
goto get_copr_error;
}
if (props->version == 2) {
props->copr_ready = ((buf[10] & 0x80) ? 1 : 0);
props->cur_cnt = (buf[0] << 8) | buf[1];
props->cur_copr = (buf[2] << 8) | buf[3];
props->avg_copr = (buf[4] << 8) | buf[5];
props->s_cur_cnt = (buf[6] << 8) | buf[7];
props->s_avg_copr = (buf[8] << 8) | buf[9];
props->comp_copr = (((buf[10] & 0x01) ? 1 : 0) << 8 & 0x1) | buf[11];
pr_debug("copr_dsi: cur_cnt %d, cur_copr %d, avg_copr %d, s_cur_cnt %d, s_avg_copr %d, copr_ready %d, comp_copr %d\n",
props->cur_cnt, props->cur_copr, props->avg_copr,
props->s_cur_cnt, props->s_avg_copr, props->copr_ready, props->comp_copr);
} else {
props->cur_copr = buf[0];
}
get_copr_error:
kfree(buf);
return ret;
}
#endif
static int panel_read_copr(struct copr_info *copr)
{
u8 *buf = NULL;
int ret, size;
struct panel_device *panel = to_panel_device(copr);
struct panel_info *panel_data;
struct copr_properties *props = &copr->props;
if (unlikely(!panel)) {
panel_err("PANEL:ERR:%s:panel is null\n", __func__);
return -ENODEV;
}
panel_data = &panel->panel_data;
ret = panel_do_copr_seqtbl_by_index(copr, COPR_GET_SEQ);
if (unlikely(ret < 0)) {
pr_err("%s, failed to do seqtbl\n", __func__);
ret = -EIO;
goto get_copr_error;
}
size = get_resource_size_by_name(panel_data, "copr");
if (size < 0) {
pr_err("%s, failed to get copr size (ret %d)\n", __func__, size);
ret = -EINVAL;
goto get_copr_error;
}
buf = kzalloc(size, GFP_KERNEL);
if (!buf) {
ret = -ENOMEM;
goto get_copr_error;
}
ret = resource_copy_by_name(panel_data, (u8 *)buf, "copr");
if (ret < 0) {
pr_err("%s, failed to get copr (ret %d)\n", __func__, ret);
ret = -EIO;
goto get_copr_error;
}
if (props->version == 2) {
props->copr_ready = ((buf[0] & 0x80) ? 1 : 0);
props->cur_cnt = ((buf[0] & 0x7F) << 9) |
(buf[1] << 1) | ((buf[2] & 0x80) ? 1 : 0);
props->cur_copr = ((buf[2] & 0x7F) << 2) | ((buf[3] & 0xC0) >> 6);
props->avg_copr = ((buf[3] & 0x3F) << 3) | ((buf[4] & 0xE0) >> 5);
props->s_cur_cnt = ((buf[4] & 0x1F) << 11) |
(buf[5] << 3) | ((buf[6] & 0xE0) >> 5);
props->s_avg_copr = ((buf[6] & 0x1F) << 4) | ((buf[7] & 0xF0) >> 4);
props->comp_copr = ((buf[7] & 0x0F) << 5) | ((buf[8] & 0xF8) >> 3);
pr_debug("copr_spi: cur_cnt %d, cur_copr %d, avg_copr %d, s_cur_cnt %d, s_avg_copr %d, copr_ready %d, comp_copr %d\n",
props->cur_cnt, props->cur_copr, props->avg_copr,
props->s_cur_cnt, props->s_avg_copr, props->copr_ready, props->comp_copr);
} else {
props->cur_copr = buf[0];
}
get_copr_error:
kfree(buf);
return ret;
}
static int panel_get_copr(struct copr_info *copr)
{
struct timespec cur_ts, last_ts, delta_ts;
struct copr_properties *props = &copr->props;
s64 elapsed_usec;
int ret;
if (unlikely(!props->support))
return -ENODEV;
ktime_get_ts(&cur_ts);
if (props->state != COPR_REG_ON) {
pr_debug("%s copr reg is not on state %d\n",
__func__, props->state);
ret = -EINVAL;
goto get_copr_error;
}
if (atomic_read(&copr->stop)) {
panel_warn("%s copr_stop\n", __func__);
ret = -EINVAL;
goto get_copr_error;
}
#ifdef CONFIG_SUPPORT_COPR_DSI
panel_read_copr_dsi(copr);
#endif
panel_read_copr(copr);
ktime_get_ts(&last_ts);
delta_ts = timespec_sub(last_ts, cur_ts);
elapsed_usec = timespec_to_ns(&delta_ts) / 1000;
pr_debug("%s elapsed_usec %lld usec (%lld.%lld %lld.%lld)\n",
__func__, elapsed_usec,
timespec_to_ns(&cur_ts) / 1000000000,
(timespec_to_ns(&cur_ts) % 1000000000) / 1000,
timespec_to_ns(&last_ts) / 1000000000,
(timespec_to_ns(&last_ts) % 1000000000) / 1000);
return 0;
get_copr_error:
return ret;
}
bool copr_is_enabled(struct copr_info *copr)
{
return (copr->props.support && copr->props.reg.copr_en && copr->props.enable);
}
static int copr_res_init(struct copr_info *copr)
{
timenval_init(&copr->res);
return 0;
}
static int copr_res_start(struct copr_info *copr, int value, struct timespec cur_ts)
{
timenval_start(&copr->res, value, cur_ts);
return 0;
}
int copr_update_start(struct copr_info *copr, int count)
{
if (unlikely(!copr->props.support))
return -ENODEV;
if (atomic_read(&copr->wq.count) < count)
atomic_set(&copr->wq.count, count);
wake_up_interruptible_all(&copr->wq.wait);
return 0;
}
int copr_update_average(struct copr_info *copr)
{
struct copr_properties *props = &copr->props;
struct timespec cur_ts;
int ret;
int cur_copr;
if (unlikely(!copr->props.support))
return -ENODEV;
if (!copr_is_enabled(copr)) {
panel_dbg("%s copr disabled\n", __func__);
return -EIO;
}
ktime_get_ts(&cur_ts);
if (props->state == COPR_UNINITIALIZED) {
panel_set_copr(copr);
panel_info("%s copr register updated\n", __func__);
}
ret = panel_get_copr(copr);
if (ret < 0) {
panel_err("%s failed to get copr (ret %d)\n",
__func__, ret);
return -EINVAL;
}
if (copr->props.version == 2) {
#ifdef CONFIG_SUPPORT_COPR_AVG
ret = panel_clear_copr(copr);
if (unlikely(ret < 0))
pr_err("%s, failed to reset copr\n", __func__);
cur_copr = props->avg_copr;
timenval_update_average(&copr->res, cur_copr, cur_ts);
#else
cur_copr = props->cur_copr;
timenval_update_snapshot(&copr->res, cur_copr, cur_ts);
#endif
} else {
cur_copr = props->cur_copr;
timenval_update_snapshot(&copr->res, cur_copr, cur_ts);
}
return 0;
}
int copr_get_value(struct copr_info *copr)
{
struct copr_properties *props = &copr->props;
int ret;
int cur_copr;
if (unlikely(!copr->props.support))
return -ENODEV;
mutex_lock(&copr->lock);
if (!copr_is_enabled(copr)) {
panel_dbg("%s copr disabled\n", __func__);
mutex_unlock(&copr->lock);
return -EIO;
}
if (props->state == COPR_UNINITIALIZED) {
panel_set_copr(copr);
panel_info("%s copr register updated\n", __func__);
}
ret = panel_get_copr(copr);
if (ret < 0) {
panel_err("%s failed to get copr (ret %d)\n",
__func__, ret);
mutex_unlock(&copr->lock);
return -EINVAL;
}
cur_copr = props->cur_copr;
mutex_unlock(&copr->lock);
return cur_copr;
}
/**
* copr_roi_get_value - get copr value of each roi
*
* @ copr : copr_info
* @ roi : a pointer of roi array.
* @ size : size of roi array.
* @ out : a pointer roi output value to be stored.
*
* Get copr snapshot for each roi's r/g/b.
* This function returns 0 if the average is valid.
* If not this function returns -ERRNO.
*/
int copr_roi_get_value(struct copr_info *copr, struct copr_roi *roi, int size, u32 *out)
{
struct copr_properties *props = &copr->props;
struct timespec cur_ts;
int ret;
int i, cur_copr;
struct copr_reg reg;
if (unlikely(!copr->props.support))
return -ENODEV;
/* update using last value or avg_copr */
mutex_lock(&copr->lock);
ret = copr_update_average(copr);
if (ret < 0) {
panel_err("%s failed to update average(ret %d)\n",
__func__, ret);
mutex_unlock(&copr->lock);
return ret;
}
if (!copr_is_enabled(copr)) {
panel_dbg("%s copr disabled\n", __func__);
mutex_unlock(&copr->lock);
return -EIO;
}
pr_debug("%s set roi\n", __func__);
memcpy(®, &copr->props.reg, sizeof(reg));
SET_COPR_REG_GAMMA(copr, false);
for (i = 0; i < size; i++) {
SET_COPR_REG_ROI(copr, &roi[i]);
SET_COPR_REG_E(copr, 0x300, 0, 0);
SET_COPR_REG_EC(copr, 0, 0x300, 0);
panel_set_copr(copr);
ret = panel_get_copr(copr);
if (ret < 0) {
panel_err("%s failed to get copr (ret %d)\n",
__func__, ret);
/* restore r/g/b efficiency & roi */
memcpy(&copr->props.reg, ®, sizeof(copr->props.reg));
mutex_unlock(&copr->lock);
return -EINVAL;
}
out[i * 3 + 0] = props->cur_copr;
out[i * 3 + 1] = props->comp_copr;
SET_COPR_REG_E(copr, 0, 0, 0x300);
SET_COPR_REG_EC(copr, 0, 0, 0);
panel_set_copr(copr);
ret = panel_get_copr(copr);
if (ret < 0) {
panel_err("%s failed to get copr (ret %d)\n",
__func__, ret);
/* restore r/g/b efficiency & roi */
memcpy(&copr->props.reg, ®, sizeof(copr->props.reg));
mutex_unlock(&copr->lock);
return -EINVAL;
}
out[i * 3 + 2] = props->cur_copr;
}
/* restore r/g/b efficiency & roi */
memcpy(&copr->props.reg, ®, sizeof(copr->props.reg));
#ifdef CONFIG_SUPPORT_COPR_AVG
if (copr->props.version == 2) {
ret = panel_do_copr_seqtbl_by_index(copr, COPR_CLR_CNT_ON_SEQ);
if (unlikely(ret < 0))
pr_err("%s, failed to do seqtbl\n", __func__);
msleep(34);
}
#endif
panel_set_copr(copr);
msleep(34);
ret = panel_get_copr(copr);
if (ret < 0) {
panel_err("%s failed to get copr (ret %d)\n",
__func__, ret);
mutex_unlock(&copr->lock);
return -EINVAL;
}
pr_debug("%s restore roi\n", __func__);
/*
* exclude elapsed time of copr roi snapshot reading
* in copr_sum_update. so update last_ts as cur_ts.
*/
ktime_get_ts(&cur_ts);
cur_copr = props->cur_copr;
copr_res_start(copr, cur_copr, cur_ts);
mutex_unlock(&copr->lock);
return 0;
}
int copr_clear_average(struct copr_info *copr)
{
timenval_clear_average(&copr->res);
return 0;
}
/**
* copr_get_average_and_clear - get copr average.
*
* @ copr : copr_info
* @ index : a index of copr result.
*
* Get copr average from sum and elapsed_msec.
* And clears copr_res's sum and elapsed_msec variables.
* This function returns 0 if the average is valid.
* If not this function returns -ERRNO.
*/
int copr_get_average_and_clear(struct copr_info *copr)
{
int avg;
if (unlikely(!copr->props.support))
return -ENODEV;
mutex_lock(&copr->lock);
copr_update_average(copr);
avg = copr->res.avg;
copr_clear_average(copr);
mutex_unlock(&copr->lock);
return avg;
}
static int set_spi_gpios(struct panel_device *panel, int en)
{
int err_num = 0;
struct copr_spi_gpios *gpio_info = &panel->spi_gpio;
panel_info("%s en : %d\n", __func__, en);
if (en) {
if (gpio_direction_output(gpio_info->gpio_sck, 0))
goto set_exit;
err_num++;
if (gpio_direction_input(gpio_info->gpio_miso))
goto set_exit;
err_num++;
if (gpio_direction_output(gpio_info->gpio_mosi, 0))
goto set_exit;
err_num++;
if (gpio_direction_output(gpio_info->gpio_cs, 0))
goto set_exit;
} else {
if (gpio_direction_input(gpio_info->gpio_sck))
goto set_exit;
err_num++;
if (gpio_direction_input(gpio_info->gpio_miso))
goto set_exit;
err_num++;
if (gpio_direction_input(gpio_info->gpio_mosi))
goto set_exit;
err_num++;
if (gpio_direction_input(gpio_info->gpio_cs))
goto set_exit;
}
return 0;
set_exit:
panel_err("%s : failed to gpio : %d\n", __func__, err_num);
return -EIO;
}
static int get_spi_gpios_dt(struct panel_device *panel)
{
int ret = 0;
struct device_node *np;
struct spi_device *spi = panel->spi;
struct copr_spi_gpios *gpio_info = &panel->spi_gpio;
if (!spi || !gpio_info) {
panel_err("%s:spi or gpio_info is null\n", __func__);
goto error_dt;
}
np = spi->master->dev.of_node;
if (!np) {
panel_err("%s:dev_of_node is null\n", __func__);
goto error_dt;
}
gpio_info->gpio_sck = of_get_named_gpio(np, "gpio-sck", 0);
if (gpio_info->gpio_sck < 0) {
panel_err("%s failed to get gpio_sck from dt\n", __func__);
goto error_dt;
}
gpio_info->gpio_miso = of_get_named_gpio(np, "gpio-miso", 0);
if (gpio_info->gpio_miso < 0) {
panel_err("%s failed to get miso from dt\n", __func__);
goto error_dt;
}
gpio_info->gpio_mosi = of_get_named_gpio(np, "gpio-mosi", 0);
if (gpio_info->gpio_mosi < 0) {
panel_err("%s failed to get mosi from dt\n", __func__);
goto error_dt;
}
gpio_info->gpio_cs = of_get_named_gpio(np, "cs-gpios", 0);
if (gpio_info->gpio_cs < 0) {
panel_err("%s failed to get cs from dt\n", __func__);
goto error_dt;
}
error_dt:
return ret;
}
int copr_enable(struct copr_info *copr)
{
struct panel_device *panel = to_panel_device(copr);
struct panel_state *state = &panel->state;
struct copr_properties *props = &copr->props;
int ret;
if (unlikely(!copr->props.support))
return -ENODEV;
if (copr_is_enabled(copr)) {
pr_info("%s already enabled\n", __func__);
return 0;
}
if (set_spi_gpios(panel, 1))
panel_err("%s:failed to set spio gpio\n", __func__);
pr_info("%s +\n", __func__);
atomic_set(&copr->stop, 0);
mutex_lock(&copr->lock);
copr->props.enable = true;
if (state->disp_on == PANEL_DISPLAY_ON) {
/*
* TODO : check whether "copr-set" is includued in "init-seq".
* If COPR_SET_SEQ is included in INIT_SEQ, set state COPR_REG_ON.
* If not, copr state should be COPR_UNINITIALIZED.
*/
props->state = COPR_REG_ON;
}
copr_res_init(copr);
#ifdef CONFIG_SUPPORT_COPR_AVG
if (copr->props.version == 2) {
ret = panel_clear_copr(copr);
if (unlikely(ret < 0))
pr_err("%s, failed to reset copr\n", __func__);
}
#endif
copr_update_average(copr);
mutex_unlock(&copr->lock);
pr_info("%s -\n", __func__);
return 0;
}
int copr_disable(struct copr_info *copr)
{
struct panel_device *panel = to_panel_device(copr);
struct copr_properties *props = &copr->props;
if (unlikely(!copr->props.support))
return -ENODEV;
if (!copr_is_enabled(copr)) {
pr_info("%s already disabled\n", __func__);
return 0;
}
pr_info("%s +\n", __func__);
atomic_set(&copr->stop, 1);
mutex_lock(&copr->lock);
if (copr->props.version < 2)
copr_update_average(copr);
if (props->enable) {
props->enable = false;
props->state = COPR_UNINITIALIZED;
}
mutex_unlock(&copr->lock);
if (set_spi_gpios(panel, 0))
panel_err("%s:failed to set spio gpio\n", __func__);
pr_info("%s -\n", __func__);
return 0;
}
static int copr_thread(void *data)
{
struct copr_info *copr = data;
int last_value;
int ret;
#ifdef CONFIG_SUPPORT_COPR_AVG
bool runnable = (copr->props.version < 2);
#else
bool runnable = true;
#endif
last_value = copr->res.last_value;
while (!kthread_should_stop()) {
ret = wait_event_interruptible(copr->wq.wait,
(atomic_read(&copr->wq.count) > 0) &&
copr_is_enabled(copr) && runnable);
if (last_value != copr->res.last_value)
atomic_dec(&copr->wq.count);
if (!ret) {
mutex_lock(&copr->lock);
copr_update_average(copr);
last_value = copr->res.last_value;
mutex_unlock(&copr->lock);
usleep_range(16660, 16670);
}
}
return 0;
}
static int copr_create_thread(struct copr_info *copr)
{
if (unlikely(!copr->props.support)) {
panel_warn("copr unsupported\n");
return 0;
}
copr->wq.thread = kthread_run(copr_thread, copr, "copr-thread");
if (IS_ERR_OR_NULL(copr->wq.thread)) {
panel_err("%s failed to run copr thread\n", __func__);
copr->wq.thread = NULL;
return PTR_ERR(copr->wq.thread);
}
return 0;
}
static int copr_fb_notifier_callback(struct notifier_block *self,
unsigned long event, void *data)
{
struct copr_info *copr;
struct fb_event *evdata = data;
int fb_blank;
int early_blank;
switch (event) {
case FB_EVENT_BLANK:
early_blank = 0;
break;
case FB_EARLY_EVENT_BLANK:
early_blank = 1;
break;
default:
return 0;
}
copr = container_of(self, struct copr_info, fb_notif);
fb_blank = *(int *)evdata->data;
pr_debug("%s: %d\n", __func__, fb_blank);
if (evdata->info->node != 0)
return 0;
if (unlikely(!copr->props.support))
return 0;
return 0;
}
static int copr_register_fb(struct copr_info *copr)
{
memset(&copr->fb_notif, 0, sizeof(copr->fb_notif));
copr->fb_notif.notifier_call = copr_fb_notifier_callback;
return fb_register_client(&copr->fb_notif);
}
int copr_probe(struct panel_device *panel, struct panel_copr_data *copr_data)
{
struct copr_info *copr;
int i;
if (!panel || !copr_data) {
pr_err("%s panel(%p) or copr_data(%p) not exist\n",
__func__, panel, copr_data);
return -EINVAL;
}
copr = &panel->copr;
atomic_set(&copr->stop, 0);
mutex_lock(&copr->lock);
memcpy(&copr->props.reg, &copr_data->reg, sizeof(struct copr_reg));
copr->props.version = copr_data->version;
copr->seqtbl = copr_data->seqtbl;
copr->nr_seqtbl = copr_data->nr_seqtbl;
copr->maptbl = copr_data->maptbl;
copr->nr_maptbl = copr_data->nr_maptbl;
for (i = 0; i < copr->nr_maptbl; i++)
copr->maptbl[i].pdata = copr;
init_waitqueue_head(&copr->wq.wait);
copr->props.support = true;
copr_register_fb(copr);
get_spi_gpios_dt(panel);
for (i = 0; i < copr->nr_maptbl; i++)
maptbl_init(&copr->maptbl[i]);
if (IS_PANEL_ACTIVE(panel) &&
copr->props.reg.copr_en) {
copr->props.enable = true;
copr->props.state = COPR_UNINITIALIZED;
atomic_set(&copr->wq.count, 5);
copr_create_thread(copr);
}
mutex_unlock(&copr->lock);
pr_info("%s registered successfully\n", __func__);
return 0;
}
|