[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20260116200451.4c951e3f@jic23-huawei>
Date: Fri, 16 Jan 2026 20:04:51 +0000
From: Jonathan Cameron <jic23@...nel.org>
To: Francesco Lavra <flavra@...libre.com>
Cc: Lorenzo Bianconi <lorenzo@...nel.org>, David Lechner
<dlechner@...libre.com>, Nuno Sá <nuno.sa@...log.com>, Andy
Shevchenko <andy@...nel.org>, linux-iio@...r.kernel.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH v2 3/3] iio: imu: st_lsm6dsx: Add support for rotation
sensor
On Thu, 15 Jan 2026 13:24:31 +0100
Francesco Lavra <flavra@...libre.com> wrote:
> Some IMU chips in the LSM6DSX family have sensor fusion features that
> combine data from the accelerometer and gyroscope. One of these features
> generates rotation vector data and makes it available in the hardware
> FIFO as a quaternion (more specifically, the X, Y and Z components of the
> quaternion vector, expressed as 16-bit half-precision floating-point
> numbers).
>
> Add support for a new sensor instance that allows receiving sensor fusion
> data, by defining a new struct st_lsm6dsx_sf_settings (which contains
> chip-specific details for the sensor fusion functionality), and adding this
> struct as a new field in struct st_lsm6dsx_settings. In st_lsm6dsx_core.c,
> populate this new struct for the LSM6DSV and LSM6DSV16X chips, and add the
> logic to initialize an additional IIO device if this struct is populated
> for the hardware type being probed.
> Note: a new IIO device is being defined (as opposed to adding channels to
> an existing device) because the rate at which sensor fusion data is
> generated may not match the data rate from any of the existing devices.
>
> Tested on LSMDSV16X.
>
> Signed-off-by: Francesco Lavra <flavra@...libre.com>
Nice. A couple of really minor comments inline.
Jonathan
> diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c
> new file mode 100644
> index 000000000000..3594d97a98ff
> --- /dev/null
> +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_fusion.c
> +
> +int st_lsm6dsx_sf_set_enable(struct st_lsm6dsx_sensor *sensor, bool enable)
> +{
> + struct st_lsm6dsx_hw *hw = sensor->hw;
> + const struct st_lsm6dsx_reg *en_reg;
> + int err;
> +
> + guard(mutex)(&hw->page_lock);
> +
> + en_reg = &hw->settings->sf_settings.enable;
> + err = st_lsm6dsx_sf_set_page(hw, true);
> + if (err < 0)
> + return err;
> +
> + err = regmap_assign_bits(hw->regmap, en_reg->addr, en_reg->mask, enable);
> + st_lsm6dsx_sf_set_page(hw, false);
Similar to below. Feels odd to check all the other accesses but not the one
setting the page back to normal page.
> +
> + return err;
> +}
> +
> +int st_lsm6dsx_sf_set_odr(struct st_lsm6dsx_sensor *sensor, bool enable)
> +{
> + struct st_lsm6dsx_hw *hw = sensor->hw;
> + const struct st_lsm6dsx_sf_settings *settings;
> + u8 data;
> + int err;
> +
> + guard(mutex)(&hw->page_lock);
> +
> + err = st_lsm6dsx_sf_set_page(hw, true);
> + if (err < 0)
> + return err;
> +
> + settings = &hw->settings->sf_settings;
> + if (enable) {
> + u8 odr_val;
> + const struct st_lsm6dsx_reg *reg = &settings->odr_table.reg;
> +
> + st_lsm6dsx_sf_get_odr_val(settings, sensor->hwfifo_odr_mHz,
> + &odr_val);
> + data = ST_LSM6DSX_SHIFT_VAL(odr_val, reg->mask);
> + err = regmap_update_bits(hw->regmap, reg->addr, reg->mask,
> + data);
> + if (err < 0)
> + goto out;
> + }
> + err = regmap_assign_bits(hw->regmap, settings->fifo_enable.addr,
> + settings->fifo_enable.mask, enable);
> +
> +out:
> + st_lsm6dsx_sf_set_page(hw, false);
Whilst there isn't much we can do about it, might be worth the dance
to check return value of this (but not overwrite the original one if
we came here via an error.
Maybe simply duplicate the call
return st_lsm6dsx_sf_set_page(hw, false);
out:
st_lsm6dsx_sf_set_page(hw, false);
return err;
> +
> + return err;
> +}
> +
> +static int st_lsm6dsx_sf_write_raw(struct iio_dev *iio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
> + const struct st_lsm6dsx_sf_settings *settings;
> + int err;
> +
> + settings = &sensor->hw->settings->sf_settings;
> + switch (mask) {
> + case IIO_CHAN_INFO_SAMP_FREQ: {
> + u32 odr_mHz;
> + u8 odr_val;
> +
> + odr_mHz = val * MILLI + val2 * MILLI / MICRO;
> + err = st_lsm6dsx_sf_get_odr_val(settings, odr_mHz, &odr_val);
Why are we getting something we don't use in here? I guess this is checking
what was asked for is supported. I'd add a comment to make that clear
as it's not that obvious (I got confused anyway!)
> + if (err)
> + return err;
> +
> + sensor->hwfifo_odr_mHz = odr_mHz;
> + return 0;
> + }
> + default:
> + return -EINVAL;
> + }
> +}
Powered by blists - more mailing lists