* [PATCH v6 0/2] iio: adc: add MAX40080 current-sense amplifier driver
@ 2026-07-30 13:17 Stefan Popa
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
0 siblings, 2 replies; 12+ messages in thread
From: Stefan Popa @ 2026-07-30 13:17 UTC (permalink / raw)
To: linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, dlechner, ciprian.hegbeli,
siratul.islam, u.kleine-koenig, joshua.crofts1, Stefan Popa
This series adds support for the MAX40080, a bidirectional current-sense
amplifier with an integrated 12-bit ADC.
The driver is placed in IIO rather than hwmon because:
- IIO's scale/raw model naturally exposes the selectable gain ranges and
lets userspace apply the correct scale per range.
- Buffered capture support is planned for a follow-up series -- each
conversion returns a matched current+voltage pair, which maps cleanly
to IIO's buffered/triggered model.
Changes since v5:
- Add register prefixes (MAX40080_CFG_MODE_MSK, etc.) for clarity
(Jonathan).
- Define MAX40080_CFG_RANGE_50MV/10MV with designated initializers
(Jonathan, Guenter).
- Use MAX40080_ADC_RES_BITS (12) instead of 4096 (Jonathan).
- Use IIO_VAL_FRACTIONAL_LOG2 for voltage scale (Jonathan).
- Return i2c_smbus_write_word_data() directly at end of max40080_init()
(Jonathan).
- Hold lock in write_raw for proper cache protection; local range copy
in read_raw prevents torn reads (Jonathan, Nuno, Sashiko).
- Add comment explaining intentional *iv = tmp before error check
(Siratul).
- Blank line consistency fixes in read_raw switch cases and
max40080_init() (Siratul).
- Use adi,max40080 compatible prefix (David).
- Add vdd-supply and interrupts properties to binding (David).
Changes since v4:
- Initialize tmp = 0 in max40080_read_iv() before read_poll_timeout to
avoid using potentially uninitialized value in the condition check
(Sashiko, Jonathan).
- Simplify max40080_get_oversampling_ratio() to use cached value instead
of reading from the device (Jonathan).
- Remove DMA-safe buffer from struct -- I2C layer already bounces data
internally for i2c_smbus_* calls (Jonathan).
- Make shunt-resistor-micro-ohms required in DT binding. The default
1 Ohm was arbitrary and rarely correct for real hardware (Jonathan).
- Remove PM ops entirely. Having only resume without suspend was flagged
as "papering over" possible power loss issues (Jonathan).
- Remove the {} scope around propname, declare it at function top (Andy).
- Remove mod_devicetable.h include -- i2c.h provides necessary types
(Jonathan).
Changes since v3:
- Removed all regmap usage -- regmap doesn't support SMBus PEC or Quick
Command, both required by this device (Nuno).
- Fixed locking: max40080_update_bits() now takes the lock internally,
and callers update cached state after the call returns (Nuno).
- Consistent comment style: capital first letter + period (Andy).
- Use propname variable to deduplicate "shunt-resistor-micro-ohms" (Andy).
- Return IIO_VAL_* inline instead of break + return pattern (Nuno).
Changes since v2:
- Added shunt-resistor-micro-ohms DT property with default value.
- Use single-shot mode with SMBus Quick Command trigger for correlated
current/voltage reads.
- Precompute current scale per range at probe time.
Link: https://lore.kernel.org/linux-iio/20260723065036.2683075-1-stefan.popa@analog.com/ [v5]
Link: https://lore.kernel.org/linux-iio/20260717123852.1140893-1-stefan.popa@analog.com/ [v4]
Link: https://lore.kernel.org/linux-iio/20260715063652.368501-1-stefan.popa@analog.com/ [v3]
Link: https://lore.kernel.org/linux-iio/20260713120226.90303-1-stefan.popa@analog.com/ [v2]
Link: https://lore.kernel.org/linux-iio/20260703102941.1141341-1-stefan.popa@analog.com/ [v1]
Stefan Popa (2):
dt-bindings: iio: adc: add adi,max40080
iio: adc: add MAX40080 current-sense amplifier driver
.../bindings/iio/adc/adi,max40080.yaml | 63 ++
MAINTAINERS | 9 +
drivers/iio/adc/Kconfig | 11 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/max40080.c | 574 ++++++++++++++++++
5 files changed, 658 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
create mode 100644 drivers/iio/adc/max40080.c
--
2.53.0
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-30 13:17 [PATCH v6 0/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
@ 2026-07-30 13:17 ` Stefan Popa
2026-07-30 13:25 ` sashiko-bot
` (3 more replies)
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
1 sibling, 4 replies; 12+ messages in thread
From: Stefan Popa @ 2026-07-30 13:17 UTC (permalink / raw)
To: linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, dlechner, ciprian.hegbeli,
siratul.islam, u.kleine-koenig, joshua.crofts1, Stefan Popa
Add device tree bindings for the Analog Devices MAX40080 bidirectional
current-sense amplifier with a 12-bit ADC and an I2C/SMBus interface.
Signed-off-by: Stefan Popa <stefan.popa@analog.com>
---
.../bindings/iio/adc/adi,max40080.yaml | 63 +++++++++++++++++++
MAINTAINERS | 8 +++
2 files changed, 71 insertions(+)
create mode 100644 Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
diff --git a/Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml b/Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
new file mode 100644
index 0000000000000..8dddf58e44125
--- /dev/null
+++ b/Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
@@ -0,0 +1,63 @@
+# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/iio/adc/adi,max40080.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Analog Devices MAX40080 bidirectional current-sense amplifier
+
+maintainers:
+ - Ciprian Hegbeli <ciprian.hegbeli@analog.com>
+ - Stefan Popa <stefan.popa@analog.com>
+
+description: |
+ The MAX40080 is a high-precision, bidirectional current-sense amplifier with
+ an integrated 12-bit ADC and an I2C/SMBus interface. It measures the voltage
+ across an external shunt resistor and the input bus voltage, and stores the
+ results in an internal FIFO.
+
+ Datasheet:
+ https://www.analog.com/en/products/max40080.html
+
+properties:
+ compatible:
+ const: adi,max40080
+
+ reg:
+ maxItems: 1
+
+ "#io-channel-cells":
+ const: 1
+
+ vdd-supply:
+ description: Power supply for the device.
+
+ interrupts:
+ maxItems: 1
+ description: ALERT output for overcurrent/undervoltage threshold events.
+
+ shunt-resistor-micro-ohms:
+ description:
+ Value of the current-sense shunt resistor connected between the IN+ and
+ IN- inputs. Used to scale the reported current.
+
+required:
+ - compatible
+ - reg
+ - shunt-resistor-micro-ohms
+
+additionalProperties: false
+
+examples:
+ - |
+ i2c {
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ adc@20 {
+ compatible = "adi,max40080";
+ reg = <0x20>;
+ #io-channel-cells = <1>;
+ shunt-resistor-micro-ohms = <100000>;
+ };
+ };
diff --git a/MAINTAINERS b/MAINTAINERS
index e087673237636..e1170230c4bb2 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -28893,3 +28893,11 @@ S: Buried alive in reporters
T: git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
F: *
F: */
+
+MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER
+M: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
+M: Stefan Popa <stefan.popa@analog.com>
+L: linux-iio@vger.kernel.org
+S: Supported
+W: https://ez.analog.com/linux-software-drivers
+F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
2026-07-30 13:17 [PATCH v6 0/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
@ 2026-07-30 13:17 ` Stefan Popa
2026-07-30 13:29 ` sashiko-bot
` (3 more replies)
1 sibling, 4 replies; 12+ messages in thread
From: Stefan Popa @ 2026-07-30 13:17 UTC (permalink / raw)
To: linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, dlechner, ciprian.hegbeli,
siratul.islam, u.kleine-koenig, joshua.crofts1, Stefan Popa
The MAX40080 is a bidirectional current-sense amplifier with an
integrated 12-bit ADC and an I2C/SMBus interface. It measures the
voltage across an external shunt resistor and the input bus voltage,
storing the results in an internal FIFO.
Add a direct-mode IIO driver exposing the current and voltage channels
with raw, scale and hardware-gain attributes, a configurable
oversampling (digital averaging) ratio, and PEC-protected register
access. The current scale is derived from the shunt resistor value
described in the device tree.
The driver operates in single-measurement mode: each raw read triggers
an on-demand conversion via SMBus Quick Command and returns a matched
current/voltage pair. This avoids the latency and complexity of the
continuous FIFO mode while ensuring each read reflects the current
state. The two selectable current-sense ranges are exposed through
scale/scale_available.
Continuous FIFO buffering, threshold events and the alert interrupt are
intentionally left out of this initial submission and may be added
later.
Co-developed-by: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
Signed-off-by: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
Signed-off-by: Stefan Popa <stefan.popa@analog.com>
---
MAINTAINERS | 1 +
drivers/iio/adc/Kconfig | 11 +
drivers/iio/adc/Makefile | 1 +
drivers/iio/adc/max40080.c | 574 +++++++++++++++++++++++++++++++++++++
4 files changed, 587 insertions(+)
create mode 100644 drivers/iio/adc/max40080.c
diff --git a/MAINTAINERS b/MAINTAINERS
index e1170230c4bb2..467e6bbf72cde 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -28901,3 +28901,4 @@ L: linux-iio@vger.kernel.org
S: Supported
W: https://ez.analog.com/linux-software-drivers
F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
+F: drivers/iio/adc/max40080.c
diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
index 58da8255525e4..ebdbad81fbbd6 100644
--- a/drivers/iio/adc/Kconfig
+++ b/drivers/iio/adc/Kconfig
@@ -1907,3 +1907,14 @@ config XILINX_AMS
xilinx-ams.
endmenu
+
+config MAX40080
+ tristate "Analog Devices MAX40080 Current Sense Amplifier"
+ depends on I2C
+ help
+ Say yes here to build support for the Analog Devices MAX40080
+ bidirectional current-sense amplifier with a 12-bit ADC and an I2C
+ interface.
+
+ To compile this driver as a module, choose M here: the module will be
+ called max40080.
diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
index 7cc8f9a12f763..9245a337dd935 100644
--- a/drivers/iio/adc/Makefile
+++ b/drivers/iio/adc/Makefile
@@ -166,3 +166,4 @@ obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
obj-$(CONFIG_XILINX_AMS) += xilinx-ams.o
xilinx-xadc-y := xilinx-xadc-core.o xilinx-xadc-events.o
obj-$(CONFIG_XILINX_XADC) += xilinx-xadc.o
+obj-$(CONFIG_MAX40080) += max40080.o
diff --git a/drivers/iio/adc/max40080.c b/drivers/iio/adc/max40080.c
new file mode 100644
index 0000000000000..82572536b704d
--- /dev/null
+++ b/drivers/iio/adc/max40080.c
@@ -0,0 +1,574 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * MAX40080 Digital Current-Sense Amplifier driver
+ *
+ * Copyright 2026 Analog Devices, Inc.
+ *
+ * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX40080.pdf
+ */
+
+#include <linux/bitfield.h>
+#include <linux/bitops.h>
+#include <linux/cleanup.h>
+#include <linux/i2c.h>
+#include <linux/iopoll.h>
+#include <linux/math64.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/property.h>
+#include <linux/unaligned.h>
+#include <linux/units.h>
+
+#include <linux/iio/iio.h>
+
+#define MAX40080_REG_CFG 0x00
+#define MAX40080_CFG_MODE_MSK GENMASK(2, 0)
+#define MAX40080_CFG_PEC_EN_MSK BIT(5)
+#define MAX40080_CFG_RANGE_MSK BIT(6)
+#define MAX40080_CFG_FILTER_MSK GENMASK(14, 12)
+
+#define MAX40080_REG_FIFO_CFG 0x0A
+#define MAX40080_FIFO_CFG_STORE_IV_MSK GENMASK(1, 0)
+
+#define MAX40080_REG_IV 0x10
+/* Current is a 13-bit two's-complement value (magnitude + sign bit). */
+#define MAX40080_IV_I_MSK GENMASK(12, 0)
+#define MAX40080_IV_I_SIGN_BIT 12
+#define MAX40080_IV_V_MAG_MSK GENMASK(27, 16)
+#define MAX40080_IV_VALID_MSK BIT(31)
+
+/* CFG.mode field values. */
+#define MAX40080_CFG_MODE_STDBY 0x00
+#define MAX40080_CFG_MODE_SINGLE 0x02
+
+/* CFG.range field values. */
+#define MAX40080_CFG_RANGE_50MV 0
+#define MAX40080_CFG_RANGE_10MV 1
+
+/* FIFO_CFG.store_iv field values. */
+#define MAX40080_FIFO_CFG_STORE_I_V 0x02
+
+#define MAX40080_ADC_RES_BITS 12
+#define MAX40080_INTER_VREF_MV 1250
+#define MAX40080_V_BUFF_GAIN 30
+#define MAX40080_CSA_50MV_GAIN 25
+#define MAX40080_CSA_10MV_GAIN 125
+
+/*
+ * The RANGE field (CFG bit 6) selects one of two current-sense full-scale
+ * ranges (the MAX40080 supports exactly two: +/-50 mV and +/-10 mV). Indexed
+ * by the CFG.range field value.
+ */
+static const int max40080_csa_gain[] = {
+ [MAX40080_CFG_RANGE_50MV] = MAX40080_CSA_50MV_GAIN,
+ [MAX40080_CFG_RANGE_10MV] = MAX40080_CSA_10MV_GAIN,
+};
+
+#define MAX40080_NUM_RANGES ARRAY_SIZE(max40080_csa_gain)
+
+struct max40080_state {
+ struct i2c_client *client;
+ /* Serializes read-modify-write access to the CFG register. */
+ struct mutex lock;
+ u32 shunt_resistor_uohm;
+ /* Cached configuration: the selected RANGE index and oversampling ratio. */
+ unsigned int range;
+ int oversampling_ratio;
+ /*
+ * Precomputed current scale (mA per code) for each RANGE setting, as
+ * {integer, nano} pairs for IIO_VAL_INT_PLUS_NANO. The range is
+ * selected by writing the corresponding scale.
+ */
+ int current_scale[MAX40080_NUM_RANGES][2];
+};
+
+static const int max40080_oversampling_avail[] = { 1, 8, 16, 32, 64, 128 };
+
+static int max40080_update_bits(struct max40080_state *st, u8 reg,
+ u16 mask, u16 val)
+{
+ int tmp;
+
+ tmp = i2c_smbus_read_word_data(st->client, reg);
+ if (tmp < 0)
+ return tmp;
+
+ tmp = (tmp & ~mask) | (val & mask);
+
+ return i2c_smbus_write_word_data(st->client, reg, tmp);
+}
+
+/*
+ * In single-measurement mode the device sits idle until it receives an SMBus
+ * Quick Command, then performs exactly one current and one voltage conversion
+ * and returns to idle. Triggering on demand this way (rather than running the
+ * FIFO continuously in active mode) means each read returns a fresh, coherent
+ * current/voltage pair instead of the oldest queued FIFO entry.
+ */
+static int max40080_trigger_measurement(struct max40080_state *st)
+{
+ return i2c_smbus_xfer(st->client->adapter, st->client->addr,
+ st->client->flags, I2C_SMBUS_WRITE, 0,
+ I2C_SMBUS_QUICK, NULL);
+}
+
+/*
+ * A single measurement holds the matched current/voltage pair in one 32-bit
+ * word (MAX40080_REG_IV). Reading all four bytes in one transaction returns
+ * both from the same conversion; reading the separate current (0x0C) and
+ * voltage (0x0E) registers would decorrelate the two channels.
+ *
+ * Unlike the word accesses used elsewhere, this is a plain I2C block read: the
+ * SMBus layer does not append or verify a PEC byte for it even when PEC is
+ * otherwise enabled for the device, so this transfer is not PEC protected.
+ */
+static int max40080_read_iv_once(struct max40080_state *st, u32 *iv)
+{
+ u8 buf[4];
+ int ret;
+
+ ret = i2c_smbus_read_i2c_block_data(st->client, MAX40080_REG_IV,
+ sizeof(buf), buf);
+ if (ret < 0)
+ return ret;
+ if (ret != sizeof(buf))
+ return -EIO;
+
+ *iv = get_unaligned_le32(buf);
+
+ return 0;
+}
+
+static int max40080_read_iv(struct max40080_state *st, u32 *iv)
+{
+ u32 tmp = 0;
+ int ret, io_ret;
+
+ guard(mutex)(&st->lock);
+
+ ret = max40080_trigger_measurement(st);
+ if (ret < 0)
+ return ret;
+
+ /*
+ * Wait for the conversion to complete by polling the FIFO valid bit
+ * (or bail out on an I2C error). Polling the device's own status makes
+ * this independent of the actual conversion time, which varies with the
+ * oversampling ratio and the bus speed. The timeout is only a safety
+ * ceiling: the worst case is the maximum 128x averaging on both the
+ * current and voltage channels at the slowest 15 ksps base rate plus the
+ * inter-channel switching time, i.e. roughly 20 ms; 50 ms leaves ample
+ * margin.
+ */
+ ret = read_poll_timeout(max40080_read_iv_once, io_ret,
+ io_ret || (tmp & MAX40080_IV_VALID_MSK),
+ 500, 50000, false, st, &tmp);
+ /*
+ * Propagate the last-read value even on timeout so the caller can
+ * inspect it for debugging.
+ */
+ *iv = tmp;
+ if (ret)
+ return ret;
+
+ return io_ret;
+}
+
+static int max40080_get_current(struct max40080_state *st, int *val)
+{
+ u32 iv = 0;
+ int ret;
+
+ ret = max40080_read_iv(st, &iv);
+ if (ret)
+ return ret;
+
+ *val = sign_extend32(FIELD_GET(MAX40080_IV_I_MSK, iv),
+ MAX40080_IV_I_SIGN_BIT);
+
+ return 0;
+}
+
+static int max40080_get_voltage(struct max40080_state *st, int *val)
+{
+ u32 iv = 0;
+ int ret;
+
+ ret = max40080_read_iv(st, &iv);
+ if (ret)
+ return ret;
+
+ *val = FIELD_GET(MAX40080_IV_V_MAG_MSK, iv);
+
+ return 0;
+}
+
+static int max40080_set_range(struct max40080_state *st, unsigned int range)
+{
+ int ret;
+
+ ret = max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_RANGE_MSK,
+ FIELD_PREP(MAX40080_CFG_RANGE_MSK, range));
+ if (ret)
+ return ret;
+
+ st->range = range;
+
+ return 0;
+}
+
+/*
+ * Precompute the current scale (mA per code) for each RANGE setting as
+ * {integer, nano} pairs. The shunt drop for a full-scale code is
+ * Vref[mV] / ((1 << ADC_RES_BITS) * gain)
+ * and current = Vshunt / Rshunt, so with Rshunt in micro-ohms the scale in
+ * mA/code is
+ * Vref[mV] * NANO * MICRO / ((1 << ADC_RES_BITS) * gain * Rshunt[uohm])
+ * expressed as an integer part plus a nano fractional part.
+ */
+static void max40080_calc_current_scale(struct max40080_state *st)
+{
+ u32 rem;
+ u64 tmp;
+
+ for (unsigned int i = 0; i < MAX40080_NUM_RANGES; i++) {
+ tmp = (u64)MAX40080_INTER_VREF_MV * NANO * MICRO;
+ tmp = div64_u64(tmp, (u64)(1 << MAX40080_ADC_RES_BITS) * max40080_csa_gain[i] *
+ st->shunt_resistor_uohm);
+ st->current_scale[i][0] = div_u64_rem(tmp, NANO, &rem);
+ st->current_scale[i][1] = rem;
+ }
+}
+
+/*
+ * max40080_oversampling_avail[] is ordered so that its index is the FILTER
+ * field value (index 0 = no averaging, index 1 = 8x, ...). Return that index
+ * for an exact match, or -EINVAL for a value that is not on the list.
+ */
+static int max40080_oversampling_to_filter(int val)
+{
+ for (unsigned int i = 0; i < ARRAY_SIZE(max40080_oversampling_avail); i++) {
+ if (max40080_oversampling_avail[i] == val)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int max40080_set_oversampling_ratio(struct max40080_state *st, int val)
+{
+ int filter;
+ int ret;
+
+ filter = max40080_oversampling_to_filter(val);
+ if (filter < 0)
+ return filter;
+
+ ret = max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_FILTER_MSK,
+ FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter));
+ if (ret)
+ return ret;
+
+ st->oversampling_ratio = val;
+
+ return 0;
+}
+
+static int max40080_read_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int *val, int *val2, long mask)
+{
+ struct max40080_state *st = iio_priv(indio_dev);
+ unsigned int range;
+ int ret;
+
+ switch (mask) {
+ case IIO_CHAN_INFO_RAW:
+ if (chan->type == IIO_CURRENT) {
+ ret = max40080_get_current(st, val);
+ if (ret)
+ return ret;
+ } else if (chan->type == IIO_VOLTAGE) {
+ ret = max40080_get_voltage(st, val);
+ if (ret)
+ return ret;
+ }
+ return IIO_VAL_INT;
+ case IIO_CHAN_INFO_SCALE:
+ if (chan->type == IIO_CURRENT) {
+ /*
+ * The selectable current-sense range is exposed through
+ * scale: each RANGE setting has its own precomputed
+ * mA-per-code value. Userspace picks the range by writing
+ * the matching scale.
+ *
+ * Take a local copy of range to ensure val and val2 come
+ * from the same setting even if a concurrent write changes
+ * st->range between the two accesses.
+ */
+ range = st->range;
+ *val = st->current_scale[range][0];
+ *val2 = st->current_scale[range][1];
+ return IIO_VAL_INT_PLUS_NANO;
+ }
+ /* voltage[mV] = raw * Vref[mV] * buffer_gain / (1 << ADC_RES_BITS) */
+ *val = MAX40080_INTER_VREF_MV * MAX40080_V_BUFF_GAIN;
+ *val2 = MAX40080_ADC_RES_BITS;
+ return IIO_VAL_FRACTIONAL_LOG2;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ *val = st->oversampling_ratio;
+ return IIO_VAL_INT;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int max40080_write_raw(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ int val, int val2, long mask)
+{
+ struct max40080_state *st = iio_priv(indio_dev);
+
+ guard(mutex)(&st->lock);
+
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ /* Only the current channel has a selectable range/scale. */
+ if (chan->type != IIO_CURRENT)
+ return -EINVAL;
+
+ for (unsigned int i = 0; i < MAX40080_NUM_RANGES; i++) {
+ if (val == st->current_scale[i][0] &&
+ val2 == st->current_scale[i][1])
+ return max40080_set_range(st, i);
+ }
+
+ return -EINVAL;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ return max40080_set_oversampling_ratio(st, val);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int max40080_write_raw_get_fmt(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ long mask)
+{
+ switch (mask) {
+ case IIO_CHAN_INFO_SCALE:
+ return IIO_VAL_INT_PLUS_NANO;
+ default:
+ return IIO_VAL_INT;
+ }
+}
+
+static int max40080_read_avail(struct iio_dev *indio_dev,
+ struct iio_chan_spec const *chan,
+ const int **vals, int *type, int *length,
+ long info)
+{
+ struct max40080_state *st = iio_priv(indio_dev);
+
+ switch (info) {
+ case IIO_CHAN_INFO_SCALE:
+ if (chan->type != IIO_CURRENT)
+ return -EINVAL;
+
+ *vals = (int *)st->current_scale;
+ *length = MAX40080_NUM_RANGES * 2;
+ *type = IIO_VAL_INT_PLUS_NANO;
+ return IIO_AVAIL_LIST;
+ case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
+ *vals = max40080_oversampling_avail;
+ *length = ARRAY_SIZE(max40080_oversampling_avail);
+ *type = IIO_VAL_INT;
+ return IIO_AVAIL_LIST;
+ default:
+ return -EINVAL;
+ }
+}
+
+static int max40080_reg_access(struct iio_dev *indio_dev, unsigned int reg,
+ unsigned int write_val, unsigned int *read_val)
+{
+ struct max40080_state *st = iio_priv(indio_dev);
+ int val;
+
+ if (read_val) {
+ val = i2c_smbus_read_word_data(st->client, reg);
+ if (val < 0)
+ return val;
+
+ *read_val = val;
+
+ return 0;
+ }
+
+ return i2c_smbus_write_word_data(st->client, reg, write_val);
+}
+
+static const struct iio_info max40080_info = {
+ .read_raw = max40080_read_raw,
+ .write_raw = max40080_write_raw,
+ .write_raw_get_fmt = max40080_write_raw_get_fmt,
+ .read_avail = max40080_read_avail,
+ .debugfs_reg_access = &max40080_reg_access,
+};
+
+static const struct iio_chan_spec max40080_channels[] = {
+ {
+ .type = IIO_CURRENT,
+ .indexed = 1,
+ .channel = 0,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
+ .info_mask_shared_by_all_available =
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
+ },
+ {
+ .type = IIO_VOLTAGE,
+ .indexed = 1,
+ .channel = 0,
+ .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
+ BIT(IIO_CHAN_INFO_SCALE),
+ .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
+ .info_mask_shared_by_all_available =
+ BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
+ },
+};
+
+/*
+ * Configure the device from the cached state. The device powers up in standby
+ * with PEC enabled (CFG POR = 0x0060), so PEC is kept enabled throughout.
+ */
+static int max40080_init(struct max40080_state *st)
+{
+ u16 fifo_cfg, cfg;
+ int ret, filter;
+
+ filter = max40080_oversampling_to_filter(st->oversampling_ratio);
+ if (filter < 0)
+ return filter;
+
+ /*
+ * Put the device in standby before (re)configuring the FIFO: the FIFO
+ * configuration register can only be written while the device is not
+ * converting.
+ */
+ cfg = FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_STDBY) |
+ FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1);
+
+ ret = i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
+ if (ret)
+ return ret;
+
+ /* Store a matched current+voltage pair per conversion. */
+ fifo_cfg = FIELD_PREP(MAX40080_FIFO_CFG_STORE_IV_MSK, MAX40080_FIFO_CFG_STORE_I_V);
+
+ ret = i2c_smbus_write_word_data(st->client, MAX40080_REG_FIFO_CFG,
+ fifo_cfg);
+ if (ret)
+ return ret;
+
+ /*
+ * Use single-measurement mode: the device stays idle and converts once
+ * per SMBus Quick Command (see max40080_trigger_measurement()), so each
+ * read returns a fresh sample rather than a queued FIFO entry.
+ */
+ cfg = FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_SINGLE) |
+ FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1) |
+ FIELD_PREP(MAX40080_CFG_RANGE_MSK, st->range) |
+ FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter);
+
+ return i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
+}
+
+static int max40080_probe(struct i2c_client *client)
+{
+ const char *propname = "shunt-resistor-micro-ohms";
+ struct device *dev = &client->dev;
+ struct iio_dev *indio_dev;
+ struct max40080_state *st;
+ int ret;
+
+ /*
+ * The device powers up with PEC enabled (CFG POR = 0x0060) and rejects
+ * unprotected transactions, so PEC support is mandatory, along with word
+ * access, the I2C block read used for the current/voltage pair, and the
+ * Quick Command used to trigger a conversion.
+ */
+ if (!i2c_check_functionality(client->adapter,
+ I2C_FUNC_SMBUS_WORD_DATA |
+ I2C_FUNC_SMBUS_I2C_BLOCK |
+ I2C_FUNC_SMBUS_QUICK |
+ I2C_FUNC_SMBUS_PEC))
+ return -EOPNOTSUPP;
+
+ client->flags |= I2C_CLIENT_PEC;
+
+ indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
+ if (!indio_dev)
+ return -ENOMEM;
+
+ st = iio_priv(indio_dev);
+ st->client = client;
+
+ ret = devm_mutex_init(dev, &st->lock);
+ if (ret)
+ return ret;
+
+ ret = device_property_read_u32(dev, propname, &st->shunt_resistor_uohm);
+ if (ret)
+ return dev_err_probe(dev, ret, "can't read %s\n", propname);
+ if (!st->shunt_resistor_uohm)
+ return dev_err_probe(dev, -EINVAL, "%s must be non-zero\n",
+ propname);
+
+ max40080_calc_current_scale(st);
+
+ /* Defaults: 50 mV range, no averaging. */
+ st->range = MAX40080_CFG_RANGE_50MV;
+ st->oversampling_ratio = 1;
+
+ indio_dev->name = "max40080";
+ indio_dev->info = &max40080_info;
+ indio_dev->modes = INDIO_DIRECT_MODE;
+ indio_dev->channels = max40080_channels;
+ indio_dev->num_channels = ARRAY_SIZE(max40080_channels);
+
+ ret = max40080_init(st);
+ if (ret)
+ return ret;
+
+ return devm_iio_device_register(dev, indio_dev);
+}
+
+static const struct i2c_device_id max40080_i2c_ids[] = {
+ { .name = "max40080" },
+ { }
+};
+MODULE_DEVICE_TABLE(i2c, max40080_i2c_ids);
+
+static const struct of_device_id max40080_of_match[] = {
+ { .compatible = "adi,max40080" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, max40080_of_match);
+
+static struct i2c_driver max40080_driver = {
+ .driver = {
+ .name = "max40080",
+ .of_match_table = max40080_of_match,
+ },
+ .probe = max40080_probe,
+ .id_table = max40080_i2c_ids,
+};
+module_i2c_driver(max40080_driver);
+
+MODULE_AUTHOR("Ciprian Hegbeli <ciprian.hegbeli@analog.com>");
+MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
+MODULE_DESCRIPTION("Analog Devices MAX40080 current-sense amplifier driver");
+MODULE_LICENSE("GPL");
--
2.53.0
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
@ 2026-07-30 13:25 ` sashiko-bot
2026-07-30 18:42 ` Siratul Islam
` (2 subsequent siblings)
3 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-07-30 13:25 UTC (permalink / raw)
To: Stefan Popa; +Cc: robh, devicetree, linux-hwmon, conor+dt
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] The MAINTAINERS entry for 'MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER' is incorrectly appended to the very end of the file, placing it after the catch-all 'THE REST' section.
--
--- Patch [1]: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 ---
commit 4f01f75006cab658c6276517a4096c41a91e9b17
Author: Stefan Popa <stefan.popa@analog.com>
dt-bindings: iio: adc: add adi,max40080
This commit adds device tree bindings for the Analog Devices MAX40080
bidirectional current-sense amplifier. It outlines the required properties
and creates a new maintainer entry for the driver.
> diff --git a/MAINTAINERS b/MAINTAINERS
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
[ ... ]
> @@ -27447,3 +27447,11 @@ S: Buried alive in reporters
> T: git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> F: *
> F: */
> +
> +MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER
[Severity: Low]
This isn't a bug, but should this new entry be sorted alphabetically within
the file? Appending it at the very bottom places it after the catch-all
fallback section, which violates the standard sorting convention.
> +M: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
> +M: Stefan Popa <stefan.popa@analog.com>
> +L: linux-iio@vger.kernel.org
> +S: Supported
> +W: https://ez.analog.com/linux-software-drivers
> +F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260730131738.822405-1-stefan.popa@analog.com?part=1
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
@ 2026-07-30 13:29 ` sashiko-bot
2026-07-31 6:00 ` Siratul Islam
` (2 subsequent siblings)
3 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-07-30 13:29 UTC (permalink / raw)
To: Stefan Popa; +Cc: linux-hwmon, devicetree, robh, conor+dt
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Stale measurements: each raw sysfs read returns the previous conversion's data due to an un-flushed FIFO.
- [Medium] Missing READ_ONCE() for `st->range` in `max40080_read_raw()` can lead to data races and torn scale values.
--
--- Patch [2]: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver ---
commit 557f2827eeb4edfa93bf2f63edf6a0664453dad6
Author: Stefan Popa <stefan.popa@analog.com>
iio: adc: add MAX40080 current-sense amplifier driver
The patch adds a direct-mode IIO driver for the MAX40080 current-sense
amplifier, exposing current and voltage channels with configurable
oversampling.
> diff --git a/drivers/iio/adc/max40080.c b/drivers/iio/adc/max40080.c
> new file mode 100644
> index 0000000000000..82572536b704d
> --- /dev/null
> +++ b/drivers/iio/adc/max40080.c
[ ... ]
> +static int max40080_read_iv(struct max40080_state *st, u32 *iv)
> +{
> + u32 tmp = 0;
> + int ret, io_ret;
> +
> + guard(mutex)(&st->lock);
> +
> + ret = max40080_trigger_measurement(st);
> + if (ret < 0)
> + return ret;
> +
> + /*
> + * Wait for the conversion to complete by polling the FIFO valid bit
> + * (or bail out on an I2C error). Polling the device's own status makes
> + * this independent of the actual conversion time, which varies with the
> + * oversampling ratio and the bus speed. The timeout is only a safety
> + * ceiling: the worst case is the maximum 128x averaging on both the
> + * current and voltage channels at the slowest 15 ksps base rate plus the
> + * inter-channel switching time, i.e. roughly 20 ms; 50 ms leaves ample
> + * margin.
> + */
> + ret = read_poll_timeout(max40080_read_iv_once, io_ret,
> + io_ret || (tmp & MAX40080_IV_VALID_MSK),
> + 500, 50000, false, st, &tmp);
[Severity: High]
Does this polling loop read a stale sample?
Writing the SINGLE mode configuration in max40080_init() triggers an initial
conversion, leaving a sample in the FIFO. Because the FIFO already contains
this unread sample, the read_poll_timeout() loop here might instantly succeed
and pop the stale sample before the newly triggered measurement has a chance
to complete.
This would cause each sysfs read to return the previous conversion's data.
> + /*
> + * Propagate the last-read value even on timeout so the caller can
> + * inspect it for debugging.
> + */
> + *iv = tmp;
> + if (ret)
> + return ret;
> +
> + return io_ret;
> +}
[ ... ]
> +static int max40080_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct max40080_state *st = iio_priv(indio_dev);
> + unsigned int range;
> + int ret;
> +
> + switch (mask) {
[ ... ]
> + case IIO_CHAN_INFO_SCALE:
> + if (chan->type == IIO_CURRENT) {
> + /*
> + * The selectable current-sense range is exposed through
> + * scale: each RANGE setting has its own precomputed
> + * mA-per-code value. Userspace picks the range by writing
> + * the matching scale.
> + *
> + * Take a local copy of range to ensure val and val2 come
> + * from the same setting even if a concurrent write changes
> + * st->range between the two accesses.
> + */
> + range = st->range;
> + *val = st->current_scale[range][0];
> + *val2 = st->current_scale[range][1];
[Severity: Medium]
Could this result in a torn read if st->range is modified concurrently?
Without a READ_ONCE() for st->range, the compiler might optimize away the local
variable and reload st->range directly from memory between reading
st->current_scale[range][0] and st->current_scale[range][1].
If max40080_write_raw() modifies st->range concurrently, this could return an
invalid combination of integer and fractional components.
> + return IIO_VAL_INT_PLUS_NANO;
> + }
--
Sashiko AI review · https://sashiko.dev/#/patchset/20260730131738.822405-1-stefan.popa@analog.com?part=2
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-07-30 13:25 ` sashiko-bot
@ 2026-07-30 18:42 ` Siratul Islam
2026-07-31 7:04 ` Krzysztof Kozlowski
2026-08-01 15:38 ` David Lechner
3 siblings, 0 replies; 12+ messages in thread
From: Siratul Islam @ 2026-07-30 18:42 UTC (permalink / raw)
To: Stefan Popa, linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, dlechner, ciprian.hegbeli,
u.kleine-koenig, joshua.crofts1, Stefan Popa
July 30, 2026 at 7:17 PM, "Stefan Popa" <stefan.popa@analog.com> wrote:
>
> Add device tree bindings for the Analog Devices MAX40080 bidirectional
> current-sense amplifier with a 12-bit ADC and an I2C/SMBus interface.
>
> Signed-off-by: Stefan Popa <stefan.popa@analog.com>
> ---
...
> +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause)
> +%YAML 1.2
> +---
> +$id: http://devicetree.org/schemas/iio/adc/adi,max40080.yaml#
> +$schema: http://devicetree.org/meta-schemas/core.yaml#
> +
> +title: Analog Devices MAX40080 bidirectional current-sense amplifier
> +
> +maintainers:
> + - Ciprian Hegbeli <ciprian.hegbeli@analog.com>
> + - Stefan Popa <stefan.popa@analog.com>
> +
> +description: |
> + The MAX40080 is a high-precision, bidirectional current-sense amplifier with
> + an integrated 12-bit ADC and an I2C/SMBus interface. It measures the voltage
> + across an external shunt resistor and the input bus voltage, and stores the
> + results in an internal FIFO.
> +
> + Datasheet:
> + https://www.analog.com/en/products/max40080.html
About this link, there were some discussion in the previous version where
Jonathan suggested a more standard format (https://www.analog.com/max40080).
But you seem to have kept the same url as before. I'll leave it up to him.
> +
> +properties:
> + compatible:
> + const: adi,max40080
> +
...
> +
> +MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER
> +M: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
> +M: Stefan Popa <stefan.popa@analog.com>
> +L: linux-iio@vger.kernel.org
> +S: Supported
> +W: https://ez.analog.com/linux-software-drivers
> +F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
> --
> 2.53.0
>
Please maintain alphabetical order for the MAINTAINERS entry,
instead of adding it to the end of the file.
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:29 ` sashiko-bot
@ 2026-07-31 6:00 ` Siratul Islam
2026-08-01 16:05 ` David Lechner
2026-08-10 19:21 ` Andy Shevchenko
3 siblings, 0 replies; 12+ messages in thread
From: Siratul Islam @ 2026-07-31 6:00 UTC (permalink / raw)
To: Stefan Popa
Cc: linux-hwmon, linux-iio, devicetree, linux-kernel, jic23, andy,
nuno.sa, linux, robh, krzk+dt, conor+dt, dlechner,
ciprian.hegbeli, u.kleine-koenig, joshua.crofts1
On Thu, 2026-07-30 at 16:17 +0300, Stefan Popa wrote:
> The MAX40080 is a bidirectional current-sense amplifier with an
> integrated 12-bit ADC and an I2C/SMBus interface. It measures the
> voltage across an external shunt resistor and the input bus voltage,
> storing the results in an internal FIFO.
>
...
> +
> +#define MAX40080_REG_FIFO_CFG 0x0A
> +#define MAX40080_FIFO_CFG_STORE_IV_MSK GENMASK(1, 0)
> +
> +#define MAX40080_REG_IV 0x10
> +/* Current is a 13-bit two's-complement value (magnitude + sign bit). */
> +#define MAX40080_IV_I_MSK GENMASK(12, 0)
> +#define MAX40080_IV_I_SIGN_BIT 12
> +#define MAX40080_IV_V_MAG_MSK GENMASK(27, 16)
> +#define MAX40080_IV_VALID_MSK BIT(31)
> +
> +/* CFG.mode field values. */
> +#define MAX40080_CFG_MODE_STDBY 0x00
> +#define MAX40080_CFG_MODE_SINGLE 0x02
> +
> +/* CFG.range field values. */
> +#define MAX40080_CFG_RANGE_50MV 0
> +#define MAX40080_CFG_RANGE_10MV 1
this and other "MV"s below should be mV(m lowercase) as
I suggested in the previous version.
> +
> +/* FIFO_CFG.store_iv field values. */
> +#define MAX40080_FIFO_CFG_STORE_I_V 0x02
I also had a question on why this is I_V and not IV like the rest of them?
> +
> +#define MAX40080_ADC_RES_BITS 12
> +#define MAX40080_INTER_VREF_MV 1250
> +#define MAX40080_V_BUFF_GAIN 30
> +#define MAX40080_CSA_50MV_GAIN 25
> +#define MAX40080_CSA_10MV_GAIN 125
...
Looks like you missed some of the reviews. If it was intentional,
please acknowledge that. It is a good idea to reply to emails
and discuss/confirm stuff before sending a new patch.
Also avoid sending --to mailing lists (linux-iio@vger.kernel.org).
Send to maintainers and Cc reviewers and lists.
--
Best regards,
Sirat
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-07-30 13:25 ` sashiko-bot
2026-07-30 18:42 ` Siratul Islam
@ 2026-07-31 7:04 ` Krzysztof Kozlowski
2026-07-31 7:06 ` Krzysztof Kozlowski
2026-08-01 15:38 ` David Lechner
3 siblings, 1 reply; 12+ messages in thread
From: Krzysztof Kozlowski @ 2026-07-31 7:04 UTC (permalink / raw)
To: Stefan Popa
Cc: linux-iio, linux-hwmon, devicetree, linux-kernel, jic23, andy,
nuno.sa, linux, robh, krzk+dt, conor+dt, dlechner,
ciprian.hegbeli, siratul.islam, u.kleine-koenig, joshua.crofts1
On Thu, Jul 30, 2026 at 04:17:37PM +0300, Stefan Popa wrote:
> + vdd-supply:
> + description: Power supply for the device.
> +
> + interrupts:
> + maxItems: 1
> + description: ALERT output for overcurrent/undervoltage threshold events.
> +
> + shunt-resistor-micro-ohms:
> + description:
> + Value of the current-sense shunt resistor connected between the IN+ and
> + IN- inputs. Used to scale the reported current.
> +
> +required:
> + - compatible
> + - reg
> + - shunt-resistor-micro-ohms
Supply is required
> +
> +additionalProperties: false
> +
> +examples:
> + - |
> + i2c {
> + #address-cells = <1>;
> + #size-cells = <0>;
> +
> + adc@20 {
> + compatible = "adi,max40080";
> + reg = <0x20>;
> + #io-channel-cells = <1>;
> + shunt-resistor-micro-ohms = <100000>;
> + };
> + };
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e087673237636..e1170230c4bb2 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -28893,3 +28893,11 @@ S: Buried alive in reporters
> T: git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
> F: *
> F: */
> +
> +MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER
Honestly, how did it appear at the end? Do you see any other
commit/patchset adding it there? No, so this should raise questions on
your side.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-31 7:04 ` Krzysztof Kozlowski
@ 2026-07-31 7:06 ` Krzysztof Kozlowski
0 siblings, 0 replies; 12+ messages in thread
From: Krzysztof Kozlowski @ 2026-07-31 7:06 UTC (permalink / raw)
To: Stefan Popa
Cc: linux-iio, linux-hwmon, devicetree, linux-kernel, jic23, andy,
nuno.sa, linux, robh, krzk+dt, conor+dt, dlechner,
ciprian.hegbeli, siratul.islam, u.kleine-koenig, joshua.crofts1
On 31/07/2026 09:04, Krzysztof Kozlowski wrote:
> On Thu, Jul 30, 2026 at 04:17:37PM +0300, Stefan Popa wrote:
>> + vdd-supply:
>> + description: Power supply for the device.
>> +
>> + interrupts:
>> + maxItems: 1
>> + description: ALERT output for overcurrent/undervoltage threshold events.
>> +
>> + shunt-resistor-micro-ohms:
>> + description:
>> + Value of the current-sense shunt resistor connected between the IN+ and
>> + IN- inputs. Used to scale the reported current.
>> +
>> +required:
>> + - compatible
>> + - reg
>> + - shunt-resistor-micro-ohms
>
> Supply is required
>
>> +
>> +additionalProperties: false
>> +
>> +examples:
>> + - |
>> + i2c {
>> + #address-cells = <1>;
>> + #size-cells = <0>;
>> +
>> + adc@20 {
>> + compatible = "adi,max40080";
>> + reg = <0x20>;
>> + #io-channel-cells = <1>;
>> + shunt-resistor-micro-ohms = <100000>;
>> + };
>> + };
>> diff --git a/MAINTAINERS b/MAINTAINERS
>> index e087673237636..e1170230c4bb2 100644
>> --- a/MAINTAINERS
>> +++ b/MAINTAINERS
>> @@ -28893,3 +28893,11 @@ S: Buried alive in reporters
>> T: git git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
>> F: *
>> F: */
>> +
>> +MAXIM MAX40080 CURRENT SENSE AMPLIFIER DRIVER
>
> Honestly, how did it appear at the end? Do you see any other
> commit/patchset adding it there? No, so this should raise questions on
> your side.
And this is quite different than v5 and nothing explains that change in
the changelog.
Use b4 prep to see what you did between revisions.
Best regards,
Krzysztof
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
` (2 preceding siblings ...)
2026-07-31 7:04 ` Krzysztof Kozlowski
@ 2026-08-01 15:38 ` David Lechner
3 siblings, 0 replies; 12+ messages in thread
From: David Lechner @ 2026-08-01 15:38 UTC (permalink / raw)
To: Stefan Popa, linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, ciprian.hegbeli, siratul.islam,
u.kleine-koenig, joshua.crofts1
On 7/30/26 8:17 AM, Stefan Popa wrote:
> Add device tree bindings for the Analog Devices MAX40080 bidirectional
> current-sense amplifier with a 12-bit ADC and an I2C/SMBus interface.
>
...
> +properties:
> + compatible:
> + const: adi,max40080
> +
> + reg:
> + maxItems: 1
> +
> + "#io-channel-cells":
> + const: 1
> +
> + vdd-supply:
> + description: Power supply for the device.
> +
> + interrupts:
> + maxItems: 1
> + description: ALERT output for overcurrent/undervoltage threshold events.
More precisely, it is `ALERT_`.
> +
> + shunt-resistor-micro-ohms:
> + description:
> + Value of the current-sense shunt resistor connected between the IN+ and
> + IN- inputs. Used to scale the reported current.
Datasheet says nothing about IN+/IN- pins. They are RS+/RS-.
> +
> +required:
> + - compatible
> + - reg
> + - shunt-resistor-micro-ohms
> +
> +additionalProperties: false
> +
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:29 ` sashiko-bot
2026-07-31 6:00 ` Siratul Islam
@ 2026-08-01 16:05 ` David Lechner
2026-08-10 19:21 ` Andy Shevchenko
3 siblings, 0 replies; 12+ messages in thread
From: David Lechner @ 2026-08-01 16:05 UTC (permalink / raw)
To: Stefan Popa, linux-iio
Cc: linux-hwmon, devicetree, linux-kernel, jic23, andy, nuno.sa,
linux, robh, krzk+dt, conor+dt, ciprian.hegbeli, siratul.islam,
u.kleine-koenig, joshua.crofts1
On 7/30/26 8:17 AM, Stefan Popa wrote:
> The MAX40080 is a bidirectional current-sense amplifier with an
> integrated 12-bit ADC and an I2C/SMBus interface. It measures the
> voltage across an external shunt resistor and the input bus voltage,
> storing the results in an internal FIFO.
>
> Add a direct-mode IIO driver exposing the current and voltage channels
> with raw, scale and hardware-gain attributes, a configurable
> oversampling (digital averaging) ratio, and PEC-protected register
> access. The current scale is derived from the shunt resistor value
> described in the device tree.
>
> The driver operates in single-measurement mode: each raw read triggers
> an on-demand conversion via SMBus Quick Command and returns a matched
> current/voltage pair. This avoids the latency and complexity of the
> continuous FIFO mode while ensuring each read reflects the current
> state. The two selectable current-sense ranges are exposed through
> scale/scale_available.
>
> Continuous FIFO buffering, threshold events and the alert interrupt are
> intentionally left out of this initial submission and may be added
> later.
>
> Co-developed-by: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
> Signed-off-by: Ciprian Hegbeli <ciprian.hegbeli@analog.com>
> Signed-off-by: Stefan Popa <stefan.popa@analog.com>
> ---
> MAINTAINERS | 1 +
> drivers/iio/adc/Kconfig | 11 +
> drivers/iio/adc/Makefile | 1 +
> drivers/iio/adc/max40080.c | 574 +++++++++++++++++++++++++++++++++++++
> 4 files changed, 587 insertions(+)
> create mode 100644 drivers/iio/adc/max40080.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index e1170230c4bb2..467e6bbf72cde 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -28901,3 +28901,4 @@ L: linux-iio@vger.kernel.org
> S: Supported
> W: https://ez.analog.com/linux-software-drivers
> F: Documentation/devicetree/bindings/iio/adc/adi,max40080.yaml
> +F: drivers/iio/adc/max40080.c
> diff --git a/drivers/iio/adc/Kconfig b/drivers/iio/adc/Kconfig
> index 58da8255525e4..ebdbad81fbbd6 100644
> --- a/drivers/iio/adc/Kconfig
> +++ b/drivers/iio/adc/Kconfig
> @@ -1907,3 +1907,14 @@ config XILINX_AMS
> xilinx-ams.
>
> endmenu
> +
> +config MAX40080
> + tristate "Analog Devices MAX40080 Current Sense Amplifier"
> + depends on I2C
> + help
> + Say yes here to build support for the Analog Devices MAX40080
> + bidirectional current-sense amplifier with a 12-bit ADC and an I2C
> + interface.
> +
> + To compile this driver as a module, choose M here: the module will be
> + called max40080.
> diff --git a/drivers/iio/adc/Makefile b/drivers/iio/adc/Makefile
> index 7cc8f9a12f763..9245a337dd935 100644
> --- a/drivers/iio/adc/Makefile
> +++ b/drivers/iio/adc/Makefile
> @@ -166,3 +166,4 @@ obj-$(CONFIG_VIPERBOARD_ADC) += viperboard_adc.o
> obj-$(CONFIG_XILINX_AMS) += xilinx-ams.o
> xilinx-xadc-y := xilinx-xadc-core.o xilinx-xadc-events.o
> obj-$(CONFIG_XILINX_XADC) += xilinx-xadc.o
> +obj-$(CONFIG_MAX40080) += max40080.o
> diff --git a/drivers/iio/adc/max40080.c b/drivers/iio/adc/max40080.c
> new file mode 100644
> index 0000000000000..82572536b704d
> --- /dev/null
> +++ b/drivers/iio/adc/max40080.c
> @@ -0,0 +1,574 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * MAX40080 Digital Current-Sense Amplifier driver
> + *
> + * Copyright 2026 Analog Devices, Inc.
> + *
> + * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX40080.pdf
> + */
> +
> +#include <linux/bitfield.h>
> +#include <linux/bitops.h>
> +#include <linux/cleanup.h>
> +#include <linux/i2c.h>
> +#include <linux/iopoll.h>
> +#include <linux/math64.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/property.h>
> +#include <linux/unaligned.h>
> +#include <linux/units.h>
> +
> +#include <linux/iio/iio.h>
> +
> +#define MAX40080_REG_CFG 0x00
> +#define MAX40080_CFG_MODE_MSK GENMASK(2, 0)
> +#define MAX40080_CFG_PEC_EN_MSK BIT(5)
> +#define MAX40080_CFG_RANGE_MSK BIT(6)
> +#define MAX40080_CFG_FILTER_MSK GENMASK(14, 12)
> +
> +#define MAX40080_REG_FIFO_CFG 0x0A
> +#define MAX40080_FIFO_CFG_STORE_IV_MSK GENMASK(1, 0)
> +
> +#define MAX40080_REG_IV 0x10
> +/* Current is a 13-bit two's-complement value (magnitude + sign bit). */
> +#define MAX40080_IV_I_MSK GENMASK(12, 0)
> +#define MAX40080_IV_I_SIGN_BIT 12
> +#define MAX40080_IV_V_MAG_MSK GENMASK(27, 16)
> +#define MAX40080_IV_VALID_MSK BIT(31)
> +
> +/* CFG.mode field values. */
> +#define MAX40080_CFG_MODE_STDBY 0x00
> +#define MAX40080_CFG_MODE_SINGLE 0x02
> +
> +/* CFG.range field values. */
> +#define MAX40080_CFG_RANGE_50MV 0
> +#define MAX40080_CFG_RANGE_10MV 1
> +
> +/* FIFO_CFG.store_iv field values. */
> +#define MAX40080_FIFO_CFG_STORE_I_V 0x02
> +
> +#define MAX40080_ADC_RES_BITS 12
> +#define MAX40080_INTER_VREF_MV 1250
> +#define MAX40080_V_BUFF_GAIN 30
> +#define MAX40080_CSA_50MV_GAIN 25
> +#define MAX40080_CSA_10MV_GAIN 125
> +
> +/*
> + * The RANGE field (CFG bit 6) selects one of two current-sense full-scale
> + * ranges (the MAX40080 supports exactly two: +/-50 mV and +/-10 mV). Indexed
> + * by the CFG.range field value.
> + */
> +static const int max40080_csa_gain[] = {
> + [MAX40080_CFG_RANGE_50MV] = MAX40080_CSA_50MV_GAIN,
> + [MAX40080_CFG_RANGE_10MV] = MAX40080_CSA_10MV_GAIN,
> +};
> +
> +#define MAX40080_NUM_RANGES ARRAY_SIZE(max40080_csa_gain)
> +
> +struct max40080_state {
> + struct i2c_client *client;
> + /* Serializes read-modify-write access to the CFG register. */
> + struct mutex lock;
> + u32 shunt_resistor_uohm;
> + /* Cached configuration: the selected RANGE index and oversampling ratio. */
> + unsigned int range;
> + int oversampling_ratio;
> + /*
> + * Precomputed current scale (mA per code) for each RANGE setting, as
> + * {integer, nano} pairs for IIO_VAL_INT_PLUS_NANO. The range is
> + * selected by writing the corresponding scale.
> + */
> + int current_scale[MAX40080_NUM_RANGES][2];
> +};
> +
> +static const int max40080_oversampling_avail[] = { 1, 8, 16, 32, 64, 128 };
> +
> +static int max40080_update_bits(struct max40080_state *st, u8 reg,
> + u16 mask, u16 val)
> +{
> + int tmp;
> +
> + tmp = i2c_smbus_read_word_data(st->client, reg);
> + if (tmp < 0)
> + return tmp;
> +
> + tmp = (tmp & ~mask) | (val & mask);
> +
> + return i2c_smbus_write_word_data(st->client, reg, tmp);
> +}
> +
> +/*
> + * In single-measurement mode the device sits idle until it receives an SMBus
> + * Quick Command, then performs exactly one current and one voltage conversion
> + * and returns to idle. Triggering on demand this way (rather than running the
> + * FIFO continuously in active mode) means each read returns a fresh, coherent
> + * current/voltage pair instead of the oldest queued FIFO entry.
> + */
> +static int max40080_trigger_measurement(struct max40080_state *st)
> +{
> + return i2c_smbus_xfer(st->client->adapter, st->client->addr,
> + st->client->flags, I2C_SMBUS_WRITE, 0,
> + I2C_SMBUS_QUICK, NULL);
> +}
> +
> +/*
> + * A single measurement holds the matched current/voltage pair in one 32-bit
> + * word (MAX40080_REG_IV). Reading all four bytes in one transaction returns
> + * both from the same conversion; reading the separate current (0x0C) and
> + * voltage (0x0E) registers would decorrelate the two channels.
> + *
> + * Unlike the word accesses used elsewhere, this is a plain I2C block read: the
> + * SMBus layer does not append or verify a PEC byte for it even when PEC is
> + * otherwise enabled for the device, so this transfer is not PEC protected.
> + */
> +static int max40080_read_iv_once(struct max40080_state *st, u32 *iv)
> +{
> + u8 buf[4];
> + int ret;
> +
> + ret = i2c_smbus_read_i2c_block_data(st->client, MAX40080_REG_IV,
> + sizeof(buf), buf);
> + if (ret < 0)
> + return ret;
> + if (ret != sizeof(buf))
> + return -EIO;
This can't return anything other than negative error or sizeof(buf),
so this is dead code.
> +
> + *iv = get_unaligned_le32(buf);
> +
> + return 0;
> +}
> +
> +static int max40080_read_iv(struct max40080_state *st, u32 *iv)
> +{
> + u32 tmp = 0;
initializing tmp is dead code.
> + int ret, io_ret;
> +
> + guard(mutex)(&st->lock);
> +
> + ret = max40080_trigger_measurement(st);
> + if (ret < 0)
> + return ret;
> +
> + /*
> + * Wait for the conversion to complete by polling the FIFO valid bit
> + * (or bail out on an I2C error). Polling the device's own status makes
> + * this independent of the actual conversion time, which varies with the
> + * oversampling ratio and the bus speed. The timeout is only a safety
> + * ceiling: the worst case is the maximum 128x averaging on both the
> + * current and voltage channels at the slowest 15 ksps base rate plus the
> + * inter-channel switching time, i.e. roughly 20 ms; 50 ms leaves ample
> + * margin.
> + */
> + ret = read_poll_timeout(max40080_read_iv_once, io_ret,
> + io_ret || (tmp & MAX40080_IV_VALID_MSK),
> + 500, 50000, false, st, &tmp);
> + /*
> + * Propagate the last-read value even on timeout so the caller can
> + * inspect it for debugging.
> + */
> + *iv = tmp;
> + if (ret)
> + return ret;
> +
> + return io_ret;
> +}
> +
> +static int max40080_get_current(struct max40080_state *st, int *val)
> +{
> + u32 iv = 0;
don't need to init here either.
> + int ret;
> +
> + ret = max40080_read_iv(st, &iv);
> + if (ret)
> + return ret;
> +
> + *val = sign_extend32(FIELD_GET(MAX40080_IV_I_MSK, iv),
> + MAX40080_IV_I_SIGN_BIT);
> +
> + return 0;
> +}
> +
> +static int max40080_get_voltage(struct max40080_state *st, int *val)
> +{
> + u32 iv = 0;
> + int ret;
> +
> + ret = max40080_read_iv(st, &iv);
> + if (ret)
> + return ret;
> +
> + *val = FIELD_GET(MAX40080_IV_V_MAG_MSK, iv);
> +
> + return 0;
> +}
> +
> +static int max40080_set_range(struct max40080_state *st, unsigned int range)
> +{
> + int ret;
> +
> + ret = max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_RANGE_MSK,
> + FIELD_PREP(MAX40080_CFG_RANGE_MSK, range));
> + if (ret)
> + return ret;
> +
> + st->range = range;
> +
> + return 0;
> +}
> +
> +/*
> + * Precompute the current scale (mA per code) for each RANGE setting as
> + * {integer, nano} pairs. The shunt drop for a full-scale code is
> + * Vref[mV] / ((1 << ADC_RES_BITS) * gain)
> + * and current = Vshunt / Rshunt, so with Rshunt in micro-ohms the scale in
> + * mA/code is
> + * Vref[mV] * NANO * MICRO / ((1 << ADC_RES_BITS) * gain * Rshunt[uohm])
> + * expressed as an integer part plus a nano fractional part.
> + */
> +static void max40080_calc_current_scale(struct max40080_state *st)
> +{
> + u32 rem;
> + u64 tmp;
> +
> + for (unsigned int i = 0; i < MAX40080_NUM_RANGES; i++) {
> + tmp = (u64)MAX40080_INTER_VREF_MV * NANO * MICRO;
> + tmp = div64_u64(tmp, (u64)(1 << MAX40080_ADC_RES_BITS) * max40080_csa_gain[i] *
BIT(MAX40080_ADC_RES_BITS)
> + st->shunt_resistor_uohm);
> + st->current_scale[i][0] = div_u64_rem(tmp, NANO, &rem);
> + st->current_scale[i][1] = rem;
> + }
> +}
> +
> +/*
> + * max40080_oversampling_avail[] is ordered so that its index is the FILTER
> + * field value (index 0 = no averaging, index 1 = 8x, ...). Return that index
> + * for an exact match, or -EINVAL for a value that is not on the list.
> + */
> +static int max40080_oversampling_to_filter(int val)
> +{
> + for (unsigned int i = 0; i < ARRAY_SIZE(max40080_oversampling_avail); i++) {
> + if (max40080_oversampling_avail[i] == val)
> + return i;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static int max40080_set_oversampling_ratio(struct max40080_state *st, int val)
> +{
> + int filter;
> + int ret;
> +
> + filter = max40080_oversampling_to_filter(val);
> + if (filter < 0)
> + return filter;
> +
> + ret = max40080_update_bits(st, MAX40080_REG_CFG, MAX40080_CFG_FILTER_MSK,
> + FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter));
> + if (ret)
> + return ret;
> +
> + st->oversampling_ratio = val;
> +
> + return 0;
> +}
> +
> +static int max40080_read_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int *val, int *val2, long mask)
> +{
> + struct max40080_state *st = iio_priv(indio_dev);
> + unsigned int range;
> + int ret;
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_RAW:
> + if (chan->type == IIO_CURRENT) {
> + ret = max40080_get_current(st, val);
> + if (ret)
> + return ret;
> + } else if (chan->type == IIO_VOLTAGE) {
> + ret = max40080_get_voltage(st, val);
> + if (ret)
> + return ret;
> + }
Looks like this could be a switch statement.
> + return IIO_VAL_INT;
> + case IIO_CHAN_INFO_SCALE:
> + if (chan->type == IIO_CURRENT) {
> + /*
> + * The selectable current-sense range is exposed through
> + * scale: each RANGE setting has its own precomputed
> + * mA-per-code value. Userspace picks the range by writing
> + * the matching scale.
> + *
> + * Take a local copy of range to ensure val and val2 come
> + * from the same setting even if a concurrent write changes
> + * st->range between the two accesses.
> + */
> + range = st->range;
Local range variable is adding more characters than it saves.
Not really needed IMHO.
> + *val = st->current_scale[range][0];
> + *val2 = st->current_scale[range][1];
> + return IIO_VAL_INT_PLUS_NANO;
> + }
> + /* voltage[mV] = raw * Vref[mV] * buffer_gain / (1 << ADC_RES_BITS) */
> + *val = MAX40080_INTER_VREF_MV * MAX40080_V_BUFF_GAIN;
> + *val2 = MAX40080_ADC_RES_BITS;
> + return IIO_VAL_FRACTIONAL_LOG2;
> + case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> + *val = st->oversampling_ratio;
> + return IIO_VAL_INT;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int max40080_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct max40080_state *st = iio_priv(indio_dev);
> +
> + guard(mutex)(&st->lock);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_SCALE:
> + /* Only the current channel has a selectable range/scale. */
> + if (chan->type != IIO_CURRENT)
> + return -EINVAL;
> +
> + for (unsigned int i = 0; i < MAX40080_NUM_RANGES; i++) {
> + if (val == st->current_scale[i][0] &&
> + val2 == st->current_scale[i][1])
> + return max40080_set_range(st, i);
> + }
> +
> + return -EINVAL;
> + case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> + return max40080_set_oversampling_ratio(st, val);
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int max40080_write_raw_get_fmt(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + long mask)
> +{
> + switch (mask) {
> + case IIO_CHAN_INFO_SCALE:
> + return IIO_VAL_INT_PLUS_NANO;
> + default:
> + return IIO_VAL_INT;
> + }
> +}
> +
> +static int max40080_read_avail(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + const int **vals, int *type, int *length,
> + long info)
> +{
> + struct max40080_state *st = iio_priv(indio_dev);
> +
> + switch (info) {
> + case IIO_CHAN_INFO_SCALE:
> + if (chan->type != IIO_CURRENT)
> + return -EINVAL;
> +
> + *vals = (int *)st->current_scale;
> + *length = MAX40080_NUM_RANGES * 2;
> + *type = IIO_VAL_INT_PLUS_NANO;
> + return IIO_AVAIL_LIST;
> + case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> + *vals = max40080_oversampling_avail;
> + *length = ARRAY_SIZE(max40080_oversampling_avail);
> + *type = IIO_VAL_INT;
> + return IIO_AVAIL_LIST;
> + default:
> + return -EINVAL;
> + }
> +}
> +
> +static int max40080_reg_access(struct iio_dev *indio_dev, unsigned int reg,
> + unsigned int write_val, unsigned int *read_val)
> +{
> + struct max40080_state *st = iio_priv(indio_dev);
> + int val;
> +
No range checking on reg?
> + if (read_val) {
> + val = i2c_smbus_read_word_data(st->client, reg);
> + if (val < 0)
> + return val;
> +
> + *read_val = val;
> +
> + return 0;
> + }
> +
> + return i2c_smbus_write_word_data(st->client, reg, write_val);
> +}
> +
> +static const struct iio_info max40080_info = {
> + .read_raw = max40080_read_raw,
> + .write_raw = max40080_write_raw,
> + .write_raw_get_fmt = max40080_write_raw_get_fmt,
> + .read_avail = max40080_read_avail,
> + .debugfs_reg_access = &max40080_reg_access,
> +};
> +
> +static const struct iio_chan_spec max40080_channels[] = {
> + {
> + .type = IIO_CURRENT,
> + .indexed = 1,
> + .channel = 0,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE),
> + .info_mask_separate_available = BIT(IIO_CHAN_INFO_SCALE),
> + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
> + .info_mask_shared_by_all_available =
> + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
> + },
> + {
> + .type = IIO_VOLTAGE,
> + .indexed = 1,
> + .channel = 0,
> + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
> + BIT(IIO_CHAN_INFO_SCALE),
> + .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
> + .info_mask_shared_by_all_available =
> + BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
> + },
> +};
> +
> +/*
> + * Configure the device from the cached state. The device powers up in standby
> + * with PEC enabled (CFG POR = 0x0060), so PEC is kept enabled throughout.
> + */
> +static int max40080_init(struct max40080_state *st)
> +{
> + u16 fifo_cfg, cfg;
> + int ret, filter;
> +
> + filter = max40080_oversampling_to_filter(st->oversampling_ratio);
> + if (filter < 0)
> + return filter;
> +
> + /*
> + * Put the device in standby before (re)configuring the FIFO: the FIFO
> + * configuration register can only be written while the device is not
> + * converting.
> + */
> + cfg = FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_STDBY) |
> + FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1);
> +
> + ret = i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
> + if (ret)
> + return ret;
> +
> + /* Store a matched current+voltage pair per conversion. */
> + fifo_cfg = FIELD_PREP(MAX40080_FIFO_CFG_STORE_IV_MSK, MAX40080_FIFO_CFG_STORE_I_V);
> +
> + ret = i2c_smbus_write_word_data(st->client, MAX40080_REG_FIFO_CFG,
> + fifo_cfg);
> + if (ret)
> + return ret;
> +
> + /*
> + * Use single-measurement mode: the device stays idle and converts once
> + * per SMBus Quick Command (see max40080_trigger_measurement()), so each
> + * read returns a fresh sample rather than a queued FIFO entry.
> + */
> + cfg = FIELD_PREP(MAX40080_CFG_MODE_MSK, MAX40080_CFG_MODE_SINGLE) |
> + FIELD_PREP(MAX40080_CFG_PEC_EN_MSK, 1) |
> + FIELD_PREP(MAX40080_CFG_RANGE_MSK, st->range) |
> + FIELD_PREP(MAX40080_CFG_FILTER_MSK, filter);
> +
> + return i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
> +}
> +
> +static int max40080_probe(struct i2c_client *client)
> +{
> + const char *propname = "shunt-resistor-micro-ohms";
> + struct device *dev = &client->dev;
> + struct iio_dev *indio_dev;
> + struct max40080_state *st;
> + int ret;
> +
> + /*
> + * The device powers up with PEC enabled (CFG POR = 0x0060) and rejects
> + * unprotected transactions, so PEC support is mandatory, along with word
> + * access, the I2C block read used for the current/voltage pair, and the
> + * Quick Command used to trigger a conversion.
> + */
> + if (!i2c_check_functionality(client->adapter,
> + I2C_FUNC_SMBUS_WORD_DATA |
> + I2C_FUNC_SMBUS_I2C_BLOCK |
> + I2C_FUNC_SMBUS_QUICK |
> + I2C_FUNC_SMBUS_PEC))
> + return -EOPNOTSUPP;
> +
> + client->flags |= I2C_CLIENT_PEC;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + st = iio_priv(indio_dev);
> + st->client = client;
> +
> + ret = devm_mutex_init(dev, &st->lock);
> + if (ret)
> + return ret;
> +
Assign propname here so we don't have to scroll to see what it is.
> + ret = device_property_read_u32(dev, propname, &st->shunt_resistor_uohm);
> + if (ret)
> + return dev_err_probe(dev, ret, "can't read %s\n", propname);
> + if (!st->shunt_resistor_uohm)
> + return dev_err_probe(dev, -EINVAL, "%s must be non-zero\n",
> + propname);
> +
> + max40080_calc_current_scale(st);
> +
> + /* Defaults: 50 mV range, no averaging. */
> + st->range = MAX40080_CFG_RANGE_50MV;
> + st->oversampling_ratio = 1;
> +
> + indio_dev->name = "max40080";
> + indio_dev->info = &max40080_info;
> + indio_dev->modes = INDIO_DIRECT_MODE;
> + indio_dev->channels = max40080_channels;
> + indio_dev->num_channels = ARRAY_SIZE(max40080_channels);
> +
> + ret = max40080_init(st);
> + if (ret)
> + return ret;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct i2c_device_id max40080_i2c_ids[] = {
> + { .name = "max40080" },
> + { }
> +};
> +MODULE_DEVICE_TABLE(i2c, max40080_i2c_ids);
> +
> +static const struct of_device_id max40080_of_match[] = {
> + { .compatible = "adi,max40080" },
> + { }
> +};
> +MODULE_DEVICE_TABLE(of, max40080_of_match);
> +
> +static struct i2c_driver max40080_driver = {
> + .driver = {
> + .name = "max40080",
> + .of_match_table = max40080_of_match,
> + },
> + .probe = max40080_probe,
> + .id_table = max40080_i2c_ids,
> +};
> +module_i2c_driver(max40080_driver);
> +
> +MODULE_AUTHOR("Ciprian Hegbeli <ciprian.hegbeli@analog.com>");
> +MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
> +MODULE_DESCRIPTION("Analog Devices MAX40080 current-sense amplifier driver");
> +MODULE_LICENSE("GPL");
^ permalink raw reply [flat|nested] 12+ messages in thread
* Re: [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
` (2 preceding siblings ...)
2026-08-01 16:05 ` David Lechner
@ 2026-08-10 19:21 ` Andy Shevchenko
3 siblings, 0 replies; 12+ messages in thread
From: Andy Shevchenko @ 2026-08-10 19:21 UTC (permalink / raw)
To: Stefan Popa
Cc: linux-iio, linux-hwmon, devicetree, linux-kernel, jic23, andy,
nuno.sa, linux, robh, krzk+dt, conor+dt, dlechner,
ciprian.hegbeli, siratul.islam, u.kleine-koenig, joshua.crofts1
On Thu, Jul 30, 2026 at 04:17:38PM +0300, Stefan Popa wrote:
> The MAX40080 is a bidirectional current-sense amplifier with an
> integrated 12-bit ADC and an I2C/SMBus interface. It measures the
> voltage across an external shunt resistor and the input bus voltage,
> storing the results in an internal FIFO.
>
> Add a direct-mode IIO driver exposing the current and voltage channels
> with raw, scale and hardware-gain attributes, a configurable
> oversampling (digital averaging) ratio, and PEC-protected register
> access. The current scale is derived from the shunt resistor value
> described in the device tree.
>
> The driver operates in single-measurement mode: each raw read triggers
> an on-demand conversion via SMBus Quick Command and returns a matched
> current/voltage pair. This avoids the latency and complexity of the
> continuous FIFO mode while ensuring each read reflects the current
> state. The two selectable current-sense ranges are exposed through
> scale/scale_available.
>
> Continuous FIFO buffering, threshold events and the alert interrupt are
> intentionally left out of this initial submission and may be added
> later.
...
> +#include <linux/bitfield.h>
> +#include <linux/bitops.h>
> +#include <linux/cleanup.h>
> +#include <linux/i2c.h>
> +#include <linux/iopoll.h>
> +#include <linux/math64.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/property.h>
+ types.h // uXX
> +#include <linux/unaligned.h>
> +#include <linux/units.h>
...
In general it _feels_ like some of the comments given in the previous rounds
were dropped or ignored. Is this true? Could it be that you mistakenly sent
one of the previous (early) versions?
That's why I stopped here.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-08-10 19:21 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-30 13:17 [PATCH v6 0/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:17 ` [PATCH v6 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-07-30 13:25 ` sashiko-bot
2026-07-30 18:42 ` Siratul Islam
2026-07-31 7:04 ` Krzysztof Kozlowski
2026-07-31 7:06 ` Krzysztof Kozlowski
2026-08-01 15:38 ` David Lechner
2026-07-30 13:17 ` [PATCH v6 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-30 13:29 ` sashiko-bot
2026-07-31 6:00 ` Siratul Islam
2026-08-01 16:05 ` David Lechner
2026-08-10 19:21 ` Andy Shevchenko
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox