tzh
2024-08-15 d4a1bd480003f3e1a0590bc46fbcb24f05652ca7
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
/*
 * drivers/leds/leds-sunxi.c - Allwinner RGB LED Driver
 *
 * Copyright (C) 2018 Allwinner Technology Limited. All rights reserved.
 * Albert Yu <yuxyun@allwinnertech.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
 
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/leds.h>
#include <linux/io.h>
#include <linux/of.h>
#include <linux/slab.h>
#include <linux/pm.h>
#include <linux/clk.h>
#include <linux/dmaengine.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/pinctrl/consumer.h>
#include <linux/dma-mapping.h>
#include <linux/dma/sunxi-dma.h>
#include <linux/debugfs.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/regulator/consumer.h>
 
#include "leds-sunxi.h"
 
/* For debug */
#define LED_ERR(fmt, arg...) pr_err("%s()%d - "fmt, __func__, __LINE__, ##arg)
 
static u32 debug_mask = 1;
#define dprintk(level_mask, fmt, arg...)                \
do {                                    \
   if (unlikely(debug_mask & level_mask))                \
       pr_warn("%s()%d - "fmt, __func__, __LINE__, ##arg);    \
} while (0)
 
static struct sunxi_led *sunxi_led;
 
static void sunxi_ledc_trans_data(struct sunxi_led *led);
 
void led_dump_reg(struct sunxi_led *led, u32 offset, u32 len)
{
   u32 i;
   u8 buf[64], cnt = 0;
 
   for (i = 0; i < len; i = i + REG_INTERVAL) {
       if (i%HEXADECIMAL == 0)
           cnt += sprintf(buf + cnt, "0x%08x: ",
                   (u32)(led->res->start + offset + i));
 
       cnt += sprintf(buf + cnt, "%08x ",
               readl(led->iomem_reg_base + offset + i));
 
       if (i%HEXADECIMAL == REG_CL) {
           pr_warn("%s\n", buf);
           cnt = 0;
       }
   }
}
 
static void sunxi_clk_get(struct sunxi_led *led)
{
   struct device *dev = led->dev;
   struct device_node *np = dev->of_node;
 
   led->clk_ledc = of_clk_get(np, 0);
   if (IS_ERR(led->clk_ledc))
       LED_ERR("failed to get clk_ledc!\n");
 
   led->clk_cpuapb = of_clk_get(np, 1);
   if (IS_ERR(led->clk_cpuapb))
       LED_ERR("failed to get clk_cpuapb!\n");
}
 
static void sunxi_clk_put(struct sunxi_led *led)
{
   clk_put(led->clk_ledc);
   clk_put(led->clk_cpuapb);
   led->clk_ledc = NULL;
   led->clk_cpuapb = NULL;
}
 
static void sunxi_clk_enable(struct sunxi_led *led)
{
   clk_prepare_enable(led->clk_ledc);
}
 
static void sunxi_clk_disable(struct sunxi_led *led)
{
   clk_disable_unprepare(led->clk_ledc);
}
 
static void sunxi_clk_init(struct sunxi_led *led)
{
   sunxi_clk_get(led);
   sunxi_clk_enable(led);
}
 
static void sunxi_clk_deinit(struct sunxi_led *led)
{
   sunxi_clk_disable(led);
   sunxi_clk_put(led);
}
 
static u32 sunxi_get_reg(int offset)
{
   struct sunxi_led *led = sunxi_led;
   u32 value = ioread32(((u8 *)led->iomem_reg_base) + offset);
 
   return value;
}
 
static void sunxi_set_reg(int offset, u32 value)
{
   struct sunxi_led *led = sunxi_led;
 
   iowrite32(value, ((u8 *)led->iomem_reg_base) + offset);
}
 
static inline void sunxi_set_reset_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 mask = 0x1FFF;
   u32 min = SUNXI_RESET_TIME_MIN_NS;
   u32 max = SUNXI_RESET_TIME_MAX_NS;
 
   if (led->reset_ns < min || led->reset_ns > max) {
       LED_ERR("invalid parameter, reset_ns should be %u-%u!\n",
               min, max);
       return;
   }
 
   n = (led->reset_ns - 42) / 42;
   reg_val = sunxi_get_reg(LED_RESET_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~(mask << 16);
   reg_val |= (n << 16);
   sunxi_set_reg(LED_RESET_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_set_t1h_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 mask = 0x3F;
   u32 shift = 21;
   u32 min = SUNXI_T1H_MIN_NS;
   u32 max = SUNXI_T1H_MAX_NS;
 
   if (led->t1h_ns < min || led->t1h_ns > max) {
       LED_ERR("invalid parameter, t1h_ns should be %u-%u!\n",
               min, max);
       return;
   }
 
   n = (led->t1h_ns - 42) / 42;
   reg_val = sunxi_get_reg(LED_T01_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~(mask << shift);
   reg_val |= n << shift;
   sunxi_set_reg(LED_T01_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_set_t1l_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 mask = 0x1F;
   u32 shift = 16;
   u32 min = SUNXI_T1L_MIN_NS;
   u32 max = SUNXI_T1L_MAX_NS;
 
   if (led->t1l_ns < min || led->t1l_ns > max) {
       LED_ERR("invalid parameter, t1l_ns should be %u-%u!\n",
               min, max);
       return;
   }
 
   n = (led->t1l_ns - 42) / 42;
   reg_val = sunxi_get_reg(LED_T01_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~(mask << shift);
   reg_val |= n << shift;
   sunxi_set_reg(LED_T01_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_set_t0h_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 mask = 0x1F;
   u32 shift = 6;
   u32 min = SUNXI_T0H_MIN_NS;
   u32 max = SUNXI_T0H_MAX_NS;
 
   if (led->t0h_ns < min || led->t0h_ns > max) {
       LED_ERR("invalid parameter, t0h_ns should be %u-%u!\n",
           min, max);
       return;
   }
 
   n = (led->t0h_ns - 42) / 42;
   reg_val = sunxi_get_reg(LED_T01_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~(mask << shift);
   reg_val |= n << shift;
   sunxi_set_reg(LED_T01_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_set_t0l_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 min = SUNXI_T0L_MIN_NS;
   u32 max = SUNXI_T0L_MAX_NS;
 
   if (led->t0l_ns < min || led->t0l_ns > max) {
       LED_ERR("invalid parameter, t0l_ns should be %u-%u!\n",
               min, max);
       return;
   }
 
   n = (led->t0l_ns - 42) / 42;
   reg_val = sunxi_get_reg(LED_T01_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~0x3F;
   reg_val |= n;
   sunxi_set_reg(LED_T01_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_set_wait_time0_ns(struct sunxi_led *led)
{
   u32 n, reg_val;
   u32 min = SUNXI_WAIT_TIME0_MIN_NS;
   u32 max = SUNXI_WAIT_TIME0_MAX_NS;
 
   if (led->wait_time0_ns < min || led->wait_time0_ns > max) {
       LED_ERR("invalid parameter, wait_time0_ns should be %u-%u!\n",
               min, max);
       return;
   }
 
   n = (led->wait_time0_ns - 42) / 42;
   reg_val = (1 << 8) | n;
   sunxi_set_reg(LEDC_WAIT_TIME0_CTRL_REG, reg_val);
}
 
static inline void sunxi_set_wait_time1_ns(struct sunxi_led *led)
{
   unsigned long long tmp, max = SUNXI_WAIT_TIME1_MAX_NS;
   u32 min = SUNXI_WAIT_TIME1_MIN_NS;
   u32 n, reg_val;
 
   if (led->wait_time1_ns < min || led->wait_time1_ns > max) {
       LED_ERR("invalid parameter, wait_time1_ns should be %u-%llu!\n",
           min, max);
       return;
   }
 
   tmp = led->wait_time1_ns;
   n = div_u64(tmp, 42);
   n -= 1;
   reg_val = (1 << 31) | n;
   sunxi_set_reg(LEDC_WAIT_TIME1_CTRL_REG, reg_val);
}
 
static inline void sunxi_set_wait_data_time_ns(struct sunxi_led *led)
{
   u32 min, max;
#ifndef SUNXI_FPGA_LEDC
   u32 mask = 0x1FFF, shift = 16, reg_val = 0, n;
#endif
   min = SUNXI_WAIT_DATA_TIME_MIN_NS;
#ifdef SUNXI_FPGA_LEDC
   /*
    * For FPGA platforms, it is easy to meet wait data timeout for
    * the obvious latency of task which is because of less cpu cores
    * and lower cpu frequency compared with IC platforms, so here we
    * permit long enough time latency.
    */
   max = SUNXI_WAIT_DATA_TIME_MAX_NS_FPGA;
#else /* SUNXI_FPGA_LEDC */
   max = SUNXI_WAIT_DATA_TIME_MAX_NS_IC;
#endif /* SUNXI_FPGA_LEDC */
 
   if (led->wait_data_time_ns < min || led->wait_data_time_ns > max) {
       LED_ERR("invalid parameter, wait_data_time_ns should be %u-%u!\n",
           min, max);
       return;
   }
 
#ifndef SUNXI_FPGA_LEDC
   n = (led->wait_data_time_ns - 42) / 42;
   reg_val &= ~(mask << shift);
   reg_val |= (n << shift);
   sunxi_set_reg(LEDC_DATA_FINISH_CNT_REG_OFFSET, reg_val);
#endif /* SUNXI_FPGA_LEDC */
}
 
static void sunxi_ledc_set_time(struct sunxi_led *led)
{
   sunxi_set_reset_ns(led);
   sunxi_set_t1h_ns(led);
   sunxi_set_t1l_ns(led);
   sunxi_set_t0h_ns(led);
   sunxi_set_t0l_ns(led);
   sunxi_set_wait_time0_ns(led);
   sunxi_set_wait_time1_ns(led);
   sunxi_set_wait_data_time_ns(led);
}
 
static void sunxi_ledc_set_length(struct sunxi_led *led)
{
   u32 reg_val;
   u32 length = led->length;
 
   if (length == 0)
       return;
 
   if (length > led->led_count)
       return;
 
   reg_val = sunxi_get_reg(LEDC_CTRL_REG_OFFSET);
   reg_val &= ~(0x1FFF << 16);
   reg_val |=  length << 16;
   sunxi_set_reg(LEDC_CTRL_REG_OFFSET, reg_val);
 
   reg_val = sunxi_get_reg(LED_RESET_TIMING_CTRL_REG_OFFSET);
   reg_val &= ~0x3FF;
   reg_val |= length - 1;
   sunxi_set_reg(LED_RESET_TIMING_CTRL_REG_OFFSET, reg_val);
}
 
static void sunxi_ledc_set_output_mode(struct sunxi_led *led, const char *str)
{
   u32 val;
   u32 mask = 0x7;
   u32 shift = 6;
   u32 reg_val = sunxi_get_reg(LEDC_CTRL_REG_OFFSET);
   if (str != NULL) {
       if (!strncmp(str, "GRB", 3))
           val = SUNXI_OUTPUT_GRB;
       else if (!strncmp(str, "GBR", 3))
           val = SUNXI_OUTPUT_GBR;
       else if (!strncmp(str, "RGB", 3))
           val = SUNXI_OUTPUT_RGB;
       else if (!strncmp(str, "RBG", 3))
           val = SUNXI_OUTPUT_RBG;
       else if (!strncmp(str, "BGR", 3))
           val = SUNXI_OUTPUT_BGR;
       else if (!strncmp(str, "BRG", 3))
           val = SUNXI_OUTPUT_BRG;
       else
           return;
   } else {
       val = led->output_mode.val;
   }
 
   reg_val &= ~(mask << shift);
   reg_val |= val;
 
   sunxi_set_reg(LEDC_CTRL_REG_OFFSET, reg_val);
 
   if (strncmp(str, led->output_mode.str, 3))
       memcpy(led->output_mode.str, str, 3);
 
   if (val != led->output_mode.val)
       led->output_mode.val = val;
}
 
static void sunxi_ledc_enable_irq(u32 mask)
{
   u32 reg_val = 0;
 
   reg_val |= mask;
   sunxi_set_reg(LEDC_INT_CTRL_REG_OFFSET, reg_val);
}
 
static void sunxi_ledc_disable_irq(u32 mask)
{
   u32 reg_val = 0;
 
   reg_val = sunxi_get_reg(LEDC_INT_CTRL_REG_OFFSET);
   reg_val &= ~mask;
   sunxi_set_reg(LEDC_INT_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_ledc_enable(struct sunxi_led *led)
{
   u32 reg_val;
 
   reg_val = sunxi_get_reg(LEDC_CTRL_REG_OFFSET);
   reg_val |=  1;
   sunxi_set_reg(LEDC_CTRL_REG_OFFSET, reg_val);
}
 
static inline void sunxi_ledc_reset(struct sunxi_led *led)
{
   u32 reg_val = sunxi_get_reg(LEDC_CTRL_REG_OFFSET);
 
   sunxi_ledc_disable_irq(LEDC_TRANS_FINISH_INT_EN | LEDC_FIFO_CPUREQ_INT_EN
           | LEDC_WAITDATA_TIMEOUT_INT_EN | LEDC_FIFO_OVERFLOW_INT_EN
           | LEDC_GLOBAL_INT_EN);
 
   if (debug_mask & DEBUG_INFO2) {
       dprintk(DEBUG_INFO2, "dump reg:\n");
       led_dump_reg(led, 0, 0x30);
   }
 
   if (led->dma_chan)
       dmaengine_terminate_all(led->dma_chan);
 
   reg_val |= 1 << 1;
   sunxi_set_reg(LEDC_CTRL_REG_OFFSET, reg_val);
}
 
#ifdef CONFIG_DEBUG_FS
static ssize_t reset_ns_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_RESET_TIME_MIN_NS;
   max = SUNXI_RESET_TIME_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->reset_ns = val;
   sunxi_set_reset_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, reset_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t reset_ns_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->reset_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations reset_ns_fops = {
   .owner = THIS_MODULE,
   .write = reset_ns_write,
   .read  = reset_ns_read,
};
 
static ssize_t t1h_ns_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_T1H_MIN_NS;
   max = SUNXI_T1H_MAX_NS;
 
   if (count >= sizeof(buffer))
       return -EINVAL;
 
   if (copy_from_user(buffer, buf, count))
       return -EFAULT;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       return -EINVAL;
 
   if (val < min || val > max)
       goto err_out;
 
   led->t1h_ns = val;
 
   sunxi_set_t1h_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, t1h_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t t1h_ns_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->t1h_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations t1h_ns_fops = {
   .owner = THIS_MODULE,
   .write = t1h_ns_write,
   .read  = t1h_ns_read,
};
 
static ssize_t t1l_ns_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_T1L_MIN_NS;
   max = SUNXI_T1L_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->t1l_ns = val;
   sunxi_set_t1l_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, t1l_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t t1l_ns_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->t1l_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations t1l_ns_fops = {
   .owner = THIS_MODULE,
   .write = t1l_ns_write,
   .read  = t1l_ns_read,
};
 
static ssize_t t0h_ns_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_T0H_MIN_NS;
   max = SUNXI_T0H_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->t0h_ns = val;
   sunxi_set_t0h_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, t0h_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t t0h_ns_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->t0h_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations t0h_ns_fops = {
   .owner = THIS_MODULE,
   .write = t0h_ns_write,
   .read  = t0h_ns_read,
};
 
static ssize_t t0l_ns_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_T0L_MIN_NS;
   max = SUNXI_T0L_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->t0l_ns = val;
   sunxi_set_t0l_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, t0l_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t t0l_ns_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->t0l_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations t0l_ns_fops = {
   .owner = THIS_MODULE,
   .write = t0l_ns_write,
   .read  = t0l_ns_read,
};
 
static ssize_t wait_time0_ns_write(struct file *filp, const char __user *buf,
               size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_WAIT_TIME0_MIN_NS;
   max = SUNXI_WAIT_TIME0_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->wait_time0_ns = val;
   sunxi_set_wait_time0_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, wait_time0_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t wait_time0_ns_read(struct file *filp, char __user *buf,
               size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->wait_time0_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations wait_time0_ns_fops = {
   .owner = THIS_MODULE,
   .write = wait_time0_ns_write,
   .read  = wait_time0_ns_read,
};
 
static ssize_t wait_time1_ns_write(struct file *filp, const char __user *buf,
               size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min;
   unsigned long long max;
   unsigned long long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_WAIT_TIME1_MIN_NS;
   max = SUNXI_WAIT_TIME1_MAX_NS;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoull(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->wait_time1_ns = val;
   sunxi_set_wait_time1_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, wait_time1_ns should be %u-%lld!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t wait_time1_ns_read(struct file *filp, char __user *buf,
               size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%lld\n", led->wait_time1_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations wait_time1_ns_fops = {
   .owner = THIS_MODULE,
   .write = wait_time1_ns_write,
   .read  = wait_time1_ns_read,
};
 
static ssize_t wait_data_time_ns_write(struct file *filp,
               const char __user *buf,
               size_t count, loff_t *offp)
{
   int err;
   char buffer[64];
   u32 min, max;
   unsigned long val;
   struct sunxi_led *led = sunxi_led;
 
   min = SUNXI_WAIT_DATA_TIME_MIN_NS;
#ifdef SUNXI_FPGA_LEDC
   max = SUNXI_WAIT_DATA_TIME_MAX_NS_FPGA;
#else
   max = SUNXI_WAIT_DATA_TIME_MAX_NS_IC;
#endif
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   err = kstrtoul(buffer, 10, &val);
   if (err)
       goto err_out;
 
   if (val < min || val > max)
       goto err_out;
 
   led->wait_data_time_ns = val;
   sunxi_set_wait_data_time_ns(led);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter, wait_data_time_ns should be %u-%u!\n",
       min, max);
 
   return -EINVAL;
}
 
static ssize_t wait_data_time_ns_read(struct file *filp, char __user *buf,
               size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%u\n", led->wait_data_time_ns);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations wait_data_time_ns_fops = {
   .owner = THIS_MODULE,
   .write = wait_data_time_ns_write,
   .read  = wait_data_time_ns_read,
};
 
static int data_show(struct seq_file *s, void *data)
{
   int i;
   struct sunxi_led *led = sunxi_led;
 
   for (i = 0; i < led->led_count; i++) {
       if (!(i % 4)) {
           if (i + 4 <= led->led_count)
               seq_printf(s, "%04d-%04d", i, i + 4);
           else
               seq_printf(s, "%04d-%04d", i, led->led_count);
       }
       seq_printf(s, " 0x%08x", led->data[i]);
       if (((i % 4) == 3) || (i == led->led_count - 1))
           seq_puts(s, "\n");
   }
 
   return 0;
}
 
static void sunxi_ledc_set_dma_mode(struct sunxi_led *led)
{
   u32 reg_val = 0;
 
   reg_val |= 1 << 5;
   sunxi_set_reg(LEDC_DMA_CTRL_REG, reg_val);
 
   sunxi_ledc_disable_irq(LEDC_FIFO_CPUREQ_INT_EN);
}
 
static void sunxi_ledc_set_cpu_mode(struct sunxi_led *led)
{
   u32 reg_val = 0;
 
   reg_val &= ~(1 << 5);
   sunxi_set_reg(LEDC_DMA_CTRL_REG, reg_val);
 
   sunxi_ledc_enable_irq(LEDC_FIFO_CPUREQ_INT_EN);
}
 
static int data_open(struct inode *inode, struct file *file)
{
   return single_open(file, data_show, inode->i_private);
}
 
static const struct file_operations data_fops = {
   .owner = THIS_MODULE,
   .open  = data_open,
   .read = seq_read,
   .llseek = seq_lseek,
   .release = single_release,
};
 
static ssize_t output_mode_write(struct file *filp, const char __user *buf,
           size_t count, loff_t *offp)
{
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   if (count >= sizeof(buffer))
       goto err_out;
 
   if (copy_from_user(buffer, buf, count))
       goto err_out;
 
   buffer[count] = '\0';
 
   sunxi_ledc_set_output_mode(led, buffer);
 
   *offp += count;
 
   return count;
 
err_out:
   LED_ERR("invalid parameter!\n");
 
   return -EINVAL;
}
 
static ssize_t output_mode_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   struct sunxi_led *led = sunxi_led;
 
   r = snprintf(buffer, 64, "%s\n", led->output_mode.str);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations output_mode_fops = {
   .owner = THIS_MODULE,
   .write = output_mode_write,
   .read  = output_mode_read,
};
 
static ssize_t hwversion_read(struct file *filp, char __user *buf,
           size_t count, loff_t *offp)
{
   int r;
   char buffer[64];
   u32 reg_val, major_ver, minor_ver;
 
   reg_val = sunxi_get_reg(LEDC_VER_NUM_REG);
   major_ver = reg_val >> 16;
   minor_ver = reg_val & 0xF;
 
   r = snprintf(buffer, 64, "r%up%u\n", major_ver, minor_ver);
 
   return simple_read_from_buffer(buf, count, offp, buffer, r);
}
 
static const struct file_operations hwversion_fops = {
   .owner = THIS_MODULE,
   .read  = hwversion_read,
};
 
static void sunxi_led_create_debugfs(struct sunxi_led *led)
{
   struct dentry *debugfs_dir, *debugfs_file;
 
   debugfs_dir = debugfs_create_dir("sunxi_leds", NULL);
   if (IS_ERR_OR_NULL(debugfs_dir)) {
       LED_ERR("debugfs_create_dir failed!\n");
       return;
   }
 
   led->debugfs_dir = debugfs_dir;
 
   debugfs_file = debugfs_create_file("reset_ns", 0660,
               debugfs_dir, NULL, &reset_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for reset_ns failed!\n");
 
   debugfs_file = debugfs_create_file("t1h_ns", 0660,
               debugfs_dir, NULL, &t1h_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for t1h_ns failed!\n");
 
   debugfs_file = debugfs_create_file("t1l_ns", 0660,
               debugfs_dir, NULL, &t1l_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for t1l_ns failed!\n");
 
   debugfs_file = debugfs_create_file("t0h_ns", 0660,
               debugfs_dir, NULL, &t0h_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for t0h_ns failed!\n");
 
   debugfs_file = debugfs_create_file("t0l_ns", 0660,
               debugfs_dir, NULL, &t0l_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for t0l_ns failed!\n");
 
   debugfs_file = debugfs_create_file("wait_time0_ns", 0660,
               debugfs_dir, NULL, &wait_time0_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for wait_time0_ns failed!\n");
 
   debugfs_file = debugfs_create_file("wait_time1_ns", 0660,
               debugfs_dir, NULL, &wait_time1_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for wait_time1_ns failed!\n");
 
   debugfs_file = debugfs_create_file("wait_data_time_ns", 0660,
               debugfs_dir, NULL, &wait_data_time_ns_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for wait_data_time_ns failed!\n");
 
   debugfs_file = debugfs_create_file("data", 0440,
               debugfs_dir, NULL, &data_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for data failed!\n");
 
   debugfs_file = debugfs_create_file("output_mode", 0660,
               debugfs_dir, NULL, &output_mode_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for output_mode failed!\n");
 
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for trans_mode failed!\n");
 
   debugfs_file = debugfs_create_file("hwversion", 0440,
               debugfs_dir, NULL, &hwversion_fops);
   if (!debugfs_file)
       LED_ERR("debugfs_create_file for hwversion failed!\n");
}
 
static void sunxi_led_remove_debugfs(struct sunxi_led *led)
{
   debugfs_remove_recursive(led->debugfs_dir);
}
#endif /* CONFIG_DEBUG_FS */
 
static void sunxi_ledc_dma_callback(void *param)
{
   dprintk(DEBUG_INFO, "finish\n");
}
 
static void sunxi_ledc_trans_data(struct sunxi_led *led)
{
   int i, err;
   size_t size;
   unsigned int slave_id;
   unsigned long flags;
   phys_addr_t dst_addr;
   struct dma_slave_config slave_config;
   struct device *dev = led->dev;
   struct dma_async_tx_descriptor *dma_desc;
 
   if (led->length <= SUNXI_LEDC_FIFO_DEPTH) {
       dprintk(DEBUG_INFO, "cpu xfer\n");
       led->start_time = current_kernel_time64();
       sunxi_ledc_set_time(led);
       sunxi_ledc_set_output_mode(led, led->output_mode.str);
       sunxi_ledc_set_cpu_mode(led);
       sunxi_ledc_set_length(led);
       sunxi_ledc_enable_irq(LEDC_TRANS_FINISH_INT_EN
               | LEDC_WAITDATA_TIMEOUT_INT_EN
               | LEDC_FIFO_OVERFLOW_INT_EN
               | LEDC_GLOBAL_INT_EN);
       sunxi_ledc_enable(led);
 
       for (i = 0; i < led->length; i++)
           sunxi_set_reg(LEDC_DATA_REG_OFFSET, led->data[i]);
   } else {
       dprintk(DEBUG_INFO, "dma xfer\n");
       size = led->length * 4;
       led->src_dma = dma_map_single(dev, led->data,
                   size, DMA_TO_DEVICE);
 
       dst_addr = led->res->start + LEDC_DATA_REG_OFFSET;
       slave_id = sunxi_slave_id(DRQDST_LEDC, DRQSRC_SDRAM);
       flags = DMA_PREP_INTERRUPT | DMA_CTRL_ACK;
 
       slave_config.direction = DMA_MEM_TO_DEV;
       slave_config.src_addr = led->src_dma;
       slave_config.dst_addr = dst_addr;
       slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
       slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
       slave_config.src_maxburst = 4;
       slave_config.dst_maxburst = 4;
       slave_config.slave_id = slave_id;
       err = dmaengine_slave_config(led->dma_chan, &slave_config);
       if (err < 0) {
           LED_ERR("dmaengine_slave_config failed!\n");
           dma_unmap_single(dev, led->src_dma,
                   size, DMA_TO_DEVICE);
           return;
       }
 
       dma_desc = dmaengine_prep_slave_single(led->dma_chan,
                           led->src_dma,
                           size,
                           DMA_MEM_TO_DEV,
                           flags);
       if (!dma_desc) {
           LED_ERR("dmaengine_prep_slave_single failed!\n");
           dma_unmap_single(dev, led->src_dma,
                   size, DMA_TO_DEVICE);
           return;
       }
 
       dma_desc->callback = sunxi_ledc_dma_callback;
 
       dmaengine_submit(dma_desc);
       dma_async_issue_pending(led->dma_chan);
 
       led->start_time = current_kernel_time64();
       sunxi_ledc_set_time(led);
       sunxi_ledc_set_output_mode(led, led->output_mode.str);
       sunxi_ledc_set_dma_mode(led);
       sunxi_ledc_set_length(led);
       sunxi_ledc_enable_irq(LEDC_TRANS_FINISH_INT_EN | LEDC_WAITDATA_TIMEOUT_INT_EN
               | LEDC_FIFO_OVERFLOW_INT_EN | LEDC_GLOBAL_INT_EN);
       sunxi_ledc_enable(led);
   }
}
 
static inline void sunxi_ledc_clear_all_irq(void)
{
   u32 reg_val = sunxi_get_reg(LEDC_INT_STS_REG_OFFSET);
 
   reg_val &= ~0x1F;
   sunxi_set_reg(LEDC_INT_STS_REG_OFFSET, reg_val);
}
 
static inline void sunxi_ledc_clear_irq(enum sunxi_ledc_irq_status_reg irq)
{
   u32 reg_val = sunxi_get_reg(LEDC_INT_STS_REG_OFFSET);
 
   reg_val &= ~irq;
   sunxi_set_reg(LEDC_INT_STS_REG_OFFSET, reg_val);
}
 
static int sunxi_ledc_complete(struct sunxi_led *led)
{
   unsigned long flags = 0;
   unsigned long timeout = 0;
 
   timeout = wait_event_timeout(led->wait, led->result, 5*HZ);
   if (timeout == 0) {
       LED_ERR("led xfer timeout\n");
       return -ETIME;
   } else if (led->result == RESULT_ERR) {
       return -ECOMM;
   }
 
   dprintk(DEBUG_INFO, "xfer complete\n");
 
   spin_lock_irqsave(&led->lock, flags);
   led->result = 0;
   spin_unlock_irqrestore(&led->lock, flags);
 
   return 0;
}
 
 
static irqreturn_t sunxi_ledc_irq_handler(int irq, void *dev_id)
{
   unsigned long flags;
   long delta_time_ns;
   u32 irq_status, max_ns;
   struct sunxi_led *led = sunxi_led;
   struct device *dev = led->dev;
   struct timespec64 current_time;
 
   spin_lock_irqsave(&led->lock, flags);
 
   irq_status = sunxi_get_reg(LEDC_INT_STS_REG_OFFSET);
 
   sunxi_ledc_clear_all_irq();
 
   if (irq_status & LEDC_TRANS_FINISH_INT) {
       if (led->dma_chan)
           dma_unmap_single(dev,
                   led->src_dma, led->length * 4,
                   DMA_TO_DEVICE);
       sunxi_ledc_reset(led);
       led->length = 0;
       led->result = RESULT_COMPLETE;
       wake_up(&led->wait);
       goto out;
   }
 
   if (irq_status & LEDC_WAITDATA_TIMEOUT_INT) {
       current_time = current_kernel_time64();
       delta_time_ns = current_time.tv_sec - led->start_time.tv_sec;
       delta_time_ns *= 1000 * 1000 * 1000;
       delta_time_ns += current_time.tv_nsec - led->start_time.tv_nsec;
 
       max_ns = led->wait_data_time_ns;
 
       if (delta_time_ns <= max_ns) {
           spin_unlock_irqrestore(&led->lock, flags);
           return IRQ_HANDLED;
       }
 
       if (led->dma_chan)
           dmaengine_terminate_all(led->dma_chan);
 
       sunxi_ledc_reset(led);
 
       if (delta_time_ns <= max_ns * 2) {
           sunxi_ledc_trans_data(led);
       } else {
           LED_ERR("wait time is more than %d ns,"
               "going to reset ledc and drop this operation!\n",
               max_ns);
           led->result = RESULT_ERR;
           wake_up(&led->wait);
           led->length = 0;
       }
 
       goto out;
   }
 
   if (irq_status & LEDC_FIFO_OVERFLOW_INT) {
       LED_ERR("there exists fifo overflow issue, irq_status=0x%x!\n",
               irq_status);
       sunxi_ledc_reset(led);
       led->result = RESULT_ERR;
       wake_up(&led->wait);
       led->length = 0;
       goto out;
   }
 
out:
   spin_unlock_irqrestore(&led->lock, flags);
   return IRQ_HANDLED;
}
 
static int sunxi_ledc_irq_init(struct sunxi_led *led)
{
   int err;
   struct device *dev = led->dev;
   unsigned long flags = 0;
   const char *name = "ledcirq";
   struct platform_device *pdev;
 
   pdev = container_of(dev, struct platform_device, dev);
 
   spin_lock_init(&led->lock);
 
   led->irqnum = platform_get_irq(pdev, 0);
   if (led->irqnum < 0)
       LED_ERR("failed to get ledc irq!\n");
 
   err = request_irq(led->irqnum, sunxi_ledc_irq_handler,
               flags, name, dev);
   if (err) {
       LED_ERR("failed to install IRQ handler for irqnum %d\n",
           led->irqnum);
       return -EPERM;
   }
 
   return 0;
}
 
static void sunxi_ledc_irq_deinit(struct sunxi_led *led)
{
   free_irq(led->irqnum, led->dev);
   sunxi_ledc_disable_irq(LEDC_TRANS_FINISH_INT_EN | LEDC_FIFO_CPUREQ_INT_EN
           | LEDC_WAITDATA_TIMEOUT_INT_EN | LEDC_FIFO_OVERFLOW_INT_EN
           | LEDC_GLOBAL_INT_EN);
}
 
static void sunxi_ledc_pinctrl_init(struct sunxi_led *led)
{
   struct device *dev = led->dev;
   struct pinctrl *pinctrl = devm_pinctrl_get_select_default(dev);
 
   led->pctrl = pinctrl;
   if (IS_ERR(pinctrl))
       LED_ERR("devm_pinctrl_get_select_default failed!\n");
}
 
static int led_regulator_request(struct sunxi_led *led)
{
   struct regulator *regu = NULL;
 
   /* Consider "n*" as nocare. Support "none", "nocare", "null", "" etc. */
   if ((led->regulator_id[0] == 'n') || (led->regulator_id[0] == 0))
       return 0;
 
   regu = regulator_get(NULL, led->regulator_id);
   if (IS_ERR(regu)) {
       LED_ERR("get regulator %s failed!\n", led->regulator_id);
       return -1;
   }
   led->regulator = regu;
 
   return 0;
}
 
static int led_regulator_release(struct sunxi_led *led)
{
   if (led->regulator == NULL)
       return 0;
 
   regulator_put(led->regulator);
   led->regulator = NULL;
 
   return 1;
}
 
static int sunxi_set_led_brightness(struct led_classdev *led_cdev,
           enum led_brightness value)
{
   unsigned long flags;
   u32 r = 0, g = 0, b = 0, shift, old_data, new_data, length;
   struct sunxi_led_info *pinfo;
   struct sunxi_led_classdev_group *pcdev_group;
   struct sunxi_led *led = sunxi_led;
   unsigned long diff_time;
   struct timeval start, end;
 
   do_gettimeofday(&start);
   pinfo = container_of(led_cdev, struct sunxi_led_info, cdev);
 
   switch (pinfo->type) {
   case LED_TYPE_G:
       pcdev_group = container_of(pinfo,
           struct sunxi_led_classdev_group, g);
       g = value;
       shift = 16;
       break;
   case LED_TYPE_R:
       pcdev_group = container_of(pinfo,
           struct sunxi_led_classdev_group, r);
       r = value;
       shift = 8;
       break;
 
   case LED_TYPE_B:
       pcdev_group = container_of(pinfo,
           struct sunxi_led_classdev_group, b);
       b = value;
       shift = 0;
       break;
   default:
       LED_ERR("led not support %d type!\n",
               pinfo->type);
       return -EINVAL;
   }
 
   old_data = led->data[pcdev_group->led_num];
   if (((old_data >> shift) & 0xFF) == value)
       return 0;
 
   if (pinfo->type != LED_TYPE_R)
       r = pcdev_group->r.cdev.brightness;
   if (pinfo->type != LED_TYPE_G)
       g = pcdev_group->g.cdev.brightness;
   if (pinfo->type != LED_TYPE_B)
       b = pcdev_group->b.cdev.brightness;
 
   /* LEDC treats input data as GRB by default */
   new_data = (g << 16) | (r << 8) | b;
   length = pcdev_group->led_num + 1;
 
   spin_lock_irqsave(&led->lock, flags);
   led->data[pcdev_group->led_num] = new_data;
   led->length = length;
   spin_unlock_irqrestore(&led->lock, flags);
 
   sunxi_ledc_trans_data(led);
   if (debug_mask & DEBUG_INFO2) {
       dprintk(DEBUG_INFO2, "dump reg:\n");
       led_dump_reg(led, 0, 0x30);
   }
 
   sunxi_ledc_complete(led);
 
   do_gettimeofday(&end);
   diff_time = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec
       - start.tv_usec;
   if (debug_mask & DEBUG_INFO1)
       pr_warn("num = %03u, time = %ld us\n", length, diff_time);
 
   return 0;
}
 
static int sunxi_register_led_classdev(struct sunxi_led *led)
{
   int i, err;
   size_t size;
   struct device *dev = led->dev;
   struct led_classdev *pcdev;
 
   if (!led->led_count)
       led->led_count = SUNXI_DEFAULT_LED_COUNT;
 
   size = sizeof(struct sunxi_led_classdev_group) * led->led_count;
   led->pcdev_group = kzalloc(size, GFP_KERNEL);
   if (!led->pcdev_group)
       return -ENOMEM;
 
   for (i = 0; i < led->led_count; i++) {
       led->pcdev_group[i].r.type = LED_TYPE_R;
       pcdev = &led->pcdev_group[i].r.cdev;
       pcdev->name = kzalloc(16, GFP_KERNEL);
       sprintf((char *)pcdev->name, "sunxi_led%dr", i);
       pcdev->brightness = LED_OFF;
       pcdev->brightness_set_blocking = sunxi_set_led_brightness;
       pcdev->dev = dev;
       err = led_classdev_register(dev, pcdev);
       if (err < 0) {
           LED_ERR("led_classdev_register %s failed!\n",
               pcdev->name);
           return err;
       }
 
       led->pcdev_group[i].g.type = LED_TYPE_G;
       pcdev = &led->pcdev_group[i].g.cdev;
       pcdev->name = kzalloc(16, GFP_KERNEL);
       sprintf((char *)pcdev->name, "sunxi_led%dg", i);
       pcdev->brightness = LED_OFF;
       pcdev->brightness_set_blocking = sunxi_set_led_brightness;
       pcdev->dev = dev;
       err = led_classdev_register(dev, pcdev);
       if (err < 0) {
           LED_ERR("led_classdev_register %s failed!\n",
           pcdev->name);
           return err;
       }
 
       led->pcdev_group[i].b.type = LED_TYPE_B;
       pcdev = &led->pcdev_group[i].b.cdev;
       pcdev->name = kzalloc(16, GFP_KERNEL);
       sprintf((char *)pcdev->name, "sunxi_led%db", i);
       pcdev->brightness = LED_OFF;
       pcdev->brightness_set_blocking = sunxi_set_led_brightness;
       pcdev->dev = dev;
       err = led_classdev_register(dev, pcdev);
       if (err < 0) {
           LED_ERR("led_classdev_register %s failed!\n",
                   pcdev->name);
           return err;
       }
 
       led->pcdev_group[i].led_num = i;
   }
 
   size = sizeof(u32) * led->led_count;
   led->data = kzalloc(size, GFP_KERNEL);
   if (!led->data)
       return -ENOMEM;
 
   return 0;
}
 
static void sunxi_unregister_led_classdev(struct sunxi_led *led)
{
   int i;
 
   for (i = 0; i < led->led_count; i++) {
       kfree(led->pcdev_group[i].b.cdev.name);
       led->pcdev_group[i].b.cdev.name = NULL;
       kfree(led->pcdev_group[i].g.cdev.name);
       led->pcdev_group[i].g.cdev.name = NULL;
       kfree(led->pcdev_group[i].r.cdev.name);
       led->pcdev_group[i].r.cdev.name = NULL;
       led_classdev_unregister(&led->pcdev_group[i].b.cdev);
       led_classdev_unregister(&led->pcdev_group[i].g.cdev);
       led_classdev_unregister(&led->pcdev_group[i].r.cdev);
   }
   kfree(led->data);
   led->data = NULL;
 
 
   kfree(led->pcdev_group);
   led->pcdev_group = NULL;
}
 
static inline int sunxi_get_u32_of_property(const char *propname, int *val)
{
   int err;
   struct sunxi_led *led = sunxi_led;
   struct device *dev = led->dev;
   struct device_node *np = dev->of_node;
 
   err = of_property_read_u32(np, propname, val);
   if (err < 0)
       LED_ERR("failed to get the value of propname %s!\n", propname);
 
   return err;
}
 
static inline int sunxi_get_str_of_property(const char *propname,
                   const char **out_string)
{
   int err;
   struct sunxi_led *led = sunxi_led;
   struct device *dev = led->dev;
   struct device_node *np = dev->of_node;
 
   err = of_property_read_string(np, propname, out_string);
   if (err < 0)
       LED_ERR("failed to get the string of propname %s!\n", propname);
 
   return err;
}
 
static void sunxi_get_para_of_property(struct sunxi_led *led)
{
   int err;
   u32 val;
   const char *str;
 
   err = sunxi_get_u32_of_property("led_count", &val);
   if (!err)
       led->led_count = val;
 
   memcpy(led->output_mode.str, "GRB", 3);
   led->output_mode.val = SUNXI_OUTPUT_GRB;
   err = sunxi_get_str_of_property("output_mode", &str);
   if (!err)
       if (!strncmp(str, "BRG", 3) ||
           !strncmp(str, "GBR", 3) ||
           !strncmp(str, "RGB", 3) ||
           !strncmp(str, "RBG", 3) ||
           !strncmp(str, "BGR", 3) ||
           !strncmp(str, "BRG", 3))
           memcpy(led->output_mode.str, str, 3);
 
   err =  sunxi_get_str_of_property("led_regulator", &str);
   if (!err) {
       if (strlen(str) >= sizeof(led->regulator_id))
           LED_ERR("illegal regulator id\n");
       else {
           strcpy(led->regulator_id, str);
           pr_info("led_regulator: %s\n", led->regulator_id);
       }
   }
 
   err = sunxi_get_u32_of_property("reset_ns", &val);
   if (!err)
       led->reset_ns = val;
 
   err = sunxi_get_u32_of_property("t1h_ns", &val);
   if (!err)
       led->t1h_ns = val;
 
   err = sunxi_get_u32_of_property("t1l_ns", &val);
   if (!err)
       led->t1l_ns = val;
 
   err = sunxi_get_u32_of_property("t0h_ns", &val);
   if (!err)
       led->t0h_ns = val;
 
   err = sunxi_get_u32_of_property("t0l_ns", &val);
   if (!err)
       led->t0l_ns = val;
 
   err = sunxi_get_u32_of_property("wait_time0_ns", &val);
   if (!err)
       led->wait_time0_ns = val;
 
   err = sunxi_get_u32_of_property("wait_time1_ns", &val);
   if (!err)
       led->wait_time1_ns = val;
 
   err = sunxi_get_u32_of_property("wait_data_time_ns", &val);
   if (!err)
       led->wait_data_time_ns = val;
}
void sunxi_led_set_all(struct sunxi_led *led, u8 channel,
       enum led_brightness value)
{
   u32 i;
   struct led_classdev *led_cdev;
 
   if (channel%3 == 0) {
       for (i = 0; i < led->led_count; i++) {
           led_cdev = &led->pcdev_group[i].r.cdev;
           mutex_lock(&led_cdev->led_access);
           sunxi_set_led_brightness(led_cdev, value);
           mutex_unlock(&led_cdev->led_access);
       }
   } else if (channel%3 == 1) {
       for (i = 0; i < led->led_count; i++) {
           led_cdev = &led->pcdev_group[i].g.cdev;
           mutex_lock(&led_cdev->led_access);
           sunxi_set_led_brightness(led_cdev, value);
           mutex_unlock(&led_cdev->led_access);
       }
   } else {
       for (i = 0; i < led->led_count; i++) {
           led_cdev = &led->pcdev_group[i].b.cdev;
           mutex_lock(&led_cdev->led_access);
           sunxi_set_led_brightness(led_cdev, value);
           mutex_unlock(&led_cdev->led_access);
       }
   }
}
static ssize_t
led_show(struct class *class, struct class_attribute *attr, char *buf)
{
   struct sunxi_led *led = sunxi_led;
 
   sunxi_led_set_all(led, 0, 0);
   sunxi_led_set_all(led, 1, 0);
   sunxi_led_set_all(led, 2, 0);
 
   sunxi_led_set_all(led, 0, 20);
   msleep(500);
   sunxi_led_set_all(led, 1, 20);
   msleep(500);
   sunxi_led_set_all(led, 2, 20);
   msleep(500);
 
   sunxi_led_set_all(led, 0, 0);
   sunxi_led_set_all(led, 1, 0);
   sunxi_led_set_all(led, 2, 0);
 
   return 0;
}
 
static struct class_attribute led_class_attrs[] = {
   __ATTR(light, 0644, led_show, NULL),
   __ATTR_NULL,
};
 
static struct class led_class = {
   .name        = "led",
   .owner        = THIS_MODULE,
   .class_attrs    = led_class_attrs,
};
 
static int sunxi_led_probe(struct platform_device *pdev)
{
   int err;
   dma_cap_mask_t mask;
   struct sunxi_led *led;
   struct device *dev = &pdev->dev;
   struct resource *mem_res = NULL;
   int ret;
 
   dprintk(DEBUG_INIT, "start\n");
 
   led = kzalloc(sizeof(struct sunxi_led), GFP_KERNEL);
   if (!led) {
       LED_ERR("kzalloc failed\n");
       ret = -ENOMEM;
   }
 
   sunxi_led = led;
 
   platform_set_drvdata(pdev, led);
   led->dev = dev;
 
   mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
   if (mem_res == NULL) {
       LED_ERR("failed to get MEM res\n");
       ret = -ENXIO;
       goto emem;
   }
 
   if (!request_mem_region(mem_res->start, resource_size(mem_res),
               mem_res->name)) {
       LED_ERR("failed to request mem region\n");
       ret = -EINVAL;
       goto emem;
   }
 
   led->iomem_reg_base = ioremap(mem_res->start, resource_size(mem_res));
   if (!led->iomem_reg_base) {
       ret = -EIO;
       goto eiomap;
   }
   led->res = mem_res;
 
   led->output_mode.str = kzalloc(3, GFP_KERNEL);
   if (!led->output_mode.str) {
       LED_ERR("kzalloc failed\n");
       ret = -ENOMEM;
       goto ezalloc_str;
   }
 
   sunxi_get_para_of_property(led);
 
   err = led_regulator_request(led);
   if (err < 0) {
       LED_ERR("request regulator failed!\n");
       ret = err;
       goto eregulator;
   }
 
   err = sunxi_register_led_classdev(led);
   if (err) {
       LED_ERR("failed to register led classdev\n");
       ret = err;
       goto eclassdev;
   }
 
   sunxi_ledc_set_time(led);
 
   sunxi_clk_init(led);
   init_waitqueue_head(&led->wait);
 
   err = sunxi_ledc_irq_init(led);
   if (err) {
       LED_ERR("failed to init irq\n");
       ret = err;
       goto eirq;
   }
 
   sunxi_ledc_pinctrl_init(led);
 
   dma_cap_zero(mask);
   dma_cap_set(DMA_SLAVE, mask);
   led->dma_chan = dma_request_channel(mask, NULL, NULL);
   if (!led->dma_chan) {
       LED_ERR("failed to get the DMA channel!\n");
       ret = -EFAULT;
       goto edma;
   }
 
#ifdef CONFIG_DEBUG_FS
       sunxi_led_create_debugfs(led);
#endif /* CONFIG_DEBUG_FS */
 
   err = class_register(&led_class);
   if (err < 0) {
       LED_ERR("class_register err\n");
       class_unregister(&led_class);
       ret = -EFAULT;
       goto eclass;
   }
 
   dprintk(DEBUG_INIT, "finish\n");
 
   return 0;
 
eclass:
   dma_release_channel(led->dma_chan);
   led->dma_chan = NULL;
   sunxi_led_remove_debugfs(led);
 
edma:
   sunxi_ledc_irq_deinit(led);
 
eirq:
   sunxi_unregister_led_classdev(led);
   sunxi_clk_deinit(led);
 
eclassdev:
   led_regulator_release(led);
 
eregulator:
   kfree(led->output_mode.str);
 
ezalloc_str:
   iounmap(led->iomem_reg_base);
   led->iomem_reg_base = NULL;
 
eiomap:
   release_mem_region(mem_res->start, resource_size(mem_res));
 
emem:
   kfree(led);
 
   return ret;
}
 
static int sunxi_led_remove(struct platform_device *pdev)
{
   struct sunxi_led *led = platform_get_drvdata(pdev);
 
   class_unregister(&led_class);
 
   sunxi_led_remove_debugfs(led);
   if (led->dma_chan) {
       dma_release_channel(led->dma_chan);
       led->dma_chan = NULL;
   }
 
   sunxi_ledc_irq_deinit(led);
 
   sunxi_unregister_led_classdev(led);
   sunxi_clk_deinit(led);
 
   led_regulator_release(led);
 
   kfree(led->output_mode.str);
   led->output_mode.str = NULL;
 
   iounmap(led->iomem_reg_base);
   led->iomem_reg_base = NULL;
 
   release_mem_region(led->res->start, resource_size(led->res));
 
   kfree(led);
   led = NULL;
 
   dprintk(DEBUG_INIT, "finish\n");
   return 0;
}
 
#ifdef CONFIG_PM
static int led_select_gpio_state(struct pinctrl *pctrl, char *name)
{
   int ret = 0;
   struct pinctrl_state *pctrl_state = NULL;
 
   pctrl_state = pinctrl_lookup_state(pctrl, name);
   if (IS_ERR(pctrl_state)) {
       LED_ERR("pinctrl_lookup_state(%s) failed! return %p\n",
               name, pctrl_state);
       return -1;
   }
 
   ret = pinctrl_select_state(pctrl, pctrl_state);
   if (ret < 0)
       LED_ERR("pinctrl_select_state(%s) failed! return %d\n",
               name, ret);
 
   return ret;
}
 
static int led_regulator_enable(struct sunxi_led *led)
{
   if (led->regulator == NULL)
       return 0;
 
   if (regulator_enable(led->regulator) != 0) {
       LED_ERR("enable regulator %s failed!\n", led->regulator_id);
       return -1;
   }
   return 0;
}
 
static int led_regulator_disable(struct sunxi_led *led)
{
   if (led->regulator == NULL)
       return 0;
 
   if (regulator_disable(led->regulator) != 0) {
       LED_ERR("enable regulator %s failed!\n", led->regulator_id);
       return -1;
   }
   return 0;
}
 
static int sunxi_led_suspend_noirq(struct device *dev)
{
   struct platform_device *pdev = to_platform_device(dev);
   struct sunxi_led *led = platform_get_drvdata(pdev);
 
   sunxi_clk_disable(led);
   led_select_gpio_state(led->pctrl, PINCTRL_STATE_SLEEP);
   led_regulator_disable(led);
 
   dprintk(DEBUG_SUSPEND, "finish\n");
 
   return 0;
}
 
static int sunxi_led_resume_noirq(struct device *dev)
{
   struct platform_device *pdev = to_platform_device(dev);
   struct sunxi_led *led = platform_get_drvdata(pdev);
 
   led_regulator_enable(led);
   led_select_gpio_state(led->pctrl, PINCTRL_STATE_DEFAULT);
   sunxi_clk_enable(led);
 
   dprintk(DEBUG_SUSPEND, "finish\n");
 
   return 0;
}
 
static const struct dev_pm_ops sunxi_led_dev_pm_ops = {
   .suspend_noirq     = sunxi_led_suspend_noirq,
   .resume_noirq     = sunxi_led_resume_noirq,
};
 
#define SUNXI_LED_DEV_PM_OPS (&sunxi_led_dev_pm_ops)
#else
#define SUNXI_LED_DEV_PM_OPS NULL
#endif
 
static const struct of_device_id sunxi_led_dt_ids[] = {
   {.compatible = "allwinner,sunxi-leds"},
   {},
};
 
static struct platform_driver sunxi_led_driver = {
   .probe        = sunxi_led_probe,
   .remove        = sunxi_led_remove,
   .driver        = {
       .name    = "sunxi-leds",
       .owner    = THIS_MODULE,
       .pm    = SUNXI_LED_DEV_PM_OPS,
       .of_match_table = sunxi_led_dt_ids,
   },
};
 
module_platform_driver(sunxi_led_driver);
module_param_named(debug, debug_mask, int, 0664);
 
MODULE_AUTHOR("Albert Yu <yuxyun@allwinnertech.com>");
MODULE_DESCRIPTION("Allwinner LED driver");
MODULE_LICENSE("GPL v2");