* [PATCH] iio: adc: mt6360: Handle error in cleanup path correctly
@ 2025-09-29 2:53 Haotian Zhang
2025-09-29 15:26 ` David Lechner
2025-09-29 16:24 ` [PATCH v2] " Haotian Zhang
0 siblings, 2 replies; 4+ messages in thread
From: Haotian Zhang @ 2025-09-29 2:53 UTC (permalink / raw)
To: David Lechner
Cc: Matthias Brugger, AngeloGioacchino Del Regno, Jonathan Cameron,
Nuno Sá, Andy Shevchenko, linux-kernel, linux-arm-kernel,
linux-mediatek, Haotian Zhang
The return value of a regmap_raw_write() in the cleanup path was
being ignored.
Fix this by checking the return value and propagating the error.
Fixes: 1f4877218f7e ("iio: adc: mt6360: Add ADC driver for MT6360")
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
drivers/iio/adc/mt6360-adc.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/adc/mt6360-adc.c b/drivers/iio/adc/mt6360-adc.c
index 69b3569c90e5..97c4af8a93fc 100644
--- a/drivers/iio/adc/mt6360-adc.c
+++ b/drivers/iio/adc/mt6360-adc.c
@@ -70,6 +70,7 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
ktime_t predict_end_t, timeout;
unsigned int pre_wait_time;
int ret;
+ int cleanup_ret;
mutex_lock(&mad->adc_lock);
@@ -130,11 +131,15 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
out_adc_conv:
/* Only keep ADC enable */
adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
- regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
+ cleanup_ret = regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG,
+ &adc_enable, sizeof(adc_enable));
+ if (ret >= 0)
+ ret = cleanup_ret;
mad->last_off_timestamps[channel] = ktime_get();
/* Config prefer channel to NO_PREFER */
regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
MT6360_NO_PREFER << MT6360_PREFERCH_SHFT);
+
out_adc_lock:
mutex_unlock(&mad->adc_lock);
--
2.50.1.windows.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH] iio: adc: mt6360: Handle error in cleanup path correctly
2025-09-29 2:53 [PATCH] iio: adc: mt6360: Handle error in cleanup path correctly Haotian Zhang
@ 2025-09-29 15:26 ` David Lechner
2025-09-29 16:24 ` [PATCH v2] " Haotian Zhang
1 sibling, 0 replies; 4+ messages in thread
From: David Lechner @ 2025-09-29 15:26 UTC (permalink / raw)
To: Haotian Zhang
Cc: Matthias Brugger, AngeloGioacchino Del Regno, Jonathan Cameron,
Nuno Sá, Andy Shevchenko, linux-kernel, linux-arm-kernel,
linux-mediatek
It looks like you missed the IIO mailing list in the CC, so this might
not show up in Jonathan's queue.
On Mon, Sep 29, 2025 at 4:54 AM Haotian Zhang <vulab@iscas.ac.cn> wrote:
>
> The return value of a regmap_raw_write() in the cleanup path was
> being ignored.
>
> Fix this by checking the return value and propagating the error.
>
> Fixes: 1f4877218f7e ("iio: adc: mt6360: Add ADC driver for MT6360")
> Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
> ---
> drivers/iio/adc/mt6360-adc.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/iio/adc/mt6360-adc.c b/drivers/iio/adc/mt6360-adc.c
> index 69b3569c90e5..97c4af8a93fc 100644
> --- a/drivers/iio/adc/mt6360-adc.c
> +++ b/drivers/iio/adc/mt6360-adc.c
> @@ -70,6 +70,7 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
> ktime_t predict_end_t, timeout;
> unsigned int pre_wait_time;
> int ret;
> + int cleanup_ret;
>
> mutex_lock(&mad->adc_lock);
>
> @@ -130,11 +131,15 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
> out_adc_conv:
> /* Only keep ADC enable */
> adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
> - regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
> + cleanup_ret = regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG,
> + &adc_enable, sizeof(adc_enable));
> + if (ret >= 0)
> + ret = cleanup_ret;
This overwrites the original return error, which is IIO_VAL_INT on
success or an error code. In either case, changing the return value
will break things.
In cleanup paths, there really isn't anything we can do with return
values other than ignore them like we were already doing or log an
error message.
> mad->last_off_timestamps[channel] = ktime_get();
> /* Config prefer channel to NO_PREFER */
> regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
The return value here is also being ignored. If we decide that we
should add error messages, then we should add one here as well.
> MT6360_NO_PREFER << MT6360_PREFERCH_SHFT);
> +
> out_adc_lock:
> mutex_unlock(&mad->adc_lock);
>
> --
> 2.50.1.windows.1
>
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH v2] iio: adc: mt6360: Handle error in cleanup path correctly
2025-09-29 2:53 [PATCH] iio: adc: mt6360: Handle error in cleanup path correctly Haotian Zhang
2025-09-29 15:26 ` David Lechner
@ 2025-09-29 16:24 ` Haotian Zhang
2025-10-04 14:31 ` Jonathan Cameron
1 sibling, 1 reply; 4+ messages in thread
From: Haotian Zhang @ 2025-09-29 16:24 UTC (permalink / raw)
To: Jonathan Cameron, linux-iio
Cc: Lars-Peter Clausen, Matthias Brugger, AngeloGioacchino Del Regno,
Abhash Jha, Al Viro, linux-kernel, linux-arm-kernel,
linux-mediatek, Haotian Zhang
The return value of a regmap_raw_write() and regmap_update_bits()
in the cleanup path was being ignored.
Fix this by checking the return value and warn on error.
Fixes: 1f4877218f7e ("iio: adc: mt6360: Add ADC driver for MT6360")
Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
---
Changes in v2:
- Do not propagate cleanup path errors.
- Log a warning on failure instead of overwriting the return value, as
suggested by the maintainer.
- Also check the return value of regmap_update_bits() for consistency.
---
drivers/iio/adc/mt6360-adc.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/drivers/iio/adc/mt6360-adc.c b/drivers/iio/adc/mt6360-adc.c
index 69b3569c90e5..9ee7247aacbe 100644
--- a/drivers/iio/adc/mt6360-adc.c
+++ b/drivers/iio/adc/mt6360-adc.c
@@ -70,6 +70,7 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
ktime_t predict_end_t, timeout;
unsigned int pre_wait_time;
int ret;
+ int cleanup_ret;
mutex_lock(&mad->adc_lock);
@@ -130,11 +131,16 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
out_adc_conv:
/* Only keep ADC enable */
adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
- regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
+ cleanup_ret = regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG,
+ &adc_enable, sizeof(adc_enable));
+ if (cleanup_ret)
+ dev_warn(mad->dev, "Failed to reset ADC config: %d\n", cleanup_ret);
mad->last_off_timestamps[channel] = ktime_get();
/* Config prefer channel to NO_PREFER */
- regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
+ cleanup_ret = regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
MT6360_NO_PREFER << MT6360_PREFERCH_SHFT);
+ if (cleanup_ret)
+ dev_warn(mad->dev, "Failed to reset prefer channel: %d\n", cleanup_ret);
out_adc_lock:
mutex_unlock(&mad->adc_lock);
--
2.50.1.windows.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* Re: [PATCH v2] iio: adc: mt6360: Handle error in cleanup path correctly
2025-09-29 16:24 ` [PATCH v2] " Haotian Zhang
@ 2025-10-04 14:31 ` Jonathan Cameron
0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Cameron @ 2025-10-04 14:31 UTC (permalink / raw)
To: Haotian Zhang
Cc: linux-iio, Lars-Peter Clausen, Matthias Brugger,
AngeloGioacchino Del Regno, Abhash Jha, Al Viro, linux-kernel,
linux-arm-kernel, linux-mediatek
On Tue, 30 Sep 2025 00:24:53 +0800
Haotian Zhang <vulab@iscas.ac.cn> wrote:
> The return value of a regmap_raw_write() and regmap_update_bits()
> in the cleanup path was being ignored.
>
> Fix this by checking the return value and warn on error.
>
> Fixes: 1f4877218f7e ("iio: adc: mt6360: Add ADC driver for MT6360")
> Signed-off-by: Haotian Zhang <vulab@iscas.ac.cn>
>
> ---
> Changes in v2:
> - Do not propagate cleanup path errors.
> - Log a warning on failure instead of overwriting the return value, as
> suggested by the maintainer.
As below. I think dev_err() is appropriate.
> - Also check the return value of regmap_update_bits() for consistency.
> ---
> drivers/iio/adc/mt6360-adc.c | 10 ++++++++--
> 1 file changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/adc/mt6360-adc.c b/drivers/iio/adc/mt6360-adc.c
> index 69b3569c90e5..9ee7247aacbe 100644
> --- a/drivers/iio/adc/mt6360-adc.c
> +++ b/drivers/iio/adc/mt6360-adc.c
> @@ -70,6 +70,7 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
> ktime_t predict_end_t, timeout;
> unsigned int pre_wait_time;
> int ret;
> + int cleanup_ret;
>
> mutex_lock(&mad->adc_lock);
>
> @@ -130,11 +131,16 @@ static int mt6360_adc_read_channel(struct mt6360_adc_data *mad, int channel, int
> out_adc_conv:
> /* Only keep ADC enable */
> adc_enable = cpu_to_be16(MT6360_ADCEN_MASK);
> - regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG, &adc_enable, sizeof(adc_enable));
> + cleanup_ret = regmap_raw_write(mad->regmap, MT6360_REG_PMUADCCFG,
> + &adc_enable, sizeof(adc_enable));
> + if (cleanup_ret)
> + dev_warn(mad->dev, "Failed to reset ADC config: %d\n", cleanup_ret);
Why warn? If this happens it's definite and error and may bite us later.
> mad->last_off_timestamps[channel] = ktime_get();
> /* Config prefer channel to NO_PREFER */
> - regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
> + cleanup_ret = regmap_update_bits(mad->regmap, MT6360_REG_PMUADCRPT1, MT6360_PREFERCH_MASK,
> MT6360_NO_PREFER << MT6360_PREFERCH_SHFT);
> + if (cleanup_ret)
> + dev_warn(mad->dev, "Failed to reset prefer channel: %d\n", cleanup_ret);
> out_adc_lock:
> mutex_unlock(&mad->adc_lock);
>
Maybe it is worth trying to surface the error if nothing else has already gone wrong. That is a little fiddly
to do but something like
if (cleanup_ret) {
dev_err()
ret = ret ?: cleanup_ret;
}
I'm not sure it is worth the complexity however, so perhaps see if others have comments on this
in next few days before spinning a v3.
Thanks,
Jonathan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-10-04 14:31 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-29 2:53 [PATCH] iio: adc: mt6360: Handle error in cleanup path correctly Haotian Zhang
2025-09-29 15:26 ` David Lechner
2025-09-29 16:24 ` [PATCH v2] " Haotian Zhang
2025-10-04 14:31 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox