Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
/*	$NetBSD: sgp40.c,v 1.5 2022/05/24 06:28:01 andvar Exp $	*/

/*
 * Copyright (c) 2021 Brad Spencer <brad@anduin.eldar.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGL`IGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: sgp40.c,v 1.5 2022/05/24 06:28:01 andvar Exp $");

/*
  Driver for the Sensirion SGP40 MOx gas sensor for air quality
*/

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/kthread.h>

#include <dev/sysmon/sysmonvar.h>
#include <dev/i2c/i2cvar.h>
#include <dev/i2c/sgp40reg.h>
#include <dev/i2c/sgp40var.h>

#include <dev/i2c/sensirion_arch_config.h>
#include <dev/i2c/sensirion_voc_algorithm.h>

static uint8_t 	sgp40_crc(uint8_t *, size_t);
static int      sgp40_cmdr(struct sgp40_sc *, uint16_t, uint8_t *, uint8_t,
    uint8_t *, size_t);
static int 	sgp40_poke(i2c_tag_t, i2c_addr_t, bool);
static int 	sgp40_match(device_t, cfdata_t, void *);
static void 	sgp40_attach(device_t, device_t, void *);
static int 	sgp40_detach(device_t, int);
static void 	sgp40_refresh(struct sysmon_envsys *, envsys_data_t *);
static int 	sgp40_verify_sysctl(SYSCTLFN_ARGS);
static int 	sgp40_verify_temp_sysctl(SYSCTLFN_ARGS);
static int 	sgp40_verify_rh_sysctl(SYSCTLFN_ARGS);
static void     sgp40_thread(void *);
static void     sgp40_stop_thread(void *);
static void     sgp40_take_measurement(void *, VocAlgorithmParams *);

#define SGP40_DEBUG
#ifdef SGP40_DEBUG
#define DPRINTF(s, l, x) \
    do { \
	if (l <= s->sc_sgp40debug) \
	    printf x; \
    } while (/*CONSTCOND*/0)
#else
#define DPRINTF(s, l, x)
#endif

CFATTACH_DECL_NEW(sgp40mox, sizeof(struct sgp40_sc),
    sgp40_match, sgp40_attach, sgp40_detach, NULL);

static struct sgp40_sensor sgp40_sensors[] = {
	{
		.desc = "VOC index",
		.type = ENVSYS_INTEGER,
	}
};

static struct sgp40_timing sgp40_timings[] = {
	{
		.cmd = SGP40_MEASURE_RAW,
		.typicaldelay = 25000,
	},
	{
		.cmd = SGP40_MEASURE_TEST,
		.typicaldelay = 240000,
	},
	{
		.cmd = SGP40_HEATER_OFF,
		.typicaldelay = 100,
	},
	{
		.cmd = SGP40_GET_SERIAL_NUMBER,
		.typicaldelay = 100,
	},
	{
		.cmd = SGP40_GET_FEATURESET,
		.typicaldelay = 1000,
	}
};

void
sgp40_thread(void *aux)
{
	struct sgp40_sc *sc = aux;
	int rv;
	VocAlgorithmParams voc_algorithm_params;

	mutex_enter(&sc->sc_threadmutex);

	VocAlgorithm_init(&voc_algorithm_params);

	while (!sc->sc_stopping) {
		rv = cv_timedwait(&sc->sc_condvar, &sc->sc_threadmutex,
		    mstohz(1000));
		if (rv == EWOULDBLOCK && !sc->sc_stopping) {
			sgp40_take_measurement(sc,&voc_algorithm_params);
		}
	}
	mutex_exit(&sc->sc_threadmutex);
	kthread_exit(0);
}

static void
sgp40_stop_thread(void *aux)
{
	struct sgp40_sc *sc;
	sc = aux;
	int error;

	mutex_enter(&sc->sc_threadmutex);
	sc->sc_stopping = true;
	cv_signal(&sc->sc_condvar);
	mutex_exit(&sc->sc_threadmutex);

	/* wait for the thread to exit */
	kthread_join(sc->sc_thread);

	mutex_enter(&sc->sc_mutex);
	error = iic_acquire_bus(sc->sc_tag, 0);
	if (error) {
		DPRINTF(sc, 2, ("%s: Could not acquire iic bus for heater off "
		    "in stop thread: %d\n", device_xname(sc->sc_dev), error));
		goto out;
	}
	error = sgp40_cmdr(sc, SGP40_HEATER_OFF, NULL, 0, NULL, 0);
	if (error) {
		DPRINTF(sc, 2, ("%s: Error turning heater off: %d\n",
		    device_xname(sc->sc_dev), error));
	}
out:
	iic_release_bus(sc->sc_tag, 0);
	mutex_exit(&sc->sc_mutex);
}

static int
sgp40_compute_temp_comp(int unconverted)
{
	/*
	 * The published algorithm for this conversion is:
	 * (temp_in_celsius + 45) * 65535 / 175
	 *
	 * However, this did not exactly yield the results that
	 * the example in the data sheet, so something a little
	 * different was done.
	 *
	 * (temp_in_celsius + 45) * 65536 / 175
	 *
	 * This was also scaled up by 10^2 and then scaled back to
	 * preserve some precision.  37449 is simply (65536 * 100) / 175
	 * and rounded.
	 */

	return (((unconverted + 45) * 100) * 37449) / 10000;
}

static int
sgp40_compute_rh_comp(int unconverted)
{
	int q;

	/*
	 * The published algorithm for this conversion is:
	 * %rh * 65535 / 100
	 *
	 * However, this did not exactly yield the results that
	 * the example in the data sheet, so something a little
	 * different was done.
	 *
	 * %rh * 65536 / 100
	 *
	 * This was also scaled up by 10^2 and then scaled back to
	 * preserve some precision.  The value is also latched to 65535
	 * as an upper limit.
	 */

	q = ((unconverted * 100) * 65536) / 10000;
	if (q > 65535)
		q = 65535;
	return q;
}

static void
sgp40_take_measurement(void *aux, VocAlgorithmParams* params)
{
	struct sgp40_sc *sc;
	sc = aux;
	uint8_t args[6];
	uint8_t buf[3];
	uint16_t rawmeasurement;
	int error;
	uint8_t crc;
	uint16_t convertedrh, convertedtemp;
	int32_t voc_index;

	mutex_enter(&sc->sc_mutex);
	convertedrh = (uint16_t)sgp40_compute_rh_comp(sc->sc_rhcomp);
	convertedtemp = (uint16_t)sgp40_compute_temp_comp(sc->sc_tempcomp);

	DPRINTF(sc, 2, ("%s: Converted RH and Temp: %04x %04x\n",
	    device_xname(sc->sc_dev), convertedrh, convertedtemp));

	args[0] = convertedrh >> 8;
	args[1] = convertedrh & 0x00ff;
	args[2] = sgp40_crc(&args[0], 2);
	args[3] = convertedtemp >> 8;
	args[4] = convertedtemp & 0x00ff;
	args[5] = sgp40_crc(&args[3], 2);

	/*
	 * The VOC algorithm has a black out time when it first starts to run
	 * and does not return any indicator that is going on, so voc_index
	 * in that case would be 0..  however, that is also a valid response
	 * otherwise, although an unlikely one.
	 */
	error = iic_acquire_bus(sc->sc_tag, 0);
	if (error) {
		DPRINTF(sc, 2, ("%s: Could not acquire iic bus for take "
		    "measurement: %d\n", device_xname(sc->sc_dev), error));
		sc->sc_voc = 0;
		sc->sc_vocvalid = false;
		goto out;
	}

	error = sgp40_cmdr(sc, SGP40_MEASURE_RAW, args, 6, buf, 3);
	iic_release_bus(sc->sc_tag, 0);
	if (error) {
		DPRINTF(sc, 2, ("%s: Failed to get measurement %d\n",
		    device_xname(sc->sc_dev), error));
		goto out;
	}

	crc = sgp40_crc(&buf[0], 2);
	DPRINTF(sc, 2, ("%s: Raw ticks and crc: %02x%02x %02x "
	    "%02x\n", device_xname(sc->sc_dev), buf[0], buf[1],
	    buf[2], crc));
	if (buf[2] != crc)
		goto out;

	rawmeasurement = buf[0] << 8;
	rawmeasurement |= buf[1];
	VocAlgorithm_process(params, rawmeasurement,
	    &voc_index);
	DPRINTF(sc, 2, ("%s: VOC index: %d\n",
	    device_xname(sc->sc_dev), voc_index));
	sc->sc_voc = voc_index;
	sc->sc_vocvalid = true;

	mutex_exit(&sc->sc_mutex);
	return;
out:
	sc->sc_voc = 0;
	sc->sc_vocvalid = false;
	mutex_exit(&sc->sc_mutex);
}

int
sgp40_verify_sysctl(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;

	node = *rnode;
	t = *(int *)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (t < 0)
		return EINVAL;

	*(int *)rnode->sysctl_data = t;

	return 0;
}

int
sgp40_verify_temp_sysctl(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;

	node = *rnode;
	t = *(int *)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (t < -45 || t > 130)
		return EINVAL;

	*(int *)rnode->sysctl_data = t;

	return 0;
}

int
sgp40_verify_rh_sysctl(SYSCTLFN_ARGS)
{
	int error, t;
	struct sysctlnode node;

	node = *rnode;
	t = *(int *)rnode->sysctl_data;
	node.sysctl_data = &t;
	error = sysctl_lookup(SYSCTLFN_CALL(&node));
	if (error || newp == NULL)
		return error;

	if (t < 0 || t > 100)
		return EINVAL;

	*(int *)rnode->sysctl_data = t;

	return 0;
}

static int
sgp40_cmddelay(uint16_t cmd)
{
	int r = -1;

	for(int i = 0;i < __arraycount(sgp40_timings);i++) {
		if (cmd == sgp40_timings[i].cmd) {
			r = sgp40_timings[i].typicaldelay;
			break;
		}
	}

	if (r == -1) {
		panic("sgp40: Bad command look up in cmd delay: cmd: %d\n",
		    cmd);
	}

	return r;
}

static int
sgp40_cmd(i2c_tag_t tag, i2c_addr_t addr, uint8_t *cmd,
    uint8_t clen, uint8_t *buf, size_t blen, int readattempts)
{
	int error;
	int cmddelay;
	uint16_t cmd16;

	cmd16 = cmd[0] << 8;
	cmd16 = cmd16 | cmd[1];

	error = iic_exec(tag, I2C_OP_WRITE_WITH_STOP, addr, cmd, clen, NULL, 0,
	    0);
	if (error)
		return error;

	/* 
	 * Every command returns something except for turning the heater off
	 * and the general soft reset which returns nothing.
	 */
	if (cmd16 == SGP40_HEATER_OFF)
		return 0;
	/*
	 * Every command has a particular delay for how long
	 * it typically takes and the max time it will take.
	 */
	cmddelay = sgp40_cmddelay(cmd16);
	delay(cmddelay);

	for (int aint = 0; aint < readattempts; aint++) {
		error = iic_exec(tag, I2C_OP_READ_WITH_STOP, addr, NULL, 0,
		    buf, blen, 0);
		if (error == 0)
			break;
		delay(1000);
	}

	return error;
}

static int
sgp40_cmdr(struct sgp40_sc *sc, uint16_t cmd, uint8_t *extraargs,
    uint8_t argslen, uint8_t *buf, size_t blen)
{
	uint8_t fullcmd[8];
	uint8_t cmdlen;
	int n;

	/*
	 * The biggest documented command + arguments is 8 uint8_t bytes long.
	 * Catch anything that ties to have an arglen more than 6
	 */
	KASSERT(argslen <= 6);

	memset(fullcmd, 0, 8);

	fullcmd[0] = cmd >> 8;
	fullcmd[1] = cmd & 0x00ff;
	cmdlen = 2;

	n = 0;
	while (extraargs != NULL && n < argslen && cmdlen <= 7) {
		fullcmd[cmdlen] = extraargs[n];
		cmdlen++;
		n++;
	}
	DPRINTF(sc, 2, ("%s: Full command and arguments: %02x %02x %02x %02x "
	    "%02x %02x %02x %02x\n",
	    device_xname(sc->sc_dev), fullcmd[0], fullcmd[1],
	    fullcmd[2], fullcmd[3], fullcmd[4], fullcmd[5],
	    fullcmd[6], fullcmd[7]));
	return sgp40_cmd(sc->sc_tag, sc->sc_addr, fullcmd, cmdlen, buf, blen,
	    sc->sc_readattempts);
}

static	uint8_t
sgp40_crc(uint8_t * data, size_t size)
{
	uint8_t crc = 0xFF;

	for (size_t i = 0; i < size; i++) {
		crc ^= data[i];
		for (size_t j = 8; j > 0; j--) {
			if (crc & 0x80)
				crc = (crc << 1) ^ 0x31;
			else
				crc <<= 1;
		}
	}
	return crc;
}

static int
sgp40_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
{
	uint8_t reg[2];
	uint8_t buf[9];
	int error;

	/*
	 * Possible bug...  this command may not work if the chip is not idle,
	 * however, it appears to be used by a lot of other code as a probe.
	 */
	reg[0] = SGP40_GET_SERIAL_NUMBER >> 8;
	reg[1] = SGP40_GET_SERIAL_NUMBER & 0x00ff;

	error = sgp40_cmd(tag, addr, reg, 2, buf, 9, 10);
	if (matchdebug) {
		printf("poke X 1: %d\n", error);
	}
	return error;
}

static int
sgp40_sysctl_init(struct sgp40_sc *sc)
{
	int error;
	const struct sysctlnode *cnode;
	int sysctlroot_num;

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    0, CTLTYPE_NODE, device_xname(sc->sc_dev),
	    SYSCTL_DESCR("SGP40 controls"), NULL, 0, NULL, 0, CTL_HW,
	    CTL_CREATE, CTL_EOL)) != 0)
		return error;

	sysctlroot_num = cnode->sysctl_num;

#ifdef SGP40_DEBUG
	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
	    SYSCTL_DESCR("Debug level"), sgp40_verify_sysctl, 0,
	    &sc->sc_sgp40debug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
	    CTL_EOL)) != 0)
		return error;

#endif

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    CTLFLAG_READWRITE, CTLTYPE_INT, "readattempts",
	    SYSCTL_DESCR("The number of times to attempt to read the values"),
	    sgp40_verify_sysctl, 0, &sc->sc_readattempts, 0, CTL_HW,
	    sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
		return error;

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    CTLFLAG_READWRITE, CTLTYPE_BOOL, "ignorecrc",
	    SYSCTL_DESCR("Ignore the CRC byte"), NULL, 0, &sc->sc_ignorecrc,
	    0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
		return error;

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    0, CTLTYPE_NODE, "compensation",
	    SYSCTL_DESCR("SGP40 measurement compensations"), NULL, 0, NULL, 0,
	    CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
		return error;
	int compensation_num = cnode->sysctl_num;

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    CTLFLAG_READWRITE, CTLTYPE_INT, "temperature",
	    SYSCTL_DESCR("Temperature compensation in celsius"),
	    sgp40_verify_temp_sysctl, 0, &sc->sc_tempcomp, 0, CTL_HW,
	    sysctlroot_num, compensation_num, CTL_CREATE, CTL_EOL)) != 0)
		return error;

	if ((error = sysctl_createv(&sc->sc_sgp40log, 0, NULL, &cnode,
	    CTLFLAG_READWRITE, CTLTYPE_INT, "humidity",
	    SYSCTL_DESCR("Humidity compensation in %RH"),
	    sgp40_verify_rh_sysctl, 0, &sc->sc_rhcomp, 0, CTL_HW,
	    sysctlroot_num, compensation_num, CTL_CREATE, CTL_EOL)) != 0)
		return error;

	return 0;
}

static int
sgp40_match(device_t parent, cfdata_t match, void *aux)
{
	struct i2c_attach_args *ia = aux;
	int error, match_result;
	const bool matchdebug = false;

	if (matchdebug)
		printf("in match\n");

	if (iic_use_direct_match(ia, match, NULL, &match_result))
		return match_result;

	/* indirect config - check for configured address */
	if (ia->ia_addr != SGP40_TYPICAL_ADDR)
		return 0;

	/*
	 * Check to see if something is really at this i2c address. This will
	 * keep phantom devices from appearing
	 */
	if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
		if (matchdebug)
			printf("in match acquire bus failed\n");
		return 0;
	}

	error = sgp40_poke(ia->ia_tag, ia->ia_addr, matchdebug);
	iic_release_bus(ia->ia_tag, 0);

	return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
}

static void
sgp40_attach(device_t parent, device_t self, void *aux)
{
	struct sgp40_sc *sc;
	struct i2c_attach_args *ia;
	int error, i;
	int ecount = 0;
	uint8_t buf[9];
	uint8_t tstcrc;
	uint16_t chiptestvalue;
	uint64_t serial_number = 0;
	uint8_t sn_crc1, sn_crc2, sn_crc3, sn_crcv1, sn_crcv2, sn_crcv3;
	uint8_t fs_crc, fs_crcv;
	uint16_t featureset;

	ia = aux;
	sc = device_private(self);

	sc->sc_dev = self;
	sc->sc_tag = ia->ia_tag;
	sc->sc_addr = ia->ia_addr;
	sc->sc_sgp40debug = 0;
	sc->sc_readattempts = 10;
	sc->sc_ignorecrc = false;
	sc->sc_stopping = false;
	sc->sc_voc = 0;
	sc->sc_vocvalid = false;
	sc->sc_tempcomp = SGP40_DEFAULT_TEMP_COMP;
	sc->sc_rhcomp = SGP40_DEFAULT_RH_COMP;
	sc->sc_sme = NULL;

	aprint_normal("\n");

	mutex_init(&sc->sc_threadmutex, MUTEX_DEFAULT, IPL_NONE);
	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
	cv_init(&sc->sc_condvar, "sgp40cv");
	sc->sc_numsensors = __arraycount(sgp40_sensors);

	if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
		aprint_error_dev(self,
		    "Unable to create sysmon structure\n");
		sc->sc_sme = NULL;
		return;
	}
	if ((error = sgp40_sysctl_init(sc)) != 0) {
		aprint_error_dev(self, "Can't setup sysctl tree (%d)\n", error);
		goto out;
	}

	error = iic_acquire_bus(sc->sc_tag, 0);
	if (error) {
		aprint_error_dev(self, "Could not acquire iic bus: %d\n",
		    error);
		goto out;
	}

	/*
	 * Usually one would reset the chip here, but that is not possible
	 * without resetting the entire bus, so we won't do that.
	 *
	 * What we will do is make sure that the chip is idle by running the
	 * turn-the-heater command.
	 */

	error = sgp40_cmdr(sc, SGP40_HEATER_OFF, NULL, 0, NULL, 0);
	if (error) {
		aprint_error_dev(self, "Failed to turn off the heater: %d\n",
		    error);
		ecount++;
	}

	error = sgp40_cmdr(sc, SGP40_GET_SERIAL_NUMBER, NULL, 0, buf, 9);
	if (error) {
		aprint_error_dev(self, "Failed to get serial number: %d\n",
		    error);
		ecount++;
	}

	sn_crc1 = sgp40_crc(&buf[0], 2);
	sn_crc2 = sgp40_crc(&buf[3], 2);
	sn_crc3 = sgp40_crc(&buf[6], 2);
	sn_crcv1 = buf[2];
	sn_crcv2 = buf[5];
	sn_crcv3 = buf[8];
	serial_number = buf[0];
	serial_number = (serial_number << 8) | buf[1];
	serial_number = (serial_number << 8) | buf[3];
	serial_number = (serial_number << 8) | buf[4];
	serial_number = (serial_number << 8) | buf[6];
	serial_number = (serial_number << 8) | buf[7];

	DPRINTF(sc, 2, ("%s: raw serial number: %02x %02x %02x %02x %02x %02x "
	    "%02x %02x %02x\n",
	    device_xname(sc->sc_dev), buf[0], buf[1], buf[2], buf[3], buf[4],
	    buf[5], buf[6], buf[7], buf[8]));

	error = sgp40_cmdr(sc, SGP40_GET_FEATURESET, NULL, 0, buf, 3);
	if (error) {
		aprint_error_dev(self, "Failed to get featureset: %d\n",
		    error);
		ecount++;
	}

	fs_crc = sgp40_crc(&buf[0], 2);
	fs_crcv = buf[2];
	featureset = buf[0];
	featureset = (featureset << 8) | buf[1];

	DPRINTF(sc, 2, ("%s: raw feature set: %02x %02x %02x\n",
	    device_xname(sc->sc_dev), buf[0], buf[1], buf[2]));

	error = sgp40_cmdr(sc, SGP40_MEASURE_TEST, NULL, 0, buf, 3);
	if (error) {
		aprint_error_dev(self, "Failed to perform a chip test: %d\n",
		    error);
		ecount++;
	}

	tstcrc = sgp40_crc(&buf[0], 2);

	DPRINTF(sc, 2, ("%s: chip test values: %02x%02x - %02x ; %02x\n",
	    device_xname(sc->sc_dev), buf[0], buf[1], buf[2], tstcrc));

	iic_release_bus(sc->sc_tag, 0);
	if (error != 0) {
		aprint_error_dev(self, "Unable to setup device\n");
		goto out;
	}

	chiptestvalue = buf[0] << 8;
	chiptestvalue |= buf[1];

	for (i = 0; i < sc->sc_numsensors; i++) {
		strlcpy(sc->sc_sensors[i].desc, sgp40_sensors[i].desc,
		    sizeof(sc->sc_sensors[i].desc));

		sc->sc_sensors[i].units = sgp40_sensors[i].type;
		sc->sc_sensors[i].state = ENVSYS_SINVALID;

		DPRINTF(sc, 2, ("%s: registering sensor %d (%s)\n", __func__, i,
		    sc->sc_sensors[i].desc));

		error = sysmon_envsys_sensor_attach(sc->sc_sme,
		    &sc->sc_sensors[i]);
		if (error) {
			aprint_error_dev(self,
			    "Unable to attach sensor %d: %d\n", i, error);
			goto out;
		}
	}

	sc->sc_sme->sme_name = device_xname(sc->sc_dev);
	sc->sc_sme->sme_cookie = sc;
	sc->sc_sme->sme_refresh = sgp40_refresh;

	DPRINTF(sc, 2, ("sgp40_attach: registering with envsys\n"));

	if (sysmon_envsys_register(sc->sc_sme)) {
		aprint_error_dev(self,
			"unable to register with sysmon\n");
		sysmon_envsys_destroy(sc->sc_sme);
		sc->sc_sme = NULL;
		return;
	}

	error = kthread_create(PRI_NONE, KTHREAD_MUSTJOIN, NULL,
	    sgp40_thread, sc, &sc->sc_thread, "%s", device_xname(sc->sc_dev));
	if (error) {
		aprint_error_dev(self,"Unable to create measurement thread\n");
		goto out;
	}

	aprint_normal_dev(self, "Sensirion SGP40, Serial number: %jx%s"
	    "Feature set word: 0x%jx%s%s%s", serial_number,
	    (sn_crc1 == sn_crcv1 && sn_crc2 == sn_crcv2 && sn_crc3 == sn_crcv3)
	    ? ", " : " (bad crc), ",
	    (uintmax_t)featureset,
	    (fs_crc == fs_crcv) ? ", " : " (bad crc), ",
	    (chiptestvalue == SGP40_TEST_RESULTS_ALL_PASSED) ?
		"All chip tests passed" :
	    (chiptestvalue == SGP40_TEST_RESULTS_SOME_FAILED) ?
		"Some chip tests failed" :
	    "Unknown test results",
	    (tstcrc == buf[2]) ? "\n" : " (bad crc)\n");
	return;
out:
	sysmon_envsys_destroy(sc->sc_sme);
	sc->sc_sme = NULL;
}

static void
sgp40_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
{
	struct sgp40_sc *sc;
	sc = sme->sme_cookie;

	mutex_enter(&sc->sc_mutex);
	if (sc->sc_vocvalid == true) {
		edata->value_cur = (uint32_t)sc->sc_voc;
		edata->state = ENVSYS_SVALID;
	} else {
		edata->state = ENVSYS_SINVALID;
	}
	mutex_exit(&sc->sc_mutex);
}

static int
sgp40_detach(device_t self, int flags)
{
	struct sgp40_sc *sc;

	sc = device_private(self);

	/* stop the measurement thread */
	sgp40_stop_thread(sc);

	/* Remove the sensors */
	mutex_enter(&sc->sc_mutex);
	if (sc->sc_sme != NULL) {
		sysmon_envsys_unregister(sc->sc_sme);
		sc->sc_sme = NULL;
	}
	mutex_exit(&sc->sc_mutex);

	/* Remove the sysctl tree */
	sysctl_teardown(&sc->sc_sgp40log);

	/* Remove the mutex */
	mutex_destroy(&sc->sc_mutex);
	mutex_destroy(&sc->sc_threadmutex);

	return 0;
}

MODULE(MODULE_CLASS_DRIVER, sgp40mox, "iic,sysmon_envsys");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
sgp40mox_modcmd(modcmd_t cmd, void *opaque)
{

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		return config_init_component(cfdriver_ioconf_sgp40mox,
		    cfattach_ioconf_sgp40mox, cfdata_ioconf_sgp40mox);
#else
		return 0;
#endif
	case MODULE_CMD_FINI:
#ifdef _MODULE
		return config_fini_component(cfdriver_ioconf_sgp40mox,
		      cfattach_ioconf_sgp40mox, cfdata_ioconf_sgp40mox);
#else
		return 0;
#endif
	default:
		return ENOTTY;
	}
}