Skip to contentSkip to conversation

Support for TI ADS7128 and ADS7138 ADCs

PATCH Superseded Tobias Sperling 2 participants 6 messages Feb 6, 2025 – Feb 16, 2025

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support for TI ADS7128 and ADS7138 ADCs

This patch series adds support for Texas Instruments ADS7128 and ADS7138, which are 12-bit, 8 channel analog-to-digital converters (ADCs) with build-in digital window comparator (DWC), using the I2C interface.

The driver exposes the interfaces to read the raw values, as well as the minimum and maximum value for each channel. In addition several settings can be configured, like the DWC, sampling frequency or an averaging filter/oversampling. Interrupts triggered by the DWC, if configured, are then exposed as IIO events.

ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module, which are not yet supported by the driver.

Regarding the I2C interface the chips using opcodes to define the way how the registeres are accessed, like single or multiple register(s) read/write or setting/clearing only bits.

---
Changes in v3:
- Make interrupt optional.
- Replace SET_RUNTIME_PM_OPS() with RUNTIME_PM_OPS() to prevent warning.
- Rework read_avail for sampling frequency to show each frequency only once.
- Use IIO_CHAN_INFO_PEAK and IIO_CHAN_INFO_TROUGH instead of ext_info.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Improved commit messages.
- dt-bindings: drop info about what driver supports, make 'avdd-supply' optional.
- General rework of driver regarding indentation and code style.
- General code improvements to make code shorter and improve readability, like remove 'goto's, order of declarations, ...
- Use kernel macros and functions, like FIELD_*, guard(), ...
- Rework i2c functions to return 0 in case of success and use i2c_master_send() if possible.
- Use struct for chip data instead of enum.
- Add comment to what the lock is used for and make sure it's used in these cases.
- Use read_avail of iio_info and extend to return also the available values for OSR.
- Rework to only accept values of the availability list.
- Use devm_* if possible and therefore drop 'remove' callback.
- Rebase to kernel 6.13 and adjust to API changes.
- Link to v1:
https://lore.kernel.org/r/[email protected]

Changes in v1 (to patch series without b4):
- dt-bindings: Extended description

---
Tobias Sperling (2):
dt-bindings: iio: adc: Introduce ADS7138
iio: adc: Add driver for ADS7128 / ADS7138

 .../devicetree/bindings/iio/adc/ti,ads7138.yaml    |  62 ++
 drivers/iio/adc/Kconfig                            |  10 +
 drivers/iio/adc/Makefile                           |   1 +
 drivers/iio/adc/ti-ads7138.c                       | 746 +++++++++++++++++++++
 4 files changed, 819 insertions(+)

---
base-commit: 05dbaf8dd8bf537d4b4eb3115ab42a5fb40ff1f5
change-id: 20241122-adc_ml-d1ce86e85b2c

Best regards,

Signature · 1 line
Tobias Sperling <[email protected]>
#1 lore raw

PATCH v3 1/2 dt-bindings: iio: adc: Introduce ADS7138

Add documentation for the driver of ADS7128 and ADS7138 12-bit, 8-channel analog-to-digital converters. These ADCs have a wide operating range and a wide feature set. Communication is based on the I2C interface. ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.

---
 .../devicetree/bindings/iio/adc/ti,ads7138.yaml    | 62 ++++++++++++++++++++++
 1 file changed, 62 insertions(+)
Documentation/devicetree/bindings/iio/adc/ti,ads7138.yaml new file +62 -0
new file mode 100644
index 0000000000000000000000000000000000000000..c73057df6d8a56230a69978a7f673045f17a276b
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/ti,ads7138.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Texas Instruments ADS7128/ADS7138 analog-to-digital converter (ADC)
+
+maintainers:
+  - Tobias Sperling <[email protected]>
+
+description: |
+  The ADS7128 and ADS7138 chips are 12-bit, 8 channel analog-to-digital
+  converters (ADC) with build-in digital window comparator (DWC), using the
+  I2C interface.
+  ADS7128 differs in the addition of further hardware features, like a
+  root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.
+
+  Datasheets:
+    https://www.ti.com/product/ADS7128
+    https://www.ti.com/product/ADS7138
+
+properties:
+  compatible:
+    enum:
+      - ti,ads7128
+      - ti,ads7138
+
+  reg:
+    maxItems: 1
+
+  avdd-supply:
+    description:
+      The regulator used as analog supply voltage as well as reference voltage.
+
+  interrupts:
+    description:
+      Interrupt on ALERT pin, triggers on low level.
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        adc@10 {
+            compatible = "ti,ads7138";
+            reg = <0x10>;
+            avdd-supply = <&reg_stb_3v3>;
+            interrupt-parent = <&gpio2>;
+            interrupts = <12 IRQ_TYPE_LEVEL_LOW>;
+        };
+    };
+...
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/2 iio: adc: Add driver for ADS7128 / ADS7138

In reply to Tobias Sperling

Add driver for ADS7128 and ADS7138 12-bit, 8-channel analog-to-digital converters. These ADCs have a wide operating range and a wide feature set. Communication is based on the I2C interface.
ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.

---
 drivers/iio/adc/Kconfig      |  10 +
 drivers/iio/adc/Makefile     |   1 +
 drivers/iio/adc/ti-ads7138.c | 746 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 757 insertions(+)
drivers/iio/adc/Kconfig +10 -0
index 849c90203071a77ec7d94cec06d4378ece44440b..c43277305a126c498f97e843c05747fddb705e9a 100644
@@ -1467,6 +1467,16 @@ config TI_ADS1119
          This driver can also be built as a module. If so, the module will be
          called ti-ads1119.
 
+config TI_ADS7138
+	tristate "Texas Instruments ADS7128 and ADS7138 ADC driver"
+	depends on I2C
+	help
+	  If you say yes here you get support for Texas Instruments ADS7128 and
+	  ADS7138 8-channel A/D converters with 12-bit resolution.
+
+	  This driver can also be built as a module. If so, the module will be
+	  called ti-ads7138.
+
 config TI_ADS7924
 	tristate "Texas Instruments ADS7924 ADC"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index ee19afba62b7fe0a68309c16f3581d98c5b8f653..1e71d8eb6406b92e5d8e99d556c38858a8b9b640 100644
@@ -133,6 +133,7 @@ obj-$(CONFIG_TI_ADS1119) += ti-ads1119.o
 obj-$(CONFIG_TI_ADS124S08) += ti-ads124s08.o
 obj-$(CONFIG_TI_ADS1298) += ti-ads1298.o
 obj-$(CONFIG_TI_ADS131E08) += ti-ads131e08.o
+obj-$(CONFIG_TI_ADS7138) += ti-ads7138.o
 obj-$(CONFIG_TI_ADS7924) += ti-ads7924.o
 obj-$(CONFIG_TI_ADS7950) += ti-ads7950.o
 obj-$(CONFIG_TI_ADS8344) += ti-ads8344.o
drivers/iio/adc/ti-ads7138.c new file +746 -0

746 lines not on this page · raw message

Signature · 1 line
2.34.1
maintainer
#3 lore raw

Re: [PATCH v3 2/2] iio: adc: Add driver for ADS7128 / ADS7138

Quoted 9 lines · On Thu, 06 Feb 2025 16:41:49 +0100 Tobias Sperling via B4 Relay…

From: Tobias Sperling <[email protected]>

Add driver for ADS7128 and ADS7138 12-bit, 8-channel analog-to-digital converters. These ADCs have a wide operating range and a wide feature set. Communication is based on the I2C interface.
ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.

Hi Tobias,

Minor comments below and one question about power management

Thanks,

Jonathan

Quoted 17 lines
drivers/iio/adc/ti-ads7138.c new file +11 -0
new file mode 100644
index 0000000000000000000000000000000000000000..16d9ae7426522ddad62fd78ac6138aab6ab41101
@@ -0,0 +1,746 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * ADS7138 - Texas Instruments Analog-to-Digital Converter
+ */
+
+#include <linux/bitfield.h>
+#include <linux/cleanup.h>
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/init.h>
+#include <linux/interrupt.h>

drivers/iio/adc/ti-ads7138.c line 21

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/regulator/consumer.h>
+#include <linux/unaligned.h>
+
+#include <linux/iio/events.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/types.h>
+
+/* AVDD (VREF) operating range in millivolts */

Generally a bad idea to use wild cards. They go wrong far too often as manufacturers love to make other uses of the product codes they cover. Just pick a device that these values apply to and use that name throughout. Given driver name I'd just call these all ADS7138 unless they don't apply to that chip.
We've been bitten by this wild card thing too often in the past!

drivers/iio/adc/ti-ads7138.c line 22

Quoted 1 line
drivers/iio/adc/ti-ads7138.c new file +1 -0
+#define ADS71x8_AVDD_MV_MIN		2350

Good perhaps to have a comment on what the two sides of this are and why it is structure like this.

Quoted 9 lines
drivers/iio/adc/ti-ads7138.c new file +9 -0
+static const int ads71x8_samp_freqs_bits[2][26] = {
+	{
+		163, 244, 326, 488, 651, 977, 1302, 1953,
+		2604, 3906, 5208, 7813, 10417, 15625, 20833, 31250,
+		41667, 62500, 83333, 125000, 166667, 250000, 333333, 500000,
+		666667, 1000000
+	}, {
+		0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18,
+		0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10,

drivers/iio/adc/ti-ads7138.c line 125

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +8 -0
+		/* Here is a hole, due to duplicate frequencies */
+		0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02,
+		0x01, 0x00
+	}
+};
 
 
+static int ads71x8_i2c_write(const struct i2c_client *client, u8 reg, u8 value)
+{
+	return ads71x8_i2c_write_block(client, reg, &value, sizeof(value));

Maybe this should use the single register write (figure 35) rather than bulk one? It makes no real difference though other than different opcode.

Quoted 7 lines
drivers/iio/adc/ti-ads7138.c new file +6 -0
+}
+
+static int ads71x8_i2c_setclear_bit(const struct i2c_client *client, u8 reg,
+				    u8 bits, u8 opcode)
 
+{
+	u8 buf[3] = { opcode, reg, bits };

drivers/iio/adc/ti-ads7138.c line 141

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+	int ret;
+
+	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
+	if (ret < 0)
+		return ret;
+	if (ret != ARRAY_SIZE(buf))
+		return -EIO;
+
+	return 0;
+}

Whilst this is currently just used for setclear_bit, it is slightly more general so maybe the name should reflect that it could be used for single register writes for instance. Naming is hard though and I can't immediately think what name covers this combination.

Quoted 20 lines
drivers/iio/adc/ti-ads7138.c new file +20 -0
+
+static int ads71x8_read_event(struct iio_dev *indio_dev,
+			      const struct iio_chan_spec *chan,
+			      enum iio_event_type type,
+			      enum iio_event_direction dir,
+			      enum iio_event_info info, int *val, int *val2)
+{
+	struct ads71x8_data *data = iio_priv(indio_dev);
+	u8 reg, values[2];
+	int ret;
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE:
+		reg = (dir == IIO_EV_DIR_RISING) ?
+				ADS71x8_REG_HIGH_TH_HYS_CH(chan->channel) :
+				ADS71x8_REG_LOW_TH_CNT_CH(chan->channel);
+		ret = ads71x8_i2c_read_block(data->client, reg, values,
+					     ARRAY_SIZE(values));
+		if (ret)
+			return ret;

drivers/iio/adc/ti-ads7138.c line 359

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+
+		*val = ((values[1] << 4) | (values[0] >> 4));
+		return IIO_VAL_INT;
+	case IIO_EV_INFO_HYSTERESIS:
+		ret = ads71x8_i2c_read(data->client,
+				       ADS71x8_REG_HIGH_TH_HYS_CH(chan->channel));
+		if (ret < 0)
+			return ret;
+
+		*val = (ret & ~ADS71x8_THRESHOLD_LSB_MASK);

Brackets don't add much, so drop them in cases like this.

Quoted 13 lines
drivers/iio/adc/ti-ads7138.c new file +13 -0
+		return IIO_VAL_INT;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int ads71x8_write_event(struct iio_dev *indio_dev,
+			       const struct iio_chan_spec *chan,
+			       enum iio_event_type type,
+			       enum iio_event_direction dir,
+			       enum iio_event_info info, int val, int val2)
+{
+	struct ads71x8_data *data = iio_priv(indio_dev);

drivers/iio/adc/ti-ads7138.c line 382

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+	u8 reg, values[2];
+	int ret;
+
+	switch (info) {
+	case IIO_EV_INFO_VALUE: {
+		if (val >= BIT(12) || val < 0)
+			return -EINVAL;
+
+		reg = (dir == IIO_EV_DIR_RISING) ?
+				ADS71x8_REG_HIGH_TH_HYS_CH(chan->channel) :

For these cases indent by just one tab more than the line above.

Quoted 41 lines
drivers/iio/adc/ti-ads7138.c new file +38 -0
+				ADS71x8_REG_LOW_TH_CNT_CH(chan->channel);
+
+		guard(mutex)(&data->lock);
+		ret = ads71x8_i2c_read(data->client, reg);
+		if (ret < 0)
+			return ret;
+
+		values[0] = ret & ~ADS71x8_THRESHOLD_LSB_MASK;
+		values[0] |= FIELD_PREP(ADS71x8_THRESHOLD_LSB_MASK, val);
+		values[1] = (val >> 4);
+		return ads71x8_i2c_write_block(data->client, reg, values,
+					       ARRAY_SIZE(values));
+	}
+	case IIO_EV_INFO_HYSTERESIS: {
+		if (val >= BIT(4) || val < 0)
+			return -EINVAL;
+
+		reg = ADS71x8_REG_HIGH_TH_HYS_CH(chan->channel);
+
+		guard(mutex)(&data->lock);
+		ret = ads71x8_i2c_read(data->client, reg);
+		if (ret < 0)
+			return ret;
+
+		values[0] = val & ~ADS71x8_THRESHOLD_LSB_MASK;
+		values[0] |= FIELD_PREP(ADS71x8_THRESHOLD_LSB_MASK, ret >> 4);
+		return ads71x8_i2c_write(data->client, reg, values[0]);
+	}
+	default:
+		return -EINVAL;
+	}
+}
 
 
 
+static irqreturn_t ads71x8_event_handler(int irq, void *priv)
+{
+	struct iio_dev *indio_dev = priv;
+	struct ads71x8_data *data = iio_priv(indio_dev);
+	u8 i, events_high, events_low;
+	u64 code;

drivers/iio/adc/ti-ads7138.c

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+	int ret;
+
+	/* Check if interrupt was trigger by us */
+	ret = ads71x8_i2c_read(data->client, ADS71x8_REG_EVENT_FLAG);
+	if (ret <= 0)
+		return IRQ_NONE;
+
+	ret = ads71x8_i2c_read(data->client, ADS71x8_REG_EVENT_HIGH_FLAG);
+	if (ret < 0) {
+		dev_warn(&data->client->dev,

I would add a local

	struct device *dev = &data->client->dev;

given it's used a few times.

Quoted 49 lines
drivers/iio/adc/ti-ads7138.c new file +49 -0
+			 "Failed to read event high flags: %d\n", ret);
+		return IRQ_HANDLED;
+	}
+	events_high = ret;
+
+	ret = ads71x8_i2c_read(data->client, ADS71x8_REG_EVENT_LOW_FLAG);
+	if (ret < 0) {
+		dev_warn(&data->client->dev,
+			 "Failed to read event low flags: %d\n", ret);
+		return IRQ_HANDLED;
+	}
+	events_low = ret;
+
+	for (i = 0; i < data->chip_data->channel_num; i++) {
+		if (events_high & BIT(i)) {
+			code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_RISING);
+			iio_push_event(indio_dev, code,
+				       iio_get_time_ns(indio_dev));
+		}
+		if (events_low & BIT(i)) {
+			code = IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, i,
+						    IIO_EV_TYPE_THRESH,
+						    IIO_EV_DIR_FALLING);
+			iio_push_event(indio_dev, code,
+				       iio_get_time_ns(indio_dev));
+		}
+	}
+
+	/* Try to clear all interrupt flags */
+	ret = ads71x8_i2c_write(data->client, ADS71x8_REG_EVENT_HIGH_FLAG, 0xFF);
+	if (ret)
+		dev_warn(&data->client->dev,
+			 "Failed to clear event high flags: %d\n", ret);
+
+	ret = ads71x8_i2c_write(data->client, ADS71x8_REG_EVENT_LOW_FLAG, 0xFF);
+	if (ret)
+		dev_warn(&data->client->dev,
+			 "Failed to clear event low flags: %d\n", ret);
+
+	return IRQ_HANDLED;
+}
+
+static int ads71x8_set_conv_mode(struct ads71x8_data *data,
+				 enum ads71x8_modes mode)
+{
+	if (mode == ADS71x8_MODE_AUTO)
+		return ads71x8_i2c_set_bit(data->client, ADS71x8_REG_OPMODE_CFG,

drivers/iio/adc/ti-ads7138.c line 607

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+					   ADS71x8_OPMODE_CFG_CONV_MODE);
+	return ads71x8_i2c_clear_bit(data->client, ADS71x8_REG_OPMODE_CFG,
+				     ADS71x8_OPMODE_CFG_CONV_MODE);
+}
+
+static int ads7138_init_hw(struct ads71x8_data *data)
+{
+	int ret;
+
+	data->vref_regu = devm_regulator_get_optional(&data->client->dev, "avdd");

avdd isn't optional. We need the power! As such I'd not paper over the lack of it being available. To avoid weird effects on reading the scale later, you may want to do a read here so that we can error out if a stub regulator has been provided.

Quoted 7 lines
drivers/iio/adc/ti-ads7138.c new file +7 -0
+	if (IS_ERR(data->vref_regu))
+		data->vref_regu = NULL;
+
+	/* Reset the chip to get a defined starting configuration */
+	ret = ads71x8_i2c_set_bit(data->client, ADS71x8_REG_GENERAL_CFG,
+				  ADS71x8_GENERAL_CFG_RST);
+	if (ret)

drivers/iio/adc/ti-ads7138.c line 624

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+		return ret;
+
+	ret = ads71x8_set_conv_mode(data, ADS71x8_MODE_AUTO);
+	if (ret)
+		return ret;
+
+	/* Enable statistics and digital window comparator */
+	ret = ads71x8_i2c_set_bit(data->client, ADS71x8_REG_GENERAL_CFG,
+				  (ADS71x8_GENERAL_CFG_STATS_EN |
+				   ADS71x8_GENERAL_CFG_DWC_EN));

The inner brackets don't add much to readability so I'd be tempted to drop them.

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+	if (ret)
+		return ret;
+
+	/* Enable all channels for auto sequencing */
+	ret = ads71x8_i2c_set_bit(data->client, ADS71x8_REG_AUTO_SEQ_CH_SEL, 0xFF);
+	if (ret)
+		return ret;
+
+	/* Set auto sequence mode and start sequencing */
+	return ads71x8_i2c_set_bit(data->client, ADS71x8_REG_SEQUENCE_CFG,

drivers/iio/adc/ti-ads7138.c line 644

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +10 -0
+				   (ADS71x8_SEQUENCE_CFG_SEQ_START |
+				    ADS71x8_SEQUENCE_CFG_SEQ_MODE));
+}
+
+static int ads71x8_probe(struct i2c_client *client)
+{
+	struct device *dev = &client->dev;
+	struct iio_dev *indio_dev;
+	struct ads71x8_data *data;
+	int ret = 0;

Value not used so drop setting it here.

Quoted 37 lines
drivers/iio/adc/ti-ads7138.c new file +37 -0
+
+	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	data->client = client;
+	data->chip_data = i2c_get_match_data(client);
+	if (!data->chip_data)
+		return -ENODEV;
+
+	ret = devm_mutex_init(dev, &data->lock);
+	if (ret)
+		return ret;
+
+	indio_dev->name = data->chip_data->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = ads71x8_channels;
+	indio_dev->num_channels = ARRAY_SIZE(ads71x8_channels);
+	indio_dev->info = &ti_ads71x8_info;
+
+	i2c_set_clientdata(client, indio_dev);
+
+	if (client->irq > 0) {
+		ret = devm_request_threaded_irq(dev, client->irq,
+						NULL, ads71x8_event_handler,
+						IRQF_TRIGGER_LOW |
+						IRQF_ONESHOT | IRQF_SHARED,
+						client->name, indio_dev);
+		if (ret)
+			return ret;
+	}
+
+	ret = ads7138_init_hw(data);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to initialize device\n");
+

drivers/iio/adc/ti-ads7138.c line 706

Quoted 10 lines
drivers/iio/adc/ti-ads7138.c new file +9 -0
+	ret = devm_iio_device_register(dev, indio_dev);
+	if (ret)
+		return dev_err_probe(dev, ret, "Failed to register iio device\n");
+
+	return 0;
+}
 
+
+static const struct dev_pm_ops ads71x8_pm_ops = {
+	RUNTIME_PM_OPS(ads71x8_runtime_suspend, ads71x8_runtime_resume, NULL)

Given it's likely that the runtime pm ops are better than nothing in suspend and resume cases as well could we make this DEFINE_RUNTIME_PM_OPS() which uses the runtime ops for those cases as well?

Quoted 1 line

+};

#4 lore raw

Re: [PATCH v3 2/2] iio: adc: Add driver for ADS7128 / ADS7138

Hi Jonathan,

I'll change the things you've mentioned. Just some comments inline.

Regards,
Tobias

Quoted 22 lines

Von: Jonathan Cameron <[email protected]>
Gesendet: Samstag, 8. Februar 2025 16:05

Quoted 9 lines

From: Tobias Sperling <[email protected]>

Add driver for ADS7128 and ADS7138 12-bit, 8-channel analog-to-digital converters. These ADCs have a wide operating range and a wide feature set. Communication is based on the I2C interface.
ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.

Hi Tobias,

Minor comments below and one question about power management

Thanks,

Jonathan

Quoted 6 lines

drivers/iio/adc/ti-ads7138.c line 125

Quoted 3 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+static int ads71x8_i2c_write(const struct i2c_client *client, u8 reg, u8 value)
+{
+	return ads71x8_i2c_write_block(client, reg, &value, sizeof(value));

Maybe this should use the single register write (figure 35) rather than bulk one? It makes no real difference though other than different opcode.

Yeah can be done, but as there's no difference I didn't want to introduce yet another function just for single writes. However, as you mentioned below, ads71x8_i2c_setclear_bit() can be used for that, too. So I'll change that.

Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/ti-ads7138.c new file +13 -0
+}
+
+static int ads71x8_i2c_setclear_bit(const struct i2c_client *client, u8 reg,
+				    u8 bits, u8 opcode)
 
+{
+	u8 buf[3] = { opcode, reg, bits };
+	int ret;
+
+	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
+	if (ret < 0)
+		return ret;
+	if (ret != ARRAY_SIZE(buf))
+		return -EIO;
Quoted 8 lines

drivers/iio/adc/ti-ads7138.c line 121

Quoted 3 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+
+	return 0;
+}

Whilst this is currently just used for setclear_bit, it is slightly more general so maybe the name should reflect that it could be used for single register writes for instance. Naming is hard though and I can't immediately think what name covers this combination.

Having something like _write_single_reg_with_opcode() in mind.

Quoted 11 lines

drivers/iio/adc/ti-ads7138.c

Quoted 5 lines
drivers/iio/adc/ti-ads7138.c new file +5 -0
+static int ads7138_init_hw(struct ads71x8_data *data)
+{
+	int ret;
+
+	data->vref_regu = devm_regulator_get_optional(&data->client->dev,

"avdd");

avdd isn't optional. We need the power! As such I'd not paper over the lack of it being available. To avoid weird effects on reading the scale later, you may want to do a read here so that we can error out if a stub regulator has been provided.

Ok, just wanted to add flexibility for the enduser to not having to define it in the DTS, but right, AVDD needs to be connected physically. Will change accordingly.

Quoted 10 lines

drivers/iio/adc/ti-ads7138.c

Quoted 3 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+
+static const struct dev_pm_ops ads71x8_pm_ops = {
+	RUNTIME_PM_OPS(ads71x8_runtime_suspend,

ads71x8_runtime_resume, NULL)

Given it's likely that the runtime pm ops are better than nothing in suspend and resume cases as well could we make this DEFINE_RUNTIME_PM_OPS() which uses the runtime ops for those cases as well?

Yes, looks like DEFINE_RUNTIME_DEV_PM_OPS() can be used to use these functions for the other cases, too.

Quoted 2 lines
Quoted 2 lines

+};

maintainer
#5 lore raw

Re: [PATCH v3 2/2] iio: adc: Add driver for ADS7128 / ADS7138

Quoted 72 lines · Tobias

Hi Jonathan,

I'll change the things you've mentioned. Just some comments inline.

Regards,
Tobias

Quoted 22 lines

Von: Jonathan Cameron <[email protected]>
Gesendet: Samstag, 8. Februar 2025 16:05

Quoted 9 lines

From: Tobias Sperling <[email protected]>

Add driver for ADS7128 and ADS7138 12-bit, 8-channel analog-to-digital converters. These ADCs have a wide operating range and a wide feature set. Communication is based on the I2C interface.
ADS7128 differs in the addition of further hardware features, like a root-mean-square (RMS) and a zero-crossing-detect (ZCD) module.

Hi Tobias,

Minor comments below and one question about power management

Thanks,

Jonathan

Quoted 6 lines

drivers/iio/adc/ti-ads7138.c line 125

Quoted 3 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+static int ads71x8_i2c_write(const struct i2c_client *client, u8 reg, u8 value)
+{
+	return ads71x8_i2c_write_block(client, reg, &value, sizeof(value));  

Maybe this should use the single register write (figure 35) rather than bulk one? It makes no real difference though other than different opcode.

Yeah can be done, but as there's no difference I didn't want to introduce yet another function just for single writes. However, as you mentioned below, ads71x8_i2c_setclear_bit() can be used for that, too. So I'll change that.

Quoted 14 lines
Quoted 14 lines
drivers/iio/adc/ti-ads7138.c new file +13 -0
+}
+
+static int ads71x8_i2c_setclear_bit(const struct i2c_client *client, u8 reg,
+				    u8 bits, u8 opcode)  
 
+{
+	u8 buf[3] = { opcode, reg, bits };
+	int ret;
+
+	ret = i2c_master_send(client, buf, ARRAY_SIZE(buf));
+	if (ret < 0)
+		return ret;
+	if (ret != ARRAY_SIZE(buf))
+		return -EIO;
Quoted 8 lines

drivers/iio/adc/ti-ads7138.c line 121

Quoted 3 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+
+	return 0;
+}  

Whilst this is currently just used for setclear_bit, it is slightly more general so maybe the name should reflect that it could be used for single register writes for instance. Naming is hard though and I can't immediately think what name covers this combination.

Having something like _write_single_reg_with_opcode() in mind.

Quoted 7 lines

drivers/iio/adc/ti-ads7138.c

Quoted 5 lines
drivers/iio/adc/ti-ads7138.c new file +5 -0
+static int ads7138_init_hw(struct ads71x8_data *data)
+{
+	int ret;
+
+	data->vref_regu = devm_regulator_get_optional(&data->client->dev,  

"avdd");

Quoted 8 lines
Quoted 4 lines

avdd isn't optional. We need the power! As such I'd not paper over the lack of it being available. To avoid weird effects on reading the scale later, you may want to do a read here so that we can error out if a stub regulator has been provided.

Ok, just wanted to add flexibility for the enduser to not having to define it in the DTS, but right, AVDD needs to be connected physically. Will change accordingly.

In most cases they will have that flexibility anyway. It is normally fine to not provide regulators.
The regulator core just provides a stub regulator instead that represents an always on supply of unknown characteristics.

Quoted 17 lines
Quoted 11 lines

drivers/iio/adc/ti-ads7138.c

Quoted 4 lines
drivers/iio/adc/ti-ads7138.c new file +3 -0
+
+static const struct dev_pm_ops ads71x8_pm_ops = {
+	RUNTIME_PM_OPS(ads71x8_runtime_suspend,  

ads71x8_runtime_resume, NULL)

Given it's likely that the runtime pm ops are better than nothing in suspend and resume cases as well could we make this DEFINE_RUNTIME_PM_OPS() which uses the runtime ops for those cases as well?

Yes, looks like DEFINE_RUNTIME_DEV_PM_OPS() can be used to use these functions for the other cases, too.

Quoted 2 lines
Quoted 2 lines

+};