huangcm
2025-09-01 53d8e046ac1bf2ebe94f671983e3d3be059df91a
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
/*
 * drivers/input/sensor/sunxi_gpadc.c
 *
 * Copyright (C) 2016 Allwinner.
 * fuzhaoke <fuzhaoke@allwinnertech.com>
 *
 * SUNXI GPADC Controller Driver
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */
 
#include <linux/init.h>
#include <linux/module.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/ioport.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/timer.h>
#include <linux/clk.h>
#include <linux/irq.h>
#include <linux/of_platform.h>
#include <linux/of_irq.h>
#include <linux/of_address.h>
#include <linux/pm.h>
#include "sunxi_gpadc.h"
#include <linux/sched.h>
 
static u32 debug_mask = 1;
#define dprintk(level_mask, fmt, ...)                \
do {                                \
   if (unlikely(debug_mask & level_mask))            \
       printk(fmt, ##__VA_ARGS__);    \
} while (0)
 
static struct sunxi_gpadc *sunxi_gpadc;
static struct status_reg  status_para;
static struct vol_reg     vol_para;
static struct sr_reg      sr_para;
static struct filter_reg  filter_para;
u32 key_vol[VOL_NUM];
u8 filter_cnt = 5;
 
static unsigned char keypad_mapindex[128] = {
   0, 0, 0, 0, 0, 0, 0, 0, 0,        /* key 1, 0-8 */
   1, 1, 1, 1, 1,                /* key 2, 9-13 */
   2, 2, 2, 2, 2, 2,            /* key 3, 14-19 */
   3, 3, 3, 3, 3, 3,            /* key 4, 20-25 */
   4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,    /* key 5, 26-36 */
   5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,    /* key 6, 37-39 */
   6, 6, 6, 6, 6, 6, 6, 6, 6,        /* key 7, 40-49 */
   7, 7, 7, 7, 7, 7, 7            /* key 8, 50-63 */
};
 
u32 sunxi_gpadc_sample_rate_read(void __iomem *reg_base, u32 clk_in)
{
   u32 div, round_clk;
 
   div = readl(reg_base + GP_SR_REG);
   div = div >> 16;
   round_clk = clk_in / (div + 1);
 
   return round_clk;
}
 
 
/* clk_in: source clock, round_clk: sample rate */
void sunxi_gpadc_sample_rate_set(void __iomem *reg_base, u32 clk_in,
       u32 round_clk)
{
   u32 div, reg_val;
   if (round_clk > clk_in)
       pr_err("%s, invalid round clk!\n", __func__);
   div = clk_in / round_clk - 1 ;
   reg_val = readl(reg_base + GP_SR_REG);
   reg_val &= ~GP_SR_CON;
   reg_val |= (div << 16);
   writel(reg_val, reg_base + GP_SR_REG);
}
 
void sunxi_gpadc_ctrl_set(void __iomem *reg_base, u32 ctrl_para)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CTRL_REG);
   reg_val |= ctrl_para;
   writel(reg_val, reg_base + GP_CTRL_REG);
}
 
static u32 sunxi_gpadc_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CS_EN_REG);
   switch (id) {
   case GP_CH_0:
       reg_val |= GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val |= GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val |= GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val |= GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_CS_EN_REG);
 
   return 0;
}
 
static u32 sunxi_gpadc_ch_deselect(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CS_EN_REG);
   switch (id) {
   case GP_CH_0:
       reg_val &= ~GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val &= ~GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val &= ~GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val &= ~GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_CS_EN_REG);
 
   return 0;
}
 
static u32 sunxi_gpadc_ch_cmp_low(void __iomem *reg_base, enum gp_channel_id id,
                           u32 low_uv)
{
   u32 reg_val = 0, low = 0, unit = 0;
 
   /* analog voltage range 0~1.8v, 12bits sample rate, unit=1.8v/(2^12) */
   unit = VOL_RANGE / 4096; /* 12bits sample rate */
   low = low_uv / unit;
   if (low > 0xfff)
       low = 0xfff;
 
   switch (id) {
   case GP_CH_0:
       reg_val = readl(reg_base + GP_CH0_CMP_DATA_REG);
       reg_val &= ~0xfff;
       reg_val |= (low & 0xfff);
       writel(reg_val, reg_base + GP_CH0_CMP_DATA_REG);
       break;
   case GP_CH_1:
       reg_val = readl(reg_base + GP_CH1_CMP_DATA_REG);
       reg_val &= ~0xfff;
       reg_val |= (low & 0xfff);
       writel(reg_val, reg_base + GP_CH1_CMP_DATA_REG);
       break;
   case GP_CH_2:
       reg_val = readl(reg_base + GP_CH2_CMP_DATA_REG);
       reg_val &= ~0xfff;
       reg_val |= (low & 0xfff);
       writel(reg_val, reg_base + GP_CH2_CMP_DATA_REG);
       break;
   case GP_CH_3:
       reg_val = readl(reg_base + GP_CH3_CMP_DATA_REG);
       reg_val &= ~0xfff;
       reg_val |= (low & 0xfff);
       writel(reg_val, reg_base + GP_CH3_CMP_DATA_REG);
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
 
   return 0;
}
static u32 sunxi_gpadc_ch_cmp_hig(void __iomem *reg_base, enum gp_channel_id id,
                               u32 hig_uv)
{
   u32 reg_val = 0, hig = 0, unit = 0;
 
   /* anolog voltage range 0~1.8v, 12bits sample rate, unit=1.8v/(2^12) */
   unit = VOL_RANGE / 4096; /* 12bits sample rate */
   hig = hig_uv / unit;
   if (hig > 0xfff)
       hig = 0xfff;
 
   switch (id) {
   case GP_CH_0:
       reg_val = readl(reg_base + GP_CH0_CMP_DATA_REG);
       reg_val &= ~(0xfff << 16);
       reg_val |= (hig & 0xfff) << 16;
       writel(reg_val, reg_base + GP_CH0_CMP_DATA_REG);
       break;
   case GP_CH_1:
       reg_val = readl(reg_base + GP_CH1_CMP_DATA_REG);
       reg_val &= ~(0xfff << 16);
       reg_val |= (hig & 0xfff) << 16;
       writel(reg_val, reg_base + GP_CH1_CMP_DATA_REG);
       break;
   case GP_CH_2:
       reg_val = readl(reg_base + GP_CH2_CMP_DATA_REG);
       reg_val &= ~(0xfff << 16);
       reg_val |= (hig & 0xfff) << 16;
       writel(reg_val, reg_base + GP_CH2_CMP_DATA_REG);
       break;
   case GP_CH_3:
       reg_val = readl(reg_base + GP_CH3_CMP_DATA_REG);
       reg_val &= ~(0xfff << 16);
       reg_val |= (hig & 0xfff) << 16;
       writel(reg_val, reg_base + GP_CH3_CMP_DATA_REG);
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
 
   return 0;
}
 
static u32 sunxi_gpadc_cmp_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CS_EN_REG);
   switch (id) {
   case GP_CH_0:
       reg_val |= GP_CH0_CMP_EN;
       break;
   case GP_CH_1:
       reg_val |= GP_CH1_CMP_EN;
       break;
   case GP_CH_2:
       reg_val |= GP_CH2_CMP_EN;
       break;
   case GP_CH_3:
       reg_val |= GP_CH3_CMP_EN;
       break;
   default:
       pr_err("%s, invalid value!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_CS_EN_REG);
 
   return 0;
}
 
static u32 sunxi_gpadc_cmp_deselect(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CTRL_REG);
   switch (id) {
   case GP_CH_0:
       reg_val &= ~GP_CH0_CMP_EN;
       break;
   case GP_CH_1:
       reg_val &= ~GP_CH1_CMP_EN;
       break;
   case GP_CH_2:
       reg_val &= ~GP_CH2_CMP_EN;
       break;
   case GP_CH_3:
       reg_val &= ~GP_CH3_CMP_EN;
       break;
   default:
       pr_err("%s, invalid value!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_CTRL_REG);
 
   return 0;
}
 
static u32 sunxi_gpadc_mode_select(void __iomem *reg_base, enum gp_select_mode mode)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CTRL_REG);
   reg_val &= ~GP_MODE_SELECT;
   reg_val |= (mode << 18);
   writel(reg_val, reg_base + GP_CTRL_REG);
 
   return 0;
}
 
/* enable gpadc function, true:enable, false:disable */
static void sunxi_gpadc_enable(void __iomem *reg_base, bool onoff)
{
   u32 reg_val = 0;
 
   reg_val = readl(reg_base + GP_CTRL_REG);
   if (true == onoff)
       reg_val |= GP_ADC_EN;
   else
       reg_val &= ~GP_ADC_EN;
   writel(reg_val, reg_base + GP_CTRL_REG);
}
 
static void sunxi_gpadc_calibration_enable(void __iomem *reg_base)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CTRL_REG);
   reg_val |= GP_CALIBRATION_ENABLE;
   writel(reg_val, reg_base + GP_CTRL_REG);
 
}
 
static u32 sunxi_enable_lowirq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_DATAL_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val |= GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val |= GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val |= GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val |= GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATAL_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_disable_lowirq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_DATAL_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val &= ~GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val &= ~GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val &= ~GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val &= ~GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATAL_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_enable_higirq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val = 0;
 
 
   reg_val = readl(reg_base + GP_DATAH_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val |= GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val |= GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val |= GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val |= GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATAH_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_disable_higirq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val = 0;
 
   reg_val = readl(reg_base + GP_DATAH_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val &= ~GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val &= ~GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val &= ~GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val &= ~GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATAH_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_gpadc_channel_id(enum gp_channel_id id)
{
   u32 channel_id;
 
   switch (id) {
   case GP_CH_0:
       channel_id = CHANNEL_0_SELECT;
       break;
   case GP_CH_1:
       channel_id = CHANNEL_1_SELECT;
       break;
   case GP_CH_2:
       channel_id = CHANNEL_2_SELECT;
       break;
   case GP_CH_3:
       channel_id = CHANNEL_3_SELECT;
       break;
   default:
       pr_err("%s,channel id select fail", __func__);
       return -EINVAL;
   }
 
   return channel_id;
}
 
static u32 sunxi_enable_irq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_DATA_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val |= GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val |= GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val |= GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val |= GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATA_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_disable_irq_ch_select(void __iomem *reg_base, enum gp_channel_id id)
{
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_DATA_INTC_REG);
   switch (id) {
   case GP_CH_0:
       reg_val &= ~GP_CH0_SELECT;
       break;
   case GP_CH_1:
       reg_val &= ~GP_CH1_SELECT;
       break;
   case GP_CH_2:
       reg_val &= ~GP_CH2_SELECT;
       break;
   case GP_CH_3:
       reg_val &= ~GP_CH3_SELECT;
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
   writel(reg_val, reg_base + GP_DATA_INTC_REG);
 
   return 0;
}
 
static u32 sunxi_ch_lowirq_status(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATAL_INTS_REG);
}
 
static void sunxi_ch_lowirq_clear_flags(void __iomem *reg_base, u32 flags)
{
   writel(flags, reg_base + GP_DATAL_INTS_REG);
}
 
static u32 sunxi_ch_higirq_status(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATAH_INTS_REG);
}
 
static void sunxi_ch_higirq_clear_flags(void __iomem *reg_base, u32 flags)
{
   writel(flags, reg_base + GP_DATAH_INTS_REG);
}
 
static u32 sunxi_ch_irq_status(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATA_INTS_REG);
}
 
static void sunxi_ch_irq_clear_flags(void __iomem *reg_base, u32 flags)
{
   writel(flags, reg_base + GP_DATA_INTS_REG);
}
 
static u32 sunxi_gpadc_read_ch_lowirq_enable(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATAL_INTC_REG);
}
 
static u32 sunxi_gpadc_read_ch_higirq_enable(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATAH_INTC_REG);
}
 
static u32 sunxi_gpadc_read_ch_irq_enable(void __iomem *reg_base)
{
   return readl(reg_base + GP_DATA_INTC_REG);
}
 
static u32 sunxi_gpadc_read_data(void __iomem *reg_base, enum gp_channel_id id)
{
   switch (id) {
   case GP_CH_0:
       return readl(reg_base + GP_CH0_DATA_REG) & GP_CH0_DATA_MASK;
   case GP_CH_1:
       return readl(reg_base + GP_CH1_DATA_REG) & GP_CH1_DATA_MASK;
   case GP_CH_2:
       return readl(reg_base + GP_CH2_DATA_REG) & GP_CH2_DATA_MASK;
   case GP_CH_3:
       return readl(reg_base + GP_CH3_DATA_REG) & GP_CH3_DATA_MASK;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -EINVAL;
   }
}
 
static int sunxi_gpadc_input_event_set(struct input_dev *input_dev, enum gp_channel_id id)
{
   int i;
 
   if (!input_dev) {
       pr_err("%s:input_dev: not enough memory for input device\n", __func__);
       return -ENOMEM;
   }
 
   switch (id) {
   case GP_CH_0:
#ifdef CONFIG_ARCH_SUN8IW18
   case GP_CH_3:
       input_dev->evbit[0] = BIT_MASK(EV_KEY);
#else
       input_dev->evbit[0] = BIT_MASK(EV_KEY)|BIT_MASK(EV_REP);
#endif
       for (i = 0; i < KEY_MAX_CNT; i++)
           set_bit(sunxi_gpadc->scankeycodes[i], input_dev->keybit);
       break;
 
   case GP_CH_1:
       input_dev->evbit[0] = BIT_MASK(EV_MSC);
       set_bit(EV_MSC, input_dev->evbit);
       set_bit(MSC_SCAN, input_dev->mscbit);
       break;
   case GP_CH_2:
       input_dev->evbit[0] = BIT_MASK(EV_MSC);
       set_bit(EV_MSC, input_dev->evbit);
       set_bit(MSC_SCAN, input_dev->mscbit);
       break;
#ifndef CONFIG_ARCH_SUN8IW18
   case GP_CH_3:
#endif
       input_dev->evbit[0] = BIT_MASK(EV_MSC);
       set_bit(EV_MSC, input_dev->evbit);
       set_bit(MSC_SCAN, input_dev->mscbit);
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       return -ENOMEM;
   }
 
   return 0;
}
 
static int sunxi_gpadc_input_register(struct sunxi_gpadc *sunxi_gpadc, int id)
{
   static struct input_dev *input_dev;
   int ret;
 
   input_dev = input_allocate_device();
   if (!input_dev) {
       pr_err("input_dev: not enough memory for input device\n");
       return -ENOMEM;
   }
 
   switch (id) {
   case GP_CH_0:
       input_dev->name = "sunxi-gpadc0";
       input_dev->phys = "sunxigpadc0/input0";
       break;
   case GP_CH_1:
       input_dev->name = "sunxi-gpadc1";
       input_dev->phys = "sunxigpadc1/input0";
       break;
   case GP_CH_2:
       input_dev->name = "sunxi-gpadc2";
       input_dev->phys = "sunxigpadc2/input0";
       break;
   case GP_CH_3:
       input_dev->name = "sunxi-gpadc3";
       input_dev->phys = "sunxigpadc3/input0";
       break;
   default:
       pr_err("%s, invalid channel id!", __func__);
       goto fail;
   }
 
   input_dev->id.bustype = BUS_HOST;
   input_dev->id.vendor = 0x0001;
   input_dev->id.product = 0x0001;
   input_dev->id.version = 0x0100;
 
   sunxi_gpadc_input_event_set(input_dev, id);
   sunxi_gpadc->input_gpadc[id] = input_dev;
   ret = input_register_device(sunxi_gpadc->input_gpadc[id]);
   if (ret) {
       pr_err("input register fail\n");
       goto fail1;
   }
 
   return 0;
 
fail1:
   input_unregister_device(sunxi_gpadc->input_gpadc[id]);
fail:
   input_free_device(sunxi_gpadc->input_gpadc[id]);
   return ret;
}
 
static int sunxi_key_init(struct sunxi_gpadc *sunxi_gpadc, struct platform_device *pdev)
{
   struct device_node *np = NULL;
   unsigned char i, j = 0;
   u32 vol, val;
   u32 set_vol[VOL_NUM];
 
   np = pdev->dev.of_node;
   if (of_property_read_u32(np, "key_cnt", &sunxi_gpadc->key_num)) {
       pr_err("%s: get key count failed", __func__);
       return -EBUSY;
   }
   pr_debug("%s key number = %d.\n", __func__, sunxi_gpadc->key_num);
   if (sunxi_gpadc->key_num < 1 || sunxi_gpadc->key_num > VOL_NUM) {
       pr_err("incorrect key number.\n");
       return -1;
   }
   for (i = 0; i < sunxi_gpadc->key_num; i++) {
       sprintf(sunxi_gpadc->key_name, "key%d_vol", i);
       if (of_property_read_u32(np, sunxi_gpadc->key_name, &vol)) {
           pr_err("%s:get%s err!\n", __func__,
                   sunxi_gpadc->key_name);
           return -EBUSY;
       }
       key_vol[i] = vol;
 
       sprintf(sunxi_gpadc->key_name, "key%d_val", i);
       if (of_property_read_u32(np, sunxi_gpadc->key_name, &val)) {
           pr_err("%s:get%s err!\n", __func__,
                   sunxi_gpadc->key_name);
           return -EBUSY;
       }
       sunxi_gpadc->scankeycodes[i] = val;
       pr_debug("%s: key%d vol= %d code= %d\n", __func__, i,
               key_vol[i], sunxi_gpadc->scankeycodes[i]);
   }
   key_vol[sunxi_gpadc->key_num] = MAXIMUM_INPUT_VOLTAGE;
 
   for (i = 0; i < sunxi_gpadc->key_num; i++) {
       set_vol[i] = key_vol[i] + (key_vol[i+1] - key_vol[i])/2;
   }
   for (i = 0; i < 128; i++) {
       if (i * SCALE_UNIT > set_vol[j])
           j++;
       keypad_mapindex[i] = j;
   }
 
   return 0;
}
 
static int sunxi_gpadc_input_register_setup(struct sunxi_gpadc *sunxi_gpadc)
{
   struct sunxi_config *config = NULL;
   int i;
 
   config = &sunxi_gpadc->gpadc_config;
   for (i = 0; i < sunxi_gpadc->channel_num; i++) {
       if (config->channel_select & sunxi_gpadc_channel_id(i))
           sunxi_gpadc_input_register(sunxi_gpadc, i);
   }
 
   return 0;
}
 
static int sunxi_gpadc_setup(struct platform_device *pdev,
                   struct sunxi_gpadc *sunxi_gpadc)
{
   struct device_node *np = NULL;
   struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
   u32 val;
   int i;
   unsigned char name[30];
 
   np = pdev->dev.of_node;
   if (of_property_read_u32(np, "gpadc_sample_rate",
               &sunxi_gpadc->gpadc_sample_rate)) {
       pr_debug("%s: get sample rate failed\n", __func__);
       sunxi_gpadc->gpadc_sample_rate = DEFAULT_SR;
   } else {
       if (sunxi_gpadc->gpadc_sample_rate < MIN_SR ||
               sunxi_gpadc->gpadc_sample_rate > MAX_SR)
           sunxi_gpadc->gpadc_sample_rate = DEFAULT_SR;
   }
 
   if (of_property_read_u32(np, "channel_num", &sunxi_gpadc->channel_num)) {
       pr_err("%s: get channel num failed\n", __func__);
       return -EBUSY;
   }
   if (of_property_read_u32(np, "channel_select", &val)) {
       pr_err("%s: get channel set select failed\n", __func__);
       gpadc_config->channel_select = 0;
   }
   gpadc_config->channel_select = val;
   if (of_property_read_u32(np, "channel_data_select", &gpadc_config->channel_data_select)) {
       pr_err("%s: get channel data setlect failed\n", __func__);
       gpadc_config->channel_data_select = 0;
   }
 
   if (of_property_read_u32(np, "channel_compare_select", &gpadc_config->channel_compare_select)) {
       pr_err("%s: get channel compare select failed\n", __func__);
       gpadc_config->channel_compare_select = 0;
   }
 
   if (of_property_read_u32(np, "channel_cld_select", &gpadc_config->channel_cld_select)) {
       pr_err("%s: get channel compare low data select failed\n", __func__);
       gpadc_config->channel_select = 0;
   }
 
   if (of_property_read_u32(np, "channel_chd_select", &gpadc_config->channel_chd_select)) {
       pr_err("%s: get channel compare hig data select failed\n", __func__);
       gpadc_config->channel_chd_select = 0;
   }
 
   for (i = 0; i < sunxi_gpadc->channel_num; i++) {
       sprintf(name, "channel%d_compare_lowdata", i);
       if (of_property_read_u32(np, name, &val)) {
           pr_err("%s:get %s err!\n", __func__, name);
           val = 0;
       }
       gpadc_config->channel_compare_lowdata[i] = val;
 
       sprintf(name, "channel%d_compare_higdata", i);
       if (of_property_read_u32(np, name, &val)) {
           pr_err("%s:get %s err!\n", __func__, name);
           val = 0;
       }
       gpadc_config->channel_compare_higdata[i] = val;
   }
 
   return 0;
}
 
static int sunxi_gpadc_hw_init(struct sunxi_gpadc *sunxi_gpadc)
{
   struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
   int i;
 
   sunxi_gpadc_sample_rate_set(sunxi_gpadc->reg_base, OSC_24MHZ,
           sunxi_gpadc->gpadc_sample_rate);
   for (i = 0; i < sunxi_gpadc->channel_num; i++) {
       if (gpadc_config->channel_select & sunxi_gpadc_channel_id(i)) {
           sunxi_gpadc_ch_select(sunxi_gpadc->reg_base, i);
           if (gpadc_config->channel_data_select & sunxi_gpadc_channel_id(i))
               sunxi_enable_irq_ch_select(sunxi_gpadc->reg_base, i);
           if (gpadc_config->channel_compare_select & sunxi_gpadc_channel_id(i)) {
               sunxi_gpadc_cmp_select(sunxi_gpadc->reg_base, i);
               if (gpadc_config->channel_cld_select & sunxi_gpadc_channel_id(i)) {
                   sunxi_enable_lowirq_ch_select(sunxi_gpadc->reg_base, i);
                   sunxi_gpadc_ch_cmp_low(sunxi_gpadc->reg_base, i,
                           gpadc_config->channel_compare_lowdata[i]);
               }
               if (gpadc_config->channel_chd_select & sunxi_gpadc_channel_id(i)) {
                   sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, i);
                   sunxi_gpadc_ch_cmp_hig(sunxi_gpadc->reg_base, i,
                           gpadc_config->channel_compare_higdata[i]);
               }
           }
       }
   }
   sunxi_gpadc_calibration_enable(sunxi_gpadc->reg_base);
   sunxi_gpadc_mode_select(sunxi_gpadc->reg_base, GP_CONTINUOUS_MODE);
   sunxi_gpadc_enable(sunxi_gpadc->reg_base, true);
 
   return 0;
}
 
static int sunxi_gpadc_hw_exit(struct sunxi_gpadc *sunxi_gpadc)
{
   struct sunxi_config *gpadc_config = &sunxi_gpadc->gpadc_config;
   int i;
 
   for (i = 0; i < sunxi_gpadc->channel_num; i++) {
       if (gpadc_config->channel_select & sunxi_gpadc_channel_id(i)) {
           sunxi_gpadc_ch_deselect(sunxi_gpadc->reg_base, i);
           if (gpadc_config->channel_data_select & sunxi_gpadc_channel_id(i))
               sunxi_disable_irq_ch_select(sunxi_gpadc->reg_base, i);
           if (gpadc_config->channel_compare_select & sunxi_gpadc_channel_id(i)) {
               sunxi_gpadc_cmp_deselect(sunxi_gpadc->reg_base, i);
               if (gpadc_config->channel_cld_select & sunxi_gpadc_channel_id(i))
                   sunxi_disable_lowirq_ch_select(sunxi_gpadc->reg_base, i);
               if (gpadc_config->channel_chd_select & sunxi_gpadc_channel_id(i))
                   sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, i);
           }
       }
   }
   sunxi_gpadc_enable(sunxi_gpadc->reg_base, false);
 
   return 0;
}
 
static u32 sunxi_gpadc_key_xfer(struct sunxi_gpadc *sunxi_gpadc, u32 data, u32 reg_low)
{
   u32 vol_data;
   u8 ch_num;
 
   dprintk(DEBUG_RUN, "data=%4d cnt=%1d ", data, sunxi_gpadc->key_cnt);
   data = ((VOL_RANGE / 4096)*data);    /* 12bits sample rate */
   vol_data = data / 1000;
 
   if (vol_data < SUNXIKEY_DOWN) {
       /* MAX compare_before = 128 */
       sunxi_gpadc->compare_before = ((data / SCALE_UNIT) / 1000)&0xff;
       dprintk(DEBUG_RUN, "bf=%3d lt=%3d\n",
               sunxi_gpadc->compare_before,
               sunxi_gpadc->compare_later);
 
       if (sunxi_gpadc->compare_before >= sunxi_gpadc->compare_later - 1
           && sunxi_gpadc->compare_before <= sunxi_gpadc->compare_later + 1)
           sunxi_gpadc->key_cnt++;
       else
           sunxi_gpadc->key_cnt = 0;
       sunxi_gpadc->compare_later = sunxi_gpadc->compare_before;
       if (sunxi_gpadc->key_cnt >= filter_cnt) {
           sunxi_gpadc->compare_later = sunxi_gpadc->compare_before;
           sunxi_gpadc->key_code = keypad_mapindex[sunxi_gpadc->compare_before];
 
           switch (reg_low) {
           case GP_CH0_LOW:
               ch_num = GP_CH_0;
               break;
           case GP_CH1_LOW:
               ch_num = GP_CH_1;
               break;
           case GP_CH2_LOW:
               ch_num = GP_CH_2;
               break;
           case GP_CH3_LOW:
               ch_num = GP_CH_3;
               break;
           default:
               pr_err("%s, invalid channel id!", __func__);
               return -EINVAL;
           }
           if (sunxi_gpadc->key_code != sunxi_gpadc->key_num) {
               input_report_key(sunxi_gpadc->input_gpadc[ch_num], sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 1);
               input_sync(sunxi_gpadc->input_gpadc[ch_num]);
           }
           sunxi_gpadc->compare_later = 0;
           sunxi_gpadc->key_cnt = 0;
       }
 
   }
 
   return 0;
}
 
#if defined(CONFIG_SUNXI_IR_CUT)
extern int ir_cut_condition, ir_cut_data, ir_cut_volt_data;
extern  wait_queue_head_t ir_cut_queue;
#endif
 
static irqreturn_t sunxi_gpadc_interrupt(int irqno, void *dev_id)
{
   struct sunxi_gpadc *sunxi_gpadc = (struct sunxi_gpadc *)dev_id;
   u32  reg_val, reg_low, reg_hig;
   u32  reg_enable_val, reg_enable_low, reg_enable_hig;
   u32 data, i;
 
   reg_enable_val = sunxi_gpadc_read_ch_irq_enable(sunxi_gpadc->reg_base);
   reg_enable_low = sunxi_gpadc_read_ch_lowirq_enable(
           sunxi_gpadc->reg_base);
   reg_enable_hig = sunxi_gpadc_read_ch_higirq_enable(
           sunxi_gpadc->reg_base);
 
   reg_val = sunxi_ch_irq_status(sunxi_gpadc->reg_base);
   sunxi_ch_irq_clear_flags(sunxi_gpadc->reg_base, reg_val);
   reg_low = sunxi_ch_lowirq_status(sunxi_gpadc->reg_base);
   sunxi_ch_lowirq_clear_flags(sunxi_gpadc->reg_base, reg_low);
   reg_hig = sunxi_ch_higirq_status(sunxi_gpadc->reg_base);
   sunxi_ch_higirq_clear_flags(sunxi_gpadc->reg_base, reg_hig);
 
   if (reg_val & GP_CH0_DATA) {
       data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_0);
   }
 
   if (reg_val & GP_CH1_DATA) {
       data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_1);
       input_event(sunxi_gpadc->input_gpadc[GP_CH_1], EV_MSC, MSC_SCAN, data);
       input_sync(sunxi_gpadc->input_gpadc[GP_CH_1]);
   }
 
   if (reg_low & GP_CH0_LOW & reg_enable_low) {
       data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_0);
       sunxi_gpadc_key_xfer(sunxi_gpadc, data, reg_low);
       sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_0);
   }
 
   if (reg_hig & GP_CH0_HIG & reg_enable_hig) {
       input_report_key(sunxi_gpadc->input_gpadc[GP_CH_0],
           sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 0);
       input_sync(sunxi_gpadc->input_gpadc[GP_CH_0]);
       sunxi_gpadc->compare_later = 0;
       sunxi_gpadc->key_cnt = 0;
       sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_0);
   }
 
#ifdef CONFIG_ARCH_SUN8IW18
   if (reg_low & GP_CH3_LOW & reg_enable_low) {
       data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, GP_CH_3);
       sunxi_gpadc_key_xfer(sunxi_gpadc, data, reg_low);
       sunxi_enable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_3);
   }
 
   if (reg_hig & GP_CH3_HIG & reg_enable_hig) {
       input_report_key(sunxi_gpadc->input_gpadc[GP_CH_3],
           sunxi_gpadc->scankeycodes[sunxi_gpadc->key_code], 0);
       input_sync(sunxi_gpadc->input_gpadc[GP_CH_3]);
       sunxi_gpadc->compare_later = 0;
       sunxi_gpadc->key_cnt = 0;
       sunxi_disable_higirq_ch_select(sunxi_gpadc->reg_base, GP_CH_3);
   }
#endif
 
   if (reg_low & GP_CH1_LOW)
       pr_debug("channel 1 low pend\n");
 
   if (reg_low & GP_CH2_LOW)
       pr_debug("channel 2 low pend\n");
 
   if (reg_low & GP_CH3_LOW)
       pr_debug("channel 3 low pend\n");
 
   if (reg_hig & GP_CH0_HIG)
       pr_debug("channel 0 hight pend\n");
 
   if (reg_hig & GP_CH1_HIG) {
       pr_debug("channel 1 hight pend\n");
   }
 
#ifndef CONFIG_ARCH_SUN8IW18
   for (i = 1; i < sunxi_gpadc->channel_num; i++) {
       if (reg_val & reg_enable_val & (1 << i)) {
           data = sunxi_gpadc_read_data(sunxi_gpadc->reg_base, i);
           input_event(sunxi_gpadc->input_gpadc[i],
                   EV_MSC, MSC_SCAN, data);
           input_sync(sunxi_gpadc->input_gpadc[i]);
           pr_debug("channel %d data pend\n", i);
       }
       if (reg_enable_low & reg_enable_hig & (1 << i)) {
           if (reg_low & reg_hig & (1 << i)) {
               data = sunxi_gpadc_read_data(
                       sunxi_gpadc->reg_base, i);
               input_event(sunxi_gpadc->input_gpadc[i],
                       EV_MSC, MSC_SCAN, data);
               input_sync(sunxi_gpadc->input_gpadc[i]);
               pr_debug("channel %d low and hig pend\n", i);
           } else
               pr_debug("invalid range\n");
       } else {
           if (reg_low & reg_enable_low & ~reg_enable_hig
                   & (1 << i)) {
               data = sunxi_gpadc_read_data(
                       sunxi_gpadc->reg_base, i);
               input_event(sunxi_gpadc->input_gpadc[i],
                       EV_MSC, MSC_SCAN, data);
               input_sync(sunxi_gpadc->input_gpadc[i]);
               pr_debug("channel %d low pend\n", i);
           } else {
               if (reg_hig & reg_enable_hig & ~reg_enable_low
                   & (1 << i)) {
                   data = sunxi_gpadc_read_data(
                           sunxi_gpadc->reg_base,
                           i);
                   input_event(sunxi_gpadc->input_gpadc[i],
                           EV_MSC, MSC_SCAN, data);
                   input_sync(sunxi_gpadc->input_gpadc[i]);
                   pr_debug("channel %d hig pend\n", i);
               }
           }
 
       }
   }
#endif
 
   return IRQ_HANDLED;
}
 
 
void __status_regs_ex(void __iomem *reg_base)
{
   unsigned char i;
   u32 reg_val;
 
   reg_val = readl(reg_base + GP_CS_EN_REG);
 
   for (i = 0; i < sunxi_gpadc->channel_num; i++) {
       if (reg_val & (1 << i))
           pr_warn("gpadc%d: okay\n", i);
       else
           pr_warn("gpadc%d: disable\n", i);
   }
}
 
void __vol_regs_ex(struct sunxi_gpadc *sunxi_gpadc)
{
   unsigned char i;
   char tmp = -1;
 
   pr_info("key_cnt = %d\n", sunxi_gpadc->key_num);
   for (i = 0; i < sunxi_gpadc->key_num; i++)
       pr_info("key%d_vol = %d\n", i, key_vol[i]);
 
   dprintk(DEBUG_INFO, "keypad_mapindex[%d] = {", MAXIMUM_SCALE);
   for (i = 0; i < MAXIMUM_SCALE; i++) {
       if (keypad_mapindex[i] != tmp)
           dprintk(DEBUG_INFO, "\n\t");
       dprintk(DEBUG_INFO, "%d, ", keypad_mapindex[i]);
       tmp = keypad_mapindex[i];
   }
   dprintk(DEBUG_INFO, "\n}\n");
}
 
static struct status_reg __parse_status_str(const char *buf, size_t size)
{
   char *ptr = NULL;
   char *str = "gpadc";
   struct status_reg reg_tmp;
 
   if (!buf)
       goto err;
 
   reg_tmp.pst = (char *)buf;
   reg_tmp.ped = reg_tmp.pst;
   reg_tmp.ped = strnchr(reg_tmp.pst, size, ',');
   if (!reg_tmp.ped)
       goto err;
 
   *reg_tmp.ped = '\0';
   reg_tmp.ped++;
   reg_tmp.pst = strim(reg_tmp.pst);
   reg_tmp.ped = strim(reg_tmp.ped);
 
   ptr = strstr(reg_tmp.pst, str);
   if (!ptr) {
       ptr = reg_tmp.pst;
       while (*ptr != 0) {
           if (*ptr >= 'A' && *ptr <= 'Z')
               *ptr += 32;
           ptr++;
       }
       ptr = reg_tmp.pst;
       ptr = strstr(ptr, str);
       if (!ptr)
           goto err;
   }
 
   reg_tmp.channel = *(reg_tmp.pst + strlen(str)) - 48;
   if (reg_tmp.channel < 0
           || reg_tmp.channel > (sunxi_gpadc->channel_num - 1))
       goto err;
 
   if (!(strlen(reg_tmp.ped) == 1))
       goto err;
 
   reg_tmp.val = *reg_tmp.ped - 48;
 
   return reg_tmp;
 
err:
   reg_tmp.pst = NULL;
   reg_tmp.ped = NULL;
   return reg_tmp;
}
 
static struct vol_reg __parse_vol_str(const char *buf, size_t size)
{
   int ret = 0;
   char *ptr = NULL;
   char *str = "vol";
   struct vol_reg reg_tmp;
 
   if (!buf)
       goto err;
 
   reg_tmp.pst = (char *)buf;
   reg_tmp.ped = reg_tmp.pst;
   reg_tmp.ped = strnchr(reg_tmp.pst, size, ',');
   if (!reg_tmp.ped)
       goto err;
 
   *reg_tmp.ped = '\0';
   reg_tmp.ped++;
   reg_tmp.pst = strim(reg_tmp.pst);
   reg_tmp.ped = strim(reg_tmp.ped);
 
   ptr = strstr(reg_tmp.pst, str);
   if (!ptr) {
       ptr = reg_tmp.pst;
       while (*ptr != 0) {
           if (*ptr >= 'A' && *ptr <= 'Z')
               *ptr += 32;
           ptr++;
       }
       ptr = reg_tmp.pst;
       ptr = strstr(ptr, str);
       if (!ptr)
           goto err;
   }
 
   reg_tmp.index = *(reg_tmp.pst + strlen(str)) - 48;
   if (reg_tmp.index < 0
           || reg_tmp.index > (sunxi_gpadc->key_num - 1))
       goto err;
 
   ret = kstrtoul(reg_tmp.ped, 10, &reg_tmp.vol);
   if (ret)
       goto err;
 
   return reg_tmp;
 
err:
   reg_tmp.pst = NULL;
   reg_tmp.ped = NULL;
   return reg_tmp;
}
 
static struct sr_reg __parse_sr_str(const char *buf, size_t size)
{
   int ret = 0;
   struct sr_reg reg_tmp;
 
   if (!buf)
       reg_tmp.pst = NULL;
 
   reg_tmp.pst = (char *)buf;
   reg_tmp.pst = strim(reg_tmp.pst);
 
   ret = kstrtoul(reg_tmp.pst, 10, &reg_tmp.val);
   if (ret)
       goto err;
   if (reg_tmp.val < MIN_SR || reg_tmp.val > MAX_SR) {
       pr_err("%s,%d err. sampling frequency: [%lu,%lu]\n",
               __func__, __LINE__, MIN_SR, MAX_SR);
       reg_tmp.pst = NULL;
   }
 
   return reg_tmp;
 
err:
   reg_tmp.pst = NULL;
   return reg_tmp;
}
 
static struct filter_reg __parse_filter_str(const char *buf, size_t size)
{
   int ret = 0;
   struct filter_reg reg_tmp;
 
   if (!buf)
       reg_tmp.pst = NULL;
 
   reg_tmp.pst = (char *)buf;
   reg_tmp.pst = strim(reg_tmp.pst);
 
   ret = kstrtoul(reg_tmp.pst, 10, &reg_tmp.val);
   if (ret)
       goto err;
 
   return reg_tmp;
 
err:
   reg_tmp.pst = NULL;
   return reg_tmp;
}
 
static ssize_t
status_show(struct class *class, struct class_attribute *attr, char *buf)
{
   __status_regs_ex(sunxi_gpadc->reg_base);
 
   return 0;
}
 
static ssize_t
status_store(struct class *class, struct class_attribute *attr,
       const char *buf, size_t count)
{
   status_para = __parse_status_str(buf, count);
   if (!status_para.pst || !status_para.ped)
       goto err;
 
   if (status_para.val) {
       sunxi_gpadc_ch_select(sunxi_gpadc->reg_base,
               status_para.channel);
       pr_warn("Enable gpadc%u\n", status_para.channel);
   } else {
       sunxi_gpadc_ch_deselect(sunxi_gpadc->reg_base,
               status_para.channel);
       pr_warn("Disable gpadc%u\n", status_para.channel);
   }
 
   return count;
 
err:
   pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
   status_para.pst = NULL;
   status_para.ped = NULL;
   status_para.channel = -1;
   status_para.val = -1;
   return -EINVAL;
}
 
static ssize_t
vol_show(struct class *class, struct class_attribute *attr, char *buf)
{
   __vol_regs_ex(sunxi_gpadc);
   return 0;
}
 
static ssize_t
vol_store(struct class *class, struct class_attribute *attr,
       const char *buf, size_t count)
{
   unsigned char i, j = 0;
   u32 gap = 0, half_gap = 0, cnt = 0;
   u32 set_vol[VOL_NUM];
 
   vol_para = __parse_vol_str(buf, count);
   if (!vol_para.pst || !vol_para.ped)
       goto err;
 
   key_vol[vol_para.index] = vol_para.vol;
 
   for (i = sunxi_gpadc->key_num - 1; i > 0; i--) {
       gap = gap + (key_vol[i] - key_vol[i-1]);
       cnt++;
   }
   half_gap = gap/cnt/2;
   for (i = 0; i < sunxi_gpadc->key_num; i++)
       set_vol[i] = key_vol[i] + half_gap;
 
   for (i = 0; i < 128; i++) {
       if (i * SCALE_UNIT > set_vol[j])
           j++;
       keypad_mapindex[i] = j;
   }
 
   return count;
 
err:
   pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
   vol_para.pst = NULL;
   vol_para.ped = NULL;
   vol_para.index = -1;
   vol_para.vol = -1;
   return -EINVAL;
}
 
static ssize_t
sr_show(struct class *class, struct class_attribute *attr, char *buf)
{
   u32 sr;
 
   sr = sunxi_gpadc_sample_rate_read(sunxi_gpadc->reg_base, OSC_24MHZ);
   pr_warn("gpadc sampling frequency: %u\n", sr);
 
   return 0;
}
 
static ssize_t
sr_store(struct class *class, struct class_attribute *attr,
       const char *buf, size_t count)
{
   sr_para = __parse_sr_str(buf, count);
   if (!sr_para.pst)
       goto err;
 
   sunxi_gpadc_sample_rate_set(sunxi_gpadc->reg_base, OSC_24MHZ,
           sr_para.val);
 
   return count;
 
err:
   pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
   return -EINVAL;
}
 
static ssize_t
filter_show(struct class *class, struct class_attribute *attr, char *buf)
{
   dprintk(DEBUG_INFO, "filter_cnt = %u\n", filter_cnt);
 
   return 0;
}
 
static ssize_t
filter_store(struct class *class, struct class_attribute *attr,
       const char *buf, size_t count)
{
   filter_para = __parse_filter_str(buf, count);
   if (!filter_para.pst)
       goto err;
 
   filter_cnt = filter_para.val;
 
   return count;
 
err:
   pr_err("%s,%d err, invalid para!\n", __func__, __LINE__);
   return -EINVAL;
}
 
 
static struct class_attribute gpadc_class_attrs[] = {
   __ATTR(status, 0644, status_show, status_store),
   __ATTR(vol,    0644, vol_show,    vol_store),
   __ATTR(sr,     0644, sr_show,     sr_store),
   __ATTR(filter, 0644, filter_show, filter_store),
   __ATTR_NULL,
};
 
static struct class gpadc_class = {
   .name        = "gpadc",
   .owner        = THIS_MODULE,
   .class_attrs    = gpadc_class_attrs,
};
 
int sunxi_gpadc_probe(struct platform_device *pdev)
{
   struct device_node *np = pdev->dev.of_node;
   int ret = 0;
 
   if (!of_device_is_available(np)) {
       pr_err("%s: sunxi gpadc is disable\n", __func__);
       return -EPERM;
   }
 
   sunxi_gpadc = kzalloc(sizeof(struct sunxi_gpadc), GFP_KERNEL);
   if (IS_ERR_OR_NULL(sunxi_gpadc)) {
       pr_err("not enough memory for sunxi_gpadc\n");
       return -ENOMEM;
   }
 
   sunxi_gpadc->reg_base = of_iomap(np, 0);
   if (NULL == sunxi_gpadc->reg_base) {
       pr_err("sunxi_gpadc iomap fail\n");
       ret = -EBUSY;
       goto fail1;
   }
 
   sunxi_gpadc->irq_num = irq_of_parse_and_map(np, 0);
   if (0 == sunxi_gpadc->irq_num) {
       pr_err("sunxi_gpadc fail to map irq\n");
       ret = -EBUSY;
       goto fail2;
   }
 
   sunxi_gpadc->mclk = of_clk_get(np, 0);
   if (IS_ERR_OR_NULL(sunxi_gpadc->mclk)) {
       pr_err("sunxi_gpadc has no clk\n");
       ret = -EINVAL;
       goto fail3;
   } else{
       if (clk_prepare_enable(sunxi_gpadc->mclk)) {
           pr_err("enable sunxi_gpadc clock failed!\n");
           ret = -EINVAL;
           goto fail3;
       }
   }
 
   sunxi_key_init(sunxi_gpadc, pdev);
   sunxi_gpadc_setup(pdev, sunxi_gpadc);
   sunxi_gpadc_hw_init(sunxi_gpadc);
   sunxi_gpadc_input_register_setup(sunxi_gpadc);
 
   platform_set_drvdata(pdev, sunxi_gpadc);
 
   if (request_irq(sunxi_gpadc->irq_num, sunxi_gpadc_interrupt,
           IRQF_TRIGGER_NONE, "sunxi-gpadc", sunxi_gpadc)) {
       pr_err("sunxi_gpadc request irq failure\n");
       return -1;
   }
 
   return 0;
 
fail3:
   free_irq(sunxi_gpadc->irq_num, sunxi_gpadc);
fail2:
   iounmap(sunxi_gpadc->reg_base);
fail1:
   kfree(sunxi_gpadc);
 
   return ret;
}
 
int sunxi_gpadc_remove(struct platform_device *pdev)
 
{
   struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
 
   sunxi_gpadc_hw_exit(sunxi_gpadc);
   free_irq(sunxi_gpadc->irq_num, sunxi_gpadc);
   clk_disable_unprepare(sunxi_gpadc->mclk);
   iounmap(sunxi_gpadc->reg_base);
   kfree(sunxi_gpadc);
 
   return 0;
}
 
#ifdef CONFIG_PM
static int sunxi_gpadc_suspend(struct device *dev)
{
   struct platform_device *pdev = to_platform_device(dev);
   struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
 
   disable_irq_nosync(sunxi_gpadc->irq_num);
   sunxi_gpadc_hw_exit(sunxi_gpadc);
   clk_disable_unprepare(sunxi_gpadc->mclk);
 
   return 0;
}
 
static int sunxi_gpadc_resume(struct device *dev)
{
   struct platform_device *pdev = to_platform_device(dev);
   struct sunxi_gpadc *sunxi_gpadc = platform_get_drvdata(pdev);
 
   enable_irq(sunxi_gpadc->irq_num);
   clk_prepare_enable(sunxi_gpadc->mclk);
   sunxi_gpadc_hw_init(sunxi_gpadc);
 
   return 0;
}
 
static const struct dev_pm_ops sunxi_gpadc_dev_pm_ops = {
   .suspend = sunxi_gpadc_suspend,
   .resume = sunxi_gpadc_resume,
};
 
#define SUNXI_GPADC_DEV_PM_OPS (&sunxi_gpadc_dev_pm_ops)
#else
#define SUNXI_GPADC_DEV_PM_OPS NULL
#endif
 
static struct of_device_id sunxi_gpadc_of_match[] = {
   { .compatible = "allwinner,sunxi-gpadc"},
   {},
};
MODULE_DEVICE_TABLE(of, sunxi_gpadc_of_match);
 
static struct platform_driver sunxi_gpadc_driver = {
   .probe  = sunxi_gpadc_probe,
   .remove = sunxi_gpadc_remove,
   .driver = {
       .name   = "sunxi-gpadc",
       .owner  = THIS_MODULE,
       .pm = SUNXI_GPADC_DEV_PM_OPS,
       .of_match_table = of_match_ptr(sunxi_gpadc_of_match),
   },
};
module_param_named(debug_mask, debug_mask, int, 0644);
 
static int __init sunxi_gpadc_init(void)
{
   int ret;
 
   ret = class_register(&gpadc_class);
   if (ret < 0)
       pr_err("%s,%d err, ret:%d\n", __func__, __LINE__, ret);
   else
       pr_info("%s,%d, success\n", __func__, __LINE__);
 
   ret = platform_driver_register(&sunxi_gpadc_driver);
   if (ret != 0)
       class_unregister(&gpadc_class);
 
   return ret;
}
module_init(sunxi_gpadc_init);
 
static void __exit sunxi_gpadc_exit(void)
{
   platform_driver_unregister(&sunxi_gpadc_driver);
   class_unregister(&gpadc_class);
}
module_exit(sunxi_gpadc_exit);
 
MODULE_AUTHOR("Fuzhaoke");
MODULE_DESCRIPTION("sunxi-gpadc driver");
MODULE_LICENSE("GPL");