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