Re: [PATCH 2/3] iio: accel: kxsd9: modernize runtime PM in read_raw

From: Jonathan Cameron

Date: Sun Jul 19 2026 - 22:20:23 EST


On Tue, 14 Jul 2026 17:55:22 +0530
Biren Pandya <birenpandya@xxxxxxxxx> wrote:

> Replace the legacy pm_runtime_get_sync() call with the modern
> pm_runtime_resume_and_get() in kxsd9_read_raw().
>
> If the resume fails, we now correctly abort the read instead of
> carrying on and reading garbage data from the sleeping hardware.
>
> Suggested-by: Jonathan Cameron <jic23@xxxxxxxxxx>
> Signed-off-by: Biren Pandya <birenpandya@xxxxxxxxx>
> ---
> drivers/iio/accel/kxsd9.c | 9 +++++++--
> 1 file changed, 7 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/accel/kxsd9.c b/drivers/iio/accel/kxsd9.c
> index b3acb20441d86..680d43135e617 100644
> --- a/drivers/iio/accel/kxsd9.c
> +++ b/drivers/iio/accel/kxsd9.c
> @@ -164,13 +164,15 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,
> struct iio_chan_spec const *chan,
> int *val, int *val2, long mask)
> {
> - int ret = -EINVAL;
> struct kxsd9_state *st = iio_priv(indio_dev);
> unsigned int regval;
> __be16 raw_val;
> u16 nval;
> + int ret;
>
> - pm_runtime_get_sync(st->dev);
> + ret = pm_runtime_resume_and_get(st->dev);

Look at PM_RUNTIME_ACQUIRE_AUTOSUSPEND() and consider
how it would improve this code - particularly with respect to
getting rid of the gotos. This function is almost a text book case
of why that stuff is useful!

Thanks,

Jonathan


> + if (ret < 0)
> + return ret;
>
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> @@ -199,6 +201,9 @@ static int kxsd9_read_raw(struct iio_dev *indio_dev,
> *val2 = kxsd9_micro_scales[regval & KXSD9_CTRL_C_FS_MASK];
> ret = IIO_VAL_INT_PLUS_MICRO;
> break;
> + default:
> + ret = -EINVAL;
> + break;
> }
>
> error_ret: