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

#include <linux/bitfield.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/units.h>

#include <linux/unaligned.h>

#include <linux/iio/buffer.h>
#include <linux/iio/events.h>
#include <linux/iio/iio.h>
#include <linux/iio/kfifo_buf.h>
#include <linux/iio/sysfs.h>

#include <linux/regulator/consumer.h>

#include "adxl380.h"

#define ADXL380_ID_VAL				380
#define ADXL382_ID_VAL				382

#define ADXL380_DEVID_AD_REG			0x00
#define ADLX380_PART_ID_REG			0x02

#define ADXL380_X_DATA_H_REG			0x15
#define ADXL380_Y_DATA_H_REG			0x17
#define ADXL380_Z_DATA_H_REG			0x19
#define ADXL380_T_DATA_H_REG			0x1B

#define ADXL380_MISC_0_REG			0x20
#define ADXL380_XL382_MSK			BIT(7)

#define ADXL380_MISC_1_REG			0x21

#define ADXL380_X_DSM_OFFSET_REG		0x4D

#define ADXL380_ACT_INACT_CTL_REG		0x37
#define ADXL380_INACT_EN_MSK			BIT(2)
#define ADXL380_ACT_EN_MSK			BIT(0)

#define ADXL380_SNSR_AXIS_EN_REG		0x38
#define ADXL380_ACT_INACT_AXIS_EN_MSK		GENMASK(2, 0)

#define ADXL380_THRESH_ACT_H_REG		0x39
#define ADXL380_TIME_ACT_H_REG			0x3B
#define ADXL380_THRESH_INACT_H_REG		0x3E
#define ADXL380_TIME_INACT_H_REG		0x40
#define ADXL380_THRESH_MAX			GENMASK(12, 0)
#define ADXL380_TIME_MAX			GENMASK(24, 0)

#define ADXL380_FIFO_CONFIG_0_REG		0x30
#define ADXL380_FIFO_SAMPLES_8_MSK		BIT(0)
#define ADXL380_FIFO_MODE_MSK			GENMASK(5, 4)

#define ADXL380_FIFO_DISABLED			0
#define ADXL380_FIFO_NORMAL			1
#define ADXL380_FIFO_STREAMED			2
#define ADXL380_FIFO_TRIGGERED			3

#define ADXL380_FIFO_CONFIG_1_REG		0x31
#define ADXL380_FIFO_STATUS_0_REG		0x1E

#define ADXL380_TAP_THRESH_REG			0x43
#define ADXL380_TAP_DUR_REG			0x44
#define ADXL380_TAP_LATENT_REG			0x45
#define ADXL380_TAP_WINDOW_REG			0x46
#define ADXL380_TAP_TIME_MAX			GENMASK(7, 0)

#define ADXL380_TAP_CFG_REG			0x47
#define ADXL380_TAP_AXIS_MSK			GENMASK(1, 0)

#define ADXL380_TRIG_CFG_REG			0x49
#define ADXL380_TRIG_CFG_DEC_2X_MSK		BIT(7)
#define ADXL380_TRIG_CFG_SINC_RATE_MSK		BIT(6)

#define ADXL380_FILTER_REG			0x50
#define ADXL380_FILTER_EQ_FILT_MSK		BIT(6)
#define ADXL380_FILTER_LPF_MODE_MSK		GENMASK(5, 4)
#define ADXL380_FILTER_HPF_PATH_MSK		BIT(3)
#define ADXL380_FILTER_HPF_CORNER_MSK		GENMASK(2, 0)

#define ADXL380_OP_MODE_REG			0x26
#define ADXL380_OP_MODE_RANGE_MSK		GENMASK(7, 6)
#define ADXL380_OP_MODE_MSK			GENMASK(3, 0)
#define ADXL380_OP_MODE_STANDBY			0
#define ADXL380_OP_MODE_HEART_SOUND		1
#define ADXL380_OP_MODE_ULP			2
#define ADXL380_OP_MODE_VLP			3
#define ADXL380_OP_MODE_LP			4
#define ADXL380_OP_MODE_LP_ULP			6
#define ADXL380_OP_MODE_LP_VLP			7
#define ADXL380_OP_MODE_RBW			8
#define ADXL380_OP_MODE_RBW_ULP			10
#define ADXL380_OP_MODE_RBW_VLP			11
#define ADXL380_OP_MODE_HP			12
#define ADXL380_OP_MODE_HP_ULP			14
#define ADXL380_OP_MODE_HP_VLP			15

#define ADXL380_OP_MODE_4G_RANGE		0
#define ADXL382_OP_MODE_15G_RANGE		0
#define ADXL380_OP_MODE_8G_RANGE		1
#define ADXL382_OP_MODE_30G_RANGE		1
#define ADXL380_OP_MODE_16G_RANGE		2
#define ADXL382_OP_MODE_60G_RANGE		2

#define ADXL380_DIG_EN_REG			0x27
#define ADXL380_CHAN_EN_MSK(chan)		BIT(4 + (chan))
#define ADXL380_FIFO_EN_MSK			BIT(3)

#define ADXL380_INT0_MAP0_REG			0x2B
#define ADXL380_INT1_MAP0_REG			0x2D
#define ADXL380_INT_MAP0_INACT_INT0_MSK		BIT(6)
#define ADXL380_INT_MAP0_ACT_INT0_MSK		BIT(5)
#define ADXL380_INT_MAP0_FIFO_WM_INT0_MSK	BIT(3)

#define ADXL380_INT0_MAP1_REG			0x2C
#define ADXL380_INT1_MAP1_REG			0x2E
#define ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK	BIT(1)
#define ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK	BIT(0)

#define ADXL380_INT0_REG			0x5D
#define ADXL380_INT0_POL_MSK			BIT(7)

#define ADXL380_RESET_REG			0x2A
#define ADXL380_FIFO_DATA			0x1D

#define ADXL380_DEVID_AD_VAL			0xAD
#define ADXL380_RESET_CODE			0x52

#define ADXL380_STATUS_0_REG			0x11
#define ADXL380_STATUS_0_FIFO_FULL_MSK		BIT(1)
#define ADXL380_STATUS_0_FIFO_WM_MSK		BIT(3)

#define ADXL380_STATUS_1_INACT_MSK		BIT(6)
#define ADXL380_STATUS_1_ACT_MSK		BIT(5)
#define ADXL380_STATUS_1_DOUBLE_TAP_MSK		BIT(1)
#define ADXL380_STATUS_1_SINGLE_TAP_MSK		BIT(0)

#define ADXL380_FIFO_SAMPLES			315UL

enum adxl380_channels {
	ADXL380_ACCEL_X,
	ADXL380_ACCEL_Y,
	ADXL380_ACCEL_Z,
	ADXL380_TEMP,
	ADXL380_CH_NUM
};

enum adxl380_axis {
	ADXL380_X_AXIS,
	ADXL380_Y_AXIS,
	ADXL380_Z_AXIS,
};

enum adxl380_activity_type {
	ADXL380_ACTIVITY,
	ADXL380_INACTIVITY,
};

enum adxl380_tap_type {
	ADXL380_SINGLE_TAP,
	ADXL380_DOUBLE_TAP,
};

enum adxl380_tap_time_type {
	ADXL380_TAP_TIME_LATENT,
	ADXL380_TAP_TIME_WINDOW,
};

static const int adxl380_range_scale_factor_tbl[] = { 1, 2, 4 };

const struct adxl380_chip_info adxl380_chip_info = {
	.name = "adxl380",
	.chip_id = ADXL380_ID_VAL,
	.scale_tbl = {
		[ADXL380_OP_MODE_4G_RANGE] = { 0, 1307226 },
		[ADXL380_OP_MODE_8G_RANGE] = { 0, 2615434 },
		[ADXL380_OP_MODE_16G_RANGE] = { 0, 5229886 },
	},
	.samp_freq_tbl = { 8000, 16000, 32000 },
	/*
	 * The datasheet defines an intercept of 470 LSB at 25 degC
	 * and a sensitivity of 10.2 LSB/C.
	 */
	.temp_offset =  25 * 102 / 10 - 470,

};
EXPORT_SYMBOL_NS_GPL(adxl380_chip_info, "IIO_ADXL380");

const struct adxl380_chip_info adxl382_chip_info = {
	.name = "adxl382",
	.chip_id = ADXL382_ID_VAL,
	.scale_tbl = {
		[ADXL382_OP_MODE_15G_RANGE] = { 0, 4903325 },
		[ADXL382_OP_MODE_30G_RANGE] = { 0, 9806650 },
		[ADXL382_OP_MODE_60G_RANGE] = { 0, 19613300 },
	},
	.samp_freq_tbl = { 16000, 32000, 64000 },
	/*
	 * The datasheet defines an intercept of 570 LSB at 25 degC
	 * and a sensitivity of 10.2 LSB/C.
	 */
	.temp_offset =  25 * 102 / 10 - 570,
};
EXPORT_SYMBOL_NS_GPL(adxl382_chip_info, "IIO_ADXL380");

static const unsigned int adxl380_th_reg_high_addr[2] = {
	[ADXL380_ACTIVITY] = ADXL380_THRESH_ACT_H_REG,
	[ADXL380_INACTIVITY] = ADXL380_THRESH_INACT_H_REG,
};

static const unsigned int adxl380_time_reg_high_addr[2] = {
	[ADXL380_ACTIVITY] = ADXL380_TIME_ACT_H_REG,
	[ADXL380_INACTIVITY] = ADXL380_TIME_INACT_H_REG,
};

static const unsigned int adxl380_tap_time_reg[2] = {
	[ADXL380_TAP_TIME_LATENT] = ADXL380_TAP_LATENT_REG,
	[ADXL380_TAP_TIME_WINDOW] = ADXL380_TAP_WINDOW_REG,
};

struct adxl380_state {
	struct regmap *regmap;
	struct device *dev;
	const struct adxl380_chip_info *chip_info;
	/*
	 * Synchronize access to members of driver state, and ensure atomicity
	 * of consecutive regmap operations.
	 */
	struct mutex lock;
	enum adxl380_axis tap_axis_en;
	u8 range;
	u8 odr;
	u8 fifo_set_size;
	u8 transf_buf[3];
	u16 watermark;
	u32 act_time_ms;
	u32 act_threshold;
	u32 inact_time_ms;
	u32 inact_threshold;
	u32 tap_latent_us;
	u32 tap_window_us;
	u32 tap_duration_us;
	u32 tap_threshold;
	int irq;
	int int_map[2];
	int lpf_tbl[4];
	int hpf_tbl[7][2];

	__be16 fifo_buf[ADXL380_FIFO_SAMPLES] __aligned(IIO_DMA_MINALIGN);
};

bool adxl380_readable_noinc_reg(struct device *dev, unsigned int reg)
{
	return reg == ADXL380_FIFO_DATA;
}
EXPORT_SYMBOL_NS_GPL(adxl380_readable_noinc_reg, "IIO_ADXL380");

static int adxl380_set_measure_en(struct adxl380_state *st, bool en)
{
	int ret;
	unsigned int act_inact_ctl;
	u8 op_mode = ADXL380_OP_MODE_STANDBY;

	if (en) {
		ret = regmap_read(st->regmap, ADXL380_ACT_INACT_CTL_REG, &act_inact_ctl);
		if (ret)
			return ret;

		/* Activity/ Inactivity detection available only in VLP/ULP mode */
		if (FIELD_GET(ADXL380_ACT_EN_MSK, act_inact_ctl) ||
		    FIELD_GET(ADXL380_INACT_EN_MSK, act_inact_ctl))
			op_mode = ADXL380_OP_MODE_VLP;
		else
			op_mode = ADXL380_OP_MODE_HP;
	}

	return regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG,
				 ADXL380_OP_MODE_MSK,
				 FIELD_PREP(ADXL380_OP_MODE_MSK, op_mode));
}

static void adxl380_scale_act_inact_thresholds(struct adxl380_state *st,
					       u8 old_range,
					       u8 new_range)
{
	st->act_threshold = mult_frac(st->act_threshold,
				      adxl380_range_scale_factor_tbl[old_range],
				      adxl380_range_scale_factor_tbl[new_range]);
	st->inact_threshold = mult_frac(st->inact_threshold,
					adxl380_range_scale_factor_tbl[old_range],
					adxl380_range_scale_factor_tbl[new_range]);
}

static int adxl380_write_act_inact_threshold(struct adxl380_state *st,
					     enum adxl380_activity_type act,
					     unsigned int th)
{
	int ret;
	u8 reg = adxl380_th_reg_high_addr[act];

	if (th > ADXL380_THRESH_MAX)
		return -EINVAL;

	ret = regmap_write(st->regmap, reg + 1, th & GENMASK(7, 0));
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, reg, GENMASK(2, 0), th >> 8);
	if (ret)
		return ret;

	if (act == ADXL380_ACTIVITY)
		st->act_threshold = th;
	else
		st->inact_threshold = th;

	return 0;
}

static int adxl380_set_act_inact_threshold(struct iio_dev *indio_dev,
					   enum adxl380_activity_type act,
					   u16 th)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = adxl380_write_act_inact_threshold(st, act, th);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_set_tap_threshold_value(struct iio_dev *indio_dev, u8 th)
{
	int ret;
	struct adxl380_state *st = iio_priv(indio_dev);

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_write(st->regmap, ADXL380_TAP_THRESH_REG, th);
	if (ret)
		return ret;

	st->tap_threshold = th;

	return adxl380_set_measure_en(st, true);
}

static int _adxl380_write_tap_time_us(struct adxl380_state *st,
				      enum adxl380_tap_time_type tap_time_type,
				      u32 us)
{
	u8 reg = adxl380_tap_time_reg[tap_time_type];
	unsigned int reg_val;
	int ret;

	/* scale factor for tap window is 1250us / LSB */
	reg_val = DIV_ROUND_CLOSEST(us, 1250);
	if (reg_val > ADXL380_TAP_TIME_MAX)
		reg_val = ADXL380_TAP_TIME_MAX;

	ret = regmap_write(st->regmap, reg, reg_val);
	if (ret)
		return ret;

	if (tap_time_type == ADXL380_TAP_TIME_WINDOW)
		st->tap_window_us = us;
	else
		st->tap_latent_us = us;

	return 0;
}

static int adxl380_write_tap_time_us(struct adxl380_state *st,
				     enum adxl380_tap_time_type tap_time_type, u32 us)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = _adxl380_write_tap_time_us(st, tap_time_type, us);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_write_tap_dur_us(struct iio_dev *indio_dev, u32 us)
{
	int ret;
	unsigned int reg_val;
	struct adxl380_state *st = iio_priv(indio_dev);

	/* 625us per code is the scale factor of TAP_DUR register */
	reg_val = DIV_ROUND_CLOSEST(us, 625);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_write(st->regmap, ADXL380_TAP_DUR_REG, reg_val);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_read_chn(struct adxl380_state *st, u8 addr)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = regmap_bulk_read(st->regmap, addr, &st->transf_buf, 2);
	if (ret)
		return ret;

	return get_unaligned_be16(st->transf_buf);
}

static int adxl380_get_odr(struct adxl380_state *st, int *odr)
{
	int ret;
	unsigned int trig_cfg, odr_idx;

	ret = regmap_read(st->regmap, ADXL380_TRIG_CFG_REG, &trig_cfg);
	if (ret)
		return ret;

	odr_idx = (FIELD_GET(ADXL380_TRIG_CFG_SINC_RATE_MSK, trig_cfg) << 1) |
		  (FIELD_GET(ADXL380_TRIG_CFG_DEC_2X_MSK, trig_cfg) & 1);

	*odr = st->chip_info->samp_freq_tbl[odr_idx];

	return 0;
}

static const int adxl380_lpf_div[] = {
	1, 4, 8, 16,
};

static int adxl380_fill_lpf_tbl(struct adxl380_state *st)
{
	int ret, i;
	int odr;

	ret = adxl380_get_odr(st, &odr);
	if (ret)
		return ret;

	for (i = 0; i < ARRAY_SIZE(st->lpf_tbl); i++)
		st->lpf_tbl[i] = DIV_ROUND_CLOSEST(odr, adxl380_lpf_div[i]);

	return 0;
}

static const int adxl380_hpf_mul[] = {
	0, 247000, 62084, 15545, 3862, 954, 238,
};

static int adxl380_fill_hpf_tbl(struct adxl380_state *st)
{
	int i, ret, odr_hz;
	u32 multiplier;
	u64 div, rem, odr;

	ret =  adxl380_get_odr(st, &odr_hz);
	if (ret)
		return ret;

	for (i = 0; i < ARRAY_SIZE(adxl380_hpf_mul); i++) {
		odr = mul_u64_u32_shr(odr_hz, MEGA, 0);
		multiplier = adxl380_hpf_mul[i];
		div = div64_u64_rem(mul_u64_u32_shr(odr, multiplier, 0),
				    TERA * 100, &rem);

		st->hpf_tbl[i][0] = div;
		st->hpf_tbl[i][1] = div_u64(rem, MEGA * 100);
	}

	return 0;
}

static int adxl380_set_odr(struct adxl380_state *st, u8 odr)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG,
				 ADXL380_TRIG_CFG_DEC_2X_MSK,
				 FIELD_PREP(ADXL380_TRIG_CFG_DEC_2X_MSK, odr & 1));
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_TRIG_CFG_REG,
				 ADXL380_TRIG_CFG_SINC_RATE_MSK,
				 FIELD_PREP(ADXL380_TRIG_CFG_SINC_RATE_MSK, odr >> 1));
	if (ret)
		return ret;

	ret = adxl380_set_measure_en(st, true);
	if (ret)
		return ret;

	ret = adxl380_fill_lpf_tbl(st);
	if (ret)
		return ret;

	return adxl380_fill_hpf_tbl(st);
}

static int adxl380_find_match_1d_tbl(const int *array, unsigned int size,
				     int val)
{
	int i;

	for (i = 0; i < size; i++) {
		if (val == array[i])
			return i;
	}

	return size - 1;
}

static int adxl380_find_match_2d_tbl(const int (*freq_tbl)[2], int n, int val, int val2)
{
	int i;

	for (i = 0; i < n; i++) {
		if (freq_tbl[i][0] == val && freq_tbl[i][1] == val2)
			return i;
	}

	return -EINVAL;
}

static int adxl380_get_lpf(struct adxl380_state *st, int *lpf)
{
	int ret;
	unsigned int trig_cfg, lpf_idx;

	guard(mutex)(&st->lock);

	ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg);
	if (ret)
		return ret;

	lpf_idx = FIELD_GET(ADXL380_FILTER_LPF_MODE_MSK, trig_cfg);

	*lpf = st->lpf_tbl[lpf_idx];

	return 0;
}

static int adxl380_set_lpf(struct adxl380_state *st, u8 lpf)
{
	int ret;
	u8 eq_bypass = 0;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	if (lpf)
		eq_bypass = 1;

	ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
				 ADXL380_FILTER_EQ_FILT_MSK,
				 FIELD_PREP(ADXL380_FILTER_EQ_FILT_MSK, eq_bypass));
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
				 ADXL380_FILTER_LPF_MODE_MSK,
				 FIELD_PREP(ADXL380_FILTER_LPF_MODE_MSK, lpf));
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_get_hpf(struct adxl380_state *st, int *hpf_int, int *hpf_frac)
{
	int ret;
	unsigned int trig_cfg, hpf_idx;

	guard(mutex)(&st->lock);

	ret = regmap_read(st->regmap, ADXL380_FILTER_REG, &trig_cfg);
	if (ret)
		return ret;

	hpf_idx = FIELD_GET(ADXL380_FILTER_HPF_CORNER_MSK, trig_cfg);

	*hpf_int = st->hpf_tbl[hpf_idx][0];
	*hpf_frac = st->hpf_tbl[hpf_idx][1];

	return 0;
}

static int adxl380_set_hpf(struct adxl380_state *st, u8 hpf)
{
	int ret;
	u8 hpf_path = 0;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	if (hpf)
		hpf_path = 1;

	ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
				 ADXL380_FILTER_HPF_PATH_MSK,
				 FIELD_PREP(ADXL380_FILTER_HPF_PATH_MSK, hpf_path));
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_FILTER_REG,
				 ADXL380_FILTER_HPF_CORNER_MSK,
				 FIELD_PREP(ADXL380_FILTER_HPF_CORNER_MSK, hpf));
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int _adxl380_set_act_inact_time_ms(struct adxl380_state *st,
					  enum adxl380_activity_type act,
					  u32 ms)
{
	u8 reg = adxl380_time_reg_high_addr[act];
	unsigned int reg_val;
	int ret;

	/* 500us per code is the scale factor of TIME_ACT / TIME_INACT registers */
	reg_val = min(DIV_ROUND_CLOSEST(ms * 1000, 500), ADXL380_TIME_MAX);

	put_unaligned_be24(reg_val, &st->transf_buf[0]);

	ret = regmap_bulk_write(st->regmap, reg, st->transf_buf, sizeof(st->transf_buf));
	if (ret)
		return ret;

	if (act == ADXL380_ACTIVITY)
		st->act_time_ms = ms;
	else
		st->inact_time_ms = ms;

	return 0;
}

static int adxl380_set_act_inact_time_ms(struct adxl380_state *st,
					 enum adxl380_activity_type act,
					 u32 ms)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = _adxl380_set_act_inact_time_ms(st, act, ms);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_set_range(struct adxl380_state *st, u8 range)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_OP_MODE_REG,
				 ADXL380_OP_MODE_RANGE_MSK,
				 FIELD_PREP(ADXL380_OP_MODE_RANGE_MSK, range));

	if (ret)
		return ret;

	adxl380_scale_act_inact_thresholds(st, st->range, range);

	/* Activity thresholds depend on range */
	ret = adxl380_write_act_inact_threshold(st, ADXL380_ACTIVITY,
						st->act_threshold);
	if (ret)
		return ret;

	ret = adxl380_write_act_inact_threshold(st, ADXL380_INACTIVITY,
						st->inact_threshold);
	if (ret)
		return ret;

	st->range = range;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_write_act_inact_en(struct adxl380_state *st,
				      enum adxl380_activity_type type,
				      bool en)
{
	if (type == ADXL380_ACTIVITY)
		return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG,
					  ADXL380_ACT_EN_MSK,
					  FIELD_PREP(ADXL380_ACT_EN_MSK, en));

	return regmap_update_bits(st->regmap, ADXL380_ACT_INACT_CTL_REG,
				  ADXL380_INACT_EN_MSK,
				  FIELD_PREP(ADXL380_INACT_EN_MSK, en));
}

static int adxl380_read_act_inact_int(struct adxl380_state *st,
				      enum adxl380_activity_type type,
				      bool *en)
{
	int ret;
	unsigned int reg_val;

	guard(mutex)(&st->lock);

	ret = regmap_read(st->regmap, st->int_map[0], &reg_val);
	if (ret)
		return ret;

	if (type == ADXL380_ACTIVITY)
		*en = FIELD_GET(ADXL380_INT_MAP0_ACT_INT0_MSK, reg_val);
	else
		*en = FIELD_GET(ADXL380_INT_MAP0_INACT_INT0_MSK, reg_val);

	return 0;
}

static int adxl380_write_act_inact_int(struct adxl380_state *st,
				       enum adxl380_activity_type act,
				       bool en)
{
	if (act == ADXL380_ACTIVITY)
		return regmap_update_bits(st->regmap, st->int_map[0],
					  ADXL380_INT_MAP0_ACT_INT0_MSK,
					  FIELD_PREP(ADXL380_INT_MAP0_ACT_INT0_MSK, en));

	return regmap_update_bits(st->regmap, st->int_map[0],
				  ADXL380_INT_MAP0_INACT_INT0_MSK,
				  FIELD_PREP(ADXL380_INT_MAP0_INACT_INT0_MSK, en));
}

static int adxl380_act_inact_config(struct adxl380_state *st,
				    enum adxl380_activity_type type,
				    bool en)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = adxl380_write_act_inact_en(st, type, en);
	if (ret)
		return ret;

	ret = adxl380_write_act_inact_int(st, type, en);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_write_tap_axis(struct adxl380_state *st,
				  enum adxl380_axis axis)
{
	int ret;

	ret = regmap_update_bits(st->regmap, ADXL380_TAP_CFG_REG,
				 ADXL380_TAP_AXIS_MSK,
				 FIELD_PREP(ADXL380_TAP_AXIS_MSK, axis));

	if (ret)
		return ret;

	st->tap_axis_en = axis;

	return 0;
}

static int adxl380_read_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool *en)
{
	int ret;
	unsigned int reg_val;

	ret = regmap_read(st->regmap, st->int_map[1], &reg_val);
	if (ret)
		return ret;

	if (type == ADXL380_SINGLE_TAP)
		*en = FIELD_GET(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, reg_val);
	else
		*en = FIELD_GET(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, reg_val);

	return 0;
}

static int adxl380_write_tap_int(struct adxl380_state *st, enum adxl380_tap_type type, bool en)
{
	if (type == ADXL380_SINGLE_TAP)
		return regmap_update_bits(st->regmap, st->int_map[1],
					  ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK,
					  FIELD_PREP(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK, en));

	return regmap_update_bits(st->regmap, st->int_map[1],
				  ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK,
				  FIELD_PREP(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK, en));
}

static int adxl380_tap_config(struct adxl380_state *st,
			      enum adxl380_axis axis,
			      enum adxl380_tap_type type,
			      bool en)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = adxl380_write_tap_axis(st, axis);
	if (ret)
		return ret;

	ret = adxl380_write_tap_int(st, type, en);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_set_fifo_samples(struct adxl380_state *st)
{
	int ret;
	u16 fifo_samples = st->watermark * st->fifo_set_size;

	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
				 ADXL380_FIFO_SAMPLES_8_MSK,
				 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK,
					    (fifo_samples & BIT(8))));
	if (ret)
		return ret;

	return regmap_write(st->regmap, ADXL380_FIFO_CONFIG_1_REG,
			    fifo_samples & 0xFF);
}

static int adxl380_get_status(struct adxl380_state *st, u8 *status0, u8 *status1)
{
	int ret;

	/* STATUS0, STATUS1 are adjacent regs */
	ret = regmap_bulk_read(st->regmap, ADXL380_STATUS_0_REG,
			       &st->transf_buf, 2);
	if (ret)
		return ret;

	*status0 = st->transf_buf[0];
	*status1 = st->transf_buf[1];

	return 0;
}

static int adxl380_get_fifo_entries(struct adxl380_state *st, u16 *fifo_entries)
{
	int ret;

	ret = regmap_bulk_read(st->regmap, ADXL380_FIFO_STATUS_0_REG,
			       &st->transf_buf, 2);
	if (ret)
		return ret;

	*fifo_entries = st->transf_buf[0] | ((BIT(0) & st->transf_buf[1]) << 8);

	return 0;
}

static void adxl380_push_event(struct iio_dev *indio_dev, s64 timestamp,
			       u8 status1)
{
	if (FIELD_GET(ADXL380_STATUS_1_ACT_MSK, status1))
		iio_push_event(indio_dev,
			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
						  IIO_EV_TYPE_THRESH, IIO_EV_DIR_RISING),
			       timestamp);

	if (FIELD_GET(ADXL380_STATUS_1_INACT_MSK, status1))
		iio_push_event(indio_dev,
			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
						  IIO_EV_TYPE_THRESH, IIO_EV_DIR_FALLING),
			       timestamp);
	if (FIELD_GET(ADXL380_STATUS_1_SINGLE_TAP_MSK, status1))
		iio_push_event(indio_dev,
			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
						  IIO_EV_TYPE_GESTURE, IIO_EV_DIR_SINGLETAP),
			       timestamp);

	if (FIELD_GET(ADXL380_STATUS_1_DOUBLE_TAP_MSK, status1))
		iio_push_event(indio_dev,
			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
						  IIO_EV_TYPE_GESTURE, IIO_EV_DIR_DOUBLETAP),
			       timestamp);
}

static irqreturn_t adxl380_irq_handler(int irq, void  *p)
{
	struct iio_dev *indio_dev = p;
	struct adxl380_state *st = iio_priv(indio_dev);
	u8 status0, status1;
	u16 fifo_entries;
	int i;
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_get_status(st, &status0, &status1);
	if (ret)
		return IRQ_HANDLED;

	adxl380_push_event(indio_dev, iio_get_time_ns(indio_dev), status1);

	if (!FIELD_GET(ADXL380_STATUS_0_FIFO_WM_MSK, status0))
		return IRQ_HANDLED;

	ret = adxl380_get_fifo_entries(st, &fifo_entries);
	if (ret)
		return IRQ_HANDLED;

	for (i = 0; i < fifo_entries; i += st->fifo_set_size) {
		ret = regmap_noinc_read(st->regmap, ADXL380_FIFO_DATA,
					&st->fifo_buf[i],
					2 * st->fifo_set_size);
		if (ret)
			return IRQ_HANDLED;
		iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
	}

	return IRQ_HANDLED;
}

static int adxl380_write_calibbias_value(struct adxl380_state *st,
					 unsigned long chan_addr,
					 s8 calibbias)
{
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_write(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, calibbias);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_read_calibbias_value(struct adxl380_state *st,
					unsigned long chan_addr,
					int *calibbias)
{
	int ret;
	unsigned int reg_val;

	guard(mutex)(&st->lock);

	ret = regmap_read(st->regmap, ADXL380_X_DSM_OFFSET_REG + chan_addr, &reg_val);
	if (ret)
		return ret;

	*calibbias = sign_extend32(reg_val, 7);

	return 0;
}

static ssize_t hwfifo_watermark_min_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	return sysfs_emit(buf, "1\n");
}

static ssize_t hwfifo_watermark_max_show(struct device *dev,
					 struct device_attribute *attr,
					 char *buf)
{
	return sysfs_emit(buf, "%lu\n", ADXL380_FIFO_SAMPLES);
}

static ssize_t adxl380_get_fifo_watermark(struct device *dev,
					  struct device_attribute *attr,
					  char *buf)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct adxl380_state *st = iio_priv(indio_dev);

	return sysfs_emit(buf, "%d\n", st->watermark);
}

static ssize_t adxl380_get_fifo_enabled(struct device *dev,
					struct device_attribute *attr,
					char *buf)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret;
	unsigned int reg_val;

	ret = regmap_read(st->regmap, ADXL380_DIG_EN_REG, &reg_val);
	if (ret)
		return ret;

	return sysfs_emit(buf, "%lu\n",
			  FIELD_GET(ADXL380_FIFO_EN_MSK, reg_val));
}

static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min, 0);
static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max, 0);
static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
		       adxl380_get_fifo_watermark, NULL, 0);
static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
		       adxl380_get_fifo_enabled, NULL, 0);

static const struct iio_dev_attr *adxl380_fifo_attributes[] = {
	&iio_dev_attr_hwfifo_watermark_min,
	&iio_dev_attr_hwfifo_watermark_max,
	&iio_dev_attr_hwfifo_watermark,
	&iio_dev_attr_hwfifo_enabled,
	NULL
};

static int adxl380_buffer_postenable(struct iio_dev *indio_dev)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int i;
	int ret;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap,
				 st->int_map[0],
				 ADXL380_INT_MAP0_FIFO_WM_INT0_MSK,
				 FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 1));
	if (ret)
		return ret;

	for_each_clear_bit(i, indio_dev->active_scan_mask, ADXL380_CH_NUM) {
		ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
					 ADXL380_CHAN_EN_MSK(i),
					 0 << (4 + i));
		if (ret)
			return ret;
	}

	st->fifo_set_size = bitmap_weight(indio_dev->active_scan_mask,
					  iio_get_masklength(indio_dev));

	if ((st->watermark * st->fifo_set_size) > ADXL380_FIFO_SAMPLES)
		st->watermark = (ADXL380_FIFO_SAMPLES  / st->fifo_set_size);

	ret = adxl380_set_fifo_samples(st);
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK,
				 FIELD_PREP(ADXL380_FIFO_EN_MSK, 1));
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static int adxl380_buffer_predisable(struct iio_dev *indio_dev)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret, i;

	guard(mutex)(&st->lock);

	ret = adxl380_set_measure_en(st, false);
	if (ret)
		return ret;

	ret = regmap_update_bits(st->regmap,
				 st->int_map[0],
				 ADXL380_INT_MAP0_FIFO_WM_INT0_MSK,
				 FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK, 0));
	if (ret)
		return ret;

	for (i = 0; i < indio_dev->num_channels; i++) {
		ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
					 ADXL380_CHAN_EN_MSK(i),
					 1 << (4 + i));
		if (ret)
			return ret;
	}

	ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG, ADXL380_FIFO_EN_MSK,
				 FIELD_PREP(ADXL380_FIFO_EN_MSK, 0));
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

static const struct iio_buffer_setup_ops adxl380_buffer_ops = {
	.postenable = adxl380_buffer_postenable,
	.predisable = adxl380_buffer_predisable,
};

static int adxl380_read_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val, int *val2, long info)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret;

	switch (info) {
	case IIO_CHAN_INFO_RAW:
		ret = iio_device_claim_direct_mode(indio_dev);
		if (ret)
			return ret;

		ret = adxl380_read_chn(st, chan->address);
		iio_device_release_direct_mode(indio_dev);
		if (ret < 0)
			return ret;

		*val = sign_extend32(ret >> chan->scan_type.shift,
				     chan->scan_type.realbits - 1);
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_SCALE:
		switch (chan->type) {
		case IIO_ACCEL:
			scoped_guard(mutex, &st->lock) {
				*val = st->chip_info->scale_tbl[st->range][0];
				*val2 = st->chip_info->scale_tbl[st->range][1];
			}
			return IIO_VAL_INT_PLUS_NANO;
		case IIO_TEMP:
			/* 10.2 LSB / Degree Celsius */
			*val = 10000;
			*val2 = 102;
			return IIO_VAL_FRACTIONAL;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_OFFSET:
		switch (chan->type) {
		case IIO_TEMP:
			*val = st->chip_info->temp_offset;
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_CALIBBIAS:
		switch (chan->type) {
		case IIO_ACCEL:
			ret = adxl380_read_calibbias_value(st, chan->scan_index, val);
			if (ret)
				return ret;
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}
	case IIO_CHAN_INFO_SAMP_FREQ:
		ret = adxl380_get_odr(st, val);
		if (ret)
			return ret;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
		ret =  adxl380_get_lpf(st, val);
		if (ret)
			return ret;
		return IIO_VAL_INT;
	case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
		ret =  adxl380_get_hpf(st, val, val2);
		if (ret)
			return ret;
		return IIO_VAL_INT_PLUS_MICRO;
	}

	return -EINVAL;
}

static int adxl380_read_avail(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan,
			      const int **vals, int *type, int *length,
			      long mask)
{
	struct adxl380_state *st = iio_priv(indio_dev);

	if (chan->type != IIO_ACCEL)
		return -EINVAL;

	switch (mask) {
	case IIO_CHAN_INFO_SCALE:
		*vals = (const int *)st->chip_info->scale_tbl;
		*type = IIO_VAL_INT_PLUS_NANO;
		*length = ARRAY_SIZE(st->chip_info->scale_tbl) * 2;
		return IIO_AVAIL_LIST;
	case IIO_CHAN_INFO_SAMP_FREQ:
		*vals = (const int *)st->chip_info->samp_freq_tbl;
		*type = IIO_VAL_INT;
		*length = ARRAY_SIZE(st->chip_info->samp_freq_tbl);
		return IIO_AVAIL_LIST;
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
		*vals = (const int *)st->lpf_tbl;
		*type = IIO_VAL_INT;
		*length = ARRAY_SIZE(st->lpf_tbl);
		return IIO_AVAIL_LIST;
	case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
		*vals = (const int *)st->hpf_tbl;
		*type = IIO_VAL_INT_PLUS_MICRO;
		/* Values are stored in a 2D matrix */
		*length = ARRAY_SIZE(st->hpf_tbl) * 2;
		return IIO_AVAIL_LIST;
	default:
		return -EINVAL;
	}
}

static int adxl380_write_raw(struct iio_dev *indio_dev,
			     struct iio_chan_spec const *chan,
			     int val, int val2, long info)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int odr_index, lpf_index, hpf_index, range_index;

	switch (info) {
	case IIO_CHAN_INFO_SAMP_FREQ:
		odr_index = adxl380_find_match_1d_tbl(st->chip_info->samp_freq_tbl,
						      ARRAY_SIZE(st->chip_info->samp_freq_tbl),
						      val);
		return adxl380_set_odr(st, odr_index);
	case IIO_CHAN_INFO_CALIBBIAS:
		return adxl380_write_calibbias_value(st, chan->scan_index, val);
	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
		lpf_index = adxl380_find_match_1d_tbl(st->lpf_tbl,
						      ARRAY_SIZE(st->lpf_tbl),
						      val);
		return adxl380_set_lpf(st, lpf_index);
	case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY:
		hpf_index = adxl380_find_match_2d_tbl(st->hpf_tbl,
						      ARRAY_SIZE(st->hpf_tbl),
						      val, val2);
		if (hpf_index < 0)
			return hpf_index;
		return adxl380_set_hpf(st, hpf_index);
	case IIO_CHAN_INFO_SCALE:
		range_index = adxl380_find_match_2d_tbl(st->chip_info->scale_tbl,
							ARRAY_SIZE(st->chip_info->scale_tbl),
							val, val2);
		if (range_index < 0)
			return range_index;
		return adxl380_set_range(st, range_index);
	default:
		return -EINVAL;
	}
}

static int adxl380_write_raw_get_fmt(struct iio_dev *indio_dev,
				     struct iio_chan_spec const *chan,
				     long info)
{
	switch (info) {
	case IIO_CHAN_INFO_SCALE:
		if (chan->type != IIO_ACCEL)
			return -EINVAL;

		return IIO_VAL_INT_PLUS_NANO;
	default:
		return IIO_VAL_INT_PLUS_MICRO;
	}
}

static int adxl380_read_event_config(struct iio_dev *indio_dev,
				     const struct iio_chan_spec *chan,
				     enum iio_event_type type,
				     enum iio_event_direction dir)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret;
	bool int_en;
	bool tap_axis_en = false;

	switch (chan->channel2) {
	case IIO_MOD_X:
		tap_axis_en = st->tap_axis_en == ADXL380_X_AXIS;
		break;
	case IIO_MOD_Y:
		tap_axis_en = st->tap_axis_en == ADXL380_Y_AXIS;
		break;
	case IIO_MOD_Z:
		tap_axis_en = st->tap_axis_en == ADXL380_Z_AXIS;
		break;
	default:
		return -EINVAL;
	}

	switch (dir) {
	case IIO_EV_DIR_RISING:
		ret = adxl380_read_act_inact_int(st, ADXL380_ACTIVITY, &int_en);
		if (ret)
			return ret;
		return int_en;
	case IIO_EV_DIR_FALLING:
		ret = adxl380_read_act_inact_int(st, ADXL380_INACTIVITY, &int_en);
		if (ret)
			return ret;
		return int_en;
	case IIO_EV_DIR_SINGLETAP:
		ret = adxl380_read_tap_int(st, ADXL380_SINGLE_TAP, &int_en);
		if (ret)
			return ret;
		return int_en && tap_axis_en;
	case IIO_EV_DIR_DOUBLETAP:
		ret = adxl380_read_tap_int(st, ADXL380_DOUBLE_TAP, &int_en);
		if (ret)
			return ret;
		return int_en && tap_axis_en;
	default:
		return -EINVAL;
	}
}

static int adxl380_write_event_config(struct iio_dev *indio_dev,
				      const struct iio_chan_spec *chan,
				      enum iio_event_type type,
				      enum iio_event_direction dir,
				      bool state)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	enum adxl380_axis axis;

	switch (chan->channel2) {
	case IIO_MOD_X:
		axis = ADXL380_X_AXIS;
		break;
	case IIO_MOD_Y:
		axis = ADXL380_Y_AXIS;
		break;
	case IIO_MOD_Z:
		axis = ADXL380_Z_AXIS;
		break;
	default:
		return -EINVAL;
	}

	switch (dir) {
	case IIO_EV_DIR_RISING:
		return adxl380_act_inact_config(st, ADXL380_ACTIVITY, state);
	case IIO_EV_DIR_FALLING:
		return adxl380_act_inact_config(st, ADXL380_INACTIVITY, state);
	case IIO_EV_DIR_SINGLETAP:
		return adxl380_tap_config(st, axis, ADXL380_SINGLE_TAP, state);
	case IIO_EV_DIR_DOUBLETAP:
		return adxl380_tap_config(st, axis, ADXL380_DOUBLE_TAP, state);
	default:
		return -EINVAL;
	}
}

static int adxl380_read_event_value(struct iio_dev *indio_dev,
				    const struct iio_chan_spec *chan,
				    enum iio_event_type type,
				    enum iio_event_direction dir,
				    enum iio_event_info info,
				    int *val, int *val2)
{
	struct adxl380_state *st = iio_priv(indio_dev);

	guard(mutex)(&st->lock);

	switch (type) {
	case IIO_EV_TYPE_THRESH:
		switch (info) {
		case IIO_EV_INFO_VALUE: {
			switch (dir) {
			case IIO_EV_DIR_RISING:
				*val = st->act_threshold;
				return IIO_VAL_INT;
			case IIO_EV_DIR_FALLING:
				*val = st->inact_threshold;
				return IIO_VAL_INT;
			default:
				return -EINVAL;
			}
		}
		case IIO_EV_INFO_PERIOD:
			switch (dir) {
			case IIO_EV_DIR_RISING:
				*val = st->act_time_ms;
				*val2 = 1000;
				return IIO_VAL_FRACTIONAL;
			case IIO_EV_DIR_FALLING:
				*val = st->inact_time_ms;
				*val2 = 1000;
				return IIO_VAL_FRACTIONAL;
			default:
				return -EINVAL;
			}
		default:
			return -EINVAL;
		}
	case IIO_EV_TYPE_GESTURE:
		switch (info) {
		case IIO_EV_INFO_VALUE:
			*val = st->tap_threshold;
			return IIO_VAL_INT;
		case IIO_EV_INFO_RESET_TIMEOUT:
			*val = st->tap_window_us;
			*val2 = 1000000;
			return IIO_VAL_FRACTIONAL;
		case IIO_EV_INFO_TAP2_MIN_DELAY:
			*val = st->tap_latent_us;
			*val2 = 1000000;
			return IIO_VAL_FRACTIONAL;
		default:
			return -EINVAL;
		}
	default:
		return -EINVAL;
	}
}

static int adxl380_write_event_value(struct iio_dev *indio_dev,
				     const struct iio_chan_spec *chan,
				     enum iio_event_type type, enum iio_event_direction dir,
				     enum iio_event_info info, int val, int val2)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	u32 val_ms, val_us;

	if (chan->type != IIO_ACCEL)
		return -EINVAL;

	switch (type) {
	case IIO_EV_TYPE_THRESH:
		switch (info) {
		case IIO_EV_INFO_VALUE:
			switch (dir) {
			case IIO_EV_DIR_RISING:
				return adxl380_set_act_inact_threshold(indio_dev,
								       ADXL380_ACTIVITY, val);
			case IIO_EV_DIR_FALLING:
				return adxl380_set_act_inact_threshold(indio_dev,
								       ADXL380_INACTIVITY, val);
			default:
				return -EINVAL;
			}
		case IIO_EV_INFO_PERIOD:
			val_ms = val * 1000 + DIV_ROUND_UP(val2, 1000);
			switch (dir) {
			case IIO_EV_DIR_RISING:
				return adxl380_set_act_inact_time_ms(st,
								     ADXL380_ACTIVITY, val_ms);
			case IIO_EV_DIR_FALLING:
				return adxl380_set_act_inact_time_ms(st,
								     ADXL380_INACTIVITY, val_ms);
			default:
				return -EINVAL;
			}

		default:
			return -EINVAL;
		}
	case IIO_EV_TYPE_GESTURE:
		switch (info) {
		case IIO_EV_INFO_VALUE:
			return adxl380_set_tap_threshold_value(indio_dev, val);
		case IIO_EV_INFO_RESET_TIMEOUT:
			val_us = val * 1000000 + val2;
			return adxl380_write_tap_time_us(st,
							 ADXL380_TAP_TIME_WINDOW,
							 val_us);
		case IIO_EV_INFO_TAP2_MIN_DELAY:
			val_us = val * 1000000 + val2;
			return adxl380_write_tap_time_us(st,
							 ADXL380_TAP_TIME_LATENT,
							 val_us);
		default:
			return -EINVAL;
		}
	default:
		return -EINVAL;
	}
}

static ssize_t in_accel_gesture_tap_maxtomin_time_show(struct device *dev,
						       struct device_attribute *attr,
						       char *buf)
{
	int vals[2];
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct adxl380_state *st = iio_priv(indio_dev);

	guard(mutex)(&st->lock);

	vals[0] = st->tap_duration_us;
	vals[1] = MICRO;

	return iio_format_value(buf, IIO_VAL_FRACTIONAL, 2, vals);
}

static ssize_t in_accel_gesture_tap_maxtomin_time_store(struct device *dev,
							struct device_attribute *attr,
							const char *buf, size_t len)
{
	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
	struct adxl380_state *st = iio_priv(indio_dev);
	int ret, val_int, val_fract_us;

	guard(mutex)(&st->lock);

	ret = iio_str_to_fixpoint(buf, 100000, &val_int, &val_fract_us);
	if (ret)
		return ret;

	/* maximum value is 255 * 625 us = 0.159375 seconds */
	if (val_int || val_fract_us > 159375 || val_fract_us < 0)
		return -EINVAL;

	ret = adxl380_write_tap_dur_us(indio_dev, val_fract_us);
	if (ret)
		return ret;

	return len;
}

static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time, 0);

static struct attribute *adxl380_event_attributes[] = {
	&iio_dev_attr_in_accel_gesture_tap_maxtomin_time.dev_attr.attr,
	NULL
};

static const struct attribute_group adxl380_event_attribute_group = {
	.attrs = adxl380_event_attributes,
};

static int adxl380_reg_access(struct iio_dev *indio_dev,
			      unsigned int reg,
			      unsigned int writeval,
			      unsigned int *readval)
{
	struct adxl380_state *st = iio_priv(indio_dev);

	if (readval)
		return regmap_read(st->regmap, reg, readval);

	return regmap_write(st->regmap, reg, writeval);
}

static int adxl380_set_watermark(struct iio_dev *indio_dev, unsigned int val)
{
	struct adxl380_state *st  = iio_priv(indio_dev);

	st->watermark = min(val, ADXL380_FIFO_SAMPLES);

	return 0;
}

static const struct iio_info adxl380_info = {
	.read_raw = adxl380_read_raw,
	.read_avail = &adxl380_read_avail,
	.write_raw = adxl380_write_raw,
	.write_raw_get_fmt = adxl380_write_raw_get_fmt,
	.read_event_config = adxl380_read_event_config,
	.write_event_config = adxl380_write_event_config,
	.read_event_value = adxl380_read_event_value,
	.write_event_value = adxl380_write_event_value,
	.event_attrs = &adxl380_event_attribute_group,
	.debugfs_reg_access = &adxl380_reg_access,
	.hwfifo_set_watermark = adxl380_set_watermark,
};

static const struct iio_event_spec adxl380_events[] = {
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_RISING,
		.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
				       BIT(IIO_EV_INFO_VALUE) |
				       BIT(IIO_EV_INFO_PERIOD),
	},
	{
		.type = IIO_EV_TYPE_THRESH,
		.dir = IIO_EV_DIR_FALLING,
		.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
				       BIT(IIO_EV_INFO_VALUE) |
				       BIT(IIO_EV_INFO_PERIOD),
	},
	{
		.type = IIO_EV_TYPE_GESTURE,
		.dir = IIO_EV_DIR_SINGLETAP,
		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
				       BIT(IIO_EV_INFO_RESET_TIMEOUT),
	},
	{
		.type = IIO_EV_TYPE_GESTURE,
		.dir = IIO_EV_DIR_DOUBLETAP,
		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
				       BIT(IIO_EV_INFO_RESET_TIMEOUT) |
				       BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
	},
};

#define ADXL380_ACCEL_CHANNEL(index, reg, axis) {			\
	.type = IIO_ACCEL,						\
	.address = reg,							\
	.modified = 1,							\
	.channel2 = IIO_MOD_##axis,					\
	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |			\
			      BIT(IIO_CHAN_INFO_CALIBBIAS),		\
	.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),	\
	.info_mask_shared_by_all_available =				\
		BIT(IIO_CHAN_INFO_SAMP_FREQ),				\
	.info_mask_shared_by_type =					\
		BIT(IIO_CHAN_INFO_SCALE) |				\
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) |	\
		BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY),	\
	.info_mask_shared_by_type_available =				\
		BIT(IIO_CHAN_INFO_SCALE) |				\
		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) |	\
		BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY),	\
	.scan_index = index,						\
	.scan_type = {							\
		.sign = 's',						\
		.realbits = 16,						\
		.storagebits = 16,					\
		.endianness = IIO_BE,					\
	},								\
	.event_spec = adxl380_events,					\
	.num_event_specs = ARRAY_SIZE(adxl380_events)			\
}

static const struct iio_chan_spec adxl380_channels[] = {
	ADXL380_ACCEL_CHANNEL(0, ADXL380_X_DATA_H_REG, X),
	ADXL380_ACCEL_CHANNEL(1, ADXL380_Y_DATA_H_REG, Y),
	ADXL380_ACCEL_CHANNEL(2, ADXL380_Z_DATA_H_REG, Z),
	{
		.type = IIO_TEMP,
		.address = ADXL380_T_DATA_H_REG,
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
				      BIT(IIO_CHAN_INFO_SCALE) |
				      BIT(IIO_CHAN_INFO_OFFSET),
		.scan_index = 3,
		.scan_type = {
			.sign = 's',
			.realbits = 12,
			.storagebits = 16,
			.shift = 4,
			.endianness = IIO_BE,
		},
	},
};

static int adxl380_config_irq(struct iio_dev *indio_dev)
{
	struct adxl380_state *st = iio_priv(indio_dev);
	unsigned long irq_flag;
	u32 irq_type;
	u8 polarity;
	int ret;

	st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT0");
	if (st->irq > 0) {
		st->int_map[0] = ADXL380_INT0_MAP0_REG;
		st->int_map[1] = ADXL380_INT0_MAP1_REG;
	} else {
		st->irq = fwnode_irq_get_byname(dev_fwnode(st->dev), "INT1");
		if (st->irq > 0)
			return dev_err_probe(st->dev, -ENODEV,
					     "no interrupt name specified");
		st->int_map[0] = ADXL380_INT1_MAP0_REG;
		st->int_map[1] = ADXL380_INT1_MAP1_REG;
	}

	irq_type = irq_get_trigger_type(st->irq);
	if (irq_type == IRQ_TYPE_LEVEL_HIGH) {
		polarity = 0;
		irq_flag = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
	} else if (irq_type == IRQ_TYPE_LEVEL_LOW) {
		polarity = 1;
		irq_flag = IRQF_TRIGGER_LOW | IRQF_ONESHOT;
	} else {
		return dev_err_probe(st->dev, -EINVAL,
				     "Invalid interrupt 0x%x. Only level interrupts supported\n",
				     irq_type);
	}

	ret = regmap_update_bits(st->regmap, ADXL380_INT0_REG,
				 ADXL380_INT0_POL_MSK,
				 FIELD_PREP(ADXL380_INT0_POL_MSK, polarity));
	if (ret)
		return ret;

	return devm_request_threaded_irq(st->dev, st->irq, NULL,
					 adxl380_irq_handler, irq_flag,
					 indio_dev->name, indio_dev);
}

static int adxl380_setup(struct iio_dev *indio_dev)
{
	unsigned int reg_val;
	u16 part_id, chip_id;
	int ret, i;
	struct adxl380_state *st = iio_priv(indio_dev);

	ret = regmap_read(st->regmap, ADXL380_DEVID_AD_REG, &reg_val);
	if (ret)
		return ret;

	if (reg_val != ADXL380_DEVID_AD_VAL)
		dev_warn(st->dev, "Unknown chip id %x\n", reg_val);

	ret = regmap_bulk_read(st->regmap, ADLX380_PART_ID_REG,
			       &st->transf_buf, 2);
	if (ret)
		return ret;

	part_id = get_unaligned_be16(st->transf_buf);
	part_id >>= 4;

	if (part_id != ADXL380_ID_VAL)
		dev_warn(st->dev, "Unknown part id %x\n", part_id);

	ret = regmap_read(st->regmap, ADXL380_MISC_0_REG, &reg_val);
	if (ret)
		return ret;

	/* Bit to differentiate between ADXL380/382. */
	if (reg_val & ADXL380_XL382_MSK)
		chip_id = ADXL382_ID_VAL;
	else
		chip_id = ADXL380_ID_VAL;

	if (chip_id != st->chip_info->chip_id)
		dev_warn(st->dev, "Unknown chip id %x\n", chip_id);

	ret = regmap_write(st->regmap, ADXL380_RESET_REG, ADXL380_RESET_CODE);
	if (ret)
		return ret;

	/*
	 * A latency of approximately 0.5 ms is required after soft reset.
	 * Stated in the register REG_RESET description.
	 */
	fsleep(500);

	for (i = 0; i < indio_dev->num_channels; i++) {
		ret = regmap_update_bits(st->regmap, ADXL380_DIG_EN_REG,
					 ADXL380_CHAN_EN_MSK(i),
					 1 << (4 + i));
		if (ret)
			return ret;
	}

	ret = regmap_update_bits(st->regmap, ADXL380_FIFO_CONFIG_0_REG,
				 ADXL380_FIFO_MODE_MSK,
				 FIELD_PREP(ADXL380_FIFO_MODE_MSK, ADXL380_FIFO_STREAMED));
	if (ret)
		return ret;

	/* Select all 3 axis for act/inact detection. */
	ret = regmap_update_bits(st->regmap, ADXL380_SNSR_AXIS_EN_REG,
				 ADXL380_ACT_INACT_AXIS_EN_MSK,
				 FIELD_PREP(ADXL380_ACT_INACT_AXIS_EN_MSK,
					    ADXL380_ACT_INACT_AXIS_EN_MSK));
	if (ret)
		return ret;

	ret = adxl380_config_irq(indio_dev);
	if (ret)
		return ret;

	ret = adxl380_fill_lpf_tbl(st);
	if (ret)
		return ret;

	ret = adxl380_fill_hpf_tbl(st);
	if (ret)
		return ret;

	return adxl380_set_measure_en(st, true);
}

int adxl380_probe(struct device *dev, struct regmap *regmap,
		  const struct adxl380_chip_info *chip_info)
{
	struct iio_dev *indio_dev;
	struct adxl380_state *st;
	int ret;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
	if (!indio_dev)
		return -ENOMEM;

	st = iio_priv(indio_dev);

	st->dev = dev;
	st->regmap = regmap;
	st->chip_info = chip_info;

	mutex_init(&st->lock);

	indio_dev->channels = adxl380_channels;
	indio_dev->num_channels = ARRAY_SIZE(adxl380_channels);
	indio_dev->name = chip_info->name;
	indio_dev->info = &adxl380_info;
	indio_dev->modes = INDIO_DIRECT_MODE;

	ret = devm_regulator_get_enable(dev, "vddio");
	if (ret)
		return dev_err_probe(st->dev, ret,
				     "Failed to get vddio regulator\n");

	ret = devm_regulator_get_enable(st->dev, "vsupply");
	if (ret)
		return dev_err_probe(st->dev, ret,
				     "Failed to get vsupply regulator\n");

	ret = adxl380_setup(indio_dev);
	if (ret)
		return ret;

	ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev,
					      &adxl380_buffer_ops,
					      adxl380_fifo_attributes);
	if (ret)
		return ret;

	return devm_iio_device_register(dev, indio_dev);
}
EXPORT_SYMBOL_NS_GPL(adxl380_probe, "IIO_ADXL380");

MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>");
MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer driver");
MODULE_LICENSE("GPL");