2019-03-16 02:14:49

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 00/11] staging: iio: ad7780: move out of staging

This series of patches contains the following:
- Adds user input for the 'gain' and 'filter' GPIO pins for the ad778x
family chips;
- Missing switch default case tidy up;
- Gain reading for the ad778x;
- Filter reading for the ad778x;
- Sets pattern macro values and mask for PATTERN status bits;
- Adds ID values for the ad7170, ad7171, ad7780 and ad7781 for ID
status bits checking;
- Moves regulator initialization to after GPIO init to maintain
consistency between probe and remove;
- Copyright edits, adding SPDX identifier and new copyright holder;
- Moves the ad7780 driver out of staging to the mainline;
- Adds device tree binding for the ad7780 driver.

Renato Lui Geh (11):
staging: iio: ad7780: add gain & filter gpio support
staging: iio: ad7780: add missing switch default case
staging: iio: ad7780: add gain reading to ad778x
staging: iio: ad7780: add filter reading to ad778x
staging: iio: ad7780: set pattern values and masks directly
staging:iio:ad7780: add chip ID values and mask
staging: iio: ad7780: move regulator to after GPIO init
staging: iio: ad7780: add SPDX identifier
staging: iio: ad7780: add new copyright holder
staging: iio: ad7780: moving ad7780 out of staging
staging: iio: ad7780: add device tree binding

Changelog:
*v3
- SPDX and regulator init as patches
- Renamed filter to odr and ad778x_filter to ad778x_odr_avail
- Removed unnecessary regulator disabling
- Removed unnecessary AD_SD_CHANNEL macro
- Changed unsigned int to unsigned long long to avoid overflow
*v4
- Split gain & filter patch into two, with the new commit adding only
filter reading
- Changed pattern values to direct values, and added pattern mask
- Added ID values and mask
- Added new copyright holder
- Added device tree binding to the ad7780 driver
*v5
- Tidy ups
- Gain reading split from "gain & filter gpio support" commit to its
own separate patch
- ad778x_odr_avail moved to more appropriate patch
- Init GPIOs in a separate function
- Raise error when ad717x tries to perform a write operation

.../bindings/iio/adc/adi,ad7780.txt | 48 +++
drivers/iio/adc/Kconfig | 12 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/ad7780.c | 376 ++++++++++++++++++
drivers/staging/iio/adc/Kconfig | 13 -
drivers/staging/iio/adc/Makefile | 1 -
drivers/staging/iio/adc/ad7780.c | 277 -------------
7 files changed, 437 insertions(+), 291 deletions(-)
create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
create mode 100644 drivers/iio/adc/ad7780.c
delete mode 100644 drivers/staging/iio/adc/ad7780.c

--
2.21.0



2019-03-16 02:14:06

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 01/11] staging: iio: ad7780: add gain & filter gpio support

Previously, the AD7780 driver only supported gpio for the 'powerdown'
pin. This commit adds suppport for the 'gain' and 'filter' pin.

Signed-off-by: Renato Lui Geh <[email protected]>
Signed-off-by: Giuliano Belinassi <[email protected]>
Co-developed-by: Giuliano Belinassi <[email protected]>
---
Changes in v3:
- Renamed ad7780_chip_info's filter to odr
- Renamed ad778x_filter to ad778x_odr_avail
- Changed vref variable from unsigned int to unsigned long long to avoid
overflow
- Removed unnecessary AD_SD_CHANNEL macro
Changes in v4:
- Removed useless macro
- Added default case for switch to suppress warning
- Removed chunks belonging to filter reading, adding these as a
patch for itself
Changes in v5:
- ad778x_odr_avail moved to filter patch
- Split gain reading to its own patch
- Init GPIOs in a separate function

drivers/staging/iio/adc/ad7780.c | 114 ++++++++++++++++++++++++++-----
1 file changed, 96 insertions(+), 18 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index c4a85789c2db..07e5e35c92a3 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -39,6 +39,9 @@
#define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
#define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 | AD7170_PAT2)

+#define AD7780_GAIN_MIDPOINT 64
+#define AD7780_FILTER_MIDPOINT 13350
+
struct ad7780_chip_info {
struct iio_chan_spec channel;
unsigned int pattern_mask;
@@ -50,7 +53,10 @@ struct ad7780_state {
const struct ad7780_chip_info *chip_info;
struct regulator *reg;
struct gpio_desc *powerdown_gpio;
- unsigned int gain;
+ struct gpio_desc *gain_gpio;
+ struct gpio_desc *filter_gpio;
+ unsigned int gain;
+ unsigned int int_vref_mv;

struct ad_sigma_delta sd;
};
@@ -104,8 +110,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
voltage_uv = regulator_get_voltage(st->reg);
if (voltage_uv < 0)
return voltage_uv;
- *val = (voltage_uv / 1000) * st->gain;
+ voltage_uv /= 1000;
+ *val = voltage_uv * st->gain;
*val2 = chan->scan_type.realbits - 1;
+ st->int_vref_mv = voltage_uv;
return IIO_VAL_FRACTIONAL_LOG2;
case IIO_CHAN_INFO_OFFSET:
*val = -(1 << (chan->scan_type.realbits - 1));
@@ -115,6 +123,50 @@ 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;
+ unsigned long long vref;
+ unsigned int full_scale, gain;
+
+ if (!chip_info->is_ad778x)
+ return -EINVAL;
+
+ switch (m) {
+ case IIO_CHAN_INFO_SCALE:
+ if (val != 0)
+ return -EINVAL;
+
+ vref = st->int_vref_mv * 1000000LL;
+ full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
+ gain = DIV_ROUND_CLOSEST(vref, full_scale);
+ gain = DIV_ROUND_CLOSEST(gain, val2);
+ st->gain = gain;
+ if (gain < AD7780_GAIN_MIDPOINT)
+ gain = 0;
+ else
+ gain = 1;
+ gpiod_set_value(st->gain_gpio, gain);
+ break;
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
+ val = 0;
+ else
+ val = 1;
+ gpiod_set_value(st->filter_gpio, val);
+ break;
+ default:
+ break;
+ }
+
+ return 0;
+}
+
static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
unsigned int raw_sample)
{
@@ -125,13 +177,6 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
return -EIO;

- if (chip_info->is_ad778x) {
- if (raw_sample & AD7780_GAIN)
- st->gain = 1;
- else
- st->gain = 128;
- }
-
return 0;
}

@@ -173,8 +218,47 @@ 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_init_gpios(struct device *dev, struct ad7780_state *st)
+{
+ int ret;
+
+ st->powerdown_gpio = devm_gpiod_get_optional(dev,
+ "powerdown",
+ GPIOD_OUT_LOW);
+ if (IS_ERR(st->powerdown_gpio)) {
+ ret = PTR_ERR(st->powerdown_gpio);
+ dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
+ return ret;
+ }
+
+ if (!st->chip_info->is_ad778x)
+ return 0;
+
+
+ st->gain_gpio = devm_gpiod_get_optional(dev,
+ "adi,gain",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(st->gain_gpio)) {
+ ret = PTR_ERR(st->gain_gpio);
+ dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
+ return ret;
+ }
+
+ st->filter_gpio = devm_gpiod_get_optional(dev,
+ "adi,filter",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(st->filter_gpio)) {
+ ret = PTR_ERR(st->filter_gpio);
+ dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
static int ad7780_probe(struct spi_device *spi)
{
struct ad7780_state *st;
@@ -212,15 +296,9 @@ static int ad7780_probe(struct spi_device *spi)
indio_dev->num_channels = 1;
indio_dev->info = &ad7780_info;

- st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
- "powerdown",
- GPIOD_OUT_LOW);
- if (IS_ERR(st->powerdown_gpio)) {
- ret = PTR_ERR(st->powerdown_gpio);
- dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
- ret);
- goto error_disable_reg;
- }
+ ret = ad7780_init_gpios(&spi->dev, st);
+ if (ret)
+ goto error_cleanup_buffer_and_trigger;

ret = ad_sd_setup_buffer_and_trigger(indio_dev);
if (ret)
--
2.21.0


2019-03-16 02:14:35

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 02/11] staging: iio: ad7780: add missing switch default case

This patch simply adds a missing switch default case in read_raw.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 07e5e35c92a3..f4cd7bc3e02f 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -118,6 +118,8 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_OFFSET:
*val = -(1 << (chan->scan_type.realbits - 1));
return IIO_VAL_INT;
+ default:
+ break;
}

return -EINVAL;
--
2.21.0


2019-03-16 02:14:59

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 03/11] staging: iio: ad7780: add gain reading to ad778x

This patch adds a new functionality of reading gain values from the
ad778x chips. This value is stored in the chip's state struct and is
updated whenever a read or write call is performed on the driver.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 5 +++++
1 file changed, 5 insertions(+)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index f4cd7bc3e02f..fbcc0d3345ca 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -42,6 +42,8 @@
#define AD7780_GAIN_MIDPOINT 64
#define AD7780_FILTER_MIDPOINT 13350

+static const unsigned int ad778x_gain[2] = { 1, 128 };
+
struct ad7780_chip_info {
struct iio_chan_spec channel;
unsigned int pattern_mask;
@@ -179,6 +181,9 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
return -EIO;

+ if (chip_info->is_ad778x)
+ st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
+
return 0;
}

--
2.21.0


2019-03-16 02:15:19

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 04/11] staging: iio: ad7780: add filter reading to ad778x

This patch adds the new feature of reading the filter odr value for
ad778x chips. This value is stored in the chip's state struct whenever a
read or write call is performed on the chip's driver.

This feature requires sharing SAMP_FREQ. Since the ad717x does not have
a filter option, the driver only shares the relevant info mask for the
ad778x family.

Signed-off-by: Renato Lui Geh <[email protected]>
---
Changes in v4:
- As mentioned in the previous patch, this was originally as part of
the 'add gain & filter gpio support' patch
Changes in v5:
- Moved ad778x_odr_avail declaration from GPIO patch to this one

drivers/staging/iio/adc/ad7780.c | 18 ++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index fbcc0d3345ca..8ff74427d975 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -42,7 +42,8 @@
#define AD7780_GAIN_MIDPOINT 64
#define AD7780_FILTER_MIDPOINT 13350

-static const unsigned int ad778x_gain[2] = { 1, 128 };
+static const unsigned int ad778x_gain[2] = { 1, 128 };
+static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };

struct ad7780_chip_info {
struct iio_chan_spec channel;
@@ -58,6 +59,7 @@ struct ad7780_state {
struct gpio_desc *gain_gpio;
struct gpio_desc *filter_gpio;
unsigned int gain;
+ unsigned int odr;
unsigned int int_vref_mv;

struct ad_sigma_delta sd;
@@ -120,6 +122,9 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
case IIO_CHAN_INFO_OFFSET:
*val = -(1 << (chan->scan_type.realbits - 1));
return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *val = st->odr;
+ return IIO_VAL_INT;
default:
break;
}
@@ -162,6 +167,7 @@ static int ad7780_write_raw(struct iio_dev *indio_dev,
val = 0;
else
val = 1;
+ st->odr = ad778x_odr_avail[val];
gpiod_set_value(st->filter_gpio, val);
break;
default:
@@ -181,8 +187,10 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
return -EIO;

- if (chip_info->is_ad778x)
+ if (chip_info->is_ad778x) {
st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
+ st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
+ }

return 0;
}
@@ -194,17 +202,19 @@ static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
};

#define AD7780_CHANNEL(bits, wordsize) \
+ AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
+#define AD7170_CHANNEL(bits, wordsize) \
AD_SD_CHANNEL_NO_SAMP_FREQ(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,
--
2.21.0


2019-03-16 02:15:26

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 05/11] staging: iio: ad7780: set pattern values and masks directly

The AD7780 driver contains status pattern bits designed for checking
whether serial transfers have been correctly performed. Pattern macros
were previously generated through bit fields. This patch sets good
pattern values directly and masks through GENMASK.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 20 +++++++++-----------
1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 8ff74427d975..94cb60c327d0 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -17,6 +17,7 @@
#include <linux/sched.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
+#include <linux/bits.h>

#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
@@ -28,16 +29,13 @@
#define AD7780_ID1 BIT(4)
#define AD7780_ID0 BIT(3)
#define AD7780_GAIN BIT(2)
-#define AD7780_PAT1 BIT(1)
-#define AD7780_PAT0 BIT(0)

-#define AD7780_PATTERN (AD7780_PAT0)
-#define AD7780_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1)

-#define AD7170_PAT2 BIT(2)
+#define AD7780_PATTERN_GOOD 1
+#define AD7780_PATTERN_MASK GENMASK(1, 0)

-#define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
-#define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 | AD7170_PAT2)
+#define AD7170_PATTERN_GOOD 5
+#define AD7170_PATTERN_MASK GENMASK(2, 0)

#define AD7780_GAIN_MIDPOINT 64
#define AD7780_FILTER_MIDPOINT 13350
@@ -209,25 +207,25 @@ static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
[ID_AD7170] = {
.channel = AD7170_CHANNEL(12, 24),
- .pattern = AD7170_PATTERN,
+ .pattern = AD7170_PATTERN_GOOD,
.pattern_mask = AD7170_PATTERN_MASK,
.is_ad778x = false,
},
[ID_AD7171] = {
.channel = AD7170_CHANNEL(16, 24),
- .pattern = AD7170_PATTERN,
+ .pattern = AD7170_PATTERN_GOOD,
.pattern_mask = AD7170_PATTERN_MASK,
.is_ad778x = false,
},
[ID_AD7780] = {
.channel = AD7780_CHANNEL(24, 32),
- .pattern = AD7780_PATTERN,
+ .pattern = AD7780_PATTERN_GOOD,
.pattern_mask = AD7780_PATTERN_MASK,
.is_ad778x = true,
},
[ID_AD7781] = {
.channel = AD7780_CHANNEL(20, 32),
- .pattern = AD7780_PATTERN,
+ .pattern = AD7780_PATTERN_GOOD,
.pattern_mask = AD7780_PATTERN_MASK,
.is_ad778x = true,
},
--
2.21.0


2019-03-16 02:16:02

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 06/11] staging:iio:ad7780: add chip ID values and mask

The ad7780 supports both the ad778x and ad717x families. Each chip has
a corresponding ID. This patch provides a mask for extracting ID values
from the status bits and also macros for the correct values for the
ad7170, ad7171, ad7780 and ad7781.

Signed-off-by: Renato Lui Geh <[email protected]>
---
Changes in v5:
- Put AD7780_ID{0,1} back

drivers/staging/iio/adc/ad7780.c | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 94cb60c327d0..977b381c1260 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -30,6 +30,12 @@
#define AD7780_ID0 BIT(3)
#define AD7780_GAIN BIT(2)

+#define AD7170_ID 0
+#define AD7171_ID 1
+#define AD7780_ID 1
+#define AD7781_ID 0
+
+#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)

#define AD7780_PATTERN_GOOD 1
#define AD7780_PATTERN_MASK GENMASK(1, 0)
--
2.21.0


2019-03-16 02:16:02

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 07/11] staging: iio: ad7780: move regulator to after GPIO init

To maintain consistency between ad7780_probe and ad7780_remove orders,
regulator initialization has been moved to after GPIO initializations.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 977b381c1260..568c5b4472ff 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -295,16 +295,6 @@ static int ad7780_probe(struct spi_device *spi)

ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);

- st->reg = devm_regulator_get(&spi->dev, "avdd");
- if (IS_ERR(st->reg))
- return PTR_ERR(st->reg);
-
- ret = regulator_enable(st->reg);
- if (ret) {
- dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
- return ret;
- }
-
st->chip_info =
&ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];

@@ -321,6 +311,16 @@ static int ad7780_probe(struct spi_device *spi)
if (ret)
goto error_cleanup_buffer_and_trigger;

+ st->reg = devm_regulator_get(&spi->dev, "avdd");
+ if (IS_ERR(st->reg))
+ return PTR_ERR(st->reg);
+
+ ret = regulator_enable(st->reg);
+ if (ret) {
+ dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
+ return ret;
+ }
+
ret = ad_sd_setup_buffer_and_trigger(indio_dev);
if (ret)
goto error_disable_reg;
--
2.21.0


2019-03-16 02:17:00

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 08/11] staging: iio: ad7780: add SPDX identifier

Add SPDX identifier (GPL-2.0) to the AD7780 driver.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index 568c5b4472ff..ff61ffa0da9f 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -1,9 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
/*
* AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
*
* Copyright 2011 Analog Devices Inc.
- *
- * Licensed under the GPL-2.
*/

#include <linux/interrupt.h>
--
2.21.0


2019-03-16 02:17:11

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 09/11] staging: iio: ad7780: add new copyright holder

This patch adds a new copyright holder to the ad7780 driver.

Signed-off-by: Renato Lui Geh <[email protected]>
---
drivers/staging/iio/adc/ad7780.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
index ff61ffa0da9f..23b731a92e32 100644
--- a/drivers/staging/iio/adc/ad7780.c
+++ b/drivers/staging/iio/adc/ad7780.c
@@ -3,6 +3,7 @@
* AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
*
* Copyright 2011 Analog Devices Inc.
+ * Copyright 2019 Renato Lui Geh
*/

#include <linux/interrupt.h>
--
2.21.0


2019-03-16 02:18:39

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 10/11] staging: iio: ad7780: moving ad7780 out of staging

Move ad7780 ADC driver out of staging and into the mainline.

The ad7780 is a sigma-delta analog to digital converter. This driver provides
reading voltage values and status bits from both the ad778x and ad717x series.
Its interface also allows writing on the FILTER and GAIN GPIO pins on the
ad778x.

Signed-off-by: Renato Lui Geh <[email protected]>
Signed-off-by: Giuliano Belinassi <[email protected]>
Co-developed-by: Giuliano Belinassi <[email protected]>
---
Changes in v3:
- Changes unrelated to moving the driver to main tree were resent as
individual patches
Changes in v4:
- Removed line warning it was safe to build the ad7780 driver by
default

drivers/iio/adc/Kconfig | 12 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/ad7780.c | 376 +++++++++++++++++++++++++++++++
drivers/staging/iio/adc/Kconfig | 13 --
drivers/staging/iio/adc/Makefile | 1 -
drivers/staging/iio/adc/ad7780.c | 376 -------------------------------
6 files changed, 389 insertions(+), 390 deletions(-)
create mode 100644 drivers/iio/adc/ad7780.c
delete mode 100644 drivers/staging/iio/adc/ad7780.c

diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index f3cc7a31bce5..6b36a45bd09f 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -108,6 +108,18 @@ config AD7766
To compile this driver as a module, choose M here: the module will be
called ad7766.

+config AD7780
+ tristate "Analog Devices AD7780 and similar ADCs driver"
+ depends on SPI
+ depends on GPIOLIB || COMPILE_TEST
+ select AD_SIGMA_DELTA
+ help
+ Say yes here to build support for Analog Devices AD7170, AD7171,
+ AD7780 and AD7781 SPI analog to digital converters (ADC).
+
+ To compile this driver as a module, choose M here: the
+ module will be called ad7780.
+
config AD7791
tristate "Analog Devices AD7791 ADC driver"
depends on SPI
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index ea5031348052..b48852157115 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -15,6 +15,7 @@ obj-$(CONFIG_AD7606_IFACE_PARALLEL) += ad7606_par.o
obj-$(CONFIG_AD7606_IFACE_SPI) += ad7606_spi.o
obj-$(CONFIG_AD7606) += ad7606.o
obj-$(CONFIG_AD7766) += ad7766.o
+obj-$(CONFIG_AD7780) += ad7780.o
obj-$(CONFIG_AD7791) += ad7791.o
obj-$(CONFIG_AD7793) += ad7793.o
obj-$(CONFIG_AD7887) += ad7887.o
diff --git a/drivers/iio/adc/ad7780.c b/drivers/iio/adc/ad7780.c
new file mode 100644
index 000000000000..23b731a92e32
--- /dev/null
+++ b/drivers/iio/adc/ad7780.c
@@ -0,0 +1,376 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
+ *
+ * Copyright 2011 Analog Devices Inc.
+ * Copyright 2019 Renato Lui Geh
+ */
+
+#include <linux/interrupt.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/sysfs.h>
+#include <linux/spi/spi.h>
+#include <linux/regulator/consumer.h>
+#include <linux/err.h>
+#include <linux/sched.h>
+#include <linux/gpio/consumer.h>
+#include <linux/module.h>
+#include <linux/bits.h>
+
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+#include <linux/iio/adc/ad_sigma_delta.h>
+
+#define AD7780_RDY BIT(7)
+#define AD7780_FILTER BIT(6)
+#define AD7780_ERR BIT(5)
+#define AD7780_ID1 BIT(4)
+#define AD7780_ID0 BIT(3)
+#define AD7780_GAIN BIT(2)
+
+#define AD7170_ID 0
+#define AD7171_ID 1
+#define AD7780_ID 1
+#define AD7781_ID 0
+
+#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
+
+#define AD7780_PATTERN_GOOD 1
+#define AD7780_PATTERN_MASK GENMASK(1, 0)
+
+#define AD7170_PATTERN_GOOD 5
+#define AD7170_PATTERN_MASK GENMASK(2, 0)
+
+#define AD7780_GAIN_MIDPOINT 64
+#define AD7780_FILTER_MIDPOINT 13350
+
+static const unsigned int ad778x_gain[2] = { 1, 128 };
+static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
+
+struct ad7780_chip_info {
+ struct iio_chan_spec channel;
+ unsigned int pattern_mask;
+ unsigned int pattern;
+ bool is_ad778x;
+};
+
+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;
+ unsigned int odr;
+ unsigned int int_vref_mv;
+
+ struct ad_sigma_delta sd;
+};
+
+enum ad7780_supported_device_ids {
+ ID_AD7170,
+ ID_AD7171,
+ ID_AD7780,
+ ID_AD7781,
+};
+
+static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
+{
+ return container_of(sd, struct ad7780_state, sd);
+}
+
+static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
+ enum ad_sigma_delta_mode mode)
+{
+ struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
+ unsigned int val;
+
+ switch (mode) {
+ case AD_SD_MODE_SINGLE:
+ case AD_SD_MODE_CONTINUOUS:
+ val = 1;
+ break;
+ default:
+ val = 0;
+ break;
+ }
+
+ gpiod_set_value(st->powerdown_gpio, val);
+
+ return 0;
+}
+
+static int ad7780_read_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);
+ int voltage_uv;
+
+ switch (m) {
+ case IIO_CHAN_INFO_RAW:
+ return ad_sigma_delta_single_conversion(indio_dev, chan, val);
+ case IIO_CHAN_INFO_SCALE:
+ voltage_uv = regulator_get_voltage(st->reg);
+ if (voltage_uv < 0)
+ return voltage_uv;
+ voltage_uv /= 1000;
+ *val = voltage_uv * st->gain;
+ *val2 = chan->scan_type.realbits - 1;
+ st->int_vref_mv = voltage_uv;
+ return IIO_VAL_FRACTIONAL_LOG2;
+ case IIO_CHAN_INFO_OFFSET:
+ *val = -(1 << (chan->scan_type.realbits - 1));
+ return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ *val = st->odr;
+ return IIO_VAL_INT;
+ default:
+ break;
+ }
+
+ 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;
+ unsigned long long vref;
+ unsigned int full_scale, gain;
+
+ if (!chip_info->is_ad778x)
+ return -EINVAL;
+
+ switch (m) {
+ case IIO_CHAN_INFO_SCALE:
+ if (val != 0)
+ return -EINVAL;
+
+ vref = st->int_vref_mv * 1000000LL;
+ full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
+ gain = DIV_ROUND_CLOSEST(vref, full_scale);
+ gain = DIV_ROUND_CLOSEST(gain, val2);
+ st->gain = gain;
+ if (gain < AD7780_GAIN_MIDPOINT)
+ gain = 0;
+ else
+ gain = 1;
+ gpiod_set_value(st->gain_gpio, gain);
+ break;
+ case IIO_CHAN_INFO_SAMP_FREQ:
+ if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
+ val = 0;
+ else
+ val = 1;
+ st->odr = ad778x_odr_avail[val];
+ gpiod_set_value(st->filter_gpio, val);
+ break;
+ default:
+ 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;
+
+ if ((raw_sample & AD7780_ERR) ||
+ ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
+ return -EIO;
+
+ if (chip_info->is_ad778x) {
+ st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
+ st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
+ }
+
+ return 0;
+}
+
+static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
+ .set_mode = ad7780_set_mode,
+ .postprocess_sample = ad7780_postprocess_sample,
+ .has_registers = false,
+};
+
+#define AD7780_CHANNEL(bits, wordsize) \
+ AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
+#define AD7170_CHANNEL(bits, wordsize) \
+ AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
+
+static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
+ [ID_AD7170] = {
+ .channel = AD7170_CHANNEL(12, 24),
+ .pattern = AD7170_PATTERN_GOOD,
+ .pattern_mask = AD7170_PATTERN_MASK,
+ .is_ad778x = false,
+ },
+ [ID_AD7171] = {
+ .channel = AD7170_CHANNEL(16, 24),
+ .pattern = AD7170_PATTERN_GOOD,
+ .pattern_mask = AD7170_PATTERN_MASK,
+ .is_ad778x = false,
+ },
+ [ID_AD7780] = {
+ .channel = AD7780_CHANNEL(24, 32),
+ .pattern = AD7780_PATTERN_GOOD,
+ .pattern_mask = AD7780_PATTERN_MASK,
+ .is_ad778x = true,
+ },
+ [ID_AD7781] = {
+ .channel = AD7780_CHANNEL(20, 32),
+ .pattern = AD7780_PATTERN_GOOD,
+ .pattern_mask = AD7780_PATTERN_MASK,
+ .is_ad778x = true,
+ },
+};
+
+static const struct iio_info ad7780_info = {
+ .read_raw = ad7780_read_raw,
+ .write_raw = ad7780_write_raw,
+};
+
+static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
+{
+ int ret;
+
+ st->powerdown_gpio = devm_gpiod_get_optional(dev,
+ "powerdown",
+ GPIOD_OUT_LOW);
+ if (IS_ERR(st->powerdown_gpio)) {
+ ret = PTR_ERR(st->powerdown_gpio);
+ dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
+ return ret;
+ }
+
+ if (!st->chip_info->is_ad778x)
+ return 0;
+
+
+ st->gain_gpio = devm_gpiod_get_optional(dev,
+ "adi,gain",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(st->gain_gpio)) {
+ ret = PTR_ERR(st->gain_gpio);
+ dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
+ return ret;
+ }
+
+ st->filter_gpio = devm_gpiod_get_optional(dev,
+ "adi,filter",
+ GPIOD_OUT_HIGH);
+ if (IS_ERR(st->filter_gpio)) {
+ ret = PTR_ERR(st->filter_gpio);
+ dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int ad7780_probe(struct spi_device *spi)
+{
+ struct ad7780_state *st;
+ struct iio_dev *indio_dev;
+ int ret;
+
+ indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ st = iio_priv(indio_dev);
+ st->gain = 1;
+
+ ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
+
+ st->chip_info =
+ &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
+
+ spi_set_drvdata(spi, indio_dev);
+
+ indio_dev->dev.parent = &spi->dev;
+ indio_dev->name = spi_get_device_id(spi)->name;
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->channels = &st->chip_info->channel;
+ indio_dev->num_channels = 1;
+ indio_dev->info = &ad7780_info;
+
+ ret = ad7780_init_gpios(&spi->dev, st);
+ if (ret)
+ goto error_cleanup_buffer_and_trigger;
+
+ st->reg = devm_regulator_get(&spi->dev, "avdd");
+ if (IS_ERR(st->reg))
+ return PTR_ERR(st->reg);
+
+ ret = regulator_enable(st->reg);
+ if (ret) {
+ dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
+ return ret;
+ }
+
+ ret = ad_sd_setup_buffer_and_trigger(indio_dev);
+ if (ret)
+ goto error_disable_reg;
+
+ ret = iio_device_register(indio_dev);
+ if (ret)
+ goto error_cleanup_buffer_and_trigger;
+
+ return 0;
+
+error_cleanup_buffer_and_trigger:
+ ad_sd_cleanup_buffer_and_trigger(indio_dev);
+error_disable_reg:
+ regulator_disable(st->reg);
+
+ return ret;
+}
+
+static int ad7780_remove(struct spi_device *spi)
+{
+ struct iio_dev *indio_dev = spi_get_drvdata(spi);
+ struct ad7780_state *st = iio_priv(indio_dev);
+
+ iio_device_unregister(indio_dev);
+ ad_sd_cleanup_buffer_and_trigger(indio_dev);
+
+ regulator_disable(st->reg);
+
+ return 0;
+}
+
+static const struct spi_device_id ad7780_id[] = {
+ {"ad7170", ID_AD7170},
+ {"ad7171", ID_AD7171},
+ {"ad7780", ID_AD7780},
+ {"ad7781", ID_AD7781},
+ {}
+};
+MODULE_DEVICE_TABLE(spi, ad7780_id);
+
+static struct spi_driver ad7780_driver = {
+ .driver = {
+ .name = "ad7780",
+ },
+ .probe = ad7780_probe,
+ .remove = ad7780_remove,
+ .id_table = ad7780_id,
+};
+module_spi_driver(ad7780_driver);
+
+MODULE_AUTHOR("Michael Hennerich <[email protected]>");
+MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
+MODULE_LICENSE("GPL v2");
diff --git a/drivers/staging/iio/adc/Kconfig b/drivers/staging/iio/adc/Kconfig
index 7a93d3a5c113..404a53c743a6 100644
--- a/drivers/staging/iio/adc/Kconfig
+++ b/drivers/staging/iio/adc/Kconfig
@@ -3,19 +3,6 @@
#
menu "Analog to digital converters"

-config AD7780
- tristate "Analog Devices AD7780 and similar ADCs driver"
- depends on SPI
- depends on GPIOLIB || COMPILE_TEST
- select AD_SIGMA_DELTA
- help
- Say yes here to build support for Analog Devices AD7170, AD7171,
- AD7780 and AD7781 SPI analog to digital converters (ADC).
- If unsure, say N (but it's safe to say "Y").
-
- To compile this driver as a module, choose M here: the
- module will be called ad7780.
-
config AD7816
tristate "Analog Devices AD7816/7/8 temperature sensor and ADC driver"
depends on SPI
diff --git a/drivers/staging/iio/adc/Makefile b/drivers/staging/iio/adc/Makefile
index 7a421088ff82..4b76769b32bc 100644
--- a/drivers/staging/iio/adc/Makefile
+++ b/drivers/staging/iio/adc/Makefile
@@ -3,7 +3,6 @@
# Makefile for industrial I/O ADC drivers
#

-obj-$(CONFIG_AD7780) += ad7780.o
obj-$(CONFIG_AD7816) += ad7816.o
obj-$(CONFIG_AD7192) += ad7192.o
obj-$(CONFIG_AD7280) += ad7280a.o
diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
deleted file mode 100644
index 23b731a92e32..000000000000
--- a/drivers/staging/iio/adc/ad7780.c
+++ /dev/null
@@ -1,376 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
- *
- * Copyright 2011 Analog Devices Inc.
- * Copyright 2019 Renato Lui Geh
- */
-
-#include <linux/interrupt.h>
-#include <linux/device.h>
-#include <linux/kernel.h>
-#include <linux/slab.h>
-#include <linux/sysfs.h>
-#include <linux/spi/spi.h>
-#include <linux/regulator/consumer.h>
-#include <linux/err.h>
-#include <linux/sched.h>
-#include <linux/gpio/consumer.h>
-#include <linux/module.h>
-#include <linux/bits.h>
-
-#include <linux/iio/iio.h>
-#include <linux/iio/sysfs.h>
-#include <linux/iio/adc/ad_sigma_delta.h>
-
-#define AD7780_RDY BIT(7)
-#define AD7780_FILTER BIT(6)
-#define AD7780_ERR BIT(5)
-#define AD7780_ID1 BIT(4)
-#define AD7780_ID0 BIT(3)
-#define AD7780_GAIN BIT(2)
-
-#define AD7170_ID 0
-#define AD7171_ID 1
-#define AD7780_ID 1
-#define AD7781_ID 0
-
-#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
-
-#define AD7780_PATTERN_GOOD 1
-#define AD7780_PATTERN_MASK GENMASK(1, 0)
-
-#define AD7170_PATTERN_GOOD 5
-#define AD7170_PATTERN_MASK GENMASK(2, 0)
-
-#define AD7780_GAIN_MIDPOINT 64
-#define AD7780_FILTER_MIDPOINT 13350
-
-static const unsigned int ad778x_gain[2] = { 1, 128 };
-static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
-
-struct ad7780_chip_info {
- struct iio_chan_spec channel;
- unsigned int pattern_mask;
- unsigned int pattern;
- bool is_ad778x;
-};
-
-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;
- unsigned int odr;
- unsigned int int_vref_mv;
-
- struct ad_sigma_delta sd;
-};
-
-enum ad7780_supported_device_ids {
- ID_AD7170,
- ID_AD7171,
- ID_AD7780,
- ID_AD7781,
-};
-
-static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
-{
- return container_of(sd, struct ad7780_state, sd);
-}
-
-static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
- enum ad_sigma_delta_mode mode)
-{
- struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
- unsigned int val;
-
- switch (mode) {
- case AD_SD_MODE_SINGLE:
- case AD_SD_MODE_CONTINUOUS:
- val = 1;
- break;
- default:
- val = 0;
- break;
- }
-
- gpiod_set_value(st->powerdown_gpio, val);
-
- return 0;
-}
-
-static int ad7780_read_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);
- int voltage_uv;
-
- switch (m) {
- case IIO_CHAN_INFO_RAW:
- return ad_sigma_delta_single_conversion(indio_dev, chan, val);
- case IIO_CHAN_INFO_SCALE:
- voltage_uv = regulator_get_voltage(st->reg);
- if (voltage_uv < 0)
- return voltage_uv;
- voltage_uv /= 1000;
- *val = voltage_uv * st->gain;
- *val2 = chan->scan_type.realbits - 1;
- st->int_vref_mv = voltage_uv;
- return IIO_VAL_FRACTIONAL_LOG2;
- case IIO_CHAN_INFO_OFFSET:
- *val = -(1 << (chan->scan_type.realbits - 1));
- return IIO_VAL_INT;
- case IIO_CHAN_INFO_SAMP_FREQ:
- *val = st->odr;
- return IIO_VAL_INT;
- default:
- break;
- }
-
- 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;
- unsigned long long vref;
- unsigned int full_scale, gain;
-
- if (!chip_info->is_ad778x)
- return -EINVAL;
-
- switch (m) {
- case IIO_CHAN_INFO_SCALE:
- if (val != 0)
- return -EINVAL;
-
- vref = st->int_vref_mv * 1000000LL;
- full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
- gain = DIV_ROUND_CLOSEST(vref, full_scale);
- gain = DIV_ROUND_CLOSEST(gain, val2);
- st->gain = gain;
- if (gain < AD7780_GAIN_MIDPOINT)
- gain = 0;
- else
- gain = 1;
- gpiod_set_value(st->gain_gpio, gain);
- break;
- case IIO_CHAN_INFO_SAMP_FREQ:
- if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
- val = 0;
- else
- val = 1;
- st->odr = ad778x_odr_avail[val];
- gpiod_set_value(st->filter_gpio, val);
- break;
- default:
- 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;
-
- if ((raw_sample & AD7780_ERR) ||
- ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
- return -EIO;
-
- if (chip_info->is_ad778x) {
- st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
- st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
- }
-
- return 0;
-}
-
-static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
- .set_mode = ad7780_set_mode,
- .postprocess_sample = ad7780_postprocess_sample,
- .has_registers = false,
-};
-
-#define AD7780_CHANNEL(bits, wordsize) \
- AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
-#define AD7170_CHANNEL(bits, wordsize) \
- AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
-
-static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
- [ID_AD7170] = {
- .channel = AD7170_CHANNEL(12, 24),
- .pattern = AD7170_PATTERN_GOOD,
- .pattern_mask = AD7170_PATTERN_MASK,
- .is_ad778x = false,
- },
- [ID_AD7171] = {
- .channel = AD7170_CHANNEL(16, 24),
- .pattern = AD7170_PATTERN_GOOD,
- .pattern_mask = AD7170_PATTERN_MASK,
- .is_ad778x = false,
- },
- [ID_AD7780] = {
- .channel = AD7780_CHANNEL(24, 32),
- .pattern = AD7780_PATTERN_GOOD,
- .pattern_mask = AD7780_PATTERN_MASK,
- .is_ad778x = true,
- },
- [ID_AD7781] = {
- .channel = AD7780_CHANNEL(20, 32),
- .pattern = AD7780_PATTERN_GOOD,
- .pattern_mask = AD7780_PATTERN_MASK,
- .is_ad778x = true,
- },
-};
-
-static const struct iio_info ad7780_info = {
- .read_raw = ad7780_read_raw,
- .write_raw = ad7780_write_raw,
-};
-
-static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
-{
- int ret;
-
- st->powerdown_gpio = devm_gpiod_get_optional(dev,
- "powerdown",
- GPIOD_OUT_LOW);
- if (IS_ERR(st->powerdown_gpio)) {
- ret = PTR_ERR(st->powerdown_gpio);
- dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
- return ret;
- }
-
- if (!st->chip_info->is_ad778x)
- return 0;
-
-
- st->gain_gpio = devm_gpiod_get_optional(dev,
- "adi,gain",
- GPIOD_OUT_HIGH);
- if (IS_ERR(st->gain_gpio)) {
- ret = PTR_ERR(st->gain_gpio);
- dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
- return ret;
- }
-
- st->filter_gpio = devm_gpiod_get_optional(dev,
- "adi,filter",
- GPIOD_OUT_HIGH);
- if (IS_ERR(st->filter_gpio)) {
- ret = PTR_ERR(st->filter_gpio);
- dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
- return ret;
- }
-
- return 0;
-}
-
-static int ad7780_probe(struct spi_device *spi)
-{
- struct ad7780_state *st;
- struct iio_dev *indio_dev;
- int ret;
-
- indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
- if (!indio_dev)
- return -ENOMEM;
-
- st = iio_priv(indio_dev);
- st->gain = 1;
-
- ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
-
- st->chip_info =
- &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
-
- spi_set_drvdata(spi, indio_dev);
-
- indio_dev->dev.parent = &spi->dev;
- indio_dev->name = spi_get_device_id(spi)->name;
- indio_dev->modes = INDIO_DIRECT_MODE;
- indio_dev->channels = &st->chip_info->channel;
- indio_dev->num_channels = 1;
- indio_dev->info = &ad7780_info;
-
- ret = ad7780_init_gpios(&spi->dev, st);
- if (ret)
- goto error_cleanup_buffer_and_trigger;
-
- st->reg = devm_regulator_get(&spi->dev, "avdd");
- if (IS_ERR(st->reg))
- return PTR_ERR(st->reg);
-
- ret = regulator_enable(st->reg);
- if (ret) {
- dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
- return ret;
- }
-
- ret = ad_sd_setup_buffer_and_trigger(indio_dev);
- if (ret)
- goto error_disable_reg;
-
- ret = iio_device_register(indio_dev);
- if (ret)
- goto error_cleanup_buffer_and_trigger;
-
- return 0;
-
-error_cleanup_buffer_and_trigger:
- ad_sd_cleanup_buffer_and_trigger(indio_dev);
-error_disable_reg:
- regulator_disable(st->reg);
-
- return ret;
-}
-
-static int ad7780_remove(struct spi_device *spi)
-{
- struct iio_dev *indio_dev = spi_get_drvdata(spi);
- struct ad7780_state *st = iio_priv(indio_dev);
-
- iio_device_unregister(indio_dev);
- ad_sd_cleanup_buffer_and_trigger(indio_dev);
-
- regulator_disable(st->reg);
-
- return 0;
-}
-
-static const struct spi_device_id ad7780_id[] = {
- {"ad7170", ID_AD7170},
- {"ad7171", ID_AD7171},
- {"ad7780", ID_AD7780},
- {"ad7781", ID_AD7781},
- {}
-};
-MODULE_DEVICE_TABLE(spi, ad7780_id);
-
-static struct spi_driver ad7780_driver = {
- .driver = {
- .name = "ad7780",
- },
- .probe = ad7780_probe,
- .remove = ad7780_remove,
- .id_table = ad7780_id,
-};
-module_spi_driver(ad7780_driver);
-
-MODULE_AUTHOR("Michael Hennerich <[email protected]>");
-MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
-MODULE_LICENSE("GPL v2");
--
2.21.0


2019-03-16 02:18:54

by Renato Lui Geh

[permalink] [raw]
Subject: [PATCH v5 11/11] staging: iio: ad7780: add device tree binding

Adds a device tree binding for the ad7780 driver.

Signed-off-by: Renato Lui Geh <[email protected]>
---
.../bindings/iio/adc/adi,ad7780.txt | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt

diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
new file mode 100644
index 000000000000..440e52555349
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
@@ -0,0 +1,48 @@
+* Analog Devices AD7170/AD7171/AD7780/AD7781
+
+Data sheets:
+
+- AD7170:
+ * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7170.pdf
+- AD7171:
+ * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7171.pdf
+- AD7780:
+ * https://www.analog.com/media/en/technical-documentation/data-sheets/ad7780.pdf
+- AD7781:
+ * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7781.pdf
+
+Required properties:
+
+- compatible: should be one of
+ * "adi,ad7170"
+ * "adi,ad7171"
+ * "adi,ad7780"
+ * "adi,ad7781"
+- reg: spi chip select number for the device
+- vref-supply: the regulator supply for the ADC reference voltage
+
+Optional properties:
+
+- powerdown-gpios: must be the device tree identifier of the PDRST pin. If
+ specified, it will be asserted during driver probe. As the
+ line is active high, it should be marked GPIO_ACTIVE_HIGH.
+- adi,gain-gpios: must be the device tree identifier of the GAIN pin. Only for
+ the ad778x chips. If specified, it will be asserted during
+ driver probe. As the line is active low, it should be marked
+ GPIO_ACTIVE_LOW.
+- adi,filter-gpios: must be the device tree identifier of the FILTER pin. Only
+ for the ad778x chips. If specified, it will be asserted
+ during driver probe. As the line is active low, it should be
+ marked GPIO_ACTIVE_LOW.
+
+Example:
+
+adc@0 {
+ compatible = "adi,ad7780";
+ reg = <0>;
+ vref-supply = <&vdd_supply>
+
+ powerdown-gpios = <&gpio 12 GPIO_ACTIVE_HIGH>;
+ adi,gain-gpios = <&gpio 5 GPIO_ACTIVE_LOW>;
+ adi,filter-gpios = <&gpio 15 GPIO_ACTIVE_LOW>;
+};
--
2.21.0


2019-03-16 17:50:28

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 02/11] staging: iio: ad7780: add missing switch default case

On Fri, 15 Mar 2019 23:12:53 -0300
Renato Lui Geh <[email protected]> wrote:

> This patch simply adds a missing switch default case in read_raw.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied,

Thanks,

Jonathan

> ---
> drivers/staging/iio/adc/ad7780.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index 07e5e35c92a3..f4cd7bc3e02f 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -118,6 +118,8 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
> case IIO_CHAN_INFO_OFFSET:
> *val = -(1 << (chan->scan_type.realbits - 1));
> return IIO_VAL_INT;
> + default:
> + break;
> }
>
> return -EINVAL;


2019-03-16 17:50:28

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 01/11] staging: iio: ad7780: add gain & filter gpio support

On Fri, 15 Mar 2019 23:12:27 -0300
Renato Lui Geh <[email protected]> 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: Renato Lui Geh <[email protected]>
> Signed-off-by: Giuliano Belinassi <[email protected]>
> Co-developed-by: Giuliano Belinassi <[email protected]>
Something a little odd happened when trying to use git am on this,
but patch was happy so I did it that way. Please give it a quick
sanity check once I push the tree out incase we have a real problem.

Applied to the togreg branch of iio.git to be shortly pushed out
as testing for the autobuilders to play with it.

Thanks,

Jonathan

> ---
> Changes in v3:
> - Renamed ad7780_chip_info's filter to odr
> - Renamed ad778x_filter to ad778x_odr_avail
> - Changed vref variable from unsigned int to unsigned long long to avoid
> overflow
> - Removed unnecessary AD_SD_CHANNEL macro
> Changes in v4:
> - Removed useless macro
> - Added default case for switch to suppress warning
> - Removed chunks belonging to filter reading, adding these as a
> patch for itself
> Changes in v5:
> - ad778x_odr_avail moved to filter patch
> - Split gain reading to its own patch
> - Init GPIOs in a separate function
>
> drivers/staging/iio/adc/ad7780.c | 114 ++++++++++++++++++++++++++-----
> 1 file changed, 96 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index c4a85789c2db..07e5e35c92a3 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -39,6 +39,9 @@
> #define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
> #define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 | AD7170_PAT2)
>
> +#define AD7780_GAIN_MIDPOINT 64
> +#define AD7780_FILTER_MIDPOINT 13350
> +
> struct ad7780_chip_info {
> struct iio_chan_spec channel;
> unsigned int pattern_mask;
> @@ -50,7 +53,10 @@ struct ad7780_state {
> const struct ad7780_chip_info *chip_info;
> struct regulator *reg;
> struct gpio_desc *powerdown_gpio;
> - unsigned int gain;
> + struct gpio_desc *gain_gpio;
> + struct gpio_desc *filter_gpio;
> + unsigned int gain;
> + unsigned int int_vref_mv;
>
> struct ad_sigma_delta sd;
> };
> @@ -104,8 +110,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
> voltage_uv = regulator_get_voltage(st->reg);
> if (voltage_uv < 0)
> return voltage_uv;
> - *val = (voltage_uv / 1000) * st->gain;
> + voltage_uv /= 1000;
> + *val = voltage_uv * st->gain;
> *val2 = chan->scan_type.realbits - 1;
> + st->int_vref_mv = voltage_uv;
> return IIO_VAL_FRACTIONAL_LOG2;
> case IIO_CHAN_INFO_OFFSET:
> *val = -(1 << (chan->scan_type.realbits - 1));
> @@ -115,6 +123,50 @@ 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;
> + unsigned long long vref;
> + unsigned int full_scale, gain;
> +
> + if (!chip_info->is_ad778x)
> + return -EINVAL;
> +
> + switch (m) {
> + case IIO_CHAN_INFO_SCALE:
> + if (val != 0)
> + return -EINVAL;
> +
> + vref = st->int_vref_mv * 1000000LL;
> + full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
> + gain = DIV_ROUND_CLOSEST(vref, full_scale);
> + gain = DIV_ROUND_CLOSEST(gain, val2);
> + st->gain = gain;
> + if (gain < AD7780_GAIN_MIDPOINT)
> + gain = 0;
> + else
> + gain = 1;
> + gpiod_set_value(st->gain_gpio, gain);
> + break;
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
> + val = 0;
> + else
> + val = 1;
> + gpiod_set_value(st->filter_gpio, val);
> + break;
> + default:
> + break;
> + }
> +
> + return 0;
> +}
> +
> static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> unsigned int raw_sample)
> {
> @@ -125,13 +177,6 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
> return -EIO;
>
> - if (chip_info->is_ad778x) {
> - if (raw_sample & AD7780_GAIN)
> - st->gain = 1;
> - else
> - st->gain = 128;
> - }
> -
> return 0;
> }
>
> @@ -173,8 +218,47 @@ 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_init_gpios(struct device *dev, struct ad7780_state *st)
> +{
> + int ret;
> +
> + st->powerdown_gpio = devm_gpiod_get_optional(dev,
> + "powerdown",
> + GPIOD_OUT_LOW);
> + if (IS_ERR(st->powerdown_gpio)) {
> + ret = PTR_ERR(st->powerdown_gpio);
> + dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + if (!st->chip_info->is_ad778x)
> + return 0;
> +
nitpick: One blank line is always enough. More just reduces the amount of
code that can be seen at a time.

> +
> + st->gain_gpio = devm_gpiod_get_optional(dev,
> + "adi,gain",
> + GPIOD_OUT_HIGH);
> + if (IS_ERR(st->gain_gpio)) {
> + ret = PTR_ERR(st->gain_gpio);
> + dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + st->filter_gpio = devm_gpiod_get_optional(dev,
> + "adi,filter",
> + GPIOD_OUT_HIGH);
> + if (IS_ERR(st->filter_gpio)) {
> + ret = PTR_ERR(st->filter_gpio);
> + dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> static int ad7780_probe(struct spi_device *spi)
> {
> struct ad7780_state *st;
> @@ -212,15 +296,9 @@ static int ad7780_probe(struct spi_device *spi)
> indio_dev->num_channels = 1;
> indio_dev->info = &ad7780_info;
>
> - st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
> - "powerdown",
> - GPIOD_OUT_LOW);
> - if (IS_ERR(st->powerdown_gpio)) {
> - ret = PTR_ERR(st->powerdown_gpio);
> - dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
> - ret);
> - goto error_disable_reg;
> - }
> + ret = ad7780_init_gpios(&spi->dev, st);
> + if (ret)
> + goto error_cleanup_buffer_and_trigger;
>
> ret = ad_sd_setup_buffer_and_trigger(indio_dev);
> if (ret)


2019-03-16 17:55:21

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 03/11] staging: iio: ad7780: add gain reading to ad778x

On Fri, 15 Mar 2019 23:13:13 -0300
Renato Lui Geh <[email protected]> wrote:

> This patch adds a new functionality of reading gain values from the
> ad778x chips. This value is stored in the chip's state struct and is
> updated whenever a read or write call is performed on the driver.
>
> Signed-off-by: Renato Lui Geh <[email protected]>

Applied, thanks

> ---
> drivers/staging/iio/adc/ad7780.c | 5 +++++
> 1 file changed, 5 insertions(+)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index f4cd7bc3e02f..fbcc0d3345ca 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -42,6 +42,8 @@
> #define AD7780_GAIN_MIDPOINT 64
> #define AD7780_FILTER_MIDPOINT 13350
>
> +static const unsigned int ad778x_gain[2] = { 1, 128 };
> +
> struct ad7780_chip_info {
> struct iio_chan_spec channel;
> unsigned int pattern_mask;
> @@ -179,6 +181,9 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
> return -EIO;
>
> + if (chip_info->is_ad778x)
> + st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
> +
> return 0;
> }
>


2019-03-16 17:56:48

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 04/11] staging: iio: ad7780: add filter reading to ad778x

On Fri, 15 Mar 2019 23:13:42 -0300
Renato Lui Geh <[email protected]> wrote:

> This patch adds the new feature of reading the filter odr value for
> ad778x chips. This value is stored in the chip's state struct whenever a
> read or write call is performed on the chip's driver.
>
> This feature requires sharing SAMP_FREQ. Since the ad717x does not have
> a filter option, the driver only shares the relevant info mask for the
> ad778x family.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied,

Thanks,

Jonathan

> ---
> Changes in v4:
> - As mentioned in the previous patch, this was originally as part of
> the 'add gain & filter gpio support' patch
> Changes in v5:
> - Moved ad778x_odr_avail declaration from GPIO patch to this one
>
> drivers/staging/iio/adc/ad7780.c | 18 ++++++++++++++----
> 1 file changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index fbcc0d3345ca..8ff74427d975 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -42,7 +42,8 @@
> #define AD7780_GAIN_MIDPOINT 64
> #define AD7780_FILTER_MIDPOINT 13350
>
> -static const unsigned int ad778x_gain[2] = { 1, 128 };
> +static const unsigned int ad778x_gain[2] = { 1, 128 };
> +static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
>
> struct ad7780_chip_info {
> struct iio_chan_spec channel;
> @@ -58,6 +59,7 @@ struct ad7780_state {
> struct gpio_desc *gain_gpio;
> struct gpio_desc *filter_gpio;
> unsigned int gain;
> + unsigned int odr;
> unsigned int int_vref_mv;
>
> struct ad_sigma_delta sd;
> @@ -120,6 +122,9 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
> case IIO_CHAN_INFO_OFFSET:
> *val = -(1 << (chan->scan_type.realbits - 1));
> return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + *val = st->odr;
> + return IIO_VAL_INT;
> default:
> break;
> }
> @@ -162,6 +167,7 @@ static int ad7780_write_raw(struct iio_dev *indio_dev,
> val = 0;
> else
> val = 1;
> + st->odr = ad778x_odr_avail[val];
> gpiod_set_value(st->filter_gpio, val);
> break;
> default:
> @@ -181,8 +187,10 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
> return -EIO;
>
> - if (chip_info->is_ad778x)
> + if (chip_info->is_ad778x) {
> st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
> + st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
> + }
>
> return 0;
> }
> @@ -194,17 +202,19 @@ static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
> };
>
> #define AD7780_CHANNEL(bits, wordsize) \
> + AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
> +#define AD7170_CHANNEL(bits, wordsize) \
> AD_SD_CHANNEL_NO_SAMP_FREQ(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,


2019-03-16 17:58:11

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 05/11] staging: iio: ad7780: set pattern values and masks directly

On Fri, 15 Mar 2019 23:14:14 -0300
Renato Lui Geh <[email protected]> wrote:

> The AD7780 driver contains status pattern bits designed for checking
> whether serial transfers have been correctly performed. Pattern macros
> were previously generated through bit fields. This patch sets good
> pattern values directly and masks through GENMASK.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied,

Thanks,

Jonathan

> ---
> drivers/staging/iio/adc/ad7780.c | 20 +++++++++-----------
> 1 file changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index 8ff74427d975..94cb60c327d0 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -17,6 +17,7 @@
> #include <linux/sched.h>
> #include <linux/gpio/consumer.h>
> #include <linux/module.h>
> +#include <linux/bits.h>
>
> #include <linux/iio/iio.h>
> #include <linux/iio/sysfs.h>
> @@ -28,16 +29,13 @@
> #define AD7780_ID1 BIT(4)
> #define AD7780_ID0 BIT(3)
> #define AD7780_GAIN BIT(2)
> -#define AD7780_PAT1 BIT(1)
> -#define AD7780_PAT0 BIT(0)
>
> -#define AD7780_PATTERN (AD7780_PAT0)
> -#define AD7780_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1)
>
> -#define AD7170_PAT2 BIT(2)
> +#define AD7780_PATTERN_GOOD 1
> +#define AD7780_PATTERN_MASK GENMASK(1, 0)
>
> -#define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
> -#define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 | AD7170_PAT2)
> +#define AD7170_PATTERN_GOOD 5
> +#define AD7170_PATTERN_MASK GENMASK(2, 0)
>
> #define AD7780_GAIN_MIDPOINT 64
> #define AD7780_FILTER_MIDPOINT 13350
> @@ -209,25 +207,25 @@ static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
> static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
> [ID_AD7170] = {
> .channel = AD7170_CHANNEL(12, 24),
> - .pattern = AD7170_PATTERN,
> + .pattern = AD7170_PATTERN_GOOD,
> .pattern_mask = AD7170_PATTERN_MASK,
> .is_ad778x = false,
> },
> [ID_AD7171] = {
> .channel = AD7170_CHANNEL(16, 24),
> - .pattern = AD7170_PATTERN,
> + .pattern = AD7170_PATTERN_GOOD,
> .pattern_mask = AD7170_PATTERN_MASK,
> .is_ad778x = false,
> },
> [ID_AD7780] = {
> .channel = AD7780_CHANNEL(24, 32),
> - .pattern = AD7780_PATTERN,
> + .pattern = AD7780_PATTERN_GOOD,
> .pattern_mask = AD7780_PATTERN_MASK,
> .is_ad778x = true,
> },
> [ID_AD7781] = {
> .channel = AD7780_CHANNEL(20, 32),
> - .pattern = AD7780_PATTERN,
> + .pattern = AD7780_PATTERN_GOOD,
> .pattern_mask = AD7780_PATTERN_MASK,
> .is_ad778x = true,
> },


2019-03-16 18:03:01

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 06/11] staging:iio:ad7780: add chip ID values and mask

On Fri, 15 Mar 2019 23:14:27 -0300
Renato Lui Geh <[email protected]> wrote:

> The ad7780 supports both the ad778x and ad717x families. Each chip has
> a corresponding ID. This patch provides a mask for extracting ID values
> from the status bits and also macros for the correct values for the
> ad7170, ad7171, ad7780 and ad7781.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
I'll admit I find it hard to summon an enthusiasm for this patch, but
it does no harm and I don't want to delay the following ones.

Hence applied anyway.

Jonathan

> ---
> Changes in v5:
> - Put AD7780_ID{0,1} back
>
> drivers/staging/iio/adc/ad7780.c | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index 94cb60c327d0..977b381c1260 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -30,6 +30,12 @@
> #define AD7780_ID0 BIT(3)
> #define AD7780_GAIN BIT(2)
>
> +#define AD7170_ID 0
> +#define AD7171_ID 1
> +#define AD7780_ID 1
> +#define AD7781_ID 0
> +
> +#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
>
> #define AD7780_PATTERN_GOOD 1
> #define AD7780_PATTERN_MASK GENMASK(1, 0)


2019-03-16 18:05:03

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 07/11] staging: iio: ad7780: move regulator to after GPIO init

On Fri, 15 Mar 2019 23:14:59 -0300
Renato Lui Geh <[email protected]> wrote:

> To maintain consistency between ad7780_probe and ad7780_remove orders,
> regulator initialization has been moved to after GPIO initializations.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied,

Thanks,

Jonathan

> ---
> drivers/staging/iio/adc/ad7780.c | 20 ++++++++++----------
> 1 file changed, 10 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index 977b381c1260..568c5b4472ff 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -295,16 +295,6 @@ static int ad7780_probe(struct spi_device *spi)
>
> ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
>
> - st->reg = devm_regulator_get(&spi->dev, "avdd");
> - if (IS_ERR(st->reg))
> - return PTR_ERR(st->reg);
> -
> - ret = regulator_enable(st->reg);
> - if (ret) {
> - dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
> - return ret;
> - }
> -
> st->chip_info =
> &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
>
> @@ -321,6 +311,16 @@ static int ad7780_probe(struct spi_device *spi)
> if (ret)
> goto error_cleanup_buffer_and_trigger;
>
> + st->reg = devm_regulator_get(&spi->dev, "avdd");
> + if (IS_ERR(st->reg))
> + return PTR_ERR(st->reg);
> +
> + ret = regulator_enable(st->reg);
> + if (ret) {
> + dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
> + return ret;
> + }
> +
> ret = ad_sd_setup_buffer_and_trigger(indio_dev);
> if (ret)
> goto error_disable_reg;


2019-03-16 18:06:09

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 09/11] staging: iio: ad7780: add new copyright holder

On Fri, 15 Mar 2019 23:15:26 -0300
Renato Lui Geh <[email protected]> wrote:

> This patch adds a new copyright holder to the ad7780 driver.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied thanks

Jonathan

> ---
> drivers/staging/iio/adc/ad7780.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index ff61ffa0da9f..23b731a92e32 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -3,6 +3,7 @@
> * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
> *
> * Copyright 2011 Analog Devices Inc.
> + * Copyright 2019 Renato Lui Geh
> */
>
> #include <linux/interrupt.h>


2019-03-16 18:06:09

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 08/11] staging: iio: ad7780: add SPDX identifier

On Fri, 15 Mar 2019 23:15:13 -0300
Renato Lui Geh <[email protected]> wrote:

> Add SPDX identifier (GPL-2.0) to the AD7780 driver.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Applied

> ---
> drivers/staging/iio/adc/ad7780.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> index 568c5b4472ff..ff61ffa0da9f 100644
> --- a/drivers/staging/iio/adc/ad7780.c
> +++ b/drivers/staging/iio/adc/ad7780.c
> @@ -1,9 +1,8 @@
> +// SPDX-License-Identifier: GPL-2.0
> /*
> * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
> *
> * Copyright 2011 Analog Devices Inc.
> - *
> - * Licensed under the GPL-2.
> */
>
> #include <linux/interrupt.h>


2019-03-16 18:20:45

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 10/11] staging: iio: ad7780: moving ad7780 out of staging

On Fri, 15 Mar 2019 23:15:55 -0300
Renato Lui Geh <[email protected]> wrote:

> Move ad7780 ADC driver out of staging and into the mainline.
>
> The ad7780 is a sigma-delta analog to digital converter. This driver provides
> reading voltage values and status bits from both the ad778x and ad717x series.
> Its interface also allows writing on the FILTER and GAIN GPIO pins on the
> ad778x.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
> Signed-off-by: Giuliano Belinassi <[email protected]>
> Co-developed-by: Giuliano Belinassi <[email protected]>

Applied to the togreg branch of iio.git and pushed out as testing.
Note I won't be pushing that out as non rebasing (togreg) until at
least next weekend, so there is a bit of time for any last minute
feedback etc.

Thanks for all your hard work on this and great to see it graduate from
staging!

Thanks,

Jonathan

> ---
> Changes in v3:
> - Changes unrelated to moving the driver to main tree were resent as
> individual patches
> Changes in v4:
> - Removed line warning it was safe to build the ad7780 driver by
> default
>
> drivers/iio/adc/Kconfig | 12 +
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/ad7780.c | 376 +++++++++++++++++++++++++++++++
> drivers/staging/iio/adc/Kconfig | 13 --
> drivers/staging/iio/adc/Makefile | 1 -
> drivers/staging/iio/adc/ad7780.c | 376 -------------------------------
> 6 files changed, 389 insertions(+), 390 deletions(-)
> create mode 100644 drivers/iio/adc/ad7780.c
> delete mode 100644 drivers/staging/iio/adc/ad7780.c
>
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index f3cc7a31bce5..6b36a45bd09f 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -108,6 +108,18 @@ config AD7766
> To compile this driver as a module, choose M here: the module will be
> called ad7766.
>
> +config AD7780
> + tristate "Analog Devices AD7780 and similar ADCs driver"
> + depends on SPI
> + depends on GPIOLIB || COMPILE_TEST
> + select AD_SIGMA_DELTA
> + help
> + Say yes here to build support for Analog Devices AD7170, AD7171,
> + AD7780 and AD7781 SPI analog to digital converters (ADC).
> +
> + To compile this driver as a module, choose M here: the
> + module will be called ad7780.
> +
> config AD7791
> tristate "Analog Devices AD7791 ADC driver"
> depends on SPI
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index ea5031348052..b48852157115 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -15,6 +15,7 @@ obj-$(CONFIG_AD7606_IFACE_PARALLEL) += ad7606_par.o
> obj-$(CONFIG_AD7606_IFACE_SPI) += ad7606_spi.o
> obj-$(CONFIG_AD7606) += ad7606.o
> obj-$(CONFIG_AD7766) += ad7766.o
> +obj-$(CONFIG_AD7780) += ad7780.o
> obj-$(CONFIG_AD7791) += ad7791.o
> obj-$(CONFIG_AD7793) += ad7793.o
> obj-$(CONFIG_AD7887) += ad7887.o
> diff --git a/drivers/iio/adc/ad7780.c b/drivers/iio/adc/ad7780.c
> new file mode 100644
> index 000000000000..23b731a92e32
> --- /dev/null
> +++ b/drivers/iio/adc/ad7780.c
> @@ -0,0 +1,376 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
> + *
> + * Copyright 2011 Analog Devices Inc.
> + * Copyright 2019 Renato Lui Geh
> + */
> +
> +#include <linux/interrupt.h>
> +#include <linux/device.h>
> +#include <linux/kernel.h>
> +#include <linux/slab.h>
> +#include <linux/sysfs.h>
> +#include <linux/spi/spi.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/err.h>
> +#include <linux/sched.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/module.h>
> +#include <linux/bits.h>
> +
> +#include <linux/iio/iio.h>
> +#include <linux/iio/sysfs.h>
> +#include <linux/iio/adc/ad_sigma_delta.h>
> +
> +#define AD7780_RDY BIT(7)
> +#define AD7780_FILTER BIT(6)
> +#define AD7780_ERR BIT(5)
> +#define AD7780_ID1 BIT(4)
> +#define AD7780_ID0 BIT(3)
> +#define AD7780_GAIN BIT(2)
> +
> +#define AD7170_ID 0
> +#define AD7171_ID 1
> +#define AD7780_ID 1
> +#define AD7781_ID 0
> +
> +#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
> +
> +#define AD7780_PATTERN_GOOD 1
> +#define AD7780_PATTERN_MASK GENMASK(1, 0)
> +
> +#define AD7170_PATTERN_GOOD 5
> +#define AD7170_PATTERN_MASK GENMASK(2, 0)
> +
> +#define AD7780_GAIN_MIDPOINT 64
> +#define AD7780_FILTER_MIDPOINT 13350
> +
> +static const unsigned int ad778x_gain[2] = { 1, 128 };
> +static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
> +
> +struct ad7780_chip_info {
> + struct iio_chan_spec channel;
> + unsigned int pattern_mask;
> + unsigned int pattern;
> + bool is_ad778x;
> +};
> +
> +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;
> + unsigned int odr;
> + unsigned int int_vref_mv;
> +
> + struct ad_sigma_delta sd;
> +};
> +
> +enum ad7780_supported_device_ids {
> + ID_AD7170,
> + ID_AD7171,
> + ID_AD7780,
> + ID_AD7781,
> +};
> +
> +static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
> +{
> + return container_of(sd, struct ad7780_state, sd);
> +}
> +
> +static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
> + enum ad_sigma_delta_mode mode)
> +{
> + struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
> + unsigned int val;
> +
> + switch (mode) {
> + case AD_SD_MODE_SINGLE:
> + case AD_SD_MODE_CONTINUOUS:
> + val = 1;
> + break;
> + default:
> + val = 0;
> + break;
> + }
> +
> + gpiod_set_value(st->powerdown_gpio, val);
> +
> + return 0;
> +}
> +
> +static int ad7780_read_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);
> + int voltage_uv;
> +
> + switch (m) {
> + case IIO_CHAN_INFO_RAW:
> + return ad_sigma_delta_single_conversion(indio_dev, chan, val);
> + case IIO_CHAN_INFO_SCALE:
> + voltage_uv = regulator_get_voltage(st->reg);
> + if (voltage_uv < 0)
> + return voltage_uv;
> + voltage_uv /= 1000;
> + *val = voltage_uv * st->gain;
> + *val2 = chan->scan_type.realbits - 1;
> + st->int_vref_mv = voltage_uv;
> + return IIO_VAL_FRACTIONAL_LOG2;
> + case IIO_CHAN_INFO_OFFSET:
> + *val = -(1 << (chan->scan_type.realbits - 1));
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + *val = st->odr;
> + return IIO_VAL_INT;
> + default:
> + break;
> + }
> +
> + 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;
> + unsigned long long vref;
> + unsigned int full_scale, gain;
> +
> + if (!chip_info->is_ad778x)
> + return -EINVAL;
> +
> + switch (m) {
> + case IIO_CHAN_INFO_SCALE:
> + if (val != 0)
> + return -EINVAL;
> +
> + vref = st->int_vref_mv * 1000000LL;
> + full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
> + gain = DIV_ROUND_CLOSEST(vref, full_scale);
> + gain = DIV_ROUND_CLOSEST(gain, val2);
> + st->gain = gain;
> + if (gain < AD7780_GAIN_MIDPOINT)
> + gain = 0;
> + else
> + gain = 1;
> + gpiod_set_value(st->gain_gpio, gain);
> + break;
> + case IIO_CHAN_INFO_SAMP_FREQ:
> + if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
> + val = 0;
> + else
> + val = 1;
> + st->odr = ad778x_odr_avail[val];
> + gpiod_set_value(st->filter_gpio, val);
> + break;
> + default:
> + 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;
> +
> + if ((raw_sample & AD7780_ERR) ||
> + ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
> + return -EIO;
> +
> + if (chip_info->is_ad778x) {
> + st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
> + st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
> + }
> +
> + return 0;
> +}
> +
> +static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
> + .set_mode = ad7780_set_mode,
> + .postprocess_sample = ad7780_postprocess_sample,
> + .has_registers = false,
> +};
> +
> +#define AD7780_CHANNEL(bits, wordsize) \
> + AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
> +#define AD7170_CHANNEL(bits, wordsize) \
> + AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
> +
> +static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
> + [ID_AD7170] = {
> + .channel = AD7170_CHANNEL(12, 24),
> + .pattern = AD7170_PATTERN_GOOD,
> + .pattern_mask = AD7170_PATTERN_MASK,
> + .is_ad778x = false,
> + },
> + [ID_AD7171] = {
> + .channel = AD7170_CHANNEL(16, 24),
> + .pattern = AD7170_PATTERN_GOOD,
> + .pattern_mask = AD7170_PATTERN_MASK,
> + .is_ad778x = false,
> + },
> + [ID_AD7780] = {
> + .channel = AD7780_CHANNEL(24, 32),
> + .pattern = AD7780_PATTERN_GOOD,
> + .pattern_mask = AD7780_PATTERN_MASK,
> + .is_ad778x = true,
> + },
> + [ID_AD7781] = {
> + .channel = AD7780_CHANNEL(20, 32),
> + .pattern = AD7780_PATTERN_GOOD,
> + .pattern_mask = AD7780_PATTERN_MASK,
> + .is_ad778x = true,
> + },
> +};
> +
> +static const struct iio_info ad7780_info = {
> + .read_raw = ad7780_read_raw,
> + .write_raw = ad7780_write_raw,
> +};
> +
> +static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
> +{
> + int ret;
> +
> + st->powerdown_gpio = devm_gpiod_get_optional(dev,
> + "powerdown",
> + GPIOD_OUT_LOW);
> + if (IS_ERR(st->powerdown_gpio)) {
> + ret = PTR_ERR(st->powerdown_gpio);
> + dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + if (!st->chip_info->is_ad778x)
> + return 0;
> +
> +
> + st->gain_gpio = devm_gpiod_get_optional(dev,
> + "adi,gain",
> + GPIOD_OUT_HIGH);
> + if (IS_ERR(st->gain_gpio)) {
> + ret = PTR_ERR(st->gain_gpio);
> + dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + st->filter_gpio = devm_gpiod_get_optional(dev,
> + "adi,filter",
> + GPIOD_OUT_HIGH);
> + if (IS_ERR(st->filter_gpio)) {
> + ret = PTR_ERR(st->filter_gpio);
> + dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
> + return ret;
> + }
> +
> + return 0;
> +}
> +
> +static int ad7780_probe(struct spi_device *spi)
> +{
> + struct ad7780_state *st;
> + struct iio_dev *indio_dev;
> + int ret;
> +
> + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + st = iio_priv(indio_dev);
> + st->gain = 1;
> +
> + ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
> +
> + st->chip_info =
> + &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
> +
> + spi_set_drvdata(spi, indio_dev);
> +
> + indio_dev->dev.parent = &spi->dev;
> + indio_dev->name = spi_get_device_id(spi)->name;
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + indio_dev->channels = &st->chip_info->channel;
> + indio_dev->num_channels = 1;
> + indio_dev->info = &ad7780_info;
> +
> + ret = ad7780_init_gpios(&spi->dev, st);
> + if (ret)
> + goto error_cleanup_buffer_and_trigger;
> +
> + st->reg = devm_regulator_get(&spi->dev, "avdd");
> + if (IS_ERR(st->reg))
> + return PTR_ERR(st->reg);
> +
> + ret = regulator_enable(st->reg);
> + if (ret) {
> + dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
> + return ret;
> + }
> +
> + ret = ad_sd_setup_buffer_and_trigger(indio_dev);
> + if (ret)
> + goto error_disable_reg;
> +
> + ret = iio_device_register(indio_dev);
> + if (ret)
> + goto error_cleanup_buffer_and_trigger;
> +
> + return 0;
> +
> +error_cleanup_buffer_and_trigger:
> + ad_sd_cleanup_buffer_and_trigger(indio_dev);
> +error_disable_reg:
> + regulator_disable(st->reg);
> +
> + return ret;
> +}
> +
> +static int ad7780_remove(struct spi_device *spi)
> +{
> + struct iio_dev *indio_dev = spi_get_drvdata(spi);
> + struct ad7780_state *st = iio_priv(indio_dev);
> +
> + iio_device_unregister(indio_dev);
> + ad_sd_cleanup_buffer_and_trigger(indio_dev);
> +
> + regulator_disable(st->reg);
> +
> + return 0;
> +}
> +
> +static const struct spi_device_id ad7780_id[] = {
> + {"ad7170", ID_AD7170},
> + {"ad7171", ID_AD7171},
> + {"ad7780", ID_AD7780},
> + {"ad7781", ID_AD7781},
> + {}
> +};
> +MODULE_DEVICE_TABLE(spi, ad7780_id);
> +
> +static struct spi_driver ad7780_driver = {
> + .driver = {
> + .name = "ad7780",
> + },
> + .probe = ad7780_probe,
> + .remove = ad7780_remove,
> + .id_table = ad7780_id,
> +};
> +module_spi_driver(ad7780_driver);
> +
> +MODULE_AUTHOR("Michael Hennerich <[email protected]>");
> +MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
> +MODULE_LICENSE("GPL v2");



2019-03-16 18:22:24

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 11/11] staging: iio: ad7780: add device tree binding

On Fri, 15 Mar 2019 23:16:13 -0300
Renato Lui Geh <[email protected]> wrote:

> Adds a device tree binding for the ad7780 driver.
>
> Signed-off-by: Renato Lui Geh <[email protected]>
Seems like a straight forward binding so applied to the togreg branch
of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,
Jonathan

> ---
> .../bindings/iio/adc/adi,ad7780.txt | 48 +++++++++++++++++++
> 1 file changed, 48 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
>
> diff --git a/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
> new file mode 100644
> index 000000000000..440e52555349
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/iio/adc/adi,ad7780.txt
> @@ -0,0 +1,48 @@
> +* Analog Devices AD7170/AD7171/AD7780/AD7781
> +
> +Data sheets:
> +
> +- AD7170:
> + * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7170.pdf
> +- AD7171:
> + * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7171.pdf
> +- AD7780:
> + * https://www.analog.com/media/en/technical-documentation/data-sheets/ad7780.pdf
> +- AD7781:
> + * https://www.analog.com/media/en/technical-documentation/data-sheets/AD7781.pdf
> +
> +Required properties:
> +
> +- compatible: should be one of
> + * "adi,ad7170"
> + * "adi,ad7171"
> + * "adi,ad7780"
> + * "adi,ad7781"
> +- reg: spi chip select number for the device
> +- vref-supply: the regulator supply for the ADC reference voltage
> +
> +Optional properties:
> +
> +- powerdown-gpios: must be the device tree identifier of the PDRST pin. If
> + specified, it will be asserted during driver probe. As the
> + line is active high, it should be marked GPIO_ACTIVE_HIGH.
> +- adi,gain-gpios: must be the device tree identifier of the GAIN pin. Only for
> + the ad778x chips. If specified, it will be asserted during
> + driver probe. As the line is active low, it should be marked
> + GPIO_ACTIVE_LOW.
> +- adi,filter-gpios: must be the device tree identifier of the FILTER pin. Only
> + for the ad778x chips. If specified, it will be asserted
> + during driver probe. As the line is active low, it should be
> + marked GPIO_ACTIVE_LOW.
> +
> +Example:
> +
> +adc@0 {
> + compatible = "adi,ad7780";
> + reg = <0>;
> + vref-supply = <&vdd_supply>
> +
> + powerdown-gpios = <&gpio 12 GPIO_ACTIVE_HIGH>;
> + adi,gain-gpios = <&gpio 5 GPIO_ACTIVE_LOW>;
> + adi,filter-gpios = <&gpio 15 GPIO_ACTIVE_LOW>;
> +};


2019-03-16 18:45:30

by Renato Lui Geh

[permalink] [raw]
Subject: Re: [PATCH v5 10/11] staging: iio: ad7780: moving ad7780 out of staging

On 03/16, Jonathan Cameron wrote:
>On Fri, 15 Mar 2019 23:15:55 -0300
>Renato Lui Geh <[email protected]> wrote:
>
>> Move ad7780 ADC driver out of staging and into the mainline.
>>
>> The ad7780 is a sigma-delta analog to digital converter. This driver provides
>> reading voltage values and status bits from both the ad778x and ad717x series.
>> Its interface also allows writing on the FILTER and GAIN GPIO pins on the
>> ad778x.
>>
>> Signed-off-by: Renato Lui Geh <[email protected]>
>> Signed-off-by: Giuliano Belinassi <[email protected]>
>> Co-developed-by: Giuliano Belinassi <[email protected]>
>
>Applied to the togreg branch of iio.git and pushed out as testing.
>Note I won't be pushing that out as non rebasing (togreg) until at
>least next weekend, so there is a bit of time for any last minute
>feedback etc.
>
>Thanks for all your hard work on this and great to see it graduate from
>staging!

Huge thanks to both you and Alexandru for all the great feedback and
help on this! Hope to continue to send many more contributions here. :)
>
>Thanks,
>
>Jonathan
>
>> ---
>> Changes in v3:
>> - Changes unrelated to moving the driver to main tree were resent as
>> individual patches
>> Changes in v4:
>> - Removed line warning it was safe to build the ad7780 driver by
>> default
>>
>> drivers/iio/adc/Kconfig | 12 +
>> drivers/iio/adc/Makefile | 1 +
>> drivers/iio/adc/ad7780.c | 376 +++++++++++++++++++++++++++++++
>> drivers/staging/iio/adc/Kconfig | 13 --
>> drivers/staging/iio/adc/Makefile | 1 -
>> drivers/staging/iio/adc/ad7780.c | 376 -------------------------------
>> 6 files changed, 389 insertions(+), 390 deletions(-)
>> create mode 100644 drivers/iio/adc/ad7780.c
>> delete mode 100644 drivers/staging/iio/adc/ad7780.c
>>
>> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
>> index f3cc7a31bce5..6b36a45bd09f 100644
>> --- a/drivers/iio/adc/Kconfig
>> +++ b/drivers/iio/adc/Kconfig
>> @@ -108,6 +108,18 @@ config AD7766
>> To compile this driver as a module, choose M here: the module will be
>> called ad7766.
>>
>> +config AD7780
>> + tristate "Analog Devices AD7780 and similar ADCs driver"
>> + depends on SPI
>> + depends on GPIOLIB || COMPILE_TEST
>> + select AD_SIGMA_DELTA
>> + help
>> + Say yes here to build support for Analog Devices AD7170, AD7171,
>> + AD7780 and AD7781 SPI analog to digital converters (ADC).
>> +
>> + To compile this driver as a module, choose M here: the
>> + module will be called ad7780.
>> +
>> config AD7791
>> tristate "Analog Devices AD7791 ADC driver"
>> depends on SPI
>> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
>> index ea5031348052..b48852157115 100644
>> --- a/drivers/iio/adc/Makefile
>> +++ b/drivers/iio/adc/Makefile
>> @@ -15,6 +15,7 @@ obj-$(CONFIG_AD7606_IFACE_PARALLEL) += ad7606_par.o
>> obj-$(CONFIG_AD7606_IFACE_SPI) += ad7606_spi.o
>> obj-$(CONFIG_AD7606) += ad7606.o
>> obj-$(CONFIG_AD7766) += ad7766.o
>> +obj-$(CONFIG_AD7780) += ad7780.o
>> obj-$(CONFIG_AD7791) += ad7791.o
>> obj-$(CONFIG_AD7793) += ad7793.o
>> obj-$(CONFIG_AD7887) += ad7887.o
>> diff --git a/drivers/iio/adc/ad7780.c b/drivers/iio/adc/ad7780.c
>> new file mode 100644
>> index 000000000000..23b731a92e32
>> --- /dev/null
>> +++ b/drivers/iio/adc/ad7780.c
>> @@ -0,0 +1,376 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * AD7170/AD7171 and AD7780/AD7781 SPI ADC driver
>> + *
>> + * Copyright 2011 Analog Devices Inc.
>> + * Copyright 2019 Renato Lui Geh
>> + */
>> +
>> +#include <linux/interrupt.h>
>> +#include <linux/device.h>
>> +#include <linux/kernel.h>
>> +#include <linux/slab.h>
>> +#include <linux/sysfs.h>
>> +#include <linux/spi/spi.h>
>> +#include <linux/regulator/consumer.h>
>> +#include <linux/err.h>
>> +#include <linux/sched.h>
>> +#include <linux/gpio/consumer.h>
>> +#include <linux/module.h>
>> +#include <linux/bits.h>
>> +
>> +#include <linux/iio/iio.h>
>> +#include <linux/iio/sysfs.h>
>> +#include <linux/iio/adc/ad_sigma_delta.h>
>> +
>> +#define AD7780_RDY BIT(7)
>> +#define AD7780_FILTER BIT(6)
>> +#define AD7780_ERR BIT(5)
>> +#define AD7780_ID1 BIT(4)
>> +#define AD7780_ID0 BIT(3)
>> +#define AD7780_GAIN BIT(2)
>> +
>> +#define AD7170_ID 0
>> +#define AD7171_ID 1
>> +#define AD7780_ID 1
>> +#define AD7781_ID 0
>> +
>> +#define AD7780_ID_MASK (AD7780_ID0 | AD7780_ID1)
>> +
>> +#define AD7780_PATTERN_GOOD 1
>> +#define AD7780_PATTERN_MASK GENMASK(1, 0)
>> +
>> +#define AD7170_PATTERN_GOOD 5
>> +#define AD7170_PATTERN_MASK GENMASK(2, 0)
>> +
>> +#define AD7780_GAIN_MIDPOINT 64
>> +#define AD7780_FILTER_MIDPOINT 13350
>> +
>> +static const unsigned int ad778x_gain[2] = { 1, 128 };
>> +static const unsigned int ad778x_odr_avail[2] = { 10000, 16700 };
>> +
>> +struct ad7780_chip_info {
>> + struct iio_chan_spec channel;
>> + unsigned int pattern_mask;
>> + unsigned int pattern;
>> + bool is_ad778x;
>> +};
>> +
>> +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;
>> + unsigned int odr;
>> + unsigned int int_vref_mv;
>> +
>> + struct ad_sigma_delta sd;
>> +};
>> +
>> +enum ad7780_supported_device_ids {
>> + ID_AD7170,
>> + ID_AD7171,
>> + ID_AD7780,
>> + ID_AD7781,
>> +};
>> +
>> +static struct ad7780_state *ad_sigma_delta_to_ad7780(struct ad_sigma_delta *sd)
>> +{
>> + return container_of(sd, struct ad7780_state, sd);
>> +}
>> +
>> +static int ad7780_set_mode(struct ad_sigma_delta *sigma_delta,
>> + enum ad_sigma_delta_mode mode)
>> +{
>> + struct ad7780_state *st = ad_sigma_delta_to_ad7780(sigma_delta);
>> + unsigned int val;
>> +
>> + switch (mode) {
>> + case AD_SD_MODE_SINGLE:
>> + case AD_SD_MODE_CONTINUOUS:
>> + val = 1;
>> + break;
>> + default:
>> + val = 0;
>> + break;
>> + }
>> +
>> + gpiod_set_value(st->powerdown_gpio, val);
>> +
>> + return 0;
>> +}
>> +
>> +static int ad7780_read_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);
>> + int voltage_uv;
>> +
>> + switch (m) {
>> + case IIO_CHAN_INFO_RAW:
>> + return ad_sigma_delta_single_conversion(indio_dev, chan, val);
>> + case IIO_CHAN_INFO_SCALE:
>> + voltage_uv = regulator_get_voltage(st->reg);
>> + if (voltage_uv < 0)
>> + return voltage_uv;
>> + voltage_uv /= 1000;
>> + *val = voltage_uv * st->gain;
>> + *val2 = chan->scan_type.realbits - 1;
>> + st->int_vref_mv = voltage_uv;
>> + return IIO_VAL_FRACTIONAL_LOG2;
>> + case IIO_CHAN_INFO_OFFSET:
>> + *val = -(1 << (chan->scan_type.realbits - 1));
>> + return IIO_VAL_INT;
>> + case IIO_CHAN_INFO_SAMP_FREQ:
>> + *val = st->odr;
>> + return IIO_VAL_INT;
>> + default:
>> + break;
>> + }
>> +
>> + 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;
>> + unsigned long long vref;
>> + unsigned int full_scale, gain;
>> +
>> + if (!chip_info->is_ad778x)
>> + return -EINVAL;
>> +
>> + switch (m) {
>> + case IIO_CHAN_INFO_SCALE:
>> + if (val != 0)
>> + return -EINVAL;
>> +
>> + vref = st->int_vref_mv * 1000000LL;
>> + full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
>> + gain = DIV_ROUND_CLOSEST(vref, full_scale);
>> + gain = DIV_ROUND_CLOSEST(gain, val2);
>> + st->gain = gain;
>> + if (gain < AD7780_GAIN_MIDPOINT)
>> + gain = 0;
>> + else
>> + gain = 1;
>> + gpiod_set_value(st->gain_gpio, gain);
>> + break;
>> + case IIO_CHAN_INFO_SAMP_FREQ:
>> + if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
>> + val = 0;
>> + else
>> + val = 1;
>> + st->odr = ad778x_odr_avail[val];
>> + gpiod_set_value(st->filter_gpio, val);
>> + break;
>> + default:
>> + 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;
>> +
>> + if ((raw_sample & AD7780_ERR) ||
>> + ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
>> + return -EIO;
>> +
>> + if (chip_info->is_ad778x) {
>> + st->gain = ad778x_gain[raw_sample & AD7780_GAIN];
>> + st->odr = ad778x_odr_avail[raw_sample & AD7780_FILTER];
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static const struct ad_sigma_delta_info ad7780_sigma_delta_info = {
>> + .set_mode = ad7780_set_mode,
>> + .postprocess_sample = ad7780_postprocess_sample,
>> + .has_registers = false,
>> +};
>> +
>> +#define AD7780_CHANNEL(bits, wordsize) \
>> + AD_SD_CHANNEL(1, 0, 0, bits, 32, wordsize - bits)
>> +#define AD7170_CHANNEL(bits, wordsize) \
>> + AD_SD_CHANNEL_NO_SAMP_FREQ(1, 0, 0, bits, 32, wordsize - bits)
>> +
>> +static const struct ad7780_chip_info ad7780_chip_info_tbl[] = {
>> + [ID_AD7170] = {
>> + .channel = AD7170_CHANNEL(12, 24),
>> + .pattern = AD7170_PATTERN_GOOD,
>> + .pattern_mask = AD7170_PATTERN_MASK,
>> + .is_ad778x = false,
>> + },
>> + [ID_AD7171] = {
>> + .channel = AD7170_CHANNEL(16, 24),
>> + .pattern = AD7170_PATTERN_GOOD,
>> + .pattern_mask = AD7170_PATTERN_MASK,
>> + .is_ad778x = false,
>> + },
>> + [ID_AD7780] = {
>> + .channel = AD7780_CHANNEL(24, 32),
>> + .pattern = AD7780_PATTERN_GOOD,
>> + .pattern_mask = AD7780_PATTERN_MASK,
>> + .is_ad778x = true,
>> + },
>> + [ID_AD7781] = {
>> + .channel = AD7780_CHANNEL(20, 32),
>> + .pattern = AD7780_PATTERN_GOOD,
>> + .pattern_mask = AD7780_PATTERN_MASK,
>> + .is_ad778x = true,
>> + },
>> +};
>> +
>> +static const struct iio_info ad7780_info = {
>> + .read_raw = ad7780_read_raw,
>> + .write_raw = ad7780_write_raw,
>> +};
>> +
>> +static int ad7780_init_gpios(struct device *dev, struct ad7780_state *st)
>> +{
>> + int ret;
>> +
>> + st->powerdown_gpio = devm_gpiod_get_optional(dev,
>> + "powerdown",
>> + GPIOD_OUT_LOW);
>> + if (IS_ERR(st->powerdown_gpio)) {
>> + ret = PTR_ERR(st->powerdown_gpio);
>> + dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
>> + return ret;
>> + }
>> +
>> + if (!st->chip_info->is_ad778x)
>> + return 0;
>> +
>> +
>> + st->gain_gpio = devm_gpiod_get_optional(dev,
>> + "adi,gain",
>> + GPIOD_OUT_HIGH);
>> + if (IS_ERR(st->gain_gpio)) {
>> + ret = PTR_ERR(st->gain_gpio);
>> + dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
>> + return ret;
>> + }
>> +
>> + st->filter_gpio = devm_gpiod_get_optional(dev,
>> + "adi,filter",
>> + GPIOD_OUT_HIGH);
>> + if (IS_ERR(st->filter_gpio)) {
>> + ret = PTR_ERR(st->filter_gpio);
>> + dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
>> + return ret;
>> + }
>> +
>> + return 0;
>> +}
>> +
>> +static int ad7780_probe(struct spi_device *spi)
>> +{
>> + struct ad7780_state *st;
>> + struct iio_dev *indio_dev;
>> + int ret;
>> +
>> + indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
>> + if (!indio_dev)
>> + return -ENOMEM;
>> +
>> + st = iio_priv(indio_dev);
>> + st->gain = 1;
>> +
>> + ad_sd_init(&st->sd, indio_dev, spi, &ad7780_sigma_delta_info);
>> +
>> + st->chip_info =
>> + &ad7780_chip_info_tbl[spi_get_device_id(spi)->driver_data];
>> +
>> + spi_set_drvdata(spi, indio_dev);
>> +
>> + indio_dev->dev.parent = &spi->dev;
>> + indio_dev->name = spi_get_device_id(spi)->name;
>> + indio_dev->modes = INDIO_DIRECT_MODE;
>> + indio_dev->channels = &st->chip_info->channel;
>> + indio_dev->num_channels = 1;
>> + indio_dev->info = &ad7780_info;
>> +
>> + ret = ad7780_init_gpios(&spi->dev, st);
>> + if (ret)
>> + goto error_cleanup_buffer_and_trigger;
>> +
>> + st->reg = devm_regulator_get(&spi->dev, "avdd");
>> + if (IS_ERR(st->reg))
>> + return PTR_ERR(st->reg);
>> +
>> + ret = regulator_enable(st->reg);
>> + if (ret) {
>> + dev_err(&spi->dev, "Failed to enable specified AVdd supply\n");
>> + return ret;
>> + }
>> +
>> + ret = ad_sd_setup_buffer_and_trigger(indio_dev);
>> + if (ret)
>> + goto error_disable_reg;
>> +
>> + ret = iio_device_register(indio_dev);
>> + if (ret)
>> + goto error_cleanup_buffer_and_trigger;
>> +
>> + return 0;
>> +
>> +error_cleanup_buffer_and_trigger:
>> + ad_sd_cleanup_buffer_and_trigger(indio_dev);
>> +error_disable_reg:
>> + regulator_disable(st->reg);
>> +
>> + return ret;
>> +}
>> +
>> +static int ad7780_remove(struct spi_device *spi)
>> +{
>> + struct iio_dev *indio_dev = spi_get_drvdata(spi);
>> + struct ad7780_state *st = iio_priv(indio_dev);
>> +
>> + iio_device_unregister(indio_dev);
>> + ad_sd_cleanup_buffer_and_trigger(indio_dev);
>> +
>> + regulator_disable(st->reg);
>> +
>> + return 0;
>> +}
>> +
>> +static const struct spi_device_id ad7780_id[] = {
>> + {"ad7170", ID_AD7170},
>> + {"ad7171", ID_AD7171},
>> + {"ad7780", ID_AD7780},
>> + {"ad7781", ID_AD7781},
>> + {}
>> +};
>> +MODULE_DEVICE_TABLE(spi, ad7780_id);
>> +
>> +static struct spi_driver ad7780_driver = {
>> + .driver = {
>> + .name = "ad7780",
>> + },
>> + .probe = ad7780_probe,
>> + .remove = ad7780_remove,
>> + .id_table = ad7780_id,
>> +};
>> +module_spi_driver(ad7780_driver);
>> +
>> +MODULE_AUTHOR("Michael Hennerich <[email protected]>");
>> +MODULE_DESCRIPTION("Analog Devices AD7780 and similar ADCs");
>> +MODULE_LICENSE("GPL v2");
>
>

2019-03-24 11:07:10

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH v5 01/11] staging: iio: ad7780: add gain & filter gpio support

On Sat, 16 Mar 2019 17:46:40 +0000
Jonathan Cameron <[email protected]> wrote:

> On Fri, 15 Mar 2019 23:12:27 -0300
> Renato Lui Geh <[email protected]> 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: Renato Lui Geh <[email protected]>
> > Signed-off-by: Giuliano Belinassi <[email protected]>
> > Co-developed-by: Giuliano Belinassi <[email protected]>
> Something a little odd happened when trying to use git am on this,
> but patch was happy so I did it that way. Please give it a quick
> sanity check once I push the tree out incase we have a real problem.
>
> Applied to the togreg branch of iio.git to be shortly pushed out
> as testing for the autobuilders to play with it.

Ah, we missed a 64 bit being divided below. Found by 0day.


>
> Thanks,
>
> Jonathan
>
> > ---
> > Changes in v3:
> > - Renamed ad7780_chip_info's filter to odr
> > - Renamed ad778x_filter to ad778x_odr_avail
> > - Changed vref variable from unsigned int to unsigned long long to avoid
> > overflow
> > - Removed unnecessary AD_SD_CHANNEL macro
> > Changes in v4:
> > - Removed useless macro
> > - Added default case for switch to suppress warning
> > - Removed chunks belonging to filter reading, adding these as a
> > patch for itself
> > Changes in v5:
> > - ad778x_odr_avail moved to filter patch
> > - Split gain reading to its own patch
> > - Init GPIOs in a separate function
> >
> > drivers/staging/iio/adc/ad7780.c | 114 ++++++++++++++++++++++++++-----
> > 1 file changed, 96 insertions(+), 18 deletions(-)
> >
> > diff --git a/drivers/staging/iio/adc/ad7780.c b/drivers/staging/iio/adc/ad7780.c
> > index c4a85789c2db..07e5e35c92a3 100644
> > --- a/drivers/staging/iio/adc/ad7780.c
> > +++ b/drivers/staging/iio/adc/ad7780.c
> > @@ -39,6 +39,9 @@
> > #define AD7170_PATTERN (AD7780_PAT0 | AD7170_PAT2)
> > #define AD7170_PATTERN_MASK (AD7780_PAT0 | AD7780_PAT1 | AD7170_PAT2)
> >
> > +#define AD7780_GAIN_MIDPOINT 64
> > +#define AD7780_FILTER_MIDPOINT 13350
> > +
> > struct ad7780_chip_info {
> > struct iio_chan_spec channel;
> > unsigned int pattern_mask;
> > @@ -50,7 +53,10 @@ struct ad7780_state {
> > const struct ad7780_chip_info *chip_info;
> > struct regulator *reg;
> > struct gpio_desc *powerdown_gpio;
> > - unsigned int gain;
> > + struct gpio_desc *gain_gpio;
> > + struct gpio_desc *filter_gpio;
> > + unsigned int gain;
> > + unsigned int int_vref_mv;
> >
> > struct ad_sigma_delta sd;
> > };
> > @@ -104,8 +110,10 @@ static int ad7780_read_raw(struct iio_dev *indio_dev,
> > voltage_uv = regulator_get_voltage(st->reg);
> > if (voltage_uv < 0)
> > return voltage_uv;
> > - *val = (voltage_uv / 1000) * st->gain;
> > + voltage_uv /= 1000;
> > + *val = voltage_uv * st->gain;
> > *val2 = chan->scan_type.realbits - 1;
> > + st->int_vref_mv = voltage_uv;
> > return IIO_VAL_FRACTIONAL_LOG2;
> > case IIO_CHAN_INFO_OFFSET:
> > *val = -(1 << (chan->scan_type.realbits - 1));
> > @@ -115,6 +123,50 @@ 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;
> > + unsigned long long vref;
> > + unsigned int full_scale, gain;
> > +
> > + if (!chip_info->is_ad778x)
> > + return -EINVAL;
> > +
> > + switch (m) {
> > + case IIO_CHAN_INFO_SCALE:
> > + if (val != 0)
> > + return -EINVAL;
> > +
> > + vref = st->int_vref_mv * 1000000LL;
> > + full_scale = 1 << (chip_info->channel.scan_type.realbits - 1);
> > + gain = DIV_ROUND_CLOSEST(vref, full_scale);

DIV_ROUND_CLOSEST_ULL needed as vref is long long.

I'll fix up.

Jonathan

> > + gain = DIV_ROUND_CLOSEST(gain, val2);
> > + st->gain = gain;
> > + if (gain < AD7780_GAIN_MIDPOINT)
> > + gain = 0;
> > + else
> > + gain = 1;
> > + gpiod_set_value(st->gain_gpio, gain);
> > + break;
> > + case IIO_CHAN_INFO_SAMP_FREQ:
> > + if (1000*val + val2/1000 < AD7780_FILTER_MIDPOINT)
> > + val = 0;
> > + else
> > + val = 1;
> > + gpiod_set_value(st->filter_gpio, val);
> > + break;
> > + default:
> > + break;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> > unsigned int raw_sample)
> > {
> > @@ -125,13 +177,6 @@ static int ad7780_postprocess_sample(struct ad_sigma_delta *sigma_delta,
> > ((raw_sample & chip_info->pattern_mask) != chip_info->pattern))
> > return -EIO;
> >
> > - if (chip_info->is_ad778x) {
> > - if (raw_sample & AD7780_GAIN)
> > - st->gain = 1;
> > - else
> > - st->gain = 128;
> > - }
> > -
> > return 0;
> > }
> >
> > @@ -173,8 +218,47 @@ 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_init_gpios(struct device *dev, struct ad7780_state *st)
> > +{
> > + int ret;
> > +
> > + st->powerdown_gpio = devm_gpiod_get_optional(dev,
> > + "powerdown",
> > + GPIOD_OUT_LOW);
> > + if (IS_ERR(st->powerdown_gpio)) {
> > + ret = PTR_ERR(st->powerdown_gpio);
> > + dev_err(dev, "Failed to request powerdown GPIO: %d\n", ret);
> > + return ret;
> > + }
> > +
> > + if (!st->chip_info->is_ad778x)
> > + return 0;
> > +
> nitpick: One blank line is always enough. More just reduces the amount of
> code that can be seen at a time.
>
> > +
> > + st->gain_gpio = devm_gpiod_get_optional(dev,
> > + "adi,gain",
> > + GPIOD_OUT_HIGH);
> > + if (IS_ERR(st->gain_gpio)) {
> > + ret = PTR_ERR(st->gain_gpio);
> > + dev_err(dev, "Failed to request gain GPIO: %d\n", ret);
> > + return ret;
> > + }
> > +
> > + st->filter_gpio = devm_gpiod_get_optional(dev,
> > + "adi,filter",
> > + GPIOD_OUT_HIGH);
> > + if (IS_ERR(st->filter_gpio)) {
> > + ret = PTR_ERR(st->filter_gpio);
> > + dev_err(dev, "Failed to request filter GPIO: %d\n", ret);
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > static int ad7780_probe(struct spi_device *spi)
> > {
> > struct ad7780_state *st;
> > @@ -212,15 +296,9 @@ static int ad7780_probe(struct spi_device *spi)
> > indio_dev->num_channels = 1;
> > indio_dev->info = &ad7780_info;
> >
> > - st->powerdown_gpio = devm_gpiod_get_optional(&spi->dev,
> > - "powerdown",
> > - GPIOD_OUT_LOW);
> > - if (IS_ERR(st->powerdown_gpio)) {
> > - ret = PTR_ERR(st->powerdown_gpio);
> > - dev_err(&spi->dev, "Failed to request powerdown GPIO: %d\n",
> > - ret);
> > - goto error_disable_reg;
> > - }
> > + ret = ad7780_init_gpios(&spi->dev, st);
> > + if (ret)
> > + goto error_cleanup_buffer_and_trigger;
> >
> > ret = ad_sd_setup_buffer_and_trigger(indio_dev);
> > if (ret)
>