From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Stefan Popa <stefan.popa@analog.com>
Cc: "Jonathan Cameron" <jic23@kernel.org>,
linux-iio@vger.kernel.org,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sa" <nuno.sa@analog.com>, "Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Siratul Islam" <siratul.islam@linux.dev>,
"Uwe Kleine-König" <u.kleine-koenig@baylibre.com>,
"Ciprian Hegbeli" <ciprian.hegbeli@analog.com>,
devicetree@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] iio: adc: add MAX40080 current-sense amplifier driver
Date: Mon, 13 Jul 2026 19:04:14 +0300 [thread overview]
Message-ID: <alUMfpXGVa9CYxlT@ashevche-desk.local> (raw)
In-Reply-To: <20260713120226.90303-3-stefan.popa@analog.com>
On Mon, Jul 13, 2026 at 03:02:26PM +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.
>
> No existing IIO driver covers this device or a register-compatible part.
> The closest relatives target different silicon with incompatible register
> maps and feature sets: max9611 is a unidirectional high-side sensor with a
> die-temperature channel and MUX-selected gain and no FIFO/PEC, while
> max34408 is an 8-bit multi-channel current monitor. The MAX40080 has a
> device-specific register map with bidirectional 13-bit current, a 64-entry
> FIFO, PEC, a single-measurement mode triggered by an SMBus Quick Command,
> and two selectable input ranges, so it warrants its own driver.
>
> Add a direct-mode IIO driver exposing the current and voltage channels
> with raw and scale attributes, a configurable oversampling (digital
> averaging) ratio, and PEC-protected register access. The two selectable
> current-sense ranges are exposed through scale/scale_available; the
> current scale is derived from the shunt-resistor-micro-ohms device-tree
> property.
...
> +#include <linux/bitops.h>
> +#include <linux/cleanup.h>
> +#include <linux/i2c.h>
> +#include <linux/iopoll.h>
> +#include <linux/math64.h>
> +#include <linux/mod_devicetable.h>
Should be not used since Uwe's rework of this header.
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/pm.h>
> +#include <linux/property.h>
> +#include <linux/time.h>
> +#include <linux/types.h>
> +#include <linux/unaligned.h>
> +#include <linux/units.h>
...
> +/* 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 4096
> +#define MAX40080_INTER_VREF_MV 1250
Let's also use _mV here.
> +#define MAX40080_V_BUFF_GAIN 30
> +#define MAX40080_CSA_50MV_GAIN 25
> +#define MAX40080_CSA_10MV_GAIN 125
And here.
...
> +#define MAX40080_NUM_RANGES ARRAY_SIZE(max40080_csa_gain)
I think I commented on this already. This hides the information, I think
the better way is to avoid using this macro.
...
> +static int max40080_read_iv(struct max40080_state *st, u32 *iv)
> +{
> + 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 || (*iv & MAX40080_IV_VALID_MSK),
> + 1 * USEC_PER_MSEC, 50 * USEC_PER_MSEC,
> + false, st, iv);
> + if (ret)
> + return ret;
Even in case of error the value in iv will be updated. Is it deliberately?
> + return io_ret;
> +}
...
> +static int max40080_get_oversampling_ratio(struct max40080_state *st, int *val)
> +{
> + int tmp;
> + u8 filter;
Keep reversed xmas tree order.
> + tmp = i2c_smbus_read_word_data(st->client, MAX40080_REG_CFG);
> + if (tmp < 0)
> + return tmp;
> +
> + filter = FIELD_GET(MAX40080_CFG_FILTER_MSK, tmp);
> + *val = max40080_oversampling_avail[filter];
> +
> + return 0;
> +}
...
> +static int max40080_set_oversampling_ratio(struct max40080_state *st, int val)
> +{
> + int ret, filter = max40080_oversampling_to_filter(val);
This style is discouraged. First of all, it's a mix of semantically different
variables. Second, the assignment of the variable that is going to be validated
might lead to subtle issues in the future.
> + if (filter < 0)
> + return filter;
So, Do just
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,
At least these two may be on a single line.
> + long mask)
...
> +static int max40080_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
You see, like here.
...
> + *vals = (int *)st->current_scale;
Why not const int?
> + *length = MAX40080_NUM_RANGES * 2;
> + *type = IIO_VAL_INT_PLUS_NANO;
...
> +static int max40080_init(struct max40080_state *st)
> +{
> + u16 fifo_cfg, cfg;
> + int ret, filter;
Here is better, but see above.
> + 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);
Previous line is long enough to justify this one to be also a single line.
> + 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);
> + ret = i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
> + if (ret)
> + return ret;
> +
> + return 0;
Just
return i2c_smbus_write_word_data(st->client, MAX40080_REG_CFG, cfg);
?
> +}
...
> +static int max40080_probe(struct i2c_client *client)
> +{
> + struct device *dev = &client->dev;
> + struct max40080_state *st;
> + struct iio_dev *indio_dev;
> + 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;
> + st->shunt_resistor_uohm = 1 * MICRO;
> + device_property_read_u32(dev, "shunt-resistor-micro-ohms",
> + &st->shunt_resistor_uohm);
> + if (!st->shunt_resistor_uohm)
> + return dev_err_probe(dev, -EINVAL,
> + "shunt-resistor-micro-ohms must be non-zero\n");
We now establish better approach for optional values
const char *propname;
propname = "shunt-resistor-micro-ohms";
if (device_property_present(dev, propname)) {
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);
} else {
st->shunt_resistor_uohm = 1 * MICRO;
}
> + max40080_calc_current_scale(st);
> +
> + /* Defaults: 50 mV range (index 0), no averaging. */
> + st->range = 0;
> + 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-07-13 16:04 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-03 10:29 [PATCH v1 0/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-03 10:29 ` [PATCH v1 1/2] dt-bindings: iio: adc: add maxim,max40080 Stefan Popa
2026-07-03 16:21 ` Conor Dooley
2026-07-03 20:42 ` David Lechner
2026-07-03 10:29 ` [PATCH v1 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-03 10:42 ` sashiko-bot
2026-07-03 12:08 ` Andy Shevchenko
2026-07-03 23:36 ` Jonathan Cameron
2026-07-03 19:42 ` Siratul Islam
2026-07-03 20:29 ` David Lechner
2026-07-04 12:05 ` Andy Shevchenko
2026-07-04 16:32 ` Siratul Islam
2026-07-04 17:05 ` Andy Shevchenko
2026-07-04 17:30 ` Siratul Islam
2026-07-04 18:52 ` Andy Shevchenko
2026-07-03 21:04 ` David Lechner
2026-07-03 23:53 ` Jonathan Cameron
2026-07-06 17:17 ` Uwe Kleine-König
2026-07-03 11:34 ` [PATCH v1 0/2] " Andy Shevchenko
2026-07-13 12:02 ` [PATCH v2 " Stefan Popa
2026-07-13 12:02 ` [PATCH v2 1/2] dt-bindings: iio: adc: add maxim,max40080 Stefan Popa
2026-07-13 16:06 ` Conor Dooley
2026-07-13 12:02 ` [PATCH v2 2/2] iio: adc: add MAX40080 current-sense amplifier driver Stefan Popa
2026-07-13 12:17 ` sashiko-bot
2026-07-13 16:04 ` Andy Shevchenko [this message]
2026-07-13 12:16 ` [PATCH v2 0/2] " Joshua Crofts
2026-07-14 12:24 ` Uwe Kleine-König
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=alUMfpXGVa9CYxlT@ashevche-desk.local \
--to=andriy.shevchenko@linux.intel.com \
--cc=ciprian.hegbeli@analog.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--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