73210b8e0ecc49f6b625d43a33825aac507b427d
[openwrt/staging/dangole.git] /
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
5
6 Add support for Sensirion SPS30 particulate matter sensor.
7
8 Signed-off-by: Tomasz Duszynski <tduszyns@gmail.com>
9 Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
10 ---
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
16
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)
23 sensors
24
25 +config SPS30
26 + tristate "SPS30 particulate matter sensor"
27 + depends on I2C
28 + select CRC8
29 + help
30 + Say Y here to build support for the Sensirion SPS30 particulate
31 + matter sensor.
32 +
33 + To compile this driver as a module, choose M here: the module will
34 + be called sps30.
35 +
36 config VZ89X
37 tristate "SGX Sensortech MiCS VZ89X VOC sensor"
38 depends on I2C
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
50 new file mode 100644
51 index 000000000000..fa3cd409b90b
52 --- /dev/null
53 +++ b/drivers/iio/chemical/sps30.c
54 @@ -0,0 +1,407 @@
55 +// SPDX-License-Identifier: GPL-2.0
56 +/*
57 + * Sensirion SPS30 particulate matter sensor driver
58 + *
59 + * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
60 + *
61 + * I2C slave address: 0x69
62 + *
63 + * TODO:
64 + * - support for turning on fan cleaning
65 + * - support for reading/setting auto cleaning interval
66 + */
67 +
68 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
69 +
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>
80 +
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
86 +
87 +/* SPS30 commands */
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
94 +
95 +enum {
96 + PM1,
97 + PM2P5,
98 + PM4,
99 + PM10,
100 +};
101 +
102 +struct sps30_state {
103 + struct i2c_client *client;
104 + /*
105 + * Guards against concurrent access to sensor registers.
106 + * Must be held whenever sequence of commands is to be executed.
107 + */
108 + struct mutex lock;
109 +};
110 +
111 +DECLARE_CRC8_TABLE(sps30_crc8_table);
112 +
113 +static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf,
114 + int txsize, u8 *rxbuf, int rxsize)
115 +{
116 + int ret;
117 +
118 + /*
119 + * Sensor does not support repeated start so instead of
120 + * sending two i2c messages in a row we just send one by one.
121 + */
122 + ret = i2c_master_send(state->client, txbuf, txsize);
123 + if (ret != txsize)
124 + return ret < 0 ? ret : -EIO;
125 +
126 + if (!rxbuf)
127 + return 0;
128 +
129 + ret = i2c_master_recv(state->client, rxbuf, rxsize);
130 + if (ret != rxsize)
131 + return ret < 0 ? ret : -EIO;
132 +
133 + return 0;
134 +}
135 +
136 +static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
137 +{
138 + /*
139 + * Internally sensor stores measurements in a following manner:
140 + *
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
145 + *
146 + * What follows next are number concentration measurements and
147 + * typical particle size measurement which we omit.
148 + */
149 + u8 buf[SPS30_MAX_READ_SIZE] = { cmd >> 8, cmd };
150 + int i, ret = 0;
151 +
152 + switch (cmd) {
153 + case SPS30_START_MEAS:
154 + buf[2] = 0x03;
155 + buf[3] = 0x00;
156 + buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
157 + ret = sps30_write_then_read(state, buf, 5, NULL, 0);
158 + break;
159 + case SPS30_STOP_MEAS:
160 + case SPS30_RESET:
161 + ret = sps30_write_then_read(state, buf, 2, NULL, 0);
162 + break;
163 + case SPS30_READ_DATA_READY_FLAG:
164 + case SPS30_READ_DATA:
165 + case SPS30_READ_SERIAL:
166 + /* every two data bytes are checksummed */
167 + size += size / 2;
168 + ret = sps30_write_then_read(state, buf, 2, buf, size);
169 + break;
170 + }
171 +
172 + if (ret)
173 + return ret;
174 +
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);
178 +
179 + if (crc != buf[i + 2]) {
180 + dev_err(&state->client->dev,
181 + "data integrity check failed\n");
182 + return -EIO;
183 + }
184 +
185 + *data++ = buf[i];
186 + *data++ = buf[i + 1];
187 + }
188 +
189 + return 0;
190 +}
191 +
192 +static s32 sps30_float_to_int_clamped(const u8 *fp)
193 +{
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;
199 +
200 + /* special case 0 */
201 + if (!exp && !mantissa)
202 + return 0;
203 +
204 + exp -= 127;
205 + if (exp < 0) {
206 + /* return values ranging from 1 to 99 */
207 + return ((((1 << 23) + mantissa) * 100) >> 23) >> (-exp);
208 + }
209 +
210 + /* return values ranging from 100 to 300000 */
211 + shift = 23 - exp;
212 + val = (1 << exp) + (mantissa >> shift);
213 + if (val >= SPS30_MAX_PM)
214 + return SPS30_MAX_PM * 100;
215 +
216 + fraction = mantissa & GENMASK(shift - 1, 0);
217 +
218 + return val * 100 + ((fraction * 100) >> shift);
219 +}
220 +
221 +static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)
222 +{
223 + int i, ret, tries = 5;
224 + u8 tmp[16];
225 +
226 + while (tries--) {
227 + ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
228 + if (ret)
229 + return -EIO;
230 +
231 + /* new measurements ready to be read */
232 + if (tmp[1] == 1)
233 + break;
234 +
235 + msleep_interruptible(300);
236 + }
237 +
238 + if (!tries)
239 + return -ETIMEDOUT;
240 +
241 + ret = sps30_do_cmd(state, SPS30_READ_DATA, tmp, sizeof(int) * size);
242 + if (ret)
243 + return ret;
244 +
245 + for (i = 0; i < size; i++)
246 + data[i] = sps30_float_to_int_clamped(&tmp[4 * i]);
247 +
248 + return 0;
249 +}
250 +
251 +static irqreturn_t sps30_trigger_handler(int irq, void *p)
252 +{
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);
256 + int ret;
257 + s32 data[4 + 2]; /* PM1, PM2P5, PM4, PM10, timestamp */
258 +
259 + mutex_lock(&state->lock);
260 + ret = sps30_do_meas(state, data, 4);
261 + mutex_unlock(&state->lock);
262 + if (ret)
263 + goto err;
264 +
265 + iio_push_to_buffers_with_timestamp(indio_dev, data,
266 + iio_get_time_ns(indio_dev));
267 +err:
268 + iio_trigger_notify_done(indio_dev->trig);
269 +
270 + return IRQ_HANDLED;
271 +}
272 +
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)
276 +{
277 + struct sps30_state *state = iio_priv(indio_dev);
278 + int data[4], ret = -EINVAL;
279 +
280 + switch (mask) {
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) {
287 + case IIO_MOD_PM1:
288 + ret = sps30_do_meas(state, data, 1);
289 + break;
290 + case IIO_MOD_PM2P5:
291 + ret = sps30_do_meas(state, data, 2);
292 + break;
293 + case IIO_MOD_PM4:
294 + ret = sps30_do_meas(state, data, 3);
295 + break;
296 + case IIO_MOD_PM10:
297 + ret = sps30_do_meas(state, data, 4);
298 + break;
299 + }
300 + mutex_unlock(&state->lock);
301 + if (ret)
302 + return ret;
303 +
304 + *val = data[chan->address] / 100;
305 + *val2 = (data[chan->address] % 100) * 10000;
306 +
307 + return IIO_VAL_INT_PLUS_MICRO;
308 + default:
309 + return -EINVAL;
310 + }
311 + case IIO_CHAN_INFO_SCALE:
312 + switch (chan->type) {
313 + case IIO_MASSCONCENTRATION:
314 + switch (chan->channel2) {
315 + case IIO_MOD_PM1:
316 + case IIO_MOD_PM2P5:
317 + case IIO_MOD_PM4:
318 + case IIO_MOD_PM10:
319 + *val = 0;
320 + *val2 = 10000;
321 +
322 + return IIO_VAL_INT_PLUS_MICRO;
323 + }
324 + default:
325 + return -EINVAL;
326 + }
327 + }
328 +
329 + return -EINVAL;
330 +}
331 +
332 +static const struct iio_info sps30_info = {
333 + .read_raw = sps30_read_raw,
334 +};
335 +
336 +#define SPS30_CHAN(_index, _mod) { \
337 + .type = IIO_MASSCONCENTRATION, \
338 + .modified = 1, \
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), \
342 + .address = _mod, \
343 + .scan_index = _index, \
344 + .scan_type = { \
345 + .sign = 'u', \
346 + .realbits = 19, \
347 + .storagebits = 32, \
348 + .endianness = IIO_CPU, \
349 + }, \
350 +}
351 +
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),
358 +};
359 +
360 +static void sps30_stop_meas(void *data)
361 +{
362 + struct sps30_state *state = data;
363 +
364 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
365 +}
366 +
367 +static const unsigned long sps30_scan_masks[] = { 0x0f, 0x00 };
368 +
369 +static int sps30_probe(struct i2c_client *client)
370 +{
371 + struct iio_dev *indio_dev;
372 + struct sps30_state *state;
373 + u8 buf[32];
374 + int ret;
375 +
376 + if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
377 + return -EOPNOTSUPP;
378 +
379 + indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
380 + if (!indio_dev)
381 + return -ENOMEM;
382 +
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;
393 +
394 + mutex_init(&state->lock);
395 + crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
396 +
397 + ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
398 + if (ret) {
399 + dev_err(&client->dev, "failed to reset device\n");
400 + return ret;
401 + }
402 + msleep(300);
403 + /*
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.
408 + */
409 + sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
410 +
411 + ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
412 + if (ret) {
413 + dev_err(&client->dev, "failed to read serial number\n");
414 + return ret;
415 + }
416 + /* returned serial number is already NUL terminated */
417 + dev_info(&client->dev, "serial number: %s\n", buf);
418 +
419 + ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
420 + if (ret) {
421 + dev_err(&client->dev, "failed to start measurement\n");
422 + return ret;
423 + }
424 +
425 + ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);
426 + if (ret)
427 + return ret;
428 +
429 + ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
430 + sps30_trigger_handler, NULL);
431 + if (ret)
432 + return ret;
433 +
434 + return devm_iio_device_register(&client->dev, indio_dev);
435 +}
436 +
437 +static const struct i2c_device_id sps30_id[] = {
438 + { "sps30" },
439 + { }
440 +};
441 +MODULE_DEVICE_TABLE(i2c, sps30_id);
442 +
443 +static const struct of_device_id sps30_of_match[] = {
444 + { .compatible = "sensirion,sps30" },
445 + { }
446 +};
447 +MODULE_DEVICE_TABLE(of, sps30_of_match);
448 +
449 +static struct i2c_driver sps30_driver = {
450 + .driver = {
451 + .name = "sps30",
452 + .of_match_table = sps30_of_match,
453 + },
454 + .id_table = sps30_id,
455 + .probe_new = sps30_probe,
456 +};
457 +module_i2c_driver(sps30_driver);
458 +
459 +MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
460 +MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
461 +MODULE_LICENSE("GPL v2");