1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2290a6ce1SLorenzo Bianconi /*
3290a6ce1SLorenzo Bianconi  * STMicroelectronics st_lsm6dsx sensor driver
4290a6ce1SLorenzo Bianconi  *
5290a6ce1SLorenzo Bianconi  * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer
6290a6ce1SLorenzo Bianconi  * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial
7290a6ce1SLorenzo Bianconi  * interface standard output.
8290a6ce1SLorenzo Bianconi  * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale
9290a6ce1SLorenzo Bianconi  * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of
10290a6ce1SLorenzo Bianconi  * +-125/+-245/+-500/+-1000/+-2000 dps
11290a6ce1SLorenzo Bianconi  * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer
12290a6ce1SLorenzo Bianconi  * allowing dynamic batching of sensor data.
1352f4b1f1SMartin Kepplinger  * LSM9DSx series is similar but includes an additional magnetometer, handled
1452f4b1f1SMartin Kepplinger  * by a different driver.
15290a6ce1SLorenzo Bianconi  *
16290a6ce1SLorenzo Bianconi  * Supported sensors:
17290a6ce1SLorenzo Bianconi  * - LSM6DS3:
18290a6ce1SLorenzo Bianconi  *   - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
19290a6ce1SLorenzo Bianconi  *   - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
20290a6ce1SLorenzo Bianconi  *   - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
21290a6ce1SLorenzo Bianconi  *   - FIFO size: 8KB
22290a6ce1SLorenzo Bianconi  *
23dbcd2088SLorenzo Bianconi  * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC/LSM6DS3TR-C:
24290a6ce1SLorenzo Bianconi  *   - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
25290a6ce1SLorenzo Bianconi  *   - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
26290a6ce1SLorenzo Bianconi  *   - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
27290a6ce1SLorenzo Bianconi  *   - FIFO size: 4KB
28290a6ce1SLorenzo Bianconi  *
29db947a79SLorenzo Bianconi  * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR/ISM330DHCX:
30801a6e0aSLorenzo Bianconi  *   - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
31801a6e0aSLorenzo Bianconi  *   - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
32801a6e0aSLorenzo Bianconi  *   - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
33801a6e0aSLorenzo Bianconi  *   - FIFO size: 3KB
34801a6e0aSLorenzo Bianconi  *
3552f4b1f1SMartin Kepplinger  * - LSM9DS1:
3652f4b1f1SMartin Kepplinger  *   - Accelerometer supported ODR [Hz]: 10, 50, 119, 238, 476, 952
3752f4b1f1SMartin Kepplinger  *   - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
3852f4b1f1SMartin Kepplinger  *   - Gyroscope supported ODR [Hz]: 15, 60, 119, 238, 476, 952
3952f4b1f1SMartin Kepplinger  *   - Gyroscope supported full-scale [dps]: +-245/+-500/+-2000
4052f4b1f1SMartin Kepplinger  *   - FIFO size: 32
4152f4b1f1SMartin Kepplinger  *
42290a6ce1SLorenzo Bianconi  * Copyright 2016 STMicroelectronics Inc.
43290a6ce1SLorenzo Bianconi  *
44290a6ce1SLorenzo Bianconi  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
45290a6ce1SLorenzo Bianconi  * Denis Ciocca <denis.ciocca@st.com>
46290a6ce1SLorenzo Bianconi  */
47290a6ce1SLorenzo Bianconi 
48290a6ce1SLorenzo Bianconi #include <linux/kernel.h>
49290a6ce1SLorenzo Bianconi #include <linux/module.h>
50290a6ce1SLorenzo Bianconi #include <linux/delay.h>
511aabad1fSSean Nyekjaer #include <linux/iio/events.h>
52290a6ce1SLorenzo Bianconi #include <linux/iio/iio.h>
53290a6ce1SLorenzo Bianconi #include <linux/iio/sysfs.h>
546ee6a368SSean Nyekjaer #include <linux/interrupt.h>
556ee6a368SSean Nyekjaer #include <linux/irq.h>
56d3f77058SLorenzo Bianconi #include <linux/pm.h>
5751a8b707SLorenzo Bianconi #include <linux/regmap.h>
5851a8b707SLorenzo Bianconi #include <linux/bitfield.h>
59290a6ce1SLorenzo Bianconi 
60dba32904SLorenzo Bianconi #include <linux/platform_data/st_sensors_pdata.h>
61dba32904SLorenzo Bianconi 
62290a6ce1SLorenzo Bianconi #include "st_lsm6dsx.h"
63290a6ce1SLorenzo Bianconi 
64290a6ce1SLorenzo Bianconi #define ST_LSM6DSX_REG_WHOAMI_ADDR		0x0f
65290a6ce1SLorenzo Bianconi 
66f48bc49bSLorenzo Bianconi static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
67b5969abfSSean Nyekjaer 	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
68b5969abfSSean Nyekjaer 	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
69b5969abfSSean Nyekjaer 	ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
70f48bc49bSLorenzo Bianconi 	IIO_CHAN_SOFT_TIMESTAMP(3),
71f48bc49bSLorenzo Bianconi };
72f48bc49bSLorenzo Bianconi 
73f48bc49bSLorenzo Bianconi static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
74f48bc49bSLorenzo Bianconi 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
75f48bc49bSLorenzo Bianconi 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
76f48bc49bSLorenzo Bianconi 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x26, IIO_MOD_Z, 2),
77f48bc49bSLorenzo Bianconi 	IIO_CHAN_SOFT_TIMESTAMP(3),
78f48bc49bSLorenzo Bianconi };
79f48bc49bSLorenzo Bianconi 
8052f4b1f1SMartin Kepplinger static const struct iio_chan_spec st_lsm6ds0_gyro_channels[] = {
8152f4b1f1SMartin Kepplinger 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x18, IIO_MOD_X, 0),
8252f4b1f1SMartin Kepplinger 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1a, IIO_MOD_Y, 1),
8352f4b1f1SMartin Kepplinger 	ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1c, IIO_MOD_Z, 2),
8452f4b1f1SMartin Kepplinger 	IIO_CHAN_SOFT_TIMESTAMP(3),
8552f4b1f1SMartin Kepplinger };
8652f4b1f1SMartin Kepplinger 
87290a6ce1SLorenzo Bianconi static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
88290a6ce1SLorenzo Bianconi 	{
8952f4b1f1SMartin Kepplinger 		.wai = 0x68,
9066b662a1SLorenzo Bianconi 		.reset = {
9166b662a1SLorenzo Bianconi 			.addr = 0x22,
9266b662a1SLorenzo Bianconi 			.mask = BIT(0),
9366b662a1SLorenzo Bianconi 		},
9466b662a1SLorenzo Bianconi 		.boot = {
9566b662a1SLorenzo Bianconi 			.addr = 0x22,
9666b662a1SLorenzo Bianconi 			.mask = BIT(7),
9766b662a1SLorenzo Bianconi 		},
9866b662a1SLorenzo Bianconi 		.bdu = {
9966b662a1SLorenzo Bianconi 			.addr = 0x22,
10066b662a1SLorenzo Bianconi 			.mask = BIT(6),
10166b662a1SLorenzo Bianconi 		},
10252f4b1f1SMartin Kepplinger 		.max_fifo_size = 32,
10352f4b1f1SMartin Kepplinger 		.id = {
10452f4b1f1SMartin Kepplinger 			{
10552f4b1f1SMartin Kepplinger 				.hw_id = ST_LSM9DS1_ID,
10652f4b1f1SMartin Kepplinger 				.name = ST_LSM9DS1_DEV_NAME,
10752f4b1f1SMartin Kepplinger 			},
10852f4b1f1SMartin Kepplinger 		},
10952f4b1f1SMartin Kepplinger 		.channels = {
11052f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_ACC] = {
11152f4b1f1SMartin Kepplinger 				.chan = st_lsm6dsx_acc_channels,
11252f4b1f1SMartin Kepplinger 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
11352f4b1f1SMartin Kepplinger 			},
11452f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_GYRO] = {
11552f4b1f1SMartin Kepplinger 				.chan = st_lsm6ds0_gyro_channels,
11652f4b1f1SMartin Kepplinger 				.len = ARRAY_SIZE(st_lsm6ds0_gyro_channels),
11752f4b1f1SMartin Kepplinger 			},
11852f4b1f1SMartin Kepplinger 		},
11952f4b1f1SMartin Kepplinger 		.odr_table = {
12052f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_ACC] = {
12152f4b1f1SMartin Kepplinger 				.reg = {
12252f4b1f1SMartin Kepplinger 					.addr = 0x20,
12352f4b1f1SMartin Kepplinger 					.mask = GENMASK(7, 5),
12452f4b1f1SMartin Kepplinger 				},
12552f4b1f1SMartin Kepplinger 				.odr_avl[0] = {  10, 0x01 },
12652f4b1f1SMartin Kepplinger 				.odr_avl[1] = {  50, 0x02 },
12752f4b1f1SMartin Kepplinger 				.odr_avl[2] = { 119, 0x03 },
12852f4b1f1SMartin Kepplinger 				.odr_avl[3] = { 238, 0x04 },
12952f4b1f1SMartin Kepplinger 				.odr_avl[4] = { 476, 0x05 },
13052f4b1f1SMartin Kepplinger 				.odr_avl[5] = { 952, 0x06 },
13152f4b1f1SMartin Kepplinger 			},
13252f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_GYRO] = {
13352f4b1f1SMartin Kepplinger 				.reg = {
13452f4b1f1SMartin Kepplinger 					.addr = 0x10,
13552f4b1f1SMartin Kepplinger 					.mask = GENMASK(7, 5),
13652f4b1f1SMartin Kepplinger 				},
13752f4b1f1SMartin Kepplinger 				.odr_avl[0] = {  15, 0x01 },
13852f4b1f1SMartin Kepplinger 				.odr_avl[1] = {  60, 0x02 },
13952f4b1f1SMartin Kepplinger 				.odr_avl[2] = { 119, 0x03 },
14052f4b1f1SMartin Kepplinger 				.odr_avl[3] = { 238, 0x04 },
14152f4b1f1SMartin Kepplinger 				.odr_avl[4] = { 476, 0x05 },
14252f4b1f1SMartin Kepplinger 				.odr_avl[5] = { 952, 0x06 },
14352f4b1f1SMartin Kepplinger 			},
14452f4b1f1SMartin Kepplinger 		},
14552f4b1f1SMartin Kepplinger 		.fs_table = {
14652f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_ACC] = {
14752f4b1f1SMartin Kepplinger 				.reg = {
14852f4b1f1SMartin Kepplinger 					.addr = 0x20,
14952f4b1f1SMartin Kepplinger 					.mask = GENMASK(4, 3),
15052f4b1f1SMartin Kepplinger 				},
1516fa02948SLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
1526fa02948SLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
1536fa02948SLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
1546fa02948SLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(732), 0x1 },
15585ae3aeeSLorenzo Bianconi 				.fs_len = 4,
15652f4b1f1SMartin Kepplinger 			},
15752f4b1f1SMartin Kepplinger 			[ST_LSM6DSX_ID_GYRO] = {
15852f4b1f1SMartin Kepplinger 				.reg = {
15952f4b1f1SMartin Kepplinger 					.addr = 0x10,
16052f4b1f1SMartin Kepplinger 					.mask = GENMASK(4, 3),
16152f4b1f1SMartin Kepplinger 				},
16252f4b1f1SMartin Kepplinger 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(245), 0x0 },
16352f4b1f1SMartin Kepplinger 				.fs_avl[1] = {  IIO_DEGREE_TO_RAD(500), 0x1 },
1640f7e1728SLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(2000), 0x3 },
16585ae3aeeSLorenzo Bianconi 				.fs_len = 3,
16652f4b1f1SMartin Kepplinger 			},
16752f4b1f1SMartin Kepplinger 		},
1687e906103SLorenzo Bianconi 		.irq_config = {
1697e906103SLorenzo Bianconi 			.irq1 = {
1707e906103SLorenzo Bianconi 				.addr = 0x0c,
1717e906103SLorenzo Bianconi 				.mask = BIT(3),
1727e906103SLorenzo Bianconi 			},
1737e906103SLorenzo Bianconi 			.irq2 = {
1747e906103SLorenzo Bianconi 				.addr = 0x0d,
1757e906103SLorenzo Bianconi 				.mask = BIT(3),
1767e906103SLorenzo Bianconi 			},
17731fe8d4eSLorenzo Bianconi 			.hla = {
17831fe8d4eSLorenzo Bianconi 				.addr = 0x22,
17931fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
18031fe8d4eSLorenzo Bianconi 			},
18131fe8d4eSLorenzo Bianconi 			.od = {
18231fe8d4eSLorenzo Bianconi 				.addr = 0x22,
18331fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
18431fe8d4eSLorenzo Bianconi 			},
1857e906103SLorenzo Bianconi 		},
18652f4b1f1SMartin Kepplinger 	},
18752f4b1f1SMartin Kepplinger 	{
188d068e4a0SLorenzo Bianconi 		.wai = 0x69,
18966b662a1SLorenzo Bianconi 		.reset = {
19066b662a1SLorenzo Bianconi 			.addr = 0x12,
19166b662a1SLorenzo Bianconi 			.mask = BIT(0),
19266b662a1SLorenzo Bianconi 		},
19366b662a1SLorenzo Bianconi 		.boot = {
19466b662a1SLorenzo Bianconi 			.addr = 0x12,
19566b662a1SLorenzo Bianconi 			.mask = BIT(7),
19666b662a1SLorenzo Bianconi 		},
19766b662a1SLorenzo Bianconi 		.bdu = {
19866b662a1SLorenzo Bianconi 			.addr = 0x12,
19966b662a1SLorenzo Bianconi 			.mask = BIT(6),
20066b662a1SLorenzo Bianconi 		},
2018f2a88a2SLorenzo Bianconi 		.max_fifo_size = 1365,
202d068e4a0SLorenzo Bianconi 		.id = {
20381956a93SLorenzo Bianconi 			{
20481956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DS3_ID,
20581956a93SLorenzo Bianconi 				.name = ST_LSM6DS3_DEV_NAME,
20681956a93SLorenzo Bianconi 			},
207d068e4a0SLorenzo Bianconi 		},
208f48bc49bSLorenzo Bianconi 		.channels = {
209f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
210f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
211f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
212f48bc49bSLorenzo Bianconi 			},
213f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
214f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
215f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
216f48bc49bSLorenzo Bianconi 			},
217f48bc49bSLorenzo Bianconi 		},
21840dd7343SLorenzo Bianconi 		.odr_table = {
21940dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
22040dd7343SLorenzo Bianconi 				.reg = {
22140dd7343SLorenzo Bianconi 					.addr = 0x10,
22240dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
22340dd7343SLorenzo Bianconi 				},
22440dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
22540dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
22640dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
22740dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
22840dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
22940dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
23040dd7343SLorenzo Bianconi 			},
23140dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
23240dd7343SLorenzo Bianconi 				.reg = {
23340dd7343SLorenzo Bianconi 					.addr = 0x11,
23440dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
23540dd7343SLorenzo Bianconi 				},
23640dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
23740dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
23840dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
23940dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
24040dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
24140dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
24240dd7343SLorenzo Bianconi 			},
24340dd7343SLorenzo Bianconi 		},
244640aca3fSLorenzo Bianconi 		.fs_table = {
245640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
246640aca3fSLorenzo Bianconi 				.reg = {
247640aca3fSLorenzo Bianconi 					.addr = 0x10,
248640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
249640aca3fSLorenzo Bianconi 				},
250640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
251640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
252640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
253640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
25485ae3aeeSLorenzo Bianconi 				.fs_len = 4,
255640aca3fSLorenzo Bianconi 			},
256640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
257640aca3fSLorenzo Bianconi 				.reg = {
258640aca3fSLorenzo Bianconi 					.addr = 0x11,
259640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
260640aca3fSLorenzo Bianconi 				},
261640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
262640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
263640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
264640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
26585ae3aeeSLorenzo Bianconi 				.fs_len = 4,
266640aca3fSLorenzo Bianconi 			},
267640aca3fSLorenzo Bianconi 		},
2687e906103SLorenzo Bianconi 		.irq_config = {
2697e906103SLorenzo Bianconi 			.irq1 = {
2707e906103SLorenzo Bianconi 				.addr = 0x0d,
2717e906103SLorenzo Bianconi 				.mask = BIT(3),
2727e906103SLorenzo Bianconi 			},
2737e906103SLorenzo Bianconi 			.irq2 = {
2747e906103SLorenzo Bianconi 				.addr = 0x0e,
2757e906103SLorenzo Bianconi 				.mask = BIT(3),
2767e906103SLorenzo Bianconi 			},
2777e906103SLorenzo Bianconi 			.lir = {
2787e906103SLorenzo Bianconi 				.addr = 0x58,
2797e906103SLorenzo Bianconi 				.mask = BIT(0),
2807e906103SLorenzo Bianconi 			},
2817e906103SLorenzo Bianconi 			.irq1_func = {
2827e906103SLorenzo Bianconi 				.addr = 0x5e,
2837e906103SLorenzo Bianconi 				.mask = BIT(5),
2847e906103SLorenzo Bianconi 			},
2857e906103SLorenzo Bianconi 			.irq2_func = {
2867e906103SLorenzo Bianconi 				.addr = 0x5f,
2877e906103SLorenzo Bianconi 				.mask = BIT(5),
2887e906103SLorenzo Bianconi 			},
28931fe8d4eSLorenzo Bianconi 			.hla = {
29031fe8d4eSLorenzo Bianconi 				.addr = 0x12,
29131fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
29231fe8d4eSLorenzo Bianconi 			},
29331fe8d4eSLorenzo Bianconi 			.od = {
29431fe8d4eSLorenzo Bianconi 				.addr = 0x12,
29531fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
29631fe8d4eSLorenzo Bianconi 			},
2977e906103SLorenzo Bianconi 		},
2987ca3ac9eSLorenzo Bianconi 		.decimator = {
2997ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
3007ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
3017ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(2, 0),
3027ca3ac9eSLorenzo Bianconi 			},
3037ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
3047ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
3057ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(5, 3),
3067ca3ac9eSLorenzo Bianconi 			},
3077ca3ac9eSLorenzo Bianconi 		},
30892617c15SLorenzo Bianconi 		.fifo_ops = {
3093b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
31050ff457dSLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_fifo,
31192617c15SLorenzo Bianconi 			.fifo_th = {
31292617c15SLorenzo Bianconi 				.addr = 0x06,
31392617c15SLorenzo Bianconi 				.mask = GENMASK(11, 0),
31492617c15SLorenzo Bianconi 			},
31592617c15SLorenzo Bianconi 			.fifo_diff = {
31692617c15SLorenzo Bianconi 				.addr = 0x3a,
31792617c15SLorenzo Bianconi 				.mask = GENMASK(11, 0),
31892617c15SLorenzo Bianconi 			},
31992617c15SLorenzo Bianconi 			.th_wl = 3, /* 1LSB = 2B */
32092617c15SLorenzo Bianconi 		},
32121345107SLorenzo Bianconi 		.ts_settings = {
32221345107SLorenzo Bianconi 			.timer_en = {
32321345107SLorenzo Bianconi 				.addr = 0x58,
32421345107SLorenzo Bianconi 				.mask = BIT(7),
32521345107SLorenzo Bianconi 			},
32621345107SLorenzo Bianconi 			.hr_timer = {
32721345107SLorenzo Bianconi 				.addr = 0x5c,
32821345107SLorenzo Bianconi 				.mask = BIT(4),
32921345107SLorenzo Bianconi 			},
33021345107SLorenzo Bianconi 			.fifo_en = {
33121345107SLorenzo Bianconi 				.addr = 0x07,
33221345107SLorenzo Bianconi 				.mask = BIT(7),
33321345107SLorenzo Bianconi 			},
33421345107SLorenzo Bianconi 			.decimator = {
33521345107SLorenzo Bianconi 				.addr = 0x09,
33621345107SLorenzo Bianconi 				.mask = GENMASK(5, 3),
33721345107SLorenzo Bianconi 			},
33821345107SLorenzo Bianconi 		},
339b5969abfSSean Nyekjaer 		.event_settings = {
340b5969abfSSean Nyekjaer 			.wakeup_reg = {
341b5969abfSSean Nyekjaer 				.addr = 0x5B,
342b5969abfSSean Nyekjaer 				.mask = GENMASK(5, 0),
343b5969abfSSean Nyekjaer 			},
3441aabad1fSSean Nyekjaer 			.wakeup_src_reg = 0x1b,
3451aabad1fSSean Nyekjaer 			.wakeup_src_status_mask = BIT(3),
3461aabad1fSSean Nyekjaer 			.wakeup_src_z_mask = BIT(0),
3471aabad1fSSean Nyekjaer 			.wakeup_src_y_mask = BIT(1),
3481aabad1fSSean Nyekjaer 			.wakeup_src_x_mask = BIT(2),
349b5969abfSSean Nyekjaer 		},
350290a6ce1SLorenzo Bianconi 	},
351290a6ce1SLorenzo Bianconi 	{
352df47710aSLorenzo Bianconi 		.wai = 0x69,
35366b662a1SLorenzo Bianconi 		.reset = {
35466b662a1SLorenzo Bianconi 			.addr = 0x12,
35566b662a1SLorenzo Bianconi 			.mask = BIT(0),
35666b662a1SLorenzo Bianconi 		},
35766b662a1SLorenzo Bianconi 		.boot = {
35866b662a1SLorenzo Bianconi 			.addr = 0x12,
35966b662a1SLorenzo Bianconi 			.mask = BIT(7),
36066b662a1SLorenzo Bianconi 		},
36166b662a1SLorenzo Bianconi 		.bdu = {
36266b662a1SLorenzo Bianconi 			.addr = 0x12,
36366b662a1SLorenzo Bianconi 			.mask = BIT(6),
36466b662a1SLorenzo Bianconi 		},
3658f2a88a2SLorenzo Bianconi 		.max_fifo_size = 682,
366df47710aSLorenzo Bianconi 		.id = {
36781956a93SLorenzo Bianconi 			{
36881956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DS3H_ID,
36981956a93SLorenzo Bianconi 				.name = ST_LSM6DS3H_DEV_NAME,
37081956a93SLorenzo Bianconi 			},
371df47710aSLorenzo Bianconi 		},
372f48bc49bSLorenzo Bianconi 		.channels = {
373f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
374f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
375f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
376f48bc49bSLorenzo Bianconi 			},
377f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
378f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
379f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
380f48bc49bSLorenzo Bianconi 			},
381f48bc49bSLorenzo Bianconi 		},
38240dd7343SLorenzo Bianconi 		.odr_table = {
38340dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
38440dd7343SLorenzo Bianconi 				.reg = {
38540dd7343SLorenzo Bianconi 					.addr = 0x10,
38640dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
38740dd7343SLorenzo Bianconi 				},
38840dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
38940dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
39040dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
39140dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
39240dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
39340dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
39440dd7343SLorenzo Bianconi 			},
39540dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
39640dd7343SLorenzo Bianconi 				.reg = {
39740dd7343SLorenzo Bianconi 					.addr = 0x11,
39840dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
39940dd7343SLorenzo Bianconi 				},
40040dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
40140dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
40240dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
40340dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
40440dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
40540dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
40640dd7343SLorenzo Bianconi 			},
40740dd7343SLorenzo Bianconi 		},
408640aca3fSLorenzo Bianconi 		.fs_table = {
409640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
410640aca3fSLorenzo Bianconi 				.reg = {
411640aca3fSLorenzo Bianconi 					.addr = 0x10,
412640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
413640aca3fSLorenzo Bianconi 				},
414640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
415640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
416640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
417640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
41885ae3aeeSLorenzo Bianconi 				.fs_len = 4,
419640aca3fSLorenzo Bianconi 			},
420640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
421640aca3fSLorenzo Bianconi 				.reg = {
422640aca3fSLorenzo Bianconi 					.addr = 0x11,
423640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
424640aca3fSLorenzo Bianconi 				},
425640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
426640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
427640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
428640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
42985ae3aeeSLorenzo Bianconi 				.fs_len = 4,
430640aca3fSLorenzo Bianconi 			},
431640aca3fSLorenzo Bianconi 		},
4327e906103SLorenzo Bianconi 		.irq_config = {
4337e906103SLorenzo Bianconi 			.irq1 = {
4347e906103SLorenzo Bianconi 				.addr = 0x0d,
4357e906103SLorenzo Bianconi 				.mask = BIT(3),
4367e906103SLorenzo Bianconi 			},
4377e906103SLorenzo Bianconi 			.irq2 = {
4387e906103SLorenzo Bianconi 				.addr = 0x0e,
4397e906103SLorenzo Bianconi 				.mask = BIT(3),
4407e906103SLorenzo Bianconi 			},
4417e906103SLorenzo Bianconi 			.lir = {
4427e906103SLorenzo Bianconi 				.addr = 0x58,
4437e906103SLorenzo Bianconi 				.mask = BIT(0),
4447e906103SLorenzo Bianconi 			},
4457e906103SLorenzo Bianconi 			.irq1_func = {
4467e906103SLorenzo Bianconi 				.addr = 0x5e,
4477e906103SLorenzo Bianconi 				.mask = BIT(5),
4487e906103SLorenzo Bianconi 			},
4497e906103SLorenzo Bianconi 			.irq2_func = {
4507e906103SLorenzo Bianconi 				.addr = 0x5f,
4517e906103SLorenzo Bianconi 				.mask = BIT(5),
4527e906103SLorenzo Bianconi 			},
45331fe8d4eSLorenzo Bianconi 			.hla = {
45431fe8d4eSLorenzo Bianconi 				.addr = 0x12,
45531fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
45631fe8d4eSLorenzo Bianconi 			},
45731fe8d4eSLorenzo Bianconi 			.od = {
45831fe8d4eSLorenzo Bianconi 				.addr = 0x12,
45931fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
46031fe8d4eSLorenzo Bianconi 			},
4617e906103SLorenzo Bianconi 		},
4627ca3ac9eSLorenzo Bianconi 		.decimator = {
4637ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
4647ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
4657ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(2, 0),
4667ca3ac9eSLorenzo Bianconi 			},
4677ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
4687ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
4697ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(5, 3),
4707ca3ac9eSLorenzo Bianconi 			},
4717ca3ac9eSLorenzo Bianconi 		},
47292617c15SLorenzo Bianconi 		.fifo_ops = {
4733b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
47450ff457dSLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_fifo,
47592617c15SLorenzo Bianconi 			.fifo_th = {
47692617c15SLorenzo Bianconi 				.addr = 0x06,
47792617c15SLorenzo Bianconi 				.mask = GENMASK(11, 0),
47892617c15SLorenzo Bianconi 			},
47992617c15SLorenzo Bianconi 			.fifo_diff = {
48092617c15SLorenzo Bianconi 				.addr = 0x3a,
48192617c15SLorenzo Bianconi 				.mask = GENMASK(11, 0),
48292617c15SLorenzo Bianconi 			},
48392617c15SLorenzo Bianconi 			.th_wl = 3, /* 1LSB = 2B */
48492617c15SLorenzo Bianconi 		},
48521345107SLorenzo Bianconi 		.ts_settings = {
48621345107SLorenzo Bianconi 			.timer_en = {
48721345107SLorenzo Bianconi 				.addr = 0x58,
48821345107SLorenzo Bianconi 				.mask = BIT(7),
48921345107SLorenzo Bianconi 			},
49021345107SLorenzo Bianconi 			.hr_timer = {
49121345107SLorenzo Bianconi 				.addr = 0x5c,
49221345107SLorenzo Bianconi 				.mask = BIT(4),
49321345107SLorenzo Bianconi 			},
49421345107SLorenzo Bianconi 			.fifo_en = {
49521345107SLorenzo Bianconi 				.addr = 0x07,
49621345107SLorenzo Bianconi 				.mask = BIT(7),
49721345107SLorenzo Bianconi 			},
49821345107SLorenzo Bianconi 			.decimator = {
49921345107SLorenzo Bianconi 				.addr = 0x09,
50021345107SLorenzo Bianconi 				.mask = GENMASK(5, 3),
50121345107SLorenzo Bianconi 			},
50221345107SLorenzo Bianconi 		},
503b5969abfSSean Nyekjaer 		.event_settings = {
504b5969abfSSean Nyekjaer 			.wakeup_reg = {
505b5969abfSSean Nyekjaer 				.addr = 0x5B,
506b5969abfSSean Nyekjaer 				.mask = GENMASK(5, 0),
507b5969abfSSean Nyekjaer 			},
5081aabad1fSSean Nyekjaer 			.wakeup_src_reg = 0x1b,
5091aabad1fSSean Nyekjaer 			.wakeup_src_status_mask = BIT(3),
5101aabad1fSSean Nyekjaer 			.wakeup_src_z_mask = BIT(0),
5111aabad1fSSean Nyekjaer 			.wakeup_src_y_mask = BIT(1),
5121aabad1fSSean Nyekjaer 			.wakeup_src_x_mask = BIT(2),
513b5969abfSSean Nyekjaer 		},
514df47710aSLorenzo Bianconi 	},
515df47710aSLorenzo Bianconi 	{
516d068e4a0SLorenzo Bianconi 		.wai = 0x6a,
51766b662a1SLorenzo Bianconi 		.reset = {
51866b662a1SLorenzo Bianconi 			.addr = 0x12,
51966b662a1SLorenzo Bianconi 			.mask = BIT(0),
52066b662a1SLorenzo Bianconi 		},
52166b662a1SLorenzo Bianconi 		.boot = {
52266b662a1SLorenzo Bianconi 			.addr = 0x12,
52366b662a1SLorenzo Bianconi 			.mask = BIT(7),
52466b662a1SLorenzo Bianconi 		},
52566b662a1SLorenzo Bianconi 		.bdu = {
52666b662a1SLorenzo Bianconi 			.addr = 0x12,
52766b662a1SLorenzo Bianconi 			.mask = BIT(6),
52866b662a1SLorenzo Bianconi 		},
5298f2a88a2SLorenzo Bianconi 		.max_fifo_size = 682,
530d068e4a0SLorenzo Bianconi 		.id = {
53181956a93SLorenzo Bianconi 			{
53281956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DSL_ID,
53381956a93SLorenzo Bianconi 				.name = ST_LSM6DSL_DEV_NAME,
53481956a93SLorenzo Bianconi 			}, {
53581956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DSM_ID,
53681956a93SLorenzo Bianconi 				.name = ST_LSM6DSM_DEV_NAME,
53781956a93SLorenzo Bianconi 			}, {
53881956a93SLorenzo Bianconi 				.hw_id = ST_ISM330DLC_ID,
53981956a93SLorenzo Bianconi 				.name = ST_ISM330DLC_DEV_NAME,
540dbcd2088SLorenzo Bianconi 			}, {
541dbcd2088SLorenzo Bianconi 				.hw_id = ST_LSM6DS3TRC_ID,
542dbcd2088SLorenzo Bianconi 				.name = ST_LSM6DS3TRC_DEV_NAME,
54381956a93SLorenzo Bianconi 			},
544d068e4a0SLorenzo Bianconi 		},
545f48bc49bSLorenzo Bianconi 		.channels = {
546f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
547f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
548f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
549f48bc49bSLorenzo Bianconi 			},
550f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
551f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
552f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
553f48bc49bSLorenzo Bianconi 			},
554f48bc49bSLorenzo Bianconi 		},
55540dd7343SLorenzo Bianconi 		.odr_table = {
55640dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
55740dd7343SLorenzo Bianconi 				.reg = {
55840dd7343SLorenzo Bianconi 					.addr = 0x10,
55940dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
56040dd7343SLorenzo Bianconi 				},
56140dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
56240dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
56340dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
56440dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
56540dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
56640dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
56740dd7343SLorenzo Bianconi 			},
56840dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
56940dd7343SLorenzo Bianconi 				.reg = {
57040dd7343SLorenzo Bianconi 					.addr = 0x11,
57140dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
57240dd7343SLorenzo Bianconi 				},
57340dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
57440dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
57540dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
57640dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
57740dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
57840dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
57940dd7343SLorenzo Bianconi 			},
58040dd7343SLorenzo Bianconi 		},
581640aca3fSLorenzo Bianconi 		.fs_table = {
582640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
583640aca3fSLorenzo Bianconi 				.reg = {
584640aca3fSLorenzo Bianconi 					.addr = 0x10,
585640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
586640aca3fSLorenzo Bianconi 				},
587640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
588640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
589640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
590640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
59185ae3aeeSLorenzo Bianconi 				.fs_len = 4,
592640aca3fSLorenzo Bianconi 			},
593640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
594640aca3fSLorenzo Bianconi 				.reg = {
595640aca3fSLorenzo Bianconi 					.addr = 0x11,
596640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
597640aca3fSLorenzo Bianconi 				},
598640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
599640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
600640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
601640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
60285ae3aeeSLorenzo Bianconi 				.fs_len = 4,
603640aca3fSLorenzo Bianconi 			},
604640aca3fSLorenzo Bianconi 		},
6057e906103SLorenzo Bianconi 		.irq_config = {
6067e906103SLorenzo Bianconi 			.irq1 = {
6077e906103SLorenzo Bianconi 				.addr = 0x0d,
6087e906103SLorenzo Bianconi 				.mask = BIT(3),
6097e906103SLorenzo Bianconi 			},
6107e906103SLorenzo Bianconi 			.irq2 = {
6117e906103SLorenzo Bianconi 				.addr = 0x0e,
6127e906103SLorenzo Bianconi 				.mask = BIT(3),
6137e906103SLorenzo Bianconi 			},
6147e906103SLorenzo Bianconi 			.lir = {
6157e906103SLorenzo Bianconi 				.addr = 0x58,
6167e906103SLorenzo Bianconi 				.mask = BIT(0),
6177e906103SLorenzo Bianconi 			},
6187e906103SLorenzo Bianconi 			.irq1_func = {
6197e906103SLorenzo Bianconi 				.addr = 0x5e,
6207e906103SLorenzo Bianconi 				.mask = BIT(5),
6217e906103SLorenzo Bianconi 			},
6227e906103SLorenzo Bianconi 			.irq2_func = {
6237e906103SLorenzo Bianconi 				.addr = 0x5f,
6247e906103SLorenzo Bianconi 				.mask = BIT(5),
6257e906103SLorenzo Bianconi 			},
62631fe8d4eSLorenzo Bianconi 			.hla = {
62731fe8d4eSLorenzo Bianconi 				.addr = 0x12,
62831fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
62931fe8d4eSLorenzo Bianconi 			},
63031fe8d4eSLorenzo Bianconi 			.od = {
63131fe8d4eSLorenzo Bianconi 				.addr = 0x12,
63231fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
63331fe8d4eSLorenzo Bianconi 			},
6347e906103SLorenzo Bianconi 		},
6357ca3ac9eSLorenzo Bianconi 		.decimator = {
6367ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
6377ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
6387ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(2, 0),
6397ca3ac9eSLorenzo Bianconi 			},
6407ca3ac9eSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
6417ca3ac9eSLorenzo Bianconi 				.addr = 0x08,
6427ca3ac9eSLorenzo Bianconi 				.mask = GENMASK(5, 3),
6437ca3ac9eSLorenzo Bianconi 			},
6447ca3ac9eSLorenzo Bianconi 		},
64592617c15SLorenzo Bianconi 		.fifo_ops = {
6463b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
64750ff457dSLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_fifo,
64892617c15SLorenzo Bianconi 			.fifo_th = {
64992617c15SLorenzo Bianconi 				.addr = 0x06,
650be75eb86SLorenzo Bianconi 				.mask = GENMASK(10, 0),
65192617c15SLorenzo Bianconi 			},
65292617c15SLorenzo Bianconi 			.fifo_diff = {
65392617c15SLorenzo Bianconi 				.addr = 0x3a,
654be75eb86SLorenzo Bianconi 				.mask = GENMASK(10, 0),
65592617c15SLorenzo Bianconi 			},
65692617c15SLorenzo Bianconi 			.th_wl = 3, /* 1LSB = 2B */
65792617c15SLorenzo Bianconi 		},
65821345107SLorenzo Bianconi 		.ts_settings = {
65921345107SLorenzo Bianconi 			.timer_en = {
66021345107SLorenzo Bianconi 				.addr = 0x19,
66121345107SLorenzo Bianconi 				.mask = BIT(5),
66221345107SLorenzo Bianconi 			},
66321345107SLorenzo Bianconi 			.hr_timer = {
66421345107SLorenzo Bianconi 				.addr = 0x5c,
66521345107SLorenzo Bianconi 				.mask = BIT(4),
66621345107SLorenzo Bianconi 			},
66721345107SLorenzo Bianconi 			.fifo_en = {
66821345107SLorenzo Bianconi 				.addr = 0x07,
66921345107SLorenzo Bianconi 				.mask = BIT(7),
67021345107SLorenzo Bianconi 			},
67121345107SLorenzo Bianconi 			.decimator = {
67221345107SLorenzo Bianconi 				.addr = 0x09,
67321345107SLorenzo Bianconi 				.mask = GENMASK(5, 3),
67421345107SLorenzo Bianconi 			},
67521345107SLorenzo Bianconi 		},
676b5969abfSSean Nyekjaer 		.event_settings = {
677b5969abfSSean Nyekjaer 			.enable_reg = {
678b5969abfSSean Nyekjaer 				.addr = 0x58,
679b5969abfSSean Nyekjaer 				.mask = BIT(7),
680b5969abfSSean Nyekjaer 			},
681b5969abfSSean Nyekjaer 			.wakeup_reg = {
682b5969abfSSean Nyekjaer 				.addr = 0x5B,
683b5969abfSSean Nyekjaer 				.mask = GENMASK(5, 0),
684b5969abfSSean Nyekjaer 			},
6851aabad1fSSean Nyekjaer 			.wakeup_src_reg = 0x1b,
6861aabad1fSSean Nyekjaer 			.wakeup_src_status_mask = BIT(3),
6871aabad1fSSean Nyekjaer 			.wakeup_src_z_mask = BIT(0),
6881aabad1fSSean Nyekjaer 			.wakeup_src_y_mask = BIT(1),
6891aabad1fSSean Nyekjaer 			.wakeup_src_x_mask = BIT(2),
690b5969abfSSean Nyekjaer 		},
691290a6ce1SLorenzo Bianconi 	},
692801a6e0aSLorenzo Bianconi 	{
693801a6e0aSLorenzo Bianconi 		.wai = 0x6c,
69466b662a1SLorenzo Bianconi 		.reset = {
69566b662a1SLorenzo Bianconi 			.addr = 0x12,
69666b662a1SLorenzo Bianconi 			.mask = BIT(0),
69766b662a1SLorenzo Bianconi 		},
69866b662a1SLorenzo Bianconi 		.boot = {
69966b662a1SLorenzo Bianconi 			.addr = 0x12,
70066b662a1SLorenzo Bianconi 			.mask = BIT(7),
70166b662a1SLorenzo Bianconi 		},
70266b662a1SLorenzo Bianconi 		.bdu = {
70366b662a1SLorenzo Bianconi 			.addr = 0x12,
70466b662a1SLorenzo Bianconi 			.mask = BIT(6),
70566b662a1SLorenzo Bianconi 		},
706801a6e0aSLorenzo Bianconi 		.max_fifo_size = 512,
707801a6e0aSLorenzo Bianconi 		.id = {
70881956a93SLorenzo Bianconi 			{
70981956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DSO_ID,
71081956a93SLorenzo Bianconi 				.name = ST_LSM6DSO_DEV_NAME,
71181956a93SLorenzo Bianconi 			}, {
71281956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DSOX_ID,
71381956a93SLorenzo Bianconi 				.name = ST_LSM6DSOX_DEV_NAME,
71481956a93SLorenzo Bianconi 			},
715801a6e0aSLorenzo Bianconi 		},
716f48bc49bSLorenzo Bianconi 		.channels = {
717f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
718f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
719f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
720f48bc49bSLorenzo Bianconi 			},
721f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
722f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
723f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
724f48bc49bSLorenzo Bianconi 			},
725f48bc49bSLorenzo Bianconi 		},
726960506edSLorenzo Bianconi 		.drdy_mask = {
727960506edSLorenzo Bianconi 			.addr = 0x13,
728960506edSLorenzo Bianconi 			.mask = BIT(3),
729960506edSLorenzo Bianconi 		},
73040dd7343SLorenzo Bianconi 		.odr_table = {
73140dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
73240dd7343SLorenzo Bianconi 				.reg = {
73340dd7343SLorenzo Bianconi 					.addr = 0x10,
73440dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
73540dd7343SLorenzo Bianconi 				},
73640dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
73740dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
73840dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
73940dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
74040dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
74140dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
74240dd7343SLorenzo Bianconi 			},
74340dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
74440dd7343SLorenzo Bianconi 				.reg = {
74540dd7343SLorenzo Bianconi 					.addr = 0x11,
74640dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
74740dd7343SLorenzo Bianconi 				},
74840dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
74940dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
75040dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
75140dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
75240dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
75340dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
75440dd7343SLorenzo Bianconi 			},
75540dd7343SLorenzo Bianconi 		},
756640aca3fSLorenzo Bianconi 		.fs_table = {
757640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
758640aca3fSLorenzo Bianconi 				.reg = {
759640aca3fSLorenzo Bianconi 					.addr = 0x10,
760640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
761640aca3fSLorenzo Bianconi 				},
762640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
763640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
764640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
765640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
76685ae3aeeSLorenzo Bianconi 				.fs_len = 4,
767640aca3fSLorenzo Bianconi 			},
768640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
769640aca3fSLorenzo Bianconi 				.reg = {
770640aca3fSLorenzo Bianconi 					.addr = 0x11,
771640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
772640aca3fSLorenzo Bianconi 				},
773640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
774640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
775640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
776640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
77785ae3aeeSLorenzo Bianconi 				.fs_len = 4,
778640aca3fSLorenzo Bianconi 			},
779640aca3fSLorenzo Bianconi 		},
7807e906103SLorenzo Bianconi 		.irq_config = {
7817e906103SLorenzo Bianconi 			.irq1 = {
7827e906103SLorenzo Bianconi 				.addr = 0x0d,
7837e906103SLorenzo Bianconi 				.mask = BIT(3),
7847e906103SLorenzo Bianconi 			},
7857e906103SLorenzo Bianconi 			.irq2 = {
7867e906103SLorenzo Bianconi 				.addr = 0x0e,
7877e906103SLorenzo Bianconi 				.mask = BIT(3),
7887e906103SLorenzo Bianconi 			},
7897e906103SLorenzo Bianconi 			.lir = {
7907e906103SLorenzo Bianconi 				.addr = 0x56,
7917e906103SLorenzo Bianconi 				.mask = BIT(0),
7927e906103SLorenzo Bianconi 			},
7937e906103SLorenzo Bianconi 			.clear_on_read = {
7947e906103SLorenzo Bianconi 				.addr = 0x56,
7957e906103SLorenzo Bianconi 				.mask = BIT(6),
7967e906103SLorenzo Bianconi 			},
7973ea39d61SLorenzo Bianconi 			.irq1_func = {
7983ea39d61SLorenzo Bianconi 				.addr = 0x5e,
7993ea39d61SLorenzo Bianconi 				.mask = BIT(5),
8003ea39d61SLorenzo Bianconi 			},
8013ea39d61SLorenzo Bianconi 			.irq2_func = {
8023ea39d61SLorenzo Bianconi 				.addr = 0x5f,
8033ea39d61SLorenzo Bianconi 				.mask = BIT(5),
8043ea39d61SLorenzo Bianconi 			},
80531fe8d4eSLorenzo Bianconi 			.hla = {
80631fe8d4eSLorenzo Bianconi 				.addr = 0x12,
80731fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
80831fe8d4eSLorenzo Bianconi 			},
80931fe8d4eSLorenzo Bianconi 			.od = {
81031fe8d4eSLorenzo Bianconi 				.addr = 0x12,
81131fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
81231fe8d4eSLorenzo Bianconi 			},
8137e906103SLorenzo Bianconi 		},
814801a6e0aSLorenzo Bianconi 		.batch = {
815801a6e0aSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
816801a6e0aSLorenzo Bianconi 				.addr = 0x09,
817801a6e0aSLorenzo Bianconi 				.mask = GENMASK(3, 0),
818801a6e0aSLorenzo Bianconi 			},
819801a6e0aSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
820801a6e0aSLorenzo Bianconi 				.addr = 0x09,
821801a6e0aSLorenzo Bianconi 				.mask = GENMASK(7, 4),
822801a6e0aSLorenzo Bianconi 			},
823801a6e0aSLorenzo Bianconi 		},
824801a6e0aSLorenzo Bianconi 		.fifo_ops = {
8253b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
826801a6e0aSLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_tagged_fifo,
827801a6e0aSLorenzo Bianconi 			.fifo_th = {
828801a6e0aSLorenzo Bianconi 				.addr = 0x07,
829801a6e0aSLorenzo Bianconi 				.mask = GENMASK(8, 0),
830801a6e0aSLorenzo Bianconi 			},
831801a6e0aSLorenzo Bianconi 			.fifo_diff = {
832801a6e0aSLorenzo Bianconi 				.addr = 0x3a,
83370575abeSmario tesi 				.mask = GENMASK(9, 0),
834801a6e0aSLorenzo Bianconi 			},
835801a6e0aSLorenzo Bianconi 			.th_wl = 1,
836801a6e0aSLorenzo Bianconi 		},
837801a6e0aSLorenzo Bianconi 		.ts_settings = {
838801a6e0aSLorenzo Bianconi 			.timer_en = {
839801a6e0aSLorenzo Bianconi 				.addr = 0x19,
840801a6e0aSLorenzo Bianconi 				.mask = BIT(5),
841801a6e0aSLorenzo Bianconi 			},
842801a6e0aSLorenzo Bianconi 			.decimator = {
843801a6e0aSLorenzo Bianconi 				.addr = 0x0a,
844801a6e0aSLorenzo Bianconi 				.mask = GENMASK(7, 6),
845801a6e0aSLorenzo Bianconi 			},
846801a6e0aSLorenzo Bianconi 		},
847c91c1c84SLorenzo Bianconi 		.shub_settings = {
848c91c1c84SLorenzo Bianconi 			.page_mux = {
849c91c1c84SLorenzo Bianconi 				.addr = 0x01,
850c91c1c84SLorenzo Bianconi 				.mask = BIT(6),
851c91c1c84SLorenzo Bianconi 			},
852c91c1c84SLorenzo Bianconi 			.master_en = {
853c91c1c84SLorenzo Bianconi 				.addr = 0x14,
854c91c1c84SLorenzo Bianconi 				.mask = BIT(2),
855c91c1c84SLorenzo Bianconi 			},
856c91c1c84SLorenzo Bianconi 			.pullup_en = {
857c91c1c84SLorenzo Bianconi 				.addr = 0x14,
858c91c1c84SLorenzo Bianconi 				.mask = BIT(3),
859c91c1c84SLorenzo Bianconi 			},
860c91c1c84SLorenzo Bianconi 			.aux_sens = {
861c91c1c84SLorenzo Bianconi 				.addr = 0x14,
862c91c1c84SLorenzo Bianconi 				.mask = GENMASK(1, 0),
863c91c1c84SLorenzo Bianconi 			},
8646d0205fdSLorenzo Bianconi 			.wr_once = {
8656d0205fdSLorenzo Bianconi 				.addr = 0x14,
8666d0205fdSLorenzo Bianconi 				.mask = BIT(6),
8676d0205fdSLorenzo Bianconi 			},
868c91c1c84SLorenzo Bianconi 			.shub_out = 0x02,
869c91c1c84SLorenzo Bianconi 			.slv0_addr = 0x15,
870c91c1c84SLorenzo Bianconi 			.dw_slv0_addr = 0x21,
8716d0205fdSLorenzo Bianconi 			.batch_en = BIT(3),
8723ea39d61SLorenzo Bianconi 		},
8733ea39d61SLorenzo Bianconi 		.event_settings = {
8743ea39d61SLorenzo Bianconi 			.enable_reg = {
8753ea39d61SLorenzo Bianconi 				.addr = 0x58,
8763ea39d61SLorenzo Bianconi 				.mask = BIT(7),
8773ea39d61SLorenzo Bianconi 			},
8783ea39d61SLorenzo Bianconi 			.wakeup_reg = {
8793ea39d61SLorenzo Bianconi 				.addr = 0x5b,
8803ea39d61SLorenzo Bianconi 				.mask = GENMASK(5, 0),
8813ea39d61SLorenzo Bianconi 			},
8823ea39d61SLorenzo Bianconi 			.wakeup_src_reg = 0x1b,
8833ea39d61SLorenzo Bianconi 			.wakeup_src_status_mask = BIT(3),
8843ea39d61SLorenzo Bianconi 			.wakeup_src_z_mask = BIT(0),
8853ea39d61SLorenzo Bianconi 			.wakeup_src_y_mask = BIT(1),
8863ea39d61SLorenzo Bianconi 			.wakeup_src_x_mask = BIT(2),
8873ea39d61SLorenzo Bianconi 		},
888801a6e0aSLorenzo Bianconi 	},
8893054c4ffSLorenzo Bianconi 	{
8903054c4ffSLorenzo Bianconi 		.wai = 0x6b,
89166b662a1SLorenzo Bianconi 		.reset = {
89266b662a1SLorenzo Bianconi 			.addr = 0x12,
89366b662a1SLorenzo Bianconi 			.mask = BIT(0),
89466b662a1SLorenzo Bianconi 		},
89566b662a1SLorenzo Bianconi 		.boot = {
89666b662a1SLorenzo Bianconi 			.addr = 0x12,
89766b662a1SLorenzo Bianconi 			.mask = BIT(7),
89866b662a1SLorenzo Bianconi 		},
89966b662a1SLorenzo Bianconi 		.bdu = {
90066b662a1SLorenzo Bianconi 			.addr = 0x12,
90166b662a1SLorenzo Bianconi 			.mask = BIT(6),
90266b662a1SLorenzo Bianconi 		},
9033054c4ffSLorenzo Bianconi 		.max_fifo_size = 512,
9043054c4ffSLorenzo Bianconi 		.id = {
90581956a93SLorenzo Bianconi 			{
90681956a93SLorenzo Bianconi 				.hw_id = ST_ASM330LHH_ID,
90781956a93SLorenzo Bianconi 				.name = ST_ASM330LHH_DEV_NAME,
90881956a93SLorenzo Bianconi 			},
9093054c4ffSLorenzo Bianconi 		},
910f48bc49bSLorenzo Bianconi 		.channels = {
911f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
912f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
913f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
914f48bc49bSLorenzo Bianconi 			},
915f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
916f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
917f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
918f48bc49bSLorenzo Bianconi 			},
919f48bc49bSLorenzo Bianconi 		},
920960506edSLorenzo Bianconi 		.drdy_mask = {
921960506edSLorenzo Bianconi 			.addr = 0x13,
922960506edSLorenzo Bianconi 			.mask = BIT(3),
923960506edSLorenzo Bianconi 		},
92440dd7343SLorenzo Bianconi 		.odr_table = {
92540dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
92640dd7343SLorenzo Bianconi 				.reg = {
92740dd7343SLorenzo Bianconi 					.addr = 0x10,
92840dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
92940dd7343SLorenzo Bianconi 				},
93040dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
93140dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
93240dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
93340dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
93440dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
93540dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
93640dd7343SLorenzo Bianconi 			},
93740dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
93840dd7343SLorenzo Bianconi 				.reg = {
93940dd7343SLorenzo Bianconi 					.addr = 0x11,
94040dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
94140dd7343SLorenzo Bianconi 				},
94240dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
94340dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
94440dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
94540dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
94640dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
94740dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
94840dd7343SLorenzo Bianconi 			},
94940dd7343SLorenzo Bianconi 		},
950640aca3fSLorenzo Bianconi 		.fs_table = {
951640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
952640aca3fSLorenzo Bianconi 				.reg = {
953640aca3fSLorenzo Bianconi 					.addr = 0x10,
954640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
955640aca3fSLorenzo Bianconi 				},
956640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
957640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
958640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
959640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
96085ae3aeeSLorenzo Bianconi 				.fs_len = 4,
961640aca3fSLorenzo Bianconi 			},
962640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
963640aca3fSLorenzo Bianconi 				.reg = {
964640aca3fSLorenzo Bianconi 					.addr = 0x11,
965640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
966640aca3fSLorenzo Bianconi 				},
967640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
968640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
969640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
970640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
97185ae3aeeSLorenzo Bianconi 				.fs_len = 4,
972640aca3fSLorenzo Bianconi 			},
973640aca3fSLorenzo Bianconi 		},
9747e906103SLorenzo Bianconi 		.irq_config = {
9757e906103SLorenzo Bianconi 			.irq1 = {
9767e906103SLorenzo Bianconi 				.addr = 0x0d,
9777e906103SLorenzo Bianconi 				.mask = BIT(3),
9787e906103SLorenzo Bianconi 			},
9797e906103SLorenzo Bianconi 			.irq2 = {
9807e906103SLorenzo Bianconi 				.addr = 0x0e,
9817e906103SLorenzo Bianconi 				.mask = BIT(3),
9827e906103SLorenzo Bianconi 			},
9837e906103SLorenzo Bianconi 			.lir = {
9847e906103SLorenzo Bianconi 				.addr = 0x56,
9857e906103SLorenzo Bianconi 				.mask = BIT(0),
9867e906103SLorenzo Bianconi 			},
9877e906103SLorenzo Bianconi 			.clear_on_read = {
9887e906103SLorenzo Bianconi 				.addr = 0x56,
9897e906103SLorenzo Bianconi 				.mask = BIT(6),
9907e906103SLorenzo Bianconi 			},
9917e906103SLorenzo Bianconi 			.irq1_func = {
9927e906103SLorenzo Bianconi 				.addr = 0x5e,
9937e906103SLorenzo Bianconi 				.mask = BIT(5),
9947e906103SLorenzo Bianconi 			},
9957e906103SLorenzo Bianconi 			.irq2_func = {
9967e906103SLorenzo Bianconi 				.addr = 0x5f,
9977e906103SLorenzo Bianconi 				.mask = BIT(5),
9987e906103SLorenzo Bianconi 			},
99931fe8d4eSLorenzo Bianconi 			.hla = {
100031fe8d4eSLorenzo Bianconi 				.addr = 0x12,
100131fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
100231fe8d4eSLorenzo Bianconi 			},
100331fe8d4eSLorenzo Bianconi 			.od = {
100431fe8d4eSLorenzo Bianconi 				.addr = 0x12,
100531fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
100631fe8d4eSLorenzo Bianconi 			},
10077e906103SLorenzo Bianconi 		},
10083054c4ffSLorenzo Bianconi 		.batch = {
10093054c4ffSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
10103054c4ffSLorenzo Bianconi 				.addr = 0x09,
10113054c4ffSLorenzo Bianconi 				.mask = GENMASK(3, 0),
10123054c4ffSLorenzo Bianconi 			},
10133054c4ffSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
10143054c4ffSLorenzo Bianconi 				.addr = 0x09,
10153054c4ffSLorenzo Bianconi 				.mask = GENMASK(7, 4),
10163054c4ffSLorenzo Bianconi 			},
10173054c4ffSLorenzo Bianconi 		},
10183054c4ffSLorenzo Bianconi 		.fifo_ops = {
10193b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
10203054c4ffSLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_tagged_fifo,
10213054c4ffSLorenzo Bianconi 			.fifo_th = {
10223054c4ffSLorenzo Bianconi 				.addr = 0x07,
10233054c4ffSLorenzo Bianconi 				.mask = GENMASK(8, 0),
10243054c4ffSLorenzo Bianconi 			},
10253054c4ffSLorenzo Bianconi 			.fifo_diff = {
10263054c4ffSLorenzo Bianconi 				.addr = 0x3a,
102770575abeSmario tesi 				.mask = GENMASK(9, 0),
10283054c4ffSLorenzo Bianconi 			},
10293054c4ffSLorenzo Bianconi 			.th_wl = 1,
10303054c4ffSLorenzo Bianconi 		},
10313054c4ffSLorenzo Bianconi 		.ts_settings = {
10323054c4ffSLorenzo Bianconi 			.timer_en = {
10333054c4ffSLorenzo Bianconi 				.addr = 0x19,
10343054c4ffSLorenzo Bianconi 				.mask = BIT(5),
10353054c4ffSLorenzo Bianconi 			},
10363054c4ffSLorenzo Bianconi 			.decimator = {
10373054c4ffSLorenzo Bianconi 				.addr = 0x0a,
10383054c4ffSLorenzo Bianconi 				.mask = GENMASK(7, 6),
10393054c4ffSLorenzo Bianconi 			},
10403054c4ffSLorenzo Bianconi 		},
1041b5969abfSSean Nyekjaer 		.event_settings = {
1042b5969abfSSean Nyekjaer 			.enable_reg = {
1043b5969abfSSean Nyekjaer 				.addr = 0x58,
1044b5969abfSSean Nyekjaer 				.mask = BIT(7),
1045b5969abfSSean Nyekjaer 			},
1046b5969abfSSean Nyekjaer 			.wakeup_reg = {
1047b5969abfSSean Nyekjaer 				.addr = 0x5B,
1048b5969abfSSean Nyekjaer 				.mask = GENMASK(5, 0),
1049b5969abfSSean Nyekjaer 			},
10501aabad1fSSean Nyekjaer 			.wakeup_src_reg = 0x1b,
10511aabad1fSSean Nyekjaer 			.wakeup_src_status_mask = BIT(3),
10521aabad1fSSean Nyekjaer 			.wakeup_src_z_mask = BIT(0),
10531aabad1fSSean Nyekjaer 			.wakeup_src_y_mask = BIT(1),
10541aabad1fSSean Nyekjaer 			.wakeup_src_x_mask = BIT(2),
1055b5969abfSSean Nyekjaer 		},
10563054c4ffSLorenzo Bianconi 	},
105743901008SLorenzo Bianconi 	{
105843901008SLorenzo Bianconi 		.wai = 0x6b,
105966b662a1SLorenzo Bianconi 		.reset = {
106066b662a1SLorenzo Bianconi 			.addr = 0x12,
106166b662a1SLorenzo Bianconi 			.mask = BIT(0),
106266b662a1SLorenzo Bianconi 		},
106366b662a1SLorenzo Bianconi 		.boot = {
106466b662a1SLorenzo Bianconi 			.addr = 0x12,
106566b662a1SLorenzo Bianconi 			.mask = BIT(7),
106666b662a1SLorenzo Bianconi 		},
106766b662a1SLorenzo Bianconi 		.bdu = {
106866b662a1SLorenzo Bianconi 			.addr = 0x12,
106966b662a1SLorenzo Bianconi 			.mask = BIT(6),
107066b662a1SLorenzo Bianconi 		},
107143901008SLorenzo Bianconi 		.max_fifo_size = 512,
107243901008SLorenzo Bianconi 		.id = {
107381956a93SLorenzo Bianconi 			{
107481956a93SLorenzo Bianconi 				.hw_id = ST_LSM6DSR_ID,
107581956a93SLorenzo Bianconi 				.name = ST_LSM6DSR_DEV_NAME,
1076db947a79SLorenzo Bianconi 			}, {
1077db947a79SLorenzo Bianconi 				.hw_id = ST_ISM330DHCX_ID,
1078db947a79SLorenzo Bianconi 				.name = ST_ISM330DHCX_DEV_NAME,
107981956a93SLorenzo Bianconi 			},
108043901008SLorenzo Bianconi 		},
1081f48bc49bSLorenzo Bianconi 		.channels = {
1082f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
1083f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_acc_channels,
1084f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
1085f48bc49bSLorenzo Bianconi 			},
1086f48bc49bSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
1087f48bc49bSLorenzo Bianconi 				.chan = st_lsm6dsx_gyro_channels,
1088f48bc49bSLorenzo Bianconi 				.len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
1089f48bc49bSLorenzo Bianconi 			},
1090f48bc49bSLorenzo Bianconi 		},
1091960506edSLorenzo Bianconi 		.drdy_mask = {
1092960506edSLorenzo Bianconi 			.addr = 0x13,
1093960506edSLorenzo Bianconi 			.mask = BIT(3),
1094960506edSLorenzo Bianconi 		},
109540dd7343SLorenzo Bianconi 		.odr_table = {
109640dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
109740dd7343SLorenzo Bianconi 				.reg = {
109840dd7343SLorenzo Bianconi 					.addr = 0x10,
109940dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
110040dd7343SLorenzo Bianconi 				},
110140dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
110240dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
110340dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
110440dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
110540dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
110640dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
110740dd7343SLorenzo Bianconi 			},
110840dd7343SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
110940dd7343SLorenzo Bianconi 				.reg = {
111040dd7343SLorenzo Bianconi 					.addr = 0x11,
111140dd7343SLorenzo Bianconi 					.mask = GENMASK(7, 4),
111240dd7343SLorenzo Bianconi 				},
111340dd7343SLorenzo Bianconi 				.odr_avl[0] = {  13, 0x01 },
111440dd7343SLorenzo Bianconi 				.odr_avl[1] = {  26, 0x02 },
111540dd7343SLorenzo Bianconi 				.odr_avl[2] = {  52, 0x03 },
111640dd7343SLorenzo Bianconi 				.odr_avl[3] = { 104, 0x04 },
111740dd7343SLorenzo Bianconi 				.odr_avl[4] = { 208, 0x05 },
111840dd7343SLorenzo Bianconi 				.odr_avl[5] = { 416, 0x06 },
111940dd7343SLorenzo Bianconi 			},
112040dd7343SLorenzo Bianconi 		},
1121640aca3fSLorenzo Bianconi 		.fs_table = {
1122640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
1123640aca3fSLorenzo Bianconi 				.reg = {
1124640aca3fSLorenzo Bianconi 					.addr = 0x10,
1125640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
1126640aca3fSLorenzo Bianconi 				},
1127640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_G_TO_M_S_2(61), 0x0 },
1128640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
1129640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
1130640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
113185ae3aeeSLorenzo Bianconi 				.fs_len = 4,
1132640aca3fSLorenzo Bianconi 			},
1133640aca3fSLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
1134640aca3fSLorenzo Bianconi 				.reg = {
1135640aca3fSLorenzo Bianconi 					.addr = 0x11,
1136640aca3fSLorenzo Bianconi 					.mask = GENMASK(3, 2),
1137640aca3fSLorenzo Bianconi 				},
1138640aca3fSLorenzo Bianconi 				.fs_avl[0] = {  IIO_DEGREE_TO_RAD(8750), 0x0 },
1139640aca3fSLorenzo Bianconi 				.fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
1140640aca3fSLorenzo Bianconi 				.fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
1141640aca3fSLorenzo Bianconi 				.fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
114285ae3aeeSLorenzo Bianconi 				.fs_len = 4,
1143640aca3fSLorenzo Bianconi 			},
1144640aca3fSLorenzo Bianconi 		},
11457e906103SLorenzo Bianconi 		.irq_config = {
11467e906103SLorenzo Bianconi 			.irq1 = {
11477e906103SLorenzo Bianconi 				.addr = 0x0d,
11487e906103SLorenzo Bianconi 				.mask = BIT(3),
11497e906103SLorenzo Bianconi 			},
11507e906103SLorenzo Bianconi 			.irq2 = {
11517e906103SLorenzo Bianconi 				.addr = 0x0e,
11527e906103SLorenzo Bianconi 				.mask = BIT(3),
11537e906103SLorenzo Bianconi 			},
11547e906103SLorenzo Bianconi 			.lir = {
11557e906103SLorenzo Bianconi 				.addr = 0x56,
11567e906103SLorenzo Bianconi 				.mask = BIT(0),
11577e906103SLorenzo Bianconi 			},
11587e906103SLorenzo Bianconi 			.clear_on_read = {
11597e906103SLorenzo Bianconi 				.addr = 0x56,
11607e906103SLorenzo Bianconi 				.mask = BIT(6),
11617e906103SLorenzo Bianconi 			},
11627e906103SLorenzo Bianconi 			.irq1_func = {
11637e906103SLorenzo Bianconi 				.addr = 0x5e,
11647e906103SLorenzo Bianconi 				.mask = BIT(5),
11657e906103SLorenzo Bianconi 			},
11667e906103SLorenzo Bianconi 			.irq2_func = {
11677e906103SLorenzo Bianconi 				.addr = 0x5f,
11687e906103SLorenzo Bianconi 				.mask = BIT(5),
11697e906103SLorenzo Bianconi 			},
117031fe8d4eSLorenzo Bianconi 			.hla = {
117131fe8d4eSLorenzo Bianconi 				.addr = 0x12,
117231fe8d4eSLorenzo Bianconi 				.mask = BIT(5),
117331fe8d4eSLorenzo Bianconi 			},
117431fe8d4eSLorenzo Bianconi 			.od = {
117531fe8d4eSLorenzo Bianconi 				.addr = 0x12,
117631fe8d4eSLorenzo Bianconi 				.mask = BIT(4),
117731fe8d4eSLorenzo Bianconi 			},
11787e906103SLorenzo Bianconi 		},
117943901008SLorenzo Bianconi 		.batch = {
118043901008SLorenzo Bianconi 			[ST_LSM6DSX_ID_ACC] = {
118143901008SLorenzo Bianconi 				.addr = 0x09,
118243901008SLorenzo Bianconi 				.mask = GENMASK(3, 0),
118343901008SLorenzo Bianconi 			},
118443901008SLorenzo Bianconi 			[ST_LSM6DSX_ID_GYRO] = {
118543901008SLorenzo Bianconi 				.addr = 0x09,
118643901008SLorenzo Bianconi 				.mask = GENMASK(7, 4),
118743901008SLorenzo Bianconi 			},
118843901008SLorenzo Bianconi 		},
118943901008SLorenzo Bianconi 		.fifo_ops = {
11903b72950dSLorenzo Bianconi 			.update_fifo = st_lsm6dsx_update_fifo,
119143901008SLorenzo Bianconi 			.read_fifo = st_lsm6dsx_read_tagged_fifo,
119243901008SLorenzo Bianconi 			.fifo_th = {
119343901008SLorenzo Bianconi 				.addr = 0x07,
119443901008SLorenzo Bianconi 				.mask = GENMASK(8, 0),
119543901008SLorenzo Bianconi 			},
119643901008SLorenzo Bianconi 			.fifo_diff = {
119743901008SLorenzo Bianconi 				.addr = 0x3a,
119870575abeSmario tesi 				.mask = GENMASK(9, 0),
119943901008SLorenzo Bianconi 			},
120043901008SLorenzo Bianconi 			.th_wl = 1,
120143901008SLorenzo Bianconi 		},
120243901008SLorenzo Bianconi 		.ts_settings = {
120343901008SLorenzo Bianconi 			.timer_en = {
120443901008SLorenzo Bianconi 				.addr = 0x19,
120543901008SLorenzo Bianconi 				.mask = BIT(5),
120643901008SLorenzo Bianconi 			},
120743901008SLorenzo Bianconi 			.decimator = {
120843901008SLorenzo Bianconi 				.addr = 0x0a,
120943901008SLorenzo Bianconi 				.mask = GENMASK(7, 6),
121043901008SLorenzo Bianconi 			},
121143901008SLorenzo Bianconi 		},
121243901008SLorenzo Bianconi 		.shub_settings = {
121343901008SLorenzo Bianconi 			.page_mux = {
121443901008SLorenzo Bianconi 				.addr = 0x01,
121543901008SLorenzo Bianconi 				.mask = BIT(6),
121643901008SLorenzo Bianconi 			},
121743901008SLorenzo Bianconi 			.master_en = {
121843901008SLorenzo Bianconi 				.addr = 0x14,
121943901008SLorenzo Bianconi 				.mask = BIT(2),
122043901008SLorenzo Bianconi 			},
122143901008SLorenzo Bianconi 			.pullup_en = {
122243901008SLorenzo Bianconi 				.addr = 0x14,
122343901008SLorenzo Bianconi 				.mask = BIT(3),
122443901008SLorenzo Bianconi 			},
122543901008SLorenzo Bianconi 			.aux_sens = {
122643901008SLorenzo Bianconi 				.addr = 0x14,
122743901008SLorenzo Bianconi 				.mask = GENMASK(1, 0),
122843901008SLorenzo Bianconi 			},
122943901008SLorenzo Bianconi 			.wr_once = {
123043901008SLorenzo Bianconi 				.addr = 0x14,
123143901008SLorenzo Bianconi 				.mask = BIT(6),
123243901008SLorenzo Bianconi 			},
123343901008SLorenzo Bianconi 			.shub_out = 0x02,
123443901008SLorenzo Bianconi 			.slv0_addr = 0x15,
123543901008SLorenzo Bianconi 			.dw_slv0_addr = 0x21,
123643901008SLorenzo Bianconi 			.batch_en = BIT(3),
1237b5969abfSSean Nyekjaer 		},
1238b5969abfSSean Nyekjaer 		.event_settings = {
1239b5969abfSSean Nyekjaer 			.enable_reg = {
1240b5969abfSSean Nyekjaer 				.addr = 0x58,
1241b5969abfSSean Nyekjaer 				.mask = BIT(7),
1242b5969abfSSean Nyekjaer 			},
1243b5969abfSSean Nyekjaer 			.wakeup_reg = {
1244b5969abfSSean Nyekjaer 				.addr = 0x5B,
1245b5969abfSSean Nyekjaer 				.mask = GENMASK(5, 0),
1246b5969abfSSean Nyekjaer 			},
12471aabad1fSSean Nyekjaer 			.wakeup_src_reg = 0x1b,
12481aabad1fSSean Nyekjaer 			.wakeup_src_status_mask = BIT(3),
12491aabad1fSSean Nyekjaer 			.wakeup_src_z_mask = BIT(0),
12501aabad1fSSean Nyekjaer 			.wakeup_src_y_mask = BIT(1),
12511aabad1fSSean Nyekjaer 			.wakeup_src_x_mask = BIT(2),
125243901008SLorenzo Bianconi 		}
125343901008SLorenzo Bianconi 	},
1254290a6ce1SLorenzo Bianconi };
1255290a6ce1SLorenzo Bianconi 
1256c91c1c84SLorenzo Bianconi int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
1257c91c1c84SLorenzo Bianconi {
1258c91c1c84SLorenzo Bianconi 	const struct st_lsm6dsx_shub_settings *hub_settings;
1259c91c1c84SLorenzo Bianconi 	unsigned int data;
1260c91c1c84SLorenzo Bianconi 	int err;
1261c91c1c84SLorenzo Bianconi 
1262c91c1c84SLorenzo Bianconi 	hub_settings = &hw->settings->shub_settings;
1263c91c1c84SLorenzo Bianconi 	data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
1264c91c1c84SLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
1265c91c1c84SLorenzo Bianconi 				 hub_settings->page_mux.mask, data);
1266c91c1c84SLorenzo Bianconi 	usleep_range(100, 150);
1267c91c1c84SLorenzo Bianconi 
1268c91c1c84SLorenzo Bianconi 	return err;
1269c91c1c84SLorenzo Bianconi }
1270c91c1c84SLorenzo Bianconi 
127181956a93SLorenzo Bianconi static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
127281956a93SLorenzo Bianconi 				   const char **name)
1273290a6ce1SLorenzo Bianconi {
127451a8b707SLorenzo Bianconi 	int err, i, j, data;
1275290a6ce1SLorenzo Bianconi 
1276290a6ce1SLorenzo Bianconi 	for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
1277d068e4a0SLorenzo Bianconi 		for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
127881956a93SLorenzo Bianconi 			if (id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
1279d068e4a0SLorenzo Bianconi 				break;
1280d068e4a0SLorenzo Bianconi 		}
1281d068e4a0SLorenzo Bianconi 		if (j < ST_LSM6DSX_MAX_ID)
1282290a6ce1SLorenzo Bianconi 			break;
1283290a6ce1SLorenzo Bianconi 	}
1284290a6ce1SLorenzo Bianconi 
1285290a6ce1SLorenzo Bianconi 	if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
1286290a6ce1SLorenzo Bianconi 		dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
1287290a6ce1SLorenzo Bianconi 		return -ENODEV;
1288290a6ce1SLorenzo Bianconi 	}
1289290a6ce1SLorenzo Bianconi 
129051a8b707SLorenzo Bianconi 	err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
1291290a6ce1SLorenzo Bianconi 	if (err < 0) {
1292290a6ce1SLorenzo Bianconi 		dev_err(hw->dev, "failed to read whoami register\n");
1293290a6ce1SLorenzo Bianconi 		return err;
1294290a6ce1SLorenzo Bianconi 	}
1295290a6ce1SLorenzo Bianconi 
1296290a6ce1SLorenzo Bianconi 	if (data != st_lsm6dsx_sensor_settings[i].wai) {
1297290a6ce1SLorenzo Bianconi 		dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
1298290a6ce1SLorenzo Bianconi 		return -ENODEV;
1299290a6ce1SLorenzo Bianconi 	}
1300290a6ce1SLorenzo Bianconi 
130181956a93SLorenzo Bianconi 	*name = st_lsm6dsx_sensor_settings[i].id[j].name;
1302290a6ce1SLorenzo Bianconi 	hw->settings = &st_lsm6dsx_sensor_settings[i];
1303290a6ce1SLorenzo Bianconi 
1304290a6ce1SLorenzo Bianconi 	return 0;
1305290a6ce1SLorenzo Bianconi }
1306290a6ce1SLorenzo Bianconi 
1307290a6ce1SLorenzo Bianconi static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
1308290a6ce1SLorenzo Bianconi 				     u32 gain)
1309290a6ce1SLorenzo Bianconi {
1310640aca3fSLorenzo Bianconi 	const struct st_lsm6dsx_fs_table_entry *fs_table;
1311739aff87SLorenzo Bianconi 	unsigned int data;
1312290a6ce1SLorenzo Bianconi 	int i, err;
1313290a6ce1SLorenzo Bianconi 
1314640aca3fSLorenzo Bianconi 	fs_table = &sensor->hw->settings->fs_table[sensor->id];
131585ae3aeeSLorenzo Bianconi 	for (i = 0; i < fs_table->fs_len; i++) {
1316640aca3fSLorenzo Bianconi 		if (fs_table->fs_avl[i].gain == gain)
1317290a6ce1SLorenzo Bianconi 			break;
131885ae3aeeSLorenzo Bianconi 	}
1319290a6ce1SLorenzo Bianconi 
132085ae3aeeSLorenzo Bianconi 	if (i == fs_table->fs_len)
1321290a6ce1SLorenzo Bianconi 		return -EINVAL;
1322290a6ce1SLorenzo Bianconi 
1323640aca3fSLorenzo Bianconi 	data = ST_LSM6DSX_SHIFT_VAL(fs_table->fs_avl[i].val,
1324640aca3fSLorenzo Bianconi 				    fs_table->reg.mask);
1325640aca3fSLorenzo Bianconi 	err = st_lsm6dsx_update_bits_locked(sensor->hw, fs_table->reg.addr,
1326640aca3fSLorenzo Bianconi 					    fs_table->reg.mask, data);
1327290a6ce1SLorenzo Bianconi 	if (err < 0)
1328290a6ce1SLorenzo Bianconi 		return err;
1329290a6ce1SLorenzo Bianconi 
1330290a6ce1SLorenzo Bianconi 	sensor->gain = gain;
1331290a6ce1SLorenzo Bianconi 
1332290a6ce1SLorenzo Bianconi 	return 0;
1333290a6ce1SLorenzo Bianconi }
1334290a6ce1SLorenzo Bianconi 
133554a6d0c6SLorenzo Bianconi int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val)
1336290a6ce1SLorenzo Bianconi {
133740dd7343SLorenzo Bianconi 	const struct st_lsm6dsx_odr_table_entry *odr_table;
13382ccc1503SLorenzo Bianconi 	int i;
1339290a6ce1SLorenzo Bianconi 
134040dd7343SLorenzo Bianconi 	odr_table = &sensor->hw->settings->odr_table[sensor->id];
1341290a6ce1SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
13426ffb55e5SLorenzo Bianconi 		/*
13436ffb55e5SLorenzo Bianconi 		 * ext devices can run at different odr respect to
13446ffb55e5SLorenzo Bianconi 		 * accel sensor
13456ffb55e5SLorenzo Bianconi 		 */
134640dd7343SLorenzo Bianconi 		if (odr_table->odr_avl[i].hz >= odr)
1347290a6ce1SLorenzo Bianconi 			break;
1348290a6ce1SLorenzo Bianconi 
1349290a6ce1SLorenzo Bianconi 	if (i == ST_LSM6DSX_ODR_LIST_SIZE)
1350290a6ce1SLorenzo Bianconi 		return -EINVAL;
1351290a6ce1SLorenzo Bianconi 
135240dd7343SLorenzo Bianconi 	*val = odr_table->odr_avl[i].val;
1353290a6ce1SLorenzo Bianconi 
1354290a6ce1SLorenzo Bianconi 	return 0;
1355290a6ce1SLorenzo Bianconi }
1356290a6ce1SLorenzo Bianconi 
13576ffb55e5SLorenzo Bianconi static u16 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u16 odr,
13586ffb55e5SLorenzo Bianconi 					   enum st_lsm6dsx_sensor_id id)
13592ccc1503SLorenzo Bianconi {
13606ffb55e5SLorenzo Bianconi 	struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
13616ffb55e5SLorenzo Bianconi 
13626ffb55e5SLorenzo Bianconi 	if (odr > 0) {
13636ffb55e5SLorenzo Bianconi 		if (hw->enable_mask & BIT(id))
13646ffb55e5SLorenzo Bianconi 			return max_t(u16, ref->odr, odr);
13656ffb55e5SLorenzo Bianconi 		else
13666ffb55e5SLorenzo Bianconi 			return odr;
13676ffb55e5SLorenzo Bianconi 	} else {
13686ffb55e5SLorenzo Bianconi 		return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
13696ffb55e5SLorenzo Bianconi 	}
13706ffb55e5SLorenzo Bianconi }
13716ffb55e5SLorenzo Bianconi 
13726ffb55e5SLorenzo Bianconi static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 req_odr)
13736ffb55e5SLorenzo Bianconi {
13746ffb55e5SLorenzo Bianconi 	struct st_lsm6dsx_sensor *ref_sensor = sensor;
137551a8b707SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
137651a8b707SLorenzo Bianconi 	const struct st_lsm6dsx_reg *reg;
1377739aff87SLorenzo Bianconi 	unsigned int data;
13786ffb55e5SLorenzo Bianconi 	u8 val = 0;
13792ccc1503SLorenzo Bianconi 	int err;
13802ccc1503SLorenzo Bianconi 
13816ffb55e5SLorenzo Bianconi 	switch (sensor->id) {
13826ffb55e5SLorenzo Bianconi 	case ST_LSM6DSX_ID_EXT0:
13836ffb55e5SLorenzo Bianconi 	case ST_LSM6DSX_ID_EXT1:
13846ffb55e5SLorenzo Bianconi 	case ST_LSM6DSX_ID_EXT2:
13856ffb55e5SLorenzo Bianconi 	case ST_LSM6DSX_ID_ACC: {
13866ffb55e5SLorenzo Bianconi 		u16 odr;
13876ffb55e5SLorenzo Bianconi 		int i;
13886ffb55e5SLorenzo Bianconi 
13896ffb55e5SLorenzo Bianconi 		/*
13906ffb55e5SLorenzo Bianconi 		 * i2c embedded controller relies on the accelerometer sensor as
13916ffb55e5SLorenzo Bianconi 		 * bus read/write trigger so we need to enable accel device
13926ffb55e5SLorenzo Bianconi 		 * at odr = max(accel_odr, ext_odr) in order to properly
13936ffb55e5SLorenzo Bianconi 		 * communicate with i2c slave devices
13946ffb55e5SLorenzo Bianconi 		 */
13956ffb55e5SLorenzo Bianconi 		ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
13966ffb55e5SLorenzo Bianconi 		for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
13976ffb55e5SLorenzo Bianconi 			if (!hw->iio_devs[i] || i == sensor->id)
13986ffb55e5SLorenzo Bianconi 				continue;
13996ffb55e5SLorenzo Bianconi 
14006ffb55e5SLorenzo Bianconi 			odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
14016ffb55e5SLorenzo Bianconi 			if (odr != req_odr)
14026ffb55e5SLorenzo Bianconi 				/* device already configured */
14036ffb55e5SLorenzo Bianconi 				return 0;
14046ffb55e5SLorenzo Bianconi 		}
14056ffb55e5SLorenzo Bianconi 		break;
14066ffb55e5SLorenzo Bianconi 	}
14076ffb55e5SLorenzo Bianconi 	default:
14086ffb55e5SLorenzo Bianconi 		break;
14096ffb55e5SLorenzo Bianconi 	}
14106ffb55e5SLorenzo Bianconi 
14116ffb55e5SLorenzo Bianconi 	if (req_odr > 0) {
14126ffb55e5SLorenzo Bianconi 		err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
14132ccc1503SLorenzo Bianconi 		if (err < 0)
14142ccc1503SLorenzo Bianconi 			return err;
14156ffb55e5SLorenzo Bianconi 	}
14162ccc1503SLorenzo Bianconi 
141740dd7343SLorenzo Bianconi 	reg = &hw->settings->odr_table[ref_sensor->id].reg;
1418739aff87SLorenzo Bianconi 	data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1419739aff87SLorenzo Bianconi 	return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
14202ccc1503SLorenzo Bianconi }
14212ccc1503SLorenzo Bianconi 
142217750443SLorenzo Bianconi int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
142317750443SLorenzo Bianconi 				 bool enable)
1424290a6ce1SLorenzo Bianconi {
142551a8b707SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
142617750443SLorenzo Bianconi 	u16 odr = enable ? sensor->odr : 0;
1427290a6ce1SLorenzo Bianconi 	int err;
1428290a6ce1SLorenzo Bianconi 
142917750443SLorenzo Bianconi 	err = st_lsm6dsx_set_odr(sensor, odr);
1430290a6ce1SLorenzo Bianconi 	if (err < 0)
1431290a6ce1SLorenzo Bianconi 		return err;
1432290a6ce1SLorenzo Bianconi 
143317750443SLorenzo Bianconi 	if (enable)
143417750443SLorenzo Bianconi 		hw->enable_mask |= BIT(sensor->id);
143517750443SLorenzo Bianconi 	else
143617750443SLorenzo Bianconi 		hw->enable_mask &= ~BIT(sensor->id);
1437290a6ce1SLorenzo Bianconi 
1438290a6ce1SLorenzo Bianconi 	return 0;
1439290a6ce1SLorenzo Bianconi }
1440290a6ce1SLorenzo Bianconi 
1441290a6ce1SLorenzo Bianconi static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
1442290a6ce1SLorenzo Bianconi 				   u8 addr, int *val)
1443290a6ce1SLorenzo Bianconi {
144451a8b707SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
1445290a6ce1SLorenzo Bianconi 	int err, delay;
1446290a6ce1SLorenzo Bianconi 	__le16 data;
1447290a6ce1SLorenzo Bianconi 
144817750443SLorenzo Bianconi 	err = st_lsm6dsx_sensor_set_enable(sensor, true);
1449290a6ce1SLorenzo Bianconi 	if (err < 0)
1450290a6ce1SLorenzo Bianconi 		return err;
1451290a6ce1SLorenzo Bianconi 
1452290a6ce1SLorenzo Bianconi 	delay = 1000000 / sensor->odr;
1453290a6ce1SLorenzo Bianconi 	usleep_range(delay, 2 * delay);
1454290a6ce1SLorenzo Bianconi 
1455739aff87SLorenzo Bianconi 	err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
1456290a6ce1SLorenzo Bianconi 	if (err < 0)
1457290a6ce1SLorenzo Bianconi 		return err;
1458290a6ce1SLorenzo Bianconi 
1459b5969abfSSean Nyekjaer 	if (!hw->enable_event)
146017750443SLorenzo Bianconi 		st_lsm6dsx_sensor_set_enable(sensor, false);
1461290a6ce1SLorenzo Bianconi 
14627b9ebe42SLorenzo Bianconi 	*val = (s16)le16_to_cpu(data);
1463290a6ce1SLorenzo Bianconi 
1464290a6ce1SLorenzo Bianconi 	return IIO_VAL_INT;
1465290a6ce1SLorenzo Bianconi }
1466290a6ce1SLorenzo Bianconi 
1467290a6ce1SLorenzo Bianconi static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
1468290a6ce1SLorenzo Bianconi 			       struct iio_chan_spec const *ch,
1469290a6ce1SLorenzo Bianconi 			       int *val, int *val2, long mask)
1470290a6ce1SLorenzo Bianconi {
1471290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1472290a6ce1SLorenzo Bianconi 	int ret;
1473290a6ce1SLorenzo Bianconi 
1474290a6ce1SLorenzo Bianconi 	switch (mask) {
1475290a6ce1SLorenzo Bianconi 	case IIO_CHAN_INFO_RAW:
1476290a6ce1SLorenzo Bianconi 		ret = iio_device_claim_direct_mode(iio_dev);
1477290a6ce1SLorenzo Bianconi 		if (ret)
1478290a6ce1SLorenzo Bianconi 			break;
1479290a6ce1SLorenzo Bianconi 
1480290a6ce1SLorenzo Bianconi 		ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
1481290a6ce1SLorenzo Bianconi 		iio_device_release_direct_mode(iio_dev);
1482290a6ce1SLorenzo Bianconi 		break;
1483290a6ce1SLorenzo Bianconi 	case IIO_CHAN_INFO_SAMP_FREQ:
1484290a6ce1SLorenzo Bianconi 		*val = sensor->odr;
1485290a6ce1SLorenzo Bianconi 		ret = IIO_VAL_INT;
1486290a6ce1SLorenzo Bianconi 		break;
1487290a6ce1SLorenzo Bianconi 	case IIO_CHAN_INFO_SCALE:
1488290a6ce1SLorenzo Bianconi 		*val = 0;
1489290a6ce1SLorenzo Bianconi 		*val2 = sensor->gain;
1490290a6ce1SLorenzo Bianconi 		ret = IIO_VAL_INT_PLUS_MICRO;
1491290a6ce1SLorenzo Bianconi 		break;
1492290a6ce1SLorenzo Bianconi 	default:
1493290a6ce1SLorenzo Bianconi 		ret = -EINVAL;
1494290a6ce1SLorenzo Bianconi 		break;
1495290a6ce1SLorenzo Bianconi 	}
1496290a6ce1SLorenzo Bianconi 
1497290a6ce1SLorenzo Bianconi 	return ret;
1498290a6ce1SLorenzo Bianconi }
1499290a6ce1SLorenzo Bianconi 
1500290a6ce1SLorenzo Bianconi static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
1501290a6ce1SLorenzo Bianconi 				struct iio_chan_spec const *chan,
1502290a6ce1SLorenzo Bianconi 				int val, int val2, long mask)
1503290a6ce1SLorenzo Bianconi {
1504290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1505290a6ce1SLorenzo Bianconi 	int err;
1506290a6ce1SLorenzo Bianconi 
1507290a6ce1SLorenzo Bianconi 	err = iio_device_claim_direct_mode(iio_dev);
1508290a6ce1SLorenzo Bianconi 	if (err)
1509290a6ce1SLorenzo Bianconi 		return err;
1510290a6ce1SLorenzo Bianconi 
1511290a6ce1SLorenzo Bianconi 	switch (mask) {
1512290a6ce1SLorenzo Bianconi 	case IIO_CHAN_INFO_SCALE:
1513290a6ce1SLorenzo Bianconi 		err = st_lsm6dsx_set_full_scale(sensor, val2);
1514290a6ce1SLorenzo Bianconi 		break;
15152ccc1503SLorenzo Bianconi 	case IIO_CHAN_INFO_SAMP_FREQ: {
15162ccc1503SLorenzo Bianconi 		u8 data;
15172ccc1503SLorenzo Bianconi 
15182ccc1503SLorenzo Bianconi 		err = st_lsm6dsx_check_odr(sensor, val, &data);
15195e3c3e33SLorenzo Bianconi 		if (!err)
15205e3c3e33SLorenzo Bianconi 			sensor->odr = val;
1521290a6ce1SLorenzo Bianconi 		break;
15222ccc1503SLorenzo Bianconi 	}
1523290a6ce1SLorenzo Bianconi 	default:
1524290a6ce1SLorenzo Bianconi 		err = -EINVAL;
1525290a6ce1SLorenzo Bianconi 		break;
1526290a6ce1SLorenzo Bianconi 	}
1527290a6ce1SLorenzo Bianconi 
1528290a6ce1SLorenzo Bianconi 	iio_device_release_direct_mode(iio_dev);
1529290a6ce1SLorenzo Bianconi 
1530290a6ce1SLorenzo Bianconi 	return err;
1531290a6ce1SLorenzo Bianconi }
1532290a6ce1SLorenzo Bianconi 
1533b5969abfSSean Nyekjaer static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw *hw, int state)
1534b5969abfSSean Nyekjaer {
153584b2e7c3SLorenzo Bianconi 	const struct st_lsm6dsx_reg *reg;
153604ca37d5SLorenzo Bianconi 	unsigned int data;
1537b5969abfSSean Nyekjaer 	int err;
1538b5969abfSSean Nyekjaer 
15397e906103SLorenzo Bianconi 	if (!hw->settings->irq_config.irq1_func.addr)
1540b5969abfSSean Nyekjaer 		return -ENOTSUPP;
1541b5969abfSSean Nyekjaer 
154284b2e7c3SLorenzo Bianconi 	reg = &hw->settings->event_settings.enable_reg;
154384b2e7c3SLorenzo Bianconi 	if (reg->addr) {
154404ca37d5SLorenzo Bianconi 		data = ST_LSM6DSX_SHIFT_VAL(state, reg->mask);
154504ca37d5SLorenzo Bianconi 		err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
154604ca37d5SLorenzo Bianconi 						    reg->mask, data);
1547b5969abfSSean Nyekjaer 		if (err < 0)
1548b5969abfSSean Nyekjaer 			return err;
154984b2e7c3SLorenzo Bianconi 	}
1550b5969abfSSean Nyekjaer 
1551b5969abfSSean Nyekjaer 	/* Enable wakeup interrupt */
155204ca37d5SLorenzo Bianconi 	data = ST_LSM6DSX_SHIFT_VAL(state, hw->irq_routing->mask);
155304ca37d5SLorenzo Bianconi 	return st_lsm6dsx_update_bits_locked(hw, hw->irq_routing->addr,
155404ca37d5SLorenzo Bianconi 					     hw->irq_routing->mask, data);
1555b5969abfSSean Nyekjaer }
1556b5969abfSSean Nyekjaer 
1557b5969abfSSean Nyekjaer static int st_lsm6dsx_read_event(struct iio_dev *iio_dev,
1558b5969abfSSean Nyekjaer 				   const struct iio_chan_spec *chan,
1559b5969abfSSean Nyekjaer 				   enum iio_event_type type,
1560b5969abfSSean Nyekjaer 				   enum iio_event_direction dir,
1561b5969abfSSean Nyekjaer 				   enum iio_event_info info,
1562b5969abfSSean Nyekjaer 				   int *val, int *val2)
1563b5969abfSSean Nyekjaer {
1564b5969abfSSean Nyekjaer 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1565b5969abfSSean Nyekjaer 	struct st_lsm6dsx_hw *hw = sensor->hw;
1566b5969abfSSean Nyekjaer 
1567b5969abfSSean Nyekjaer 	if (type != IIO_EV_TYPE_THRESH)
1568b5969abfSSean Nyekjaer 		return -EINVAL;
1569b5969abfSSean Nyekjaer 
1570b5969abfSSean Nyekjaer 	*val2 = 0;
1571b5969abfSSean Nyekjaer 	*val = hw->event_threshold;
1572b5969abfSSean Nyekjaer 
1573b5969abfSSean Nyekjaer 	return IIO_VAL_INT;
1574b5969abfSSean Nyekjaer }
1575b5969abfSSean Nyekjaer 
1576b307f495SLorenzo Bianconi static int
1577b307f495SLorenzo Bianconi st_lsm6dsx_write_event(struct iio_dev *iio_dev,
1578b5969abfSSean Nyekjaer 		       const struct iio_chan_spec *chan,
1579b5969abfSSean Nyekjaer 		       enum iio_event_type type,
1580b5969abfSSean Nyekjaer 		       enum iio_event_direction dir,
1581b5969abfSSean Nyekjaer 		       enum iio_event_info info,
1582b5969abfSSean Nyekjaer 		       int val, int val2)
1583b5969abfSSean Nyekjaer {
1584b5969abfSSean Nyekjaer 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1585b5969abfSSean Nyekjaer 	struct st_lsm6dsx_hw *hw = sensor->hw;
158604ca37d5SLorenzo Bianconi 	const struct st_lsm6dsx_reg *reg;
158704ca37d5SLorenzo Bianconi 	unsigned int data;
1588b5969abfSSean Nyekjaer 	int err;
1589b5969abfSSean Nyekjaer 
1590b5969abfSSean Nyekjaer 	if (type != IIO_EV_TYPE_THRESH)
1591b5969abfSSean Nyekjaer 		return -EINVAL;
1592b5969abfSSean Nyekjaer 
1593b5969abfSSean Nyekjaer 	if (val < 0 || val > 31)
1594b5969abfSSean Nyekjaer 		return -EINVAL;
1595b5969abfSSean Nyekjaer 
159604ca37d5SLorenzo Bianconi 	reg = &hw->settings->event_settings.wakeup_reg;
159704ca37d5SLorenzo Bianconi 	data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
159804ca37d5SLorenzo Bianconi 	err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
159904ca37d5SLorenzo Bianconi 					    reg->mask, data);
160004ca37d5SLorenzo Bianconi 	if (err < 0)
1601b5969abfSSean Nyekjaer 		return -EINVAL;
1602b5969abfSSean Nyekjaer 
1603b5969abfSSean Nyekjaer 	hw->event_threshold = val;
1604b5969abfSSean Nyekjaer 
1605b5969abfSSean Nyekjaer 	return 0;
1606b5969abfSSean Nyekjaer }
1607b5969abfSSean Nyekjaer 
1608b307f495SLorenzo Bianconi static int
1609b307f495SLorenzo Bianconi st_lsm6dsx_read_event_config(struct iio_dev *iio_dev,
1610b5969abfSSean Nyekjaer 			     const struct iio_chan_spec *chan,
1611b5969abfSSean Nyekjaer 			     enum iio_event_type type,
1612b5969abfSSean Nyekjaer 			     enum iio_event_direction dir)
1613b5969abfSSean Nyekjaer {
1614b5969abfSSean Nyekjaer 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1615b5969abfSSean Nyekjaer 	struct st_lsm6dsx_hw *hw = sensor->hw;
1616b5969abfSSean Nyekjaer 
1617b5969abfSSean Nyekjaer 	if (type != IIO_EV_TYPE_THRESH)
1618b5969abfSSean Nyekjaer 		return -EINVAL;
1619b5969abfSSean Nyekjaer 
16201aabad1fSSean Nyekjaer 	return !!(hw->enable_event & BIT(chan->channel2));
1621b5969abfSSean Nyekjaer }
1622b5969abfSSean Nyekjaer 
1623b307f495SLorenzo Bianconi static int
1624b307f495SLorenzo Bianconi st_lsm6dsx_write_event_config(struct iio_dev *iio_dev,
1625b5969abfSSean Nyekjaer 			      const struct iio_chan_spec *chan,
1626b5969abfSSean Nyekjaer 			      enum iio_event_type type,
1627b307f495SLorenzo Bianconi 			      enum iio_event_direction dir, int state)
1628b5969abfSSean Nyekjaer {
1629b5969abfSSean Nyekjaer 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1630b5969abfSSean Nyekjaer 	struct st_lsm6dsx_hw *hw = sensor->hw;
16311aabad1fSSean Nyekjaer 	u8 enable_event;
1632b5969abfSSean Nyekjaer 	int err = 0;
1633b5969abfSSean Nyekjaer 
1634b5969abfSSean Nyekjaer 	if (type != IIO_EV_TYPE_THRESH)
1635b5969abfSSean Nyekjaer 		return -EINVAL;
1636b5969abfSSean Nyekjaer 
16371aabad1fSSean Nyekjaer 	if (state) {
16381aabad1fSSean Nyekjaer 		enable_event = hw->enable_event | BIT(chan->channel2);
16391aabad1fSSean Nyekjaer 
1640b5969abfSSean Nyekjaer 		/* do not enable events if they are already enabled */
16411aabad1fSSean Nyekjaer 		if (hw->enable_event)
16421aabad1fSSean Nyekjaer 			goto out;
16431aabad1fSSean Nyekjaer 	} else {
16441aabad1fSSean Nyekjaer 		enable_event = hw->enable_event & ~BIT(chan->channel2);
16451aabad1fSSean Nyekjaer 
16461aabad1fSSean Nyekjaer 		/* only turn off sensor if no events is enabled */
16471aabad1fSSean Nyekjaer 		if (enable_event)
16481aabad1fSSean Nyekjaer 			goto out;
16491aabad1fSSean Nyekjaer 	}
16501aabad1fSSean Nyekjaer 
16511aabad1fSSean Nyekjaer 	/* stop here if no changes have been made */
16521aabad1fSSean Nyekjaer 	if (hw->enable_event == enable_event)
1653b5969abfSSean Nyekjaer 		return 0;
1654b5969abfSSean Nyekjaer 
1655b5969abfSSean Nyekjaer 	err = st_lsm6dsx_event_setup(hw, state);
1656b5969abfSSean Nyekjaer 	if (err < 0)
1657b5969abfSSean Nyekjaer 		return err;
1658b5969abfSSean Nyekjaer 
1659d278d447SLorenzo Bianconi 	mutex_lock(&hw->conf_lock);
1660b5969abfSSean Nyekjaer 	err = st_lsm6dsx_sensor_set_enable(sensor, state);
1661d278d447SLorenzo Bianconi 	mutex_unlock(&hw->conf_lock);
1662b5969abfSSean Nyekjaer 	if (err < 0)
1663b5969abfSSean Nyekjaer 		return err;
1664b5969abfSSean Nyekjaer 
16651aabad1fSSean Nyekjaer out:
16661aabad1fSSean Nyekjaer 	hw->enable_event = enable_event;
1667b5969abfSSean Nyekjaer 
1668b5969abfSSean Nyekjaer 	return 0;
1669b5969abfSSean Nyekjaer }
1670b5969abfSSean Nyekjaer 
1671d40464f3SLorenzo Bianconi int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
1672290a6ce1SLorenzo Bianconi {
1673290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1674290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
16758f2a88a2SLorenzo Bianconi 	int err;
1676290a6ce1SLorenzo Bianconi 
16778f2a88a2SLorenzo Bianconi 	if (val < 1 || val > hw->settings->max_fifo_size)
1678290a6ce1SLorenzo Bianconi 		return -EINVAL;
1679290a6ce1SLorenzo Bianconi 
1680335eaedcSLorenzo Bianconi 	mutex_lock(&hw->conf_lock);
1681335eaedcSLorenzo Bianconi 
1682290a6ce1SLorenzo Bianconi 	err = st_lsm6dsx_update_watermark(sensor, val);
1683335eaedcSLorenzo Bianconi 
1684335eaedcSLorenzo Bianconi 	mutex_unlock(&hw->conf_lock);
1685335eaedcSLorenzo Bianconi 
1686290a6ce1SLorenzo Bianconi 	if (err < 0)
1687290a6ce1SLorenzo Bianconi 		return err;
1688290a6ce1SLorenzo Bianconi 
1689290a6ce1SLorenzo Bianconi 	sensor->watermark = val;
1690290a6ce1SLorenzo Bianconi 
1691290a6ce1SLorenzo Bianconi 	return 0;
1692290a6ce1SLorenzo Bianconi }
1693290a6ce1SLorenzo Bianconi 
1694290a6ce1SLorenzo Bianconi static ssize_t
1695290a6ce1SLorenzo Bianconi st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
1696290a6ce1SLorenzo Bianconi 					  struct device_attribute *attr,
1697290a6ce1SLorenzo Bianconi 					  char *buf)
1698290a6ce1SLorenzo Bianconi {
1699290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
1700290a6ce1SLorenzo Bianconi 	enum st_lsm6dsx_sensor_id id = sensor->id;
170140dd7343SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
1702290a6ce1SLorenzo Bianconi 	int i, len = 0;
1703290a6ce1SLorenzo Bianconi 
1704290a6ce1SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
1705290a6ce1SLorenzo Bianconi 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
170640dd7343SLorenzo Bianconi 				 hw->settings->odr_table[id].odr_avl[i].hz);
1707290a6ce1SLorenzo Bianconi 	buf[len - 1] = '\n';
1708290a6ce1SLorenzo Bianconi 
1709290a6ce1SLorenzo Bianconi 	return len;
1710290a6ce1SLorenzo Bianconi }
1711290a6ce1SLorenzo Bianconi 
1712290a6ce1SLorenzo Bianconi static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
1713290a6ce1SLorenzo Bianconi 					    struct device_attribute *attr,
1714290a6ce1SLorenzo Bianconi 					    char *buf)
1715290a6ce1SLorenzo Bianconi {
1716290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
17170f7e1728SLorenzo Bianconi 	const struct st_lsm6dsx_fs_table_entry *fs_table;
1718640aca3fSLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = sensor->hw;
1719290a6ce1SLorenzo Bianconi 	int i, len = 0;
1720290a6ce1SLorenzo Bianconi 
172185ae3aeeSLorenzo Bianconi 	fs_table = &hw->settings->fs_table[sensor->id];
172285ae3aeeSLorenzo Bianconi 	for (i = 0; i < fs_table->fs_len; i++)
1723290a6ce1SLorenzo Bianconi 		len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
17240f7e1728SLorenzo Bianconi 				 fs_table->fs_avl[i].gain);
1725290a6ce1SLorenzo Bianconi 	buf[len - 1] = '\n';
1726290a6ce1SLorenzo Bianconi 
1727290a6ce1SLorenzo Bianconi 	return len;
1728290a6ce1SLorenzo Bianconi }
1729290a6ce1SLorenzo Bianconi 
1730290a6ce1SLorenzo Bianconi static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
1731290a6ce1SLorenzo Bianconi static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
1732290a6ce1SLorenzo Bianconi 		       st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1733290a6ce1SLorenzo Bianconi static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
1734290a6ce1SLorenzo Bianconi 		       st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1735290a6ce1SLorenzo Bianconi 
1736290a6ce1SLorenzo Bianconi static struct attribute *st_lsm6dsx_acc_attributes[] = {
1737290a6ce1SLorenzo Bianconi 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1738290a6ce1SLorenzo Bianconi 	&iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1739290a6ce1SLorenzo Bianconi 	NULL,
1740290a6ce1SLorenzo Bianconi };
1741290a6ce1SLorenzo Bianconi 
1742290a6ce1SLorenzo Bianconi static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
1743290a6ce1SLorenzo Bianconi 	.attrs = st_lsm6dsx_acc_attributes,
1744290a6ce1SLorenzo Bianconi };
1745290a6ce1SLorenzo Bianconi 
1746290a6ce1SLorenzo Bianconi static const struct iio_info st_lsm6dsx_acc_info = {
1747290a6ce1SLorenzo Bianconi 	.attrs = &st_lsm6dsx_acc_attribute_group,
1748290a6ce1SLorenzo Bianconi 	.read_raw = st_lsm6dsx_read_raw,
1749290a6ce1SLorenzo Bianconi 	.write_raw = st_lsm6dsx_write_raw,
1750b5969abfSSean Nyekjaer 	.read_event_value = st_lsm6dsx_read_event,
1751b5969abfSSean Nyekjaer 	.write_event_value = st_lsm6dsx_write_event,
1752b5969abfSSean Nyekjaer 	.read_event_config = st_lsm6dsx_read_event_config,
1753b5969abfSSean Nyekjaer 	.write_event_config = st_lsm6dsx_write_event_config,
1754290a6ce1SLorenzo Bianconi 	.hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1755290a6ce1SLorenzo Bianconi };
1756290a6ce1SLorenzo Bianconi 
1757290a6ce1SLorenzo Bianconi static struct attribute *st_lsm6dsx_gyro_attributes[] = {
1758290a6ce1SLorenzo Bianconi 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1759290a6ce1SLorenzo Bianconi 	&iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
1760290a6ce1SLorenzo Bianconi 	NULL,
1761290a6ce1SLorenzo Bianconi };
1762290a6ce1SLorenzo Bianconi 
1763290a6ce1SLorenzo Bianconi static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
1764290a6ce1SLorenzo Bianconi 	.attrs = st_lsm6dsx_gyro_attributes,
1765290a6ce1SLorenzo Bianconi };
1766290a6ce1SLorenzo Bianconi 
1767290a6ce1SLorenzo Bianconi static const struct iio_info st_lsm6dsx_gyro_info = {
1768290a6ce1SLorenzo Bianconi 	.attrs = &st_lsm6dsx_gyro_attribute_group,
1769290a6ce1SLorenzo Bianconi 	.read_raw = st_lsm6dsx_read_raw,
1770290a6ce1SLorenzo Bianconi 	.write_raw = st_lsm6dsx_write_raw,
1771290a6ce1SLorenzo Bianconi 	.hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1772290a6ce1SLorenzo Bianconi };
1773290a6ce1SLorenzo Bianconi 
1774dba32904SLorenzo Bianconi static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
1775dba32904SLorenzo Bianconi {
1776dba32904SLorenzo Bianconi 	struct device_node *np = hw->dev->of_node;
1777dba32904SLorenzo Bianconi 
1778dba32904SLorenzo Bianconi 	if (!np)
1779dba32904SLorenzo Bianconi 		return -EINVAL;
1780dba32904SLorenzo Bianconi 
1781bf235277SLorenzo Bianconi 	return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin);
1782dba32904SLorenzo Bianconi }
1783dba32904SLorenzo Bianconi 
17847e906103SLorenzo Bianconi static int
17857e906103SLorenzo Bianconi st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw,
17867e906103SLorenzo Bianconi 			const struct st_lsm6dsx_reg **drdy_reg)
1787dba32904SLorenzo Bianconi {
1788dba32904SLorenzo Bianconi 	int err = 0, drdy_pin;
1789dba32904SLorenzo Bianconi 
1790dba32904SLorenzo Bianconi 	if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) {
1791dba32904SLorenzo Bianconi 		struct st_sensors_platform_data *pdata;
1792dba32904SLorenzo Bianconi 		struct device *dev = hw->dev;
1793dba32904SLorenzo Bianconi 
1794dba32904SLorenzo Bianconi 		pdata = (struct st_sensors_platform_data *)dev->platform_data;
1795dba32904SLorenzo Bianconi 		drdy_pin = pdata ? pdata->drdy_int_pin : 1;
1796dba32904SLorenzo Bianconi 	}
1797dba32904SLorenzo Bianconi 
1798dba32904SLorenzo Bianconi 	switch (drdy_pin) {
1799dba32904SLorenzo Bianconi 	case 1:
18007e906103SLorenzo Bianconi 		hw->irq_routing = &hw->settings->irq_config.irq1_func;
18017e906103SLorenzo Bianconi 		*drdy_reg = &hw->settings->irq_config.irq1;
1802dba32904SLorenzo Bianconi 		break;
1803dba32904SLorenzo Bianconi 	case 2:
18047e906103SLorenzo Bianconi 		hw->irq_routing = &hw->settings->irq_config.irq2_func;
18057e906103SLorenzo Bianconi 		*drdy_reg = &hw->settings->irq_config.irq2;
1806dba32904SLorenzo Bianconi 		break;
1807dba32904SLorenzo Bianconi 	default:
1808dba32904SLorenzo Bianconi 		dev_err(hw->dev, "unsupported data ready pin\n");
1809dba32904SLorenzo Bianconi 		err = -EINVAL;
1810dba32904SLorenzo Bianconi 		break;
1811dba32904SLorenzo Bianconi 	}
1812dba32904SLorenzo Bianconi 
1813dba32904SLorenzo Bianconi 	return err;
1814dba32904SLorenzo Bianconi }
1815dba32904SLorenzo Bianconi 
1816c91c1c84SLorenzo Bianconi static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
1817c91c1c84SLorenzo Bianconi {
1818c91c1c84SLorenzo Bianconi 	const struct st_lsm6dsx_shub_settings *hub_settings;
1819c91c1c84SLorenzo Bianconi 	struct device_node *np = hw->dev->of_node;
1820c91c1c84SLorenzo Bianconi 	struct st_sensors_platform_data *pdata;
1821c91c1c84SLorenzo Bianconi 	unsigned int data;
1822c91c1c84SLorenzo Bianconi 	int err = 0;
1823c91c1c84SLorenzo Bianconi 
1824c91c1c84SLorenzo Bianconi 	hub_settings = &hw->settings->shub_settings;
1825c91c1c84SLorenzo Bianconi 
1826c91c1c84SLorenzo Bianconi 	pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
1827c91c1c84SLorenzo Bianconi 	if ((np && of_property_read_bool(np, "st,pullups")) ||
1828c91c1c84SLorenzo Bianconi 	    (pdata && pdata->pullups)) {
1829c91c1c84SLorenzo Bianconi 		err = st_lsm6dsx_set_page(hw, true);
1830c91c1c84SLorenzo Bianconi 		if (err < 0)
1831c91c1c84SLorenzo Bianconi 			return err;
1832c91c1c84SLorenzo Bianconi 
1833c91c1c84SLorenzo Bianconi 		data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
1834c91c1c84SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap,
1835c91c1c84SLorenzo Bianconi 					 hub_settings->pullup_en.addr,
1836c91c1c84SLorenzo Bianconi 					 hub_settings->pullup_en.mask, data);
1837c91c1c84SLorenzo Bianconi 
1838c91c1c84SLorenzo Bianconi 		st_lsm6dsx_set_page(hw, false);
1839c91c1c84SLorenzo Bianconi 
1840c91c1c84SLorenzo Bianconi 		if (err < 0)
1841c91c1c84SLorenzo Bianconi 			return err;
1842c91c1c84SLorenzo Bianconi 	}
1843c91c1c84SLorenzo Bianconi 
1844c91c1c84SLorenzo Bianconi 	if (hub_settings->aux_sens.addr) {
1845c91c1c84SLorenzo Bianconi 		/* configure aux sensors */
1846c91c1c84SLorenzo Bianconi 		err = st_lsm6dsx_set_page(hw, true);
1847c91c1c84SLorenzo Bianconi 		if (err < 0)
1848c91c1c84SLorenzo Bianconi 			return err;
1849c91c1c84SLorenzo Bianconi 
1850c91c1c84SLorenzo Bianconi 		data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
1851c91c1c84SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap,
1852c91c1c84SLorenzo Bianconi 					 hub_settings->aux_sens.addr,
1853c91c1c84SLorenzo Bianconi 					 hub_settings->aux_sens.mask, data);
1854c91c1c84SLorenzo Bianconi 
1855c91c1c84SLorenzo Bianconi 		st_lsm6dsx_set_page(hw, false);
1856c91c1c84SLorenzo Bianconi 	}
1857c91c1c84SLorenzo Bianconi 
1858c91c1c84SLorenzo Bianconi 	return err;
1859c91c1c84SLorenzo Bianconi }
1860c91c1c84SLorenzo Bianconi 
186121345107SLorenzo Bianconi static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
186221345107SLorenzo Bianconi {
186321345107SLorenzo Bianconi 	const struct st_lsm6dsx_hw_ts_settings *ts_settings;
186421345107SLorenzo Bianconi 	int err, val;
186521345107SLorenzo Bianconi 
186621345107SLorenzo Bianconi 	ts_settings = &hw->settings->ts_settings;
186721345107SLorenzo Bianconi 	/* enable hw timestamp generation if necessary */
186821345107SLorenzo Bianconi 	if (ts_settings->timer_en.addr) {
186921345107SLorenzo Bianconi 		val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
187021345107SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap,
187121345107SLorenzo Bianconi 					 ts_settings->timer_en.addr,
187221345107SLorenzo Bianconi 					 ts_settings->timer_en.mask, val);
187321345107SLorenzo Bianconi 		if (err < 0)
187421345107SLorenzo Bianconi 			return err;
187521345107SLorenzo Bianconi 	}
187621345107SLorenzo Bianconi 
187721345107SLorenzo Bianconi 	/* enable high resolution for hw ts timer if necessary */
187821345107SLorenzo Bianconi 	if (ts_settings->hr_timer.addr) {
187921345107SLorenzo Bianconi 		val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
188021345107SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap,
188121345107SLorenzo Bianconi 					 ts_settings->hr_timer.addr,
188221345107SLorenzo Bianconi 					 ts_settings->hr_timer.mask, val);
188321345107SLorenzo Bianconi 		if (err < 0)
188421345107SLorenzo Bianconi 			return err;
188521345107SLorenzo Bianconi 	}
188621345107SLorenzo Bianconi 
188721345107SLorenzo Bianconi 	/* enable ts queueing in FIFO if necessary */
188821345107SLorenzo Bianconi 	if (ts_settings->fifo_en.addr) {
188921345107SLorenzo Bianconi 		val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
189021345107SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap,
189121345107SLorenzo Bianconi 					 ts_settings->fifo_en.addr,
189221345107SLorenzo Bianconi 					 ts_settings->fifo_en.mask, val);
189321345107SLorenzo Bianconi 		if (err < 0)
189421345107SLorenzo Bianconi 			return err;
189521345107SLorenzo Bianconi 	}
189621345107SLorenzo Bianconi 	return 0;
189721345107SLorenzo Bianconi }
189821345107SLorenzo Bianconi 
1899290a6ce1SLorenzo Bianconi static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
1900290a6ce1SLorenzo Bianconi {
19017e906103SLorenzo Bianconi 	const struct st_lsm6dsx_reg *reg;
1902290a6ce1SLorenzo Bianconi 	int err;
1903290a6ce1SLorenzo Bianconi 
190419435425SLorenzo Bianconi 	/* device sw reset */
190566b662a1SLorenzo Bianconi 	reg = &hw->settings->reset;
190666b662a1SLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
190766b662a1SLorenzo Bianconi 				 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1908290a6ce1SLorenzo Bianconi 	if (err < 0)
1909290a6ce1SLorenzo Bianconi 		return err;
1910290a6ce1SLorenzo Bianconi 
191119435425SLorenzo Bianconi 	msleep(50);
191219435425SLorenzo Bianconi 
191319435425SLorenzo Bianconi 	/* reload trimming parameter */
191466b662a1SLorenzo Bianconi 	reg = &hw->settings->boot;
191566b662a1SLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
191666b662a1SLorenzo Bianconi 				 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
191719435425SLorenzo Bianconi 	if (err < 0)
191819435425SLorenzo Bianconi 		return err;
191919435425SLorenzo Bianconi 
192019435425SLorenzo Bianconi 	msleep(50);
1921290a6ce1SLorenzo Bianconi 
1922290a6ce1SLorenzo Bianconi 	/* enable Block Data Update */
192366b662a1SLorenzo Bianconi 	reg = &hw->settings->bdu;
192466b662a1SLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
192566b662a1SLorenzo Bianconi 				 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1926290a6ce1SLorenzo Bianconi 	if (err < 0)
1927290a6ce1SLorenzo Bianconi 		return err;
1928290a6ce1SLorenzo Bianconi 
1929290a6ce1SLorenzo Bianconi 	/* enable FIFO watermak interrupt */
19307e906103SLorenzo Bianconi 	err = st_lsm6dsx_get_drdy_reg(hw, &reg);
1931290a6ce1SLorenzo Bianconi 	if (err < 0)
1932290a6ce1SLorenzo Bianconi 		return err;
1933290a6ce1SLorenzo Bianconi 
19347e906103SLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
19357e906103SLorenzo Bianconi 				 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
193621345107SLorenzo Bianconi 	if (err < 0)
193721345107SLorenzo Bianconi 		return err;
193821345107SLorenzo Bianconi 
19399db02d32SLorenzo Bianconi 	/* enable Latched interrupts for device events */
19407e906103SLorenzo Bianconi 	if (hw->settings->irq_config.lir.addr) {
19417e906103SLorenzo Bianconi 		reg = &hw->settings->irq_config.lir;
19427e906103SLorenzo Bianconi 		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
19437e906103SLorenzo Bianconi 					 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
19449db02d32SLorenzo Bianconi 		if (err < 0)
19459db02d32SLorenzo Bianconi 			return err;
194622ea5651SLorenzo Bianconi 
194722ea5651SLorenzo Bianconi 		/* enable clear on read for latched interrupts */
19487e906103SLorenzo Bianconi 		if (hw->settings->irq_config.clear_on_read.addr) {
19497e906103SLorenzo Bianconi 			reg = &hw->settings->irq_config.clear_on_read;
195022ea5651SLorenzo Bianconi 			err = regmap_update_bits(hw->regmap,
19517e906103SLorenzo Bianconi 					reg->addr, reg->mask,
19527e906103SLorenzo Bianconi 					ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
195322ea5651SLorenzo Bianconi 			if (err < 0)
195422ea5651SLorenzo Bianconi 				return err;
195522ea5651SLorenzo Bianconi 		}
19569db02d32SLorenzo Bianconi 	}
19579db02d32SLorenzo Bianconi 
1958960506edSLorenzo Bianconi 	/* enable drdy-mas if available */
1959960506edSLorenzo Bianconi 	if (hw->settings->drdy_mask.addr) {
1960960506edSLorenzo Bianconi 		reg = &hw->settings->drdy_mask;
1961960506edSLorenzo Bianconi 		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1962960506edSLorenzo Bianconi 					 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1963960506edSLorenzo Bianconi 		if (err < 0)
1964960506edSLorenzo Bianconi 			return err;
1965960506edSLorenzo Bianconi 	}
1966960506edSLorenzo Bianconi 
1967c91c1c84SLorenzo Bianconi 	err = st_lsm6dsx_init_shub(hw);
1968c91c1c84SLorenzo Bianconi 	if (err < 0)
1969c91c1c84SLorenzo Bianconi 		return err;
1970c91c1c84SLorenzo Bianconi 
197121345107SLorenzo Bianconi 	return st_lsm6dsx_init_hw_timer(hw);
1972290a6ce1SLorenzo Bianconi }
1973290a6ce1SLorenzo Bianconi 
1974290a6ce1SLorenzo Bianconi static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
1975510c0106SLorenzo Bianconi 					       enum st_lsm6dsx_sensor_id id,
1976510c0106SLorenzo Bianconi 					       const char *name)
1977290a6ce1SLorenzo Bianconi {
1978290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor;
1979290a6ce1SLorenzo Bianconi 	struct iio_dev *iio_dev;
1980290a6ce1SLorenzo Bianconi 
1981290a6ce1SLorenzo Bianconi 	iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
1982290a6ce1SLorenzo Bianconi 	if (!iio_dev)
1983290a6ce1SLorenzo Bianconi 		return NULL;
1984290a6ce1SLorenzo Bianconi 
1985290a6ce1SLorenzo Bianconi 	iio_dev->modes = INDIO_DIRECT_MODE;
1986290a6ce1SLorenzo Bianconi 	iio_dev->dev.parent = hw->dev;
1987290a6ce1SLorenzo Bianconi 	iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
1988f48bc49bSLorenzo Bianconi 	iio_dev->channels = hw->settings->channels[id].chan;
1989f48bc49bSLorenzo Bianconi 	iio_dev->num_channels = hw->settings->channels[id].len;
1990290a6ce1SLorenzo Bianconi 
1991290a6ce1SLorenzo Bianconi 	sensor = iio_priv(iio_dev);
1992290a6ce1SLorenzo Bianconi 	sensor->id = id;
1993290a6ce1SLorenzo Bianconi 	sensor->hw = hw;
199440dd7343SLorenzo Bianconi 	sensor->odr = hw->settings->odr_table[id].odr_avl[0].hz;
1995640aca3fSLorenzo Bianconi 	sensor->gain = hw->settings->fs_table[id].fs_avl[0].gain;
1996290a6ce1SLorenzo Bianconi 	sensor->watermark = 1;
1997290a6ce1SLorenzo Bianconi 
1998290a6ce1SLorenzo Bianconi 	switch (id) {
1999290a6ce1SLorenzo Bianconi 	case ST_LSM6DSX_ID_ACC:
2000290a6ce1SLorenzo Bianconi 		iio_dev->info = &st_lsm6dsx_acc_info;
2001510c0106SLorenzo Bianconi 		scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
2002510c0106SLorenzo Bianconi 			  name);
2003290a6ce1SLorenzo Bianconi 		break;
2004290a6ce1SLorenzo Bianconi 	case ST_LSM6DSX_ID_GYRO:
2005290a6ce1SLorenzo Bianconi 		iio_dev->info = &st_lsm6dsx_gyro_info;
2006510c0106SLorenzo Bianconi 		scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
2007510c0106SLorenzo Bianconi 			  name);
2008290a6ce1SLorenzo Bianconi 		break;
2009290a6ce1SLorenzo Bianconi 	default:
2010290a6ce1SLorenzo Bianconi 		return NULL;
2011290a6ce1SLorenzo Bianconi 	}
2012510c0106SLorenzo Bianconi 	iio_dev->name = sensor->name;
2013290a6ce1SLorenzo Bianconi 
2014290a6ce1SLorenzo Bianconi 	return iio_dev;
2015290a6ce1SLorenzo Bianconi }
2016290a6ce1SLorenzo Bianconi 
2017615bd378SLorenzo Bianconi static bool
2018615bd378SLorenzo Bianconi st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
20191aabad1fSSean Nyekjaer {
2020615bd378SLorenzo Bianconi 	const struct st_lsm6dsx_event_settings *event_settings;
2021615bd378SLorenzo Bianconi 	int err, data;
2022615bd378SLorenzo Bianconi 	s64 timestamp;
20231aabad1fSSean Nyekjaer 
2024615bd378SLorenzo Bianconi 	if (!hw->enable_event)
2025615bd378SLorenzo Bianconi 		return false;
2026615bd378SLorenzo Bianconi 
2027615bd378SLorenzo Bianconi 	event_settings = &hw->settings->event_settings;
2028615bd378SLorenzo Bianconi 	err = st_lsm6dsx_read_locked(hw, event_settings->wakeup_src_reg,
2029615bd378SLorenzo Bianconi 				     &data, sizeof(data));
2030615bd378SLorenzo Bianconi 	if (err < 0)
2031615bd378SLorenzo Bianconi 		return false;
2032615bd378SLorenzo Bianconi 
2033615bd378SLorenzo Bianconi 	timestamp = iio_get_time_ns(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
20341aabad1fSSean Nyekjaer 	if ((data & hw->settings->event_settings.wakeup_src_z_mask) &&
20351aabad1fSSean Nyekjaer 	    (hw->enable_event & BIT(IIO_MOD_Z)))
20361aabad1fSSean Nyekjaer 		iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
20371aabad1fSSean Nyekjaer 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
20381aabad1fSSean Nyekjaer 						  0,
20391aabad1fSSean Nyekjaer 						  IIO_MOD_Z,
20401aabad1fSSean Nyekjaer 						  IIO_EV_TYPE_THRESH,
20411aabad1fSSean Nyekjaer 						  IIO_EV_DIR_EITHER),
20421aabad1fSSean Nyekjaer 						  timestamp);
20431aabad1fSSean Nyekjaer 
20441aabad1fSSean Nyekjaer 	if ((data & hw->settings->event_settings.wakeup_src_y_mask) &&
20451aabad1fSSean Nyekjaer 	    (hw->enable_event & BIT(IIO_MOD_Y)))
20461aabad1fSSean Nyekjaer 		iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
20471aabad1fSSean Nyekjaer 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
20481aabad1fSSean Nyekjaer 						  0,
20491aabad1fSSean Nyekjaer 						  IIO_MOD_Y,
20501aabad1fSSean Nyekjaer 						  IIO_EV_TYPE_THRESH,
20511aabad1fSSean Nyekjaer 						  IIO_EV_DIR_EITHER),
20521aabad1fSSean Nyekjaer 						  timestamp);
20531aabad1fSSean Nyekjaer 
20541aabad1fSSean Nyekjaer 	if ((data & hw->settings->event_settings.wakeup_src_x_mask) &&
20551aabad1fSSean Nyekjaer 	    (hw->enable_event & BIT(IIO_MOD_X)))
20561aabad1fSSean Nyekjaer 		iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
20571aabad1fSSean Nyekjaer 			       IIO_MOD_EVENT_CODE(IIO_ACCEL,
20581aabad1fSSean Nyekjaer 						  0,
20591aabad1fSSean Nyekjaer 						  IIO_MOD_X,
20601aabad1fSSean Nyekjaer 						  IIO_EV_TYPE_THRESH,
20611aabad1fSSean Nyekjaer 						  IIO_EV_DIR_EITHER),
20621aabad1fSSean Nyekjaer 						  timestamp);
2063615bd378SLorenzo Bianconi 
2064615bd378SLorenzo Bianconi 	return data & event_settings->wakeup_src_status_mask;
20651aabad1fSSean Nyekjaer }
20661aabad1fSSean Nyekjaer 
20676ee6a368SSean Nyekjaer static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
20686ee6a368SSean Nyekjaer {
20696ee6a368SSean Nyekjaer 	struct st_lsm6dsx_hw *hw = private;
2070615bd378SLorenzo Bianconi 	bool event;
20716ee6a368SSean Nyekjaer 	int count;
20721aabad1fSSean Nyekjaer 
2073615bd378SLorenzo Bianconi 	event = st_lsm6dsx_report_motion_event(hw);
20746ee6a368SSean Nyekjaer 
2075a912ee4cSLorenzo Bianconi 	if (!hw->settings->fifo_ops.read_fifo)
2076a912ee4cSLorenzo Bianconi 		return event ? IRQ_HANDLED : IRQ_NONE;
2077a912ee4cSLorenzo Bianconi 
20786ee6a368SSean Nyekjaer 	mutex_lock(&hw->fifo_lock);
20796ee6a368SSean Nyekjaer 	count = hw->settings->fifo_ops.read_fifo(hw);
20806ee6a368SSean Nyekjaer 	mutex_unlock(&hw->fifo_lock);
20816ee6a368SSean Nyekjaer 
2082615bd378SLorenzo Bianconi 	return count || event ? IRQ_HANDLED : IRQ_NONE;
20836ee6a368SSean Nyekjaer }
20846ee6a368SSean Nyekjaer 
20856ee6a368SSean Nyekjaer static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
20866ee6a368SSean Nyekjaer {
20876ee6a368SSean Nyekjaer 	struct device_node *np = hw->dev->of_node;
208831fe8d4eSLorenzo Bianconi 	struct st_sensors_platform_data *pdata;
208931fe8d4eSLorenzo Bianconi 	const struct st_lsm6dsx_reg *reg;
20906ee6a368SSean Nyekjaer 	unsigned long irq_type;
20916ee6a368SSean Nyekjaer 	bool irq_active_low;
20926ee6a368SSean Nyekjaer 	int err;
20936ee6a368SSean Nyekjaer 
20946ee6a368SSean Nyekjaer 	irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
20956ee6a368SSean Nyekjaer 
20966ee6a368SSean Nyekjaer 	switch (irq_type) {
20976ee6a368SSean Nyekjaer 	case IRQF_TRIGGER_HIGH:
20986ee6a368SSean Nyekjaer 	case IRQF_TRIGGER_RISING:
20996ee6a368SSean Nyekjaer 		irq_active_low = false;
21006ee6a368SSean Nyekjaer 		break;
21016ee6a368SSean Nyekjaer 	case IRQF_TRIGGER_LOW:
21026ee6a368SSean Nyekjaer 	case IRQF_TRIGGER_FALLING:
21036ee6a368SSean Nyekjaer 		irq_active_low = true;
21046ee6a368SSean Nyekjaer 		break;
21056ee6a368SSean Nyekjaer 	default:
21066ee6a368SSean Nyekjaer 		dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
21076ee6a368SSean Nyekjaer 		return -EINVAL;
21086ee6a368SSean Nyekjaer 	}
21096ee6a368SSean Nyekjaer 
211031fe8d4eSLorenzo Bianconi 	reg = &hw->settings->irq_config.hla;
211131fe8d4eSLorenzo Bianconi 	err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
211231fe8d4eSLorenzo Bianconi 				 ST_LSM6DSX_SHIFT_VAL(irq_active_low,
211331fe8d4eSLorenzo Bianconi 						      reg->mask));
21146ee6a368SSean Nyekjaer 	if (err < 0)
21156ee6a368SSean Nyekjaer 		return err;
21166ee6a368SSean Nyekjaer 
21176ee6a368SSean Nyekjaer 	pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
21186ee6a368SSean Nyekjaer 	if ((np && of_property_read_bool(np, "drive-open-drain")) ||
21196ee6a368SSean Nyekjaer 	    (pdata && pdata->open_drain)) {
212031fe8d4eSLorenzo Bianconi 		reg = &hw->settings->irq_config.od;
212131fe8d4eSLorenzo Bianconi 		err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
212231fe8d4eSLorenzo Bianconi 					 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
21236ee6a368SSean Nyekjaer 		if (err < 0)
21246ee6a368SSean Nyekjaer 			return err;
21256ee6a368SSean Nyekjaer 
21266ee6a368SSean Nyekjaer 		irq_type |= IRQF_SHARED;
21276ee6a368SSean Nyekjaer 	}
21286ee6a368SSean Nyekjaer 
21296ee6a368SSean Nyekjaer 	err = devm_request_threaded_irq(hw->dev, hw->irq,
2130a3aa17d4SSean Nyekjaer 					NULL,
21316ee6a368SSean Nyekjaer 					st_lsm6dsx_handler_thread,
21326ee6a368SSean Nyekjaer 					irq_type | IRQF_ONESHOT,
21336ee6a368SSean Nyekjaer 					"lsm6dsx", hw);
21346ee6a368SSean Nyekjaer 	if (err) {
21356ee6a368SSean Nyekjaer 		dev_err(hw->dev, "failed to request trigger irq %d\n",
21366ee6a368SSean Nyekjaer 			hw->irq);
21376ee6a368SSean Nyekjaer 		return err;
21386ee6a368SSean Nyekjaer 	}
21396ee6a368SSean Nyekjaer 
21406ee6a368SSean Nyekjaer 	return 0;
21416ee6a368SSean Nyekjaer }
21426ee6a368SSean Nyekjaer 
214381956a93SLorenzo Bianconi int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
214451a8b707SLorenzo Bianconi 		     struct regmap *regmap)
2145290a6ce1SLorenzo Bianconi {
2146b7a73b33SLorenzo Bianconi 	struct st_sensors_platform_data *pdata = dev->platform_data;
2147c91c1c84SLorenzo Bianconi 	const struct st_lsm6dsx_shub_settings *hub_settings;
2148b7a73b33SLorenzo Bianconi 	struct device_node *np = dev->of_node;
2149290a6ce1SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw;
215081956a93SLorenzo Bianconi 	const char *name = NULL;
2151290a6ce1SLorenzo Bianconi 	int i, err;
2152290a6ce1SLorenzo Bianconi 
2153290a6ce1SLorenzo Bianconi 	hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
2154290a6ce1SLorenzo Bianconi 	if (!hw)
2155290a6ce1SLorenzo Bianconi 		return -ENOMEM;
2156290a6ce1SLorenzo Bianconi 
2157290a6ce1SLorenzo Bianconi 	dev_set_drvdata(dev, (void *)hw);
2158290a6ce1SLorenzo Bianconi 
2159290a6ce1SLorenzo Bianconi 	mutex_init(&hw->fifo_lock);
2160335eaedcSLorenzo Bianconi 	mutex_init(&hw->conf_lock);
2161739aff87SLorenzo Bianconi 	mutex_init(&hw->page_lock);
2162290a6ce1SLorenzo Bianconi 
216391a6b841SLorenzo Bianconi 	hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
216491a6b841SLorenzo Bianconi 	if (!hw->buff)
216591a6b841SLorenzo Bianconi 		return -ENOMEM;
216691a6b841SLorenzo Bianconi 
2167290a6ce1SLorenzo Bianconi 	hw->dev = dev;
2168290a6ce1SLorenzo Bianconi 	hw->irq = irq;
216951a8b707SLorenzo Bianconi 	hw->regmap = regmap;
2170290a6ce1SLorenzo Bianconi 
217181956a93SLorenzo Bianconi 	err = st_lsm6dsx_check_whoami(hw, hw_id, &name);
2172290a6ce1SLorenzo Bianconi 	if (err < 0)
2173290a6ce1SLorenzo Bianconi 		return err;
2174290a6ce1SLorenzo Bianconi 
21756ffb55e5SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
2176510c0106SLorenzo Bianconi 		hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
2177290a6ce1SLorenzo Bianconi 		if (!hw->iio_devs[i])
2178290a6ce1SLorenzo Bianconi 			return -ENOMEM;
2179290a6ce1SLorenzo Bianconi 	}
2180290a6ce1SLorenzo Bianconi 
2181290a6ce1SLorenzo Bianconi 	err = st_lsm6dsx_init_device(hw);
2182290a6ce1SLorenzo Bianconi 	if (err < 0)
2183290a6ce1SLorenzo Bianconi 		return err;
2184290a6ce1SLorenzo Bianconi 
2185c91c1c84SLorenzo Bianconi 	hub_settings = &hw->settings->shub_settings;
2186c91c1c84SLorenzo Bianconi 	if (hub_settings->master_en.addr) {
2187c91c1c84SLorenzo Bianconi 		err = st_lsm6dsx_shub_probe(hw, name);
2188c91c1c84SLorenzo Bianconi 		if (err < 0)
2189c91c1c84SLorenzo Bianconi 			return err;
2190c91c1c84SLorenzo Bianconi 	}
2191c91c1c84SLorenzo Bianconi 
2192290a6ce1SLorenzo Bianconi 	if (hw->irq > 0) {
21936ee6a368SSean Nyekjaer 		err = st_lsm6dsx_irq_setup(hw);
21946ee6a368SSean Nyekjaer 		if (err < 0)
21956ee6a368SSean Nyekjaer 			return err;
21966ee6a368SSean Nyekjaer 
2197290a6ce1SLorenzo Bianconi 		err = st_lsm6dsx_fifo_setup(hw);
2198290a6ce1SLorenzo Bianconi 		if (err < 0)
2199290a6ce1SLorenzo Bianconi 			return err;
2200290a6ce1SLorenzo Bianconi 	}
2201290a6ce1SLorenzo Bianconi 
2202290a6ce1SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
22036ffb55e5SLorenzo Bianconi 		if (!hw->iio_devs[i])
22046ffb55e5SLorenzo Bianconi 			continue;
22056ffb55e5SLorenzo Bianconi 
2206290a6ce1SLorenzo Bianconi 		err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
2207290a6ce1SLorenzo Bianconi 		if (err)
2208290a6ce1SLorenzo Bianconi 			return err;
2209290a6ce1SLorenzo Bianconi 	}
2210290a6ce1SLorenzo Bianconi 
2211b7a73b33SLorenzo Bianconi 	if ((np && of_property_read_bool(np, "wakeup-source")) ||
2212b7a73b33SLorenzo Bianconi 	    (pdata && pdata->wakeup_source))
22134c997dfaSSean Nyekjaer 		device_init_wakeup(dev, true);
22144c997dfaSSean Nyekjaer 
2215290a6ce1SLorenzo Bianconi 	return 0;
2216290a6ce1SLorenzo Bianconi }
2217290a6ce1SLorenzo Bianconi EXPORT_SYMBOL(st_lsm6dsx_probe);
2218290a6ce1SLorenzo Bianconi 
22193cec4850SLorenzo Bianconi static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
2220d3f77058SLorenzo Bianconi {
2221d3f77058SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2222d3f77058SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor;
2223d3f77058SLorenzo Bianconi 	int i, err = 0;
2224d3f77058SLorenzo Bianconi 
2225d3f77058SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
22266ffb55e5SLorenzo Bianconi 		if (!hw->iio_devs[i])
22276ffb55e5SLorenzo Bianconi 			continue;
22286ffb55e5SLorenzo Bianconi 
2229d3f77058SLorenzo Bianconi 		sensor = iio_priv(hw->iio_devs[i]);
2230d3f77058SLorenzo Bianconi 		if (!(hw->enable_mask & BIT(sensor->id)))
2231d3f77058SLorenzo Bianconi 			continue;
2232d3f77058SLorenzo Bianconi 
22334c997dfaSSean Nyekjaer 		if (device_may_wakeup(dev) &&
22344c997dfaSSean Nyekjaer 		    sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event) {
22354c997dfaSSean Nyekjaer 			/* Enable wake from IRQ */
22364c997dfaSSean Nyekjaer 			enable_irq_wake(hw->irq);
22374c997dfaSSean Nyekjaer 			continue;
22384c997dfaSSean Nyekjaer 		}
22394c997dfaSSean Nyekjaer 
2240bce0d57dSLorenzo Bianconi 		if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
2241bce0d57dSLorenzo Bianconi 		    sensor->id == ST_LSM6DSX_ID_EXT1 ||
2242bce0d57dSLorenzo Bianconi 		    sensor->id == ST_LSM6DSX_ID_EXT2)
2243bce0d57dSLorenzo Bianconi 			err = st_lsm6dsx_shub_set_enable(sensor, false);
2244bce0d57dSLorenzo Bianconi 		else
2245bce0d57dSLorenzo Bianconi 			err = st_lsm6dsx_sensor_set_enable(sensor, false);
2246d3f77058SLorenzo Bianconi 		if (err < 0)
2247d3f77058SLorenzo Bianconi 			return err;
2248bce0d57dSLorenzo Bianconi 
2249bce0d57dSLorenzo Bianconi 		hw->suspend_mask |= BIT(sensor->id);
2250d3f77058SLorenzo Bianconi 	}
2251d3f77058SLorenzo Bianconi 
2252d3f77058SLorenzo Bianconi 	if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
2253d3f77058SLorenzo Bianconi 		err = st_lsm6dsx_flush_fifo(hw);
2254d3f77058SLorenzo Bianconi 
2255d3f77058SLorenzo Bianconi 	return err;
2256d3f77058SLorenzo Bianconi }
2257d3f77058SLorenzo Bianconi 
22583cec4850SLorenzo Bianconi static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
2259d3f77058SLorenzo Bianconi {
2260d3f77058SLorenzo Bianconi 	struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2261d3f77058SLorenzo Bianconi 	struct st_lsm6dsx_sensor *sensor;
2262d3f77058SLorenzo Bianconi 	int i, err = 0;
2263d3f77058SLorenzo Bianconi 
2264d3f77058SLorenzo Bianconi 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
22656ffb55e5SLorenzo Bianconi 		if (!hw->iio_devs[i])
22666ffb55e5SLorenzo Bianconi 			continue;
22676ffb55e5SLorenzo Bianconi 
2268d3f77058SLorenzo Bianconi 		sensor = iio_priv(hw->iio_devs[i]);
22694c997dfaSSean Nyekjaer 		if (device_may_wakeup(dev) &&
22704c997dfaSSean Nyekjaer 		    sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event)
22714c997dfaSSean Nyekjaer 			disable_irq_wake(hw->irq);
22724c997dfaSSean Nyekjaer 
2273bce0d57dSLorenzo Bianconi 		if (!(hw->suspend_mask & BIT(sensor->id)))
2274d3f77058SLorenzo Bianconi 			continue;
2275d3f77058SLorenzo Bianconi 
2276bce0d57dSLorenzo Bianconi 		if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
2277bce0d57dSLorenzo Bianconi 		    sensor->id == ST_LSM6DSX_ID_EXT1 ||
2278bce0d57dSLorenzo Bianconi 		    sensor->id == ST_LSM6DSX_ID_EXT2)
2279bce0d57dSLorenzo Bianconi 			err = st_lsm6dsx_shub_set_enable(sensor, true);
2280bce0d57dSLorenzo Bianconi 		else
2281bce0d57dSLorenzo Bianconi 			err = st_lsm6dsx_sensor_set_enable(sensor, true);
2282d3f77058SLorenzo Bianconi 		if (err < 0)
2283d3f77058SLorenzo Bianconi 			return err;
2284bce0d57dSLorenzo Bianconi 
2285bce0d57dSLorenzo Bianconi 		hw->suspend_mask &= ~BIT(sensor->id);
2286d3f77058SLorenzo Bianconi 	}
2287d3f77058SLorenzo Bianconi 
2288d3f77058SLorenzo Bianconi 	if (hw->enable_mask)
2289d3f77058SLorenzo Bianconi 		err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
2290d3f77058SLorenzo Bianconi 
2291d3f77058SLorenzo Bianconi 	return err;
2292d3f77058SLorenzo Bianconi }
2293d3f77058SLorenzo Bianconi 
2294d3f77058SLorenzo Bianconi const struct dev_pm_ops st_lsm6dsx_pm_ops = {
2295d3f77058SLorenzo Bianconi 	SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
2296d3f77058SLorenzo Bianconi };
2297d3f77058SLorenzo Bianconi EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
2298d3f77058SLorenzo Bianconi 
2299290a6ce1SLorenzo Bianconi MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
2300290a6ce1SLorenzo Bianconi MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
2301290a6ce1SLorenzo Bianconi MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
2302290a6ce1SLorenzo Bianconi MODULE_LICENSE("GPL v2");
2303