Re: [PATCH 1/2] staging: iio: ad7780: Add gain & filter gpio support

From: Popa, Stefan Serban
Date: Thu Nov 29 2018 - 06:18:24 EST


On Mi, 2018-11-28 at 16:15 -0200, Giuliano Belinassi wrote:
> Previously, the AD7780 driver only supported gpio for the 'powerdown'
> pin. This commit adds suppport for the 'gain' and 'filter' pin.
>
> Signed-off-by: Giuliano Belinassi <giuliano.belinassi@xxxxxx>
> ---
> Changes in v2:
> ÂÂÂÂ- Now this patch is part of the patchset that aims to remove ad7780
> out of staging. https://marc.info/?l=linux-iio&m=154282349808890&w=2
> ÂÂÂÂ- Also, now it reads voltage and filter values from the userspace
> instead of gpio pin states.

Hello,
Please see bellow.

>
> Âdrivers/staging/iio/adc/ad7780.cÂÂÂÂÂÂÂ| 78 ++++++++++++++++++++++++--
> Âinclude/linux/iio/adc/ad_sigma_delta.h |ÂÂ5 ++
> Â2 files changed, 79 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c
> b/drivers/staging/iio/adc/ad7780.c
> index c4a85789c2db..05979a79fda3 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -39,6 +39,12 @@
> Â#define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
> Â#define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 |
> AD7170_PAT2)
> Â
> +#define AD7780_GAIN_GPIO 0
> +#define AD7780_FILTER_GPIO 1
> +
> +#define AD7780_GAIN_MIDPOINT 64
> +#define AD7780_FILTER_MIDPOINT 13350
> +
> Âstruct ad7780_chip_info {
> Â struct iio_chan_spec channel;
> Â unsigned int pattern_mask;
> @@ -50,6 +56,8 @@ struct ad7780_state {
> Â const struct ad7780_chip_info *chip_info;
> Â struct regulator *reg;
> Â struct gpio_desc *powerdown_gpio;
> + struct gpio_desc *gain_gpio;
> + struct gpio_desc *filter_gpio;
> Â unsigned int gain;
> Â
> Â struct ad_sigma_delta sd;
> @@ -115,18 +123,65 @@ static int ad7780_read_raw(struct iio_dev
> *indio_dev,
> Â return -EINVAL;
> Â}
> Â
> +static int ad7780_write_raw(struct iio_dev *indio_dev,
> + ÂÂÂÂstruct iio_chan_spec const *chan,
> + ÂÂÂÂint val,
> + ÂÂÂÂint val2,
> + ÂÂÂÂlong m)
> +{
> + struct ad7780_state *st = iio_priv(indio_dev);
> + const struct ad7780_chip_info *chip_info = st->chip_info;
> + int uvref, gain;
> + unsigned int full_scale;
> +
> + if (!chip_info->is_ad778x)
> + return 0;
> +
> + switch (m) {
> + case IIO_CHAN_INFO_SCALE:
> + if (val != 0)
> + return -EINVAL;
> +
> + uvref = regulator_get_voltage(st->reg);

regulator_get_voltage() has already been called in the probe function and
the result is stored inÂst->int_vref_mv.
My suggestion would be to use a local vref variable declared as unsigned
int. It is my fault that I haven't explained correctly in the previous
email, but you need to multiply vref_mv withÂ1000000LL in order to get the
right precision: vref = st->int_vref_mv * 1000000LL. Afterwards you will be
able to perform the divisions.
> +
> + if (uvref < 0)
> + return uvref;
> +
> + full_scale = 1 << (chip_info->channel.scan_type.realbits
> - 1);
> + gain = DIV_ROUND_CLOSEST(uvref, full_scale);
> + gain = DIV_ROUND_CLOSEST(gain, val2);
> +
> + gpiod_set_value(st->gain_gpio, gain <
> AD7780_GAIN_MIDPOINT ? 0 : 1);

Once the gain is set, you can store it in st->gain variable.

> + case IIO_CHAN_INFO_SAMP_FREQ:
> + if (val2 != 0)
> + return -EINVAL;
> +
> + gpiod_set_value(st->filter_gpio, val <
> AD7780_FILTER_MIDPOINT ? 0 : 1);

This is probably fine, although I am not a big fan of the ternary operator.
A simple if else statement would do. However, I don't feel strongly about
it, so feel free to disagree.Â

> + break;
> + }
> +
> + return 0;
> +}
> +
> Âstatic int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> Â ÂÂÂÂÂunsigned int raw_sample)
> Â{
> Â struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
> Â const struct ad7780_chip_info *chip_info = st->chip_info;
> + int val;
> Â
> Â if ((raw_sample & AD7780_ERR) ||
> Â ÂÂÂÂ((raw_sample & chip_info->pattern_mask) != chip_info-
> >pattern))
> Â return -EIO;
> Â
> Â if (chip_info->is_ad778x) {
> - if (raw_sample & AD7780_GAIN)
> + val = raw_sample & AD7780_GAIN;
> +
> + if (val != gpiod_get_value(st->gain_gpio))
> + return -EIO;

It is not obvious to me what is the point of this check. Maybe you can add
a comment?

> +
> + if (val)
> Â st->gain = 1;
> Â else
> Â st->gain = 128;

Do we still need this? I am not convinced.Â

> @@ -141,18 +196,20 @@ static const struct ad_sigma_delta_info
> ad7780_sigma_delta_info = {
> Â .has_registers = false,
> Â};
> Â
> -#define AD7780_CHANNEL(bits, wordsize) \
> +#define AD7170_CHANNEL(bits, wordsize) \
> Â AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
> +#define AD7780_CHANNEL(bits, wordsize) \
> + AD_SD_CHANNEL_GAIN_FILTER(1, 0, 0, bits, 32, wordsize - bits)
> Â
> Âstatic const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
> Â [ID_AD7170] = {
> - .channel = AD7780_CHANNEL(12, 24),
> + .channel = AD7170_CHANNEL(12, 24),
> Â .pattern = AD7170_PATTERN,
> Â .pattern_mask = AD7170_PATTERN_MASK,
> Â .is_ad778x = false,
> Â },
> Â [ID_AD7171] = {
> - .channel = AD7780_CHANNEL(16, 24),
> + .channel = AD7170_CHANNEL(16, 24),
> Â .pattern = AD7170_PATTERN,
> Â .pattern_mask = AD7170_PATTERN_MASK,
> Â .is_ad778x = false,
> @@ -173,6 +230,7 @@ static const struct ad7780_chip_info
> ad7780_chip_info_tbl[] = {
> Â
> Âstatic const struct iio_info ad7780_info = {
> Â .read_raw = ad7780_read_raw,
> + .write_raw = ad7780_write_raw,
> Â};
> Â
> Âstatic int ad7780_probe(struct spi_device *spi)
> @@ -222,6 +280,18 @@ static int ad7780_probe(struct spi_device *spi)
> Â goto error_disable_reg;
> Â }
> Â
> + if (st->chip_info->is_ad778x) {
> + st->gain_gpio = devm_gpiod_get_optional(&spi->dev,
> + "gain",
> + GPIOD_OUT_HIGH);
> + if (IS_ERR(st->gain_gpio)) {

if the GPIO is optional, then we should continue in case ofÂ-ENODEV.

Shouldn't we have aÂdevm_gpiod_get_optional() call also for filter_gpio?

> + ret = PTR_ERR(st->gain_gpio);
> + dev_err(&spi->dev, "Failed to request gain GPIO:
> %d\n",
> + ret);
> + goto error_disable_reg;
> + }
> + }
> +
> Â ret = ad_sd_setup_buffer_and_trigger(indio_dev);
> Â if (ret)
> Â goto error_disable_reg;
> diff --git a/include/linux/iio/adc/ad_sigma_delta.h
> b/include/linux/iio/adc/ad_sigma_delta.h
> index 730ead1a46df..6cadab6fd5fd 100644
> --- a/include/linux/iio/adc/ad_sigma_delta.h
> +++ b/include/linux/iio/adc/ad_sigma_delta.h
> @@ -173,6 +173,11 @@ int ad_sd_validate_trigger(struct iio_dev
> *indio_dev, struct iio_trigger *trig);
> Â __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
> Â _storagebits, _shift, NULL, IIO_VOLTAGE, 0)
> Â
> +#define AD_SD_CHANNEL_GAIN_FILTER(_si, _channel, _address, _bits, \
> + _storagebits, _shift) \
> + __AD_SD_CHANNEL(_si, _channel, -1, _address, _bits, \
> + _storagebits, _shift, NULL, IIO_VOLTAGE,
> BIT(IIO_CHAN_INFO_RAW))
> +
> Â#define AD_SD_TEMP_CHANNEL(_si, _address, _bits, _storagebits, _shift) \
> Â __AD_SD_CHANNEL(_si, 0, -1, _address, _bits, \
> Â _storagebits, _shift, NULL, IIO_TEMP, \