[PATCH v5 2/3] iio: adc: ingenic-adc: refactor ingenic_adc_read_chan_info_raw()
Felipe Ribeiro de Souza
felipers em ime.usp.br
Ter Maio 5 22:58:29 -03 2026
Extract the sample logic from ingenic_adc_read_chan_info_raw() into
a new helper function __ingenic_adc_read_chan() to improve code
readability and modularity.
The helper handles the mutex-protected section for sampling channels,
while the main function manages mutex and clock enabling/disabling.
Signed-off-by: Felipe Ribeiro de Souza <felipers em ime.usp.br>
Co-developed-by: Lucas Ivars Cadima Ciziks <lucas em ciziks.com>
Signed-off-by: Lucas Ivars Cadima Ciziks <lucas em ciziks.com>
---
v2:
- Adjust order of #include.
- Replace guard() in ingenic_adc_read_chan_info_raw() by scoped_guard()
to preserve original behavior.
v3:
- Split out ingenic_adc_read_chan_info_raw() logic to helper function.
v4:
- Adjust code style for better readability.
v5:
- Rename ingenic_adc_enable_unlocked() to __ingenic_adc_enable() and
ingenic_adc_read_chan_locked() to __ingenic_adc_read_chan() to keep
the convention about the locked functions name.
- Move the mutex lock back to ingenic_adc_read_chan_info_raw().
---
drivers/iio/adc/ingenic-adc.c | 40 +++++++++++++++++++++--------------
1 file changed, 24 insertions(+), 16 deletions(-)
diff --git a/drivers/iio/adc/ingenic-adc.c b/drivers/iio/adc/ingenic-adc.c
index 231ff8d584c2d..91e3ea6615942 100644
--- a/drivers/iio/adc/ingenic-adc.c
+++ b/drivers/iio/adc/ingenic-adc.c
@@ -627,22 +627,12 @@ static int ingenic_adc_read_avail(struct iio_dev *iio_dev,
}
}
-static int ingenic_adc_read_chan_info_raw(struct iio_dev *iio_dev,
- struct iio_chan_spec const *chan,
- int *val)
+static int __ingenic_adc_read_chan(struct ingenic_adc *adc,
+ struct iio_chan_spec const *chan,
+ int *val)
{
int cmd, ret, engine = (chan->channel == INGENIC_ADC_BATTERY);
- struct ingenic_adc *adc = iio_priv(iio_dev);
-
- ret = clk_enable(adc->clk);
- if (ret) {
- dev_err(iio_dev->dev.parent, "Failed to enable clock: %d\n",
- ret);
- return ret;
- }
- /* We cannot sample the aux channels in parallel. */
- mutex_lock(&adc->aux_lock);
if (adc->soc_data->has_aux_md && engine == 0) {
switch (chan->channel) {
case INGENIC_ADC_AUX0:
@@ -661,7 +651,7 @@ static int ingenic_adc_read_chan_info_raw(struct iio_dev *iio_dev,
ret = ingenic_adc_capture(adc, engine);
if (ret)
- goto out;
+ return ret;
switch (chan->channel) {
case INGENIC_ADC_AUX0:
@@ -674,9 +664,27 @@ static int ingenic_adc_read_chan_info_raw(struct iio_dev *iio_dev,
break;
}
- ret = IIO_VAL_INT;
-out:
+ return IIO_VAL_INT;
+}
+
+static int ingenic_adc_read_chan_info_raw(struct iio_dev *iio_dev,
+ struct iio_chan_spec const *chan,
+ int *val)
+{
+ struct ingenic_adc *adc = iio_priv(iio_dev);
+ int ret;
+
+ ret = clk_enable(adc->clk);
+ if (ret) {
+ dev_err(iio_dev->dev.parent, "Failed to enable clock: %d\n", ret);
+ return ret;
+ }
+
+ /* We cannot sample the aux channels in parallel. */
+ mutex_lock(&adc->aux_lock);
+ ret = __ingenic_adc_read_chan(adc, chan, val);
mutex_unlock(&adc->aux_lock);
+
clk_disable(adc->clk);
return ret;
--
2.43.0
Mais detalhes sobre a lista de discussão kernel