1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * sgp30.c - Support for Sensirion SGP Gas Sensors 4 * 5 * Copyright (C) 2018 Andreas Brauchli <andreas.brauchli@sensirion.com> 6 * 7 * I2C slave address: 0x58 8 * 9 * Datasheets: 10 * https://www.sensirion.com/file/datasheet_sgp30 11 * https://www.sensirion.com/file/datasheet_sgpc3 12 * 13 * TODO: 14 * - baseline support 15 * - humidity compensation 16 * - power mode switching (SGPC3) 17 */ 18 19 #include <linux/crc8.h> 20 #include <linux/delay.h> 21 #include <linux/kthread.h> 22 #include <linux/module.h> 23 #include <linux/mod_devicetable.h> 24 #include <linux/mutex.h> 25 #include <linux/i2c.h> 26 #include <linux/iio/iio.h> 27 #include <linux/iio/sysfs.h> 28 29 #define SGP_WORD_LEN 2 30 #define SGP_CRC8_POLYNOMIAL 0x31 31 #define SGP_CRC8_INIT 0xff 32 #define SGP_CRC8_LEN 1 33 #define SGP_CMD(cmd_word) cpu_to_be16(cmd_word) 34 #define SGP_CMD_DURATION_US 12000 35 #define SGP_MEASUREMENT_DURATION_US 50000 36 #define SGP_CMD_LEN SGP_WORD_LEN 37 #define SGP_CMD_MAX_BUF_SIZE (SGP_CMD_LEN + 2 * SGP_WORD_LEN) 38 #define SGP_MEASUREMENT_LEN 2 39 #define SGP30_MEASURE_INTERVAL_HZ 1 40 #define SGPC3_MEASURE_INTERVAL_HZ 2 41 #define SGP_VERS_PRODUCT(data) ((((data)->feature_set) & 0xf000) >> 12) 42 #define SGP_VERS_RESERVED(data) ((((data)->feature_set) & 0x0800) >> 11) 43 #define SGP_VERS_GEN(data) ((((data)->feature_set) & 0x0600) >> 9) 44 #define SGP_VERS_ENG_BIT(data) ((((data)->feature_set) & 0x0100) >> 8) 45 #define SGP_VERS_MAJOR(data) ((((data)->feature_set) & 0x00e0) >> 5) 46 #define SGP_VERS_MINOR(data) (((data)->feature_set) & 0x001f) 47 48 DECLARE_CRC8_TABLE(sgp_crc8_table); 49 50 enum sgp_product_id { 51 SGP30 = 0, 52 SGPC3, 53 }; 54 55 enum sgp30_channel_idx { 56 SGP30_IAQ_TVOC_IDX = 0, 57 SGP30_IAQ_CO2EQ_IDX, 58 SGP30_SIG_ETOH_IDX, 59 SGP30_SIG_H2_IDX, 60 }; 61 62 enum sgpc3_channel_idx { 63 SGPC3_IAQ_TVOC_IDX = 10, 64 SGPC3_SIG_ETOH_IDX, 65 }; 66 67 enum sgp_cmd { 68 SGP_CMD_IAQ_INIT = SGP_CMD(0x2003), 69 SGP_CMD_IAQ_MEASURE = SGP_CMD(0x2008), 70 SGP_CMD_GET_FEATURE_SET = SGP_CMD(0x202f), 71 SGP_CMD_GET_SERIAL_ID = SGP_CMD(0x3682), 72 73 SGP30_CMD_MEASURE_SIGNAL = SGP_CMD(0x2050), 74 75 SGPC3_CMD_MEASURE_RAW = SGP_CMD(0x2046), 76 }; 77 78 struct sgp_version { 79 u8 major; 80 u8 minor; 81 }; 82 83 struct sgp_crc_word { 84 __be16 value; 85 u8 crc8; 86 } __attribute__((__packed__)); 87 88 union sgp_reading { 89 u8 start; 90 struct sgp_crc_word raw_words[4]; 91 }; 92 93 enum _iaq_buffer_state { 94 IAQ_BUFFER_EMPTY = 0, 95 IAQ_BUFFER_DEFAULT_VALS, 96 IAQ_BUFFER_VALID, 97 }; 98 99 struct sgp_data { 100 struct i2c_client *client; 101 struct task_struct *iaq_thread; 102 struct mutex data_lock; 103 unsigned long iaq_init_start_jiffies; 104 unsigned long iaq_defval_skip_jiffies; 105 u16 product_id; 106 u16 feature_set; 107 unsigned long measure_interval_jiffies; 108 enum sgp_cmd iaq_init_cmd; 109 enum sgp_cmd measure_iaq_cmd; 110 enum sgp_cmd measure_gas_signals_cmd; 111 union sgp_reading buffer; 112 union sgp_reading iaq_buffer; 113 enum _iaq_buffer_state iaq_buffer_state; 114 }; 115 116 struct sgp_device { 117 const struct iio_chan_spec *channels; 118 int num_channels; 119 }; 120 121 static const struct sgp_version supported_versions_sgp30[] = { 122 { 123 .major = 1, 124 .minor = 0, 125 }, 126 }; 127 128 static const struct sgp_version supported_versions_sgpc3[] = { 129 { 130 .major = 0, 131 .minor = 4, 132 }, 133 }; 134 135 static const struct iio_chan_spec sgp30_channels[] = { 136 { 137 .type = IIO_CONCENTRATION, 138 .channel2 = IIO_MOD_VOC, 139 .modified = 1, 140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 141 .address = SGP30_IAQ_TVOC_IDX, 142 }, 143 { 144 .type = IIO_CONCENTRATION, 145 .channel2 = IIO_MOD_CO2, 146 .modified = 1, 147 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 148 .address = SGP30_IAQ_CO2EQ_IDX, 149 }, 150 { 151 .type = IIO_CONCENTRATION, 152 .channel2 = IIO_MOD_ETHANOL, 153 .modified = 1, 154 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 155 .address = SGP30_SIG_ETOH_IDX, 156 }, 157 { 158 .type = IIO_CONCENTRATION, 159 .channel2 = IIO_MOD_H2, 160 .modified = 1, 161 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 162 .address = SGP30_SIG_H2_IDX, 163 }, 164 }; 165 166 static const struct iio_chan_spec sgpc3_channels[] = { 167 { 168 .type = IIO_CONCENTRATION, 169 .channel2 = IIO_MOD_VOC, 170 .modified = 1, 171 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), 172 .address = SGPC3_IAQ_TVOC_IDX, 173 }, 174 { 175 .type = IIO_CONCENTRATION, 176 .channel2 = IIO_MOD_ETHANOL, 177 .modified = 1, 178 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 179 .address = SGPC3_SIG_ETOH_IDX, 180 }, 181 }; 182 183 static const struct sgp_device sgp_devices[] = { 184 [SGP30] = { 185 .channels = sgp30_channels, 186 .num_channels = ARRAY_SIZE(sgp30_channels), 187 }, 188 [SGPC3] = { 189 .channels = sgpc3_channels, 190 .num_channels = ARRAY_SIZE(sgpc3_channels), 191 }, 192 }; 193 194 /** 195 * sgp_verify_buffer() - verify the checksums of the data buffer words 196 * 197 * @data: SGP data 198 * @buf: Raw data buffer 199 * @word_count: Num data words stored in the buffer, excluding CRC bytes 200 * 201 * Return: 0 on success, negative error otherwise. 202 */ 203 static int sgp_verify_buffer(const struct sgp_data *data, 204 union sgp_reading *buf, size_t word_count) 205 { 206 size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN); 207 int i; 208 u8 crc; 209 u8 *data_buf = &buf->start; 210 211 for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) { 212 crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN, 213 SGP_CRC8_INIT); 214 if (crc != data_buf[i + SGP_WORD_LEN]) { 215 dev_err(&data->client->dev, "CRC error\n"); 216 return -EIO; 217 } 218 } 219 220 return 0; 221 } 222 223 /** 224 * sgp_read_cmd() - reads data from sensor after issuing a command 225 * The caller must hold data->data_lock for the duration of the call. 226 * @data: SGP data 227 * @cmd: SGP Command to issue 228 * @buf: Raw data buffer to use 229 * @word_count: Num words to read, excluding CRC bytes 230 * @duration_us: Time taken to sensor to take a reading and data to be ready. 231 * 232 * Return: 0 on success, negative error otherwise. 233 */ 234 static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd, 235 union sgp_reading *buf, size_t word_count, 236 unsigned long duration_us) 237 { 238 int ret; 239 struct i2c_client *client = data->client; 240 size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN); 241 u8 *data_buf; 242 243 ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN); 244 if (ret != SGP_CMD_LEN) 245 return -EIO; 246 usleep_range(duration_us, duration_us + 1000); 247 248 if (word_count == 0) 249 return 0; 250 251 data_buf = &buf->start; 252 ret = i2c_master_recv(client, data_buf, size); 253 if (ret < 0) 254 return ret; 255 if (ret != size) 256 return -EIO; 257 258 return sgp_verify_buffer(data, buf, word_count); 259 } 260 261 /** 262 * sgp_measure_iaq() - measure and retrieve IAQ values from sensor 263 * The caller must hold data->data_lock for the duration of the call. 264 * @data: SGP data 265 * 266 * Return: 0 on success, -EBUSY on default values, negative error 267 * otherwise. 268 */ 269 270 static int sgp_measure_iaq(struct sgp_data *data) 271 { 272 int ret; 273 /* data contains default values */ 274 bool default_vals = !time_after(jiffies, data->iaq_init_start_jiffies + 275 data->iaq_defval_skip_jiffies); 276 277 ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data->iaq_buffer, 278 SGP_MEASUREMENT_LEN, SGP_MEASUREMENT_DURATION_US); 279 if (ret < 0) 280 return ret; 281 282 data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS; 283 284 if (default_vals) 285 return -EBUSY; 286 287 data->iaq_buffer_state = IAQ_BUFFER_VALID; 288 289 return 0; 290 } 291 292 static void sgp_iaq_thread_sleep_until(const struct sgp_data *data, 293 unsigned long sleep_jiffies) 294 { 295 const long IAQ_POLL = 50000; 296 297 while (!time_after(jiffies, sleep_jiffies)) { 298 usleep_range(IAQ_POLL, IAQ_POLL + 10000); 299 if (kthread_should_stop() || data->iaq_init_start_jiffies == 0) 300 return; 301 } 302 } 303 304 static int sgp_iaq_threadfn(void *p) 305 { 306 struct sgp_data *data = (struct sgp_data *)p; 307 unsigned long next_update_jiffies; 308 int ret; 309 310 while (!kthread_should_stop()) { 311 mutex_lock(&data->data_lock); 312 if (data->iaq_init_start_jiffies == 0) { 313 ret = sgp_read_cmd(data, data->iaq_init_cmd, NULL, 0, 314 SGP_CMD_DURATION_US); 315 if (ret < 0) 316 goto unlock_sleep_continue; 317 data->iaq_init_start_jiffies = jiffies; 318 } 319 320 ret = sgp_measure_iaq(data); 321 if (ret && ret != -EBUSY) { 322 dev_warn(&data->client->dev, 323 "IAQ measurement error [%d]\n", ret); 324 } 325 unlock_sleep_continue: 326 next_update_jiffies = jiffies + data->measure_interval_jiffies; 327 mutex_unlock(&data->data_lock); 328 sgp_iaq_thread_sleep_until(data, next_update_jiffies); 329 } 330 331 return 0; 332 } 333 334 static int sgp_read_raw(struct iio_dev *indio_dev, 335 struct iio_chan_spec const *chan, int *val, 336 int *val2, long mask) 337 { 338 struct sgp_data *data = iio_priv(indio_dev); 339 struct sgp_crc_word *words; 340 int ret; 341 342 switch (mask) { 343 case IIO_CHAN_INFO_PROCESSED: 344 mutex_lock(&data->data_lock); 345 if (data->iaq_buffer_state != IAQ_BUFFER_VALID) { 346 mutex_unlock(&data->data_lock); 347 return -EBUSY; 348 } 349 words = data->iaq_buffer.raw_words; 350 switch (chan->address) { 351 case SGP30_IAQ_TVOC_IDX: 352 case SGPC3_IAQ_TVOC_IDX: 353 *val = 0; 354 *val2 = be16_to_cpu(words[1].value); 355 ret = IIO_VAL_INT_PLUS_NANO; 356 break; 357 case SGP30_IAQ_CO2EQ_IDX: 358 *val = 0; 359 *val2 = be16_to_cpu(words[0].value); 360 ret = IIO_VAL_INT_PLUS_MICRO; 361 break; 362 default: 363 ret = -EINVAL; 364 break; 365 } 366 mutex_unlock(&data->data_lock); 367 break; 368 case IIO_CHAN_INFO_RAW: 369 mutex_lock(&data->data_lock); 370 if (chan->address == SGPC3_SIG_ETOH_IDX) { 371 if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY) 372 ret = -EBUSY; 373 else 374 ret = 0; 375 words = data->iaq_buffer.raw_words; 376 } else { 377 ret = sgp_read_cmd(data, data->measure_gas_signals_cmd, 378 &data->buffer, SGP_MEASUREMENT_LEN, 379 SGP_MEASUREMENT_DURATION_US); 380 words = data->buffer.raw_words; 381 } 382 if (ret) { 383 mutex_unlock(&data->data_lock); 384 return ret; 385 } 386 387 switch (chan->address) { 388 case SGP30_SIG_ETOH_IDX: 389 *val = be16_to_cpu(words[1].value); 390 ret = IIO_VAL_INT; 391 break; 392 case SGPC3_SIG_ETOH_IDX: 393 case SGP30_SIG_H2_IDX: 394 *val = be16_to_cpu(words[0].value); 395 ret = IIO_VAL_INT; 396 break; 397 default: 398 ret = -EINVAL; 399 break; 400 } 401 mutex_unlock(&data->data_lock); 402 break; 403 default: 404 return -EINVAL; 405 } 406 407 return ret; 408 } 409 410 static int sgp_check_compat(struct sgp_data *data, 411 unsigned int product_id) 412 { 413 struct device *dev = &data->client->dev; 414 const struct sgp_version *supported_versions; 415 u16 ix, num_fs; 416 u16 product, generation, major, minor; 417 418 /* driver does not match product */ 419 generation = SGP_VERS_GEN(data); 420 if (generation != 0) { 421 dev_err(dev, 422 "incompatible product generation %d != 0", generation); 423 return -ENODEV; 424 } 425 426 product = SGP_VERS_PRODUCT(data); 427 if (product != product_id) { 428 dev_err(dev, "sensor reports a different product: 0x%04x\n", 429 product); 430 return -ENODEV; 431 } 432 433 if (SGP_VERS_RESERVED(data)) 434 dev_warn(dev, "reserved bit is set\n"); 435 436 /* engineering samples are not supported: no interface guarantees */ 437 if (SGP_VERS_ENG_BIT(data)) 438 return -ENODEV; 439 440 switch (product) { 441 case SGP30: 442 supported_versions = supported_versions_sgp30; 443 num_fs = ARRAY_SIZE(supported_versions_sgp30); 444 break; 445 case SGPC3: 446 supported_versions = supported_versions_sgpc3; 447 num_fs = ARRAY_SIZE(supported_versions_sgpc3); 448 break; 449 default: 450 return -ENODEV; 451 } 452 453 major = SGP_VERS_MAJOR(data); 454 minor = SGP_VERS_MINOR(data); 455 for (ix = 0; ix < num_fs; ix++) { 456 if (major == supported_versions[ix].major && 457 minor >= supported_versions[ix].minor) 458 return 0; 459 } 460 dev_err(dev, "unsupported sgp version: %d.%d\n", major, minor); 461 462 return -ENODEV; 463 } 464 465 static void sgp_init(struct sgp_data *data) 466 { 467 data->iaq_init_cmd = SGP_CMD_IAQ_INIT; 468 data->iaq_init_start_jiffies = 0; 469 data->iaq_buffer_state = IAQ_BUFFER_EMPTY; 470 switch (SGP_VERS_PRODUCT(data)) { 471 case SGP30: 472 data->measure_interval_jiffies = SGP30_MEASURE_INTERVAL_HZ * HZ; 473 data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE; 474 data->measure_gas_signals_cmd = SGP30_CMD_MEASURE_SIGNAL; 475 data->product_id = SGP30; 476 data->iaq_defval_skip_jiffies = 15 * HZ; 477 break; 478 case SGPC3: 479 data->measure_interval_jiffies = SGPC3_MEASURE_INTERVAL_HZ * HZ; 480 data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW; 481 data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW; 482 data->product_id = SGPC3; 483 data->iaq_defval_skip_jiffies = 484 43 * data->measure_interval_jiffies; 485 break; 486 } 487 } 488 489 static const struct iio_info sgp_info = { 490 .read_raw = sgp_read_raw, 491 }; 492 493 static const struct of_device_id sgp_dt_ids[] = { 494 { .compatible = "sensirion,sgp30", .data = (void *)SGP30 }, 495 { .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 }, 496 { } 497 }; 498 499 static int sgp_probe(struct i2c_client *client, 500 const struct i2c_device_id *id) 501 { 502 struct device *dev = &client->dev; 503 struct iio_dev *indio_dev; 504 struct sgp_data *data; 505 unsigned long product_id; 506 int ret; 507 508 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 509 if (!indio_dev) 510 return -ENOMEM; 511 512 if (dev_fwnode(dev)) 513 product_id = (unsigned long)device_get_match_data(dev); 514 else 515 product_id = id->driver_data; 516 517 data = iio_priv(indio_dev); 518 i2c_set_clientdata(client, indio_dev); 519 data->client = client; 520 crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL); 521 mutex_init(&data->data_lock); 522 523 /* get feature set version and write it to client data */ 524 ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data->buffer, 1, 525 SGP_CMD_DURATION_US); 526 if (ret < 0) 527 return ret; 528 529 data->feature_set = be16_to_cpu(data->buffer.raw_words[0].value); 530 531 ret = sgp_check_compat(data, product_id); 532 if (ret) 533 return ret; 534 535 indio_dev->info = &sgp_info; 536 indio_dev->name = id->name; 537 indio_dev->modes = INDIO_DIRECT_MODE; 538 indio_dev->channels = sgp_devices[product_id].channels; 539 indio_dev->num_channels = sgp_devices[product_id].num_channels; 540 541 sgp_init(data); 542 543 ret = devm_iio_device_register(dev, indio_dev); 544 if (ret) { 545 dev_err(dev, "failed to register iio device\n"); 546 return ret; 547 } 548 549 data->iaq_thread = kthread_run(sgp_iaq_threadfn, data, 550 "%s-iaq", data->client->name); 551 552 return 0; 553 } 554 555 static void sgp_remove(struct i2c_client *client) 556 { 557 struct iio_dev *indio_dev = i2c_get_clientdata(client); 558 struct sgp_data *data = iio_priv(indio_dev); 559 560 if (data->iaq_thread) 561 kthread_stop(data->iaq_thread); 562 } 563 564 static const struct i2c_device_id sgp_id[] = { 565 { "sgp30", SGP30 }, 566 { "sgpc3", SGPC3 }, 567 { } 568 }; 569 570 MODULE_DEVICE_TABLE(i2c, sgp_id); 571 MODULE_DEVICE_TABLE(of, sgp_dt_ids); 572 573 static struct i2c_driver sgp_driver = { 574 .driver = { 575 .name = "sgp30", 576 .of_match_table = sgp_dt_ids, 577 }, 578 .probe = sgp_probe, 579 .remove = sgp_remove, 580 .id_table = sgp_id, 581 }; 582 module_i2c_driver(sgp_driver); 583 584 MODULE_AUTHOR("Andreas Brauchli <andreas.brauchli@sensirion.com>"); 585 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>"); 586 MODULE_DESCRIPTION("Sensirion SGP gas sensors"); 587 MODULE_LICENSE("GPL v2"); 588