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
|
/* Copyright (c) 2014-2015, 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.
*/
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/jiffies.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <linux/miscdevice.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/sysctl.h>
#include <linux/regulator/consumer.h>
#include <linux/input.h>
#include <linux/regmap.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/interrupt.h>
#include <linux/sensors.h>
#include <linux/pm_wakeup.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>
#define LTR553_I2C_NAME "ltr553"
#define LTR553_LIGHT_INPUT_NAME "ltr553-light"
#define LTR553_PROXIMITY_INPUT_NAME "ltr553-proximity"
#define LTR553_REG_ALS_CTL 0x80
#define LTR553_REG_PS_CTL 0x81
#define LTR553_REG_PS_LED 0x82
#define LTR553_REG_PS_N_PULSES 0x83
#define LTR553_REG_PS_MEAS_RATE 0x84
#define LTR553_REG_ALS_MEAS_RATE 0x85
#define LTR553_REG_PART_ID 0x86
#define LTR553_REG_ALS_DATA_CH1_0 0x88
#define LTR553_REG_ALS_DATA_CH1_1 0x89
#define LTR553_REG_ALS_DATA_CH0_0 0x8A
#define LTR553_REG_ALS_DATA_CH0_1 0x8B
#define LTR553_REG_ALS_PS_STATUS 0x8C
#define LTR553_REG_PS_DATA_0 0x8D
#define LTR553_REG_INTERRUPT 0x8F
#define LTR553_REG_PS_THRES_UP_0 0x90
#define LTR553_REG_PS_OFFSET_1 0x94
#define LTR553_REG_PS_OFFSET_0 0x95
#define LTR553_REG_ALS_THRES_UP_0 0x97
#define LTR553_REG_INTERRUPT_PERSIST 0x9E
#define LTR553_REG_MAGIC 0xFF
#define LTR553_PART_ID 0x92
#define LTR553_ALS_SENSITIVITY 70
#define LTR553_BOOT_TIME_MS 120
#define LTR553_WAKE_TIME_MS 10
#define LTR553_PS_SATURATE_MASK 0x8000
#define LTR553_ALS_INT_MASK 0x08
#define LTR553_PS_INT_MASK 0x02
#define LTR553_ALS_MEASURE_MASK 0x38
#define LTR553_ALS_GAIN_MASK 0x1c
/* default measurement rate is 100 ms */
#define LTR553_ALS_DEFAULT_MEASURE_RATE 0x01
#define LTR553_PS_MEASUREMENT_RATE_10MS 0x08
#define LTR553_CALIBRATE_SAMPLES 15
#define ALS_GAIN_SWITCH_THRESHOLD 60000
#define LTR553_ALS_INVALID(value) (value & 0x80)
/* LTR553 ALS data is 16 bit */
#define ALS_DATA_MASK 0xffff
#define ALS_LOW_BYTE(data) ((data) & 0xff)
#define ALS_HIGH_BYTE(data) (((data) >> 8) & 0xff)
/* LTR553 PS data is 11 bit */
#define PS_DATA_MASK 0x7ff
#define PS_LOW_BYTE(data) ((data) & 0xff)
#define PS_HIGH_BYTE(data) (((data) >> 8) & 0x7)
/* Calculated by 10% transmittance */
#define LTR553_MAX_LUX (ALS_DATA_MASK * 10)
/* both als and ps interrupt are enabled */
#define LTR553_INTERRUPT_SETTING 0x03
/* Any proximity distance change will wakeup SoC */
#define LTR553_WAKEUP_ANY_CHANGE 0xff
#define CAL_BUF_LEN 16
enum {
CMD_WRITE = 0,
CMD_READ = 1,
};
struct regulator_map {
struct regulator *regulator;
int min_uv;
int max_uv;
char *supply;
};
struct pinctrl_config {
struct pinctrl *pinctrl;
struct pinctrl_state *state[2];
char *name[2];
};
struct ltr553_data {
struct i2c_client *i2c;
struct regmap *regmap;
struct regulator *config;
struct input_dev *input_light;
struct input_dev *input_proximity;
struct workqueue_struct *workqueue;
struct sensors_classdev als_cdev;
struct sensors_classdev ps_cdev;
struct mutex ops_lock;
ktime_t last_als_ts;
ktime_t last_ps_ts;
struct work_struct report_work;
struct work_struct als_enable_work;
struct work_struct als_disable_work;
struct work_struct ps_enable_work;
struct work_struct ps_disable_work;
atomic_t wake_count;
int irq_gpio;
int irq;
bool als_enabled;
bool ps_enabled;
u32 irq_flags;
int als_delay;
int ps_delay;
int als_cal;
int ps_cal;
int als_gain;
int als_persist;
int als_integration_time;
int als_measure_rate;
int ps_led;
int ps_pulses;
int ps_measure_rate;
int als_ps_persist;
int ps_wakeup_threshold;
int last_als;
int last_ps;
int flush_count;
int power_enabled;
unsigned int reg_addr;
char calibrate_buf[CAL_BUF_LEN];
unsigned int bias;
};
struct als_coeff {
int ch0_coeff_i;
int ch1_coeff_i;
int ch0_coeff_f;
int ch1_coeff_f;
int win_fac;
int sign;
} __attribute__((__packed__));
static struct regulator_map power_config[] = {
{.supply = "vdd", .min_uv = 2000000, .max_uv = 3300000, },
{.supply = "vio", .min_uv = 1750000, .max_uv = 1950000, },
};
static struct pinctrl_config pin_config = {
.name = { "default", "sleep" },
};
static struct als_coeff eqtn_map[] = {
{
.ch0_coeff_i = 1,
.ch1_coeff_i = 1,
.ch0_coeff_f = 7743,
.ch1_coeff_f = 1059,
.win_fac = 44,
.sign = 1,
},
{
.ch0_coeff_i = 4,
.ch1_coeff_i = 1,
.ch0_coeff_f = 2785,
.ch1_coeff_f = 9548,
.win_fac = 50,
.sign = -1,
},
{
.ch0_coeff_i = 0,
.ch1_coeff_i = 0,
.ch0_coeff_f = 5926,
.ch1_coeff_f = 1185,
.win_fac = 40,
.sign = 1,
},
{
.ch0_coeff_i = 0,
.ch1_coeff_i = 0,
.ch0_coeff_f = 0,
.ch1_coeff_f = 0,
.win_fac = 1,
.sign = 1,
},
};
/* ALS integration time in 10ms */
static int als_int_fac_table[] = { 10, 5, 20, 40, 15, 25, 30, 35 };
/* ALS gain table, index 4 & 5 are reserved */
static int als_gain_table[] = {1, 2, 4, 8, 1, 1, 48, 96};
/* ALS measurement repeat rate in ms */
static int als_mrr_table[] = {50, 100, 200, 500, 1000, 2000, 2000, 2000};
/* PS measurement repeat rate in ms */
static int ps_mrr_table[] = { 50, 70, 100, 200, 500, 1000, 2000, 10,
10, 10, 10, 10, 10, 10, 10, 10};
/* Tuned for devices with rubber */
static int ps_distance_table[] = { 790, 337, 195, 114, 78, 62, 50 };
static int sensitivity_table[] = {150, 150, 100, 100, 0, 0, 100, 1};
static struct sensors_classdev als_cdev = {
.name = "ltr553-light",
.vendor = "Lite-On Technology Corp",
.version = 1,
.handle = SENSORS_LIGHT_HANDLE,
.type = SENSOR_TYPE_LIGHT,
.max_range = "65536",
.resolution = "1.0",
.sensor_power = "0.25",
.min_delay = 50000,
.max_delay = 2000,
.fifo_reserved_event_count = 0,
.fifo_max_event_count = 0,
.flags = 2,
.enabled = 0,
.delay_msec = 50,
.sensors_enable = NULL,
.sensors_poll_delay = NULL,
};
static struct sensors_classdev ps_cdev = {
.name = "ltr553-proximity",
.vendor = "Lite-On Technology Corp",
.version = 1,
.handle = SENSORS_PROXIMITY_HANDLE,
.type = SENSOR_TYPE_PROXIMITY,
.max_range = "7",
.resolution = "1.0",
.sensor_power = "0.25",
.min_delay = 10000,
.max_delay = 2000,
.fifo_reserved_event_count = 0,
.fifo_max_event_count = 0,
.flags = 3,
.enabled = 0,
.delay_msec = 50,
.sensors_enable = NULL,
.sensors_poll_delay = NULL,
};
static int sensor_power_init(struct device *dev, struct regulator_map *map,
int size)
{
int rc;
int i;
for (i = 0; i < size; i++) {
map[i].regulator = devm_regulator_get(dev, map[i].supply);
if (IS_ERR(map[i].regulator)) {
rc = PTR_ERR(map[i].regulator);
dev_err(dev, "Regualtor get failed vdd rc=%d\n", rc);
goto exit;
}
if (regulator_count_voltages(map[i].regulator) > 0) {
rc = regulator_set_voltage(map[i].regulator,
map[i].min_uv, map[i].max_uv);
if (rc) {
dev_err(dev, "Regulator set failed vdd rc=%d\n",
rc);
goto exit;
}
}
}
return 0;
exit:
/* Regulator not set correctly */
for (i = i - 1; i >= 0; i--) {
if (regulator_count_voltages(map[i].regulator))
regulator_set_voltage(map[i].regulator, 0,
map[i].max_uv);
}
return rc;
}
static int sensor_power_deinit(struct device *dev, struct regulator_map *map,
int size)
{
int i;
for (i = 0; i < size; i++) {
if (!IS_ERR_OR_NULL(map[i].regulator)) {
if (regulator_count_voltages(map[i].regulator) > 0)
regulator_set_voltage(map[i].regulator, 0,
map[i].max_uv);
}
}
return 0;
}
static int sensor_power_config(struct device *dev, struct regulator_map *map,
int size, bool enable)
{
int i;
int rc = 0;
if (enable) {
for (i = 0; i < size; i++) {
rc = regulator_enable(map[i].regulator);
if (rc) {
dev_err(dev, "enable %s failed.\n",
map[i].supply);
goto exit_enable;
}
}
} else {
for (i = 0; i < size; i++) {
rc = regulator_disable(map[i].regulator);
if (rc) {
dev_err(dev, "disable %s failed.\n",
map[i].supply);
goto exit_disable;
}
}
}
return 0;
exit_enable:
for (i = i - 1; i >= 0; i--)
regulator_disable(map[i].regulator);
return rc;
exit_disable:
for (i = i - 1; i >= 0; i--)
if (regulator_enable(map[i].regulator))
dev_err(dev, "enable %s failed\n", map[i].supply);
return rc;
}
static int sensor_pinctrl_init(struct device *dev,
struct pinctrl_config *config)
{
config->pinctrl = devm_pinctrl_get(dev);
if (IS_ERR_OR_NULL(config->pinctrl)) {
dev_err(dev, "Failed to get pinctrl\n");
return PTR_ERR(config->pinctrl);
}
config->state[0] =
pinctrl_lookup_state(config->pinctrl, config->name[0]);
if (IS_ERR_OR_NULL(config->state[0])) {
dev_err(dev, "Failed to look up %s\n", config->name[0]);
return PTR_ERR(config->state[0]);
}
config->state[1] =
pinctrl_lookup_state(config->pinctrl, config->name[1]);
if (IS_ERR_OR_NULL(config->state[1])) {
dev_err(dev, "Failed to look up %s\n", config->name[1]);
return PTR_ERR(config->state[1]);
}
return 0;
}
static int ltr553_parse_dt(struct device *dev, struct ltr553_data *ltr)
{
struct device_node *dp = dev->of_node;
u32 value;
int rc;
int i;
rc = of_get_named_gpio_flags(dp, "liteon,irq-gpio", 0,
<r->irq_flags);
if (rc < 0) {
dev_err(dev, "unable to read irq gpio\n");
return rc;
}
ltr->irq_gpio = rc;
/* als ps persist */
rc = of_property_read_u32(dp, "liteon,als-ps-persist", &value);
if (rc) {
dev_err(dev, "read liteon,als-ps-persist failed\n");
return rc;
}
ltr->als_ps_persist = value;
/* ps led */
rc = of_property_read_u32(dp, "liteon,ps-led", &value);
if (rc) {
dev_err(dev, "read liteon,ps-led failed\n");
return rc;
}
ltr->ps_led = value;
/* ps pulses */
rc = of_property_read_u32(dp, "liteon,ps-pulses", &value);
if (rc) {
dev_err(dev, "read liteon,ps-pulses failed\n");
return rc;
}
if (value > 0x7) {
dev_err(dev, "liteon,ps-pulses out of range\n");
return -EINVAL;
}
ltr->ps_pulses = value;
/* als integration time */
rc = of_property_read_u32(dp, "liteon,als-integration-time", &value);
if (rc) {
dev_err(dev, "read liteon,als-integration-time failed\n");
return rc;
}
if (value > 0x7) {
dev_err(dev, "liteon,als-integration-time out of range\n");
return -EINVAL;
}
ltr->als_integration_time = value;
/* ps wakeup threshold */
rc = of_property_read_u32(dp, "liteon,wakeup-threshold", &value);
if (rc) {
dev_err(dev, "liteon,wakeup-threshold incorrect, drop to default\n");
value = LTR553_WAKEUP_ANY_CHANGE;
}
if ((value >= ARRAY_SIZE(ps_distance_table)) &&
(value != LTR553_WAKEUP_ANY_CHANGE)) {
dev_err(dev, "wakeup threshold too big\n");
return -EINVAL;
}
ltr->ps_wakeup_threshold = value;
/* ps distance table */
rc = of_property_read_u32_array(dp, "liteon,ps-distance-table",
ps_distance_table, ARRAY_SIZE(ps_distance_table));
if ((rc == -ENODATA) || (rc == -EOVERFLOW)) {
dev_warn(dev, "liteon,ps-distance-table not correctly set\n");
return rc;
}
for (i = 1; i < ARRAY_SIZE(ps_distance_table); i++) {
if (ps_distance_table[i - 1] < ps_distance_table[i]) {
dev_err(dev, "ps distance table should in descend order\n");
return -EINVAL;
}
}
if (ps_distance_table[0] > PS_DATA_MASK) {
dev_err(dev, "distance table out of range\n");
return -EINVAL;
}
/* als gain */
rc = of_property_read_u32(dp, "liteon,als-gain", &value);
if (rc) {
dev_err(dev, "read liteon,als-gain failed. Drop to default\n");
value = 0;
}
/* 4 & 5 are reserved */
if ((value > 0x7) || (value == 0x4) || (value == 0x5)) {
dev_err(dev, "liteon,als-gain invalid\n");
return -EINVAL;
}
ltr->als_gain = value;
/* als sensitivity */
rc = of_property_read_u32_array(dp, "liteon,als-sensitivity",
sensitivity_table, ARRAY_SIZE(sensitivity_table));
if (rc)
dev_info(dev, "read liteon,als-sensitivity failed. Drop to default\n");
/* als equation map */
rc = of_property_read_u32_array(dp, "liteon,als-equation-0",
&eqtn_map[0].ch0_coeff_i, 6);
if (rc)
dev_warn(dev, "read liteon,als-equation-0 failed. Drop to default\n");
rc = of_property_read_u32_array(dp, "liteon,als-equation-0",
&eqtn_map[1].ch0_coeff_i, 6);
if (rc)
dev_warn(dev, "read liteon,als-equation-1 failed. Drop to default\n");
rc = of_property_read_u32_array(dp, "liteon,als-equation-0",
&eqtn_map[2].ch0_coeff_i, 6);
if (rc)
dev_warn(dev, "read liteon,als-equation-2 failed. Drop to default\n");
rc = of_property_read_u32_array(dp, "liteon,als-equation-3",
&eqtn_map[3].ch0_coeff_i, 6);
if (rc)
dev_warn(dev, "read liteon,als-equation-3 failed. Drop to default\n");
return 0;
}
static int ltr553_check_device(struct ltr553_data *ltr)
{
unsigned int part_id;
int rc;
rc = regmap_read(ltr->regmap, LTR553_REG_PART_ID, &part_id);
if (rc) {
dev_err(<r->i2c->dev, "read reg %d failed.(%d)\n",
LTR553_REG_PART_ID, rc);
return rc;
}
if (part_id != LTR553_PART_ID)
return -ENODEV;
return 0;
}
static int ltr553_init_input(struct ltr553_data *ltr)
{
struct input_dev *input;
int status;
input = devm_input_allocate_device(<r->i2c->dev);
if (!input) {
dev_err(<r->i2c->dev, "allocate light input device failed\n");
return -ENOMEM;
}
input->name = LTR553_LIGHT_INPUT_NAME;
input->phys = "ltr553/input0";
input->id.bustype = BUS_I2C;
input_set_capability(input, EV_ABS, ABS_MISC);
input_set_abs_params(input, ABS_MISC, 0, LTR553_MAX_LUX, 0, 0);
status = input_register_device(input);
if (status) {
dev_err(<r->i2c->dev, "register light input device failed.\n");
return status;
}
ltr->input_light = input;
input = devm_input_allocate_device(<r->i2c->dev);
if (!input) {
dev_err(<r->i2c->dev, "allocate proximity input device failed\n");
return -ENOMEM;
}
input->name = LTR553_PROXIMITY_INPUT_NAME;
input->phys = "ltr553/input1";
input->id.bustype = BUS_I2C;
input_set_capability(input, EV_ABS, ABS_DISTANCE);
input_set_abs_params(input, ABS_DISTANCE, 0,
ARRAY_SIZE(ps_distance_table), 0, 0);
status = input_register_device(input);
if (status) {
dev_err(<r->i2c->dev, "register proxmity input device failed.\n");
return status;
}
ltr->input_proximity = input;
return 0;
}
static int ltr553_init_device(struct ltr553_data *ltr)
{
int rc;
unsigned int tmp;
/* Enable als/ps interrupt */
rc = regmap_write(ltr->regmap, LTR553_REG_INTERRUPT,
LTR553_INTERRUPT_SETTING);
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_INTERRUPT);
return rc;
}
rc = regmap_write(ltr->regmap, LTR553_REG_INTERRUPT_PERSIST,
ltr->als_ps_persist);
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_INTERRUPT_PERSIST);
return rc;
}
rc = regmap_write(ltr->regmap, LTR553_REG_PS_N_PULSES, ltr->ps_pulses);
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_PS_N_PULSES);
return rc;
}
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_MEAS_RATE,
(ltr->als_integration_time << 3) | (ltr->als_measure_rate));
if (rc) {
dev_err(<r->i2c->dev, "write %d failed\n",
LTR553_REG_ALS_MEAS_RATE);
return rc;
}
rc = regmap_write(ltr->regmap, LTR553_REG_PS_MEAS_RATE,
ltr->ps_measure_rate);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed\n",
LTR553_REG_PS_MEAS_RATE);
return rc;
}
/* Set calibration parameter low byte */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_OFFSET_0,
PS_LOW_BYTE(ltr->bias));
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_PS_OFFSET_0);
return rc;
}
/* Set calibration parameter high byte */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_OFFSET_1,
PS_HIGH_BYTE(ltr->bias));
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_PS_OFFSET_1);
return rc;
}
/* set up als gain */
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL, &tmp);
if (rc) {
dev_err(<r->i2c->dev, "read %d register failed\n",
LTR553_REG_ALS_CTL);
return rc;
}
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
(tmp & (~0x1c)) | (ltr->als_gain << 2));
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_ALS_CTL);
return rc;
}
return 0;
}
/* Calculate the lux value based on ADC data */
static int ltr553_calc_lux(int ch0data, int ch1data, int gain, int als_int_fac)
{
int ratio;
int lux_i;
int lux_f;
int lux;
struct als_coeff *eqtn;
/* avoid divided by 0 */
if ((ch0data == 0) && (ch1data == 0))
return 0;
ratio = ch1data * 100 / (ch0data + ch1data);
if (ratio < 45)
eqtn = &eqtn_map[0];
else if ((ratio >= 45) && (ratio < 68))
eqtn = &eqtn_map[1];
else if ((ratio >= 68) && (ratio < 99))
eqtn = &eqtn_map[2];
else
eqtn = &eqtn_map[3];
lux_i = (ch0data * eqtn->ch0_coeff_i + ch1data * eqtn->ch1_coeff_i *
eqtn->sign) * eqtn->win_fac;
lux_f = (ch0data * eqtn->ch0_coeff_f + ch1data * eqtn->ch1_coeff_f *
eqtn->sign) / 100 * eqtn->win_fac;
lux = (lux_i + abs(lux_f) / 100) / (gain * als_int_fac);
return lux;
}
/* Calculate adc value based on lux. Return value is positive */
static int ltr553_calc_adc(int ratio, int lux, int gain, int als_int_fac)
{
int divisor_i;
int divisor_f;
int dividend;
struct als_coeff *eqtn;
int result;
/* avoid devided by 0 */
if (ratio == 0)
return 0;
if (ratio < 45)
eqtn = &eqtn_map[0];
else if ((ratio >= 45) && (ratio < 68))
eqtn = &eqtn_map[1];
else if ((ratio >= 68) && (ratio < 99))
eqtn = &eqtn_map[2];
else
eqtn = &eqtn_map[3];
dividend = lux * gain * als_int_fac;
divisor_i = ((100 - ratio) * eqtn->ch0_coeff_i / ratio +
eqtn->ch1_coeff_i * eqtn->sign) * eqtn->win_fac;
divisor_f = abs((100 - ratio) * eqtn->ch0_coeff_f / ratio +
eqtn->ch1_coeff_f * eqtn->sign) * eqtn->win_fac / 10000;
/* avoid divided by 0 */
if ((divisor_i + divisor_f) == 0)
return 0;
result = dividend / (divisor_i + divisor_f);
return result <= 0 ? 1 : result;
}
/* update als gain and threshold */
static int ltr553_als_update_setting(struct ltr553_data *ltr,
int ch0data, int ch1data, int als_int_fac)
{
int gain_index;
unsigned int config;
unsigned int ratio;
unsigned int adc_base;
int rc;
int adc;
int i;
u8 als_data[4];
for (i = ARRAY_SIZE(als_gain_table) - 1; i >= 0; i--) {
if ((i == 4) || (i == 5))
continue;
if ((ch0data + ch1data) * als_gain_table[i] /
als_gain_table[ltr->als_gain] <
ALS_GAIN_SWITCH_THRESHOLD)
break;
}
gain_index = i < 0 ? 0 : i;
/*
* Disable als and enable it again to avoid incorrect value.
* Updating als gain during als measurement cycle will cause
* incorrect light sensor adc value. The logic here is to handle
* this scenario.
*/
if (ltr->als_gain != gain_index) {
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL, &config);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
return rc;
}
/* disable als sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
config & (~0x1));
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
return rc;
}
/* write new als gain */
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
(config & (~0x1c)) | (gain_index << 2));
if (rc) {
dev_err(<r->i2c->dev, "write %d register failed\n",
LTR553_REG_ALS_CTL);
return rc;
}
}
if ((ch0data == 0) && (ch1data == 0)) {
adc = 1;
} else {
ratio = ch1data * 100 / (ch0data + ch1data);
dev_dbg(<r->i2c->dev, "ratio:%d\n", ratio);
adc = ltr553_calc_adc(ratio, sensitivity_table[gain_index],
als_gain_table[gain_index], als_int_fac);
}
dev_dbg(<r->i2c->dev, "adc:%d\n", adc);
/* catch'ya! */
adc_base = ch0data * als_gain_table[gain_index] /
als_gain_table[ltr->als_gain];
/* upper threshold */
if (adc_base + adc > ALS_DATA_MASK) {
als_data[0] = 0xff;
als_data[1] = 0xff;
} else {
als_data[0] = ALS_LOW_BYTE(adc_base + adc);
als_data[1] = ALS_HIGH_BYTE(adc_base + adc);
}
/* lower threshold */
if (adc_base < adc) {
als_data[2] = 0x0;
als_data[3] = 0x0;
} else {
als_data[2] = ALS_LOW_BYTE(adc_base - adc);
als_data[3] = ALS_HIGH_BYTE(adc_base - adc);
}
rc = regmap_bulk_write(ltr->regmap, LTR553_REG_ALS_THRES_UP_0,
als_data, 4);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_THRES_UP_0, rc);
return rc;
}
if (ltr->als_gain != gain_index) {
/* enable als_sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
(config & (~0x1c)) | (gain_index << 2) | 0x1);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
return rc;
}
ltr->als_gain = gain_index;
}
return 0;
}
static int ltr553_process_data(struct ltr553_data *ltr, int als_ps)
{
int als_int_fac;
ktime_t timestamp;
int rc = 0;
unsigned int tmp;
u8 als_data[4];
int lux;
int ch0data;
int ch1data;
u8 ps_data[4];
int i;
int distance;
timestamp = ktime_get();
if (als_ps) { /* process als data */
/* Read data */
rc = regmap_bulk_read(ltr->regmap, LTR553_REG_ALS_DATA_CH1_0,
als_data, 4);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_DATA_CH1_0, rc);
goto exit;
}
ch0data = als_data[2] | (als_data[3] << 8);
ch1data = als_data[0] | (als_data[1] << 8);
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_MEAS_RATE, &tmp);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_MEAS_RATE, rc);
goto exit;
}
tmp = (tmp & LTR553_ALS_MEASURE_MASK) >> 3;
als_int_fac = als_int_fac_table[tmp];
lux = ltr553_calc_lux(ch0data, ch1data,
als_gain_table[ltr->als_gain], als_int_fac);
dev_dbg(<r->i2c->dev, "lux:%d als_data:0x%x-0x%x-0x%x-0x%x\n",
lux, als_data[0], als_data[1],
als_data[2], als_data[3]);
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_PS_STATUS, &tmp);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_PS_STATUS, rc);
goto exit;
}
if ((lux != ltr->last_als) && (!LTR553_ALS_INVALID(tmp))) {
input_report_abs(ltr->input_light, ABS_MISC, lux);
input_event(ltr->input_light, EV_SYN, SYN_TIME_SEC,
ktime_to_timespec(timestamp).tv_sec);
input_event(ltr->input_light, EV_SYN, SYN_TIME_NSEC,
ktime_to_timespec(timestamp).tv_nsec);
input_sync(ltr->input_light);
ltr->last_als_ts = timestamp;
}
ltr->last_als = lux;
dev_dbg(<r->i2c->dev, "previous als_gain:%d\n",
ltr->als_gain);
rc = ltr553_als_update_setting(ltr, ch0data, ch1data,
als_int_fac);
if (rc) {
dev_err(<r->i2c->dev, "update setting failed\n");
goto exit;
}
dev_dbg(<r->i2c->dev, "new als_gain:%d\n",
ltr->als_gain);
} else { /* process ps value */
rc = regmap_bulk_read(ltr->regmap, LTR553_REG_PS_DATA_0,
ps_data, 2);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_PS_DATA_0, rc);
goto exit;
}
dev_dbg(<r->i2c->dev, "ps data: 0x%x 0x%x\n",
ps_data[0], ps_data[1]);
tmp = (ps_data[1] << 8) | ps_data[0];
if (tmp & LTR553_PS_SATURATE_MASK)
distance = 0;
else {
for (i = 0; i < ARRAY_SIZE(ps_distance_table); i++) {
if (tmp > ps_distance_table[i]) {
distance = i;
break;
}
}
distance = i;
}
if (distance != ltr->last_ps) {
input_report_abs(ltr->input_proximity, ABS_DISTANCE,
distance);
input_event(ltr->input_proximity, EV_SYN, SYN_TIME_SEC,
ktime_to_timespec(timestamp).tv_sec);
input_event(ltr->input_proximity, EV_SYN, SYN_TIME_NSEC,
ktime_to_timespec(timestamp).tv_nsec);
input_sync(ltr->input_proximity);
ltr->last_ps_ts = timestamp;
}
ltr->last_ps = distance;
/* lower threshold */
if (distance < ARRAY_SIZE(ps_distance_table))
tmp = ps_distance_table[distance];
else
tmp = 0;
ps_data[2] = PS_LOW_BYTE(tmp);
ps_data[3] = PS_HIGH_BYTE(tmp);
/* upper threshold */
if (distance > 0)
tmp = ps_distance_table[distance - 1];
else
tmp = PS_DATA_MASK;
ps_data[0] = PS_LOW_BYTE(tmp);
ps_data[1] = PS_HIGH_BYTE(tmp);
dev_dbg(<r->i2c->dev, "ps threshold: 0x%x 0x%x 0x%x 0x%x\n",
ps_data[0], ps_data[1], ps_data[2], ps_data[3]);
rc = regmap_bulk_write(ltr->regmap, LTR553_REG_PS_THRES_UP_0,
ps_data, 4);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_THRES_UP_0, rc);
goto exit;
}
}
exit:
return rc;
}
static irqreturn_t ltr553_irq_handler(int irq, void *data)
{
struct ltr553_data *ltr = data;
bool rc;
rc = queue_work(ltr->workqueue, <r->report_work);
/* wake up event should hold a wake lock until reported */
if (rc && (atomic_inc_return(<r->wake_count) == 1))
pm_stay_awake(<r->i2c->dev);
return IRQ_HANDLED;
}
static void ltr553_report_work(struct work_struct *work)
{
struct ltr553_data *ltr = container_of(work, struct ltr553_data,
report_work);
int rc;
unsigned int status;
u8 buf[7];
mutex_lock(<r->ops_lock);
/* avoid fake interrupt */
if (!ltr->power_enabled) {
dev_dbg(<r->i2c->dev, "fake interrupt triggered\n");
goto exit;
}
/* read status */
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_PS_STATUS, &status);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_PS_STATUS, rc);
status |= LTR553_PS_INT_MASK;
goto exit;
}
dev_dbg(<r->i2c->dev, "interrupt issued status=0x%x.\n", status);
/* als interrupt issueed */
if ((status & LTR553_ALS_INT_MASK) && (ltr->als_enabled)) {
rc = ltr553_process_data(ltr, 1);
if (rc)
goto exit;
dev_dbg(<r->i2c->dev, "process als done!\n");
}
if ((status & LTR553_PS_INT_MASK) && (ltr->ps_enabled)) {
rc = ltr553_process_data(ltr, 0);
if (rc)
goto exit;
dev_dbg(<r->i2c->dev, "process ps data done!\n");
pm_wakeup_event(<r->input_proximity->dev, 200);
}
exit:
if (atomic_dec_and_test(<r->wake_count)) {
pm_relax(<r->i2c->dev);
dev_dbg(<r->i2c->dev, "wake lock released\n");
}
/* clear interrupt */
if (regmap_bulk_read(ltr->regmap, LTR553_REG_ALS_DATA_CH1_0,
buf, ARRAY_SIZE(buf)))
dev_err(<r->i2c->dev, "clear interrupt failed\n");
mutex_unlock(<r->ops_lock);
}
static int ltr553_enable_ps(struct ltr553_data *ltr, int enable)
{
unsigned int config;
unsigned int tmp;
int rc = 0;
u8 buf[7];
rc = regmap_read(ltr->regmap, LTR553_REG_PS_CTL, &config);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
return rc;
}
if (enable) {
/* Enable ps sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_CTL,
config | 0x02);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
goto exit;
}
rc = regmap_read(ltr->regmap, LTR553_REG_PS_MEAS_RATE, &tmp);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_PS_MEAS_RATE, rc);
goto exit;
}
/* Wait for data ready */
msleep(ps_mrr_table[tmp & 0xf] + LTR553_WAKE_TIME_MS);
/* clear last ps value */
ltr->last_ps = -1;
rc = ltr553_process_data(ltr, 0);
if (rc) {
dev_err(<r->i2c->dev, "process ps data failed\n");
goto exit;
}
/* clear interrupt */
rc = regmap_bulk_read(ltr->regmap, LTR553_REG_ALS_DATA_CH1_0,
buf, ARRAY_SIZE(buf));
if (rc) {
dev_err(<r->i2c->dev, "clear interrupt failed\n");
goto exit;
}
ltr->ps_enabled = true;
} else {
/* disable ps_sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_CTL,
config & (~0x02));
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
goto exit;
}
ltr->ps_enabled = false;
}
exit:
return rc;
}
static int ltr553_enable_als(struct ltr553_data *ltr, int enable)
{
int rc = 0;
unsigned int config;
unsigned int tmp;
u8 buf[7];
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL, &config);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
goto exit;
}
if (enable) {
/* enable als_sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
config | 0x1);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
goto exit;
}
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_MEAS_RATE, &tmp);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_MEAS_RATE, rc);
goto exit;
}
/* Wait for data ready */
msleep(als_mrr_table[tmp & 0x7] + LTR553_WAKE_TIME_MS);
/* Clear last value and report even not change. */
ltr->last_als = -1;
rc = ltr553_process_data(ltr, 1);
if (rc) {
dev_err(<r->i2c->dev, "process als data failed\n");
goto exit;
}
/* clear interrupt */
rc = regmap_bulk_read(ltr->regmap, LTR553_REG_ALS_DATA_CH1_0,
buf, ARRAY_SIZE(buf));
if (rc) {
dev_err(<r->i2c->dev, "clear interrupt failed\n");
goto exit;
}
ltr->als_enabled = true;
} else {
/* disable als sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
config & (~0x1));
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, rc);
goto exit;
}
ltr->als_enabled = false;
}
exit:
return rc;
}
static int ltr553_als_sync_delay(struct ltr553_data *ltr,
unsigned int als_delay)
{
int index = 0;
int i;
unsigned int val;
int rc = 0;
int min;
if (!ltr->power_enabled) {
dev_dbg(<r->i2c->dev, "power is not enabled\n");
return 0;
}
min = abs(als_delay - als_mrr_table[0]);
for (i = 0; i < ARRAY_SIZE(als_mrr_table); i++) {
if (als_mrr_table[i] >= 10 *
als_int_fac_table[ltr->als_integration_time]) {
if (als_delay == als_mrr_table[i]) {
index = i;
break;
}
if (min > abs(als_delay - als_mrr_table[i])) {
index = i;
min = abs(als_delay - als_mrr_table[i]);
}
}
}
dev_dbg(<r->i2c->dev, "als delay %d ms\n", als_mrr_table[index]);
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_MEAS_RATE, &val);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed\n",
LTR553_REG_ALS_MEAS_RATE);
goto exit;
}
val &= ~0x7;
ltr->als_measure_rate = index;
rc = regmap_write(ltr->regmap, LTR553_REG_ALS_MEAS_RATE, val | index);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed\n",
LTR553_REG_ALS_MEAS_RATE);
goto exit;
}
exit:
return rc;
}
static int ltr553_ps_sync_delay(struct ltr553_data *ltr, unsigned int ps_delay)
{
int index = 0;
int i;
int rc = 0;
int min;
if (!ltr->power_enabled) {
dev_dbg(<r->i2c->dev, "power is not enabled\n");
return 0;
}
min = abs(ps_delay - ps_mrr_table[0]);
for (i = 0; i < ARRAY_SIZE(ps_mrr_table); i++) {
if (ps_delay == ps_mrr_table[i]) {
index = i;
break;
}
if (min > abs(ps_delay - ps_mrr_table[i])) {
min = abs(ps_delay - ps_mrr_table[i]);
index = i;
}
}
ltr->ps_measure_rate = index;
dev_dbg(<r->i2c->dev, "ps delay %d ms\n", ps_mrr_table[index]);
rc = regmap_write(ltr->regmap, LTR553_REG_PS_MEAS_RATE, index);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed\n",
LTR553_REG_PS_MEAS_RATE);
goto exit;
}
exit:
return rc;
}
static void ltr553_als_enable_work(struct work_struct *work)
{
struct ltr553_data *ltr = container_of(work, struct ltr553_data,
als_enable_work);
mutex_lock(<r->ops_lock);
if (!ltr->power_enabled) { /* new HAL? */
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), true)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
msleep(LTR553_BOOT_TIME_MS);
ltr->power_enabled = true;
if (ltr553_init_device(ltr)) {
dev_err(<r->i2c->dev, "init device failed\n");
goto exit_power_off;
}
ltr553_als_sync_delay(ltr, ltr->als_delay);
}
if (ltr553_enable_als(ltr, 1)) {
dev_err(<r->i2c->dev, "enable als failed\n");
goto exit_power_off;
}
exit_power_off:
if ((!ltr->als_enabled) && (!ltr->ps_enabled) &&
ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
ltr->power_enabled = false;
}
exit:
mutex_unlock(<r->ops_lock);
}
static void ltr553_als_disable_work(struct work_struct *work)
{
struct ltr553_data *ltr = container_of(work, struct ltr553_data,
als_disable_work);
mutex_lock(<r->ops_lock);
if (ltr553_enable_als(ltr, 0)) {
dev_err(<r->i2c->dev, "disable als failed\n");
goto exit;
}
if ((!ltr->als_enabled) && (!ltr->ps_enabled) && ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
ltr->power_enabled = false;
}
exit:
mutex_unlock(<r->ops_lock);
}
static void ltr553_ps_enable_work(struct work_struct *work)
{
struct ltr553_data *ltr = container_of(work, struct ltr553_data,
ps_enable_work);
mutex_lock(<r->ops_lock);
if (!ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), true)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
msleep(LTR553_BOOT_TIME_MS);
ltr->power_enabled = true;
if (ltr553_init_device(ltr)) {
dev_err(<r->i2c->dev, "init device failed\n");
goto exit_power_off;
}
ltr553_ps_sync_delay(ltr, ltr->ps_delay);
}
if (ltr553_enable_ps(ltr, 1)) {
dev_err(<r->i2c->dev, "enable ps failed\n");
goto exit_power_off;
}
exit_power_off:
if ((!ltr->als_enabled) && (!ltr->ps_enabled) &&
ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
ltr->power_enabled = false;
}
exit:
mutex_unlock(<r->ops_lock);
}
static void ltr553_ps_disable_work(struct work_struct *work)
{
struct ltr553_data *ltr = container_of(work, struct ltr553_data,
ps_disable_work);
mutex_lock(<r->ops_lock);
if (ltr553_enable_ps(ltr, 0)) {
dev_err(<r->i2c->dev, "ltrsable ps failed\n");
goto exit;
}
if ((!ltr->als_enabled) && (!ltr->ps_enabled) && ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
ltr->power_enabled = false;
}
exit:
mutex_unlock(<r->ops_lock);
}
static struct regmap_config ltr553_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
};
static int ltr553_cdev_enable_als(struct sensors_classdev *sensors_cdev,
unsigned int enable)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, als_cdev);
mutex_lock(<r->ops_lock);
if (enable)
queue_work(ltr->workqueue, <r->als_enable_work);
else
queue_work(ltr->workqueue, <r->als_disable_work);
mutex_unlock(<r->ops_lock);
return 0;
}
static int ltr553_cdev_enable_ps(struct sensors_classdev *sensors_cdev,
unsigned int enable)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, ps_cdev);
mutex_lock(<r->ops_lock);
if (enable)
queue_work(ltr->workqueue, <r->ps_enable_work);
else
queue_work(ltr->workqueue, <r->ps_disable_work);
mutex_unlock(<r->ops_lock);
return 0;
}
static int ltr553_cdev_set_als_delay(struct sensors_classdev *sensors_cdev,
unsigned int delay_msec)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, als_cdev);
int rc;
mutex_lock(<r->ops_lock);
ltr->als_delay = delay_msec;
rc = ltr553_als_sync_delay(ltr, delay_msec);
mutex_unlock(<r->ops_lock);
return rc;
}
static int ltr553_cdev_set_ps_delay(struct sensors_classdev *sensors_cdev,
unsigned int delay_msec)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, ps_cdev);
int rc;
mutex_lock(<r->ops_lock);
ltr->ps_delay = delay_msec;
rc = ltr553_ps_sync_delay(ltr, delay_msec);
mutex_unlock(<r->ops_lock);
return 0;
}
static int ltr553_cdev_ps_flush(struct sensors_classdev *sensors_cdev)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, ps_cdev);
input_event(ltr->input_proximity, EV_SYN, SYN_CONFIG,
ltr->flush_count++);
input_sync(ltr->input_proximity);
return 0;
}
static int ltr553_cdev_als_flush(struct sensors_classdev *sensors_cdev)
{
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, als_cdev);
input_event(ltr->input_light, EV_SYN, SYN_CONFIG, ltr->flush_count++);
input_sync(ltr->input_light);
return 0;
}
/* This function should be called when sensor is disabled */
static int ltr553_cdev_ps_calibrate(struct sensors_classdev *sensors_cdev,
int axis, int apply_now)
{
int rc;
int power;
unsigned int config;
unsigned int interrupt;
u16 min = PS_DATA_MASK;
u8 ps_data[2];
int count = LTR553_CALIBRATE_SAMPLES;
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, ps_cdev);
if (axis != AXIS_BIAS)
return 0;
mutex_lock(<r->ops_lock);
/* Ensure only be called when sensors in standy mode */
if (ltr->als_enabled || ltr->ps_enabled) {
rc = -EPERM;
goto exit;
}
power = ltr->power_enabled;
if (!power) {
rc = sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), true);
if (rc) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
msleep(LTR553_BOOT_TIME_MS);
rc = ltr553_init_device(ltr);
if (rc) {
dev_err(<r->i2c->dev, "init ltr553 failed\n");
goto exit;
}
}
rc = regmap_read(ltr->regmap, LTR553_REG_INTERRUPT, &interrupt);
if (rc) {
dev_err(<r->i2c->dev, "read interrupt configuration failed\n");
goto exit_power_off;
}
/* disable interrupt */
rc = regmap_write(ltr->regmap, LTR553_REG_INTERRUPT, 0x0);
if (rc) {
dev_err(<r->i2c->dev, "disable interrupt failed\n");
goto exit_power_off;
}
rc = regmap_read(ltr->regmap, LTR553_REG_PS_CTL, &config);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
goto exit_enable_interrupt;
}
/* clear offset */
ps_data[0] = 0;
ps_data[1] = 0;
rc = regmap_bulk_write(ltr->regmap, LTR553_REG_PS_OFFSET_1,
ps_data, 2);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_OFFSET_1, rc);
goto exit_enable_interrupt;
}
/* enable ps sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_CTL, config | 0x02);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
goto exit_enable_interrupt;
}
/* ps measurement rate set to fastest rate */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_MEAS_RATE,
LTR553_PS_MEASUREMENT_RATE_10MS);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_PS_MEAS_RATE, rc);
goto exit_enable_interrupt;
}
msleep(LTR553_WAKE_TIME_MS);
while (--count) {
/* the measurement rate is 10 ms */
usleep_range(11000, 12000);
rc = regmap_bulk_read(ltr->regmap, LTR553_REG_PS_DATA_0,
ps_data, 2);
if (rc) {
dev_err(<r->i2c->dev, "read PS data failed\n");
break;
}
if (min > ((ps_data[1] << 8) | ps_data[0]))
min = (ps_data[1] << 8) | ps_data[0];
}
/* disable ps sensor */
rc = regmap_write(ltr->regmap, LTR553_REG_PS_CTL, config);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_CTL, rc);
goto exit_enable_interrupt;
}
if (!count) {
if (min > (PS_DATA_MASK >> 1)) {
dev_err(<r->i2c->dev, "ps data out of range, check if shield\n");
rc = -EINVAL;
goto exit_enable_interrupt;
}
if (apply_now) {
ps_data[1] = PS_LOW_BYTE(min);
ps_data[0] = PS_HIGH_BYTE(min);
rc = regmap_bulk_write(ltr->regmap,
LTR553_REG_PS_OFFSET_1, ps_data, 2);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_OFFSET_1, rc);
goto exit_enable_interrupt;
}
ltr->bias = min;
}
snprintf(ltr->calibrate_buf, sizeof(ltr->calibrate_buf),
"0,0,%d", min);
dev_dbg(<r->i2c->dev, "result: %s\n", ltr->calibrate_buf);
} else {
dev_err(<r->i2c->dev, "calibration failed\n");
rc = -EINVAL;
}
exit_enable_interrupt:
if (regmap_write(ltr->regmap, LTR553_REG_INTERRUPT, interrupt)) {
dev_err(<r->i2c->dev, "enable interrupt failed\n");
goto exit_power_off;
}
exit_power_off:
if (!power) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power off sensor failed.\n");
goto exit;
}
}
exit:
mutex_unlock(<r->ops_lock);
return rc;
}
static int ltr553_cdev_ps_write_cal(struct sensors_classdev *sensors_cdev,
struct cal_result_t *cal_result)
{
int power;
u8 ps_data[2];
int rc = 0;
struct ltr553_data *ltr = container_of(sensors_cdev,
struct ltr553_data, ps_cdev);
mutex_lock(<r->ops_lock);
power = ltr->power_enabled;
if (!power) {
rc = sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), true);
if (rc) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
}
ltr->bias = cal_result->bias;
ps_data[1] = PS_LOW_BYTE(cal_result->bias);
ps_data[0] = PS_HIGH_BYTE(cal_result->bias);
rc = regmap_bulk_write(ltr->regmap, LTR553_REG_PS_OFFSET_1,
ps_data, 2);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_PS_OFFSET_1, rc);
goto exit_power_off;
}
snprintf(ltr->calibrate_buf, sizeof(ltr->calibrate_buf), "0,0,%d",
ltr->bias);
exit_power_off:
if (!power) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power off sensor failed.\n");
goto exit;
}
}
exit:
mutex_unlock(<r->ops_lock);
return rc;
};
static ssize_t ltr553_register_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct ltr553_data *ltr = dev_get_drvdata(dev);
unsigned int val;
int rc;
ssize_t count = 0;
int i;
if (ltr->reg_addr == LTR553_REG_MAGIC) {
for (i = 0; i <= 0x1f; i++) {
rc = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL + i,
&val);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed\n",
LTR553_REG_ALS_CTL + i);
break;
}
count += snprintf(&buf[count], PAGE_SIZE,
"0x%x: 0x%x\n", LTR553_REG_ALS_CTL + i,
val);
}
} else {
rc = regmap_read(ltr->regmap, ltr->reg_addr, &val);
if (rc) {
dev_err(<r->i2c->dev, "read %d failed\n",
ltr->reg_addr);
return rc;
}
count += snprintf(&buf[count], PAGE_SIZE, "0x%x:0x%x\n",
ltr->reg_addr, val);
}
return count;
}
static ssize_t ltr553_register_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct ltr553_data *ltr = dev_get_drvdata(dev);
unsigned int reg;
unsigned int val;
unsigned int cmd;
int rc;
if (sscanf(buf, "%u %u %u\n", &cmd, ®, &val) < 2) {
dev_err(<r->i2c->dev, "argument error\n");
return -EINVAL;
}
if (cmd == CMD_WRITE) {
rc = regmap_write(ltr->regmap, reg, val);
if (rc) {
dev_err(<r->i2c->dev, "write %d failed\n", reg);
return rc;
}
} else if (cmd == CMD_READ) {
ltr->reg_addr = reg;
dev_dbg(<r->i2c->dev, "register address set to 0x%x\n", reg);
}
return size;
}
static DEVICE_ATTR(register, S_IWUSR | S_IRUGO,
ltr553_register_show,
ltr553_register_store);
static struct attribute *ltr553_attr[] = {
&dev_attr_register.attr,
NULL
};
static const struct attribute_group ltr553_attr_group = {
.attrs = ltr553_attr,
};
static int ltr553_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct ltr553_data *ltr;
int res = 0;
dev_dbg(&client->dev, "probling ltr553...\n");
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
dev_err(&client->dev, "ltr553 i2c check failed.\n");
return -ENODEV;
}
ltr = devm_kzalloc(&client->dev, sizeof(struct ltr553_data),
GFP_KERNEL);
if (!ltr) {
dev_err(&client->dev, "memory allocation failed,\n");
return -ENOMEM;
}
ltr->i2c = client;
if (client->dev.of_node) {
res = ltr553_parse_dt(&client->dev, ltr);
if (res) {
dev_err(&client->dev,
"unable to parse device tree.(%d)\n", res);
goto out;
}
} else {
dev_err(&client->dev, "device tree not found.\n");
res = -ENODEV;
goto out;
}
dev_set_drvdata(&client->dev, ltr);
mutex_init(<r->ops_lock);
ltr->regmap = devm_regmap_init_i2c(client, <r553_regmap_config);
if (IS_ERR(ltr->regmap)) {
dev_err(&client->dev, "init regmap failed.(%ld)\n",
PTR_ERR(ltr->regmap));
res = PTR_ERR(ltr->regmap);
goto out;
}
res = sensor_power_init(&client->dev, power_config,
ARRAY_SIZE(power_config));
if (res) {
dev_err(&client->dev, "init power failed.\n");
goto out;
}
res = sensor_power_config(&client->dev, power_config,
ARRAY_SIZE(power_config), true);
if (res) {
dev_err(&client->dev, "power up sensor failed.\n");
goto err_power_config;
}
res = sensor_pinctrl_init(&client->dev, &pin_config);
if (res) {
dev_err(&client->dev, "init pinctrl failed.\n");
goto err_pinctrl_init;
}
msleep(LTR553_BOOT_TIME_MS);
res = ltr553_check_device(ltr);
if (res) {
dev_err(&client->dev, "check device failed.\n");
goto err_check_device;
}
ltr->als_measure_rate = LTR553_ALS_DEFAULT_MEASURE_RATE;
res = ltr553_init_device(ltr);
if (res) {
dev_err(&client->dev, "check device failed.\n");
goto err_init_device;
}
/* configure interrupt */
if (gpio_is_valid(ltr->irq_gpio)) {
res = gpio_request(ltr->irq_gpio, "ltr553_interrupt");
if (res) {
dev_err(&client->dev,
"unable to request interrupt gpio %d\n",
ltr->irq_gpio);
goto err_request_gpio;
}
res = gpio_direction_input(ltr->irq_gpio);
if (res) {
dev_err(&client->dev,
"unable to set direction for gpio %d\n",
ltr->irq_gpio);
goto err_set_direction;
}
ltr->irq = gpio_to_irq(ltr->irq_gpio);
res = devm_request_irq(&client->dev, ltr->irq,
ltr553_irq_handler,
ltr->irq_flags | IRQF_ONESHOT,
"ltr553", ltr);
if (res) {
dev_err(&client->dev,
"request irq %d failed(%d),\n",
ltr->irq, res);
goto err_request_irq;
}
/* device wakeup initialization */
device_init_wakeup(&client->dev, 1);
ltr->workqueue = alloc_workqueue("ltr553_workqueue",
WQ_NON_REENTRANT | WQ_FREEZABLE, 0);
INIT_WORK(<r->report_work, ltr553_report_work);
INIT_WORK(<r->als_enable_work, ltr553_als_enable_work);
INIT_WORK(<r->als_disable_work, ltr553_als_disable_work);
INIT_WORK(<r->ps_enable_work, ltr553_ps_enable_work);
INIT_WORK(<r->ps_disable_work, ltr553_ps_disable_work);
} else {
res = -ENODEV;
goto err_init_device;
}
res = sysfs_create_group(&client->dev.kobj, <r553_attr_group);
if (res) {
dev_err(&client->dev, "sysfs create group failed\n");
goto err_create_group;
}
res = ltr553_init_input(ltr);
if (res) {
dev_err(&client->dev, "init input failed.\n");
goto err_init_input;
}
ltr->als_cdev = als_cdev;
ltr->als_cdev.sensors_enable = ltr553_cdev_enable_als;
ltr->als_cdev.sensors_poll_delay = ltr553_cdev_set_als_delay;
ltr->als_cdev.sensors_flush = ltr553_cdev_als_flush;
res = sensors_classdev_register(&client->dev, <r->als_cdev);
if (res) {
dev_err(&client->dev, "sensors class register failed.\n");
goto err_register_als_cdev;
}
ltr->ps_cdev = ps_cdev;
ltr->ps_cdev.sensors_enable = ltr553_cdev_enable_ps;
ltr->ps_cdev.sensors_poll_delay = ltr553_cdev_set_ps_delay;
ltr->ps_cdev.sensors_flush = ltr553_cdev_ps_flush;
ltr->ps_cdev.sensors_calibrate = ltr553_cdev_ps_calibrate;
ltr->ps_cdev.sensors_write_cal_params = ltr553_cdev_ps_write_cal;
ltr->ps_cdev.params = ltr->calibrate_buf;
res = sensors_classdev_register(&client->dev, <r->ps_cdev);
if (res) {
dev_err(&client->dev, "sensors class register failed.\n");
goto err_register_ps_cdev;
}
sensor_power_config(&client->dev, power_config,
ARRAY_SIZE(power_config), false);
dev_dbg(&client->dev, "ltr553 successfully probed!\n");
return 0;
err_register_ps_cdev:
sensors_classdev_unregister(<r->als_cdev);
err_register_als_cdev:
err_init_input:
sysfs_remove_group(&client->dev.kobj, <r553_attr_group);
err_create_group:
err_request_irq:
err_set_direction:
gpio_free(ltr->irq_gpio);
err_request_gpio:
err_init_device:
device_init_wakeup(&client->dev, 0);
err_check_device:
err_pinctrl_init:
sensor_power_config(&client->dev, power_config,
ARRAY_SIZE(power_config), false);
err_power_config:
sensor_power_deinit(&client->dev, power_config,
ARRAY_SIZE(power_config));
out:
return res;
}
static int ltr553_remove(struct i2c_client *client)
{
struct ltr553_data *ltr = dev_get_drvdata(&client->dev);
sensors_classdev_unregister(<r->ps_cdev);
sensors_classdev_unregister(<r->als_cdev);
if (ltr->input_light)
input_unregister_device(ltr->input_light);
if (ltr->input_proximity)
input_unregister_device(ltr->input_proximity);
destroy_workqueue(ltr->workqueue);
device_init_wakeup(<r->i2c->dev, 0);
sensor_power_config(&client->dev, power_config,
ARRAY_SIZE(power_config), false);
sensor_power_deinit(&client->dev, power_config,
ARRAY_SIZE(power_config));
return 0;
}
static int ltr553_suspend(struct device *dev)
{
int res = 0;
struct ltr553_data *ltr = dev_get_drvdata(dev);
u8 ps_data[4];
unsigned int config;
int idx = ltr->ps_wakeup_threshold;
dev_dbg(dev, "suspending ltr553...");
mutex_lock(<r->ops_lock);
/* proximity is enabled */
if (ltr->ps_enabled) {
/* disable als sensor to avoid wake up by als interrupt */
if (ltr->als_enabled) {
res = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL,
&config);
if (res) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_CTL, res);
return res;
}
res = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
config & (~0x1));
if (res) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, res);
goto exit;
}
}
/* Don't power off sensor because proximity is a
* wake up sensor.
*/
if (device_may_wakeup(<r->i2c->dev)) {
dev_dbg(<r->i2c->dev, "enable irq wake\n");
enable_irq_wake(ltr->irq);
}
/* Setup threshold to avoid frequent wakeup */
if (device_may_wakeup(<r->i2c->dev) &&
(idx != LTR553_WAKEUP_ANY_CHANGE)) {
dev_dbg(<r->i2c->dev, "last ps: %d\n", ltr->last_ps);
if (ltr->last_ps > idx) {
ps_data[2] = 0x0;
ps_data[3] = 0x0;
ps_data[0] =
PS_LOW_BYTE(ps_distance_table[idx]);
ps_data[1] =
PS_HIGH_BYTE(ps_distance_table[idx]);
} else {
ps_data[2] =
PS_LOW_BYTE(ps_distance_table[idx]);
ps_data[3] =
PS_HIGH_BYTE(ps_distance_table[idx]);
ps_data[0] = PS_LOW_BYTE(PS_DATA_MASK);
ps_data[1] = PS_HIGH_BYTE(PS_DATA_MASK);
}
res = regmap_bulk_write(ltr->regmap,
LTR553_REG_PS_THRES_UP_0, ps_data, 4);
if (res) {
dev_err(<r->i2c->dev, "set up threshold failed\n");
goto exit;
}
}
} else {
/* power off */
disable_irq(ltr->irq);
if (ltr->power_enabled) {
res = sensor_power_config(dev, power_config,
ARRAY_SIZE(power_config), false);
if (res) {
dev_err(dev, "failed to suspend ltr553\n");
enable_irq(ltr->irq);
goto exit;
}
}
pinctrl_select_state(pin_config.pinctrl, pin_config.state[1]);
}
exit:
mutex_unlock(<r->ops_lock);
return res;
}
static int ltr553_resume(struct device *dev)
{
int res = 0;
struct ltr553_data *ltr = dev_get_drvdata(dev);
unsigned int config;
dev_dbg(dev, "resuming ltr553...");
if (ltr->ps_enabled) {
if (device_may_wakeup(<r->i2c->dev)) {
dev_dbg(<r->i2c->dev, "disable irq wake\n");
disable_irq_wake(ltr->irq);
}
if (ltr->als_enabled) {
res = regmap_read(ltr->regmap, LTR553_REG_ALS_CTL,
&config);
if (res) {
dev_err(<r->i2c->dev, "read %d failed.(%d)\n",
LTR553_REG_ALS_CTL, res);
goto exit;
}
res = regmap_write(ltr->regmap, LTR553_REG_ALS_CTL,
config | 0x1);
if (res) {
dev_err(<r->i2c->dev, "write %d failed.(%d)\n",
LTR553_REG_ALS_CTL, res);
goto exit;
}
}
} else {
pinctrl_select_state(pin_config.pinctrl, pin_config.state[0]);
/* Power up sensor */
if (ltr->power_enabled) {
res = sensor_power_config(dev, power_config,
ARRAY_SIZE(power_config), true);
if (res) {
dev_err(dev, "failed to power up ltr553\n");
goto exit;
}
msleep(LTR553_BOOT_TIME_MS);
res = ltr553_init_device(ltr);
if (res) {
dev_err(dev, "failed to init ltr553\n");
goto exit_power_off;
}
}
if (ltr->als_enabled) {
res = ltr553_enable_als(ltr, ltr->als_enabled);
if (res) {
dev_err(dev, "failed to enable ltr553\n");
goto exit_power_off;
}
}
enable_irq(ltr->irq);
}
return res;
exit_power_off:
if ((!ltr->als_enabled) && (!ltr->ps_enabled) &&
ltr->power_enabled) {
if (sensor_power_config(<r->i2c->dev, power_config,
ARRAY_SIZE(power_config), false)) {
dev_err(<r->i2c->dev, "power up sensor failed.\n");
goto exit;
}
ltr->power_enabled = false;
}
exit:
return res;
}
static const struct i2c_device_id ltr553_id[] = {
{ LTR553_I2C_NAME, 0 },
{ }
};
static struct of_device_id ltr553_match_table[] = {
{ .compatible = "liteon,ltr553", },
{ },
};
static const struct dev_pm_ops ltr553_pm_ops = {
.suspend = ltr553_suspend,
.resume = ltr553_resume,
};
static struct i2c_driver ltr553_driver = {
.probe = ltr553_probe,
.remove = ltr553_remove,
.id_table = ltr553_id,
.driver = {
.owner = THIS_MODULE,
.name = LTR553_I2C_NAME,
.of_match_table = ltr553_match_table,
.pm = <r553_pm_ops,
},
};
module_i2c_driver(ltr553_driver);
MODULE_DESCRIPTION("LTR-553ALPS Driver");
MODULE_LICENSE("GPL v2");
|