diff mbox series

[v3,2/6] iio: adc: ad7173: refactor channel configuration parsing

Message ID 20240527-ad4111-v3-2-7e9eddbbd3eb@analog.com (mailing list archive)
State Superseded
Headers show
Series Add support for AD411x | expand

Commit Message

Dumitru Ceclan via B4 Relay May 27, 2024, 5:02 p.m. UTC
From: Dumitru Ceclan <dumitru.ceclan@analog.com>

Move configurations regarding number of channels from
*_fw_parse_device_config to *_fw_parse_channel_config.

Suggested-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Link: https://lore.kernel.org/all/20240303162148.3ad91aa2@jic23-huawei/
Reviewed-by: David Lechner <dlechner@baylibre.com>
Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com>
---
 drivers/iio/adc/ad7173.c | 29 +++++++++++++++++------------
 1 file changed, 17 insertions(+), 12 deletions(-)

Comments

Nuno Sá May 29, 2024, 12:24 p.m. UTC | #1
On Mon, 2024-05-27 at 20:02 +0300, Dumitru Ceclan via B4 Relay wrote:
> From: Dumitru Ceclan <dumitru.ceclan@analog.com>
> 
> Move configurations regarding number of channels from
> *_fw_parse_device_config to *_fw_parse_channel_config.
> 
> Suggested-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
> Link: https://lore.kernel.org/all/20240303162148.3ad91aa2@jic23-huawei/
> Reviewed-by: David Lechner <dlechner@baylibre.com>
> Signed-off-by: Dumitru Ceclan <dumitru.ceclan@analog.com>
> ---

Reviewed-by: Nuno Sa <nuno.sa@analog.com>

>  drivers/iio/adc/ad7173.c | 29 +++++++++++++++++------------
>  1 file changed, 17 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/iio/adc/ad7173.c b/drivers/iio/adc/ad7173.c
> index ebacdacf64b9..9e507e2c66f0 100644
> --- a/drivers/iio/adc/ad7173.c
> +++ b/drivers/iio/adc/ad7173.c
> @@ -913,7 +913,23 @@ static int ad7173_fw_parse_channel_config(struct iio_dev
> *indio_dev)
>  	struct device *dev = indio_dev->dev.parent;
>  	struct iio_chan_spec *chan_arr, *chan;
>  	unsigned int ain[2], chan_index = 0;
> -	int ref_sel, ret;
> +	int ref_sel, ret, num_channels;
> +
> +	num_channels = device_get_child_node_count(dev);
> +
> +	if (st->info->has_temp)
> +		num_channels++;
> +
> +	if (num_channels == 0)
> +		return dev_err_probe(dev, -ENODATA, "No channels specified\n");
> +
> +	if (num_channels > st->info->num_channels)
> +		return dev_err_probe(dev, -EINVAL,
> +			"Too many channels specified. Maximum is %d, not including
> temperature channel if supported.\n",
> +			st->info->num_channels);
> +
> +	indio_dev->num_channels = num_channels;
> +	st->num_channels = num_channels;
>  
>  	chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels),
>  				st->num_channels, GFP_KERNEL);
> @@ -1008,7 +1024,6 @@ static int ad7173_fw_parse_device_config(struct iio_dev
> *indio_dev)
>  {
>  	struct ad7173_state *st = iio_priv(indio_dev);
>  	struct device *dev = indio_dev->dev.parent;
> -	unsigned int num_channels;
>  	int ret;
>  
>  	st->regulators[0].supply =
> ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF];
> @@ -1067,16 +1082,6 @@ static int ad7173_fw_parse_device_config(struct iio_dev
> *indio_dev)
>  
>  	ad7173_sigma_delta_info.irq_line = ret;
>  
> -	num_channels = device_get_child_node_count(dev);
> -
> -	if (st->info->has_temp)
> -		num_channels++;
> -
> -	if (num_channels == 0)
> -		return dev_err_probe(dev, -ENODATA, "No channels specified\n");
> -	indio_dev->num_channels = num_channels;
> -	st->num_channels = num_channels;
> -
>  	return ad7173_fw_parse_channel_config(indio_dev);
>  }
>  
>
diff mbox series

Patch

diff --git a/drivers/iio/adc/ad7173.c b/drivers/iio/adc/ad7173.c
index ebacdacf64b9..9e507e2c66f0 100644
--- a/drivers/iio/adc/ad7173.c
+++ b/drivers/iio/adc/ad7173.c
@@ -913,7 +913,23 @@  static int ad7173_fw_parse_channel_config(struct iio_dev *indio_dev)
 	struct device *dev = indio_dev->dev.parent;
 	struct iio_chan_spec *chan_arr, *chan;
 	unsigned int ain[2], chan_index = 0;
-	int ref_sel, ret;
+	int ref_sel, ret, num_channels;
+
+	num_channels = device_get_child_node_count(dev);
+
+	if (st->info->has_temp)
+		num_channels++;
+
+	if (num_channels == 0)
+		return dev_err_probe(dev, -ENODATA, "No channels specified\n");
+
+	if (num_channels > st->info->num_channels)
+		return dev_err_probe(dev, -EINVAL,
+			"Too many channels specified. Maximum is %d, not including temperature channel if supported.\n",
+			st->info->num_channels);
+
+	indio_dev->num_channels = num_channels;
+	st->num_channels = num_channels;
 
 	chan_arr = devm_kcalloc(dev, sizeof(*indio_dev->channels),
 				st->num_channels, GFP_KERNEL);
@@ -1008,7 +1024,6 @@  static int ad7173_fw_parse_device_config(struct iio_dev *indio_dev)
 {
 	struct ad7173_state *st = iio_priv(indio_dev);
 	struct device *dev = indio_dev->dev.parent;
-	unsigned int num_channels;
 	int ret;
 
 	st->regulators[0].supply = ad7173_ref_sel_str[AD7173_SETUP_REF_SEL_EXT_REF];
@@ -1067,16 +1082,6 @@  static int ad7173_fw_parse_device_config(struct iio_dev *indio_dev)
 
 	ad7173_sigma_delta_info.irq_line = ret;
 
-	num_channels = device_get_child_node_count(dev);
-
-	if (st->info->has_temp)
-		num_channels++;
-
-	if (num_channels == 0)
-		return dev_err_probe(dev, -ENODATA, "No channels specified\n");
-	indio_dev->num_channels = num_channels;
-	st->num_channels = num_channels;
-
 	return ad7173_fw_parse_channel_config(indio_dev);
 }