Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

iio: pressure: Add driver for Sensirion SDP500

Sensirion SDP500 is a digital differential pressure sensor. The sensor is
accessed over I2C.

Signed-off-by: Petar Stoykov <pd.pstoykov@gmail.com>
Link: https://patch.msgid.link/20240725-mainline_sdp500-v4-2-ea2f5b189958@gmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Petar Stoykov and committed by
Jonathan Cameron
6ddb86d9 0302c9dc

+166
+9
drivers/iio/pressure/Kconfig
··· 250 250 This driver can also be built as a module. If so, the module will 251 251 be called ms5637. 252 252 253 + config SDP500 254 + tristate "Sensirion SDP500 differential pressure sensor I2C driver" 255 + depends on I2C 256 + help 257 + Say Y here to build support for Sensirion SDP500 differential pressure 258 + sensor I2C driver. 259 + To compile this driver as a module, choose M here: the core module 260 + will be called sdp500. 261 + 253 262 config IIO_ST_PRESS 254 263 tristate "STMicroelectronics pressure sensor Driver" 255 264 depends on (I2C || SPI_MASTER) && SYSFS
+1
drivers/iio/pressure/Makefile
··· 30 30 obj-$(CONFIG_MS5611_I2C) += ms5611_i2c.o 31 31 obj-$(CONFIG_MS5611_SPI) += ms5611_spi.o 32 32 obj-$(CONFIG_MS5637) += ms5637.o 33 + obj-$(CONFIG_SDP500) += sdp500.o 33 34 obj-$(CONFIG_IIO_ST_PRESS) += st_pressure.o 34 35 st_pressure-y := st_pressure_core.o 35 36 st_pressure-$(CONFIG_IIO_BUFFER) += st_pressure_buffer.o
+156
drivers/iio/pressure/sdp500.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Driver for Sensirion sdp500 and sdp510 pressure sensors 4 + * 5 + * Datasheet: https://sensirion.com/resource/datasheet/sdp600 6 + */ 7 + 8 + #include <linux/i2c.h> 9 + #include <linux/crc8.h> 10 + #include <linux/iio/iio.h> 11 + #include <linux/mod_devicetable.h> 12 + #include <linux/regulator/consumer.h> 13 + #include <asm/unaligned.h> 14 + 15 + #define SDP500_CRC8_POLYNOMIAL 0x31 /* x8+x5+x4+1 (normalized to 0x31) */ 16 + #define SDP500_READ_SIZE 3 17 + 18 + #define SDP500_I2C_START_MEAS 0xF1 19 + 20 + struct sdp500_data { 21 + struct device *dev; 22 + }; 23 + 24 + DECLARE_CRC8_TABLE(sdp500_crc8_table); 25 + 26 + static int sdp500_start_measurement(struct sdp500_data *data) 27 + { 28 + struct i2c_client *client = to_i2c_client(data->dev); 29 + 30 + return i2c_smbus_write_byte(client, SDP500_I2C_START_MEAS); 31 + } 32 + 33 + static const struct iio_chan_spec sdp500_channels[] = { 34 + { 35 + .type = IIO_PRESSURE, 36 + .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | 37 + BIT(IIO_CHAN_INFO_SCALE), 38 + }, 39 + }; 40 + 41 + static int sdp500_read_raw(struct iio_dev *indio_dev, 42 + struct iio_chan_spec const *chan, 43 + int *val, int *val2, long mask) 44 + { 45 + int ret; 46 + u8 rxbuf[SDP500_READ_SIZE]; 47 + u8 received_crc, calculated_crc; 48 + struct sdp500_data *data = iio_priv(indio_dev); 49 + struct i2c_client *client = to_i2c_client(data->dev); 50 + 51 + switch (mask) { 52 + case IIO_CHAN_INFO_RAW: 53 + ret = i2c_master_recv(client, rxbuf, SDP500_READ_SIZE); 54 + if (ret < 0) { 55 + dev_err(data->dev, "Failed to receive data"); 56 + return ret; 57 + } 58 + if (ret != SDP500_READ_SIZE) { 59 + dev_err(data->dev, "Data is received wrongly"); 60 + return -EIO; 61 + } 62 + 63 + received_crc = rxbuf[2]; 64 + calculated_crc = crc8(sdp500_crc8_table, rxbuf, 65 + sizeof(rxbuf) - 1, 0x00); 66 + if (received_crc != calculated_crc) { 67 + dev_err(data->dev, 68 + "calculated crc = 0x%.2X, received 0x%.2X", 69 + calculated_crc, received_crc); 70 + return -EIO; 71 + } 72 + 73 + *val = get_unaligned_be16(rxbuf); 74 + return IIO_VAL_INT; 75 + case IIO_CHAN_INFO_SCALE: 76 + *val = 1; 77 + *val2 = 60; 78 + 79 + return IIO_VAL_FRACTIONAL; 80 + default: 81 + return -EINVAL; 82 + } 83 + } 84 + 85 + static const struct iio_info sdp500_info = { 86 + .read_raw = &sdp500_read_raw, 87 + }; 88 + 89 + static int sdp500_probe(struct i2c_client *client) 90 + { 91 + struct iio_dev *indio_dev; 92 + struct sdp500_data *data; 93 + struct device *dev = &client->dev; 94 + int ret; 95 + u8 rxbuf[SDP500_READ_SIZE]; 96 + 97 + ret = devm_regulator_get_enable(dev, "vdd"); 98 + if (ret) 99 + return dev_err_probe(dev, ret, 100 + "Failed to get and enable regulator\n"); 101 + 102 + indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 103 + if (!indio_dev) 104 + return -ENOMEM; 105 + 106 + /* has to be done before the first i2c communication */ 107 + crc8_populate_msb(sdp500_crc8_table, SDP500_CRC8_POLYNOMIAL); 108 + 109 + data = iio_priv(indio_dev); 110 + data->dev = dev; 111 + 112 + indio_dev->name = "sdp500"; 113 + indio_dev->channels = sdp500_channels; 114 + indio_dev->info = &sdp500_info; 115 + indio_dev->modes = INDIO_DIRECT_MODE; 116 + indio_dev->num_channels = ARRAY_SIZE(sdp500_channels); 117 + 118 + ret = sdp500_start_measurement(data); 119 + if (ret) 120 + return dev_err_probe(dev, ret, "Failed to start measurement"); 121 + 122 + /* First measurement is not correct, read it out to get rid of it */ 123 + i2c_master_recv(client, rxbuf, SDP500_READ_SIZE); 124 + 125 + ret = devm_iio_device_register(dev, indio_dev); 126 + if (ret) 127 + return dev_err_probe(dev, ret, "Failed to register indio_dev"); 128 + 129 + return 0; 130 + } 131 + 132 + static const struct i2c_device_id sdp500_id[] = { 133 + { "sdp500" }, 134 + { } 135 + }; 136 + MODULE_DEVICE_TABLE(i2c, sdp500_id); 137 + 138 + static const struct of_device_id sdp500_of_match[] = { 139 + { .compatible = "sensirion,sdp500" }, 140 + { } 141 + }; 142 + MODULE_DEVICE_TABLE(of, sdp500_of_match); 143 + 144 + static struct i2c_driver sdp500_driver = { 145 + .driver = { 146 + .name = "sensirion,sdp500", 147 + .of_match_table = sdp500_of_match, 148 + }, 149 + .probe = sdp500_probe, 150 + .id_table = sdp500_id, 151 + }; 152 + module_i2c_driver(sdp500_driver); 153 + 154 + MODULE_AUTHOR("Thomas Sioutas <thomas.sioutas@prodrive-technologies.com>"); 155 + MODULE_DESCRIPTION("Driver for Sensirion SDP500 differential pressure sensor"); 156 + MODULE_LICENSE("GPL");