From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Stefan Popa <stefan.popa@analog.com>
Cc: linux-iio@vger.kernel.org, jic23@kernel.org, lars@metafoo.de,
Michael.Hennerich@analog.com, robh@kernel.org,
krzk+dt@kernel.org, conor+dt@kernel.org, andy@kernel.org,
dlechner@baylibre.com, nuno.sa@analog.com,
siratul.islam@linux.dev, u.kleine-koenig@baylibre.com,
linux@roeck-us.net, joshua.crofts1@gmail.com,
ciprian.hegbeli@analog.com, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v7 2/2] iio: adc: add MAX40080 current-sense amplifier driver
Date: Tue, 18 Aug 2026 18:44:33 +0300 [thread overview]
Message-ID: <aoR94S2qknBVQrT3@ashevche-desk.local> (raw)
In-Reply-To: <20260818142928.8244-3-stefan.popa@analog.com>
On Tue, Aug 18, 2026 at 05:29:28PM +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.
...
> endmenu
> +
Wrong placement for a new entry.
> +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.
...
> 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
Why is not ordered?
...
+ array_size.h
I think I repeated this three times already.
> +#include <linux/bitfield.h>
> +#include <linux/bitops.h>
> +#include <linux/cleanup.h>
> +#include <linux/i2c.h>
> +#include <linux/types.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>
...
> +static int max40080_read_iv_once(struct max40080_state *st, u32 *iv)
> +{
> + u8 buf[4];
Can this be __le32?
> + int ret;
> +
> + ret = i2c_smbus_read_i2c_block_data(st->client, MAX40080_REG_IV,
> + sizeof(buf), buf);
> + if (ret < 0)
> + return ret;
> +
> + *iv = get_unaligned_le32(buf);
In that case le32_to_cpu() from asm/byteorder.h may be used.
> + return 0;
> +}
...
> +static void max40080_calc_current_scale(struct max40080_state *st)
> +{
> + u64 numerator, denominator;
> + u32 rem;
> +
> + for (unsigned int i = 0; i < ARRAY_SIZE(max40080_csa_gain); i++) {
> + numerator = 1ULL * MAX40080_INTER_VREF_mV * NANO * MICRO;
> + denominator = 1ULL * BIT(MAX40080_ADC_RES_BITS) * max40080_csa_gain[i] *
Btw, this 1ULL * BIT() can be replaced with BIT_ULL().
> + st->shunt_resistor_uOhm;
> + numerator = div64_u64(numerator, denominator);
> + st->current_scale[i][0] = div_u64_rem(numerator, NANO, &rem);
> + st->current_scale[i][1] = rem;
> + }
> +}
...
> +static const struct iio_chan_spec max40080_channels[] = {
> + {
> + .type = IIO_CURRENT,
> + .indexed = 1,
> + .channel = 0,
No need
> + .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,
Same, it's default.
> + .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),
> + },
> +};
...
> +static int max40080_probe(struct i2c_client *client)
> +{
> + const char *propname = "shunt-resistor-micro-ohms";
It's better to split and use when it's required.
> + 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;
propname = "shunt-resistor-micro-ohms";
> + 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);
I would dare to put this on a single line.
> + 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);
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-08-18 15:44 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-08-18 14:29 [PATCH v7 0/2] " Stefan Popa
2026-08-18 14:29 ` [PATCH v7 1/2] dt-bindings: iio: adc: add adi,max40080 Stefan Popa
2026-08-27 9:14 ` Krzysztof Kozlowski
2026-08-18 14:29 ` [PATCH v7 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-08-18 15:44 ` Andy Shevchenko [this message]
2026-08-18 16:19 ` Jonathan Cameron
2026-08-19 6:51 ` Andy Shevchenko
2026-08-18 16:38 ` Jonathan Cameron
2026-08-19 8:00 ` Siratul Islam
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aoR94S2qknBVQrT3@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=ciprian.hegbeli@analog.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=joshua.crofts1@gmail.com \
--cc=krzk+dt@kernel.org \
--cc=lars@metafoo.de \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@roeck-us.net \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
--cc=siratul.islam@linux.dev \
--cc=stefan.popa@analog.com \
--cc=u.kleine-koenig@baylibre.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®