sps30.c (c546d49656143855093c7b7fde60866e6e23a69d) | sps30.c (62129a0849d27cc94ced832bcf9dcde283dcbe08) |
---|---|
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Sensirion SPS30 particulate matter sensor driver 4 * 5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com> 6 * 7 * I2C slave address: 0x69 | 1// SPDX-License-Identifier: GPL-2.0 2/* 3 * Sensirion SPS30 particulate matter sensor driver 4 * 5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com> 6 * 7 * I2C slave address: 0x69 |
8 * 9 * TODO: 10 * - support for reading/setting auto cleaning interval | |
11 */ 12 13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14 15#include <asm/unaligned.h> 16#include <linux/crc8.h> 17#include <linux/delay.h> 18#include <linux/i2c.h> 19#include <linux/iio/buffer.h> 20#include <linux/iio/iio.h> 21#include <linux/iio/sysfs.h> 22#include <linux/iio/trigger_consumer.h> 23#include <linux/iio/triggered_buffer.h> | 8 */ 9 10#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 11 12#include <asm/unaligned.h> 13#include <linux/crc8.h> 14#include <linux/delay.h> 15#include <linux/i2c.h> 16#include <linux/iio/buffer.h> 17#include <linux/iio/iio.h> 18#include <linux/iio/sysfs.h> 19#include <linux/iio/trigger_consumer.h> 20#include <linux/iio/triggered_buffer.h> |
21#include <linux/kernel.h> |
|
24#include <linux/module.h> 25 26#define SPS30_CRC8_POLYNOMIAL 0x31 27/* max number of bytes needed to store PM measurements or serial string */ 28#define SPS30_MAX_READ_SIZE 48 29/* sensor measures reliably up to 3000 ug / m3 */ 30#define SPS30_MAX_PM 3000 | 22#include <linux/module.h> 23 24#define SPS30_CRC8_POLYNOMIAL 0x31 25/* max number of bytes needed to store PM measurements or serial string */ 26#define SPS30_MAX_READ_SIZE 48 27/* sensor measures reliably up to 3000 ug / m3 */ 28#define SPS30_MAX_PM 3000 |
29/* minimum and maximum self cleaning periods in seconds */ 30#define SPS30_AUTO_CLEANING_PERIOD_MIN 0 31#define SPS30_AUTO_CLEANING_PERIOD_MAX 604800 |
|
31 32/* SPS30 commands */ 33#define SPS30_START_MEAS 0x0010 34#define SPS30_STOP_MEAS 0x0104 35#define SPS30_RESET 0xd304 36#define SPS30_READ_DATA_READY_FLAG 0x0202 37#define SPS30_READ_DATA 0x0300 38#define SPS30_READ_SERIAL 0xd033 39#define SPS30_START_FAN_CLEANING 0x5607 | 32 33/* SPS30 commands */ 34#define SPS30_START_MEAS 0x0010 35#define SPS30_STOP_MEAS 0x0104 36#define SPS30_RESET 0xd304 37#define SPS30_READ_DATA_READY_FLAG 0x0202 38#define SPS30_READ_DATA 0x0300 39#define SPS30_READ_SERIAL 0xd033 40#define SPS30_START_FAN_CLEANING 0x5607 |
41#define SPS30_AUTO_CLEANING_PERIOD 0x8004 42/* not a sensor command per se, used only to distinguish write from read */ 43#define SPS30_READ_AUTO_CLEANING_PERIOD 0x8005 |
|
40 41enum { 42 PM1, 43 PM2P5, 44 PM4, 45 PM10, 46}; 47 | 44 45enum { 46 PM1, 47 PM2P5, 48 PM4, 49 PM10, 50}; 51 |
52enum { 53 RESET, 54 MEASURING, 55}; 56 |
|
48struct sps30_state { 49 struct i2c_client *client; 50 /* 51 * Guards against concurrent access to sensor registers. 52 * Must be held whenever sequence of commands is to be executed. 53 */ 54 struct mutex lock; | 57struct sps30_state { 58 struct i2c_client *client; 59 /* 60 * Guards against concurrent access to sensor registers. 61 * Must be held whenever sequence of commands is to be executed. 62 */ 63 struct mutex lock; |
64 int state; |
|
55}; 56 57DECLARE_CRC8_TABLE(sps30_crc8_table); 58 59static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf, 60 int txsize, u8 *rxbuf, int rxsize) 61{ 62 int ret; --- 39 unchanged lines hidden (view full) --- 102 buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE); 103 ret = sps30_write_then_read(state, buf, 5, NULL, 0); 104 break; 105 case SPS30_STOP_MEAS: 106 case SPS30_RESET: 107 case SPS30_START_FAN_CLEANING: 108 ret = sps30_write_then_read(state, buf, 2, NULL, 0); 109 break; | 65}; 66 67DECLARE_CRC8_TABLE(sps30_crc8_table); 68 69static int sps30_write_then_read(struct sps30_state *state, u8 *txbuf, 70 int txsize, u8 *rxbuf, int rxsize) 71{ 72 int ret; --- 39 unchanged lines hidden (view full) --- 112 buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE); 113 ret = sps30_write_then_read(state, buf, 5, NULL, 0); 114 break; 115 case SPS30_STOP_MEAS: 116 case SPS30_RESET: 117 case SPS30_START_FAN_CLEANING: 118 ret = sps30_write_then_read(state, buf, 2, NULL, 0); 119 break; |
120 case SPS30_READ_AUTO_CLEANING_PERIOD: 121 buf[0] = SPS30_AUTO_CLEANING_PERIOD >> 8; 122 buf[1] = (u8)SPS30_AUTO_CLEANING_PERIOD; |
|
110 case SPS30_READ_DATA_READY_FLAG: 111 case SPS30_READ_DATA: 112 case SPS30_READ_SERIAL: 113 /* every two data bytes are checksummed */ 114 size += size / 2; 115 ret = sps30_write_then_read(state, buf, 2, buf, size); 116 break; | 123 case SPS30_READ_DATA_READY_FLAG: 124 case SPS30_READ_DATA: 125 case SPS30_READ_SERIAL: 126 /* every two data bytes are checksummed */ 127 size += size / 2; 128 ret = sps30_write_then_read(state, buf, 2, buf, size); 129 break; |
130 case SPS30_AUTO_CLEANING_PERIOD: 131 buf[2] = data[0]; 132 buf[3] = data[1]; 133 buf[4] = crc8(sps30_crc8_table, &buf[2], 2, CRC8_INIT_VALUE); 134 buf[5] = data[2]; 135 buf[6] = data[3]; 136 buf[7] = crc8(sps30_crc8_table, &buf[5], 2, CRC8_INIT_VALUE); 137 ret = sps30_write_then_read(state, buf, 8, NULL, 0); 138 break; |
|
117 } 118 119 if (ret) 120 return ret; 121 122 /* validate received data and strip off crc bytes */ 123 for (i = 0; i < size; i += 3) { 124 u8 crc = crc8(sps30_crc8_table, &buf[i], 2, CRC8_INIT_VALUE); --- 40 unchanged lines hidden (view full) --- 165 return val * 100 + ((fraction * 100) >> shift); 166} 167 168static int sps30_do_meas(struct sps30_state *state, s32 *data, int size) 169{ 170 int i, ret, tries = 5; 171 u8 tmp[16]; 172 | 139 } 140 141 if (ret) 142 return ret; 143 144 /* validate received data and strip off crc bytes */ 145 for (i = 0; i < size; i += 3) { 146 u8 crc = crc8(sps30_crc8_table, &buf[i], 2, CRC8_INIT_VALUE); --- 40 unchanged lines hidden (view full) --- 187 return val * 100 + ((fraction * 100) >> shift); 188} 189 190static int sps30_do_meas(struct sps30_state *state, s32 *data, int size) 191{ 192 int i, ret, tries = 5; 193 u8 tmp[16]; 194 |
195 if (state->state == RESET) { 196 ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0); 197 if (ret) 198 return ret; 199 200 state->state = MEASURING; 201 } 202 |
|
173 while (tries--) { 174 ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2); 175 if (ret) 176 return -EIO; 177 178 /* new measurements ready to be read */ 179 if (tmp[1] == 1) 180 break; --- 90 unchanged lines hidden (view full) --- 271 default: 272 return -EINVAL; 273 } 274 } 275 276 return -EINVAL; 277} 278 | 203 while (tries--) { 204 ret = sps30_do_cmd(state, SPS30_READ_DATA_READY_FLAG, tmp, 2); 205 if (ret) 206 return -EIO; 207 208 /* new measurements ready to be read */ 209 if (tmp[1] == 1) 210 break; --- 90 unchanged lines hidden (view full) --- 301 default: 302 return -EINVAL; 303 } 304 } 305 306 return -EINVAL; 307} 308 |
309static int sps30_do_cmd_reset(struct sps30_state *state) 310{ 311 int ret; 312 313 ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0); 314 msleep(300); 315 /* 316 * Power-on-reset causes sensor to produce some glitch on i2c bus and 317 * some controllers end up in error state. Recover simply by placing 318 * some data on the bus, for example STOP_MEAS command, which 319 * is NOP in this case. 320 */ 321 sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0); 322 state->state = RESET; 323 324 return ret; 325} 326 |
|
279static ssize_t start_cleaning_store(struct device *dev, 280 struct device_attribute *attr, 281 const char *buf, size_t len) 282{ 283 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 284 struct sps30_state *state = iio_priv(indio_dev); 285 int val, ret; 286 --- 4 unchanged lines hidden (view full) --- 291 ret = sps30_do_cmd(state, SPS30_START_FAN_CLEANING, NULL, 0); 292 mutex_unlock(&state->lock); 293 if (ret) 294 return ret; 295 296 return len; 297} 298 | 327static ssize_t start_cleaning_store(struct device *dev, 328 struct device_attribute *attr, 329 const char *buf, size_t len) 330{ 331 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 332 struct sps30_state *state = iio_priv(indio_dev); 333 int val, ret; 334 --- 4 unchanged lines hidden (view full) --- 339 ret = sps30_do_cmd(state, SPS30_START_FAN_CLEANING, NULL, 0); 340 mutex_unlock(&state->lock); 341 if (ret) 342 return ret; 343 344 return len; 345} 346 |
347static ssize_t cleaning_period_show(struct device *dev, 348 struct device_attribute *attr, 349 char *buf) 350{ 351 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 352 struct sps30_state *state = iio_priv(indio_dev); 353 u8 tmp[4]; 354 int ret; 355 356 mutex_lock(&state->lock); 357 ret = sps30_do_cmd(state, SPS30_READ_AUTO_CLEANING_PERIOD, tmp, 4); 358 mutex_unlock(&state->lock); 359 if (ret) 360 return ret; 361 362 return sprintf(buf, "%d\n", get_unaligned_be32(tmp)); 363} 364 365static ssize_t cleaning_period_store(struct device *dev, 366 struct device_attribute *attr, 367 const char *buf, size_t len) 368{ 369 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 370 struct sps30_state *state = iio_priv(indio_dev); 371 int val, ret; 372 u8 tmp[4]; 373 374 if (kstrtoint(buf, 0, &val)) 375 return -EINVAL; 376 377 if ((val < SPS30_AUTO_CLEANING_PERIOD_MIN) || 378 (val > SPS30_AUTO_CLEANING_PERIOD_MAX)) 379 return -EINVAL; 380 381 put_unaligned_be32(val, tmp); 382 383 mutex_lock(&state->lock); 384 ret = sps30_do_cmd(state, SPS30_AUTO_CLEANING_PERIOD, tmp, 0); 385 if (ret) { 386 mutex_unlock(&state->lock); 387 return ret; 388 } 389 390 msleep(20); 391 392 /* 393 * sensor requires reset in order to return up to date self cleaning 394 * period 395 */ 396 ret = sps30_do_cmd_reset(state); 397 if (ret) 398 dev_warn(dev, 399 "period changed but reads will return the old value\n"); 400 401 mutex_unlock(&state->lock); 402 403 return len; 404} 405 406static ssize_t cleaning_period_available_show(struct device *dev, 407 struct device_attribute *attr, 408 char *buf) 409{ 410 return snprintf(buf, PAGE_SIZE, "[%d %d %d]\n", 411 SPS30_AUTO_CLEANING_PERIOD_MIN, 1, 412 SPS30_AUTO_CLEANING_PERIOD_MAX); 413} 414 |
|
299static IIO_DEVICE_ATTR_WO(start_cleaning, 0); | 415static IIO_DEVICE_ATTR_WO(start_cleaning, 0); |
416static IIO_DEVICE_ATTR_RW(cleaning_period, 0); 417static IIO_DEVICE_ATTR_RO(cleaning_period_available, 0); |
|
300 301static struct attribute *sps30_attrs[] = { 302 &iio_dev_attr_start_cleaning.dev_attr.attr, | 418 419static struct attribute *sps30_attrs[] = { 420 &iio_dev_attr_start_cleaning.dev_attr.attr, |
421 &iio_dev_attr_cleaning_period.dev_attr.attr, 422 &iio_dev_attr_cleaning_period_available.dev_attr.attr, |
|
303 NULL 304}; 305 306static const struct attribute_group sps30_attr_group = { 307 .attrs = sps30_attrs, 308}; 309 310static const struct iio_info sps30_info = { --- 46 unchanged lines hidden (view full) --- 357 358 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state)); 359 if (!indio_dev) 360 return -ENOMEM; 361 362 state = iio_priv(indio_dev); 363 i2c_set_clientdata(client, indio_dev); 364 state->client = client; | 423 NULL 424}; 425 426static const struct attribute_group sps30_attr_group = { 427 .attrs = sps30_attrs, 428}; 429 430static const struct iio_info sps30_info = { --- 46 unchanged lines hidden (view full) --- 477 478 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*state)); 479 if (!indio_dev) 480 return -ENOMEM; 481 482 state = iio_priv(indio_dev); 483 i2c_set_clientdata(client, indio_dev); 484 state->client = client; |
485 state->state = RESET; |
|
365 indio_dev->dev.parent = &client->dev; 366 indio_dev->info = &sps30_info; 367 indio_dev->name = client->name; 368 indio_dev->channels = sps30_channels; 369 indio_dev->num_channels = ARRAY_SIZE(sps30_channels); 370 indio_dev->modes = INDIO_DIRECT_MODE; 371 indio_dev->available_scan_masks = sps30_scan_masks; 372 373 mutex_init(&state->lock); 374 crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL); 375 | 486 indio_dev->dev.parent = &client->dev; 487 indio_dev->info = &sps30_info; 488 indio_dev->name = client->name; 489 indio_dev->channels = sps30_channels; 490 indio_dev->num_channels = ARRAY_SIZE(sps30_channels); 491 indio_dev->modes = INDIO_DIRECT_MODE; 492 indio_dev->available_scan_masks = sps30_scan_masks; 493 494 mutex_init(&state->lock); 495 crc8_populate_msb(sps30_crc8_table, SPS30_CRC8_POLYNOMIAL); 496 |
376 ret = sps30_do_cmd(state, SPS30_RESET, NULL, 0); | 497 ret = sps30_do_cmd_reset(state); |
377 if (ret) { 378 dev_err(&client->dev, "failed to reset device\n"); 379 return ret; 380 } | 498 if (ret) { 499 dev_err(&client->dev, "failed to reset device\n"); 500 return ret; 501 } |
381 msleep(300); 382 /* 383 * Power-on-reset causes sensor to produce some glitch on i2c bus and 384 * some controllers end up in error state. Recover simply by placing 385 * some data on the bus, for example STOP_MEAS command, which 386 * is NOP in this case. 387 */ 388 sps30_do_cmd(state, SPS30_STOP_MEAS, NULL, 0); | |
389 390 ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf)); 391 if (ret) { 392 dev_err(&client->dev, "failed to read serial number\n"); 393 return ret; 394 } 395 /* returned serial number is already NUL terminated */ 396 dev_info(&client->dev, "serial number: %s\n", buf); 397 | 502 503 ret = sps30_do_cmd(state, SPS30_READ_SERIAL, buf, sizeof(buf)); 504 if (ret) { 505 dev_err(&client->dev, "failed to read serial number\n"); 506 return ret; 507 } 508 /* returned serial number is already NUL terminated */ 509 dev_info(&client->dev, "serial number: %s\n", buf); 510 |
398 ret = sps30_do_cmd(state, SPS30_START_MEAS, NULL, 0); 399 if (ret) { 400 dev_err(&client->dev, "failed to start measurement\n"); 401 return ret; 402 } 403 | |
404 ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state); 405 if (ret) 406 return ret; 407 408 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 409 sps30_trigger_handler, NULL); 410 if (ret) 411 return ret; --- 29 unchanged lines hidden --- | 511 ret = devm_add_action_or_reset(&client->dev, sps30_stop_meas, state); 512 if (ret) 513 return ret; 514 515 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev, NULL, 516 sps30_trigger_handler, NULL); 517 if (ret) 518 return ret; --- 29 unchanged lines hidden --- |