Skip to contentSkip to conversation

Add support for AD7191

PATCH Superseded Alisa-Dariana Roman 3 participants 8 messages Feb 3, 2025 – Feb 8, 2025

Subsystems (5)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add support for AD7191

Thank you all for your feedback! Here is the updated series of patches!

Kind regards,
Alisa-Dariana Roman.

---

v3: https://lore.kernel.org/all/[email protected]/

v3 -> v4:
- addressed all replies for v3
- refactored the scale and sampling frequencies configurations to use 2
different arrays for gpio case vs pinstrap case

v2: https://lore.kernel.org/all/[email protected]/

v2 -> v3:
- correct binding title
- remove clksel_state and clksel_gpio, assume the clksel pin is always
pinstrapped
- rephrase clocks description accordingly
- simplify binding constraints
- specify in binding description that PDOWN must be connected to SPI's
controller's CS
- add minItems for gpios in bindings
- make scope explicit for mutex guard
- remove spi irq check
- add id_table to spi_driver struct
- changed comments as suggested
- use spi_message_init_with_transfers()
- default returns an error in ad7191_set_mode()
- replace hard-coded 2 with st->pga_gpios->ndescs
- use gpiod_set_array_value_cansleep()
- change .storagebits to 32
- check return value for ad_sd_init()
- change to adi,odr-value and adi,pga-value, which now accepts the value as
suggested
- modify variables names and refactor the setup of odr and pga gpios,
indexes and available arrays into ad7191_config_setup(), since they are all related
- add ad7191.rst

v1: https://lore.kernel.org/all/[email protected]/

v1 -> v2:
- removed patch adding function in ad_sigma_delta.h/.c
- added a function set_cs() for asserting/deasserting the cs
- handle pinstrapping cases
- refactored all clock handling
- updated bindings: corrected and added new things
- -> address of the channels is used in set_channel()
- addressed all the other changes

#1 lore raw

PATCH v4 1/3 dt-bindings: iio: adc: add AD7191

AD7191 is a pin-programmable, ultra-low noise 24-bit sigma-delta ADC designed for precision bridge sensor measurements. It features two differential analog input channels, selectable output rates, programmable gain, internal temperature sensor and simultaneous 50Hz/60Hz rejection.

---
 .../bindings/iio/adc/adi,ad7191.yaml          | 149 ++++++++++++++++++
 MAINTAINERS                                   |   7 +
 2 files changed, 156 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,ad7191.yaml
Documentation/devicetree/bindings/iio/adc/adi,ad7191.yaml new file +149 -0
new file mode 100644
index 000000000000..801ed319ee82
@@ -0,0 +1,149 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+# Copyright 2025 Analog Devices Inc.
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,ad7191.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices AD7191 ADC
+
+maintainers:
+  - Alisa-Dariana Roman <[email protected]>
+
+description: |
+  Bindings for the Analog Devices AD7191 ADC device. Datasheet can be
+  found here:
+  https://www.analog.com/media/en/technical-documentation/data-sheets/AD7191.pdf
+  The device's PDOWN pin must be connected to the SPI controller's chip select
+  pin.
+
+properties:
+  compatible:
+    enum:
+      - adi,ad7191
+
+  reg:
+    maxItems: 1
+
+  spi-cpol: true
+
+  spi-cpha: true
+
+  clocks:
+    maxItems: 1
+    description:
+      Must be present when CLKSEL pin is tied HIGH to select external clock
+      source (either a crystal between MCLK1 and MCLK2 pins, or a
+      CMOS-compatible clock driving MCLK2 pin). Must be absent when CLKSEL pin
+      is tied LOW to use the internal 4.92MHz clock.
+
+  interrupts:
+    maxItems: 1
+
+  avdd-supply:
+    description: AVdd voltage supply
+
+  dvdd-supply:
+    description: DVdd voltage supply
+
+  vref-supply:
+    description: Vref voltage supply
+
+  odr-gpios:
+    description:
+      ODR1 and ODR2 pins for output data rate selection. Should be defined if
+      adi,odr-value is absent.
+    minItems: 2
+    maxItems: 2
+
+  adi,odr-value:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Should be present if ODR pins are pin-strapped. Possible values:
+      120 Hz (ODR1=0, ODR2=0)
+      60 Hz (ODR1=0, ODR2=1)
+      50 Hz (ODR1=1, ODR2=0)
+      10 Hz (ODR1=1, ODR2=1)
+      If defined, odr-gpios must be absent.
+    enum: [120, 60, 50, 10]
+
+  pga-gpios:
+    description:
+      PGA1 and PGA2 pins for gain selection. Should be defined if adi,pga-value
+      is absent.
+    minItems: 2
+    maxItems: 2
+
+  adi,pga-value:
+    $ref: /schemas/types.yaml#/definitions/uint32
+    description: |
+      Should be present if PGA pins are pin-strapped. Possible values:
+      Gain 1 (PGA1=0, PGA2=0)
+      Gain 8 (PGA1=0, PGA2=1)
+      Gain 64 (PGA1=1, PGA2=0)
+      Gain 128 (PGA1=1, PGA2=1)
+      If defined, pga-gpios must be absent.
+    enum: [1, 8, 64, 128]
+
+  temp-gpios:
+    description: TEMP pin for temperature sensor enable.
+    maxItems: 1
+
+  chan-gpios:
+    description: CHAN pin for input channel selection.
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - avdd-supply
+  - dvdd-supply
+  - vref-supply
+  - spi-cpol
+  - spi-cpha
+  - temp-gpios
+  - chan-gpios
+
+allOf:
+  - $ref: /schemas/spi/spi-peripheral-props.yaml#
+  - oneOf:
+      - required:
+          - adi,odr-value
+      - required:
+          - odr-gpios
+  - oneOf:
+      - required:
+          - adi,pga-value
+      - required:
+          - pga-gpios
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@0 {
+            compatible = "adi,ad7191";
+            reg = <0>;
+            spi-max-frequency = <1000000>;
+            spi-cpol;
+            spi-cpha;
+            clocks = <&ad7191_mclk>;
+            interrupts = <25 IRQ_TYPE_EDGE_FALLING>;
+            interrupt-parent = <&gpio>;
+            avdd-supply = <&avdd>;
+            dvdd-supply = <&dvdd>;
+            vref-supply = <&vref>;
+            adi,pga-value = <1>;
+            odr-gpios = <&gpio 23 GPIO_ACTIVE_HIGH>, <&gpio 24 GPIO_ACTIVE_HIGH>;
+            temp-gpios = <&gpio 22 GPIO_ACTIVE_HIGH>;
+            chan-gpios = <&gpio 27 GPIO_ACTIVE_HIGH>;
+        };
+    };
MAINTAINERS +7 -0
index 98a3c1e46311..262beced3143 100644
@@ -1302,6 +1302,13 @@ W:	http://ez.analog.com/community/linux-device-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad7091r*
 F:	drivers/iio/adc/ad7091r*
 
+ANALOG DEVICES INC AD7191 DRIVER
+M:	Alisa-Dariana Roman <[email protected]>
+L:	[email protected]
+S:	Supported
+W:	https://ez.analog.com/linux-software-drivers
+F:	Documentation/devicetree/bindings/iio/adc/adi,ad7191.yaml
+
 ANALOG DEVICES INC AD7192 DRIVER
 M:	Alisa-Dariana Roman <[email protected]>
 L:	[email protected]
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/3 iio: adc: ad7191: add AD7191

In reply to Alisa-Dariana Roman

AD7191 is a pin-programmable, ultra-low noise 24-bit sigma-delta ADC designed for precision bridge sensor measurements. It features two differential analog input channels, selectable output rates, programmable gain, internal temperature sensor and simultaneous 50Hz/60Hz rejection.

---
 MAINTAINERS              |   1 +
 drivers/iio/adc/Kconfig  |  10 +
 drivers/iio/adc/Makefile |   1 +
 drivers/iio/adc/ad7191.c | 559 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 571 insertions(+)
 create mode 100644 drivers/iio/adc/ad7191.c
MAINTAINERS +1 -0
index 262beced3143..1340c27d9e5a 100644
@@ -1308,6 +1308,7 @@ L:	[email protected]
 S:	Supported
 W:	https://ez.analog.com/linux-software-drivers
 F:	Documentation/devicetree/bindings/iio/adc/adi,ad7191.yaml
+F:	drivers/iio/adc/ad7191.c
 
 ANALOG DEVICES INC AD7192 DRIVER
 M:	Alisa-Dariana Roman <[email protected]>
drivers/iio/adc/Kconfig +10 -0
index 849c90203071..70a662846aa2 100644
@@ -112,6 +112,16 @@ config AD7173
 	  To compile this driver as a module, choose M here: the module will be
 	  called ad7173.
 
+config AD7191
+	tristate "Analog Devices AD7191 ADC driver"
+	depends on SPI
+	select AD_SIGMA_DELTA
+	help
+	  Say yes here to build support for Analog Devices AD7191.
+
+	  To compile this driver as a module, choose M here: the
+	  module will be called ad7191.
+
 config AD7192
 	tristate "Analog Devices AD7192 and similar ADC driver"
 	depends on SPI
drivers/iio/adc/Makefile +1 -0
index ee19afba62b7..54335c613988 100644
@@ -14,6 +14,7 @@ obj-$(CONFIG_AD7091R5) += ad7091r5.o
 obj-$(CONFIG_AD7091R8) += ad7091r8.o
 obj-$(CONFIG_AD7124) += ad7124.o
 obj-$(CONFIG_AD7173) += ad7173.o
+obj-$(CONFIG_AD7191) += ad7191.o
 obj-$(CONFIG_AD7192) += ad7192.o
 obj-$(CONFIG_AD7266) += ad7266.o
 obj-$(CONFIG_AD7280) += ad7280a.o
drivers/iio/adc/ad7191.c new file +559 -0

559 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#3 lore raw

Re: [PATCH v4 2/3] iio: adc: ad7191: add AD7191

Quoted 8 lines · Alisa-Dariana Roman

AD7191 is a pin-programmable, ultra-low noise 24-bit sigma-delta ADC designed for precision bridge sensor measurements. It features two differential analog input channels, selectable output rates, programmable gain, internal temperature sensor and simultaneous 50Hz/60Hz rejection.

---

...

drivers/iio/adc/ad7191.c line 68

Quoted 13 lines
drivers/iio/adc/ad7191.c new file +13 -0
+struct ad7191_state {
+	struct ad_sigma_delta		sd;
+	struct mutex			lock; /* Protect device state */
+
+	struct gpio_descs		*odr_gpios;
+	struct gpio_descs		*pga_gpios;
+	struct gpio_desc		*temp_gpio;
+	struct gpio_desc		*chan_gpio;
+
+	u16				int_vref_mv;
+	u32				scale_avail_gpio[4][2];
+	u32				scale_avail_pinstrap[1][2];
+	const u32			(*scale_avail)[2];

This feels a bit reduant to have two arrays and then a pointer to one of those arrays. We could just have a single static const array of 4 and use that in both cases. (also see further comments later)

drivers/iio/adc/ad7191.c line 73

Quoted 5 lines
drivers/iio/adc/ad7191.c new file +5 -0
+	size_t				scale_avail_size;
+	u32				scale_index;
+	u32				samp_freq_avail_gpio[4];
+	u32				samp_freq_avail_pinstrap[1];
+	const u32			*samp_freq_avail;

ditto

Quoted 23 lines
drivers/iio/adc/ad7191.c new file +23 -0
+	size_t				samp_freq_avail_size;
+	u32				samp_freq_index;
+
+	struct clk			*mclk;
+};
+
+static int ad7191_set_channel(struct ad_sigma_delta *sd, unsigned int address)
+{
+	struct ad7191_state *st = ad_sigma_delta_to_ad7191(sd);
+	u8 temp_gpio_val, chan_gpio_val;
+
+	if (!FIELD_FIT(AD7191_CHAN_MASK | AD7191_TEMP_MASK, address))
+		return -EINVAL;
+
+	chan_gpio_val = FIELD_GET(AD7191_CHAN_MASK, address);
+	temp_gpio_val = FIELD_GET(AD7191_TEMP_MASK, address);
+
+	gpiod_set_value(st->chan_gpio, chan_gpio_val);
+	gpiod_set_value(st->temp_gpio, temp_gpio_val);
+
+	return 0;
+}
+

...

drivers/iio/adc/ad7191.c line 160

Quoted 7 lines
drivers/iio/adc/ad7191.c new file +7 -0
+
+static int ad7191_config_setup(struct iio_dev *indio_dev)
+{
+	struct ad7191_state *st = iio_priv(indio_dev);
+	struct device *dev = &st->sd.spi->dev;
+	/* Sampling frequencies in Hz, see Table 5 */
+	const int samp_freq[4] = { 120, 60, 50, 10 };

As per my earlier suggestion, we can make this static const...

drivers/iio/adc/ad7191.c line 162

Quoted 2 lines
drivers/iio/adc/ad7191.c new file +2 -0
+	/* Gain options, see Table 7 */
+	const int gain[4] = { 1, 8, 64, 128 };

ditto

drivers/iio/adc/ad7191.c line 169

Quoted 7 lines
drivers/iio/adc/ad7191.c new file +7 -0
+	int odr_value, odr_index, pga_value, pga_index, i, ret;
+	u64 scale_uv;
+
+	st->samp_freq_index = 0;
+	st->scale_index = 0;
+
+	ret = device_property_read_u32(dev, "adi,odr-value", &odr_value);

Shoud also check if (ret && ret != -EINVAL) first to catch other errors like someone put a string in the .dts instead of a u32.

drivers/iio/adc/ad7191.c line 180

Quoted 11 lines
drivers/iio/adc/ad7191.c new file +11 -0
+	if (ret == -EINVAL) {
+		st->odr_gpios = devm_gpiod_get_array(dev, "odr", GPIOD_OUT_LOW);
+		if (IS_ERR(st->odr_gpios))
+			return dev_err_probe(dev, PTR_ERR(st->odr_gpios),
+					     "Failed to get odr gpios.\n");
+
+		for (i = 0; i < ARRAY_SIZE(samp_freq); i++)
+			st->samp_freq_avail_gpio[i] = samp_freq[i];
+
+		st->samp_freq_avail = st->samp_freq_avail_gpio;
+		st->samp_freq_avail_size = ARRAY_SIZE(st->samp_freq_avail_gpio);

...then here instead of copying...

		st->samp_freq_avail = samp_freq;
		st->samp_freq_avail_size = ARRAY_SIZE(samp_freq);

drivers/iio/adc/ad7191.c line 185

Quoted 5 lines
drivers/iio/adc/ad7191.c new file +5 -0
+	} else {
+		for (i = 0; i < ARRAY_SIZE(samp_freq); i++) {
+			if (odr_value != samp_freq[i])
+				continue;
+			odr_index = i;

missing break;?

Also, should we error if match not found? Otherwise we could have uninitalized odr_index;

drivers/iio/adc/ad7191.c line 192

Quoted 7 lines
drivers/iio/adc/ad7191.c new file +7 -0
+		}
+
+		st->samp_freq_avail_pinstrap[0] = samp_freq[odr_index];
+
+		st->samp_freq_avail = st->samp_freq_avail_pinstrap;
+		st->samp_freq_avail_size = ARRAY_SIZE(st->samp_freq_avail_pinstrap);
+

and here...

		st->samp_freq_avail = &samp_freq[odr_index];
		st->samp_freq_avail_size = 1;

drivers/iio/adc/ad7191.c line 196

Quoted 4 lines
drivers/iio/adc/ad7191.c new file +4 -0
+		st->odr_gpios = NULL;
+	}
+
+	ret = device_property_read_u32(dev, "adi,pga-value", &pga_value);

ditto about error checking

Quoted 24 lines
drivers/iio/adc/ad7191.c new file +24 -0
+	if (ret == -EINVAL) {
+		st->pga_gpios = devm_gpiod_get_array(dev, "pga", GPIOD_OUT_LOW);
+		if (IS_ERR(st->pga_gpios))
+			return dev_err_probe(dev, PTR_ERR(st->pga_gpios),
+					     "Failed to get pga gpios.\n");
+
+		for (i = 0; i < ARRAY_SIZE(st->scale_avail_gpio); i++) {
+			scale_uv = ((u64)st->int_vref_mv * NANO) >>
+				(indio_dev->channels[0].scan_type.realbits - 1);
+			do_div(scale_uv, gain[i]);
+			st->scale_avail_gpio[i][1] = do_div(scale_uv, NANO);
+			st->scale_avail_gpio[i][0] = scale_uv;
+		}
+
+		st->scale_avail = st->scale_avail_gpio;
+		st->scale_avail_size = ARRAY_SIZE(st->scale_avail_gpio);
+	} else {
+		for (i = 0; i < ARRAY_SIZE(gain); i++) {
+			if (pga_value != gain[i])
+				continue;
+			pga_index = i;
+		}
+
+		scale_uv = ((u64)st->int_vref_mv * NANO) >>

drivers/iio/adc/ad7191.c line 230

Quoted 10 lines
drivers/iio/adc/ad7191.c new file +10 -0
+			(indio_dev->channels[0].scan_type.realbits - 1);
+		do_div(scale_uv, gain[pga_index]);
+		st->scale_avail_pinstrap[0][1] = do_div(scale_uv, NANO);
+		st->scale_avail_pinstrap[0][0] = scale_uv;
+
+		st->scale_avail = st->scale_avail_pinstrap;
+		st->scale_avail_size = ARRAY_SIZE(st->scale_avail_pinstrap);
+
+		st->pga_gpios = NULL;
+	}

and ditto about st->scale_avail and pinstrap matching for loop

Quoted 14 lines
drivers/iio/adc/ad7191.c new file +14 -0
+
+	st->temp_gpio = devm_gpiod_get(dev, "temp", GPIOD_OUT_LOW);
+	if (IS_ERR(st->temp_gpio))
+		return dev_err_probe(dev, PTR_ERR(st->temp_gpio),
+				     "Failed to get temp gpio.\n");
+
+	st->chan_gpio = devm_gpiod_get(dev, "chan", GPIOD_OUT_LOW);
+	if (IS_ERR(st->chan_gpio))
+		return dev_err_probe(dev, PTR_ERR(st->chan_gpio),
+				     "Failed to get chan gpio.\n");
+
+	return 0;
+}
+

...

drivers/iio/adc/ad7191.c line 325

Quoted 11 lines
drivers/iio/adc/ad7191.c new file +11 -0
+
+static int ad7191_set_gain(struct ad7191_state *st, int gain_index)
+{
+	unsigned long value = gain_index;
+
+	st->scale_index = gain_index;
+
+	return gpiod_set_array_value_cansleep(st->pga_gpios->ndescs,
+					      st->pga_gpios->desc,
+					      st->pga_gpios->info, &value);
+}

Depending on timing, we might be able to take advantage of [1].

But it isn't merged yet and needs another revision and you are very fast, so don't wait on it. ;-)

[1]: https://lore.kernel.org/linux-iio/[email protected]/

drivers/iio/adc/ad7191.c line 440

Quoted 9 lines
drivers/iio/adc/ad7191.c new file +9 -0
+
+static const struct iio_chan_spec ad7191_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.address = AD7191_CH_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),

Should this one be info_mask_separate?

Since this is a multiplexed ADC and not simelutaneous sampling, I would expect that if the ORD pins are set to 10Hz (0.1s period), then a buffered read with all channels enabled would take 0.3s to do the 3 samples (effective sample rate of 3.33Hz), but if only one channel was enabled in the buffer, then it only takes 0.1s to do all of the samples (effective sample rate is 10Hz).

The iio convention is to use info_mask_separate for the sampling_frequency attribute to indicate that the rate only applies to each individual channel and not the combined rate to do one "set" of samples for all enabled channels.

A sampling_frequency attribute that was shared_by_all would mean that on each period equivlent to this rate, all samples are read no matter how many channels were enabled for a buffered read.

Quoted 8 lines
drivers/iio/adc/ad7191.c new file +8 -0
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.scan_type = {
+			.sign = 'u',
+			.realbits = 24,
+			.storagebits = 32,
+			.endianness = IIO_BE,
+		},
+	},
maintainer
#4 lore raw

Re: [PATCH v4 2/3] iio: adc: ad7191: add AD7191

Quoted 21 lines

drivers/iio/adc/ad7191.c line 440

Quoted 12 lines
drivers/iio/adc/ad7191.c new file +9 -0
+
+static const struct iio_chan_spec ad7191_channels[] = {
+	{
+		.type = IIO_TEMP,
+		.address = AD7191_CH_TEMP,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+				      BIT(IIO_CHAN_INFO_OFFSET),
+		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),
+		.info_mask_shared_by_all_available = BIT(IIO_CHAN_INFO_SAMP_FREQ),  

Should this one be info_mask_separate?

Since this is a multiplexed ADC and not simelutaneous sampling, I would expect that if the ORD pins are set to 10Hz (0.1s period), then a buffered read with all channels enabled would take 0.3s to do the 3 samples (effective sample rate of 3.33Hz), but if only one channel was enabled in the buffer, then it only takes 0.1s to do all of the samples (effective sample rate is 10Hz).

Quoted 7 lines

The iio convention is to use info_mask_separate for the sampling_frequency attribute to indicate that the rate only applies to each individual channel and not the combined rate to do one "set" of samples for all enabled channels.

A sampling_frequency attribute that was shared_by_all would mean that on each period equivlent to this rate, all samples are read no matter how many channels were enabled for a buffered read.

I think this device channel selection is onehot. As such only one channel is ever enabled at a time. See num_slots in ad_sigma_delta which is set to 1 if not specified. We could argue that maybe one day the infrastructure will be added to make that code deal with dynamic channel changes, settling time and all that mess though so maybe it should be separate even though it makes no difference today.

Jonathan

maintainer
#5 lore raw

Re: [PATCH v4 2/3] iio: adc: ad7191: add AD7191

In reply to Alisa-Dariana Roman
Quoted 7 lines · On Mon, 3 Feb 2025 15:31:27 +0200 Alisa-Dariana Roman…

AD7191 is a pin-programmable, ultra-low noise 24-bit sigma-delta ADC designed for precision bridge sensor measurements. It features two differential analog input channels, selectable output rates, programmable gain, internal temperature sensor and simultaneous 50Hz/60Hz rejection.

Hi,

Just one trivial additional comment from me as you'll be doing a v5 anyway. If it had just been this I'd have tweaked it whilst applying.

Thanks,

Jonathan

drivers/iio/adc/ad7191.c

Quoted 7 lines
drivers/iio/adc/ad7191.c new file +1 -0
new file mode 100644
index 000000000000..4a9e66853294
 
+static int ad7191_setup(struct iio_dev *indio_dev, struct device *dev)

dev isn't used, so drop that parameter.

Quoted 14 lines
drivers/iio/adc/ad7191.c new file +14 -0
+{
+	struct ad7191_state *st = iio_priv(indio_dev);
+	int ret;
+
+	ret = ad7191_init_regulators(indio_dev);
+	if (ret)
+		return ret;
+
+	ret = ad7191_config_setup(indio_dev);
+	if (ret)
+		return ret;
+
+	return ad7191_clock_setup(st);
+}
#6 lore raw

PATCH v4 3/3 docs: iio: add AD7191

In reply to Alisa-Dariana Roman

Add documentation for AD7191 driver.

---
 Documentation/iio/ad7191.rst | 250 +++++++++++++++++++++++++++++++++++
 Documentation/iio/index.rst  |   1 +
 2 files changed, 251 insertions(+)
 create mode 100644 Documentation/iio/ad7191.rst
Documentation/iio/ad7191.rst new file +250 -0
new file mode 100644
index 000000000000..b55f3c13e45a
@@ -0,0 +1,250 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+==============
+AD7191 driver
+==============
+
+Device driver for Analog Devices AD7191 ADC.
+
+==================
+Supported devices
+==================
+
+* `AD7191 <https://www.analog.com/AD7191>`_
+
+The AD7191 is a high precision, low noise, 24-bit Σ-Δ ADC with integrated PGA.
+It features two differential input channels, an internal temperature sensor, and
+configurable sampling rates.
+
+=====================
+Device Configuration
+=====================
+
+--------------------
+Pin Configuration
+--------------------
+
+The driver supports both pin-strapped and GPIO-controlled configurations for ODR
+(Output Data Rate) and PGA (Programmable Gain Amplifier) settings. These
+configurations are mutually exclusive - you must use either pin-strapped or GPIO
+control for each setting, not both.
+
+^^^^^^^^^^^^^^^^^^^^
+ODR Configuration
+^^^^^^^^^^^^^^^^^^^^
+
+The ODR can be configured either through GPIO control or pin-strapping:
+
+- When using GPIO control, specify the "odr-gpios" property in the device tree
+- For pin-strapped configuration, specify the "adi,odr-value" property in the
+  device tree
+
+Available ODR settings:
+
+  - 120 Hz (ODR1=0, ODR2=0)
+  - 60 Hz (ODR1=0, ODR2=1)
+  - 50 Hz (ODR1=1, ODR2=0)
+  - 10 Hz (ODR1=1, ODR2=1)
+
+^^^^^^^^^^^^^^^^^^^
+PGA Configuration
+^^^^^^^^^^^^^^^^^^^
+
+The PGA can be configured either through GPIO control or pin-strapping:
+
+- When using GPIO control, specify the "pga-gpios" property in the device tree
+- For pin-strapped configuration, specify the "adi,pga-value" property in the
+  device tree
+
+Available PGA gain settings:
+
+  - 1x (PGA1=0, PGA2=0)
+  - 8x (PGA1=0, PGA2=1)
+  - 64x (PGA1=1, PGA2=0)
+  - 128x (PGA1=1, PGA2=1)
+
+--------------------
+Clock Configuration
+--------------------
+
+The AD7191 supports both internal and external clock sources:
+
+- When CLKSEL pin is tied LOW: Uses internal 4.92MHz clock (no clock property
+  needed)
+- When CLKSEL pin is tied HIGH: Requires external clock source
+  - Can be a crystal between MCLK1 and MCLK2 pins
+  - Or a CMOS-compatible clock driving MCLK2 pin
+  - Must specify the "clocks" property in device tree when using external clock
+
+--------------------------
+SPI Interface Requirements
+--------------------------
+
+The AD7191 has specific SPI interface requirements:
+
+- The DOUT/RDY output is dual-purpose and requires SPI bus locking
+- DOUT/RDY must be connected to an interrupt-capable GPIO
+- The SPI controller's chip select must be connected to the PDOWN pin of the ADC
+- When CS (PDOWN) is high, the device powers down and resets internal circuitry
+- SPI mode 3 operation (CPOL=1, CPHA=1) is required
+
+-------------------------
+Power Supply Requirements
+-------------------------
+
+The device requires the following power supplies:
+
+- AVdd: Analog power supply
+- DVdd: Digital power supply
+- Vref: Reference voltage supply (external)
+
+All power supplies must be specified in the device tree.
+
+===================
+Device Attributes
+===================
+
+The AD7191 provides several attributes through the IIO sysfs interface:
+
+-----------------------------------
+Voltage Input Differential Channels
+-----------------------------------
+
++-------------------+----------------------------------------------------------+
+| Attribute         | Description                                              |
++===================+==========================================================+
+| raw               | Raw ADC output value                                     |
++-------------------+----------------------------------------------------------+
+| scale             | Scale factor to convert raw value to voltage             |
++-------------------+----------------------------------------------------------+
+| offset            | Voltage offset                                           |
++-------------------+----------------------------------------------------------+
+| sampling_frequency| Current sampling frequency setting                       |
++-------------------+----------------------------------------------------------+
+
+--------------------
+Temperature Sensor
+--------------------
+
++-------------------+----------------------------------------------------------+
+| Attribute         | Description                                              |
++===================+==========================================================+
+| raw               | Raw temperature sensor output value                      |
++-------------------+----------------------------------------------------------+
+| scale             | Scale factor to convert raw value to temperature         |
++-------------------+----------------------------------------------------------+
+| offset            | Temperature calibration offset                           |
++-------------------+----------------------------------------------------------+
+
+--------------------
+Available Attributes
+--------------------
+
+The following attributes show available configuration options:
+
+- sampling_frequency_available: List of supported sampling frequencies
+- scale_available: List of supported scale factors (based on PGA settings)
+
+=====================
+Channel Configuration
+=====================
+
+The device provides three channels:
+
+1. Temperature Sensor
+   - 24-bit unsigned
+   - Internal temperature measurement
+   - Temperature in millidegrees Celsius
+
+2. Differential Input (AIN1-AIN2)
+   - 24-bit unsigned
+   - Differential voltage measurement
+   - Configurable gain via PGA
+
+3. Differential Input (AIN3-AIN4)
+   - 24-bit unsigned
+   - Differential voltage measurement
+   - Configurable gain via PGA
+
+=====================
+Device Tree Bindings
+=====================
+
+-------------------
+Required Properties
+-------------------
+
+- compatible: Should be "adi,ad7191"
+- reg: SPI chip select number
+- spi-max-frequency: Maximum SPI clock frequency
+- spi-cpol: Must be present (set to 1)
+- spi-cpha: Must be present (set to 1)
+- interrupts: Interrupt mapping for DOUT/RDY pin
+- avdd-supply: Analog power supply
+- dvdd-supply: Digital power supply
+- vref-supply: Reference voltage supply
+- temp-gpios: GPIO for temperature channel selection
+- chan-gpios: GPIO for input channel selection
+
+-------------------
+Optional Properties
+-------------------
+
+- clocks: Required when using external clock (CLKSEL=1), must be absent for
+  internal clock
+- adi,odr-value: Pin-strapped ODR configuration (120, 60, 50, or 10)
+- adi,pga-value: Pin-strapped PGA configuration (1, 8, 64, or 128)
+- odr-gpios: GPIOs for ODR control (mutually exclusive with adi,odr-value)
+- pga-gpios: GPIOs for PGA control (mutually exclusive with adi,pga-value)
+
+-------------------
+Example Device Tree
+-------------------
+
+.. code-block:: dts
+
+    #include <dt-bindings/gpio/gpio.h>
+    #include <dt-bindings/interrupt-controller/irq.h>
+
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        ad7191@0 {
+            compatible = "adi,ad7191";
+            reg = <0>;
+            spi-max-frequency = <1000000>;
+
+            /* Required SPI mode 3 */
+            spi-cpol;
+            spi-cpha;
+
+            /* Interrupt for DOUT/RDY pin */
+            interrupts = <25 IRQ_TYPE_EDGE_FALLING>;
+            interrupt-parent = <&gpio>;
+
+            /* Power supplies */
+            avdd-supply = <&avdd>;
+            dvdd-supply = <&dvdd>;
+            vref-supply = <&vref>;
+
+            /* Optional external clock */
+            clocks = <&ad7191_mclk>;
+
+            /* Configuration - either use GPIO control or pin-strapped values */
+            adi,pga-value = <1>;
+            odr-gpios = <&gpio 23 GPIO_ACTIVE_HIGH>,
+                       <&gpio 24 GPIO_ACTIVE_HIGH>;
+
+            /* Required GPIO controls */
+            temp-gpios = <&gpio 22 GPIO_ACTIVE_HIGH>;
+            chan-gpios = <&gpio 27 GPIO_ACTIVE_HIGH>;
+        };
+    };
+
+================
+Buffer Support
+================
+
+This driver supports IIO triggered buffers. See Documentation/iio/iio_devbuf.rst
+for more information about IIO triggered buffers.
Documentation/iio/index.rst +1 -0
index 5710f5b9e958..9235f38f47a1 100644
@@ -20,6 +20,7 @@ Industrial I/O Kernel Drivers
 
    ad4000
    ad4695
+   ad7191
    ad7380
    ad7606
    ad7625
Signature · 1 line
2.43.0
reviewer
#7 lore raw

Re: [PATCH v4 3/3] docs: iio: add AD7191

Quoted 27 lines · Alisa-Dariana Roman

Add documentation for AD7191 driver.

---
 Documentation/iio/ad7191.rst | 250 +++++++++++++++++++++++++++++++++++
 Documentation/iio/index.rst  |   1 +
 2 files changed, 251 insertions(+)
 create mode 100644 Documentation/iio/ad7191.rst
Documentation/iio/ad7191.rst new file +12 -0
new file mode 100644
index 000000000000..b55f3c13e45a
@@ -0,0 +1,250 @@
+.. SPDX-License-Identifier: GPL-2.0-only
+
+==============
+AD7191 driver
+==============
+
+Device driver for Analog Devices AD7191 ADC.
+
+==================
+Supported devices
+==================
+

Documentation/iio/ad7191.rst line 22

Quoted 10 lines
Documentation/iio/ad7191.rst new file +10 -0
+* `AD7191 <https://www.analog.com/AD7191>`_
+
+The AD7191 is a high precision, low noise, 24-bit Σ-Δ ADC with integrated PGA.
+It features two differential input channels, an internal temperature sensor, and
+configurable sampling rates.
+
+=====================
+Device Configuration
+=====================
+

I would call this section the Devicetree/wiring section since devicetree has to do with how the chip is wired up.

The existing Device Tree Bindings section at the end pretty much just repeats the DT bindings .yml doc, so we could drop that section from this doc.

What you have written in this section already covers it very well.

Documentation/iio/ad7191.rst line 25

Quoted 3 lines
Documentation/iio/ad7191.rst new file +3 -0
+--------------------
+Pin Configuration
+--------------------

I think it looks the nicest when the --- exactly line up with the text and I think that is the usual kernel style as well.

Example:

-----------------
Pin Configuration
-----------------

Same applies to the rest of the doc.

Documentation/iio/ad7191.rst line 105

Quoted 3 lines
Documentation/iio/ad7191.rst new file +3 -0
+===================
+Device Attributes
+===================

There isn't really anything unusual about attributes on this chip compared to typical ADCs, so I would be OK if we left out this section. If we do keep it though, I think we should write out the full attribute name since some are separate, some shared_by_type and some shared_by_all, so they have different prefixes.

Quoted 32 lines
Documentation/iio/ad7191.rst new file +32 -0
+
+The AD7191 provides several attributes through the IIO sysfs interface:
+
+-----------------------------------
+Voltage Input Differential Channels
+-----------------------------------
+
++-------------------+----------------------------------------------------------+
+| Attribute         | Description                                              |
++===================+==========================================================+
+| raw               | Raw ADC output value                                     |
++-------------------+----------------------------------------------------------+
+| scale             | Scale factor to convert raw value to voltage             |
++-------------------+----------------------------------------------------------+
+| offset            | Voltage offset                                           |
++-------------------+----------------------------------------------------------+
+| sampling_frequency| Current sampling frequency setting                       |
++-------------------+----------------------------------------------------------+
+
+--------------------
+Temperature Sensor
+--------------------
+
++-------------------+----------------------------------------------------------+
+| Attribute         | Description                                              |
++===================+==========================================================+
+| raw               | Raw temperature sensor output value                      |
++-------------------+----------------------------------------------------------+
+| scale             | Scale factor to convert raw value to temperature         |
++-------------------+----------------------------------------------------------+
+| offset            | Temperature calibration offset                           |
++-------------------+----------------------------------------------------------+

Documentation/iio/ad7191.rst line 147

Quoted 10 lines
Documentation/iio/ad7191.rst new file +10 -0
+
+--------------------
+Available Attributes
+--------------------
+
+The following attributes show available configuration options:
+
+- sampling_frequency_available: List of supported sampling frequencies
+- scale_available: List of supported scale factors (based on PGA settings)
+

One of these days, we should probably write a generic page on the common attributes raw/scale/offset and somewhat common sampling_frequency/ oversampling_ratio (probably a few more that I'm forgetting). :-)