-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinv_mpu_misc.c
2022 lines (1785 loc) · 54 KB
/
inv_mpu_misc.c
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
/*
* Copyright (C) 2012 Invensense, Inc.
*
* 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.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/sysfs.h>
#include <linux/jiffies.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/kfifo.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/crc32.h>
#include "inv_mpu_iio.h"
#include "inv_test/inv_counters.h"
/* DMP defines */
#define DMP_ORIENTATION_TIME 500
#define DMP_ORIENTATION_ANGLE 60
#define DMP_DEFAULT_FIFO_RATE 200
#define DMP_TAP_SCALE (767603923 / 5)
#define DMP_MULTI_SHIFT 30
#define DMP_MULTI_TAP_TIME 500
#define DMP_SHAKE_REJECT_THRESH 100
#define DMP_SHAKE_REJECT_TIME 10
#define DMP_SHAKE_REJECT_TIMEOUT 10
#define DMP_ANGLE_SCALE 15
#define DMP_PRECISION 1000
#define DMP_MAX_DIVIDER 4
#define DMP_MAX_MIN_TAPS 4
#define DMP_IMAGE_CRC_VALUE 0x5cee889c
/*--- Test parameters defaults --- */
#define DEF_OLDEST_SUPP_PROD_REV 8
#define DEF_OLDEST_SUPP_SW_REV 2
/* sample rate */
#define DEF_SELFTEST_SAMPLE_RATE 0
/* full scale setting dps */
#define DEF_SELFTEST_GYRO_FS (0 << 3)
#define DEF_SELFTEST_ACCEL_FS (2 << 3)
#define DEF_SELFTEST_GYRO_SENS (32768 / 250)
/* wait time before collecting data */
#define DEF_GYRO_WAIT_TIME 10
#define DEF_ST_STABLE_TIME 200
#define DEF_ST_6500_STABLE_TIME 20
#define DEF_GYRO_SCALE 131
#define DEF_ST_PRECISION 1000
#define DEF_ST_ACCEL_FS_MG 8000UL
#define DEF_ST_SCALE (1L << 15)
#define DEF_ST_TRY_TIMES 2
#define DEF_ST_COMPASS_RESULT_SHIFT 2
#define DEF_ST_ACCEL_RESULT_SHIFT 1
#define DEF_ST_OTP0_THRESH 60
#define DEF_ST_ABS_THRESH 20
#define DEF_ST_TOR 2
#define X 0
#define Y 1
#define Z 2
/*---- MPU6050 notable product revisions ----*/
#define MPU_PRODUCT_KEY_B1_E1_5 105
#define MPU_PRODUCT_KEY_B2_F1 431
/* accelerometer Hw self test min and max bias shift (mg) */
#define DEF_ACCEL_ST_SHIFT_MIN 300
#define DEF_ACCEL_ST_SHIFT_MAX 950
#define DEF_ACCEL_ST_SHIFT_DELTA 140
#define DEF_GYRO_CT_SHIFT_DELTA 140
/* gyroscope Coriolis self test min and max bias shift (dps) */
#define DEF_GYRO_CT_SHIFT_MIN 10
#define DEF_GYRO_CT_SHIFT_MAX 105
/*---- MPU6500 Self Test Pass/Fail Criteria ----*/
/* Gyro Offset Max Value (dps) */
#define DEF_GYRO_OFFSET_MAX 20
/* Gyro Self Test Absolute Limits ST_AL (dps) */
#define DEF_GYRO_ST_AL 60
/* Accel Self Test Absolute Limits ST_AL (mg) */
#define DEF_ACCEL_ST_AL_MIN 225
#define DEF_ACCEL_ST_AL_MAX 675
#define DEF_6500_ACCEL_ST_SHIFT_DELTA 500
#define DEF_6500_GYRO_CT_SHIFT_DELTA 500
#define DEF_ST_MPU6500_ACCEL_LPF 2
#define DEF_ST_6500_ACCEL_FS_MG 2000UL
#define DEF_SELFTEST_6500_ACCEL_FS (0 << 3)
/* Note: The ST_AL values are only used when ST_OTP = 0,
* i.e no factory self test values for reference
*/
/* NOTE: product entries are in chronological order */
static const struct prod_rev_map_t prod_rev_map[] = {
/* prod_ver = 0 */
{MPL_PROD_KEY(0, 1), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 2), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 3), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 4), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 5), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 6), MPU_SILICON_REV_A2, 131, 16384},
/* prod_ver = 1 */
{MPL_PROD_KEY(0, 7), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 8), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 9), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 10), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 11), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 12), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 13), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 14), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 15), MPU_SILICON_REV_A2, 131, 16384},
{MPL_PROD_KEY(0, 27), MPU_SILICON_REV_A2, 131, 16384},
/* prod_ver = 1 */
{MPL_PROD_KEY(1, 16), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 17), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 18), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 19), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 20), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 28), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 1), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 2), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 3), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 4), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 5), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(1, 6), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 2 */
{MPL_PROD_KEY(2, 7), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 8), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 9), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 10), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 11), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 12), MPU_SILICON_REV_B1, 131, 16384},
{MPL_PROD_KEY(2, 29), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 3 */
{MPL_PROD_KEY(3, 30), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 4 */
{MPL_PROD_KEY(4, 31), MPU_SILICON_REV_B1, 131, 8192},
{MPL_PROD_KEY(4, 1), MPU_SILICON_REV_B1, 131, 8192},
{MPL_PROD_KEY(4, 3), MPU_SILICON_REV_B1, 131, 8192},
/* prod_ver = 5 */
{MPL_PROD_KEY(5, 3), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 6 */
{MPL_PROD_KEY(6, 19), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 7 */
{MPL_PROD_KEY(7, 19), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 8 */
{MPL_PROD_KEY(8, 19), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 9 */
{MPL_PROD_KEY(9, 19), MPU_SILICON_REV_B1, 131, 16384},
/* prod_ver = 10 */
{MPL_PROD_KEY(10, 19), MPU_SILICON_REV_B1, 131, 16384}
};
/*
* List of product software revisions
*
* NOTE :
* software revision 0 falls back to the old detection method
* based off the product version and product revision per the
* table above
*/
static const struct prod_rev_map_t sw_rev_map[] = {
{0, 0, 0, 0},
{1, MPU_SILICON_REV_B1, 131, 8192}, /* rev C */
{2, MPU_SILICON_REV_B1, 131, 16384} /* rev D */
};
static const u16 mpu_6500_st_tb[256] = {
2620, 2646, 2672, 2699, 2726, 2753, 2781, 2808,
2837, 2865, 2894, 2923, 2952, 2981, 3011, 3041,
3072, 3102, 3133, 3165, 3196, 3228, 3261, 3293,
3326, 3359, 3393, 3427, 3461, 3496, 3531, 3566,
3602, 3638, 3674, 3711, 3748, 3786, 3823, 3862,
3900, 3939, 3979, 4019, 4059, 4099, 4140, 4182,
4224, 4266, 4308, 4352, 4395, 4439, 4483, 4528,
4574, 4619, 4665, 4712, 4759, 4807, 4855, 4903,
4953, 5002, 5052, 5103, 5154, 5205, 5257, 5310,
5363, 5417, 5471, 5525, 5581, 5636, 5693, 5750,
5807, 5865, 5924, 5983, 6043, 6104, 6165, 6226,
6289, 6351, 6415, 6479, 6544, 6609, 6675, 6742,
6810, 6878, 6946, 7016, 7086, 7157, 7229, 7301,
7374, 7448, 7522, 7597, 7673, 7750, 7828, 7906,
7985, 8065, 8145, 8227, 8309, 8392, 8476, 8561,
8647, 8733, 8820, 8909, 8998, 9088, 9178, 9270,
9363, 9457, 9551, 9647, 9743, 9841, 9939, 10038,
10139, 10240, 10343, 10446, 10550, 10656, 10763, 10870,
10979, 11089, 11200, 11312, 11425, 11539, 11654, 11771,
11889, 12008, 12128, 12249, 12371, 12495, 12620, 12746,
12874, 13002, 13132, 13264, 13396, 13530, 13666, 13802,
13940, 14080, 14221, 14363, 14506, 14652, 14798, 14946,
15096, 15247, 15399, 15553, 15709, 15866, 16024, 16184,
16346, 16510, 16675, 16842, 17010, 17180, 17352, 17526,
17701, 17878, 18057, 18237, 18420, 18604, 18790, 18978,
19167, 19359, 19553, 19748, 19946, 20145, 20347, 20550,
20756, 20963, 21173, 21385, 21598, 21814, 22033, 22253,
22475, 22700, 22927, 23156, 23388, 23622, 23858, 24097,
24338, 24581, 24827, 25075, 25326, 25579, 25835, 26093,
26354, 26618, 26884, 27153, 27424, 27699, 27976, 28255,
28538, 28823, 29112, 29403, 29697, 29994, 30294, 30597,
30903, 31212, 31524, 31839, 32157, 32479, 32804
};
static const int accel_st_tb[31] = {
340, 351, 363, 375, 388, 401, 414, 428,
443, 458, 473, 489, 506, 523, 541, 559,
578, 597, 617, 638, 660, 682, 705, 729,
753, 779, 805, 832, 860, 889, 919
};
static const int gyro_6050_st_tb[31] = {
3275, 3425, 3583, 3748, 3920, 4100, 4289, 4486,
4693, 4909, 5134, 5371, 5618, 5876, 6146, 6429,
6725, 7034, 7358, 7696, 8050, 8421, 8808, 9213,
9637, 10080, 10544, 11029, 11537, 12067, 12622
};
static const int gyro_3500_st_tb[255] = {
2620, 2646, 2672, 2699, 2726, 2753, 2781, 2808,
2837, 2865, 2894, 2923, 2952, 2981, 3011, 3041,
3072, 3102, 3133, 3165, 3196, 3228, 3261, 3293,
3326, 3359, 3393, 3427, 3461, 3496, 3531, 3566,
3602, 3638, 3674, 3711, 3748, 3786, 3823, 3862,
3900, 3939, 3979, 4019, 4059, 4099, 4140, 4182,
4224, 4266, 4308, 4352, 4395, 4439, 4483, 4528,
4574, 4619, 4665, 4712, 4759, 4807, 4855, 4903,
4953, 5002, 5052, 5103, 5154, 5205, 5257, 5310,
5363, 5417, 5471, 5525, 5581, 5636, 5693, 5750,
5807, 5865, 5924, 5983, 6043, 6104, 6165, 6226,
6289, 6351, 6415, 6479, 6544, 6609, 6675, 6742,
6810, 6878, 6946, 7016, 7086, 7157, 7229, 7301,
7374, 7448, 7522, 7597, 7673, 7750, 7828, 7906,
7985, 8065, 8145, 8227, 8309, 8392, 8476, 8561,
8647, 8733, 8820, 8909, 8998, 9088, 9178, 9270,
9363, 9457, 9551, 9647, 9743, 9841, 9939, 10038,
10139, 10240, 10343, 10446, 10550, 10656, 10763, 10870,
10979, 11089, 11200, 11312, 11425, 11539, 11654, 11771,
11889, 12008, 12128, 12249, 12371, 12495, 12620, 12746,
12874, 13002, 13132, 13264, 13396, 13530, 13666, 13802,
13940, 14080, 14221, 14363, 14506, 14652, 14798, 14946,
15096, 15247, 15399, 15553, 15709, 15866, 16024, 16184,
16346, 16510, 16675, 16842, 17010, 17180, 17352, 17526,
17701, 17878, 18057, 18237, 18420, 18604, 18790, 18978,
19167, 19359, 19553, 19748, 19946, 20145, 20347, 20550,
20756, 20963, 21173, 21385, 21598, 21814, 22033, 22253,
22475, 22700, 22927, 23156, 23388, 23622, 23858, 24097,
24338, 24581, 24827, 25075, 25326, 25579, 25835, 26093,
26354, 26618, 26884, 27153, 27424, 27699, 27976, 28255,
28538, 28823, 29112, 29403, 29697, 29994, 30294, 30597,
30903, 31212, 31524, 31839, 32157, 32479, 32804
};
char *wr_pr_debug_begin(u8 const *data, u32 len, char *string)
{
int ii;
string = kmalloc(len * 2 + 1, GFP_KERNEL);
for (ii = 0; ii < len; ii++)
sprintf(&string[ii * 2], "%02X", data[ii]);
string[len * 2] = 0;
return string;
}
char *wr_pr_debug_end(char *string)
{
kfree(string);
return "";
}
int mpu_memory_write(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
u32 len, u8 const *data)
{
u8 bank[2];
u8 addr[2];
u8 buf[513];
struct i2c_msg msgs[3];
int res;
if (!data || !st)
return -EINVAL;
if (len >= (sizeof(buf) - 1))
return -ENOMEM;
bank[0] = REG_BANK_SEL;
bank[1] = mem_addr >> 8;
addr[0] = REG_MEM_START_ADDR;
addr[1] = mem_addr & 0xFF;
buf[0] = REG_MEM_RW;
memcpy(buf + 1, data, len);
/* write message */
msgs[0].addr = mpu_addr;
msgs[0].flags = 0;
msgs[0].buf = bank;
msgs[0].len = sizeof(bank);
msgs[1].addr = mpu_addr;
msgs[1].flags = 0;
msgs[1].buf = addr;
msgs[1].len = sizeof(addr);
msgs[2].addr = mpu_addr;
msgs[2].flags = 0;
msgs[2].buf = (u8 *)buf;
msgs[2].len = len + 1;
INV_I2C_INC_MPUWRITE(3 + 3 + (2 + len));
#ifdef CONFIG_DYNAMIC_DEBUG
{
char *write = 0;
pr_debug("%s WM%02X%02X%02X%s%s - %d\n", st->hw->name,
mpu_addr, bank[1], addr[1],
wr_pr_debug_begin(data, len, write),
wr_pr_debug_end(write),
len);
}
#endif
res = i2c_transfer(st->sl_handle, msgs, 3);
if (res != 3) {
if (res >= 0)
res = -EIO;
return res;
} else {
return 0;
}
}
int mpu_memory_read(struct inv_mpu_state *st, u8 mpu_addr, u16 mem_addr,
u32 len, u8 *data)
{
u8 bank[2];
u8 addr[2];
u8 buf;
struct i2c_msg msgs[4];
int res;
if (!data || !st)
return -EINVAL;
bank[0] = REG_BANK_SEL;
bank[1] = mem_addr >> 8;
addr[0] = REG_MEM_START_ADDR;
addr[1] = mem_addr & 0xFF;
buf = REG_MEM_RW;
/* write message */
msgs[0].addr = mpu_addr;
msgs[0].flags = 0;
msgs[0].buf = bank;
msgs[0].len = sizeof(bank);
msgs[1].addr = mpu_addr;
msgs[1].flags = 0;
msgs[1].buf = addr;
msgs[1].len = sizeof(addr);
msgs[2].addr = mpu_addr;
msgs[2].flags = 0;
msgs[2].buf = &buf;
msgs[2].len = 1;
msgs[3].addr = mpu_addr;
msgs[3].flags = I2C_M_RD;
msgs[3].buf = data;
msgs[3].len = len;
res = i2c_transfer(st->sl_handle, msgs, 4);
if (res != 4) {
if (res >= 0)
res = -EIO;
} else
res = 0;
INV_I2C_INC_MPUWRITE(3 + 3 + 3);
INV_I2C_INC_MPUREAD(len);
#ifdef CONFIG_DYNAMIC_DEBUG
{
char *read = 0;
pr_debug("%s RM%02X%02X%02X%02X - %s%s\n", st->hw->name,
mpu_addr, bank[1], addr[1], len,
wr_pr_debug_begin(data, len, read),
wr_pr_debug_end(read));
}
#endif
return res;
}
int mpu_memory_write_unaligned(struct inv_mpu_state *st, u16 key, int len,
u8 const *d)
{
u32 addr;
int start, end;
int len1, len2;
int result = 0;
if (len > MPU_MEM_BANK_SIZE)
return -EINVAL;
addr = inv_dmp_get_address(key);
if (addr > MPU6XXX_MAX_MPU_MEM)
return -EINVAL;
start = (addr >> 8);
end = ((addr + len - 1) >> 8);
if (start == end) {
result = mpu_memory_write(st, st->i2c_addr, addr, len, d);
} else {
end <<= 8;
len1 = end - addr;
len2 = len - len1;
result = mpu_memory_write(st, st->i2c_addr, addr, len1, d);
result |= mpu_memory_write(st, st->i2c_addr, end, len2,
d + len1);
}
return result;
}
/**
* index_of_key()- Inverse lookup of the index of an MPL product key .
* @key: the MPL product indentifier also referred to as 'key'.
*/
static short index_of_key(u16 key)
{
int i;
for (i = 0; i < NUM_OF_PROD_REVS; i++)
if (prod_rev_map[i].mpl_product_key == key)
return (short)i;
return -EINVAL;
}
int inv_get_silicon_rev_mpu6500(struct inv_mpu_state *st)
{
struct inv_chip_info_s *chip_info = &st->chip_info;
int result;
u8 whoami, sw_rev;
result = inv_i2c_read(st, REG_WHOAMI, 1, &whoami);
if (result)
return result;
if (whoami != MPU6500_ID && whoami != MPU9250_ID &&
whoami != MPU9350_ID && whoami != MPU6515_ID)
return -EINVAL;
/*memory read need more time after power up */
msleep(POWER_UP_TIME);
result = mpu_memory_read(st, st->i2c_addr,
MPU6500_MEM_REV_ADDR, 1, &sw_rev);
sw_rev &= INV_MPU_REV_MASK;
if (result)
return result;
if (sw_rev != 0)
return -EINVAL;
/* these values are place holders and not real values */
chip_info->product_id = MPU6500_PRODUCT_REVISION;
chip_info->product_revision = MPU6500_PRODUCT_REVISION;
chip_info->silicon_revision = MPU6500_PRODUCT_REVISION;
chip_info->software_revision = sw_rev;
chip_info->gyro_sens_trim = DEFAULT_GYRO_TRIM;
chip_info->accel_sens_trim = DEFAULT_ACCEL_TRIM;
chip_info->multi = 1;
return 0;
}
int inv_get_silicon_rev_mpu6050(struct inv_mpu_state *st)
{
int result;
struct inv_reg_map_s *reg;
u8 prod_ver = 0x00, prod_rev = 0x00;
struct prod_rev_map_t *p_rev;
u8 bank =
(BIT_PRFTCH_EN | BIT_CFG_USER_BANK | MPU_MEM_OTP_BANK_0);
u16 mem_addr = ((bank << 8) | MEM_ADDR_PROD_REV);
u16 key;
u8 regs[5];
u16 sw_rev;
short index;
struct inv_chip_info_s *chip_info = &st->chip_info;
reg = &st->reg;
result = inv_i2c_read(st, REG_PRODUCT_ID, 1, &prod_ver);
if (result)
return result;
prod_ver &= 0xf;
/*memory read need more time after power up */
msleep(POWER_UP_TIME);
result = mpu_memory_read(st, st->i2c_addr, mem_addr,
1, &prod_rev);
if (result)
return result;
prod_rev >>= 2;
/* clean the prefetch and cfg user bank bits */
result = inv_i2c_single_write(st, reg->bank_sel, 0);
if (result)
return result;
/* get the software-product version, read from XA_OFFS_L */
result = inv_i2c_read(st, REG_XA_OFFS_L_TC,
SOFT_PROD_VER_BYTES, regs);
if (result)
return result;
sw_rev = (regs[4] & 0x01) << 2 | /* 0x0b, bit 0 */
(regs[2] & 0x01) << 1 | /* 0x09, bit 0 */
(regs[0] & 0x01); /* 0x07, bit 0 */
/* if 0, use the product key to determine the type of part */
if (sw_rev == 0) {
key = MPL_PROD_KEY(prod_ver, prod_rev);
if (key == 0)
return -EINVAL;
index = index_of_key(key);
if (index < 0 || index >= NUM_OF_PROD_REVS)
return -EINVAL;
/* check MPL is compiled for this device */
if (prod_rev_map[index].silicon_rev != MPU_SILICON_REV_B1)
return -EINVAL;
p_rev = (struct prod_rev_map_t *)&prod_rev_map[index];
/* if valid, use the software product key */
} else if (sw_rev < ARRAY_SIZE(sw_rev_map)) {
p_rev = (struct prod_rev_map_t *)&sw_rev_map[sw_rev];
} else {
return -EINVAL;
}
chip_info->product_id = prod_ver;
chip_info->product_revision = prod_rev;
chip_info->silicon_revision = p_rev->silicon_rev;
chip_info->software_revision = sw_rev;
chip_info->gyro_sens_trim = p_rev->gyro_trim;
chip_info->accel_sens_trim = p_rev->accel_trim;
if (chip_info->accel_sens_trim == 0)
chip_info->accel_sens_trim = DEFAULT_ACCEL_TRIM;
chip_info->multi = DEFAULT_ACCEL_TRIM / chip_info->accel_sens_trim;
if (chip_info->multi != 1)
pr_info("multi is %d\n", chip_info->multi);
return result;
}
/**
* read_accel_hw_self_test_prod_shift()- read the accelerometer hardware
* self-test bias shift calculated
* during final production test and
* stored in chip non-volatile memory.
* @st: main data structure.
* @st_prod: A pointer to an array of 3 elements to hold the values
* for production hardware self-test bias shifts returned to the
* user.
* @accel_sens: accel sensitivity.
*/
static int read_accel_hw_self_test_prod_shift(struct inv_mpu_state *st,
int *st_prod, int *accel_sens)
{
u8 regs[4];
u8 shift_code[3];
int result, i;
for (i = 0; i < 3; i++)
st_prod[i] = 0;
result = inv_i2c_read(st, REG_ST_GCT_X, ARRAY_SIZE(regs), regs);
if (result)
return result;
if ((0 == regs[0]) && (0 == regs[1]) &&
(0 == regs[2]) && (0 == regs[3]))
return -EINVAL;
shift_code[X] = ((regs[0] & 0xE0) >> 3) | ((regs[3] & 0x30) >> 4);
shift_code[Y] = ((regs[1] & 0xE0) >> 3) | ((regs[3] & 0x0C) >> 2);
shift_code[Z] = ((regs[2] & 0xE0) >> 3) | (regs[3] & 0x03);
for (i = 0; i < 3; i++)
if (shift_code[i] != 0)
st_prod[i] = accel_sens[i] *
accel_st_tb[shift_code[i] - 1];
return 0;
}
/**
* inv_check_accel_self_test()- check accel self test. this function returns
* zero as success. A non-zero return value
* indicates failure in self test.
* @*st: main data structure.
* @*reg_avg: average value of normal test.
* @*st_avg: average value of self test
*/
static int inv_check_accel_self_test(struct inv_mpu_state *st,
int *reg_avg, int *st_avg){
int gravity, reg_z_avg, g_z_sign, j, ret_val;
int tmp;
int st_shift_prod[THREE_AXIS], st_shift_cust[THREE_AXIS];
int st_shift_ratio[THREE_AXIS];
int accel_sens[THREE_AXIS];
if (st->chip_info.software_revision < DEF_OLDEST_SUPP_SW_REV &&
st->chip_info.product_revision < DEF_OLDEST_SUPP_PROD_REV)
return 0;
g_z_sign = 1;
ret_val = 0;
tmp = DEF_ST_SCALE * DEF_ST_PRECISION / DEF_ST_ACCEL_FS_MG;
for (j = 0; j < 3; j++)
accel_sens[j] = tmp;
if (MPL_PROD_KEY(st->chip_info.product_id,
st->chip_info.product_revision) ==
MPU_PRODUCT_KEY_B1_E1_5) {
/* half sensitivity Z accelerometer parts */
accel_sens[Z] /= 2;
} else {
/* half sensitivity X, Y, Z accelerometer parts */
accel_sens[X] /= st->chip_info.multi;
accel_sens[Y] /= st->chip_info.multi;
accel_sens[Z] /= st->chip_info.multi;
}
gravity = accel_sens[Z];
reg_z_avg = reg_avg[Z] - g_z_sign * gravity * DEF_ST_PRECISION;
ret_val = read_accel_hw_self_test_prod_shift(st, st_shift_prod,
accel_sens);
if (ret_val)
return ret_val;
for (j = 0; j < 3; j++) {
st_shift_cust[j] = abs(reg_avg[j] - st_avg[j]);
if (st_shift_prod[j]) {
tmp = st_shift_prod[j] / DEF_ST_PRECISION;
st_shift_ratio[j] = abs(st_shift_cust[j] / tmp
- DEF_ST_PRECISION);
if (st_shift_ratio[j] > DEF_ACCEL_ST_SHIFT_DELTA)
ret_val = 1;
} else {
if (st_shift_cust[j] <
DEF_ACCEL_ST_SHIFT_MIN * gravity)
ret_val = 1;
if (st_shift_cust[j] >
DEF_ACCEL_ST_SHIFT_MAX * gravity)
ret_val = 1;
}
}
return ret_val;
}
/**
* inv_check_3500_gyro_self_test() check gyro self test. this function returns
* zero as success. A non-zero return value
* indicates failure in self test.
* @*st: main data structure.
* @*reg_avg: average value of normal test.
* @*st_avg: average value of self test
*/
static int inv_check_3500_gyro_self_test(struct inv_mpu_state *st,
int *reg_avg, int *st_avg){
int result;
int gst[3], ret_val;
int gst_otp[3], i;
u8 st_code[THREE_AXIS];
ret_val = 0;
for (i = 0; i < 3; i++)
gst[i] = st_avg[i] - reg_avg[i];
result = inv_i2c_read(st, REG_3500_OTP, THREE_AXIS, st_code);
if (result)
return result;
gst_otp[0] = 0;
gst_otp[1] = 0;
gst_otp[2] = 0;
for (i = 0; i < 3; i++) {
if (st_code[i] != 0)
gst_otp[i] = gyro_3500_st_tb[st_code[i] - 1];
}
/* check self test value passing criterion. Using the DEF_ST_TOR
* for certain degree of tolerance */
for (i = 0; i < 3; i++) {
if (gst_otp[i] == 0) {
if (abs(gst[i]) * DEF_ST_TOR < DEF_ST_OTP0_THRESH *
DEF_ST_PRECISION *
DEF_GYRO_SCALE)
ret_val |= (1 << i);
} else {
if (abs(gst[i]/gst_otp[i] - DEF_ST_PRECISION) >
DEF_GYRO_CT_SHIFT_DELTA)
ret_val |= (1 << i);
}
}
/* check for absolute value passing criterion. Using DEF_ST_TOR
* for certain degree of tolerance */
for (i = 0; i < 3; i++) {
if (abs(reg_avg[i]) > DEF_ST_TOR * DEF_ST_ABS_THRESH *
DEF_ST_PRECISION * DEF_GYRO_SCALE)
ret_val |= (1 << i);
}
return ret_val;
}
/**
* inv_check_6050_gyro_self_test() - check 6050 gyro self test. this function
* returns zero as success. A non-zero return
* value indicates failure in self test.
* @*st: main data structure.
* @*reg_avg: average value of normal test.
* @*st_avg: average value of self test
*/
static int inv_check_6050_gyro_self_test(struct inv_mpu_state *st,
int *reg_avg, int *st_avg){
int result;
int ret_val;
int ct_shift_prod[3], st_shift_cust[3], st_shift_ratio[3], i;
u8 regs[3];
if (st->chip_info.software_revision < DEF_OLDEST_SUPP_SW_REV &&
st->chip_info.product_revision < DEF_OLDEST_SUPP_PROD_REV)
return 0;
ret_val = 0;
result = inv_i2c_read(st, REG_ST_GCT_X, 3, regs);
if (result)
return result;
regs[X] &= 0x1f;
regs[Y] &= 0x1f;
regs[Z] &= 0x1f;
for (i = 0; i < 3; i++) {
if (regs[i] != 0)
ct_shift_prod[i] = gyro_6050_st_tb[regs[i] - 1];
else
ct_shift_prod[i] = 0;
}
ct_shift_prod[1] = -ct_shift_prod[1];
for (i = 0; i < 3; i++) {
st_shift_cust[i] = st_avg[i] - reg_avg[i];
if (ct_shift_prod[i]) {
st_shift_ratio[i] = abs(st_shift_cust[i] /
ct_shift_prod[i] - DEF_ST_PRECISION);
if (st_shift_ratio[i] > DEF_GYRO_CT_SHIFT_DELTA)
ret_val = 1;
} else {
if (st_shift_cust[i] < DEF_ST_PRECISION *
DEF_GYRO_CT_SHIFT_MIN * DEF_SELFTEST_GYRO_SENS)
ret_val = 1;
if (st_shift_cust[i] > DEF_ST_PRECISION *
DEF_GYRO_CT_SHIFT_MAX * DEF_SELFTEST_GYRO_SENS)
ret_val = 1;
}
}
/* check for absolute value passing criterion. Using DEF_ST_TOR
* for certain degree of tolerance */
for (i = 0; i < 3; i++)
if (abs(reg_avg[i]) > DEF_ST_TOR * DEF_ST_ABS_THRESH *
DEF_ST_PRECISION * DEF_GYRO_SCALE)
ret_val = 1;
return ret_val;
}
/**
* inv_check_6500_gyro_self_test() - check 6500 gyro self test. this function
* returns zero as success. A non-zero return
* value indicates failure in self test.
* @*st: main data structure.
* @*reg_avg: average value of normal test.
* @*st_avg: average value of self test
*/
static int inv_check_6500_gyro_self_test(struct inv_mpu_state *st,
int *reg_avg, int *st_avg) {
u8 regs[3];
int ret_val, result;
int otp_value_zero = 0;
int ct_shift_prod[3], st_shift_cust[3], i;
ret_val = 0;
result = inv_i2c_read(st, REG_6500_XG_ST_DATA, 3, regs);
if (result)
return result;
pr_debug("%s self_test gyro shift_code - %02x %02x %02x\n",
st->hw->name, regs[0], regs[1], regs[2]);
for (i = 0; i < 3; i++) {
if (regs[i] != 0) {
ct_shift_prod[i] = mpu_6500_st_tb[regs[i] - 1];
} else {
ct_shift_prod[i] = 0;
otp_value_zero = 1;
}
}
pr_debug("%s self_test gyro ct_shift_prod - %+d %+d %+d\n",
st->hw->name, ct_shift_prod[0], ct_shift_prod[1],
ct_shift_prod[2]);
for (i = 0; i < 3; i++) {
st_shift_cust[i] = st_avg[i] - reg_avg[i];
if (!otp_value_zero) {
/* Self Test Pass/Fail Criteria A */
if (st_shift_cust[i] < DEF_6500_GYRO_CT_SHIFT_DELTA
* ct_shift_prod[i])
ret_val = 1;
} else {
/* Self Test Pass/Fail Criteria B */
if (st_shift_cust[i] < DEF_GYRO_ST_AL *
DEF_SELFTEST_GYRO_SENS *
DEF_ST_PRECISION)
ret_val = 1;
}
}
pr_debug("%s self_test gyro st_shift_cust - %+d %+d %+d\n",
st->hw->name, st_shift_cust[0], st_shift_cust[1],
st_shift_cust[2]);
if (ret_val == 0) {
/* Self Test Pass/Fail Criteria C */
for (i = 0; i < 3; i++)
if (abs(reg_avg[i]) > DEF_GYRO_OFFSET_MAX *
DEF_SELFTEST_GYRO_SENS *
DEF_ST_PRECISION)
ret_val = 1;
}
return ret_val;
}
/**
* inv_check_6500_accel_self_test() - check 6500 accel self test. this function
* returns zero as success. A non-zero return
* value indicates failure in self test.
* @*st: main data structure.
* @*reg_avg: average value of normal test.
* @*st_avg: average value of self test
*/
static int inv_check_6500_accel_self_test(struct inv_mpu_state *st,
int *reg_avg, int *st_avg) {
int ret_val, result;
int ct_shift_prod[3], st_shift_cust[3], st_shift_ratio[3], i;
u8 regs[3];
int otp_value_zero = 0;
#define ACCEL_ST_AL_MIN ((DEF_ACCEL_ST_AL_MIN * DEF_ST_SCALE \
/ DEF_ST_6500_ACCEL_FS_MG) * DEF_ST_PRECISION)
#define ACCEL_ST_AL_MAX ((DEF_ACCEL_ST_AL_MAX * DEF_ST_SCALE \
/ DEF_ST_6500_ACCEL_FS_MG) * DEF_ST_PRECISION)
ret_val = 0;
result = inv_i2c_read(st, REG_6500_XA_ST_DATA, 3, regs);
if (result)
return result;
pr_debug("%s self_test accel shift_code - %02x %02x %02x\n",
st->hw->name, regs[0], regs[1], regs[2]);
for (i = 0; i < 3; i++) {
if (regs[i] != 0) {
ct_shift_prod[i] = mpu_6500_st_tb[regs[i] - 1];
} else {
ct_shift_prod[i] = 0;
otp_value_zero = 1;
}
}
pr_debug("%s self_test accel ct_shift_prod - %+d %+d %+d\n",
st->hw->name, ct_shift_prod[0], ct_shift_prod[1],
ct_shift_prod[2]);
if (!otp_value_zero) {
/* Self Test Pass/Fail Criteria A */
for (i = 0; i < 3; i++) {
st_shift_cust[i] = st_avg[i] - reg_avg[i];
st_shift_ratio[i] = abs(st_shift_cust[i] /
ct_shift_prod[i] - DEF_ST_PRECISION);
if (st_shift_ratio[i] > DEF_6500_ACCEL_ST_SHIFT_DELTA)
ret_val = 1;
}
} else {
/* Self Test Pass/Fail Criteria B */
for (i = 0; i < 3; i++) {
st_shift_cust[i] = abs(st_avg[i] - reg_avg[i]);
if (st_shift_cust[i] < ACCEL_ST_AL_MIN ||
st_shift_cust[i] > ACCEL_ST_AL_MAX)
ret_val = 1;
}
}
pr_debug("%s self_test accel st_shift_cust - %+d %+d %+d\n",
st->hw->name, st_shift_cust[0], st_shift_cust[1],
st_shift_cust[2]);
return ret_val;
}
/*
* inv_do_test() - do the actual test of self testing
*/
int inv_do_test(struct inv_mpu_state *st, int self_test_flag,
int *gyro_result, int *accel_result)
{
struct inv_reg_map_s *reg;
int result, i, j, packet_size;
u8 data[BYTES_PER_SENSOR * 2], d;
bool has_accel;
int fifo_count, packet_count, ind, s;
reg = &st->reg;
has_accel = (st->chip_type != INV_ITG3500);
if (has_accel)
packet_size = BYTES_PER_SENSOR * 2;
else
packet_size = BYTES_PER_SENSOR;
result = inv_i2c_single_write(st, reg->int_enable, 0);
if (result)
return result;
/* disable the sensor output to FIFO */
result = inv_i2c_single_write(st, reg->fifo_en, 0);
if (result)
return result;
/* disable fifo reading */
result = inv_i2c_single_write(st, reg->user_ctrl, 0);
if (result)
return result;
/* clear FIFO */
result = inv_i2c_single_write(st, reg->user_ctrl, BIT_FIFO_RST);
if (result)
return result;
/* setup parameters */
result = inv_i2c_single_write(st, reg->lpf, INV_FILTER_98HZ);
if (result)
return result;
if (INV_MPU6500 == st->chip_type) {
/* config accel LPF register for MPU6500 */
result = inv_i2c_single_write(st, REG_6500_ACCEL_CONFIG2,
DEF_ST_MPU6500_ACCEL_LPF |
BIT_FIFO_SIZE_1K);
if (result)
return result;
}
result = inv_i2c_single_write(st, reg->sample_rate_div,
DEF_SELFTEST_SAMPLE_RATE);
if (result)
return result;
/* wait for the sampling rate change to stabilize */
mdelay(INV_MPU_SAMPLE_RATE_CHANGE_STABLE);
result = inv_i2c_single_write(st, reg->gyro_config,
self_test_flag | DEF_SELFTEST_GYRO_FS);
if (result)
return result;
if (has_accel) {
if (INV_MPU6500 == st->chip_type)
d = DEF_SELFTEST_6500_ACCEL_FS;
else
d = DEF_SELFTEST_ACCEL_FS;
d |= self_test_flag;
result = inv_i2c_single_write(st, reg->accel_config, d);
if (result)
return result;
}
/* wait for the output to get stable */
if (self_test_flag) {
if (INV_MPU6500 == st->chip_type)
msleep(DEF_ST_6500_STABLE_TIME);
else
msleep(DEF_ST_STABLE_TIME);
}
/* enable FIFO reading */
result = inv_i2c_single_write(st, reg->user_ctrl, BIT_FIFO_EN);
if (result)
return result;
/* enable sensor output to FIFO */
if (has_accel)
d = BITS_GYRO_OUT | BIT_ACCEL_OUT;
else
d = BITS_GYRO_OUT;
for (i = 0; i < THREE_AXIS; i++) {
gyro_result[i] = 0;
accel_result[i] = 0;
}
s = 0;
while (s < st->self_test.samples) {
result = inv_i2c_single_write(st, reg->fifo_en, d);
if (result)
return result;
mdelay(DEF_GYRO_WAIT_TIME);
result = inv_i2c_single_write(st, reg->fifo_en, 0);