16ddb86d9SPetar Stoykov // SPDX-License-Identifier: GPL-2.0-only
26ddb86d9SPetar Stoykov /*
36ddb86d9SPetar Stoykov * Driver for Sensirion sdp500 and sdp510 pressure sensors
46ddb86d9SPetar Stoykov *
56ddb86d9SPetar Stoykov * Datasheet: https://sensirion.com/resource/datasheet/sdp600
66ddb86d9SPetar Stoykov */
76ddb86d9SPetar Stoykov
86ddb86d9SPetar Stoykov #include <linux/i2c.h>
96ddb86d9SPetar Stoykov #include <linux/crc8.h>
106ddb86d9SPetar Stoykov #include <linux/iio/iio.h>
116ddb86d9SPetar Stoykov #include <linux/mod_devicetable.h>
126ddb86d9SPetar Stoykov #include <linux/regulator/consumer.h>
13*5f60d5f6SAl Viro #include <linux/unaligned.h>
146ddb86d9SPetar Stoykov
156ddb86d9SPetar Stoykov #define SDP500_CRC8_POLYNOMIAL 0x31 /* x8+x5+x4+1 (normalized to 0x31) */
166ddb86d9SPetar Stoykov #define SDP500_READ_SIZE 3
176ddb86d9SPetar Stoykov
186ddb86d9SPetar Stoykov #define SDP500_I2C_START_MEAS 0xF1
196ddb86d9SPetar Stoykov
206ddb86d9SPetar Stoykov struct sdp500_data {
216ddb86d9SPetar Stoykov struct device *dev;
226ddb86d9SPetar Stoykov };
236ddb86d9SPetar Stoykov
246ddb86d9SPetar Stoykov DECLARE_CRC8_TABLE(sdp500_crc8_table);
256ddb86d9SPetar Stoykov
sdp500_start_measurement(struct sdp500_data * data)266ddb86d9SPetar Stoykov static int sdp500_start_measurement(struct sdp500_data *data)
276ddb86d9SPetar Stoykov {
286ddb86d9SPetar Stoykov struct i2c_client *client = to_i2c_client(data->dev);
296ddb86d9SPetar Stoykov
306ddb86d9SPetar Stoykov return i2c_smbus_write_byte(client, SDP500_I2C_START_MEAS);
316ddb86d9SPetar Stoykov }
326ddb86d9SPetar Stoykov
336ddb86d9SPetar Stoykov static const struct iio_chan_spec sdp500_channels[] = {
346ddb86d9SPetar Stoykov {
356ddb86d9SPetar Stoykov .type = IIO_PRESSURE,
366ddb86d9SPetar Stoykov .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
376ddb86d9SPetar Stoykov BIT(IIO_CHAN_INFO_SCALE),
386ddb86d9SPetar Stoykov },
396ddb86d9SPetar Stoykov };
406ddb86d9SPetar Stoykov
sdp500_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)416ddb86d9SPetar Stoykov static int sdp500_read_raw(struct iio_dev *indio_dev,
426ddb86d9SPetar Stoykov struct iio_chan_spec const *chan,
436ddb86d9SPetar Stoykov int *val, int *val2, long mask)
446ddb86d9SPetar Stoykov {
456ddb86d9SPetar Stoykov int ret;
466ddb86d9SPetar Stoykov u8 rxbuf[SDP500_READ_SIZE];
476ddb86d9SPetar Stoykov u8 received_crc, calculated_crc;
486ddb86d9SPetar Stoykov struct sdp500_data *data = iio_priv(indio_dev);
496ddb86d9SPetar Stoykov struct i2c_client *client = to_i2c_client(data->dev);
506ddb86d9SPetar Stoykov
516ddb86d9SPetar Stoykov switch (mask) {
526ddb86d9SPetar Stoykov case IIO_CHAN_INFO_RAW:
536ddb86d9SPetar Stoykov ret = i2c_master_recv(client, rxbuf, SDP500_READ_SIZE);
546ddb86d9SPetar Stoykov if (ret < 0) {
556ddb86d9SPetar Stoykov dev_err(data->dev, "Failed to receive data");
566ddb86d9SPetar Stoykov return ret;
576ddb86d9SPetar Stoykov }
586ddb86d9SPetar Stoykov if (ret != SDP500_READ_SIZE) {
596ddb86d9SPetar Stoykov dev_err(data->dev, "Data is received wrongly");
606ddb86d9SPetar Stoykov return -EIO;
616ddb86d9SPetar Stoykov }
626ddb86d9SPetar Stoykov
636ddb86d9SPetar Stoykov received_crc = rxbuf[2];
646ddb86d9SPetar Stoykov calculated_crc = crc8(sdp500_crc8_table, rxbuf,
656ddb86d9SPetar Stoykov sizeof(rxbuf) - 1, 0x00);
666ddb86d9SPetar Stoykov if (received_crc != calculated_crc) {
676ddb86d9SPetar Stoykov dev_err(data->dev,
686ddb86d9SPetar Stoykov "calculated crc = 0x%.2X, received 0x%.2X",
696ddb86d9SPetar Stoykov calculated_crc, received_crc);
706ddb86d9SPetar Stoykov return -EIO;
716ddb86d9SPetar Stoykov }
726ddb86d9SPetar Stoykov
736ddb86d9SPetar Stoykov *val = get_unaligned_be16(rxbuf);
746ddb86d9SPetar Stoykov return IIO_VAL_INT;
756ddb86d9SPetar Stoykov case IIO_CHAN_INFO_SCALE:
766ddb86d9SPetar Stoykov *val = 1;
776ddb86d9SPetar Stoykov *val2 = 60;
786ddb86d9SPetar Stoykov
796ddb86d9SPetar Stoykov return IIO_VAL_FRACTIONAL;
806ddb86d9SPetar Stoykov default:
816ddb86d9SPetar Stoykov return -EINVAL;
826ddb86d9SPetar Stoykov }
836ddb86d9SPetar Stoykov }
846ddb86d9SPetar Stoykov
856ddb86d9SPetar Stoykov static const struct iio_info sdp500_info = {
866ddb86d9SPetar Stoykov .read_raw = &sdp500_read_raw,
876ddb86d9SPetar Stoykov };
886ddb86d9SPetar Stoykov
sdp500_probe(struct i2c_client * client)896ddb86d9SPetar Stoykov static int sdp500_probe(struct i2c_client *client)
906ddb86d9SPetar Stoykov {
916ddb86d9SPetar Stoykov struct iio_dev *indio_dev;
926ddb86d9SPetar Stoykov struct sdp500_data *data;
936ddb86d9SPetar Stoykov struct device *dev = &client->dev;
946ddb86d9SPetar Stoykov int ret;
956ddb86d9SPetar Stoykov u8 rxbuf[SDP500_READ_SIZE];
966ddb86d9SPetar Stoykov
976ddb86d9SPetar Stoykov ret = devm_regulator_get_enable(dev, "vdd");
986ddb86d9SPetar Stoykov if (ret)
996ddb86d9SPetar Stoykov return dev_err_probe(dev, ret,
1006ddb86d9SPetar Stoykov "Failed to get and enable regulator\n");
1016ddb86d9SPetar Stoykov
1026ddb86d9SPetar Stoykov indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1036ddb86d9SPetar Stoykov if (!indio_dev)
1046ddb86d9SPetar Stoykov return -ENOMEM;
1056ddb86d9SPetar Stoykov
1066ddb86d9SPetar Stoykov /* has to be done before the first i2c communication */
1076ddb86d9SPetar Stoykov crc8_populate_msb(sdp500_crc8_table, SDP500_CRC8_POLYNOMIAL);
1086ddb86d9SPetar Stoykov
1096ddb86d9SPetar Stoykov data = iio_priv(indio_dev);
1106ddb86d9SPetar Stoykov data->dev = dev;
1116ddb86d9SPetar Stoykov
1126ddb86d9SPetar Stoykov indio_dev->name = "sdp500";
1136ddb86d9SPetar Stoykov indio_dev->channels = sdp500_channels;
1146ddb86d9SPetar Stoykov indio_dev->info = &sdp500_info;
1156ddb86d9SPetar Stoykov indio_dev->modes = INDIO_DIRECT_MODE;
1166ddb86d9SPetar Stoykov indio_dev->num_channels = ARRAY_SIZE(sdp500_channels);
1176ddb86d9SPetar Stoykov
1186ddb86d9SPetar Stoykov ret = sdp500_start_measurement(data);
1196ddb86d9SPetar Stoykov if (ret)
1206ddb86d9SPetar Stoykov return dev_err_probe(dev, ret, "Failed to start measurement");
1216ddb86d9SPetar Stoykov
1226ddb86d9SPetar Stoykov /* First measurement is not correct, read it out to get rid of it */
1236ddb86d9SPetar Stoykov i2c_master_recv(client, rxbuf, SDP500_READ_SIZE);
1246ddb86d9SPetar Stoykov
1256ddb86d9SPetar Stoykov ret = devm_iio_device_register(dev, indio_dev);
1266ddb86d9SPetar Stoykov if (ret)
1276ddb86d9SPetar Stoykov return dev_err_probe(dev, ret, "Failed to register indio_dev");
1286ddb86d9SPetar Stoykov
1296ddb86d9SPetar Stoykov return 0;
1306ddb86d9SPetar Stoykov }
1316ddb86d9SPetar Stoykov
1326ddb86d9SPetar Stoykov static const struct i2c_device_id sdp500_id[] = {
1336ddb86d9SPetar Stoykov { "sdp500" },
1346ddb86d9SPetar Stoykov { }
1356ddb86d9SPetar Stoykov };
1366ddb86d9SPetar Stoykov MODULE_DEVICE_TABLE(i2c, sdp500_id);
1376ddb86d9SPetar Stoykov
1386ddb86d9SPetar Stoykov static const struct of_device_id sdp500_of_match[] = {
1396ddb86d9SPetar Stoykov { .compatible = "sensirion,sdp500" },
1406ddb86d9SPetar Stoykov { }
1416ddb86d9SPetar Stoykov };
1426ddb86d9SPetar Stoykov MODULE_DEVICE_TABLE(of, sdp500_of_match);
1436ddb86d9SPetar Stoykov
1446ddb86d9SPetar Stoykov static struct i2c_driver sdp500_driver = {
1456ddb86d9SPetar Stoykov .driver = {
1466ddb86d9SPetar Stoykov .name = "sensirion,sdp500",
1476ddb86d9SPetar Stoykov .of_match_table = sdp500_of_match,
1486ddb86d9SPetar Stoykov },
1496ddb86d9SPetar Stoykov .probe = sdp500_probe,
1506ddb86d9SPetar Stoykov .id_table = sdp500_id,
1516ddb86d9SPetar Stoykov };
1526ddb86d9SPetar Stoykov module_i2c_driver(sdp500_driver);
1536ddb86d9SPetar Stoykov
1546ddb86d9SPetar Stoykov MODULE_AUTHOR("Thomas Sioutas <thomas.sioutas@prodrive-technologies.com>");
1556ddb86d9SPetar Stoykov MODULE_DESCRIPTION("Driver for Sensirion SDP500 differential pressure sensor");
1566ddb86d9SPetar Stoykov MODULE_LICENSE("GPL");
157