Skip to contentSkip to conversation

iio: health: add MAX86150 ECG and PPG biosensor driver

PATCH Superseded Md Shofiqul Islam 3 participants 3 messages Jul 7 IIO

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

PATCH v8 2/2 iio: health: add MAX86150 ECG and PPG biosensor driver

Add a new IIO driver for the Analog Devices MAX86150 integrated biosensor, which combines two PPG optical channels (Red/IR LED) and one ECG biopotential channel in a single I2C device.

The device has a 32-entry hardware FIFO with a configurable almost-full interrupt. Because all samples arrive via the FIFO, the driver uses a kfifo buffer (like the sibling MAX30100 and MAX30102 drivers) rather than the triggered-buffer framework. The interrupt handler drains the FIFO on each A_FULL event and timestamps samples back-calculated from the interrupt arrival time by one sample_period_ns per step.

Key implementation details:
- FIFO draining via iio_buffer_setup_ops postenable/predisable
- DMA-safe FIFO read buffer aligned to IIO_DMA_MINALIGN
- IIO_DECLARE_BUFFER_WITH_TS for the push buffer
- 24-bit FIFO words decoded via get_unaligned_be24()
- regmap_set_bits() / regmap_clear_bits() for single-direction writes
- Overflow drops all samples; timestamps are unreliable after overflow
- Device remains in shutdown between captures to suppress LED current
- vdd and vled regulators required per datasheet; vref is not a supply

---
 MAINTAINERS                   |   7 +
 drivers/iio/health/Kconfig    |  18 ++
 drivers/iio/health/Makefile   |   1 +
 drivers/iio/health/max86150.c | 533 ++++++++++++++++++++++++++++++++++
 4 files changed, 559 insertions(+)
 create mode 100644 drivers/iio/health/max86150.c
MAINTAINERS +7 -0
index 3115538ce8291..361a7c8b99ead 100644
@@ -15863,6 +15863,13 @@ S:	Supported
 F:	Documentation/devicetree/bindings/power/supply/maxim,max77976.yaml
 F:	drivers/power/supply/max77976_charger.c
 
+MAX86150 ECG AND PPG BIOSENSOR DRIVER
+M:	Md Shofiqul Islam <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	Documentation/devicetree/bindings/iio/health/adi,max86150.yaml
+F:	drivers/iio/health/max86150.c
+
 MAXIM MUIC CHARGER DRIVERS FOR EXYNOS BASED BOARDS
 M:	Krzysztof Kozlowski <[email protected]>
 L:	[email protected]
drivers/iio/health/Kconfig +18 -0
index a89f3abf11f4a..e5b8b6bb9351e 100644
@@ -62,4 +62,22 @@ config MAX30102
 
 endmenu
 
+
+config MAX86150
+	tristate "MAX86150 ECG and PPG biosensor"
+	depends on I2C
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	select REGMAP_I2C
+	help
+	  Say Y here to enable support for the Maxim MAX86150 combined
+	  ECG and photoplethysmography (PPG) biosensor.
+
+	  The driver exposes three IIO channels: two PPG optical channels
+	  (Red and IR LED) for heart rate and SpO2 monitoring, and one
+	  ECG channel for biopotential recording.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called max86150.
+
 endmenu
drivers/iio/health/Makefile +1 -0
index 9108171122588..04fc73c584449 100644
@@ -9,3 +9,4 @@ obj-$(CONFIG_AFE4403)		+= afe4403.o
 obj-$(CONFIG_AFE4404)		+= afe4404.o
 obj-$(CONFIG_MAX30100)		+= max30100.o
 obj-$(CONFIG_MAX30102)		+= max30102.o
+obj-$(CONFIG_MAX86150)		+= max86150.o
drivers/iio/health/max86150.c new file +533 -0

533 lines not on this page · raw message

Signature · 1 line
2.51.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Critical] Lack of IRQ synchronization during buffer predisable leads to Use-After-Free/NULL-pointer…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Critical] Lack of IRQ synchronization during buffer predisable leads to Use-After-Free/NULL-pointer dereference of active_scan_mask.
- [High] FIFO rollover and full condition handling causes an IRQ storm and deadlocks data capture.
- [High] Stale interrupt status flags are not cleared before polling in direct read mode, returning garbage data.
- [High] Interrupt handler returns IRQ_HANDLED on I2C read failure, causing a continuous IRQ storm.
- [High] Concurrent access to shared fifo_raw buffer causes data corruption and unsafe DMA memory sharing.
- [Medium] Missing Power-On Reset (POR) delay after enabling regulators causes I2C communication to fail.
--

--- Patch [2]: [PATCH v8 2/2] iio: health: add MAX86150 ECG and PPG biosensor driver --- commit d1ed22d53669726571ae3dc244a73238d9b89d76
Author: Md Shofiqul Islam <[email protected]>

iio: health: add MAX86150 ECG and PPG biosensor driver

Add a new IIO driver for the Analog Devices MAX86150 integrated biosensor. The device has a 32-entry hardware FIFO with a configurable almost-full interrupt.

Quoted 5 lines
drivers/iio/health/max86150.c new file +0 -0
new file mode 100644
index 0000000000000..1e3d28cb41b70

[ ... ]

drivers/iio/health/max86150.c line 160

Quoted 7 lines
drivers/iio/health/max86150.c new file +7 -0
+static int max86150_read_one_sample(struct max86150_data *data,
+				    u32 *ppg_red, u32 *ppg_ir, s32 *ecg)
+{
+	int ret;
+
+	ret = regmap_noinc_read(data->regmap, MAX86150_REG_FIFO_DATA,
+				data->fifo_raw, MAX86150_SAMPLE_BYTES);

[Severity: High]
Are there concurrent access issues with the shared fifo_raw buffer?

The data->fifo_raw buffer appears to be shared between direct mode (max86150_read_raw) and buffer mode (max86150_interrupt_handler). Since there is no private mutex, both contexts might execute max86150_read_one_sample() simultaneously.

Could this pass the same fifo_raw pointer to regmap_noinc_read() concurrently, potentially causing overlapping DMA maps or interleaved writes to the buffer?

[ ... ]

Quoted 30 lines
drivers/iio/health/max86150.c new file +30 -0
+static int max86150_read_raw(struct iio_dev *indio_dev,
+			     struct iio_chan_spec const *chan,
+			     int *val, int *val2, long mask)
+{
+	struct max86150_data *data = iio_priv(indio_dev);
+	unsigned int ppg_rdy_status;
+	u32 ppg_red, ppg_ir;
+	s32 ecg;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_RAW:
+		if (!iio_device_claim_direct(indio_dev))
+			return -EBUSY;
+
+		ret = regmap_clear_bits(data->regmap, MAX86150_REG_SYS_CTRL,
+					MAX86150_SYS_CTRL_SHDN);
+		if (ret)
+			goto out_shutdown;
+
+		ret = regmap_write(data->regmap, MAX86150_REG_FIFO_WR_PTR, 0);
+		if (ret)
+			goto out_shutdown;
+		ret = regmap_write(data->regmap, MAX86150_REG_OVF_COUNTER, 0);
+		if (ret)
+			goto out_shutdown;
+		ret = regmap_write(data->regmap, MAX86150_REG_FIFO_RD_PTR, 0);
+		if (ret)
+			goto out_shutdown;
+

drivers/iio/health/max86150.c line 211

Quoted 10 lines
drivers/iio/health/max86150.c new file +10 -0
+		/*
+		 * Poll PPG_RDY rather than sleeping a fixed interval -- the
+		 * internal oscillator may start slower than nominal, leaving
+		 * the FIFO empty if we read too early.
+		 */
+		ret = regmap_read_poll_timeout(data->regmap,
+					       MAX86150_REG_INT_STATUS1,
+					       ppg_rdy_status,
+					       ppg_rdy_status & MAX86150_INT_PPG_RDY,
+					       1000, 25000);

[Severity: High]
Will this polling loop exit prematurely if there are stale interrupt status flags?

If MAX86150_INT_PPG_RDY was set during a previous active period, the INT_STATUS1 register might still hold this bit. Because the driver resets the FIFO pointers to 0 and immediately calls regmap_read_poll_timeout() without doing a dummy read to clear INT_STATUS1, the loop could exit immediately on the first iteration.

The driver would then read an empty FIFO and return invalid data.

[ ... ]

drivers/iio/health/max86150.c line 288

Quoted 9 lines
drivers/iio/health/max86150.c new file +9 -0
+static int max86150_buffer_predisable(struct iio_dev *indio_dev)
+{
+	struct max86150_data *data = iio_priv(indio_dev);
+
+	regmap_write(data->regmap, MAX86150_REG_INT_ENABLE1, 0);
+	regmap_set_bits(data->regmap, MAX86150_REG_SYS_CTRL,
+			MAX86150_SYS_CTRL_SHDN);
+	return 0;
+}

[Severity: Critical]
Is it possible for the interrupt handler to execute after this function returns?

When the IIO buffer is disabled, this stops new hardware interrupts via I2C, but it doesn't call synchronize_irq(). The IIO core will then proceed to free indio_dev->active_scan_mask and set it to NULL.

If the threaded IRQ handler is currently executing or preempted on another CPU, won't it subsequently call test_bit(..., indio_dev->active_scan_mask) and dereference the NULL pointer?

[ ... ]

drivers/iio/health/max86150.c line 307

Quoted 13 lines
drivers/iio/health/max86150.c new file +13 -0
+static irqreturn_t max86150_interrupt_handler(int irq, void *private)
+{
+	struct iio_dev *indio_dev = private;
+	struct max86150_data *data = iio_priv(indio_dev);
+	unsigned int status, wr_ptr, rd_ptr, ovf;
+	u32 ppg_red, ppg_ir;
+	s32 ecg;
+	s64 ts;
+	int ret, n_avail, i, j;
+
+	ret = regmap_read(data->regmap, MAX86150_REG_INT_STATUS1, &status);
+	if (ret)
+		return IRQ_HANDLED;

[Severity: High]
Does returning IRQ_HANDLED on read failure risk causing an IRQ storm?

If the I2C read fails and the handler returns IRQ_HANDLED, the kernel will unmask the interrupt line. Because the interrupt is level-triggered and the device still holds the line asserted, it will immediately fire again. Returning IRQ_NONE would allow the kernel's spurious interrupt detector to safely disable the line.

[ ... ]

drivers/iio/health/max86150.c line 333

Quoted 10 lines
drivers/iio/health/max86150.c new file +10 -0
+	ret = regmap_read(data->regmap, MAX86150_REG_FIFO_WR_PTR, &wr_ptr);
+	if (ret)
+		return IRQ_HANDLED;
+	ret = regmap_read(data->regmap, MAX86150_REG_FIFO_RD_PTR, &rd_ptr);
+	if (ret)
+		return IRQ_HANDLED;
+
+	n_avail = (wr_ptr - rd_ptr) & (MAX86150_FIFO_DEPTH - 1);
+	if (!n_avail)
+		return IRQ_HANDLED;

[Severity: High]
Can n_avail evaluate to 0 when the FIFO is completely full?

If the CPU is delayed and the 32-entry FIFO fills completely, wr_ptr wraps and equals rd_ptr. The calculation (wr_ptr - rd_ptr) & 31 evaluates to 0.

Since the device initialization enables MAX86150_FIFO_CONFIG_ROLLOVER_EN, the hardware OVF_COUNTER is never incremented. The handler will see OVF_COUNTER == 0 and n_avail == 0, and exit with IRQ_HANDLED without reading any data.

Could this cause an IRQ storm as the hardware re-asserts the A_FULL interrupt immediately because the FIFO remains full?

[ ... ]

Quoted 19 lines
drivers/iio/health/max86150.c new file +19 -0
+static int max86150_probe(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev;
+	struct max86150_data *data;
+	unsigned int part_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+
+	ret = devm_regulator_get_enable(&client->dev, "vdd");
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to get/enable vdd supply\n");
+
+	ret = devm_regulator_get_enable(&client->dev, "vled");

drivers/iio/health/max86150.c line 461

Quoted 10 lines
drivers/iio/health/max86150.c new file +10 -0
+	if (ret)
+		return dev_err_probe(&client->dev, ret,
+				     "Failed to get/enable vled supply\n");
+
+	data->regmap = devm_regmap_init_i2c(client, &max86150_regmap_config);
+	if (IS_ERR(data->regmap))
+		return dev_err_probe(&client->dev, PTR_ERR(data->regmap),
+				     "Failed to initialise regmap\n");
+
+	ret = regmap_read(data->regmap, MAX86150_REG_PART_ID, &part_id);

[Severity: Medium]
Is a Power-On Reset delay needed after enabling the regulators?

The regulators might need time to stabilize, and the device typically requires a startup period before its I2C interface becomes responsive. If regmap_read() happens immediately after enabling the supplies, can this lead to an I2C communication failure during probe?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#2 lore raw
In reply to Md Shofiqul Islam
Quoted 20 lines · Md Shofiqul Islam

Add a new IIO driver for the Analog Devices MAX86150 integrated biosensor, which combines two PPG optical channels (Red/IR LED) and one ECG biopotential channel in a single I2C device.

The device has a 32-entry hardware FIFO with a configurable almost-full interrupt. Because all samples arrive via the FIFO, the driver uses a kfifo buffer (like the sibling MAX30100 and MAX30102 drivers) rather than the triggered-buffer framework. The interrupt handler drains the FIFO on each A_FULL event and timestamps samples back-calculated from the interrupt arrival time by one sample_period_ns per step.

Key implementation details:
- FIFO draining via iio_buffer_setup_ops postenable/predisable
- DMA-safe FIFO read buffer aligned to IIO_DMA_MINALIGN
- IIO_DECLARE_BUFFER_WITH_TS for the push buffer
- 24-bit FIFO words decoded via get_unaligned_be24()
- regmap_set_bits() / regmap_clear_bits() for single-direction writes
- Overflow drops all samples; timestamps are unreliable after overflow
- Device remains in shutdown between captures to suppress LED current
- vdd and vled regulators required per datasheet; vref is not a supply

...

Quoted 3 lines

endmenu

+

Now too many blank lines. It rings a bell that I have already commented on this in v6 or so.

Quoted 18 lines
drivers/iio/health/Kconfig +17 -0
+config MAX86150
+	tristate "MAX86150 ECG and PPG biosensor"
+	depends on I2C
+	select IIO_BUFFER
+	select IIO_KFIFO_BUF
+	select REGMAP_I2C
+	help
+	  Say Y here to enable support for the Maxim MAX86150 combined
+	  ECG and photoplethysmography (PPG) biosensor.
+
+	  The driver exposes three IIO channels: two PPG optical channels
+	  (Red and IR LED) for heart rate and SpO2 monitoring, and one
+	  ECG channel for biopotential recording.
+
+	  This driver can also be built as a module. If so, the module
+	  will be called max86150.
+
 endmenu

...

drivers/iio/health/max86150.c line 26

Quoted 10 lines
drivers/iio/health/max86150.c new file +10 -0
+#include <linux/bitfield.h>
+#include <linux/delay.h>
+#include <linux/i2c.h>
+#include <linux/irq.h>
+#include <linux/iio/buffer.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/kfifo_buf.h>
+#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/regulator/consumer.h>

+ types.h // uXX

Quoted 1 line

+#include <linux/unaligned.h>

Also sounds like I already commented on this and on the grouping of linux/iio/*.h. Since there are no links in the cover letter I can't quickly confirm. So I stop review here and waiting for v9 with properly formed series and cover letter, and take a chance to go through previous reviews to check if you addressed everything or replied to the comments you are not agree with.

...

drivers/iio/health/max86150.c line 510

Quoted 2 lines
drivers/iio/health/max86150.c new file +2 -0
+static const struct i2c_device_id max86150_id[] = {
+	{ "max86150" },

This should be C99 initialiser.

Quoted 2 lines
(quoted diff) +2 -0
+	{ }
+};
Signature · 2 lines
With Best Regards,
Andy Shevchenko