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
|
// SPDX-License-Identifier: GPL-2.0
/*
* MEMSensing digital 3-Axis accelerometer
*
* MSA311 is a tri-axial, low-g accelerometer with I2C digital output for
* sensitivity consumer applications. It has dynamic user-selectable full
* scales range of +-2g/+-4g/+-8g/+-16g and allows acceleration measurements
* with output data rates from 1Hz to 1000Hz.
*
* MSA311 is available in an ultra small (2mm x 2mm, height 0.95mm) LGA package
* and is guaranteed to operate over -40C to +85C.
*
* This driver supports following MSA311 features:
* - IIO interface
* - Different power modes: NORMAL, SUSPEND
* - ODR (Output Data Rate) selection
* - Scale selection
* - IIO triggered buffer
* - NEW_DATA interrupt + trigger
*
* Below features to be done:
* - Motion Events: ACTIVE, TAP, ORIENT, FREEFALL
* - Low Power mode
*
* Copyright (c) 2022, SberDevices. All Rights Reserved.
*
* Author: Dmitry Rokosov <ddrokosov@sberdevices.ru>
*/
#include <linux/i2c.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/string_helpers.h>
#include <linux/units.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/trigger.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#define MSA311_SOFT_RESET_REG 0x00
#define MSA311_PARTID_REG 0x01
#define MSA311_ACC_X_REG 0x02
#define MSA311_ACC_Y_REG 0x04
#define MSA311_ACC_Z_REG 0x06
#define MSA311_MOTION_INT_REG 0x09
#define MSA311_DATA_INT_REG 0x0A
#define MSA311_TAP_ACTIVE_STS_REG 0x0B
#define MSA311_ORIENT_STS_REG 0x0C
#define MSA311_RANGE_REG 0x0F
#define MSA311_ODR_REG 0x10
#define MSA311_PWR_MODE_REG 0x11
#define MSA311_SWAP_POLARITY_REG 0x12
#define MSA311_INT_SET_0_REG 0x16
#define MSA311_INT_SET_1_REG 0x17
#define MSA311_INT_MAP_0_REG 0x19
#define MSA311_INT_MAP_1_REG 0x1A
#define MSA311_INT_CONFIG_REG 0x20
#define MSA311_INT_LATCH_REG 0x21
#define MSA311_FREEFALL_DUR_REG 0x22
#define MSA311_FREEFALL_TH_REG 0x23
#define MSA311_FREEFALL_HY_REG 0x24
#define MSA311_ACTIVE_DUR_REG 0x27
#define MSA311_ACTIVE_TH_REG 0x28
#define MSA311_TAP_DUR_REG 0x2A
#define MSA311_TAP_TH_REG 0x2B
#define MSA311_ORIENT_HY_REG 0x2C
#define MSA311_Z_BLOCK_REG 0x2D
#define MSA311_OFFSET_X_REG 0x38
#define MSA311_OFFSET_Y_REG 0x39
#define MSA311_OFFSET_Z_REG 0x3A
enum msa311_fields {
/* Soft_Reset */
F_SOFT_RESET_I2C, F_SOFT_RESET_SPI,
/* Motion_Interrupt */
F_ORIENT_INT, F_S_TAP_INT, F_D_TAP_INT, F_ACTIVE_INT, F_FREEFALL_INT,
/* Data_Interrupt */
F_NEW_DATA_INT,
/* Tap_Active_Status */
F_TAP_SIGN, F_TAP_FIRST_X, F_TAP_FIRST_Y, F_TAP_FIRST_Z, F_ACTV_SIGN,
F_ACTV_FIRST_X, F_ACTV_FIRST_Y, F_ACTV_FIRST_Z,
/* Orientation_Status */
F_ORIENT_Z, F_ORIENT_X_Y,
/* Range */
F_FS,
/* ODR */
F_X_AXIS_DIS, F_Y_AXIS_DIS, F_Z_AXIS_DIS, F_ODR,
/* Power Mode/Bandwidth */
F_PWR_MODE, F_LOW_POWER_BW,
/* Swap_Polarity */
F_X_POLARITY, F_Y_POLARITY, F_Z_POLARITY, F_X_Y_SWAP,
/* Int_Set_0 */
F_ORIENT_INT_EN, F_S_TAP_INT_EN, F_D_TAP_INT_EN, F_ACTIVE_INT_EN_Z,
F_ACTIVE_INT_EN_Y, F_ACTIVE_INT_EN_X,
/* Int_Set_1 */
F_NEW_DATA_INT_EN, F_FREEFALL_INT_EN,
/* Int_Map_0 */
F_INT1_ORIENT, F_INT1_S_TAP, F_INT1_D_TAP, F_INT1_ACTIVE,
F_INT1_FREEFALL,
/* Int_Map_1 */
F_INT1_NEW_DATA,
/* Int_Config */
F_INT1_OD, F_INT1_LVL,
/* Int_Latch */
F_RESET_INT, F_LATCH_INT,
/* Freefall_Hy */
F_FREEFALL_MODE, F_FREEFALL_HY,
/* Active_Dur */
F_ACTIVE_DUR,
/* Tap_Dur */
F_TAP_QUIET, F_TAP_SHOCK, F_TAP_DUR,
/* Tap_Th */
F_TAP_TH,
/* Orient_Hy */
F_ORIENT_HYST, F_ORIENT_BLOCKING, F_ORIENT_MODE,
/* Z_Block */
F_Z_BLOCKING,
/* End of register map */
F_MAX_FIELDS,
};
static const struct reg_field msa311_reg_fields[] = {
/* Soft_Reset */
[F_SOFT_RESET_I2C] = REG_FIELD(MSA311_SOFT_RESET_REG, 2, 2),
[F_SOFT_RESET_SPI] = REG_FIELD(MSA311_SOFT_RESET_REG, 5, 5),
/* Motion_Interrupt */
[F_ORIENT_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 6, 6),
[F_S_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 5, 5),
[F_D_TAP_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 4, 4),
[F_ACTIVE_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 2, 2),
[F_FREEFALL_INT] = REG_FIELD(MSA311_MOTION_INT_REG, 0, 0),
/* Data_Interrupt */
[F_NEW_DATA_INT] = REG_FIELD(MSA311_DATA_INT_REG, 0, 0),
/* Tap_Active_Status */
[F_TAP_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 7, 7),
[F_TAP_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 6, 6),
[F_TAP_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 5, 5),
[F_TAP_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 4, 4),
[F_ACTV_SIGN] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 3, 3),
[F_ACTV_FIRST_X] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 2, 2),
[F_ACTV_FIRST_Y] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 1, 1),
[F_ACTV_FIRST_Z] = REG_FIELD(MSA311_TAP_ACTIVE_STS_REG, 0, 0),
/* Orientation_Status */
[F_ORIENT_Z] = REG_FIELD(MSA311_ORIENT_STS_REG, 6, 6),
[F_ORIENT_X_Y] = REG_FIELD(MSA311_ORIENT_STS_REG, 4, 5),
/* Range */
[F_FS] = REG_FIELD(MSA311_RANGE_REG, 0, 1),
/* ODR */
[F_X_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 7, 7),
[F_Y_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 6, 6),
[F_Z_AXIS_DIS] = REG_FIELD(MSA311_ODR_REG, 5, 5),
[F_ODR] = REG_FIELD(MSA311_ODR_REG, 0, 3),
/* Power Mode/Bandwidth */
[F_PWR_MODE] = REG_FIELD(MSA311_PWR_MODE_REG, 6, 7),
[F_LOW_POWER_BW] = REG_FIELD(MSA311_PWR_MODE_REG, 1, 4),
/* Swap_Polarity */
[F_X_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 3, 3),
[F_Y_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 2, 2),
[F_Z_POLARITY] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 1, 1),
[F_X_Y_SWAP] = REG_FIELD(MSA311_SWAP_POLARITY_REG, 0, 0),
/* Int_Set_0 */
[F_ORIENT_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 6, 6),
[F_S_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 5, 5),
[F_D_TAP_INT_EN] = REG_FIELD(MSA311_INT_SET_0_REG, 4, 4),
[F_ACTIVE_INT_EN_Z] = REG_FIELD(MSA311_INT_SET_0_REG, 2, 2),
[F_ACTIVE_INT_EN_Y] = REG_FIELD(MSA311_INT_SET_0_REG, 1, 1),
[F_ACTIVE_INT_EN_X] = REG_FIELD(MSA311_INT_SET_0_REG, 0, 0),
/* Int_Set_1 */
[F_NEW_DATA_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 4, 4),
[F_FREEFALL_INT_EN] = REG_FIELD(MSA311_INT_SET_1_REG, 3, 3),
/* Int_Map_0 */
[F_INT1_ORIENT] = REG_FIELD(MSA311_INT_MAP_0_REG, 6, 6),
[F_INT1_S_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 5, 5),
[F_INT1_D_TAP] = REG_FIELD(MSA311_INT_MAP_0_REG, 4, 4),
[F_INT1_ACTIVE] = REG_FIELD(MSA311_INT_MAP_0_REG, 2, 2),
[F_INT1_FREEFALL] = REG_FIELD(MSA311_INT_MAP_0_REG, 0, 0),
/* Int_Map_1 */
[F_INT1_NEW_DATA] = REG_FIELD(MSA311_INT_MAP_1_REG, 0, 0),
/* Int_Config */
[F_INT1_OD] = REG_FIELD(MSA311_INT_CONFIG_REG, 1, 1),
[F_INT1_LVL] = REG_FIELD(MSA311_INT_CONFIG_REG, 0, 0),
/* Int_Latch */
[F_RESET_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 7, 7),
[F_LATCH_INT] = REG_FIELD(MSA311_INT_LATCH_REG, 0, 3),
/* Freefall_Hy */
[F_FREEFALL_MODE] = REG_FIELD(MSA311_FREEFALL_HY_REG, 2, 2),
[F_FREEFALL_HY] = REG_FIELD(MSA311_FREEFALL_HY_REG, 0, 1),
/* Active_Dur */
[F_ACTIVE_DUR] = REG_FIELD(MSA311_ACTIVE_DUR_REG, 0, 1),
/* Tap_Dur */
[F_TAP_QUIET] = REG_FIELD(MSA311_TAP_DUR_REG, 7, 7),
[F_TAP_SHOCK] = REG_FIELD(MSA311_TAP_DUR_REG, 6, 6),
[F_TAP_DUR] = REG_FIELD(MSA311_TAP_DUR_REG, 0, 2),
/* Tap_Th */
[F_TAP_TH] = REG_FIELD(MSA311_TAP_TH_REG, 0, 4),
/* Orient_Hy */
[F_ORIENT_HYST] = REG_FIELD(MSA311_ORIENT_HY_REG, 4, 6),
[F_ORIENT_BLOCKING] = REG_FIELD(MSA311_ORIENT_HY_REG, 2, 3),
[F_ORIENT_MODE] = REG_FIELD(MSA311_ORIENT_HY_REG, 0, 1),
/* Z_Block */
[F_Z_BLOCKING] = REG_FIELD(MSA311_Z_BLOCK_REG, 0, 3),
};
#define MSA311_WHO_AM_I 0x13
/*
* Possible Full Scale ranges
*
* Axis data is 12-bit signed value, so
*
* fs0 = (2 + 2) * 9.81 / (2^11) = 0.009580
* fs1 = (4 + 4) * 9.81 / (2^11) = 0.019160
* fs2 = (8 + 8) * 9.81 / (2^11) = 0.038320
* fs3 = (16 + 16) * 9.81 / (2^11) = 0.076641
*/
enum {
MSA311_FS_2G,
MSA311_FS_4G,
MSA311_FS_8G,
MSA311_FS_16G,
};
struct iio_decimal_fract {
int integral;
int microfract;
};
static const struct iio_decimal_fract msa311_fs_table[] = {
{0, 9580}, {0, 19160}, {0, 38320}, {0, 76641},
};
/* Possible Output Data Rate values */
enum {
MSA311_ODR_1_HZ,
MSA311_ODR_1_95_HZ,
MSA311_ODR_3_9_HZ,
MSA311_ODR_7_81_HZ,
MSA311_ODR_15_63_HZ,
MSA311_ODR_31_25_HZ,
MSA311_ODR_62_5_HZ,
MSA311_ODR_125_HZ,
MSA311_ODR_250_HZ,
MSA311_ODR_500_HZ,
MSA311_ODR_1000_HZ,
};
static const struct iio_decimal_fract msa311_odr_table[] = {
{1, 0}, {1, 950000}, {3, 900000}, {7, 810000}, {15, 630000},
{31, 250000}, {62, 500000}, {125, 0}, {250, 0}, {500, 0}, {1000, 0},
};
/* All supported power modes */
#define MSA311_PWR_MODE_NORMAL 0b00
#define MSA311_PWR_MODE_LOW 0b01
#define MSA311_PWR_MODE_UNKNOWN 0b10
#define MSA311_PWR_MODE_SUSPEND 0b11
static const char * const msa311_pwr_modes[] = {
[MSA311_PWR_MODE_NORMAL] = "normal",
[MSA311_PWR_MODE_LOW] = "low",
[MSA311_PWR_MODE_UNKNOWN] = "unknown",
[MSA311_PWR_MODE_SUSPEND] = "suspend",
};
/* Autosuspend delay */
#define MSA311_PWR_SLEEP_DELAY_MS 2000
/* Possible INT1 types and levels */
enum {
MSA311_INT1_OD_PUSH_PULL,
MSA311_INT1_OD_OPEN_DRAIN,
};
enum {
MSA311_INT1_LVL_LOW,
MSA311_INT1_LVL_HIGH,
};
/* Latch INT modes */
#define MSA311_LATCH_INT_NOT_LATCHED 0b0000
#define MSA311_LATCH_INT_250MS 0b0001
#define MSA311_LATCH_INT_500MS 0b0010
#define MSA311_LATCH_INT_1S 0b0011
#define MSA311_LATCH_INT_2S 0b0100
#define MSA311_LATCH_INT_4S 0b0101
#define MSA311_LATCH_INT_8S 0b0110
#define MSA311_LATCH_INT_1MS 0b1010
#define MSA311_LATCH_INT_2MS 0b1011
#define MSA311_LATCH_INT_25MS 0b1100
#define MSA311_LATCH_INT_50MS 0b1101
#define MSA311_LATCH_INT_100MS 0b1110
#define MSA311_LATCH_INT_LATCHED 0b0111
static const struct regmap_range msa311_readonly_registers[] = {
regmap_reg_range(MSA311_PARTID_REG, MSA311_ORIENT_STS_REG),
};
static const struct regmap_access_table msa311_writeable_table = {
.no_ranges = msa311_readonly_registers,
.n_no_ranges = ARRAY_SIZE(msa311_readonly_registers),
};
static const struct regmap_range msa311_writeonly_registers[] = {
regmap_reg_range(MSA311_SOFT_RESET_REG, MSA311_SOFT_RESET_REG),
};
static const struct regmap_access_table msa311_readable_table = {
.no_ranges = msa311_writeonly_registers,
.n_no_ranges = ARRAY_SIZE(msa311_writeonly_registers),
};
static const struct regmap_range msa311_volatile_registers[] = {
regmap_reg_range(MSA311_ACC_X_REG, MSA311_ORIENT_STS_REG),
};
static const struct regmap_access_table msa311_volatile_table = {
.yes_ranges = msa311_volatile_registers,
.n_yes_ranges = ARRAY_SIZE(msa311_volatile_registers),
};
static const struct regmap_config msa311_regmap_config = {
.name = "msa311",
.reg_bits = 8,
.val_bits = 8,
.max_register = MSA311_OFFSET_Z_REG,
.wr_table = &msa311_writeable_table,
.rd_table = &msa311_readable_table,
.volatile_table = &msa311_volatile_table,
.cache_type = REGCACHE_RBTREE,
};
#define MSA311_GENMASK(field) ({ \
typeof(&(msa311_reg_fields)[0]) _field; \
_field = &msa311_reg_fields[(field)]; \
GENMASK(_field->msb, _field->lsb); \
})
/**
* struct msa311_priv - MSA311 internal private state
* @regs: Underlying I2C bus adapter used to abstract slave
* register accesses
* @fields: Abstract objects for each registers fields access
* @dev: Device handler associated with appropriate bus client
* @lock: Protects msa311 device state between setup and data access routines
* (power transitions, samp_freq/scale tune, retrieving axes data, etc)
* @chip_name: Chip name in the format "msa311-%02x" % partid
* @new_data_trig: Optional NEW_DATA interrupt driven trigger used
* to notify external consumers a new sample is ready
*/
struct msa311_priv {
struct regmap *regs;
struct regmap_field *fields[F_MAX_FIELDS];
struct device *dev;
struct mutex lock;
char *chip_name;
struct iio_trigger *new_data_trig;
};
enum msa311_si {
MSA311_SI_X,
MSA311_SI_Y,
MSA311_SI_Z,
MSA311_SI_TIMESTAMP,
};
#define MSA311_ACCEL_CHANNEL(axis) { \
.type = IIO_ACCEL, \
.modified = 1, \
.channel2 = IIO_MOD_##axis, \
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) | \
BIT(IIO_CHAN_INFO_SAMP_FREQ), \
.scan_index = MSA311_SI_##axis, \
.scan_type = { \
.sign = 's', \
.realbits = 12, \
.storagebits = 16, \
.shift = 4, \
.endianness = IIO_LE, \
}, \
.datasheet_name = "ACC_"#axis, \
}
static const struct iio_chan_spec msa311_channels[] = {
MSA311_ACCEL_CHANNEL(X),
MSA311_ACCEL_CHANNEL(Y),
MSA311_ACCEL_CHANNEL(Z),
IIO_CHAN_SOFT_TIMESTAMP(MSA311_SI_TIMESTAMP),
};
/**
* msa311_get_odr() - Read Output Data Rate (ODR) value from MSA311 accel
* @msa311: MSA311 internal private state
* @odr: output ODR value
*
* This function should be called under msa311->lock.
*
* Return: 0 on success, -ERRNO in other failures
*/
static int msa311_get_odr(struct msa311_priv *msa311, unsigned int *odr)
{
int err;
err = regmap_field_read(msa311->fields[F_ODR], odr);
if (err)
return err;
/*
* Filter the same 1000Hz ODR register values based on datasheet info.
* ODR can be equal to 1010-1111 for 1000Hz, but function returns 1010
* all the time.
*/
if (*odr > MSA311_ODR_1000_HZ)
*odr = MSA311_ODR_1000_HZ;
return 0;
}
/**
* msa311_set_odr() - Setup Output Data Rate (ODR) value for MSA311 accel
* @msa311: MSA311 internal private state
* @odr: requested ODR value
*
* This function should be called under msa311->lock. Possible ODR values:
* - 1Hz (not available in normal mode)
* - 1.95Hz (not available in normal mode)
* - 3.9Hz
* - 7.81Hz
* - 15.63Hz
* - 31.25Hz
* - 62.5Hz
* - 125Hz
* - 250Hz
* - 500Hz
* - 1000Hz
*
* Return: 0 on success, -EINVAL for bad ODR value in the certain power mode,
* -ERRNO in other failures
*/
static int msa311_set_odr(struct msa311_priv *msa311, unsigned int odr)
{
struct device *dev = msa311->dev;
unsigned int pwr_mode;
bool good_odr;
int err;
err = regmap_field_read(msa311->fields[F_PWR_MODE], &pwr_mode);
if (err)
return err;
/* Filter bad ODR values */
if (pwr_mode == MSA311_PWR_MODE_NORMAL)
good_odr = (odr > MSA311_ODR_1_95_HZ);
else
good_odr = false;
if (!good_odr) {
dev_err(dev,
"can't set odr %u.%06uHz, not available in %s mode\n",
msa311_odr_table[odr].integral,
msa311_odr_table[odr].microfract,
msa311_pwr_modes[pwr_mode]);
return -EINVAL;
}
return regmap_field_write(msa311->fields[F_ODR], odr);
}
/**
* msa311_wait_for_next_data() - Wait next accel data available after resume
* @msa311: MSA311 internal private state
*
* Return: 0 on success, -EINTR if msleep() was interrupted,
* -ERRNO in other failures
*/
static int msa311_wait_for_next_data(struct msa311_priv *msa311)
{
static const unsigned int unintr_thresh_ms = 20;
struct device *dev = msa311->dev;
unsigned long freq_uhz;
unsigned long wait_ms;
unsigned int odr;
int err;
err = msa311_get_odr(msa311, &odr);
if (err) {
dev_err(dev, "can't get actual frequency (%pe)\n",
ERR_PTR(err));
return err;
}
/*
* After msa311 resuming is done, we need to wait for data
* to be refreshed by accel logic.
* A certain timeout is calculated based on the current ODR value.
* If requested timeout isn't so long (let's assume 20ms),
* we can wait for next data in uninterruptible sleep.
*/
freq_uhz = msa311_odr_table[odr].integral * MICROHZ_PER_HZ +
msa311_odr_table[odr].microfract;
wait_ms = (MICROHZ_PER_HZ / freq_uhz) * MSEC_PER_SEC;
if (wait_ms < unintr_thresh_ms)
usleep_range(wait_ms * USEC_PER_MSEC,
unintr_thresh_ms * USEC_PER_MSEC);
else if (msleep_interruptible(wait_ms))
return -EINTR;
return 0;
}
/**
* msa311_set_pwr_mode() - Install certain MSA311 power mode
* @msa311: MSA311 internal private state
* @mode: Power mode can be equal to NORMAL or SUSPEND
*
* This function should be called under msa311->lock.
*
* Return: 0 on success, -ERRNO on failure
*/
static int msa311_set_pwr_mode(struct msa311_priv *msa311, unsigned int mode)
{
struct device *dev = msa311->dev;
unsigned int prev_mode;
int err;
if (mode >= ARRAY_SIZE(msa311_pwr_modes))
return -EINVAL;
dev_dbg(dev, "transition to %s mode\n", msa311_pwr_modes[mode]);
err = regmap_field_read(msa311->fields[F_PWR_MODE], &prev_mode);
if (err)
return err;
err = regmap_field_write(msa311->fields[F_PWR_MODE], mode);
if (err)
return err;
/* Wait actual data if we wake up */
if (prev_mode == MSA311_PWR_MODE_SUSPEND &&
mode == MSA311_PWR_MODE_NORMAL)
return msa311_wait_for_next_data(msa311);
return 0;
}
/**
* msa311_get_axis() - Read MSA311 accel data for certain IIO channel axis spec
* @msa311: MSA311 internal private state
* @chan: IIO channel specification
* @axis: Output accel axis data for requested IIO channel spec
*
* This function should be called under msa311->lock.
*
* Return: 0 on success, -EINVAL for unknown IIO channel specification,
* -ERRNO in other failures
*/
static int msa311_get_axis(struct msa311_priv *msa311,
const struct iio_chan_spec * const chan,
__le16 *axis)
{
struct device *dev = msa311->dev;
unsigned int axis_reg;
if (chan->scan_index < MSA311_SI_X || chan->scan_index > MSA311_SI_Z) {
dev_err(dev, "invalid scan_index value [%d]\n",
chan->scan_index);
return -EINVAL;
}
/* Axes data layout has 2 byte gap for each axis starting from X axis */
axis_reg = MSA311_ACC_X_REG + (chan->scan_index << 1);
return regmap_bulk_read(msa311->regs, axis_reg, axis, sizeof(*axis));
}
static int msa311_read_raw_data(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
__le16 axis;
int err;
err = pm_runtime_resume_and_get(dev);
if (err)
return err;
err = iio_device_claim_direct_mode(indio_dev);
if (err)
return err;
mutex_lock(&msa311->lock);
err = msa311_get_axis(msa311, chan, &axis);
mutex_unlock(&msa311->lock);
iio_device_release_direct_mode(indio_dev);
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
if (err) {
dev_err(dev, "can't get axis %s (%pe)\n",
chan->datasheet_name, ERR_PTR(err));
return err;
}
/*
* Axis data format is:
* ACC_X = (ACC_X_MSB[7:0] << 4) | ACC_X_LSB[7:4]
*/
*val = sign_extend32(le16_to_cpu(axis) >> chan->scan_type.shift,
chan->scan_type.realbits - 1);
return IIO_VAL_INT;
}
static int msa311_read_scale(struct iio_dev *indio_dev, int *val, int *val2)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
unsigned int fs;
int err;
mutex_lock(&msa311->lock);
err = regmap_field_read(msa311->fields[F_FS], &fs);
mutex_unlock(&msa311->lock);
if (err) {
dev_err(dev, "can't get actual scale (%pe)\n", ERR_PTR(err));
return err;
}
*val = msa311_fs_table[fs].integral;
*val2 = msa311_fs_table[fs].microfract;
return IIO_VAL_INT_PLUS_MICRO;
}
static int msa311_read_samp_freq(struct iio_dev *indio_dev,
int *val, int *val2)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
unsigned int odr;
int err;
mutex_lock(&msa311->lock);
err = msa311_get_odr(msa311, &odr);
mutex_unlock(&msa311->lock);
if (err) {
dev_err(dev, "can't get actual frequency (%pe)\n",
ERR_PTR(err));
return err;
}
*val = msa311_odr_table[odr].integral;
*val2 = msa311_odr_table[odr].microfract;
return IIO_VAL_INT_PLUS_MICRO;
}
static int msa311_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int *val, int *val2, long mask)
{
switch (mask) {
case IIO_CHAN_INFO_RAW:
return msa311_read_raw_data(indio_dev, chan, val, val2);
case IIO_CHAN_INFO_SCALE:
return msa311_read_scale(indio_dev, val, val2);
case IIO_CHAN_INFO_SAMP_FREQ:
return msa311_read_samp_freq(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int msa311_read_avail(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
const int **vals, int *type,
int *length, long mask)
{
switch (mask) {
case IIO_CHAN_INFO_SAMP_FREQ:
*vals = (int *)msa311_odr_table;
*type = IIO_VAL_INT_PLUS_MICRO;
/* ODR value has 2 ints (integer and fractional parts) */
*length = ARRAY_SIZE(msa311_odr_table) * 2;
return IIO_AVAIL_LIST;
case IIO_CHAN_INFO_SCALE:
*vals = (int *)msa311_fs_table;
*type = IIO_VAL_INT_PLUS_MICRO;
/* FS value has 2 ints (integer and fractional parts) */
*length = ARRAY_SIZE(msa311_fs_table) * 2;
return IIO_AVAIL_LIST;
default:
return -EINVAL;
}
}
static int msa311_write_scale(struct iio_dev *indio_dev, int val, int val2)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
unsigned int fs;
int err;
/* We do not have fs >= 1, so skip such values */
if (val)
return 0;
err = pm_runtime_resume_and_get(dev);
if (err)
return err;
err = -EINVAL;
for (fs = 0; fs < ARRAY_SIZE(msa311_fs_table); fs++)
/* Do not check msa311_fs_table[fs].integral, it's always 0 */
if (val2 == msa311_fs_table[fs].microfract) {
mutex_lock(&msa311->lock);
err = regmap_field_write(msa311->fields[F_FS], fs);
mutex_unlock(&msa311->lock);
break;
}
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
if (err)
dev_err(dev, "can't update scale (%pe)\n", ERR_PTR(err));
return err;
}
static int msa311_write_samp_freq(struct iio_dev *indio_dev, int val, int val2)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
unsigned int odr;
int err;
err = pm_runtime_resume_and_get(dev);
if (err)
return err;
/*
* Sampling frequency changing is prohibited when buffer mode is
* enabled, because sometimes MSA311 chip returns outliers during
* frequency values growing up in the read operation moment.
*/
err = iio_device_claim_direct_mode(indio_dev);
if (err)
return err;
err = -EINVAL;
for (odr = 0; odr < ARRAY_SIZE(msa311_odr_table); odr++)
if (val == msa311_odr_table[odr].integral &&
val2 == msa311_odr_table[odr].microfract) {
mutex_lock(&msa311->lock);
err = msa311_set_odr(msa311, odr);
mutex_unlock(&msa311->lock);
break;
}
iio_device_release_direct_mode(indio_dev);
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
if (err)
dev_err(dev, "can't update frequency (%pe)\n", ERR_PTR(err));
return err;
}
static int msa311_write_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan,
int val, int val2, long mask)
{
switch (mask) {
case IIO_CHAN_INFO_SCALE:
return msa311_write_scale(indio_dev, val, val2);
case IIO_CHAN_INFO_SAMP_FREQ:
return msa311_write_samp_freq(indio_dev, val, val2);
default:
return -EINVAL;
}
}
static int msa311_debugfs_reg_access(struct iio_dev *indio_dev,
unsigned int reg, unsigned int writeval,
unsigned int *readval)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
int err;
if (reg > regmap_get_max_register(msa311->regs))
return -EINVAL;
err = pm_runtime_resume_and_get(dev);
if (err)
return err;
mutex_lock(&msa311->lock);
if (readval)
err = regmap_read(msa311->regs, reg, readval);
else
err = regmap_write(msa311->regs, reg, writeval);
mutex_unlock(&msa311->lock);
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
if (err)
dev_err(dev, "can't %s register %u from debugfs (%pe)\n",
str_read_write(readval), reg, ERR_PTR(err));
return err;
}
static int msa311_buffer_preenable(struct iio_dev *indio_dev)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
return pm_runtime_resume_and_get(dev);
}
static int msa311_buffer_postdisable(struct iio_dev *indio_dev)
{
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
return 0;
}
static int msa311_set_new_data_trig_state(struct iio_trigger *trig, bool state)
{
struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
struct msa311_priv *msa311 = iio_priv(indio_dev);
struct device *dev = msa311->dev;
int err;
mutex_lock(&msa311->lock);
err = regmap_field_write(msa311->fields[F_NEW_DATA_INT_EN], state);
mutex_unlock(&msa311->lock);
if (err)
dev_err(dev,
"can't %s buffer due to new_data_int failure (%pe)\n",
str_enable_disable(state), ERR_PTR(err));
return err;
}
static int msa311_validate_device(struct iio_trigger *trig,
struct iio_dev *indio_dev)
{
return iio_trigger_get_drvdata(trig) == indio_dev ? 0 : -EINVAL;
}
static irqreturn_t msa311_buffer_thread(int irq, void *p)
{
struct iio_poll_func *pf = p;
struct msa311_priv *msa311 = iio_priv(pf->indio_dev);
struct iio_dev *indio_dev = pf->indio_dev;
const struct iio_chan_spec *chan;
struct device *dev = msa311->dev;
int bit, err, i = 0;
__le16 axis;
struct {
__le16 channels[MSA311_SI_Z + 1];
s64 ts __aligned(8);
} buf;
memset(&buf, 0, sizeof(buf));
mutex_lock(&msa311->lock);
for_each_set_bit(bit, indio_dev->active_scan_mask,
indio_dev->masklength) {
chan = &msa311_channels[bit];
err = msa311_get_axis(msa311, chan, &axis);
if (err) {
mutex_unlock(&msa311->lock);
dev_err(dev, "can't get axis %s (%pe)\n",
chan->datasheet_name, ERR_PTR(err));
goto notify_done;
}
buf.channels[i++] = axis;
}
mutex_unlock(&msa311->lock);
iio_push_to_buffers_with_timestamp(indio_dev, &buf,
iio_get_time_ns(indio_dev));
notify_done:
iio_trigger_notify_done(indio_dev->trig);
return IRQ_HANDLED;
}
static irqreturn_t msa311_irq_thread(int irq, void *p)
{
struct msa311_priv *msa311 = iio_priv(p);
unsigned int new_data_int_enabled;
struct device *dev = msa311->dev;
int err;
mutex_lock(&msa311->lock);
/*
* We do not check NEW_DATA int status, because based on the
* specification it's cleared automatically after a fixed time.
* So just check that is enabled by driver logic.
*/
err = regmap_field_read(msa311->fields[F_NEW_DATA_INT_EN],
&new_data_int_enabled);
mutex_unlock(&msa311->lock);
if (err) {
dev_err(dev, "can't read new_data interrupt state (%pe)\n",
ERR_PTR(err));
return IRQ_NONE;
}
if (new_data_int_enabled)
iio_trigger_poll_nested(msa311->new_data_trig);
return IRQ_HANDLED;
}
static const struct iio_info msa311_info = {
.read_raw = msa311_read_raw,
.read_avail = msa311_read_avail,
.write_raw = msa311_write_raw,
.debugfs_reg_access = msa311_debugfs_reg_access,
};
static const struct iio_buffer_setup_ops msa311_buffer_setup_ops = {
.preenable = msa311_buffer_preenable,
.postdisable = msa311_buffer_postdisable,
};
static const struct iio_trigger_ops msa311_new_data_trig_ops = {
.set_trigger_state = msa311_set_new_data_trig_state,
.validate_device = msa311_validate_device,
};
static int msa311_check_partid(struct msa311_priv *msa311)
{
struct device *dev = msa311->dev;
unsigned int partid;
int err;
err = regmap_read(msa311->regs, MSA311_PARTID_REG, &partid);
if (err)
return dev_err_probe(dev, err, "failed to read partid\n");
if (partid != MSA311_WHO_AM_I)
dev_warn(dev, "invalid partid (%#x), expected (%#x)\n",
partid, MSA311_WHO_AM_I);
msa311->chip_name = devm_kasprintf(dev, GFP_KERNEL,
"msa311-%02x", partid);
if (!msa311->chip_name)
return dev_err_probe(dev, -ENOMEM, "can't alloc chip name\n");
return 0;
}
static int msa311_soft_reset(struct msa311_priv *msa311)
{
struct device *dev = msa311->dev;
int err;
err = regmap_write(msa311->regs, MSA311_SOFT_RESET_REG,
MSA311_GENMASK(F_SOFT_RESET_I2C) |
MSA311_GENMASK(F_SOFT_RESET_SPI));
if (err)
return dev_err_probe(dev, err, "can't soft reset all logic\n");
return 0;
}
static int msa311_chip_init(struct msa311_priv *msa311)
{
struct device *dev = msa311->dev;
const char zero_bulk[2] = { };
int err;
err = regmap_write(msa311->regs, MSA311_RANGE_REG, MSA311_FS_16G);
if (err)
return dev_err_probe(dev, err, "failed to setup accel range\n");
/* Disable all interrupts by default */
err = regmap_bulk_write(msa311->regs, MSA311_INT_SET_0_REG,
zero_bulk, sizeof(zero_bulk));
if (err)
return dev_err_probe(dev, err,
"can't disable set0/set1 interrupts\n");
/* Unmap all INT1 interrupts by default */
err = regmap_bulk_write(msa311->regs, MSA311_INT_MAP_0_REG,
zero_bulk, sizeof(zero_bulk));
if (err)
return dev_err_probe(dev, err,
"failed to unmap map0/map1 interrupts\n");
/* Disable all axes by default */
err = regmap_update_bits(msa311->regs, MSA311_ODR_REG,
MSA311_GENMASK(F_X_AXIS_DIS) |
MSA311_GENMASK(F_Y_AXIS_DIS) |
MSA311_GENMASK(F_Z_AXIS_DIS), 0);
if (err)
return dev_err_probe(dev, err, "can't enable all axes\n");
err = msa311_set_odr(msa311, MSA311_ODR_125_HZ);
if (err)
return dev_err_probe(dev, err,
"failed to set accel frequency\n");
return 0;
}
static int msa311_setup_interrupts(struct msa311_priv *msa311)
{
struct device *dev = msa311->dev;
struct i2c_client *i2c = to_i2c_client(dev);
struct iio_dev *indio_dev = i2c_get_clientdata(i2c);
struct iio_trigger *trig;
int err;
/* Keep going without interrupts if no initialized I2C IRQ */
if (i2c->irq <= 0)
return 0;
err = devm_request_threaded_irq(&i2c->dev, i2c->irq, NULL,
msa311_irq_thread, IRQF_ONESHOT,
msa311->chip_name, indio_dev);
if (err)
return dev_err_probe(dev, err, "failed to request IRQ\n");
trig = devm_iio_trigger_alloc(dev, "%s-new-data", msa311->chip_name);
if (!trig)
return dev_err_probe(dev, -ENOMEM,
"can't allocate newdata trigger\n");
msa311->new_data_trig = trig;
msa311->new_data_trig->ops = &msa311_new_data_trig_ops;
iio_trigger_set_drvdata(msa311->new_data_trig, indio_dev);
err = devm_iio_trigger_register(dev, msa311->new_data_trig);
if (err)
return dev_err_probe(dev, err,
"can't register newdata trigger\n");
err = regmap_field_write(msa311->fields[F_INT1_OD],
MSA311_INT1_OD_PUSH_PULL);
if (err)
return dev_err_probe(dev, err,
"can't enable push-pull interrupt\n");
err = regmap_field_write(msa311->fields[F_INT1_LVL],
MSA311_INT1_LVL_HIGH);
if (err)
return dev_err_probe(dev, err,
"can't set active interrupt level\n");
err = regmap_field_write(msa311->fields[F_LATCH_INT],
MSA311_LATCH_INT_LATCHED);
if (err)
return dev_err_probe(dev, err,
"can't latch interrupt\n");
err = regmap_field_write(msa311->fields[F_RESET_INT], 1);
if (err)
return dev_err_probe(dev, err,
"can't reset interrupt\n");
err = regmap_field_write(msa311->fields[F_INT1_NEW_DATA], 1);
if (err)
return dev_err_probe(dev, err,
"can't map new data interrupt\n");
return 0;
}
static int msa311_regmap_init(struct msa311_priv *msa311)
{
struct regmap_field **fields = msa311->fields;
struct device *dev = msa311->dev;
struct i2c_client *i2c = to_i2c_client(dev);
struct regmap *regmap;
int i;
regmap = devm_regmap_init_i2c(i2c, &msa311_regmap_config);
if (IS_ERR(regmap))
return dev_err_probe(dev, PTR_ERR(regmap),
"failed to register i2c regmap\n");
msa311->regs = regmap;
for (i = 0; i < F_MAX_FIELDS; i++) {
fields[i] = devm_regmap_field_alloc(dev,
msa311->regs,
msa311_reg_fields[i]);
if (IS_ERR(msa311->fields[i]))
return dev_err_probe(dev, PTR_ERR(msa311->fields[i]),
"can't alloc field[%d]\n", i);
}
return 0;
}
static void msa311_powerdown(void *msa311)
{
msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
}
static int msa311_probe(struct i2c_client *i2c)
{
struct device *dev = &i2c->dev;
struct msa311_priv *msa311;
struct iio_dev *indio_dev;
int err;
indio_dev = devm_iio_device_alloc(dev, sizeof(*msa311));
if (!indio_dev)
return dev_err_probe(dev, -ENOMEM,
"IIO device allocation failed\n");
msa311 = iio_priv(indio_dev);
msa311->dev = dev;
i2c_set_clientdata(i2c, indio_dev);
err = msa311_regmap_init(msa311);
if (err)
return err;
mutex_init(&msa311->lock);
err = devm_regulator_get_enable(dev, "vdd");
if (err)
return dev_err_probe(dev, err, "can't get vdd supply\n");
err = msa311_check_partid(msa311);
if (err)
return err;
err = msa311_soft_reset(msa311);
if (err)
return err;
err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
if (err)
return dev_err_probe(dev, err, "failed to power on device\n");
/*
* Register powerdown deferred callback which suspends the chip
* after module unloaded.
*
* MSA311 should be in SUSPEND mode in the two cases:
* 1) When driver is loaded, but we do not have any data or
* configuration requests to it (we are solving it using
* autosuspend feature).
* 2) When driver is unloaded and device is not used (devm action is
* used in this case).
*/
err = devm_add_action_or_reset(dev, msa311_powerdown, msa311);
if (err)
return dev_err_probe(dev, err, "can't add powerdown action\n");
err = pm_runtime_set_active(dev);
if (err)
return err;
err = devm_pm_runtime_enable(dev);
if (err)
return err;
pm_runtime_get_noresume(dev);
pm_runtime_set_autosuspend_delay(dev, MSA311_PWR_SLEEP_DELAY_MS);
pm_runtime_use_autosuspend(dev);
err = msa311_chip_init(msa311);
if (err)
return err;
indio_dev->modes = INDIO_DIRECT_MODE;
indio_dev->channels = msa311_channels;
indio_dev->num_channels = ARRAY_SIZE(msa311_channels);
indio_dev->name = msa311->chip_name;
indio_dev->info = &msa311_info;
err = devm_iio_triggered_buffer_setup(dev, indio_dev,
iio_pollfunc_store_time,
msa311_buffer_thread,
&msa311_buffer_setup_ops);
if (err)
return dev_err_probe(dev, err,
"can't setup IIO trigger buffer\n");
err = msa311_setup_interrupts(msa311);
if (err)
return err;
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
err = devm_iio_device_register(dev, indio_dev);
if (err)
return dev_err_probe(dev, err, "IIO device register failed\n");
return 0;
}
static int msa311_runtime_suspend(struct device *dev)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct msa311_priv *msa311 = iio_priv(indio_dev);
int err;
mutex_lock(&msa311->lock);
err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_SUSPEND);
mutex_unlock(&msa311->lock);
if (err)
dev_err(dev, "failed to power off device (%pe)\n",
ERR_PTR(err));
return err;
}
static int msa311_runtime_resume(struct device *dev)
{
struct iio_dev *indio_dev = dev_get_drvdata(dev);
struct msa311_priv *msa311 = iio_priv(indio_dev);
int err;
mutex_lock(&msa311->lock);
err = msa311_set_pwr_mode(msa311, MSA311_PWR_MODE_NORMAL);
mutex_unlock(&msa311->lock);
if (err)
dev_err(dev, "failed to power on device (%pe)\n",
ERR_PTR(err));
return err;
}
static DEFINE_RUNTIME_DEV_PM_OPS(msa311_pm_ops, msa311_runtime_suspend,
msa311_runtime_resume, NULL);
static const struct i2c_device_id msa311_i2c_id[] = {
{ .name = "msa311" },
{ }
};
MODULE_DEVICE_TABLE(i2c, msa311_i2c_id);
static const struct of_device_id msa311_of_match[] = {
{ .compatible = "memsensing,msa311" },
{ }
};
MODULE_DEVICE_TABLE(of, msa311_of_match);
static struct i2c_driver msa311_driver = {
.driver = {
.name = "msa311",
.of_match_table = msa311_of_match,
.pm = pm_ptr(&msa311_pm_ops),
},
.probe = msa311_probe,
.id_table = msa311_i2c_id,
};
module_i2c_driver(msa311_driver);
MODULE_AUTHOR("Dmitry Rokosov <ddrokosov@sberdevices.ru>");
MODULE_DESCRIPTION("MEMSensing MSA311 3-axis accelerometer driver");
MODULE_LICENSE("GPL");
|