Re: [PATCH v5 5/5] staging: iio: adc: ad7816: Simplify channel validation using chip_info

Jonathan Cameron
2025-04-21T12:37:59Z

On Sat, 19 Apr 2025 21:49:10 -0400
Gabriel Shahrouzi <gshahrouzi@gmail.com> wrote:

> Refactor the channel validation logic within ad7816_store_channel() to
> leverage the max_channels field previously introduced in the
> ad7816_chip_info structure.
>
> Signed-off-by: Gabriel Shahrouzi <gshahrouzi@gmail.com>
Carries forward the odd mask usage from patch 1. So fix that up and then
I think this is fine. Given you introduce max_channels just for this, ideal
would be to only introduce it in this patch (with a better name - see
earlier comment)

Jonathan

> ---
> drivers/staging/iio/adc/ad7816.c | 12 ++----------
> 1 file changed, 2 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7816.c
> index ab7520a8a3da9..7a59cfbcc6e33 100644
> --- a/drivers/staging/iio/adc/ad7816.c
> +++ b/drivers/staging/iio/adc/ad7816.c
> @@ -204,17 +204,9 @@ static ssize_t ad7816_store_channel(struct device *dev,
> if (ret)
> return ret;
>
> - if (data > AD7816_CS_MAX && data != AD7816_CS_MASK) {
> + if (data > chip->chip_info->max_channels && data != AD7816_CS_MASK) {
> dev_err(&chip->spi_dev->dev, "Invalid channel id %lu for %s.\n",
> - data, indio_dev->name);
> - return -EINVAL;
> - } else if (strcmp(indio_dev->name, "ad7818") == 0 && data > 1 && data != AD7816_CS_MASK) {
> - dev_err(&chip->spi_dev->dev,
> - "Invalid channel id %lu for ad7818.\n", data);
> - return -EINVAL;
> - } else if (strcmp(indio_dev->name, "ad7816") == 0 && data > 0 && data != AD7816_CS_MASK) {
> - dev_err(&chip->spi_dev->dev,
> - "Invalid channel id %lu for ad7816.\n", data);
> + data, chip->chip_info->name);
> return -EINVAL;
> }
>