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/ASM330LHHX/LSM6DSR/ISM330DHCX/LSM6DST/LSM6DSOP/
30 * LSM6DSTX/LSM6DSO16IS/ISM330IS:
31 * - Accelerometer/Gyroscope supported ODR [Hz]: 12.5, 26, 52, 104, 208, 416,
33 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
34 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
37 * - LSM6DSV/LSM6DSV16X:
38 * - Accelerometer/Gyroscope supported ODR [Hz]: 7.5, 15, 30, 60, 120, 240,
40 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
41 * - Gyroscope supported full-scale [dps]: +-125/+-250/+-500/+-1000/+-2000
45 * - Accelerometer supported ODR [Hz]: 10, 50, 119, 238, 476, 952
46 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
47 * - Gyroscope supported ODR [Hz]: 15, 60, 119, 238, 476, 952
48 * - Gyroscope supported full-scale [dps]: +-245/+-500/+-2000
51 * Copyright 2016 STMicroelectronics Inc.
53 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
54 * Denis Ciocca <denis.ciocca@st.com>
57 #include <linux/kernel.h>
58 #include <linux/module.h>
59 #include <linux/acpi.h>
60 #include <linux/delay.h>
61 #include <linux/iio/events.h>
62 #include <linux/iio/iio.h>
63 #include <linux/iio/sysfs.h>
64 #include <linux/iio/triggered_buffer.h>
65 #include <linux/iio/trigger_consumer.h>
66 #include <linux/interrupt.h>
67 #include <linux/irq.h>
68 #include <linux/minmax.h>
70 #include <linux/property.h>
71 #include <linux/regmap.h>
72 #include <linux/bitfield.h>
74 #include <linux/platform_data/st_sensors_pdata.h>
76 #include "st_lsm6dsx.h"
78 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
80 #define ST_LSM6DSX_TS_SENSITIVITY 25000UL /* 25us */
82 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
83 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x28, IIO_MOD_X, 0),
84 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2a, IIO_MOD_Y, 1),
85 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL, 0x2c, IIO_MOD_Z, 2),
86 IIO_CHAN_SOFT_TIMESTAMP(3),
89 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
90 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x22, IIO_MOD_X, 0),
91 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x24, IIO_MOD_Y, 1),
92 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x26, IIO_MOD_Z, 2),
93 IIO_CHAN_SOFT_TIMESTAMP(3),
96 static const struct iio_chan_spec st_lsm6ds0_gyro_channels[] = {
97 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x18, IIO_MOD_X, 0),
98 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1a, IIO_MOD_Y, 1),
99 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, 0x1c, IIO_MOD_Z, 2),
100 IIO_CHAN_SOFT_TIMESTAMP(3),
103 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
119 .hw_id = ST_LSM9DS1_ID,
120 .name = ST_LSM9DS1_DEV_NAME,
123 .hw_id = ST_LSM6DS0_ID,
124 .name = ST_LSM6DS0_DEV_NAME,
129 [ST_LSM6DSX_ID_ACC] = {
130 .chan = st_lsm6dsx_acc_channels,
131 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
133 [ST_LSM6DSX_ID_GYRO] = {
134 .chan = st_lsm6ds0_gyro_channels,
135 .len = ARRAY_SIZE(st_lsm6ds0_gyro_channels),
139 [ST_LSM6DSX_ID_ACC] = {
142 .mask = GENMASK(7, 5),
144 .odr_avl[0] = { 10000, 0x01 },
145 .odr_avl[1] = { 50000, 0x02 },
146 .odr_avl[2] = { 119000, 0x03 },
147 .odr_avl[3] = { 238000, 0x04 },
148 .odr_avl[4] = { 476000, 0x05 },
149 .odr_avl[5] = { 952000, 0x06 },
152 [ST_LSM6DSX_ID_GYRO] = {
155 .mask = GENMASK(7, 5),
157 .odr_avl[0] = { 14900, 0x01 },
158 .odr_avl[1] = { 59500, 0x02 },
159 .odr_avl[2] = { 119000, 0x03 },
160 .odr_avl[3] = { 238000, 0x04 },
161 .odr_avl[4] = { 476000, 0x05 },
162 .odr_avl[5] = { 952000, 0x06 },
167 [ST_LSM6DSX_ID_ACC] = {
170 .mask = GENMASK(4, 3),
172 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
173 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
174 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
175 .fs_avl[3] = { IIO_G_TO_M_S_2(732000), 0x1 },
178 [ST_LSM6DSX_ID_GYRO] = {
181 .mask = GENMASK(4, 3),
184 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
185 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
186 .fs_avl[2] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
227 .hw_id = ST_LSM6DS3_ID,
228 .name = ST_LSM6DS3_DEV_NAME,
233 [ST_LSM6DSX_ID_ACC] = {
234 .chan = st_lsm6dsx_acc_channels,
235 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
237 [ST_LSM6DSX_ID_GYRO] = {
238 .chan = st_lsm6dsx_gyro_channels,
239 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
243 [ST_LSM6DSX_ID_ACC] = {
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 },
256 [ST_LSM6DSX_ID_GYRO] = {
259 .mask = GENMASK(7, 4),
261 .odr_avl[0] = { 12500, 0x01 },
262 .odr_avl[1] = { 26000, 0x02 },
263 .odr_avl[2] = { 52000, 0x03 },
264 .odr_avl[3] = { 104000, 0x04 },
265 .odr_avl[4] = { 208000, 0x05 },
266 .odr_avl[5] = { 416000, 0x06 },
271 [ST_LSM6DSX_ID_ACC] = {
274 .mask = GENMASK(3, 2),
276 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
277 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
278 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
279 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
282 [ST_LSM6DSX_ID_GYRO] = {
285 .mask = GENMASK(3, 2),
287 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
288 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
289 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
290 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
325 [ST_LSM6DSX_ID_ACC] = {
327 .mask = GENMASK(2, 0),
329 [ST_LSM6DSX_ID_GYRO] = {
331 .mask = GENMASK(5, 3),
335 .update_fifo = st_lsm6dsx_update_fifo,
336 .read_fifo = st_lsm6dsx_read_fifo,
339 .mask = GENMASK(11, 0),
343 .mask = GENMASK(11, 0),
346 .th_wl = 3, /* 1LSB = 2B */
363 .mask = GENMASK(5, 3),
369 .mask = GENMASK(5, 0),
371 .wakeup_src_reg = 0x1b,
372 .wakeup_src_status_mask = BIT(3),
373 .wakeup_src_z_mask = BIT(0),
374 .wakeup_src_y_mask = BIT(1),
375 .wakeup_src_x_mask = BIT(2),
393 .hw_id = ST_LSM6DS3H_ID,
394 .name = ST_LSM6DS3H_DEV_NAME,
399 [ST_LSM6DSX_ID_ACC] = {
400 .chan = st_lsm6dsx_acc_channels,
401 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
403 [ST_LSM6DSX_ID_GYRO] = {
404 .chan = st_lsm6dsx_gyro_channels,
405 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
409 [ST_LSM6DSX_ID_ACC] = {
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 },
422 [ST_LSM6DSX_ID_GYRO] = {
425 .mask = GENMASK(7, 4),
427 .odr_avl[0] = { 12500, 0x01 },
428 .odr_avl[1] = { 26000, 0x02 },
429 .odr_avl[2] = { 52000, 0x03 },
430 .odr_avl[3] = { 104000, 0x04 },
431 .odr_avl[4] = { 208000, 0x05 },
432 .odr_avl[5] = { 416000, 0x06 },
437 [ST_LSM6DSX_ID_ACC] = {
440 .mask = GENMASK(3, 2),
442 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
443 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
444 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
445 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
448 [ST_LSM6DSX_ID_GYRO] = {
451 .mask = GENMASK(3, 2),
453 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
454 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
455 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
456 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
491 [ST_LSM6DSX_ID_ACC] = {
493 .mask = GENMASK(2, 0),
495 [ST_LSM6DSX_ID_GYRO] = {
497 .mask = GENMASK(5, 3),
501 .update_fifo = st_lsm6dsx_update_fifo,
502 .read_fifo = st_lsm6dsx_read_fifo,
505 .mask = GENMASK(11, 0),
509 .mask = GENMASK(11, 0),
512 .th_wl = 3, /* 1LSB = 2B */
529 .mask = GENMASK(5, 3),
535 .mask = GENMASK(5, 0),
537 .wakeup_src_reg = 0x1b,
538 .wakeup_src_status_mask = BIT(3),
539 .wakeup_src_z_mask = BIT(0),
540 .wakeup_src_y_mask = BIT(1),
541 .wakeup_src_x_mask = BIT(2),
559 .hw_id = ST_LSM6DSL_ID,
560 .name = ST_LSM6DSL_DEV_NAME,
563 .hw_id = ST_LSM6DSM_ID,
564 .name = ST_LSM6DSM_DEV_NAME,
567 .hw_id = ST_ISM330DLC_ID,
568 .name = ST_ISM330DLC_DEV_NAME,
571 .hw_id = ST_LSM6DS3TRC_ID,
572 .name = ST_LSM6DS3TRC_DEV_NAME,
577 [ST_LSM6DSX_ID_ACC] = {
578 .chan = st_lsm6dsx_acc_channels,
579 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
581 [ST_LSM6DSX_ID_GYRO] = {
582 .chan = st_lsm6dsx_gyro_channels,
583 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
587 [ST_LSM6DSX_ID_ACC] = {
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 },
600 [ST_LSM6DSX_ID_GYRO] = {
603 .mask = GENMASK(7, 4),
605 .odr_avl[0] = { 12500, 0x01 },
606 .odr_avl[1] = { 26000, 0x02 },
607 .odr_avl[2] = { 52000, 0x03 },
608 .odr_avl[3] = { 104000, 0x04 },
609 .odr_avl[4] = { 208000, 0x05 },
610 .odr_avl[5] = { 416000, 0x06 },
615 [ST_LSM6DSX_ID_ACC] = {
618 .mask = GENMASK(3, 2),
620 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
621 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
622 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
623 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
626 [ST_LSM6DSX_ID_GYRO] = {
629 .mask = GENMASK(3, 2),
631 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
632 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
633 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
634 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
638 .samples_to_discard = {
639 [ST_LSM6DSX_ID_ACC] = {
640 .val[0] = { 12500, 1 },
641 .val[1] = { 26000, 1 },
642 .val[2] = { 52000, 1 },
643 .val[3] = { 104000, 2 },
644 .val[4] = { 208000, 2 },
645 .val[5] = { 416000, 2 },
647 [ST_LSM6DSX_ID_GYRO] = {
648 .val[0] = { 12500, 2 },
649 .val[1] = { 26000, 5 },
650 .val[2] = { 52000, 7 },
651 .val[3] = { 104000, 12 },
652 .val[4] = { 208000, 20 },
653 .val[5] = { 416000, 36 },
687 [ST_LSM6DSX_ID_ACC] = {
689 .mask = GENMASK(2, 0),
691 [ST_LSM6DSX_ID_GYRO] = {
693 .mask = GENMASK(5, 3),
695 [ST_LSM6DSX_ID_EXT0] = {
697 .mask = GENMASK(2, 0),
701 .update_fifo = st_lsm6dsx_update_fifo,
702 .read_fifo = st_lsm6dsx_read_fifo,
705 .mask = GENMASK(10, 0),
709 .mask = GENMASK(10, 0),
712 .th_wl = 3, /* 1LSB = 2B */
729 .mask = GENMASK(5, 3),
747 .mask = GENMASK(5, 4),
762 .dw_slv0_addr = 0x0e,
772 .mask = GENMASK(5, 0),
774 .wakeup_src_reg = 0x1b,
775 .wakeup_src_status_mask = BIT(3),
776 .wakeup_src_z_mask = BIT(0),
777 .wakeup_src_y_mask = BIT(1),
778 .wakeup_src_x_mask = BIT(2),
796 .hw_id = ST_LSM6DSR_ID,
797 .name = ST_LSM6DSR_DEV_NAME,
800 .hw_id = ST_ISM330DHCX_ID,
801 .name = ST_ISM330DHCX_DEV_NAME,
804 .hw_id = ST_LSM6DSRX_ID,
805 .name = ST_LSM6DSRX_DEV_NAME,
808 .hw_id = ST_LSM6DSO_ID,
809 .name = ST_LSM6DSO_DEV_NAME,
812 .hw_id = ST_LSM6DSOX_ID,
813 .name = ST_LSM6DSOX_DEV_NAME,
816 .hw_id = ST_LSM6DST_ID,
817 .name = ST_LSM6DST_DEV_NAME,
820 .hw_id = ST_ASM330LHHX_ID,
821 .name = ST_ASM330LHHX_DEV_NAME,
824 .hw_id = ST_LSM6DSTX_ID,
825 .name = ST_LSM6DSTX_DEV_NAME,
830 [ST_LSM6DSX_ID_ACC] = {
831 .chan = st_lsm6dsx_acc_channels,
832 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
834 [ST_LSM6DSX_ID_GYRO] = {
835 .chan = st_lsm6dsx_gyro_channels,
836 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
844 [ST_LSM6DSX_ID_ACC] = {
847 .mask = GENMASK(7, 4),
849 .odr_avl[0] = { 12500, 0x01 },
850 .odr_avl[1] = { 26000, 0x02 },
851 .odr_avl[2] = { 52000, 0x03 },
852 .odr_avl[3] = { 104000, 0x04 },
853 .odr_avl[4] = { 208000, 0x05 },
854 .odr_avl[5] = { 416000, 0x06 },
855 .odr_avl[6] = { 833000, 0x07 },
858 [ST_LSM6DSX_ID_GYRO] = {
861 .mask = GENMASK(7, 4),
863 .odr_avl[0] = { 12500, 0x01 },
864 .odr_avl[1] = { 26000, 0x02 },
865 .odr_avl[2] = { 52000, 0x03 },
866 .odr_avl[3] = { 104000, 0x04 },
867 .odr_avl[4] = { 208000, 0x05 },
868 .odr_avl[5] = { 416000, 0x06 },
869 .odr_avl[6] = { 833000, 0x07 },
874 [ST_LSM6DSX_ID_ACC] = {
877 .mask = GENMASK(3, 2),
879 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
880 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
881 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
882 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
885 [ST_LSM6DSX_ID_GYRO] = {
888 .mask = GENMASK(3, 2),
890 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
891 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
892 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
893 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
932 [ST_LSM6DSX_ID_ACC] = {
934 .mask = GENMASK(3, 0),
936 [ST_LSM6DSX_ID_GYRO] = {
938 .mask = GENMASK(7, 4),
942 .update_fifo = st_lsm6dsx_update_fifo,
943 .read_fifo = st_lsm6dsx_read_tagged_fifo,
946 .mask = GENMASK(8, 0),
950 .mask = GENMASK(9, 0),
962 .mask = GENMASK(7, 6),
983 .mask = GENMASK(1, 0),
995 .dw_slv0_addr = 0x21,
1005 .mask = GENMASK(5, 0),
1007 .wakeup_src_reg = 0x1b,
1008 .wakeup_src_status_mask = BIT(3),
1009 .wakeup_src_z_mask = BIT(0),
1010 .wakeup_src_y_mask = BIT(1),
1011 .wakeup_src_x_mask = BIT(2),
1029 .hw_id = ST_ASM330LHH_ID,
1030 .name = ST_ASM330LHH_DEV_NAME,
1033 .hw_id = ST_LSM6DSOP_ID,
1034 .name = ST_LSM6DSOP_DEV_NAME,
1037 .hw_id = ST_ASM330LHB_ID,
1038 .name = ST_ASM330LHB_DEV_NAME,
1043 [ST_LSM6DSX_ID_ACC] = {
1044 .chan = st_lsm6dsx_acc_channels,
1045 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
1047 [ST_LSM6DSX_ID_GYRO] = {
1048 .chan = st_lsm6dsx_gyro_channels,
1049 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
1057 [ST_LSM6DSX_ID_ACC] = {
1060 .mask = GENMASK(7, 4),
1062 .odr_avl[0] = { 12500, 0x01 },
1063 .odr_avl[1] = { 26000, 0x02 },
1064 .odr_avl[2] = { 52000, 0x03 },
1065 .odr_avl[3] = { 104000, 0x04 },
1066 .odr_avl[4] = { 208000, 0x05 },
1067 .odr_avl[5] = { 416000, 0x06 },
1068 .odr_avl[6] = { 833000, 0x07 },
1071 [ST_LSM6DSX_ID_GYRO] = {
1074 .mask = GENMASK(7, 4),
1076 .odr_avl[0] = { 12500, 0x01 },
1077 .odr_avl[1] = { 26000, 0x02 },
1078 .odr_avl[2] = { 52000, 0x03 },
1079 .odr_avl[3] = { 104000, 0x04 },
1080 .odr_avl[4] = { 208000, 0x05 },
1081 .odr_avl[5] = { 416000, 0x06 },
1082 .odr_avl[6] = { 833000, 0x07 },
1087 [ST_LSM6DSX_ID_ACC] = {
1090 .mask = GENMASK(3, 2),
1092 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
1093 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
1094 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
1095 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
1098 [ST_LSM6DSX_ID_GYRO] = {
1101 .mask = GENMASK(3, 2),
1103 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
1104 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
1105 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
1106 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
1145 [ST_LSM6DSX_ID_ACC] = {
1147 .mask = GENMASK(3, 0),
1149 [ST_LSM6DSX_ID_GYRO] = {
1151 .mask = GENMASK(7, 4),
1155 .update_fifo = st_lsm6dsx_update_fifo,
1156 .read_fifo = st_lsm6dsx_read_tagged_fifo,
1159 .mask = GENMASK(8, 0),
1163 .mask = GENMASK(9, 0),
1175 .mask = GENMASK(7, 6),
1186 .mask = GENMASK(5, 0),
1188 .wakeup_src_reg = 0x1b,
1189 .wakeup_src_status_mask = BIT(3),
1190 .wakeup_src_z_mask = BIT(0),
1191 .wakeup_src_y_mask = BIT(1),
1192 .wakeup_src_x_mask = BIT(2),
1210 .hw_id = ST_LSM6DSV_ID,
1211 .name = ST_LSM6DSV_DEV_NAME,
1214 .hw_id = ST_LSM6DSV16X_ID,
1215 .name = ST_LSM6DSV16X_DEV_NAME,
1220 [ST_LSM6DSX_ID_ACC] = {
1221 .chan = st_lsm6dsx_acc_channels,
1222 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
1224 [ST_LSM6DSX_ID_GYRO] = {
1225 .chan = st_lsm6dsx_gyro_channels,
1226 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
1234 [ST_LSM6DSX_ID_ACC] = {
1237 .mask = GENMASK(3, 0),
1239 .odr_avl[0] = { 7500, 0x02 },
1240 .odr_avl[1] = { 15000, 0x03 },
1241 .odr_avl[2] = { 30000, 0x04 },
1242 .odr_avl[3] = { 60000, 0x05 },
1243 .odr_avl[4] = { 120000, 0x06 },
1244 .odr_avl[5] = { 240000, 0x07 },
1245 .odr_avl[6] = { 480000, 0x08 },
1246 .odr_avl[7] = { 960000, 0x09 },
1249 [ST_LSM6DSX_ID_GYRO] = {
1252 .mask = GENMASK(3, 0),
1254 .odr_avl[0] = { 7500, 0x02 },
1255 .odr_avl[1] = { 15000, 0x03 },
1256 .odr_avl[2] = { 30000, 0x04 },
1257 .odr_avl[3] = { 60000, 0x05 },
1258 .odr_avl[4] = { 120000, 0x06 },
1259 .odr_avl[5] = { 240000, 0x07 },
1260 .odr_avl[6] = { 480000, 0x08 },
1261 .odr_avl[7] = { 960000, 0x09 },
1266 [ST_LSM6DSX_ID_ACC] = {
1269 .mask = GENMASK(1, 0),
1271 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
1272 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x1 },
1273 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x2 },
1274 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x3 },
1277 [ST_LSM6DSX_ID_GYRO] = {
1280 .mask = GENMASK(3, 0),
1282 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x1 },
1283 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x2 },
1284 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x3 },
1285 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x4 },
1320 [ST_LSM6DSX_ID_ACC] = {
1322 .mask = GENMASK(3, 0),
1324 [ST_LSM6DSX_ID_GYRO] = {
1326 .mask = GENMASK(7, 4),
1330 .update_fifo = st_lsm6dsx_update_fifo,
1331 .read_fifo = st_lsm6dsx_read_tagged_fifo,
1334 .mask = GENMASK(7, 0),
1338 .mask = GENMASK(8, 0),
1350 .mask = GENMASK(7, 6),
1370 .mask = GENMASK(1, 0),
1382 .dw_slv0_addr = 0x21,
1392 .mask = GENMASK(5, 0),
1394 .wakeup_src_reg = 0x45,
1395 .wakeup_src_status_mask = BIT(3),
1396 .wakeup_src_z_mask = BIT(0),
1397 .wakeup_src_y_mask = BIT(1),
1398 .wakeup_src_x_mask = BIT(2),
1416 .hw_id = ST_LSM6DSO16IS_ID,
1417 .name = ST_LSM6DSO16IS_DEV_NAME,
1420 .hw_id = ST_ISM330IS_ID,
1421 .name = ST_ISM330IS_DEV_NAME,
1426 [ST_LSM6DSX_ID_ACC] = {
1427 .chan = st_lsm6dsx_acc_channels,
1428 .len = ARRAY_SIZE(st_lsm6dsx_acc_channels),
1430 [ST_LSM6DSX_ID_GYRO] = {
1431 .chan = st_lsm6dsx_gyro_channels,
1432 .len = ARRAY_SIZE(st_lsm6dsx_gyro_channels),
1436 [ST_LSM6DSX_ID_ACC] = {
1439 .mask = GENMASK(7, 4),
1441 .odr_avl[0] = { 12500, 0x01 },
1442 .odr_avl[1] = { 26000, 0x02 },
1443 .odr_avl[2] = { 52000, 0x03 },
1444 .odr_avl[3] = { 104000, 0x04 },
1445 .odr_avl[4] = { 208000, 0x05 },
1446 .odr_avl[5] = { 416000, 0x06 },
1447 .odr_avl[6] = { 833000, 0x07 },
1450 [ST_LSM6DSX_ID_GYRO] = {
1453 .mask = GENMASK(7, 4),
1455 .odr_avl[0] = { 12500, 0x01 },
1456 .odr_avl[1] = { 26000, 0x02 },
1457 .odr_avl[2] = { 52000, 0x03 },
1458 .odr_avl[3] = { 104000, 0x04 },
1459 .odr_avl[4] = { 208000, 0x05 },
1460 .odr_avl[5] = { 416000, 0x06 },
1461 .odr_avl[6] = { 833000, 0x07 },
1466 [ST_LSM6DSX_ID_ACC] = {
1469 .mask = GENMASK(3, 2),
1471 .fs_avl[0] = { IIO_G_TO_M_S_2(61000), 0x0 },
1472 .fs_avl[1] = { IIO_G_TO_M_S_2(122000), 0x2 },
1473 .fs_avl[2] = { IIO_G_TO_M_S_2(244000), 0x3 },
1474 .fs_avl[3] = { IIO_G_TO_M_S_2(488000), 0x1 },
1477 [ST_LSM6DSX_ID_GYRO] = {
1480 .mask = GENMASK(3, 2),
1482 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750000), 0x0 },
1483 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500000), 0x1 },
1484 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000000), 0x2 },
1485 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000000), 0x3 },
1516 .mask = GENMASK(1, 0),
1528 .dw_slv0_addr = 0x21,
1533 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
1535 const struct st_lsm6dsx_shub_settings *hub_settings;
1539 hub_settings = &hw->settings->shub_settings;
1540 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
1541 err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
1542 hub_settings->page_mux.mask, data);
1543 usleep_range(100, 150);
1548 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id,
1551 int err, i, j, data;
1553 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
1554 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
1555 if (st_lsm6dsx_sensor_settings[i].id[j].name &&
1556 id == st_lsm6dsx_sensor_settings[i].id[j].hw_id)
1559 if (j < ST_LSM6DSX_MAX_ID)
1563 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
1564 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
1568 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
1570 dev_err(hw->dev, "failed to read whoami register\n");
1574 if (data != st_lsm6dsx_sensor_settings[i].id[j].wai) {
1575 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
1579 *name = st_lsm6dsx_sensor_settings[i].id[j].name;
1580 hw->settings = &st_lsm6dsx_sensor_settings[i];
1585 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
1588 const struct st_lsm6dsx_fs_table_entry *fs_table;
1592 fs_table = &sensor->hw->settings->fs_table[sensor->id];
1593 for (i = 0; i < fs_table->fs_len; i++) {
1594 if (fs_table->fs_avl[i].gain == gain)
1598 if (i == fs_table->fs_len)
1601 data = ST_LSM6DSX_SHIFT_VAL(fs_table->fs_avl[i].val,
1602 fs_table->reg.mask);
1603 err = st_lsm6dsx_update_bits_locked(sensor->hw, fs_table->reg.addr,
1604 fs_table->reg.mask, data);
1608 sensor->gain = gain;
1613 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u32 odr, u8 *val)
1615 const struct st_lsm6dsx_odr_table_entry *odr_table;
1618 odr_table = &sensor->hw->settings->odr_table[sensor->id];
1619 for (i = 0; i < odr_table->odr_len; i++) {
1621 * ext devices can run at different odr respect to
1624 if (odr_table->odr_avl[i].milli_hz >= odr)
1628 if (i == odr_table->odr_len)
1631 *val = odr_table->odr_avl[i].val;
1632 return odr_table->odr_avl[i].milli_hz;
1636 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u32 odr,
1637 enum st_lsm6dsx_sensor_id id)
1639 struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
1642 if (hw->enable_mask & BIT(id))
1643 return max_t(u32, ref->odr, odr);
1647 return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
1652 st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u32 req_odr)
1654 struct st_lsm6dsx_sensor *ref_sensor = sensor;
1655 struct st_lsm6dsx_hw *hw = sensor->hw;
1656 const struct st_lsm6dsx_reg *reg;
1661 switch (sensor->id) {
1662 case ST_LSM6DSX_ID_GYRO:
1664 case ST_LSM6DSX_ID_EXT0:
1665 case ST_LSM6DSX_ID_EXT1:
1666 case ST_LSM6DSX_ID_EXT2:
1667 case ST_LSM6DSX_ID_ACC: {
1672 * i2c embedded controller relies on the accelerometer sensor as
1673 * bus read/write trigger so we need to enable accel device
1674 * at odr = max(accel_odr, ext_odr) in order to properly
1675 * communicate with i2c slave devices
1677 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
1678 for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
1679 if (!hw->iio_devs[i] || i == sensor->id)
1682 odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
1684 /* device already configured */
1689 default: /* should never occur */
1694 err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
1699 reg = &hw->settings->odr_table[ref_sensor->id].reg;
1700 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1701 return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
1705 __st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
1708 struct st_lsm6dsx_hw *hw = sensor->hw;
1709 u32 odr = enable ? sensor->odr : 0;
1712 err = st_lsm6dsx_set_odr(sensor, odr);
1717 hw->enable_mask |= BIT(sensor->id);
1719 hw->enable_mask &= ~BIT(sensor->id);
1725 st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor, bool enable)
1727 struct st_lsm6dsx_hw *hw = sensor->hw;
1729 if (sensor->id == ST_LSM6DSX_ID_GYRO || enable)
1732 return hw->enable_event;
1735 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
1738 if (st_lsm6dsx_check_events(sensor, enable))
1741 return __st_lsm6dsx_sensor_set_enable(sensor, enable);
1744 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
1747 struct st_lsm6dsx_hw *hw = sensor->hw;
1751 err = st_lsm6dsx_sensor_set_enable(sensor, true);
1756 * we need to wait for sensor settling time before
1757 * reading data in order to avoid corrupted samples
1759 delay = 1000000000 / sensor->odr;
1760 usleep_range(3 * delay, 4 * delay);
1762 err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
1766 if (!hw->enable_event) {
1767 err = st_lsm6dsx_sensor_set_enable(sensor, false);
1772 *val = (s16)le16_to_cpu(data);
1777 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
1778 struct iio_chan_spec const *ch,
1779 int *val, int *val2, long mask)
1781 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1785 case IIO_CHAN_INFO_RAW:
1786 ret = iio_device_claim_direct_mode(iio_dev);
1790 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
1791 iio_device_release_direct_mode(iio_dev);
1793 case IIO_CHAN_INFO_SAMP_FREQ:
1794 *val = sensor->odr / 1000;
1795 *val2 = (sensor->odr % 1000) * 1000;
1796 ret = IIO_VAL_INT_PLUS_MICRO;
1798 case IIO_CHAN_INFO_SCALE:
1800 *val2 = sensor->gain;
1801 ret = IIO_VAL_INT_PLUS_NANO;
1811 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
1812 struct iio_chan_spec const *chan,
1813 int val, int val2, long mask)
1815 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1818 err = iio_device_claim_direct_mode(iio_dev);
1823 case IIO_CHAN_INFO_SCALE:
1824 err = st_lsm6dsx_set_full_scale(sensor, val2);
1826 case IIO_CHAN_INFO_SAMP_FREQ: {
1829 val = val * 1000 + val2 / 1000;
1830 val = st_lsm6dsx_check_odr(sensor, val, &data);
1842 iio_device_release_direct_mode(iio_dev);
1847 static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw *hw, int state)
1849 const struct st_lsm6dsx_reg *reg;
1853 if (!hw->settings->irq_config.irq1_func.addr)
1856 reg = &hw->settings->event_settings.enable_reg;
1858 data = ST_LSM6DSX_SHIFT_VAL(state, reg->mask);
1859 err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
1865 /* Enable wakeup interrupt */
1866 data = ST_LSM6DSX_SHIFT_VAL(state, hw->irq_routing->mask);
1867 return st_lsm6dsx_update_bits_locked(hw, hw->irq_routing->addr,
1868 hw->irq_routing->mask, data);
1871 static int st_lsm6dsx_read_event(struct iio_dev *iio_dev,
1872 const struct iio_chan_spec *chan,
1873 enum iio_event_type type,
1874 enum iio_event_direction dir,
1875 enum iio_event_info info,
1876 int *val, int *val2)
1878 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1879 struct st_lsm6dsx_hw *hw = sensor->hw;
1881 if (type != IIO_EV_TYPE_THRESH)
1885 *val = hw->event_threshold;
1891 st_lsm6dsx_write_event(struct iio_dev *iio_dev,
1892 const struct iio_chan_spec *chan,
1893 enum iio_event_type type,
1894 enum iio_event_direction dir,
1895 enum iio_event_info info,
1898 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1899 struct st_lsm6dsx_hw *hw = sensor->hw;
1900 const struct st_lsm6dsx_reg *reg;
1904 if (type != IIO_EV_TYPE_THRESH)
1907 if (val < 0 || val > 31)
1910 reg = &hw->settings->event_settings.wakeup_reg;
1911 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
1912 err = st_lsm6dsx_update_bits_locked(hw, reg->addr,
1917 hw->event_threshold = val;
1923 st_lsm6dsx_read_event_config(struct iio_dev *iio_dev,
1924 const struct iio_chan_spec *chan,
1925 enum iio_event_type type,
1926 enum iio_event_direction dir)
1928 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1929 struct st_lsm6dsx_hw *hw = sensor->hw;
1931 if (type != IIO_EV_TYPE_THRESH)
1934 return !!(hw->enable_event & BIT(chan->channel2));
1938 st_lsm6dsx_write_event_config(struct iio_dev *iio_dev,
1939 const struct iio_chan_spec *chan,
1940 enum iio_event_type type,
1941 enum iio_event_direction dir, int state)
1943 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1944 struct st_lsm6dsx_hw *hw = sensor->hw;
1948 if (type != IIO_EV_TYPE_THRESH)
1952 enable_event = hw->enable_event | BIT(chan->channel2);
1954 /* do not enable events if they are already enabled */
1955 if (hw->enable_event)
1958 enable_event = hw->enable_event & ~BIT(chan->channel2);
1960 /* only turn off sensor if no events is enabled */
1965 /* stop here if no changes have been made */
1966 if (hw->enable_event == enable_event)
1969 err = st_lsm6dsx_event_setup(hw, state);
1973 mutex_lock(&hw->conf_lock);
1974 if (enable_event || !(hw->fifo_mask & BIT(sensor->id)))
1975 err = __st_lsm6dsx_sensor_set_enable(sensor, state);
1976 mutex_unlock(&hw->conf_lock);
1981 hw->enable_event = enable_event;
1986 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
1988 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
1989 struct st_lsm6dsx_hw *hw = sensor->hw;
1992 val = clamp_val(val, 1, hw->settings->fifo_ops.max_size);
1994 mutex_lock(&hw->conf_lock);
1996 err = st_lsm6dsx_update_watermark(sensor, val);
1998 mutex_unlock(&hw->conf_lock);
2003 sensor->watermark = val;
2009 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
2010 struct device_attribute *attr,
2013 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_to_iio_dev(dev));
2014 const struct st_lsm6dsx_odr_table_entry *odr_table;
2017 odr_table = &sensor->hw->settings->odr_table[sensor->id];
2018 for (i = 0; i < odr_table->odr_len; i++)
2019 len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%03d ",
2020 odr_table->odr_avl[i].milli_hz / 1000,
2021 odr_table->odr_avl[i].milli_hz % 1000);
2022 buf[len - 1] = '\n';
2027 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
2028 struct device_attribute *attr,
2031 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_to_iio_dev(dev));
2032 const struct st_lsm6dsx_fs_table_entry *fs_table;
2033 struct st_lsm6dsx_hw *hw = sensor->hw;
2036 fs_table = &hw->settings->fs_table[sensor->id];
2037 for (i = 0; i < fs_table->fs_len; i++)
2038 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%09u ",
2039 fs_table->fs_avl[i].gain);
2040 buf[len - 1] = '\n';
2045 static int st_lsm6dsx_write_raw_get_fmt(struct iio_dev *indio_dev,
2046 struct iio_chan_spec const *chan,
2050 case IIO_CHAN_INFO_SCALE:
2051 switch (chan->type) {
2054 return IIO_VAL_INT_PLUS_NANO;
2056 return IIO_VAL_INT_PLUS_MICRO;
2059 return IIO_VAL_INT_PLUS_MICRO;
2063 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
2064 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
2065 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
2066 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
2067 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
2069 static struct attribute *st_lsm6dsx_acc_attributes[] = {
2070 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
2071 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
2075 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
2076 .attrs = st_lsm6dsx_acc_attributes,
2079 static const struct iio_info st_lsm6dsx_acc_info = {
2080 .attrs = &st_lsm6dsx_acc_attribute_group,
2081 .read_raw = st_lsm6dsx_read_raw,
2082 .write_raw = st_lsm6dsx_write_raw,
2083 .read_event_value = st_lsm6dsx_read_event,
2084 .write_event_value = st_lsm6dsx_write_event,
2085 .read_event_config = st_lsm6dsx_read_event_config,
2086 .write_event_config = st_lsm6dsx_write_event_config,
2087 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
2088 .write_raw_get_fmt = st_lsm6dsx_write_raw_get_fmt,
2091 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
2092 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
2093 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
2097 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
2098 .attrs = st_lsm6dsx_gyro_attributes,
2101 static const struct iio_info st_lsm6dsx_gyro_info = {
2102 .attrs = &st_lsm6dsx_gyro_attribute_group,
2103 .read_raw = st_lsm6dsx_read_raw,
2104 .write_raw = st_lsm6dsx_write_raw,
2105 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
2106 .write_raw_get_fmt = st_lsm6dsx_write_raw_get_fmt,
2109 static int st_lsm6dsx_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
2111 struct device *dev = hw->dev;
2113 if (!dev_fwnode(dev))
2116 return device_property_read_u32(dev, "st,drdy-int-pin", drdy_pin);
2120 st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw,
2121 const struct st_lsm6dsx_reg **drdy_reg)
2123 int err = 0, drdy_pin;
2125 if (st_lsm6dsx_get_drdy_pin(hw, &drdy_pin) < 0) {
2126 struct st_sensors_platform_data *pdata;
2127 struct device *dev = hw->dev;
2129 pdata = (struct st_sensors_platform_data *)dev->platform_data;
2130 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
2135 hw->irq_routing = &hw->settings->irq_config.irq1_func;
2136 *drdy_reg = &hw->settings->irq_config.irq1;
2139 hw->irq_routing = &hw->settings->irq_config.irq2_func;
2140 *drdy_reg = &hw->settings->irq_config.irq2;
2143 dev_err(hw->dev, "unsupported data ready pin\n");
2151 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
2153 const struct st_lsm6dsx_shub_settings *hub_settings;
2154 struct st_sensors_platform_data *pdata;
2155 struct device *dev = hw->dev;
2159 hub_settings = &hw->settings->shub_settings;
2161 pdata = (struct st_sensors_platform_data *)dev->platform_data;
2162 if ((dev_fwnode(dev) && device_property_read_bool(dev, "st,pullups")) ||
2163 (pdata && pdata->pullups)) {
2164 if (hub_settings->pullup_en.sec_page) {
2165 err = st_lsm6dsx_set_page(hw, true);
2170 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
2171 err = regmap_update_bits(hw->regmap,
2172 hub_settings->pullup_en.addr,
2173 hub_settings->pullup_en.mask, data);
2175 if (hub_settings->pullup_en.sec_page)
2176 st_lsm6dsx_set_page(hw, false);
2182 if (hub_settings->aux_sens.addr) {
2183 /* configure aux sensors */
2184 err = st_lsm6dsx_set_page(hw, true);
2188 data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
2189 err = regmap_update_bits(hw->regmap,
2190 hub_settings->aux_sens.addr,
2191 hub_settings->aux_sens.mask, data);
2193 st_lsm6dsx_set_page(hw, false);
2199 if (hub_settings->emb_func.addr) {
2200 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->emb_func.mask);
2201 err = regmap_update_bits(hw->regmap,
2202 hub_settings->emb_func.addr,
2203 hub_settings->emb_func.mask, data);
2209 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
2211 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
2214 ts_settings = &hw->settings->ts_settings;
2215 /* enable hw timestamp generation if necessary */
2216 if (ts_settings->timer_en.addr) {
2217 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
2218 err = regmap_update_bits(hw->regmap,
2219 ts_settings->timer_en.addr,
2220 ts_settings->timer_en.mask, val);
2225 /* enable high resolution for hw ts timer if necessary */
2226 if (ts_settings->hr_timer.addr) {
2227 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
2228 err = regmap_update_bits(hw->regmap,
2229 ts_settings->hr_timer.addr,
2230 ts_settings->hr_timer.mask, val);
2235 /* enable ts queueing in FIFO if necessary */
2236 if (ts_settings->fifo_en.addr) {
2237 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
2238 err = regmap_update_bits(hw->regmap,
2239 ts_settings->fifo_en.addr,
2240 ts_settings->fifo_en.mask, val);
2245 /* calibrate timestamp sensitivity */
2246 hw->ts_gain = ST_LSM6DSX_TS_SENSITIVITY;
2247 if (ts_settings->freq_fine) {
2248 err = regmap_read(hw->regmap, ts_settings->freq_fine, &val);
2253 * linearize the AN5192 formula:
2254 * 1 / (1 + x) ~= 1 - x (Taylor’s Series)
2255 * ttrim[s] = 1 / (40000 * (1 + 0.0015 * val))
2256 * ttrim[ns] ~= 25000 - 37.5 * val
2257 * ttrim[ns] ~= 25000 - (37500 * val) / 1000
2259 hw->ts_gain -= ((s8)val * 37500) / 1000;
2265 static int st_lsm6dsx_reset_device(struct st_lsm6dsx_hw *hw)
2267 const struct st_lsm6dsx_reg *reg;
2271 * flush hw FIFO before device reset in order to avoid
2272 * possible races on interrupt line 1. If the first interrupt
2273 * line is asserted during hw reset the device will work in
2274 * I3C-only mode (if it is supported)
2276 err = st_lsm6dsx_flush_fifo(hw);
2277 if (err < 0 && err != -ENOTSUPP)
2280 /* device sw reset */
2281 reg = &hw->settings->reset;
2282 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2283 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2289 /* reload trimming parameter */
2290 reg = &hw->settings->boot;
2291 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2292 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2301 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
2303 const struct st_lsm6dsx_reg *reg;
2306 err = st_lsm6dsx_reset_device(hw);
2310 /* enable Block Data Update */
2311 reg = &hw->settings->bdu;
2312 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2313 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2317 /* enable FIFO watermak interrupt */
2318 err = st_lsm6dsx_get_drdy_reg(hw, ®);
2322 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2323 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2327 /* enable Latched interrupts for device events */
2328 if (hw->settings->irq_config.lir.addr) {
2329 reg = &hw->settings->irq_config.lir;
2330 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2331 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2335 /* enable clear on read for latched interrupts */
2336 if (hw->settings->irq_config.clear_on_read.addr) {
2337 reg = &hw->settings->irq_config.clear_on_read;
2338 err = regmap_update_bits(hw->regmap,
2339 reg->addr, reg->mask,
2340 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2346 /* enable drdy-mas if available */
2347 if (hw->settings->drdy_mask.addr) {
2348 reg = &hw->settings->drdy_mask;
2349 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2350 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2355 err = st_lsm6dsx_init_shub(hw);
2359 return st_lsm6dsx_init_hw_timer(hw);
2362 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
2363 enum st_lsm6dsx_sensor_id id,
2366 struct st_lsm6dsx_sensor *sensor;
2367 struct iio_dev *iio_dev;
2369 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
2373 iio_dev->modes = INDIO_DIRECT_MODE;
2374 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
2375 iio_dev->channels = hw->settings->channels[id].chan;
2376 iio_dev->num_channels = hw->settings->channels[id].len;
2378 sensor = iio_priv(iio_dev);
2381 sensor->odr = hw->settings->odr_table[id].odr_avl[0].milli_hz;
2382 sensor->gain = hw->settings->fs_table[id].fs_avl[0].gain;
2383 sensor->watermark = 1;
2386 case ST_LSM6DSX_ID_ACC:
2387 iio_dev->info = &st_lsm6dsx_acc_info;
2388 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
2391 case ST_LSM6DSX_ID_GYRO:
2392 iio_dev->info = &st_lsm6dsx_gyro_info;
2393 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
2399 iio_dev->name = sensor->name;
2405 st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw)
2407 const struct st_lsm6dsx_event_settings *event_settings;
2411 if (!hw->enable_event)
2414 event_settings = &hw->settings->event_settings;
2415 err = st_lsm6dsx_read_locked(hw, event_settings->wakeup_src_reg,
2416 &data, sizeof(data));
2420 timestamp = iio_get_time_ns(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
2421 if ((data & hw->settings->event_settings.wakeup_src_z_mask) &&
2422 (hw->enable_event & BIT(IIO_MOD_Z)))
2423 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2424 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2431 if ((data & hw->settings->event_settings.wakeup_src_y_mask) &&
2432 (hw->enable_event & BIT(IIO_MOD_Y)))
2433 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2434 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2441 if ((data & hw->settings->event_settings.wakeup_src_x_mask) &&
2442 (hw->enable_event & BIT(IIO_MOD_X)))
2443 iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC],
2444 IIO_MOD_EVENT_CODE(IIO_ACCEL,
2451 return data & event_settings->wakeup_src_status_mask;
2454 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
2456 struct st_lsm6dsx_hw *hw = private;
2457 int fifo_len = 0, len;
2460 event = st_lsm6dsx_report_motion_event(hw);
2462 if (!hw->settings->fifo_ops.read_fifo)
2463 return event ? IRQ_HANDLED : IRQ_NONE;
2466 * If we are using edge IRQs, new samples can arrive while
2467 * processing current interrupt since there are no hw
2468 * guarantees the irq line stays "low" long enough to properly
2469 * detect the new interrupt. In this case the new sample will
2471 * Polling FIFO status register allow us to read new
2472 * samples even if the interrupt arrives while processing
2473 * previous data and the timeslot where the line is "low" is
2474 * too short to be properly detected.
2477 mutex_lock(&hw->fifo_lock);
2478 len = hw->settings->fifo_ops.read_fifo(hw);
2479 mutex_unlock(&hw->fifo_lock);
2485 return fifo_len || event ? IRQ_HANDLED : IRQ_NONE;
2488 static irqreturn_t st_lsm6dsx_sw_trigger_handler_thread(int irq,
2491 struct iio_poll_func *pf = private;
2492 struct iio_dev *iio_dev = pf->indio_dev;
2493 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
2494 struct st_lsm6dsx_hw *hw = sensor->hw;
2496 if (sensor->id == ST_LSM6DSX_ID_EXT0 ||
2497 sensor->id == ST_LSM6DSX_ID_EXT1 ||
2498 sensor->id == ST_LSM6DSX_ID_EXT2)
2499 st_lsm6dsx_shub_read_output(hw,
2500 (u8 *)hw->scan[sensor->id].channels,
2501 sizeof(hw->scan[sensor->id].channels));
2503 st_lsm6dsx_read_locked(hw, iio_dev->channels[0].address,
2504 hw->scan[sensor->id].channels,
2505 sizeof(hw->scan[sensor->id].channels));
2507 iio_push_to_buffers_with_timestamp(iio_dev, &hw->scan[sensor->id],
2508 iio_get_time_ns(iio_dev));
2509 iio_trigger_notify_done(iio_dev->trig);
2514 static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw *hw)
2516 struct st_sensors_platform_data *pdata;
2517 const struct st_lsm6dsx_reg *reg;
2518 struct device *dev = hw->dev;
2519 unsigned long irq_type;
2520 bool irq_active_low;
2523 irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
2526 case IRQF_TRIGGER_HIGH:
2527 case IRQF_TRIGGER_RISING:
2528 irq_active_low = false;
2530 case IRQF_TRIGGER_LOW:
2531 case IRQF_TRIGGER_FALLING:
2532 irq_active_low = true;
2535 dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
2539 reg = &hw->settings->irq_config.hla;
2540 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2541 ST_LSM6DSX_SHIFT_VAL(irq_active_low,
2546 pdata = (struct st_sensors_platform_data *)dev->platform_data;
2547 if ((dev_fwnode(dev) && device_property_read_bool(dev, "drive-open-drain")) ||
2548 (pdata && pdata->open_drain)) {
2549 reg = &hw->settings->irq_config.od;
2550 err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
2551 ST_LSM6DSX_SHIFT_VAL(1, reg->mask));
2555 irq_type |= IRQF_SHARED;
2558 err = devm_request_threaded_irq(hw->dev, hw->irq,
2560 st_lsm6dsx_handler_thread,
2561 irq_type | IRQF_ONESHOT,
2564 dev_err(hw->dev, "failed to request trigger irq %d\n",
2572 static int st_lsm6dsx_sw_buffer_preenable(struct iio_dev *iio_dev)
2574 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
2576 return st_lsm6dsx_device_set_enable(sensor, true);
2579 static int st_lsm6dsx_sw_buffer_postdisable(struct iio_dev *iio_dev)
2581 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
2583 return st_lsm6dsx_device_set_enable(sensor, false);
2586 static const struct iio_buffer_setup_ops st_lsm6dsx_sw_buffer_ops = {
2587 .preenable = st_lsm6dsx_sw_buffer_preenable,
2588 .postdisable = st_lsm6dsx_sw_buffer_postdisable,
2591 static int st_lsm6dsx_sw_buffers_setup(struct st_lsm6dsx_hw *hw)
2595 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2598 if (!hw->iio_devs[i])
2601 err = devm_iio_triggered_buffer_setup(hw->dev,
2602 hw->iio_devs[i], NULL,
2603 st_lsm6dsx_sw_trigger_handler_thread,
2604 &st_lsm6dsx_sw_buffer_ops);
2612 static int st_lsm6dsx_init_regulators(struct device *dev)
2614 /* vdd-vddio power regulators */
2615 static const char * const regulators[] = { "vdd", "vddio" };
2618 err = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulators),
2621 return dev_err_probe(dev, err, "failed to enable regulators\n");
2630 static int lsm6dsx_get_acpi_mount_matrix(struct device *dev,
2631 struct iio_mount_matrix *orientation)
2633 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
2634 struct acpi_device *adev = ACPI_COMPANION(dev);
2635 union acpi_object *obj, *elements;
2640 if (!has_acpi_companion(dev))
2643 if (!acpi_has_method(adev->handle, "ROTM"))
2646 status = acpi_evaluate_object(adev->handle, "ROTM", NULL, &buffer);
2647 if (ACPI_FAILURE(status)) {
2648 dev_warn(dev, "Failed to get ACPI mount matrix: %d\n", status);
2652 obj = buffer.pointer;
2653 if (obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 3)
2654 goto unknown_format;
2656 elements = obj->package.elements;
2657 for (i = 0; i < 3; i++) {
2658 if (elements[i].type != ACPI_TYPE_STRING)
2659 goto unknown_format;
2661 str = elements[i].string.pointer;
2662 if (sscanf(str, "%d %d %d", &val[0], &val[1], &val[2]) != 3)
2663 goto unknown_format;
2665 for (j = 0; j < 3; j++) {
2667 case -1: str = "-1"; break;
2668 case 0: str = "0"; break;
2669 case 1: str = "1"; break;
2670 default: goto unknown_format;
2672 orientation->rotation[i * 3 + j] = str;
2676 kfree(buffer.pointer);
2680 dev_warn(dev, "Unknown ACPI mount matrix format, ignoring\n");
2681 kfree(buffer.pointer);
2687 static int lsm6dsx_get_acpi_mount_matrix(struct device *dev,
2688 struct iio_mount_matrix *orientation)
2695 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id,
2696 struct regmap *regmap)
2698 struct st_sensors_platform_data *pdata = dev->platform_data;
2699 const struct st_lsm6dsx_shub_settings *hub_settings;
2700 struct st_lsm6dsx_hw *hw;
2701 const char *name = NULL;
2704 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
2708 dev_set_drvdata(dev, (void *)hw);
2710 mutex_init(&hw->fifo_lock);
2711 mutex_init(&hw->conf_lock);
2712 mutex_init(&hw->page_lock);
2714 err = st_lsm6dsx_init_regulators(dev);
2718 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
2724 hw->regmap = regmap;
2726 err = st_lsm6dsx_check_whoami(hw, hw_id, &name);
2730 for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
2731 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
2732 if (!hw->iio_devs[i])
2736 err = st_lsm6dsx_init_device(hw);
2740 hub_settings = &hw->settings->shub_settings;
2741 if (hub_settings->master_en.addr &&
2742 (!dev_fwnode(dev) ||
2743 !device_property_read_bool(dev, "st,disable-sensor-hub"))) {
2744 err = st_lsm6dsx_shub_probe(hw, name);
2750 err = st_lsm6dsx_irq_setup(hw);
2754 err = st_lsm6dsx_fifo_setup(hw);
2759 if (!hw->irq || !hw->settings->fifo_ops.read_fifo) {
2761 * Rely on sw triggers (e.g. hr-timers) if irq pin is not
2762 * connected of if the device does not support HW FIFO
2764 err = st_lsm6dsx_sw_buffers_setup(hw);
2769 err = lsm6dsx_get_acpi_mount_matrix(hw->dev, &hw->orientation);
2771 err = iio_read_mount_matrix(hw->dev, &hw->orientation);
2776 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2777 if (!hw->iio_devs[i])
2780 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
2785 if ((dev_fwnode(dev) && device_property_read_bool(dev, "wakeup-source")) ||
2786 (pdata && pdata->wakeup_source))
2787 device_init_wakeup(dev, true);
2791 EXPORT_SYMBOL_NS(st_lsm6dsx_probe, IIO_LSM6DSX);
2793 static int st_lsm6dsx_suspend(struct device *dev)
2795 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2796 struct st_lsm6dsx_sensor *sensor;
2799 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2800 if (!hw->iio_devs[i])
2803 sensor = iio_priv(hw->iio_devs[i]);
2804 if (!(hw->enable_mask & BIT(sensor->id)))
2807 if (device_may_wakeup(dev) &&
2808 sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event) {
2809 /* Enable wake from IRQ */
2810 enable_irq_wake(hw->irq);
2814 err = st_lsm6dsx_device_set_enable(sensor, false);
2818 hw->suspend_mask |= BIT(sensor->id);
2822 err = st_lsm6dsx_flush_fifo(hw);
2827 static int st_lsm6dsx_resume(struct device *dev)
2829 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
2830 struct st_lsm6dsx_sensor *sensor;
2833 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
2834 if (!hw->iio_devs[i])
2837 sensor = iio_priv(hw->iio_devs[i]);
2838 if (device_may_wakeup(dev) &&
2839 sensor->id == ST_LSM6DSX_ID_ACC && hw->enable_event)
2840 disable_irq_wake(hw->irq);
2842 if (!(hw->suspend_mask & BIT(sensor->id)))
2845 err = st_lsm6dsx_device_set_enable(sensor, true);
2849 hw->suspend_mask &= ~BIT(sensor->id);
2853 err = st_lsm6dsx_resume_fifo(hw);
2858 EXPORT_NS_SIMPLE_DEV_PM_OPS(st_lsm6dsx_pm_ops, st_lsm6dsx_suspend,
2859 st_lsm6dsx_resume, IIO_LSM6DSX);
2861 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
2862 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
2863 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
2864 MODULE_LICENSE("GPL v2");