Add support for Analog Devices MAX30210
Subsystems (4)
v5 cover Add support for Analog Devices MAX30210
This series adds support for the Analog Devices MAX30210 I2C temperature sensor.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf
The device provides a 16-bit temperature output and includes features such as a hardware FIFO, programmable sampling rate, threshold alarms, and interrupt support. These capabilities map naturally to the IIO subsystem, particularly the buffered interface and event framework -- hwmon has no equivalent buffered-capture framework, and its alarm attributes are polled rather than interrupt-driven, so there is no path to supporting the FIFO or the autonomous threshold comparators there.
Although the MAX30210 belongs to the same device family as the MAX30208, it is implemented as a separate driver. The MAX30210 has a significantly different feature set, including a 64-sample hardware FIFO with configurable watermark interrupts, programmable sampling rates, and event support. These features require buffered operation and IRQ-driven data handling which are not present in the MAX30208 driver (which talks to the device directly via i2c_smbus_*() calls with no regmap, and has no IIO buffer or event interface at all).
In addition, the MAX30210 uses a different register map and a more flexible I2C address selection scheme. Supporting both devices in a single driver would introduce substantial conditional logic, so a separate driver keeps the implementation simpler and easier to maintain.
Patch 1 adds the devicetree binding.
Patch 2 adds the IIO driver implementation.
Changes since v4:
https://lore.kernel.org/all/[email protected]/
- Patch 1: restore Krzysztof's Reviewed-by, which was accidentally left out of v4.
- Add Datasheet: tags and a datasheet link in the driver header.
- Disable interrupt sources and autonomous conversion on removal, independently of whether a powerdown GPIO is present.
- Only take/release an autonomous-conversion reference when an interrupt enable bit really changes; repeated writes of the same value no longer leak a reference.
- buffer_preenable() now unwinds the A_FULL interrupt enable on failure and buffer_postdisable() always clears it.
- Return IRQ_NONE from the interrupt handler when none of the handled status bits is set.
- Wait tPU before the software reset when no powerdown GPIO is used.
- Minor review cleanups: error paths out of line in max30210_autoconv_get(), min() for the period clamp, devm_mutex_init(), designated initializer for i2c_device_id, header grouping, one-line val/val2/mask argument lists.
- I kept requesting the interrupt after devm_iio_device_register() so it is freed before the event interface on removal. I could not test the proposed core change because my test platform runs a 6.12-based kernel.
Tested on real MAX30210 hardware (Raspberry Pi 4) with a 6.12-based vendor kernel: no-IRQ fallback, buffer and event refcounting (independently and concurrently), repeated rmmod/insmod under live interrupts, powerdown GPIO release on removal and FIFO flush correctness. That kernel has no KASAN or lockdep, and this version is only build-tested against mainline.
Changes since v3:
https://lore.kernel.org/all/[email protected]/
- regmap_noinc_read() for FIFO reads instead of regmap_bulk_read(), which (combined with regmap caching) could silently return stale cached values of unrelated registers instead of real FIFO data.
- Mark both bytes of TEMP_DATA_REG volatile for the same reason.
- Decouple threshold events from the buffer via a refcounted autoconv_get()/autoconv_put() pair, so either can be used independently or concurrently.
- Don't let a single-shot direct-mode read race with/clobber autonomous conversion mode if a threshold event or the buffer is already using it -- return -EBUSY instead.
- Don't hard-fail probe when there is no interrupt; fall back to direct-mode-only operation.
- Fix devm teardown ordering so an in-flight interrupt can't use a freed event interface on removal.
- Re-assert the powerdown GPIO on removal so the device reaches standby instead of staying powered indefinitely.
- Style/API fixes from review (register/field grouping, _MASK naming, FIELD_PREP/regmap_assign_bits usage, C99 initializers, dropped redundant i2c_check_functionality() check, comment cleanup).
Changes since v2:
https://lore.kernel.org/all/[email protected]/
- Fix FIFO byte-count handling and sample decoding.
- Fix IIO debug register read/write access.
- Validate event threshold values and sampling-frequency register values.
- Return the correct sampling-frequency format and reject direct reads while buffered capture is enabled.
- Clean up the Kconfig entry and device-tree supply declaration.
John Erasmus Mari Geronimo (2):
dt-bindings: iio: temperature: add ADI MAX30210
iio: temperature: add support for Analog Devices MAX30210
.../iio/temperature/adi,max30210.yaml | 61 ++ MAINTAINERS | 8 + drivers/iio/temperature/Kconfig | 13 + drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/max30210.c | 844 ++++++++++++++++++ 5 files changed, 927 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/temperature/adi,max30210.yaml create mode 100644 drivers/iio/temperature/max30210.c
Signature · 1 line
2.34.1
PATCH v5 1/2 dt-bindings: iio: temperature: add ADI MAX30210
Add device tree binding documentation for the Analog Devices MAX30210 temperature sensor.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf
--- .../iio/temperature/adi,max30210.yaml | 61 +++++++++++++++++++ MAINTAINERS | 7 +++ 2 files changed, 68 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/temperature/adi,max30210.yaml
Documentation/devicetree/bindings/iio/temperature/adi,max30210.yaml new file +61 -0
@@ -0,0 +1,61 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +# Copyright 2026 Analog Devices Inc. +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/temperature/adi,max30210.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Analog Devices MAX30210 Temperature Sensor + +maintainers: + - John Erasmus Mari Geronimo <[email protected]> + +description: | + The MAX30210 is a temperature sensor with an I2C interface. + https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf + +properties: + compatible: + enum: + - adi,max30210 + + reg: + maxItems: 1 + + vdd-supply: true + + powerdown-gpios: + description: GPIO connected to the CVT/PDB pin (active low). + maxItems: 1 + + interrupts: + description: Connected to INT pin. Interrupt triggered on both rising and falling edges. + maxItems: 1 + +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/irq.h> + #include <dt-bindings/gpio/gpio.h> + + i2c { + #address-cells = <1>; + #size-cells = <0>; + + temperature-sensor@40 { + compatible = "adi,max30210"; + reg = <0x40>; + vdd-supply = <&vdd>; + powerdown-gpios = <&gpio 12 GPIO_ACTIVE_LOW>; + + interrupt-parent = <&gpio>; + interrupts = <17 IRQ_TYPE_EDGE_BOTH>; + }; + }; +...
MAINTAINERS +7 -0
@@ -1653,6 +1653,13 @@ W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/dac/adi,max22007.yaml F: drivers/iio/dac/max22007.c +ANALOG DEVICES INC MAX30210 DRIVER +M: John Erasmus Mari Geronimo <[email protected]> +L: [email protected] +S: Supported +W: https://ez.analog.com/linux-software-drivers +F: Documentation/devicetree/bindings/iio/temperature/adi,max30210.yaml + ANALOG DEVICES INC ADA4250 DRIVER M: Antoniu Miclaus <[email protected]> L: [email protected]
Signature · 1 line
2.34.1
PATCH v5 2/2 iio: temperature: add support for Analog Devices MAX30210
In reply to John Erasmus Mari GeronimoAdd 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.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf
--- MAINTAINERS | 1 + drivers/iio/temperature/Kconfig | 13 + drivers/iio/temperature/Makefile | 1 + drivers/iio/temperature/max30210.c | 844 +++++++++++++++++++++++++++++ 4 files changed, 859 insertions(+) create mode 100644 drivers/iio/temperature/max30210.c
MAINTAINERS +1 -0
@@ -1659,6 +1659,7 @@ L: [email protected] S: Supported W: https://ez.analog.com/linux-software-drivers F: Documentation/devicetree/bindings/iio/temperature/adi,max30210.yaml +F: drivers/iio/temperature/max30210.c ANALOG DEVICES INC ADA4250 DRIVER M: Antoniu Miclaus <[email protected]>
drivers/iio/temperature/Kconfig +13 -0
@@ -152,6 +152,19 @@ config MAX30208 This driver can also be built as a module. If so, the module will be called max30208. +config MAX30210 + tristate "Analog Devices MAX30210 temperature sensor" + depends on I2C + select REGMAP_I2C + select IIO_BUFFER + select IIO_KFIFO_BUF + help + If you say yes here you get support for MAX30210 low-power digital + temperature sensor chip connected via I2C. + + This driver can also be built as a module. If so, the module + will be called max30210. + config MAX31856 tristate "MAX31856 thermocouple sensor" depends on SPI
drivers/iio/temperature/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_LTC2983) += ltc2983.o obj-$(CONFIG_HID_SENSOR_TEMP) += hid-sensor-temperature.o obj-$(CONFIG_MAXIM_THERMOCOUPLE) += maxim_thermocouple.o obj-$(CONFIG_MAX30208) += max30208.o +obj-$(CONFIG_MAX30210) += max30210.o obj-$(CONFIG_MAX31856) += max31856.o obj-$(CONFIG_MAX31865) += max31865.o obj-$(CONFIG_MCP9600) += mcp9600.o
drivers/iio/temperature/max30210.c new file +844 -0
844 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH v5 2/2] iio: temperature: add support for Analog Devices MAX30210
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] iio: temperature: max30210: wrong scale factor in max30210_read_raw() - [High] iio: temperature…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] iio: temperature: max30210: wrong scale factor in max30210_read_raw()
- [High] iio: temperature: max30210: race between buffer disable and max30210_fifo_read()
- [Medium] iio: temperature: max30210: broken events and missing quiesce without IRQ
- [Medium] iio: temperature: max30210: FIFO enable order and watermark race in preenable
- [Medium] iio: temperature: max30210: missing SYS_CONF_REG in max30210_volatile_reg()
- [Medium] iio: temperature: max30210: IRQ requested after devm_iio_device_register()
- [Medium] iio: temperature: max30210: non-sleeping gpiod_set_value() in max30210_setup()
- [Medium] iio: temperature: max30210: unmasked status bits in max30210_irq_handler()
- [Medium] iio: temperature: max30210: unconditional decrement in max30210_autoconv_put() --
commit 3d7457ec9559f89625f738367b02f298331f750f
Author: John Erasmus Mari Geronimo <[email protected]>
iio: temperature: add support for Analog Devices MAX30210
This commit adds an IIO driver for the Analog Devices MAX30210 I2C temperature sensor, supporting direct mode reads, configurable sampling frequency, threshold events, and a 64-sample hardware FIFO buffer.
Quoted 5 lines
drivers/iio/temperature/max30210.c new file +0
-0
[ ... ]
Quoted 9 lines
drivers/iio/temperature/max30210.c new file +9
-0+static bool max30210_volatile_reg (struct device *dev,unsigned int reg) +{ +switch (reg) { +case MAX30210_STATUS_REG : +case MAX30210_FIFO_DATA_REG : +case MAX30210_FIFO_CONF_2_REG : +case MAX30210_TEMP_CONV_REG : +/* +* TEMP_DATA_REG is a 16-bit value spanning two byte registers;
drivers/iio/temperature/max30210.c line 126
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+* both must be volatile or regmap_bulk_read()'s range check falls +* back to caching the second byte forever (see max30210_read_temp()). +*/ +case MAX30210_TEMP_DATA_REG : +case MAX30210_TEMP_DATA_REG +1 : +return true; +default : +return false; + } +}
[Severity: Medium]
Should MAX30210_SYS_CONF_REG also be marked volatile in max30210_volatile_reg()?
When probing without powerdown-gpios, max30210_setup() calls:
regmap_set_bits(st->regmap, MAX30210_SYS_CONF_REG,
MAX30210_SYSCONF_RESET);Because max30210_regmap uses REGCACHE_MAPLE and MAX30210_SYS_CONF_REG is not in max30210_volatile_reg(), the self-clearing MAX30210_SYSCONF_RESET bit (0x01) remains cached after the hardware finishes resetting and clears it to
0. Would this cause subsequent reads (such as via max30210_reg_access()) to return a stale 0x01 value, or cause future read-modify-write operations on MAX30210_SYS_CONF_REG to re-trigger a device reset?
[ ... ]
drivers/iio/temperature/max30210.c line 190
Quoted 13 lines
drivers/iio/temperature/max30210.c new file +13
-0+static int max30210_autoconv_put (struct max30210_state *st) +{ +int ret =0 ; + +lockdep_assert_held (&st->lock); + +if (st->autoconv_users ==1 ) + ret =regmap_write (st->regmap,MAX30210_TEMP_CONV_REG ,0 ); +if (st->autoconv_users) + st->autoconv_users--; + +return ret; +}
[Severity: Medium]
If regmap_write() fails in max30210_autoconv_put() when st->autoconv_users is 1, should st->autoconv_users still be decremented?
Decrementing st->autoconv_users on error leaves st->autoconv_users at 0 while autonomous conversions remain enabled in MAX30210_TEMP_CONV_REG. In max30210_write_event_config(), MAX30210_INT_EN_REG is cleared before calling max30210_autoconv_put(st), so a subsequent attempt to disable the event sees the bit already cleared and returns 0 early without stopping MAX30210_TEMP_CONV_REG, while max30210_read_raw() sees st->autoconv_users == 0 even though AUTO mode is still running.
Quoted 60 lines
drivers/iio/temperature/max30210.c new file +60
-0+ +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]); +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; +int ret; + + ret =regmap_read (st->regmap,MAX30210_STATUS_REG , &status); +if (ret) { +dev_err (&indio_dev->dev,"Status read failed\n" ); +return IRQ_NONE ; + } + +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));
drivers/iio/temperature/max30210.c line 260
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ +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 ; +}
[Severity: Medium]
Does max30210_irq_handler() need to mask status with the enabled bits in MAX30210_INT_EN_REG before dispatching FIFO reads and threshold events?
In hardware, MAX30210_STATUS_REG sets the A_FULL, TEMP_HI, and TEMP_LO flags whenever those conditions occur during autonomous conversions, regardless of which bits are enabled in MAX30210_INT_EN_REG.
If only threshold events are enabled (buffer disabled), autonomous conversions still fill the FIFO and set MAX30210_STATUS_A_FULL_MASK, causing a threshold interrupt to also invoke max30210_fifo_read() while the buffer is disabled. Conversely, if the FIFO buffer (or only one threshold direction) is enabled and the temperature crosses an unenabled threshold in MAX30210_TEMP_ALM_HI_REG or MAX30210_TEMP_ALM_LO_REG, every FIFO interrupt will see TEMP_HI or TEMP_LO set in status and push disabled threshold events via iio_push_event().
[ ... ]
Quoted 4 lines
drivers/iio/temperature/max30210.c new file +4
-0+static int max30210_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{
drivers/iio/temperature/max30210.c line 437
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+struct max30210_state *st =iio_priv (indio_dev); +unsigned int uval; +int ret; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : + *val =5 ; + *val2 =MILLI ; + +return IIO_VAL_FRACTIONAL ;
[Severity: High]
Is the scale factor for IIO_CHAN_INFO_SCALE in max30210_read_raw() off by a factor of 1000?
Per Documentation/ABI/testing/sysfs-bus-iio, IIO_TEMP channels report values in milli-degrees Celsius. Since one LSB of the MAX30210 temperature data register is 0.005 degrees Celsius (5 milli-degrees Celsius, matching MAX30208 in drivers/iio/temperature/max30208.c), setting *val = 5 and *val2 = MILLI (1000) with IIO_VAL_FRACTIONAL reports 5 / 1000 = 0.005000000 instead of 5. Should this set *val = 5 and return IIO_VAL_INT?
[ ... ]
Quoted 23 lines
drivers/iio/temperature/max30210.c new file +23
-0+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 <1 || val >MAX30210_FIFO_SIZE ) +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 ; +}
[ ... ]
Quoted 21 lines
drivers/iio/temperature/max30210.c new file +21
-0+static int max30210_buffer_preenable (struct iio_dev *indio_dev) +{ +struct max30210_state *st =iio_priv (indio_dev); +int ret; + +/* Enable FIFO-full interrupt */ + ret =regmap_set_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); +if (ret) +return ret; + +/* Flush FIFO before starting autonomous conversions */ + ret =regmap_set_bits (st->regmap,MAX30210_FIFO_CONF_2_REG , +MAX30210_FIFOCONF2_FLUSH_FIFO ); +if (ret) + goto err_disable_irq; + +scoped_guard (mutex, &st->lock) + ret = max30210_autoconv_get(st); +if (ret) + goto err_disable_irq;
drivers/iio/temperature/max30210.c line 629
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ +return 0 ; + +err_disable_irq: +/* The core won't call postdisable() when preenable() fails. */ +regmap_clear_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); + +return ret; +}
[Severity: Medium]
Could the ordering in max30210_buffer_preenable() and max30210_set_watermark() cause stale FIFO reads and a race on st->watermark?
First, max30210_buffer_preenable() enables MAX30210_STATUS_A_FULL_MASK in MAX30210_INT_EN_REG before flushing the FIFO via MAX30210_FIFOCONF2_FLUSH_FIFO. If autonomous conversions were already active for threshold events, A_FULL can already be asserted in MAX30210_STATUS_REG and trigger max30210_irq_handler() on stale FIFO samples before the flush happens.
Second, in iio_enable_buffers(), setup_ops->preenable() runs before info->hwfifo_set_watermark(). Because the FIFO interrupt and autonomous conversions are started in .preenable rather than .postenable, max30210_irq_handler() -> max30210_fifo_read() can run concurrently while max30210_set_watermark() updates MAX30210_FIFO_CONF_1_REG and st->watermark without locking. In max30210_fifo_read(), st->watermark is read unlocked for the regmap_noinc_read() length and re-read unlocked in the loop bound:
ret = regmap_noinc_read(st->regmap, MAX30210_FIFO_DATA_REG,
st->fifo_buf,
MAX30210_FIFO_BYTES_PER_SAMPLE * st->watermark);
...
for (unsigned int i = 0; i < st->watermark; i++) {If max30210_set_watermark() increases st->watermark while regmap_noinc_read() is in flight, the loop can iterate past the number of samples actually read into st->fifo_buf and push uninitialized or stale bytes to the IIO buffer.
Additionally, when val > MAX30210_FIFO_SIZE, should max30210_set_watermark() clamp val to MAX30210_FIFO_SIZE instead of returning -EINVAL, as expected by iio_enable_buffers() and Documentation/ABI/testing/sysfs-bus-iio?
Quoted 11 lines
drivers/iio/temperature/max30210.c new file +11
-0+ +static int max30210_buffer_postdisable (struct iio_dev *indio_dev) +{ +struct max30210_state *st =iio_priv (indio_dev); +int ret, err; + +scoped_guard (mutex, &st->lock) + err = max30210_autoconv_put(st); + +/* Keep going on error so the interrupt is always disabled below. */ + ret = regmap_set_bits(st->regmap, MAX30210_FIFO_CONF_2_REG,
drivers/iio/temperature/max30210.c line 650
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ MAX30210_FIFOCONF2_FLUSH_FIFO); +if (!err) + err = ret; + +/* Disable FIFO-full interrupt */ + ret =regmap_clear_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); + +return err ?: ret; +}
[Severity: High]
Can max30210_buffer_postdisable() return while max30210_irq_handler() is still executing max30210_fifo_read() on another CPU?
When disabling the buffer via __iio_update_buffers() -> iio_disable_buffers() -> max30210_buffer_postdisable(), the IIO core is operating in INDIO_BUFFER_SOFTWARE mode and does not synchronize client->irq. max30210_buffer_postdisable() also flushes the FIFO before clearing MAX30210_STATUS_A_FULL_MASK and returns without calling synchronize_irq() or serializing against max30210_fifo_read().
If max30210_fifo_read() is in flight during an I2C read and calling iio_push_to_buffers(), __iio_update_buffers() immediately proceeds to iio_buffer_deactivate() and deletes the buffer from iio_dev_opaque->buffer_list via list_del_init() while iio_push_to_buffers() iterates buffer_list unlocked, and a subsequent buffer resize and re-enable via iio_request_update_kfifo() can free and reallocate the underlying kfifo while iio_store_to_kfifo() is writing to it. Should max30210_buffer_postdisable() disable MAX30210_STATUS_A_FULL_MASK first and synchronize with the IRQ handler before flushing the FIFO and returning?
[ ... ]
Quoted 20 lines
drivers/iio/temperature/max30210.c new file +20
-0+static void max30210_powerdown (void *data) +{ +struct gpio_desc *powerdown_gpio = data; + +gpiod_set_value (powerdown_gpio,1 ); +} + +static void max30210_quiesce (void *data) +{ +struct max30210_state *st = data; + +/* Stop interrupt sources and autonomous conversions; errors are not actionable here */ +regmap_write (st->regmap,MAX30210_INT_EN_REG ,0 ); +regmap_write (st->regmap,MAX30210_TEMP_CONV_REG ,0 ); +} + +static int max30210_setup (struct max30210_state *st,struct device *dev) +{ +struct gpio_desc *powerdown_gpio; +unsigned int val;
drivers/iio/temperature/max30210.c line 727
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+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) { +/* Deassert powerdown to power up device */ +gpiod_set_value (powerdown_gpio,0 );
[Severity: Medium]
Should max30210_setup() and max30210_powerdown() use gpiod_set_value_cansleep() instead of gpiod_set_value()?
Both functions run in sleepable process context, so if powerdown-gpios is connected through an I2C or SPI GPIO expander, calling gpiod_set_value() will trigger a warning in gpiolib. Also, in max30210_setup(), powerdown_gpio is requested with GPIOD_OUT_HIGH and immediately set to 0 on the next line without any delay; should it either be requested with GPIOD_OUT_LOW directly or wait between asserting and deasserting powerdown?
[ ... ]
Quoted 8 lines
drivers/iio/temperature/max30210.c new file +8
-0+ indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = &max30210_channel; + indio_dev->num_channels =1 ; + indio_dev->name ="max30210" ; + indio_dev->info = &max30210_info; + +/* Currently, without an interrupt there is no way to drain the FIFO into the buffer */ +if (client->irq) {
drivers/iio/temperature/max30210.c line 802
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ ret =devm_iio_kfifo_buffer_setup_ext (dev, indio_dev, &max30210_buffer_ops, + max30210_fifo_attributes); +if (ret) +return ret; + +/* Runs after unregister, so nothing can re-arm the device; before powerdown. */ + ret =devm_add_action_or_reset (dev, max30210_quiesce, st); +if (ret) +return ret; + }
[Severity: Medium]
What happens if client->irq is not provided, but userspace enables threshold events via sysfs?
In max30210_probe(), indio_dev->channels is unconditionally set to &max30210_channel (which includes max30210_events) and indio_dev->info is set to &max30210_info. When client->irq == 0, no IRQ handler is registered, so events can never be delivered, yet writing 1 to events/in_temp_thresh_*_en still invokes max30210_write_event_config() -> max30210_autoconv_get(st). That starts autonomous conversions and increments st->autoconv_users, which causes max30210_read_raw() to reject all single-shot direct reads with -EBUSY, and because max30210_quiesce() is only registered inside the if (client->irq) block, unbinding the driver leaves autonomous conversions and interrupt bits enabled in hardware. Should event support only be registered when client->irq is present?
Quoted 6 lines
drivers/iio/temperature/max30210.c new file +6
-0+ + ret =devm_iio_device_register (dev, indio_dev); +if (ret) +return ret; + +if (!client->irq)
drivers/iio/temperature/max30210.c line 818
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+return 0 ; + +/* +* Request the IRQ after registering the IIO device so devm tears it +* down first on removal, before the event interface is freed. +*/ +return devm_request_threaded_irq (dev, client->irq,NULL , + max30210_irq_handler,IRQF_ONESHOT , + indio_dev->name, indio_dev); +}
[Severity: Medium]
Should devm_request_threaded_irq() be called before devm_iio_device_register() in max30210_probe()?
Registering the IIO device first exposes the sysfs and character device interfaces to userspace before max30210_irq_handler() is installed, and on driver removal devres frees the IRQ while the IIO device is still registered and before devm_iio_device_unreg() disables buffers or max30210_quiesce() masks hardware interrupts. Note that the event interface is freed in iio_dev_release() (registered via devm_iio_device_alloc()), so requesting the IRQ before devm_iio_device_register() still ensures the IRQ is torn down before indio_dev is freed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v5 2/2] iio: temperature: add support for Analog Devices MAX30210
In reply to John Erasmus Mari GeronimoQuoted 14 lines · John Erasmus Mari Geronimo
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 signalingThe device provides 16-bit signed temperature data and a 64-sample FIFO.
...
drivers/iio/temperature/max30210.c line 282
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+static int max30210_read_event (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info,int *val,int *val2) +{ +struct max30210_state *st =iio_priv (indio_dev); + +if (info !=IIO_EV_INFO_VALUE ) +return -EINVAL ;
Seems in the same way you can test for IIO_EV_TYPE_THRESH.
Quoted 20 lines
drivers/iio/temperature/max30210.c new file +20
-0+switch (dir) { +case IIO_EV_DIR_RISING : +switch (type) { +case IIO_EV_TYPE_THRESH : +return max30210_read_temp (st->regmap,MAX30210_TEMP_ALM_HI_REG , val); +default : +return -EINVAL ; + } +case IIO_EV_DIR_FALLING : +switch (type) { +case IIO_EV_TYPE_THRESH : +return max30210_read_temp (st->regmap,MAX30210_TEMP_ALM_LO_REG , val); +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} +
drivers/iio/temperature/max30210.c line 313
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+static int max30210_write_event (struct iio_dev *indio_dev, +const struct iio_chan_spec *chan, +enum iio_event_type type, +enum iio_event_direction dir, +enum iio_event_info info,int val,int val2) +{ +struct max30210_state *st =iio_priv (indio_dev); + +if (info !=IIO_EV_INFO_VALUE ) +return -EINVAL ;
Ditto.
Quoted 5 lines
drivers/iio/temperature/max30210.c new file +5
-0+if (val <S16_MIN || val >S16_MAX ) +return -EINVAL ; + +switch (dir) { +case IIO_EV_DIR_RISING :
drivers/iio/temperature/max30210.c line 329
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+switch (type) { +case IIO_EV_TYPE_THRESH : +return max30210_write_temp (st->regmap,MAX30210_TEMP_ALM_HI_REG , val); +default : +return -EINVAL ; + } +case IIO_EV_DIR_FALLING : +switch (type) { +case IIO_EV_TYPE_THRESH : +return max30210_write_temp (st->regmap,MAX30210_TEMP_ALM_LO_REG , val);
You can reduce the lines at least by 7 characters with
struct regmap *map = st->regmap;
at the top of the function. Use same trick elsewhere.
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} + +static int max30210_read_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan,
drivers/iio/temperature/max30210.c line 349
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ enum iio_event_type type, + enum iio_event_direction dir) +{ +struct max30210_state *st =iio_priv (indio_dev); +unsigned int val; +int ret; + + ret =regmap_read (st->regmap,MAX30210_INT_EN_REG , &val); +if (ret) +return ret;
Do you need to do this for other than IIO_EV_TYPE_THRESH? Otherwise check for that first and bail out earlier.
Quoted 19 lines
drivers/iio/temperature/max30210.c new file +19
-0+switch (dir) { +case IIO_EV_DIR_RISING : +switch (type) { +case IIO_EV_TYPE_THRESH : +return FIELD_GET (MAX30210_STATUS_TEMP_HI_MASK , val); +default : +return -EINVAL ; + } +case IIO_EV_DIR_FALLING : +switch (type) { +case IIO_EV_TYPE_THRESH : +return FIELD_GET (MAX30210_STATUS_TEMP_LO_MASK , val); +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +}
...
drivers/iio/temperature/max30210.c line 402
Quoted 9 lines
drivers/iio/temperature/max30210.c new file +9
-0+guard (mutex)(&st->lock); + +/* The core does not dedupe; only take/drop a reference on a real change. */ + ret =regmap_read (st->regmap,MAX30210_INT_EN_REG , &val); +if (ret) +return ret; + +if (!!(val & mask) == state) +return 0 ;
regmap_test_bits() ?
Quoted 8 lines
drivers/iio/temperature/max30210.c new file +8
-0+/* +* Threshold events need the device sampling autonomously, independent +* of whether the FIFO buffer is also enabled. +*/ +if (state) { + ret =max30210_autoconv_get (st); +if (ret) +return ret;
drivers/iio/temperature/max30210.c line 421
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +9
-0+ } + ret =regmap_assign_bits (st->regmap,MAX30210_INT_EN_REG , mask, state); +if (ret) { +if (state) +max30210_autoconv_put (st); +return ret; + } + +return state ?0 :max30210_autoconv_put (st);
Why not simply
if (state) {
ret = regmap_set_bits(map, MAX30210_INT_EN_REG, mask);
if (ret)
max30210_autoconv_put(st);
return ret;
} else {
ret = regmap_clear_bits(map, MAX30210_INT_EN_REG, mask);
if (ret)
return ret;
return max30210_autoconv_put(st);
}(Yeah, if I suggested _assign_bits() earlier, I was clearly wrong, this
is more complex logic here.)
Quoted 1 line
+}
...
Quoted 6 lines
drivers/iio/temperature/max30210.c new file +6
-0+static int max30210_read_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); +unsigned int uval;
drivers/iio/temperature/max30210.c line 439
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+int ret; + +switch (mask) { +case IIO_CHAN_INFO_SCALE : + *val =5 ; + *val2 =MILLI ; + +return IIO_VAL_FRACTIONAL ; +case IIO_CHAN_INFO_SAMP_FREQ : { +unsigned int period;
Units? period_Hz?
Quoted 45 lines
drivers/iio/temperature/max30210.c new file +45
-0+ ret =regmap_read (st->regmap,MAX30210_TEMP_CONF_2_REG , &uval); +if (ret) +return ret; + + period =FIELD_GET (MAX30210_TEMPCONF2_TEMP_PERIOD_MASK , uval); + +/* +* TEMP_PERIOD is an index into the sampling frequency lookup +* table. Clamp in case the register holds a reserved value. +*/ + period =min (period,ARRAY_SIZE (max30210_samp_freq_avail) -1 ); + + *val = max30210_samp_freq_avail[period][0 ]; + *val2 = max30210_samp_freq_avail[period][1 ]; + +return IIO_VAL_INT_PLUS_MICRO ; + } +case IIO_CHAN_INFO_RAW : { +IIO_DEV_ACQUIRE_DIRECT_MODE (indio_dev, claim); +if (IIO_DEV_ACQUIRE_FAILED (claim)) +return -EBUSY ; + +guard (mutex)(&st->lock); + +/* +* TEMP_DATA_REG is only refreshed by a single-shot +* conversion; the buffer/FIFO path is the only thing that +* gets updated during AUTO mode. If AUTO is already running +* for the buffer and/or events, triggering a one-off +* conversion here would stomp the AUTO bit out from under +* whoever enabled it, and there is no way to get a fresh +* single-shot value without doing so -- refuse instead of +* returning stale data. +*/ +if (st->autoconv_users >0 ) +return -EBUSY ; + + ret =regmap_write (st->regmap,MAX30210_TEMP_CONV_REG , +MAX30210_TEMPCONV_CONV_T ); +if (ret) +return ret; + +/* +* Wait until CONVERT_T auto-clears. +* Datasheet:
drivers/iio/temperature/max30210.c line 494
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +9
-0+* tBIAS_WU = 260 µs +* tINT = 8 ms +* +* Worst-case conversion ≈ 8.26 ms. +* Use 10 ms timeout for margin. +*/ + ret =regmap_read_poll_timeout (st->regmap,MAX30210_TEMP_CONV_REG , uval, + !(uval &MAX30210_TEMPCONV_CONV_T ), +500 ,10 *USEC_PER_MSEC );
Logical split
ret = regmap_read_poll_timeout(map, MAX30210_TEMP_CONV_REG, uval, !(uval & MAX30210_TEMPCONV_CONV_T), 500, 10 * USEC_PER_MSEC);
drivers/iio/temperature/max30210.c line 498
Quoted 4 lines
drivers/iio/temperature/max30210.c new file +4
-0+if (ret) +return ret; + +return max30210_read_temp (st->regmap,MAX30210_TEMP_DATA_REG , val);
return max30210_read_temp(map, MAX30210_TEMP_DATA_REG, val);
will fit into 80 characters limit.
Quoted 5 lines
drivers/iio/temperature/max30210.c new file +5
-0+ } +default : +return -EINVAL ; + } +}
...
drivers/iio/temperature/max30210.c line 566
Quoted 13 lines
drivers/iio/temperature/max30210.c new file +13
-0+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 <1 || val >MAX30210_FIFO_SIZE ) +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. +*/
The rule of thumb is to make lines in the comments balanced (by length):
/*
* 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.
*/
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ 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 ; +}
...
Quoted 11 lines
drivers/iio/temperature/max30210.c new file +11
-0+static int max30210_buffer_preenable (struct iio_dev *indio_dev) +{ +struct max30210_state *st =iio_priv (indio_dev); +int ret; + +/* Enable FIFO-full interrupt */ + ret =regmap_set_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); +if (ret) +return ret; +
drivers/iio/temperature/max30210.c line 619
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +9
-0+/* Flush FIFO before starting autonomous conversions */ + ret =regmap_set_bits (st->regmap,MAX30210_FIFO_CONF_2_REG , +MAX30210_FIFOCONF2_FLUSH_FIFO ); +if (ret) + goto err_disable_irq; +scoped_guard (mutex, &st->lock) + ret = max30210_autoconv_get(st); +if (ret) + goto err_disable_irq;
Oh, does it compile this way with scoped_guard()?
Quoted 20 lines
drivers/iio/temperature/max30210.c new file +19
-0+return 0 ; + +err_disable_irq: +/* The core won't call postdisable() when preenable() fails. */ +regmap_clear_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); + +return ret; +} +static int max30210_buffer_postdisable (struct iio_dev *indio_dev) +{ +struct max30210_state *st =iio_priv (indio_dev); +int ret, err; + +scoped_guard (mutex, &st->lock) + err = max30210_autoconv_put(st); + +/* Keep going on error so the interrupt is always disabled below. */ + ret = regmap_set_bits(st->regmap, MAX30210_FIFO_CONF_2_REG,
drivers/iio/temperature/max30210.c line 650
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ MAX30210_FIFOCONF2_FLUSH_FIFO); +if (!err) + err = ret; + +/* Disable FIFO-full interrupt */ + ret =regmap_clear_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); + +return err ?: ret; +}
TBH, I don't like how these two callbacks are implemented in terms of goto:s and additional error code variable, but I have nothing to suggest, so we would live with the above... :-(
...
drivers/iio/temperature/max30210.c line 703
Quoted 6 lines
drivers/iio/temperature/max30210.c new file +6
-0+static void max30210_powerdown (void *data) +{ +struct gpio_desc *powerdown_gpio = data; + +gpiod_set_value (powerdown_gpio,1 ); +}
static void max30210_powerdown(void *powerdown_gpio)
{
gpiod_set_value(powerdown_gpio, 1);
}...
drivers/iio/temperature/max30210.c line 709
Quoted 5 lines
drivers/iio/temperature/max30210.c new file +5
-0+static void max30210_quiesce (void *data) +{ +struct max30210_state *st = data; + +/* Stop interrupt sources and autonomous conversions; errors are not actionable here */
Make it multi-line to fit 80 characters limit.
Quoted 3 lines
drivers/iio/temperature/max30210.c new file +3
-0+regmap_write (st->regmap,MAX30210_INT_EN_REG ,0 ); +regmap_write (st->regmap,MAX30210_TEMP_CONV_REG ,0 ); +}
...
Quoted 11 lines
drivers/iio/temperature/max30210.c new file +11
-0+static int max30210_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct iio_dev *indio_dev; +struct max30210_state *st; +int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*st)); +if (!indio_dev) +return -ENOMEM ; +
drivers/iio/temperature/max30210.c line 772
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ st =iio_priv (indio_dev); + + ret =devm_mutex_init (dev, &st->lock); +if (ret) +return ret; + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to enable vdd regulator.\n" );
Here it is fine to have a single line
return dev_err_probe(dev, ret, "Failed to enable vdd regulator.\n");
Quoted 11 lines
drivers/iio/temperature/max30210.c new file +11
-0+ st->regmap =devm_regmap_init_i2c (client, &max30210_regmap); +if (IS_ERR (st->regmap)) +return dev_err_probe (dev,PTR_ERR (st->regmap), +"Failed to allocate regmap.\n" ); + + st->watermark =MAX30210_WATERMARK_DEFAULT ; + + ret =max30210_setup (st, dev); +if (ret) +return ret; +
drivers/iio/temperature/max30210.c line 794
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+ indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = &max30210_channel; + indio_dev->num_channels =1 ; + indio_dev->name ="max30210" ; + indio_dev->info = &max30210_info; + +/* Currently, without an interrupt there is no way to drain the FIFO into the buffer */ +if (client->irq) { + ret =devm_iio_kfifo_buffer_setup_ext (dev, indio_dev, &max30210_buffer_ops, + max30210_fifo_attributes);
ret = devm_iio_kfifo_buffer_setup_ext(dev, indio_dev, &max30210_buffer_ops, max30210_fifo_attributes);
Quoted 14 lines
drivers/iio/temperature/max30210.c new file +14
-0+if (ret) +return ret; + +/* Runs after unregister, so nothing can re-arm the device; before powerdown. */ + ret =devm_add_action_or_reset (dev, max30210_quiesce, st); +if (ret) +return ret; + } + + ret =devm_iio_device_register (dev, indio_dev); +if (ret) +return ret; + +if (!client->irq)
drivers/iio/temperature/max30210.c line 818
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+return 0 ; + +/* +* Request the IRQ after registering the IIO device so devm tears it +* down first on removal, before the event interface is freed. +*/ +return devm_request_threaded_irq (dev, client->irq,NULL , + max30210_irq_handler,IRQF_ONESHOT , + indio_dev->name, indio_dev); +}
...
Module description:
Quoted 1 line
+MODULE_DESCRIPTION("MAX30210 low-power digital temperature sensor");
Kconfig:
drivers/iio/temperature/Kconfig config MAX30208 · line 156
Quoted 1 line
drivers/iio/temperature/Kconfig +1
-0+ tristate "Analog Devices MAX30210 temperature sensor"
Top comment:
Quoted 1 line
* Analog Devices MAX30210 I2C Temperature Sensor driver
Make all three texts to be the same.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jonathan Cameron
maintainerRe: [PATCH v5 2/2] iio: temperature: add support for Analog Devices MAX30210
In reply to John Erasmus Mari GeronimoQuoted 12 lines · On Fri, 23 Oct 2026 16:06:23 +0800 John Erasmus Mari Geronimo…
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
Quoted 5 lines
The device provides 16-bit signed temperature data and a 64-sample FIFO.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf
Signed-off-by John Erasmus Mari Geronimo <[email protected]>
A few things from sashiko
https://sashiko.dev/#/patchset/20261023080623.1830-1-johnerasmusmari.geronimo%40analog.com
Feel free to just reply to the thread saying why they are wrong if they are.
In particularly it reminded me of the reply I made to patch 2 about what we might want to do to fix the event interface lifetime issue. I haven't had a chance to get back to that since.
Quoted 15 lines
drivers/iio/temperature/max30210.c new file +9
-0+ +static int max30210_buffer_preenable (struct iio_dev *indio_dev) +{ +struct max30210_state *st =iio_priv (indio_dev); +int ret; + +/* Enable FIFO-full interrupt */ + ret =regmap_set_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK );
drivers/iio/temperature/max30210.c line 616
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+if (ret) +return ret; + +/* Flush FIFO before starting autonomous conversions */ + ret =regmap_set_bits (st->regmap,MAX30210_FIFO_CONF_2_REG , +MAX30210_FIFOCONF2_FLUSH_FIFO ); +if (ret) + goto err_disable_irq; + +scoped_guard (mutex, &st->lock)
See the stuff in cleanup.h Don't combine guards and gotos in same function. They may be fine in some cases but it is considered bad practice. Here I'd just not use the guard.
Make sure to check for other instances of this.
Quoted 71 lines
drivers/iio/temperature/max30210.c new file +69
-0+ ret =max30210_autoconv_get (st); +if (ret) + goto err_disable_irq; + +return 0 ; + +err_disable_irq: +/* The core won't call postdisable() when preenable() fails. */ +regmap_clear_bits (st->regmap,MAX30210_INT_EN_REG , +MAX30210_STATUS_A_FULL_MASK ); + +return ret; +} +static int max30210_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev; +struct iio_dev *indio_dev; +struct max30210_state *st; +int ret; + + indio_dev =devm_iio_device_alloc (dev,sizeof (*st)); +if (!indio_dev) +return -ENOMEM ; + + st =iio_priv (indio_dev); + + ret =devm_mutex_init (dev, &st->lock); +if (ret) +return ret; + + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret, +"Failed to enable vdd regulator.\n" ); + + st->regmap =devm_regmap_init_i2c (client, &max30210_regmap); +if (IS_ERR (st->regmap)) +return dev_err_probe (dev,PTR_ERR (st->regmap), +"Failed to allocate regmap.\n" ); + + st->watermark =MAX30210_WATERMARK_DEFAULT ; + + ret =max30210_setup (st, dev); +if (ret) +return ret; + + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = &max30210_channel; + indio_dev->num_channels =1 ; + indio_dev->name ="max30210" ; + indio_dev->info = &max30210_info; + +/* Currently, without an interrupt there is no way to drain the FIFO into the buffer */ +if (client->irq) { + ret =devm_iio_kfifo_buffer_setup_ext (dev, indio_dev, &max30210_buffer_ops, + max30210_fifo_attributes); +if (ret) +return ret; + +/* Runs after unregister, so nothing can re-arm the device; before powerdown. */ + ret =devm_add_action_or_reset (dev, max30210_quiesce, st); +if (ret) +return ret; + } + + ret =devm_iio_device_register (dev, indio_dev); +if (ret) +return ret; +
drivers/iio/temperature/max30210.c line 817
Quoted 10 lines
drivers/iio/temperature/max30210.c new file +10
-0+if (!client->irq) +return 0 ; + +/* +* Request the IRQ after registering the IIO device so devm tears it +* down first on removal, before the event interface is freed. +*/ +return devm_request_threaded_irq (dev, client->irq,NULL , + max30210_irq_handler,IRQF_ONESHOT , + indio_dev->name, indio_dev);
Please take a look at what I said on v4 about this. I agree there is an issue in the core code but don't want to fix it by introducing a race here. It would be helpful to know if the code I put in that thread works or not. If you are seeing reasons not to make a change along those lines reply to that thread to continue the discussion.
Jonathan
Quoted 1 line
+}
Re: [PATCH v5 2/2] iio: temperature: add support for Analog Devices MAX30210
Quoted 4 lines · Jonathan Cameron
A few things from sashiko
Feel free to just reply to the thread saying why they are wrong if they are.
Thanks. I will address the sashiko findings and your guard/goto comment in v6, with one exception: requesting the interrupt before devm_iio_device_register().
The report says the event interface is freed in iio_dev_release(). In the current tree it is freed in iio_device_unregister_eventset() (kfree(ev_int)), i.e. during unregister. With the interrupt requested first, devm unregisters the device before it frees the interrupt, so the handler can run iio_push_event() while the event interface is being freed. Requesting the interrupt after registering avoids that for now.
Quoted 3 lines
In particularly it reminded me of the reply I made to patch 2 about what we might want to do to fix the event interface lifetime issue. I haven't had a chance to get back to that since.
No problem. I am happy to flip the order once the lifetime is fixed in the core. My test setup runs a 6.12 based kernel, so I can only build test your patch, not run it on the hardware. If you post it as a series I will build test it against this driver and say so.
Thanks,
John Erasmus
Guenter Roeck
maintainerQuoted 5 lines · John Erasmus Mari Geronimo
This series adds support for the Analog Devices MAX30210 I2C temperature sensor.
Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/max30210.pdf
Quoted 8 lines
The device provides a 16-bit temperature output and includes features such as a hardware FIFO, programmable sampling rate, threshold alarms, and interrupt support. These capabilities map naturally to the IIO subsystem, particularly the buffered interface and event framework -- hwmon has no equivalent buffered-capture framework, and its alarm attributes are polled rather than interrupt-driven, so there is no path to supporting the FIFO or the autonomous threshold comparators there.
Oh, please, do not make up stuff. The hardware monitoring subsystem does support interrupts, but it is not mandatory. Sure, most drivers don't implement it, but that does not mean it is not supported.
Guenter
Quoted 10 lines · Guenter Roeck
Quoted 8 lines · John Erasmus Mari Geronimo
The device provides a 16-bit temperature output and includes features such as a hardware FIFO, programmable sampling rate, threshold alarms, and interrupt support. These capabilities map naturally to the IIO subsystem, particularly the buffered interface and event framework -- hwmon has no equivalent buffered-capture framework, and its alarm attributes are polled rather than interrupt-driven, so there is no path to supporting the FIFO or the autonomous threshold comparators there.
Quoted 3 lines
Oh, please, do not make up stuff. The hardware monitoring subsystem does support interrupts, but it is not mandatory. Sure, most drivers don't implement it, but that does not mean it is not supported.
You are right, sorry about that. hwmon does support alarm interrupts (for example via hwmon_notify_event()); they are just optional. My claim that alarms are polled only, and that there is no path for the threshold comparators in hwmon, was wrong. I will remove it from the cover letter.
The reason I went with IIO is narrower than what I wrote: the MAX30210 has a 64-sample hardware FIFO with a watermark interrupt, and I want to expose it as buffered capture (IIO kfifo buffer). As far as I can see hwmon has no equivalent for streaming samples out of a hardware FIFO. The related MAX30208 is an IIO driver as well.
Thanks,
John Erasmus