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]: 13, 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]: 13, 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:
30 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
31 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
32 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
36 * - Accelerometer supported ODR [Hz]: 10, 50, 119, 238, 476, 952
37 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
38 * - Gyroscope supported ODR [Hz]: 15, 60, 119, 238, 476, 952
39 * - Gyroscope supported full-scale [dps]: +-245/+-500/+-2000
42 * Copyright 2016 STMicroelectronics Inc.
44 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
45 * Denis Ciocca <denis.ciocca@st.com>
48 #include <linux/kernel.h>
49 #include <linux/module.h>
50 #include <linux/delay.h>
51 #include <linux/iio/events.h>
52 #include <linux/iio/iio.h>
53 #include <linux/iio/sysfs.h>
54 #include <linux/interrupt.h>
55 #include <linux/irq.h>
57 #include <linux/property.h>
58 #include <linux/regmap.h>
59 #include <linux/bitfield.h>
61 #include <linux/platform_data/st_sensors_pdata.h>
63 #include "st_lsm6dsx.h"
65 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
67 #define ST_LSM6DSX_TS_SENSITIVITY 25000UL /* 25us */
69 static const struct iio_chan_spec st_lsm6dsx_acc_channels
[] = {
70 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL
, 0x28, IIO_MOD_X
, 0),
71 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL
, 0x2a, IIO_MOD_Y
, 1),
72 ST_LSM6DSX_CHANNEL_ACC(IIO_ACCEL
, 0x2c, IIO_MOD_Z
, 2),
73 IIO_CHAN_SOFT_TIMESTAMP(3),
76 static const struct iio_chan_spec st_lsm6dsx_gyro_channels
[] = {
77 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x22, IIO_MOD_X
, 0),
78 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x24, IIO_MOD_Y
, 1),
79 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x26, IIO_MOD_Z
, 2),
80 IIO_CHAN_SOFT_TIMESTAMP(3),
83 static const struct iio_chan_spec st_lsm6ds0_gyro_channels
[] = {
84 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x18, IIO_MOD_X
, 0),
85 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x1a, IIO_MOD_Y
, 1),
86 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, 0x1c, IIO_MOD_Z
, 2),
87 IIO_CHAN_SOFT_TIMESTAMP(3),
90 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings
[] = {
108 .hw_id
= ST_LSM9DS1_ID
,
109 .name
= ST_LSM9DS1_DEV_NAME
,
111 .hw_id
= ST_LSM6DS0_ID
,
112 .name
= ST_LSM6DS0_DEV_NAME
,
116 [ST_LSM6DSX_ID_ACC
] = {
117 .chan
= st_lsm6dsx_acc_channels
,
118 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
120 [ST_LSM6DSX_ID_GYRO
] = {
121 .chan
= st_lsm6ds0_gyro_channels
,
122 .len
= ARRAY_SIZE(st_lsm6ds0_gyro_channels
),
126 [ST_LSM6DSX_ID_ACC
] = {
129 .mask
= GENMASK(7, 5),
131 .odr_avl
[0] = { 10000, 0x01 },
132 .odr_avl
[1] = { 50000, 0x02 },
133 .odr_avl
[2] = { 119000, 0x03 },
134 .odr_avl
[3] = { 238000, 0x04 },
135 .odr_avl
[4] = { 476000, 0x05 },
136 .odr_avl
[5] = { 952000, 0x06 },
139 [ST_LSM6DSX_ID_GYRO
] = {
142 .mask
= GENMASK(7, 5),
144 .odr_avl
[0] = { 14900, 0x01 },
145 .odr_avl
[1] = { 59500, 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 },
154 [ST_LSM6DSX_ID_ACC
] = {
157 .mask
= GENMASK(4, 3),
159 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
160 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
161 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
162 .fs_avl
[3] = { IIO_G_TO_M_S_2(732), 0x1 },
165 [ST_LSM6DSX_ID_GYRO
] = {
168 .mask
= GENMASK(4, 3),
171 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
172 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
173 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
210 .max_fifo_size
= 1365,
213 .hw_id
= ST_LSM6DS3_ID
,
214 .name
= ST_LSM6DS3_DEV_NAME
,
218 [ST_LSM6DSX_ID_ACC
] = {
219 .chan
= st_lsm6dsx_acc_channels
,
220 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
222 [ST_LSM6DSX_ID_GYRO
] = {
223 .chan
= st_lsm6dsx_gyro_channels
,
224 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
228 [ST_LSM6DSX_ID_ACC
] = {
231 .mask
= GENMASK(7, 4),
233 .odr_avl
[0] = { 12500, 0x01 },
234 .odr_avl
[1] = { 26000, 0x02 },
235 .odr_avl
[2] = { 52000, 0x03 },
236 .odr_avl
[3] = { 104000, 0x04 },
237 .odr_avl
[4] = { 208000, 0x05 },
238 .odr_avl
[5] = { 416000, 0x06 },
241 [ST_LSM6DSX_ID_GYRO
] = {
244 .mask
= GENMASK(7, 4),
246 .odr_avl
[0] = { 12500, 0x01 },
247 .odr_avl
[1] = { 26000, 0x02 },
248 .odr_avl
[2] = { 52000, 0x03 },
249 .odr_avl
[3] = { 104000, 0x04 },
250 .odr_avl
[4] = { 208000, 0x05 },
251 .odr_avl
[5] = { 416000, 0x06 },
256 [ST_LSM6DSX_ID_ACC
] = {
259 .mask
= GENMASK(3, 2),
261 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
262 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
263 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
264 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
267 [ST_LSM6DSX_ID_GYRO
] = {
270 .mask
= GENMASK(3, 2),
272 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
273 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
274 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
275 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
310 [ST_LSM6DSX_ID_ACC
] = {
312 .mask
= GENMASK(2, 0),
314 [ST_LSM6DSX_ID_GYRO
] = {
316 .mask
= GENMASK(5, 3),
320 .update_fifo
= st_lsm6dsx_update_fifo
,
321 .read_fifo
= st_lsm6dsx_read_fifo
,
324 .mask
= GENMASK(11, 0),
328 .mask
= GENMASK(11, 0),
330 .th_wl
= 3, /* 1LSB = 2B */
347 .mask
= GENMASK(5, 3),
353 .mask
= GENMASK(5, 0),
355 .wakeup_src_reg
= 0x1b,
356 .wakeup_src_status_mask
= BIT(3),
357 .wakeup_src_z_mask
= BIT(0),
358 .wakeup_src_y_mask
= BIT(1),
359 .wakeup_src_x_mask
= BIT(2),
376 .max_fifo_size
= 682,
379 .hw_id
= ST_LSM6DS3H_ID
,
380 .name
= ST_LSM6DS3H_DEV_NAME
,
384 [ST_LSM6DSX_ID_ACC
] = {
385 .chan
= st_lsm6dsx_acc_channels
,
386 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
388 [ST_LSM6DSX_ID_GYRO
] = {
389 .chan
= st_lsm6dsx_gyro_channels
,
390 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
394 [ST_LSM6DSX_ID_ACC
] = {
397 .mask
= GENMASK(7, 4),
399 .odr_avl
[0] = { 12500, 0x01 },
400 .odr_avl
[1] = { 26000, 0x02 },
401 .odr_avl
[2] = { 52000, 0x03 },
402 .odr_avl
[3] = { 104000, 0x04 },
403 .odr_avl
[4] = { 208000, 0x05 },
404 .odr_avl
[5] = { 416000, 0x06 },
407 [ST_LSM6DSX_ID_GYRO
] = {
410 .mask
= GENMASK(7, 4),
412 .odr_avl
[0] = { 12500, 0x01 },
413 .odr_avl
[1] = { 26000, 0x02 },
414 .odr_avl
[2] = { 52000, 0x03 },
415 .odr_avl
[3] = { 104000, 0x04 },
416 .odr_avl
[4] = { 208000, 0x05 },
417 .odr_avl
[5] = { 416000, 0x06 },
422 [ST_LSM6DSX_ID_ACC
] = {
425 .mask
= GENMASK(3, 2),
427 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
428 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
429 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
430 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
433 [ST_LSM6DSX_ID_GYRO
] = {
436 .mask
= GENMASK(3, 2),
438 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
439 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
440 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
441 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
476 [ST_LSM6DSX_ID_ACC
] = {
478 .mask
= GENMASK(2, 0),
480 [ST_LSM6DSX_ID_GYRO
] = {
482 .mask
= GENMASK(5, 3),
486 .update_fifo
= st_lsm6dsx_update_fifo
,
487 .read_fifo
= st_lsm6dsx_read_fifo
,
490 .mask
= GENMASK(11, 0),
494 .mask
= GENMASK(11, 0),
496 .th_wl
= 3, /* 1LSB = 2B */
513 .mask
= GENMASK(5, 3),
519 .mask
= GENMASK(5, 0),
521 .wakeup_src_reg
= 0x1b,
522 .wakeup_src_status_mask
= BIT(3),
523 .wakeup_src_z_mask
= BIT(0),
524 .wakeup_src_y_mask
= BIT(1),
525 .wakeup_src_x_mask
= BIT(2),
542 .max_fifo_size
= 682,
545 .hw_id
= ST_LSM6DSL_ID
,
546 .name
= ST_LSM6DSL_DEV_NAME
,
548 .hw_id
= ST_LSM6DSM_ID
,
549 .name
= ST_LSM6DSM_DEV_NAME
,
551 .hw_id
= ST_ISM330DLC_ID
,
552 .name
= ST_ISM330DLC_DEV_NAME
,
554 .hw_id
= ST_LSM6DS3TRC_ID
,
555 .name
= ST_LSM6DS3TRC_DEV_NAME
,
559 [ST_LSM6DSX_ID_ACC
] = {
560 .chan
= st_lsm6dsx_acc_channels
,
561 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
563 [ST_LSM6DSX_ID_GYRO
] = {
564 .chan
= st_lsm6dsx_gyro_channels
,
565 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
569 [ST_LSM6DSX_ID_ACC
] = {
572 .mask
= GENMASK(7, 4),
574 .odr_avl
[0] = { 12500, 0x01 },
575 .odr_avl
[1] = { 26000, 0x02 },
576 .odr_avl
[2] = { 52000, 0x03 },
577 .odr_avl
[3] = { 104000, 0x04 },
578 .odr_avl
[4] = { 208000, 0x05 },
579 .odr_avl
[5] = { 416000, 0x06 },
582 [ST_LSM6DSX_ID_GYRO
] = {
585 .mask
= GENMASK(7, 4),
587 .odr_avl
[0] = { 12500, 0x01 },
588 .odr_avl
[1] = { 26000, 0x02 },
589 .odr_avl
[2] = { 52000, 0x03 },
590 .odr_avl
[3] = { 104000, 0x04 },
591 .odr_avl
[4] = { 208000, 0x05 },
592 .odr_avl
[5] = { 416000, 0x06 },
597 [ST_LSM6DSX_ID_ACC
] = {
600 .mask
= GENMASK(3, 2),
602 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
603 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
604 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
605 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
608 [ST_LSM6DSX_ID_GYRO
] = {
611 .mask
= GENMASK(3, 2),
613 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
614 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
615 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
616 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
651 [ST_LSM6DSX_ID_ACC
] = {
653 .mask
= GENMASK(2, 0),
655 [ST_LSM6DSX_ID_GYRO
] = {
657 .mask
= GENMASK(5, 3),
659 [ST_LSM6DSX_ID_EXT0
] = {
661 .mask
= GENMASK(2, 0),
665 .update_fifo
= st_lsm6dsx_update_fifo
,
666 .read_fifo
= st_lsm6dsx_read_fifo
,
669 .mask
= GENMASK(10, 0),
673 .mask
= GENMASK(10, 0),
675 .th_wl
= 3, /* 1LSB = 2B */
692 .mask
= GENMASK(5, 3),
710 .mask
= GENMASK(5, 4),
725 .dw_slv0_addr
= 0x0e,
735 .mask
= GENMASK(5, 0),
737 .wakeup_src_reg
= 0x1b,
738 .wakeup_src_status_mask
= BIT(3),
739 .wakeup_src_z_mask
= BIT(0),
740 .wakeup_src_y_mask
= BIT(1),
741 .wakeup_src_x_mask
= BIT(2),
758 .max_fifo_size
= 512,
761 .hw_id
= ST_LSM6DSO_ID
,
762 .name
= ST_LSM6DSO_DEV_NAME
,
764 .hw_id
= ST_LSM6DSOX_ID
,
765 .name
= ST_LSM6DSOX_DEV_NAME
,
769 [ST_LSM6DSX_ID_ACC
] = {
770 .chan
= st_lsm6dsx_acc_channels
,
771 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
773 [ST_LSM6DSX_ID_GYRO
] = {
774 .chan
= st_lsm6dsx_gyro_channels
,
775 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
783 [ST_LSM6DSX_ID_ACC
] = {
786 .mask
= GENMASK(7, 4),
788 .odr_avl
[0] = { 12500, 0x01 },
789 .odr_avl
[1] = { 26000, 0x02 },
790 .odr_avl
[2] = { 52000, 0x03 },
791 .odr_avl
[3] = { 104000, 0x04 },
792 .odr_avl
[4] = { 208000, 0x05 },
793 .odr_avl
[5] = { 416000, 0x06 },
796 [ST_LSM6DSX_ID_GYRO
] = {
799 .mask
= GENMASK(7, 4),
801 .odr_avl
[0] = { 12500, 0x01 },
802 .odr_avl
[1] = { 26000, 0x02 },
803 .odr_avl
[2] = { 52000, 0x03 },
804 .odr_avl
[3] = { 104000, 0x04 },
805 .odr_avl
[4] = { 208000, 0x05 },
806 .odr_avl
[5] = { 416000, 0x06 },
811 [ST_LSM6DSX_ID_ACC
] = {
814 .mask
= GENMASK(3, 2),
816 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
817 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
818 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
819 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
822 [ST_LSM6DSX_ID_GYRO
] = {
825 .mask
= GENMASK(3, 2),
827 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
828 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
829 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
830 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
869 [ST_LSM6DSX_ID_ACC
] = {
871 .mask
= GENMASK(3, 0),
873 [ST_LSM6DSX_ID_GYRO
] = {
875 .mask
= GENMASK(7, 4),
879 .update_fifo
= st_lsm6dsx_update_fifo
,
880 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
883 .mask
= GENMASK(8, 0),
887 .mask
= GENMASK(9, 0),
898 .mask
= GENMASK(7, 6),
919 .mask
= GENMASK(1, 0),
931 .dw_slv0_addr
= 0x21,
941 .mask
= GENMASK(5, 0),
943 .wakeup_src_reg
= 0x1b,
944 .wakeup_src_status_mask
= BIT(3),
945 .wakeup_src_z_mask
= BIT(0),
946 .wakeup_src_y_mask
= BIT(1),
947 .wakeup_src_x_mask
= BIT(2),
964 .max_fifo_size
= 512,
967 .hw_id
= ST_ASM330LHH_ID
,
968 .name
= ST_ASM330LHH_DEV_NAME
,
972 [ST_LSM6DSX_ID_ACC
] = {
973 .chan
= st_lsm6dsx_acc_channels
,
974 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
976 [ST_LSM6DSX_ID_GYRO
] = {
977 .chan
= st_lsm6dsx_gyro_channels
,
978 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
986 [ST_LSM6DSX_ID_ACC
] = {
989 .mask
= GENMASK(7, 4),
991 .odr_avl
[0] = { 12500, 0x01 },
992 .odr_avl
[1] = { 26000, 0x02 },
993 .odr_avl
[2] = { 52000, 0x03 },
994 .odr_avl
[3] = { 104000, 0x04 },
995 .odr_avl
[4] = { 208000, 0x05 },
996 .odr_avl
[5] = { 416000, 0x06 },
999 [ST_LSM6DSX_ID_GYRO
] = {
1002 .mask
= GENMASK(7, 4),
1004 .odr_avl
[0] = { 12500, 0x01 },
1005 .odr_avl
[1] = { 26000, 0x02 },
1006 .odr_avl
[2] = { 52000, 0x03 },
1007 .odr_avl
[3] = { 104000, 0x04 },
1008 .odr_avl
[4] = { 208000, 0x05 },
1009 .odr_avl
[5] = { 416000, 0x06 },
1014 [ST_LSM6DSX_ID_ACC
] = {
1017 .mask
= GENMASK(3, 2),
1019 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
1020 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
1021 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
1022 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
1025 [ST_LSM6DSX_ID_GYRO
] = {
1028 .mask
= GENMASK(3, 2),
1030 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
1031 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
1032 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
1033 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
1072 [ST_LSM6DSX_ID_ACC
] = {
1074 .mask
= GENMASK(3, 0),
1076 [ST_LSM6DSX_ID_GYRO
] = {
1078 .mask
= GENMASK(7, 4),
1082 .update_fifo
= st_lsm6dsx_update_fifo
,
1083 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
1086 .mask
= GENMASK(8, 0),
1090 .mask
= GENMASK(9, 0),
1101 .mask
= GENMASK(7, 6),
1112 .mask
= GENMASK(5, 0),
1114 .wakeup_src_reg
= 0x1b,
1115 .wakeup_src_status_mask
= BIT(3),
1116 .wakeup_src_z_mask
= BIT(0),
1117 .wakeup_src_y_mask
= BIT(1),
1118 .wakeup_src_x_mask
= BIT(2),
1135 .max_fifo_size
= 512,
1138 .hw_id
= ST_LSM6DSR_ID
,
1139 .name
= ST_LSM6DSR_DEV_NAME
,
1141 .hw_id
= ST_ISM330DHCX_ID
,
1142 .name
= ST_ISM330DHCX_DEV_NAME
,
1144 .hw_id
= ST_LSM6DSRX_ID
,
1145 .name
= ST_LSM6DSRX_DEV_NAME
,
1149 [ST_LSM6DSX_ID_ACC
] = {
1150 .chan
= st_lsm6dsx_acc_channels
,
1151 .len
= ARRAY_SIZE(st_lsm6dsx_acc_channels
),
1153 [ST_LSM6DSX_ID_GYRO
] = {
1154 .chan
= st_lsm6dsx_gyro_channels
,
1155 .len
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
),
1163 [ST_LSM6DSX_ID_ACC
] = {
1166 .mask
= GENMASK(7, 4),
1168 .odr_avl
[0] = { 12500, 0x01 },
1169 .odr_avl
[1] = { 26000, 0x02 },
1170 .odr_avl
[2] = { 52000, 0x03 },
1171 .odr_avl
[3] = { 104000, 0x04 },
1172 .odr_avl
[4] = { 208000, 0x05 },
1173 .odr_avl
[5] = { 416000, 0x06 },
1176 [ST_LSM6DSX_ID_GYRO
] = {
1179 .mask
= GENMASK(7, 4),
1181 .odr_avl
[0] = { 12500, 0x01 },
1182 .odr_avl
[1] = { 26000, 0x02 },
1183 .odr_avl
[2] = { 52000, 0x03 },
1184 .odr_avl
[3] = { 104000, 0x04 },
1185 .odr_avl
[4] = { 208000, 0x05 },
1186 .odr_avl
[5] = { 416000, 0x06 },
1191 [ST_LSM6DSX_ID_ACC
] = {
1194 .mask
= GENMASK(3, 2),
1196 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
1197 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
1198 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
1199 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
1202 [ST_LSM6DSX_ID_GYRO
] = {
1205 .mask
= GENMASK(3, 2),
1207 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
1208 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
1209 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
1210 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
1249 [ST_LSM6DSX_ID_ACC
] = {
1251 .mask
= GENMASK(3, 0),
1253 [ST_LSM6DSX_ID_GYRO
] = {
1255 .mask
= GENMASK(7, 4),
1259 .update_fifo
= st_lsm6dsx_update_fifo
,
1260 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
1263 .mask
= GENMASK(8, 0),
1267 .mask
= GENMASK(9, 0),
1278 .mask
= GENMASK(7, 6),
1299 .mask
= GENMASK(1, 0),
1311 .dw_slv0_addr
= 0x21,
1321 .mask
= GENMASK(5, 0),
1323 .wakeup_src_reg
= 0x1b,
1324 .wakeup_src_status_mask
= BIT(3),
1325 .wakeup_src_z_mask
= BIT(0),
1326 .wakeup_src_y_mask
= BIT(1),
1327 .wakeup_src_x_mask
= BIT(2),
1332 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw
*hw
, bool enable
)
1334 const struct st_lsm6dsx_shub_settings
*hub_settings
;
1338 hub_settings
= &hw
->settings
->shub_settings
;
1339 data
= ST_LSM6DSX_SHIFT_VAL(enable
, hub_settings
->page_mux
.mask
);
1340 err
= regmap_update_bits(hw
->regmap
, hub_settings
->page_mux
.addr
,
1341 hub_settings
->page_mux
.mask
, data
);
1342 usleep_range(100, 150);
1347 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw
*hw
, int id
,
1350 int err
, i
, j
, data
;
1352 for (i
= 0; i
< ARRAY_SIZE(st_lsm6dsx_sensor_settings
); i
++) {
1353 for (j
= 0; j
< ST_LSM6DSX_MAX_ID
; j
++) {
1354 if (st_lsm6dsx_sensor_settings
[i
].id
[j
].name
&&
1355 id
== st_lsm6dsx_sensor_settings
[i
].id
[j
].hw_id
)
1358 if (j
< ST_LSM6DSX_MAX_ID
)
1362 if (i
== ARRAY_SIZE(st_lsm6dsx_sensor_settings
)) {
1363 dev_err(hw
->dev
, "unsupported hw id [%02x]\n", id
);
1367 err
= regmap_read(hw
->regmap
, ST_LSM6DSX_REG_WHOAMI_ADDR
, &data
);
1369 dev_err(hw
->dev
, "failed to read whoami register\n");
1373 if (data
!= st_lsm6dsx_sensor_settings
[i
].wai
) {
1374 dev_err(hw
->dev
, "unsupported whoami [%02x]\n", data
);
1378 *name
= st_lsm6dsx_sensor_settings
[i
].id
[j
].name
;
1379 hw
->settings
= &st_lsm6dsx_sensor_settings
[i
];
1384 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor
*sensor
,
1387 const struct st_lsm6dsx_fs_table_entry
*fs_table
;
1391 fs_table
= &sensor
->hw
->settings
->fs_table
[sensor
->id
];
1392 for (i
= 0; i
< fs_table
->fs_len
; i
++) {
1393 if (fs_table
->fs_avl
[i
].gain
== gain
)
1397 if (i
== fs_table
->fs_len
)
1400 data
= ST_LSM6DSX_SHIFT_VAL(fs_table
->fs_avl
[i
].val
,
1401 fs_table
->reg
.mask
);
1402 err
= st_lsm6dsx_update_bits_locked(sensor
->hw
, fs_table
->reg
.addr
,
1403 fs_table
->reg
.mask
, data
);
1407 sensor
->gain
= gain
;
1412 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor
*sensor
, u32 odr
, u8
*val
)
1414 const struct st_lsm6dsx_odr_table_entry
*odr_table
;
1417 odr_table
= &sensor
->hw
->settings
->odr_table
[sensor
->id
];
1418 for (i
= 0; i
< odr_table
->odr_len
; i
++) {
1420 * ext devices can run at different odr respect to
1423 if (odr_table
->odr_avl
[i
].milli_hz
>= odr
)
1427 if (i
== odr_table
->odr_len
)
1430 *val
= odr_table
->odr_avl
[i
].val
;
1431 return odr_table
->odr_avl
[i
].milli_hz
;
1435 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw
*hw
, u32 odr
,
1436 enum st_lsm6dsx_sensor_id id
)
1438 struct st_lsm6dsx_sensor
*ref
= iio_priv(hw
->iio_devs
[id
]);
1441 if (hw
->enable_mask
& BIT(id
))
1442 return max_t(u32
, ref
->odr
, odr
);
1446 return (hw
->enable_mask
& BIT(id
)) ? ref
->odr
: 0;
1451 st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor
*sensor
, u32 req_odr
)
1453 struct st_lsm6dsx_sensor
*ref_sensor
= sensor
;
1454 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1455 const struct st_lsm6dsx_reg
*reg
;
1460 switch (sensor
->id
) {
1461 case ST_LSM6DSX_ID_EXT0
:
1462 case ST_LSM6DSX_ID_EXT1
:
1463 case ST_LSM6DSX_ID_EXT2
:
1464 case ST_LSM6DSX_ID_ACC
: {
1469 * i2c embedded controller relies on the accelerometer sensor as
1470 * bus read/write trigger so we need to enable accel device
1471 * at odr = max(accel_odr, ext_odr) in order to properly
1472 * communicate with i2c slave devices
1474 ref_sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
1475 for (i
= ST_LSM6DSX_ID_ACC
; i
< ST_LSM6DSX_ID_MAX
; i
++) {
1476 if (!hw
->iio_devs
[i
] || i
== sensor
->id
)
1479 odr
= st_lsm6dsx_check_odr_dependency(hw
, req_odr
, i
);
1481 /* device already configured */
1491 err
= st_lsm6dsx_check_odr(ref_sensor
, req_odr
, &val
);
1496 reg
= &hw
->settings
->odr_table
[ref_sensor
->id
].reg
;
1497 data
= ST_LSM6DSX_SHIFT_VAL(val
, reg
->mask
);
1498 return st_lsm6dsx_update_bits_locked(hw
, reg
->addr
, reg
->mask
, data
);
1502 __st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor
*sensor
,
1505 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1506 u32 odr
= enable
? sensor
->odr
: 0;
1509 err
= st_lsm6dsx_set_odr(sensor
, odr
);
1514 hw
->enable_mask
|= BIT(sensor
->id
);
1516 hw
->enable_mask
&= ~BIT(sensor
->id
);
1522 st_lsm6dsx_check_events(struct st_lsm6dsx_sensor
*sensor
, bool enable
)
1524 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1526 if (sensor
->id
== ST_LSM6DSX_ID_GYRO
|| enable
)
1529 return hw
->enable_event
;
1532 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor
*sensor
,
1535 if (st_lsm6dsx_check_events(sensor
, enable
))
1538 return __st_lsm6dsx_sensor_set_enable(sensor
, enable
);
1541 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor
*sensor
,
1544 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1548 err
= st_lsm6dsx_sensor_set_enable(sensor
, true);
1552 delay
= 1000000000 / sensor
->odr
;
1553 usleep_range(delay
, 2 * delay
);
1555 err
= st_lsm6dsx_read_locked(hw
, addr
, &data
, sizeof(data
));
1559 if (!hw
->enable_event
) {
1560 err
= st_lsm6dsx_sensor_set_enable(sensor
, false);
1565 *val
= (s16
)le16_to_cpu(data
);
1570 static int st_lsm6dsx_read_raw(struct iio_dev
*iio_dev
,
1571 struct iio_chan_spec
const *ch
,
1572 int *val
, int *val2
, long mask
)
1574 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1578 case IIO_CHAN_INFO_RAW
:
1579 ret
= iio_device_claim_direct_mode(iio_dev
);
1583 ret
= st_lsm6dsx_read_oneshot(sensor
, ch
->address
, val
);
1584 iio_device_release_direct_mode(iio_dev
);
1586 case IIO_CHAN_INFO_SAMP_FREQ
:
1587 *val
= sensor
->odr
/ 1000;
1588 *val2
= (sensor
->odr
% 1000) * 1000;
1589 ret
= IIO_VAL_INT_PLUS_MICRO
;
1591 case IIO_CHAN_INFO_SCALE
:
1593 *val2
= sensor
->gain
;
1594 ret
= IIO_VAL_INT_PLUS_MICRO
;
1604 static int st_lsm6dsx_write_raw(struct iio_dev
*iio_dev
,
1605 struct iio_chan_spec
const *chan
,
1606 int val
, int val2
, long mask
)
1608 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1611 err
= iio_device_claim_direct_mode(iio_dev
);
1616 case IIO_CHAN_INFO_SCALE
:
1617 err
= st_lsm6dsx_set_full_scale(sensor
, val2
);
1619 case IIO_CHAN_INFO_SAMP_FREQ
: {
1622 val
= val
* 1000 + val2
/ 1000;
1623 val
= st_lsm6dsx_check_odr(sensor
, val
, &data
);
1635 iio_device_release_direct_mode(iio_dev
);
1640 static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw
*hw
, int state
)
1642 const struct st_lsm6dsx_reg
*reg
;
1646 if (!hw
->settings
->irq_config
.irq1_func
.addr
)
1649 reg
= &hw
->settings
->event_settings
.enable_reg
;
1651 data
= ST_LSM6DSX_SHIFT_VAL(state
, reg
->mask
);
1652 err
= st_lsm6dsx_update_bits_locked(hw
, reg
->addr
,
1658 /* Enable wakeup interrupt */
1659 data
= ST_LSM6DSX_SHIFT_VAL(state
, hw
->irq_routing
->mask
);
1660 return st_lsm6dsx_update_bits_locked(hw
, hw
->irq_routing
->addr
,
1661 hw
->irq_routing
->mask
, data
);
1664 static int st_lsm6dsx_read_event(struct iio_dev
*iio_dev
,
1665 const struct iio_chan_spec
*chan
,
1666 enum iio_event_type type
,
1667 enum iio_event_direction dir
,
1668 enum iio_event_info info
,
1669 int *val
, int *val2
)
1671 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1672 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1674 if (type
!= IIO_EV_TYPE_THRESH
)
1678 *val
= hw
->event_threshold
;
1684 st_lsm6dsx_write_event(struct iio_dev
*iio_dev
,
1685 const struct iio_chan_spec
*chan
,
1686 enum iio_event_type type
,
1687 enum iio_event_direction dir
,
1688 enum iio_event_info info
,
1691 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1692 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1693 const struct st_lsm6dsx_reg
*reg
;
1697 if (type
!= IIO_EV_TYPE_THRESH
)
1700 if (val
< 0 || val
> 31)
1703 reg
= &hw
->settings
->event_settings
.wakeup_reg
;
1704 data
= ST_LSM6DSX_SHIFT_VAL(val
, reg
->mask
);
1705 err
= st_lsm6dsx_update_bits_locked(hw
, reg
->addr
,
1710 hw
->event_threshold
= val
;
1716 st_lsm6dsx_read_event_config(struct iio_dev
*iio_dev
,
1717 const struct iio_chan_spec
*chan
,
1718 enum iio_event_type type
,
1719 enum iio_event_direction dir
)
1721 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1722 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1724 if (type
!= IIO_EV_TYPE_THRESH
)
1727 return !!(hw
->enable_event
& BIT(chan
->channel2
));
1731 st_lsm6dsx_write_event_config(struct iio_dev
*iio_dev
,
1732 const struct iio_chan_spec
*chan
,
1733 enum iio_event_type type
,
1734 enum iio_event_direction dir
, int state
)
1736 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1737 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1741 if (type
!= IIO_EV_TYPE_THRESH
)
1745 enable_event
= hw
->enable_event
| BIT(chan
->channel2
);
1747 /* do not enable events if they are already enabled */
1748 if (hw
->enable_event
)
1751 enable_event
= hw
->enable_event
& ~BIT(chan
->channel2
);
1753 /* only turn off sensor if no events is enabled */
1758 /* stop here if no changes have been made */
1759 if (hw
->enable_event
== enable_event
)
1762 err
= st_lsm6dsx_event_setup(hw
, state
);
1766 mutex_lock(&hw
->conf_lock
);
1767 if (enable_event
|| !(hw
->fifo_mask
& BIT(sensor
->id
)))
1768 err
= __st_lsm6dsx_sensor_set_enable(sensor
, state
);
1769 mutex_unlock(&hw
->conf_lock
);
1774 hw
->enable_event
= enable_event
;
1779 int st_lsm6dsx_set_watermark(struct iio_dev
*iio_dev
, unsigned int val
)
1781 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
1782 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1785 if (val
< 1 || val
> hw
->settings
->max_fifo_size
)
1788 mutex_lock(&hw
->conf_lock
);
1790 err
= st_lsm6dsx_update_watermark(sensor
, val
);
1792 mutex_unlock(&hw
->conf_lock
);
1797 sensor
->watermark
= val
;
1803 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device
*dev
,
1804 struct device_attribute
*attr
,
1807 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
1808 const struct st_lsm6dsx_odr_table_entry
*odr_table
;
1811 odr_table
= &sensor
->hw
->settings
->odr_table
[sensor
->id
];
1812 for (i
= 0; i
< odr_table
->odr_len
; i
++)
1813 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d.%03d ",
1814 odr_table
->odr_avl
[i
].milli_hz
/ 1000,
1815 odr_table
->odr_avl
[i
].milli_hz
% 1000);
1816 buf
[len
- 1] = '\n';
1821 static ssize_t
st_lsm6dsx_sysfs_scale_avail(struct device
*dev
,
1822 struct device_attribute
*attr
,
1825 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
1826 const struct st_lsm6dsx_fs_table_entry
*fs_table
;
1827 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
1830 fs_table
= &hw
->settings
->fs_table
[sensor
->id
];
1831 for (i
= 0; i
< fs_table
->fs_len
; i
++)
1832 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06u ",
1833 fs_table
->fs_avl
[i
].gain
);
1834 buf
[len
- 1] = '\n';
1839 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail
);
1840 static IIO_DEVICE_ATTR(in_accel_scale_available
, 0444,
1841 st_lsm6dsx_sysfs_scale_avail
, NULL
, 0);
1842 static IIO_DEVICE_ATTR(in_anglvel_scale_available
, 0444,
1843 st_lsm6dsx_sysfs_scale_avail
, NULL
, 0);
1845 static struct attribute
*st_lsm6dsx_acc_attributes
[] = {
1846 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
1847 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
1851 static const struct attribute_group st_lsm6dsx_acc_attribute_group
= {
1852 .attrs
= st_lsm6dsx_acc_attributes
,
1855 static const struct iio_info st_lsm6dsx_acc_info
= {
1856 .attrs
= &st_lsm6dsx_acc_attribute_group
,
1857 .read_raw
= st_lsm6dsx_read_raw
,
1858 .write_raw
= st_lsm6dsx_write_raw
,
1859 .read_event_value
= st_lsm6dsx_read_event
,
1860 .write_event_value
= st_lsm6dsx_write_event
,
1861 .read_event_config
= st_lsm6dsx_read_event_config
,
1862 .write_event_config
= st_lsm6dsx_write_event_config
,
1863 .hwfifo_set_watermark
= st_lsm6dsx_set_watermark
,
1866 static struct attribute
*st_lsm6dsx_gyro_attributes
[] = {
1867 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
1868 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
1872 static const struct attribute_group st_lsm6dsx_gyro_attribute_group
= {
1873 .attrs
= st_lsm6dsx_gyro_attributes
,
1876 static const struct iio_info st_lsm6dsx_gyro_info
= {
1877 .attrs
= &st_lsm6dsx_gyro_attribute_group
,
1878 .read_raw
= st_lsm6dsx_read_raw
,
1879 .write_raw
= st_lsm6dsx_write_raw
,
1880 .hwfifo_set_watermark
= st_lsm6dsx_set_watermark
,
1883 static int st_lsm6dsx_get_drdy_pin(struct st_lsm6dsx_hw
*hw
, int *drdy_pin
)
1885 struct device
*dev
= hw
->dev
;
1887 if (!dev_fwnode(dev
))
1890 return device_property_read_u32(dev
, "st,drdy-int-pin", drdy_pin
);
1894 st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw
*hw
,
1895 const struct st_lsm6dsx_reg
**drdy_reg
)
1897 int err
= 0, drdy_pin
;
1899 if (st_lsm6dsx_get_drdy_pin(hw
, &drdy_pin
) < 0) {
1900 struct st_sensors_platform_data
*pdata
;
1901 struct device
*dev
= hw
->dev
;
1903 pdata
= (struct st_sensors_platform_data
*)dev
->platform_data
;
1904 drdy_pin
= pdata
? pdata
->drdy_int_pin
: 1;
1909 hw
->irq_routing
= &hw
->settings
->irq_config
.irq1_func
;
1910 *drdy_reg
= &hw
->settings
->irq_config
.irq1
;
1913 hw
->irq_routing
= &hw
->settings
->irq_config
.irq2_func
;
1914 *drdy_reg
= &hw
->settings
->irq_config
.irq2
;
1917 dev_err(hw
->dev
, "unsupported data ready pin\n");
1925 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw
*hw
)
1927 const struct st_lsm6dsx_shub_settings
*hub_settings
;
1928 struct st_sensors_platform_data
*pdata
;
1929 struct device
*dev
= hw
->dev
;
1933 hub_settings
= &hw
->settings
->shub_settings
;
1935 pdata
= (struct st_sensors_platform_data
*)dev
->platform_data
;
1936 if ((dev_fwnode(dev
) && device_property_read_bool(dev
, "st,pullups")) ||
1937 (pdata
&& pdata
->pullups
)) {
1938 if (hub_settings
->pullup_en
.sec_page
) {
1939 err
= st_lsm6dsx_set_page(hw
, true);
1944 data
= ST_LSM6DSX_SHIFT_VAL(1, hub_settings
->pullup_en
.mask
);
1945 err
= regmap_update_bits(hw
->regmap
,
1946 hub_settings
->pullup_en
.addr
,
1947 hub_settings
->pullup_en
.mask
, data
);
1949 if (hub_settings
->pullup_en
.sec_page
)
1950 st_lsm6dsx_set_page(hw
, false);
1956 if (hub_settings
->aux_sens
.addr
) {
1957 /* configure aux sensors */
1958 err
= st_lsm6dsx_set_page(hw
, true);
1962 data
= ST_LSM6DSX_SHIFT_VAL(3, hub_settings
->aux_sens
.mask
);
1963 err
= regmap_update_bits(hw
->regmap
,
1964 hub_settings
->aux_sens
.addr
,
1965 hub_settings
->aux_sens
.mask
, data
);
1967 st_lsm6dsx_set_page(hw
, false);
1973 if (hub_settings
->emb_func
.addr
) {
1974 data
= ST_LSM6DSX_SHIFT_VAL(1, hub_settings
->emb_func
.mask
);
1975 err
= regmap_update_bits(hw
->regmap
,
1976 hub_settings
->emb_func
.addr
,
1977 hub_settings
->emb_func
.mask
, data
);
1983 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw
*hw
)
1985 const struct st_lsm6dsx_hw_ts_settings
*ts_settings
;
1988 ts_settings
= &hw
->settings
->ts_settings
;
1989 /* enable hw timestamp generation if necessary */
1990 if (ts_settings
->timer_en
.addr
) {
1991 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->timer_en
.mask
);
1992 err
= regmap_update_bits(hw
->regmap
,
1993 ts_settings
->timer_en
.addr
,
1994 ts_settings
->timer_en
.mask
, val
);
1999 /* enable high resolution for hw ts timer if necessary */
2000 if (ts_settings
->hr_timer
.addr
) {
2001 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->hr_timer
.mask
);
2002 err
= regmap_update_bits(hw
->regmap
,
2003 ts_settings
->hr_timer
.addr
,
2004 ts_settings
->hr_timer
.mask
, val
);
2009 /* enable ts queueing in FIFO if necessary */
2010 if (ts_settings
->fifo_en
.addr
) {
2011 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->fifo_en
.mask
);
2012 err
= regmap_update_bits(hw
->regmap
,
2013 ts_settings
->fifo_en
.addr
,
2014 ts_settings
->fifo_en
.mask
, val
);
2019 /* calibrate timestamp sensitivity */
2020 hw
->ts_gain
= ST_LSM6DSX_TS_SENSITIVITY
;
2021 if (ts_settings
->freq_fine
) {
2022 err
= regmap_read(hw
->regmap
, ts_settings
->freq_fine
, &val
);
2027 * linearize the AN5192 formula:
2028 * 1 / (1 + x) ~= 1 - x (Taylor’s Series)
2029 * ttrim[s] = 1 / (40000 * (1 + 0.0015 * val))
2030 * ttrim[ns] ~= 25000 - 37.5 * val
2031 * ttrim[ns] ~= 25000 - (37500 * val) / 1000
2033 hw
->ts_gain
-= ((s8
)val
* 37500) / 1000;
2039 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw
*hw
)
2041 const struct st_lsm6dsx_reg
*reg
;
2044 /* device sw reset */
2045 reg
= &hw
->settings
->reset
;
2046 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2047 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2053 /* reload trimming parameter */
2054 reg
= &hw
->settings
->boot
;
2055 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2056 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2062 /* enable Block Data Update */
2063 reg
= &hw
->settings
->bdu
;
2064 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2065 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2069 /* enable FIFO watermak interrupt */
2070 err
= st_lsm6dsx_get_drdy_reg(hw
, ®
);
2074 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2075 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2079 /* enable Latched interrupts for device events */
2080 if (hw
->settings
->irq_config
.lir
.addr
) {
2081 reg
= &hw
->settings
->irq_config
.lir
;
2082 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2083 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2087 /* enable clear on read for latched interrupts */
2088 if (hw
->settings
->irq_config
.clear_on_read
.addr
) {
2089 reg
= &hw
->settings
->irq_config
.clear_on_read
;
2090 err
= regmap_update_bits(hw
->regmap
,
2091 reg
->addr
, reg
->mask
,
2092 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2098 /* enable drdy-mas if available */
2099 if (hw
->settings
->drdy_mask
.addr
) {
2100 reg
= &hw
->settings
->drdy_mask
;
2101 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2102 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2107 err
= st_lsm6dsx_init_shub(hw
);
2111 return st_lsm6dsx_init_hw_timer(hw
);
2114 static struct iio_dev
*st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw
*hw
,
2115 enum st_lsm6dsx_sensor_id id
,
2118 struct st_lsm6dsx_sensor
*sensor
;
2119 struct iio_dev
*iio_dev
;
2121 iio_dev
= devm_iio_device_alloc(hw
->dev
, sizeof(*sensor
));
2125 iio_dev
->modes
= INDIO_DIRECT_MODE
;
2126 iio_dev
->dev
.parent
= hw
->dev
;
2127 iio_dev
->available_scan_masks
= st_lsm6dsx_available_scan_masks
;
2128 iio_dev
->channels
= hw
->settings
->channels
[id
].chan
;
2129 iio_dev
->num_channels
= hw
->settings
->channels
[id
].len
;
2131 sensor
= iio_priv(iio_dev
);
2134 sensor
->odr
= hw
->settings
->odr_table
[id
].odr_avl
[0].milli_hz
;
2135 sensor
->gain
= hw
->settings
->fs_table
[id
].fs_avl
[0].gain
;
2136 sensor
->watermark
= 1;
2139 case ST_LSM6DSX_ID_ACC
:
2140 iio_dev
->info
= &st_lsm6dsx_acc_info
;
2141 scnprintf(sensor
->name
, sizeof(sensor
->name
), "%s_accel",
2144 case ST_LSM6DSX_ID_GYRO
:
2145 iio_dev
->info
= &st_lsm6dsx_gyro_info
;
2146 scnprintf(sensor
->name
, sizeof(sensor
->name
), "%s_gyro",
2152 iio_dev
->name
= sensor
->name
;
2158 st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw
*hw
)
2160 const struct st_lsm6dsx_event_settings
*event_settings
;
2164 if (!hw
->enable_event
)
2167 event_settings
= &hw
->settings
->event_settings
;
2168 err
= st_lsm6dsx_read_locked(hw
, event_settings
->wakeup_src_reg
,
2169 &data
, sizeof(data
));
2173 timestamp
= iio_get_time_ns(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
2174 if ((data
& hw
->settings
->event_settings
.wakeup_src_z_mask
) &&
2175 (hw
->enable_event
& BIT(IIO_MOD_Z
)))
2176 iio_push_event(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
],
2177 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
2184 if ((data
& hw
->settings
->event_settings
.wakeup_src_y_mask
) &&
2185 (hw
->enable_event
& BIT(IIO_MOD_Y
)))
2186 iio_push_event(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
],
2187 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
2194 if ((data
& hw
->settings
->event_settings
.wakeup_src_x_mask
) &&
2195 (hw
->enable_event
& BIT(IIO_MOD_X
)))
2196 iio_push_event(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
],
2197 IIO_MOD_EVENT_CODE(IIO_ACCEL
,
2204 return data
& event_settings
->wakeup_src_status_mask
;
2207 static irqreturn_t
st_lsm6dsx_handler_thread(int irq
, void *private)
2209 struct st_lsm6dsx_hw
*hw
= private;
2213 event
= st_lsm6dsx_report_motion_event(hw
);
2215 if (!hw
->settings
->fifo_ops
.read_fifo
)
2216 return event
? IRQ_HANDLED
: IRQ_NONE
;
2218 mutex_lock(&hw
->fifo_lock
);
2219 count
= hw
->settings
->fifo_ops
.read_fifo(hw
);
2220 mutex_unlock(&hw
->fifo_lock
);
2222 return count
|| event
? IRQ_HANDLED
: IRQ_NONE
;
2225 static int st_lsm6dsx_irq_setup(struct st_lsm6dsx_hw
*hw
)
2227 struct st_sensors_platform_data
*pdata
;
2228 const struct st_lsm6dsx_reg
*reg
;
2229 struct device
*dev
= hw
->dev
;
2230 unsigned long irq_type
;
2231 bool irq_active_low
;
2234 irq_type
= irqd_get_trigger_type(irq_get_irq_data(hw
->irq
));
2237 case IRQF_TRIGGER_HIGH
:
2238 case IRQF_TRIGGER_RISING
:
2239 irq_active_low
= false;
2241 case IRQF_TRIGGER_LOW
:
2242 case IRQF_TRIGGER_FALLING
:
2243 irq_active_low
= true;
2246 dev_info(hw
->dev
, "mode %lx unsupported\n", irq_type
);
2250 reg
= &hw
->settings
->irq_config
.hla
;
2251 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2252 ST_LSM6DSX_SHIFT_VAL(irq_active_low
,
2257 pdata
= (struct st_sensors_platform_data
*)dev
->platform_data
;
2258 if ((dev_fwnode(dev
) && device_property_read_bool(dev
, "drive-open-drain")) ||
2259 (pdata
&& pdata
->open_drain
)) {
2260 reg
= &hw
->settings
->irq_config
.od
;
2261 err
= regmap_update_bits(hw
->regmap
, reg
->addr
, reg
->mask
,
2262 ST_LSM6DSX_SHIFT_VAL(1, reg
->mask
));
2266 irq_type
|= IRQF_SHARED
;
2269 err
= devm_request_threaded_irq(hw
->dev
, hw
->irq
,
2271 st_lsm6dsx_handler_thread
,
2272 irq_type
| IRQF_ONESHOT
,
2275 dev_err(hw
->dev
, "failed to request trigger irq %d\n",
2283 int st_lsm6dsx_probe(struct device
*dev
, int irq
, int hw_id
,
2284 struct regmap
*regmap
)
2286 struct st_sensors_platform_data
*pdata
= dev
->platform_data
;
2287 const struct st_lsm6dsx_shub_settings
*hub_settings
;
2288 struct st_lsm6dsx_hw
*hw
;
2289 const char *name
= NULL
;
2292 hw
= devm_kzalloc(dev
, sizeof(*hw
), GFP_KERNEL
);
2296 dev_set_drvdata(dev
, (void *)hw
);
2298 mutex_init(&hw
->fifo_lock
);
2299 mutex_init(&hw
->conf_lock
);
2300 mutex_init(&hw
->page_lock
);
2302 hw
->buff
= devm_kzalloc(dev
, ST_LSM6DSX_BUFF_SIZE
, GFP_KERNEL
);
2308 hw
->regmap
= regmap
;
2310 err
= st_lsm6dsx_check_whoami(hw
, hw_id
, &name
);
2314 for (i
= 0; i
< ST_LSM6DSX_ID_EXT0
; i
++) {
2315 hw
->iio_devs
[i
] = st_lsm6dsx_alloc_iiodev(hw
, i
, name
);
2316 if (!hw
->iio_devs
[i
])
2320 err
= st_lsm6dsx_init_device(hw
);
2324 hub_settings
= &hw
->settings
->shub_settings
;
2325 if (hub_settings
->master_en
.addr
) {
2326 err
= st_lsm6dsx_shub_probe(hw
, name
);
2332 err
= st_lsm6dsx_irq_setup(hw
);
2336 err
= st_lsm6dsx_fifo_setup(hw
);
2341 err
= iio_read_mount_matrix(hw
->dev
, "mount-matrix", &hw
->orientation
);
2345 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
2346 if (!hw
->iio_devs
[i
])
2349 err
= devm_iio_device_register(hw
->dev
, hw
->iio_devs
[i
]);
2354 if ((dev_fwnode(dev
) && device_property_read_bool(dev
, "wakeup-source")) ||
2355 (pdata
&& pdata
->wakeup_source
))
2356 device_init_wakeup(dev
, true);
2360 EXPORT_SYMBOL(st_lsm6dsx_probe
);
2362 static int __maybe_unused
st_lsm6dsx_suspend(struct device
*dev
)
2364 struct st_lsm6dsx_hw
*hw
= dev_get_drvdata(dev
);
2365 struct st_lsm6dsx_sensor
*sensor
;
2368 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
2369 if (!hw
->iio_devs
[i
])
2372 sensor
= iio_priv(hw
->iio_devs
[i
]);
2373 if (!(hw
->enable_mask
& BIT(sensor
->id
)))
2376 if (device_may_wakeup(dev
) &&
2377 sensor
->id
== ST_LSM6DSX_ID_ACC
&& hw
->enable_event
) {
2378 /* Enable wake from IRQ */
2379 enable_irq_wake(hw
->irq
);
2383 if (sensor
->id
== ST_LSM6DSX_ID_EXT0
||
2384 sensor
->id
== ST_LSM6DSX_ID_EXT1
||
2385 sensor
->id
== ST_LSM6DSX_ID_EXT2
)
2386 err
= st_lsm6dsx_shub_set_enable(sensor
, false);
2388 err
= st_lsm6dsx_sensor_set_enable(sensor
, false);
2392 hw
->suspend_mask
|= BIT(sensor
->id
);
2396 err
= st_lsm6dsx_flush_fifo(hw
);
2401 static int __maybe_unused
st_lsm6dsx_resume(struct device
*dev
)
2403 struct st_lsm6dsx_hw
*hw
= dev_get_drvdata(dev
);
2404 struct st_lsm6dsx_sensor
*sensor
;
2407 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
2408 if (!hw
->iio_devs
[i
])
2411 sensor
= iio_priv(hw
->iio_devs
[i
]);
2412 if (device_may_wakeup(dev
) &&
2413 sensor
->id
== ST_LSM6DSX_ID_ACC
&& hw
->enable_event
)
2414 disable_irq_wake(hw
->irq
);
2416 if (!(hw
->suspend_mask
& BIT(sensor
->id
)))
2419 if (sensor
->id
== ST_LSM6DSX_ID_EXT0
||
2420 sensor
->id
== ST_LSM6DSX_ID_EXT1
||
2421 sensor
->id
== ST_LSM6DSX_ID_EXT2
)
2422 err
= st_lsm6dsx_shub_set_enable(sensor
, true);
2424 err
= st_lsm6dsx_sensor_set_enable(sensor
, true);
2428 hw
->suspend_mask
&= ~BIT(sensor
->id
);
2432 err
= st_lsm6dsx_set_fifo_mode(hw
, ST_LSM6DSX_FIFO_CONT
);
2437 const struct dev_pm_ops st_lsm6dsx_pm_ops
= {
2438 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend
, st_lsm6dsx_resume
)
2440 EXPORT_SYMBOL(st_lsm6dsx_pm_ops
);
2442 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
2443 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
2444 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
2445 MODULE_LICENSE("GPL v2");