aboutsummaryrefslogblamecommitdiff
path: root/sys/dev/iicbus/htu21.c
blob: 5c59ebe8f54a5584594a8eca72018e104d1cb3ea (plain) (tree)
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








































































































































































































































































































































































































































































































































                                                                                
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2021 Andriy Gapon
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include "opt_platform.h"

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/sysctl.h>
#include <sys/systm.h>

#include <machine/bus.h>

#include <dev/iicbus/iicbus.h>
#include <dev/iicbus/iiconf.h>

#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif

/*
 * Driver for HTU21D and compatible temperature and humidity sensors.
 * Reference documents:
 * - Measurement Specialties HTU21D datasheet,
 * - Sensirion SHT21 datasheet,
 * - Silicon Labs Si7021 datasheet,
 * - HTU2X Serial Number Reading application note,
 * - Sensirion Electronic Identification Code (How to read-out the serial number
 *   of SHT2x) application note.
 */
#define	HTU21_ADDR		0x40

#define	HTU21_GET_TEMP		0xe3
#define	HTU21_GET_HUM		0xe5
#define	HTU21_GET_TEMP_NH	0xf3
#define	HTU21_GET_HUM_NH	0xf5
#define	HTU21_WRITE_CFG		0xe6
#define	HTU21_READ_CFG		0xe7
#define	HTU21_RESET		0xfe

#define	HTU2x_SERIAL0_0		0xfa
#define	HTU2x_SERIAL0_1		0x0f
#define	HTU2x_SERIAL1_0		0xfc
#define	HTU2x_SERIAL1_1		0xc9

struct htu21_softc {
	device_t		sc_dev;
	uint32_t		sc_addr;
	uint8_t			sc_serial[8];
	int			sc_errcount;
	bool			sc_hold;
};

#ifdef FDT
static struct ofw_compat_data compat_data[] = {
	{ "meas,htu21",		true },
	{ NULL,			false }
};
#endif

static uint8_t
calc_crc(uint16_t data)
{
	static const uint16_t polynomial = 0x3100;
	int i;

	for (i = 0; i < 16; i++) {
		int msb_neq = data & 0x8000;

		data <<= 1;
		if (msb_neq)
			data ^= polynomial;
	}
	return (data >> 8);
}

static int
check_crc_16(const uint8_t *data, uint8_t expected)
{
	uint8_t crc;

	crc = calc_crc(((uint16_t)data[0] << 8) | data[1]);
	return (crc == expected);
}

static int
check_crc_8(const uint8_t data, uint8_t expected)
{
	uint8_t crc;

	crc = calc_crc(data);
	return (crc == expected);
}

static int
htu21_get_measurement(device_t dev, uint8_t cmd, uint8_t *data, int count)
{

	struct iic_msg msgs[2];
	struct htu21_softc *sc;
	int error;

	sc = device_get_softc(dev);
	msgs[0].slave = sc->sc_addr;
	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
	msgs[0].len = 1;
	msgs[0].buf = &cmd;

	msgs[1].slave = sc->sc_addr;
	msgs[1].flags = IIC_M_RD;
	msgs[1].len = count;
	msgs[1].buf = data;

	error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
	return (error);
}

static int
htu21_get_measurement_nohold(device_t dev, uint8_t cmd,
    uint8_t *data, int count)
{
	struct iic_msg msgs[2];
	struct htu21_softc *sc;
	int error;
	int i;

	sc = device_get_softc(dev);

	msgs[0].slave = sc->sc_addr;
	msgs[0].flags = IIC_M_WR;
	msgs[0].len = 1;
	msgs[0].buf = &cmd;

	msgs[1].slave = sc->sc_addr;
	msgs[1].flags = IIC_M_RD;
	msgs[1].len = count;
	msgs[1].buf = data;

	error = iicbus_transfer_excl(dev, &msgs[0], 1, IIC_INTRWAIT);
	if (error != 0)
		return (error);

	for (i = 0; i < hz; i++) {
		error = iicbus_transfer_excl(dev, &msgs[1], 1, IIC_INTRWAIT);
		if (error == 0)
			return (0);
		if (error != IIC_ENOACK)
			break;
		pause("htu21", 1);
	}
	return (error);
}

static int
htu21_temp_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct htu21_softc *sc;
	device_t dev;
	uint8_t raw_data[3];
	int error, temp;

	dev = arg1;
	sc = device_get_softc(dev);

	if (sc->sc_hold)
		error = htu21_get_measurement(dev, HTU21_GET_TEMP,
		    raw_data, nitems(raw_data));
	else
		error = htu21_get_measurement_nohold(dev, HTU21_GET_TEMP_NH,
		    raw_data, nitems(raw_data));

	if (error != 0) {
		return (EIO);
	} else if (!check_crc_16(raw_data, raw_data[2])) {
		temp = -1;
		sc->sc_errcount++;
	} else {
		temp = (((uint16_t)raw_data[0]) << 8) | (raw_data[1] & 0xfc);
		temp = ((temp * 17572) >> 16 ) + 27315 - 4685;
	}

	error = sysctl_handle_int(oidp, &temp, 0, req);
	return (error);
}

static int
htu21_rh_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct htu21_softc *sc;
	device_t dev;
	uint8_t raw_data[3];
	int error, rh;

	dev = arg1;
	sc = device_get_softc(dev);

	if (sc->sc_hold)
		error = htu21_get_measurement(dev, HTU21_GET_HUM,
		    raw_data, nitems(raw_data));
	else
		error = htu21_get_measurement_nohold(dev, HTU21_GET_HUM_NH,
		    raw_data, nitems(raw_data));

	if (error != 0) {
		return (EIO);
	} else if (!check_crc_16(raw_data, raw_data[2])) {
		rh = -1;
		sc->sc_errcount++;
	} else {
		rh = (((uint16_t)raw_data[0]) << 8) | (raw_data[1] & 0xfc);
		rh = ((rh * 12500) >> 16 ) - 600;
	}

	error = sysctl_handle_int(oidp, &rh, 0, req);
	return (error);
}

static int
htu21_get_cfg(device_t dev, uint8_t *cfg)
{

	struct iic_msg msgs[2];
	struct htu21_softc *sc;
	uint8_t cmd;
	int error;

	sc = device_get_softc(dev);
	cmd = HTU21_READ_CFG;
	msgs[0].slave = sc->sc_addr;
	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
	msgs[0].len = 1;
	msgs[0].buf = &cmd;

	msgs[1].slave = sc->sc_addr;
	msgs[1].flags = IIC_M_RD;
	msgs[1].len = 1;
	msgs[1].buf = cfg;

	error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
	return (error);
}

static int
htu21_set_cfg(device_t dev, uint8_t cfg)
{

	struct iic_msg msg;
	struct htu21_softc *sc;
	uint8_t buf[2];
	int error;

	sc = device_get_softc(dev);
	buf[0] = HTU21_WRITE_CFG;
	buf[1] = cfg;
	msg.slave = sc->sc_addr;
	msg.flags = IIC_M_WR;
	msg.len = 2;
	msg.buf = buf;

	error = iicbus_transfer_excl(dev, &msg, 1, IIC_INTRWAIT);
	return (error);
}

static int
htu21_heater_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct htu21_softc *sc;
	device_t dev;
	uint8_t cfg;
	int error, heater;

	dev = arg1;
	sc = device_get_softc(dev);

	error = htu21_get_cfg(dev, &cfg);
	if (error != 0)
		return (EIO);

	heater = (cfg & 0x04) != 0;
	error = sysctl_handle_int(oidp, &heater, 0, req);
	if (error != 0 || req->newptr == NULL)
		return (error);

	cfg &= ~0x04;
	cfg |= (heater > 0) << 2;
	error = htu21_set_cfg(dev, cfg);
	return (error != 0 ? EIO : 0);
}

static int
htu21_power_sysctl(SYSCTL_HANDLER_ARGS)
{
	struct htu21_softc *sc;
	device_t dev;
	uint8_t cfg;
	int error, power;

	dev = arg1;
	sc = device_get_softc(dev);

	error = htu21_get_cfg(dev, &cfg);
	if (error != 0)
		return (EIO);

	power = (cfg & 0x40) == 0;
	error = sysctl_handle_int(oidp, &power, 0, req);
	return (error);
}

/*
 * May be incompatible with some chips like SHT21 and Si7021.
 */
static int
htu21_get_serial(device_t dev)
{

	struct iic_msg msgs[2];
	struct htu21_softc *sc;
	uint8_t data[8];
	uint8_t cmd[2];
	int error, cksum_err;
	int i;

	sc = device_get_softc(dev);
	cmd[0] = HTU2x_SERIAL0_0;
	cmd[1] = HTU2x_SERIAL0_1;
	msgs[0].slave = sc->sc_addr;
	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
	msgs[0].len = nitems(cmd);
	msgs[0].buf = cmd;

	msgs[1].slave = sc->sc_addr;
	msgs[1].flags = IIC_M_RD;
	msgs[1].len = nitems(data);
	msgs[1].buf = data;

	error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
	if (error != 0)
		return (EIO);

	cksum_err = 0;
	for (i = 0; i < nitems(data); i += 2) {
		if (!check_crc_8(data[i], data[i + 1]))
			cksum_err = EINVAL;
		sc->sc_serial[2 + i / 2] = data[i];
	}

	cmd[0] = HTU2x_SERIAL1_0;
	cmd[1] = HTU2x_SERIAL1_1;
	msgs[0].slave = sc->sc_addr;
	msgs[0].flags = IIC_M_WR | IIC_M_NOSTOP;
	msgs[0].len = nitems(cmd);
	msgs[0].buf = cmd;

	msgs[1].slave = sc->sc_addr;
	msgs[1].flags = IIC_M_RD;
	msgs[1].len = 6;
	msgs[1].buf = data;

	error = iicbus_transfer_excl(dev, msgs, nitems(msgs), IIC_INTRWAIT);
	if (error != 0)
		return (EIO);

	if (!check_crc_16(&data[0], data[2]))
		cksum_err = EINVAL;
	sc->sc_serial[6] = data[0];
	sc->sc_serial[7] = data[1];

	if (!check_crc_16(&data[3], data[5]))
		cksum_err = EINVAL;
	sc->sc_serial[0] = data[3];
	sc->sc_serial[1] = data[4];

	return (cksum_err);
}

static void
htu21_start(void *arg)
{
	device_t dev;
	struct htu21_softc *sc;
	struct sysctl_ctx_list *ctx;
	struct sysctl_oid *tree_node;
	struct sysctl_oid_list *tree;
	int error;

	sc = arg;
	dev = sc->sc_dev;

	for (int i = 0; i < 5; i++) {
		error = htu21_get_serial(dev);
		if (error == 0)
			break;
	}
	if (error != EIO) {
		device_printf(dev, "serial number: %8D (checksum %scorrect)\n",
		    sc->sc_serial, ":", error == 0 ? "" : "in");
	} else {
		device_printf(dev, "failed to get serial number, err = %d\n",
		    error);
	}

	ctx = device_get_sysctl_ctx(dev);
	tree_node = device_get_sysctl_tree(dev);
	tree = SYSCTL_CHILDREN(tree_node);

	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "temperature",
	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
	    htu21_temp_sysctl, "IK2", "Current temperature");
	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "humidity",
	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
	    htu21_rh_sysctl, "I", "Relative humidity in 0.01%% units");
	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "heater",
	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, 0,
	    htu21_heater_sysctl, "IU", "Enable built-in heater");
	SYSCTL_ADD_PROC(ctx, tree, OID_AUTO, "power",
	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, dev, 0,
	    htu21_power_sysctl, "IU", "If sensor's power is good");
	SYSCTL_ADD_BOOL(ctx, tree, OID_AUTO, "hold_bus",
	    CTLFLAG_RW, &sc->sc_hold, 0,
	    "Whether device should hold I2C bus while measuring");
	SYSCTL_ADD_INT(ctx, tree, OID_AUTO, "crc_errors",
	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, &sc->sc_errcount, 0,
	    "Number of checksum errors");
}

static int
htu21_probe(device_t dev)
{
	uint8_t addr;

#ifdef FDT
	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
		return (ENXIO);
#endif
	addr = iicbus_get_addr(dev);
	if (addr != (HTU21_ADDR << 1)) {
		device_printf(dev, "non-standard slave address 0x%02x\n",
		    addr >> 1);
	}

	device_set_desc(dev, "HTU21 temperature and humidity sensor");
	return (BUS_PROBE_GENERIC);
}

static int
htu21_attach(device_t dev)
{
	struct htu21_softc *sc;

	sc = device_get_softc(dev);
	sc->sc_dev = dev;
	sc->sc_addr = iicbus_get_addr(dev);

	/*
	 * We have to wait until interrupts are enabled.  Usually I2C read
	 * and write only works when the interrupts are available.
	 */
	config_intrhook_oneshot(htu21_start, sc);
	return (0);
}

static int
htu21_detach(device_t dev)
{
	return (0);
}

static device_method_t  htu21_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		htu21_probe),
	DEVMETHOD(device_attach,	htu21_attach),
	DEVMETHOD(device_detach,	htu21_detach),

	DEVMETHOD_END
};

static driver_t htu21_driver = {
	"htu21",
	htu21_methods,
	sizeof(struct htu21_softc)
};

static devclass_t htu21_devclass;

DRIVER_MODULE(htu21, iicbus, htu21_driver, htu21_devclass, 0, 0);
MODULE_DEPEND(htu21, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER);
MODULE_VERSION(htu21, 1);
#ifdef FDT
IICBUS_FDT_PNP_INFO(compat_data);
#endif