Re: [PATCH v3 2/2] iio: adc: palmas_gpadc: Use guard(mutex) to manage locks
From: Andy Shevchenko
Date: Wed Nov 25 2026 - 06:35:20 EST
On Wed, Nov 25, 2026 at 02:10:54AM +0530, Vaibhav Chahar wrote:
> Use guard(mutex) instead of manual mutex_lock() and mutex_unlock()
> calls to modernize the driver and simplify error handling paths.
>
> Made a new helper function palmas_gpadc_read_raw_locked() and called
> palmas_gpadc_read_done() after it so it can still run before the mutex
> is unlocked, allowing the goto and labels to be removed safely.
...
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
>
> switch (mask) {
> case IIO_CHAN_INFO_RAW:
> case IIO_CHAN_INFO_PROCESSED:
> - ret = palmas_gpadc_read_prepare(adc, adc_chan);
> - if (ret < 0)
> - goto out;
> -
> - ret = palmas_gpadc_start_conversion(adc, adc_chan);
> - if (ret < 0) {
> - dev_err(adc->dev,
> - "ADC start conversion failed\n");
> - goto out;
> - }
> -
> - if (mask == IIO_CHAN_INFO_PROCESSED)
> - ret = palmas_gpadc_get_calibrated_code(
> - adc, adc_chan, ret);
> -
> - *val = ret;
> -
> - ret = IIO_VAL_INT;
> - goto out;
> + ret = palmas_gpadc_read_raw_locked(adc, adc_chan, val, mask);
> + palmas_gpadc_read_done(adc, adc_chan);
> + return ret;
Hmm... But now this _read_done call makes sense inside the helper.
return palmas_gpadc_read_raw_locked(adc, adc_chan, val, mask);
> }
>
> - mutex_unlock(&adc->lock);
> - return ret;
> -
> -out:
> - palmas_gpadc_read_done(adc, adc_chan);
> - mutex_unlock(&adc->lock);
> -
> return ret;
Are you expect anything than 0 here?
...
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
>
> if (palmas_gpadc_get_event(adc, adc_chan, dir))
> - ret = 1;
> + return 1;
>
> - mutex_unlock(&adc->lock);
> -
> - return ret;
> + return 0;
> }
So, what is the returning value of palmas_gpadc_get_event()? It might be
possible to simplify more, but current approach is good enough.
...
> - mutex_lock(&adc->lock);
> + guard(mutex)(&adc->lock);
> +
> switch (info) {
> case IIO_EV_INFO_VALUE:
> - if (val < 0 || val > 0xFFF) {
> - ret = -EINVAL;
> - goto out_unlock;
> - }
> + if (val < 0 || val > 0xFFF)
> + return -EINVAL;
> if (dir == IIO_EV_DIR_RISING) {
> old = adc->thresholds[adc_chan].high;
> adc->thresholds[adc_chan].high = val;
> @@ -759,20 +743,14 @@ static int palmas_gpadc_write_event_value(struct iio_dev *indio_dev,
> old = adc->thresholds[adc_chan].low;
> adc->thresholds[adc_chan].low = val;
> }
> - ret = 0;
> break;
> default:
> - ret = -EINVAL;
> - goto out_unlock;
> + return -EINVAL;
> }
>
> if (val != old && palmas_gpadc_get_event(adc, adc_chan, dir))
> - ret = palmas_gpadc_reconfigure_event_channels(adc);
> -
> -out_unlock:
> - mutex_unlock(&adc->lock);
> -
> - return ret;
> + return palmas_gpadc_reconfigure_event_channels(adc);
I don't see the full context, but can this be moved inside the switch-case now?
> + return 0;
--
With Best Regards,
Andy Shevchenko