1 // SPDX-License-Identifier: GPL-2.0
3 * Sensirion SPS30 particulate matter sensor driver
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
7 * I2C slave address: 0x69
10 #include <asm/unaligned.h>
11 #include <linux/crc8.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/buffer.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/trigger_consumer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
22 #define SPS30_CRC8_POLYNOMIAL 0x31
23 /* max number of bytes needed to store PM measurements or serial string */
24 #define SPS30_MAX_READ_SIZE 48
25 /* sensor measures reliably up to 3000 ug / m3 */
26 #define SPS30_MAX_PM 3000
27 /* minimum and maximum self cleaning periods in seconds */
28 #define SPS30_AUTO_CLEANING_PERIOD_MIN 0
29 #define SPS30_AUTO_CLEANING_PERIOD_MAX 604800
32 #define SPS30_START_MEAS 0x0010
33 #define SPS30_STOP_MEAS 0x0104
34 #define SPS30_RESET 0xd304
35 #define SPS30_READ_DATA_READY_FLAG 0x0202
36 #define SPS30_READ_DATA 0x0300
37 #define SPS30_READ_SERIAL 0xd033
38 #define SPS30_START_FAN_CLEANING 0x5607
39 #define SPS30_AUTO_CLEANING_PERIOD 0x8004
40 /* not a sensor command per se, used only to distinguish write from read */
41 #define SPS30_READ_AUTO_CLEANING_PERIOD 0x8005
56 struct i2c_client *client;
58 * Guards against concurrent access to sensor registers.
59 * Must be held whenever sequence of commands is to be executed.
65 DECLARE_CRC8_TABLE(sps30_crc8_table);
67 static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf,
68 int txsize, u8 *rxbuf, int rxsize)
73 * Sensor does not support repeated start so instead of
74 * sending two i2c messages in a row we just send one by one.
76 ret = i2c_master_send(state->client, txbuf, txsize);
78 return ret < 0 ? ret : -EIO;
83 ret = i2c_master_recv(state->client, rxbuf, rxsize);
85 return ret < 0 ? ret : -EIO;
90 static int sps30_do_cmd(struct sps30_state *state, u16 cmd, u8 *data, int size)
93 * Internally sensor stores measurements in a following manner:
95 * PM1: upper two bytes, crc8, lower two bytes, crc8
96 * PM2P5: upper two bytes, crc8, lower two bytes, crc8
97 * PM4: upper two bytes, crc8, lower two bytes, crc8
98 * PM10: upper two bytes, crc8, lower two bytes, crc8
100 * What follows next are number concentration measurements and
101 * typical particle size measurement which we omit.
103 u8 buf[SPS30_MAX_READ_SIZE] = { cmd >> 8, cmd };
107 case SPS30_START_MEAS:
110 buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
111 ret = sps30_write_then_read(state, buf, 5, NULL, 0);
113 case SPS30_STOP_MEAS:
115 case SPS30_START_FAN_CLEANING:
116 ret = sps30_write_then_read(state, buf, 2, NULL, 0);
118 case SPS30_READ_AUTO_CLEANING_PERIOD:
119 buf[0] = SPS30_AUTO_CLEANING_PERIOD >> 8;
120 buf[1] = (u8)(SPS30_AUTO_CLEANING_PERIOD & 0xff);
122 case SPS30_READ_DATA_READY_FLAG:
123 case SPS30_READ_DATA:
124 case SPS30_READ_SERIAL:
125 /* every two data bytes are checksummed */
127 ret = sps30_write_then_read(state, buf, 2, buf, size);
129 case SPS30_AUTO_CLEANING_PERIOD:
132 buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE);
135 buf[7] = crc8(sps30_crc8_table, &buf[5], 2, CRC8_INIT_VALUE);
136 ret = sps30_write_then_read(state, buf, 8, NULL, 0);
143 /* validate received data and strip off crc bytes */
144 for (i = 0; i < size; i += 3) {
145 u8 crc = crc8(sps30_crc8_table, &buf[i], 2, CRC8_INIT_VALUE);
147 if (crc != buf[i + 2]) {
148 dev_err(&state->client->dev,
149 "data integrity check failed\n");
154 *data++ = buf[i + 1];
160 static s32 sps30_float_to_int_clamped(const u8 *fp)
162 int val = get_unaligned_be32(fp);
163 int mantissa = val & GENMASK(22, 0);
164 /* this is fine since passed float is always non-negative */
169 if (!exp && !mantissa)
174 /* return values ranging from 1 to 99 */
175 return ((((1 << 23) + mantissa) * 100) >> 23) >> (-exp);
178 /* return values ranging from 100 to 300000 */
180 val = (1 << exp) + (mantissa >> shift);
181 if (val >= SPS30_MAX_PM)
182 return SPS30_MAX_PM * 100;
184 fraction = mantissa & GENMASK(shift - 1, 0);
186 return val * 100 + ((fraction * 100) >> shift);
189 static int sps30_do_meas(struct sps30_state *state, s32 *data, int size)
191 int i, ret, tries = 5;
194 if (state->state == RESET) {
195 ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0);
199 state->state = MEASURING;
203 ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2);
207 /* new measurements ready to be read */
211 msleep_interruptible(300);
217 ret = sps30_do_cmd(state, SPS30_READ_DATA, tmp, sizeof(int) * size);
221 for (i = 0; i < size; i++)
222 data[i] = sps30_float_to_int_clamped(&tmp[4 * i]);
227 static irqreturn_t sps30_trigger_handler(int irq, void *p)
229 struct iio_poll_func *pf = p;
230 struct iio_dev *indio_dev = pf->indio_dev;
231 struct sps30_state *state = iio_priv(indio_dev);
234 s32 data[4]; /* PM1, PM2P5, PM4, PM10 */
238 mutex_lock(&state->lock);
239 ret = sps30_do_meas(state, scan.data, ARRAY_SIZE(scan.data));
240 mutex_unlock(&state->lock);
244 iio_push_to_buffers_with_timestamp(indio_dev, &scan,
245 iio_get_time_ns(indio_dev));
247 iio_trigger_notify_done(indio_dev->trig);
252 static int sps30_read_raw(struct iio_dev *indio_dev,
253 struct iio_chan_spec const *chan,
254 int *val, int *val2, long mask)
256 struct sps30_state *state = iio_priv(indio_dev);
257 int data[4], ret = -EINVAL;
260 case IIO_CHAN_INFO_PROCESSED:
261 switch (chan->type) {
262 case IIO_MASSCONCENTRATION:
263 mutex_lock(&state->lock);
264 /* read up to the number of bytes actually needed */
265 switch (chan->channel2) {
267 ret = sps30_do_meas(state, data, 1);
270 ret = sps30_do_meas(state, data, 2);
273 ret = sps30_do_meas(state, data, 3);
276 ret = sps30_do_meas(state, data, 4);
279 mutex_unlock(&state->lock);
283 *val = data[chan->address] / 100;
284 *val2 = (data[chan->address] % 100) * 10000;
286 return IIO_VAL_INT_PLUS_MICRO;
290 case IIO_CHAN_INFO_SCALE:
291 switch (chan->type) {
292 case IIO_MASSCONCENTRATION:
293 switch (chan->channel2) {
301 return IIO_VAL_INT_PLUS_MICRO;
313 static int sps30_do_cmd_reset(struct sps30_state *state)
317 ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0);
320 * Power-on-reset causes sensor to produce some glitch on i2c bus and
321 * some controllers end up in error state. Recover simply by placing
322 * some data on the bus, for example STOP_MEAS command, which
323 * is NOP in this case.
325 sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
326 state->state = RESET;
331 static ssize_t start_cleaning_store(struct device *dev,
332 struct device_attribute *attr,
333 const char *buf, size_t len)
335 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
336 struct sps30_state *state = iio_priv(indio_dev);
339 if (kstrtoint(buf, 0, &val) || val != 1)
342 mutex_lock(&state->lock);
343 ret = sps30_do_cmd(state, SPS30_START_FAN_CLEANING, NULL, 0);
344 mutex_unlock(&state->lock);
351 static ssize_t cleaning_period_show(struct device *dev,
352 struct device_attribute *attr,
355 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
356 struct sps30_state *state = iio_priv(indio_dev);
360 mutex_lock(&state->lock);
361 ret = sps30_do_cmd(state, SPS30_READ_AUTO_CLEANING_PERIOD, tmp, 4);
362 mutex_unlock(&state->lock);
366 return sprintf(buf, "%d\n", get_unaligned_be32(tmp));
369 static ssize_t cleaning_period_store(struct device *dev,
370 struct device_attribute *attr,
371 const char *buf, size_t len)
373 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
374 struct sps30_state *state = iio_priv(indio_dev);
378 if (kstrtoint(buf, 0, &val))
381 if ((val < SPS30_AUTO_CLEANING_PERIOD_MIN) ||
382 (val > SPS30_AUTO_CLEANING_PERIOD_MAX))
385 put_unaligned_be32(val, tmp);
387 mutex_lock(&state->lock);
388 ret = sps30_do_cmd(state, SPS30_AUTO_CLEANING_PERIOD, tmp, 0);
390 mutex_unlock(&state->lock);
397 * sensor requires reset in order to return up to date self cleaning
400 ret = sps30_do_cmd_reset(state);
403 "period changed but reads will return the old value\n");
405 mutex_unlock(&state->lock);
410 static ssize_t cleaning_period_available_show(struct device *dev,
411 struct device_attribute *attr,
414 return snprintf(buf, PAGE_SIZE, "[%d %d %d]\n",
415 SPS30_AUTO_CLEANING_PERIOD_MIN, 1,
416 SPS30_AUTO_CLEANING_PERIOD_MAX);
419 static IIO_DEVICE_ATTR_WO(start_cleaning, 0);
420 static IIO_DEVICE_ATTR_RW(cleaning_period, 0);
421 static IIO_DEVICE_ATTR_RO(cleaning_period_available, 0);
423 static struct attribute *sps30_attrs[] = {
424 &iio_dev_attr_start_cleaning.dev_attr.attr,
425 &iio_dev_attr_cleaning_period.dev_attr.attr,
426 &iio_dev_attr_cleaning_period_available.dev_attr.attr,
430 static const struct attribute_group sps30_attr_group = {
431 .attrs = sps30_attrs,
434 static const struct iio_info sps30_info = {
435 .attrs = &sps30_attr_group,
436 .read_raw = sps30_read_raw,
439 #define SPS30_CHAN(_index, _mod) { \
440 .type = IIO_MASSCONCENTRATION, \
442 .channel2 = IIO_MOD_ ## _mod, \
443 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
444 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
446 .scan_index = _index, \
451 .endianness = IIO_CPU, \
455 static const struct iio_chan_spec sps30_channels[] = {
457 SPS30_CHAN(1, PM2P5),
460 IIO_CHAN_SOFT_TIMESTAMP(4),
463 static void sps30_stop_meas(void *data)
465 struct sps30_state *state = data;
467 sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0);
470 static const unsigned long sps30_scan_masks[] = { 0x0f, 0x00 };
472 static int sps30_probe(struct i2c_client *client)
474 struct iio_dev *indio_dev;
475 struct sps30_state *state;
479 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
482 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state));
486 state = iio_priv(indio_dev);
487 i2c_set_clientdata(client, indio_dev);
488 state->client = client;
489 state->state = RESET;
490 indio_dev->info = &sps30_info;
491 indio_dev->name = client->name;
492 indio_dev->channels = sps30_channels;
493 indio_dev->num_channels = ARRAY_SIZE(sps30_channels);
494 indio_dev->modes = INDIO_DIRECT_MODE;
495 indio_dev->available_scan_masks = sps30_scan_masks;
497 mutex_init(&state->lock);
498 crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL);
500 ret = sps30_do_cmd_reset(state);
502 dev_err(&client->dev, "failed to reset device\n");
506 ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf));
508 dev_err(&client->dev, "failed to read serial number\n");
511 /* returned serial number is already NUL terminated */
512 dev_info(&client->dev, "serial number: %s\n", buf);
514 ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state);
518 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL,
519 sps30_trigger_handler, NULL);
523 return devm_iio_device_register(&client->dev, indio_dev);
526 static const struct i2c_device_id sps30_id[] = {
530 MODULE_DEVICE_TABLE(i2c, sps30_id);
532 static const struct of_device_id sps30_of_match[] = {
533 { .compatible = "sensirion,sps30" },
536 MODULE_DEVICE_TABLE(of, sps30_of_match);
538 static struct i2c_driver sps30_driver = {
541 .of_match_table = sps30_of_match,
543 .id_table = sps30_id,
544 .probe_new = sps30_probe,
546 module_i2c_driver(sps30_driver);
548 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
549 MODULE_DESCRIPTION("Sensirion SPS30 particulate matter sensor driver");
550 MODULE_LICENSE("GPL v2");