From: David Lechner <dlechner@baylibre.com>
To: Stefan Popa <stefan.popa@analog.com>,
Jonathan Cameron <jic23@kernel.org>
Cc: "Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>,
"Rob Herring" <robh@kernel.org>,
"Krzysztof Kozlowski" <krzk+dt@kernel.org>,
"Conor Dooley" <conor+dt@kernel.org>,
"Ciprian Hegbeli" <ciprian.hegbeli@analog.com>,
linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v1 2/2] iio: adc: add MAX40080 current-sense amplifier driver
Date: Fri, 3 Jul 2026 16:04:14 -0500 [thread overview]
Message-ID: <d78d5f30-7f32-4998-8dc4-f17c61a5f81b@baylibre.com> (raw)
In-Reply-To: <20260703102941.1141341-3-stefan.popa@analog.com>
On 7/3/26 5:29 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
Patch does not implemnt hardware-gain attribute (which is correct,
so just fix the commit message).
> oversampling (digital averaging) ratio, and PEC-protected register
> access. The current scale is derived from the shunt resistor value
> described in the device tree.
>
> +static int max40080_update_bits(struct max40080_state *st, u8 reg,
> + u16 mask, u16 val)
> +{
> + int ret;
> + int tmp;
> +
> + guard(mutex)(&st->lock);
Usually we don't want the lock at this level in case anything needs to
update more than one register in an atomic operation.
> +
> + tmp = i2c_smbus_read_word_data(st->client, reg);
> + if (tmp < 0)
> + return tmp;
> +
> + tmp &= ~mask;
> + tmp |= val & mask;
> +
> + ret = i2c_smbus_write_word_data(st->client, reg, tmp);
> + if (ret < 0)
> + return ret;
> +
> + 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;
> + }
Se usually use switch statement in IIO instead of else if.
> +
> + 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.
> + */
> + ret = max40080_get_range(st, &range);
> + if (ret)
> + return ret;
> +
> + *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 / ADC_RES */
> + *val = MAX40080_INTER_VREF_MV * MAX40080_V_BUFF_GAIN;
> + *val2 = MAX40080_ADC_RES;
> + return IIO_VAL_FRACTIONAL;
> + case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> + ret = max40080_get_oversampling_ratio(st, val);
> + if (ret)
> + return ret;
> + return IIO_VAL_INT;
> + default:
> + return -EINVAL;
> + }
> +}
> +
...
> +static int max40080_probe(struct i2c_client *client)
> +{
> + struct device *dev = &client->dev;
> + struct iio_dev *indio_dev;
> + struct max40080_state *st;
> + int ret;
> +
> + if (!i2c_check_functionality(client->adapter,
> + I2C_FUNC_SMBUS_WORD_DATA |
> + I2C_FUNC_SMBUS_I2C_BLOCK |
> + I2C_FUNC_SMBUS_QUICK))
> + return -EOPNOTSUPP;
> +
> + client->flags |= I2C_CLIENT_PEC;
> +
> + indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
> + if (!indio_dev)
> + return -ENOMEM;
> +
> + i2c_set_clientdata(client, indio_dev);
> +
> + st = iio_priv(indio_dev);
> + st->client = client;
> +
> + ret = devm_mutex_init(dev, &st->lock);
> + if (ret)
> + return ret;
> +
> + if (device_property_read_u32(dev, "shunt-resistor-micro-ohms",
> + &st->shunt_resistor_uohm))
> + st->shunt_resistor_uohm = 1000000; /* default 1 ohm */
> +
> + if (!st->shunt_resistor_uohm)
> + return dev_err_probe(dev, -EINVAL,
> + "shunt-resistor-micro-ohms must be non-zero\n");
> +
> + max40080_calc_current_scale(st);
> +
> + 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);
> +
> + /* No averaging by default; configurable at runtime via sysfs. */
> + ret = max40080_init(st, 1);
Why have a paramter if it is always the same value? Can just move
this comment into the init function and drop the arg.
> + if (ret)
> + return ret;
> +
> + return devm_iio_device_register(dev, indio_dev);
> +}
> +
> +static const struct i2c_device_id max40080_i2c_ids[] = {
> + { "max40080" },
Include `.name = `
> + { }
> +};
> +MODULE_DEVICE_TABLE(i2c, max40080_i2c_ids);
> +
next prev parent reply other threads:[~2026-07-03 21:04 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-03 10:29 [PATCH v1 0/2] " 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 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 [this message]
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 16:04 ` Andy Shevchenko
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=d78d5f30-7f32-4998-8dc4-f17c61a5f81b@baylibre.com \
--to=dlechner@baylibre.com \
--cc=andy@kernel.org \
--cc=ciprian.hegbeli@analog.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--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=stefan.popa@analog.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