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.
16 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
17 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
18 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
21 * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC:
22 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
23 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
24 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
27 * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR
28 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
29 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
30 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
33 * Copyright 2016 STMicroelectronics Inc.
35 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
36 * Denis Ciocca <denis.ciocca@st.com>
39 #include <linux/kernel.h>
40 #include <linux/module.h>
41 #include <linux/delay.h>
42 #include <linux/iio/iio.h>
43 #include <linux/iio/sysfs.h>
45 #include <linux/regmap.h>
46 #include <linux/bitfield.h>
48 #include <linux/platform_data/st_sensors_pdata.h>
50 #include "st_lsm6dsx.h"
52 #define ST_LSM6DSX_REG_INT1_ADDR 0x0d
53 #define ST_LSM6DSX_REG_INT2_ADDR 0x0e
54 #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3)
55 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
56 #define ST_LSM6DSX_REG_RESET_ADDR 0x12
57 #define ST_LSM6DSX_REG_RESET_MASK BIT(0)
58 #define ST_LSM6DSX_REG_BOOT_MASK BIT(7)
59 #define ST_LSM6DSX_REG_BDU_ADDR 0x12
60 #define ST_LSM6DSX_REG_BDU_MASK BIT(6)
61 #define ST_LSM6DSX_REG_INT2_ON_INT1_ADDR 0x13
62 #define ST_LSM6DSX_REG_INT2_ON_INT1_MASK BIT(5)
64 #define ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR 0x28
65 #define ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR 0x2a
66 #define ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR 0x2c
68 #define ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR 0x22
69 #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR 0x24
70 #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR 0x26
72 static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table
[] = {
73 [ST_LSM6DSX_ID_ACC
] = {
76 .mask
= GENMASK(7, 4),
78 .odr_avl
[0] = { 13, 0x01 },
79 .odr_avl
[1] = { 26, 0x02 },
80 .odr_avl
[2] = { 52, 0x03 },
81 .odr_avl
[3] = { 104, 0x04 },
82 .odr_avl
[4] = { 208, 0x05 },
83 .odr_avl
[5] = { 416, 0x06 },
85 [ST_LSM6DSX_ID_GYRO
] = {
88 .mask
= GENMASK(7, 4),
90 .odr_avl
[0] = { 13, 0x01 },
91 .odr_avl
[1] = { 26, 0x02 },
92 .odr_avl
[2] = { 52, 0x03 },
93 .odr_avl
[3] = { 104, 0x04 },
94 .odr_avl
[4] = { 208, 0x05 },
95 .odr_avl
[5] = { 416, 0x06 },
99 static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table
[] = {
100 [ST_LSM6DSX_ID_ACC
] = {
103 .mask
= GENMASK(3, 2),
105 .fs_avl
[0] = { IIO_G_TO_M_S_2(61), 0x0 },
106 .fs_avl
[1] = { IIO_G_TO_M_S_2(122), 0x2 },
107 .fs_avl
[2] = { IIO_G_TO_M_S_2(244), 0x3 },
108 .fs_avl
[3] = { IIO_G_TO_M_S_2(488), 0x1 },
110 [ST_LSM6DSX_ID_GYRO
] = {
113 .mask
= GENMASK(3, 2),
115 .fs_avl
[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
116 .fs_avl
[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
117 .fs_avl
[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
118 .fs_avl
[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
122 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings
[] = {
125 .max_fifo_size
= 1365,
130 [ST_LSM6DSX_ID_ACC
] = {
132 .mask
= GENMASK(2, 0),
134 [ST_LSM6DSX_ID_GYRO
] = {
136 .mask
= GENMASK(5, 3),
140 .read_fifo
= st_lsm6dsx_read_fifo
,
143 .mask
= GENMASK(11, 0),
147 .mask
= GENMASK(11, 0),
149 .th_wl
= 3, /* 1LSB = 2B */
166 .mask
= GENMASK(5, 3),
172 .max_fifo_size
= 682,
174 [0] = ST_LSM6DS3H_ID
,
177 [ST_LSM6DSX_ID_ACC
] = {
179 .mask
= GENMASK(2, 0),
181 [ST_LSM6DSX_ID_GYRO
] = {
183 .mask
= GENMASK(5, 3),
187 .read_fifo
= st_lsm6dsx_read_fifo
,
190 .mask
= GENMASK(11, 0),
194 .mask
= GENMASK(11, 0),
196 .th_wl
= 3, /* 1LSB = 2B */
213 .mask
= GENMASK(5, 3),
219 .max_fifo_size
= 682,
223 [2] = ST_ISM330DLC_ID
,
226 [ST_LSM6DSX_ID_ACC
] = {
228 .mask
= GENMASK(2, 0),
230 [ST_LSM6DSX_ID_GYRO
] = {
232 .mask
= GENMASK(5, 3),
236 .read_fifo
= st_lsm6dsx_read_fifo
,
239 .mask
= GENMASK(10, 0),
243 .mask
= GENMASK(10, 0),
245 .th_wl
= 3, /* 1LSB = 2B */
262 .mask
= GENMASK(5, 3),
268 .max_fifo_size
= 512,
271 [1] = ST_LSM6DSOX_ID
,
274 [ST_LSM6DSX_ID_ACC
] = {
276 .mask
= GENMASK(3, 0),
278 [ST_LSM6DSX_ID_GYRO
] = {
280 .mask
= GENMASK(7, 4),
284 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
287 .mask
= GENMASK(8, 0),
291 .mask
= GENMASK(8, 0),
302 .mask
= GENMASK(7, 6),
320 .mask
= GENMASK(1, 0),
328 .dw_slv0_addr
= 0x21,
334 .max_fifo_size
= 512,
336 [0] = ST_ASM330LHH_ID
,
339 [ST_LSM6DSX_ID_ACC
] = {
341 .mask
= GENMASK(3, 0),
343 [ST_LSM6DSX_ID_GYRO
] = {
345 .mask
= GENMASK(7, 4),
349 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
352 .mask
= GENMASK(8, 0),
356 .mask
= GENMASK(8, 0),
367 .mask
= GENMASK(7, 6),
373 .max_fifo_size
= 512,
378 [ST_LSM6DSX_ID_ACC
] = {
380 .mask
= GENMASK(3, 0),
382 [ST_LSM6DSX_ID_GYRO
] = {
384 .mask
= GENMASK(7, 4),
388 .read_fifo
= st_lsm6dsx_read_tagged_fifo
,
391 .mask
= GENMASK(8, 0),
395 .mask
= GENMASK(8, 0),
406 .mask
= GENMASK(7, 6),
424 .mask
= GENMASK(1, 0),
432 .dw_slv0_addr
= 0x21,
438 static const struct iio_chan_spec st_lsm6dsx_acc_channels
[] = {
439 ST_LSM6DSX_CHANNEL(IIO_ACCEL
, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR
,
441 ST_LSM6DSX_CHANNEL(IIO_ACCEL
, ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR
,
443 ST_LSM6DSX_CHANNEL(IIO_ACCEL
, ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR
,
445 IIO_CHAN_SOFT_TIMESTAMP(3),
448 static const struct iio_chan_spec st_lsm6dsx_gyro_channels
[] = {
449 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR
,
451 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR
,
453 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL
, ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR
,
455 IIO_CHAN_SOFT_TIMESTAMP(3),
458 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw
*hw
, bool enable
)
460 const struct st_lsm6dsx_shub_settings
*hub_settings
;
464 hub_settings
= &hw
->settings
->shub_settings
;
465 data
= ST_LSM6DSX_SHIFT_VAL(enable
, hub_settings
->page_mux
.mask
);
466 err
= regmap_update_bits(hw
->regmap
, hub_settings
->page_mux
.addr
,
467 hub_settings
->page_mux
.mask
, data
);
468 usleep_range(100, 150);
473 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw
*hw
, int id
)
477 for (i
= 0; i
< ARRAY_SIZE(st_lsm6dsx_sensor_settings
); i
++) {
478 for (j
= 0; j
< ST_LSM6DSX_MAX_ID
; j
++) {
479 if (id
== st_lsm6dsx_sensor_settings
[i
].id
[j
])
482 if (j
< ST_LSM6DSX_MAX_ID
)
486 if (i
== ARRAY_SIZE(st_lsm6dsx_sensor_settings
)) {
487 dev_err(hw
->dev
, "unsupported hw id [%02x]\n", id
);
491 err
= regmap_read(hw
->regmap
, ST_LSM6DSX_REG_WHOAMI_ADDR
, &data
);
493 dev_err(hw
->dev
, "failed to read whoami register\n");
497 if (data
!= st_lsm6dsx_sensor_settings
[i
].wai
) {
498 dev_err(hw
->dev
, "unsupported whoami [%02x]\n", data
);
502 hw
->settings
= &st_lsm6dsx_sensor_settings
[i
];
507 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor
*sensor
,
510 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
511 const struct st_lsm6dsx_reg
*reg
;
516 for (i
= 0; i
< ST_LSM6DSX_FS_LIST_SIZE
; i
++)
517 if (st_lsm6dsx_fs_table
[sensor
->id
].fs_avl
[i
].gain
== gain
)
520 if (i
== ST_LSM6DSX_FS_LIST_SIZE
)
523 val
= st_lsm6dsx_fs_table
[sensor
->id
].fs_avl
[i
].val
;
524 reg
= &st_lsm6dsx_fs_table
[sensor
->id
].reg
;
525 data
= ST_LSM6DSX_SHIFT_VAL(val
, reg
->mask
);
526 err
= st_lsm6dsx_update_bits_locked(hw
, reg
->addr
, reg
->mask
, data
);
535 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor
*sensor
, u16 odr
, u8
*val
)
539 for (i
= 0; i
< ST_LSM6DSX_ODR_LIST_SIZE
; i
++)
541 * ext devices can run at different odr respect to
544 if (st_lsm6dsx_odr_table
[sensor
->id
].odr_avl
[i
].hz
>= odr
)
547 if (i
== ST_LSM6DSX_ODR_LIST_SIZE
)
550 *val
= st_lsm6dsx_odr_table
[sensor
->id
].odr_avl
[i
].val
;
555 static u16
st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw
*hw
, u16 odr
,
556 enum st_lsm6dsx_sensor_id id
)
558 struct st_lsm6dsx_sensor
*ref
= iio_priv(hw
->iio_devs
[id
]);
561 if (hw
->enable_mask
& BIT(id
))
562 return max_t(u16
, ref
->odr
, odr
);
566 return (hw
->enable_mask
& BIT(id
)) ? ref
->odr
: 0;
570 static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor
*sensor
, u16 req_odr
)
572 struct st_lsm6dsx_sensor
*ref_sensor
= sensor
;
573 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
574 const struct st_lsm6dsx_reg
*reg
;
579 switch (sensor
->id
) {
580 case ST_LSM6DSX_ID_EXT0
:
581 case ST_LSM6DSX_ID_EXT1
:
582 case ST_LSM6DSX_ID_EXT2
:
583 case ST_LSM6DSX_ID_ACC
: {
588 * i2c embedded controller relies on the accelerometer sensor as
589 * bus read/write trigger so we need to enable accel device
590 * at odr = max(accel_odr, ext_odr) in order to properly
591 * communicate with i2c slave devices
593 ref_sensor
= iio_priv(hw
->iio_devs
[ST_LSM6DSX_ID_ACC
]);
594 for (i
= ST_LSM6DSX_ID_ACC
; i
< ST_LSM6DSX_ID_MAX
; i
++) {
595 if (!hw
->iio_devs
[i
] || i
== sensor
->id
)
598 odr
= st_lsm6dsx_check_odr_dependency(hw
, req_odr
, i
);
600 /* device already configured */
610 err
= st_lsm6dsx_check_odr(ref_sensor
, req_odr
, &val
);
615 reg
= &st_lsm6dsx_odr_table
[ref_sensor
->id
].reg
;
616 data
= ST_LSM6DSX_SHIFT_VAL(val
, reg
->mask
);
617 return st_lsm6dsx_update_bits_locked(hw
, reg
->addr
, reg
->mask
, data
);
620 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor
*sensor
,
623 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
624 u16 odr
= enable
? sensor
->odr
: 0;
627 err
= st_lsm6dsx_set_odr(sensor
, odr
);
632 hw
->enable_mask
|= BIT(sensor
->id
);
634 hw
->enable_mask
&= ~BIT(sensor
->id
);
639 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor
*sensor
,
642 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
646 err
= st_lsm6dsx_sensor_set_enable(sensor
, true);
650 delay
= 1000000 / sensor
->odr
;
651 usleep_range(delay
, 2 * delay
);
653 err
= st_lsm6dsx_read_locked(hw
, addr
, &data
, sizeof(data
));
657 st_lsm6dsx_sensor_set_enable(sensor
, false);
659 *val
= (s16
)le16_to_cpu(data
);
664 static int st_lsm6dsx_read_raw(struct iio_dev
*iio_dev
,
665 struct iio_chan_spec
const *ch
,
666 int *val
, int *val2
, long mask
)
668 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
672 case IIO_CHAN_INFO_RAW
:
673 ret
= iio_device_claim_direct_mode(iio_dev
);
677 ret
= st_lsm6dsx_read_oneshot(sensor
, ch
->address
, val
);
678 iio_device_release_direct_mode(iio_dev
);
680 case IIO_CHAN_INFO_SAMP_FREQ
:
684 case IIO_CHAN_INFO_SCALE
:
686 *val2
= sensor
->gain
;
687 ret
= IIO_VAL_INT_PLUS_MICRO
;
697 static int st_lsm6dsx_write_raw(struct iio_dev
*iio_dev
,
698 struct iio_chan_spec
const *chan
,
699 int val
, int val2
, long mask
)
701 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
704 err
= iio_device_claim_direct_mode(iio_dev
);
709 case IIO_CHAN_INFO_SCALE
:
710 err
= st_lsm6dsx_set_full_scale(sensor
, val2
);
712 case IIO_CHAN_INFO_SAMP_FREQ
: {
715 err
= st_lsm6dsx_check_odr(sensor
, val
, &data
);
725 iio_device_release_direct_mode(iio_dev
);
730 int st_lsm6dsx_set_watermark(struct iio_dev
*iio_dev
, unsigned int val
)
732 struct st_lsm6dsx_sensor
*sensor
= iio_priv(iio_dev
);
733 struct st_lsm6dsx_hw
*hw
= sensor
->hw
;
736 if (val
< 1 || val
> hw
->settings
->max_fifo_size
)
739 mutex_lock(&hw
->conf_lock
);
741 err
= st_lsm6dsx_update_watermark(sensor
, val
);
743 mutex_unlock(&hw
->conf_lock
);
748 sensor
->watermark
= val
;
754 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device
*dev
,
755 struct device_attribute
*attr
,
758 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
759 enum st_lsm6dsx_sensor_id id
= sensor
->id
;
762 for (i
= 0; i
< ST_LSM6DSX_ODR_LIST_SIZE
; i
++)
763 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ",
764 st_lsm6dsx_odr_table
[id
].odr_avl
[i
].hz
);
770 static ssize_t
st_lsm6dsx_sysfs_scale_avail(struct device
*dev
,
771 struct device_attribute
*attr
,
774 struct st_lsm6dsx_sensor
*sensor
= iio_priv(dev_get_drvdata(dev
));
775 enum st_lsm6dsx_sensor_id id
= sensor
->id
;
778 for (i
= 0; i
< ST_LSM6DSX_FS_LIST_SIZE
; i
++)
779 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "0.%06u ",
780 st_lsm6dsx_fs_table
[id
].fs_avl
[i
].gain
);
786 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail
);
787 static IIO_DEVICE_ATTR(in_accel_scale_available
, 0444,
788 st_lsm6dsx_sysfs_scale_avail
, NULL
, 0);
789 static IIO_DEVICE_ATTR(in_anglvel_scale_available
, 0444,
790 st_lsm6dsx_sysfs_scale_avail
, NULL
, 0);
792 static struct attribute
*st_lsm6dsx_acc_attributes
[] = {
793 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
794 &iio_dev_attr_in_accel_scale_available
.dev_attr
.attr
,
798 static const struct attribute_group st_lsm6dsx_acc_attribute_group
= {
799 .attrs
= st_lsm6dsx_acc_attributes
,
802 static const struct iio_info st_lsm6dsx_acc_info
= {
803 .attrs
= &st_lsm6dsx_acc_attribute_group
,
804 .read_raw
= st_lsm6dsx_read_raw
,
805 .write_raw
= st_lsm6dsx_write_raw
,
806 .hwfifo_set_watermark
= st_lsm6dsx_set_watermark
,
809 static struct attribute
*st_lsm6dsx_gyro_attributes
[] = {
810 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
811 &iio_dev_attr_in_anglvel_scale_available
.dev_attr
.attr
,
815 static const struct attribute_group st_lsm6dsx_gyro_attribute_group
= {
816 .attrs
= st_lsm6dsx_gyro_attributes
,
819 static const struct iio_info st_lsm6dsx_gyro_info
= {
820 .attrs
= &st_lsm6dsx_gyro_attribute_group
,
821 .read_raw
= st_lsm6dsx_read_raw
,
822 .write_raw
= st_lsm6dsx_write_raw
,
823 .hwfifo_set_watermark
= st_lsm6dsx_set_watermark
,
826 static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw
*hw
, int *drdy_pin
)
828 struct device_node
*np
= hw
->dev
->of_node
;
833 return of_property_read_u32(np
, "st,drdy-int-pin", drdy_pin
);
836 static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw
*hw
, u8
*drdy_reg
)
838 int err
= 0, drdy_pin
;
840 if (st_lsm6dsx_of_get_drdy_pin(hw
, &drdy_pin
) < 0) {
841 struct st_sensors_platform_data
*pdata
;
842 struct device
*dev
= hw
->dev
;
844 pdata
= (struct st_sensors_platform_data
*)dev
->platform_data
;
845 drdy_pin
= pdata
? pdata
->drdy_int_pin
: 1;
850 *drdy_reg
= ST_LSM6DSX_REG_INT1_ADDR
;
853 *drdy_reg
= ST_LSM6DSX_REG_INT2_ADDR
;
856 dev_err(hw
->dev
, "unsupported data ready pin\n");
864 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw
*hw
)
866 const struct st_lsm6dsx_shub_settings
*hub_settings
;
867 struct device_node
*np
= hw
->dev
->of_node
;
868 struct st_sensors_platform_data
*pdata
;
872 hub_settings
= &hw
->settings
->shub_settings
;
874 pdata
= (struct st_sensors_platform_data
*)hw
->dev
->platform_data
;
875 if ((np
&& of_property_read_bool(np
, "st,pullups")) ||
876 (pdata
&& pdata
->pullups
)) {
877 err
= st_lsm6dsx_set_page(hw
, true);
881 data
= ST_LSM6DSX_SHIFT_VAL(1, hub_settings
->pullup_en
.mask
);
882 err
= regmap_update_bits(hw
->regmap
,
883 hub_settings
->pullup_en
.addr
,
884 hub_settings
->pullup_en
.mask
, data
);
886 st_lsm6dsx_set_page(hw
, false);
892 if (hub_settings
->aux_sens
.addr
) {
893 /* configure aux sensors */
894 err
= st_lsm6dsx_set_page(hw
, true);
898 data
= ST_LSM6DSX_SHIFT_VAL(3, hub_settings
->aux_sens
.mask
);
899 err
= regmap_update_bits(hw
->regmap
,
900 hub_settings
->aux_sens
.addr
,
901 hub_settings
->aux_sens
.mask
, data
);
903 st_lsm6dsx_set_page(hw
, false);
909 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw
*hw
)
911 const struct st_lsm6dsx_hw_ts_settings
*ts_settings
;
914 ts_settings
= &hw
->settings
->ts_settings
;
915 /* enable hw timestamp generation if necessary */
916 if (ts_settings
->timer_en
.addr
) {
917 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->timer_en
.mask
);
918 err
= regmap_update_bits(hw
->regmap
,
919 ts_settings
->timer_en
.addr
,
920 ts_settings
->timer_en
.mask
, val
);
925 /* enable high resolution for hw ts timer if necessary */
926 if (ts_settings
->hr_timer
.addr
) {
927 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->hr_timer
.mask
);
928 err
= regmap_update_bits(hw
->regmap
,
929 ts_settings
->hr_timer
.addr
,
930 ts_settings
->hr_timer
.mask
, val
);
935 /* enable ts queueing in FIFO if necessary */
936 if (ts_settings
->fifo_en
.addr
) {
937 val
= ST_LSM6DSX_SHIFT_VAL(1, ts_settings
->fifo_en
.mask
);
938 err
= regmap_update_bits(hw
->regmap
,
939 ts_settings
->fifo_en
.addr
,
940 ts_settings
->fifo_en
.mask
, val
);
947 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw
*hw
)
952 /* device sw reset */
953 err
= regmap_update_bits(hw
->regmap
, ST_LSM6DSX_REG_RESET_ADDR
,
954 ST_LSM6DSX_REG_RESET_MASK
,
955 FIELD_PREP(ST_LSM6DSX_REG_RESET_MASK
, 1));
961 /* reload trimming parameter */
962 err
= regmap_update_bits(hw
->regmap
, ST_LSM6DSX_REG_RESET_ADDR
,
963 ST_LSM6DSX_REG_BOOT_MASK
,
964 FIELD_PREP(ST_LSM6DSX_REG_BOOT_MASK
, 1));
970 /* enable Block Data Update */
971 err
= regmap_update_bits(hw
->regmap
, ST_LSM6DSX_REG_BDU_ADDR
,
972 ST_LSM6DSX_REG_BDU_MASK
,
973 FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK
, 1));
977 /* enable FIFO watermak interrupt */
978 err
= st_lsm6dsx_get_drdy_reg(hw
, &drdy_int_reg
);
982 err
= regmap_update_bits(hw
->regmap
, drdy_int_reg
,
983 ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK
,
984 FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK
,
989 err
= st_lsm6dsx_init_shub(hw
);
993 return st_lsm6dsx_init_hw_timer(hw
);
996 static struct iio_dev
*st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw
*hw
,
997 enum st_lsm6dsx_sensor_id id
,
1000 struct st_lsm6dsx_sensor
*sensor
;
1001 struct iio_dev
*iio_dev
;
1003 iio_dev
= devm_iio_device_alloc(hw
->dev
, sizeof(*sensor
));
1007 iio_dev
->modes
= INDIO_DIRECT_MODE
;
1008 iio_dev
->dev
.parent
= hw
->dev
;
1009 iio_dev
->available_scan_masks
= st_lsm6dsx_available_scan_masks
;
1011 sensor
= iio_priv(iio_dev
);
1014 sensor
->odr
= st_lsm6dsx_odr_table
[id
].odr_avl
[0].hz
;
1015 sensor
->gain
= st_lsm6dsx_fs_table
[id
].fs_avl
[0].gain
;
1016 sensor
->watermark
= 1;
1019 case ST_LSM6DSX_ID_ACC
:
1020 iio_dev
->channels
= st_lsm6dsx_acc_channels
;
1021 iio_dev
->num_channels
= ARRAY_SIZE(st_lsm6dsx_acc_channels
);
1022 iio_dev
->info
= &st_lsm6dsx_acc_info
;
1024 scnprintf(sensor
->name
, sizeof(sensor
->name
), "%s_accel",
1027 case ST_LSM6DSX_ID_GYRO
:
1028 iio_dev
->channels
= st_lsm6dsx_gyro_channels
;
1029 iio_dev
->num_channels
= ARRAY_SIZE(st_lsm6dsx_gyro_channels
);
1030 iio_dev
->info
= &st_lsm6dsx_gyro_info
;
1032 scnprintf(sensor
->name
, sizeof(sensor
->name
), "%s_gyro",
1038 iio_dev
->name
= sensor
->name
;
1043 int st_lsm6dsx_probe(struct device
*dev
, int irq
, int hw_id
, const char *name
,
1044 struct regmap
*regmap
)
1046 const struct st_lsm6dsx_shub_settings
*hub_settings
;
1047 struct st_lsm6dsx_hw
*hw
;
1050 hw
= devm_kzalloc(dev
, sizeof(*hw
), GFP_KERNEL
);
1054 dev_set_drvdata(dev
, (void *)hw
);
1056 mutex_init(&hw
->fifo_lock
);
1057 mutex_init(&hw
->conf_lock
);
1058 mutex_init(&hw
->page_lock
);
1060 hw
->buff
= devm_kzalloc(dev
, ST_LSM6DSX_BUFF_SIZE
, GFP_KERNEL
);
1066 hw
->regmap
= regmap
;
1068 err
= st_lsm6dsx_check_whoami(hw
, hw_id
);
1072 for (i
= 0; i
< ST_LSM6DSX_ID_EXT0
; i
++) {
1073 hw
->iio_devs
[i
] = st_lsm6dsx_alloc_iiodev(hw
, i
, name
);
1074 if (!hw
->iio_devs
[i
])
1078 err
= st_lsm6dsx_init_device(hw
);
1082 hub_settings
= &hw
->settings
->shub_settings
;
1083 if (hub_settings
->master_en
.addr
) {
1084 err
= st_lsm6dsx_shub_probe(hw
, name
);
1090 err
= st_lsm6dsx_fifo_setup(hw
);
1095 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
1096 if (!hw
->iio_devs
[i
])
1099 err
= devm_iio_device_register(hw
->dev
, hw
->iio_devs
[i
]);
1106 EXPORT_SYMBOL(st_lsm6dsx_probe
);
1108 static int __maybe_unused
st_lsm6dsx_suspend(struct device
*dev
)
1110 struct st_lsm6dsx_hw
*hw
= dev_get_drvdata(dev
);
1111 struct st_lsm6dsx_sensor
*sensor
;
1112 const struct st_lsm6dsx_reg
*reg
;
1116 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
1117 if (!hw
->iio_devs
[i
])
1120 sensor
= iio_priv(hw
->iio_devs
[i
]);
1121 if (!(hw
->enable_mask
& BIT(sensor
->id
)))
1124 reg
= &st_lsm6dsx_odr_table
[sensor
->id
].reg
;
1125 data
= ST_LSM6DSX_SHIFT_VAL(0, reg
->mask
);
1126 err
= st_lsm6dsx_update_bits_locked(hw
, reg
->addr
, reg
->mask
,
1132 if (hw
->fifo_mode
!= ST_LSM6DSX_FIFO_BYPASS
)
1133 err
= st_lsm6dsx_flush_fifo(hw
);
1138 static int __maybe_unused
st_lsm6dsx_resume(struct device
*dev
)
1140 struct st_lsm6dsx_hw
*hw
= dev_get_drvdata(dev
);
1141 struct st_lsm6dsx_sensor
*sensor
;
1144 for (i
= 0; i
< ST_LSM6DSX_ID_MAX
; i
++) {
1145 if (!hw
->iio_devs
[i
])
1148 sensor
= iio_priv(hw
->iio_devs
[i
]);
1149 if (!(hw
->enable_mask
& BIT(sensor
->id
)))
1152 err
= st_lsm6dsx_set_odr(sensor
, sensor
->odr
);
1157 if (hw
->enable_mask
)
1158 err
= st_lsm6dsx_set_fifo_mode(hw
, ST_LSM6DSX_FIFO_CONT
);
1163 const struct dev_pm_ops st_lsm6dsx_pm_ops
= {
1164 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend
, st_lsm6dsx_resume
)
1166 EXPORT_SYMBOL(st_lsm6dsx_pm_ops
);
1168 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
1169 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1170 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
1171 MODULE_LICENSE("GPL v2");