1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
|
/* Copyright (c) 2014-2015, 2017, 2019, The Linux Foundation.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#define pr_fmt(fmt) "haptics: %s: " fmt, __func__
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/log2.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/pwm.h>
#include <linux/regmap.h>
#include <linux/regulator/consumer.h>
#include <linux/slab.h>
#include <linux/qpnp-misc.h>
#include <linux/qpnp/qpnp-revid.h>
/* Register definitions */
#define HAP_STATUS_1_REG(chip) (chip->base + 0x0A)
#define HAP_BUSY_BIT BIT(1)
#define SC_FLAG_BIT BIT(3)
#define AUTO_RES_ERROR_BIT BIT(4)
#define HAP_LRA_AUTO_RES_LO_REG(chip) (chip->base + 0x0B)
#define HAP_LRA_AUTO_RES_HI_REG(chip) (chip->base + 0x0C)
#define HAP_INT_RT_STS_REG(chip) (chip->base + 0x10)
#define SC_INT_RT_STS_BIT BIT(0)
#define PLAY_INT_RT_STS_BIT BIT(1)
#define HAP_EN_CTL_REG(chip) (chip->base + 0x46)
#define HAP_EN_BIT BIT(7)
#define HAP_EN_CTL2_REG(chip) (chip->base + 0x48)
#define BRAKE_EN_BIT BIT(0)
#define HAP_AUTO_RES_CTRL_REG(chip) (chip->base + 0x4B)
#define AUTO_RES_EN_BIT BIT(7)
#define AUTO_RES_ERR_RECOVERY_BIT BIT(3)
#define HAP_CFG1_REG(chip) (chip->base + 0x4C)
#define HAP_ACT_TYPE_MASK BIT(0)
#define HAP_LRA 0
#define HAP_ERM 1
#define HAP_CFG2_REG(chip) (chip->base + 0x4D)
#define HAP_WAVE_SINE 0
#define HAP_WAVE_SQUARE 1
#define HAP_LRA_RES_TYPE_MASK BIT(0)
#define HAP_SEL_REG(chip) (chip->base + 0x4E)
#define HAP_WF_SOURCE_MASK GENMASK(5, 4)
#define HAP_WF_SOURCE_SHIFT 4
#define HAP_LRA_AUTO_RES_REG(chip) (chip->base + 0x4F)
/* For pmi8998 */
#define LRA_AUTO_RES_MODE_MASK GENMASK(6, 4)
#define LRA_AUTO_RES_MODE_SHIFT 4
#define LRA_HIGH_Z_MASK GENMASK(3, 2)
#define LRA_HIGH_Z_SHIFT 2
#define LRA_RES_CAL_MASK GENMASK(1, 0)
#define HAP_RES_CAL_PERIOD_MIN 4
#define HAP_RES_CAL_PERIOD_MAX 32
/* For pm660 */
#define PM660_AUTO_RES_MODE_BIT BIT(7)
#define PM660_AUTO_RES_MODE_SHIFT 7
#define PM660_CAL_DURATION_MASK GENMASK(6, 5)
#define PM660_CAL_DURATION_SHIFT 5
#define PM660_QWD_DRIVE_DURATION_BIT BIT(4)
#define PM660_QWD_DRIVE_DURATION_SHIFT 4
#define PM660_CAL_EOP_BIT BIT(3)
#define PM660_CAL_EOP_SHIFT 3
#define PM660_LRA_RES_CAL_MASK GENMASK(2, 0)
#define HAP_PM660_RES_CAL_PERIOD_MAX 256
#define HAP_VMAX_CFG_REG(chip) (chip->base + 0x51)
#define HAP_VMAX_OVD_BIT BIT(6)
#define HAP_VMAX_MASK GENMASK(5, 1)
#define HAP_VMAX_SHIFT 1
#define HAP_VMAX_MIN_MV 116
#define HAP_VMAX_MAX_MV 3596
#define HAP_ILIM_CFG_REG(chip) (chip->base + 0x52)
#define HAP_ILIM_SEL_MASK BIT(0)
#define HAP_ILIM_400_MA 0
#define HAP_ILIM_800_MA 1
#define HAP_SC_DEB_REG(chip) (chip->base + 0x53)
#define HAP_SC_DEB_MASK GENMASK(2, 0)
#define HAP_SC_DEB_CYCLES_MIN 0
#define HAP_DEF_SC_DEB_CYCLES 8
#define HAP_SC_DEB_CYCLES_MAX 32
#define HAP_RATE_CFG1_REG(chip) (chip->base + 0x54)
#define HAP_RATE_CFG1_MASK GENMASK(7, 0)
#define HAP_RATE_CFG2_REG(chip) (chip->base + 0x55)
#define HAP_RATE_CFG2_MASK GENMASK(3, 0)
/* Shift needed to convert drive period upper bits [11:8] */
#define HAP_RATE_CFG2_SHIFT 8
#define HAP_INT_PWM_REG(chip) (chip->base + 0x56)
#define INT_PWM_FREQ_SEL_MASK GENMASK(1, 0)
#define INT_PWM_FREQ_253_KHZ 0
#define INT_PWM_FREQ_505_KHZ 1
#define INT_PWM_FREQ_739_KHZ 2
#define INT_PWM_FREQ_1076_KHZ 3
#define HAP_EXT_PWM_REG(chip) (chip->base + 0x57)
#define EXT_PWM_FREQ_SEL_MASK GENMASK(1, 0)
#define EXT_PWM_FREQ_25_KHZ 0
#define EXT_PWM_FREQ_50_KHZ 1
#define EXT_PWM_FREQ_75_KHZ 2
#define EXT_PWM_FREQ_100_KHZ 3
#define HAP_PWM_CAP_REG(chip) (chip->base + 0x58)
#define HAP_SC_CLR_REG(chip) (chip->base + 0x59)
#define SC_CLR_BIT BIT(0)
#define HAP_BRAKE_REG(chip) (chip->base + 0x5C)
#define HAP_BRAKE_PAT_MASK 0x3
#define HAP_WF_REPEAT_REG(chip) (chip->base + 0x5E)
#define WF_REPEAT_MASK GENMASK(6, 4)
#define WF_REPEAT_SHIFT 4
#define WF_REPEAT_MIN 1
#define WF_REPEAT_MAX 128
#define WF_S_REPEAT_MASK GENMASK(1, 0)
#define WF_S_REPEAT_MIN 1
#define WF_S_REPEAT_MAX 8
#define HAP_WF_S1_REG(chip) (chip->base + 0x60)
#define HAP_WF_SIGN_BIT BIT(7)
#define HAP_WF_OVD_BIT BIT(6)
#define HAP_WF_SAMP_MAX GENMASK(5, 1)
#define HAP_WF_SAMPLE_LEN 8
#define HAP_PLAY_REG(chip) (chip->base + 0x70)
#define PLAY_BIT BIT(7)
#define PAUSE_BIT BIT(0)
#define HAP_SEC_ACCESS_REG(chip) (chip->base + 0xD0)
#define HAP_TEST2_REG(chip) (chip->base + 0xE3)
#define HAP_EXT_PWM_DTEST_MASK GENMASK(6, 4)
#define HAP_EXT_PWM_DTEST_SHIFT 4
#define PWM_MAX_DTEST_LINES 4
#define HAP_EXT_PWM_PEAK_DATA 0x7F
#define HAP_EXT_PWM_HALF_DUTY 50
#define HAP_EXT_PWM_FULL_DUTY 100
#define HAP_EXT_PWM_DATA_FACTOR 39
/* Other definitions */
#define HAP_BRAKE_PAT_LEN 4
#define HAP_WAVE_SAMP_LEN 8
#define NUM_WF_SET 4
#define HAP_WAVE_SAMP_SET_LEN (HAP_WAVE_SAMP_LEN * NUM_WF_SET)
#define HAP_RATE_CFG_STEP_US 5
#define HAP_WAVE_PLAY_RATE_US_MIN 0
#define HAP_DEF_WAVE_PLAY_RATE_US 5715
#define HAP_WAVE_PLAY_RATE_US_MAX 20475
#define HAP_MAX_PLAY_TIME_MS 15000
enum hap_brake_pat {
NO_BRAKE = 0,
BRAKE_VMAX_4,
BRAKE_VMAX_2,
BRAKE_VMAX,
};
enum hap_auto_res_mode {
HAP_AUTO_RES_NONE,
HAP_AUTO_RES_ZXD,
HAP_AUTO_RES_QWD,
HAP_AUTO_RES_MAX_QWD,
HAP_AUTO_RES_ZXD_EOP,
};
enum hap_pm660_auto_res_mode {
HAP_PM660_AUTO_RES_ZXD,
HAP_PM660_AUTO_RES_QWD,
};
/* high Z option lines */
enum hap_high_z {
HAP_LRA_HIGH_Z_NONE, /* opt0 for PM660 */
HAP_LRA_HIGH_Z_OPT1,
HAP_LRA_HIGH_Z_OPT2,
HAP_LRA_HIGH_Z_OPT3,
};
/* play modes */
enum hap_mode {
HAP_DIRECT,
HAP_BUFFER,
HAP_AUDIO,
HAP_PWM,
};
/* wave/sample repeat */
enum hap_rep_type {
HAP_WAVE_REPEAT = 1,
HAP_WAVE_SAMP_REPEAT,
};
/* status flags */
enum hap_status {
AUTO_RESONANCE_ENABLED = BIT(0),
};
enum hap_play_control {
HAP_STOP,
HAP_PAUSE,
HAP_PLAY,
};
/* pwm channel parameters */
struct pwm_param {
struct pwm_device *pwm_dev;
u32 duty_us;
u32 period_us;
};
/*
* hap_lra_ares_param - Haptic auto_resonance parameters
* @ lra_qwd_drive_duration - LRA QWD drive duration
* @ calibrate_at_eop - Calibrate at EOP
* @ lra_res_cal_period - LRA resonance calibration period
* @ auto_res_mode - auto resonace mode
* @ lra_high_z - high z option line
*/
struct hap_lra_ares_param {
int lra_qwd_drive_duration;
int calibrate_at_eop;
enum hap_high_z lra_high_z;
u16 lra_res_cal_period;
u8 auto_res_mode;
};
/*
* hap_chip - Haptics data structure
* @ pdev - platform device pointer
* @ regmap - regmap pointer
* @ bus_lock - spin lock for bus read/write
* @ play_lock - mutex lock for haptics play/enable control
* @ haptics_work - haptics worker
* @ stop_timer - hrtimer for stopping haptics
* @ auto_res_err_poll_timer - hrtimer for auto-resonance error
* @ base - base address
* @ play_irq - irq for play
* @ sc_irq - irq for short circuit
* @ pwm_data - pwm configuration
* @ ares_cfg - auto resonance configuration
* @ play_time_ms - play time set by the user in ms
* @ max_play_time_ms - max play time in ms
* @ vmax_mv - max voltage in mv
* @ ilim_ma - limiting current in ma
* @ sc_deb_cycles - short circuit debounce cycles
* @ wave_play_rate_us - play rate for waveform
* @ last_rate_cfg - Last rate config updated
* @ wave_rep_cnt - waveform repeat count
* @ wave_s_rep_cnt - waveform sample repeat count
* @ wf_samp_len - waveform sample length
* @ ext_pwm_freq_khz - external pwm frequency in KHz
* @ ext_pwm_dtest_line - DTEST line for external pwm
* @ status_flags - status
* @ play_mode - play mode
* @ act_type - actuator type
* @ wave_shape - waveform shape
* @ wave_samp_idx - wave sample id used to refer start of a sample set
* @ wave_samp - array of wave samples
* @ brake_pat - pattern for active breaking
* @ en_brake - brake state
* @ misc_clk_trim_error_reg - MISC clock trim error register if present
* @ clk_trim_error_code - MISC clock trim error code
* @ drive_period_code_max_limit - calculated drive period code with
percentage variation on the higher side.
* @ drive_period_code_min_limit - calculated drive period code with
percentage variation on the lower side
* @ drive_period_code_max_var_pct - maximum limit of percentage variation of
drive period code
* @ drive_period_code_min_var_pct - minimum limit of percentage variation of
drive period code
* @ last_sc_time - Last time short circuit was detected
* @ sc_count - counter to determine the duration of short circuit
condition
* @ perm_disable - Flag to disable module permanently
* @ state - current state of haptics
* @ module_en - module enable status of haptics
* @ lra_auto_mode - Auto mode selection
* @ play_irq_en - Play interrupt enable status
* @ auto_res_err_recovery_hw - Enable auto resonance error recovery by HW
*/
struct hap_chip {
struct platform_device *pdev;
struct regmap *regmap;
struct pmic_revid_data *revid;
struct led_classdev cdev;
spinlock_t bus_lock;
struct mutex play_lock;
struct mutex param_lock;
struct work_struct haptics_work;
struct hrtimer stop_timer;
struct hrtimer auto_res_err_poll_timer;
u16 base;
int play_irq;
int sc_irq;
struct pwm_param pwm_data;
struct hap_lra_ares_param ares_cfg;
struct regulator *vcc_pon;
u32 play_time_ms;
u32 max_play_time_ms;
u32 vmax_mv;
u8 ilim_ma;
u32 sc_deb_cycles;
u32 wave_play_rate_us;
u16 last_rate_cfg;
u32 wave_rep_cnt;
u32 wave_s_rep_cnt;
u32 wf_samp_len;
u32 ext_pwm_freq_khz;
u8 ext_pwm_dtest_line;
u32 status_flags;
enum hap_mode play_mode;
u8 act_type;
u8 wave_shape;
u8 wave_samp_idx;
u32 wave_samp[HAP_WAVE_SAMP_SET_LEN];
u32 brake_pat[HAP_BRAKE_PAT_LEN];
bool en_brake;
u32 misc_clk_trim_error_reg;
u8 clk_trim_error_code;
u16 drive_period_code_max_limit;
u16 drive_period_code_min_limit;
u8 drive_period_code_max_var_pct;
u8 drive_period_code_min_var_pct;
ktime_t last_sc_time;
u8 sc_count;
bool perm_disable;
atomic_t state;
bool module_en;
bool lra_auto_mode;
bool play_irq_en;
bool auto_res_err_recovery_hw;
bool vcc_pon_enabled;
int vmax_override;
};
struct hap_chip *gchip;
static int qpnp_haptics_parse_buffer_dt(struct hap_chip *chip);
static int qpnp_haptics_parse_pwm_dt(struct hap_chip *chip);
static int qpnp_haptics_read_reg(struct hap_chip *chip, u16 addr, u8 *val,
int len)
{
int rc;
rc = regmap_bulk_read(chip->regmap, addr, val, len);
if (rc < 0)
pr_err("Error reading address: 0x%x - rc %d\n", addr, rc);
return rc;
}
static inline bool is_secure(u16 addr)
{
return ((addr & 0xFF) > 0xD0);
}
static int qpnp_haptics_write_reg(struct hap_chip *chip, u16 addr, u8 *val,
int len)
{
unsigned long flags;
unsigned int unlock = 0xA5;
int rc = 0, i;
spin_lock_irqsave(&chip->bus_lock, flags);
if (is_secure(addr)) {
for (i = 0; i < len; i++) {
rc = regmap_write(chip->regmap,
HAP_SEC_ACCESS_REG(chip), unlock);
if (rc < 0) {
pr_err("Error writing unlock code - rc %d\n",
rc);
goto out;
}
rc = regmap_write(chip->regmap, addr + i, val[i]);
if (rc < 0) {
pr_err("Error writing address 0x%x - rc %d\n",
addr + i, rc);
goto out;
}
}
} else {
if (len > 1)
rc = regmap_bulk_write(chip->regmap, addr, val, len);
else
rc = regmap_write(chip->regmap, addr, *val);
}
if (rc < 0)
pr_err("Error writing address: 0x%x - rc %d\n", addr, rc);
out:
spin_unlock_irqrestore(&chip->bus_lock, flags);
return rc;
}
static int qpnp_haptics_masked_write_reg(struct hap_chip *chip, u16 addr,
u8 mask, u8 val)
{
unsigned long flags;
unsigned int unlock = 0xA5;
int rc;
spin_lock_irqsave(&chip->bus_lock, flags);
if (is_secure(addr)) {
rc = regmap_write(chip->regmap, HAP_SEC_ACCESS_REG(chip),
unlock);
if (rc < 0) {
pr_err("Error writing unlock code - rc %d\n", rc);
goto out;
}
}
rc = regmap_update_bits(chip->regmap, addr, mask, val);
if (rc < 0)
pr_err("Error writing address: 0x%x - rc %d\n", addr, rc);
if (!rc)
pr_debug("wrote to address 0x%x = 0x%x\n", addr, val);
out:
spin_unlock_irqrestore(&chip->bus_lock, flags);
return rc;
}
static inline int get_buffer_mode_duration(struct hap_chip *chip)
{
int sample_count, sample_duration;
sample_count = chip->wave_rep_cnt * chip->wave_s_rep_cnt *
chip->wf_samp_len;
sample_duration = sample_count * chip->wave_play_rate_us;
pr_debug("sample_count: %d sample_duration: %d\n", sample_count,
sample_duration);
return (sample_duration / 1000);
}
static bool is_sw_lra_auto_resonance_control(struct hap_chip *chip)
{
if (chip->act_type != HAP_LRA)
return false;
if (chip->auto_res_err_recovery_hw)
return false;
/*
* For short pattern in auto mode, we use buffer mode and auto
* resonance is not needed.
*/
if (chip->lra_auto_mode && chip->play_mode == HAP_BUFFER)
return false;
return true;
}
#define HAPTICS_BACK_EMF_DELAY_US 20000
static int qpnp_haptics_auto_res_enable(struct hap_chip *chip, bool enable)
{
int rc = 0;
u32 delay_us = HAPTICS_BACK_EMF_DELAY_US;
u8 val;
bool auto_res_mode_qwd;
if (chip->act_type != HAP_LRA)
return 0;
if (chip->revid->pmic_subtype == PM660_SUBTYPE)
auto_res_mode_qwd = (chip->ares_cfg.auto_res_mode ==
HAP_PM660_AUTO_RES_QWD);
else
auto_res_mode_qwd = (chip->ares_cfg.auto_res_mode ==
HAP_AUTO_RES_QWD);
/*
* Do not enable auto resonance if auto mode is enabled and auto
* resonance mode is QWD, meaning long pattern.
*/
if (chip->lra_auto_mode && auto_res_mode_qwd && enable) {
pr_debug("auto_mode enabled, not enabling auto_res\n");
return 0;
}
/*
* For auto resonance detection to work properly, sufficient back-emf
* has to be generated. In general, back-emf takes some time to build
* up. When the auto resonance mode is chosen as QWD, high-z will be
* applied for every LRA cycle and hence there won't be enough back-emf
* at the start-up. Hence, the motor needs to vibrate for few LRA cycles
* after the PLAY bit is asserted. Enable the auto resonance after
* 'time_required_to_generate_back_emf_us' is completed.
*/
if (auto_res_mode_qwd && enable)
usleep_range(delay_us, delay_us + 1);
val = enable ? AUTO_RES_EN_BIT : 0;
if (chip->revid->pmic_subtype == PM660_SUBTYPE)
rc = qpnp_haptics_masked_write_reg(chip,
HAP_AUTO_RES_CTRL_REG(chip),
AUTO_RES_EN_BIT, val);
else
rc = qpnp_haptics_masked_write_reg(chip, HAP_TEST2_REG(chip),
AUTO_RES_EN_BIT, val);
if (rc < 0)
return rc;
if (enable)
chip->status_flags |= AUTO_RESONANCE_ENABLED;
else
chip->status_flags &= ~AUTO_RESONANCE_ENABLED;
pr_debug("auto_res %sabled\n", enable ? "en" : "dis");
return rc;
}
static int qpnp_haptics_update_rate_cfg(struct hap_chip *chip, u16 play_rate)
{
int rc;
u8 val[2];
if (chip->last_rate_cfg == play_rate) {
pr_debug("Same rate_cfg %x\n", play_rate);
return 0;
}
val[0] = play_rate & HAP_RATE_CFG1_MASK;
val[1] = (play_rate >> HAP_RATE_CFG2_SHIFT) & HAP_RATE_CFG2_MASK;
rc = qpnp_haptics_write_reg(chip, HAP_RATE_CFG1_REG(chip), val, 2);
if (rc < 0)
return rc;
pr_debug("Play rate code 0x%x\n", play_rate);
chip->last_rate_cfg = play_rate;
return 0;
}
static void qpnp_haptics_update_lra_frequency(struct hap_chip *chip)
{
u8 lra_auto_res[2], val;
u32 play_rate_code;
u16 rate_cfg;
int rc;
rc = qpnp_haptics_read_reg(chip, HAP_LRA_AUTO_RES_LO_REG(chip),
lra_auto_res, 2);
if (rc < 0) {
pr_err("Error in reading LRA_AUTO_RES_LO/HI, rc=%d\n", rc);
return;
}
play_rate_code =
(lra_auto_res[1] & 0xF0) << 4 | (lra_auto_res[0] & 0xFF);
pr_debug("lra_auto_res_lo = 0x%x lra_auto_res_hi = 0x%x play_rate_code = 0x%x\n",
lra_auto_res[0], lra_auto_res[1], play_rate_code);
rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1);
if (rc < 0)
return;
/*
* If the drive period code read from AUTO_RES_LO and AUTO_RES_HI
* registers is more than the max limit percent variation or less
* than the min limit percent variation specified through DT, then
* auto-resonance is disabled.
*/
if ((val & AUTO_RES_ERROR_BIT) ||
((play_rate_code <= chip->drive_period_code_min_limit) ||
(play_rate_code >= chip->drive_period_code_max_limit))) {
if (val & AUTO_RES_ERROR_BIT)
pr_debug("Auto-resonance error %x\n", val);
else
pr_debug("play rate %x out of bounds [min: 0x%x, max: 0x%x]\n",
play_rate_code,
chip->drive_period_code_min_limit,
chip->drive_period_code_max_limit);
rc = qpnp_haptics_auto_res_enable(chip, false);
if (rc < 0)
pr_debug("Auto-resonance disable failed\n");
return;
}
/*
* bits[7:4] of AUTO_RES_HI should be written to bits[3:0] of RATE_CFG2
*/
lra_auto_res[1] >>= 4;
rate_cfg = lra_auto_res[1] << 8 | lra_auto_res[0];
rc = qpnp_haptics_update_rate_cfg(chip, rate_cfg);
if (rc < 0)
pr_debug("Error in updating rate_cfg\n");
}
#define MAX_RETRIES 5
#define HAP_CYCLES 4
static bool is_haptics_idle(struct hap_chip *chip)
{
unsigned long wait_time_us;
int rc, i;
u8 val;
rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1);
if (rc < 0)
return false;
if (!(val & HAP_BUSY_BIT))
return true;
if (chip->play_time_ms <= 20)
wait_time_us = chip->play_time_ms * 1000;
else
wait_time_us = chip->wave_play_rate_us * HAP_CYCLES;
for (i = 0; i < MAX_RETRIES; i++) {
/* wait for play_rate cycles */
usleep_range(wait_time_us, wait_time_us + 1);
if (chip->play_mode == HAP_DIRECT ||
chip->play_mode == HAP_PWM)
return true;
rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val,
1);
if (rc < 0)
return false;
if (!(val & HAP_BUSY_BIT))
return true;
}
if (i >= MAX_RETRIES && (val & HAP_BUSY_BIT)) {
pr_debug("Haptics Busy after %d retries\n", i);
return false;
}
return true;
}
static int qpnp_haptics_mod_enable(struct hap_chip *chip, bool enable)
{
u8 val;
int rc;
if (chip->module_en == enable)
return 0;
if (!enable) {
if (!is_haptics_idle(chip))
pr_debug("Disabling module forcibly\n");
}
val = enable ? HAP_EN_BIT : 0;
rc = qpnp_haptics_write_reg(chip, HAP_EN_CTL_REG(chip), &val, 1);
if (rc < 0)
return rc;
chip->module_en = enable;
return 0;
}
static int qpnp_haptics_play_control(struct hap_chip *chip,
enum hap_play_control ctrl)
{
u8 val;
int rc;
switch (ctrl) {
case HAP_STOP:
val = 0;
break;
case HAP_PAUSE:
val = PAUSE_BIT;
break;
case HAP_PLAY:
val = PLAY_BIT;
break;
default:
return 0;
}
rc = qpnp_haptics_write_reg(chip, HAP_PLAY_REG(chip), &val, 1);
if (rc < 0) {
pr_err("Error in writing to PLAY_REG, rc=%d\n", rc);
return rc;
}
pr_debug("haptics play ctrl: %d\n", ctrl);
return rc;
}
#define AUTO_RES_ERR_POLL_TIME_NS (20 * NSEC_PER_MSEC)
static int qpnp_haptics_play(struct hap_chip *chip, bool enable)
{
int rc = 0, time_ms = chip->play_time_ms;
if (chip->perm_disable && enable)
return 0;
mutex_lock(&chip->play_lock);
if (enable) {
if (chip->play_mode == HAP_PWM) {
rc = pwm_enable(chip->pwm_data.pwm_dev);
if (rc < 0) {
pr_err("Error in enabling PWM, rc=%d\n", rc);
goto out;
}
}
rc = qpnp_haptics_auto_res_enable(chip, false);
if (rc < 0) {
pr_err("Error in disabling auto_res, rc=%d\n", rc);
goto out;
}
rc = qpnp_haptics_mod_enable(chip, true);
if (rc < 0) {
pr_err("Error in enabling module, rc=%d\n", rc);
goto out;
}
rc = qpnp_haptics_play_control(chip, HAP_PLAY);
if (rc < 0) {
pr_err("Error in enabling play, rc=%d\n", rc);
goto out;
}
if (chip->play_mode == HAP_BUFFER)
time_ms = get_buffer_mode_duration(chip);
hrtimer_start(&chip->stop_timer,
ktime_set(time_ms / MSEC_PER_SEC,
(time_ms % MSEC_PER_SEC) * NSEC_PER_MSEC),
HRTIMER_MODE_REL);
rc = qpnp_haptics_auto_res_enable(chip, true);
if (rc < 0) {
pr_err("Error in enabling auto_res, rc=%d\n", rc);
goto out;
}
if (is_sw_lra_auto_resonance_control(chip))
hrtimer_start(&chip->auto_res_err_poll_timer,
ktime_set(0, AUTO_RES_ERR_POLL_TIME_NS),
HRTIMER_MODE_REL);
} else {
rc = qpnp_haptics_play_control(chip, HAP_STOP);
if (rc < 0) {
pr_err("Error in disabling play, rc=%d\n", rc);
goto out;
}
if (is_sw_lra_auto_resonance_control(chip)) {
if (chip->status_flags & AUTO_RESONANCE_ENABLED)
qpnp_haptics_update_lra_frequency(chip);
hrtimer_cancel(&chip->auto_res_err_poll_timer);
}
if (chip->play_mode == HAP_PWM)
pwm_disable(chip->pwm_data.pwm_dev);
if (chip->play_mode == HAP_BUFFER)
chip->wave_samp_idx = 0;
}
out:
mutex_unlock(&chip->play_lock);
return rc;
}
static void qpnp_haptics_work(struct work_struct *work)
{
struct hap_chip *chip = container_of(work, struct hap_chip,
haptics_work);
int rc;
bool enable;
enable = atomic_read(&chip->state);
pr_debug("state: %d\n", enable);
if (chip->vcc_pon && enable && !chip->vcc_pon_enabled) {
rc = regulator_enable(chip->vcc_pon);
if (rc < 0)
pr_err("%s: could not enable vcc_pon regulator rc=%d\n",
__func__, rc);
else
chip->vcc_pon_enabled = true;
}
rc = qpnp_haptics_play(chip, enable);
if (rc < 0)
pr_err("Error in %sing haptics, rc=%d\n",
enable ? "play" : "stopp", rc);
if (chip->vcc_pon && !enable && chip->vcc_pon_enabled) {
rc = regulator_disable(chip->vcc_pon);
if (rc)
pr_err("%s: could not disable vcc_pon regulator rc=%d\n",
__func__, rc);
else
chip->vcc_pon_enabled = false;
}
}
static enum hrtimer_restart hap_stop_timer(struct hrtimer *timer)
{
struct hap_chip *chip = container_of(timer, struct hap_chip,
stop_timer);
atomic_set(&chip->state, 0);
schedule_work(&chip->haptics_work);
return HRTIMER_NORESTART;
}
static enum hrtimer_restart hap_auto_res_err_poll_timer(struct hrtimer *timer)
{
struct hap_chip *chip = container_of(timer, struct hap_chip,
auto_res_err_poll_timer);
if (!(chip->status_flags & AUTO_RESONANCE_ENABLED))
return HRTIMER_NORESTART;
qpnp_haptics_update_lra_frequency(chip);
hrtimer_forward(&chip->auto_res_err_poll_timer, ktime_get(),
ktime_set(0, AUTO_RES_ERR_POLL_TIME_NS));
return HRTIMER_NORESTART;
}
static int qpnp_haptics_suspend(struct device *dev)
{
struct hap_chip *chip = dev_get_drvdata(dev);
int rc;
rc = qpnp_haptics_play(chip, false);
if (rc < 0)
pr_err("Error in stopping haptics, rc=%d\n", rc);
rc = qpnp_haptics_mod_enable(chip, false);
if (rc < 0)
pr_err("Error in disabling module, rc=%d\n", rc);
return 0;
}
static int qpnp_haptics_wave_rep_config(struct hap_chip *chip,
enum hap_rep_type type)
{
int rc;
u8 val = 0, mask = 0;
if (type & HAP_WAVE_REPEAT) {
if (chip->wave_rep_cnt < WF_REPEAT_MIN)
chip->wave_rep_cnt = WF_REPEAT_MIN;
else if (chip->wave_rep_cnt > WF_REPEAT_MAX)
chip->wave_rep_cnt = WF_REPEAT_MAX;
mask = WF_REPEAT_MASK;
val = ilog2(chip->wave_rep_cnt) << WF_REPEAT_SHIFT;
}
if (type & HAP_WAVE_SAMP_REPEAT) {
if (chip->wave_s_rep_cnt < WF_S_REPEAT_MIN)
chip->wave_s_rep_cnt = WF_S_REPEAT_MIN;
else if (chip->wave_s_rep_cnt > WF_S_REPEAT_MAX)
chip->wave_s_rep_cnt = WF_S_REPEAT_MAX;
mask |= WF_S_REPEAT_MASK;
val |= ilog2(chip->wave_s_rep_cnt);
}
rc = qpnp_haptics_masked_write_reg(chip, HAP_WF_REPEAT_REG(chip),
mask, val);
return rc;
}
/* configuration api for buffer mode */
static int qpnp_haptics_buffer_config(struct hap_chip *chip, u32 *wave_samp,
bool overdrive)
{
u8 buf[HAP_WAVE_SAMP_LEN];
u32 *ptr;
int rc, i;
if (wave_samp) {
ptr = wave_samp;
} else {
if (chip->wave_samp_idx >= ARRAY_SIZE(chip->wave_samp)) {
pr_err("Incorrect wave_samp_idx %d\n",
chip->wave_samp_idx);
return -EINVAL;
}
ptr = &chip->wave_samp[chip->wave_samp_idx];
}
/* Don't set override bit in waveform sample for PM660 */
if (chip->revid->pmic_subtype == PM660_SUBTYPE)
overdrive = false;
/* Configure WAVE_SAMPLE1 to WAVE_SAMPLE8 register */
for (i = 0; i < HAP_WAVE_SAMP_LEN; i++) {
buf[i] = ptr[i];
if (buf[i])
buf[i] |= (overdrive ? HAP_WF_OVD_BIT : 0);
}
rc = qpnp_haptics_write_reg(chip, HAP_WF_S1_REG(chip), buf,
HAP_WAVE_SAMP_LEN);
return rc;
}
/* configuration api for pwm */
static int qpnp_haptics_pwm_config(struct hap_chip *chip)
{
u8 val = 0;
int rc;
if (chip->ext_pwm_freq_khz == 0)
return 0;
/* Configure the EXTERNAL_PWM register */
if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_25_KHZ) {
chip->ext_pwm_freq_khz = EXT_PWM_FREQ_25_KHZ;
val = 0;
} else if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_50_KHZ) {
chip->ext_pwm_freq_khz = EXT_PWM_FREQ_50_KHZ;
val = 1;
} else if (chip->ext_pwm_freq_khz <= EXT_PWM_FREQ_75_KHZ) {
chip->ext_pwm_freq_khz = EXT_PWM_FREQ_75_KHZ;
val = 2;
} else {
chip->ext_pwm_freq_khz = EXT_PWM_FREQ_100_KHZ;
val = 3;
}
rc = qpnp_haptics_masked_write_reg(chip, HAP_EXT_PWM_REG(chip),
EXT_PWM_FREQ_SEL_MASK, val);
if (rc < 0)
return rc;
if (chip->ext_pwm_dtest_line < 0 ||
chip->ext_pwm_dtest_line > PWM_MAX_DTEST_LINES) {
pr_err("invalid dtest line\n");
return -EINVAL;
}
if (chip->ext_pwm_dtest_line > 0) {
/* disable auto res for PWM mode */
val = chip->ext_pwm_dtest_line << HAP_EXT_PWM_DTEST_SHIFT;
rc = qpnp_haptics_masked_write_reg(chip, HAP_TEST2_REG(chip),
HAP_EXT_PWM_DTEST_MASK | AUTO_RES_EN_BIT, val);
if (rc < 0)
return rc;
}
rc = pwm_config(chip->pwm_data.pwm_dev,
chip->pwm_data.duty_us * NSEC_PER_USEC,
chip->pwm_data.period_us * NSEC_PER_USEC);
if (rc < 0) {
pr_err("pwm_config failed, rc=%d\n", rc);
return rc;
}
return 0;
}
static int qpnp_haptics_lra_auto_res_config(struct hap_chip *chip,
struct hap_lra_ares_param *tmp_cfg)
{
struct hap_lra_ares_param *ares_cfg;
int rc;
u8 val = 0, mask = 0;
/* disable auto resonance for ERM */
if (chip->act_type == HAP_ERM) {
val = 0x00;
rc = qpnp_haptics_write_reg(chip, HAP_LRA_AUTO_RES_REG(chip),
&val, 1);
return rc;
}
if (chip->auto_res_err_recovery_hw) {
rc = qpnp_haptics_masked_write_reg(chip,
HAP_AUTO_RES_CTRL_REG(chip),
AUTO_RES_ERR_RECOVERY_BIT, AUTO_RES_ERR_RECOVERY_BIT);
if (rc < 0)
return rc;
}
if (tmp_cfg)
ares_cfg = tmp_cfg;
else
ares_cfg = &chip->ares_cfg;
if (ares_cfg->lra_res_cal_period < HAP_RES_CAL_PERIOD_MIN)
ares_cfg->lra_res_cal_period = HAP_RES_CAL_PERIOD_MIN;
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
if (ares_cfg->lra_res_cal_period >
HAP_PM660_RES_CAL_PERIOD_MAX)
ares_cfg->lra_res_cal_period =
HAP_PM660_RES_CAL_PERIOD_MAX;
if (ares_cfg->auto_res_mode == HAP_PM660_AUTO_RES_QWD)
ares_cfg->lra_res_cal_period = 0;
if (ares_cfg->lra_res_cal_period)
val = ilog2(ares_cfg->lra_res_cal_period /
HAP_RES_CAL_PERIOD_MIN) + 1;
} else {
if (ares_cfg->lra_res_cal_period > HAP_RES_CAL_PERIOD_MAX)
ares_cfg->lra_res_cal_period =
HAP_RES_CAL_PERIOD_MAX;
if (ares_cfg->lra_res_cal_period)
val = ilog2(ares_cfg->lra_res_cal_period /
HAP_RES_CAL_PERIOD_MIN);
}
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
val |= ares_cfg->auto_res_mode << PM660_AUTO_RES_MODE_SHIFT;
mask = PM660_AUTO_RES_MODE_BIT;
val |= ares_cfg->lra_high_z << PM660_CAL_DURATION_SHIFT;
mask |= PM660_CAL_DURATION_MASK;
if (ares_cfg->lra_qwd_drive_duration != -EINVAL) {
val |= ares_cfg->lra_qwd_drive_duration <<
PM660_QWD_DRIVE_DURATION_SHIFT;
mask |= PM660_QWD_DRIVE_DURATION_BIT;
}
if (ares_cfg->calibrate_at_eop != -EINVAL) {
val |= ares_cfg->calibrate_at_eop <<
PM660_CAL_EOP_SHIFT;
mask |= PM660_CAL_EOP_BIT;
}
mask |= PM660_LRA_RES_CAL_MASK;
} else {
val |= (ares_cfg->auto_res_mode << LRA_AUTO_RES_MODE_SHIFT);
val |= (ares_cfg->lra_high_z << LRA_HIGH_Z_SHIFT);
mask = LRA_AUTO_RES_MODE_MASK | LRA_HIGH_Z_MASK |
LRA_RES_CAL_MASK;
}
pr_debug("mode: %d hi_z period: %d cal_period: %d\n",
ares_cfg->auto_res_mode, ares_cfg->lra_high_z,
ares_cfg->lra_res_cal_period);
rc = qpnp_haptics_masked_write_reg(chip, HAP_LRA_AUTO_RES_REG(chip),
mask, val);
return rc;
}
/* configuration api for play mode */
static int qpnp_haptics_play_mode_config(struct hap_chip *chip)
{
u8 val = 0;
int rc;
if (!is_haptics_idle(chip))
return -EBUSY;
val = chip->play_mode << HAP_WF_SOURCE_SHIFT;
rc = qpnp_haptics_masked_write_reg(chip, HAP_SEL_REG(chip),
HAP_WF_SOURCE_MASK, val);
if (!rc) {
if (chip->play_mode == HAP_BUFFER && !chip->play_irq_en) {
enable_irq(chip->play_irq);
chip->play_irq_en = true;
} else if (chip->play_mode != HAP_BUFFER && chip->play_irq_en) {
disable_irq(chip->play_irq);
chip->play_irq_en = false;
}
}
return rc;
}
/* configuration api for max voltage */
static int qpnp_haptics_vmax_config(struct hap_chip *chip, int vmax_mv,
bool overdrive)
{
u8 val = 0;
int rc;
if (vmax_mv < 0)
return -EINVAL;
/* Allow setting override bit in VMAX_CFG only for PM660 */
if (chip->revid->pmic_subtype != PM660_SUBTYPE)
overdrive = false;
if (vmax_mv < HAP_VMAX_MIN_MV)
vmax_mv = HAP_VMAX_MIN_MV;
else if (vmax_mv > HAP_VMAX_MAX_MV)
vmax_mv = HAP_VMAX_MAX_MV;
val = DIV_ROUND_CLOSEST(vmax_mv, HAP_VMAX_MIN_MV);
val <<= HAP_VMAX_SHIFT;
if (overdrive)
val |= HAP_VMAX_OVD_BIT;
rc = qpnp_haptics_masked_write_reg(chip, HAP_VMAX_CFG_REG(chip),
HAP_VMAX_MASK | HAP_VMAX_OVD_BIT, val);
return rc;
}
/* configuration api for ilim */
static int qpnp_haptics_ilim_config(struct hap_chip *chip)
{
int rc;
if (chip->ilim_ma < HAP_ILIM_400_MA)
chip->ilim_ma = HAP_ILIM_400_MA;
else if (chip->ilim_ma > HAP_ILIM_800_MA)
chip->ilim_ma = HAP_ILIM_800_MA;
rc = qpnp_haptics_masked_write_reg(chip, HAP_ILIM_CFG_REG(chip),
HAP_ILIM_SEL_MASK, chip->ilim_ma);
return rc;
}
/* configuration api for short circuit debounce */
static int qpnp_haptics_sc_deb_config(struct hap_chip *chip)
{
u8 val = 0;
int rc;
if (chip->sc_deb_cycles < HAP_SC_DEB_CYCLES_MIN)
chip->sc_deb_cycles = HAP_SC_DEB_CYCLES_MIN;
else if (chip->sc_deb_cycles > HAP_SC_DEB_CYCLES_MAX)
chip->sc_deb_cycles = HAP_SC_DEB_CYCLES_MAX;
if (chip->sc_deb_cycles != HAP_SC_DEB_CYCLES_MIN)
val = ilog2(chip->sc_deb_cycles /
HAP_DEF_SC_DEB_CYCLES) + 1;
else
val = HAP_SC_DEB_CYCLES_MIN;
rc = qpnp_haptics_masked_write_reg(chip, HAP_SC_DEB_REG(chip),
HAP_SC_DEB_MASK, val);
return rc;
}
static int qpnp_haptics_brake_config(struct hap_chip *chip, u32 *brake_pat)
{
int rc, i;
u32 temp, *ptr;
u8 val;
/* Configure BRAKE register */
rc = qpnp_haptics_masked_write_reg(chip, HAP_EN_CTL2_REG(chip),
BRAKE_EN_BIT, (u8)chip->en_brake);
if (rc < 0)
return rc;
/* If braking is not enabled, skip configuring brake pattern */
if (!chip->en_brake)
return 0;
if (!brake_pat)
ptr = chip->brake_pat;
else
ptr = brake_pat;
for (i = HAP_BRAKE_PAT_LEN - 1, val = 0; i >= 0; i--) {
ptr[i] &= HAP_BRAKE_PAT_MASK;
temp = i << 1;
val |= ptr[i] << temp;
}
rc = qpnp_haptics_write_reg(chip, HAP_BRAKE_REG(chip), &val, 1);
if (rc < 0)
return rc;
return 0;
}
static int qpnp_haptics_auto_mode_config(struct hap_chip *chip, int time_ms)
{
struct hap_lra_ares_param ares_cfg;
enum hap_mode old_play_mode;
u8 old_ares_mode;
u32 brake_pat[HAP_BRAKE_PAT_LEN] = {0};
u32 wave_samp[HAP_WAVE_SAMP_LEN] = {0};
int rc, vmax_mv;
if (!chip->lra_auto_mode)
return false;
/* For now, this is for LRA only */
if (chip->act_type == HAP_ERM)
return 0;
old_ares_mode = chip->ares_cfg.auto_res_mode;
old_play_mode = chip->play_mode;
pr_debug("auto_mode, time_ms: %d\n", time_ms);
if (time_ms <= 20) {
wave_samp[0] = HAP_WF_SAMP_MAX;
wave_samp[1] = HAP_WF_SAMP_MAX;
chip->wf_samp_len = 2;
if (time_ms > 15) {
wave_samp[2] = HAP_WF_SAMP_MAX;
chip->wf_samp_len = 3;
}
/* short pattern */
rc = qpnp_haptics_parse_buffer_dt(chip);
if (!rc) {
rc = qpnp_haptics_wave_rep_config(chip,
HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT);
if (rc < 0) {
pr_err("Error in configuring wave_rep config %d\n",
rc);
return rc;
}
rc = qpnp_haptics_buffer_config(chip, wave_samp, true);
if (rc < 0) {
pr_err("Error in configuring buffer mode %d\n",
rc);
return rc;
}
}
ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT1;
ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MIN;
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
ares_cfg.auto_res_mode = HAP_PM660_AUTO_RES_QWD;
ares_cfg.lra_qwd_drive_duration = 0;
ares_cfg.calibrate_at_eop = 0;
} else {
ares_cfg.auto_res_mode = HAP_AUTO_RES_ZXD_EOP;
ares_cfg.lra_qwd_drive_duration = -EINVAL;
ares_cfg.calibrate_at_eop = -EINVAL;
}
vmax_mv = HAP_VMAX_MAX_MV;
rc = qpnp_haptics_vmax_config(chip, vmax_mv, true);
if (rc < 0)
return rc;
/* enable play_irq for buffer mode */
if (chip->play_irq >= 0 && !chip->play_irq_en) {
enable_irq(chip->play_irq);
chip->play_irq_en = true;
}
brake_pat[0] = BRAKE_VMAX;
chip->play_mode = HAP_BUFFER;
chip->wave_shape = HAP_WAVE_SQUARE;
} else {
/* long pattern */
ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT1;
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
ares_cfg.auto_res_mode = HAP_PM660_AUTO_RES_ZXD;
ares_cfg.lra_res_cal_period =
HAP_PM660_RES_CAL_PERIOD_MAX;
ares_cfg.lra_qwd_drive_duration = 0;
ares_cfg.calibrate_at_eop = 1;
} else {
ares_cfg.auto_res_mode = HAP_AUTO_RES_QWD;
ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MAX;
ares_cfg.lra_qwd_drive_duration = -EINVAL;
ares_cfg.calibrate_at_eop = -EINVAL;
}
vmax_mv = chip->vmax_mv;
rc = qpnp_haptics_vmax_config(chip, vmax_mv, false);
if (rc < 0)
return rc;
/* enable play_irq for direct mode */
if (chip->play_irq >= 0 && chip->play_irq_en) {
disable_irq(chip->play_irq);
chip->play_irq_en = false;
}
chip->play_mode = HAP_DIRECT;
chip->wave_shape = HAP_WAVE_SINE;
}
chip->ares_cfg.auto_res_mode = ares_cfg.auto_res_mode;
rc = qpnp_haptics_lra_auto_res_config(chip, &ares_cfg);
if (rc < 0) {
chip->ares_cfg.auto_res_mode = old_ares_mode;
return rc;
}
rc = qpnp_haptics_play_mode_config(chip);
if (rc < 0) {
chip->play_mode = old_play_mode;
return rc;
}
rc = qpnp_haptics_brake_config(chip, brake_pat);
if (rc < 0)
return rc;
rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG2_REG(chip),
HAP_LRA_RES_TYPE_MASK, chip->wave_shape);
if (rc < 0)
return rc;
return 0;
}
void set_vibrate(int val)
{
int rc;
if (val > gchip->max_play_time_ms)
return;
mutex_lock(&gchip->param_lock);
rc = qpnp_haptics_auto_mode_config(gchip, val);
if (rc < 0) {
pr_err("Unable to do auto mode config\n");
mutex_unlock(&gchip->param_lock);
return;
}
gchip->play_time_ms = val;
mutex_unlock(&gchip->param_lock);
hrtimer_cancel(&gchip->stop_timer);
if (is_sw_lra_auto_resonance_control(gchip))
hrtimer_cancel(&gchip->auto_res_err_poll_timer);
cancel_work_sync(&gchip->haptics_work);
atomic_set(&gchip->state, 1);
schedule_work(&gchip->haptics_work);
}
static irqreturn_t qpnp_haptics_play_irq_handler(int irq, void *data)
{
struct hap_chip *chip = data;
int rc;
if (chip->play_mode != HAP_BUFFER)
goto irq_handled;
if (chip->wave_samp[chip->wave_samp_idx + HAP_WAVE_SAMP_LEN] > 0) {
chip->wave_samp_idx += HAP_WAVE_SAMP_LEN;
if (chip->wave_samp_idx >= ARRAY_SIZE(chip->wave_samp)) {
pr_debug("Samples over\n");
} else {
pr_debug("moving to next sample set %d\n",
chip->wave_samp_idx);
/* Moving to next set of wave sample */
rc = qpnp_haptics_buffer_config(chip, NULL, false);
if (rc < 0) {
pr_err("Error in configuring buffer, rc=%d\n",
rc);
goto irq_handled;
}
}
}
irq_handled:
return IRQ_HANDLED;
}
#define SC_MAX_COUNT 5
#define SC_COUNT_RST_DELAY_US 1000000
static irqreturn_t qpnp_haptics_sc_irq_handler(int irq, void *data)
{
struct hap_chip *chip = data;
int rc;
u8 val;
s64 sc_delta_time_us;
ktime_t temp;
rc = qpnp_haptics_read_reg(chip, HAP_STATUS_1_REG(chip), &val, 1);
if (rc < 0)
goto irq_handled;
if (!(val & SC_FLAG_BIT)) {
chip->sc_count = 0;
goto irq_handled;
}
pr_debug("SC irq fired\n");
temp = ktime_get();
sc_delta_time_us = ktime_us_delta(temp, chip->last_sc_time);
chip->last_sc_time = temp;
if (sc_delta_time_us > SC_COUNT_RST_DELAY_US)
chip->sc_count = 0;
else
chip->sc_count++;
val = SC_CLR_BIT;
rc = qpnp_haptics_write_reg(chip, HAP_SC_CLR_REG(chip), &val, 1);
if (rc < 0) {
pr_err("Error in writing to SC_CLR_REG, rc=%d\n", rc);
goto irq_handled;
}
/* Permanently disable module if SC condition persists */
if (chip->sc_count > SC_MAX_COUNT) {
pr_crit("SC persists, permanently disabling haptics\n");
rc = qpnp_haptics_mod_enable(chip, false);
if (rc < 0) {
pr_err("Error in disabling module, rc=%d\n", rc);
goto irq_handled;
}
chip->perm_disable = true;
}
irq_handled:
return IRQ_HANDLED;
}
/* All sysfs show/store functions below */
#define HAP_STR_SIZE 128
static int parse_string(const char *in_buf, char *out_buf)
{
int i;
if (snprintf(out_buf, HAP_STR_SIZE, "%s", in_buf) > HAP_STR_SIZE)
return -EINVAL;
for (i = 0; i < strlen(out_buf); i++) {
if (out_buf[i] == ' ' || out_buf[i] == '\n' ||
out_buf[i] == '\t') {
out_buf[i] = '\0';
break;
}
}
return 0;
}
static ssize_t qpnp_haptics_show_state(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->module_en);
}
static ssize_t qpnp_haptics_store_state(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
/* At present, nothing to do with setting state */
return count;
}
static ssize_t qpnp_haptics_show_duration(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
ktime_t time_rem;
s64 time_us = 0;
if (hrtimer_active(&chip->stop_timer)) {
time_rem = hrtimer_get_remaining(&chip->stop_timer);
time_us = ktime_to_us(time_rem);
}
return snprintf(buf, PAGE_SIZE, "%lld\n", div_s64(time_us, 1000));
}
static ssize_t qpnp_haptics_store_duration(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
u32 val;
int rc;
rc = kstrtouint(buf, 0, &val);
if (rc < 0)
return rc;
/* setting 0 on duration is NOP for now */
if (val <= 0)
return count;
if (val > chip->max_play_time_ms)
return -EINVAL;
mutex_lock(&chip->param_lock);
rc = qpnp_haptics_auto_mode_config(chip, val);
if (rc < 0) {
pr_err("Unable to do auto mode config\n");
mutex_unlock(&chip->param_lock);
return rc;
}
chip->play_time_ms = val;
mutex_unlock(&chip->param_lock);
return count;
}
static ssize_t qpnp_haptics_show_activate(struct device *dev,
struct device_attribute *attr, char *buf)
{
/* For now nothing to show */
return snprintf(buf, PAGE_SIZE, "%d\n", 0);
}
static ssize_t qpnp_haptics_store_activate(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
u32 val;
int rc;
rc = kstrtouint(buf, 0, &val);
if (rc < 0)
return rc;
if (val != 0 && val != 1)
return count;
if (chip->vmax_mv <= HAP_VMAX_MIN_MV && (val != 0))
return count;
if (val) {
hrtimer_cancel(&chip->stop_timer);
if (is_sw_lra_auto_resonance_control(chip))
hrtimer_cancel(&chip->auto_res_err_poll_timer);
cancel_work_sync(&chip->haptics_work);
atomic_set(&chip->state, 1);
schedule_work(&chip->haptics_work);
} else {
rc = qpnp_haptics_mod_enable(chip, false);
if (rc < 0) {
pr_err("Error in disabling module, rc=%d\n", rc);
return rc;
}
}
return count;
}
static ssize_t qpnp_haptics_show_play_mode(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
char *str;
if (chip->play_mode == HAP_BUFFER)
str = "buffer";
else if (chip->play_mode == HAP_DIRECT)
str = "direct";
else if (chip->play_mode == HAP_AUDIO)
str = "audio";
else if (chip->play_mode == HAP_PWM)
str = "pwm";
else
return -EINVAL;
return snprintf(buf, PAGE_SIZE, "%s\n", str);
}
static ssize_t qpnp_haptics_store_play_mode(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
char str[HAP_STR_SIZE + 1];
int rc = 0, temp, old_mode;
rc = parse_string(buf, str);
if (rc < 0)
return rc;
if (strcmp(str, "buffer") == 0)
temp = HAP_BUFFER;
else if (strcmp(str, "direct") == 0)
temp = HAP_DIRECT;
else if (strcmp(str, "audio") == 0)
temp = HAP_AUDIO;
else if (strcmp(str, "pwm") == 0)
temp = HAP_PWM;
else
return -EINVAL;
if (temp == chip->play_mode)
return count;
if (temp == HAP_BUFFER) {
rc = qpnp_haptics_parse_buffer_dt(chip);
if (!rc) {
rc = qpnp_haptics_wave_rep_config(chip,
HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT);
if (rc < 0) {
pr_err("Error in configuring wave_rep config %d\n",
rc);
return rc;
}
}
rc = qpnp_haptics_buffer_config(chip, NULL, true);
} else if (temp == HAP_PWM) {
rc = qpnp_haptics_parse_pwm_dt(chip);
if (!rc)
rc = qpnp_haptics_pwm_config(chip);
}
if (rc < 0)
return rc;
rc = qpnp_haptics_mod_enable(chip, false);
if (rc < 0)
return rc;
old_mode = chip->play_mode;
chip->play_mode = temp;
rc = qpnp_haptics_play_mode_config(chip);
if (rc < 0) {
chip->play_mode = old_mode;
return rc;
}
if (chip->play_mode == HAP_AUDIO) {
rc = qpnp_haptics_mod_enable(chip, true);
if (rc < 0) {
chip->play_mode = old_mode;
return rc;
}
}
return count;
}
static ssize_t qpnp_haptics_show_wf_samp(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
char str[HAP_STR_SIZE + 1];
char *ptr = str;
int i, len = 0;
for (i = 0; i < ARRAY_SIZE(chip->wave_samp); i++) {
len = scnprintf(ptr, HAP_STR_SIZE, "%x ", chip->wave_samp[i]);
ptr += len;
}
ptr[len] = '\0';
return snprintf(buf, PAGE_SIZE, "%s\n", str);
}
static ssize_t qpnp_haptics_store_wf_samp(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
u8 samp[HAP_WAVE_SAMP_SET_LEN] = {0};
int bytes_read, rc;
unsigned int data, pos = 0, i = 0;
while (pos < count && i < ARRAY_SIZE(samp) &&
sscanf(buf + pos, "%x%n", &data, &bytes_read) == 1) {
/* bit 0 is not used in WF_Sx */
samp[i++] = data & GENMASK(7, 1);
pos += bytes_read;
}
chip->wf_samp_len = i;
for (i = 0; i < ARRAY_SIZE(chip->wave_samp); i++)
chip->wave_samp[i] = samp[i];
rc = qpnp_haptics_buffer_config(chip, NULL, false);
if (rc < 0) {
pr_err("Error in configuring buffer mode %d\n", rc);
return rc;
}
return count;
}
static ssize_t qpnp_haptics_show_wf_rep_count(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->wave_rep_cnt);
}
static ssize_t qpnp_haptics_store_wf_rep_count(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int data, rc, old_wave_rep_cnt;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
old_wave_rep_cnt = chip->wave_rep_cnt;
chip->wave_rep_cnt = data;
rc = qpnp_haptics_wave_rep_config(chip, HAP_WAVE_REPEAT);
if (rc < 0) {
chip->wave_rep_cnt = old_wave_rep_cnt;
return rc;
}
return count;
}
static ssize_t qpnp_haptics_show_wf_s_rep_count(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->wave_s_rep_cnt);
}
static ssize_t qpnp_haptics_store_wf_s_rep_count(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int data, rc, old_wave_s_rep_cnt;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
old_wave_s_rep_cnt = chip->wave_s_rep_cnt;
chip->wave_s_rep_cnt = data;
rc = qpnp_haptics_wave_rep_config(chip, HAP_WAVE_SAMP_REPEAT);
if (rc < 0) {
chip->wave_s_rep_cnt = old_wave_s_rep_cnt;
return rc;
}
return count;
}
static ssize_t qpnp_haptics_show_vmax(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->vmax_mv);
}
static ssize_t qpnp_haptics_store_vmax(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int data, rc, old_vmax_mv;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
if (chip->vmax_override)
return count;
old_vmax_mv = chip->vmax_mv;
chip->vmax_mv = data;
rc = qpnp_haptics_vmax_config(chip, chip->vmax_mv, false);
if (rc < 0) {
chip->vmax_mv = old_vmax_mv;
return rc;
}
return count;
}
static ssize_t qpnp_haptics_show_vmax_mv_user(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->vmax_mv);
}
static ssize_t qpnp_haptics_store_vmax_mv_user(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int data, rc, old_vmax_mv;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
old_vmax_mv = chip->vmax_mv;
chip->vmax_mv = data;
rc = qpnp_haptics_vmax_config(chip, chip->vmax_mv, false);
if (rc < 0) {
chip->vmax_mv = old_vmax_mv;
return rc;
}
return count;
}
static ssize_t qpnp_haptics_show_vmax_override(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->vmax_override);
}
static ssize_t qpnp_haptics_store_vmax_override(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int data, rc;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
if (data != 0 && data != 1)
return count;
chip->vmax_override = !!data;
return count;
}
static ssize_t qpnp_haptics_show_lra_auto_mode(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
return snprintf(buf, PAGE_SIZE, "%d\n", chip->lra_auto_mode);
}
static ssize_t qpnp_haptics_store_lra_auto_mode(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct led_classdev *cdev = dev_get_drvdata(dev);
struct hap_chip *chip = container_of(cdev, struct hap_chip, cdev);
int rc, data;
rc = kstrtoint(buf, 10, &data);
if (rc < 0)
return rc;
if (data != 0 && data != 1)
return count;
chip->lra_auto_mode = !!data;
return count;
}
static struct device_attribute qpnp_haptics_attrs[] = {
__ATTR(state, 0664, qpnp_haptics_show_state, qpnp_haptics_store_state),
__ATTR(duration, 0664, qpnp_haptics_show_duration,
qpnp_haptics_store_duration),
__ATTR(activate, 0664, qpnp_haptics_show_activate,
qpnp_haptics_store_activate),
__ATTR(play_mode, 0664, qpnp_haptics_show_play_mode,
qpnp_haptics_store_play_mode),
__ATTR(wf_samp, 0664, qpnp_haptics_show_wf_samp,
qpnp_haptics_store_wf_samp),
__ATTR(wf_rep_count, 0664, qpnp_haptics_show_wf_rep_count,
qpnp_haptics_store_wf_rep_count),
__ATTR(wf_s_rep_count, 0664, qpnp_haptics_show_wf_s_rep_count,
qpnp_haptics_store_wf_s_rep_count),
__ATTR(vmax_mv, 0664, qpnp_haptics_show_vmax, qpnp_haptics_store_vmax),
__ATTR(vmax_override, 0664, qpnp_haptics_show_vmax_override, qpnp_haptics_store_vmax_override),
__ATTR(vmax_mv_user, 0664, qpnp_haptics_show_vmax_mv_user, qpnp_haptics_store_vmax_mv_user),
__ATTR(lra_auto_mode, 0664, qpnp_haptics_show_lra_auto_mode,
qpnp_haptics_store_lra_auto_mode),
};
/* Dummy functions for brightness */
static
enum led_brightness qpnp_haptics_brightness_get(struct led_classdev *cdev)
{
return 0;
}
static void qpnp_haptics_brightness_set(struct led_classdev *cdev,
enum led_brightness level)
{
}
static int qpnp_haptics_config(struct hap_chip *chip)
{
u8 rc_clk_err_deci_pct;
u16 play_rate = 0;
int rc;
/* Configure the CFG1 register for actuator type */
rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG1_REG(chip),
HAP_ACT_TYPE_MASK, chip->act_type);
if (rc < 0)
return rc;
/* Configure auto resonance parameters */
rc = qpnp_haptics_lra_auto_res_config(chip, NULL);
if (rc < 0)
return rc;
/* Configure the PLAY MODE register */
rc = qpnp_haptics_play_mode_config(chip);
if (rc < 0)
return rc;
/* Configure the VMAX register */
rc = qpnp_haptics_vmax_config(chip, chip->vmax_mv, false);
if (rc < 0)
return rc;
/* Configure the ILIM register */
rc = qpnp_haptics_ilim_config(chip);
if (rc < 0)
return rc;
/* Configure the short circuit debounce register */
rc = qpnp_haptics_sc_deb_config(chip);
if (rc < 0)
return rc;
/* Configure the WAVE SHAPE register */
rc = qpnp_haptics_masked_write_reg(chip, HAP_CFG2_REG(chip),
HAP_LRA_RES_TYPE_MASK, chip->wave_shape);
if (rc < 0)
return rc;
play_rate = chip->wave_play_rate_us / HAP_RATE_CFG_STEP_US;
/*
* The frequency of 19.2 MHz RC clock is subject to variation. Currently
* some PMI chips have MISC_TRIM_ERROR_RC19P2_CLK register present in
* MISC peripheral. This register holds the trim error of RC clock.
*/
if (chip->act_type == HAP_LRA && chip->misc_clk_trim_error_reg) {
/*
* Error is available in bits[3:0] and each LSB is 0.7%.
* Bit 7 is the sign bit for error code. If it is set, then a
* negative error correction needs to be made. Otherwise, a
* positive error correction needs to be made.
*/
rc_clk_err_deci_pct = (chip->clk_trim_error_code & 0x0F) * 7;
if (chip->clk_trim_error_code & BIT(7))
play_rate = (play_rate *
(1000 - rc_clk_err_deci_pct)) / 1000;
else
play_rate = (play_rate *
(1000 + rc_clk_err_deci_pct)) / 1000;
pr_debug("TRIM register = 0x%x, play_rate=%d\n",
chip->clk_trim_error_code, play_rate);
}
/*
* Configure RATE_CFG1 and RATE_CFG2 registers.
* Note: For ERM these registers act as play rate and
* for LRA these represent resonance period
*/
rc = qpnp_haptics_update_rate_cfg(chip, play_rate);
if (chip->act_type == HAP_LRA) {
chip->drive_period_code_max_limit = (play_rate *
(100 + chip->drive_period_code_max_var_pct)) / 100;
chip->drive_period_code_min_limit = (play_rate *
(100 - chip->drive_period_code_min_var_pct)) / 100;
pr_debug("Drive period code max limit %x min limit %x\n",
chip->drive_period_code_max_limit,
chip->drive_period_code_min_limit);
}
rc = qpnp_haptics_brake_config(chip, NULL);
if (rc < 0)
return rc;
if (chip->play_mode == HAP_BUFFER) {
rc = qpnp_haptics_wave_rep_config(chip,
HAP_WAVE_REPEAT | HAP_WAVE_SAMP_REPEAT);
if (rc < 0)
return rc;
rc = qpnp_haptics_buffer_config(chip, NULL, false);
} else if (chip->play_mode == HAP_PWM) {
rc = qpnp_haptics_pwm_config(chip);
} else if (chip->play_mode == HAP_AUDIO) {
rc = qpnp_haptics_mod_enable(chip, true);
}
if (rc < 0)
return rc;
/* setup play irq */
if (chip->play_irq >= 0) {
rc = devm_request_threaded_irq(&chip->pdev->dev, chip->play_irq,
NULL, qpnp_haptics_play_irq_handler, IRQF_ONESHOT,
"haptics_play_irq", chip);
if (rc < 0) {
pr_err("Unable to request play(%d) IRQ(err:%d)\n",
chip->play_irq, rc);
return rc;
}
/* use play_irq only for buffer mode */
if (chip->play_mode != HAP_BUFFER) {
disable_irq(chip->play_irq);
chip->play_irq_en = false;
}
}
/* setup short circuit irq */
if (chip->sc_irq >= 0) {
rc = devm_request_threaded_irq(&chip->pdev->dev, chip->sc_irq,
NULL, qpnp_haptics_sc_irq_handler, IRQF_ONESHOT,
"haptics_sc_irq", chip);
if (rc < 0) {
pr_err("Unable to request sc(%d) IRQ(err:%d)\n",
chip->sc_irq, rc);
return rc;
}
}
return rc;
}
static int qpnp_haptics_parse_buffer_dt(struct hap_chip *chip)
{
struct device_node *node = chip->pdev->dev.of_node;
u32 temp;
int rc, i, wf_samp_len;
if (chip->wave_rep_cnt > 0 || chip->wave_s_rep_cnt > 0)
return 0;
chip->wave_rep_cnt = WF_REPEAT_MIN;
rc = of_property_read_u32(node, "qcom,wave-rep-cnt", &temp);
if (!rc) {
chip->wave_rep_cnt = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read rep cnt rc=%d\n", rc);
return rc;
}
chip->wave_s_rep_cnt = WF_S_REPEAT_MIN;
rc = of_property_read_u32(node,
"qcom,wave-samp-rep-cnt", &temp);
if (!rc) {
chip->wave_s_rep_cnt = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read samp rep cnt rc=%d\n", rc);
return rc;
}
wf_samp_len = of_property_count_elems_of_size(node,
"qcom,wave-samples", sizeof(u32));
if (wf_samp_len > 0) {
if (wf_samp_len > HAP_WAVE_SAMP_SET_LEN) {
pr_err("Invalid length for wave samples\n");
return -EINVAL;
}
rc = of_property_read_u32_array(node, "qcom,wave-samples",
chip->wave_samp, wf_samp_len);
if (rc < 0) {
pr_err("Error in reading qcom,wave-samples, rc=%d\n",
rc);
return rc;
}
} else {
/* Use default values */
for (i = 0; i < HAP_WAVE_SAMP_LEN; i++)
chip->wave_samp[i] = HAP_WF_SAMP_MAX;
wf_samp_len = HAP_WAVE_SAMP_LEN;
}
chip->wf_samp_len = wf_samp_len;
return 0;
}
static int qpnp_haptics_parse_pwm_dt(struct hap_chip *chip)
{
struct device_node *node = chip->pdev->dev.of_node;
u32 temp;
int rc;
if (chip->pwm_data.period_us > 0 && chip->pwm_data.duty_us > 0)
return 0;
chip->pwm_data.pwm_dev = of_pwm_get(node, NULL);
if (IS_ERR(chip->pwm_data.pwm_dev)) {
rc = PTR_ERR(chip->pwm_data.pwm_dev);
pr_err("Cannot get PWM device rc=%d\n", rc);
chip->pwm_data.pwm_dev = NULL;
return rc;
}
rc = of_property_read_u32(node, "qcom,period-us", &temp);
if (!rc) {
chip->pwm_data.period_us = temp;
} else {
pr_err("Cannot read PWM period rc=%d\n", rc);
return rc;
}
rc = of_property_read_u32(node, "qcom,duty-us", &temp);
if (!rc) {
chip->pwm_data.duty_us = temp;
} else {
pr_err("Cannot read PWM duty rc=%d\n", rc);
return rc;
}
rc = of_property_read_u32(node, "qcom,ext-pwm-dtest-line", &temp);
if (!rc)
chip->ext_pwm_dtest_line = temp;
rc = of_property_read_u32(node, "qcom,ext-pwm-freq-khz", &temp);
if (!rc) {
chip->ext_pwm_freq_khz = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read ext pwm freq rc=%d\n", rc);
return rc;
}
return 0;
}
static int qpnp_haptics_parse_dt(struct hap_chip *chip)
{
struct device_node *node = chip->pdev->dev.of_node;
struct device_node *revid_node, *misc_node;
const char *temp_str;
int rc, temp;
struct regulator *vcc_pon;
rc = of_property_read_u32(node, "reg", &temp);
if (rc < 0) {
pr_err("Couldn't find reg in node = %s rc = %d\n",
node->full_name, rc);
return rc;
}
if (temp <= 0) {
pr_err("Invalid base address %x\n", temp);
return -EINVAL;
}
chip->base = (u16)temp;
revid_node = of_parse_phandle(node, "qcom,pmic-revid", 0);
if (!revid_node) {
pr_err("Missing qcom,pmic-revid property\n");
return -EINVAL;
}
chip->revid = get_revid_data(revid_node);
of_node_put(revid_node);
if (IS_ERR_OR_NULL(chip->revid)) {
pr_err("Unable to get pmic_revid rc=%ld\n",
PTR_ERR(chip->revid));
/*
* the revid peripheral must be registered, any failure
* here only indicates that the rev-id module has not
* probed yet.
*/
return -EPROBE_DEFER;
}
if (of_find_property(node, "qcom,pmic-misc", NULL)) {
misc_node = of_parse_phandle(node, "qcom,pmic-misc", 0);
if (!misc_node)
return -EINVAL;
rc = of_property_read_u32(node, "qcom,misc-clk-trim-error-reg",
&chip->misc_clk_trim_error_reg);
if (rc < 0 || !chip->misc_clk_trim_error_reg) {
pr_err("Invalid or missing misc-clk-trim-error-reg\n");
of_node_put(misc_node);
return rc;
}
rc = qpnp_misc_read_reg(misc_node,
chip->misc_clk_trim_error_reg,
&chip->clk_trim_error_code);
if (rc < 0) {
pr_err("Couldn't get clk_trim_error_code, rc=%d\n", rc);
of_node_put(misc_node);
return -EPROBE_DEFER;
}
of_node_put(misc_node);
}
chip->play_irq = platform_get_irq_byname(chip->pdev, "hap-play-irq");
if (chip->play_irq < 0) {
pr_err("Unable to get play irq\n");
return chip->play_irq;
}
chip->sc_irq = platform_get_irq_byname(chip->pdev, "hap-sc-irq");
if (chip->sc_irq < 0) {
pr_err("Unable to get sc irq\n");
return chip->sc_irq;
}
chip->act_type = HAP_LRA;
rc = of_property_read_u32(node, "qcom,actuator-type", &temp);
if (!rc) {
if (temp != HAP_LRA && temp != HAP_ERM) {
pr_err("Incorrect actuator type\n");
return -EINVAL;
}
chip->act_type = temp;
}
chip->lra_auto_mode = of_property_read_bool(node, "qcom,lra-auto-mode");
rc = of_property_read_string(node, "qcom,play-mode", &temp_str);
if (!rc) {
if (strcmp(temp_str, "direct") == 0)
chip->play_mode = HAP_DIRECT;
else if (strcmp(temp_str, "buffer") == 0)
chip->play_mode = HAP_BUFFER;
else if (strcmp(temp_str, "pwm") == 0)
chip->play_mode = HAP_PWM;
else if (strcmp(temp_str, "audio") == 0)
chip->play_mode = HAP_AUDIO;
else {
pr_err("Invalid play mode\n");
return -EINVAL;
}
} else {
if (rc == -EINVAL && chip->act_type == HAP_LRA) {
pr_info("Play mode not specified, using auto mode\n");
chip->lra_auto_mode = true;
} else {
pr_err("Unable to read play mode\n");
return rc;
}
}
chip->max_play_time_ms = HAP_MAX_PLAY_TIME_MS;
rc = of_property_read_u32(node, "qcom,max-play-time-ms", &temp);
if (!rc) {
chip->max_play_time_ms = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read max-play-time rc=%d\n", rc);
return rc;
}
chip->vmax_mv = HAP_VMAX_MAX_MV;
rc = of_property_read_u32(node, "qcom,vmax-mv", &temp);
if (!rc) {
chip->vmax_mv = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read Vmax rc=%d\n", rc);
return rc;
}
chip->ilim_ma = HAP_ILIM_400_MA;
rc = of_property_read_u32(node, "qcom,ilim-ma", &temp);
if (!rc) {
chip->ilim_ma = (u8)temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read ILIM rc=%d\n", rc);
return rc;
}
chip->sc_deb_cycles = HAP_DEF_SC_DEB_CYCLES;
rc = of_property_read_u32(node, "qcom,sc-dbc-cycles", &temp);
if (!rc) {
chip->sc_deb_cycles = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read sc debounce rc=%d\n", rc);
return rc;
}
chip->wave_shape = HAP_WAVE_SQUARE;
rc = of_property_read_string(node, "qcom,wave-shape", &temp_str);
if (!rc) {
if (strcmp(temp_str, "sine") == 0)
chip->wave_shape = HAP_WAVE_SINE;
else if (strcmp(temp_str, "square") == 0)
chip->wave_shape = HAP_WAVE_SQUARE;
else {
pr_err("Unsupported wave shape\n");
return -EINVAL;
}
} else if (rc != -EINVAL) {
pr_err("Unable to read wave shape rc=%d\n", rc);
return rc;
}
chip->wave_play_rate_us = HAP_DEF_WAVE_PLAY_RATE_US;
rc = of_property_read_u32(node,
"qcom,wave-play-rate-us", &temp);
if (!rc) {
chip->wave_play_rate_us = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read play rate rc=%d\n", rc);
return rc;
}
if (chip->wave_play_rate_us < HAP_WAVE_PLAY_RATE_US_MIN)
chip->wave_play_rate_us = HAP_WAVE_PLAY_RATE_US_MIN;
else if (chip->wave_play_rate_us > HAP_WAVE_PLAY_RATE_US_MAX)
chip->wave_play_rate_us = HAP_WAVE_PLAY_RATE_US_MAX;
chip->en_brake = of_property_read_bool(node, "qcom,en-brake");
rc = of_property_count_elems_of_size(node,
"qcom,brake-pattern", sizeof(u32));
if (rc > 0) {
if (rc != HAP_BRAKE_PAT_LEN) {
pr_err("Invalid length for brake pattern\n");
return -EINVAL;
}
rc = of_property_read_u32_array(node, "qcom,brake-pattern",
chip->brake_pat, HAP_BRAKE_PAT_LEN);
if (rc < 0) {
pr_err("Error in reading qcom,brake-pattern, rc=%d\n",
rc);
return rc;
}
}
/* Read the following properties only for LRA */
if (chip->act_type == HAP_LRA) {
rc = of_property_read_string(node, "qcom,lra-auto-res-mode",
&temp_str);
if (!rc) {
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
chip->ares_cfg.auto_res_mode =
HAP_PM660_AUTO_RES_QWD;
if (strcmp(temp_str, "zxd") == 0)
chip->ares_cfg.auto_res_mode =
HAP_PM660_AUTO_RES_ZXD;
else if (strcmp(temp_str, "qwd") == 0)
chip->ares_cfg.auto_res_mode =
HAP_PM660_AUTO_RES_QWD;
} else {
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_ZXD_EOP;
if (strcmp(temp_str, "none") == 0)
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_NONE;
else if (strcmp(temp_str, "zxd") == 0)
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_ZXD;
else if (strcmp(temp_str, "qwd") == 0)
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_QWD;
else if (strcmp(temp_str, "max-qwd") == 0)
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_MAX_QWD;
else
chip->ares_cfg.auto_res_mode =
HAP_AUTO_RES_ZXD_EOP;
}
} else if (rc != -EINVAL) {
pr_err("Unable to read auto res mode rc=%d\n", rc);
return rc;
}
chip->ares_cfg.lra_high_z = HAP_LRA_HIGH_Z_OPT3;
rc = of_property_read_string(node, "qcom,lra-high-z",
&temp_str);
if (!rc) {
if (strcmp(temp_str, "none") == 0)
chip->ares_cfg.lra_high_z =
HAP_LRA_HIGH_Z_NONE;
else if (strcmp(temp_str, "opt1") == 0)
chip->ares_cfg.lra_high_z =
HAP_LRA_HIGH_Z_OPT1;
else if (strcmp(temp_str, "opt2") == 0)
chip->ares_cfg.lra_high_z =
HAP_LRA_HIGH_Z_OPT2;
else
chip->ares_cfg.lra_high_z =
HAP_LRA_HIGH_Z_OPT3;
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
if (strcmp(temp_str, "opt0") == 0)
chip->ares_cfg.lra_high_z =
HAP_LRA_HIGH_Z_NONE;
}
} else if (rc != -EINVAL) {
pr_err("Unable to read LRA high-z rc=%d\n", rc);
return rc;
}
chip->ares_cfg.lra_res_cal_period = HAP_RES_CAL_PERIOD_MAX;
rc = of_property_read_u32(node,
"qcom,lra-res-cal-period", &temp);
if (!rc) {
chip->ares_cfg.lra_res_cal_period = temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read cal period rc=%d\n", rc);
return rc;
}
chip->ares_cfg.lra_qwd_drive_duration = -EINVAL;
chip->ares_cfg.calibrate_at_eop = -EINVAL;
if (chip->revid->pmic_subtype == PM660_SUBTYPE) {
rc = of_property_read_u32(node,
"qcom,lra-qwd-drive-duration",
&chip->ares_cfg.lra_qwd_drive_duration);
if (rc && rc != -EINVAL) {
pr_err("Unable to read LRA QWD drive duration rc=%d\n",
rc);
return rc;
}
rc = of_property_read_u32(node,
"qcom,lra-calibrate-at-eop",
&chip->ares_cfg.calibrate_at_eop);
if (rc && rc != -EINVAL) {
pr_err("Unable to read Calibrate at EOP rc=%d\n",
rc);
return rc;
}
}
chip->drive_period_code_max_var_pct = 25;
rc = of_property_read_u32(node,
"qcom,drive-period-code-max-variation-pct", &temp);
if (!rc) {
if (temp > 0 && temp < 100)
chip->drive_period_code_max_var_pct = (u8)temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read drive period code max var pct rc=%d\n",
rc);
return rc;
}
chip->drive_period_code_min_var_pct = 25;
rc = of_property_read_u32(node,
"qcom,drive-period-code-min-variation-pct", &temp);
if (!rc) {
if (temp > 0 && temp < 100)
chip->drive_period_code_min_var_pct = (u8)temp;
} else if (rc != -EINVAL) {
pr_err("Unable to read drive period code min var pct rc=%d\n",
rc);
return rc;
}
chip->auto_res_err_recovery_hw =
of_property_read_bool(node,
"qcom,auto-res-err-recovery-hw");
if (chip->revid->pmic_subtype != PM660_SUBTYPE)
chip->auto_res_err_recovery_hw = false;
}
if (rc == -EINVAL)
rc = 0;
if (chip->play_mode == HAP_BUFFER)
rc = qpnp_haptics_parse_buffer_dt(chip);
else if (chip->play_mode == HAP_PWM)
rc = qpnp_haptics_parse_pwm_dt(chip);
if (of_find_property(node, "vcc_pon-supply", NULL)) {
vcc_pon = regulator_get(&chip->pdev->dev, "vcc_pon");
if (IS_ERR(vcc_pon)) {
rc = PTR_ERR(vcc_pon);
dev_err(&chip->pdev->dev,
"regulator get failed vcc_pon rc=%d\n", rc);
}
chip->vcc_pon = vcc_pon;
}
return rc;
}
static int qpnp_haptics_probe(struct platform_device *pdev)
{
struct hap_chip *chip;
int rc, i;
chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
chip->regmap = dev_get_regmap(pdev->dev.parent, NULL);
if (!chip->regmap) {
dev_err(&pdev->dev, "Couldn't get parent's regmap\n");
return -EINVAL;
}
chip->pdev = pdev;
rc = qpnp_haptics_parse_dt(chip);
if (rc < 0) {
dev_err(&pdev->dev, "Error in parsing DT parameters, rc=%d\n",
rc);
return rc;
}
spin_lock_init(&chip->bus_lock);
mutex_init(&chip->play_lock);
mutex_init(&chip->param_lock);
INIT_WORK(&chip->haptics_work, qpnp_haptics_work);
rc = qpnp_haptics_config(chip);
if (rc < 0) {
dev_err(&pdev->dev, "Error in configuring haptics, rc=%d\n",
rc);
goto fail;
}
hrtimer_init(&chip->stop_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
chip->stop_timer.function = hap_stop_timer;
hrtimer_init(&chip->auto_res_err_poll_timer, CLOCK_MONOTONIC,
HRTIMER_MODE_REL);
chip->auto_res_err_poll_timer.function = hap_auto_res_err_poll_timer;
dev_set_drvdata(&pdev->dev, chip);
chip->cdev.name = "vibrator";
chip->cdev.brightness_get = qpnp_haptics_brightness_get;
chip->cdev.brightness_set = qpnp_haptics_brightness_set;
chip->cdev.max_brightness = 100;
rc = devm_led_classdev_register(&pdev->dev, &chip->cdev);
if (rc < 0) {
dev_err(&pdev->dev, "Error in registering led class device, rc=%d\n",
rc);
goto register_fail;
}
for (i = 0; i < ARRAY_SIZE(qpnp_haptics_attrs); i++) {
rc = sysfs_create_file(&chip->cdev.dev->kobj,
&qpnp_haptics_attrs[i].attr);
if (rc < 0) {
dev_err(&pdev->dev, "Error in creating sysfs file, rc=%d\n",
rc);
goto sysfs_fail;
}
}
gchip = chip;
return 0;
sysfs_fail:
for (--i; i >= 0; i--)
sysfs_remove_file(&chip->cdev.dev->kobj,
&qpnp_haptics_attrs[i].attr);
register_fail:
cancel_work_sync(&chip->haptics_work);
hrtimer_cancel(&chip->auto_res_err_poll_timer);
hrtimer_cancel(&chip->stop_timer);
fail:
mutex_destroy(&chip->play_lock);
mutex_destroy(&chip->param_lock);
if (chip->pwm_data.pwm_dev)
pwm_put(chip->pwm_data.pwm_dev);
dev_set_drvdata(&pdev->dev, NULL);
return rc;
}
static int qpnp_haptics_remove(struct platform_device *pdev)
{
struct hap_chip *chip = dev_get_drvdata(&pdev->dev);
cancel_work_sync(&chip->haptics_work);
hrtimer_cancel(&chip->auto_res_err_poll_timer);
hrtimer_cancel(&chip->stop_timer);
mutex_destroy(&chip->play_lock);
mutex_destroy(&chip->param_lock);
if (chip->pwm_data.pwm_dev)
pwm_put(chip->pwm_data.pwm_dev);
dev_set_drvdata(&pdev->dev, NULL);
return 0;
}
static void qpnp_haptics_shutdown(struct platform_device *pdev)
{
struct hap_chip *chip = dev_get_drvdata(&pdev->dev);
cancel_work_sync(&chip->haptics_work);
/* disable haptics */
qpnp_haptics_mod_enable(chip, false);
}
static const struct dev_pm_ops qpnp_haptics_pm_ops = {
.suspend = qpnp_haptics_suspend,
};
static const struct of_device_id hap_match_table[] = {
{ .compatible = "qcom,qpnp-haptics" },
{ },
};
static struct platform_driver qpnp_haptics_driver = {
.driver = {
.name = "qcom,qpnp-haptics",
.of_match_table = hap_match_table,
.pm = &qpnp_haptics_pm_ops,
},
.probe = qpnp_haptics_probe,
.remove = qpnp_haptics_remove,
.shutdown = qpnp_haptics_shutdown,
};
module_platform_driver(qpnp_haptics_driver);
MODULE_DESCRIPTION("QPNP haptics driver");
MODULE_LICENSE("GPL v2");
|