1 // SPDX-License-Identifier: GPL-2.0
3 * sgp30.c - Support for Sensirion SGP Gas Sensors
5 * Copyright (C) 2018 Andreas Brauchli <andreas.brauchli@sensirion.com>
7 * I2C slave address: 0x58
10 * https://www.sensirion.com/file/datasheet_sgp30
11 * https://www.sensirion.com/file/datasheet_sgpc3
15 * - humidity compensation
16 * - power mode switching (SGPC3)
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>
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)
48 DECLARE_CRC8_TABLE(sgp_crc8_table
);
55 enum sgp30_channel_idx
{
56 SGP30_IAQ_TVOC_IDX
= 0,
62 enum sgpc3_channel_idx
{
63 SGPC3_IAQ_TVOC_IDX
= 10,
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),
73 SGP30_CMD_MEASURE_SIGNAL
= SGP_CMD(0x2050),
75 SGPC3_CMD_MEASURE_RAW
= SGP_CMD(0x2046),
86 } __attribute__((__packed__
));
90 struct sgp_crc_word raw_words
[4];
93 enum _iaq_buffer_state
{
95 IAQ_BUFFER_DEFAULT_VALS
,
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
;
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
;
117 const struct iio_chan_spec
*channels
;
121 static const struct sgp_version supported_versions_sgp30
[] = {
128 static const struct sgp_version supported_versions_sgpc3
[] = {
135 static const struct iio_chan_spec sgp30_channels
[] = {
137 .type
= IIO_CONCENTRATION
,
138 .channel2
= IIO_MOD_VOC
,
140 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
141 .address
= SGP30_IAQ_TVOC_IDX
,
144 .type
= IIO_CONCENTRATION
,
145 .channel2
= IIO_MOD_CO2
,
147 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
148 .address
= SGP30_IAQ_CO2EQ_IDX
,
151 .type
= IIO_CONCENTRATION
,
152 .channel2
= IIO_MOD_ETHANOL
,
154 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
155 .address
= SGP30_SIG_ETOH_IDX
,
158 .type
= IIO_CONCENTRATION
,
159 .channel2
= IIO_MOD_H2
,
161 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
162 .address
= SGP30_SIG_H2_IDX
,
166 static const struct iio_chan_spec sgpc3_channels
[] = {
168 .type
= IIO_CONCENTRATION
,
169 .channel2
= IIO_MOD_VOC
,
171 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
172 .address
= SGPC3_IAQ_TVOC_IDX
,
175 .type
= IIO_CONCENTRATION
,
176 .channel2
= IIO_MOD_ETHANOL
,
178 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
179 .address
= SGPC3_SIG_ETOH_IDX
,
183 static const struct sgp_device sgp_devices
[] = {
185 .channels
= sgp30_channels
,
186 .num_channels
= ARRAY_SIZE(sgp30_channels
),
189 .channels
= sgpc3_channels
,
190 .num_channels
= ARRAY_SIZE(sgpc3_channels
),
195 * sgp_verify_buffer() - verify the checksums of the data buffer words
198 * @buf: Raw data buffer
199 * @word_count: Num data words stored in the buffer, excluding CRC bytes
201 * Return: 0 on success, negative error otherwise.
203 static int sgp_verify_buffer(const struct sgp_data
*data
,
204 union sgp_reading
*buf
, size_t word_count
)
206 size_t size
= word_count
* (SGP_WORD_LEN
+ SGP_CRC8_LEN
);
209 u8
*data_buf
= &buf
->start
;
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
,
214 if (crc
!= data_buf
[i
+ SGP_WORD_LEN
]) {
215 dev_err(&data
->client
->dev
, "CRC error\n");
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.
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.
232 * Return: 0 on success, negative error otherwise.
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
)
239 struct i2c_client
*client
= data
->client
;
240 size_t size
= word_count
* (SGP_WORD_LEN
+ SGP_CRC8_LEN
);
243 ret
= i2c_master_send(client
, (const char *)&cmd
, SGP_CMD_LEN
);
244 if (ret
!= SGP_CMD_LEN
)
246 usleep_range(duration_us
, duration_us
+ 1000);
251 data_buf
= &buf
->start
;
252 ret
= i2c_master_recv(client
, data_buf
, size
);
258 return sgp_verify_buffer(data
, buf
, word_count
);
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.
266 * Return: 0 on success, -EBUSY on default values, negative error
270 static int sgp_measure_iaq(struct sgp_data
*data
)
273 /* data contains default values */
274 bool default_vals
= !time_after(jiffies
, data
->iaq_init_start_jiffies
+
275 data
->iaq_defval_skip_jiffies
);
277 ret
= sgp_read_cmd(data
, data
->measure_iaq_cmd
, &data
->iaq_buffer
,
278 SGP_MEASUREMENT_LEN
, SGP_MEASUREMENT_DURATION_US
);
282 data
->iaq_buffer_state
= IAQ_BUFFER_DEFAULT_VALS
;
287 data
->iaq_buffer_state
= IAQ_BUFFER_VALID
;
292 static void sgp_iaq_thread_sleep_until(const struct sgp_data
*data
,
293 unsigned long sleep_jiffies
)
295 const long IAQ_POLL
= 50000;
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)
304 static int sgp_iaq_threadfn(void *p
)
306 struct sgp_data
*data
= (struct sgp_data
*)p
;
307 unsigned long next_update_jiffies
;
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
);
316 goto unlock_sleep_continue
;
317 data
->iaq_init_start_jiffies
= jiffies
;
320 ret
= sgp_measure_iaq(data
);
321 if (ret
&& ret
!= -EBUSY
) {
322 dev_warn(&data
->client
->dev
,
323 "IAQ measurement error [%d]\n", ret
);
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
);
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
)
338 struct sgp_data
*data
= iio_priv(indio_dev
);
339 struct sgp_crc_word
*words
;
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
);
349 words
= data
->iaq_buffer
.raw_words
;
350 switch (chan
->address
) {
351 case SGP30_IAQ_TVOC_IDX
:
352 case SGPC3_IAQ_TVOC_IDX
:
354 *val2
= be16_to_cpu(words
[1].value
);
355 ret
= IIO_VAL_INT_PLUS_NANO
;
357 case SGP30_IAQ_CO2EQ_IDX
:
359 *val2
= be16_to_cpu(words
[0].value
);
360 ret
= IIO_VAL_INT_PLUS_MICRO
;
366 mutex_unlock(&data
->data_lock
);
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
)
375 words
= data
->iaq_buffer
.raw_words
;
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
;
383 mutex_unlock(&data
->data_lock
);
387 switch (chan
->address
) {
388 case SGP30_SIG_ETOH_IDX
:
389 *val
= be16_to_cpu(words
[1].value
);
392 case SGPC3_SIG_ETOH_IDX
:
393 case SGP30_SIG_H2_IDX
:
394 *val
= be16_to_cpu(words
[0].value
);
401 mutex_unlock(&data
->data_lock
);
410 static int sgp_check_compat(struct sgp_data
*data
,
411 unsigned int product_id
)
413 struct device
*dev
= &data
->client
->dev
;
414 const struct sgp_version
*supported_versions
;
416 u16 product
, generation
, major
, minor
;
418 /* driver does not match product */
419 generation
= SGP_VERS_GEN(data
);
420 if (generation
!= 0) {
422 "incompatible product generation %d != 0", generation
);
426 product
= SGP_VERS_PRODUCT(data
);
427 if (product
!= product_id
) {
428 dev_err(dev
, "sensor reports a different product: 0x%04hx\n",
433 if (SGP_VERS_RESERVED(data
))
434 dev_warn(dev
, "reserved bit is set\n");
436 /* engineering samples are not supported: no interface guarantees */
437 if (SGP_VERS_ENG_BIT(data
))
442 supported_versions
= supported_versions_sgp30
;
443 num_fs
= ARRAY_SIZE(supported_versions_sgp30
);
446 supported_versions
= supported_versions_sgpc3
;
447 num_fs
= ARRAY_SIZE(supported_versions_sgpc3
);
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
)
460 dev_err(dev
, "unsupported sgp version: %d.%d\n", major
, minor
);
465 static void sgp_init(struct sgp_data
*data
)
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
)) {
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
;
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
;
489 static const struct iio_info sgp_info
= {
490 .read_raw
= sgp_read_raw
,
493 static const struct of_device_id sgp_dt_ids
[] = {
494 { .compatible
= "sensirion,sgp30", .data
= (void *)SGP30
},
495 { .compatible
= "sensirion,sgpc3", .data
= (void *)SGPC3
},
499 static int sgp_probe(struct i2c_client
*client
,
500 const struct i2c_device_id
*id
)
502 struct device
*dev
= &client
->dev
;
503 struct iio_dev
*indio_dev
;
504 struct sgp_data
*data
;
505 unsigned long product_id
;
508 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
513 product_id
= (unsigned long)device_get_match_data(dev
);
515 product_id
= id
->driver_data
;
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
);
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
);
529 data
->feature_set
= be16_to_cpu(data
->buffer
.raw_words
[0].value
);
531 ret
= sgp_check_compat(data
, product_id
);
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
;
543 ret
= devm_iio_device_register(dev
, indio_dev
);
545 dev_err(dev
, "failed to register iio device\n");
549 data
->iaq_thread
= kthread_run(sgp_iaq_threadfn
, data
,
550 "%s-iaq", data
->client
->name
);
555 static int sgp_remove(struct i2c_client
*client
)
557 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
558 struct sgp_data
*data
= iio_priv(indio_dev
);
560 if (data
->iaq_thread
)
561 kthread_stop(data
->iaq_thread
);
566 static const struct i2c_device_id sgp_id
[] = {
572 MODULE_DEVICE_TABLE(i2c
, sgp_id
);
573 MODULE_DEVICE_TABLE(of
, sgp_dt_ids
);
575 static struct i2c_driver sgp_driver
= {
578 .of_match_table
= sgp_dt_ids
,
581 .remove
= sgp_remove
,
584 module_i2c_driver(sgp_driver
);
586 MODULE_AUTHOR("Andreas Brauchli <andreas.brauchli@sensirion.com>");
587 MODULE_AUTHOR("Pascal Sachs <pascal.sachs@sensirion.com>");
588 MODULE_DESCRIPTION("Sensirion SGP gas sensors");
589 MODULE_LICENSE("GPL v2");