1 From 232e0f6ddeaee104d64675fe7d0cc142cf955f35 Mon Sep 17 00:00:00 2001
2 From: Tomasz Duszynski <tduszyns@gmail.com>
3 Date: Fri, 14 Dec 2018 19:28:02 +0100
4 Subject: [PATCH] iio: chemical: add support for Sensirion SPS30 sensor
6 Add support for Sensirion SPS30 particulate matter sensor.
8 Signed-off-by: Tomasz Duszynski <tduszyns@gmail.com>
9 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
11 drivers/iio/chemical/Kconfig | 11 +
12 drivers/iio/chemical/Makefile | 1 +
13 drivers/iio/chemical/sps30.c | 407 ++++++++++++++++++++++++++++++++++
14 3 files changed, 419 insertions(+)
15 create mode 100644 drivers/iio/chemical/sps30.c
17 diff --git a/drivers/iio/chemical/Kconfig b/drivers/iio/chemical/Kconfig
18 index b8e005be4f87..57832b4360e9 100644
19 --- a/drivers/iio/chemical/Kconfig
20 +++ b/drivers/iio/chemical/Kconfig
21 @@ -61,6 +61,17 @@ config IAQCORE
22 iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
26 + tristate "SPS30 particulate matter sensor"
30 + Say Y here to build support for the Sensirion SPS30 particulate
33 + To compile this driver as a module, choose M here: the module will
37 tristate "SGX Sensortech MiCS VZ89X VOC sensor"
39 diff --git a/drivers/iio/chemical/Makefile b/drivers/iio/chemical/Makefile
40 index 2f4c4ba4d781..9f42f4252151 100644
41 --- a/drivers/iio/chemical/Makefile
42 +++ b/drivers/iio/chemical/Makefile
43 @@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
44 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
45 obj-$(CONFIG_CCS811) += ccs811.o
46 obj-$(CONFIG_IAQCORE) += ams-iaq-core.o
47 +obj-$(CONFIG_SPS30) += sps30.o
48 obj-$(CONFIG_VZ89X) += vz89x.o
49 diff --git a/drivers/iio/chemical/sps30.c b/drivers/iio/chemical/sps30.c
51 index 000000000000..fa3cd409b90b
53 +++ b/drivers/iio/chemical/sps30.c
55 +// SPDX-License-Identifier: GPL-2.0
57 + * Sensirion SPS30 particulate matter sensor driver
59 + * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
61 + * I2C slave address: 0x69
64 + * - support for turning on fan cleaning
65 + * - support for reading/setting auto cleaning interval
68 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
70 +#include <asm/unaligned.h>
71 +#include <linux/crc8.h>
72 +#include <linux/delay.h>
73 +#include <linux/i2c.h>
74 +#include <linux/iio/buffer.h>
75 +#include <linux/iio/iio.h>
76 +#include <linux/iio/sysfs.h>
77 +#include <linux/iio/trigger_consumer.h>
78 +#include <linux/iio/triggered_buffer.h>
79 +#include <linux/module.h>
81 +#define SPS30_CRC8_POLYNOMIAL 0x31
82 +/* max number of bytes needed to store PM measurements or serial string */
83 +#define SPS30_MAX_READ_SIZE 48
84 +/* sensor measures reliably up to 3000 ug / m3 */
85 +#define SPS30_MAX_PM 3000
88 +#define SPS30_START_MEAS 0x0010
89 +#define SPS30_STOP_MEAS 0x0104
90 +#define SPS30_RESET 0xd304
91 +#define SPS30_READ_DATA_READY_FLAG 0x0202
92 +#define SPS30_READ_DATA 0x0300
93 +#define SPS30_READ_SERIAL 0xd033
102 +struct sps30_state {
103 + struct i2c_client *client;
105 + * Guards against concurrent access to sensor registers.
106 + * Must be held whenever sequence of commands is to be executed.
111 +DECLARE_CRC8_TABLE(sps30_crc8_table);
113 +static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf,
114 + int txsize, u8 *rxbuf, int rxsize)
119 + * Sensor does not support repeated start so instead of
120 + * sending two i2c messages in a row we just send one by one.
122 + ret = i2c_master_send(state->client, txbuf, txsize);
124 + return ret < 0 ? ret : -EIO;
129 + ret = i2c_master_recv(state->client, rxbuf, rxsize);
131 + return ret < 0 ? ret : -EIO;
136 +static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
139 + * Internally sensor stores measurements in a following manner:
141 + * PM1: upper two bytes, crc8, lower two bytes, crc8
142 + * PM2P5: upper two bytes, crc8, lower two bytes, crc8
143 + * PM4: upper two bytes, crc8, lower two bytes, crc8
144 + * PM10: upper two bytes, crc8, lower two bytes, crc8
146 + * What follows next are number concentration measurements and
147 + * typical particle size measurement which we omit.
149 + u8 buf[SPS30_MAX_READ_SIZE] = { cmd >> 8, cmd };
153 + case SPS30_START_MEAS:
156 + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
157 + ret = sps30_write_then_read(state, buf, 5, NULL, 0);
159 + case SPS30_STOP_MEAS:
161 + ret = sps30_write_then_read(state, buf, 2, NULL, 0);
163 + case SPS30_READ_DATA_READY_FLAG:
164 + case SPS30_READ_DATA:
165 + case SPS30_READ_SERIAL:
166 + /* every two data bytes are checksummed */
168 + ret = sps30_write_then_read(state, buf, 2, buf, size);
175 + /* validate received data and strip off crc bytes */
176 + for (i = 0; i < size; i += 3) {
177 + u8 crc = crc8(sps30_crc8_table, &buf[i], 2, CRC8_INIT_VALUE);
179 + if (crc != buf[i + 2]) {
180 + dev_err(&state->client->dev,
181 + "data integrity check failed\n");
186 + *data++ = buf[i + 1];
192 +static s32 sps30_float_to_int_clamped(const u8 *fp)
194 + int val = get_unaligned_be32(fp);
195 + int mantissa = val & GENMASK(22, 0);
196 + /* this is fine since passed float is always non-negative */
197 + int exp = val >> 23;
198 + int fraction, shift;
200 + /* special case 0 */
201 + if (!exp && !mantissa)
206 + /* return values ranging from 1 to 99 */
207 + return ((((1 << 23) + mantissa) * 100) >> 23) >> (-exp);
210 + /* return values ranging from 100 to 300000 */
212 + val = (1 << exp) + (mantissa >> shift);
213 + if (val >= SPS30_MAX_PM)
214 + return SPS30_MAX_PM * 100;
216 + fraction = mantissa & GENMASK(shift - 1, 0);
218 + return val * 100 + ((fraction * 100) >> shift);
221 +static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)
223 + int i, ret, tries = 5;
227 + ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
231 + /* new measurements ready to be read */
235 + msleep_interruptible(300);
241 + ret = sps30_do_cmd(state, SPS30_READ_DATA, tmp, sizeof(int) * size);
245 + for (i = 0; i < size; i++)
246 + data[i] = sps30_float_to_int_clamped(&tmp[4 * i]);
251 +static irqreturn_t sps30_trigger_handler(int irq, void *p)
253 + struct iio_poll_func *pf = p;
254 + struct iio_dev *indio_dev = pf->indio_dev;
255 + struct sps30_state *state = iio_priv(indio_dev);
257 + s32 data[4 + 2]; /* PM1, PM2P5, PM4, PM10, timestamp */
259 + mutex_lock(&state->lock);
260 + ret = sps30_do_meas(state, data, 4);
261 + mutex_unlock(&state->lock);
265 + iio_push_to_buffers_with_timestamp(indio_dev, data,
266 + iio_get_time_ns(indio_dev));
268 + iio_trigger_notify_done(indio_dev->trig);
270 + return IRQ_HANDLED;
273 +static int sps30_read_raw(struct iio_dev *indio_dev,
274 + struct iio_chan_spec const *chan,
275 + int *val, int *val2, long mask)
277 + struct sps30_state *state = iio_priv(indio_dev);
278 + int data[4], ret = -EINVAL;
281 + case IIO_CHAN_INFO_PROCESSED:
282 + switch (chan->type) {
283 + case IIO_MASSCONCENTRATION:
284 + mutex_lock(&state->lock);
285 + /* read up to the number of bytes actually needed */
286 + switch (chan->channel2) {
288 + ret = sps30_do_meas(state, data, 1);
290 + case IIO_MOD_PM2P5:
291 + ret = sps30_do_meas(state, data, 2);
294 + ret = sps30_do_meas(state, data, 3);
297 + ret = sps30_do_meas(state, data, 4);
300 + mutex_unlock(&state->lock);
304 + *val = data[chan->address] / 100;
305 + *val2 = (data[chan->address] % 100) * 10000;
307 + return IIO_VAL_INT_PLUS_MICRO;
311 + case IIO_CHAN_INFO_SCALE:
312 + switch (chan->type) {
313 + case IIO_MASSCONCENTRATION:
314 + switch (chan->channel2) {
316 + case IIO_MOD_PM2P5:
322 + return IIO_VAL_INT_PLUS_MICRO;
332 +static const struct iio_info sps30_info = {
333 + .read_raw = sps30_read_raw,
336 +#define SPS30_CHAN(_index, _mod) { \
337 + .type = IIO_MASSCONCENTRATION, \
339 + .channel2 = IIO_MOD_ ## _mod, \
340 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
341 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
343 + .scan_index = _index, \
347 + .storagebits = 32, \
348 + .endianness = IIO_CPU, \
352 +static const struct iio_chan_spec sps30_channels[] = {
353 + SPS30_CHAN(0, PM1),
354 + SPS30_CHAN(1, PM2P5),
355 + SPS30_CHAN(2, PM4),
356 + SPS30_CHAN(3, PM10),
357 + IIO_CHAN_SOFT_TIMESTAMP(4),
360 +static void sps30_stop_meas(void *data)
362 + struct sps30_state *state = data;
364 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
367 +static const unsigned long sps30_scan_masks[] = { 0x0f, 0x00 };
369 +static int sps30_probe(struct i2c_client *client)
371 + struct iio_dev *indio_dev;
372 + struct sps30_state *state;
376 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
377 + return -EOPNOTSUPP;
379 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
383 + state = iio_priv(indio_dev);
384 + i2c_set_clientdata(client, indio_dev);
385 + state->client = client;
386 + indio_dev->dev.parent = &client->dev;
387 + indio_dev->info = &sps30_info;
388 + indio_dev->name = client->name;
389 + indio_dev->channels = sps30_channels;
390 + indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
391 + indio_dev->modes = INDIO_DIRECT_MODE;
392 + indio_dev->available_scan_masks = sps30_scan_masks;
394 + mutex_init(&state->lock);
395 + crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
397 + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
399 + dev_err(&client->dev, "failed to reset device\n");
404 + * Power-on-reset causes sensor to produce some glitch on i2c bus and
405 + * some controllers end up in error state. Recover simply by placing
406 + * some data on the bus, for example STOP_MEAS command, which
407 + * is NOP in this case.
409 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
411 + ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
413 + dev_err(&client->dev, "failed to read serial number\n");
416 + /* returned serial number is already NUL terminated */
417 + dev_info(&client->dev, "serial number: %s\n", buf);
419 + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
421 + dev_err(&client->dev, "failed to start measurement\n");
425 + ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);
429 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
430 + sps30_trigger_handler, NULL);
434 + return devm_iio_device_register(&client->dev, indio_dev);
437 +static const struct i2c_device_id sps30_id[] = {
441 +MODULE_DEVICE_TABLE(i2c, sps30_id);
443 +static const struct of_device_id sps30_of_match[] = {
444 + { .compatible = "sensirion,sps30" },
447 +MODULE_DEVICE_TABLE(of, sps30_of_match);
449 +static struct i2c_driver sps30_driver = {
452 + .of_match_table = sps30_of_match,
454 + .id_table = sps30_id,
455 + .probe_new = sps30_probe,
457 +module_i2c_driver(sps30_driver);
459 +MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
460 +MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
461 +MODULE_LICENSE("GPL v2");