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 unsigned long product_id
;
118 const struct iio_chan_spec
*channels
;
122 static const struct sgp_version supported_versions_sgp30
[] = {
129 static const struct sgp_version supported_versions_sgpc3
[] = {
136 static const struct iio_chan_spec sgp30_channels
[] = {
138 .type
= IIO_CONCENTRATION
,
139 .channel2
= IIO_MOD_VOC
,
141 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
142 .address
= SGP30_IAQ_TVOC_IDX
,
145 .type
= IIO_CONCENTRATION
,
146 .channel2
= IIO_MOD_CO2
,
148 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
149 .address
= SGP30_IAQ_CO2EQ_IDX
,
152 .type
= IIO_CONCENTRATION
,
153 .channel2
= IIO_MOD_ETHANOL
,
155 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
156 .address
= SGP30_SIG_ETOH_IDX
,
159 .type
= IIO_CONCENTRATION
,
160 .channel2
= IIO_MOD_H2
,
162 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
163 .address
= SGP30_SIG_H2_IDX
,
167 static const struct iio_chan_spec sgpc3_channels
[] = {
169 .type
= IIO_CONCENTRATION
,
170 .channel2
= IIO_MOD_VOC
,
172 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
173 .address
= SGPC3_IAQ_TVOC_IDX
,
176 .type
= IIO_CONCENTRATION
,
177 .channel2
= IIO_MOD_ETHANOL
,
179 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
180 .address
= SGPC3_SIG_ETOH_IDX
,
184 static const struct sgp_device sgp_devices
[] = {
187 .channels
= sgp30_channels
,
188 .num_channels
= ARRAY_SIZE(sgp30_channels
),
192 .channels
= sgpc3_channels
,
193 .num_channels
= ARRAY_SIZE(sgpc3_channels
),
198 * sgp_verify_buffer() - verify the checksums of the data buffer words
201 * @buf: Raw data buffer
202 * @word_count: Num data words stored in the buffer, excluding CRC bytes
204 * Return: 0 on success, negative error otherwise.
206 static int sgp_verify_buffer(const struct sgp_data
*data
,
207 union sgp_reading
*buf
, size_t word_count
)
209 size_t size
= word_count
* (SGP_WORD_LEN
+ SGP_CRC8_LEN
);
212 u8
*data_buf
= &buf
->start
;
214 for (i
= 0; i
< size
; i
+= SGP_WORD_LEN
+ SGP_CRC8_LEN
) {
215 crc
= crc8(sgp_crc8_table
, &data_buf
[i
], SGP_WORD_LEN
,
217 if (crc
!= data_buf
[i
+ SGP_WORD_LEN
]) {
218 dev_err(&data
->client
->dev
, "CRC error\n");
227 * sgp_read_cmd() - reads data from sensor after issuing a command
228 * The caller must hold data->data_lock for the duration of the call.
230 * @cmd: SGP Command to issue
231 * @buf: Raw data buffer to use
232 * @word_count: Num words to read, excluding CRC bytes
233 * @duration_us: Time taken to sensor to take a reading and data to be ready.
235 * Return: 0 on success, negative error otherwise.
237 static int sgp_read_cmd(struct sgp_data
*data
, enum sgp_cmd cmd
,
238 union sgp_reading
*buf
, size_t word_count
,
239 unsigned long duration_us
)
242 struct i2c_client
*client
= data
->client
;
243 size_t size
= word_count
* (SGP_WORD_LEN
+ SGP_CRC8_LEN
);
246 ret
= i2c_master_send(client
, (const char *)&cmd
, SGP_CMD_LEN
);
247 if (ret
!= SGP_CMD_LEN
)
249 usleep_range(duration_us
, duration_us
+ 1000);
254 data_buf
= &buf
->start
;
255 ret
= i2c_master_recv(client
, data_buf
, size
);
261 return sgp_verify_buffer(data
, buf
, word_count
);
265 * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
266 * The caller must hold data->data_lock for the duration of the call.
269 * Return: 0 on success, -EBUSY on default values, negative error
273 static int sgp_measure_iaq(struct sgp_data
*data
)
276 /* data contains default values */
277 bool default_vals
= !time_after(jiffies
, data
->iaq_init_start_jiffies
+
278 data
->iaq_defval_skip_jiffies
);
280 ret
= sgp_read_cmd(data
, data
->measure_iaq_cmd
, &data
->iaq_buffer
,
281 SGP_MEASUREMENT_LEN
, SGP_MEASUREMENT_DURATION_US
);
285 data
->iaq_buffer_state
= IAQ_BUFFER_DEFAULT_VALS
;
290 data
->iaq_buffer_state
= IAQ_BUFFER_VALID
;
295 static void sgp_iaq_thread_sleep_until(const struct sgp_data
*data
,
296 unsigned long sleep_jiffies
)
298 const long IAQ_POLL
= 50000;
300 while (!time_after(jiffies
, sleep_jiffies
)) {
301 usleep_range(IAQ_POLL
, IAQ_POLL
+ 10000);
302 if (kthread_should_stop() || data
->iaq_init_start_jiffies
== 0)
307 static int sgp_iaq_threadfn(void *p
)
309 struct sgp_data
*data
= (struct sgp_data
*)p
;
310 unsigned long next_update_jiffies
;
313 while (!kthread_should_stop()) {
314 mutex_lock(&data
->data_lock
);
315 if (data
->iaq_init_start_jiffies
== 0) {
316 ret
= sgp_read_cmd(data
, data
->iaq_init_cmd
, NULL
, 0,
317 SGP_CMD_DURATION_US
);
319 goto unlock_sleep_continue
;
320 data
->iaq_init_start_jiffies
= jiffies
;
323 ret
= sgp_measure_iaq(data
);
324 if (ret
&& ret
!= -EBUSY
) {
325 dev_warn(&data
->client
->dev
,
326 "IAQ measurement error [%d]\n", ret
);
328 unlock_sleep_continue
:
329 next_update_jiffies
= jiffies
+ data
->measure_interval_jiffies
;
330 mutex_unlock(&data
->data_lock
);
331 sgp_iaq_thread_sleep_until(data
, next_update_jiffies
);
337 static int sgp_read_raw(struct iio_dev
*indio_dev
,
338 struct iio_chan_spec
const *chan
, int *val
,
339 int *val2
, long mask
)
341 struct sgp_data
*data
= iio_priv(indio_dev
);
342 struct sgp_crc_word
*words
;
346 case IIO_CHAN_INFO_PROCESSED
:
347 mutex_lock(&data
->data_lock
);
348 if (data
->iaq_buffer_state
!= IAQ_BUFFER_VALID
) {
349 mutex_unlock(&data
->data_lock
);
352 words
= data
->iaq_buffer
.raw_words
;
353 switch (chan
->address
) {
354 case SGP30_IAQ_TVOC_IDX
:
355 case SGPC3_IAQ_TVOC_IDX
:
357 *val2
= be16_to_cpu(words
[1].value
);
358 ret
= IIO_VAL_INT_PLUS_NANO
;
360 case SGP30_IAQ_CO2EQ_IDX
:
362 *val2
= be16_to_cpu(words
[0].value
);
363 ret
= IIO_VAL_INT_PLUS_MICRO
;
369 mutex_unlock(&data
->data_lock
);
371 case IIO_CHAN_INFO_RAW
:
372 mutex_lock(&data
->data_lock
);
373 if (chan
->address
== SGPC3_SIG_ETOH_IDX
) {
374 if (data
->iaq_buffer_state
== IAQ_BUFFER_EMPTY
)
378 words
= data
->iaq_buffer
.raw_words
;
380 ret
= sgp_read_cmd(data
, data
->measure_gas_signals_cmd
,
381 &data
->buffer
, SGP_MEASUREMENT_LEN
,
382 SGP_MEASUREMENT_DURATION_US
);
383 words
= data
->buffer
.raw_words
;
386 mutex_unlock(&data
->data_lock
);
390 switch (chan
->address
) {
391 case SGP30_SIG_ETOH_IDX
:
392 *val
= be16_to_cpu(words
[1].value
);
395 case SGPC3_SIG_ETOH_IDX
:
396 case SGP30_SIG_H2_IDX
:
397 *val
= be16_to_cpu(words
[0].value
);
404 mutex_unlock(&data
->data_lock
);
413 static int sgp_check_compat(struct sgp_data
*data
,
414 unsigned int product_id
)
416 struct device
*dev
= &data
->client
->dev
;
417 const struct sgp_version
*supported_versions
;
419 u16 product
, generation
, major
, minor
;
421 /* driver does not match product */
422 generation
= SGP_VERS_GEN(data
);
423 if (generation
!= 0) {
425 "incompatible product generation %d != 0", generation
);
429 product
= SGP_VERS_PRODUCT(data
);
430 if (product
!= product_id
) {
431 dev_err(dev
, "sensor reports a different product: 0x%04x\n",
436 if (SGP_VERS_RESERVED(data
))
437 dev_warn(dev
, "reserved bit is set\n");
439 /* engineering samples are not supported: no interface guarantees */
440 if (SGP_VERS_ENG_BIT(data
))
445 supported_versions
= supported_versions_sgp30
;
446 num_fs
= ARRAY_SIZE(supported_versions_sgp30
);
449 supported_versions
= supported_versions_sgpc3
;
450 num_fs
= ARRAY_SIZE(supported_versions_sgpc3
);
456 major
= SGP_VERS_MAJOR(data
);
457 minor
= SGP_VERS_MINOR(data
);
458 for (ix
= 0; ix
< num_fs
; ix
++) {
459 if (major
== supported_versions
[ix
].major
&&
460 minor
>= supported_versions
[ix
].minor
)
463 dev_err(dev
, "unsupported sgp version: %d.%d\n", major
, minor
);
468 static void sgp_init(struct sgp_data
*data
)
470 data
->iaq_init_cmd
= SGP_CMD_IAQ_INIT
;
471 data
->iaq_init_start_jiffies
= 0;
472 data
->iaq_buffer_state
= IAQ_BUFFER_EMPTY
;
473 switch (SGP_VERS_PRODUCT(data
)) {
475 data
->measure_interval_jiffies
= SGP30_MEASURE_INTERVAL_HZ
* HZ
;
476 data
->measure_iaq_cmd
= SGP_CMD_IAQ_MEASURE
;
477 data
->measure_gas_signals_cmd
= SGP30_CMD_MEASURE_SIGNAL
;
478 data
->product_id
= SGP30
;
479 data
->iaq_defval_skip_jiffies
= 15 * HZ
;
482 data
->measure_interval_jiffies
= SGPC3_MEASURE_INTERVAL_HZ
* HZ
;
483 data
->measure_iaq_cmd
= SGPC3_CMD_MEASURE_RAW
;
484 data
->measure_gas_signals_cmd
= SGPC3_CMD_MEASURE_RAW
;
485 data
->product_id
= SGPC3
;
486 data
->iaq_defval_skip_jiffies
=
487 43 * data
->measure_interval_jiffies
;
492 static const struct iio_info sgp_info
= {
493 .read_raw
= sgp_read_raw
,
496 static const struct of_device_id sgp_dt_ids
[] = {
497 { .compatible
= "sensirion,sgp30", .data
= &sgp_devices
[SGP30
] },
498 { .compatible
= "sensirion,sgpc3", .data
= &sgp_devices
[SGPC3
] },
502 static int sgp_probe(struct i2c_client
*client
)
504 const struct i2c_device_id
*id
= i2c_client_get_device_id(client
);
505 const struct sgp_device
*match_data
;
506 struct device
*dev
= &client
->dev
;
507 struct iio_dev
*indio_dev
;
508 struct sgp_data
*data
;
511 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
515 match_data
= i2c_get_match_data(client
);
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
, match_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
= match_data
->channels
;
539 indio_dev
->num_channels
= match_data
->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 void 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
);
564 static const struct i2c_device_id sgp_id
[] = {
565 { "sgp30", (kernel_ulong_t
)&sgp_devices
[SGP30
] },
566 { "sgpc3", (kernel_ulong_t
)&sgp_devices
[SGPC3
] },
570 MODULE_DEVICE_TABLE(i2c
, sgp_id
);
571 MODULE_DEVICE_TABLE(of
, sgp_dt_ids
);
573 static struct i2c_driver sgp_driver
= {
576 .of_match_table
= sgp_dt_ids
,
579 .remove
= sgp_remove
,
582 module_i2c_driver(sgp_driver
);
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");