1 // SPDX-License-Identifier: GPL-2.0-only
3 * STMicroelectronics st_lsm6dsx sensor driver
5 * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer
6 * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial
7 * interface standard output.
8 * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale
9 * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of
10 * +-125/+-245/+-500/+-1000/+-2000 dps
11 * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer
12 * allowing dynamic batching of sensor data.
13 * LSM9DSx series is similar but includes an additional magnetometer, handled
14 * by a different driver.
18 * - Accelerometer/Gyroscope supported ODR [Hz]: 12.5, 26, 52, 104, 208, 416
19 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
20 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
23 * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC/LSM6DS3TR-C:
24 * - Accelerometer/Gyroscope supported ODR [Hz]: 12.5, 26, 52, 104, 208, 416
25 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
26 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
29 * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR/ISM330DHCX/LSM6DST/LSM6DSOP:
30 * - Accelerometer/Gyroscope supported ODR [Hz]: 12.5, 26, 52, 104, 208, 416,
32 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
33 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
37 * - Accelerometer supported ODR [Hz]: 10, 50, 119, 238, 476, 952
38 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
39 * - Gyroscope supported ODR [Hz]: 15, 60, 119, 238, 476, 952
40 * - Gyroscope supported full-scale [dps]: +-245/+-500/+-2000
43 * Copyright 2016 STMicroelectronics Inc.
45 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
46 * Denis Ciocca <denis.ciocca@st.com>
49 #include <linux/kernel.h>
50 #include <linux/module.h>
51 #include <linux/delay.h>
52 #include <linux/iio/events.h>
53 #include <linux/iio/iio.h>
54 #include <linux/iio/sysfs.h>
55 #include <linux/interrupt.h>
56 #include <linux/irq.h>
58 #include <linux/property.h>
59 #include <linux/regmap.h>
60 #include <linux/bitfield.h>
62 #include <linux/platform_data/st_sensors_pdata.h>
64 #include "st_lsm6dsx.h"
66 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
68 #define ST_LSM6DSX_TS_SENSITIVITY 25000UL /* 25us */
70 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
71 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
72 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
73 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
74 IIO_CHAN_SOFT_TIMESTAMP(3),
77 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
78 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
79 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
80 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x26, IIO_MOD_Z, 2),
81 IIO_CHAN_SOFT_TIMESTAMP(3),
84 static const struct iio_chan_spec st_lsm6ds0_gyro_channels[] = {
85 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x18, IIO_MOD_X, 0),
86 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1a, IIO_MOD_Y, 1),
87 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1c, IIO_MOD_Z, 2),
88 IIO_CHAN_SOFT_TIMESTAMP(3),
91 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
108 .hw_id = ST_LSM9DS1_ID,
109 .name = ST_LSM9DS1_DEV_NAME,
112 .hw_id = ST_LSM6DS0_ID,
113 .name = ST_LSM6DS0_DEV_NAME,
118 [ST_LSM6DSX_ID_ACC] = {
119 .chan = st_lsm6dsx_acc_channels,
120 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
122 [ST_LSM6DSX_ID_GYRO] = {
123 .chan = st_lsm6ds0_gyro_channels,
124 .len = ARRAY_SIZE(st_lsm6ds0_gyro_channels),
128 [ST_LSM6DSX_ID_ACC] = {
131 .mask = GENMASK(7, 5),
133 .odr_avl[0] = { 10000, 0x01 },
134 .odr_avl[1] = { 50000, 0x02 },
135 .odr_avl[2] = { 119000, 0x03 },
136 .odr_avl[3] = { 238000, 0x04 },
137 .odr_avl[4] = { 476000, 0x05 },
138 .odr_avl[5] = { 952000, 0x06 },
141 [ST_LSM6DSX_ID_GYRO] = {
144 .mask = GENMASK(7, 5),
146 .odr_avl[0] = { 14900, 0x01 },
147 .odr_avl[1] = { 59500, 0x02 },
148 .odr_avl[2] = { 119000, 0x03 },
149 .odr_avl[3] = { 238000, 0x04 },
150 .odr_avl[4] = { 476000, 0x05 },
151 .odr_avl[5] = { 952000, 0x06 },
156 [ST_LSM6DSX_ID_ACC] = {
159 .mask = GENMASK(4, 3),
161 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
162 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
163 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
164 .fs_avl[3] = { IIO_G_TO_M_S_2(732000), 0x1 },
167 [ST_LSM6DSX_ID_GYRO] = {
170 .mask = GENMASK(4, 3),
173 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
174 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
175 .fs_avl[2] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
211 .max_fifo_size = 1365,
214 .hw_id = ST_LSM6DS3_ID,
215 .name = ST_LSM6DS3_DEV_NAME,
220 [ST_LSM6DSX_ID_ACC] = {
221 .chan = st_lsm6dsx_acc_channels,
222 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
224 [ST_LSM6DSX_ID_GYRO] = {
225 .chan = st_lsm6dsx_gyro_channels,
226 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
230 [ST_LSM6DSX_ID_ACC] = {
233 .mask = GENMASK(7, 4),
235 .odr_avl[0] = { 12500, 0x01 },
236 .odr_avl[1] = { 26000, 0x02 },
237 .odr_avl[2] = { 52000, 0x03 },
238 .odr_avl[3] = { 104000, 0x04 },
239 .odr_avl[4] = { 208000, 0x05 },
240 .odr_avl[5] = { 416000, 0x06 },
243 [ST_LSM6DSX_ID_GYRO] = {
246 .mask = GENMASK(7, 4),
248 .odr_avl[0] = { 12500, 0x01 },
249 .odr_avl[1] = { 26000, 0x02 },
250 .odr_avl[2] = { 52000, 0x03 },
251 .odr_avl[3] = { 104000, 0x04 },
252 .odr_avl[4] = { 208000, 0x05 },
253 .odr_avl[5] = { 416000, 0x06 },
258 [ST_LSM6DSX_ID_ACC] = {
261 .mask = GENMASK(3, 2),
263 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
264 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
265 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
266 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
269 [ST_LSM6DSX_ID_GYRO] = {
272 .mask = GENMASK(3, 2),
274 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
275 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
276 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
277 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
312 [ST_LSM6DSX_ID_ACC] = {
314 .mask = GENMASK(2, 0),
316 [ST_LSM6DSX_ID_GYRO] = {
318 .mask = GENMASK(5, 3),
322 .update_fifo = st_lsm6dsx_update_fifo,
323 .read_fifo = st_lsm6dsx_read_fifo,
326 .mask = GENMASK(11, 0),
330 .mask = GENMASK(11, 0),
332 .th_wl = 3, /* 1LSB = 2B */
349 .mask = GENMASK(5, 3),
355 .mask = GENMASK(5, 0),
357 .wakeup_src_reg = 0x1b,
358 .wakeup_src_status_mask = BIT(3),
359 .wakeup_src_z_mask = BIT(0),
360 .wakeup_src_y_mask = BIT(1),
361 .wakeup_src_x_mask = BIT(2),
377 .max_fifo_size = 682,
380 .hw_id = ST_LSM6DS3H_ID,
381 .name = ST_LSM6DS3H_DEV_NAME,
386 [ST_LSM6DSX_ID_ACC] = {
387 .chan = st_lsm6dsx_acc_channels,
388 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
390 [ST_LSM6DSX_ID_GYRO] = {
391 .chan = st_lsm6dsx_gyro_channels,
392 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
396 [ST_LSM6DSX_ID_ACC] = {
399 .mask = GENMASK(7, 4),
401 .odr_avl[0] = { 12500, 0x01 },
402 .odr_avl[1] = { 26000, 0x02 },
403 .odr_avl[2] = { 52000, 0x03 },
404 .odr_avl[3] = { 104000, 0x04 },
405 .odr_avl[4] = { 208000, 0x05 },
406 .odr_avl[5] = { 416000, 0x06 },
409 [ST_LSM6DSX_ID_GYRO] = {
412 .mask = GENMASK(7, 4),
414 .odr_avl[0] = { 12500, 0x01 },
415 .odr_avl[1] = { 26000, 0x02 },
416 .odr_avl[2] = { 52000, 0x03 },
417 .odr_avl[3] = { 104000, 0x04 },
418 .odr_avl[4] = { 208000, 0x05 },
419 .odr_avl[5] = { 416000, 0x06 },
424 [ST_LSM6DSX_ID_ACC] = {
427 .mask = GENMASK(3, 2),
429 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
430 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
431 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
432 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
435 [ST_LSM6DSX_ID_GYRO] = {
438 .mask = GENMASK(3, 2),
440 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
441 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
442 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
443 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
478 [ST_LSM6DSX_ID_ACC] = {
480 .mask = GENMASK(2, 0),
482 [ST_LSM6DSX_ID_GYRO] = {
484 .mask = GENMASK(5, 3),
488 .update_fifo = st_lsm6dsx_update_fifo,
489 .read_fifo = st_lsm6dsx_read_fifo,
492 .mask = GENMASK(11, 0),
496 .mask = GENMASK(11, 0),
498 .th_wl = 3, /* 1LSB = 2B */
515 .mask = GENMASK(5, 3),
521 .mask = GENMASK(5, 0),
523 .wakeup_src_reg = 0x1b,
524 .wakeup_src_status_mask = BIT(3),
525 .wakeup_src_z_mask = BIT(0),
526 .wakeup_src_y_mask = BIT(1),
527 .wakeup_src_x_mask = BIT(2),
543 .max_fifo_size = 682,
546 .hw_id = ST_LSM6DSL_ID,
547 .name = ST_LSM6DSL_DEV_NAME,
550 .hw_id = ST_LSM6DSM_ID,
551 .name = ST_LSM6DSM_DEV_NAME,
554 .hw_id = ST_ISM330DLC_ID,
555 .name = ST_ISM330DLC_DEV_NAME,
558 .hw_id = ST_LSM6DS3TRC_ID,
559 .name = ST_LSM6DS3TRC_DEV_NAME,
564 [ST_LSM6DSX_ID_ACC] = {
565 .chan = st_lsm6dsx_acc_channels,
566 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
568 [ST_LSM6DSX_ID_GYRO] = {
569 .chan = st_lsm6dsx_gyro_channels,
570 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
574 [ST_LSM6DSX_ID_ACC] = {
577 .mask = GENMASK(7, 4),
579 .odr_avl[0] = { 12500, 0x01 },
580 .odr_avl[1] = { 26000, 0x02 },
581 .odr_avl[2] = { 52000, 0x03 },
582 .odr_avl[3] = { 104000, 0x04 },
583 .odr_avl[4] = { 208000, 0x05 },
584 .odr_avl[5] = { 416000, 0x06 },
587 [ST_LSM6DSX_ID_GYRO] = {
590 .mask = GENMASK(7, 4),
592 .odr_avl[0] = { 12500, 0x01 },
593 .odr_avl[1] = { 26000, 0x02 },
594 .odr_avl[2] = { 52000, 0x03 },
595 .odr_avl[3] = { 104000, 0x04 },
596 .odr_avl[4] = { 208000, 0x05 },
597 .odr_avl[5] = { 416000, 0x06 },
602 [ST_LSM6DSX_ID_ACC] = {
605 .mask = GENMASK(3, 2),
607 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
608 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
609 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
610 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
613 [ST_LSM6DSX_ID_GYRO] = {
616 .mask = GENMASK(3, 2),
618 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
619 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
620 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
621 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
656 [ST_LSM6DSX_ID_ACC] = {
658 .mask = GENMASK(2, 0),
660 [ST_LSM6DSX_ID_GYRO] = {
662 .mask = GENMASK(5, 3),
664 [ST_LSM6DSX_ID_EXT0] = {
666 .mask = GENMASK(2, 0),
670 .update_fifo = st_lsm6dsx_update_fifo,
671 .read_fifo = st_lsm6dsx_read_fifo,
674 .mask = GENMASK(10, 0),
678 .mask = GENMASK(10, 0),
680 .th_wl = 3, /* 1LSB = 2B */
697 .mask = GENMASK(5, 3),
715 .mask = GENMASK(5, 4),
730 .dw_slv0_addr = 0x0e,
740 .mask = GENMASK(5, 0),
742 .wakeup_src_reg = 0x1b,
743 .wakeup_src_status_mask = BIT(3),
744 .wakeup_src_z_mask = BIT(0),
745 .wakeup_src_y_mask = BIT(1),
746 .wakeup_src_x_mask = BIT(2),
762 .max_fifo_size = 512,
765 .hw_id = ST_LSM6DSR_ID,
766 .name = ST_LSM6DSR_DEV_NAME,
769 .hw_id = ST_ISM330DHCX_ID,
770 .name = ST_ISM330DHCX_DEV_NAME,
773 .hw_id = ST_LSM6DSRX_ID,
774 .name = ST_LSM6DSRX_DEV_NAME,
777 .hw_id = ST_LSM6DSO_ID,
778 .name = ST_LSM6DSO_DEV_NAME,
781 .hw_id = ST_LSM6DSOX_ID,
782 .name = ST_LSM6DSOX_DEV_NAME,
785 .hw_id = ST_LSM6DST_ID,
786 .name = ST_LSM6DST_DEV_NAME,
791 [ST_LSM6DSX_ID_ACC] = {
792 .chan = st_lsm6dsx_acc_channels,
793 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
795 [ST_LSM6DSX_ID_GYRO] = {
796 .chan = st_lsm6dsx_gyro_channels,
797 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
805 [ST_LSM6DSX_ID_ACC] = {
808 .mask = GENMASK(7, 4),
810 .odr_avl[0] = { 12500, 0x01 },
811 .odr_avl[1] = { 26000, 0x02 },
812 .odr_avl[2] = { 52000, 0x03 },
813 .odr_avl[3] = { 104000, 0x04 },
814 .odr_avl[4] = { 208000, 0x05 },
815 .odr_avl[5] = { 416000, 0x06 },
816 .odr_avl[6] = { 833000, 0x07 },
819 [ST_LSM6DSX_ID_GYRO] = {
822 .mask = GENMASK(7, 4),
824 .odr_avl[0] = { 12500, 0x01 },
825 .odr_avl[1] = { 26000, 0x02 },
826 .odr_avl[2] = { 52000, 0x03 },
827 .odr_avl[3] = { 104000, 0x04 },
828 .odr_avl[4] = { 208000, 0x05 },
829 .odr_avl[5] = { 416000, 0x06 },
830 .odr_avl[6] = { 833000, 0x07 },
835 [ST_LSM6DSX_ID_ACC] = {
838 .mask = GENMASK(3, 2),
840 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
841 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
842 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
843 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
846 [ST_LSM6DSX_ID_GYRO] = {
849 .mask = GENMASK(3, 2),
851 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
852 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
853 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
854 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
893 [ST_LSM6DSX_ID_ACC] = {
895 .mask = GENMASK(3, 0),
897 [ST_LSM6DSX_ID_GYRO] = {
899 .mask = GENMASK(7, 4),
903 .update_fifo = st_lsm6dsx_update_fifo,
904 .read_fifo = st_lsm6dsx_read_tagged_fifo,
907 .mask = GENMASK(8, 0),
911 .mask = GENMASK(9, 0),
922 .mask = GENMASK(7, 6),
943 .mask = GENMASK(1, 0),
955 .dw_slv0_addr = 0x21,
965 .mask = GENMASK(5, 0),
967 .wakeup_src_reg = 0x1b,
968 .wakeup_src_status_mask = BIT(3),
969 .wakeup_src_z_mask = BIT(0),
970 .wakeup_src_y_mask = BIT(1),
971 .wakeup_src_x_mask = BIT(2),
987 .max_fifo_size = 512,
990 .hw_id = ST_ASM330LHH_ID,
991 .name = ST_ASM330LHH_DEV_NAME,
994 .hw_id = ST_LSM6DSOP_ID,
995 .name = ST_LSM6DSOP_DEV_NAME,
1000 [ST_LSM6DSX_ID_ACC] = {
1001 .chan = st_lsm6dsx_acc_channels,
1002 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
1004 [ST_LSM6DSX_ID_GYRO] = {
1005 .chan = st_lsm6dsx_gyro_channels,
1006 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
1014 [ST_LSM6DSX_ID_ACC] = {
1017 .mask = GENMASK(7, 4),
1019 .odr_avl[0] = { 12500, 0x01 },
1020 .odr_avl[1] = { 26000, 0x02 },
1021 .odr_avl[2] = { 52000, 0x03 },
1022 .odr_avl[3] = { 104000, 0x04 },
1023 .odr_avl[4] = { 208000, 0x05 },
1024 .odr_avl[5] = { 416000, 0x06 },
1025 .odr_avl[6] = { 833000, 0x07 },
1028 [ST_LSM6DSX_ID_GYRO] = {
1031 .mask = GENMASK(7, 4),
1033 .odr_avl[0] = { 12500, 0x01 },
1034 .odr_avl[1] = { 26000, 0x02 },
1035 .odr_avl[2] = { 52000, 0x03 },
1036 .odr_avl[3] = { 104000, 0x04 },
1037 .odr_avl[4] = { 208000, 0x05 },
1038 .odr_avl[5] = { 416000, 0x06 },
1039 .odr_avl[6] = { 833000, 0x07 },
1044 [ST_LSM6DSX_ID_ACC] = {
1047 .mask = GENMASK(3, 2),
1049 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
1050 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
1051 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
1052 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
1055 [ST_LSM6DSX_ID_GYRO] = {
1058 .mask = GENMASK(3, 2),
1060 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
1061 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
1062 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
1063 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
1102 [ST_LSM6DSX_ID_ACC] = {
1104 .mask = GENMASK(3, 0),
1106 [ST_LSM6DSX_ID_GYRO] = {
1108 .mask = GENMASK(7, 4),
1112 .update_fifo = st_lsm6dsx_update_fifo,
1113 .read_fifo = st_lsm6dsx_read_tagged_fifo,
1116 .mask = GENMASK(8, 0),
1120 .mask = GENMASK(9, 0),
1131 .mask = GENMASK(7, 6),
1142 .mask = GENMASK(5, 0),
1144 .wakeup_src_reg = 0x1b,
1145 .wakeup_src_status_mask = BIT(3),
1146 .wakeup_src_z_mask = BIT(0),
1147 .wakeup_src_y_mask = BIT(1),
1148 .wakeup_src_x_mask = BIT(2),
1153 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
1155 const struct st_lsm6dsx_shub_settings *hub_settings;
1159 hub_settings = &hw->settings->shub_settings;
1160 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
1161 err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
1162 hub_settings->page_mux.mask, data);
1163 usleep_range(100, 150);
1168 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
1171 int err, i, j, data;
1173 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
1174 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
1175 if (st_lsm6dsx_sensor_settings[i].id[j].name &&
1176 id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
1179 if (j < ST_LSM6DSX_MAX_ID)
1183 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
1184 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
1188 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
1190 dev_err(hw->dev, "failed to read whoami register\n");
1194 if (data != st_lsm6dsx_sensor_settings[i].id[j].wai) {
1195 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
1199 *name = st_lsm6dsx_sensor_settings[i].id[j].name;
1200 hw->settings = &st_lsm6dsx_sensor_settings[i];
1205 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
1208 const struct st_lsm6dsx_fs_table_entry *fs_table;
1212 fs_table = &sensor->hw->settings->fs_table[sensor->id];
1213 for (i = 0; i < fs_table->fs_len; i++) {
1214 if (fs_table->fs_avl[i].gain == gain)
1218 if (i == fs_table->fs_len)
1221 data = ST_LSM6DSX_SHIFT_VAL(fs_table->fs_avl[i].val,
1222 fs_table->reg.mask);
1223 err = st_lsm6dsx_update_bits_locked(sensor->hw, fs_table->reg.addr,
1224 fs_table->reg.mask, data);
1228 sensor->gain = gain;
1233 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val)
1235 const struct st_lsm6dsx_odr_table_entry *odr_table;
1238 odr_table = &sensor->hw->settings->odr_table[sensor->id];
1239 for (i = 0; i < odr_table->odr_len; i++) {
1241 * ext devices can run at different odr respect to
1244 if (odr_table->odr_avl[i].milli_hz >= odr)
1248 if (i == odr_table->odr_len)
1251 *val = odr_table->odr_avl[i].val;
1252 return odr_table->odr_avl[i].milli_hz;
1256 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u32 odr,
1257 enum st_lsm6dsx_sensor_id id)
1259 struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
1262 if (hw->enable_mask & BIT(id))
1263 return max_t(u32, ref->odr, odr);
1267 return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
1272 st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
1274 struct st_lsm6dsx_sensor *ref_sensor = sensor;
1275 struct st_lsm6dsx_hw *hw = sensor->hw;
1276 const struct st_lsm6dsx_reg *reg;
1281 switch (sensor->id) {
1282 case ST_LSM6DSX_ID_GYRO:
1284 case ST_LSM6DSX_ID_EXT0:
1285 case ST_LSM6DSX_ID_EXT1:
1286 case ST_LSM6DSX_ID_EXT2:
1287 case ST_LSM6DSX_ID_ACC: {
1292 * i2c embedded controller relies on the accelerometer sensor as
1293 * bus read/write trigger so we need to enable accel device
1294 * at odr = max(accel_odr, ext_odr) in order to properly
1295 * communicate with i2c slave devices
1297 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
1298 for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
1299 if (!hw->iio_devs[i] || i == sensor->id)
1302 odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
1304 /* device already configured */
1309 default: /* should never occur */
1314 err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
1319 reg = &hw->settings->odr_table[ref_sensor->id].reg;
1320 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1321 return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
1325 __st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
1328 struct st_lsm6dsx_hw *hw = sensor->hw;
1329 u32 odr = enable ? sensor->odr : 0;
1332 err = st_lsm6dsx_set_odr(sensor, odr);
1337 hw->enable_mask |= BIT(sensor->id);
1339 hw->enable_mask &= ~BIT(sensor->id);
1345 st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor, bool enable)
1347 struct st_lsm6dsx_hw *hw = sensor->hw;
1349 if (sensor->id == ST_LSM6DSX_ID_GYRO || enable)
1352 return hw->enable_event;
1355 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
1358 if (st_lsm6dsx_check_events(sensor, enable))
1361 return __st_lsm6dsx_sensor_set_enable(sensor, enable);
1364 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
1367 struct st_lsm6dsx_hw *hw = sensor->hw;
1371 err = st_lsm6dsx_sensor_set_enable(sensor, true);
1376 * we need to wait for sensor settling time before
1377 * reading data in order to avoid corrupted samples
1379 delay = 1000000000 / sensor->odr;
1380 usleep_range(3 * delay, 4 * delay);
1382 err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
1386 if (!hw->enable_event) {
1387 err = st_lsm6dsx_sensor_set_enable(sensor, false);
1392 *val = (s16)le16_to_cpu(data);
1397 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
1398 struct iio_chan_spec const *ch,
1399 int *val, int *val2, long mask)
1401 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1405 case IIO_CHAN_INFO_RAW:
1406 ret = iio_device_claim_direct_mode(iio_dev);
1410 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
1411 iio_device_release_direct_mode(iio_dev);
1413 case IIO_CHAN_INFO_SAMP_FREQ:
1414 *val = sensor->odr / 1000;
1415 *val2 = (sensor->odr % 1000) * 1000;
1416 ret = IIO_VAL_INT_PLUS_MICRO;
1418 case IIO_CHAN_INFO_SCALE:
1420 *val2 = sensor->gain;
1421 ret = IIO_VAL_INT_PLUS_NANO;
1431 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
1432 struct iio_chan_spec const *chan,
1433 int val, int val2, long mask)
1435 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1438 err = iio_device_claim_direct_mode(iio_dev);
1443 case IIO_CHAN_INFO_SCALE:
1444 err = st_lsm6dsx_set_full_scale(sensor, val2);
1446 case IIO_CHAN_INFO_SAMP_FREQ: {
1449 val = val * 1000 + val2 / 1000;
1450 val = st_lsm6dsx_check_odr(sensor, val, &data);
1462 iio_device_release_direct_mode(iio_dev);
1467 static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw *hw, int state)
1469 const struct st_lsm6dsx_reg *reg;
1473 if (!hw->settings->irq_config.irq1_func.addr)
1476 reg = &hw->settings->event_settings.enable_reg;
1478 data = ST_LSM6DSX_SHIFT_VAL(state, reg->mask);
1479 err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
1485 /* Enable wakeup interrupt */
1486 data = ST_LSM6DSX_SHIFT_VAL(state, hw->irq_routing->mask);
1487 return st_lsm6dsx_update_bits_locked(hw, hw->irq_routing->addr,
1488 hw->irq_routing->mask, data);
1491 static int st_lsm6dsx_read_event(struct iio_dev *iio_dev,
1492 const struct iio_chan_spec *chan,
1493 enum iio_event_type type,
1494 enum iio_event_direction dir,
1495 enum iio_event_info info,
1496 int *val, int *val2)
1498 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1499 struct st_lsm6dsx_hw *hw = sensor->hw;
1501 if (type != IIO_EV_TYPE_THRESH)
1505 *val = hw->event_threshold;
1511 st_lsm6dsx_write_event(struct iio_dev *iio_dev,
1512 const struct iio_chan_spec *chan,
1513 enum iio_event_type type,
1514 enum iio_event_direction dir,
1515 enum iio_event_info info,
1518 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1519 struct st_lsm6dsx_hw *hw = sensor->hw;
1520 const struct st_lsm6dsx_reg *reg;
1524 if (type != IIO_EV_TYPE_THRESH)
1527 if (val < 0 || val > 31)
1530 reg = &hw->settings->event_settings.wakeup_reg;
1531 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1532 err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
1537 hw->event_threshold = val;
1543 st_lsm6dsx_read_event_config(struct iio_dev *iio_dev,
1544 const struct iio_chan_spec *chan,
1545 enum iio_event_type type,
1546 enum iio_event_direction dir)
1548 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1549 struct st_lsm6dsx_hw *hw = sensor->hw;
1551 if (type != IIO_EV_TYPE_THRESH)
1554 return !!(hw->enable_event & BIT(chan->channel2));
1558 st_lsm6dsx_write_event_config(struct iio_dev *iio_dev,
1559 const struct iio_chan_spec *chan,
1560 enum iio_event_type type,
1561 enum iio_event_direction dir, int state)
1563 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1564 struct st_lsm6dsx_hw *hw = sensor->hw;
1568 if (type != IIO_EV_TYPE_THRESH)
1572 enable_event = hw->enable_event | BIT(chan->channel2);
1574 /* do not enable events if they are already enabled */
1575 if (hw->enable_event)
1578 enable_event = hw->enable_event & ~BIT(chan->channel2);
1580 /* only turn off sensor if no events is enabled */
1585 /* stop here if no changes have been made */
1586 if (hw->enable_event == enable_event)
1589 err = st_lsm6dsx_event_setup(hw, state);
1593 mutex_lock(&hw->conf_lock);
1594 if (enable_event || !(hw->fifo_mask & BIT(sensor->id)))
1595 err = __st_lsm6dsx_sensor_set_enable(sensor, state);
1596 mutex_unlock(&hw->conf_lock);
1601 hw->enable_event = enable_event;
1606 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
1608 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1609 struct st_lsm6dsx_hw *hw = sensor->hw;
1612 if (val < 1 || val > hw->settings->max_fifo_size)
1615 mutex_lock(&hw->conf_lock);
1617 err = st_lsm6dsx_update_watermark(sensor, val);
1619 mutex_unlock(&hw->conf_lock);
1624 sensor->watermark = val;
1630 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
1631 struct device_attribute *attr,
1634 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
1635 const struct st_lsm6dsx_odr_table_entry *odr_table;
1638 odr_table = &sensor->hw->settings->odr_table[sensor->id];
1639 for (i = 0; i < odr_table->odr_len; i++)
1640 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%03d ",
1641 odr_table->odr_avl[i].milli_hz / 1000,
1642 odr_table->odr_avl[i].milli_hz % 1000);
1643 buf[len - 1] = '\n';
1648 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
1649 struct device_attribute *attr,
1652 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
1653 const struct st_lsm6dsx_fs_table_entry *fs_table;
1654 struct st_lsm6dsx_hw *hw = sensor->hw;
1657 fs_table = &hw->settings->fs_table[sensor->id];
1658 for (i = 0; i < fs_table->fs_len; i++)
1659 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%09u ",
1660 fs_table->fs_avl[i].gain);
1661 buf[len - 1] = '\n';
1666 static int st_lsm6dsx_write_raw_get_fmt(struct iio_dev *indio_dev,
1667 struct iio_chan_spec const *chan,
1671 case IIO_CHAN_INFO_SCALE:
1672 switch (chan->type) {
1675 return IIO_VAL_INT_PLUS_NANO;
1677 return IIO_VAL_INT_PLUS_MICRO;
1680 return IIO_VAL_INT_PLUS_MICRO;
1684 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
1685 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
1686 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1687 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
1688 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
1690 static struct attribute *st_lsm6dsx_acc_attributes[] = {
1691 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1692 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
1696 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
1697 .attrs = st_lsm6dsx_acc_attributes,
1700 static const struct iio_info st_lsm6dsx_acc_info = {
1701 .attrs = &st_lsm6dsx_acc_attribute_group,
1702 .read_raw = st_lsm6dsx_read_raw,
1703 .write_raw = st_lsm6dsx_write_raw,
1704 .read_event_value = st_lsm6dsx_read_event,
1705 .write_event_value = st_lsm6dsx_write_event,
1706 .read_event_config = st_lsm6dsx_read_event_config,
1707 .write_event_config = st_lsm6dsx_write_event_config,
1708 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1709 .write_raw_get_fmt = st_lsm6dsx_write_raw_get_fmt,
1712 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
1713 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1714 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
1718 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
1719 .attrs = st_lsm6dsx_gyro_attributes,
1722 static const struct iio_info st_lsm6dsx_gyro_info = {
1723 .attrs = &st_lsm6dsx_gyro_attribute_group,
1724 .read_raw = st_lsm6dsx_read_raw,
1725 .write_raw = st_lsm6dsx_write_raw,
1726 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
1727 .write_raw_get_fmt = st_lsm6dsx_write_raw_get_fmt,
1730 static int st_lsm6dsx_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
1732 struct device *dev = hw->dev;
1734 if (!dev_fwnode(dev))
1737 return device_property_read_u32(dev, "st,drdy-int-pin", drdy_pin);
1741 st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw,
1742 const struct st_lsm6dsx_reg **drdy_reg)
1744 int err = 0, drdy_pin;
1746 if (st_lsm6dsx_get_drdy_pin(hw, &drdy_pin) < 0) {
1747 struct st_sensors_platform_data *pdata;
1748 struct device *dev = hw->dev;
1750 pdata = (struct st_sensors_platform_data *)dev->platform_data;
1751 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
1756 hw->irq_routing = &hw->settings->irq_config.irq1_func;
1757 *drdy_reg = &hw->settings->irq_config.irq1;
1760 hw->irq_routing = &hw->settings->irq_config.irq2_func;
1761 *drdy_reg = &hw->settings->irq_config.irq2;
1764 dev_err(hw->dev, "unsupported data ready pin\n");
1772 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
1774 const struct st_lsm6dsx_shub_settings *hub_settings;
1775 struct st_sensors_platform_data *pdata;
1776 struct device *dev = hw->dev;
1780 hub_settings = &hw->settings->shub_settings;
1782 pdata = (struct st_sensors_platform_data *)dev->platform_data;
1783 if ((dev_fwnode(dev) && device_property_read_bool(dev, "st,pullups")) ||
1784 (pdata && pdata->pullups)) {
1785 if (hub_settings->pullup_en.sec_page) {
1786 err = st_lsm6dsx_set_page(hw, true);
1791 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
1792 err = regmap_update_bits(hw->regmap,
1793 hub_settings->pullup_en.addr,
1794 hub_settings->pullup_en.mask, data);
1796 if (hub_settings->pullup_en.sec_page)
1797 st_lsm6dsx_set_page(hw, false);
1803 if (hub_settings->aux_sens.addr) {
1804 /* configure aux sensors */
1805 err = st_lsm6dsx_set_page(hw, true);
1809 data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
1810 err = regmap_update_bits(hw->regmap,
1811 hub_settings->aux_sens.addr,
1812 hub_settings->aux_sens.mask, data);
1814 st_lsm6dsx_set_page(hw, false);
1820 if (hub_settings->emb_func.addr) {
1821 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->emb_func.mask);
1822 err = regmap_update_bits(hw->regmap,
1823 hub_settings->emb_func.addr,
1824 hub_settings->emb_func.mask, data);
1830 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
1832 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
1835 ts_settings = &hw->settings->ts_settings;
1836 /* enable hw timestamp generation if necessary */
1837 if (ts_settings->timer_en.addr) {
1838 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
1839 err = regmap_update_bits(hw->regmap,
1840 ts_settings->timer_en.addr,
1841 ts_settings->timer_en.mask, val);
1846 /* enable high resolution for hw ts timer if necessary */
1847 if (ts_settings->hr_timer.addr) {
1848 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
1849 err = regmap_update_bits(hw->regmap,
1850 ts_settings->hr_timer.addr,
1851 ts_settings->hr_timer.mask, val);
1856 /* enable ts queueing in FIFO if necessary */
1857 if (ts_settings->fifo_en.addr) {
1858 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
1859 err = regmap_update_bits(hw->regmap,
1860 ts_settings->fifo_en.addr,
1861 ts_settings->fifo_en.mask, val);
1866 /* calibrate timestamp sensitivity */
1867 hw->ts_gain = ST_LSM6DSX_TS_SENSITIVITY;
1868 if (ts_settings->freq_fine) {
1869 err = regmap_read(hw->regmap, ts_settings->freq_fine, &val);
1874 * linearize the AN5192 formula:
1875 * 1 / (1 + x) ~= 1 - x (Taylor’s Series)
1876 * ttrim[s] = 1 / (40000 * (1 + 0.0015 * val))
1877 * ttrim[ns] ~= 25000 - 37.5 * val
1878 * ttrim[ns] ~= 25000 - (37500 * val) / 1000
1880 hw->ts_gain -= ((s8)val * 37500) / 1000;
1886 static int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw)
1888 const struct st_lsm6dsx_reg *reg;
1892 * flush hw FIFO before device reset in order to avoid
1893 * possible races on interrupt line 1. If the first interrupt
1894 * line is asserted during hw reset the device will work in
1895 * I3C-only mode (if it is supported)
1897 err = st_lsm6dsx_flush_fifo(hw);
1898 if (err < 0 && err != -ENOTSUPP)
1901 /* device sw reset */
1902 reg = &hw->settings->reset;
1903 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1904 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1910 /* reload trimming parameter */
1911 reg = &hw->settings->boot;
1912 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1913 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1922 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
1924 const struct st_lsm6dsx_reg *reg;
1927 err = st_lsm6dsx_reset_device(hw);
1931 /* enable Block Data Update */
1932 reg = &hw->settings->bdu;
1933 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1934 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1938 /* enable FIFO watermak interrupt */
1939 err = st_lsm6dsx_get_drdy_reg(hw, ®);
1943 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1944 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1948 /* enable Latched interrupts for device events */
1949 if (hw->settings->irq_config.lir.addr) {
1950 reg = &hw->settings->irq_config.lir;
1951 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1952 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1956 /* enable clear on read for latched interrupts */
1957 if (hw->settings->irq_config.clear_on_read.addr) {
1958 reg = &hw->settings->irq_config.clear_on_read;
1959 err = regmap_update_bits(hw->regmap,
1960 reg->addr, reg->mask,
1961 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1967 /* enable drdy-mas if available */
1968 if (hw->settings->drdy_mask.addr) {
1969 reg = &hw->settings->drdy_mask;
1970 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
1971 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
1976 err = st_lsm6dsx_init_shub(hw);
1980 return st_lsm6dsx_init_hw_timer(hw);
1983 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
1984 enum st_lsm6dsx_sensor_id id,
1987 struct st_lsm6dsx_sensor *sensor;
1988 struct iio_dev *iio_dev;
1990 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
1994 iio_dev->modes = INDIO_DIRECT_MODE;
1995 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
1996 iio_dev->channels = hw->settings->channels[id].chan;
1997 iio_dev->num_channels = hw->settings->channels[id].len;
1999 sensor = iio_priv(iio_dev);
2002 sensor->odr = hw->settings->odr_table[id].odr_avl[0].milli_hz;
2003 sensor->gain = hw->settings->fs_table[id].fs_avl[0].gain;
2004 sensor->watermark = 1;
2007 case ST_LSM6DSX_ID_ACC:
2008 iio_dev->info = &st_lsm6dsx_acc_info;
2009 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
2012 case ST_LSM6DSX_ID_GYRO:
2013 iio_dev->info = &st_lsm6dsx_gyro_info;
2014 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
2020 iio_dev->name = sensor->name;
2026 st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
2028 const struct st_lsm6dsx_event_settings *event_settings;
2032 if (!hw->enable_event)
2035 event_settings = &hw->settings->event_settings;
2036 err = st_lsm6dsx_read_locked(hw, event_settings->wakeup_src_reg,
2037 &data, sizeof(data));
2041 timestamp = iio_get_time_ns(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
2042 if ((data & hw->settings->event_settings.wakeup_src_z_mask) &&
2043 (hw->enable_event & BIT(IIO_MOD_Z)))
2044 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2045 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2052 if ((data & hw->settings->event_settings.wakeup_src_y_mask) &&
2053 (hw->enable_event & BIT(IIO_MOD_Y)))
2054 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2055 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2062 if ((data & hw->settings->event_settings.wakeup_src_x_mask) &&
2063 (hw->enable_event & BIT(IIO_MOD_X)))
2064 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2065 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2072 return data & event_settings->wakeup_src_status_mask;
2075 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
2077 struct st_lsm6dsx_hw *hw = private;
2078 int fifo_len = 0, len;
2081 event = st_lsm6dsx_report_motion_event(hw);
2083 if (!hw->settings->fifo_ops.read_fifo)
2084 return event ? IRQ_HANDLED : IRQ_NONE;
2087 * If we are using edge IRQs, new samples can arrive while
2088 * processing current interrupt since there are no hw
2089 * guarantees the irq line stays "low" long enough to properly
2090 * detect the new interrupt. In this case the new sample will
2092 * Polling FIFO status register allow us to read new
2093 * samples even if the interrupt arrives while processing
2094 * previous data and the timeslot where the line is "low" is
2095 * too short to be properly detected.
2098 mutex_lock(&hw->fifo_lock);
2099 len = hw->settings->fifo_ops.read_fifo(hw);
2100 mutex_unlock(&hw->fifo_lock);
2106 return fifo_len || event ? IRQ_HANDLED : IRQ_NONE;
2109 static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
2111 struct st_sensors_platform_data *pdata;
2112 const struct st_lsm6dsx_reg *reg;
2113 struct device *dev = hw->dev;
2114 unsigned long irq_type;
2115 bool irq_active_low;
2118 irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
2121 case IRQF_TRIGGER_HIGH:
2122 case IRQF_TRIGGER_RISING:
2123 irq_active_low = false;
2125 case IRQF_TRIGGER_LOW:
2126 case IRQF_TRIGGER_FALLING:
2127 irq_active_low = true;
2130 dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
2134 reg = &hw->settings->irq_config.hla;
2135 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2136 ST_LSM6DSX_SHIFT_VAL(irq_active_low,
2141 pdata = (struct st_sensors_platform_data *)dev->platform_data;
2142 if ((dev_fwnode(dev) && device_property_read_bool(dev, "drive-open-drain")) ||
2143 (pdata && pdata->open_drain)) {
2144 reg = &hw->settings->irq_config.od;
2145 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2146 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2150 irq_type |= IRQF_SHARED;
2153 err = devm_request_threaded_irq(hw->dev, hw->irq,
2155 st_lsm6dsx_handler_thread,
2156 irq_type | IRQF_ONESHOT,
2159 dev_err(hw->dev, "failed to request trigger irq %d\n",
2167 static int st_lsm6dsx_init_regulators(struct device *dev)
2169 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2172 /* vdd-vddio power regulators */
2173 hw->regulators[0].supply = "vdd";
2174 hw->regulators[1].supply = "vddio";
2175 err = devm_regulator_bulk_get(dev, ARRAY_SIZE(hw->regulators),
2178 return dev_err_probe(dev, err, "failed to get regulators\n");
2180 err = regulator_bulk_enable(ARRAY_SIZE(hw->regulators),
2183 dev_err(dev, "failed to enable regulators: %d\n", err);
2192 static void st_lsm6dsx_chip_uninit(void *data)
2194 struct st_lsm6dsx_hw *hw = data;
2196 regulator_bulk_disable(ARRAY_SIZE(hw->regulators), hw->regulators);
2199 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
2200 struct regmap *regmap)
2202 struct st_sensors_platform_data *pdata = dev->platform_data;
2203 const struct st_lsm6dsx_shub_settings *hub_settings;
2204 struct st_lsm6dsx_hw *hw;
2205 const char *name = NULL;
2208 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
2212 dev_set_drvdata(dev, (void *)hw);
2214 mutex_init(&hw->fifo_lock);
2215 mutex_init(&hw->conf_lock);
2216 mutex_init(&hw->page_lock);
2218 err = st_lsm6dsx_init_regulators(dev);
2222 err = devm_add_action_or_reset(dev, st_lsm6dsx_chip_uninit, hw);
2226 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
2232 hw->regmap = regmap;
2234 err = st_lsm6dsx_check_whoami(hw, hw_id, &name);
2238 for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
2239 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
2240 if (!hw->iio_devs[i])
2244 err = st_lsm6dsx_init_device(hw);
2248 hub_settings = &hw->settings->shub_settings;
2249 if (hub_settings->master_en.addr) {
2250 err = st_lsm6dsx_shub_probe(hw, name);
2256 err = st_lsm6dsx_irq_setup(hw);
2260 err = st_lsm6dsx_fifo_setup(hw);
2265 err = iio_read_mount_matrix(hw->dev, &hw->orientation);
2269 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2270 if (!hw->iio_devs[i])
2273 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
2278 if ((dev_fwnode(dev) && device_property_read_bool(dev, "wakeup-source")) ||
2279 (pdata && pdata->wakeup_source))
2280 device_init_wakeup(dev, true);
2284 EXPORT_SYMBOL(st_lsm6dsx_probe);
2286 static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
2288 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2289 struct st_lsm6dsx_sensor *sensor;
2292 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2293 if (!hw->iio_devs[i])
2296 sensor = iio_priv(hw->iio_devs[i]);
2297 if (!(hw->enable_mask & BIT(sensor->id)))
2300 if (device_may_wakeup(dev) &&
2301 sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event) {
2302 /* Enable wake from IRQ */
2303 enable_irq_wake(hw->irq);
2307 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
2308 sensor->id == ST_LSM6DSX_ID_EXT1 ||
2309 sensor->id == ST_LSM6DSX_ID_EXT2)
2310 err = st_lsm6dsx_shub_set_enable(sensor, false);
2312 err = st_lsm6dsx_sensor_set_enable(sensor, false);
2316 hw->suspend_mask |= BIT(sensor->id);
2320 err = st_lsm6dsx_flush_fifo(hw);
2325 static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
2327 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2328 struct st_lsm6dsx_sensor *sensor;
2331 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2332 if (!hw->iio_devs[i])
2335 sensor = iio_priv(hw->iio_devs[i]);
2336 if (device_may_wakeup(dev) &&
2337 sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event)
2338 disable_irq_wake(hw->irq);
2340 if (!(hw->suspend_mask & BIT(sensor->id)))
2343 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
2344 sensor->id == ST_LSM6DSX_ID_EXT1 ||
2345 sensor->id == ST_LSM6DSX_ID_EXT2)
2346 err = st_lsm6dsx_shub_set_enable(sensor, true);
2348 err = st_lsm6dsx_sensor_set_enable(sensor, true);
2352 hw->suspend_mask &= ~BIT(sensor->id);
2356 err = st_lsm6dsx_resume_fifo(hw);
2361 const struct dev_pm_ops st_lsm6dsx_pm_ops = {
2362 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
2364 EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
2366 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
2367 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
2368 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
2369 MODULE_LICENSE("GPL v2");