iio: pressure: add Sensirion SDP31 driver
Subsystems (2)
A message that is not in the archive (sent off-list or not yet received): [email protected]
v2 cover iio: pressure: add Sensirion SDP31 driver
This series adds support for the Sensirion SDP31 differential pressure sensor, an I2C device that also reports temperature.
The SDP31 uses a completely different I2C command protocol from the already supported SDP500 (command-triggered measurements with a per-word CRC-8), so extending the sdp500 driver does not make sense. No compatible parts from other vendors were found.
Patch 1 adds the device tree binding.
Patch 2 adds the IIO driver, which exposes differential pressure and temperature channels and validates each measurement using the sensor's CRC-8 checksum.
The driver was developed and tested on an SDP31 wired to the I2C bus of a Raspberry Pi 4.
Changes since v1:
- Explain in this cover letter why a new driver is added instead of extending the sdp500 driver.
- dt-bindings: make vdd-supply required, and enable the regulator in the driver using devm_regulator_get_enable().
- Move the MAINTAINERS entry into the dt-bindings patch; the driver patch only adds its own file.
- Populate the CRC-8 table with DO_ONCE() to avoid a race between concurrent probes.
- Use devm_mutex_init() and take the lock inside sdp31_measure() using guard(mutex).
- Build the command with __be16/cpu_to_be16() and parse the reply with get_unaligned_be16().
- Use a named initializer in the i2c_device_id table.
- Minor cleanups: local struct device *dev, single-line dev_err_probe(), and a comment on the mutex.
v1: https://lore.kernel.org/linux-iio/[email protected]/
Muhammad Abu Bakar (2):
dt-bindings: iio: pressure: add Sensirion SDP31
iio: pressure: add Sensirion SDP31 driver
.../iio/pressure/sensirion,sdp31.yaml | 41 ++++ MAINTAINERS | 6 + drivers/iio/pressure/Kconfig | 11 + drivers/iio/pressure/Makefile | 1 + drivers/iio/pressure/sdp31.c | 209 ++++++++++++++++++ 5 files changed, 268 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/pressure/sensirion,sdp31.yaml create mode 100644 drivers/iio/pressure/sdp31.c
Signature · 1 line
2.43.0
PATCH v2 1/2 dt-bindings: iio: pressure: add Sensirion SDP31
Add device tree bindings for the Sensirion SDP31 differential pressure sensor, an I2C device that also reports temperature.
--- .../iio/pressure/sensirion,sdp31.yaml | 41 +++++++++++++++++++ MAINTAINERS | 5 +++ 2 files changed, 46 insertions(+) create mode 100644 Documentation/devicetree/bindings/iio/pressure/sensirion,sdp31.yaml
Documentation/devicetree/bindings/iio/pressure/sensirion,sdp31.yaml new file +41 -0
@@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/iio/pressure/sensirion,sdp31.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Sensirion SDP31 differential pressure sensor + +maintainers: + - Muhammad Abu Bakar <[email protected]> + +description: | + Differential pressure sensor from Sensirion with an I2C interface. + +properties: + compatible: + const: sensirion,sdp31 + + reg: + maxItems: 1 + + vdd-supply: true + +required: + - compatible + - reg + - vdd-supply + +additionalProperties: false + +examples: + - | + i2c { + #address-cells = <1>; + #size-cells = <0>; + pressure@21 { + compatible = "sensirion,sdp31"; + reg = <0x21>; + vdd-supply = <&vdd_3v3>; + }; + };
MAINTAINERS +5 -0
@@ -24855,6 +24855,11 @@ S: Maintained F: Documentation/devicetree/bindings/iio/chemical/sensirion,scd4x.yaml F: drivers/iio/chemical/scd4x.c +SENSIRION SDP31 DIFFERENTIAL PRESSURE SENSOR DRIVER +M: Muhammad Abu Bakar <[email protected]> +S: Maintained +F: Documentation/devicetree/bindings/iio/pressure/sensirion,sdp31.yaml + SENSIRION SDP500 DIFFERENTIAL PRESSURE SENSOR DRIVER M: Petar Stoykov <[email protected]> S: Maintained
Signature · 1 line
2.43.0
Re: [PATCH v2 1/2] dt-bindings: iio: pressure: add Sensirion SDP31
Quoted 2 lines · Muhammad Abu Bakar
Add device tree bindings for the Sensirion SDP31 differential pressure sensor, an I2C device that also reports temperature.
I don't see how you addressed previous feedback.
Best regards,
Krzysztof
PATCH v2 2/2 iio: pressure: add Sensirion SDP31 driver
In reply to Muhammad Abu BakarAdd an IIO driver for the Sensirion SDP31 differential pressure sensor. The device is accessed over I2C and reports differential pressure and temperature. Each measurement is validated using the sensor's CRC-8 checksum.
Tested on an SDP31 connected to a Raspberry Pi 4 I2C bus.
--- MAINTAINERS | 1 + drivers/iio/pressure/Kconfig | 11 ++ drivers/iio/pressure/Makefile | 1 + drivers/iio/pressure/sdp31.c | 209 ++++++++++++++++++++++++++++++++++ 4 files changed, 222 insertions(+) create mode 100644 drivers/iio/pressure/sdp31.c
MAINTAINERS +1 -0
@@ -24859,6 +24859,7 @@ SENSIRION SDP31 DIFFERENTIAL PRESSURE SENSOR DRIVER M: Muhammad Abu Bakar <[email protected]> S: Maintained F: Documentation/devicetree/bindings/iio/pressure/sensirion,sdp31.yaml +F: drivers/iio/pressure/sdp31.c SENSIRION SDP500 DIFFERENTIAL PRESSURE SENSOR DRIVER M: Petar Stoykov <[email protected]>
drivers/iio/pressure/Kconfig +11 -0
@@ -286,6 +286,17 @@ config MS5637 This driver can also be built as a module. If so, the module will be called ms5637. +config SDP31 + tristate "Sensirion SDP31 differential pressure sensor I2C driver" + depends on I2C + select CRC8 + help + Say Y here to build support for Sensirion SDP31 differential pressure + sensor I2C driver. + + To compile this driver as a module, choose M here: the core module + will be called sdp31. + config SDP500 tristate "Sensirion SDP500 differential pressure sensor I2C driver" depends on I2C
drivers/iio/pressure/Makefile +1 -0
@@ -35,6 +35,7 @@ obj-$(CONFIG_MS5611) += ms5611_core.o obj-$(CONFIG_MS5611_I2C) += ms5611_i2c.o obj-$(CONFIG_MS5611_SPI) += ms5611_spi.o obj-$(CONFIG_MS5637) += ms5637.o +obj-$(CONFIG_SDP31) += sdp31.o obj-$(CONFIG_SDP500) += sdp500.o obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o st_pressure-y := st_pressure_core.o
drivers/iio/pressure/sdp31.c new file +209 -0
209 lines not on this page · raw message
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] iio: sdp31: missing sensor power-on delay after vdd enable --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] iio: sdp31: missing sensor power-on delay after vdd enable
--
commit 29a27ea5bbd1f787014c3c0a7f644c621fff7ae3
Author: Muhammad Abu Bakar <[email protected]>
iio: pressure: add Sensirion SDP31 driver
Add an IIO driver for the Sensirion SDP31 differential pressure sensor. The device is accessed over I2C and reports differential pressure and temperature. Each measurement is validated using the sensor's CRC-8 checksum.
Quoted 5 lines
drivers/iio/pressure/sdp31.c new file +0
-0
[ ... ]
Quoted 21 lines
drivers/iio/pressure/sdp31.c new file +21
-0+static int sdp31_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev;/* #5 */ +struct iio_dev *indio_dev; +struct sdp31_data *data; +struct sdp31_reading r; +int ret; + +/* #9: get and enable the sensor's supply (auto-disabled on remove). */ + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"failed to enable regulator\n" ); + + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + data =iio_priv (indio_dev); + data->client = client; + + ret =devm_mutex_init (dev, &data->lock);/* #6 */
drivers/iio/pressure/sdp31.c line 171
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+if (ret) +return ret; + +/* #7: populate the shared CRC table exactly once, race-free. */ +DO_ONCE (crc8_populate_msb, sdp31_crc8_table,SDP31_CRC8_POLY ); + +/* Confirm the sensor is present and learn its scale factor. */ + ret =sdp31_measure (data, &r); +if (ret) +return dev_err_probe (dev, ret,"failed to read from sensor\n" );/* #8 */
[Severity: Medium]
Will this I2C transaction fail if the sensor is still booting?
The driver calls devm_regulator_get_enable() to turn on the vdd supply and then almost immediately calls sdp31_measure() to communicate with the sensor. If the regulator physically toggles power, the sensor might still be in its hardware initialization sequence and could NACK the initial I2C trigger command.
Does this need a startup delay between enabling the regulator and the first I2C transaction to ensure the sensor is fully ready to process commands, preventing an unconditional probe failure on such platforms?
Quoted 3 lines
drivers/iio/pressure/sdp31.c new file +3
-0+if (!r.scale) +return dev_err_probe (dev, -EINVAL ,"invalid scale factor\n" ); + data->dp_scale = r.scale;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Jonathan Cameron
maintainerQuoted 8 lines · On Sun, 20 Sep 2026 21:47:24 +0500 Muhammad Abu Bakar…
Add an IIO driver for the Sensirion SDP31 differential pressure sensor. The device is accessed over I2C and reports differential pressure and temperature. Each measurement is validated using the sensor's CRC-8 checksum.
Tested on an SDP31 connected to a Raspberry Pi 4 I2C bus.
Signed-off-by Muhammad Abu Bakar <[email protected]>
Hi Muhammad,
You seem to have a bunch of comments in the code calling out changes from earlier versions. Those may well be helpful whilst developing (or are they LLM generated?). Either way we don't want to merge them into the upstream kernel.
Otherwise a few minor comments inline
Thanks,
Jonathan
Quoted 12 lines
drivers/iio/pressure/sdp31.c new file +6
-0@@ -0,0 +1,209 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +#include <linux/cleanup.h> +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/i2c.h> +#include <linux/module.h>
drivers/iio/pressure/sdp31.c line 16
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+#include <linux/mutex.h> +#include <linux/once.h> +#include <linux/regulator/consumer.h> +#include <linux/unaligned.h> + +#include <linux/iio/iio.h> + +#define SDP31_CMD_TRIG_DP 0x362F +#define SDP31_MEAS_DELAY_MS 50 +#define SDP31_TEMP_SCALE 5
I'd rather see this one at last just inline in the place I'd look for the scale. The define isn't adding anything that isn't better indicated by how it is used.
drivers/iio/pressure/sdp31.c line 38
Quoted 9 lines
drivers/iio/pressure/sdp31.c new file +8
-0+#define SDP31_CRC8_POLY 0x31 +#define SDP31_CRC8_INIT 0xff + +/* #2: build the command as a be16 and send it directly. */ +static int sdp31_send_cmd (struct i2c_client *client,u16 cmd) +{ +__be16 buf =cpu_to_be16 (cmd); +int ret =i2c_master_send (client, (u8 *)&buf,sizeof (buf));
Tiny thing but I'd split into
int ret; ret = i2c_masster_send(); if (ret < 0)
as more important to have the code visibly coupled with the error check than the declaration of ret.
drivers/iio/pressure/sdp31.c line 48
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+ +if (ret <0 ) +return ret; +return (ret ==sizeof (buf)) ?0 : -EIO ; +} + +static int sdp31_check_crc (const u8 *word) +{ +if (crc8 (sdp31_crc8_table, word,2 ,SDP31_CRC8_INIT ) != word[2 ]) +return -EIO ;
As below. I'd have a blank line here.
Quoted 22 lines
drivers/iio/pressure/sdp31.c new file +22
-0+return 0 ; +} + +/* #3: takes sdp31_data and holds the lock for the whole transaction. */ +static int sdp31_measure (struct sdp31_data *data,struct sdp31_reading *out) +{ +u8 rx[9 ]; +int ret; + +guard (mutex)(&data->lock); + + ret =sdp31_send_cmd (data->client,SDP31_CMD_TRIG_DP ); +if (ret) +return ret; + +msleep (SDP31_MEAS_DELAY_MS ); + + ret =i2c_master_recv (data->client, rx,sizeof (rx)); +if (ret <0 ) +return ret; +if (ret !=sizeof (rx)) +return -EIO ;
drivers/iio/pressure/sdp31.c line 80
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+ +if (sdp31_check_crc (&rx[0 ]) || +sdp31_check_crc (&rx[3 ]) || +sdp31_check_crc (&rx[6 ])) +return -EIO ; + +/* #4: use the unaligned big-endian helper instead of manual shifts. */ + out->pressure = (s16 )get_unaligned_be16 (&rx[0 ]); + out->temp = (s16 )get_unaligned_be16 (&rx[3 ]); + out->scale =get_unaligned_be16 (&rx[6 ]);
Trivial but it helps readability a little to have a blank line before simple return statements like this.
Quoted 24 lines
drivers/iio/pressure/sdp31.c new file +24
-0+return 0 ; +} + +static int sdp31_read_raw (struct iio_dev *indio_dev, +struct iio_chan_spec const *chan, +int *val,int *val2,long mask) +{ +struct sdp31_data *data =iio_priv (indio_dev); +struct sdp31_reading r; +int ret; + +switch (mask) { +case IIO_CHAN_INFO_RAW : + ret =sdp31_measure (data, &r);/* locking now lives inside */ +if (ret) +return ret; +switch (chan->type) { +case IIO_PRESSURE : + *val = r.pressure; +return IIO_VAL_INT ; +case IIO_TEMP : + *val = r.temp; +return IIO_VAL_INT ; +default :
drivers/iio/pressure/sdp31.c line 114
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+return -EINVAL ; + } +case IIO_CHAN_INFO_SCALE : +switch (chan->type) { +case IIO_PRESSURE : + *val =1 ; + *val2 = data->dp_scale *1000 ; +return IIO_VAL_FRACTIONAL ; +case IIO_TEMP : + *val =SDP31_TEMP_SCALE ;
As above. *val = 5; is better than a define that brings no value other than making anyone reading the code go look to see what value it has.
Quoted 31 lines
drivers/iio/pressure/sdp31.c new file +30
-0+return IIO_VAL_INT ; +default : +return -EINVAL ; + } +default : +return -EINVAL ; + } +} + +static int sdp31_probe (struct i2c_client *client) +{ +struct device *dev = &client->dev;/* #5 */ +struct iio_dev *indio_dev; +struct sdp31_data *data; +struct sdp31_reading r; +int ret; + +/* #9: get and enable the sensor's supply (auto-disabled on remove). */ + ret =devm_regulator_get_enable (dev,"vdd" ); +if (ret) +return dev_err_probe (dev, ret,"failed to enable regulator\n" ); + + indio_dev =devm_iio_device_alloc (dev,sizeof (*data)); +if (!indio_dev) +return -ENOMEM ; + + data =iio_priv (indio_dev); + data->client = client; + + ret =devm_mutex_init (dev, &data->lock);/* #6 */
drivers/iio/pressure/sdp31.c line 171
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+if (ret) +return ret; + +/* #7: populate the shared CRC table exactly once, race-free. */ +DO_ONCE (crc8_populate_msb, sdp31_crc8_table,SDP31_CRC8_POLY ); + +/* Confirm the sensor is present and learn its scale factor. */ + ret =sdp31_measure (data, &r); +if (ret) +return dev_err_probe (dev, ret,"failed to read from sensor\n" );/* #8 */
As below, these need clearing out.
Quoted 5 lines
drivers/iio/pressure/sdp31.c new file +5
-0+if (!r.scale) +return dev_err_probe (dev, -EINVAL ,"invalid scale factor\n" ); + data->dp_scale = r.scale; + + indio_dev->name ="sdp31" ;
drivers/iio/pressure/sdp31.c line 186
Quoted 10 lines
drivers/iio/pressure/sdp31.c new file +10
-0+ indio_dev->info = &sdp31_info; + indio_dev->modes =INDIO_DIRECT_MODE ; + indio_dev->channels = sdp31_channels; + indio_dev->num_channels =ARRAY_SIZE (sdp31_channels); + +return devm_iio_device_register (dev, indio_dev); +} + +static const struct i2c_device_id sdp31_id[] = { + { .name ="sdp31" },/* #10: named initializer */
I'm guessing this is an LLM trying to be helpful and highlight changes? We don't want that record of changes in an upstream driver.
Quoted 3 lines
drivers/iio/pressure/sdp31.c new file +3
-0+ { } +}; +MODULE_DEVICE_TABLE (i2c, sdp31_id);
Quoted 33 lines · Muhammad Abu Bakar
This series adds support for the Sensirion SDP31 differential pressure sensor, an I2C device that also reports temperature.
The SDP31 uses a completely different I2C command protocol from the already supported SDP500 (command-triggered measurements with a per-word CRC-8), so extending the sdp500 driver does not make sense. No compatible parts from other vendors were found.
Patch 1 adds the device tree binding.
Patch 2 adds the IIO driver, which exposes differential pressure and temperature channels and validates each measurement using the sensor's CRC-8 checksum.The driver was developed and tested on an SDP31 wired to the I2C bus of a Raspberry Pi 4.
Changes since v1:
- Explain in this cover letter why a new driver is added instead of extending the sdp500 driver.
- dt-bindings: make vdd-supply required, and enable the regulator in the driver usingdevm_regulator_get_enable().
- Move theMAINTAINERSentry into the dt-bindings patch; the driver patch only adds its own file.
- Populate the CRC-8 table withDO_ONCE()to avoid a race between concurrent probes.
- Usedevm_mutex_init()and take the lock insidesdp31_measure()using guard(mutex).
- Build the command with __be16/cpu_to_be16() and parse the reply withget_unaligned_be16().
- Use a named initializer in thei2c_device_idtable.
- Minor cleanups: local struct device *dev, single-linedev_err_probe(), and a comment on the mutex.
Quoted 1 line
Link does not work.
Best regards,
Krzysztof