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
|
/* MPU6515 motion sensor driver
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* 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/interrupt.h>
#include <linux/i2c.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/input.h>
#include <linux/workqueue.h>
#include <linux/kobject.h>
#include <linux/earlysuspend.h>
#include <linux/platform_device.h>
#include <asm/atomic.h>
#include <cust_acc.h>
#include <linux/hwmsensor.h>
#include <linux/hwmsen_dev.h>
#include <linux/sensors_io.h>
#include "mpu6515.h"
#include <linux/hwmsen_helper.h>
#include <mach/mt_typedefs.h>
#include <mach/mt_gpio.h>
#include <mach/mt_pm_ldo.h>
#include <accel.h>
#include <linux/batch.h>
#ifdef CUSTOM_KERNEL_SENSORHUB
#include <SCP_sensorHub.h>
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
#define POWER_NONE_MACRO MT65XX_POWER_NONE
/*----------------------------------------------------------------------------*/
//#define DEBUG 1
//#define GSENSOR_UT
/*----------------------------------------------------------------------------*/
#define CONFIG_MPU6515_LOWPASS /*apply low pass filter on output*/
#define SW_CALIBRATION
//#define USE_EARLY_SUSPEND
/*----------------------------------------------------------------------------*/
#define MPU6515_AXIS_X 0
#define MPU6515_AXIS_Y 1
#define MPU6515_AXIS_Z 2
#define MPU6515_AXES_NUM 3
#define MPU6515_DATA_LEN 6
#define MPU6515_DEV_NAME "MPU6515G" /* name must different with gyro mpu6515 */
/*----------------------------------------------------------------------------*/
static const struct i2c_device_id mpu6515_i2c_id[] = {{MPU6515_DEV_NAME,0},{}};
static struct i2c_board_info __initdata i2c_mpu6515={ I2C_BOARD_INFO(MPU6515_DEV_NAME, (MPU6515_I2C_SLAVE_ADDR>>1))};
/*----------------------------------------------------------------------------*/
static int mpu6515_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id);
static int mpu6515_i2c_remove(struct i2c_client *client);
static int mpu6515_i2c_detect(struct i2c_client *client, struct i2c_board_info *info);
#if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
static int mpu6515_suspend(struct i2c_client *client, pm_message_t msg) ;
static int mpu6515_resume(struct i2c_client *client);
#endif
static int gsensor_local_init(void);
static int gsensor_remove(void);
#ifdef CUSTOM_KERNEL_SENSORHUB
static int gsensor_setup_irq(void);
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
static int gsensor_set_delay(u64 ns);
/*----------------------------------------------------------------------------*/
typedef enum
{
MPU6515_TRC_FILTER = 0x01,
MPU6515_TRC_RAWDATA = 0x02,
MPU6515_TRC_IOCTL = 0x04,
MPU6515_TRC_CALI = 0X08,
MPU6515_TRC_INFO = 0X10,
} MPU6515_TRC;
/*----------------------------------------------------------------------------*/
struct scale_factor
{
u8 whole;
u8 fraction;
};
/*----------------------------------------------------------------------------*/
struct data_resolution
{
struct scale_factor scalefactor;
int sensitivity;
};
/*----------------------------------------------------------------------------*/
#define C_MAX_FIR_LENGTH (32)
/*----------------------------------------------------------------------------*/
struct data_filter
{
s16 raw[C_MAX_FIR_LENGTH][MPU6515_AXES_NUM];
int sum[MPU6515_AXES_NUM];
int num;
int idx;
};
/*----------------------------------------------------------------------------*/
struct mpu6515_i2c_data
{
struct i2c_client *client;
struct acc_hw *hw;
struct hwmsen_convert cvt;
#ifdef CUSTOM_KERNEL_SENSORHUB
struct work_struct irq_work;
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
/*misc*/
struct data_resolution *reso;
atomic_t trace;
atomic_t suspend;
atomic_t selftest;
atomic_t filter;
s16 cali_sw[MPU6515_AXES_NUM+1];
/*data*/
s8 offset[MPU6515_AXES_NUM+1]; /*+1: for 4-byte alignment*/
s16 data[MPU6515_AXES_NUM+1];
#ifdef CUSTOM_KERNEL_SENSORHUB
int SCP_init_done;
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
#if defined(CONFIG_MPU6515_LOWPASS)
atomic_t firlen;
atomic_t fir_en;
struct data_filter fir;
#endif
/*early suspend*/
#if defined(CONFIG_HAS_EARLYSUSPEND) && defined(USE_EARLY_SUSPEND)
struct early_suspend early_drv;
#endif
u8 bandwidth;
};
/*----------------------------------------------------------------------------*/
static struct i2c_driver mpu6515_i2c_driver = {
.driver = {
.name = MPU6515_DEV_NAME,
},
.probe = mpu6515_i2c_probe,
.remove = mpu6515_i2c_remove,
.detect = mpu6515_i2c_detect,
#if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
.suspend = mpu6515_suspend,
.resume = mpu6515_resume,
#endif
.id_table = mpu6515_i2c_id,
};
/*----------------------------------------------------------------------------*/
static struct i2c_client *mpu6515_i2c_client = NULL;
static struct mpu6515_i2c_data *obj_i2c_data = NULL;
static bool sensor_power = false;
static bool scp_sensor_power = false;
static GSENSOR_VECTOR3D gsensor_gain;
static char selftestRes[8]= {0};
static DEFINE_MUTEX(gsensor_mutex);
static DEFINE_MUTEX(gsensor_scp_en_mutex);
static bool enable_status = false;
static int gsensor_init_flag =-1; // 0<==>OK -1 <==> fail
static struct acc_init_info mpu6515_init_info = {
.name = "mpu6515",
.init = gsensor_local_init,
.uninit = gsensor_remove,
};
/*----------------------------------------------------------------------------*/
#define GSE_TAG "[Gsensor] "
#define GSE_FUN(f) printk(GSE_TAG"%s\n", __func__)
#define GSE_ERR(fmt, args...) printk(GSE_TAG"%s %d : "fmt, __func__, __LINE__, ##args)
#define GSE_LOG(fmt, args...) printk(GSE_TAG fmt, ##args)
/*----------------------------------------------------------------------------*/
static struct data_resolution mpu6515_data_resolution[] = {
/*8 combination by {FULL_RES,RANGE}*/
{{ 0, 6}, 16384}, /*+/-2g in 16-bit resolution: 0.06 mg/LSB*/
{{ 0, 12}, 8192}, /*+/-4g in 16-bit resolution: 0.12 mg/LSB*/
{{ 0, 24}, 4096}, /*+/-8g in 16-bit resolution: 0.24 mg/LSB*/
{{ 0, 49}, 2048}, /*+/-16g in 16-bit resolution: 0.49 mg/LSB*/
};
/*----------------------------------------------------------------------------*/
static struct data_resolution mpu6515_offset_resolution = {{ 0, 5}, 2048};
static unsigned int power_on = 0;
extern int MPU6515_gyro_power(void);
extern int MPU6515_gyro_mode(void);
int MPU6515_gse_power( void)
{
return(power_on);
}
EXPORT_SYMBOL(MPU6515_gse_power);
int MPU6515_gse_mode(void)
{
return sensor_power;
}
EXPORT_SYMBOL(MPU6515_gse_mode);
int MPU6515_i2c_master_send(u8 *buf, u8 len)
{
#ifndef GSENSOR_UT
int res = 0;
if (NULL == mpu6515_i2c_client)
{
GSE_ERR("MPU6515_i2c_master_send null ptr!!\n");
}
else
{
res = i2c_master_send(mpu6515_i2c_client, buf, len);
}
return res;
#else
return 1;
#endif
}
EXPORT_SYMBOL(MPU6515_i2c_master_send);
int MPU6515_i2c_master_recv(u8 *buf, u8 len)
{
#ifndef GSENSOR_UT
int res = 0;
if (NULL == mpu6515_i2c_client)
{
GSE_ERR("MPU6515_i2c_master_recv null ptr!!\n");
}
else
{
res = i2c_master_recv(mpu6515_i2c_client, buf, len);
}
return res;
#else
return 1;
#endif
}
EXPORT_SYMBOL(MPU6515_i2c_master_recv);
#ifdef CUSTOM_KERNEL_SENSORHUB
int MPU6515_SCP_SetPowerMode(bool enable, int sensorType)
{
static bool gsensor_scp_en_status = false;
static unsigned int gsensor_scp_en_map = 0;
SCP_SENSOR_HUB_DATA req;
int len;
int err = 0;
mutex_lock(&gsensor_scp_en_mutex);
if (sensorType >= 32)
{
GSE_ERR("Out of index!\n");
return -1;
}
if (true == enable)
{
gsensor_scp_en_map |= (1<<sensorType);
}
else
{
gsensor_scp_en_map &= ~(1<<sensorType);
}
if (0 == gsensor_scp_en_map)
enable = false;
else
enable = true;
if (gsensor_scp_en_status != enable)
{
gsensor_scp_en_status = enable;
req.activate_req.sensorType = ID_ACCELEROMETER;
req.activate_req.action = SENSOR_HUB_ACTIVATE;
req.activate_req.enable = enable;
len = sizeof(req.activate_req);
err = SCP_sensorHub_req_send(&req, &len, 1);
if (err)
{
GSE_ERR("SCP_sensorHub_req_send fail!\n");
}
}
mutex_unlock(&gsensor_scp_en_mutex);
return err;
}
EXPORT_SYMBOL(MPU6515_SCP_SetPowerMode);
#endif //#ifdef CUSTOM_KERNEL_SENSORHUB
/*----------------------------------------------------------------------------*/
static int mpu_i2c_read_block(struct i2c_client *client, u8 addr, u8 *data, u8 len){
int err;
data[0] = addr;
client->addr &=I2C_MASK_FLAG;
client->addr |=I2C_WR_FLAG;
client->addr |=I2C_RS_FLAG;
err = i2c_master_send(client, data, (len<<8)|0x1);
client->addr &=I2C_MASK_FLAG;
if (err < 0)
{
GSE_ERR("i2c_transfer error: (%d %p %d) %d\n", addr, data, len, err);
}
else
{
err = 0;
}
return err;
}
int MPU6515_hwmsen_read_block(u8 addr, u8 *buf, u8 len)
{
#ifndef GSENSOR_UT
if (NULL == mpu6515_i2c_client)
{
GSE_ERR("MPU6515_hwmsen_read_block null ptr!!\n");
return MPU6515_ERR_I2C;
}
return mpu_i2c_read_block(mpu6515_i2c_client, addr, buf, len);
#else
return 0;
#endif
}
EXPORT_SYMBOL(MPU6515_hwmsen_read_block);
int MPU6515_hwmsen_read_byte(u8 addr, u8 *buf)
{
#ifndef GSENSOR_UT
if (NULL == mpu6515_i2c_client)
{
GSE_ERR("MPU6515_hwmsen_read_byte null ptr!!\n");
return MPU6515_ERR_I2C;
}
return mpu_i2c_read_block(mpu6515_i2c_client, addr, buf, 1);
#else
return 0;
#endif
}
EXPORT_SYMBOL(MPU6515_hwmsen_read_byte);
/*--------------------mpu6515 power control function----------------------------------*/
static void MPU6515_power(struct acc_hw *hw, unsigned int on)
{
#ifndef FPGA_EARLY_PORTING
static unsigned int power_on = 0;
if(hw->power_id != POWER_NONE_MACRO) // have externel LDO
{
GSE_LOG("power %s\n", on ? "on" : "off");
if(power_on == on) // power status not change
{
GSE_LOG("ignore power control: %d\n", on);
}
else if(on) // power on
{
if(!hwPowerOn(hw->power_id, hw->power_vol, "MPU6515G"))
{
GSE_ERR("power on fails!!\n");
}
}
else // power off
{
if (!hwPowerDown(hw->power_id, "MPU6515G"))
{
GSE_ERR("power off fail!!\n");
}
}
}
power_on = on;
#endif
}
/*----------------------------------------------------------------------------*/
static int MPU6515_SetPowerMode(struct i2c_client *client, bool enable)
{
int res = 0;
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
u8 databuf[2];
if (enable == sensor_power)
{
GSE_LOG("Sensor power status is newest!\n");
return MPU6515_SUCCESS;
}
#if 0
databuf[0] = MPU6515_REG_POWER_CTL;
res = i2c_master_send(client, databuf, 0x1);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 1);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
#else
if (hwmsen_read_byte(client, MPU6515_REG_POWER_CTL, databuf))
{
GSE_ERR("read power ctl register err!\n");
return MPU6515_ERR_I2C;
}
#endif
if ((databuf[0] & 0x1f) != 0x1)
GSE_ERR("MPU6515 PWR_MGMT_1 = %x\n", databuf[0]);
databuf[0] &= ~MPU6515_SLEEP;
if (enable == FALSE)
{
if (MPU6515_gyro_mode() == false)
{
databuf[0] |= MPU6515_SLEEP;
}
}
else
{
// do nothing
}
databuf[1] = databuf[0];
databuf[0] = MPU6515_REG_POWER_CTL;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
GSE_LOG("set power mode failed!\n");
return MPU6515_ERR_I2C;
}
if (atomic_read(&obj->trace) & MPU6515_TRC_INFO)
{
GSE_LOG("set power mode ok %d!\n", databuf[1]);
}
sensor_power = enable;
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_SetDataResolution(struct mpu6515_i2c_data *obj)
{
int err;
u8 dat = 0, reso = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if ((err = mpu_i2c_read_block(obj->client, MPU6515_REG_DATA_FORMAT, &dat, 1)))
{
GSE_ERR("write data format fail!!\n");
return err;
}
/*the data_reso is combined by 3 bits: {FULL_RES, DATA_RANGE}*/
reso = 0x00;
reso = (dat & MPU6515_RANGE_16G) >> 3;
if (reso < sizeof(mpu6515_data_resolution)/sizeof(mpu6515_data_resolution[0]))
{
obj->reso = &mpu6515_data_resolution[reso];
return 0;
}
else
{
return -EINVAL;
}
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadData(struct i2c_client *client, s16 data[MPU6515_AXES_NUM])
{
struct mpu6515_i2c_data *priv = i2c_get_clientdata(client);
int err = 0;
u8 buf[MPU6515_DATA_LEN] = {0};
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if (NULL == client)
{
return -EINVAL;
}
{
/* write then burst read */
mpu_i2c_read_block(client, MPU6515_REG_DATAX0, buf, MPU6515_DATA_LEN);
data[MPU6515_AXIS_X] = (s16)((buf[MPU6515_AXIS_X*2] << 8) |
(buf[MPU6515_AXIS_X*2+1] ));
data[MPU6515_AXIS_Y] = (s16)((buf[MPU6515_AXIS_Y*2] << 8) |
(buf[MPU6515_AXIS_Y*2+1] ));
data[MPU6515_AXIS_Z] = (s16)((buf[MPU6515_AXIS_Z*2] << 8) |
(buf[MPU6515_AXIS_Z*2+1] ));
if (atomic_read(&priv->trace) & MPU6515_TRC_RAWDATA)
{
GSE_LOG("[%08X %08X %08X] => [%5d %5d %5d]\n", data[MPU6515_AXIS_X], data[MPU6515_AXIS_Y], data[MPU6515_AXIS_Z],
data[MPU6515_AXIS_X], data[MPU6515_AXIS_Y], data[MPU6515_AXIS_Z]);
}
#ifdef CONFIG_MPU6515_LOWPASS
if (atomic_read(&priv->filter))
{
if (atomic_read(&priv->fir_en) && !atomic_read(&priv->suspend))
{
int idx, firlen = atomic_read(&priv->firlen);
if (priv->fir.num < firlen)
{
priv->fir.raw[priv->fir.num][MPU6515_AXIS_X] = data[MPU6515_AXIS_X];
priv->fir.raw[priv->fir.num][MPU6515_AXIS_Y] = data[MPU6515_AXIS_Y];
priv->fir.raw[priv->fir.num][MPU6515_AXIS_Z] = data[MPU6515_AXIS_Z];
priv->fir.sum[MPU6515_AXIS_X] += data[MPU6515_AXIS_X];
priv->fir.sum[MPU6515_AXIS_Y] += data[MPU6515_AXIS_Y];
priv->fir.sum[MPU6515_AXIS_Z] += data[MPU6515_AXIS_Z];
if (atomic_read(&priv->trace) & MPU6515_TRC_FILTER)
{
GSE_LOG("add [%2d] [%5d %5d %5d] => [%5d %5d %5d]\n", priv->fir.num,
priv->fir.raw[priv->fir.num][MPU6515_AXIS_X], priv->fir.raw[priv->fir.num][MPU6515_AXIS_Y], priv->fir.raw[priv->fir.num][MPU6515_AXIS_Z],
priv->fir.sum[MPU6515_AXIS_X], priv->fir.sum[MPU6515_AXIS_Y], priv->fir.sum[MPU6515_AXIS_Z]);
}
priv->fir.num++;
priv->fir.idx++;
}
else
{
idx = priv->fir.idx % firlen;
priv->fir.sum[MPU6515_AXIS_X] -= priv->fir.raw[idx][MPU6515_AXIS_X];
priv->fir.sum[MPU6515_AXIS_Y] -= priv->fir.raw[idx][MPU6515_AXIS_Y];
priv->fir.sum[MPU6515_AXIS_Z] -= priv->fir.raw[idx][MPU6515_AXIS_Z];
priv->fir.raw[idx][MPU6515_AXIS_X] = data[MPU6515_AXIS_X];
priv->fir.raw[idx][MPU6515_AXIS_Y] = data[MPU6515_AXIS_Y];
priv->fir.raw[idx][MPU6515_AXIS_Z] = data[MPU6515_AXIS_Z];
priv->fir.sum[MPU6515_AXIS_X] += data[MPU6515_AXIS_X];
priv->fir.sum[MPU6515_AXIS_Y] += data[MPU6515_AXIS_Y];
priv->fir.sum[MPU6515_AXIS_Z] += data[MPU6515_AXIS_Z];
priv->fir.idx++;
data[MPU6515_AXIS_X] = priv->fir.sum[MPU6515_AXIS_X]/firlen;
data[MPU6515_AXIS_Y] = priv->fir.sum[MPU6515_AXIS_Y]/firlen;
data[MPU6515_AXIS_Z] = priv->fir.sum[MPU6515_AXIS_Z]/firlen;
if (atomic_read(&priv->trace) & MPU6515_TRC_FILTER)
{
GSE_LOG("add [%2d] [%5d %5d %5d] => [%5d %5d %5d] : [%5d %5d %5d]\n", idx,
priv->fir.raw[idx][MPU6515_AXIS_X], priv->fir.raw[idx][MPU6515_AXIS_Y], priv->fir.raw[idx][MPU6515_AXIS_Z],
priv->fir.sum[MPU6515_AXIS_X], priv->fir.sum[MPU6515_AXIS_Y], priv->fir.sum[MPU6515_AXIS_Z],
data[MPU6515_AXIS_X], data[MPU6515_AXIS_Y], data[MPU6515_AXIS_Z]);
}
}
}
}
#endif
}
return err;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadOffset(struct i2c_client *client, s8 ofs[MPU6515_AXES_NUM])
{
int err = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
#ifdef SW_CALIBRATION
ofs[0]=ofs[1]=ofs[2]=0x0;
#else
if ((err = mpu_i2c_read_block(client, MPU6515_REG_OFSX, ofs, MPU6515_AXES_NUM)))
{
GSE_ERR("error: %d\n", err);
}
#endif
//GSE_LOG("offesx=%x, y=%x, z=%x",ofs[0],ofs[1],ofs[2]);
return err;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ResetCalibration(struct i2c_client *client)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int err = 0;
#ifdef CUSTOM_KERNEL_SENSORHUB
SCP_SENSOR_HUB_DATA data;
MPU6515_CUST_DATA *pCustData;
unsigned int len;
#endif
#ifdef GSENSOR_UT
GSE_FUN();
#endif
#ifdef CUSTOM_KERNEL_SENSORHUB
if (0 != obj->SCP_init_done)
{
pCustData = (MPU6515_CUST_DATA *)&data.set_cust_req.custData;
data.set_cust_req.sensorType = ID_ACCELEROMETER;
data.set_cust_req.action = SENSOR_HUB_SET_CUST;
pCustData->resetCali.action = MPU6515_CUST_ACTION_RESET_CALI;
len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->resetCali);
SCP_sensorHub_req_send(&data, &len, 1);
}
#endif
memset(obj->cali_sw, 0x00, sizeof(obj->cali_sw));
memset(obj->offset, 0x00, sizeof(obj->offset));
return err;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadCalibration(struct i2c_client *client, int dat[MPU6515_AXES_NUM])
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
#ifdef SW_CALIBRATION
int mul;
#else
int err;
#endif
#ifdef GSENSOR_UT
GSE_FUN();
#endif
#ifdef SW_CALIBRATION
mul = 0;//only SW Calibration, disable HW Calibration
#else
if ((err = MPU6515_ReadOffset(client, obj->offset)))
{
GSE_ERR("read offset fail, %d\n", err);
return err;
}
mul = obj->reso->sensitivity/mpu6515_offset_resolution.sensitivity;
#endif
dat[obj->cvt.map[MPU6515_AXIS_X]] = obj->cvt.sign[MPU6515_AXIS_X]*(obj->offset[MPU6515_AXIS_X]*mul + obj->cali_sw[MPU6515_AXIS_X]);
dat[obj->cvt.map[MPU6515_AXIS_Y]] = obj->cvt.sign[MPU6515_AXIS_Y]*(obj->offset[MPU6515_AXIS_Y]*mul + obj->cali_sw[MPU6515_AXIS_Y]);
dat[obj->cvt.map[MPU6515_AXIS_Z]] = obj->cvt.sign[MPU6515_AXIS_Z]*(obj->offset[MPU6515_AXIS_Z]*mul + obj->cali_sw[MPU6515_AXIS_Z]);
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadCalibrationEx(struct i2c_client *client, int act[MPU6515_AXES_NUM], int raw[MPU6515_AXES_NUM])
{
/*raw: the raw calibration data; act: the actual calibration data*/
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
#ifdef SW_CALIBRATION
int mul;
#else
int err;
#endif
#ifdef GSENSOR_UT
GSE_FUN();
#endif
#ifdef SW_CALIBRATION
mul = 0;//only SW Calibration, disable HW Calibration
#else
if ((err = MPU6515_ReadOffset(client, obj->offset)))
{
GSE_ERR("read offset fail, %d\n", err);
return err;
}
mul = obj->reso->sensitivity/mpu6515_offset_resolution.sensitivity;
#endif
raw[MPU6515_AXIS_X] = obj->offset[MPU6515_AXIS_X]*mul + obj->cali_sw[MPU6515_AXIS_X];
raw[MPU6515_AXIS_Y] = obj->offset[MPU6515_AXIS_Y]*mul + obj->cali_sw[MPU6515_AXIS_Y];
raw[MPU6515_AXIS_Z] = obj->offset[MPU6515_AXIS_Z]*mul + obj->cali_sw[MPU6515_AXIS_Z];
act[obj->cvt.map[MPU6515_AXIS_X]] = obj->cvt.sign[MPU6515_AXIS_X]*raw[MPU6515_AXIS_X];
act[obj->cvt.map[MPU6515_AXIS_Y]] = obj->cvt.sign[MPU6515_AXIS_Y]*raw[MPU6515_AXIS_Y];
act[obj->cvt.map[MPU6515_AXIS_Z]] = obj->cvt.sign[MPU6515_AXIS_Z]*raw[MPU6515_AXIS_Z];
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_WriteCalibration(struct i2c_client *client, int dat[MPU6515_AXES_NUM])
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int err;
int cali[MPU6515_AXES_NUM], raw[MPU6515_AXES_NUM];
#ifdef CUSTOM_KERNEL_SENSORHUB
SCP_SENSOR_HUB_DATA data;
MPU6515_CUST_DATA *pCustData;
unsigned int len;
#endif
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if ((err = MPU6515_ReadCalibrationEx(client, cali, raw))) /*offset will be updated in obj->offset*/
{
GSE_ERR("read offset fail, %d\n", err);
return err;
}
GSE_LOG("OLDOFF: (%+3d %+3d %+3d): (%+3d %+3d %+3d) / (%+3d %+3d %+3d)\n",
raw[MPU6515_AXIS_X], raw[MPU6515_AXIS_Y], raw[MPU6515_AXIS_Z],
obj->offset[MPU6515_AXIS_X], obj->offset[MPU6515_AXIS_Y], obj->offset[MPU6515_AXIS_Z],
obj->cali_sw[MPU6515_AXIS_X], obj->cali_sw[MPU6515_AXIS_Y], obj->cali_sw[MPU6515_AXIS_Z]);
#ifdef CUSTOM_KERNEL_SENSORHUB
pCustData = (MPU6515_CUST_DATA *)data.set_cust_req.custData;
data.set_cust_req.sensorType = ID_ACCELEROMETER;
data.set_cust_req.action = SENSOR_HUB_SET_CUST;
pCustData->setCali.action = MPU6515_CUST_ACTION_SET_CALI;
pCustData->setCali.data[MPU6515_AXIS_X] = dat[MPU6515_AXIS_X];
pCustData->setCali.data[MPU6515_AXIS_Y] = dat[MPU6515_AXIS_Y];
pCustData->setCali.data[MPU6515_AXIS_Z] = dat[MPU6515_AXIS_Z];
len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(pCustData->setCali);
SCP_sensorHub_req_send(&data, &len, 1);
#endif
/*calculate the real offset expected by caller*/
cali[MPU6515_AXIS_X] += dat[MPU6515_AXIS_X];
cali[MPU6515_AXIS_Y] += dat[MPU6515_AXIS_Y];
cali[MPU6515_AXIS_Z] += dat[MPU6515_AXIS_Z];
GSE_LOG("UPDATE: (%+3d %+3d %+3d)\n",
dat[MPU6515_AXIS_X], dat[MPU6515_AXIS_Y], dat[MPU6515_AXIS_Z]);
obj->cali_sw[MPU6515_AXIS_X] = obj->cvt.sign[MPU6515_AXIS_X]*(cali[obj->cvt.map[MPU6515_AXIS_X]]);
obj->cali_sw[MPU6515_AXIS_Y] = obj->cvt.sign[MPU6515_AXIS_Y]*(cali[obj->cvt.map[MPU6515_AXIS_Y]]);
obj->cali_sw[MPU6515_AXIS_Z] = obj->cvt.sign[MPU6515_AXIS_Z]*(cali[obj->cvt.map[MPU6515_AXIS_Z]]);
return err;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_CheckDeviceID(struct i2c_client *client)
{
u8 databuf[10];
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
memset(databuf, 0, sizeof(u8)*10);
databuf[0] = MPU6515_REG_DEVID;
res = i2c_master_send(client, databuf, 0x1);
if (res <= 0)
{
GSE_ERR("i2c_master_send failed : %d\n", res);
goto exit_MPU6515_CheckDeviceID;
}
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 0x01);
if (res <= 0)
{
GSE_ERR("i2c_master_recv failed : %d\n", res);
goto exit_MPU6515_CheckDeviceID;
}
GSE_LOG("MPU6515_CheckDeviceID 0x%x\n", databuf[0]);
exit_MPU6515_CheckDeviceID:
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_SetDataFormat(struct i2c_client *client, u8 dataformat)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
u8 databuf[2];
int res = 0;
#ifndef GSENSOR_UT
memset(databuf, 0, sizeof(u8)*2);
databuf[0] = MPU6515_REG_DATA_FORMAT;
res = i2c_master_send(client, databuf, 0x1);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 0x01);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
/* write */
databuf[1] = databuf[0] | dataformat;
databuf[0] = MPU6515_REG_DATA_FORMAT;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
return MPU6515_SetDataResolution(obj);
#else
GSE_LOG("dataformat = %d\n", dataformat);
obj->reso = &mpu6515_data_resolution[3];
#endif
}
/*----------------------------------------------------------------------------*/
static int MPU6515_SetBWRate(struct i2c_client *client, u8 bwrate)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
u8 databuf[10];
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if( (obj->bandwidth != bwrate) || (atomic_read(&obj->suspend)) )
{
memset(databuf, 0, sizeof(u8)*10);
/* read */
databuf[0] = MPU6515_REG_BW_RATE;
res = i2c_master_send(client, databuf, 0x1);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 0x01);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
/* write */
databuf[1] = databuf[0] | bwrate;
databuf[0] = MPU6515_REG_BW_RATE;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
obj->bandwidth = bwrate;
}
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_Dev_Reset(struct i2c_client *client)
{
#ifndef CUSTOM_KERNEL_SENSORHUB
u8 databuf[10];
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
memset(databuf, 0, sizeof(u8)*10);
/* read */
databuf[0] = MPU6515_REG_POWER_CTL;
res = i2c_master_send(client, databuf, 0x1);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 0x01);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
if ((databuf[0] & 0x1f) != 0x1)
GSE_ERR("MPU6515 PWR_MGMT_1 = %x\n", databuf[0]);
/* write */
databuf[1] = databuf[0] | MPU6515_DEV_RESET;
databuf[0] = MPU6515_REG_POWER_CTL;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
do
{
databuf[0] = MPU6515_REG_POWER_CTL;
res = i2c_master_send(client, databuf, 0x1);
udelay(500);
databuf[0] = 0x0;
res = i2c_master_recv(client, databuf, 0x01);
printk("[Gsensor] check reset bit");
}while((databuf[0]&MPU6515_DEV_RESET) != 0);
msleep(50);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_Reset(struct i2c_client *client)
{
#ifndef CUSTOM_KERNEL_SENSORHUB
u8 databuf[10];
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
/* write */
databuf[1] = 0x7; /* reset gyro, g-sensor, temperature */
databuf[0] = MPU6515_REG_RESET;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
msleep(20);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_SetIntEnable(struct i2c_client *client, u8 intenable)
{
u8 databuf[2];
int res = 0;
#ifndef GSENSOR_UT
memset(databuf, 0, sizeof(u8)*2);
databuf[0] = MPU6515_REG_INT_ENABLE;
databuf[1] = intenable;
res = i2c_master_send(client, databuf, 0x2);
if (res <= 0)
{
return MPU6515_ERR_I2C;
}
#else
GSE_FUN();
#endif
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_gpio_config(void)
{
//because we donot use EINT to support low power
// config to GPIO input mode + PD
//set to GPIO_GSE_1_EINT_PIN
/*
mt_set_gpio_mode(GPIO_GSE_1_EINT_PIN, GPIO_GSE_1_EINT_PIN_M_GPIO);
mt_set_gpio_dir(GPIO_GSE_1_EINT_PIN, GPIO_DIR_IN);
mt_set_gpio_pull_enable(GPIO_GSE_1_EINT_PIN, GPIO_PULL_ENABLE);
mt_set_gpio_pull_select(GPIO_GSE_1_EINT_PIN, GPIO_PULL_DOWN);
*/
//set to GPIO_GSE_2_EINT_PIN
/*
mt_set_gpio_mode(GPIO_GSE_2_EINT_PIN, GPIO_GSE_2_EINT_PIN_M_GPIO);
mt_set_gpio_dir(GPIO_GSE_2_EINT_PIN, GPIO_DIR_IN);
mt_set_gpio_pull_enable(GPIO_GSE_2_EINT_PIN, GPIO_PULL_ENABLE);
mt_set_gpio_pull_select(GPIO_GSE_2_EINT_PIN, GPIO_PULL_DOWN);
*/
return 0;
}
static int mpu6515_init_client(struct i2c_client *client, int reset_cali)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
mpu6515_gpio_config();
res = MPU6515_SetPowerMode(client, true);
if (res != MPU6515_SUCCESS)
{
GSE_ERR("set power error\n");
return res;
}
res = MPU6515_CheckDeviceID(client);
if (res != MPU6515_SUCCESS)
{
GSE_ERR("Check ID error\n");
return res;
}
//res = gsensor_set_delay(5000000);
res = MPU6515_SetBWRate(client, MPU6515_BW_184HZ);
if (res != MPU6515_SUCCESS ) //0x2C->BW=100Hz
{
GSE_ERR("set BWRate error\n");
return res;
}
res = MPU6515_SetDataFormat(client, MPU6515_RANGE_16G);
if (res != MPU6515_SUCCESS) //0x2C->BW=100Hz
{
GSE_ERR("set data format error\n");
return res;
}
gsensor_gain.x = gsensor_gain.y = gsensor_gain.z = obj->reso->sensitivity;
#ifdef CUSTOM_KERNEL_SENSORHUB
res = gsensor_setup_irq();
if(res != MPU6515_SUCCESS)
{
return res;
}
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
res = MPU6515_SetIntEnable(client, 0x00);//disable INT
if (res != MPU6515_SUCCESS)
{
GSE_ERR("mpu6515_SetIntEnable error\n");
return res;
}
if (0 != reset_cali)
{
/*reset calibration only in power on*/
res = MPU6515_ResetCalibration(client);
if (res != MPU6515_SUCCESS)
{
return res;
}
}
res = MPU6515_SetPowerMode(client, enable_status);
if (res != MPU6515_SUCCESS)
{
GSE_ERR("set power error\n");
return res;
}
#ifdef CONFIG_MPU6515_LOWPASS
memset(&obj->fir, 0x00, sizeof(obj->fir));
#endif
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadAllReg(struct i2c_client *client, char *buf, int bufsize)
{
u8 total_len= 0x5C; //(0x75-0x19);
u8 addr = 0x19;
u8 buff[total_len+1];
int err = 0;
int i;
if (sensor_power == FALSE)
{
err = MPU6515_SetPowerMode(client, true);
if (err)
{
GSE_ERR("Power on mpu6515 error %d!\n", err);
}
msleep(50);
}
mpu_i2c_read_block(client, addr, buff, total_len);
for ( i=0; i<=total_len; i++)
{
GSE_LOG("MPU6515 reg=0x%x, data=0x%x \n",(addr+i), buff[i]);
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadChipInfo(struct i2c_client *client, char *buf, int bufsize)
{
u8 databuf[10];
#ifdef GSENSOR_UT
GSE_FUN();
#endif
memset(databuf, 0, sizeof(u8)*10);
if ((NULL == buf)||(bufsize<=30))
{
return -1;
}
if (NULL == client)
{
*buf = 0;
return -2;
}
sprintf(buf, "MPU6515 Chip");
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadSensorData(struct i2c_client *client, char *buf, int bufsize)
{
struct mpu6515_i2c_data *obj = obj_i2c_data; //(struct mpu6515_i2c_data*)i2c_get_clientdata(client);
int acc[MPU6515_AXES_NUM];
int res = 0;
client = obj->client;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if (atomic_read(&obj->suspend))
{
return -3;
}
if (NULL == buf)
{
return -1;
}
if (NULL == client)
{
*buf = 0;
return -2;
}
if ((res = MPU6515_ReadData(client, obj->data)))
{
GSE_ERR("I2C error: ret value=%d", res);
return -3;
}
else
{
obj->data[MPU6515_AXIS_X] += obj->cali_sw[MPU6515_AXIS_X];
obj->data[MPU6515_AXIS_Y] += obj->cali_sw[MPU6515_AXIS_Y];
obj->data[MPU6515_AXIS_Z] += obj->cali_sw[MPU6515_AXIS_Z];
/*remap coordinate*/
acc[obj->cvt.map[MPU6515_AXIS_X]] = obj->cvt.sign[MPU6515_AXIS_X]*obj->data[MPU6515_AXIS_X];
acc[obj->cvt.map[MPU6515_AXIS_Y]] = obj->cvt.sign[MPU6515_AXIS_Y]*obj->data[MPU6515_AXIS_Y];
acc[obj->cvt.map[MPU6515_AXIS_Z]] = obj->cvt.sign[MPU6515_AXIS_Z]*obj->data[MPU6515_AXIS_Z];
//Out put the mg
acc[MPU6515_AXIS_X] = acc[MPU6515_AXIS_X] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
acc[MPU6515_AXIS_Y] = acc[MPU6515_AXIS_Y] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
acc[MPU6515_AXIS_Z] = acc[MPU6515_AXIS_Z] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
sprintf(buf, "%04x %04x %04x", acc[MPU6515_AXIS_X], acc[MPU6515_AXIS_Y], acc[MPU6515_AXIS_Z]);
if (atomic_read(&obj->trace) & MPU6515_TRC_IOCTL)
{
GSE_LOG("gsensor data: %s!\n", buf);
}
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_ReadRawData(struct i2c_client *client, char *buf)
{
struct mpu6515_i2c_data *obj = (struct mpu6515_i2c_data*)i2c_get_clientdata(client);
int res = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if (!buf || !client)
{
return EINVAL;
}
if (atomic_read(&obj->suspend))
{
return EIO;
}
if ((res = MPU6515_ReadData(client, obj->data)))
{
GSE_ERR("I2C error: ret value=%d", res);
return EIO;
}
else
{
sprintf(buf, "%04x %04x %04x", obj->data[MPU6515_AXIS_X],
obj->data[MPU6515_AXIS_Y], obj->data[MPU6515_AXIS_Z]);
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_InitSelfTest(struct i2c_client *client)
{
int res = 0;
u8 data;
res = MPU6515_SetBWRate(client, MPU6515_BW_184HZ);
if (res != MPU6515_SUCCESS ) //0x2C->BW=100Hz
{
return res;
}
res = mpu_i2c_read_block(client, MPU6515_REG_DATA_FORMAT, &data, 1);
if (res != MPU6515_SUCCESS)
{
return res;
}
return MPU6515_SUCCESS;
}
/*----------------------------------------------------------------------------*/
static int MPU6515_JudgeTestResult(struct i2c_client *client, s32 prv[MPU6515_AXES_NUM], s32 nxt[MPU6515_AXES_NUM])
{
struct criteria
{
int min;
int max;
};
struct criteria self[4][3] = {
{{ 0, 540}, { 0, 540}, { 0, 875}},
{{ 0, 270}, { 0, 270}, { 0, 438}},
{{ 0, 135}, { 0, 135}, { 0, 219}},
{{ 0, 67}, { 0, 67}, { 0, 110}},
};
struct criteria (*ptr)[3] = NULL;
u8 format;
int res;
if ((res = mpu_i2c_read_block(client, MPU6515_REG_DATA_FORMAT, &format, 1)))
return res;
format = format & MPU6515_RANGE_16G;
switch (format)
{
case MPU6515_RANGE_2G:
GSE_LOG("format use self[0]\n");
ptr = &self[0];
break;
case MPU6515_RANGE_4G:
GSE_LOG("format use self[1]\n");
ptr = &self[1];
break;
case MPU6515_RANGE_8G:
GSE_LOG("format use self[2]\n");
ptr = &self[2];
break;
case MPU6515_RANGE_16G:
GSE_LOG("format use self[3]\n");
ptr = &self[3];
break;
default:
GSE_LOG("format unknow use \n");
break;
}
if (!ptr)
{
GSE_ERR("null pointer\n");
return -EINVAL;
}
GSE_LOG("format=0x%x\n",format);
GSE_LOG("X diff is %ld\n",abs(nxt[MPU6515_AXIS_X] - prv[MPU6515_AXIS_X]));
GSE_LOG("Y diff is %ld\n",abs(nxt[MPU6515_AXIS_Y] - prv[MPU6515_AXIS_Y]));
GSE_LOG("Z diff is %ld\n",abs(nxt[MPU6515_AXIS_Z] - prv[MPU6515_AXIS_Z]));
if ((abs(nxt[MPU6515_AXIS_X] - prv[MPU6515_AXIS_X]) > (*ptr)[MPU6515_AXIS_X].max) ||
(abs(nxt[MPU6515_AXIS_X] - prv[MPU6515_AXIS_X]) < (*ptr)[MPU6515_AXIS_X].min))
{
GSE_ERR("X is over range\n");
res = -EINVAL;
}
if ((abs(nxt[MPU6515_AXIS_Y] - prv[MPU6515_AXIS_Y]) > (*ptr)[MPU6515_AXIS_Y].max) ||
(abs(nxt[MPU6515_AXIS_Y] - prv[MPU6515_AXIS_Y]) < (*ptr)[MPU6515_AXIS_Y].min))
{
GSE_ERR("Y is over range\n");
res = -EINVAL;
}
if ((abs(nxt[MPU6515_AXIS_Z] - prv[MPU6515_AXIS_Z]) > (*ptr)[MPU6515_AXIS_Z].max) ||
(abs(nxt[MPU6515_AXIS_Z] - prv[MPU6515_AXIS_Z]) < (*ptr)[MPU6515_AXIS_Z].min))
{
GSE_ERR("Z is over range\n");
res = -EINVAL;
}
return res;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_chipinfo_value(struct device_driver *ddri, char *buf)
{
struct i2c_client *client = mpu6515_i2c_client;
char strbuf[MPU6515_BUFSIZE];
if (NULL == client)
{
GSE_ERR("i2c client is null!!\n");
return 0;
}
if (sensor_power == false)
{
MPU6515_SetPowerMode(client, true);
msleep(50);
}
MPU6515_ReadAllReg(client, strbuf, MPU6515_BUFSIZE);
MPU6515_ReadChipInfo(client, strbuf, MPU6515_BUFSIZE);
return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
}
/*----------------------------------------------------------------------------*/
static ssize_t show_sensordata_value(struct device_driver *ddri, char *buf)
{
struct i2c_client *client = mpu6515_i2c_client;
char strbuf[MPU6515_BUFSIZE];
if (NULL == client)
{
GSE_ERR("i2c client is null!!\n");
return 0;
}
MPU6515_ReadSensorData(client, strbuf, MPU6515_BUFSIZE);
return snprintf(buf, PAGE_SIZE, "%s\n", strbuf);
}
/*----------------------------------------------------------------------------*/
static ssize_t show_cali_value(struct device_driver *ddri, char *buf)
{
struct i2c_client *client = mpu6515_i2c_client;
struct mpu6515_i2c_data *obj;
int err, len = 0, mul;
int tmp[MPU6515_AXES_NUM];
if (NULL == client)
{
GSE_ERR("i2c client is null!!\n");
return 0;
}
obj = i2c_get_clientdata(client);
if ((err = MPU6515_ReadOffset(client, obj->offset)))
{
return -EINVAL;
}
else if ((err = MPU6515_ReadCalibration(client, tmp)))
{
return -EINVAL;
}
else
{
mul = obj->reso->sensitivity/mpu6515_offset_resolution.sensitivity;
len += snprintf(buf+len, PAGE_SIZE-len, "[HW ][%d] (%+3d, %+3d, %+3d) : (0x%02X, 0x%02X, 0x%02X)\n", mul,
obj->offset[MPU6515_AXIS_X], obj->offset[MPU6515_AXIS_Y], obj->offset[MPU6515_AXIS_Z],
obj->offset[MPU6515_AXIS_X], obj->offset[MPU6515_AXIS_Y], obj->offset[MPU6515_AXIS_Z]);
len += snprintf(buf+len, PAGE_SIZE-len, "[SW ][%d] (%+3d, %+3d, %+3d)\n", 1,
obj->cali_sw[MPU6515_AXIS_X], obj->cali_sw[MPU6515_AXIS_Y], obj->cali_sw[MPU6515_AXIS_Z]);
len += snprintf(buf+len, PAGE_SIZE-len, "[ALL] (%+3d, %+3d, %+3d) : (%+3d, %+3d, %+3d)\n",
obj->offset[MPU6515_AXIS_X]*mul + obj->cali_sw[MPU6515_AXIS_X],
obj->offset[MPU6515_AXIS_Y]*mul + obj->cali_sw[MPU6515_AXIS_Y],
obj->offset[MPU6515_AXIS_Z]*mul + obj->cali_sw[MPU6515_AXIS_Z],
tmp[MPU6515_AXIS_X], tmp[MPU6515_AXIS_Y], tmp[MPU6515_AXIS_Z]);
return len;
}
}
/*----------------------------------------------------------------------------*/
static ssize_t store_cali_value(struct device_driver *ddri, const char *buf, size_t count)
{
struct i2c_client *client = mpu6515_i2c_client;
int err, x, y, z;
int dat[MPU6515_AXES_NUM];
if (!strncmp(buf, "rst", 3))
{
if ((err = MPU6515_ResetCalibration(client)))
{
GSE_ERR("reset offset err = %d\n", err);
}
}
else if (3 == sscanf(buf, "0x%02X 0x%02X 0x%02X", &x, &y, &z))
{
dat[MPU6515_AXIS_X] = x;
dat[MPU6515_AXIS_Y] = y;
dat[MPU6515_AXIS_Z] = z;
if ((err = MPU6515_WriteCalibration(client, dat)))
{
GSE_ERR("write calibration err = %d\n", err);
}
}
else
{
GSE_ERR("invalid format\n");
}
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_self_value(struct device_driver *ddri, char *buf)
{
struct i2c_client *client = mpu6515_i2c_client;
if (NULL == client)
{
GSE_ERR("i2c client is null!!\n");
return 0;
}
return snprintf(buf, 8, "%s\n", selftestRes);
}
/*----------------------------------------------------------------------------*/
static ssize_t store_self_value(struct device_driver *ddri, const char *buf, size_t count)
{ /*write anything to this register will trigger the process*/
struct item
{
s16 raw[MPU6515_AXES_NUM];
};
struct i2c_client *client = mpu6515_i2c_client;
int idx, res, num;
struct item *prv = NULL, *nxt = NULL;
s32 avg_prv[MPU6515_AXES_NUM] = {0, 0, 0};
s32 avg_nxt[MPU6515_AXES_NUM] = {0, 0, 0};
if (1 != sscanf(buf, "%d", &num))
{
GSE_ERR("parse number fail\n");
return count;
}
else if (num == 0)
{
GSE_ERR("invalid data count\n");
return count;
}
prv = kzalloc(sizeof(*prv) * num, GFP_KERNEL);
nxt = kzalloc(sizeof(*nxt) * num, GFP_KERNEL);
if (!prv || !nxt)
{
goto exit;
}
GSE_LOG("NORMAL:\n");
MPU6515_SetPowerMode(client,true);
msleep(50);
for (idx = 0; idx < num; idx++)
{
if ((res = MPU6515_ReadData(client, prv[idx].raw)))
{
GSE_ERR("read data fail: %d\n", res);
goto exit;
}
avg_prv[MPU6515_AXIS_X] += prv[idx].raw[MPU6515_AXIS_X];
avg_prv[MPU6515_AXIS_Y] += prv[idx].raw[MPU6515_AXIS_Y];
avg_prv[MPU6515_AXIS_Z] += prv[idx].raw[MPU6515_AXIS_Z];
GSE_LOG("[%5d %5d %5d]\n", prv[idx].raw[MPU6515_AXIS_X], prv[idx].raw[MPU6515_AXIS_Y], prv[idx].raw[MPU6515_AXIS_Z]);
}
avg_prv[MPU6515_AXIS_X] /= num;
avg_prv[MPU6515_AXIS_Y] /= num;
avg_prv[MPU6515_AXIS_Z] /= num;
/*initial setting for self test*/
GSE_LOG("SELFTEST:\n");
for (idx = 0; idx < num; idx++)
{
if ((res = MPU6515_ReadData(client, nxt[idx].raw)))
{
GSE_ERR("read data fail: %d\n", res);
goto exit;
}
avg_nxt[MPU6515_AXIS_X] += nxt[idx].raw[MPU6515_AXIS_X];
avg_nxt[MPU6515_AXIS_Y] += nxt[idx].raw[MPU6515_AXIS_Y];
avg_nxt[MPU6515_AXIS_Z] += nxt[idx].raw[MPU6515_AXIS_Z];
GSE_LOG("[%5d %5d %5d]\n", nxt[idx].raw[MPU6515_AXIS_X], nxt[idx].raw[MPU6515_AXIS_Y], nxt[idx].raw[MPU6515_AXIS_Z]);
}
avg_nxt[MPU6515_AXIS_X] /= num;
avg_nxt[MPU6515_AXIS_Y] /= num;
avg_nxt[MPU6515_AXIS_Z] /= num;
GSE_LOG("X: %5d - %5d = %5d \n", avg_nxt[MPU6515_AXIS_X], avg_prv[MPU6515_AXIS_X], avg_nxt[MPU6515_AXIS_X] - avg_prv[MPU6515_AXIS_X]);
GSE_LOG("Y: %5d - %5d = %5d \n", avg_nxt[MPU6515_AXIS_Y], avg_prv[MPU6515_AXIS_Y], avg_nxt[MPU6515_AXIS_Y] - avg_prv[MPU6515_AXIS_Y]);
GSE_LOG("Z: %5d - %5d = %5d \n", avg_nxt[MPU6515_AXIS_Z], avg_prv[MPU6515_AXIS_Z], avg_nxt[MPU6515_AXIS_Z] - avg_prv[MPU6515_AXIS_Z]);
if (!MPU6515_JudgeTestResult(client, avg_prv, avg_nxt))
{
GSE_LOG("SELFTEST : PASS\n");
strcpy(selftestRes,"y");
}
else
{
GSE_LOG("SELFTEST : FAIL\n");
strcpy(selftestRes,"n");
}
exit:
/*restore the setting*/
mpu6515_init_client(client, 0);
kfree(prv);
kfree(nxt);
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_selftest_value(struct device_driver *ddri, char *buf)
{
struct i2c_client *client = mpu6515_i2c_client;
struct mpu6515_i2c_data *obj;
if (NULL == client)
{
GSE_ERR("i2c client is null!!\n");
return 0;
}
obj = i2c_get_clientdata(client);
return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&obj->selftest));
}
/*----------------------------------------------------------------------------*/
static ssize_t store_selftest_value(struct device_driver *ddri, const char *buf, size_t count)
{
struct mpu6515_i2c_data *obj = obj_i2c_data;
int tmp;
if (NULL == obj)
{
GSE_ERR("i2c data obj is null!!\n");
return 0;
}
if (1 == sscanf(buf, "%d", &tmp))
{
if (atomic_read(&obj->selftest) && !tmp)
{
/*enable -> disable*/
mpu6515_init_client(obj->client, 0);
}
else if (!atomic_read(&obj->selftest) && tmp)
{
/*disable -> enable*/
MPU6515_InitSelfTest(obj->client);
}
GSE_LOG("selftest: %d => %d\n", atomic_read(&obj->selftest), tmp);
atomic_set(&obj->selftest, tmp);
}
else
{
GSE_ERR("invalid content: '%s', length = %d\n", buf, (int)count);
}
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_firlen_value(struct device_driver *ddri, char *buf)
{
#ifdef CONFIG_MPU6515_LOWPASS
struct i2c_client *client = mpu6515_i2c_client;
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
if (atomic_read(&obj->firlen))
{
int idx, len = atomic_read(&obj->firlen);
GSE_LOG("len = %2d, idx = %2d\n", obj->fir.num, obj->fir.idx);
for (idx = 0; idx < len; idx++)
{
GSE_LOG("[%5d %5d %5d]\n", obj->fir.raw[idx][MPU6515_AXIS_X], obj->fir.raw[idx][MPU6515_AXIS_Y], obj->fir.raw[idx][MPU6515_AXIS_Z]);
}
GSE_LOG("sum = [%5d %5d %5d]\n", obj->fir.sum[MPU6515_AXIS_X], obj->fir.sum[MPU6515_AXIS_Y], obj->fir.sum[MPU6515_AXIS_Z]);
GSE_LOG("avg = [%5d %5d %5d]\n", obj->fir.sum[MPU6515_AXIS_X]/len, obj->fir.sum[MPU6515_AXIS_Y]/len, obj->fir.sum[MPU6515_AXIS_Z]/len);
}
return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&obj->firlen));
#else
return snprintf(buf, PAGE_SIZE, "not support\n");
#endif
}
/*----------------------------------------------------------------------------*/
static ssize_t store_firlen_value(struct device_driver *ddri, const char *buf, size_t count)
{
#ifdef CONFIG_MPU6515_LOWPASS
struct i2c_client *client = mpu6515_i2c_client;
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int firlen;
if (1 != sscanf(buf, "%d", &firlen))
{
GSE_ERR("invallid format\n");
}
else if (firlen > C_MAX_FIR_LENGTH)
{
GSE_ERR("exceeds maximum filter length\n");
}
else
{
atomic_set(&obj->firlen, firlen);
if (0 == firlen)
{
atomic_set(&obj->fir_en, 0);
}
else
{
memset(&obj->fir, 0x00, sizeof(obj->fir));
atomic_set(&obj->fir_en, 1);
}
}
#endif
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_trace_value(struct device_driver *ddri, char *buf)
{
ssize_t res;
struct mpu6515_i2c_data *obj = obj_i2c_data;
if (obj == NULL)
{
GSE_ERR("i2c_data obj is null!!\n");
return 0;
}
res = snprintf(buf, PAGE_SIZE, "0x%04X\n", atomic_read(&obj->trace));
return res;
}
/*----------------------------------------------------------------------------*/
static ssize_t store_trace_value(struct device_driver *ddri, const char *buf, size_t count)
{
struct mpu6515_i2c_data *obj = obj_i2c_data;
int trace;
if (obj == NULL)
{
GSE_ERR("i2c_data obj is null!!\n");
return 0;
}
if (1 == sscanf(buf, "0x%x", &trace))
{
atomic_set(&obj->trace, trace);
}
else
{
GSE_ERR("invalid content: '%s', length = %d\n", buf, (int)count);
}
return count;
}
/*----------------------------------------------------------------------------*/
static ssize_t show_status_value(struct device_driver *ddri, char *buf)
{
ssize_t len = 0;
int err;
struct mpu6515_i2c_data *obj = obj_i2c_data;
u8 dat = 0;
if (obj == NULL)
{
GSE_ERR("i2c_data obj is null!!\n");
return 0;
}
if ((err = mpu_i2c_read_block(obj->client, MPU6515_REG_POWER_CTL, &dat, 1)))
{
GSE_ERR("write data format fail!!\n");
return err;
}
if (obj->hw)
{
len += snprintf(buf+len, PAGE_SIZE-len, "CUST: %d %d (%d %d), %x\n",
obj->hw->i2c_num, obj->hw->direction, obj->hw->power_id, obj->hw->power_vol, dat);
}
else
{
len += snprintf(buf+len, PAGE_SIZE-len, "CUST: NULL\n");
}
return len;
}
/*----------------------------------------------------------------------------*/
static DRIVER_ATTR(chipinfo, S_IRUGO, show_chipinfo_value, NULL);
static DRIVER_ATTR(sensordata, S_IRUGO, show_sensordata_value, NULL);
static DRIVER_ATTR(cali, S_IWUSR | S_IRUGO, show_cali_value, store_cali_value);
static DRIVER_ATTR(self, S_IWUSR | S_IRUGO, show_selftest_value, store_selftest_value);
static DRIVER_ATTR(selftest, S_IWUSR | S_IRUGO, show_self_value , store_self_value );
static DRIVER_ATTR(firlen, S_IWUSR | S_IRUGO, show_firlen_value, store_firlen_value);
static DRIVER_ATTR(trace, S_IWUSR | S_IRUGO, show_trace_value, store_trace_value);
static DRIVER_ATTR(status, S_IRUGO, show_status_value, NULL);
/*----------------------------------------------------------------------------*/
static struct driver_attribute *mpu6515_attr_list[] = {
&driver_attr_chipinfo, /*chip information*/
&driver_attr_sensordata, /*dump sensor data*/
&driver_attr_cali, /*show calibration data*/
&driver_attr_self, /*self test demo*/
&driver_attr_selftest, /*self control: 0: disable, 1: enable*/
&driver_attr_firlen, /*filter length: 0: disable, others: enable*/
&driver_attr_trace, /*trace log*/
&driver_attr_status,
};
/*----------------------------------------------------------------------------*/
static int mpu6515_create_attr(struct device_driver *driver)
{
int idx, err = 0;
int num = (int)(sizeof(mpu6515_attr_list)/sizeof(mpu6515_attr_list[0]));
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if (driver == NULL)
{
return -EINVAL;
}
for (idx = 0; idx < num; idx++)
{
if (0 != (err = driver_create_file(driver, mpu6515_attr_list[idx])))
{
GSE_ERR("driver_create_file (%s) = %d\n", mpu6515_attr_list[idx]->attr.name, err);
break;
}
}
return err;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_delete_attr(struct device_driver *driver)
{
int idx ,err = 0;
int num = (int)(sizeof(mpu6515_attr_list)/sizeof(mpu6515_attr_list[0]));
if (driver == NULL)
{
return -EINVAL;
}
for (idx = 0; idx < num; idx++)
{
driver_remove_file(driver, mpu6515_attr_list[idx]);
}
return err;
}
/*----------------------------------------------------------------------------*/
#ifdef CUSTOM_KERNEL_SENSORHUB
static void gsensor_irq_work(struct work_struct *work)
{
struct mpu6515_i2c_data *obj = obj_i2c_data;
struct scp_acc_hw scp_hw;
MPU6515_CUST_DATA *p_cust_data;
SCP_SENSOR_HUB_DATA data;
int max_cust_data_size_per_packet;
int i;
uint sizeOfCustData;
uint len;
char *p = (char *)&scp_hw;
GSE_FUN();
scp_hw.i2c_num = obj->hw->i2c_num;
scp_hw.direction = obj->hw->direction;
scp_hw.power_id = obj->hw->power_id;
scp_hw.power_vol = obj->hw->power_vol;
scp_hw.firlen = obj->hw->firlen;
memcpy(scp_hw.i2c_addr, obj->hw->i2c_addr, sizeof(obj->hw->i2c_addr));
scp_hw.power_vio_id = obj->hw->power_vio_id;
scp_hw.power_vio_vol = obj->hw->power_vio_vol;
scp_hw.is_batch_supported = obj->hw->is_batch_supported;
p_cust_data = (MPU6515_CUST_DATA *)data.set_cust_req.custData;
sizeOfCustData = sizeof(scp_hw);
max_cust_data_size_per_packet = sizeof(data.set_cust_req.custData) - offsetof(MPU6515_SET_CUST, data);
for (i=0;sizeOfCustData>0;i++)
{
data.set_cust_req.sensorType = ID_ACCELEROMETER;
data.set_cust_req.action = SENSOR_HUB_SET_CUST;
p_cust_data->setCust.action = MPU6515_CUST_ACTION_SET_CUST;
p_cust_data->setCust.part = i;
if (sizeOfCustData > max_cust_data_size_per_packet)
{
len = max_cust_data_size_per_packet;
}
else
{
len = sizeOfCustData;
}
memcpy(p_cust_data->setCust.data, p, len);
sizeOfCustData -= len;
p += len;
len += offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + offsetof(MPU6515_SET_CUST, data);
SCP_sensorHub_req_send(&data, &len, 1);
}
p_cust_data = (MPU6515_CUST_DATA *)&data.set_cust_req.custData;
data.set_cust_req.sensorType = ID_ACCELEROMETER;
data.set_cust_req.action = SENSOR_HUB_SET_CUST;
p_cust_data->resetCali.action = MPU6515_CUST_ACTION_RESET_CALI;
len = offsetof(SCP_SENSOR_HUB_SET_CUST_REQ, custData) + sizeof(p_cust_data->resetCali);
SCP_sensorHub_req_send(&data, &len, 1);
obj->SCP_init_done = 1;
}
/*----------------------------------------------------------------------------*/
static int gsensor_irq_handler(void* data, uint len)
{
struct mpu6515_i2c_data *obj = obj_i2c_data;
SCP_SENSOR_HUB_DATA_P rsp = (SCP_SENSOR_HUB_DATA_P)data;
GSE_FUN();
GSE_ERR("len = %d, type = %d, action = %d, errCode = %d\n", len, rsp->rsp.sensorType, rsp->rsp.action, rsp->rsp.errCode);
if(!obj)
{
return -1;
}
switch(rsp->rsp.action)
{
case SENSOR_HUB_NOTIFY:
switch(rsp->notify_rsp.event)
{
case SCP_INIT_DONE:
schedule_work(&obj->irq_work);
//schedule_delayed_work(&obj->irq_work, HZ);
break;
default:
GSE_ERR("Error sensor hub notify");
break;
}
break;
default:
GSE_ERR("Error sensor hub action");
break;
}
return 0;
}
static int gsensor_setup_irq()
{
int err = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
err = SCP_sensorHub_rsp_registration(ID_ACCELEROMETER, gsensor_irq_handler);
return err;
}
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
/******************************************************************************
* Function Configuration
******************************************************************************/
static int mpu6515_open(struct inode *inode, struct file *file)
{
file->private_data = mpu6515_i2c_client;
if (file->private_data == NULL)
{
GSE_ERR("null pointer!!\n");
return -EINVAL;
}
return nonseekable_open(inode, file);
}
/*----------------------------------------------------------------------------*/
static int mpu6515_release(struct inode *inode, struct file *file)
{
file->private_data = NULL;
return 0;
}
/*----------------------------------------------------------------------------*/
static long mpu6515_unlocked_ioctl(struct file *file, unsigned int cmd,
unsigned long arg)
{
struct i2c_client *client = (struct i2c_client*)file->private_data;
struct mpu6515_i2c_data *obj = (struct mpu6515_i2c_data*)i2c_get_clientdata(client);
char strbuf[MPU6515_BUFSIZE];
void __user *data;
SENSOR_DATA sensor_data;
long err = 0;
int cali[3];
#ifdef GSENSOR_UT
GSE_FUN();
#endif
if (_IOC_DIR(cmd) & _IOC_READ)
{
err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
}
else if (_IOC_DIR(cmd) & _IOC_WRITE)
{
err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
}
if (err)
{
GSE_ERR("access error: %08X, (%2d, %2d)\n", cmd, _IOC_DIR(cmd), _IOC_SIZE(cmd));
return -EFAULT;
}
switch (cmd)
{
case GSENSOR_IOCTL_INIT:
mpu6515_init_client(client, 0);
break;
case GSENSOR_IOCTL_READ_CHIPINFO:
data = (void __user *) arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
MPU6515_ReadChipInfo(client, strbuf, MPU6515_BUFSIZE);
if (copy_to_user(data, strbuf, strlen(strbuf)+1))
{
err = -EFAULT;
break;
}
break;
case GSENSOR_IOCTL_READ_SENSORDATA:
data = (void __user *) arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
mutex_lock(&gsensor_mutex);
MPU6515_SetPowerMode(client, true);
MPU6515_ReadSensorData(client, strbuf, MPU6515_BUFSIZE);
mutex_unlock(&gsensor_mutex);
if (copy_to_user(data, strbuf, strlen(strbuf)+1))
{
err = -EFAULT;
break;
}
break;
case GSENSOR_IOCTL_READ_GAIN:
data = (void __user *) arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
if (copy_to_user(data, &gsensor_gain, sizeof(GSENSOR_VECTOR3D)))
{
err = -EFAULT;
break;
}
break;
case GSENSOR_IOCTL_READ_RAW_DATA:
data = (void __user *) arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
if (atomic_read(&obj->suspend))
{
err = -EINVAL;
}
else
{
MPU6515_ReadRawData(client, strbuf);
if (copy_to_user(data, strbuf, strlen(strbuf)+1))
{
err = -EFAULT;
break;
}
}
break;
case GSENSOR_IOCTL_SET_CALI:
data = (void __user*)arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
if (copy_from_user(&sensor_data, data, sizeof(sensor_data)))
{
err = -EFAULT;
break;
}
if (atomic_read(&obj->suspend))
{
GSE_ERR("Perform calibration in suspend state!!\n");
err = -EINVAL;
}
else
{
cali[MPU6515_AXIS_X] = sensor_data.x * obj->reso->sensitivity / GRAVITY_EARTH_1000;
cali[MPU6515_AXIS_Y] = sensor_data.y * obj->reso->sensitivity / GRAVITY_EARTH_1000;
cali[MPU6515_AXIS_Z] = sensor_data.z * obj->reso->sensitivity / GRAVITY_EARTH_1000;
err = MPU6515_WriteCalibration(client, cali);
}
break;
case GSENSOR_IOCTL_CLR_CALI:
err = MPU6515_ResetCalibration(client);
break;
case GSENSOR_IOCTL_GET_CALI:
data = (void __user*)arg;
if (data == NULL)
{
err = -EINVAL;
break;
}
if ((err = MPU6515_ReadCalibration(client, cali)))
{
break;
}
sensor_data.x = cali[MPU6515_AXIS_X] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
sensor_data.y = cali[MPU6515_AXIS_Y] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
sensor_data.z = cali[MPU6515_AXIS_Z] * GRAVITY_EARTH_1000 / obj->reso->sensitivity;
if (copy_to_user(data, &sensor_data, sizeof(sensor_data)))
{
err = -EFAULT;
break;
}
break;
default:
GSE_ERR("unknown IOCTL: 0x%08x\n", cmd);
err = -ENOIOCTLCMD;
break;
}
return err;
}
#if IS_ENABLED(CONFIG_COMPAT)
static long compat_mpu6515_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
long ret;
GSE_FUN();
if (!filp->f_op || !filp->f_op->unlocked_ioctl) {
GSE_ERR("compat_ion_ioctl file has no f_op or no f_op->unlocked_ioctl.\n");
return -ENOTTY;
}
switch (cmd) {
case GSENSOR_IOCTL_SET_CALI:
case GSENSOR_IOCTL_CLR_CALI:
case GSENSOR_IOCTL_GET_CALI:
return filp->f_op->unlocked_ioctl(filp, cmd,
(unsigned long)compat_ptr(arg));
default: {
GSE_ERR("compat_ion_ioctl : No such command!! 0x%x\n", cmd);
return -ENOIOCTLCMD;
}
}
}
#endif
/*----------------------------------------------------------------------------*/
static struct file_operations mpu6515_fops = {
.open = mpu6515_open,
.release = mpu6515_release,
.unlocked_ioctl = mpu6515_unlocked_ioctl,
#if IS_ENABLED(CONFIG_COMPAT)
.compat_ioctl = compat_mpu6515_unlocked_ioctl,
#endif
};
/*----------------------------------------------------------------------------*/
static struct miscdevice mpu6515_device = {
.minor = MISC_DYNAMIC_MINOR,
.name = "gsensor",
.fops = &mpu6515_fops,
};
/*----------------------------------------------------------------------------*/
#if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
/*----------------------------------------------------------------------------*/
static int mpu6515_suspend(struct i2c_client *client, pm_message_t msg)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int err = 0;
GSE_FUN();
if (msg.event == PM_EVENT_SUSPEND)
{
if (obj == NULL)
{
GSE_ERR("null pointer!!\n");
return -EINVAL;
}
//mutex_lock(&gsensor_mutex);
atomic_set(&obj->suspend, 1);
#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = MPU6515_SetPowerMode(obj->client, false)))
#else //#ifndef CUSTOM_KERNEL_SENSORHUB
if (0)//(err = MPU6515_SCP_SetPowerMode(false, ID_ACCELEROMETER))) //need not disable g sensor in suspend mode if use sensor hub.
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
{
GSE_ERR("write power control fail!!\n");
return err;
}
//mutex_unlock(&gsensor_mutex);
#ifndef CUSTOM_KERNEL_SENSORHUB
MPU6515_power(obj->hw, 0);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
GSE_LOG("mpu6515_suspend ok\n");
}
return err;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_resume(struct i2c_client *client)
{
struct mpu6515_i2c_data *obj = i2c_get_clientdata(client);
int err;
GSE_FUN();
if (obj == NULL)
{
GSE_ERR("null pointer!!\n");
return -EINVAL;
}
#ifndef CUSTOM_KERNEL_SENSORHUB
MPU6515_power(obj->hw, 1);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
//mutex_lock(&gsensor_mutex);
#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = mpu6515_init_client(client, 0)))
#else //#ifndef CUSTOM_KERNEL_SENSORHUB
if (0)//(err = MPU6515_SCP_SetPowerMode(enable_status, ID_ACCELEROMETER))) //need not disable g sensor in suspend mode if use sensor hub.
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
{
GSE_ERR("initialize client fail!!\n");
return err;
}
atomic_set(&obj->suspend, 0);
//mutex_unlock(&gsensor_mutex);
GSE_LOG("mpu6515_resume ok\n");
return 0;
}
/*----------------------------------------------------------------------------*/
#else //#if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
/*----------------------------------------------------------------------------*/
static void mpu6515_early_suspend(struct early_suspend *h)
{
struct mpu6515_i2c_data *obj = container_of(h, struct mpu6515_i2c_data, early_drv);
int err;
GSE_FUN();
if (obj == NULL)
{
GSE_ERR("null pointer!!\n");
return;
}
//mutex_lock(&gsensor_mutex);
atomic_set(&obj->suspend, 1);
#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = MPU6515_SetPowerMode(obj->client, false)))
#else //#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = MPU6515_SCP_SetPowerMode(false, ID_ACCELEROMETER)))
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
{
GSE_ERR("write power control fail!!\n");
return;
}
#ifndef CUSTOM_KERNEL_SENSORHUB
if (MPU6515_gyro_mode() == false)
{
MPU6515_Dev_Reset(obj->client);
MPU6515_Reset(obj->client);
sensor_power = true;
MPU6515_SetPowerMode(obj->client, false);
}
obj->bandwidth = 0;
//mutex_unlock(&gsensor_mutex);
MPU6515_power(obj->hw, 0);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
}
/*----------------------------------------------------------------------------*/
static void mpu6515_late_resume(struct early_suspend *h)
{
struct mpu6515_i2c_data *obj = container_of(h, struct mpu6515_i2c_data, early_drv);
int err;
GSE_FUN();
if (obj == NULL)
{
GSE_ERR("null pointer!!\n");
return;
}
#ifndef CUSTOM_KERNEL_SENSORHUB
MPU6515_power(obj->hw, 1);
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
//mutex_lock(&gsensor_mutex);
#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = mpu6515_init_client(obj->client, 0)))
#else //#ifndef CUSTOM_KERNEL_SENSORHUB
if ((err = MPU6515_SCP_SetPowerMode(enable_status, ID_ACCELEROMETER)))
#endif //#ifndef CUSTOM_KERNEL_SENSORHUB
{
GSE_ERR("initialize client fail!!\n");
return;
}
atomic_set(&obj->suspend, 0);
//mutex_unlock(&gsensor_mutex);
}
/*----------------------------------------------------------------------------*/
#endif //#if !defined(CONFIG_HAS_EARLYSUSPEND) || !defined(USE_EARLY_SUSPEND)
/*----------------------------------------------------------------------------*/
// if use this typ of enable , Gsensor should report inputEvent(x, y, z ,stats, div) to HAL
static int gsensor_open_report_data(int open)
{
//should queuq work to report event if is_report_input_direct=true
return 0;
}
/*----------------------------------------------------------------------------*/
// if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL
#ifndef CUSTOM_KERNEL_SENSORHUB
static int gsensor_enable_nodata(int en)
{
int err = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
mutex_lock(&gsensor_mutex);
if(((en == 0) && (sensor_power == false)) ||((en == 1) && (sensor_power == true)))
{
enable_status = sensor_power;
GSE_LOG("Gsensor device have updated!\n");
}
else
{
enable_status = !sensor_power;
if (atomic_read(&obj_i2c_data->suspend) == 0)
{
err = MPU6515_SetPowerMode(obj_i2c_data->client, enable_status);
GSE_LOG("Gsensor not in suspend gsensor_SetPowerMode!, enable_status = %d\n",enable_status);
}
else
{
GSE_LOG("Gsensor in suspend and can not enable or disable!enable_status = %d\n",enable_status);
}
}
mutex_unlock(&gsensor_mutex);
if(err != MPU6515_SUCCESS)
{
printk("gsensor_enable_nodata fail!\n");
return -1;
}
printk("gsensor_enable_nodata OK!!!\n");
return 0;
}
#endif
/*----------------------------------------------------------------------------*/
// if use this typ of enable , Gsensor only enabled but not report inputEvent to HAL
#ifdef CUSTOM_KERNEL_SENSORHUB
static int scp_gsensor_enable_nodata(int en)
{
int err = 0;
#ifdef GSENSOR_UT
GSE_FUN();
#endif
mutex_lock(&gsensor_mutex);
if(((en == 0) && (scp_sensor_power == false)) ||((en == 1) && (scp_sensor_power == true)))
{
enable_status = scp_sensor_power;
GSE_LOG("Gsensor device have updated!\n");
}
else
{
enable_status = !scp_sensor_power;
if (atomic_read(&obj_i2c_data->suspend) == 0)
{
err = MPU6515_SCP_SetPowerMode(en, ID_ACCELEROMETER);
if (0 == err)
{
scp_sensor_power = enable_status;
}
GSE_LOG("Gsensor not in suspend gsensor_SetPowerMode!, enable_status = %d\n",scp_sensor_power);
}
else
{
GSE_LOG("Gsensor in suspend and can not enable or disable!enable_status = %d\n",scp_sensor_power);
}
}
mutex_unlock(&gsensor_mutex);
if(err != MPU6515_SUCCESS)
{
printk("scp_gsensor_enable_nodata fail!\n");
return -1;
}
printk("scp_gsensor_enable_nodata OK!!!\n");
return 0;
}
#endif
/*----------------------------------------------------------------------------*/
static int gsensor_set_delay(u64 ns)
{
int err = 0;
int value;
#ifdef CUSTOM_KERNEL_SENSORHUB
SCP_SENSOR_HUB_DATA req;
int len;
#else//#ifdef CUSTOM_KERNEL_SENSORHUB
int sample_delay;
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
#ifdef GSENSOR_UT
GSE_FUN();
#endif
value = (int)ns/1000/1000;
#ifdef CUSTOM_KERNEL_SENSORHUB
req.set_delay_req.sensorType = ID_ACCELEROMETER;
req.set_delay_req.action = SENSOR_HUB_SET_DELAY;
req.set_delay_req.delay = value;
len = sizeof(req.activate_req);
err = SCP_sensorHub_req_send(&req, &len, 1);
if (err)
{
GSE_ERR("SCP_sensorHub_req_send!\n");
return err;
}
#else//#ifdef CUSTOM_KERNEL_SENSORHUB
if(value <= 5)
{
sample_delay = MPU6515_BW_184HZ;
}
else if(value <= 10)
{
sample_delay = MPU6515_BW_92HZ;
}
else
{
sample_delay = MPU6515_BW_41HZ;
}
mutex_lock(&gsensor_mutex);
err = MPU6515_SetBWRate(obj_i2c_data->client, sample_delay);
mutex_unlock(&gsensor_mutex);
if(err != MPU6515_SUCCESS ) //0x2C->BW=100Hz
{
GSE_ERR("Set delay parameter error!\n");
return -1;
}
if(value >= 50)
{
atomic_set(&obj_i2c_data->filter, 0);
}
else
{
#if defined(CONFIG_MPU6515_LOWPASS)
obj_i2c_data->fir.num = 0;
obj_i2c_data->fir.idx = 0;
obj_i2c_data->fir.sum[MPU6515_AXIS_X] = 0;
obj_i2c_data->fir.sum[MPU6515_AXIS_Y] = 0;
obj_i2c_data->fir.sum[MPU6515_AXIS_Z] = 0;
atomic_set(&obj_i2c_data->filter, 1);
#endif
}
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
GSE_LOG("gsensor_set_delay (%d)\n",value);
return 0;
}
/*----------------------------------------------------------------------------*/
static int gsensor_get_data(int* x ,int* y,int* z, int* status)
{
#ifdef CUSTOM_KERNEL_SENSORHUB
SCP_SENSOR_HUB_DATA req;
int len;
int err = 0;
#else
char buff[MPU6515_BUFSIZE];
#endif //#ifdef CUSTOM_KERNEL_SENSORHUB
//GSE_FUN();
#ifdef CUSTOM_KERNEL_SENSORHUB
req.get_data_req.sensorType = ID_ACCELEROMETER;
req.get_data_req.action = SENSOR_HUB_GET_DATA;
len = sizeof(req.get_data_req);
err = SCP_sensorHub_req_send(&req, &len, 1);
if (err)
{
GSE_ERR("SCP_sensorHub_req_send!\n");
return err;
}
if (ID_ACCELEROMETER != req.get_data_rsp.sensorType ||
SENSOR_HUB_GET_DATA != req.get_data_rsp.action ||
0 != req.get_data_rsp.errCode)
{
GSE_ERR("error : %d\n", req.get_data_rsp.errCode);
return req.get_data_rsp.errCode;
}
//sscanf(buff, "%x %x %x", req.get_data_rsp.int16_Data[0], req.get_data_rsp.int16_Data[1], req.get_data_rsp.int16_Data[2]);
*x = (int)req.get_data_rsp.int16_Data[0]*GRAVITY_EARTH_1000/1000;
*y = (int)req.get_data_rsp.int16_Data[1]*GRAVITY_EARTH_1000/1000;
*z = (int)req.get_data_rsp.int16_Data[2]*GRAVITY_EARTH_1000/1000;
//GSE_ERR("x = %d, y = %d, z = %d\n", *x, *y, *z);
*status = SENSOR_STATUS_ACCURACY_MEDIUM;
if(atomic_read(&obj_i2c_data->trace) & MPU6515_TRC_RAWDATA)
{
GSE_ERR("x = %d, y = %d, z = %d\n", *x, *y, *z);
}
#else//#ifdef CUSTOM_KERNEL_SENSORHUB
mutex_lock(&gsensor_mutex);
MPU6515_ReadSensorData(obj_i2c_data->client, buff, MPU6515_BUFSIZE);
mutex_unlock(&gsensor_mutex);
sscanf(buff, "%x %x %x", x, y, z);
*status = SENSOR_STATUS_ACCURACY_MEDIUM;
#endif
return 0;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_i2c_detect(struct i2c_client *client, struct i2c_board_info *info)
{
strcpy(info->type, MPU6515_DEV_NAME);
return 0;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
struct i2c_client *new_client;
struct mpu6515_i2c_data *obj;
struct acc_control_path ctl={0};
struct acc_data_path data={0};
int err = 0;
GSE_FUN();
if (!(obj = kzalloc(sizeof(*obj), GFP_KERNEL)))
{
err = -ENOMEM;
goto exit;
}
memset(obj, 0, sizeof(struct mpu6515_i2c_data));
obj->hw = get_cust_acc_hw();
if ((err = hwmsen_get_convert(obj->hw->direction, &obj->cvt)))
{
GSE_ERR("invalid direction: %d\n", obj->hw->direction);
goto exit;
}
#ifdef CUSTOM_KERNEL_SENSORHUB
INIT_WORK(&obj->irq_work, gsensor_irq_work);
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
obj_i2c_data = obj;
obj->client = client;
#ifdef FPGA_EARLY_PORTING
obj->client->timing = 100;
#else
obj->client->timing = 400;
#endif
new_client = obj->client;
i2c_set_clientdata(new_client,obj);
atomic_set(&obj->trace, 0);
atomic_set(&obj->suspend, 0);
#ifdef CUSTOM_KERNEL_SENSORHUB
obj->SCP_init_done = 0;
#endif//#ifdef CUSTOM_KERNEL_SENSORHUB
#ifdef CONFIG_MPU6515_LOWPASS
if (obj->hw->firlen > C_MAX_FIR_LENGTH)
{
atomic_set(&obj->firlen, C_MAX_FIR_LENGTH);
}
else
{
atomic_set(&obj->firlen, obj->hw->firlen);
}
if (atomic_read(&obj->firlen) > 0)
{
atomic_set(&obj->fir_en, 1);
}
#endif
mpu6515_i2c_client = new_client;
MPU6515_Dev_Reset(new_client);
MPU6515_Reset(new_client);
if ((err = mpu6515_init_client(new_client, 1)))
{
goto exit_init_failed;
}
if ((err = misc_register(&mpu6515_device)))
{
GSE_ERR("mpu6515_device register failed\n");
goto exit_misc_device_register_failed;
}
if ((err = mpu6515_create_attr(&mpu6515_init_info.platform_diver_addr->driver)))
{
GSE_ERR("create attribute err = %d\n", err);
goto exit_create_attr_failed;
}
ctl.open_report_data= gsensor_open_report_data;
#ifdef CUSTOM_KERNEL_SENSORHUB
ctl.enable_nodata = scp_gsensor_enable_nodata;
#else
ctl.enable_nodata = gsensor_enable_nodata;
#endif
ctl.set_delay = gsensor_set_delay;
ctl.is_report_input_direct = false;
#ifdef CUSTOM_KERNEL_SENSORHUB
ctl.is_support_batch = obj->hw->is_batch_supported;
#else
ctl.is_support_batch = false;
#endif
err = acc_register_control_path(&ctl);
if(err)
{
GSE_ERR("register acc control path err\n");
goto exit_create_attr_failed;
}
data.get_data = gsensor_get_data;
data.vender_div = 1000;
err = acc_register_data_path(&data);
if(err)
{
GSE_ERR("register acc data path err\n");
goto exit_create_attr_failed;
}
err = batch_register_support_info(ID_ACCELEROMETER,ctl.is_support_batch, 102, 0); //divisor is 1000/9.8
if(err)
{
GSE_ERR("register gsensor batch support err = %d\n", err);
goto exit_create_attr_failed;
}
#if defined(CONFIG_HAS_EARLYSUSPEND) && defined(USE_EARLY_SUSPEND)
obj->early_drv.level = EARLY_SUSPEND_LEVEL_STOP_DRAWING - 2,
obj->early_drv.suspend = mpu6515_early_suspend,
obj->early_drv.resume = mpu6515_late_resume,
register_early_suspend(&obj->early_drv);
#endif
gsensor_init_flag =0;
GSE_LOG("%s: OK\n", __func__);
return 0;
exit_create_attr_failed:
misc_deregister(&mpu6515_device);
exit_misc_device_register_failed:
exit_init_failed:
//i2c_detach_client(new_client);
exit_kfree:
kfree(obj);
exit:
GSE_ERR("%s: err = %d\n", __func__, err);
gsensor_init_flag = -1;
return err;
}
/*----------------------------------------------------------------------------*/
static int mpu6515_i2c_remove(struct i2c_client *client)
{
int err = 0;
if ((err = mpu6515_delete_attr(&mpu6515_init_info.platform_diver_addr->driver)))
{
GSE_ERR("mpu6515_delete_attr fail: %d\n", err);
}
if ((err = misc_deregister(&mpu6515_device)))
{
GSE_ERR("misc_deregister fail: %d\n", err);
}
mpu6515_i2c_client = NULL;
i2c_unregister_device(client);
kfree(i2c_get_clientdata(client));
return 0;
}
/*----------------------------------------------------------------------------*/
static int gsensor_local_init(void)
{
struct acc_hw *hw = get_cust_acc_hw();
GSE_FUN();
MPU6515_power(hw, 1);
if(i2c_add_driver(&mpu6515_i2c_driver))
{
GSE_ERR("add driver error\n");
return -1;
}
if(-1 == gsensor_init_flag)
{
return -1;
}
return 0;
}
/*----------------------------------------------------------------------------*/
static int gsensor_remove()
{
struct acc_hw *hw = get_cust_acc_hw();
GSE_FUN();
MPU6515_power(hw, 0);
i2c_del_driver(&mpu6515_i2c_driver);
return 0;
}
/*----------------------------------------------------------------------------*/
static int __init mpu6515gse_init(void)
{
struct acc_hw *hw = get_cust_acc_hw();
GSE_LOG("%s: i2c_number=%d\n", __func__,hw->i2c_num);
i2c_register_board_info(hw->i2c_num, &i2c_mpu6515, 1);
acc_driver_add(&mpu6515_init_info);
return 0;
}
/*----------------------------------------------------------------------------*/
static void __exit mpu6515gse_exit(void)
{
GSE_FUN();
}
/*----------------------------------------------------------------------------*/
module_init(mpu6515gse_init);
module_exit(mpu6515gse_exit);
/*----------------------------------------------------------------------------*/
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("MPU6515 gse driver");
MODULE_AUTHOR("Yucong.Xiong@mediatek.com");
|