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