2 * STMicroelectronics st_lsm6dsx sensor driver
4 * The ST LSM6DSx IMU MEMS series consists of 3D digital accelerometer
5 * and 3D digital gyroscope system-in-package with a digital I2C/SPI serial
6 * interface standard output.
7 * LSM6DSx IMU MEMS series has a dynamic user-selectable full-scale
8 * acceleration range of +-2/+-4/+-8/+-16 g and an angular rate range of
9 * +-125/+-245/+-500/+-1000/+-2000 dps
10 * LSM6DSx series has an integrated First-In-First-Out (FIFO) buffer
11 * allowing dynamic batching of sensor data.
15 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
16 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
17 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
20 * - LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC:
21 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
22 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
23 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
26 * - LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR
27 * - Accelerometer/Gyroscope supported ODR [Hz]: 13, 26, 52, 104, 208, 416
28 * - Accelerometer supported full-scale [g]: +-2/+-4/+-8/+-16
29 * - Gyroscope supported full-scale [dps]: +-125/+-245/+-500/+-1000/+-2000
32 * Copyright 2016 STMicroelectronics Inc.
34 * Lorenzo Bianconi <lorenzo.bianconi@st.com>
35 * Denis Ciocca <denis.ciocca@st.com>
37 * Licensed under the GPL-2.
40 #include <linux/kernel.h>
41 #include <linux/module.h>
42 #include <linux/delay.h>
43 #include <linux/iio/iio.h>
44 #include <linux/iio/sysfs.h>
46 #include <linux/regmap.h>
47 #include <linux/bitfield.h>
49 #include <linux/platform_data/st_sensors_pdata.h>
51 #include "st_lsm6dsx.h"
53 #define ST_LSM6DSX_REG_INT1_ADDR 0x0d
54 #define ST_LSM6DSX_REG_INT2_ADDR 0x0e
55 #define ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK BIT(3)
56 #define ST_LSM6DSX_REG_WHOAMI_ADDR 0x0f
57 #define ST_LSM6DSX_REG_RESET_ADDR 0x12
58 #define ST_LSM6DSX_REG_RESET_MASK BIT(0)
59 #define ST_LSM6DSX_REG_BOOT_MASK BIT(7)
60 #define ST_LSM6DSX_REG_BDU_ADDR 0x12
61 #define ST_LSM6DSX_REG_BDU_MASK BIT(6)
62 #define ST_LSM6DSX_REG_INT2_ON_INT1_ADDR 0x13
63 #define ST_LSM6DSX_REG_INT2_ON_INT1_MASK BIT(5)
65 #define ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR 0x28
66 #define ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR 0x2a
67 #define ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR 0x2c
69 #define ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR 0x22
70 #define ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR 0x24
71 #define ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR 0x26
73 static const struct st_lsm6dsx_odr_table_entry st_lsm6dsx_odr_table[] = {
74 [ST_LSM6DSX_ID_ACC] = {
77 .mask = GENMASK(7, 4),
79 .odr_avl[0] = { 13, 0x01 },
80 .odr_avl[1] = { 26, 0x02 },
81 .odr_avl[2] = { 52, 0x03 },
82 .odr_avl[3] = { 104, 0x04 },
83 .odr_avl[4] = { 208, 0x05 },
84 .odr_avl[5] = { 416, 0x06 },
86 [ST_LSM6DSX_ID_GYRO] = {
89 .mask = GENMASK(7, 4),
91 .odr_avl[0] = { 13, 0x01 },
92 .odr_avl[1] = { 26, 0x02 },
93 .odr_avl[2] = { 52, 0x03 },
94 .odr_avl[3] = { 104, 0x04 },
95 .odr_avl[4] = { 208, 0x05 },
96 .odr_avl[5] = { 416, 0x06 },
100 static const struct st_lsm6dsx_fs_table_entry st_lsm6dsx_fs_table[] = {
101 [ST_LSM6DSX_ID_ACC] = {
104 .mask = GENMASK(3, 2),
106 .fs_avl[0] = { IIO_G_TO_M_S_2(61), 0x0 },
107 .fs_avl[1] = { IIO_G_TO_M_S_2(122), 0x2 },
108 .fs_avl[2] = { IIO_G_TO_M_S_2(244), 0x3 },
109 .fs_avl[3] = { IIO_G_TO_M_S_2(488), 0x1 },
111 [ST_LSM6DSX_ID_GYRO] = {
114 .mask = GENMASK(3, 2),
116 .fs_avl[0] = { IIO_DEGREE_TO_RAD(8750), 0x0 },
117 .fs_avl[1] = { IIO_DEGREE_TO_RAD(17500), 0x1 },
118 .fs_avl[2] = { IIO_DEGREE_TO_RAD(35000), 0x2 },
119 .fs_avl[3] = { IIO_DEGREE_TO_RAD(70000), 0x3 },
123 static const struct st_lsm6dsx_settings st_lsm6dsx_sensor_settings[] = {
126 .max_fifo_size = 1365,
131 [ST_LSM6DSX_ID_ACC] = {
133 .mask = GENMASK(2, 0),
135 [ST_LSM6DSX_ID_GYRO] = {
137 .mask = GENMASK(5, 3),
141 .read_fifo = st_lsm6dsx_read_fifo,
144 .mask = GENMASK(11, 0),
148 .mask = GENMASK(11, 0),
150 .th_wl = 3, /* 1LSB = 2B */
167 .mask = GENMASK(5, 3),
173 .max_fifo_size = 682,
175 [0] = ST_LSM6DS3H_ID,
178 [ST_LSM6DSX_ID_ACC] = {
180 .mask = GENMASK(2, 0),
182 [ST_LSM6DSX_ID_GYRO] = {
184 .mask = GENMASK(5, 3),
188 .read_fifo = st_lsm6dsx_read_fifo,
191 .mask = GENMASK(11, 0),
195 .mask = GENMASK(11, 0),
197 .th_wl = 3, /* 1LSB = 2B */
214 .mask = GENMASK(5, 3),
220 .max_fifo_size = 682,
224 [2] = ST_ISM330DLC_ID,
227 [ST_LSM6DSX_ID_ACC] = {
229 .mask = GENMASK(2, 0),
231 [ST_LSM6DSX_ID_GYRO] = {
233 .mask = GENMASK(5, 3),
237 .read_fifo = st_lsm6dsx_read_fifo,
240 .mask = GENMASK(10, 0),
244 .mask = GENMASK(10, 0),
246 .th_wl = 3, /* 1LSB = 2B */
263 .mask = GENMASK(5, 3),
269 .max_fifo_size = 512,
272 [1] = ST_LSM6DSOX_ID,
275 [ST_LSM6DSX_ID_ACC] = {
277 .mask = GENMASK(3, 0),
279 [ST_LSM6DSX_ID_GYRO] = {
281 .mask = GENMASK(7, 4),
285 .read_fifo = st_lsm6dsx_read_tagged_fifo,
288 .mask = GENMASK(8, 0),
292 .mask = GENMASK(8, 0),
303 .mask = GENMASK(7, 6),
321 .mask = GENMASK(1, 0),
329 .dw_slv0_addr = 0x21,
335 .max_fifo_size = 512,
337 [0] = ST_ASM330LHH_ID,
340 [ST_LSM6DSX_ID_ACC] = {
342 .mask = GENMASK(3, 0),
344 [ST_LSM6DSX_ID_GYRO] = {
346 .mask = GENMASK(7, 4),
350 .read_fifo = st_lsm6dsx_read_tagged_fifo,
353 .mask = GENMASK(8, 0),
357 .mask = GENMASK(8, 0),
368 .mask = GENMASK(7, 6),
374 .max_fifo_size = 512,
379 [ST_LSM6DSX_ID_ACC] = {
381 .mask = GENMASK(3, 0),
383 [ST_LSM6DSX_ID_GYRO] = {
385 .mask = GENMASK(7, 4),
389 .read_fifo = st_lsm6dsx_read_tagged_fifo,
392 .mask = GENMASK(8, 0),
396 .mask = GENMASK(8, 0),
407 .mask = GENMASK(7, 6),
425 .mask = GENMASK(1, 0),
433 .dw_slv0_addr = 0x21,
439 static const struct iio_chan_spec st_lsm6dsx_acc_channels[] = {
440 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_X_L_ADDR,
442 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Y_L_ADDR,
444 ST_LSM6DSX_CHANNEL(IIO_ACCEL, ST_LSM6DSX_REG_ACC_OUT_Z_L_ADDR,
446 IIO_CHAN_SOFT_TIMESTAMP(3),
449 static const struct iio_chan_spec st_lsm6dsx_gyro_channels[] = {
450 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_X_L_ADDR,
452 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Y_L_ADDR,
454 ST_LSM6DSX_CHANNEL(IIO_ANGL_VEL, ST_LSM6DSX_REG_GYRO_OUT_Z_L_ADDR,
456 IIO_CHAN_SOFT_TIMESTAMP(3),
459 int st_lsm6dsx_set_page(struct st_lsm6dsx_hw *hw, bool enable)
461 const struct st_lsm6dsx_shub_settings *hub_settings;
465 hub_settings = &hw->settings->shub_settings;
466 data = ST_LSM6DSX_SHIFT_VAL(enable, hub_settings->page_mux.mask);
467 err = regmap_update_bits(hw->regmap, hub_settings->page_mux.addr,
468 hub_settings->page_mux.mask, data);
469 usleep_range(100, 150);
474 static int st_lsm6dsx_check_whoami(struct st_lsm6dsx_hw *hw, int id)
478 for (i = 0; i < ARRAY_SIZE(st_lsm6dsx_sensor_settings); i++) {
479 for (j = 0; j < ST_LSM6DSX_MAX_ID; j++) {
480 if (id == st_lsm6dsx_sensor_settings[i].id[j])
483 if (j < ST_LSM6DSX_MAX_ID)
487 if (i == ARRAY_SIZE(st_lsm6dsx_sensor_settings)) {
488 dev_err(hw->dev, "unsupported hw id [%02x]\n", id);
492 err = regmap_read(hw->regmap, ST_LSM6DSX_REG_WHOAMI_ADDR, &data);
494 dev_err(hw->dev, "failed to read whoami register\n");
498 if (data != st_lsm6dsx_sensor_settings[i].wai) {
499 dev_err(hw->dev, "unsupported whoami [%02x]\n", data);
503 hw->settings = &st_lsm6dsx_sensor_settings[i];
508 static int st_lsm6dsx_set_full_scale(struct st_lsm6dsx_sensor *sensor,
511 struct st_lsm6dsx_hw *hw = sensor->hw;
512 const struct st_lsm6dsx_reg *reg;
517 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
518 if (st_lsm6dsx_fs_table[sensor->id].fs_avl[i].gain == gain)
521 if (i == ST_LSM6DSX_FS_LIST_SIZE)
524 val = st_lsm6dsx_fs_table[sensor->id].fs_avl[i].val;
525 reg = &st_lsm6dsx_fs_table[sensor->id].reg;
526 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
527 err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
536 int st_lsm6dsx_check_odr(struct st_lsm6dsx_sensor *sensor, u16 odr, u8 *val)
540 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
542 * ext devices can run at different odr respect to
545 if (st_lsm6dsx_odr_table[sensor->id].odr_avl[i].hz >= odr)
548 if (i == ST_LSM6DSX_ODR_LIST_SIZE)
551 *val = st_lsm6dsx_odr_table[sensor->id].odr_avl[i].val;
556 static u16 st_lsm6dsx_check_odr_dependency(struct st_lsm6dsx_hw *hw, u16 odr,
557 enum st_lsm6dsx_sensor_id id)
559 struct st_lsm6dsx_sensor *ref = iio_priv(hw->iio_devs[id]);
562 if (hw->enable_mask & BIT(id))
563 return max_t(u16, ref->odr, odr);
567 return (hw->enable_mask & BIT(id)) ? ref->odr : 0;
571 static int st_lsm6dsx_set_odr(struct st_lsm6dsx_sensor *sensor, u16 req_odr)
573 struct st_lsm6dsx_sensor *ref_sensor = sensor;
574 struct st_lsm6dsx_hw *hw = sensor->hw;
575 const struct st_lsm6dsx_reg *reg;
580 switch (sensor->id) {
581 case ST_LSM6DSX_ID_EXT0:
582 case ST_LSM6DSX_ID_EXT1:
583 case ST_LSM6DSX_ID_EXT2:
584 case ST_LSM6DSX_ID_ACC: {
589 * i2c embedded controller relies on the accelerometer sensor as
590 * bus read/write trigger so we need to enable accel device
591 * at odr = max(accel_odr, ext_odr) in order to properly
592 * communicate with i2c slave devices
594 ref_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
595 for (i = ST_LSM6DSX_ID_ACC; i < ST_LSM6DSX_ID_MAX; i++) {
596 if (!hw->iio_devs[i] || i == sensor->id)
599 odr = st_lsm6dsx_check_odr_dependency(hw, req_odr, i);
601 /* device already configured */
611 err = st_lsm6dsx_check_odr(ref_sensor, req_odr, &val);
616 reg = &st_lsm6dsx_odr_table[ref_sensor->id].reg;
617 data = ST_LSM6DSX_SHIFT_VAL(val, reg->mask);
618 return st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data);
621 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor,
624 struct st_lsm6dsx_hw *hw = sensor->hw;
625 u16 odr = enable ? sensor->odr : 0;
628 err = st_lsm6dsx_set_odr(sensor, odr);
633 hw->enable_mask |= BIT(sensor->id);
635 hw->enable_mask &= ~BIT(sensor->id);
640 static int st_lsm6dsx_read_oneshot(struct st_lsm6dsx_sensor *sensor,
643 struct st_lsm6dsx_hw *hw = sensor->hw;
647 err = st_lsm6dsx_sensor_set_enable(sensor, true);
651 delay = 1000000 / sensor->odr;
652 usleep_range(delay, 2 * delay);
654 err = st_lsm6dsx_read_locked(hw, addr, &data, sizeof(data));
658 st_lsm6dsx_sensor_set_enable(sensor, false);
660 *val = (s16)le16_to_cpu(data);
665 static int st_lsm6dsx_read_raw(struct iio_dev *iio_dev,
666 struct iio_chan_spec const *ch,
667 int *val, int *val2, long mask)
669 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
673 case IIO_CHAN_INFO_RAW:
674 ret = iio_device_claim_direct_mode(iio_dev);
678 ret = st_lsm6dsx_read_oneshot(sensor, ch->address, val);
679 iio_device_release_direct_mode(iio_dev);
681 case IIO_CHAN_INFO_SAMP_FREQ:
685 case IIO_CHAN_INFO_SCALE:
687 *val2 = sensor->gain;
688 ret = IIO_VAL_INT_PLUS_MICRO;
698 static int st_lsm6dsx_write_raw(struct iio_dev *iio_dev,
699 struct iio_chan_spec const *chan,
700 int val, int val2, long mask)
702 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
705 err = iio_device_claim_direct_mode(iio_dev);
710 case IIO_CHAN_INFO_SCALE:
711 err = st_lsm6dsx_set_full_scale(sensor, val2);
713 case IIO_CHAN_INFO_SAMP_FREQ: {
716 err = st_lsm6dsx_check_odr(sensor, val, &data);
726 iio_device_release_direct_mode(iio_dev);
731 int st_lsm6dsx_set_watermark(struct iio_dev *iio_dev, unsigned int val)
733 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
734 struct st_lsm6dsx_hw *hw = sensor->hw;
737 if (val < 1 || val > hw->settings->max_fifo_size)
740 mutex_lock(&hw->conf_lock);
742 err = st_lsm6dsx_update_watermark(sensor, val);
744 mutex_unlock(&hw->conf_lock);
749 sensor->watermark = val;
755 st_lsm6dsx_sysfs_sampling_frequency_avail(struct device *dev,
756 struct device_attribute *attr,
759 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
760 enum st_lsm6dsx_sensor_id id = sensor->id;
763 for (i = 0; i < ST_LSM6DSX_ODR_LIST_SIZE; i++)
764 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ",
765 st_lsm6dsx_odr_table[id].odr_avl[i].hz);
771 static ssize_t st_lsm6dsx_sysfs_scale_avail(struct device *dev,
772 struct device_attribute *attr,
775 struct st_lsm6dsx_sensor *sensor = iio_priv(dev_get_drvdata(dev));
776 enum st_lsm6dsx_sensor_id id = sensor->id;
779 for (i = 0; i < ST_LSM6DSX_FS_LIST_SIZE; i++)
780 len += scnprintf(buf + len, PAGE_SIZE - len, "0.%06u ",
781 st_lsm6dsx_fs_table[id].fs_avl[i].gain);
787 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(st_lsm6dsx_sysfs_sampling_frequency_avail);
788 static IIO_DEVICE_ATTR(in_accel_scale_available, 0444,
789 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
790 static IIO_DEVICE_ATTR(in_anglvel_scale_available, 0444,
791 st_lsm6dsx_sysfs_scale_avail, NULL, 0);
793 static struct attribute *st_lsm6dsx_acc_attributes[] = {
794 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
795 &iio_dev_attr_in_accel_scale_available.dev_attr.attr,
799 static const struct attribute_group st_lsm6dsx_acc_attribute_group = {
800 .attrs = st_lsm6dsx_acc_attributes,
803 static const struct iio_info st_lsm6dsx_acc_info = {
804 .attrs = &st_lsm6dsx_acc_attribute_group,
805 .read_raw = st_lsm6dsx_read_raw,
806 .write_raw = st_lsm6dsx_write_raw,
807 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
810 static struct attribute *st_lsm6dsx_gyro_attributes[] = {
811 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
812 &iio_dev_attr_in_anglvel_scale_available.dev_attr.attr,
816 static const struct attribute_group st_lsm6dsx_gyro_attribute_group = {
817 .attrs = st_lsm6dsx_gyro_attributes,
820 static const struct iio_info st_lsm6dsx_gyro_info = {
821 .attrs = &st_lsm6dsx_gyro_attribute_group,
822 .read_raw = st_lsm6dsx_read_raw,
823 .write_raw = st_lsm6dsx_write_raw,
824 .hwfifo_set_watermark = st_lsm6dsx_set_watermark,
827 static int st_lsm6dsx_of_get_drdy_pin(struct st_lsm6dsx_hw *hw, int *drdy_pin)
829 struct device_node *np = hw->dev->of_node;
834 return of_property_read_u32(np, "st,drdy-int-pin", drdy_pin);
837 static int st_lsm6dsx_get_drdy_reg(struct st_lsm6dsx_hw *hw, u8 *drdy_reg)
839 int err = 0, drdy_pin;
841 if (st_lsm6dsx_of_get_drdy_pin(hw, &drdy_pin) < 0) {
842 struct st_sensors_platform_data *pdata;
843 struct device *dev = hw->dev;
845 pdata = (struct st_sensors_platform_data *)dev->platform_data;
846 drdy_pin = pdata ? pdata->drdy_int_pin : 1;
851 *drdy_reg = ST_LSM6DSX_REG_INT1_ADDR;
854 *drdy_reg = ST_LSM6DSX_REG_INT2_ADDR;
857 dev_err(hw->dev, "unsupported data ready pin\n");
865 static int st_lsm6dsx_init_shub(struct st_lsm6dsx_hw *hw)
867 const struct st_lsm6dsx_shub_settings *hub_settings;
868 struct device_node *np = hw->dev->of_node;
869 struct st_sensors_platform_data *pdata;
873 hub_settings = &hw->settings->shub_settings;
875 pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
876 if ((np && of_property_read_bool(np, "st,pullups")) ||
877 (pdata && pdata->pullups)) {
878 err = st_lsm6dsx_set_page(hw, true);
882 data = ST_LSM6DSX_SHIFT_VAL(1, hub_settings->pullup_en.mask);
883 err = regmap_update_bits(hw->regmap,
884 hub_settings->pullup_en.addr,
885 hub_settings->pullup_en.mask, data);
887 st_lsm6dsx_set_page(hw, false);
893 if (hub_settings->aux_sens.addr) {
894 /* configure aux sensors */
895 err = st_lsm6dsx_set_page(hw, true);
899 data = ST_LSM6DSX_SHIFT_VAL(3, hub_settings->aux_sens.mask);
900 err = regmap_update_bits(hw->regmap,
901 hub_settings->aux_sens.addr,
902 hub_settings->aux_sens.mask, data);
904 st_lsm6dsx_set_page(hw, false);
910 static int st_lsm6dsx_init_hw_timer(struct st_lsm6dsx_hw *hw)
912 const struct st_lsm6dsx_hw_ts_settings *ts_settings;
915 ts_settings = &hw->settings->ts_settings;
916 /* enable hw timestamp generation if necessary */
917 if (ts_settings->timer_en.addr) {
918 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->timer_en.mask);
919 err = regmap_update_bits(hw->regmap,
920 ts_settings->timer_en.addr,
921 ts_settings->timer_en.mask, val);
926 /* enable high resolution for hw ts timer if necessary */
927 if (ts_settings->hr_timer.addr) {
928 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->hr_timer.mask);
929 err = regmap_update_bits(hw->regmap,
930 ts_settings->hr_timer.addr,
931 ts_settings->hr_timer.mask, val);
936 /* enable ts queueing in FIFO if necessary */
937 if (ts_settings->fifo_en.addr) {
938 val = ST_LSM6DSX_SHIFT_VAL(1, ts_settings->fifo_en.mask);
939 err = regmap_update_bits(hw->regmap,
940 ts_settings->fifo_en.addr,
941 ts_settings->fifo_en.mask, val);
948 static int st_lsm6dsx_init_device(struct st_lsm6dsx_hw *hw)
953 /* device sw reset */
954 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR,
955 ST_LSM6DSX_REG_RESET_MASK,
956 FIELD_PREP(ST_LSM6DSX_REG_RESET_MASK, 1));
962 /* reload trimming parameter */
963 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_RESET_ADDR,
964 ST_LSM6DSX_REG_BOOT_MASK,
965 FIELD_PREP(ST_LSM6DSX_REG_BOOT_MASK, 1));
971 /* enable Block Data Update */
972 err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_BDU_ADDR,
973 ST_LSM6DSX_REG_BDU_MASK,
974 FIELD_PREP(ST_LSM6DSX_REG_BDU_MASK, 1));
978 /* enable FIFO watermak interrupt */
979 err = st_lsm6dsx_get_drdy_reg(hw, &drdy_int_reg);
983 err = regmap_update_bits(hw->regmap, drdy_int_reg,
984 ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
985 FIELD_PREP(ST_LSM6DSX_REG_FIFO_FTH_IRQ_MASK,
990 err = st_lsm6dsx_init_shub(hw);
994 return st_lsm6dsx_init_hw_timer(hw);
997 static struct iio_dev *st_lsm6dsx_alloc_iiodev(struct st_lsm6dsx_hw *hw,
998 enum st_lsm6dsx_sensor_id id,
1001 struct st_lsm6dsx_sensor *sensor;
1002 struct iio_dev *iio_dev;
1004 iio_dev = devm_iio_device_alloc(hw->dev, sizeof(*sensor));
1008 iio_dev->modes = INDIO_DIRECT_MODE;
1009 iio_dev->dev.parent = hw->dev;
1010 iio_dev->available_scan_masks = st_lsm6dsx_available_scan_masks;
1012 sensor = iio_priv(iio_dev);
1015 sensor->odr = st_lsm6dsx_odr_table[id].odr_avl[0].hz;
1016 sensor->gain = st_lsm6dsx_fs_table[id].fs_avl[0].gain;
1017 sensor->watermark = 1;
1020 case ST_LSM6DSX_ID_ACC:
1021 iio_dev->channels = st_lsm6dsx_acc_channels;
1022 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_acc_channels);
1023 iio_dev->info = &st_lsm6dsx_acc_info;
1025 scnprintf(sensor->name, sizeof(sensor->name), "%s_accel",
1028 case ST_LSM6DSX_ID_GYRO:
1029 iio_dev->channels = st_lsm6dsx_gyro_channels;
1030 iio_dev->num_channels = ARRAY_SIZE(st_lsm6dsx_gyro_channels);
1031 iio_dev->info = &st_lsm6dsx_gyro_info;
1033 scnprintf(sensor->name, sizeof(sensor->name), "%s_gyro",
1039 iio_dev->name = sensor->name;
1044 int st_lsm6dsx_probe(struct device *dev, int irq, int hw_id, const char *name,
1045 struct regmap *regmap)
1047 const struct st_lsm6dsx_shub_settings *hub_settings;
1048 struct st_lsm6dsx_hw *hw;
1051 hw = devm_kzalloc(dev, sizeof(*hw), GFP_KERNEL);
1055 dev_set_drvdata(dev, (void *)hw);
1057 mutex_init(&hw->fifo_lock);
1058 mutex_init(&hw->conf_lock);
1059 mutex_init(&hw->page_lock);
1061 hw->buff = devm_kzalloc(dev, ST_LSM6DSX_BUFF_SIZE, GFP_KERNEL);
1067 hw->regmap = regmap;
1069 err = st_lsm6dsx_check_whoami(hw, hw_id);
1073 for (i = 0; i < ST_LSM6DSX_ID_EXT0; i++) {
1074 hw->iio_devs[i] = st_lsm6dsx_alloc_iiodev(hw, i, name);
1075 if (!hw->iio_devs[i])
1079 err = st_lsm6dsx_init_device(hw);
1083 hub_settings = &hw->settings->shub_settings;
1084 if (hub_settings->master_en.addr) {
1085 err = st_lsm6dsx_shub_probe(hw, name);
1091 err = st_lsm6dsx_fifo_setup(hw);
1096 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1097 if (!hw->iio_devs[i])
1100 err = devm_iio_device_register(hw->dev, hw->iio_devs[i]);
1107 EXPORT_SYMBOL(st_lsm6dsx_probe);
1109 static int __maybe_unused st_lsm6dsx_suspend(struct device *dev)
1111 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
1112 struct st_lsm6dsx_sensor *sensor;
1113 const struct st_lsm6dsx_reg *reg;
1117 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1118 if (!hw->iio_devs[i])
1121 sensor = iio_priv(hw->iio_devs[i]);
1122 if (!(hw->enable_mask & BIT(sensor->id)))
1125 reg = &st_lsm6dsx_odr_table[sensor->id].reg;
1126 data = ST_LSM6DSX_SHIFT_VAL(0, reg->mask);
1127 err = st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask,
1133 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS)
1134 err = st_lsm6dsx_flush_fifo(hw);
1139 static int __maybe_unused st_lsm6dsx_resume(struct device *dev)
1141 struct st_lsm6dsx_hw *hw = dev_get_drvdata(dev);
1142 struct st_lsm6dsx_sensor *sensor;
1145 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
1146 if (!hw->iio_devs[i])
1149 sensor = iio_priv(hw->iio_devs[i]);
1150 if (!(hw->enable_mask & BIT(sensor->id)))
1153 err = st_lsm6dsx_set_odr(sensor, sensor->odr);
1158 if (hw->enable_mask)
1159 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
1164 const struct dev_pm_ops st_lsm6dsx_pm_ops = {
1165 SET_SYSTEM_SLEEP_PM_OPS(st_lsm6dsx_suspend, st_lsm6dsx_resume)
1167 EXPORT_SYMBOL(st_lsm6dsx_pm_ops);
1169 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi@st.com>");
1170 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
1171 MODULE_DESCRIPTION("STMicroelectronics st_lsm6dsx driver");
1172 MODULE_LICENSE("GPL v2");