summaryrefslogtreecommitdiff
path: root/display/panel-google-hk3.c
blob: 8986378675648b3f9e14e6b7b120a23632676bd1 (plain)
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
// SPDX-License-Identifier: GPL-2.0-only
/*
 * MIPI-DSI based HK3 AMOLED LCD panel driver.
 *
 * Copyright (c) 2022 Google LLC
 *
 * 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 <drm/drm_vblank.h>
#include <linux/debugfs.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/thermal.h>
#include <video/mipi_display.h>

#include "include/trace/dpu_trace.h"
#include "include/trace/panel_trace.h"
#include "panel/panel-samsung-drv.h"

/**
 * enum hk3_panel_feature - features supported by this panel
 * @FEAT_HBM: high brightness mode
 * @FEAT_IRC_OFF: IR compensation off state
 * @FEAT_IRC_Z_MODE: IR compensation on state and use Flat Z mode
 * @FEAT_EARLY_EXIT: early exit from a long frame
 * @FEAT_OP_NS: normal speed (not high speed)
 * @FEAT_FRAME_AUTO: automatic (not manual) frame control
 * @FEAT_MAX: placeholder, counter for number of features
 *
 * The following features are correlated, if one or more of them change, the others need
 * to be updated unconditionally.
 */
enum hk3_panel_feature {
	FEAT_HBM = 0,
	FEAT_IRC_OFF,
	FEAT_IRC_Z_MODE,
	FEAT_EARLY_EXIT,
	FEAT_OP_NS,
	FEAT_FRAME_AUTO,
	FEAT_MAX,
};

/**
 * enum hk3_lhbm_brt - local hbm brightness
 * @LHBM_R_COARSE: red coarse
 * @LHBM_GB_COARSE: green and blue coarse
 * @LHBM_R_FINE: red fine
 * @LHBM_G_FINE: green fine
 * @LHBM_B_FINE: blue fine
 * @LHBM_BRT_LEN: local hbm brightness array length
 */
enum hk3_lhbm_brt {
	LHBM_R_COARSE = 0,
	LHBM_GB_COARSE,
	LHBM_R_FINE,
	LHBM_G_FINE,
	LHBM_B_FINE,
	LHBM_BRT_LEN
};
#define LHBM_BRT_CMD_LEN (LHBM_BRT_LEN + 1)

/**
 * enum hk3_lhbm_brt_overdrive_group - lhbm brightness overdrive group number
 * @LHBM_OVERDRIVE_GRP_0_NIT: group number for 0 nit
 * @LHBM_OVERDRIVE_GRP_6_NIT: group number for 0-6 nits
 * @LHBM_OVERDRIVE_GRP_50_NIT: group number for 6-50 nits
 * @LHBM_OVERDRIVE_GRP_300_NIT: group number for 50-300 nits
 * @LHBM_OVERDRIVE_GRP_MAX: maximum group number
 */
enum hk3_lhbm_brt_overdrive_group {
	LHBM_OVERDRIVE_GRP_0_NIT = 0,
	LHBM_OVERDRIVE_GRP_6_NIT,
	LHBM_OVERDRIVE_GRP_50_NIT,
	LHBM_OVERDRIVE_GRP_300_NIT,
	LHBM_OVERDRIVE_GRP_MAX
};

/**
 * enum hk3_material - different materials in HW
 * @MATERIAL_E6: EVT1 material E6
 * @MATERIAL_E7_DOE: EVT1 material E7
 * @MATERIAL_E7: EVT1.1 maetrial E7
 * @MATERIAL_LPC5: EVT1.1 material LPC5
 */
enum hk3_material {
	MATERIAL_E6 = 0,
	MATERIAL_E7_DOE,
	MATERIAL_E7,
	MATERIAL_LPC5
};

struct hk3_lhbm_ctl {
	/** @brt_normal: normal LHBM brightness parameters */
	u8 brt_normal[LHBM_BRT_LEN];
	/** @brt_overdrive: overdrive LHBM brightness parameters */
	u8 brt_overdrive[LHBM_OVERDRIVE_GRP_MAX][LHBM_BRT_LEN];
	/** @overdrived: whether LHBM is overdrived */
	bool overdrived;
	/** @hist_roi_configured: whether LHBM histogram configuration is done */
	bool hist_roi_configured;
};

#define HK3_VREG_STR_SIZE 11
#define HK3_VREG_PARAM_NUM 5

/**
 * HK3_VREG_STR
 * @ctx: exynos_panel struct
 *
 * Expect to have five values for the Vreg parameters:
 * EVT1.1 and earlier: 0x1B
 * DVT1 and later: 0x1A
 */
#define HK3_VREG_STR(ctx) (((ctx)->panel_rev >= PANEL_REV_DVT1) ? "1a1a1a1a1a" : "1b1b1b1b1b")

/**
 * struct hk3_panel - panel specific info
 *
 * This struct maintains hk3 panel specific info. The variables with the prefix hw_ keep
 * track of the features that were actually committed to hardware, and should be modified
 * after sending cmds to panel, i.e. updating hw state.
 */
struct hk3_panel {
	/** @base: base panel struct */
	struct exynos_panel base;
	/** @feat: software or working correlated features, not guaranteed to be effective in panel */
	DECLARE_BITMAP(feat, FEAT_MAX);
	/** @hw_feat: correlated states effective in panel */
	DECLARE_BITMAP(hw_feat, FEAT_MAX);
	/** @hw_vrefresh: vrefresh rate effective in panel */
	u32 hw_vrefresh;
	/** @hw_idle_vrefresh: idle vrefresh rate effective in panel */
	u32 hw_idle_vrefresh;
	/**
	 * @auto_mode_vrefresh: indicates current minimum refresh rate while in auto mode,
	 *			if 0 it means that auto mode is not enabled
	 */
	u32 auto_mode_vrefresh;
	/** @force_changeable_te: force changeable TE (instead of fixed) during early exit */
	bool force_changeable_te;
	/** @force_changeable_te2: force changeable TE (instead of fixed) for monitoring refresh rate */
	bool force_changeable_te2;
	/** @hw_acl_setting: automatic current limiting setting */
	u8 hw_acl_setting;
	/** @hw_dbv: indicate the current dbv, will be zero after sleep in/out */
	u16 hw_dbv;
	/** @hw_za_enabled: whether zonal attenuation is enabled */
	bool hw_za_enabled;
	/** @force_za_off: force to turn off zonal attenuation */
	bool force_za_off;
	/** @lhbm_ctl: lhbm brightness control */
	struct hk3_lhbm_ctl lhbm_ctl;
	/** @material: the material version used in panel */
	enum hk3_material material;
	/** @tz: thermal zone device for reading temperature */
	struct thermal_zone_device *tz;
	/** @hw_temp: the temperature applied into panel */
	u32 hw_temp;
	/**
	 * @pending_temp_update: whether there is pending temperature update. It will be
	 *                       handled in the commit_done function.
	 */
	bool pending_temp_update;
	/**
	 * @is_pixel_off: pixel-off command is sent to panel. Only sending normal-on or resetting
	 *		  panel can recover to normal mode after entering pixel-off state.
	 */
	bool is_pixel_off;
	/** @hw_vreg: the Vreg setting after calling hk3_read_back_vreg() */
	char hw_vreg[HK3_VREG_STR_SIZE];
	/**
	 * @read_vreg: whether need to read back Vreg setting after self_refresh. The Vreg cannot
	 *	       be read right after it's set, so we have to wait for taking effect, but
	 *	       cannot block the main thread.
	 */
	bool read_vreg;
};

#define to_spanel(ctx) container_of(ctx, struct hk3_panel, base)

/* 1344x2992 */
static const struct drm_dsc_config wqhd_pps_config = {
	.line_buf_depth = 9,
	.bits_per_component = 8,
	.convert_rgb = true,
	.slice_width = 672,
	.slice_height = 187,
	.simple_422 = false,
	.pic_width = 1344,
	.pic_height = 2992,
	.rc_tgt_offset_high = 3,
	.rc_tgt_offset_low = 3,
	.bits_per_pixel = 128,
	.rc_edge_factor = 6,
	.rc_quant_incr_limit1 = 11,
	.rc_quant_incr_limit0 = 11,
	.initial_xmit_delay = 512,
	.initial_dec_delay = 592,
	.block_pred_enable = true,
	.first_line_bpg_offset = 12,
	.initial_offset = 6144,
	.rc_buf_thresh = {
		14, 28, 42, 56,
		70, 84, 98, 105,
		112, 119, 121, 123,
		125, 126
	},
	.rc_range_params = {
		{.range_min_qp = 0, .range_max_qp = 4, .range_bpg_offset = 2},
		{.range_min_qp = 0, .range_max_qp = 4, .range_bpg_offset = 0},
		{.range_min_qp = 1, .range_max_qp = 5, .range_bpg_offset = 0},
		{.range_min_qp = 1, .range_max_qp = 6, .range_bpg_offset = 62},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 60},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 58},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 8, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 9, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 10, .range_bpg_offset = 54},
		{.range_min_qp = 5, .range_max_qp = 11, .range_bpg_offset = 54},
		{.range_min_qp = 5, .range_max_qp = 12, .range_bpg_offset = 52},
		{.range_min_qp = 5, .range_max_qp = 13, .range_bpg_offset = 52},
		{.range_min_qp = 7, .range_max_qp = 13, .range_bpg_offset = 52},
		{.range_min_qp = 13, .range_max_qp = 15, .range_bpg_offset = 52}
	},
	.rc_model_size = 8192,
	.flatness_min_qp = 3,
	.flatness_max_qp = 12,
	.initial_scale_value = 32,
	.scale_decrement_interval = 9,
	.scale_increment_interval = 5177,
	.nfl_bpg_offset = 133,
	.slice_bpg_offset = 112,
	.final_offset = 4336,
	.vbr_enable = false,
	.slice_chunk_size = 672,
	.dsc_version_minor = 1,
	.dsc_version_major = 1,
	.native_422 = false,
	.native_420 = false,
	.second_line_bpg_offset = 0,
	.nsl_bpg_offset = 0,
	.second_line_offset_adj = 0,
};

/* 1008x2244 */
static const struct drm_dsc_config fhd_pps_config = {
	.line_buf_depth = 9,
	.bits_per_component = 8,
	.convert_rgb = true,
	.slice_width = 504,
	.slice_height = 187,
	.simple_422 = false,
	.pic_width = 1008,
	.pic_height = 2244,
	.rc_tgt_offset_high = 3,
	.rc_tgt_offset_low = 3,
	.bits_per_pixel = 128,
	.rc_edge_factor = 6,
	.rc_quant_incr_limit1 = 11,
	.rc_quant_incr_limit0 = 11,
	.initial_xmit_delay = 512,
	.initial_dec_delay = 508,
	.block_pred_enable = true,
	.first_line_bpg_offset = 12,
	.initial_offset = 6144,
	.rc_buf_thresh = {
		14, 28, 42, 56,
		70, 84, 98, 105,
		112, 119, 121, 123,
		125, 126
	},
	.rc_range_params = {
		{.range_min_qp = 0, .range_max_qp = 4, .range_bpg_offset = 2},
		{.range_min_qp = 0, .range_max_qp = 4, .range_bpg_offset = 0},
		{.range_min_qp = 1, .range_max_qp = 5, .range_bpg_offset = 0},
		{.range_min_qp = 1, .range_max_qp = 6, .range_bpg_offset = 62},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 60},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 58},
		{.range_min_qp = 3, .range_max_qp = 7, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 8, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 9, .range_bpg_offset = 56},
		{.range_min_qp = 3, .range_max_qp = 10, .range_bpg_offset = 54},
		{.range_min_qp = 5, .range_max_qp = 11, .range_bpg_offset = 54},
		{.range_min_qp = 5, .range_max_qp = 12, .range_bpg_offset = 52},
		{.range_min_qp = 5, .range_max_qp = 13, .range_bpg_offset = 52},
		{.range_min_qp = 7, .range_max_qp = 13, .range_bpg_offset = 52},
		{.range_min_qp = 13, .range_max_qp = 15, .range_bpg_offset = 52}
	},
	.rc_model_size = 8192,
	.flatness_min_qp = 3,
	.flatness_max_qp = 12,
	.initial_scale_value = 32,
	.scale_decrement_interval = 7,
	.scale_increment_interval = 4482,
	.nfl_bpg_offset = 133,
	.slice_bpg_offset = 150,
	.final_offset = 4336,
	.vbr_enable = false,
	.slice_chunk_size = 504,
	.dsc_version_minor = 1,
	.dsc_version_major = 1,
	.native_422 = false,
	.native_420 = false,
	.second_line_bpg_offset = 0,
	.nsl_bpg_offset = 0,
	.second_line_offset_adj = 0,
};

#define HK3_WRCTRLD_DIMMING_BIT    0x08
#define HK3_WRCTRLD_BCTRL_BIT      0x20
#define HK3_WRCTRLD_HBM_BIT        0xC0
#define HK3_WRCTRLD_LOCAL_HBM_BIT  0x10

#define HK3_TE2_CHANGEABLE 0x04
#define HK3_TE2_FIXED      0x51
#define HK3_TE2_RISING_EDGE_OFFSET 0x10
#define HK3_TE2_FALLING_EDGE_OFFSET 0x30
#define HK3_TE2_FALLING_EDGE_OFFSET_NS 0x25

#define HK3_TE_USEC_AOD 693
#define HK3_TE_USEC_120HZ 273
#define HK3_TE_USEC_60HZ_HS 8500
#define HK3_TE_USEC_60HZ_NS 546
#define HK3_TE_PERIOD_DELTA_TOLERANCE_USEC 2000

#define MIPI_DSI_FREQ_DEFAULT 1368
#define MIPI_DSI_FREQ_ALTERNATIVE 1346

#define PROJECT "HK3"

static const u8 unlock_cmd_f0[] = { 0xF0, 0x5A, 0x5A };
static const u8 lock_cmd_f0[]   = { 0xF0, 0xA5, 0xA5 };
static const u8 freq_update[] = { 0xF7, 0x0F };
static const u8 lhbm_brightness_index[] = { 0xB0, 0x03, 0x21, 0x95 };
static const u8 lhbm_brightness_reg = 0x95;
static const u8 pixel_off[] = { 0x22 };
static const u8 sync_begin[] = { 0xE4, 0x00, 0x2C, 0x2C, 0xA2, 0x00, 0x00 };
static const u8 sync_end[] = { 0xE4, 0x00, 0x2C, 0x2C, 0x82, 0x00, 0x00 };
static const u8 aod_on[] = { MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x24 };
static const u8 aod_off[] = { MIPI_DCS_WRITE_CONTROL_DISPLAY, 0x20 };
/* 50 nits */
static const u8 aod_dbv[] = { MIPI_DCS_SET_DISPLAY_BRIGHTNESS, 0x03, 0x55 };

static const struct exynos_dsi_cmd hk3_lp_low_cmds[] = {
	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	/* AOD Low Mode, 10nit */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x52, 0x94),
	EXYNOS_DSI_CMD_SEQ(0x94, 0x01, 0x07, 0x6A, 0x02),
	EXYNOS_DSI_CMD0(lock_cmd_f0),
};

static const struct exynos_dsi_cmd hk3_lp_high_cmds[] = {
	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	/* AOD High Mode, 50nit */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x52, 0x94),
	EXYNOS_DSI_CMD_SEQ(0x94, 0x00, 0x07, 0x6A, 0x02),
	EXYNOS_DSI_CMD0(lock_cmd_f0),
};

static const struct exynos_binned_lp hk3_binned_lp[] = {
	/* low threshold 40 nits */
	BINNED_LP_MODE_TIMING("low", 766, hk3_lp_low_cmds,
			      HK3_TE2_RISING_EDGE_OFFSET, HK3_TE2_FALLING_EDGE_OFFSET),
	BINNED_LP_MODE_TIMING("high", 3307, hk3_lp_high_cmds,
			      HK3_TE2_RISING_EDGE_OFFSET, HK3_TE2_FALLING_EDGE_OFFSET)
};

static inline bool is_in_comp_range(int temp)
{
	return (temp >= 10 && temp <= 49);
}

/* Read temperature and apply appropriate gain into DDIC for burn-in compensation if needed */
static void hk3_update_disp_therm(struct exynos_panel *ctx)
{
	/* temperature*1000 in celsius */
	int temp, ret;
	struct hk3_panel *spanel = to_spanel(ctx);

	if (IS_ERR_OR_NULL(spanel->tz))
		return;

	if (ctx->panel_rev < PANEL_REV_EVT1_1 || ctx->panel_state != PANEL_STATE_NORMAL)
		return;

	spanel->pending_temp_update = false;

	ret = thermal_zone_get_temp(spanel->tz, &temp);
	if (ret) {
		dev_err(ctx->dev, "%s: fail to read temperature ret:%d\n", __func__, ret);
		return;
	}

	temp = DIV_ROUND_CLOSEST(temp, 1000);
	dev_dbg(ctx->dev, "%s: temp=%d\n", __func__, temp);
	if (temp == spanel->hw_temp || !is_in_comp_range(temp))
		return;

	dev_dbg(ctx->dev, "%s: apply gain into ddic at %ddeg c\n", __func__, temp);

	DPU_ATRACE_BEGIN(__func__);
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x03, 0x67);
	EXYNOS_DCS_BUF_ADD(ctx, 0x67, temp);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	DPU_ATRACE_END(__func__);

	spanel->hw_temp = temp;
}

static u8 hk3_get_te2_option(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	if (!ctx || !ctx->current_mode || spanel->force_changeable_te2)
		return HK3_TE2_CHANGEABLE;

	if (ctx->current_mode->exynos_mode.is_lp_mode ||
	    (test_bit(FEAT_EARLY_EXIT, spanel->feat) &&
		spanel->auto_mode_vrefresh < 30))
		return HK3_TE2_FIXED;

	return HK3_TE2_CHANGEABLE;
}

static void hk3_update_te2_internal(struct exynos_panel *ctx, bool lock)
{
	struct exynos_panel_te2_timing timing = {
		.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
		.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
	};
	u32 rising, falling;
	struct hk3_panel *spanel = to_spanel(ctx);
	u8 option = hk3_get_te2_option(ctx);
	u8 idx;

	if (!ctx)
		return;

	/* skip TE2 update if going through RRS */
	if (ctx->mode_in_progress == MODE_RES_IN_PROGRESS ||
	    ctx->mode_in_progress == MODE_RES_AND_RR_IN_PROGRESS) {
		dev_dbg(ctx->dev, "%s: RRS in progress, skip\n", __func__);
		return;
	}

	if (test_bit(FEAT_OP_NS, spanel->feat)) {
		rising = HK3_TE2_RISING_EDGE_OFFSET;
		falling = HK3_TE2_FALLING_EDGE_OFFSET_NS;
	} else {
		if (exynos_panel_get_current_mode_te2(ctx, &timing)) {
			dev_dbg(ctx->dev, "failed to get TE2 timng\n");
			return;
		}
		rising = timing.rising_edge;
		falling = timing.falling_edge;
	}

	ctx->te2.option = (option == HK3_TE2_FIXED) ? TE2_OPT_FIXED : TE2_OPT_CHANGEABLE;

	dev_dbg(ctx->dev,
		"TE2 updated: %s mode, option %s, idle %s, rising=0x%X falling=0x%X\n",
		test_bit(FEAT_OP_NS, spanel->feat) ? "NS" : "HS",
		(option == HK3_TE2_CHANGEABLE) ? "changeable" : "fixed",
		ctx->panel_idle_vrefresh ? "active" : "inactive",
		rising, falling);

	if (lock)
		EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x42, 0xF2);
	EXYNOS_DCS_BUF_ADD(ctx, 0xF2, 0x0D);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x01, 0xB9);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, option);
	idx = option == HK3_TE2_FIXED ? 0x22 : 0x1E;
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, idx, 0xB9);
	if (option == HK3_TE2_FIXED) {
		EXYNOS_DCS_BUF_ADD(ctx, 0xB9, (rising >> 8) & 0xF, rising & 0xFF,
			(falling >> 8) & 0xF, falling & 0xFF,
			(rising >> 8) & 0xF, rising & 0xFF,
			(falling >> 8) & 0xF, falling & 0xFF);
	} else {
		EXYNOS_DCS_BUF_ADD(ctx, 0xB9, (rising >> 8) & 0xF, rising & 0xFF,
			(falling >> 8) & 0xF, falling & 0xFF);
	}
	if (lock)
		EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
}

static void hk3_update_te2(struct exynos_panel *ctx)
{
	hk3_update_te2_internal(ctx, true);
}

static inline bool is_auto_mode_allowed(struct exynos_panel *ctx)
{
	/* don't want to enable auto mode/early exit during dimming on */
	if (ctx->dimming_on)
		return false;

	if (ctx->idle_delay_ms) {
		const unsigned int delta_ms = panel_get_idle_time_delta(ctx);

		if (delta_ms < ctx->idle_delay_ms)
			return false;
	}

	return ctx->panel_idle_enabled;
}

static u32 hk3_get_min_idle_vrefresh(struct exynos_panel *ctx,
				     const struct exynos_panel_mode *pmode)
{
	const int vrefresh = drm_mode_vrefresh(&pmode->mode);
	int min_idle_vrefresh = ctx->min_vrefresh;

	if ((min_idle_vrefresh < 0) || !is_auto_mode_allowed(ctx))
		return 0;

	if (min_idle_vrefresh <= 1)
		min_idle_vrefresh = 1;
	else if (min_idle_vrefresh <= 10)
		min_idle_vrefresh = 10;
	else if (min_idle_vrefresh <= 30)
		min_idle_vrefresh = 30;
	else
		return 0;

	if (min_idle_vrefresh >= vrefresh) {
		dev_dbg(ctx->dev, "min idle vrefresh (%d) higher than target (%d)\n",
				min_idle_vrefresh, vrefresh);
		return 0;
	}

	dev_dbg(ctx->dev, "%s: min_idle_vrefresh %d\n", __func__, min_idle_vrefresh);

	return min_idle_vrefresh;
}

static void hk3_set_panel_feat(struct exynos_panel *ctx,
	const u32 vrefresh, const u32 idle_vrefresh, const unsigned long *feat, bool enforce)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	u8 val;
	DECLARE_BITMAP(changed_feat, FEAT_MAX);

	if (enforce) {
		bitmap_fill(changed_feat, FEAT_MAX);
	} else {
		bitmap_xor(changed_feat, feat, spanel->hw_feat, FEAT_MAX);
		if (bitmap_empty(changed_feat, FEAT_MAX) &&
			vrefresh == spanel->hw_vrefresh &&
			idle_vrefresh == spanel->hw_idle_vrefresh) {
			dev_dbg(ctx->dev, "%s: no changes, skip update\n", __func__);
			return;
		}
	}

	spanel->hw_vrefresh = vrefresh;
	spanel->hw_idle_vrefresh = idle_vrefresh;
	bitmap_copy(spanel->hw_feat, feat, FEAT_MAX);
	dev_dbg(ctx->dev,
		"op=%s ee=%s hbm=%s irc=%s fi=%s fps=%u idle_fps=%u\n",
		test_bit(FEAT_OP_NS, feat) ? "ns" : "hs",
		test_bit(FEAT_EARLY_EXIT, feat) ? "on" : "off",
		test_bit(FEAT_HBM, feat) ? "on" : "off",
		ctx->panel_rev >= PANEL_REV_EVT1 ?
			(test_bit(FEAT_IRC_Z_MODE, feat) ? "flat_z" : "flat") :
			(test_bit(FEAT_IRC_OFF, feat) ? "off" : "on"),
		test_bit(FEAT_FRAME_AUTO, feat) ? "auto" : "manual",
		vrefresh,
		idle_vrefresh);

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);

	/* TE setting */
	if (test_bit(FEAT_EARLY_EXIT, changed_feat) ||
		test_bit(FEAT_OP_NS, changed_feat)) {
		if (test_bit(FEAT_EARLY_EXIT, feat) && !spanel->force_changeable_te) {
			/* Fixed TE */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x51);
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x02, 0xB9);
			val = test_bit(FEAT_OP_NS, feat) ? 0x01 : 0x00;
			EXYNOS_DCS_BUF_ADD(ctx, 0xB9, val);
		} else {
			/* Changeable TE */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x04);
			/* Changeable TE width setting and frequency */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x04, 0xB9);
			/* width 273us in normal mode */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x0B, 0xBB, 0x00, 0x2F);
		}
	}

	/* TE2 setting */
	if (test_bit(FEAT_OP_NS, changed_feat))
		hk3_update_te2_internal(ctx, false);

	/*
	 * HBM IRC setting
	 *
	 * Description: after EVT1, IRC will be always on. "Flat mode" is used to
	 * replace IRC on for normal mode and HDR video, and "Flat Z mode" is used
	 * to replace IRC off for sunlight environment.
	 */
	if (ctx->panel_rev >= PANEL_REV_EVT1) {
		if (test_bit(FEAT_IRC_Z_MODE, changed_feat)) {
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x02, 0x00, 0x92);
			if (test_bit(FEAT_IRC_Z_MODE, feat)) {
				if (spanel->material == MATERIAL_E6) {
					EXYNOS_DCS_BUF_ADD(ctx, 0x92, 0xBE, 0x98);
					EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x02, 0xF3, 0x68);
					EXYNOS_DCS_BUF_ADD(ctx, 0x68, 0x97, 0x87, 0x87, 0xFB, 0xFD, 0xF1);
				} else {
					EXYNOS_DCS_BUF_ADD(ctx, 0x92, 0xF1, 0xC1);
					EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x02, 0xF3, 0x68);
					EXYNOS_DCS_BUF_ADD(ctx, 0x68, 0x82, 0x70, 0x23, 0x91, 0x88, 0x3C);
				}
			} else {
				EXYNOS_DCS_BUF_ADD(ctx, 0x92, 0x00, 0x00);
				EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x02, 0xF3, 0x68);
				if (spanel->material == MATERIAL_E6)
					EXYNOS_DCS_BUF_ADD(ctx, 0x68, 0x71, 0x81, 0x59, 0x90, 0xA2, 0x80);
				else
					EXYNOS_DCS_BUF_ADD(ctx, 0x68, 0x77, 0x81, 0x23, 0x8C, 0x99, 0x3C);
			}
		}
	} else {
		if (test_bit(FEAT_IRC_OFF, changed_feat)) {
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x01, 0x9B, 0x92);
			val = test_bit(FEAT_IRC_OFF, feat) ? 0x07 : 0x27;
			EXYNOS_DCS_BUF_ADD(ctx, 0x92, val);
		}
	}

	/*
	 * Operating Mode: NS or HS
	 *
	 * Description: the configs could possibly be overrided by frequency setting,
	 * depending on FI mode.
	 */
	if (test_bit(FEAT_OP_NS, changed_feat)) {
		/* mode set */
		EXYNOS_DCS_BUF_ADD(ctx, 0xF2, 0x01);
		val = test_bit(FEAT_OP_NS, feat) ? 0x18 : 0x00;
		EXYNOS_DCS_BUF_ADD(ctx, 0x60, val);
	}

	/*
	 * Note: the following command sequence should be sent as a whole if one of panel
	 * state defined by enum panel_state changes or at turning on panel, or unexpected
	 * behaviors will be seen, e.g. black screen, flicker.
	 */

	/*
	 * Early-exit: enable or disable
	 *
	 * Description: early-exit sequence overrides some configs HBM set.
	 */
	if (test_bit(FEAT_EARLY_EXIT, feat)) {
		if (test_bit(FEAT_HBM, feat))
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21, 0x00, 0x83, 0x03, 0x01);
		else
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21, 0x01, 0x83, 0x03, 0x03);
	} else {
		if (test_bit(FEAT_HBM, feat))
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21, 0x80, 0x83, 0x03, 0x01);
		else
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21, 0x81, 0x83, 0x03, 0x03);
	}
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x10, 0xBD);
	val = test_bit(FEAT_EARLY_EXIT, feat) ? 0x22 : 0x00;
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, val);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x82, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, val, val, val, val);
	val = test_bit(FEAT_OP_NS, feat) ? 0x4E : 0x1E;
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, val, 0xBD);
	if (test_bit(FEAT_HBM, feat)) {
		if (test_bit(FEAT_OP_NS, feat))
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00, 0x02,
				0x00, 0x04, 0x00, 0x0A, 0x00, 0x16, 0x00, 0x76);
		else
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00, 0x01,
				0x00, 0x03, 0x00, 0x0B, 0x00, 0x17, 0x00, 0x77);
	} else {
		if (test_bit(FEAT_OP_NS, feat))
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00, 0x04,
				0x00, 0x08, 0x00, 0x14, 0x00, 0x2C, 0x00, 0xEC);
		else
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00, 0x02,
				0x00, 0x06, 0x00, 0x16, 0x00, 0x2E, 0x00, 0xEE);
	}

	/*
	 * Frequency setting: FI, frequency, idle frequency
	 *
	 * Description: this sequence possibly overrides some configs early-exit
	 * and operation set, depending on FI mode.
	 */
	if (test_bit(FEAT_FRAME_AUTO, feat)) {
		if (test_bit(FEAT_OP_NS, feat)) {
			/* threshold setting */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x0C, 0xBD);
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00);
		} else {
			/* initial frequency */
			EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x92, 0xBD);
			if (vrefresh == 60) {
				val = test_bit(FEAT_HBM, feat) ? 0x01 : 0x02;
			} else {
				if (vrefresh != 120)
					dev_warn(ctx->dev, "%s: unsupported init freq %d (hs)\n",
						 __func__, vrefresh);
				/* 120Hz */
				val = 0x00;
			}
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, val);
		}
		/* target frequency */
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x12, 0xBD);
		if (test_bit(FEAT_OP_NS, feat)) {
			if (idle_vrefresh == 30) {
				val = test_bit(FEAT_HBM, feat) ? 0x02 : 0x04;
			} else if (idle_vrefresh == 10) {
				val = test_bit(FEAT_HBM, feat) ? 0x0A : 0x14;
			} else {
				if (idle_vrefresh != 1)
					dev_warn(ctx->dev, "%s: unsupported target freq %d (ns)\n",
						 __func__, idle_vrefresh);
				/* 1Hz */
				val = test_bit(FEAT_HBM, feat) ? 0x76 : 0xEC;
			}
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, val);
		} else {
			if (idle_vrefresh == 30) {
				val = test_bit(FEAT_HBM, feat) ? 0x03 : 0x06;
			} else if (idle_vrefresh == 10) {
				val = test_bit(FEAT_HBM, feat) ? 0x0B : 0x16;
			} else {
				if (idle_vrefresh != 1)
					dev_warn(ctx->dev, "%s: unsupported target freq %d (hs)\n",
						 __func__, idle_vrefresh);
				/* 1Hz */
				val = test_bit(FEAT_HBM, feat) ? 0x77 : 0xEE;
			}
			EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, val);
		}
		/* step setting */
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x9E, 0xBD);
		if (test_bit(FEAT_OP_NS, feat)) {
			if (test_bit(FEAT_HBM, feat))
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x02, 0x00, 0x0A, 0x00, 0x00);
			else
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x04, 0x00, 0x14, 0x00, 0x00);
		} else {
			if (test_bit(FEAT_HBM, feat))
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x01, 0x00, 0x03, 0x00, 0x0B);
			else
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x02, 0x00, 0x06, 0x00, 0x16);
		}
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0xAE, 0xBD);
		if (test_bit(FEAT_OP_NS, feat)) {
			if (idle_vrefresh == 30) {
				/* 60Hz -> 30Hz idle */
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00);
			} else if (idle_vrefresh == 10) {
				/* 60Hz -> 10Hz idle */
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x01, 0x00, 0x00);
			} else {
				if (idle_vrefresh != 1)
					dev_warn(ctx->dev, "%s: unsupported freq step to %d (ns)\n",
						 __func__, idle_vrefresh);
				/* 60Hz -> 1Hz idle */
				EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x01, 0x03, 0x00);
			}
		} else {
			if (vrefresh == 60) {
				if (idle_vrefresh == 30) {
					/* 60Hz -> 30Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x01, 0x00, 0x00);
				} else if (idle_vrefresh == 10) {
					/* 60Hz -> 10Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x01, 0x01, 0x00);
				} else {
					if (idle_vrefresh != 1)
						dev_warn(ctx->dev, "%s: unsupported freq step to %d (hs)\n",
							 __func__, vrefresh);
					/* 60Hz -> 1Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x01, 0x01, 0x03);
				}
			} else {
				if (vrefresh != 120)
					dev_warn(ctx->dev, "%s: unsupported freq step from %d (hs)\n",
						 __func__, vrefresh);
				if (idle_vrefresh == 30) {
					/* 120Hz -> 30Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x00, 0x00);
				} else if (idle_vrefresh == 10) {
					/* 120Hz -> 10Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x03, 0x00);
				} else {
					if (idle_vrefresh != 1)
						dev_warn(ctx->dev, "%s: unsupported freq step to %d (hs)\n",
						 __func__, idle_vrefresh);
					/* 120Hz -> 1Hz idle */
					EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x01, 0x03);
				}
			}
		}
		EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0xA3);
	} else { /* manual */
		EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21);
		if (test_bit(FEAT_OP_NS, feat)) {
			if (vrefresh == 1) {
				val = 0x1F;
			} else if (vrefresh == 5) {
				val = 0x1E;
			} else if (vrefresh == 10) {
				val = 0x1B;
			} else if (vrefresh == 30) {
				val = 0x19;
			} else {
				if (vrefresh != 60)
					dev_warn(ctx->dev,
						 "%s: unsupported manual freq %d (ns)\n",
						 __func__, vrefresh);
				/* 60Hz */
				val = 0x18;
			}
		} else {
			if (vrefresh == 1) {
				val = 0x07;
			} else if (vrefresh == 5) {
				val = 0x06;
			} else if (vrefresh == 10) {
				val = 0x03;
			} else if (vrefresh == 30) {
				val = 0x02;
			} else if (vrefresh == 60) {
				val = 0x01;
			} else {
				if (vrefresh != 120)
					dev_warn(ctx->dev,
						 "%s: unsupported manual freq %d (hs)\n",
						 __func__, vrefresh);
				/* 120Hz */
				val = 0x00;
			}
		}
		EXYNOS_DCS_BUF_ADD(ctx, 0x60, val);
	}

	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);;
}

/**
 * hk3_disable_panel_feat - set the panel at the state of powering up except refresh rate
 * @ctx: exynos_panel struct
 * @vrefresh: refresh rate
 * This function disables HBM, switches to HS, sets manual mode and changeable TE.
 */
static void hk3_disable_panel_feat(struct exynos_panel *ctx, u32 vrefresh)
{
	DECLARE_BITMAP(feat, FEAT_MAX);

	bitmap_zero(feat, FEAT_MAX);
	hk3_set_panel_feat(ctx, vrefresh, 0, feat, true);
}

static void hk3_update_panel_feat(struct exynos_panel *ctx, u32 vrefresh, bool enforce)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	hk3_set_panel_feat(ctx, vrefresh, spanel->auto_mode_vrefresh, spanel->feat, enforce);
}

static void hk3_update_refresh_mode(struct exynos_panel *ctx,
					const struct exynos_panel_mode *pmode,
					const u32 idle_vrefresh)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	u32 vrefresh = drm_mode_vrefresh(&pmode->mode);

	/*
	 * Skip idle update if going through RRS without refresh rate change. If
	 * we're switching resolution and refresh rate in the same atomic commit
	 * (MODE_RES_AND_RR_IN_PROGRESS), we shouldn't skip the update to
	 * ensure the refresh rate will be set correctly to avoid problems.
	 */
	if (ctx->mode_in_progress == MODE_RES_IN_PROGRESS) {
		dev_dbg(ctx->dev, "%s: RRS in progress without RR change, skip\n", __func__);
		notify_panel_mode_changed(ctx, false);
		return;
	}

	dev_dbg(ctx->dev, "%s: mode: %s set idle_vrefresh: %u\n", __func__,
		pmode->mode.name, idle_vrefresh);

	if (idle_vrefresh)
		set_bit(FEAT_FRAME_AUTO, spanel->feat);
	else
		clear_bit(FEAT_FRAME_AUTO, spanel->feat);

	if (vrefresh == 120 || idle_vrefresh)
		set_bit(FEAT_EARLY_EXIT, spanel->feat);
	else
		clear_bit(FEAT_EARLY_EXIT, spanel->feat);

	spanel->auto_mode_vrefresh = idle_vrefresh;
	/*
	 * Note: when mode is explicitly set, panel performs early exit to get out
	 * of idle at next vsync, and will not back to idle until not seeing new
	 * frame traffic for a while. If idle_vrefresh != 0, try best to guess what
	 * panel_idle_vrefresh will be soon, and hk3_update_idle_state() in
	 * new frame commit will correct it if the guess is wrong.
	 */
	ctx->panel_idle_vrefresh = idle_vrefresh;
	hk3_update_panel_feat(ctx, vrefresh, false);

	notify_panel_mode_changed(ctx, false);

	dev_dbg(ctx->dev, "%s: display state is notified\n", __func__);
}

static void hk3_change_frequency(struct exynos_panel *ctx,
				 const struct exynos_panel_mode *pmode)
{
	u32 vrefresh = drm_mode_vrefresh(&pmode->mode);
	u32 idle_vrefresh = 0;

	if (vrefresh > ctx->op_hz) {
		/* resolution may has been changed but refresh rate */
		if (ctx->mode_in_progress == MODE_RES_AND_RR_IN_PROGRESS)
			notify_panel_mode_changed(ctx, false);
		dev_err(ctx->dev,
		"invalid freq setting: op_hz=%u, vrefresh=%u\n",
		ctx->op_hz, vrefresh);
		return;
	}

	if (pmode->idle_mode == IDLE_MODE_ON_INACTIVITY)
		idle_vrefresh = hk3_get_min_idle_vrefresh(ctx, pmode);

	hk3_update_refresh_mode(ctx, pmode, idle_vrefresh);

	dev_dbg(ctx->dev, "change to %u hz\n", vrefresh);
}

static void hk3_panel_idle_notification(struct exynos_panel *ctx,
		u32 display_id, u32 vrefresh, u32 idle_te_vrefresh)
{
	char event_string[64];
	char *envp[] = { event_string, NULL };
	struct drm_device *dev = ctx->bridge.dev;

	if (!dev) {
		dev_warn(ctx->dev, "%s: drm_device is null\n", __func__);
	} else {
		snprintf(event_string, sizeof(event_string),
			"PANEL_IDLE_ENTER=%u,%u,%u", display_id, vrefresh, idle_te_vrefresh);
		kobject_uevent_env(&dev->primary->kdev->kobj, KOBJ_CHANGE, envp);
	}
}

static void hk3_wait_one_vblank(struct exynos_panel *ctx)
{
	struct drm_crtc *crtc = NULL;

	if (ctx->exynos_connector.base.state)
		crtc = ctx->exynos_connector.base.state->crtc;

	DPU_ATRACE_BEGIN(__func__);
	if (crtc) {
		int ret = drm_crtc_vblank_get(crtc);

		if (!ret) {
			drm_crtc_wait_one_vblank(crtc);
			drm_crtc_vblank_put(crtc);
		} else {
			usleep_range(8350, 8500);
		}
	} else {
		usleep_range(8350, 8500);
	}
	DPU_ATRACE_END(__func__);
}

static void hk3_read_back_vreg(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
	char buf[HK3_VREG_PARAM_NUM] = {0};
	int ret;

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD_AND_FLUSH(ctx, 0xB0, 0x00, 0x31, 0xF4);
	ret = mipi_dsi_dcs_read(dsi, 0xF4, buf, HK3_VREG_PARAM_NUM);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	if (ret != HK3_VREG_PARAM_NUM) {
		dev_warn(ctx->dev, "unable to read vreg setting (%d)\n", ret);
	} else {
		exynos_bin2hex(buf, HK3_VREG_PARAM_NUM,
			       spanel->hw_vreg, sizeof(spanel->hw_vreg));
		if (!strcmp(spanel->hw_vreg, HK3_VREG_STR(ctx)))
			dev_dbg(ctx->dev, "normal vreg: %s\n", spanel->hw_vreg);
		else
			dev_warn(ctx->dev, "abnormal vreg: %s (expect %s)\n",
				 spanel->hw_vreg, HK3_VREG_STR(ctx));
	}

	spanel->read_vreg = false;
}

static bool hk3_set_self_refresh(struct exynos_panel *ctx, bool enable)
{
	const struct exynos_panel_mode *pmode = ctx->current_mode;
	struct hk3_panel *spanel = to_spanel(ctx);
	u32 idle_vrefresh;

	dev_dbg(ctx->dev, "%s: %d\n", __func__, enable);

	if (unlikely(!pmode))
		return false;

	if (enable && spanel->read_vreg)
		hk3_read_back_vreg(ctx);

	/* self refresh is not supported in lp mode since that always makes use of early exit */
	if (pmode->exynos_mode.is_lp_mode) {
		/* set 1Hz while self refresh is active, otherwise clear it */
		ctx->panel_idle_vrefresh = enable ? 1 : 0;
		notify_panel_mode_changed(ctx, true);
		return false;
	}

	if (spanel->pending_temp_update && enable)
		hk3_update_disp_therm(ctx);

	idle_vrefresh = hk3_get_min_idle_vrefresh(ctx, pmode);

	if (pmode->idle_mode != IDLE_MODE_ON_SELF_REFRESH) {
		/*
		 * if idle mode is on inactivity, may need to update the target fps for auto mode,
		 * or switch to manual mode if idle should be disabled (idle_vrefresh=0)
		 */
		if ((pmode->idle_mode == IDLE_MODE_ON_INACTIVITY) &&
			(spanel->auto_mode_vrefresh != idle_vrefresh)) {
			hk3_update_refresh_mode(ctx, pmode, idle_vrefresh);
			return true;
		}
		return false;
	}

	if (!enable)
		idle_vrefresh = 0;

	/* if there's no change in idle state then skip cmds */
	if (ctx->panel_idle_vrefresh == idle_vrefresh)
		return false;

	DPU_ATRACE_BEGIN(__func__);
	hk3_update_refresh_mode(ctx, pmode, idle_vrefresh);

	if (idle_vrefresh) {
		const int vrefresh = drm_mode_vrefresh(&pmode->mode);

		hk3_panel_idle_notification(ctx, 0, vrefresh, 120);
	} else if (ctx->panel_need_handle_idle_exit) {
		/*
		 * after exit idle mode with fixed TE at non-120hz, TE may still keep at 120hz.
		 * If any layer that already be assigned to DPU that can't be handled at 120hz,
		 * panel_need_handle_idle_exit will be set then we need to wait one vblank to
		 * avoid underrun issue.
		 */
		dev_dbg(ctx->dev, "wait one vblank after exit idle\n");
		hk3_wait_one_vblank(ctx);
	}

	DPU_ATRACE_END(__func__);

	return true;
}

static void hk3_update_lhbm_hist_config(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	struct hk3_lhbm_ctl *ctl = &spanel->lhbm_ctl;
	const struct exynos_panel_mode *pmode = ctx->current_mode;
	const struct drm_display_mode *mode;
	int d = 766, r = 115;

	if (ctl->hist_roi_configured)
		return;

	if (!pmode) {
		dev_err(ctx->dev, "no current mode set\n");
		return;
	}
	mode = &pmode->mode;
	if (mode->hdisplay == 1008) {
		d = 575;
		r = 87;
	}
	if (!exynos_drm_connector_set_lhbm_hist(&ctx->exynos_connector,
		mode->hdisplay, mode->vdisplay, d, r)) {
		ctl->hist_roi_configured = true;
		dev_info(ctx->dev, "configure lhbm hist: %d %d %d %d\n",
			mode->hdisplay, mode->vdisplay, d, r);
	}
}

static int hk3_atomic_check(struct exynos_panel *ctx, struct drm_atomic_state *state)
{
	struct drm_connector *conn = &ctx->exynos_connector.base;
	struct drm_connector_state *new_conn_state = drm_atomic_get_new_connector_state(state, conn);
	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
	struct hk3_panel *spanel = to_spanel(ctx);

	hk3_update_lhbm_hist_config(ctx);

	if (!ctx->current_mode || drm_mode_vrefresh(&ctx->current_mode->mode) == 120 ||
	    !new_conn_state || !new_conn_state->crtc)
		return 0;

	new_crtc_state = drm_atomic_get_new_crtc_state(state, new_conn_state->crtc);
	old_crtc_state = drm_atomic_get_old_crtc_state(state, new_conn_state->crtc);
	if (!old_crtc_state || !new_crtc_state || !new_crtc_state->active)
		return 0;

	if ((spanel->auto_mode_vrefresh && old_crtc_state->self_refresh_active) ||
	    !drm_atomic_crtc_effectively_active(old_crtc_state)) {
		struct drm_display_mode *mode = &new_crtc_state->adjusted_mode;

		/* set clock to max refresh rate on self refresh exit or resume due to early exit */
		mode->clock = mode->htotal * mode->vtotal * 120 / 1000;

		if (mode->clock != new_crtc_state->mode.clock) {
			new_crtc_state->mode_changed = true;
			dev_dbg(ctx->dev, "raise mode (%s) clock to 120hz on %s\n",
				mode->name,
				old_crtc_state->self_refresh_active ? "self refresh exit" : "resume");
		}
	} else if (old_crtc_state->active_changed &&
		   (old_crtc_state->adjusted_mode.clock != old_crtc_state->mode.clock)) {
		/* clock hacked in last commit due to self refresh exit or resume, undo that */
		new_crtc_state->mode_changed = true;
		new_crtc_state->adjusted_mode.clock = new_crtc_state->mode.clock;
		dev_dbg(ctx->dev, "restore mode (%s) clock after self refresh exit or resume\n",
			new_crtc_state->mode.name);
	}

	return 0;
}

static void hk3_write_display_mode(struct exynos_panel *ctx,
				   const struct drm_display_mode *mode)
{
	u8 val = HK3_WRCTRLD_BCTRL_BIT;

	if (IS_HBM_ON(ctx->hbm_mode))
		val |= HK3_WRCTRLD_HBM_BIT;

	if (ctx->hbm.local_hbm.enabled)
		val |= HK3_WRCTRLD_LOCAL_HBM_BIT;

	if (ctx->dimming_on)
		val |= HK3_WRCTRLD_DIMMING_BIT;

	dev_dbg(ctx->dev,
		"%s(wrctrld:0x%x, hbm: %s, dimming: %s local_hbm: %s)\n",
		__func__, val, IS_HBM_ON(ctx->hbm_mode) ? "on" : "off",
		ctx->dimming_on ? "on" : "off",
		ctx->hbm.local_hbm.enabled ? "on" : "off");

	EXYNOS_DCS_BUF_ADD_AND_FLUSH(ctx, MIPI_DCS_WRITE_CONTROL_DISPLAY, val);
}

#define HK3_OPR_VAL_LEN 2
#define HK3_MAX_OPR_VAL 0x3FF
/* Get OPR (on pixel ratio), the unit is percent */
static int hk3_get_opr(struct exynos_panel *ctx, u8 *opr)
{
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
	u8 buf[HK3_OPR_VAL_LEN] = {0};
	u16 val;
	int ret;

	DPU_ATRACE_BEGIN(__func__);
	EXYNOS_DCS_WRITE_TABLE(ctx, unlock_cmd_f0);
	EXYNOS_DCS_WRITE_SEQ(ctx, 0xB0, 0x00, 0xE7, 0x91);
	ret = mipi_dsi_dcs_read(dsi, 0x91, buf, HK3_OPR_VAL_LEN);
	EXYNOS_DCS_WRITE_TABLE(ctx, lock_cmd_f0);
	DPU_ATRACE_END(__func__);

	if (ret != HK3_OPR_VAL_LEN) {
		dev_warn(ctx->dev, "Failed to read OPR (%d)\n", ret);
		return ret;
	}

	val = (buf[0] << 8) | buf[1];
	*opr = DIV_ROUND_CLOSEST(val * 100, HK3_MAX_OPR_VAL);
	dev_dbg(ctx->dev, "%s: %u (0x%X)\n", __func__, *opr, val);

	return 0;
}

#define HK3_ZA_THRESHOLD_OPR 80
static void hk3_update_za(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	bool enable_za = false;
	u8 opr;

	if ((spanel->hw_acl_setting > 0) && !spanel->force_za_off) {
		if (ctx->panel_rev != PANEL_REV_PROTO1) {
			enable_za = true;
		} else if (!hk3_get_opr(ctx, &opr)) {
			enable_za = (opr > HK3_ZA_THRESHOLD_OPR);
		} else {
			dev_warn(ctx->dev, "Unable to update za\n");
			return;
		}
	}

	if (spanel->hw_za_enabled != enable_za) {
		/* LP setting - 0x21 or 0x11: 7.5%, 0x00: off */
		u8 val = 0;

		EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x01, 0x6C, 0x92);
		if (enable_za)
			val = (ctx->panel_rev == PANEL_REV_PROTO1) ? 0x21 : 0x11;
		EXYNOS_DCS_BUF_ADD(ctx, 0x92, val);
		EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);

		spanel->hw_za_enabled = enable_za;
		dev_info(ctx->dev, "%s: %s\n", __func__, enable_za ? "on" : "off");
	}
}

#define HK3_ACL_ZA_THRESHOLD_DBV_P1_0 3917
#define HK3_ACL_ZA_THRESHOLD_DBV_P1_1 3781
#define HK3_ACL_ENHANCED_THRESHOLD_DBV 3865
#define HK3_ACL_NORMAL_THRESHOLD_DBV_1 3570
#define HK3_ACL_NORMAL_THRESHOLD_DBV_2 3963

/* updated za when acl mode changed */
static void hk3_set_acl_mode(struct exynos_panel *ctx, enum exynos_acl_mode mode)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	u16 dbv_th = 0;
	u8 setting = 0;
	bool enable_acl = false;
	/*
	 * ACL mode and setting:
	 *
	 * P1.0
	 *    NORMAL/ENHANCED- 5% (0x01)
	 * P1.1
	 *    NORMAL/ENHANCED- 7.5% (0x02)
	 *
	 * EVT1 and later
	 *    ENHANCED   - 17%  (0x03)
	 *    NORMAL     - 12%  (0x02)
	 *               - 7.5% (0x01)
	 *
	 * Set 0x00 to disable it
	 */
	if (ctx->panel_rev == PANEL_REV_PROTO1) {
		dbv_th = HK3_ACL_ZA_THRESHOLD_DBV_P1_0;
		setting = 0x01;
	} else if (ctx->panel_rev == PANEL_REV_PROTO1_1) {
		dbv_th = HK3_ACL_ZA_THRESHOLD_DBV_P1_1;
		setting = 0x02;
	} else {
		if (mode == ACL_ENHANCED) {
			dbv_th = HK3_ACL_ENHANCED_THRESHOLD_DBV;
			setting = 0x03;
		} else if (mode == ACL_NORMAL) {
			if (spanel->hw_dbv >= HK3_ACL_NORMAL_THRESHOLD_DBV_1 &&
				spanel->hw_dbv < HK3_ACL_NORMAL_THRESHOLD_DBV_2) {
				dbv_th = HK3_ACL_NORMAL_THRESHOLD_DBV_1;
				setting = 0x01;
			} else if (spanel->hw_dbv >= HK3_ACL_NORMAL_THRESHOLD_DBV_2) {
				dbv_th = HK3_ACL_NORMAL_THRESHOLD_DBV_2;
				setting = 0x02;
			}
		}
	}

	enable_acl = (spanel->hw_dbv >= dbv_th && IS_HBM_ON(ctx->hbm_mode) && mode != ACL_OFF);
	if (enable_acl == false)
		setting = 0;

	if (spanel->hw_acl_setting != setting) {
		EXYNOS_DCS_WRITE_SEQ(ctx, 0x55, setting);
		spanel->hw_acl_setting = setting;
		dev_info(ctx->dev, "%s: %d\n", __func__, setting);
		/* Keep ZA off after EVT1 */
		if (ctx->panel_rev < PANEL_REV_EVT1)
			hk3_update_za(ctx);
	}
}

static int hk3_set_brightness(struct exynos_panel *ctx, u16 br)
{
	int ret;
	u16 brightness;
	struct hk3_panel *spanel = to_spanel(ctx);

	if (ctx->current_mode->exynos_mode.is_lp_mode) {
		const struct exynos_panel_funcs *funcs;

		/* don't stay at pixel-off state in AOD, or black screen is possibly seen */
		if (spanel->is_pixel_off) {
			EXYNOS_DCS_WRITE_SEQ(ctx, MIPI_DCS_ENTER_NORMAL_MODE);
			spanel->is_pixel_off = false;
		}
		funcs = ctx->desc->exynos_panel_func;
		if (funcs && funcs->set_binned_lp)
			funcs->set_binned_lp(ctx, br);
		return 0;
	}

	/* Use pixel off command instead of setting DBV 0 */
	if (!br) {
		if (!spanel->is_pixel_off) {
			EXYNOS_DCS_WRITE_TABLE(ctx, pixel_off);
			spanel->is_pixel_off = true;
			dev_dbg(ctx->dev, "%s: pixel off instead of dbv 0\n", __func__);
		}
		return 0;
	} else if (br && spanel->is_pixel_off) {
		EXYNOS_DCS_WRITE_SEQ(ctx, MIPI_DCS_ENTER_NORMAL_MODE);
		spanel->is_pixel_off = false;
	}

	brightness = (br & 0xff) << 8 | br >> 8;
	ret = exynos_dcs_set_brightness(ctx, brightness);
	if (!ret) {
		spanel->hw_dbv = br;
		hk3_set_acl_mode(ctx, ctx->acl_mode);
	}

	return ret;
}

static const struct exynos_dsi_cmd hk3_display_on_cmds[] = {
	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	EXYNOS_DSI_CMD0(sync_begin),
	/* AMP type change (return) */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x4F, 0xF4),
	EXYNOS_DSI_CMD_SEQ(0xF4, 0x70),
	/* Vreg = 7.1V (return) */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x31, 0xF4),
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_GE(PANEL_REV_DVT1), 0xF4, 0x1A, 0x1A, 0x1A, 0x1A, 0x1A),
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_LT(PANEL_REV_DVT1), 0xF4, 0x1B, 0x1B, 0x1B, 0x1B, 0x1B),
	EXYNOS_DSI_CMD0(sync_end),
	EXYNOS_DSI_CMD0(lock_cmd_f0),

	EXYNOS_DSI_CMD_SEQ(MIPI_DCS_SET_DISPLAY_ON),
};
static DEFINE_EXYNOS_CMD_SET(hk3_display_on);

static const struct exynos_dsi_cmd hk3_display_off_cmds[] = {
	EXYNOS_DSI_CMD_SEQ(MIPI_DCS_SET_DISPLAY_OFF),

	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	EXYNOS_DSI_CMD0(sync_begin),
	/* AMP type change */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x4F, 0xF4),
	EXYNOS_DSI_CMD_SEQ(0xF4, 0x50),
	/* Vreg = 4.5 */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x31, 0xF4),
	EXYNOS_DSI_CMD_SEQ(0xF4, 0x00, 0x00, 0x00, 0x00, 0x00),
	EXYNOS_DSI_CMD0(sync_end),
	EXYNOS_DSI_CMD0(lock_cmd_f0),
};
static DEFINE_EXYNOS_CMD_SET(hk3_display_off);

static unsigned int hk3_get_te_usec(struct exynos_panel *ctx,
				    const struct exynos_panel_mode *pmode)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	if (spanel->hw_vrefresh != 60)
		return pmode->exynos_mode.te_usec;
	else
		return (test_bit(FEAT_OP_NS, spanel->feat) ? HK3_TE_USEC_60HZ_NS :
							     HK3_TE_USEC_60HZ_HS);
}

static u32 hk3_get_te_width_usec(u32 vrefresh, bool is_ns)
{
	/* TODO: update this line if supporting 30 Hz normal mode in the future */
	if (vrefresh == 30)
		return HK3_TE_USEC_AOD;
	else if (vrefresh == 120)
		return HK3_TE_USEC_120HZ;
	else
		return is_ns ? HK3_TE_USEC_60HZ_NS : HK3_TE_USEC_60HZ_HS;
}

static void hk3_wait_for_vsync_done(struct exynos_panel *ctx, u32 vrefresh, bool is_ns)
{
	u32 te_width_us = hk3_get_te_width_usec(vrefresh, is_ns);

	dev_dbg(ctx->dev, "%s: %dhz\n", __func__, vrefresh);

	DPU_ATRACE_BEGIN(__func__);
	exynos_panel_wait_for_vsync_done(ctx, te_width_us,
		EXYNOS_VREFRESH_TO_PERIOD_USEC(vrefresh));
	/* add 1ms tolerance */
	exynos_panel_msleep(1);
	DPU_ATRACE_END(__func__);
}

/**
 * hk3_wait_for_vsync_done_changeable - wait for finishing vsync for changeable TE to avoid
 * fake TE at transition from fixed TE to changeable TE.
 * @ctx: panel struct
 * @vrefresh: current refresh rate
 * @is_ns: whether it is normal speed or not
 */
static void hk3_wait_for_vsync_done_changeable(struct exynos_panel *ctx, u32 vrefresh, bool is_ns)
{
	int i = 0;
	const int timeout = 5;
	u32 te_width_us = hk3_get_te_width_usec(vrefresh, is_ns);

	while (i++ < timeout) {
		ktime_t t;
		s64 delta_us;
		int period_us = EXYNOS_VREFRESH_TO_PERIOD_USEC(vrefresh);

		exynos_panel_wait_for_vblank(ctx);
		t = ktime_get();
		exynos_panel_wait_for_vblank(ctx);
		delta_us = ktime_us_delta(ktime_get(), t);
		if (abs(delta_us - period_us) < HK3_TE_PERIOD_DELTA_TOLERANCE_USEC)
			break;
	}
	if (i >= timeout)
		dev_warn(ctx->dev, "timeout of waiting for changeable TE @ %d Hz\n", vrefresh);
	usleep_range(te_width_us, te_width_us + 10);
}

static bool hk3_is_peak_vrefresh(u32 vrefresh, bool is_ns)
{
	return (is_ns && vrefresh == 60) || (!is_ns && vrefresh == 120);
}

static void hk3_set_lp_mode(struct exynos_panel *ctx, const struct exynos_panel_mode *pmode)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	const u16 brightness = exynos_panel_get_brightness(ctx);
	bool is_changeable_te = !test_bit(FEAT_EARLY_EXIT, spanel->feat);
	bool is_ns = test_bit(FEAT_OP_NS, spanel->feat);
	bool panel_enabled = is_panel_enabled(ctx);
	u32 vrefresh = panel_enabled ? spanel->hw_vrefresh : 60;

	dev_dbg(ctx->dev, "%s: panel: %s\n", __func__, panel_enabled ? "ON" : "OFF");

	DPU_ATRACE_BEGIN(__func__);

	hk3_disable_panel_feat(ctx, vrefresh);
	if (panel_enabled) {
		/* init sequence has sent display-off command already */
		if (!hk3_is_peak_vrefresh(vrefresh, is_ns) && is_changeable_te)
			hk3_wait_for_vsync_done_changeable(ctx, vrefresh, is_ns);
		else
			hk3_wait_for_vsync_done(ctx, vrefresh, is_ns);
		exynos_panel_send_cmd_set(ctx, &hk3_display_off_cmd_set);
	}
	/* display should be off here, set dbv before entering lp mode */
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, aod_dbv);
	hk3_wait_for_vsync_done(ctx, vrefresh, false);

	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, aod_on);
	exynos_panel_set_binned_lp(ctx, brightness);
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	/* Fixed TE: sync on */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x51);
	/* Default TE pulse width 693us */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x08, 0xB9);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x0B, 0xE0, 0x00, 0x2F, 0x0B, 0xE0, 0x00, 0x2F);
	/* Frequency set for AOD */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x02, 0xB9);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x00);
	/* Auto frame insertion: 1Hz */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x18, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x04, 0x00, 0x74);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0xB8, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00, 0x08);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0xC8, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x03);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0xA7);
	/* Enable early exit */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0xE8, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x00);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x10, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x22);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x82, 0xBD);
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x22, 0x22, 0x22, 0x22);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	exynos_panel_send_cmd_set(ctx, &hk3_display_on_cmd_set);

	spanel->hw_vrefresh = 30;
	spanel->read_vreg = true;

	DPU_ATRACE_END(__func__);

	dev_info(ctx->dev, "enter %dhz LP mode\n", drm_mode_vrefresh(&pmode->mode));
}

static void hk3_set_nolp_mode(struct exynos_panel *ctx,
			      const struct exynos_panel_mode *pmode)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	dev_dbg(ctx->dev, "%s\n", __func__);

	DPU_ATRACE_BEGIN(__func__);

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	/* manual mode */
	EXYNOS_DCS_BUF_ADD(ctx, 0xBD, 0x21);
	/* Changeable TE is a must to ensure command sync */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x04);
	/* Changeable TE width setting and frequency */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x04, 0xB9);
	/* width 693us in AOD mode */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x0B, 0xE0, 0x00, 0x2F);
	/* AOD 30Hz */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x01, 0x60);
	EXYNOS_DCS_BUF_ADD(ctx, 0x60, 0x00);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	spanel->hw_idle_vrefresh = 0;

	hk3_wait_for_vsync_done(ctx, 30, false);
	exynos_panel_send_cmd_set(ctx, &hk3_display_off_cmd_set);

	hk3_wait_for_vsync_done(ctx, 30, false);
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	/* TE width setting */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x04, 0xB9);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB9, 0x0B, 0xBB, 0x00, 0x2F, /* changeable TE */
			   0x0B, 0xBB, 0x00, 0x2F, 0x0B, 0xBB, 0x00, 0x2F); /* fixed TE */
	/* disabling AOD low Mode is a must before aod-off */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x52, 0x94);
	EXYNOS_DCS_BUF_ADD(ctx, 0x94, 0x00);
	EXYNOS_DCS_BUF_ADD_SET(ctx, lock_cmd_f0);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, aod_off);
	hk3_update_panel_feat(ctx, drm_mode_vrefresh(&pmode->mode), true);
	/* backlight control and dimming */
	hk3_write_display_mode(ctx, &pmode->mode);
	hk3_change_frequency(ctx, pmode);
	exynos_panel_send_cmd_set(ctx, &hk3_display_on_cmd_set);
	spanel->read_vreg = true;

	DPU_ATRACE_END(__func__);

	dev_info(ctx->dev, "exit LP mode\n");
}

static const struct exynos_dsi_cmd hk3_init_cmds[] = {
	EXYNOS_DSI_CMD_SEQ_DELAY(10, MIPI_DCS_EXIT_SLEEP_MODE),

	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	/* AMP type change */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x4F, 0xF4),
	EXYNOS_DSI_CMD_SEQ(0xF4, 0x50),
	/* VREG 4.5V */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x31, 0xF4),
	EXYNOS_DSI_CMD_SEQ(0xF4, 0x00, 0x00, 0x00, 0x00, 0x00),
	EXYNOS_DSI_CMD(lock_cmd_f0, 110),
	/* Enable TE*/
	EXYNOS_DSI_CMD_SEQ(MIPI_DCS_SET_TEAR_ON),

	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	/* AOD Transition Set */
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_LT(PANEL_REV_DVT1), 0xB0, 0x00, 0x03, 0xBB),
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_LT(PANEL_REV_DVT1), 0xBB, 0x41),

	/* TSP SYNC Enable (Auto Set) */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x3C, 0xB9),
	EXYNOS_DSI_CMD_SEQ(0xB9, 0x19, 0x09),

	/* FFC: off, 165MHz, MIPI Speed 1368 Mbps */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x36, 0xC5),
	EXYNOS_DSI_CMD_SEQ(0xC5, 0x10, 0x10, 0x50, 0x05, 0x4D, 0x31, 0x40, 0x00,
				 0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
				 0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
				 0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
				 0x40, 0x00, 0x40, 0x00),

	/* TE width setting */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x04, 0xB9),
	EXYNOS_DSI_CMD_SEQ(0xB9, 0x0B, 0xBB, 0x00, 0x2F, /* changeable TE */
			   0x0B, 0xBB, 0x00, 0x2F, 0x0B, 0xBB, 0x00, 0x2F), /* fixed TE */

	/* enable OPEC (auto still IMG detect off) */
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_LT(PANEL_REV_MP), 0xB0, 0x00, 0x1D, 0x63),
	EXYNOS_DSI_CMD_SEQ_REV(PANEL_REV_LT(PANEL_REV_MP), 0x63, 0x02, 0x18),

	/* PMIC Fast Discharge off */
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x18, 0xB1),
	EXYNOS_DSI_CMD_SEQ(0xB1, 0x55, 0x01),
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x00, 0x13, 0xB1),
	EXYNOS_DSI_CMD_SEQ(0xB1, 0x80),

	EXYNOS_DSI_CMD0(freq_update),
	EXYNOS_DSI_CMD0(lock_cmd_f0),
	/* CASET: 1343 */
	EXYNOS_DSI_CMD_SEQ(MIPI_DCS_SET_COLUMN_ADDRESS, 0x00, 0x00, 0x05, 0x3F),
	/* PASET: 2991 */
	EXYNOS_DSI_CMD_SEQ(MIPI_DCS_SET_PAGE_ADDRESS, 0x00, 0x00, 0x0B, 0xAF),
};
static DEFINE_EXYNOS_CMD_SET(hk3_init);

static const struct exynos_dsi_cmd hk3_ns_gamma_fix_cmds[] = {
	EXYNOS_DSI_CMD0(unlock_cmd_f0),
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x02, 0x3F, 0xCB),
	EXYNOS_DSI_CMD_SEQ(0xCB, 0x0A),
	EXYNOS_DSI_CMD_SEQ(0xB0, 0x02, 0x45, 0xCB),
	EXYNOS_DSI_CMD_SEQ(0xCB, 0x0A),
	EXYNOS_DSI_CMD0(freq_update),
	EXYNOS_DSI_CMD0(lock_cmd_f0),
};
static DEFINE_EXYNOS_CMD_SET(hk3_ns_gamma_fix);

static void hk3_lhbm_luminance_opr_setting(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	bool is_ns_mode = test_bit(FEAT_OP_NS, spanel->feat);

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x02, 0xF9, 0x95);
	/* DBV setting */
	EXYNOS_DCS_BUF_ADD(ctx, 0x95, 0x00, 0x40, 0x0C, 0x01, 0x90, 0x33, 0x06, 0x60,
				0xCC, 0x11, 0x92, 0x7F);
	EXYNOS_DCS_BUF_ADD(ctx, 0x71, 0xC6, 0x00, 0x00, 0x19);
	/* 120Hz base (HS) offset */
	EXYNOS_DCS_BUF_ADD(ctx, 0x6C, 0x9C, 0x9F, 0x59, 0x58, 0x50, 0x2F, 0x2B, 0x2E);
	EXYNOS_DCS_BUF_ADD(ctx, 0x71, 0xC6, 0x00, 0x00, 0x6A);
	/* 60Hz base (NS) offset */
	EXYNOS_DCS_BUF_ADD(ctx, 0x6C, 0xA0, 0xA7, 0x57, 0x5C, 0x52, 0x37, 0x37, 0x40);

	/* Target frequency */
	EXYNOS_DCS_BUF_ADD(ctx, 0x60, is_ns_mode ? 0x18 : 0x00);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	/* Opposite setting of target frequency */
	EXYNOS_DCS_BUF_ADD(ctx, 0x60, is_ns_mode ? 0x00 : 0x18);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	/* Target frequency */
	EXYNOS_DCS_BUF_ADD(ctx, 0x60, is_ns_mode ? 0x18 : 0x00);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
}

static void hk3_negative_field_setting(struct exynos_panel *ctx)
{
	/* all settings will take effect in AOD mode automatically */
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	/* Vint -3V */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x21, 0xF4);
	EXYNOS_DCS_BUF_ADD(ctx, 0xF4, 0x1E);
	/* Vaint -4V */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x69, 0xF4);
	EXYNOS_DCS_BUF_ADD(ctx, 0xF4, 0x78);
	/* VGL -8V */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x17, 0xF4);
	EXYNOS_DCS_BUF_ADD(ctx, 0xF4, 0x1E);
	EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
}

static int hk3_enable(struct drm_panel *panel)
{
	struct exynos_panel *ctx = container_of(panel, struct exynos_panel, panel);
	const struct exynos_panel_mode *pmode = ctx->current_mode;
	const struct drm_display_mode *mode;
	struct hk3_panel *spanel = to_spanel(ctx);
	const bool needs_reset = !is_panel_enabled(ctx);
	bool is_ns = needs_reset ? false : test_bit(FEAT_OP_NS, spanel->feat);
	struct drm_dsc_picture_parameter_set pps_payload;
	bool is_fhd;
	u32 vrefresh;

	if (!pmode) {
		dev_err(ctx->dev, "no current mode set\n");
		return -EINVAL;
	}
	mode = &pmode->mode;
	is_fhd = mode->hdisplay == 1008;
	vrefresh = drm_mode_vrefresh(mode);

	dev_info(ctx->dev, "%s (%s)\n", __func__, is_fhd ? "fhd" : "wqhd");

	DPU_ATRACE_BEGIN(__func__);

	if (needs_reset)
		exynos_panel_reset(ctx);

	if (ctx->mode_in_progress == MODE_RES_IN_PROGRESS) {
		u32 te_width_us = hk3_get_te_width_usec(vrefresh, is_ns);

		exynos_panel_wait_for_vsync_done(ctx, te_width_us,
			EXYNOS_VREFRESH_TO_PERIOD_USEC(vrefresh));
	} else if (ctx->mode_in_progress == MODE_RES_AND_RR_IN_PROGRESS) {
		u32 te_width_us = hk3_get_te_width_usec(ctx->last_rr, is_ns);

		exynos_panel_wait_for_vsync_done(ctx, te_width_us,
			EXYNOS_VREFRESH_TO_PERIOD_USEC(ctx->last_rr));
	}

	/* DSC related configuration */
	PANEL_SEQ_LABEL_BEGIN("pps");
	drm_dsc_pps_payload_pack(&pps_payload,
				 is_fhd ? &fhd_pps_config : &wqhd_pps_config);
	EXYNOS_DCS_WRITE_SEQ(ctx, 0x9D, 0x01);
	EXYNOS_PPS_WRITE_BUF(ctx, &pps_payload);
	PANEL_SEQ_LABEL_END("pps");

	if (needs_reset) {
		PANEL_SEQ_LABEL_BEGIN("init_cmd");
		exynos_panel_send_cmd_set(ctx, &hk3_init_cmd_set);
		PANEL_SEQ_LABEL_END("init_cmd");
		if (ctx->panel_rev == PANEL_REV_PROTO1)
			hk3_lhbm_luminance_opr_setting(ctx);
		if (ctx->panel_rev >= PANEL_REV_DVT1)
			hk3_negative_field_setting(ctx);

		spanel->is_pixel_off = false;
		ctx->dsi_hs_clk = MIPI_DSI_FREQ_DEFAULT;
	}

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD(ctx, 0xC3, is_fhd ? 0x0D : 0x0C);
	/* 8/10bit config for QHD/FHD */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x01, 0xF2);
	EXYNOS_DCS_BUF_ADD(ctx, 0xF2, is_fhd ? 0x81 : 0x01);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);

	if (needs_reset && spanel->material == MATERIAL_E7_DOE)
		exynos_panel_send_cmd_set(ctx, &hk3_ns_gamma_fix_cmd_set);

	if (pmode->exynos_mode.is_lp_mode) {
		hk3_set_lp_mode(ctx, pmode);
	} else {
		hk3_update_panel_feat(ctx, vrefresh, true);
		hk3_write_display_mode(ctx, mode); /* dimming and HBM */
		hk3_change_frequency(ctx, pmode);

		if (needs_reset || (ctx->panel_state == PANEL_STATE_BLANK)) {
			hk3_wait_for_vsync_done(ctx, needs_reset ? 60 : vrefresh, is_ns);
			exynos_panel_send_cmd_set(ctx, &hk3_display_on_cmd_set);
			spanel->read_vreg = true;
		}
	}

	spanel->lhbm_ctl.hist_roi_configured = false;

	DPU_ATRACE_END(__func__);

	return 0;
}

static int hk3_disable(struct drm_panel *panel)
{
	struct exynos_panel *ctx = container_of(panel, struct exynos_panel, panel);
	struct hk3_panel *spanel = to_spanel(ctx);
	u32 vrefresh = spanel->hw_vrefresh;
	int ret;

	dev_info(ctx->dev, "%s\n", __func__);

	/* skip disable sequence if going through RRS */
	if (ctx->mode_in_progress == MODE_RES_IN_PROGRESS ||
	    ctx->mode_in_progress == MODE_RES_AND_RR_IN_PROGRESS) {
		dev_dbg(ctx->dev, "%s: RRS in progress, skip\n", __func__);
		return 0;
	}

	ret = exynos_panel_disable(panel);
	if (ret)
		return ret;

	hk3_disable_panel_feat(ctx, 60);
	/*
	 * can't get crtc pointer here, fallback to sleep. hk3_disable_panel_feat() sends freq
	 * update command to trigger early exit if auto mode is enabled before, waiting for one
	 * frame (for either auto or manual mode) should be sufficient to make sure the previous
	 * commands become effective.
	 */
	exynos_panel_msleep(EXYNOS_VREFRESH_TO_PERIOD_USEC(vrefresh) / 1000 + 1);

	exynos_panel_send_cmd_set(ctx, &hk3_display_off_cmd_set);
	exynos_panel_msleep(20);
	if (ctx->panel_state == PANEL_STATE_OFF)
		EXYNOS_DCS_WRITE_SEQ_DELAY(ctx, 100, MIPI_DCS_ENTER_SLEEP_MODE);

	/* panel register state gets reset after disabling hardware */
	bitmap_clear(spanel->hw_feat, 0, FEAT_MAX);
	spanel->hw_vrefresh = 60;
	spanel->hw_idle_vrefresh = 0;
	spanel->hw_acl_setting = 0;
	spanel->hw_za_enabled = false;
	spanel->hw_dbv = 0;

	return 0;
}

/*
 * 120hz auto mode takes at least 2 frames to start lowering refresh rate in addition to
 * time to next vblank. Use just over 2 frames time to consider worst case scenario
 */
#define EARLY_EXIT_THRESHOLD_US 17000

/**
 * hk3_update_idle_state - update panel auto frame insertion state
 * @ctx: panel struct
 *
 * - update timestamp of switching to manual mode in case its been a while since the
 *   last frame update and auto mode may have started to lower refresh rate.
 * - trigger early exit by command if it's changeable TE and no switching delay, which
 *   could result in fast 120 Hz boost and seeing 120 Hz TE earlier, otherwise disable
 *   auto refresh mode to avoid lowering frequency too fast.
 */
static void hk3_update_idle_state(struct exynos_panel *ctx)
{
	s64 delta_us;
	struct hk3_panel *spanel = to_spanel(ctx);

	ctx->panel_idle_vrefresh = 0;
	if (!test_bit(FEAT_FRAME_AUTO, spanel->feat))
		return;

	delta_us = ktime_us_delta(ktime_get(), ctx->last_commit_ts);
	if (delta_us < EARLY_EXIT_THRESHOLD_US) {
		dev_dbg(ctx->dev, "skip early exit. %lldus since last commit\n",
			delta_us);
		return;
	}

	/* triggering early exit causes a switch to 120hz */
	ctx->last_mode_set_ts = ktime_get();

	DPU_ATRACE_BEGIN(__func__);

	if (!ctx->idle_delay_ms && spanel->force_changeable_te) {
		dev_dbg(ctx->dev, "sending early exit out cmd\n");
		EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
		EXYNOS_DCS_BUF_ADD_SET(ctx, freq_update);
		EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	} else {
		/* turn off auto mode to prevent panel from lowering frequency too fast */
		hk3_update_refresh_mode(ctx, ctx->current_mode, 0);
	}

	DPU_ATRACE_END(__func__);
}

static void hk3_commit_done(struct exynos_panel *ctx)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	if (ctx->current_mode->exynos_mode.is_lp_mode)
		return;

	/* skip idle update if going through RRS */
	if (ctx->mode_in_progress == MODE_RES_IN_PROGRESS ||
	    ctx->mode_in_progress == MODE_RES_AND_RR_IN_PROGRESS) {
		dev_dbg(ctx->dev, "%s: RRS in progress, skip\n", __func__);
		return;
	}

	hk3_update_idle_state(ctx);

	hk3_update_za(ctx);

	if (spanel->pending_temp_update)
		hk3_update_disp_therm(ctx);
}

static void hk3_set_hbm_mode(struct exynos_panel *ctx,
			     enum exynos_hbm_mode mode)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	const struct exynos_panel_mode *pmode = ctx->current_mode;

	if (mode == ctx->hbm_mode)
		return;

	if (unlikely(!pmode))
		return;

	ctx->hbm_mode = mode;

	if (IS_HBM_ON(mode)) {
		set_bit(FEAT_HBM, spanel->feat);
		/* enforce IRC on for factory builds */
#ifndef PANEL_FACTORY_BUILD
		if (mode == HBM_ON_IRC_ON)
			clear_bit(ctx->panel_rev >= PANEL_REV_EVT1 ?
				  FEAT_IRC_Z_MODE : FEAT_IRC_OFF, spanel->feat);
		else
			set_bit(ctx->panel_rev >= PANEL_REV_EVT1 ?
				FEAT_IRC_Z_MODE : FEAT_IRC_OFF, spanel->feat);
#endif
	} else {
		clear_bit(FEAT_HBM, spanel->feat);
		clear_bit(ctx->panel_rev >= PANEL_REV_EVT1 ?
			  FEAT_IRC_Z_MODE : FEAT_IRC_OFF, spanel->feat);
	}

	if (ctx->panel_state == PANEL_STATE_NORMAL) {
		if (!IS_HBM_ON(mode))
			hk3_write_display_mode(ctx, &pmode->mode);
		hk3_update_panel_feat(ctx, drm_mode_vrefresh(&pmode->mode), false);
		if (IS_HBM_ON(mode))
			hk3_write_display_mode(ctx, &pmode->mode);
	}
}

static void hk3_set_dimming_on(struct exynos_panel *ctx,
			       bool dimming_on)
{
	const struct exynos_panel_mode *pmode = ctx->current_mode;

	ctx->dimming_on = dimming_on;
	if (pmode->exynos_mode.is_lp_mode) {
		dev_info(ctx->dev,"in lp mode, skip to update");
		return;
	}
	hk3_write_display_mode(ctx, &pmode->mode);
}

static void hk3_set_local_hbm_brightness(struct exynos_panel *ctx, bool is_first_stage)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	struct hk3_lhbm_ctl *ctl = &spanel->lhbm_ctl;
	const u8 *brt;
	enum hk3_lhbm_brt_overdrive_group group = LHBM_OVERDRIVE_GRP_MAX;
	static u8 cmd[LHBM_BRT_CMD_LEN];
	int i;

	if (!is_local_hbm_post_enabling_supported(ctx))
		return;

	dev_info(ctx->dev, "set LHBM brightness at %s stage\n", is_first_stage ? "1st" : "2nd");
	if (is_first_stage) {
		u32 gray = exynos_drm_connector_get_lhbm_gray_level(&ctx->exynos_connector);
		u32 dbv = exynos_panel_get_brightness(ctx);
		u32 normal_dbv_max = ctx->desc->brt_capability->normal.level.max;
		u32 normal_nit_max = ctx->desc->brt_capability->normal.nits.max;
		u32 luma = 0;

		if (gray < 15) {
			group = LHBM_OVERDRIVE_GRP_0_NIT;
		} else {
			if (dbv <= normal_dbv_max)
				luma = panel_cmn_calc_gamma_2_2_luminance(dbv, normal_dbv_max,
				normal_nit_max);
			else
				luma = panel_cmn_calc_linear_luminance(dbv, 700, -1271);
			luma = panel_cmn_calc_gamma_2_2_luminance(gray, 255, luma);

			if (luma < 6)
				group = LHBM_OVERDRIVE_GRP_6_NIT;
			else if (luma < 50)
				group = LHBM_OVERDRIVE_GRP_50_NIT;
			else if (luma < 300)
				group = LHBM_OVERDRIVE_GRP_300_NIT;
			else
				group = LHBM_OVERDRIVE_GRP_MAX;
		}
		dev_dbg(ctx->dev, "check LHBM overdrive condition | gray=%u dbv=%u luma=%u\n",
			gray, dbv, luma);
	}

	if (group < LHBM_OVERDRIVE_GRP_MAX) {
		brt = ctl->brt_overdrive[group];
		ctl->overdrived = true;
	} else {
		brt = ctl->brt_normal;
		ctl->overdrived = false;
	}
	cmd[0] = lhbm_brightness_reg;
	for (i = 0; i < LHBM_BRT_LEN; i++)
		cmd[i+1] = brt[i];
	dev_dbg(ctx->dev, "set %s brightness: [%d] %*ph\n",
		ctl->overdrived ? "overdrive" : "normal",
		ctl->overdrived ? group : -1, LHBM_BRT_LEN, brt);
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD_SET(ctx, lhbm_brightness_index);
	EXYNOS_DCS_BUF_ADD_SET(ctx, cmd);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
}

static void hk3_set_local_hbm_mode(struct exynos_panel *ctx,
				 bool local_hbm_en)
{
	const struct exynos_panel_mode *pmode = ctx->current_mode;

	/* TODO: LHBM Position & Size */
	hk3_write_display_mode(ctx, &pmode->mode);

	if (local_hbm_en)
		hk3_set_local_hbm_brightness(ctx, true);

}

static void hk3_set_local_hbm_mode_post(struct exynos_panel *ctx)
{
	const struct hk3_panel *spanel = to_spanel(ctx);

	if (spanel->lhbm_ctl.overdrived)
		hk3_set_local_hbm_brightness(ctx, false);
}

static void hk3_mode_set(struct exynos_panel *ctx,
			 const struct exynos_panel_mode *pmode)
{
	hk3_change_frequency(ctx, pmode);
}

static bool hk3_is_mode_seamless(const struct exynos_panel *ctx,
				     const struct exynos_panel_mode *pmode)
{
	const struct drm_display_mode *c = &ctx->current_mode->mode;
	const struct drm_display_mode *n = &pmode->mode;

	/* seamless mode set can happen if active region resolution is same */
	return (c->vdisplay == n->vdisplay) && (c->hdisplay == n->hdisplay) &&
	       (c->flags == n->flags);
}

static int hk3_set_op_hz(struct exynos_panel *ctx, unsigned int hz)
{
	struct hk3_panel *spanel = to_spanel(ctx);
	u32 vrefresh = drm_mode_vrefresh(&ctx->current_mode->mode);

	if (vrefresh > hz || (hz != 60 && hz != 120)) {
		dev_err(ctx->dev, "invalid op_hz=%d for vrefresh=%d\n",
			hz, vrefresh);
		return -EINVAL;
	}

	DPU_ATRACE_BEGIN(__func__);

	ctx->op_hz = hz;
	if (hz == 60)
		set_bit(FEAT_OP_NS, spanel->feat);
	else
		clear_bit(FEAT_OP_NS, spanel->feat);

	if (is_panel_active(ctx))
		hk3_update_panel_feat(ctx, vrefresh, false);
	dev_info(ctx->dev, "%s op_hz at %d\n",
		is_panel_active(ctx) ? "set" : "cache", hz);

	DPU_ATRACE_END(__func__);

	return 0;
}

static int hk3_read_id(struct exynos_panel *ctx)
{
	return exynos_panel_read_ddic_id(ctx);
}

/* Note the format is 0x<DAh><DBh><DCh> which is reverse of bootloader (0x<DCh><DBh><DAh>) */
static void hk3_get_panel_material(struct exynos_panel *ctx, u32 id)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	switch (id) {
	case 0x000A4000:
		spanel->material = MATERIAL_E6;
		break;
	case 0x000A4020:
		spanel->material = MATERIAL_E7_DOE;
		break;
	case 0x000A4420:
		spanel->material = MATERIAL_E7;
		break;
	case 0x000A4520:
		spanel->material = MATERIAL_LPC5;
		break;
	default:
		dev_warn(ctx->dev, "unknown material from panel (%#x), default to E7\n", id);
		spanel->material = MATERIAL_E7;
		break;
	}

	dev_info(ctx->dev, "%s: %d\n", __func__, spanel->material);
}

static void hk3_get_panel_rev(struct exynos_panel *ctx, u32 id)
{
	/* extract command 0xDB */
	u8 build_code = (id & 0xFF00) >> 8;
	u8 rev = ((build_code & 0xE0) >> 3) | ((build_code & 0x0C) >> 2);

	exynos_panel_get_panel_rev(ctx, rev);

	hk3_get_panel_material(ctx, id);
}

static void hk3_normal_mode_work(struct exynos_panel *ctx)
{
	if (ctx->self_refresh_active) {
		hk3_update_disp_therm(ctx);
	} else {
		struct hk3_panel *spanel = to_spanel(ctx);

		spanel->pending_temp_update = true;
	}
}

static void hk3_pre_update_ffc(struct exynos_panel *ctx)
{
	dev_dbg(ctx->dev, "%s\n", __func__);

	DPU_ATRACE_BEGIN(__func__);

	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	/* FFC off */
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x36, 0xC5);
	EXYNOS_DCS_BUF_ADD(ctx, 0xC5, 0x10);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);

	DPU_ATRACE_END(__func__);
}

static void hk3_update_ffc(struct exynos_panel *ctx, unsigned int hs_clk)
{
	dev_dbg(ctx->dev, "%s: hs_clk: current=%d, target=%d\n",
		__func__, ctx->dsi_hs_clk, hs_clk);

	DPU_ATRACE_BEGIN(__func__);

	if (hs_clk != MIPI_DSI_FREQ_DEFAULT && hs_clk != MIPI_DSI_FREQ_ALTERNATIVE) {
		dev_warn(ctx->dev, "%s: invalid hs_clk=%d for FFC\n", __func__, hs_clk);
	} else if (ctx->dsi_hs_clk != hs_clk) {
		dev_info(ctx->dev, "%s: updating for hs_clk=%d\n", __func__, hs_clk);
		ctx->dsi_hs_clk = hs_clk;

		/* Update FFC */
		EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x37, 0xC5);
		if (hs_clk == MIPI_DSI_FREQ_DEFAULT)
			EXYNOS_DCS_BUF_ADD(ctx, 0xC5, 0x10, 0x50, 0x05, 0x4D, 0x31, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4D, 0x31, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00);
		else /* MIPI_DSI_FREQ_ALTERNATIVE */
			EXYNOS_DCS_BUF_ADD(ctx, 0xC5, 0x10, 0x50, 0x05, 0x4E, 0x74, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4E, 0x74, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4E, 0x74, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00, 0x4E, 0x74, 0x40, 0x00,
						0x40, 0x00, 0x40, 0x00);
		EXYNOS_DCS_BUF_ADD_SET(ctx, lock_cmd_f0);
	}

	/* FFC on */
	EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
	EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x36, 0xC5);
	EXYNOS_DCS_BUF_ADD(ctx, 0xC5, 0x11);
	EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);

	DPU_ATRACE_END(__func__);
}

static void hk3_get_pwr_vreg(struct exynos_panel *ctx, char *buf, size_t len)
{
	struct hk3_panel *spanel = to_spanel(ctx);

	strlcpy(buf, spanel->hw_vreg, len);
}

static const struct exynos_display_underrun_param underrun_param = {
	.te_idle_us = 350,
	.te_var = 1,
};

static const u32 hk3_bl_range[] = {
	94, 180, 270, 360, 3307
};

static const int hk3_vrefresh_range[] = {
#ifdef PANEL_FACTORY_BUILD
	1, 5, 10, 30, 60, 120
#else
	1, 10, 30, 60, 120
#endif
};

static const int hk3_lp_vrefresh_range[] = {
	1, 30
};

#define HK3_WQHD_DSC {\
	.enabled = true,\
	.dsc_count = 2,\
	.slice_count = 2,\
	.slice_height = 187,\
	.cfg = &wqhd_pps_config,\
}
#define HK3_FHD_DSC {\
	.enabled = true,\
	.dsc_count = 2,\
	.slice_count = 2,\
	.slice_height = 187,\
	.cfg = &fhd_pps_config,\
}

static const struct exynos_panel_mode hk3_modes[] = {
#ifdef PANEL_FACTORY_BUILD
	{
		.mode = {
			.name = "1344x2992x1",
			.clock = 4545,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 52, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_UNSUPPORTED,
	},
	{
		.mode = {
			.name = "1344x2992x5",
			.clock = 22725,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 52, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_UNSUPPORTED,
	},
	{
		.mode = {
			.name = "1344x2992x10",
			.clock = 45147,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 42, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_UNSUPPORTED,
	},
	{
		.mode = {
			.name = "1344x2992x30",
			.clock = 135441,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			/* change hsa and hbp to avoid conflicting to LP mode 30Hz */
			.hsync_end = 1344 + 80 + 22, // add hsa
			.htotal = 1344 + 80 + 22 + 44, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_UNSUPPORTED,
	},
#endif
	{
		.mode = {
			.name = "1344x2992x60",
			.clock = 270882,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 42, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.type = DRM_MODE_TYPE_PREFERRED,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_ON_SELF_REFRESH,
	},
	{
		.mode = {
			.name = "1344x2992x120",
			.clock = 541764,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 42, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.te_usec = HK3_TE_USEC_120HZ,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_ON_INACTIVITY,
	},
#ifndef PANEL_FACTORY_BUILD
	{
		.mode = {
			.name = "1008x2244x60",
			.clock = 157320,
			.hdisplay = 1008,
			.hsync_start = 1008 + 80, // add hfp
			.hsync_end = 1008 + 80 + 24, // add hsa
			.htotal = 1008 + 80 + 24 + 38, // add hbp
			.vdisplay = 2244,
			.vsync_start = 2244 + 12, // add vfp
			.vsync_end = 2244 + 12 + 4, // add vsa
			.vtotal = 2244 + 12 + 4 + 20, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.bpc = 8,
			.dsc = HK3_FHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_ON_SELF_REFRESH,
	},
	{
		.mode = {
			.name = "1008x2244x120",
			.clock = 314640,
			.hdisplay = 1008,
			.hsync_start = 1008 + 80, // add hfp
			.hsync_end = 1008 + 80 + 24, // add hsa
			.htotal = 1008 + 80 + 24 + 38, // add hbp
			.vdisplay = 2244,
			.vsync_start = 2244 + 12, // add vfp
			.vsync_end = 2244 + 12 + 4, // add vsa
			.vtotal = 2244 + 12 + 4 + 20, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.te_usec = HK3_TE_USEC_120HZ,
			.bpc = 8,
			.dsc = HK3_FHD_DSC,
			.underrun_param = &underrun_param,
		},
		.te2_timing = {
			.rising_edge = HK3_TE2_RISING_EDGE_OFFSET,
			.falling_edge = HK3_TE2_FALLING_EDGE_OFFSET,
		},
		.idle_mode = IDLE_MODE_ON_INACTIVITY,
	},
#endif
};

static const struct exynos_panel_mode hk3_lp_modes[] = {
	{
		.mode = {
			.name = "1344x2992x30",
			.clock = 135441,
			.hdisplay = 1344,
			.hsync_start = 1344 + 80, // add hfp
			.hsync_end = 1344 + 80 + 24, // add hsa
			.htotal = 1344 + 80 + 24 + 42, // add hbp
			.vdisplay = 2992,
			.vsync_start = 2992 + 12, // add vfp
			.vsync_end = 2992 + 12 + 4, // add vsa
			.vtotal = 2992 + 12 + 4 + 22, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.te_usec = HK3_TE_USEC_AOD,
			.bpc = 8,
			.dsc = HK3_WQHD_DSC,
			.underrun_param = &underrun_param,
			.is_lp_mode = true,
		},
	},
#ifndef PANEL_FACTORY_BUILD
	{
		.mode = {
			.name = "1008x2244x30",
			.clock = 78660,
			.hdisplay = 1008,
			.hsync_start = 1008 + 80, // add hfp
			.hsync_end = 1008 + 80 + 24, // add hsa
			.htotal = 1008 + 80 + 24 + 38, // add hbp
			.vdisplay = 2244,
			.vsync_start = 2244 + 12, // add vfp
			.vsync_end = 2244 + 12 + 4, // add vsa
			.vtotal = 2244 + 12 + 4 + 20, // add vbp
			.flags = 0,
			.width_mm = 70,
			.height_mm = 155,
		},
		.exynos_mode = {
			.mode_flags = MIPI_DSI_CLOCK_NON_CONTINUOUS,
			.vblank_usec = 120,
			.te_usec = HK3_TE_USEC_AOD,
			.bpc = 8,
			.dsc = HK3_FHD_DSC,
			.underrun_param = &underrun_param,
			.is_lp_mode = true,
		},
	},
#endif
};

static void hk3_calc_lhbm_od_brightness(u8 n_fine, u8 n_coarse,
	u8 *o_fine, u8 *o_coarse,
	u8 fine_offset_0, u8 fine_offset_1,
	u8 coarse_offset_0, u8 coarse_offset_1)
{
	if (((int)n_fine + (int)fine_offset_0) <= 0xFF) {
		*o_coarse = n_coarse + coarse_offset_0;
		*o_fine = n_fine + fine_offset_0;
	} else {
		*o_coarse = n_coarse + coarse_offset_1;
		*o_fine = n_fine - fine_offset_1;
	}
}

static void hk3_lhbm_brightness_init(struct exynos_panel *ctx)
{
	struct mipi_dsi_device *dsi = to_mipi_dsi_device(ctx->dev);
	struct hk3_panel *spanel = to_spanel(ctx);
	struct hk3_lhbm_ctl *ctl = &spanel->lhbm_ctl;
	int ret;
	u8 g_coarse, b_coarse;
	u8 *p_norm = ctl->brt_normal;
	u8 *p_over;
	enum hk3_lhbm_brt_overdrive_group grp;

	EXYNOS_DCS_WRITE_TABLE(ctx, unlock_cmd_f0);
	EXYNOS_DCS_WRITE_TABLE(ctx, lhbm_brightness_index);
	ret = mipi_dsi_dcs_read(dsi, lhbm_brightness_reg, p_norm, LHBM_BRT_LEN);
	EXYNOS_DCS_WRITE_TABLE(ctx, lock_cmd_f0);
	if (ret != LHBM_BRT_LEN) {
		dev_err(ctx->dev, "failed to read lhbm brightness ret=%d\n", ret);
		return;
	}
	dev_dbg(ctx->dev, "lhbm normal brightness: %*ph\n", LHBM_BRT_LEN, p_norm);

	/* 0 nit */
	grp = LHBM_OVERDRIVE_GRP_0_NIT;
	p_over = ctl->brt_overdrive[grp];
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_R_FINE], p_norm[LHBM_R_COARSE],
		&p_over[LHBM_R_FINE], &p_over[LHBM_R_COARSE],
		0x00, 0x00, 0x01, 0x01);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_G_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_G_FINE], &g_coarse,
		0x00, 0x00, 0x10, 0x10);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_B_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_B_FINE], &b_coarse,
		0x5C, 0x79, 0x01, 0x02);
	p_over[LHBM_GB_COARSE] = (g_coarse & 0xF0) | (b_coarse & 0x0F);

	/* 0 - 6 nits */
	grp = LHBM_OVERDRIVE_GRP_6_NIT;
	p_over = ctl->brt_overdrive[grp];
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_R_FINE], p_norm[LHBM_R_COARSE],
		&p_over[LHBM_R_FINE], &p_over[LHBM_R_COARSE],
		0x63, 0x7A, 0x00, 0x01);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_G_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_G_FINE], &g_coarse,
		0x70, 0x80, 0x00, 0x10);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_B_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_B_FINE], &b_coarse,
		0x90, 0x43, 0x00, 0x01);
	p_over[LHBM_GB_COARSE] = (g_coarse & 0xF0) | (b_coarse & 0x0F);

	/* 6 - 100 nits */
	grp = LHBM_OVERDRIVE_GRP_50_NIT;
	p_over = ctl->brt_overdrive[grp];
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_R_FINE], p_norm[LHBM_R_COARSE],
		&p_over[LHBM_R_FINE], &p_over[LHBM_R_COARSE],
		0x45, 0x8F, 0x00, 0x01);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_G_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_G_FINE], &g_coarse,
		0x55, 0x98, 0x00, 0x10);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_B_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_B_FINE], &b_coarse,
		0x75, 0x58, 0x00, 0x01);
	p_over[LHBM_GB_COARSE] = (g_coarse & 0xF0) | (b_coarse & 0x0F);

	/* 100 - 300 nits */
	grp = LHBM_OVERDRIVE_GRP_300_NIT;
	p_over = ctl->brt_overdrive[grp];
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_R_FINE], p_norm[LHBM_R_COARSE],
		&p_over[LHBM_R_FINE], &p_over[LHBM_R_COARSE],
		0x44, 0xA2, 0x00, 0x01);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_G_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_G_FINE], &g_coarse,
		0x41, 0xAC, 0x00, 0x10);
	hk3_calc_lhbm_od_brightness(p_norm[LHBM_B_FINE], p_norm[LHBM_GB_COARSE],
		&p_over[LHBM_B_FINE], &b_coarse,
		0x55, 0x78, 0x00, 0x01);
	p_over[LHBM_GB_COARSE] = (g_coarse & 0xF0) | (b_coarse & 0x0F);

	for (grp = 0; grp < LHBM_OVERDRIVE_GRP_MAX; grp++) {
		dev_dbg(ctx->dev, "lhbm overdrive brightness[%d]: %*ph\n",
			grp, LHBM_BRT_LEN, ctl->brt_overdrive[grp]);
	}
}

static void hk3_panel_init(struct exynos_panel *ctx)
{
#ifdef CONFIG_DEBUG_FS
	struct dentry *csroot = ctx->debugfs_cmdset_entry;
	struct hk3_panel *spanel = to_spanel(ctx);

	exynos_panel_debugfs_create_cmdset(ctx, csroot, &hk3_init_cmd_set, "init");
	debugfs_create_bool("force_changeable_te", 0644, ctx->debugfs_entry,
				&spanel->force_changeable_te);
	debugfs_create_bool("force_changeable_te2", 0644, ctx->debugfs_entry,
				&spanel->force_changeable_te2);
	debugfs_create_bool("force_za_off", 0644, ctx->debugfs_entry,
				&spanel->force_za_off);
	debugfs_create_u8("hw_acl_setting", 0644, ctx->debugfs_entry,
				&spanel->hw_acl_setting);
#endif

#ifdef PANEL_FACTORY_BUILD
	ctx->panel_idle_enabled = false;
#endif
	hk3_lhbm_brightness_init(ctx);

	if (ctx->panel_rev < PANEL_REV_DVT1) {
		/* AOD Transition Set */
		EXYNOS_DCS_BUF_ADD_SET(ctx, unlock_cmd_f0);
		EXYNOS_DCS_BUF_ADD(ctx, 0xB0, 0x00, 0x03, 0xBB);
		EXYNOS_DCS_BUF_ADD(ctx, 0xBB, 0x41);
		EXYNOS_DCS_BUF_ADD_SET_AND_FLUSH(ctx, lock_cmd_f0);
	}

	if (ctx->panel_rev >= PANEL_REV_DVT1)
		hk3_negative_field_setting(ctx);

	spanel->tz = thermal_zone_get_zone_by_name("disp_therm");
	if (IS_ERR_OR_NULL(spanel->tz))
		dev_err(ctx->dev, "%s: failed to get thermal zone disp_therm\n",
			__func__);
}

static int hk3_panel_probe(struct mipi_dsi_device *dsi)
{
	struct hk3_panel *spanel;

	spanel = devm_kzalloc(&dsi->dev, sizeof(*spanel), GFP_KERNEL);
	if (!spanel)
		return -ENOMEM;

	spanel->base.op_hz = 120;
	spanel->hw_vrefresh = 60;
	spanel->hw_acl_setting = 0;
	spanel->hw_za_enabled = false;
	spanel->hw_dbv = 0;
	/* ddic default temp */
	spanel->hw_temp = 25;
	spanel->pending_temp_update = false;
	spanel->is_pixel_off = false;
	spanel->read_vreg = false;

	return exynos_panel_common_init(dsi, &spanel->base);
}

static int hk3_panel_config(struct exynos_panel *ctx)
{
	exynos_panel_model_init(ctx, PROJECT, 0);

	return 0;
}

static const struct drm_panel_funcs hk3_drm_funcs = {
	.disable = hk3_disable,
	.unprepare = exynos_panel_unprepare,
	.prepare = exynos_panel_prepare,
	.enable = hk3_enable,
	.get_modes = exynos_panel_get_modes,
};

static const struct exynos_panel_funcs hk3_exynos_funcs = {
	.set_brightness = hk3_set_brightness,
	.set_lp_mode = hk3_set_lp_mode,
	.set_nolp_mode = hk3_set_nolp_mode,
	.set_binned_lp = exynos_panel_set_binned_lp,
	.set_hbm_mode = hk3_set_hbm_mode,
	.set_dimming_on = hk3_set_dimming_on,
	.set_local_hbm_mode = hk3_set_local_hbm_mode,
	.set_local_hbm_mode_post = hk3_set_local_hbm_mode_post,
	.is_mode_seamless = hk3_is_mode_seamless,
	.mode_set = hk3_mode_set,
	.panel_init = hk3_panel_init,
	.panel_config = hk3_panel_config,
	.get_panel_rev = hk3_get_panel_rev,
	.get_te2_edges = exynos_panel_get_te2_edges,
	.configure_te2_edges = exynos_panel_configure_te2_edges,
	.update_te2 = hk3_update_te2,
	.commit_done = hk3_commit_done,
	.atomic_check = hk3_atomic_check,
	.set_self_refresh = hk3_set_self_refresh,
	.set_op_hz = hk3_set_op_hz,
	.read_id = hk3_read_id,
	.get_te_usec = hk3_get_te_usec,
	.set_acl_mode = hk3_set_acl_mode,
	.run_normal_mode_work = hk3_normal_mode_work,
	.pre_update_ffc = hk3_pre_update_ffc,
	.update_ffc = hk3_update_ffc,
	.get_pwr_vreg = hk3_get_pwr_vreg,
};

const struct brightness_capability hk3_brightness_capability = {
	.normal = {
		.nits = {
			.min = 2,
			.max = 1000,
		},
		.level = {
			.min = 196,
			.max = 3307,
		},
		.percentage = {
			.min = 0,
			.max = 63,
		},
	},
	.hbm = {
		.nits = {
			.min = 1000,
			.max = 1600,
		},
		.level = {
			.min = 3308,
			.max = 4095,
		},
		.percentage = {
			.min = 63,
			.max = 100,
		},
	},
};

const struct exynos_panel_desc google_hk3 = {
	.data_lane_cnt = 4,
	.max_brightness = 4095,
	.dft_brightness = 1353, /* 140 nits */
	.brt_capability = &hk3_brightness_capability,
	.dbv_extra_frame = true,
	/* supported HDR format bitmask : 1(DOLBY_VISION), 2(HDR10), 3(HLG) */
	.hdr_formats = BIT(2) | BIT(3),
	.max_luminance = 10000000,
	.max_avg_luminance = 1200000,
	.min_luminance = 5,
	.bl_range = hk3_bl_range,
	.bl_num_ranges = ARRAY_SIZE(hk3_bl_range),
	.modes = hk3_modes,
	.num_modes = ARRAY_SIZE(hk3_modes),
	.vrefresh_range = hk3_vrefresh_range,
	.vrefresh_range_count = ARRAY_SIZE(hk3_vrefresh_range),
	.lp_mode = hk3_lp_modes,
	.lp_mode_count = ARRAY_SIZE(hk3_lp_modes),
	.lp_vrefresh_range = hk3_lp_vrefresh_range,
	.lp_vrefresh_range_count = ARRAY_SIZE(hk3_lp_vrefresh_range),
	.binned_lp = hk3_binned_lp,
	.num_binned_lp = ARRAY_SIZE(hk3_binned_lp),
	.is_panel_idle_supported = true,
	.no_lhbm_rr_constraints = true,
	.panel_func = &hk3_drm_funcs,
	.exynos_panel_func = &hk3_exynos_funcs,
	.lhbm_effective_delay_frames = 1,
	.lhbm_post_cmd_delay_frames = 1,
	.normal_mode_work_delay_ms = 30000,
	.default_dsi_hs_clk = MIPI_DSI_FREQ_DEFAULT,
	.reset_timing_ms = {1, 1, 5},
	.reg_ctrl_enable = {
		{PANEL_REG_ID_VDDI, 1},
		{PANEL_REG_ID_VCI, 10},
	},
	.reg_ctrl_post_enable = {
		{PANEL_REG_ID_VDDD, 1},
	},
	.reg_ctrl_pre_disable = {
		{PANEL_REG_ID_VDDD, 1},
	},
	.reg_ctrl_disable = {
		{PANEL_REG_ID_VCI, 1},
		{PANEL_REG_ID_VDDI, 1},
	},
};

static const struct of_device_id exynos_panel_of_match[] = {
	{ .compatible = "google,hk3", .data = &google_hk3 },
	{ }
};
MODULE_DEVICE_TABLE(of, exynos_panel_of_match);

static struct mipi_dsi_driver exynos_panel_driver = {
	.probe = hk3_panel_probe,
	.remove = exynos_panel_remove,
	.driver = {
		.name = "panel-google-hk3",
		.of_match_table = exynos_panel_of_match,
	},
};
module_mipi_dsi_driver(exynos_panel_driver);

MODULE_AUTHOR("Chris Lu <luchris@google.com>");
MODULE_DESCRIPTION("MIPI-DSI based Google HK3 panel driver");
MODULE_LICENSE("GPL");