summaryrefslogtreecommitdiffstats
path: root/drivers/input/misc/iqs269a.c
blob: cd14ff9f57cf239819d76cf3bae0da8c13524655 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
// SPDX-License-Identifier: GPL-2.0+
/*
 * Azoteq IQS269A Capacitive Touch Controller
 *
 * Copyright (C) 2020 Jeff LaBundy <jeff@labundy.com>
 *
 * This driver registers up to 3 input devices: one representing capacitive or
 * inductive keys as well as Hall-effect switches, and one for each of the two
 * axial sliders presented by the device.
 */

#include <linux/bits.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/i2c.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/slab.h>

#define IQS269_VER_INFO				0x00
#define IQS269_VER_INFO_PROD_NUM		0x4F
#define IQS269_VER_INFO_FW_NUM_2		0x03
#define IQS269_VER_INFO_FW_NUM_3		0x10

#define IQS269_SYS_FLAGS			0x02
#define IQS269_SYS_FLAGS_SHOW_RESET		BIT(15)
#define IQS269_SYS_FLAGS_PWR_MODE_MASK		GENMASK(12, 11)
#define IQS269_SYS_FLAGS_PWR_MODE_SHIFT		11
#define IQS269_SYS_FLAGS_IN_ATI			BIT(10)

#define IQS269_CHx_COUNTS			0x08

#define IQS269_SLIDER_X				0x30

#define IQS269_CAL_DATA_A			0x35
#define IQS269_CAL_DATA_A_HALL_BIN_L_MASK	GENMASK(15, 12)
#define IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT	12
#define IQS269_CAL_DATA_A_HALL_BIN_R_MASK	GENMASK(11, 8)
#define IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT	8

#define IQS269_SYS_SETTINGS			0x80
#define IQS269_SYS_SETTINGS_CLK_DIV		BIT(15)
#define IQS269_SYS_SETTINGS_ULP_AUTO		BIT(14)
#define IQS269_SYS_SETTINGS_DIS_AUTO		BIT(13)
#define IQS269_SYS_SETTINGS_PWR_MODE_MASK	GENMASK(12, 11)
#define IQS269_SYS_SETTINGS_PWR_MODE_SHIFT	11
#define IQS269_SYS_SETTINGS_PWR_MODE_MAX	3
#define IQS269_SYS_SETTINGS_ULP_UPDATE_MASK	GENMASK(10, 8)
#define IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT	8
#define IQS269_SYS_SETTINGS_ULP_UPDATE_MAX	7
#define IQS269_SYS_SETTINGS_SLIDER_SWIPE	BIT(7)
#define IQS269_SYS_SETTINGS_RESEED_OFFSET	BIT(6)
#define IQS269_SYS_SETTINGS_EVENT_MODE		BIT(5)
#define IQS269_SYS_SETTINGS_EVENT_MODE_LP	BIT(4)
#define IQS269_SYS_SETTINGS_REDO_ATI		BIT(2)
#define IQS269_SYS_SETTINGS_ACK_RESET		BIT(0)

#define IQS269_FILT_STR_LP_LTA_MASK		GENMASK(7, 6)
#define IQS269_FILT_STR_LP_LTA_SHIFT		6
#define IQS269_FILT_STR_LP_CNT_MASK		GENMASK(5, 4)
#define IQS269_FILT_STR_LP_CNT_SHIFT		4
#define IQS269_FILT_STR_NP_LTA_MASK		GENMASK(3, 2)
#define IQS269_FILT_STR_NP_LTA_SHIFT		2
#define IQS269_FILT_STR_NP_CNT_MASK		GENMASK(1, 0)
#define IQS269_FILT_STR_MAX			3

#define IQS269_EVENT_MASK_SYS			BIT(6)
#define IQS269_EVENT_MASK_GESTURE		BIT(3)
#define IQS269_EVENT_MASK_DEEP			BIT(2)
#define IQS269_EVENT_MASK_TOUCH			BIT(1)
#define IQS269_EVENT_MASK_PROX			BIT(0)

#define IQS269_RATE_NP_MS_MAX			255
#define IQS269_RATE_LP_MS_MAX			255
#define IQS269_RATE_ULP_MS_MAX			4080
#define IQS269_TIMEOUT_PWR_MS_MAX		130560
#define IQS269_TIMEOUT_LTA_MS_MAX		130560

#define IQS269_MISC_A_ATI_BAND_DISABLE		BIT(15)
#define IQS269_MISC_A_ATI_LP_ONLY		BIT(14)
#define IQS269_MISC_A_ATI_BAND_TIGHTEN		BIT(13)
#define IQS269_MISC_A_FILT_DISABLE		BIT(12)
#define IQS269_MISC_A_GPIO3_SELECT_MASK		GENMASK(10, 8)
#define IQS269_MISC_A_GPIO3_SELECT_SHIFT	8
#define IQS269_MISC_A_DUAL_DIR			BIT(6)
#define IQS269_MISC_A_TX_FREQ_MASK		GENMASK(5, 4)
#define IQS269_MISC_A_TX_FREQ_SHIFT		4
#define IQS269_MISC_A_TX_FREQ_MAX		3
#define IQS269_MISC_A_GLOBAL_CAP_SIZE		BIT(0)

#define IQS269_MISC_B_RESEED_UI_SEL_MASK	GENMASK(7, 6)
#define IQS269_MISC_B_RESEED_UI_SEL_SHIFT	6
#define IQS269_MISC_B_RESEED_UI_SEL_MAX		3
#define IQS269_MISC_B_TRACKING_UI_ENABLE	BIT(4)
#define IQS269_MISC_B_FILT_STR_SLIDER		GENMASK(1, 0)

#define IQS269_TOUCH_HOLD_SLIDER_SEL		0x89
#define IQS269_TOUCH_HOLD_DEFAULT		0x14
#define IQS269_TOUCH_HOLD_MS_MIN		256
#define IQS269_TOUCH_HOLD_MS_MAX		65280

#define IQS269_TIMEOUT_TAP_MS_MAX		4080
#define IQS269_TIMEOUT_SWIPE_MS_MAX		4080
#define IQS269_THRESH_SWIPE_MAX			255

#define IQS269_CHx_ENG_A_MEAS_CAP_SIZE		BIT(15)
#define IQS269_CHx_ENG_A_RX_GND_INACTIVE	BIT(13)
#define IQS269_CHx_ENG_A_LOCAL_CAP_SIZE		BIT(12)
#define IQS269_CHx_ENG_A_ATI_MODE_MASK		GENMASK(9, 8)
#define IQS269_CHx_ENG_A_ATI_MODE_SHIFT		8
#define IQS269_CHx_ENG_A_ATI_MODE_MAX		3
#define IQS269_CHx_ENG_A_INV_LOGIC		BIT(7)
#define IQS269_CHx_ENG_A_PROJ_BIAS_MASK		GENMASK(6, 5)
#define IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT	5
#define IQS269_CHx_ENG_A_PROJ_BIAS_MAX		3
#define IQS269_CHx_ENG_A_SENSE_MODE_MASK	GENMASK(3, 0)
#define IQS269_CHx_ENG_A_SENSE_MODE_MAX		15

#define IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE	BIT(13)
#define IQS269_CHx_ENG_B_SENSE_FREQ_MASK	GENMASK(10, 9)
#define IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT	9
#define IQS269_CHx_ENG_B_SENSE_FREQ_MAX		3
#define IQS269_CHx_ENG_B_STATIC_ENABLE		BIT(8)
#define IQS269_CHx_ENG_B_ATI_BASE_MASK		GENMASK(7, 6)
#define IQS269_CHx_ENG_B_ATI_BASE_75		0x00
#define IQS269_CHx_ENG_B_ATI_BASE_100		0x40
#define IQS269_CHx_ENG_B_ATI_BASE_150		0x80
#define IQS269_CHx_ENG_B_ATI_BASE_200		0xC0
#define IQS269_CHx_ENG_B_ATI_TARGET_MASK	GENMASK(5, 0)
#define IQS269_CHx_ENG_B_ATI_TARGET_MAX		2016

#define IQS269_CHx_WEIGHT_MAX			255
#define IQS269_CHx_THRESH_MAX			255
#define IQS269_CHx_HYST_DEEP_MASK		GENMASK(7, 4)
#define IQS269_CHx_HYST_DEEP_SHIFT		4
#define IQS269_CHx_HYST_TOUCH_MASK		GENMASK(3, 0)
#define IQS269_CHx_HYST_MAX			15

#define IQS269_CHx_HALL_INACTIVE		6
#define IQS269_CHx_HALL_ACTIVE			7

#define IQS269_HALL_PAD_R			BIT(0)
#define IQS269_HALL_PAD_L			BIT(1)
#define IQS269_HALL_PAD_INV			BIT(6)

#define IQS269_HALL_UI				0xF5
#define IQS269_HALL_UI_ENABLE			BIT(15)

#define IQS269_MAX_REG				0xFF

#define IQS269_OTP_OPTION_DEFAULT		0x00
#define IQS269_OTP_OPTION_TWS			0xD0
#define IQS269_OTP_OPTION_HOLD			BIT(7)

#define IQS269_NUM_CH				8
#define IQS269_NUM_SL				2

#define iqs269_irq_wait()			usleep_range(200, 250)

enum iqs269_local_cap_size {
	IQS269_LOCAL_CAP_SIZE_0,
	IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY,
	IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5,
};

enum iqs269_st_offs {
	IQS269_ST_OFFS_PROX,
	IQS269_ST_OFFS_DIR,
	IQS269_ST_OFFS_TOUCH,
	IQS269_ST_OFFS_DEEP,
};

enum iqs269_th_offs {
	IQS269_TH_OFFS_PROX,
	IQS269_TH_OFFS_TOUCH,
	IQS269_TH_OFFS_DEEP,
};

enum iqs269_event_id {
	IQS269_EVENT_PROX_DN,
	IQS269_EVENT_PROX_UP,
	IQS269_EVENT_TOUCH_DN,
	IQS269_EVENT_TOUCH_UP,
	IQS269_EVENT_DEEP_DN,
	IQS269_EVENT_DEEP_UP,
};

enum iqs269_slider_id {
	IQS269_SLIDER_NONE,
	IQS269_SLIDER_KEY,
	IQS269_SLIDER_RAW,
};

enum iqs269_gesture_id {
	IQS269_GESTURE_TAP,
	IQS269_GESTURE_HOLD,
	IQS269_GESTURE_FLICK_POS,
	IQS269_GESTURE_FLICK_NEG,
	IQS269_NUM_GESTURES,
};

struct iqs269_switch_desc {
	unsigned int code;
	bool enabled;
};

struct iqs269_event_desc {
	const char *name;
	enum iqs269_st_offs st_offs;
	enum iqs269_th_offs th_offs;
	bool dir_up;
	u8 mask;
};

static const struct iqs269_event_desc iqs269_events[] = {
	[IQS269_EVENT_PROX_DN] = {
		.name = "event-prox",
		.st_offs = IQS269_ST_OFFS_PROX,
		.th_offs = IQS269_TH_OFFS_PROX,
		.mask = IQS269_EVENT_MASK_PROX,
	},
	[IQS269_EVENT_PROX_UP] = {
		.name = "event-prox-alt",
		.st_offs = IQS269_ST_OFFS_PROX,
		.th_offs = IQS269_TH_OFFS_PROX,
		.dir_up = true,
		.mask = IQS269_EVENT_MASK_PROX,
	},
	[IQS269_EVENT_TOUCH_DN] = {
		.name = "event-touch",
		.st_offs = IQS269_ST_OFFS_TOUCH,
		.th_offs = IQS269_TH_OFFS_TOUCH,
		.mask = IQS269_EVENT_MASK_TOUCH,
	},
	[IQS269_EVENT_TOUCH_UP] = {
		.name = "event-touch-alt",
		.st_offs = IQS269_ST_OFFS_TOUCH,
		.th_offs = IQS269_TH_OFFS_TOUCH,
		.dir_up = true,
		.mask = IQS269_EVENT_MASK_TOUCH,
	},
	[IQS269_EVENT_DEEP_DN] = {
		.name = "event-deep",
		.st_offs = IQS269_ST_OFFS_DEEP,
		.th_offs = IQS269_TH_OFFS_DEEP,
		.mask = IQS269_EVENT_MASK_DEEP,
	},
	[IQS269_EVENT_DEEP_UP] = {
		.name = "event-deep-alt",
		.st_offs = IQS269_ST_OFFS_DEEP,
		.th_offs = IQS269_TH_OFFS_DEEP,
		.dir_up = true,
		.mask = IQS269_EVENT_MASK_DEEP,
	},
};

struct iqs269_ver_info {
	u8 prod_num;
	u8 sw_num;
	u8 hw_num;
	u8 fw_num;
} __packed;

struct iqs269_ch_reg {
	u8 rx_enable;
	u8 tx_enable;
	__be16 engine_a;
	__be16 engine_b;
	__be16 ati_comp;
	u8 thresh[3];
	u8 hyst;
	u8 assoc_select;
	u8 assoc_weight;
} __packed;

struct iqs269_sys_reg {
	__be16 general;
	u8 active;
	u8 filter;
	u8 reseed;
	u8 event_mask;
	u8 rate_np;
	u8 rate_lp;
	u8 rate_ulp;
	u8 timeout_pwr;
	u8 timeout_rdy;
	u8 timeout_lta;
	__be16 misc_a;
	__be16 misc_b;
	u8 blocking;
	u8 padding;
	u8 slider_select[IQS269_NUM_SL];
	u8 timeout_tap;
	u8 timeout_swipe;
	u8 thresh_swipe;
	u8 redo_ati;
	struct iqs269_ch_reg ch_reg[IQS269_NUM_CH];
} __packed;

struct iqs269_flags {
	__be16 system;
	u8 gesture;
	u8 padding;
	u8 states[4];
} __packed;

struct iqs269_private {
	struct i2c_client *client;
	struct regmap *regmap;
	struct mutex lock;
	struct iqs269_switch_desc switches[ARRAY_SIZE(iqs269_events)];
	struct iqs269_ver_info ver_info;
	struct iqs269_sys_reg sys_reg;
	struct completion ati_done;
	struct input_dev *keypad;
	struct input_dev *slider[IQS269_NUM_SL];
	unsigned int keycode[ARRAY_SIZE(iqs269_events) * IQS269_NUM_CH];
	unsigned int sl_code[IQS269_NUM_SL][IQS269_NUM_GESTURES];
	unsigned int otp_option;
	unsigned int ch_num;
	bool hall_enable;
	bool ati_current;
};

static enum iqs269_slider_id iqs269_slider_type(struct iqs269_private *iqs269,
						int slider_num)
{
	int i;

	/*
	 * Slider 1 is unavailable if the touch-and-hold option is enabled via
	 * OTP. In that case, the channel selection register is repurposed for
	 * the touch-and-hold timer ceiling.
	 */
	if (slider_num && (iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
		return IQS269_SLIDER_NONE;

	if (!iqs269->sys_reg.slider_select[slider_num])
		return IQS269_SLIDER_NONE;

	for (i = 0; i < IQS269_NUM_GESTURES; i++)
		if (iqs269->sl_code[slider_num][i] != KEY_RESERVED)
			return IQS269_SLIDER_KEY;

	return IQS269_SLIDER_RAW;
}

static int iqs269_ati_mode_set(struct iqs269_private *iqs269,
			       unsigned int ch_num, unsigned int mode)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_a;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	if (mode > IQS269_CHx_ENG_A_ATI_MODE_MAX)
		return -EINVAL;

	mutex_lock(&iqs269->lock);

	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);

	engine_a &= ~IQS269_CHx_ENG_A_ATI_MODE_MASK;
	engine_a |= (mode << IQS269_CHx_ENG_A_ATI_MODE_SHIFT);

	ch_reg[ch_num].engine_a = cpu_to_be16(engine_a);
	iqs269->ati_current = false;

	mutex_unlock(&iqs269->lock);

	return 0;
}

static int iqs269_ati_mode_get(struct iqs269_private *iqs269,
			       unsigned int ch_num, unsigned int *mode)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_a;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	mutex_lock(&iqs269->lock);
	engine_a = be16_to_cpu(ch_reg[ch_num].engine_a);
	mutex_unlock(&iqs269->lock);

	engine_a &= IQS269_CHx_ENG_A_ATI_MODE_MASK;
	*mode = (engine_a >> IQS269_CHx_ENG_A_ATI_MODE_SHIFT);

	return 0;
}

static int iqs269_ati_base_set(struct iqs269_private *iqs269,
			       unsigned int ch_num, unsigned int base)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_b;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	switch (base) {
	case 75:
		base = IQS269_CHx_ENG_B_ATI_BASE_75;
		break;

	case 100:
		base = IQS269_CHx_ENG_B_ATI_BASE_100;
		break;

	case 150:
		base = IQS269_CHx_ENG_B_ATI_BASE_150;
		break;

	case 200:
		base = IQS269_CHx_ENG_B_ATI_BASE_200;
		break;

	default:
		return -EINVAL;
	}

	mutex_lock(&iqs269->lock);

	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);

	engine_b &= ~IQS269_CHx_ENG_B_ATI_BASE_MASK;
	engine_b |= base;

	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
	iqs269->ati_current = false;

	mutex_unlock(&iqs269->lock);

	return 0;
}

static int iqs269_ati_base_get(struct iqs269_private *iqs269,
			       unsigned int ch_num, unsigned int *base)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_b;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	mutex_lock(&iqs269->lock);
	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
	mutex_unlock(&iqs269->lock);

	switch (engine_b & IQS269_CHx_ENG_B_ATI_BASE_MASK) {
	case IQS269_CHx_ENG_B_ATI_BASE_75:
		*base = 75;
		return 0;

	case IQS269_CHx_ENG_B_ATI_BASE_100:
		*base = 100;
		return 0;

	case IQS269_CHx_ENG_B_ATI_BASE_150:
		*base = 150;
		return 0;

	case IQS269_CHx_ENG_B_ATI_BASE_200:
		*base = 200;
		return 0;

	default:
		return -EINVAL;
	}
}

static int iqs269_ati_target_set(struct iqs269_private *iqs269,
				 unsigned int ch_num, unsigned int target)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_b;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	if (target > IQS269_CHx_ENG_B_ATI_TARGET_MAX)
		return -EINVAL;

	mutex_lock(&iqs269->lock);

	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);

	engine_b &= ~IQS269_CHx_ENG_B_ATI_TARGET_MASK;
	engine_b |= target / 32;

	ch_reg[ch_num].engine_b = cpu_to_be16(engine_b);
	iqs269->ati_current = false;

	mutex_unlock(&iqs269->lock);

	return 0;
}

static int iqs269_ati_target_get(struct iqs269_private *iqs269,
				 unsigned int ch_num, unsigned int *target)
{
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	u16 engine_b;

	if (ch_num >= IQS269_NUM_CH)
		return -EINVAL;

	mutex_lock(&iqs269->lock);
	engine_b = be16_to_cpu(ch_reg[ch_num].engine_b);
	mutex_unlock(&iqs269->lock);

	*target = (engine_b & IQS269_CHx_ENG_B_ATI_TARGET_MASK) * 32;

	return 0;
}

static int iqs269_parse_mask(const struct fwnode_handle *fwnode,
			     const char *propname, u8 *mask)
{
	unsigned int val[IQS269_NUM_CH];
	int count, error, i;

	count = fwnode_property_count_u32(fwnode, propname);
	if (count < 0)
		return 0;

	if (count > IQS269_NUM_CH)
		return -EINVAL;

	error = fwnode_property_read_u32_array(fwnode, propname, val, count);
	if (error)
		return error;

	*mask = 0;

	for (i = 0; i < count; i++) {
		if (val[i] >= IQS269_NUM_CH)
			return -EINVAL;

		*mask |= BIT(val[i]);
	}

	return 0;
}

static int iqs269_parse_chan(struct iqs269_private *iqs269,
			     const struct fwnode_handle *ch_node)
{
	struct i2c_client *client = iqs269->client;
	struct fwnode_handle *ev_node;
	struct iqs269_ch_reg *ch_reg;
	u16 engine_a, engine_b;
	unsigned int reg, val;
	int error, i;

	error = fwnode_property_read_u32(ch_node, "reg", &reg);
	if (error) {
		dev_err(&client->dev, "Failed to read channel number: %d\n",
			error);
		return error;
	} else if (reg >= IQS269_NUM_CH) {
		dev_err(&client->dev, "Invalid channel number: %u\n", reg);
		return -EINVAL;
	}

	iqs269->sys_reg.active |= BIT(reg);
	if (!fwnode_property_present(ch_node, "azoteq,reseed-disable"))
		iqs269->sys_reg.reseed |= BIT(reg);

	if (fwnode_property_present(ch_node, "azoteq,blocking-enable"))
		iqs269->sys_reg.blocking |= BIT(reg);

	if (fwnode_property_present(ch_node, "azoteq,slider0-select"))
		iqs269->sys_reg.slider_select[0] |= BIT(reg);

	if (fwnode_property_present(ch_node, "azoteq,slider1-select") &&
	    !(iqs269->otp_option & IQS269_OTP_OPTION_HOLD))
		iqs269->sys_reg.slider_select[1] |= BIT(reg);

	ch_reg = &iqs269->sys_reg.ch_reg[reg];

	error = iqs269_parse_mask(ch_node, "azoteq,rx-enable",
				  &ch_reg->rx_enable);
	if (error) {
		dev_err(&client->dev, "Invalid channel %u RX enable mask: %d\n",
			reg, error);
		return error;
	}

	error = iqs269_parse_mask(ch_node, "azoteq,tx-enable",
				  &ch_reg->tx_enable);
	if (error) {
		dev_err(&client->dev, "Invalid channel %u TX enable mask: %d\n",
			reg, error);
		return error;
	}

	engine_a = be16_to_cpu(ch_reg->engine_a);
	engine_b = be16_to_cpu(ch_reg->engine_b);

	engine_a |= IQS269_CHx_ENG_A_MEAS_CAP_SIZE;
	if (fwnode_property_present(ch_node, "azoteq,meas-cap-decrease"))
		engine_a &= ~IQS269_CHx_ENG_A_MEAS_CAP_SIZE;

	engine_a |= IQS269_CHx_ENG_A_RX_GND_INACTIVE;
	if (fwnode_property_present(ch_node, "azoteq,rx-float-inactive"))
		engine_a &= ~IQS269_CHx_ENG_A_RX_GND_INACTIVE;

	engine_a &= ~IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
	engine_b &= ~IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
	if (!fwnode_property_read_u32(ch_node, "azoteq,local-cap-size", &val)) {
		switch (val) {
		case IQS269_LOCAL_CAP_SIZE_0:
			break;

		case IQS269_LOCAL_CAP_SIZE_GLOBAL_0pF5:
			engine_a |= IQS269_CHx_ENG_A_LOCAL_CAP_SIZE;
			fallthrough;

		case IQS269_LOCAL_CAP_SIZE_GLOBAL_ONLY:
			engine_b |= IQS269_CHx_ENG_B_LOCAL_CAP_ENABLE;
			break;

		default:
			dev_err(&client->dev,
				"Invalid channel %u local cap. size: %u\n", reg,
				val);
			return -EINVAL;
		}
	}

	engine_a &= ~IQS269_CHx_ENG_A_INV_LOGIC;
	if (fwnode_property_present(ch_node, "azoteq,invert-enable"))
		engine_a |= IQS269_CHx_ENG_A_INV_LOGIC;

	if (!fwnode_property_read_u32(ch_node, "azoteq,proj-bias", &val)) {
		if (val > IQS269_CHx_ENG_A_PROJ_BIAS_MAX) {
			dev_err(&client->dev,
				"Invalid channel %u bias current: %u\n", reg,
				val);
			return -EINVAL;
		}

		engine_a &= ~IQS269_CHx_ENG_A_PROJ_BIAS_MASK;
		engine_a |= (val << IQS269_CHx_ENG_A_PROJ_BIAS_SHIFT);
	}

	if (!fwnode_property_read_u32(ch_node, "azoteq,sense-mode", &val)) {
		if (val > IQS269_CHx_ENG_A_SENSE_MODE_MAX) {
			dev_err(&client->dev,
				"Invalid channel %u sensing mode: %u\n", reg,
				val);
			return -EINVAL;
		}

		engine_a &= ~IQS269_CHx_ENG_A_SENSE_MODE_MASK;
		engine_a |= val;
	}

	if (!fwnode_property_read_u32(ch_node, "azoteq,sense-freq", &val)) {
		if (val > IQS269_CHx_ENG_B_SENSE_FREQ_MAX) {
			dev_err(&client->dev,
				"Invalid channel %u sensing frequency: %u\n",
				reg, val);
			return -EINVAL;
		}

		engine_b &= ~IQS269_CHx_ENG_B_SENSE_FREQ_MASK;
		engine_b |= (val << IQS269_CHx_ENG_B_SENSE_FREQ_SHIFT);
	}

	engine_b &= ~IQS269_CHx_ENG_B_STATIC_ENABLE;
	if (fwnode_property_present(ch_node, "azoteq,static-enable"))
		engine_b |= IQS269_CHx_ENG_B_STATIC_ENABLE;

	ch_reg->engine_a = cpu_to_be16(engine_a);
	ch_reg->engine_b = cpu_to_be16(engine_b);

	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-mode", &val)) {
		error = iqs269_ati_mode_set(iqs269, reg, val);
		if (error) {
			dev_err(&client->dev,
				"Invalid channel %u ATI mode: %u\n", reg, val);
			return error;
		}
	}

	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-base", &val)) {
		error = iqs269_ati_base_set(iqs269, reg, val);
		if (error) {
			dev_err(&client->dev,
				"Invalid channel %u ATI base: %u\n", reg, val);
			return error;
		}
	}

	if (!fwnode_property_read_u32(ch_node, "azoteq,ati-target", &val)) {
		error = iqs269_ati_target_set(iqs269, reg, val);
		if (error) {
			dev_err(&client->dev,
				"Invalid channel %u ATI target: %u\n", reg,
				val);
			return error;
		}
	}

	error = iqs269_parse_mask(ch_node, "azoteq,assoc-select",
				  &ch_reg->assoc_select);
	if (error) {
		dev_err(&client->dev, "Invalid channel %u association: %d\n",
			reg, error);
		return error;
	}

	if (!fwnode_property_read_u32(ch_node, "azoteq,assoc-weight", &val)) {
		if (val > IQS269_CHx_WEIGHT_MAX) {
			dev_err(&client->dev,
				"Invalid channel %u associated weight: %u\n",
				reg, val);
			return -EINVAL;
		}

		ch_reg->assoc_weight = val;
	}

	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
		ev_node = fwnode_get_named_child_node(ch_node,
						      iqs269_events[i].name);
		if (!ev_node)
			continue;

		if (!fwnode_property_read_u32(ev_node, "azoteq,thresh", &val)) {
			if (val > IQS269_CHx_THRESH_MAX) {
				dev_err(&client->dev,
					"Invalid channel %u threshold: %u\n",
					reg, val);
				fwnode_handle_put(ev_node);
				return -EINVAL;
			}

			ch_reg->thresh[iqs269_events[i].th_offs] = val;
		}

		if (!fwnode_property_read_u32(ev_node, "azoteq,hyst", &val)) {
			u8 *hyst = &ch_reg->hyst;

			if (val > IQS269_CHx_HYST_MAX) {
				dev_err(&client->dev,
					"Invalid channel %u hysteresis: %u\n",
					reg, val);
				fwnode_handle_put(ev_node);
				return -EINVAL;
			}

			if (i == IQS269_EVENT_DEEP_DN ||
			    i == IQS269_EVENT_DEEP_UP) {
				*hyst &= ~IQS269_CHx_HYST_DEEP_MASK;
				*hyst |= (val << IQS269_CHx_HYST_DEEP_SHIFT);
			} else if (i == IQS269_EVENT_TOUCH_DN ||
				   i == IQS269_EVENT_TOUCH_UP) {
				*hyst &= ~IQS269_CHx_HYST_TOUCH_MASK;
				*hyst |= val;
			}
		}

		error = fwnode_property_read_u32(ev_node, "linux,code", &val);
		fwnode_handle_put(ev_node);
		if (error == -EINVAL) {
			continue;
		} else if (error) {
			dev_err(&client->dev,
				"Failed to read channel %u code: %d\n", reg,
				error);
			return error;
		}

		switch (reg) {
		case IQS269_CHx_HALL_ACTIVE:
			if (iqs269->hall_enable) {
				iqs269->switches[i].code = val;
				iqs269->switches[i].enabled = true;
			}
			fallthrough;

		case IQS269_CHx_HALL_INACTIVE:
			if (iqs269->hall_enable)
				break;
			fallthrough;

		default:
			iqs269->keycode[i * IQS269_NUM_CH + reg] = val;
		}

		iqs269->sys_reg.event_mask &= ~iqs269_events[i].mask;
	}

	return 0;
}

static int iqs269_parse_prop(struct iqs269_private *iqs269)
{
	struct iqs269_sys_reg *sys_reg = &iqs269->sys_reg;
	struct i2c_client *client = iqs269->client;
	struct fwnode_handle *ch_node;
	u16 general, misc_a, misc_b;
	unsigned int val;
	int error;

	iqs269->hall_enable = device_property_present(&client->dev,
						      "azoteq,hall-enable");

	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_SETTINGS, sys_reg,
				sizeof(*sys_reg));
	if (error)
		return error;

	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-lta",
				      &val)) {
		if (val > IQS269_FILT_STR_MAX) {
			dev_err(&client->dev, "Invalid filter strength: %u\n",
				val);
			return -EINVAL;
		}

		sys_reg->filter &= ~IQS269_FILT_STR_LP_LTA_MASK;
		sys_reg->filter |= (val << IQS269_FILT_STR_LP_LTA_SHIFT);
	}

	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-lp-cnt",
				      &val)) {
		if (val > IQS269_FILT_STR_MAX) {
			dev_err(&client->dev, "Invalid filter strength: %u\n",
				val);
			return -EINVAL;
		}

		sys_reg->filter &= ~IQS269_FILT_STR_LP_CNT_MASK;
		sys_reg->filter |= (val << IQS269_FILT_STR_LP_CNT_SHIFT);
	}

	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-lta",
				      &val)) {
		if (val > IQS269_FILT_STR_MAX) {
			dev_err(&client->dev, "Invalid filter strength: %u\n",
				val);
			return -EINVAL;
		}

		sys_reg->filter &= ~IQS269_FILT_STR_NP_LTA_MASK;
		sys_reg->filter |= (val << IQS269_FILT_STR_NP_LTA_SHIFT);
	}

	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-np-cnt",
				      &val)) {
		if (val > IQS269_FILT_STR_MAX) {
			dev_err(&client->dev, "Invalid filter strength: %u\n",
				val);
			return -EINVAL;
		}

		sys_reg->filter &= ~IQS269_FILT_STR_NP_CNT_MASK;
		sys_reg->filter |= val;
	}

	if (!device_property_read_u32(&client->dev, "azoteq,rate-np-ms",
				      &val)) {
		if (val > IQS269_RATE_NP_MS_MAX) {
			dev_err(&client->dev, "Invalid report rate: %u\n", val);
			return -EINVAL;
		}

		sys_reg->rate_np = val;
	}

	if (!device_property_read_u32(&client->dev, "azoteq,rate-lp-ms",
				      &val)) {
		if (val > IQS269_RATE_LP_MS_MAX) {
			dev_err(&client->dev, "Invalid report rate: %u\n", val);
			return -EINVAL;
		}

		sys_reg->rate_lp = val;
	}

	if (!device_property_read_u32(&client->dev, "azoteq,rate-ulp-ms",
				      &val)) {
		if (val > IQS269_RATE_ULP_MS_MAX) {
			dev_err(&client->dev, "Invalid report rate: %u\n", val);
			return -EINVAL;
		}

		sys_reg->rate_ulp = val / 16;
	}

	if (!device_property_read_u32(&client->dev, "azoteq,timeout-pwr-ms",
				      &val)) {
		if (val > IQS269_TIMEOUT_PWR_MS_MAX) {
			dev_err(&client->dev, "Invalid timeout: %u\n", val);
			return -EINVAL;
		}

		sys_reg->timeout_pwr = val / 512;
	}

	if (!device_property_read_u32(&client->dev, "azoteq,timeout-lta-ms",
				      &val)) {
		if (val > IQS269_TIMEOUT_LTA_MS_MAX) {
			dev_err(&client->dev, "Invalid timeout: %u\n", val);
			return -EINVAL;
		}

		sys_reg->timeout_lta = val / 512;
	}

	misc_a = be16_to_cpu(sys_reg->misc_a);
	misc_b = be16_to_cpu(sys_reg->misc_b);

	misc_a &= ~IQS269_MISC_A_ATI_BAND_DISABLE;
	if (device_property_present(&client->dev, "azoteq,ati-band-disable"))
		misc_a |= IQS269_MISC_A_ATI_BAND_DISABLE;

	misc_a &= ~IQS269_MISC_A_ATI_LP_ONLY;
	if (device_property_present(&client->dev, "azoteq,ati-lp-only"))
		misc_a |= IQS269_MISC_A_ATI_LP_ONLY;

	misc_a &= ~IQS269_MISC_A_ATI_BAND_TIGHTEN;
	if (device_property_present(&client->dev, "azoteq,ati-band-tighten"))
		misc_a |= IQS269_MISC_A_ATI_BAND_TIGHTEN;

	misc_a &= ~IQS269_MISC_A_FILT_DISABLE;
	if (device_property_present(&client->dev, "azoteq,filt-disable"))
		misc_a |= IQS269_MISC_A_FILT_DISABLE;

	if (!device_property_read_u32(&client->dev, "azoteq,gpio3-select",
				      &val)) {
		if (val >= IQS269_NUM_CH) {
			dev_err(&client->dev, "Invalid GPIO3 selection: %u\n",
				val);
			return -EINVAL;
		}

		misc_a &= ~IQS269_MISC_A_GPIO3_SELECT_MASK;
		misc_a |= (val << IQS269_MISC_A_GPIO3_SELECT_SHIFT);
	}

	misc_a &= ~IQS269_MISC_A_DUAL_DIR;
	if (device_property_present(&client->dev, "azoteq,dual-direction"))
		misc_a |= IQS269_MISC_A_DUAL_DIR;

	if (!device_property_read_u32(&client->dev, "azoteq,tx-freq", &val)) {
		if (val > IQS269_MISC_A_TX_FREQ_MAX) {
			dev_err(&client->dev,
				"Invalid excitation frequency: %u\n", val);
			return -EINVAL;
		}

		misc_a &= ~IQS269_MISC_A_TX_FREQ_MASK;
		misc_a |= (val << IQS269_MISC_A_TX_FREQ_SHIFT);
	}

	misc_a &= ~IQS269_MISC_A_GLOBAL_CAP_SIZE;
	if (device_property_present(&client->dev, "azoteq,global-cap-increase"))
		misc_a |= IQS269_MISC_A_GLOBAL_CAP_SIZE;

	if (!device_property_read_u32(&client->dev, "azoteq,reseed-select",
				      &val)) {
		if (val > IQS269_MISC_B_RESEED_UI_SEL_MAX) {
			dev_err(&client->dev, "Invalid reseed selection: %u\n",
				val);
			return -EINVAL;
		}

		misc_b &= ~IQS269_MISC_B_RESEED_UI_SEL_MASK;
		misc_b |= (val << IQS269_MISC_B_RESEED_UI_SEL_SHIFT);
	}

	misc_b &= ~IQS269_MISC_B_TRACKING_UI_ENABLE;
	if (device_property_present(&client->dev, "azoteq,tracking-enable"))
		misc_b |= IQS269_MISC_B_TRACKING_UI_ENABLE;

	if (!device_property_read_u32(&client->dev, "azoteq,filt-str-slider",
				      &val)) {
		if (val > IQS269_FILT_STR_MAX) {
			dev_err(&client->dev, "Invalid filter strength: %u\n",
				val);
			return -EINVAL;
		}

		misc_b &= ~IQS269_MISC_B_FILT_STR_SLIDER;
		misc_b |= val;
	}

	sys_reg->misc_a = cpu_to_be16(misc_a);
	sys_reg->misc_b = cpu_to_be16(misc_b);

	sys_reg->active = 0;
	sys_reg->reseed = 0;

	sys_reg->blocking = 0;

	sys_reg->slider_select[0] = 0;

	/*
	 * If configured via OTP to do so, the device asserts a pulse on the
	 * GPIO4 pin for approximately 60 ms once a selected channel is held
	 * in a state of touch for a configurable length of time.
	 *
	 * In that case, the register used for slider 1 channel selection is
	 * repurposed for the touch-and-hold timer ceiling.
	 */
	if (iqs269->otp_option & IQS269_OTP_OPTION_HOLD) {
		if (!device_property_read_u32(&client->dev,
					      "azoteq,touch-hold-ms", &val)) {
			if (val < IQS269_TOUCH_HOLD_MS_MIN ||
			    val > IQS269_TOUCH_HOLD_MS_MAX) {
				dev_err(&client->dev,
					"Invalid touch-and-hold ceiling: %u\n",
					val);
				return -EINVAL;
			}

			sys_reg->slider_select[1] = val / 256;
		} else if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
			/*
			 * The default touch-and-hold timer ceiling initially
			 * read from early revisions of silicon is invalid if
			 * the device experienced a soft reset between power-
			 * on and the read operation.
			 *
			 * To protect against this case, explicitly cache the
			 * default value so that it is restored each time the
			 * device is re-initialized.
			 */
			sys_reg->slider_select[1] = IQS269_TOUCH_HOLD_DEFAULT;
		}
	} else {
		sys_reg->slider_select[1] = 0;
	}

	sys_reg->event_mask = ~((u8)IQS269_EVENT_MASK_SYS);

	device_for_each_child_node(&client->dev, ch_node) {
		error = iqs269_parse_chan(iqs269, ch_node);
		if (error) {
			fwnode_handle_put(ch_node);
			return error;
		}
	}

	/*
	 * Volunteer all active channels to participate in ATI when REDO-ATI is
	 * manually triggered.
	 */
	sys_reg->redo_ati = sys_reg->active;

	general = be16_to_cpu(sys_reg->general);

	if (device_property_present(&client->dev, "azoteq,clk-div"))
		general |= IQS269_SYS_SETTINGS_CLK_DIV;

	/*
	 * Configure the device to automatically switch between normal and low-
	 * power modes as a function of sensing activity. Ultra-low-power mode,
	 * if enabled, is reserved for suspend.
	 */
	general &= ~IQS269_SYS_SETTINGS_ULP_AUTO;
	general &= ~IQS269_SYS_SETTINGS_DIS_AUTO;
	general &= ~IQS269_SYS_SETTINGS_PWR_MODE_MASK;

	if (!device_property_read_u32(&client->dev, "azoteq,suspend-mode",
				      &val)) {
		if (val > IQS269_SYS_SETTINGS_PWR_MODE_MAX) {
			dev_err(&client->dev, "Invalid suspend mode: %u\n",
				val);
			return -EINVAL;
		}

		general |= (val << IQS269_SYS_SETTINGS_PWR_MODE_SHIFT);
	}

	if (!device_property_read_u32(&client->dev, "azoteq,ulp-update",
				      &val)) {
		if (val > IQS269_SYS_SETTINGS_ULP_UPDATE_MAX) {
			dev_err(&client->dev, "Invalid update rate: %u\n", val);
			return -EINVAL;
		}

		general &= ~IQS269_SYS_SETTINGS_ULP_UPDATE_MASK;
		general |= (val << IQS269_SYS_SETTINGS_ULP_UPDATE_SHIFT);
	}

	if (device_property_present(&client->dev, "linux,keycodes")) {
		int scale = 1;
		int count = device_property_count_u32(&client->dev,
						      "linux,keycodes");
		if (count > IQS269_NUM_GESTURES * IQS269_NUM_SL) {
			dev_err(&client->dev, "Too many keycodes present\n");
			return -EINVAL;
		} else if (count < 0) {
			dev_err(&client->dev, "Failed to count keycodes: %d\n",
				count);
			return count;
		}

		error = device_property_read_u32_array(&client->dev,
						       "linux,keycodes",
						       *iqs269->sl_code, count);
		if (error) {
			dev_err(&client->dev, "Failed to read keycodes: %d\n",
				error);
			return error;
		}

		if (device_property_present(&client->dev,
					    "azoteq,gesture-swipe"))
			general |= IQS269_SYS_SETTINGS_SLIDER_SWIPE;

		/*
		 * Early revisions of silicon use a more granular step size for
		 * tap and swipe gesture timeouts; scale them appropriately.
		 */
		if (iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3)
			scale = 4;

		if (!device_property_read_u32(&client->dev,
					      "azoteq,timeout-tap-ms", &val)) {
			if (val > IQS269_TIMEOUT_TAP_MS_MAX / scale) {
				dev_err(&client->dev, "Invalid timeout: %u\n",
					val);
				return -EINVAL;
			}

			sys_reg->timeout_tap = val / (16 / scale);
		}

		if (!device_property_read_u32(&client->dev,
					      "azoteq,timeout-swipe-ms",
					      &val)) {
			if (val > IQS269_TIMEOUT_SWIPE_MS_MAX / scale) {
				dev_err(&client->dev, "Invalid timeout: %u\n",
					val);
				return -EINVAL;
			}

			sys_reg->timeout_swipe = val / (16 / scale);
		}

		if (!device_property_read_u32(&client->dev,
					      "azoteq,thresh-swipe", &val)) {
			if (val > IQS269_THRESH_SWIPE_MAX) {
				dev_err(&client->dev, "Invalid threshold: %u\n",
					val);
				return -EINVAL;
			}

			sys_reg->thresh_swipe = val;
		}

		sys_reg->event_mask &= ~IQS269_EVENT_MASK_GESTURE;
	}

	general &= ~IQS269_SYS_SETTINGS_RESEED_OFFSET;
	if (device_property_present(&client->dev, "azoteq,reseed-offset"))
		general |= IQS269_SYS_SETTINGS_RESEED_OFFSET;

	general |= IQS269_SYS_SETTINGS_EVENT_MODE;

	/*
	 * As per the datasheet, enable streaming during normal-power mode if
	 * raw coordinates will be read from either slider. In that case, the
	 * device returns to event mode during low-power mode.
	 */
	if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
	    iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW)
		general |= IQS269_SYS_SETTINGS_EVENT_MODE_LP;

	general |= IQS269_SYS_SETTINGS_REDO_ATI;
	general |= IQS269_SYS_SETTINGS_ACK_RESET;

	sys_reg->general = cpu_to_be16(general);

	return 0;
}

static const struct reg_sequence iqs269_tws_init[] = {
	{ IQS269_TOUCH_HOLD_SLIDER_SEL, IQS269_TOUCH_HOLD_DEFAULT },
	{ 0xF0, 0x580F },
	{ 0xF0, 0x59EF },
};

static int iqs269_dev_init(struct iqs269_private *iqs269)
{
	int error;

	mutex_lock(&iqs269->lock);

	/*
	 * Early revisions of silicon require the following workaround in order
	 * to restore any OTP-enabled functionality after a soft reset.
	 */
	if (iqs269->otp_option == IQS269_OTP_OPTION_TWS &&
	    iqs269->ver_info.fw_num < IQS269_VER_INFO_FW_NUM_3) {
		error = regmap_multi_reg_write(iqs269->regmap, iqs269_tws_init,
					       ARRAY_SIZE(iqs269_tws_init));
		if (error)
			goto err_mutex;
	}

	error = regmap_update_bits(iqs269->regmap, IQS269_HALL_UI,
				   IQS269_HALL_UI_ENABLE,
				   iqs269->hall_enable ? ~0 : 0);
	if (error)
		goto err_mutex;

	error = regmap_raw_write(iqs269->regmap, IQS269_SYS_SETTINGS,
				 &iqs269->sys_reg, sizeof(iqs269->sys_reg));
	if (error)
		goto err_mutex;

	/*
	 * The following delay gives the device time to deassert its RDY output
	 * so as to prevent an interrupt from being serviced prematurely.
	 */
	usleep_range(2000, 2100);

	iqs269->ati_current = true;

err_mutex:
	mutex_unlock(&iqs269->lock);

	return error;
}

static int iqs269_input_init(struct iqs269_private *iqs269)
{
	struct i2c_client *client = iqs269->client;
	unsigned int sw_code, keycode;
	int error, i, j;

	iqs269->keypad = devm_input_allocate_device(&client->dev);
	if (!iqs269->keypad)
		return -ENOMEM;

	iqs269->keypad->keycodemax = ARRAY_SIZE(iqs269->keycode);
	iqs269->keypad->keycode = iqs269->keycode;
	iqs269->keypad->keycodesize = sizeof(*iqs269->keycode);

	iqs269->keypad->name = "iqs269a_keypad";
	iqs269->keypad->id.bustype = BUS_I2C;

	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
		sw_code = iqs269->switches[i].code;

		for (j = 0; j < IQS269_NUM_CH; j++) {
			keycode = iqs269->keycode[i * IQS269_NUM_CH + j];

			/*
			 * Hall-effect sensing repurposes a pair of dedicated
			 * channels, only one of which reports events.
			 */
			switch (j) {
			case IQS269_CHx_HALL_ACTIVE:
				if (iqs269->hall_enable &&
				    iqs269->switches[i].enabled)
					input_set_capability(iqs269->keypad,
							     EV_SW, sw_code);
				fallthrough;

			case IQS269_CHx_HALL_INACTIVE:
				if (iqs269->hall_enable)
					continue;
				fallthrough;

			default:
				if (keycode != KEY_RESERVED)
					input_set_capability(iqs269->keypad,
							     EV_KEY, keycode);
			}
		}
	}

	for (i = 0; i < IQS269_NUM_SL; i++) {
		if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_NONE)
			continue;

		iqs269->slider[i] = devm_input_allocate_device(&client->dev);
		if (!iqs269->slider[i])
			return -ENOMEM;

		iqs269->slider[i]->keycodemax = ARRAY_SIZE(iqs269->sl_code[i]);
		iqs269->slider[i]->keycode = iqs269->sl_code[i];
		iqs269->slider[i]->keycodesize = sizeof(**iqs269->sl_code);

		iqs269->slider[i]->name = i ? "iqs269a_slider_1"
					    : "iqs269a_slider_0";
		iqs269->slider[i]->id.bustype = BUS_I2C;

		for (j = 0; j < IQS269_NUM_GESTURES; j++)
			if (iqs269->sl_code[i][j] != KEY_RESERVED)
				input_set_capability(iqs269->slider[i], EV_KEY,
						     iqs269->sl_code[i][j]);

		/*
		 * Present the slider as a narrow trackpad if one or more chan-
		 * nels have been selected to participate, but no gestures have
		 * been mapped to a keycode.
		 */
		if (iqs269_slider_type(iqs269, i) == IQS269_SLIDER_RAW) {
			input_set_capability(iqs269->slider[i],
					     EV_KEY, BTN_TOUCH);
			input_set_abs_params(iqs269->slider[i],
					     ABS_X, 0, 255, 0, 0);
		}

		error = input_register_device(iqs269->slider[i]);
		if (error) {
			dev_err(&client->dev,
				"Failed to register slider %d: %d\n", i, error);
			return error;
		}
	}

	return 0;
}

static int iqs269_report(struct iqs269_private *iqs269)
{
	struct i2c_client *client = iqs269->client;
	struct iqs269_flags flags;
	unsigned int sw_code, keycode;
	int error, i, j;
	u8 slider_x[IQS269_NUM_SL];
	u8 dir_mask, state;

	error = regmap_raw_read(iqs269->regmap, IQS269_SYS_FLAGS, &flags,
				sizeof(flags));
	if (error) {
		dev_err(&client->dev, "Failed to read device status: %d\n",
			error);
		return error;
	}

	/*
	 * The device resets itself if its own watchdog bites, which can happen
	 * in the event of an I2C communication error. In this case, the device
	 * asserts a SHOW_RESET interrupt and all registers must be restored.
	 */
	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_SHOW_RESET) {
		dev_err(&client->dev, "Unexpected device reset\n");

		error = iqs269_dev_init(iqs269);
		if (error)
			dev_err(&client->dev,
				"Failed to re-initialize device: %d\n", error);

		return error;
	}

	if (be16_to_cpu(flags.system) & IQS269_SYS_FLAGS_IN_ATI)
		return 0;

	if (iqs269_slider_type(iqs269, 0) == IQS269_SLIDER_RAW ||
	    iqs269_slider_type(iqs269, 1) == IQS269_SLIDER_RAW) {
		error = regmap_raw_read(iqs269->regmap, IQS269_SLIDER_X,
					slider_x, sizeof(slider_x));
		if (error) {
			dev_err(&client->dev,
				"Failed to read slider position: %d\n", error);
			return error;
		}
	}

	for (i = 0; i < IQS269_NUM_SL; i++) {
		flags.gesture >>= (i * IQS269_NUM_GESTURES);

		switch (iqs269_slider_type(iqs269, i)) {
		case IQS269_SLIDER_NONE:
			continue;

		case IQS269_SLIDER_KEY:
			for (j = 0; j < IQS269_NUM_GESTURES; j++)
				input_report_key(iqs269->slider[i],
						 iqs269->sl_code[i][j],
						 flags.gesture & BIT(j));

			if (!(flags.gesture & (BIT(IQS269_GESTURE_FLICK_NEG) |
					       BIT(IQS269_GESTURE_FLICK_POS) |
					       BIT(IQS269_GESTURE_TAP))))
				break;

			input_sync(iqs269->slider[i]);

			/*
			 * Momentary gestures are followed by a complementary
			 * release cycle so as to emulate a full keystroke.
			 */
			for (j = 0; j < IQS269_NUM_GESTURES; j++)
				if (j != IQS269_GESTURE_HOLD)
					input_report_key(iqs269->slider[i],
							 iqs269->sl_code[i][j],
							 0);
			break;

		case IQS269_SLIDER_RAW:
			/*
			 * The slider is considered to be in a state of touch
			 * if any selected channels are in a state of touch.
			 */
			state = flags.states[IQS269_ST_OFFS_TOUCH];
			state &= iqs269->sys_reg.slider_select[i];

			input_report_key(iqs269->slider[i], BTN_TOUCH, state);

			if (state)
				input_report_abs(iqs269->slider[i],
						 ABS_X, slider_x[i]);
			break;
		}

		input_sync(iqs269->slider[i]);
	}

	for (i = 0; i < ARRAY_SIZE(iqs269_events); i++) {
		dir_mask = flags.states[IQS269_ST_OFFS_DIR];
		if (!iqs269_events[i].dir_up)
			dir_mask = ~dir_mask;

		state = flags.states[iqs269_events[i].st_offs] & dir_mask;

		sw_code = iqs269->switches[i].code;

		for (j = 0; j < IQS269_NUM_CH; j++) {
			keycode = iqs269->keycode[i * IQS269_NUM_CH + j];

			switch (j) {
			case IQS269_CHx_HALL_ACTIVE:
				if (iqs269->hall_enable &&
				    iqs269->switches[i].enabled)
					input_report_switch(iqs269->keypad,
							    sw_code,
							    state & BIT(j));
				fallthrough;

			case IQS269_CHx_HALL_INACTIVE:
				if (iqs269->hall_enable)
					continue;
				fallthrough;

			default:
				input_report_key(iqs269->keypad, keycode,
						 state & BIT(j));
			}
		}
	}

	input_sync(iqs269->keypad);

	/*
	 * The following completion signals that ATI has finished, any initial
	 * switch states have been reported and the keypad can be registered.
	 */
	complete_all(&iqs269->ati_done);

	return 0;
}

static irqreturn_t iqs269_irq(int irq, void *context)
{
	struct iqs269_private *iqs269 = context;

	if (iqs269_report(iqs269))
		return IRQ_NONE;

	/*
	 * The device does not deassert its interrupt (RDY) pin until shortly
	 * after receiving an I2C stop condition; the following delay ensures
	 * the interrupt handler does not return before this time.
	 */
	iqs269_irq_wait();

	return IRQ_HANDLED;
}

static ssize_t counts_show(struct device *dev,
			   struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct i2c_client *client = iqs269->client;
	__le16 counts;
	int error;

	if (!iqs269->ati_current || iqs269->hall_enable)
		return -EPERM;

	if (!completion_done(&iqs269->ati_done))
		return -EBUSY;

	/*
	 * Unsolicited I2C communication prompts the device to assert its RDY
	 * pin, so disable the interrupt line until the operation is finished
	 * and RDY has been deasserted.
	 */
	disable_irq(client->irq);

	error = regmap_raw_read(iqs269->regmap,
				IQS269_CHx_COUNTS + iqs269->ch_num * 2,
				&counts, sizeof(counts));

	iqs269_irq_wait();
	enable_irq(client->irq);

	if (error)
		return error;

	return sysfs_emit(buf, "%u\n", le16_to_cpu(counts));
}

static ssize_t hall_bin_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	struct i2c_client *client = iqs269->client;
	unsigned int val;
	int error;

	disable_irq(client->irq);

	error = regmap_read(iqs269->regmap, IQS269_CAL_DATA_A, &val);

	iqs269_irq_wait();
	enable_irq(client->irq);

	if (error)
		return error;

	switch (ch_reg[IQS269_CHx_HALL_ACTIVE].rx_enable &
		ch_reg[IQS269_CHx_HALL_INACTIVE].rx_enable) {
	case IQS269_HALL_PAD_R:
		val &= IQS269_CAL_DATA_A_HALL_BIN_R_MASK;
		val >>= IQS269_CAL_DATA_A_HALL_BIN_R_SHIFT;
		break;

	case IQS269_HALL_PAD_L:
		val &= IQS269_CAL_DATA_A_HALL_BIN_L_MASK;
		val >>= IQS269_CAL_DATA_A_HALL_BIN_L_SHIFT;
		break;

	default:
		return -EINVAL;
	}

	return sysfs_emit(buf, "%u\n", val);
}

static ssize_t hall_enable_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%u\n", iqs269->hall_enable);
}

static ssize_t hall_enable_store(struct device *dev,
				 struct device_attribute *attr, const char *buf,
				 size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	mutex_lock(&iqs269->lock);

	iqs269->hall_enable = val;
	iqs269->ati_current = false;

	mutex_unlock(&iqs269->lock);

	return count;
}

static ssize_t ch_number_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%u\n", iqs269->ch_num);
}

static ssize_t ch_number_store(struct device *dev,
			       struct device_attribute *attr, const char *buf,
			       size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	if (val >= IQS269_NUM_CH)
		return -EINVAL;

	iqs269->ch_num = val;

	return count;
}

static ssize_t rx_enable_show(struct device *dev,
			      struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;

	return sysfs_emit(buf, "%u\n", ch_reg[iqs269->ch_num].rx_enable);
}

static ssize_t rx_enable_store(struct device *dev,
			       struct device_attribute *attr, const char *buf,
			       size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct iqs269_ch_reg *ch_reg = iqs269->sys_reg.ch_reg;
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	if (val > 0xFF)
		return -EINVAL;

	mutex_lock(&iqs269->lock);

	ch_reg[iqs269->ch_num].rx_enable = val;
	iqs269->ati_current = false;

	mutex_unlock(&iqs269->lock);

	return count;
}

static ssize_t ati_mode_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = iqs269_ati_mode_get(iqs269, iqs269->ch_num, &val);
	if (error)
		return error;

	return sysfs_emit(buf, "%u\n", val);
}

static ssize_t ati_mode_store(struct device *dev,
			      struct device_attribute *attr, const char *buf,
			      size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	error = iqs269_ati_mode_set(iqs269, iqs269->ch_num, val);
	if (error)
		return error;

	return count;
}

static ssize_t ati_base_show(struct device *dev,
			     struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = iqs269_ati_base_get(iqs269, iqs269->ch_num, &val);
	if (error)
		return error;

	return sysfs_emit(buf, "%u\n", val);
}

static ssize_t ati_base_store(struct device *dev,
			      struct device_attribute *attr, const char *buf,
			      size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	error = iqs269_ati_base_set(iqs269, iqs269->ch_num, val);
	if (error)
		return error;

	return count;
}

static ssize_t ati_target_show(struct device *dev,
			       struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = iqs269_ati_target_get(iqs269, iqs269->ch_num, &val);
	if (error)
		return error;

	return sysfs_emit(buf, "%u\n", val);
}

static ssize_t ati_target_store(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	error = iqs269_ati_target_set(iqs269, iqs269->ch_num, val);
	if (error)
		return error;

	return count;
}

static ssize_t ati_trigger_show(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%u\n",
			  iqs269->ati_current &&
			  completion_done(&iqs269->ati_done));
}

static ssize_t ati_trigger_store(struct device *dev,
				 struct device_attribute *attr, const char *buf,
				 size_t count)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct i2c_client *client = iqs269->client;
	unsigned int val;
	int error;

	error = kstrtouint(buf, 10, &val);
	if (error)
		return error;

	if (!val)
		return count;

	disable_irq(client->irq);
	reinit_completion(&iqs269->ati_done);

	error = iqs269_dev_init(iqs269);

	iqs269_irq_wait();
	enable_irq(client->irq);

	if (error)
		return error;

	if (!wait_for_completion_timeout(&iqs269->ati_done,
					 msecs_to_jiffies(2000)))
		return -ETIMEDOUT;

	return count;
}

static DEVICE_ATTR_RO(counts);
static DEVICE_ATTR_RO(hall_bin);
static DEVICE_ATTR_RW(hall_enable);
static DEVICE_ATTR_RW(ch_number);
static DEVICE_ATTR_RW(rx_enable);
static DEVICE_ATTR_RW(ati_mode);
static DEVICE_ATTR_RW(ati_base);
static DEVICE_ATTR_RW(ati_target);
static DEVICE_ATTR_RW(ati_trigger);

static struct attribute *iqs269_attrs[] = {
	&dev_attr_counts.attr,
	&dev_attr_hall_bin.attr,
	&dev_attr_hall_enable.attr,
	&dev_attr_ch_number.attr,
	&dev_attr_rx_enable.attr,
	&dev_attr_ati_mode.attr,
	&dev_attr_ati_base.attr,
	&dev_attr_ati_target.attr,
	&dev_attr_ati_trigger.attr,
	NULL,
};
ATTRIBUTE_GROUPS(iqs269);

static const struct regmap_config iqs269_regmap_config = {
	.reg_bits = 8,
	.val_bits = 16,
	.max_register = IQS269_MAX_REG,
};

static int iqs269_probe(struct i2c_client *client)
{
	struct iqs269_private *iqs269;
	int error;

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

	i2c_set_clientdata(client, iqs269);
	iqs269->client = client;

	iqs269->regmap = devm_regmap_init_i2c(client, &iqs269_regmap_config);
	if (IS_ERR(iqs269->regmap)) {
		error = PTR_ERR(iqs269->regmap);
		dev_err(&client->dev, "Failed to initialize register map: %d\n",
			error);
		return error;
	}

	mutex_init(&iqs269->lock);
	init_completion(&iqs269->ati_done);

	iqs269->otp_option = (uintptr_t)device_get_match_data(&client->dev);

	error = regmap_raw_read(iqs269->regmap, IQS269_VER_INFO,
				&iqs269->ver_info, sizeof(iqs269->ver_info));
	if (error)
		return error;

	if (iqs269->ver_info.prod_num != IQS269_VER_INFO_PROD_NUM) {
		dev_err(&client->dev, "Unrecognized product number: 0x%02X\n",
			iqs269->ver_info.prod_num);
		return -EINVAL;
	}

	error = iqs269_parse_prop(iqs269);
	if (error)
		return error;

	error = iqs269_dev_init(iqs269);
	if (error) {
		dev_err(&client->dev, "Failed to initialize device: %d\n",
			error);
		return error;
	}

	error = iqs269_input_init(iqs269);
	if (error)
		return error;

	error = devm_request_threaded_irq(&client->dev, client->irq,
					  NULL, iqs269_irq, IRQF_ONESHOT,
					  client->name, iqs269);
	if (error) {
		dev_err(&client->dev, "Failed to request IRQ: %d\n", error);
		return error;
	}

	if (!wait_for_completion_timeout(&iqs269->ati_done,
					 msecs_to_jiffies(2000))) {
		dev_err(&client->dev, "Failed to complete ATI\n");
		return -ETIMEDOUT;
	}

	/*
	 * The keypad may include one or more switches and is not registered
	 * until ATI is complete and the initial switch states are read.
	 */
	error = input_register_device(iqs269->keypad);
	if (error) {
		dev_err(&client->dev, "Failed to register keypad: %d\n", error);
		return error;
	}

	return error;
}

static u16 iqs269_general_get(struct iqs269_private *iqs269)
{
	u16 general = be16_to_cpu(iqs269->sys_reg.general);

	general &= ~IQS269_SYS_SETTINGS_REDO_ATI;
	general &= ~IQS269_SYS_SETTINGS_ACK_RESET;

	return general | IQS269_SYS_SETTINGS_DIS_AUTO;
}

static int iqs269_suspend(struct device *dev)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct i2c_client *client = iqs269->client;
	int error;
	u16 general = iqs269_general_get(iqs269);

	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
		return 0;

	disable_irq(client->irq);

	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS, general);

	iqs269_irq_wait();
	enable_irq(client->irq);

	return error;
}

static int iqs269_resume(struct device *dev)
{
	struct iqs269_private *iqs269 = dev_get_drvdata(dev);
	struct i2c_client *client = iqs269->client;
	int error;
	u16 general = iqs269_general_get(iqs269);

	if (!(general & IQS269_SYS_SETTINGS_PWR_MODE_MASK))
		return 0;

	disable_irq(client->irq);

	error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
			     general & ~IQS269_SYS_SETTINGS_PWR_MODE_MASK);
	if (!error)
		error = regmap_write(iqs269->regmap, IQS269_SYS_SETTINGS,
				     general & ~IQS269_SYS_SETTINGS_DIS_AUTO);

	iqs269_irq_wait();
	enable_irq(client->irq);

	return error;
}

static DEFINE_SIMPLE_DEV_PM_OPS(iqs269_pm, iqs269_suspend, iqs269_resume);

static const struct of_device_id iqs269_of_match[] = {
	{
		.compatible = "azoteq,iqs269a",
		.data = (void *)IQS269_OTP_OPTION_DEFAULT,
	},
	{
		.compatible = "azoteq,iqs269a-00",
		.data = (void *)IQS269_OTP_OPTION_DEFAULT,
	},
	{
		.compatible = "azoteq,iqs269a-d0",
		.data = (void *)IQS269_OTP_OPTION_TWS,
	},
	{ }
};
MODULE_DEVICE_TABLE(of, iqs269_of_match);

static struct i2c_driver iqs269_i2c_driver = {
	.driver = {
		.name = "iqs269a",
		.dev_groups = iqs269_groups,
		.of_match_table = iqs269_of_match,
		.pm = pm_sleep_ptr(&iqs269_pm),
	},
	.probe = iqs269_probe,
};
module_i2c_driver(iqs269_i2c_driver);

MODULE_AUTHOR("Jeff LaBundy <jeff@labundy.com>");
MODULE_DESCRIPTION("Azoteq IQS269A Capacitive Touch Controller");
MODULE_LICENSE("GPL");