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 --- a/drivers/iio/chemical/Kconfig
18 +++ b/drivers/iio/chemical/Kconfig
19 @@ -61,6 +61,17 @@ config IAQCORE
20 iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
24 + tristate "SPS30 particulate matter sensor"
28 + Say Y here to build support for the Sensirion SPS30 particulate
31 + To compile this driver as a module, choose M here: the module will
35 tristate "SGX Sensortech MiCS VZ89X VOC sensor"
37 --- a/drivers/iio/chemical/Makefile
38 +++ b/drivers/iio/chemical/Makefile
39 @@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
40 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
41 obj-$(CONFIG_CCS811) += ccs811.o
42 obj-$(CONFIG_IAQCORE) += ams-iaq-core.o
43 +obj-$(CONFIG_SPS30) += sps30.o
44 obj-$(CONFIG_VZ89X) += vz89x.o
46 +++ b/drivers/iio/chemical/sps30.c
48 +// SPDX-License-Identifier: GPL-2.0
50 + * Sensirion SPS30 particulate matter sensor driver
52 + * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
54 + * I2C slave address: 0x69
57 + * - support for turning on fan cleaning
58 + * - support for reading/setting auto cleaning interval
61 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
63 +#include <asm/unaligned.h>
64 +#include <linux/crc8.h>
65 +#include <linux/delay.h>
66 +#include <linux/i2c.h>
67 +#include <linux/iio/buffer.h>
68 +#include <linux/iio/iio.h>
69 +#include <linux/iio/sysfs.h>
70 +#include <linux/iio/trigger_consumer.h>
71 +#include <linux/iio/triggered_buffer.h>
72 +#include <linux/module.h>
74 +#define SPS30_CRC8_POLYNOMIAL 0x31
75 +/* max number of bytes needed to store PM measurements or serial string */
76 +#define SPS30_MAX_READ_SIZE 48
77 +/* sensor measures reliably up to 3000 ug / m3 */
78 +#define SPS30_MAX_PM 3000
81 +#define SPS30_START_MEAS 0x0010
82 +#define SPS30_STOP_MEAS 0x0104
83 +#define SPS30_RESET 0xd304
84 +#define SPS30_READ_DATA_READY_FLAG 0x0202
85 +#define SPS30_READ_DATA 0x0300
86 +#define SPS30_READ_SERIAL 0xd033
96 + struct i2c_client *client;
98 + * Guards against concurrent access to sensor registers.
99 + * Must be held whenever sequence of commands is to be executed.
104 +DECLARE_CRC8_TABLE(sps30_crc8_table);
106 +static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf,
107 + int txsize, u8 *rxbuf, int rxsize)
112 + * Sensor does not support repeated start so instead of
113 + * sending two i2c messages in a row we just send one by one.
115 + ret = i2c_master_send(state->client, txbuf, txsize);
117 + return ret < 0 ? ret : -EIO;
122 + ret = i2c_master_recv(state->client, rxbuf, rxsize);
124 + return ret < 0 ? ret : -EIO;
129 +static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
132 + * Internally sensor stores measurements in a following manner:
134 + * PM1: upper two bytes, crc8, lower two bytes, crc8
135 + * PM2P5: upper two bytes, crc8, lower two bytes, crc8
136 + * PM4: upper two bytes, crc8, lower two bytes, crc8
137 + * PM10: upper two bytes, crc8, lower two bytes, crc8
139 + * What follows next are number concentration measurements and
140 + * typical particle size measurement which we omit.
142 + u8 buf[SPS30_MAX_READ_SIZE] = { cmd >> 8, cmd };
146 + case SPS30_START_MEAS:
149 + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
150 + ret = sps30_write_then_read(state, buf, 5, NULL, 0);
152 + case SPS30_STOP_MEAS:
154 + ret = sps30_write_then_read(state, buf, 2, NULL, 0);
156 + case SPS30_READ_DATA_READY_FLAG:
157 + case SPS30_READ_DATA:
158 + case SPS30_READ_SERIAL:
159 + /* every two data bytes are checksummed */
161 + ret = sps30_write_then_read(state, buf, 2, buf, size);
168 + /* validate received data and strip off crc bytes */
169 + for (i = 0; i < size; i += 3) {
170 + u8 crc = crc8(sps30_crc8_table, &buf[i], 2, CRC8_INIT_VALUE);
172 + if (crc != buf[i + 2]) {
173 + dev_err(&state->client->dev,
174 + "data integrity check failed\n");
179 + *data++ = buf[i + 1];
185 +static s32 sps30_float_to_int_clamped(const u8 *fp)
187 + int val = get_unaligned_be32(fp);
188 + int mantissa = val & GENMASK(22, 0);
189 + /* this is fine since passed float is always non-negative */
190 + int exp = val >> 23;
191 + int fraction, shift;
193 + /* special case 0 */
194 + if (!exp && !mantissa)
199 + /* return values ranging from 1 to 99 */
200 + return ((((1 << 23) + mantissa) * 100) >> 23) >> (-exp);
203 + /* return values ranging from 100 to 300000 */
205 + val = (1 << exp) + (mantissa >> shift);
206 + if (val >= SPS30_MAX_PM)
207 + return SPS30_MAX_PM * 100;
209 + fraction = mantissa & GENMASK(shift - 1, 0);
211 + return val * 100 + ((fraction * 100) >> shift);
214 +static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)
216 + int i, ret, tries = 5;
220 + ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
224 + /* new measurements ready to be read */
228 + msleep_interruptible(300);
234 + ret = sps30_do_cmd(state, SPS30_READ_DATA, tmp, sizeof(int) * size);
238 + for (i = 0; i < size; i++)
239 + data[i] = sps30_float_to_int_clamped(&tmp[4 * i]);
244 +static irqreturn_t sps30_trigger_handler(int irq, void *p)
246 + struct iio_poll_func *pf = p;
247 + struct iio_dev *indio_dev = pf->indio_dev;
248 + struct sps30_state *state = iio_priv(indio_dev);
250 + s32 data[4 + 2]; /* PM1, PM2P5, PM4, PM10, timestamp */
252 + mutex_lock(&state->lock);
253 + ret = sps30_do_meas(state, data, 4);
254 + mutex_unlock(&state->lock);
258 + iio_push_to_buffers_with_timestamp(indio_dev, data,
259 + iio_get_time_ns(indio_dev));
261 + iio_trigger_notify_done(indio_dev->trig);
263 + return IRQ_HANDLED;
266 +static int sps30_read_raw(struct iio_dev *indio_dev,
267 + struct iio_chan_spec const *chan,
268 + int *val, int *val2, long mask)
270 + struct sps30_state *state = iio_priv(indio_dev);
271 + int data[4], ret = -EINVAL;
274 + case IIO_CHAN_INFO_PROCESSED:
275 + switch (chan->type) {
276 + case IIO_MASSCONCENTRATION:
277 + mutex_lock(&state->lock);
278 + /* read up to the number of bytes actually needed */
279 + switch (chan->channel2) {
281 + ret = sps30_do_meas(state, data, 1);
283 + case IIO_MOD_PM2P5:
284 + ret = sps30_do_meas(state, data, 2);
287 + ret = sps30_do_meas(state, data, 3);
290 + ret = sps30_do_meas(state, data, 4);
293 + mutex_unlock(&state->lock);
297 + *val = data[chan->address] / 100;
298 + *val2 = (data[chan->address] % 100) * 10000;
300 + return IIO_VAL_INT_PLUS_MICRO;
304 + case IIO_CHAN_INFO_SCALE:
305 + switch (chan->type) {
306 + case IIO_MASSCONCENTRATION:
307 + switch (chan->channel2) {
309 + case IIO_MOD_PM2P5:
315 + return IIO_VAL_INT_PLUS_MICRO;
325 +static const struct iio_info sps30_info = {
326 + .read_raw = sps30_read_raw,
329 +#define SPS30_CHAN(_index, _mod) { \
330 + .type = IIO_MASSCONCENTRATION, \
332 + .channel2 = IIO_MOD_ ## _mod, \
333 + .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
334 + .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
336 + .scan_index = _index, \
340 + .storagebits = 32, \
341 + .endianness = IIO_CPU, \
345 +static const struct iio_chan_spec sps30_channels[] = {
346 + SPS30_CHAN(0, PM1),
347 + SPS30_CHAN(1, PM2P5),
348 + SPS30_CHAN(2, PM4),
349 + SPS30_CHAN(3, PM10),
350 + IIO_CHAN_SOFT_TIMESTAMP(4),
353 +static void sps30_stop_meas(void *data)
355 + struct sps30_state *state = data;
357 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
360 +static const unsigned long sps30_scan_masks[] = { 0x0f, 0x00 };
362 +static int sps30_probe(struct i2c_client *client)
364 + struct iio_dev *indio_dev;
365 + struct sps30_state *state;
369 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
370 + return -EOPNOTSUPP;
372 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
376 + state = iio_priv(indio_dev);
377 + i2c_set_clientdata(client, indio_dev);
378 + state->client = client;
379 + indio_dev->dev.parent = &client->dev;
380 + indio_dev->info = &sps30_info;
381 + indio_dev->name = client->name;
382 + indio_dev->channels = sps30_channels;
383 + indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
384 + indio_dev->modes = INDIO_DIRECT_MODE;
385 + indio_dev->available_scan_masks = sps30_scan_masks;
387 + mutex_init(&state->lock);
388 + crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
390 + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
392 + dev_err(&client->dev, "failed to reset device\n");
397 + * Power-on-reset causes sensor to produce some glitch on i2c bus and
398 + * some controllers end up in error state. Recover simply by placing
399 + * some data on the bus, for example STOP_MEAS command, which
400 + * is NOP in this case.
402 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
404 + ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
406 + dev_err(&client->dev, "failed to read serial number\n");
409 + /* returned serial number is already NUL terminated */
410 + dev_info(&client->dev, "serial number: %s\n", buf);
412 + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
414 + dev_err(&client->dev, "failed to start measurement\n");
418 + ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);
422 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
423 + sps30_trigger_handler, NULL);
427 + return devm_iio_device_register(&client->dev, indio_dev);
430 +static const struct i2c_device_id sps30_id[] = {
434 +MODULE_DEVICE_TABLE(i2c, sps30_id);
436 +static const struct of_device_id sps30_of_match[] = {
437 + { .compatible = "sensirion,sps30" },
440 +MODULE_DEVICE_TABLE(of, sps30_of_match);
442 +static struct i2c_driver sps30_driver = {
445 + .of_match_table = sps30_of_match,
447 + .id_table = sps30_id,
448 + .probe_new = sps30_probe,
450 +module_i2c_driver(sps30_driver);
452 +MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
453 +MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
454 +MODULE_LICENSE("GPL v2");