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

#define USE_INT
/* #define DEBUG_TIME_LOG */
#ifdef DEBUG_TIME_LOG
struct timeval start_tv, stop_tv;
#endif

/*
 * Global data
 */

#ifdef CAMERA_CCI
static struct stmvl53l0_module_fn_t stmvl53l0_module_func_tbl = {
	.init = stmvl53l0_init_cci,
	.deinit = stmvl53l0_exit_cci,
	.power_up = stmvl53l0_power_up_cci,
	.power_down = stmvl53l0_power_down_cci,
	.query_power_status = stmvl53l0_cci_power_status,
};
#else
static struct stmvl53l0_module_fn_t stmvl53l0_module_func_tbl = {
	.init = stmvl53l0_init_i2c,
	.deinit = stmvl53l0_exit_i2c,
	.power_up = stmvl53l0_power_up_i2c,
	.power_down = stmvl53l0_power_down_i2c,
	.stmv53l0_cci_power_status = NULL;
};
#endif
struct stmvl53l0_module_fn_t *pmodule_func_tbl;

struct stmvl53l0_api_fn_t {
	int8_t (*GetVersion)(VL53L0_Version_t *pVersion);
	int8_t (*GetPalSpecVersion)(VL53L0_Version_t *pPalSpecVersion);

	int8_t (*GetProductRevision)(VL53L0_DEV Dev,
					uint8_t *pProductRevisionMajor,
					uint8_t *pProductRevisionMinor);
	int8_t (*GetDeviceInfo)(VL53L0_DEV Dev,
				VL53L0_DeviceInfo_t *pVL53L0_DeviceInfo);
	int8_t (*GetDeviceErrorStatus)(VL53L0_DEV Dev,
				VL53L0_DeviceError *pDeviceErrorStatus);
	int8_t (*GetRangeStatusString)(uint8_t RangeStatus,
				char *pRangeStatusString);
	int8_t (*GetDeviceErrorString)(VL53L0_DeviceError ErrorCode,
				char *pDeviceErrorString);
	int8_t (*GetPalErrorString)(VL53L0_Error PalErrorCode,
				char *pPalErrorString);
	int8_t (*GetPalStateString)(VL53L0_State PalStateCode,
				char *pPalStateString);
	int8_t (*GetPalState)(VL53L0_DEV Dev,	VL53L0_State *pPalState);
	int8_t (*SetPowerMode)(VL53L0_DEV Dev,
				VL53L0_PowerModes PowerMode);
	int8_t (*GetPowerMode)(VL53L0_DEV Dev,
				VL53L0_PowerModes *pPowerMode);
	int8_t (*SetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev,
				int32_t OffsetCalibrationDataMicroMeter);
	int8_t (*GetOffsetCalibrationDataMicroMeter)(VL53L0_DEV Dev,
				int32_t *pOffsetCalibrationDataMicroMeter);
	int8_t (*SetLinearityCorrectiveGain)(VL53L0_DEV Dev,
				int16_t LinearityCorrectiveGain);
	int8_t (*GetLinearityCorrectiveGain)(VL53L0_DEV Dev,
				uint16_t *pLinearityCorrectiveGain);
	int8_t (*SetGroupParamHold)(VL53L0_DEV Dev,
				uint8_t GroupParamHold);
	int8_t (*GetUpperLimitMilliMeter)(VL53L0_DEV Dev,
				uint16_t *pUpperLimitMilliMeter);
	int8_t (*SetDeviceAddress)(VL53L0_DEV Dev,
				uint8_t DeviceAddress);
	int8_t (*DataInit)(VL53L0_DEV Dev);
	int8_t (*SetTuningSettingBuffer)(VL53L0_DEV Dev,
				uint8_t *pTuningSettingBuffer,
				uint8_t UseInternalTuningSettings);
	int8_t (*GetTuningSettingBuffer)(VL53L0_DEV Dev,
				uint8_t **pTuningSettingBuffer,
				uint8_t *pUseInternalTuningSettings);
	int8_t (*StaticInit)(VL53L0_DEV Dev);
	int8_t (*WaitDeviceBooted)(VL53L0_DEV Dev);
	int8_t (*ResetDevice)(VL53L0_DEV Dev);
	int8_t (*SetDeviceParameters)(VL53L0_DEV Dev,
			const VL53L0_DeviceParameters_t *pDeviceParameters);
	int8_t (*GetDeviceParameters)(VL53L0_DEV Dev,
				VL53L0_DeviceParameters_t *pDeviceParameters);
	int8_t (*SetDeviceMode)(VL53L0_DEV Dev,
				VL53L0_DeviceModes DeviceMode);
	int8_t (*GetDeviceMode)(VL53L0_DEV Dev,
				VL53L0_DeviceModes *pDeviceMode);
	int8_t (*SetHistogramMode)(VL53L0_DEV Dev,
				VL53L0_HistogramModes HistogramMode);
	int8_t (*GetHistogramMode)(VL53L0_DEV Dev,
				VL53L0_HistogramModes *pHistogramMode);
	int8_t (*SetMeasurementTimingBudgetMicroSeconds)(VL53L0_DEV Dev,
				uint32_t  MeasurementTimingBudgetMicroSeconds);
	int8_t (*GetMeasurementTimingBudgetMicroSeconds)(
				VL53L0_DEV Dev,
				uint32_t *pMeasurementTimingBudgetMicroSeconds);
	int8_t (*GetVcselPulsePeriod)(VL53L0_DEV Dev,
				VL53L0_VcselPeriod VcselPeriodType,
				uint8_t	*pVCSELPulsePeriod);
	int8_t (*SetVcselPulsePeriod)(VL53L0_DEV Dev,
				VL53L0_VcselPeriod VcselPeriodType,
				uint8_t VCSELPulsePeriod);
	int8_t (*SetSequenceStepEnable)(VL53L0_DEV Dev,
				VL53L0_SequenceStepId SequenceStepId,
				uint8_t SequenceStepEnabled);
	int8_t (*GetSequenceStepEnable)(VL53L0_DEV Dev,
				VL53L0_SequenceStepId SequenceStepId,
				uint8_t *pSequenceStepEnabled);
	int8_t (*GetSequenceStepEnables)(VL53L0_DEV Dev,
		VL53L0_SchedulerSequenceSteps_t *pSchedulerSequenceSteps);
	int8_t (*SetSequenceStepTimeout)(VL53L0_DEV Dev,
				VL53L0_SequenceStepId SequenceStepId,
				FixPoint1616_t TimeOutMilliSecs);
	int8_t (*GetSequenceStepTimeout)(VL53L0_DEV Dev,
				VL53L0_SequenceStepId SequenceStepId,
				FixPoint1616_t *pTimeOutMilliSecs);
	int8_t (*GetNumberOfSequenceSteps)(VL53L0_DEV Dev,
				uint8_t *pNumberOfSequenceSteps);
	int8_t (*GetSequenceStepsInfo)(
				VL53L0_SequenceStepId SequenceStepId,
				char *pSequenceStepsString);
	int8_t (*SetInterMeasurementPeriodMilliSeconds)(
				VL53L0_DEV Dev,
				uint32_t InterMeasurementPeriodMilliSeconds);
	int8_t (*GetInterMeasurementPeriodMilliSeconds)(
				VL53L0_DEV Dev,
				uint32_t *pInterMeasurementPeriodMilliSeconds);
	int8_t (*SetXTalkCompensationEnable)(VL53L0_DEV Dev,
				uint8_t XTalkCompensationEnable);
	int8_t (*GetXTalkCompensationEnable)(VL53L0_DEV Dev,
				uint8_t *pXTalkCompensationEnable);
	int8_t (*SetXTalkCompensationRateMegaCps)(
				VL53L0_DEV Dev,
				FixPoint1616_t XTalkCompensationRateMegaCps);
	int8_t (*GetXTalkCompensationRateMegaCps)(
				VL53L0_DEV Dev,
				FixPoint1616_t *pXTalkCompensationRateMegaCps);
	int8_t (*GetNumberOfLimitCheck)(
				uint16_t *pNumberOfLimitCheck);
	int8_t (*GetLimitCheckInfo)(VL53L0_DEV Dev,
				uint16_t LimitCheckId, char *pLimitCheckString);
	int8_t (*SetLimitCheckEnable)(VL53L0_DEV Dev,
				uint16_t LimitCheckId,
				uint8_t LimitCheckEnable);
	int8_t (*GetLimitCheckEnable)(VL53L0_DEV Dev,
		uint16_t LimitCheckId, uint8_t *pLimitCheckEnable);
	int8_t (*SetLimitCheckValue)(VL53L0_DEV Dev,
				uint16_t LimitCheckId,
				FixPoint1616_t LimitCheckValue);
	int8_t (*GetLimitCheckValue)(VL53L0_DEV Dev,
				uint16_t LimitCheckId,
				FixPoint1616_t *pLimitCheckValue);
	int8_t (*GetLimitCheckCurrent)(VL53L0_DEV Dev,
		uint16_t LimitCheckId, FixPoint1616_t *pLimitCheckCurrent);
	int8_t (*SetWrapAroundCheckEnable)(VL53L0_DEV Dev,
				uint8_t WrapAroundCheckEnable);
	int8_t (*GetWrapAroundCheckEnable)(VL53L0_DEV Dev,
				uint8_t *pWrapAroundCheckEnable);
	int8_t (*PerformSingleMeasurement)(VL53L0_DEV Dev);
	int8_t (*PerformRefCalibration)(VL53L0_DEV Dev,
				uint8_t *pVhvSettings, uint8_t *pPhaseCal);
	int8_t (*SetRefCalibration)(VL53L0_DEV Dev,
			uint8_t VhvSettings,
			uint8_t PhaseCal);
	int8_t (*GetRefCalibration)(VL53L0_DEV Dev,
			uint8_t *pVhvSettings,
			uint8_t *pPhaseCal);
	int8_t (*PerformXTalkCalibration)(VL53L0_DEV Dev,
				FixPoint1616_t XTalkCalDistance,
				FixPoint1616_t *pXTalkCompensationRateMegaCps);
	int8_t (*PerformOffsetCalibration)(VL53L0_DEV Dev,
				FixPoint1616_t CalDistanceMilliMeter,
				int32_t *pOffsetMicroMeter);
	int8_t (*StartMeasurement)(VL53L0_DEV Dev);
	int8_t (*StopMeasurement)(VL53L0_DEV Dev);
	int8_t (*GetMeasurementDataReady)(VL53L0_DEV Dev,
				uint8_t *pMeasurementDataReady);
	int8_t (*WaitDeviceReadyForNewMeasurement)(VL53L0_DEV Dev,
				uint32_t MaxLoop);
	int8_t (*GetRangingMeasurementData)(VL53L0_DEV Dev,
		VL53L0_RangingMeasurementData_t *pRangingMeasurementData);
	int8_t (*GetHistogramMeasurementData)(VL53L0_DEV Dev,
		VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData);
	int8_t (*PerformSingleRangingMeasurement)(VL53L0_DEV Dev,
		VL53L0_RangingMeasurementData_t *pRangingMeasurementData);
	int8_t (*PerformSingleHistogramMeasurement)(VL53L0_DEV Dev,
		VL53L0_HistogramMeasurementData_t *pHistogramMeasurementData);
	int8_t (*SetNumberOfROIZones)(VL53L0_DEV Dev,
				uint8_t NumberOfROIZones);
	int8_t (*GetNumberOfROIZones)(VL53L0_DEV Dev,
				uint8_t *pNumberOfROIZones);
	int8_t (*GetMaxNumberOfROIZones)(VL53L0_DEV Dev,
				uint8_t *pMaxNumberOfROIZones);
	int8_t (*SetGpioConfig)(VL53L0_DEV Dev,
				uint8_t Pin,
				VL53L0_DeviceModes DeviceMode,
				VL53L0_GpioFunctionality Functionality,
				VL53L0_InterruptPolarity Polarity);
	int8_t (*GetGpioConfig)(VL53L0_DEV Dev,
				uint8_t Pin,
				VL53L0_DeviceModes *pDeviceMode,
				VL53L0_GpioFunctionality *pFunctionality,
				VL53L0_InterruptPolarity *pPolarity);
	int8_t (*SetInterruptThresholds)(VL53L0_DEV Dev,
				VL53L0_DeviceModes DeviceMode,
				FixPoint1616_t ThresholdLow,
				FixPoint1616_t ThresholdHigh);
	int8_t (*GetInterruptThresholds)(VL53L0_DEV Dev,
				VL53L0_DeviceModes DeviceMode,
				FixPoint1616_t *pThresholdLow,
				FixPoint1616_t *pThresholdHigh);
	int8_t (*ClearInterruptMask)(VL53L0_DEV Dev,
				uint32_t InterruptMask);
	int8_t (*GetInterruptMaskStatus)(VL53L0_DEV Dev,
				uint32_t *pInterruptMaskStatus);
	int8_t (*EnableInterruptMask)(VL53L0_DEV Dev, uint32_t InterruptMask);
	int8_t (*SetSpadAmbientDamperThreshold)(VL53L0_DEV Dev,
				uint16_t SpadAmbientDamperThreshold);
	int8_t (*GetSpadAmbientDamperThreshold)(VL53L0_DEV Dev,
				uint16_t *pSpadAmbientDamperThreshold);
	int8_t (*SetSpadAmbientDamperFactor)(VL53L0_DEV Dev,
				uint16_t SpadAmbientDamperFactor);
	int8_t (*GetSpadAmbientDamperFactor)(VL53L0_DEV Dev,
				uint16_t *pSpadAmbientDamperFactor);
	int8_t (*PerformRefSpadManagement)(VL53L0_DEV Dev,
		uint32_t *refSpadCount, uint8_t *isApertureSpads);
	int8_t (*SetReferenceSpads)(VL53L0_DEV Dev,
			 uint32_t count, uint8_t isApertureSpads);
	int8_t (*GetReferenceSpads)(VL53L0_DEV Dev,
			uint32_t *pSpadCount, uint8_t *pIsApertureSpads);
};

static struct stmvl53l0_api_fn_t stmvl53l0_api_func_tbl = {
	.GetVersion = VL53L0_GetVersion,
	.GetPalSpecVersion = VL53L0_GetPalSpecVersion,
	.GetProductRevision = VL53L0_GetProductRevision,
	.GetDeviceInfo = VL53L0_GetDeviceInfo,
	.GetDeviceErrorStatus = VL53L0_GetDeviceErrorStatus,
	.GetRangeStatusString = VL53L0_GetRangeStatusString,
	.GetDeviceErrorString = VL53L0_GetDeviceErrorString,
	.GetPalErrorString = VL53L0_GetPalErrorString,
	.GetPalState = VL53L0_GetPalState,
	.SetPowerMode = VL53L0_SetPowerMode,
	.GetPowerMode = VL53L0_GetPowerMode,
	.SetOffsetCalibrationDataMicroMeter =
		VL53L0_SetOffsetCalibrationDataMicroMeter,
	.SetLinearityCorrectiveGain =
		VL53L0_SetLinearityCorrectiveGain,
	.GetLinearityCorrectiveGain =
		VL53L0_GetLinearityCorrectiveGain,
	.GetOffsetCalibrationDataMicroMeter =
		VL53L0_GetOffsetCalibrationDataMicroMeter,
	.SetGroupParamHold = VL53L0_SetGroupParamHold,
	.GetUpperLimitMilliMeter = VL53L0_GetUpperLimitMilliMeter,
	.SetDeviceAddress = VL53L0_SetDeviceAddress,
	.DataInit = VL53L0_DataInit,
	.SetTuningSettingBuffer = VL53L0_SetTuningSettingBuffer,
	.GetTuningSettingBuffer = VL53L0_GetTuningSettingBuffer,
	.StaticInit = VL53L0_StaticInit,
	.WaitDeviceBooted = VL53L0_WaitDeviceBooted,
	.ResetDevice = VL53L0_ResetDevice,
	.SetDeviceParameters = VL53L0_SetDeviceParameters,
	.SetDeviceMode = VL53L0_SetDeviceMode,
	.GetDeviceMode = VL53L0_GetDeviceMode,
	.SetHistogramMode = VL53L0_SetHistogramMode,
	.GetHistogramMode = VL53L0_GetHistogramMode,
	.SetMeasurementTimingBudgetMicroSeconds =
		VL53L0_SetMeasurementTimingBudgetMicroSeconds,
	.GetMeasurementTimingBudgetMicroSeconds =
		VL53L0_GetMeasurementTimingBudgetMicroSeconds,
	.GetVcselPulsePeriod = VL53L0_GetVcselPulsePeriod,
	.SetVcselPulsePeriod = VL53L0_SetVcselPulsePeriod,
	.SetSequenceStepEnable = VL53L0_SetSequenceStepEnable,
	.GetSequenceStepEnable = VL53L0_GetSequenceStepEnable,
	.GetSequenceStepEnables = VL53L0_GetSequenceStepEnables,
	.SetSequenceStepTimeout = VL53L0_SetSequenceStepTimeout,
	.GetSequenceStepTimeout = VL53L0_GetSequenceStepTimeout,
	.GetNumberOfSequenceSteps = VL53L0_GetNumberOfSequenceSteps,
	.GetSequenceStepsInfo = VL53L0_GetSequenceStepsInfo,
	.SetInterMeasurementPeriodMilliSeconds =
		VL53L0_SetInterMeasurementPeriodMilliSeconds,
	.GetInterMeasurementPeriodMilliSeconds =
		VL53L0_GetInterMeasurementPeriodMilliSeconds,
	.SetXTalkCompensationEnable = VL53L0_SetXTalkCompensationEnable,
	.GetXTalkCompensationEnable = VL53L0_GetXTalkCompensationEnable,
	.SetXTalkCompensationRateMegaCps =
		VL53L0_SetXTalkCompensationRateMegaCps,
	.GetXTalkCompensationRateMegaCps =
		VL53L0_GetXTalkCompensationRateMegaCps,
	.GetNumberOfLimitCheck = VL53L0_GetNumberOfLimitCheck,
	.GetLimitCheckInfo = VL53L0_GetLimitCheckInfo,
	.SetLimitCheckEnable = VL53L0_SetLimitCheckEnable,
	.GetLimitCheckEnable = VL53L0_GetLimitCheckEnable,
	.SetLimitCheckValue = VL53L0_SetLimitCheckValue,
	.GetLimitCheckValue = VL53L0_GetLimitCheckValue,
	.GetLimitCheckCurrent = VL53L0_GetLimitCheckCurrent,
	.SetWrapAroundCheckEnable = VL53L0_SetWrapAroundCheckEnable,
	.GetWrapAroundCheckEnable = VL53L0_GetWrapAroundCheckEnable,
	.PerformSingleMeasurement = VL53L0_PerformSingleMeasurement,
	.PerformRefCalibration = VL53L0_PerformRefCalibration,
	.SetRefCalibration = VL53L0_SetRefCalibration,
	.GetRefCalibration = VL53L0_GetRefCalibration,
	.PerformXTalkCalibration = VL53L0_PerformXTalkCalibration,
	.PerformOffsetCalibration = VL53L0_PerformOffsetCalibration,
	.StartMeasurement = VL53L0_StartMeasurement,
	.StopMeasurement = VL53L0_StopMeasurement,
	.GetMeasurementDataReady = VL53L0_GetMeasurementDataReady,
	.WaitDeviceReadyForNewMeasurement =
		VL53L0_WaitDeviceReadyForNewMeasurement,
	.GetRangingMeasurementData = VL53L0_GetRangingMeasurementData,
	.GetHistogramMeasurementData = VL53L0_GetHistogramMeasurementData,
	.PerformSingleRangingMeasurement =
		VL53L0_PerformSingleRangingMeasurement,
	.PerformSingleHistogramMeasurement =
		VL53L0_PerformSingleHistogramMeasurement,
	.SetNumberOfROIZones = VL53L0_SetNumberOfROIZones,
	.GetNumberOfROIZones = VL53L0_GetNumberOfROIZones,
	.GetMaxNumberOfROIZones = VL53L0_GetMaxNumberOfROIZones,
	.SetGpioConfig = VL53L0_SetGpioConfig,
	.GetGpioConfig = VL53L0_GetGpioConfig,
	.SetInterruptThresholds = VL53L0_SetInterruptThresholds,
	.GetInterruptThresholds = VL53L0_GetInterruptThresholds,
	.ClearInterruptMask = VL53L0_ClearInterruptMask,
	.GetInterruptMaskStatus = VL53L0_GetInterruptMaskStatus,
	.EnableInterruptMask = VL53L0_EnableInterruptMask,
	.SetSpadAmbientDamperThreshold = VL53L0_SetSpadAmbientDamperThreshold,
	.GetSpadAmbientDamperThreshold = VL53L0_GetSpadAmbientDamperThreshold,
	.SetSpadAmbientDamperFactor = VL53L0_SetSpadAmbientDamperFactor,
	.GetSpadAmbientDamperFactor = VL53L0_GetSpadAmbientDamperFactor,
	.PerformRefSpadManagement = VL53L0_PerformRefSpadManagement,
	.SetReferenceSpads = VL53L0_SetReferenceSpads,
	.GetReferenceSpads = VL53L0_GetReferenceSpads,

};
struct stmvl53l0_api_fn_t *papi_func_tbl;

/*
 * IOCTL definitions
 */
#define VL53L0_IOCTL_INIT			_IO('p', 0x01)
#define VL53L0_IOCTL_XTALKCALB		_IOW('p', 0x02, unsigned int)
#define VL53L0_IOCTL_OFFCALB		_IOW('p', 0x03, unsigned int)
#define VL53L0_IOCTL_STOP			_IO('p', 0x05)
#define VL53L0_IOCTL_SETXTALK		_IOW('p', 0x06, unsigned int)
#define VL53L0_IOCTL_SETOFFSET		_IOW('p', 0x07, int8_t)
#define VL53L0_IOCTL_ACTIVATE_USE_CASE	_IOW('p', 0x08, uint8_t)
#define VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE	\
			_IOW('p', 0x09, struct stmvl53l0_custom_use_case)

#define VL53L0_IOCTL_GETDATAS \
			_IOR('p', 0x0b, VL53L0_RangingMeasurementData_t)
#define VL53L0_IOCTL_REGISTER \
			_IOWR('p', 0x0c, struct stmvl53l0_register)
#define VL53L0_IOCTL_PARAMETER \
			_IOWR('p', 0x0d, struct stmvl53l0_parameter)


/* Mask fields to indicate Offset and Xtalk Comp
 * values have been set by application
 */
#define SET_OFFSET_CALIB_DATA_MICROMETER_MASK 0x1
#define SET_XTALK_COMP_RATE_MCPS_MASK         0x2

/* Macros used across different functions */
#define USE_CASE_LONG_DISTANCE	1
#define USE_CASE_HIGH_ACCURACY	2
#define USE_CASE_HIGH_SPEED		3
#define USE_CASE_CUSTOM			4

#define LONG_DISTANCE_TIMING_BUDGET			26000
#define LONG_DISTANCE_SIGNAL_RATE_LIMIT		(65536 / 10) /* 0.1  */
#define LONG_DISTANCE_SIGMA_LIMIT			(60*65536)
#define LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD	18
#define LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD	14



#define HIGH_ACCURACY_TIMING_BUDGET				200000
#define HIGH_ACCURACY_SIGNAL_RATE_LIMIT	 (25 * 65536 / 100) /*0.25*/
#define HIGH_ACCURACY_SIGMA_LIMIT				(18*65536)
#define HIGH_ACCURACY_PRE_RANGE_PULSE_PERIOD	14
#define HIGH_ACCURACY_FINAL_RANGE_PULSE_PERIOD	10



#define HIGH_SPEED_TIMING_BUDGET				20000
#define HIGH_SPEED_SIGNAL_RATE_LIMIT	(25 * 65536 / 100) /* 0.25 */
#define HIGH_SPEED_SIGMA_LIMIT					(32*65536)
#define HIGH_SPEED_PRE_RANGE_PULSE_PERIOD		14
#define HIGH_SPEED_FINAL_RANGE_PULSE_PERIOD		10





static long stmvl53l0_ioctl(struct file *file,
				unsigned int cmd, unsigned long arg);
/*static int stmvl53l0_flush(struct file *file, fl_owner_t id);*/
static int stmvl53l0_open(struct inode *inode, struct file *file);
static int stmvl53l0_init_client(struct stmvl53l0_data *data);
static int stmvl53l0_start(struct stmvl53l0_data *data, uint8_t scaling,
			init_mode_e mode);
static int stmvl53l0_stop(struct stmvl53l0_data *data);
static int stmvl53l0_config_use_case(struct stmvl53l0_data *data);

#ifdef DEBUG_TIME_LOG
static void stmvl53l0_DebugTimeGet(struct timeval *ptv)
{
	do_gettimeofday(ptv);
}

static void stmvl53l0_DebugTimeDuration(struct timeval *pstart_tv,
			struct timeval *pstop_tv)
{
	long total_sec, total_msec;

	total_sec = pstop_tv->tv_sec - pstart_tv->tv_sec;
	total_msec = (pstop_tv->tv_usec - pstart_tv->tv_usec)/1000;
	total_msec += total_sec * 1000;
	pr_err("elapsedTime:%ld\n", total_msec);
}
#endif

static void stmvl53l0_setupAPIFunctions(struct stmvl53l0_data *data)
{
	uint8_t revision = 0;
	VL53L0_DEV vl53l0_dev = data;

	/* Read Revision ID */
	VL53L0_RdByte(vl53l0_dev,
				VL53L0_REG_IDENTIFICATION_REVISION_ID,
				&revision);
	vl53l0_errmsg("read REVISION_ID: 0x%x\n", revision);
	revision = (revision & 0xF0) >> 4;
	if (revision == 1) {
		/*cut 1.1*/
		vl53l0_errmsg("to setup API cut 1.1\n");
		papi_func_tbl->GetVersion = VL53L0_GetVersion;
		papi_func_tbl->GetPalSpecVersion = VL53L0_GetPalSpecVersion;
		papi_func_tbl->GetProductRevision = VL53L0_GetProductRevision;
		papi_func_tbl->GetDeviceInfo = VL53L0_GetDeviceInfo;
		papi_func_tbl->GetDeviceErrorStatus =
			 VL53L0_GetDeviceErrorStatus;
		papi_func_tbl->GetRangeStatusString =
			 VL53L0_GetRangeStatusString;
		papi_func_tbl->GetDeviceErrorString =
			 VL53L0_GetDeviceErrorString;
		papi_func_tbl->GetPalErrorString =
			 VL53L0_GetPalErrorString;
		papi_func_tbl->GetPalState =
			 VL53L0_GetPalState;
		papi_func_tbl->SetPowerMode =
			 VL53L0_SetPowerMode;
		papi_func_tbl->GetPowerMode =
			 VL53L0_GetPowerMode;
		papi_func_tbl->SetOffsetCalibrationDataMicroMeter =
				VL53L0_SetOffsetCalibrationDataMicroMeter;
		papi_func_tbl->GetOffsetCalibrationDataMicroMeter =
				VL53L0_GetOffsetCalibrationDataMicroMeter;
		papi_func_tbl->SetLinearityCorrectiveGain =
VL53L0_SetLinearityCorrectiveGain;
		papi_func_tbl->GetLinearityCorrectiveGain =
VL53L0_GetLinearityCorrectiveGain;
		papi_func_tbl->SetGroupParamHold = VL53L0_SetGroupParamHold;
		papi_func_tbl->GetUpperLimitMilliMeter =
			 VL53L0_GetUpperLimitMilliMeter;
		papi_func_tbl->SetDeviceAddress =
			 VL53L0_SetDeviceAddress;
		papi_func_tbl->DataInit =
			 VL53L0_DataInit;
		papi_func_tbl->SetTuningSettingBuffer =
			 VL53L0_SetTuningSettingBuffer;
		papi_func_tbl->GetTuningSettingBuffer =
			 VL53L0_GetTuningSettingBuffer;
		papi_func_tbl->StaticInit =
			 VL53L0_StaticInit;
		papi_func_tbl->WaitDeviceBooted =
			 VL53L0_WaitDeviceBooted;
		papi_func_tbl->ResetDevice =
			 VL53L0_ResetDevice;
		papi_func_tbl->SetDeviceParameters =
			 VL53L0_SetDeviceParameters;
		papi_func_tbl->SetDeviceMode = VL53L0_SetDeviceMode;
		papi_func_tbl->GetDeviceMode = VL53L0_GetDeviceMode;
		papi_func_tbl->SetHistogramMode = VL53L0_SetHistogramMode;
		papi_func_tbl->GetHistogramMode = VL53L0_GetHistogramMode;
		papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds =
		 VL53L0_SetMeasurementTimingBudgetMicroSeconds;
		papi_func_tbl->GetMeasurementTimingBudgetMicroSeconds =
			 VL53L0_GetMeasurementTimingBudgetMicroSeconds;
		papi_func_tbl->GetVcselPulsePeriod = VL53L0_GetVcselPulsePeriod;
		papi_func_tbl->SetVcselPulsePeriod = VL53L0_SetVcselPulsePeriod;
		papi_func_tbl->SetSequenceStepEnable =
				 VL53L0_SetSequenceStepEnable;
		papi_func_tbl->GetSequenceStepEnable =
				 VL53L0_GetSequenceStepEnable;
		papi_func_tbl->GetSequenceStepEnables =
				 VL53L0_GetSequenceStepEnables;
		papi_func_tbl->SetSequenceStepTimeout =
				 VL53L0_SetSequenceStepTimeout;
		papi_func_tbl->GetSequenceStepTimeout =
				 VL53L0_GetSequenceStepTimeout;
		papi_func_tbl->GetNumberOfSequenceSteps =
				 VL53L0_GetNumberOfSequenceSteps;
		papi_func_tbl->GetSequenceStepsInfo =
				 VL53L0_GetSequenceStepsInfo;
		papi_func_tbl->SetInterMeasurementPeriodMilliSeconds =
				 VL53L0_SetInterMeasurementPeriodMilliSeconds;
		papi_func_tbl->GetInterMeasurementPeriodMilliSeconds =
				VL53L0_GetInterMeasurementPeriodMilliSeconds;
		papi_func_tbl->SetXTalkCompensationEnable =
				 VL53L0_SetXTalkCompensationEnable;
		papi_func_tbl->GetXTalkCompensationEnable =
				 VL53L0_GetXTalkCompensationEnable;
		papi_func_tbl->SetXTalkCompensationRateMegaCps =
				 VL53L0_SetXTalkCompensationRateMegaCps;
		papi_func_tbl->GetXTalkCompensationRateMegaCps =
				 VL53L0_GetXTalkCompensationRateMegaCps;
		papi_func_tbl->GetNumberOfLimitCheck =
				 VL53L0_GetNumberOfLimitCheck;
		papi_func_tbl->GetLimitCheckInfo =
				 VL53L0_GetLimitCheckInfo;
		papi_func_tbl->SetLimitCheckEnable =
				 VL53L0_SetLimitCheckEnable;
		papi_func_tbl->GetLimitCheckEnable =
				 VL53L0_GetLimitCheckEnable;
		papi_func_tbl->SetLimitCheckValue =
				 VL53L0_SetLimitCheckValue;
		papi_func_tbl->GetLimitCheckValue =
				 VL53L0_GetLimitCheckValue;
		papi_func_tbl->GetLimitCheckCurrent =
				 VL53L0_GetLimitCheckCurrent;
		papi_func_tbl->SetWrapAroundCheckEnable =
				 VL53L0_SetWrapAroundCheckEnable;
		papi_func_tbl->GetWrapAroundCheckEnable =
				 VL53L0_GetWrapAroundCheckEnable;
		papi_func_tbl->PerformSingleMeasurement =
				 VL53L0_PerformSingleMeasurement;
		papi_func_tbl->PerformRefCalibration =
				 VL53L0_PerformRefCalibration;
		papi_func_tbl->SetRefCalibration =
				 VL53L0_SetRefCalibration;
		papi_func_tbl->GetRefCalibration =
					 VL53L0_GetRefCalibration;
		papi_func_tbl->PerformXTalkCalibration =
				 VL53L0_PerformXTalkCalibration;
		papi_func_tbl->PerformOffsetCalibration =
				 VL53L0_PerformOffsetCalibration;
		papi_func_tbl->StartMeasurement =
				 VL53L0_StartMeasurement;
		papi_func_tbl->StopMeasurement =
				 VL53L0_StopMeasurement;
		papi_func_tbl->GetMeasurementDataReady =
				 VL53L0_GetMeasurementDataReady;
		papi_func_tbl->WaitDeviceReadyForNewMeasurement =
			VL53L0_WaitDeviceReadyForNewMeasurement;
		papi_func_tbl->GetRangingMeasurementData =
			VL53L0_GetRangingMeasurementData;
		papi_func_tbl->GetHistogramMeasurementData =
			VL53L0_GetHistogramMeasurementData;
		papi_func_tbl->PerformSingleRangingMeasurement =
			VL53L0_PerformSingleRangingMeasurement;
		papi_func_tbl->PerformSingleHistogramMeasurement =
			VL53L0_PerformSingleHistogramMeasurement;
		papi_func_tbl->SetNumberOfROIZones =
			 VL53L0_SetNumberOfROIZones;
		papi_func_tbl->GetNumberOfROIZones =
			VL53L0_GetNumberOfROIZones;
		papi_func_tbl->GetMaxNumberOfROIZones =
			 VL53L0_GetMaxNumberOfROIZones;
		papi_func_tbl->SetGpioConfig =
			 VL53L0_SetGpioConfig;
		papi_func_tbl->GetGpioConfig =
			 VL53L0_GetGpioConfig;
		papi_func_tbl->SetInterruptThresholds =
			 VL53L0_SetInterruptThresholds;
		papi_func_tbl->GetInterruptThresholds =
			 VL53L0_GetInterruptThresholds;
		papi_func_tbl->ClearInterruptMask =
			 VL53L0_ClearInterruptMask;
		papi_func_tbl->GetInterruptMaskStatus =
				 VL53L0_GetInterruptMaskStatus;
		papi_func_tbl->EnableInterruptMask = VL53L0_EnableInterruptMask;
		papi_func_tbl->SetSpadAmbientDamperThreshold =
			VL53L0_SetSpadAmbientDamperThreshold;
		papi_func_tbl->GetSpadAmbientDamperThreshold =
			VL53L0_GetSpadAmbientDamperThreshold;
		papi_func_tbl->SetSpadAmbientDamperFactor =
			VL53L0_SetSpadAmbientDamperFactor;
		papi_func_tbl->GetSpadAmbientDamperFactor =
			VL53L0_GetSpadAmbientDamperFactor;
		papi_func_tbl->PerformRefSpadManagement =
			 VL53L0_PerformRefSpadManagement;
		papi_func_tbl->SetReferenceSpads = VL53L0_SetReferenceSpads;
		papi_func_tbl->GetReferenceSpads = VL53L0_GetReferenceSpads;
	} else if (revision == 0) {
		/*cut 1.0*/
		vl53l0_errmsg("to setup API cut 1.0\n");
		papi_func_tbl->GetVersion = VL53L010_GetVersion;
		papi_func_tbl->GetPalSpecVersion = VL53L010_GetPalSpecVersion;
		/* papi_func_tbl->GetProductRevision = NULL;*/
		papi_func_tbl->GetDeviceInfo = VL53L010_GetDeviceInfo;
		papi_func_tbl->GetDeviceErrorStatus =
					 VL53L010_GetDeviceErrorStatus;
		papi_func_tbl->GetDeviceErrorString =
					 VL53L010_GetDeviceErrorString;
		papi_func_tbl->GetPalErrorString =
					 VL53L010_GetPalErrorString;
		papi_func_tbl->GetPalState =
					 VL53L010_GetPalState;
		papi_func_tbl->SetPowerMode =
					 VL53L010_SetPowerMode;
		papi_func_tbl->GetPowerMode =
					 VL53L010_GetPowerMode;
		papi_func_tbl->SetOffsetCalibrationDataMicroMeter =
			VL53L010_SetOffsetCalibrationDataMicroMeter;
		papi_func_tbl->GetOffsetCalibrationDataMicroMeter =
			VL53L010_GetOffsetCalibrationDataMicroMeter;
		papi_func_tbl->SetGroupParamHold =
					 VL53L010_SetGroupParamHold;
		papi_func_tbl->GetUpperLimitMilliMeter =
					 VL53L010_GetUpperLimitMilliMeter;
		papi_func_tbl->SetDeviceAddress =
					 VL53L010_SetDeviceAddress;
		papi_func_tbl->DataInit = VL53L010_DataInit;
		/*
		 *papi_func_tbl->SetTuningSettingBuffer = NULL;
		 *papi_func_tbl->GetTuningSettingBuffer = NULL;
		*/
		papi_func_tbl->StaticInit = VL53L010_StaticInit;
		papi_func_tbl->WaitDeviceBooted = VL53L010_WaitDeviceBooted;
		papi_func_tbl->ResetDevice = VL53L010_ResetDevice;
		papi_func_tbl->SetDeviceParameters =
					 VL53L010_SetDeviceParameters;
		papi_func_tbl->SetDeviceMode = VL53L010_SetDeviceMode;
		papi_func_tbl->GetDeviceMode = VL53L010_GetDeviceMode;
		papi_func_tbl->SetHistogramMode = VL53L010_SetHistogramMode;
		papi_func_tbl->GetHistogramMode = VL53L010_GetHistogramMode;
		papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds =
			VL53L010_SetMeasurementTimingBudgetMicroSeconds;
		papi_func_tbl->GetMeasurementTimingBudgetMicroSeconds =
			VL53L010_GetMeasurementTimingBudgetMicroSeconds;
		/*
		 * papi_func_tbl->GetVcselPulsePeriod = NULL;
		 *papi_func_tbl->SetVcselPulsePeriod = NULL;
		 *papi_func_tbl->SetSequenceStepEnable = NULL;
		 *papi_func_tbl->GetSequenceStepEnable = NULL;
		 *papi_func_tbl->GetSequenceStepEnables = NULL;
		 *papi_func_tbl->SetSequenceStepTimeout = NULL;
		 *papi_func_tbl->GetSequenceStepTimeout = NULL;
		 *papi_func_tbl->GetNumberOfSequenceSteps =NULL;
		 *papi_func_tbl->GetSequenceStepsInfo = NULL;
		 */
		papi_func_tbl->SetInterMeasurementPeriodMilliSeconds =
			VL53L010_SetInterMeasurementPeriodMilliSeconds;
		papi_func_tbl->GetInterMeasurementPeriodMilliSeconds =
			VL53L010_GetInterMeasurementPeriodMilliSeconds;
		papi_func_tbl->SetXTalkCompensationEnable =
			VL53L010_SetXTalkCompensationEnable;
		papi_func_tbl->GetXTalkCompensationEnable =
			VL53L010_GetXTalkCompensationEnable;
		papi_func_tbl->SetXTalkCompensationRateMegaCps =
			VL53L010_SetXTalkCompensationRateMegaCps;
		papi_func_tbl->GetXTalkCompensationRateMegaCps =
			VL53L010_GetXTalkCompensationRateMegaCps;
		papi_func_tbl->GetNumberOfLimitCheck =
				 VL53L010_GetNumberOfLimitCheck;
		papi_func_tbl->GetLimitCheckInfo = VL53L010_GetLimitCheckInfo;
		papi_func_tbl->SetLimitCheckEnable =
					 VL53L010_SetLimitCheckEnable;
		papi_func_tbl->GetLimitCheckEnable =
					 VL53L010_GetLimitCheckEnable;
		papi_func_tbl->SetLimitCheckValue =
					 VL53L010_SetLimitCheckValue;
		papi_func_tbl->GetLimitCheckValue =
					 VL53L010_GetLimitCheckValue;
		papi_func_tbl->GetLimitCheckCurrent =
					 VL53L010_GetLimitCheckCurrent;
		papi_func_tbl->SetWrapAroundCheckEnable =
			VL53L010_SetWrapAroundCheckEnable;
		papi_func_tbl->GetWrapAroundCheckEnable =
			VL53L010_GetWrapAroundCheckEnable;
		papi_func_tbl->PerformSingleMeasurement =
			VL53L010_PerformSingleMeasurement;
		/*papi_func_tbl->PerformRefCalibration =
		 * VL53L010_PerformRefCalibration;
		 */
		papi_func_tbl->PerformRefCalibration = NULL;
		papi_func_tbl->SetRefCalibration = NULL;
		papi_func_tbl->GetRefCalibration = NULL;
		papi_func_tbl->PerformXTalkCalibration =
				 VL53L010_PerformXTalkCalibration;
		papi_func_tbl->PerformOffsetCalibration =
			VL53L010_PerformOffsetCalibration;
		papi_func_tbl->StartMeasurement = VL53L010_StartMeasurement;
		papi_func_tbl->StopMeasurement = VL53L010_StopMeasurement;
		papi_func_tbl->GetMeasurementDataReady =
					 VL53L010_GetMeasurementDataReady;
		papi_func_tbl->WaitDeviceReadyForNewMeasurement =
			VL53L010_WaitDeviceReadyForNewMeasurement;
		papi_func_tbl->GetRangingMeasurementData =
			VL53L010_GetRangingMeasurementData;
		papi_func_tbl->GetHistogramMeasurementData =
			VL53L010_GetHistogramMeasurementData;
		papi_func_tbl->PerformSingleRangingMeasurement =
			VL53L010_PerformSingleRangingMeasurement;
		papi_func_tbl->PerformSingleHistogramMeasurement =
			VL53L010_PerformSingleHistogramMeasurement;
		papi_func_tbl->SetNumberOfROIZones =
				 VL53L010_SetNumberOfROIZones;
		papi_func_tbl->GetNumberOfROIZones =
				 VL53L010_GetNumberOfROIZones;
		papi_func_tbl->GetMaxNumberOfROIZones =
				 VL53L010_GetMaxNumberOfROIZones;
		papi_func_tbl->SetGpioConfig = VL53L010_SetGpioConfig;
		papi_func_tbl->GetGpioConfig = VL53L010_GetGpioConfig;
		papi_func_tbl->SetInterruptThresholds =
				 VL53L010_SetInterruptThresholds;
		papi_func_tbl->GetInterruptThresholds =
				 VL53L010_GetInterruptThresholds;
		papi_func_tbl->ClearInterruptMask =
				 VL53L010_ClearInterruptMask;
		papi_func_tbl->GetInterruptMaskStatus =
				 VL53L010_GetInterruptMaskStatus;
		papi_func_tbl->EnableInterruptMask =
				 VL53L010_EnableInterruptMask;
		papi_func_tbl->SetSpadAmbientDamperThreshold =
			VL53L010_SetSpadAmbientDamperThreshold;
		papi_func_tbl->GetSpadAmbientDamperThreshold =
			VL53L010_GetSpadAmbientDamperThreshold;
		papi_func_tbl->SetSpadAmbientDamperFactor =
			VL53L010_SetSpadAmbientDamperFactor;
		papi_func_tbl->GetSpadAmbientDamperFactor =
			VL53L010_GetSpadAmbientDamperFactor;
		papi_func_tbl->PerformRefSpadManagement = NULL;
		papi_func_tbl->SetReferenceSpads = NULL;
		papi_func_tbl->GetReferenceSpads = NULL;
	}

}

static void stmvl53l0_ps_read_measurement(struct stmvl53l0_data *data)
{
	struct timeval tv;
	VL53L0_DEV vl53l0_dev = data;
	VL53L0_Error Status = VL53L0_ERROR_NONE;
	FixPoint1616_t LimitCheckCurrent;

	do_gettimeofday(&tv);

	data->ps_data = data->rangeData.RangeMilliMeter;
	input_report_abs(data->input_dev_ps, ABS_DISTANCE,
		(int)(data->ps_data + 5) / 10);
	input_report_abs(data->input_dev_ps, ABS_HAT0X, tv.tv_sec);
	input_report_abs(data->input_dev_ps, ABS_HAT0Y, tv.tv_usec);
	input_report_abs(data->input_dev_ps, ABS_HAT1X,
		data->rangeData.RangeMilliMeter);
	input_report_abs(data->input_dev_ps, ABS_HAT1Y,
		data->rangeData.RangeStatus);
	input_report_abs(data->input_dev_ps, ABS_HAT2X,
		data->rangeData.SignalRateRtnMegaCps);
	input_report_abs(data->input_dev_ps, ABS_HAT2Y,
		data->rangeData.AmbientRateRtnMegaCps);
	input_report_abs(data->input_dev_ps, ABS_HAT3X,
		data->rangeData.MeasurementTimeUsec);
	input_report_abs(data->input_dev_ps, ABS_HAT3Y,
		data->rangeData.RangeDMaxMilliMeter);
	Status = papi_func_tbl->GetLimitCheckCurrent(vl53l0_dev,
					VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE,
					&LimitCheckCurrent);
	if (Status == VL53L0_ERROR_NONE) {
		input_report_abs(data->input_dev_ps, ABS_WHEEL,
				LimitCheckCurrent);
	}
	input_report_abs(data->input_dev_ps, ABS_PRESSURE,
		data->rangeData.EffectiveSpadRtnCount);
	input_sync(data->input_dev_ps);

	if (data->enableDebug)
		vl53l0_errmsg(
"range:%d, RtnRateMcps:%d,err:0x%x,Dmax:%d,rtnambr:%d,time:%d,Spad:%d,SigmaLimit:%d\n",
			data->rangeData.RangeMilliMeter,
			data->rangeData.SignalRateRtnMegaCps,
			data->rangeData.RangeStatus,
			data->rangeData.RangeDMaxMilliMeter,
			data->rangeData.AmbientRateRtnMegaCps,
			data->rangeData.MeasurementTimeUsec,
			data->rangeData.EffectiveSpadRtnCount,
			LimitCheckCurrent
			);


}

static void stmvl53l0_cancel_handler(struct stmvl53l0_data *data)
{
	unsigned long flags;
	bool ret;

	spin_lock_irqsave(&data->update_lock.wait_lock, flags);
	/*
	 * If work is already scheduled then subsequent schedules will not
	 * change the scheduled time that's why we have to cancel it first.
	 */
	ret = cancel_delayed_work(&data->dwork);
	if (ret == 0)
		vl53l0_errmsg("cancel_delayed_work return FALSE\n");

	spin_unlock_irqrestore(&data->update_lock.wait_lock, flags);

}

void stmvl53l0_schedule_handler(struct stmvl53l0_data *data)
{
	unsigned long flags;

	spin_lock_irqsave(&data->update_lock.wait_lock, flags);
	/*
	 * If work is already scheduled then subsequent schedules will not
	 * change the scheduled time that's why we have to cancel it first.
	 */
	cancel_delayed_work(&data->dwork);
	schedule_delayed_work(&data->dwork, 0);
	spin_unlock_irqrestore(&data->update_lock.wait_lock, flags);

}


#ifdef USE_INT
static irqreturn_t stmvl53l0_interrupt_handler(int vec, void *info)
{

	struct stmvl53l0_data *data = (struct stmvl53l0_data *)info;

	if (data->irq == vec) {
		data->interrupt_received = 1;
		schedule_delayed_work(&data->dwork, 0);
	}
	return IRQ_HANDLED;
}
#else
/* Flag used to exit the thread when kthread_stop() is invoked */
static int poll_thread_exit;
int stmvl53l0_poll_thread(void *data)
{
	VL53L0_DEV vl53l0_dev = data;
	VL53L0_Error Status = VL53L0_ERROR_NONE;
	uint32_t sleep_time = 0;
	uint32_t interruptStatus = 0;

	pr_err("%s(%d) : Starting Polling thread\n", __func__, __LINE__);

	while (!kthread_should_stop()) {
		/* Check if enable_ps_sensor is true or exit request is made.
		 * If not block
		 */
		wait_event(vl53l0_dev->poll_thread_wq,
			(vl53l0_dev->enable_ps_sensor || poll_thread_exit));
		if (poll_thread_exit) {
			pr_err(
		"%s(%d) : Exiting the poll thread\n", __func__, __LINE__);
			break;
		}

		mutex_lock(&vl53l0_dev->work_mutex);

		sleep_time = vl53l0_dev->delay_ms;
		Status = VL53L0_GetInterruptMaskStatus(vl53l0_dev,
					 &interruptStatus);
		if (Status == VL53L0_ERROR_NONE &&
			interruptStatus &&
			interruptStatus != vl53l0_dev->interruptStatus) {
			vl53l0_dev->interruptStatus = interruptStatus;
			vl53l0_dev->noInterruptCount = 0;
			stmvl53l0_schedule_handler(vl53l0_dev);
		} else {
			vl53l0_dev->noInterruptCount++;
		}

		/*Force Clear interrupt mask and restart if
		 *no interrupt after twice the timingBudget
		 */
		if ((vl53l0_dev->noInterruptCount * vl53l0_dev->delay_ms) >
			 (vl53l0_dev->timingBudget * 2)) {
			pr_err("No interrupt after (%u) msec(TimingBudget = %u) . Clear Interrupt Mask and restart\n",
				(vl53l0_dev->noInterruptCount *
					 vl53l0_dev->delay_ms),
					vl53l0_dev->timingBudget);
			Status = papi_func_tbl->ClearInterruptMask(vl53l0_dev,
					 0);
			if (vl53l0_dev->deviceMode ==
			 VL53L0_DEVICEMODE_SINGLE_RANGING) {
				Status = papi_func_tbl->StartMeasurement(
							vl53l0_dev);
				if (Status != VL53L0_ERROR_NONE) {
					pr_err("%s(%d) : Status = %d\n",
						 __func__, __LINE__, Status);
				}
			}
		}
		mutex_unlock(&vl53l0_dev->work_mutex);
		/* Sleep for delay_ms milliseconds */
		msleep(sleep_time);
	}

	return 0;
}
#endif

/* work handler */
static void stmvl53l0_work_handler(struct work_struct *work)
{
	struct stmvl53l0_data *data = container_of(work, struct stmvl53l0_data,
				dwork.work);
	VL53L0_DEV vl53l0_dev = data;

	VL53L0_Error Status = VL53L0_ERROR_NONE;

	mutex_lock(&data->work_mutex);
	/* vl53l0_dbgmsg("Enter\n"); */

	if (pmodule_func_tbl->query_power_status(data->client_object) == 0) {
		if (data->enable_ps_sensor == 1) {
			stmvl53l0_stop(data);
			data->enable_ps_sensor = 0;
		}
	}

	if (vl53l0_dev->enable_ps_sensor == 1) {
#ifdef DEBUG_TIME_LOG
		stmvl53l0_DebugTimeGet(&stop_tv);
		stmvl53l0_DebugTimeDuration(&start_tv, &stop_tv);
#endif
		/* Check if ISR has scheduled this function */
		if (vl53l0_dev->interrupt_received == 1) {
			Status = papi_func_tbl->GetInterruptMaskStatus(
					vl53l0_dev,
					&vl53l0_dev->interruptStatus);
			if (Status != VL53L0_ERROR_NONE)
				pr_err("%s(%d) : Status = %d\n",
						 __func__, __LINE__, Status);
			vl53l0_dev->interrupt_received = 0;
		}
		if (data->enableDebug)
			pr_err("interruptStatus:0x%x, interrupt_received:%d\n",
				vl53l0_dev->interruptStatus,
				vl53l0_dev->interrupt_received);

		if (vl53l0_dev->interruptStatus == vl53l0_dev->gpio_function) {
			Status =
				papi_func_tbl->GetRangingMeasurementData(
						vl53l0_dev,
						&(data->rangeData));
			/* to push the measurement */
			if (Status == VL53L0_ERROR_NONE)
				stmvl53l0_ps_read_measurement(data);
			else
				pr_err("%s(%d) : Status = %d\n",
						__func__, __LINE__, Status);

			if (data->enableDebug)
				pr_err("Measured range:%d\n",
					data->rangeData.RangeMilliMeter);

			Status = papi_func_tbl->ClearInterruptMask(
					vl53l0_dev, 0);
			if (Status != VL53L0_ERROR_NONE) {
				pr_err("%s(%d) : Status = %d\n",
						__func__, __LINE__, Status);
			}

			if (data->deviceMode ==
					VL53L0_DEVICEMODE_SINGLE_RANGING) {
				/* Before restarting measurement
				 *  check if use case needs to be changed
				 */

				if (data->updateUseCase) {
					Status =
						stmvl53l0_config_use_case(data);
					if (Status !=
							VL53L0_ERROR_NONE) {
						vl53l0_errmsg(
					"Failed to configure Use case = %u\n",
							vl53l0_dev->useCase);
					} else {
						data->updateUseCase = 0;
					}
				}
				Status =
					papi_func_tbl->StartMeasurement(
							vl53l0_dev);
			}
		}
#ifdef DEBUG_TIME_LOG
		stmvl53l0_DebugTimeGet(&start_tv);
#endif

		}


	vl53l0_dev->interruptStatus = 0;

	mutex_unlock(&data->work_mutex);

}


/*
 * SysFS support
 */
static ssize_t stmvl53l0_show_enable_ps_sensor(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	return snprintf(buf, 5, "%d\n", data->enable_ps_sensor);
}

static ssize_t stmvl53l0_store_enable_ps_sensor(struct device *dev,
				struct device_attribute *attr, const char *buf,
				size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);
	unsigned long val = 0;

	int ret = kstrtoul(buf, 10, &val);

	if (ret != 0)
		return ret;

	if ((val != 0) && (val != 1)) {
		vl53l0_errmsg("store unvalid value = %lu\n", val);
		return count;
	}
	mutex_lock(&data->work_mutex);
	vl53l0_dbgmsg("Enter, enable_ps_sensor flag:%d\n",
		data->enable_ps_sensor);
	vl53l0_dbgmsg("enable ps senosr ( %ld)\n", val);

	if (val == 1) {
		/* turn on tof sensor */
		if (data->enable_ps_sensor == 0) {
			/* to start */
			stmvl53l0_start(data, 3, NORMAL_MODE);
		} else {
			vl53l0_errmsg("Already enabled. Skip !");
		}
	} else {
		/* turn off tof sensor */
		if (data->enable_ps_sensor == 1) {
			data->enable_ps_sensor = 0;
			/* to stop */
			stmvl53l0_stop(data);
		}
	}
	vl53l0_dbgmsg("End\n");
	mutex_unlock(&data->work_mutex);

	return count;
}

static DEVICE_ATTR(enable_ps_sensor, 0664/*S_IWUGO | S_IRUGO*/,
				   stmvl53l0_show_enable_ps_sensor,
					stmvl53l0_store_enable_ps_sensor);

static ssize_t stmvl53l0_show_enable_debug(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	return snprintf(buf, 5, "%d\n", data->enableDebug);
}

/* for debug */
static ssize_t stmvl53l0_store_enable_debug(struct device *dev,
					struct device_attribute *attr, const
					char *buf, size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);
	unsigned long on = 0;

	int ret = kstrtoul(buf, 10, &on);

	if (ret != 0)
		return ret;

	if ((on != 0) &&  (on != 1)) {
		vl53l0_errmsg("set debug=%lu\n", on);
		return count;
	}
	data->enableDebug = on;

	return count;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(enable_debug, 0660/*S_IWUSR | S_IRUGO*/,
				   stmvl53l0_show_enable_debug,
					stmvl53l0_store_enable_debug);

static ssize_t stmvl53l0_show_set_delay_ms(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	return snprintf(buf, 5, "%d\n", data->delay_ms);
}

/* for work handler scheduler time */
static ssize_t stmvl53l0_store_set_delay_ms(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);
	unsigned long delay_ms = 0;

	int ret = kstrtoul(buf, 10, &delay_ms);

	if (ret != 0)
		return ret;
	if (delay_ms == 0) {
		vl53l0_errmsg("set delay_ms=%lu\n", delay_ms);
		return count;
	}
	mutex_lock(&data->work_mutex);
	data->delay_ms = delay_ms;
	mutex_unlock(&data->work_mutex);

	return count;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(set_delay_ms, 0660/*S_IWUGO | S_IRUGO*/,
				   stmvl53l0_show_set_delay_ms,
					stmvl53l0_store_set_delay_ms);

/* Timing Budget */
static ssize_t stmvl53l0_show_timing_budget(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	return snprintf(buf, 10, "%d\n", data->timingBudget);
}

static ssize_t stmvl53l0_store_set_timing_budget(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);
	unsigned long timingBudget = 0;
	int ret = kstrtoul(buf, 10, &timingBudget);

	if (ret != 0)
		return ret;
	if (timingBudget == 0) {
		vl53l0_errmsg("set timingBudget=%lu\n", timingBudget);
		return count;
	}
	mutex_lock(&data->work_mutex);
	data->timingBudget = timingBudget;
	mutex_unlock(&data->work_mutex);

	return count;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(set_timing_budget, 0660/*S_IWUGO | S_IRUGO*/,
				   stmvl53l0_show_timing_budget,
					stmvl53l0_store_set_timing_budget);


/* Use case  */
static ssize_t stmvl53l0_show_use_case(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	switch (data->useCase) {
	case USE_CASE_LONG_DISTANCE:
		return snprintf(buf, 20, "Long Distance\n");
	case USE_CASE_HIGH_ACCURACY:
		return snprintf(buf, 20, "High Accuracy\n");
	case USE_CASE_HIGH_SPEED:
		return snprintf(buf, 20, "High Speed\n");
	default:
		break;
	}

	return snprintf(buf, 25, "Unknown use case\n");
}

static ssize_t stmvl53l0_store_set_use_case(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);
	unsigned long useCase = 0;
	int ret = kstrtoul(buf, 10, &useCase);

	if (ret != 0)
		return ret;


	mutex_lock(&data->work_mutex);

	if (useCase == USE_CASE_LONG_DISTANCE) {
		data->timingBudget = LONG_DISTANCE_TIMING_BUDGET;
	} else if (useCase == USE_CASE_HIGH_SPEED) {
		data->timingBudget = HIGH_SPEED_TIMING_BUDGET;
	} else if (useCase == USE_CASE_HIGH_ACCURACY) {
		data->timingBudget = HIGH_ACCURACY_TIMING_BUDGET;
	} else {
		count = -EINVAL;
		mutex_unlock(&data->work_mutex);
		return count;
	}

	data->useCase = useCase;
	mutex_unlock(&data->work_mutex);

	return count;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(set_use_case, 0660/*S_IWUGO | S_IRUGO*/,
				   stmvl53l0_show_use_case,
					stmvl53l0_store_set_use_case);


/* Get Current configuration info */
static ssize_t stmvl53l0_show_current_configuration(struct device *dev,
				struct device_attribute *attr, char *buf)
{
	struct stmvl53l0_data *vl53l0_dev = dev_get_drvdata(dev);
	VL53L0_Error Status = VL53L0_ERROR_NONE;
	int ret = -1;
	FixPoint1616_t	LimitValue = 0;
	uint8_t LimitEnable = 0;
	uint32_t refSpadCount = 0;
	uint8_t isApertureSpads = 0;
	uint8_t VhvSettings = 0;
	uint8_t PhaseCal = 0;
	uint8_t pulsePeriod = 0;
	uint32_t timingBudget = 0;
	int32_t offsetCalibrationDataMicroMeter = -1;
	uint8_t XTalkCompensationEnable = 0;
	FixPoint1616_t xTalkCompensationRateMegaCps = 0;


	ret = scnprintf(buf, PAGE_SIZE, "VL53L0 current configuration:\n");

	mutex_lock(&vl53l0_dev->work_mutex);
	pr_err("Driver Config:UseCase:%d, offsetCalDistance:%u,xtalkCalDistance:%u,setCalibratedValue:0x%X\n",
			vl53l0_dev->useCase, vl53l0_dev->offsetCalDistance,
			vl53l0_dev->xtalkCalDistance,
			vl53l0_dev->setCalibratedValue);

	ret += scnprintf(buf + ret, PAGE_SIZE - ret,
			"Driver Config:UseCase:%d, offsetCalDistance:%u,xtalkCalDistance:%u,setCalibratedValue:0x%X\n",
			vl53l0_dev->useCase,
			vl53l0_dev->offsetCalDistance,
			vl53l0_dev->xtalkCalDistance,
			vl53l0_dev->setCalibratedValue);

	if (vl53l0_dev->useCase == USE_CASE_CUSTOM) {
		pr_err("CustomUseCase : Sigma=%u :Signal=%u: Pre=%u :Final=%u\n",
					vl53l0_dev->sigmaLimit,
					vl53l0_dev->signalRateLimit,
					vl53l0_dev->preRangePulsePeriod,
					vl53l0_dev->finalRangePulsePeriod);

		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
				"CustomUseCase : Sigma=%u :Signal=%u: Pre=%u :Final=%u\n",
					vl53l0_dev->sigmaLimit,
					vl53l0_dev->signalRateLimit,
					vl53l0_dev->preRangePulsePeriod,
					vl53l0_dev->finalRangePulsePeriod);
	}


	Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev,
					VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE,
					&LimitValue);

	if (Status == VL53L0_ERROR_NONE) {
		Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev,
					VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE,
					&LimitEnable);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("Get LimitCheckValue SIGMA_FINAL_RANGE as:%d,Enable:%d\n",
				(LimitValue>>16), LimitEnable);
		ret	+= scnprintf(buf + ret, PAGE_SIZE - ret,
					 "Sigma Limit:%u, Enable:%u\n",
					(LimitValue>>16),
					 LimitEnable);
		Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
				&LimitValue);
		Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
				&LimitEnable);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("Get LimitCheckValue SIGNAL_FINAL_RANGE as:%d(Fix1616),Enable:%d\n",
				(LimitValue), LimitEnable);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
							 "SIGNAL Limit:%u, Enable:%u\n",
							LimitValue,
							LimitEnable);

		Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_REF_CLIP,
				&LimitValue);
		Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_REF_CLIP,
				&LimitEnable);

	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("Get LimitCheckValue SIGNAL_REF_CLIP as:%d(fix1616),Enable:%d\n",
				(LimitValue), LimitEnable);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
					"RefClipLimit:%u, Enable:%u\n",
					LimitValue,
					LimitEnable);

		Status = papi_func_tbl->GetLimitCheckValue(vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
				&LimitValue);
		Status = papi_func_tbl->GetLimitCheckEnable(vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
				&LimitEnable);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err(
	"Get LimitCheckValue RANGE_IGNORE_THRESHOLDas:%d(fix1616),Enable:%d\n",
				(LimitValue),
				LimitEnable);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
				 "RngIgnoreThresh:%u, Enable:%u\n",
				LimitValue,
				LimitEnable);

		Status = papi_func_tbl->GetRefCalibration(vl53l0_dev,
				&VhvSettings, &PhaseCal); /* Ref calibration */


	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("GetRefCalibration - Vhv = %u, PhaseCal = %u\n",
						VhvSettings, PhaseCal);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
					"Vhv:%u, PhCal:%u\n",
					VhvSettings,
					PhaseCal);

		/* Ref Spad Management */
		Status = papi_func_tbl->GetReferenceSpads(vl53l0_dev,
			&refSpadCount, &isApertureSpads);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("GetSpads - Count = %u, IsAperture = %u\n",
						refSpadCount, isApertureSpads);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
						"SpadCount:%u, IsAperture:%u\n",
						refSpadCount,
						isApertureSpads);

		Status = papi_func_tbl->GetMeasurementTimingBudgetMicroSeconds(
					vl53l0_dev,
					&timingBudget);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("TimingBudget = %u\n", timingBudget);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "TimBudget:%u\n",
								timingBudget);
		Status = papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev,
					VL53L0_VCSEL_PERIOD_PRE_RANGE,
					&pulsePeriod);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("GetVcselPulsePeriod - PRE_RANGE = %u\n",
						pulsePeriod);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
					 "PulsePreRange:%u\n",
					pulsePeriod);

		Status = papi_func_tbl->GetVcselPulsePeriod(vl53l0_dev,
					VL53L0_VCSEL_PERIOD_FINAL_RANGE,
					&pulsePeriod);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("GetVcselPulsePeriod - FINAL_RANGE = %u\n",
						pulsePeriod);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret,
				 "PulseFinalRange:%u\n",
				pulsePeriod);

		Status = papi_func_tbl->GetOffsetCalibrationDataMicroMeter(
				vl53l0_dev,
				&offsetCalibrationDataMicroMeter);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("OffsetCalibrationDataMicroMeter = %d\n",
				 offsetCalibrationDataMicroMeter);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "Offset:%d\n",
					offsetCalibrationDataMicroMeter);

		Status = papi_func_tbl->GetXTalkCompensationEnable(vl53l0_dev,
					&XTalkCompensationEnable);
	}

	if (Status == VL53L0_ERROR_NONE) {
		pr_err("Xtalk Enable = %u\n", XTalkCompensationEnable);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "XtalkEnable:%u\n",
					XTalkCompensationEnable);

		Status = papi_func_tbl->GetXTalkCompensationRateMegaCps(
					vl53l0_dev,
					&xTalkCompensationRateMegaCps);
	}


	if (Status == VL53L0_ERROR_NONE) {
		pr_err("XtalkComp MCPS = %u\n", xTalkCompensationRateMegaCps);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "XtalkMcps:%u\n",
					xTalkCompensationRateMegaCps);

	} else {
		pr_err("Error = %d\n", Status);
		ret += scnprintf(buf + ret, PAGE_SIZE - ret, "Error:%d\n",
					Status);

	}

	pr_err("Total Bytes returned = %d\n", ret);

	mutex_unlock(&vl53l0_dev->work_mutex);


	return ret;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(show_current_configuration, 0660/*S_IWUGO | S_IRUGO*/,
				   stmvl53l0_show_current_configuration,
					NULL);
/* for work handler scheduler time */
static ssize_t stmvl53l0_do_flush(struct device *dev,
					struct device_attribute *attr,
					const char *buf, size_t count)
{
	struct stmvl53l0_data *data = dev_get_drvdata(dev);

	int ret = 0;
	/* Revisit : Avoid lock if it takes too long time */
	mutex_lock(&data->work_mutex);

	vl53l0_dbgmsg("Starting timer to fire in 1ms (%ld)\n", jiffies);
	ret = mod_timer(&data->timer, jiffies + msecs_to_jiffies(1));
	if (ret)
		pr_err("Error from mod_timer = %d\n", ret);

	mutex_unlock(&data->work_mutex);
	return count;
}

/* DEVICE_ATTR(name,mode,show,store) */
static DEVICE_ATTR(do_flush, 0660/*S_IWUGO | S_IRUGO*/,
				   NULL,
					stmvl53l0_do_flush);
static struct attribute *stmvl53l0_attributes[] = {
	&dev_attr_enable_ps_sensor.attr,
	&dev_attr_enable_debug.attr,
	&dev_attr_set_delay_ms.attr,
	&dev_attr_set_timing_budget.attr,
	&dev_attr_set_use_case.attr,
	&dev_attr_do_flush.attr,
	&dev_attr_show_current_configuration.attr,
	NULL
};


static const struct attribute_group stmvl53l0_attr_group = {
	.attrs = stmvl53l0_attributes,
};

/*
 * misc device file operation functions
 */
static int stmvl53l0_ioctl_handler(struct file *file,
			unsigned int cmd, unsigned long arg,
			void __user *p)
{
	int rc = 0;
	unsigned int xtalkint = 0;
	unsigned int targetDistance = 0;
	int8_t offsetint = 0;
	uint8_t useCase = 0;
	struct stmvl53l0_custom_use_case customUseCase;
	struct stmvl53l0_data *data =
			container_of(file->private_data,
				struct stmvl53l0_data, miscdev);
	struct stmvl53l0_register reg;
	struct stmvl53l0_parameter parameter;
	VL53L0_DEV vl53l0_dev = data;
	VL53L0_DeviceModes deviceMode;
	uint8_t page_num = 0;
	VL53L0_Error Status = VL53L0_ERROR_NONE;

	if (!data)
		return -EINVAL;

	vl53l0_dbgmsg("Enter enable_ps_sensor:%d\n", data->enable_ps_sensor);
	switch (cmd) {
	/* enable */
	case VL53L0_IOCTL_INIT:
		vl53l0_dbgmsg("VL53L0_IOCTL_INIT\n");
		/* turn on tof sensor only if it's not enabled by other
		 * client
		 */
		if (data->enable_ps_sensor == 0) {
			/* to start */
			stmvl53l0_start(data, 3, NORMAL_MODE);
		} else
			rc = -EINVAL;
		break;
	/* crosstalk calibration */
	case VL53L0_IOCTL_XTALKCALB:
		vl53l0_dbgmsg("VL53L0_IOCTL_XTALKCALB\n");
		data->xtalkCalDistance = 100;
		if (copy_from_user(&targetDistance, (unsigned int *)p,
			sizeof(unsigned int))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		data->xtalkCalDistance = targetDistance;

		/* turn on tof sensor only if it's not enabled by other
		 * client
		 */
		if (data->enable_ps_sensor == 0) {
			/* to start */
			stmvl53l0_start(data, 3, XTALKCALIB_MODE);
		} else
			rc = -EINVAL;
		break;
	/* set up Xtalk value */
	case VL53L0_IOCTL_SETXTALK:
		vl53l0_dbgmsg("VL53L0_IOCTL_SETXTALK\n");
		if (copy_from_user(&xtalkint, (unsigned int *)p,
			sizeof(unsigned int))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		vl53l0_dbgmsg("SETXTALK as 0x%x\n", xtalkint);

		/* later
		* SetXTalkCompensationRate(vl53l0_dev, xtalkint);
		*/
		break;
	/* offset calibration */
	case VL53L0_IOCTL_OFFCALB:
		vl53l0_dbgmsg("VL53L0_IOCTL_OFFCALB\n");
		data->offsetCalDistance = 50;
		if (copy_from_user(&targetDistance, (unsigned int *)p,
			sizeof(unsigned int))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		data->offsetCalDistance = targetDistance;
		if (data->enable_ps_sensor == 0) {
			/* to start */
			stmvl53l0_start(data, 3, OFFSETCALIB_MODE);
		} else
			rc = -EINVAL;
		break;
	/* set up offset value */
	case VL53L0_IOCTL_SETOFFSET:
		vl53l0_dbgmsg("VL53L0_IOCTL_SETOFFSET\n");
		if (copy_from_user(&offsetint, (int8_t *)p, sizeof(int8_t))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		vl53l0_dbgmsg("SETOFFSET as %d\n", offsetint);

		/* later
		* SetOffsetCalibrationData(vl53l0_dev, offsetint);
		*/
		break;

	/* Config use case */
	case VL53L0_IOCTL_ACTIVATE_USE_CASE:
		vl53l0_dbgmsg("VL53L0_IOCTL_ACTIVATE_USE_CASE\n");
		if (copy_from_user(&useCase, (uint8_t *)p, sizeof(uint8_t))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}

		/* Validate the user passed use case.
		 * Update the timingBudget value. The other
		 * parameters are updated approrpiately in config_use_case()
	     * Currently the timing budget can be updated through
	     * sysfs entry, and this needs additional steps to manage.
		 */
		switch (useCase) {
		case USE_CASE_LONG_DISTANCE:
			data->timingBudget = LONG_DISTANCE_TIMING_BUDGET;
			break;

		case USE_CASE_HIGH_ACCURACY:
			data->timingBudget = HIGH_ACCURACY_TIMING_BUDGET;
			break;

		case USE_CASE_HIGH_SPEED:
			data->timingBudget = HIGH_SPEED_TIMING_BUDGET;
			break;

		default:
			vl53l0_errmsg("%d, Unknown Use case = %u\n", __LINE__,
				 useCase);
			return -EFAULT;
		}
		vl53l0_dbgmsg("useCase as %d\n", useCase);
		/* record the use case */
		data->useCase = useCase;

		/* If ranging is in progress, let the work handler
		 * update the use case
		 */
		if (data->enable_ps_sensor)
			data->updateUseCase = 1;
		break;

	/* Config Custom use case */
	case VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE:
		vl53l0_dbgmsg("VL53L0_IOCTL_ACTIVATE_CUSTOM_USE_CASE\n");
		if (copy_from_user(&customUseCase,
			 (struct stmvl53l0_custom_use_case *)p,
			sizeof(struct stmvl53l0_custom_use_case))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}

		data->sigmaLimit = customUseCase.sigmaLimit;
		data->signalRateLimit = customUseCase.signalRateLimit;
		data->preRangePulsePeriod = customUseCase.preRangePulsePeriod;
		data->finalRangePulsePeriod =
					customUseCase.finalRangePulsePeriod;
		data->timingBudget = customUseCase.timingBudget;
		vl53l0_dbgmsg(
			"Sigma=%u,Signal=%u,Pre=%u,Final=%u,timingBudget=%u\n",
						data->sigmaLimit,
						 data->signalRateLimit,
						data->preRangePulsePeriod,
						 data->finalRangePulsePeriod,
						data->timingBudget);

		/* record the use case */
		data->useCase = USE_CASE_CUSTOM;
		/* If ranging is in progress,
		 * let the work handler update the use case
		 */
		if (data->enable_ps_sensor)
			data->updateUseCase = 1;

		break;


	/* disable */
	case VL53L0_IOCTL_STOP:
		vl53l0_dbgmsg("VL53L0_IOCTL_STOP\n");
		/* turn off tof sensor only if it's enabled by other client */
		if (data->enable_ps_sensor == 1) {
			data->enable_ps_sensor = 0;
			/* to stop */
			stmvl53l0_stop(data);
		}
		break;
	/* Get all range data */
	case VL53L0_IOCTL_GETDATAS:
		vl53l0_dbgmsg("VL53L0_IOCTL_GETDATAS\n");
		if (copy_to_user((VL53L0_RangingMeasurementData_t *)p,
			&(data->rangeData),
			sizeof(VL53L0_RangingMeasurementData_t))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		break;
	/* Register tool */
	case VL53L0_IOCTL_REGISTER:
		vl53l0_dbgmsg("VL53L0_IOCTL_REGISTER\n");
		if (copy_from_user(&reg, (struct stmvl53l0_register *)p,
			sizeof(struct stmvl53l0_register))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		reg.status = 0;
		page_num = (uint8_t)((reg.reg_index & 0x0000ff00) >> 8);
		vl53l0_dbgmsg(
"VL53L0_IOCTL_REGISTER,	page number:%d\n", page_num);
		if (page_num != 0)
			reg.status = VL53L0_WrByte(vl53l0_dev, 0xFF, page_num);

		switch (reg.reg_bytes) {
		case(4):
			if (reg.is_read)
				reg.status = VL53L0_RdDWord(vl53l0_dev,
					(uint8_t)reg.reg_index,
					&reg.reg_data);
			else
				reg.status = VL53L0_WrDWord(vl53l0_dev,
					(uint8_t)reg.reg_index,
					reg.reg_data);
			break;
		case(2):
			if (reg.is_read)
				reg.status = VL53L0_RdWord(vl53l0_dev,
					(uint8_t)reg.reg_index,
					(uint16_t *)&reg.reg_data);
			else
				reg.status = VL53L0_WrWord(vl53l0_dev,
					(uint8_t)reg.reg_index,
					(uint16_t)reg.reg_data);
			break;
		case(1):
			if (reg.is_read)
				reg.status = VL53L0_RdByte(vl53l0_dev,
					(uint8_t)reg.reg_index,
					(uint8_t *)&reg.reg_data);
			else
				reg.status = VL53L0_WrByte(vl53l0_dev,
					(uint8_t)reg.reg_index,
					(uint8_t)reg.reg_data);
			break;
		default:
			reg.status = -1;

		}
		if (page_num != 0)
			reg.status = VL53L0_WrByte(vl53l0_dev, 0xFF, 0);


		if (copy_to_user((struct stmvl53l0_register *)p, &reg,
				sizeof(struct stmvl53l0_register))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		break;
	/* parameter access */
	case VL53L0_IOCTL_PARAMETER:
		vl53l0_dbgmsg("VL53L0_IOCTL_PARAMETER\n");
		if (copy_from_user(&parameter, (struct stmvl53l0_parameter *)p,
				sizeof(struct stmvl53l0_parameter))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		parameter.status = 0;
		if (data->enableDebug)
			vl53l0_dbgmsg(
			"VL53L0_IOCTL_PARAMETER Name = %d\n", parameter.name);
		switch (parameter.name) {
		case (OFFSET_PAR):
			if (parameter.is_read)
				parameter.status =
			    papi_func_tbl->GetOffsetCalibrationDataMicroMeter(
						vl53l0_dev, &parameter.value);
			else {
				parameter.status =
			    papi_func_tbl->SetOffsetCalibrationDataMicroMeter(
						vl53l0_dev, parameter.value);
				  data->OffsetMicroMeter = parameter.value;
				  data->setCalibratedValue
			       |= SET_OFFSET_CALIB_DATA_MICROMETER_MASK;

			}
			vl53l0_dbgmsg("get parameter value as %d\n",
				 parameter.value);
			break;

		case (REFERENCESPADS_PAR):
			if (parameter.is_read) {
				parameter.status =
				  papi_func_tbl->GetReferenceSpads(vl53l0_dev,
						(uint32_t *)&(parameter.value),
				    (uint8_t *)&(parameter.value2));
				if (data->enableDebug)
					vl53l0_dbgmsg(
						"Get RefSpad : Count:%u, Type:%u\n",
						parameter.value,
						(uint8_t)parameter.value2);
			} else {
				if (data->enableDebug)
					vl53l0_dbgmsg(
					"Set RefSpad : Count:%u, Type:%u\n",
					parameter.value,
					(uint8_t)parameter.value2);

				parameter.status =
				  papi_func_tbl->SetReferenceSpads(vl53l0_dev,
				(uint32_t)(parameter.value),
				(uint8_t)(parameter.value2));

				data->refSpadCount = parameter.value;
				data->isApertureSpads =
					(uint8_t)(parameter.value2);
			}
			break;

		case (REFCALIBRATION_PAR):
			if (parameter.is_read) {
				parameter.status =
			     papi_func_tbl->GetRefCalibration(vl53l0_dev,
			     (uint8_t *)&(parameter.value),
			     (uint8_t *)&(parameter.value2));
					if (data->enableDebug)
						vl53l0_dbgmsg(
						"Get Ref : Vhv:%u, PhaseCal:%u\n",
						(uint8_t)parameter.value,
						(uint8_t)parameter.value2);
			} else {
				if (data->enableDebug)
					vl53l0_dbgmsg(
					"Set Ref : Vhv:%u, PhaseCal:%u\n",
					(uint8_t)parameter.value,
					(uint8_t)parameter.value2);
					parameter.status =
					    papi_func_tbl->SetRefCalibration(
					    vl53l0_dev,
					    (uint8_t)(parameter.value),
					    (uint8_t)(parameter.value2));
				data->VhvSettings = (uint8_t)parameter.value;
				data->PhaseCal    = (uint8_t)(parameter.value2);
			}
			break;
		case (XTALKRATE_PAR):
			if (parameter.is_read)
				parameter.status =
				papi_func_tbl->GetXTalkCompensationRateMegaCps(
				    vl53l0_dev,
				    (FixPoint1616_t *)
				    &parameter.value);
			else {
				/* Range Ignore Threshold value */
				FixPoint1616_t ritValue = 0;

				parameter.status =
			papi_func_tbl->SetXTalkCompensationRateMegaCps(
						vl53l0_dev,
						(FixPoint1616_t)
							parameter.value);
				data->XTalkCompensationRateMegaCps =
					 parameter.value;
				data->setCalibratedValue |=
					 SET_XTALK_COMP_RATE_MCPS_MASK;


				/*0.7 KCps converted to MCps */
				if (data->XTalkCompensationRateMegaCps <
						 7*65536/10000) {
					ritValue = 15 * 7 * 65536/100000;
				} else {
					ritValue = 15 *
					vl53l0_dev->XTalkCompensationRateMegaCps
							 /10;
				}

				if (papi_func_tbl->SetLimitCheckEnable
					!= NULL) {
					Status =
					papi_func_tbl->SetLimitCheckEnable(
					vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
					1);
				}

				if ((Status == VL53L0_ERROR_NONE)  &&
					(papi_func_tbl->SetLimitCheckValue
						 != NULL)) {
					vl53l0_dbgmsg(
					"Set RIT - %u\n", ritValue);
					Status =
				papi_func_tbl->SetLimitCheckValue(
				vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
				ritValue);
				}

			}
			break;
		case (XTALKENABLE_PAR):
			if (parameter.is_read)
				parameter.status =
				papi_func_tbl->GetXTalkCompensationEnable(
					vl53l0_dev,
					(uint8_t *) &parameter.value);
			else
				parameter.status =
				papi_func_tbl->SetXTalkCompensationEnable(
				vl53l0_dev,
				(uint8_t) parameter.value);
			break;
		case (GPIOFUNC_PAR):
			if (parameter.is_read) {
				parameter.status =
				papi_func_tbl->GetGpioConfig(
				vl53l0_dev,
				0,
				&deviceMode,
				&data->gpio_function,
				&data->gpio_polarity);
				parameter.value =
					 data->gpio_function;
			} else {
				data->gpio_function = parameter.value;
				parameter.status =
				  papi_func_tbl->SetGpioConfig(
					vl53l0_dev,
					 0,
					 0,
					data->gpio_function,
					data->gpio_polarity);
			}
			break;
		case (LOWTHRESH_PAR):
			if (parameter.is_read) {
				parameter.status =
				  papi_func_tbl->GetInterruptThresholds(
					vl53l0_dev,
					 0,
					&(data->low_threshold),
					&(data->high_threshold));
				parameter.value =
					 data->low_threshold >> 16;
			} else {
				data->low_threshold = parameter.value << 16;
				parameter.status =
				  papi_func_tbl->SetInterruptThresholds(
					vl53l0_dev,
					 0,
					data->low_threshold,
					 data->high_threshold);
			}
			break;
		case (HIGHTHRESH_PAR):
			if (parameter.is_read) {
				parameter.status =
				  papi_func_tbl->GetInterruptThresholds(
					vl53l0_dev,
					 0,
					&(data->low_threshold),
					 &(data->high_threshold));
				parameter.value =
					data->high_threshold >> 16;
			} else {
				data->high_threshold =
					 parameter.value << 16;
				parameter.status =
				  papi_func_tbl->SetInterruptThresholds(
					vl53l0_dev,
					 0,
					data->low_threshold,
					 data->high_threshold);
			}
			break;
		case (DEVICEMODE_PAR):
			if (parameter.is_read) {
				parameter.status =
					papi_func_tbl->GetDeviceMode(
					vl53l0_dev,
				 (VL53L0_DeviceModes *)&(parameter.value));
			} else {
				parameter.status =
				  papi_func_tbl->SetDeviceMode(
					vl53l0_dev,
					(VL53L0_DeviceModes)(parameter.value));
				data->deviceMode =
				 (VL53L0_DeviceModes)(parameter.value);
			}
			break;



		case (INTERMEASUREMENT_PAR):
			if (parameter.is_read) {
				parameter.status =
			   papi_func_tbl->GetInterMeasurementPeriodMilliSeconds(
					vl53l0_dev,
					(uint32_t *)&(parameter.value));
			} else {
				parameter.status =
			  papi_func_tbl->SetInterMeasurementPeriodMilliSeconds(
					vl53l0_dev,
					(uint32_t)(parameter.value));
				data->interMeasurems = parameter.value;
			}
			break;

		}

		if (copy_to_user((struct stmvl53l0_parameter *)p, &parameter,
				sizeof(struct stmvl53l0_parameter))) {
			vl53l0_errmsg("%d, fail\n", __LINE__);
			return -EFAULT;
		}
		break;
	default:
		rc = -EINVAL;
		break;
	}

	return rc;
}

static int stmvl53l0_open(struct inode *inode, struct file *file)
{
	return 0;
}

/* Flush will be invoked on close(device_fd) */
static int stmvl53l0_flush(struct file *file, fl_owner_t id)
{
	struct stmvl53l0_data *data = container_of(file->private_data,
					struct stmvl53l0_data, miscdev);
	(void) file;
	(void) id;
	/* Revisit : Check if the
	 *instance are opened multiple times on some platforms
	 */
	mutex_lock(&data->work_mutex);
	if (data) {
		if (data->enable_ps_sensor == 1) {
			/* turn off tof sensor if it's enabled */
			data->enable_ps_sensor = 0;
			/* to stop */
			stmvl53l0_stop(data);
		}
	}
	mutex_unlock(&data->work_mutex);

	return 0;
}

static long stmvl53l0_ioctl(struct file *file,
				unsigned int cmd, unsigned long arg)
{
	long ret;
	struct stmvl53l0_data *data =
			container_of(file->private_data,
					struct stmvl53l0_data, miscdev);
	mutex_lock(&data->work_mutex);
	ret = stmvl53l0_ioctl_handler(file, cmd, arg, (void __user *)arg);
	mutex_unlock(&data->work_mutex);

	return ret;
}

/*
 * Initialization function
 */
static int stmvl53l0_init_client(struct stmvl53l0_data *data)
{

	VL53L0_Error Status = VL53L0_ERROR_NONE;
	VL53L0_DeviceInfo_t DeviceInfo;
	VL53L0_DEV vl53l0_dev = data;
	uint32_t refSpadCount = 0;
	uint8_t isApertureSpads = 0;
	uint8_t VhvSettings = 0;
	uint8_t PhaseCal = 0;


	vl53l0_dbgmsg("Enter\n");

	data->I2cDevAddr      = 0x52;
	data->comms_type      = 1;
	data->comms_speed_khz = 400;



	/* Setup API functions based on revision */
	stmvl53l0_setupAPIFunctions(data);

	/* Perform Ref and RefSpad calibrations and save the values */


	if (data->reset) {
		pr_err("Call of VL53L0_DataInit\n");
		/* Data initialization */
		Status = papi_func_tbl->DataInit(vl53l0_dev);
		/* data->reset = 0; */
		if (Status != VL53L0_ERROR_NONE) {
			vl53l0_errmsg(
			 "%d- error status %d\n", __LINE__, Status);
			return Status;
		}
	}

	vl53l0_dbgmsg("VL53L0_GetDeviceInfo:\n");
	Status = papi_func_tbl->GetDeviceInfo(vl53l0_dev, &DeviceInfo);
	if (Status == VL53L0_ERROR_NONE) {
		pr_err("Device Name : %s\n", DeviceInfo.Name);
		pr_err("Device Type : %s\n", DeviceInfo.Type);
		pr_err("Device ID : %s\n", DeviceInfo.ProductId);
		pr_err("Product type: %d\n", DeviceInfo.ProductType);
		pr_err("ProductRevisionMajor : %d\n",
			DeviceInfo.ProductRevisionMajor);
		pr_err("ProductRevisionMinor : %d\n",
				DeviceInfo.ProductRevisionMinor);
	}
		/* Device Initialization */
	vl53l0_dbgmsg("Call of VL53L0_StaticInit\n");
	Status = papi_func_tbl->StaticInit(vl53l0_dev);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg("%d- error status %d\n", __LINE__, Status);
		return Status;
	}





	if (papi_func_tbl->PerformRefCalibration != NULL && data->reset) {
		vl53l0_dbgmsg("Call of VL53L0_PerformRefCalibration\n");
		Status = papi_func_tbl->PerformRefCalibration(vl53l0_dev,
				&VhvSettings, &PhaseCal); /* Ref calibration */
		if (Status !=
			 VL53L0_ERROR_NONE) {
			vl53l0_errmsg(
			"%d- error status %d\n", __LINE__, Status);
			return Status;
		}
	}
	vl53l0_dbgmsg("VHV = %u, PhaseCal = %u\n", VhvSettings, PhaseCal);
	vl53l0_dev->VhvSettings = VhvSettings;
	vl53l0_dev->PhaseCal = PhaseCal;

	if (papi_func_tbl->PerformRefSpadManagement != NULL && data->reset) {
		vl53l0_dbgmsg(
			"Call of VL53L0_PerformRefSpadManagement\n");
		Status = papi_func_tbl->PerformRefSpadManagement(vl53l0_dev,
				&refSpadCount,
				&isApertureSpads); /* Ref Spad Management */
		if (Status != VL53L0_ERROR_NONE) {
			vl53l0_errmsg(
			"%d- error status %d\n", __LINE__, Status);
			return Status;
		}
	}

	vl53l0_dbgmsg(
		"SpadCount = %u, isAperature = %u\n",
		 refSpadCount, isApertureSpads);
	vl53l0_dev->refSpadCount = refSpadCount;
	vl53l0_dev->isApertureSpads = isApertureSpads;




	if (Status == VL53L0_ERROR_NONE && data->reset) {
		if ((papi_func_tbl->SetOffsetCalibrationDataMicroMeter
			  != NULL) &&
		(vl53l0_dev->setCalibratedValue &
			 SET_OFFSET_CALIB_DATA_MICROMETER_MASK)) {
			vl53l0_dbgmsg(
			"Call of SetOffsetCalibrationDataMicroMeter\n");
			Status =
			papi_func_tbl->SetOffsetCalibrationDataMicroMeter(
				vl53l0_dev,
				vl53l0_dev->OffsetMicroMeter);
			if (Status != VL53L0_ERROR_NONE) {
				vl53l0_errmsg(
				"%d- error status %d\n", __LINE__, Status);
				return Status;
			}
		}
	}



	if (data->reset) {
		if ((papi_func_tbl->SetXTalkCompensationRateMegaCps != NULL) &&
		(vl53l0_dev->setCalibratedValue &
			SET_XTALK_COMP_RATE_MCPS_MASK)) {
			vl53l0_dbgmsg(
			"Call of SetXTalkCompensationRateMegaCps\n");
			Status = papi_func_tbl->SetXTalkCompensationRateMegaCps(
				vl53l0_dev,
				vl53l0_dev->XTalkCompensationRateMegaCps);
			if (Status != VL53L0_ERROR_NONE) {
				vl53l0_errmsg(
					"%d- error status %d\n",
					 __LINE__, Status);
				return Status;
			}
		}
	}

	if (data->reset) {
		if (vl53l0_dev->setCalibratedValue &
			/* Xtalk calibration done*/
			 SET_XTALK_COMP_RATE_MCPS_MASK) {
			/* Range Ignore Threshold */
			FixPoint1616_t ritValue = 0;

			if (vl53l0_dev->XTalkCompensationRateMegaCps <
				7*65536/10000) {
				/*0.7 KCps converted to MCps */
				ritValue = 15 * 7 * 65536/100000;
			} else {
				ritValue = 15 *
				 vl53l0_dev->XTalkCompensationRateMegaCps/10;
			}

			if (papi_func_tbl->SetLimitCheckEnable != NULL) {
				Status = papi_func_tbl->SetLimitCheckEnable(
				vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
				1);
				if (Status != VL53L0_ERROR_NONE) {
					vl53l0_errmsg(
					"%d- error status %d\n", __LINE__,
					 Status);
					return Status;
				}
			}

			if (papi_func_tbl->SetLimitCheckValue != NULL) {
				vl53l0_dbgmsg("Set RIT - %u\n", ritValue);
				Status = papi_func_tbl->SetLimitCheckValue(
				vl53l0_dev,
				VL53L0_CHECKENABLE_RANGE_IGNORE_THRESHOLD,
				ritValue);
				if (Status != VL53L0_ERROR_NONE) {
					vl53l0_errmsg(
					"%d- error status %d\n",
					 __LINE__, Status);
					return Status;
				}
			}
		}
		data->reset = 0;
	}

	/* Setup in single ranging mode */

		pr_err("Call of VL53L0_SetDeviceMode\n");
	Status = papi_func_tbl->SetDeviceMode(vl53l0_dev,
					VL53L0_DEVICEMODE_SINGLE_RANGING);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg("%d- error status %d\n", __LINE__, Status);
		return Status;
	}


	Status = papi_func_tbl->SetWrapAroundCheckEnable(vl53l0_dev, 1);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg("%d- error status %d\n", __LINE__, Status);
		return Status;
	}


	vl53l0_dbgmsg("End\n");

	return 0;
}


static int stmvl53l0_config_use_case(struct stmvl53l0_data *data)
{
	VL53L0_DEV		vl53l0_dev = data;
	VL53L0_Error	Status = VL53L0_ERROR_NONE;
	FixPoint1616_t	signalRateLimit;
	FixPoint1616_t	sigmaLimit;
	uint32_t		preRangePulsePeriod;
	uint32_t		finalRangePulsePeriod;

	vl53l0_dbgmsg("Enter\n");

	switch (vl53l0_dev->useCase) {

	case USE_CASE_LONG_DISTANCE:
		sigmaLimit			= LONG_DISTANCE_SIGMA_LIMIT;
		signalRateLimit		= LONG_DISTANCE_SIGNAL_RATE_LIMIT;
		preRangePulsePeriod	= LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD;
		finalRangePulsePeriod = LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD;
		break;

	case USE_CASE_HIGH_ACCURACY:
		sigmaLimit		= HIGH_ACCURACY_SIGMA_LIMIT;
		signalRateLimit	= HIGH_ACCURACY_SIGNAL_RATE_LIMIT;
		preRangePulsePeriod	= HIGH_ACCURACY_PRE_RANGE_PULSE_PERIOD;
		finalRangePulsePeriod = HIGH_ACCURACY_FINAL_RANGE_PULSE_PERIOD;
		break;

	case USE_CASE_HIGH_SPEED:
		sigmaLimit		= HIGH_SPEED_SIGMA_LIMIT;
		signalRateLimit	= HIGH_SPEED_SIGNAL_RATE_LIMIT;
		preRangePulsePeriod	= HIGH_SPEED_PRE_RANGE_PULSE_PERIOD;
		finalRangePulsePeriod = HIGH_SPEED_FINAL_RANGE_PULSE_PERIOD;
		break;

	case USE_CASE_CUSTOM:
		/* Set by application through IOCTL interface */
		sigmaLimit			= vl53l0_dev->sigmaLimit;
		signalRateLimit		= vl53l0_dev->signalRateLimit;
		preRangePulsePeriod = vl53l0_dev->preRangePulsePeriod;
		finalRangePulsePeriod = vl53l0_dev->finalRangePulsePeriod;
		break;

	default:
		vl53l0_errmsg(
			"Invalid use case = %d\n", vl53l0_dev->useCase);
		/* Invalid parameter, should not reach here */
		return -EINVAL;
	}

	vl53l0_dbgmsg(
		"Configure UseCase(%d) : Sigma=%u,Signal=%u,Pre=%u,Final=%u,timingBudget=%u\n",
					vl53l0_dev->useCase,
					sigmaLimit,
					signalRateLimit,
					preRangePulsePeriod,
					finalRangePulsePeriod,
					vl53l0_dev->timingBudget);

	if (papi_func_tbl->SetLimitCheckEnable != NULL) {
		Status = papi_func_tbl->SetLimitCheckEnable(
			vl53l0_dev,
			VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE,
			 1);

		if (Status == VL53L0_ERROR_NONE) {
			Status = papi_func_tbl->SetLimitCheckEnable(
				vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
				 1);
		} else {
			vl53l0_errmsg(
			"SetLimitCheckEnable(SIGMA_FINAL_RANGE) failed with errcode = %d\n",
			 Status);
		}
	}


	if (Status == VL53L0_ERROR_NONE) {
		Status = papi_func_tbl->SetLimitCheckValue(
				vl53l0_dev,
				VL53L0_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE,
				signalRateLimit);
	} else {
		vl53l0_errmsg(
			"SetLimitCheckEnable(SIGNAL_RATE_FINAL_RANGE) failed with errcode = %d\n",
		 Status);
	}

	if (Status == VL53L0_ERROR_NONE) {
		Status = papi_func_tbl->SetLimitCheckValue(vl53l0_dev,
					VL53L0_CHECKENABLE_SIGMA_FINAL_RANGE,
					sigmaLimit);
	} else {
		vl53l0_dbgmsg(
			"SIGNAL_RATE_FINAL_RANGE failed with errcode = %d\n",
			 Status);
	}

	if (Status == VL53L0_ERROR_NONE) {
		Status =
		papi_func_tbl->SetMeasurementTimingBudgetMicroSeconds(
				vl53l0_dev,
				vl53l0_dev->timingBudget);
	} else {
		vl53l0_dbgmsg(
			"SIGMA_FINAL_RANGE failed with errcode = %d\n",
			 Status);
	}


	if (Status == VL53L0_ERROR_NONE) {
		Status = papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev,
					VL53L0_VCSEL_PERIOD_PRE_RANGE,
					preRangePulsePeriod);
	} else {
		vl53l0_dbgmsg(
		 "SetMeasurementTimingBudget failed with errcode = %d\n",
			 Status);
	}

	if (Status == VL53L0_ERROR_NONE) {
		Status = papi_func_tbl->SetVcselPulsePeriod(vl53l0_dev,
			VL53L0_VCSEL_PERIOD_FINAL_RANGE,
			finalRangePulsePeriod);
	} else
		vl53l0_dbgmsg(
		"SetVcselPulsePeriod(PRE) failed with errcode = %d\n", Status);


	if (Status != VL53L0_ERROR_NONE)
		vl53l0_dbgmsg(
		"SetVcselPulsePeriod(FINAL)failed with errcode = %d\n", Status);

	vl53l0_dbgmsg("End\n");
	return Status;
}
static int stmvl53l0_start(struct stmvl53l0_data *data, uint8_t scaling,
	init_mode_e mode)
{
	int rc = 0;
	VL53L0_DEV vl53l0_dev = data;
	VL53L0_Error Status = VL53L0_ERROR_NONE;

	vl53l0_dbgmsg("Enter\n");

	/* Power up */
	rc = pmodule_func_tbl->power_up(data->client_object, &data->reset);
	if (rc) {
		vl53l0_errmsg("%d,error rc %d\n", __LINE__, rc);
		return rc;
	}

	/* init */
	rc = stmvl53l0_init_client(data);
	if (rc) {
		vl53l0_errmsg("%d, error rc %d\n", __LINE__, rc);
		pmodule_func_tbl->power_down(data->client_object);
		return -EINVAL;
	}

	/* check mode */
	if (mode != NORMAL_MODE)
		papi_func_tbl->SetXTalkCompensationEnable(vl53l0_dev, 1);

	if (mode == OFFSETCALIB_MODE) {
		/*VL53L0_SetOffsetCalibrationDataMicroMeter(vl53l0_dev, 0);*/
		FixPoint1616_t OffsetMicroMeter;

		papi_func_tbl->PerformOffsetCalibration(vl53l0_dev,
			(data->offsetCalDistance<<16),
			&OffsetMicroMeter);
		pr_err("Offset calibration:%u\n", OffsetMicroMeter);
		vl53l0_dev->OffsetMicroMeter = OffsetMicroMeter;
		vl53l0_dev->setCalibratedValue |=
		 SET_OFFSET_CALIB_DATA_MICROMETER_MASK;

		return rc;
	} else if (mode == XTALKCALIB_MODE) {
		FixPoint1616_t XTalkCompensationRateMegaCps;
		/*caltarget distance : 100mm and convert to
		* fixed point 16 16 format
		*/
		papi_func_tbl->PerformXTalkCalibration(vl53l0_dev,
			(data->xtalkCalDistance<<16),
			&XTalkCompensationRateMegaCps);
		pr_err("Xtalk calibration:%u\n",
			 XTalkCompensationRateMegaCps);
		vl53l0_dev->XTalkCompensationRateMegaCps =
				 XTalkCompensationRateMegaCps;
		vl53l0_dev->setCalibratedValue |=
			 SET_XTALK_COMP_RATE_MCPS_MASK;

		return rc;
	}
	/* set up device parameters */
	data->gpio_polarity = VL53L0_INTERRUPTPOLARITY_LOW;

	/* Following two calls are made from IOCTL as well */
	Status = papi_func_tbl->SetGpioConfig(vl53l0_dev, 0, 0,
		data->gpio_function,
		VL53L0_INTERRUPTPOLARITY_LOW);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg("Failed to SetGpioConfig. Error = %d\n", Status);
		return -EPERM;
	}


	Status = papi_func_tbl->SetInterruptThresholds(vl53l0_dev, 0,
		data->low_threshold,
		 data->high_threshold);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg(
		"Failed to SetInterruptThresholds. Error = %d\n", Status);
		return -EPERM;
	}


	if (data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING) {
		Status = papi_func_tbl->SetInterMeasurementPeriodMilliSeconds(
			vl53l0_dev,
			data->interMeasurems);
		if (Status != VL53L0_ERROR_NONE) {
			vl53l0_errmsg(
			"Failed to SetInterMeasurementPeriodMilliSeconds. Error = %d\n",
			 Status);
			return -EPERM;
		}
		pr_err(
			"DeviceMode:0x%x, interMeasurems:%d==\n",
			data->deviceMode,
			data->interMeasurems);


	}


	Status = papi_func_tbl->SetDeviceMode(
			vl53l0_dev,
			data->deviceMode);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg("Failed to SetDeviceMode. Error = %d\n", Status);
		return -EPERM;
	}

	Status = papi_func_tbl->ClearInterruptMask(vl53l0_dev,
							0);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg(
		"Failed to ClearInterruptMask. Error = %d\n", Status);
		return -EPERM;
	}



	Status = stmvl53l0_config_use_case(vl53l0_dev);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg(
			"Failed to configure Use case = %u\n",
				vl53l0_dev->useCase);
		return -EPERM;
	}

	/* start the ranging */
	Status = papi_func_tbl->StartMeasurement(vl53l0_dev);
	if (Status != VL53L0_ERROR_NONE) {
		vl53l0_errmsg(
			"Failed to StartMeasurement. Error = %d\n", Status);
		return -EPERM;
	}

	data->enable_ps_sensor = 1;

#ifndef USE_INT
	/* Unblock the thread execution */
	wake_up(&vl53l0_dev->poll_thread_wq);
#endif

	vl53l0_dbgmsg("End\n");

	return rc;
}

static int stmvl53l0_stop(struct stmvl53l0_data *data)
{
	int rc = 0;
	VL53L0_DEV vl53l0_dev = data;

	vl53l0_dbgmsg("Enter\n");

	/* stop - if continuous mode */
	if (data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_RANGING ||
		data->deviceMode == VL53L0_DEVICEMODE_CONTINUOUS_TIMED_RANGING)
		papi_func_tbl->StopMeasurement(vl53l0_dev);

	/* clean interrupt */
	papi_func_tbl->ClearInterruptMask(vl53l0_dev, 0);

	/* cancel work handler */
	stmvl53l0_cancel_handler(data);

	/* Clear updateUseCase pending operation */
	data->updateUseCase = 0;
	/* power down */
	rc = pmodule_func_tbl->power_down(data->client_object);
	if (rc) {
		vl53l0_errmsg("%d, error rc %d\n", __LINE__, rc);
		return rc;
	}
	vl53l0_dbgmsg("End\n");

	return rc;
}
static void stmvl53l0_timer_fn(unsigned long data)
{

	VL53L0_DEV vl53l0_dev = (VL53L0_DEV)data;

	vl53l0_dev->flushCount++;

	input_report_abs(vl53l0_dev->input_dev_ps, ABS_GAS,
		  vl53l0_dev->flushCount);


	input_sync(vl53l0_dev->input_dev_ps);

	vl53l0_dbgmsg("Sensor HAL Flush Count = %u\n", vl53l0_dev->flushCount);
}



/*
 * I2C init/probing/exit functions
 */
static const struct file_operations stmvl53l0_ranging_fops = {
	.owner =			THIS_MODULE,
	.unlocked_ioctl =	stmvl53l0_ioctl,
	.open =				stmvl53l0_open,
	.flush =			stmvl53l0_flush,
};



int stmvl53l0_setup(struct stmvl53l0_data *data)
{
	int rc = 0;

#ifdef USE_INT
	int irq = 0;
#endif

	vl53l0_dbgmsg("Enter\n");

	/* init mutex */
	mutex_init(&data->update_lock);
	mutex_init(&data->work_mutex);

#ifdef USE_INT
	/* init interrupt */
	gpio_request(data->irq_gpio, "vl53l0_gpio_int");
	gpio_direction_input(data->irq_gpio);
	irq = gpio_to_irq(data->irq_gpio);
	if (irq < 0) {
		vl53l0_errmsg("filed to map GPIO: %d to interrupt:%d\n",
			data->irq_gpio, irq);
	} else {
		vl53l0_dbgmsg("register_irq:%d\n", irq);
		/* IRQF_TRIGGER_FALLING- poliarity:0 IRQF_TRIGGER_RISNG -
		 * poliarty:1
		 */
		rc = request_threaded_irq(irq, NULL,
				stmvl53l0_interrupt_handler,
				IRQF_TRIGGER_FALLING|IRQF_ONESHOT,
				"vl53l0_interrupt",
				(void *)data);
		if (rc) {
			vl53l0_errmsg(
"%d, Could not allocate STMVL53L0_INT ! result:%d\n",  __LINE__, rc);
			free_irq(irq, data);
			goto exit_free_irq;
		}
	}
	data->irq = irq;
	vl53l0_errmsg("interrupt is hooked\n");
#else

	init_waitqueue_head(&data->poll_thread_wq);

	data->poll_thread = kthread_run(&stmvl53l0_poll_thread,
							(void *)data,
							"STM-VL53L0");
	if (data->poll_thread == NULL) {
		pr_err(
	"%s(%d) - Failed to create Polling thread\n", __func__, __LINE__);
		goto exit_free_irq;
	}
#endif

	/* init work handler */
	INIT_DELAYED_WORK(&data->dwork, stmvl53l0_work_handler);

	/* Register to Input Device */
	data->input_dev_ps = input_allocate_device();
	if (!data->input_dev_ps) {
		rc = -ENOMEM;
		vl53l0_errmsg("%d error:%d\n", __LINE__, rc);
		goto exit_free_irq;
	}
	set_bit(EV_ABS, data->input_dev_ps->evbit);
	/* range in cm*/
	input_set_abs_params(data->input_dev_ps, ABS_DISTANCE, 0, 76, 0, 0);
	/* tv_sec */
	input_set_abs_params(data->input_dev_ps, ABS_HAT0X, 0, 0xffffffff,
		0, 0);
	/* tv_usec */
	input_set_abs_params(data->input_dev_ps, ABS_HAT0Y, 0, 0xffffffff,
		0, 0);
	/* range in_mm */
	input_set_abs_params(data->input_dev_ps, ABS_HAT1X, 0, 765, 0, 0);
	/* error code change maximum to 0xff for more flexibility */
	input_set_abs_params(data->input_dev_ps, ABS_HAT1Y, 0, 0xff, 0, 0);
	/* rtnRate */
	input_set_abs_params(data->input_dev_ps, ABS_HAT2X, 0, 0xffffffff,
		0, 0);
	/* rtn_amb_rate */
	input_set_abs_params(data->input_dev_ps, ABS_HAT2Y, 0, 0xffffffff,
		0, 0);
	/* rtn_conv_time */
	input_set_abs_params(data->input_dev_ps, ABS_HAT3X, 0, 0xffffffff,
		0, 0);
	/* dmax */
	input_set_abs_params(data->input_dev_ps, ABS_HAT3Y, 0, 0xffffffff,
		0, 0);

	input_set_abs_params(data->input_dev_ps, ABS_PRESSURE, 0, 0xffffffff,
		0, 0);

	input_set_abs_params(data->input_dev_ps, ABS_WHEEL, 0, 0xffffffff,
		0, 0);

	input_set_abs_params(data->input_dev_ps, ABS_GAS, 0, 0xffffffff,
		0, 0);
	data->input_dev_ps->name = "STM VL53L0 proximity sensor";

	rc = input_register_device(data->input_dev_ps);
	if (rc) {
		rc = -ENOMEM;
		vl53l0_errmsg("%d error:%d\n", __LINE__, rc);
		goto exit_free_dev_ps;
	}
	/* setup drv data */
	input_set_drvdata(data->input_dev_ps, data);

	/* Register sysfs hooks */
	data->range_kobj = kobject_create_and_add("range", kernel_kobj);
	if (!data->range_kobj) {
		rc = -ENOMEM;
		vl53l0_errmsg("%d error:%d\n", __LINE__, rc);
		goto exit_unregister_dev_ps;
	}
	rc = sysfs_create_group(&data->input_dev_ps->dev.kobj,
			&stmvl53l0_attr_group);
	if (rc) {
		rc = -ENOMEM;
		vl53l0_errmsg("%d error:%d\n", __LINE__, rc);
		goto exit_unregister_dev_ps_1;
	}

	setup_timer(&data->timer,
				 stmvl53l0_timer_fn,
				(unsigned long)data);

	/* to register as a misc device */
	data->miscdev.minor = MISC_DYNAMIC_MINOR;
	data->miscdev.name = "stmvl53l0_ranging";
	data->miscdev.fops = &stmvl53l0_ranging_fops;
	vl53l0_errmsg("Misc device registration name:%s\n", data->dev_name);
	if (misc_register(&data->miscdev) != 0)
		vl53l0_errmsg(
"Could not register misc. dev for stmvl53l0	ranging\n");

	/* init default device parameter value */
	data->enable_ps_sensor = 0;
	data->reset = 1;
	data->delay_ms = 30;	/* delay time to 30ms */
	data->enableDebug = 0;
	data->gpio_polarity = VL53L0_INTERRUPTPOLARITY_LOW;
	data->gpio_function = VL53L0_GPIOFUNCTIONALITY_NEW_MEASURE_READY;
	data->low_threshold = 60;
	data->high_threshold = 200;
	data->deviceMode = VL53L0_DEVICEMODE_SINGLE_RANGING;
	data->interMeasurems = 30;
	data->useCase = USE_CASE_LONG_DISTANCE;
	data->timingBudget = LONG_DISTANCE_TIMING_BUDGET;

	/* Set default values used in Custom Mode Use Case */
	data->signalRateLimit = LONG_DISTANCE_SIGNAL_RATE_LIMIT;
	data->sigmaLimit = LONG_DISTANCE_SIGMA_LIMIT;
	data->preRangePulsePeriod = LONG_DISTANCE_PRE_RANGE_PULSE_PERIOD;
	data->finalRangePulsePeriod = LONG_DISTANCE_FINAL_RANGE_PULSE_PERIOD;




	vl53l0_dbgmsg("support ver. %s enabled\n", DRIVER_VERSION);
	vl53l0_dbgmsg("End");

	return 0;
exit_unregister_dev_ps_1:
	kobject_put(data->range_kobj);
exit_unregister_dev_ps:
	input_unregister_device(data->input_dev_ps);
exit_free_dev_ps:
	input_free_device(data->input_dev_ps);
exit_free_irq:
#ifdef USE_INT
	free_irq(irq, data);
#endif
	kfree(data);
	return rc;
}

void stmvl53l0_cleanup(struct stmvl53l0_data *data)
{
#ifndef USE_INT
	pr_err("%s(%d) : Stop poll_thread\n", __func__, __LINE__);
	poll_thread_exit = 1;
	kthread_stop(data->poll_thread);
#endif
}
static int __init stmvl53l0_init(void)
{
	int ret = -1;

	vl53l0_dbgmsg("Enter\n");

	/* assign function table */
	pmodule_func_tbl = &stmvl53l0_module_func_tbl;
	papi_func_tbl = &stmvl53l0_api_func_tbl;

	/* client specific init function */
	ret = pmodule_func_tbl->init();

	if (ret)
		vl53l0_errmsg("%d failed with %d\n", __LINE__, ret);

	vl53l0_dbgmsg("End\n");

	return ret;
}

static void __exit stmvl53l0_exit(void)
{
	vl53l0_dbgmsg("Enter\n");

	vl53l0_dbgmsg("End\n");
}


MODULE_AUTHOR("STMicroelectronics Imaging Division");
MODULE_DESCRIPTION("ST FlightSense Time-of-Flight sensor driver");
MODULE_LICENSE("GPL");
MODULE_VERSION(DRIVER_VERSION);

module_init(stmvl53l0_init);
module_exit(stmvl53l0_exit);