From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: John Erasmus Mari Geronimo <johnerasmusmari.geronimo@analog.com>
Cc: linux-iio@vger.kernel.org, devicetree@vger.kernel.org,
krzk+dt@kernel.org, jic23@kernel.org, dlechner@baylibre.com,
nuno.sa@analog.com, Michael.Hennerich@analog.com,
andy@kernel.org, robh@kernel.org, conor+dt@kernel.org,
joshua.crofts1@gmail.com
Subject: Re: [PATCH v6 2/2] iio: temperature: add support for Analog Devices MAX30210
Date: Tue, 10 Nov 2026 22:53:18 +0200 [thread overview]
Message-ID: <avOEPk_cwJSqxECQ@ashevche-desk.local> (raw)
In-Reply-To: <20261110021442.410-3-johnerasmusmari.geronimo@analog.com>
On Tue, Nov 10, 2026 at 10:14:42AM +0800, John Erasmus Mari Geronimo wrote:
> Add support for the Analog Devices MAX30210 I2C temperature
> sensor.
>
> The driver uses regmap for register access and integrates with
> the IIO framework. It supports:
>
> - Direct mode temperature conversion
> - Configurable sampling frequency
> - Threshold events
> - FIFO operation with IIO kfifo buffer support
> - Optional interrupt-driven data ready signaling
>
> The device provides 16-bit signed temperature data and a
> 64-sample FIFO.
...
> + /* Drop the reference even if the write fails; a later direct read clears AUTO anyway. */
It's a bit too long. Make it multi-line.
...
> +static void max30210_fifo_read(struct iio_dev *indio_dev)
> +{
> + struct max30210_state *st = iio_priv(indio_dev);
> + int ret;
> +
> + /*
> + * FIFO_DATA_REG auto-increments in hardware; bulk_read would treat
> + * each subsequent byte as a separate cacheable register.
> + */
> + ret = regmap_noinc_read(st->regmap, MAX30210_FIFO_DATA_REG,
> + st->fifo_buf,
> + MAX30210_FIFO_BYTES_PER_SAMPLE * st->watermark);
> + if (ret) {
> + dev_err(&indio_dev->dev, "Failed to read from fifo.\n");
> + return;
> + }
> +
> + for (unsigned int i = 0; i < st->watermark; i++) {
> + u32 raw = get_unaligned_be24(&st->fifo_buf[MAX30210_FIFO_BYTES_PER_SAMPLE * i]);
unsigned int idx = MAX30210_FIFO_BYTES_PER_SAMPLE * i;
u32 raw = get_unaligned_be24(&st->fifo_buf[idx]);
> + s16 temp;
> +
> + if (raw == MAX30210_FIFO_INVAL_DATA) {
> + dev_err_ratelimited(&indio_dev->dev, "Invalid data\n");
> + continue;
> + }
> +
> + temp = sign_extend32(FIELD_GET(MAX30210_FIFO_TEMP_MASK, raw), 15);
> +
> + iio_push_to_buffers(indio_dev, &temp);
> + }
> +}
...
> +static irqreturn_t max30210_irq_handler(int irq, void *dev_id)
> +{
> + struct iio_dev *indio_dev = dev_id;
> + struct max30210_state *st = iio_priv(indio_dev);
> + unsigned int status, enabled;
> + int ret;
> +
> + ret = regmap_read(st->regmap, MAX30210_STATUS_REG, &status);
> + if (ret) {
> + dev_err(&indio_dev->dev, "Status read failed\n");
It's always recommended to have the messages to be ratelimited in IRQ handler,
that's in case of intermittent problems with reading this may flood the kernel
log.
> + return IRQ_NONE;
> + }
> +
> + /* STATUS flags are set whether or not they are enabled in INT_EN. */
> + ret = regmap_read(st->regmap, MAX30210_INT_EN_REG, &enabled);
> + if (ret) {
> + dev_err(&indio_dev->dev, "Interrupt enable read failed\n");
> + return IRQ_NONE;
> + }
> +
> + status &= enabled;
> + if (!(status & (MAX30210_STATUS_A_FULL_MASK |
> + MAX30210_STATUS_TEMP_HI_MASK |
> + MAX30210_STATUS_TEMP_LO_MASK)))
> + return IRQ_NONE;
> +
> + if (status & MAX30210_STATUS_A_FULL_MASK)
> + max30210_fifo_read(indio_dev);
> +
> + if (status & MAX30210_STATUS_TEMP_HI_MASK)
> + iio_push_event(indio_dev,
> + IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
> + IIO_EV_TYPE_THRESH,
> + IIO_EV_DIR_RISING),
> + iio_get_time_ns(indio_dev));
> +
> + if (status & MAX30210_STATUS_TEMP_LO_MASK)
> + iio_push_event(indio_dev,
> + IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
> + IIO_EV_TYPE_THRESH,
> + IIO_EV_DIR_FALLING),
> + iio_get_time_ns(indio_dev));
> +
> + return IRQ_HANDLED;
> +}
...
> +static int max30210_write_raw(struct iio_dev *indio_dev,
> + struct iio_chan_spec const *chan,
> + int val, int val2, long mask)
> +{
> + struct max30210_state *st = iio_priv(indio_dev);
> +
> + switch (mask) {
> + case IIO_CHAN_INFO_SAMP_FREQ: {
struct regmap *map = st->regmap;
> + if (val < 0 || val2 < 0)
> + return -EINVAL;
> +
> + IIO_DEV_ACQUIRE_DIRECT_MODE(indio_dev, claim);
> + if (IIO_DEV_ACQUIRE_FAILED(claim))
> + return -EBUSY;
> +
> + for (unsigned int i = 0; i < ARRAY_SIZE(max30210_samp_freq_avail); i++) {
unsigned int mask = MAX30210_TEMPCONF2_TEMP_PERIOD_MASK;
unsigned int value = field_prep(mask, i);
> + if (val != max30210_samp_freq_avail[i][0] ||
> + val2 != max30210_samp_freq_avail[i][1])
> + continue;
> +
> + return regmap_update_bits(st->regmap, MAX30210_TEMP_CONF_2_REG,
> + MAX30210_TEMPCONF2_TEMP_PERIOD_MASK,
> + FIELD_PREP(MAX30210_TEMPCONF2_TEMP_PERIOD_MASK, i));
return regmap_update_bits(map, MAX30210_TEMP_CONF_2_REG,
mask, value);
> + }
> +
> + return -EINVAL;
> + }
> + default:
> + return -EINVAL;
> + }
> +}
...
> +static int max30210_set_watermark(struct iio_dev *indio_dev, unsigned int val)
> +{
> + struct max30210_state *st = iio_priv(indio_dev);
> + int ret;
> +
> + if (!val)
> + return -EINVAL;
> +
> + val = min(val, MAX30210_FIFO_SIZE);
I would put check here to make it look more natural.
val = min(val, MAX30210_FIFO_SIZE);
if (!val)
return -EINVAL;
> + /*
> + * FIFO_A_FULL sets the number of empty FIFO slots remaining when
> + * the A_FULL interrupt fires. For a watermark of N samples, set
> + * FIFO_A_FULL = FIFO_SIZE - N.
> + */
> + ret = regmap_write(st->regmap, MAX30210_FIFO_CONF_1_REG,
> + FIELD_PREP(MAX30210_FIFOCONF1_A_FULL_MASK,
> + MAX30210_FIFO_SIZE - val));
> + if (ret)
> + return ret;
> +
> + st->watermark = val;
> +
> + return 0;
> +}
...
> +static int max30210_setup(struct max30210_state *st, struct device *dev)
> +{
> + struct gpio_desc *powerdown_gpio;
> + unsigned int val;
> + int ret;
> +
> + powerdown_gpio = devm_gpiod_get_optional(dev, "powerdown", GPIOD_OUT_HIGH);
> + if (IS_ERR(powerdown_gpio))
> + return dev_err_probe(dev, PTR_ERR(powerdown_gpio),
> + "failed to request powerdown GPIO\n");
> +
> + if (powerdown_gpio) {
> + /* CVT/PDB needs a 5 us low pulse; hold it asserted for twice that. */
If I am not mistaken somewhere else you used Greek MU, why not here? Please,
check the comments for the consistency.
> + fsleep(10);
> +
> + /* Deassert powerdown to power up device */
> + gpiod_set_value_cansleep(powerdown_gpio, 0);
> +
> + ret = devm_add_action_or_reset(dev, max30210_powerdown, powerdown_gpio);
> + if (ret)
> + return ret;
> + } else {
> + /* The supply was just enabled; the device needs tPU before it accepts I2C. */
> + fsleep(700);
> +
> + ret = regmap_set_bits(st->regmap, MAX30210_SYS_CONF_REG,
> + MAX30210_SYSCONF_RESET);
> + if (ret)
> + return ret;
> + }
> +
> + /*
> + * Datasheet Figure 6:
> + * tPU max = 700 µs after power-up or reset before device is ready.
> + */
> + fsleep(700);
> +
> + /* Clear status byte */
> + return regmap_read(st->regmap, MAX30210_STATUS_REG, &val);
> +}
--
With Best Regards,
Andy Shevchenko
next prev parent reply other threads:[~2026-11-10 20:53 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-10 2:14 [PATCH v6 0/2] Add support for Analog Devices MAX30210 John Erasmus Mari Geronimo
2026-11-10 2:14 ` [PATCH v6 1/2] dt-bindings: iio: temperature: add ADI MAX30210 John Erasmus Mari Geronimo
2026-11-10 2:14 ` [PATCH v6 2/2] iio: temperature: add support for Analog Devices MAX30210 John Erasmus Mari Geronimo
2026-11-10 2:33 ` sashiko-bot
2026-11-10 20:53 ` Andy Shevchenko [this message]
2026-11-10 20:55 ` Andy Shevchenko
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=avOEPk_cwJSqxECQ@ashevche-desk.local \
--to=andriy.shevchenko@intel.com \
--cc=Michael.Hennerich@analog.com \
--cc=andy@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=johnerasmusmari.geronimo@analog.com \
--cc=joshua.crofts1@gmail.com \
--cc=krzk+dt@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=nuno.sa@analog.com \
--cc=robh@kernel.org \
/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