aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/iicbus/ads111x.c
blob: 0464de1800c056fb6a67ca557f2a512b957b1779 (plain) (blame)
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
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2019 Ian Lepore.
 *
 * 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 ``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 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.
 */

/*
 * Driver for Texas Instruments ADS101x and ADS111x family i2c ADC chips.
 */

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

#include "opt_platform.h"

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

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

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

#include "iicbus_if.h"

/*
 * Chip registers, bit definitions, shifting and masking values.
 */
#define	ADS111x_CONV			0	/* Reg 0: Latest sample (ro) */

#define	ADS111x_CONF			1	/* Reg 1: Config (rw) */
#define	  ADS111x_CONF_OS_SHIFT		15	/* Operational state */
#define	  ADS111x_CONF_MUX_SHIFT	12	/* Input mux setting */
#define	  ADS111x_CONF_GAIN_SHIFT	 9	/* Programmable gain amp */
#define	  ADS111x_CONF_MODE_SHIFT	 8	/* Operational mode */
#define	  ADS111x_CONF_RATE_SHIFT	 5	/* Sample rate */
#define	  ADS111x_CONF_COMP_DISABLE	 3	/* Comparator disable */

#define	ADS111x_LOTHRESH		2	/* Compare lo threshold (rw) */

#define	ADS111x_HITHRESH		3	/* Compare hi threshold (rw) */

/*
 * On config write, the operational-state bit starts a measurement, on read it
 * indicates when the measurement process is complete/idle.
 */
#define	  ADS111x_CONF_MEASURE		(1u << ADS111x_CONF_OS_SHIFT)
#define	  ADS111x_CONF_IDLE		(1u << ADS111x_CONF_OS_SHIFT)

/*
 * The default values for config items that are not per-channel.  Mostly, this
 * turns off the comparator on chips that have that feature, because this driver
 * doesn't support it directly.  However, the user is allowed to enable the
 * comparator and we'll leave it alone if they do.  That allows them connect the
 * alert pin to something and use the feature without any help from this driver.
 */
#define	ADS111x_CONF_DEFAULT    \
    ((1 << ADS111x_CONF_MODE_SHIFT) | ADS111x_CONF_COMP_DISABLE)
#define	ADS111x_CONF_USERMASK   0x001f

/*
 * Per-channel defaults.  The chip only has one control register, and we load
 * per-channel values into it every time we make a measurement on that channel.
 * These are the default values for the control register from the datasheet, for
 * values we maintain on a per-channel basis.
 */
#define	DEFAULT_GAINIDX		2
#define	DEFAULT_RATEIDX		4

/*
 * Full-scale ranges for each available amplifier setting, in microvolts.  The
 * ADS1x13 chips are fixed-range, the other chips contain a programmable gain
 * amplifier, and the full scale range is based on the amplifier setting.
 */
static const u_int fixedranges[8] = {
	2048000, 2048000, 2048000, 2048000, 2048000, 2048000, 2048000, 2048000,
};
static const u_int gainranges[8] = {
	6144000, 4096000, 2048000, 1024000,  512000,  256000,  256000,  256000,
};

/* The highest value for the ADS101x chip is 0x7ff0, for ADS111x it's 0x7fff. */
#define	ADS101x_RANGEDIV	((1 << 15) - 15)
#define	ADS111x_RANGEDIV	((1 << 15) - 1)

/* Samples per second; varies based on chip type. */
static const u_int rates101x[8] = {128, 250, 490, 920, 1600, 2400, 3300, 3300};
static const u_int rates111x[8] = {  8,  16,  32,  64,  128,  250,  475,  860};

struct ads111x_channel {
	u_int gainidx;		/* Amplifier (full-scale range) config index */
	u_int rateidx;		/* Samples per second config index */
	bool  configured;	/* Channel has been configured */
};

#define	ADS111x_MAX_CHANNELS	8

struct ads111x_chipinfo {
	const char	*name;
	const u_int	*rangetab;
	const u_int	*ratetab;
	u_int		numchan;
	int		rangediv;
};

static struct ads111x_chipinfo ads111x_chip_infos[] = {
	{ "ADS1013", fixedranges, rates101x, 1, ADS101x_RANGEDIV },
	{ "ADS1014", gainranges,  rates101x, 1, ADS101x_RANGEDIV },
	{ "ADS1015", gainranges,  rates101x, 8, ADS101x_RANGEDIV },
	{ "ADS1113", fixedranges, rates111x, 1, ADS111x_RANGEDIV },
	{ "ADS1114", gainranges,  rates111x, 1, ADS111x_RANGEDIV },
	{ "ADS1115", gainranges,  rates111x, 8, ADS111x_RANGEDIV },
};

#ifdef FDT
static struct ofw_compat_data compat_data[] = {
	{"ti,ads1013",   (uintptr_t)&ads111x_chip_infos[0]},
	{"ti,ads1014",   (uintptr_t)&ads111x_chip_infos[1]},
	{"ti,ads1015",   (uintptr_t)&ads111x_chip_infos[2]},
	{"ti,ads1113",   (uintptr_t)&ads111x_chip_infos[3]},
	{"ti,ads1114",   (uintptr_t)&ads111x_chip_infos[4]},
	{"ti,ads1115",   (uintptr_t)&ads111x_chip_infos[5]},
	{NULL,           (uintptr_t)NULL},
};
#endif

struct ads111x_softc {
	device_t	dev;
	struct sx	lock;
	int		addr;
	int		cfgword;
	const struct ads111x_chipinfo
			*chipinfo;
	struct ads111x_channel
			channels[ADS111x_MAX_CHANNELS];
};

static int
ads111x_write_2(struct ads111x_softc *sc, int reg, int val) 
{
	uint8_t data[3];
	struct iic_msg msgs[1];
	uint8_t slaveaddr;

	slaveaddr = iicbus_get_addr(sc->dev);

	data[0] = reg;
	be16enc(&data[1], val);

	msgs[0].slave = slaveaddr;
	msgs[0].flags = IIC_M_WR;
	msgs[0].len   = sizeof(data);
	msgs[0].buf   = data;

	return (iicbus_transfer_excl(sc->dev, msgs, nitems(msgs), IIC_WAIT));
}

static int
ads111x_read_2(struct ads111x_softc *sc, int reg, int *val) 
{
	int err;
	uint8_t data[2];

	err = iic2errno(iicdev_readfrom(sc->dev, reg, data, 2, IIC_WAIT));
	if (err == 0)
		*val = (int16_t)be16dec(data);

	return (err);
}

static int
ads111x_sample_voltage(struct ads111x_softc *sc, int channum, int *voltage) 
{
	struct ads111x_channel *chan;
	int err, cfgword, convword, rate, retries, waitns;
	int64_t fsrange;

	chan = &sc->channels[channum];

	/* Ask the chip to do a one-shot measurement of the given channel. */
	cfgword = sc->cfgword |
	    (1 << ADS111x_CONF_OS_SHIFT) |
	    (channum << ADS111x_CONF_MUX_SHIFT) |
	    (chan->gainidx << ADS111x_CONF_GAIN_SHIFT) |
	    (chan->rateidx << ADS111x_CONF_RATE_SHIFT);
	if ((err = ads111x_write_2(sc, ADS111x_CONF, cfgword)) != 0)
		return (err);

	/*
	 * Calculate how long it will take to make the measurement at the
	 * current sampling rate (round up).  The measurement averaging time
	 * ranges from 300us to 125ms, so we yield the cpu while waiting.
	 */
	rate = sc->chipinfo->ratetab[chan->rateidx];
	waitns = (1000000000 + rate - 1) / rate;
	err = pause_sbt("ads111x", nstosbt(waitns), 0, C_PREL(2));
	if (err != 0 && err != EWOULDBLOCK)
		return (err);

	/*
	 * In theory the measurement should be available now; we waited long
	 * enough.  However, the chip times its averaging intervals using an
	 * internal 1 MHz oscillator which likely isn't running at the same rate
	 * as the system clock, so we have to double-check that the measurement
	 * is complete before reading the result.  If it's not ready yet, yield
	 * the cpu again for 5% of the time we originally calculated.
	 *
	 * Unlike most i2c slaves, this device does not auto-increment the
	 * register number on reads, so we can't read both status and
	 * measurement in one operation.
	 */
	for (retries = 5; ; --retries) {
		if (retries == 0)
			return (EWOULDBLOCK);
		if ((err = ads111x_read_2(sc, ADS111x_CONF, &cfgword)) != 0)
			return (err);
		if (cfgword & ADS111x_CONF_IDLE)
			break;
		pause_sbt("ads111x", nstosbt(waitns / 20), 0, C_PREL(2));
	}

	/* Retrieve the sample and convert it to microvolts. */
	if ((err = ads111x_read_2(sc, ADS111x_CONV, &convword)) != 0)
		return (err);
	fsrange = sc->chipinfo->rangetab[chan->gainidx];
	*voltage = (int)((convword * fsrange ) / sc->chipinfo->rangediv);

	return (err);
}

static int
ads111x_sysctl_gainidx(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int chan, err, gainidx;

	sc = arg1;
	chan = arg2;

	gainidx = sc->channels[chan].gainidx;
	err = sysctl_handle_int(oidp, &gainidx, 0, req);
	if (err != 0 || req->newptr == NULL)
		return (err);
	if (gainidx < 0 || gainidx > 7)
		return (EINVAL);
	sx_xlock(&sc->lock);
	sc->channels[chan].gainidx = gainidx;
	sx_xunlock(&sc->lock);

	return (err);
}

static int
ads111x_sysctl_rateidx(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int chan, err, rateidx;

	sc = arg1;
	chan = arg2;

	rateidx = sc->channels[chan].rateidx;
	err = sysctl_handle_int(oidp, &rateidx, 0, req);
	if (err != 0 || req->newptr == NULL)
		return (err);
	if (rateidx < 0 || rateidx > 7)
		return (EINVAL);
	sx_xlock(&sc->lock);
	sc->channels[chan].rateidx = rateidx;
	sx_xunlock(&sc->lock);

	return (err);
}

static int
ads111x_sysctl_voltage(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int chan, err, voltage;

	sc = arg1;
	chan = arg2;

	if (req->oldptr != NULL) {
		sx_xlock(&sc->lock);
		err = ads111x_sample_voltage(sc, chan, &voltage);
		sx_xunlock(&sc->lock);
		if (err != 0) {
			device_printf(sc->dev,
			    "conversion read failed, error %d\n", err);
			return (err);
		}
	}
	err = sysctl_handle_int(oidp, &voltage, 0, req);
	return (err);
}

static int
ads111x_sysctl_config(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int config, err;

	sc = arg1;
	config = sc->cfgword & ADS111x_CONF_USERMASK;
	err = sysctl_handle_int(oidp, &config, 0, req);
	if (err != 0 || req->newptr == NULL)
		return (err);
	sx_xlock(&sc->lock);
	sc->cfgword = config & ADS111x_CONF_USERMASK;
	err = ads111x_write_2(sc, ADS111x_CONF, sc->cfgword);
	sx_xunlock(&sc->lock);

	return (err);
}
static int
ads111x_sysctl_lothresh(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int thresh, err;

	sc = arg1;
	if ((err = ads111x_read_2(sc, ADS111x_LOTHRESH, &thresh)) != 0)
		return (err);
	err = sysctl_handle_int(oidp, &thresh, 0, req);
	if (err != 0 || req->newptr == NULL)
		return (err);
	sx_xlock(&sc->lock);
	err = ads111x_write_2(sc, ADS111x_CONF, thresh);
	sx_xunlock(&sc->lock);

	return (err);
}

static int
ads111x_sysctl_hithresh(SYSCTL_HANDLER_ARGS)
{
	struct ads111x_softc *sc;
	int thresh, err;

	sc = arg1;
	if ((err = ads111x_read_2(sc, ADS111x_HITHRESH, &thresh)) != 0)
		return (err);
	err = sysctl_handle_int(oidp, &thresh, 0, req);
	if (err != 0 || req->newptr == NULL)
		return (err);
	sx_xlock(&sc->lock);
	err = ads111x_write_2(sc, ADS111x_CONF, thresh);
	sx_xunlock(&sc->lock);

	return (err);
}

static void
ads111x_setup_channel(struct ads111x_softc *sc, int chan, int gainidx, int rateidx)
{
	struct ads111x_channel *c;
	struct sysctl_ctx_list *ctx;
	struct sysctl_oid *chantree, *devtree;
	char chanstr[4];

	c = &sc->channels[chan];
	c->gainidx = gainidx;
	c->rateidx = rateidx;

	/*
	 *  If setting up the channel for the first time, create channel's
	 *  sysctl entries.  We might have already configured the channel if
	 *  config data for it exists in both FDT and hints.
	 */

	if (c->configured)
		return;

	ctx = device_get_sysctl_ctx(sc->dev);
	devtree = device_get_sysctl_tree(sc->dev);
	snprintf(chanstr, sizeof(chanstr), "%d", chan);
	chantree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(devtree), OID_AUTO,
	    chanstr, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "channel data");
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
	    "gain_index", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
	    sc, chan, ads111x_sysctl_gainidx, "I",
	    "programmable gain amp setting, 0-7");
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
	    "rate_index", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
	    sc, chan, ads111x_sysctl_rateidx, "I", "sample rate setting, 0-7");
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
	    "voltage",
	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_NEEDGIANT, sc,
	    chan, ads111x_sysctl_voltage, "I", "sampled voltage in microvolts");

	c->configured = true;
}

static void
ads111x_add_channels(struct ads111x_softc *sc)
{
	const char *name;
	uint32_t chan, gainidx, num_added, rateidx, unit;
	bool found;

#ifdef FDT
	phandle_t child, node;

	/* Configure any channels that have FDT data. */
	num_added = 0;
	node = ofw_bus_get_node(sc->dev);
	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
		if (OF_getencprop(child, "reg", &chan, sizeof(chan)) == -1)
			continue;
		if (chan >= ADS111x_MAX_CHANNELS)
			continue;
		gainidx = DEFAULT_GAINIDX;
		rateidx = DEFAULT_RATEIDX;
		OF_getencprop(child, "ti,gain", &gainidx, sizeof(gainidx));
		OF_getencprop(child, "ti,datarate", &rateidx, sizeof(rateidx));
		ads111x_setup_channel(sc, chan, gainidx, rateidx);
		++num_added;
	}
#else
	num_added = 0;
#endif

	/* Configure any channels that have hint data. */
	name = device_get_name(sc->dev);
	unit = device_get_unit(sc->dev);
	for (chan = 0; chan < sc->chipinfo->numchan; ++chan) {
		char resname[16];
		found = false;
		gainidx = DEFAULT_GAINIDX;
		rateidx = DEFAULT_RATEIDX;
		snprintf(resname, sizeof(resname), "%d.gain_index", chan);
		if (resource_int_value(name, unit, resname, &gainidx) == 0)
			found = true;
		snprintf(resname, sizeof(resname), "%d.rate_index", chan);
		if (resource_int_value(name, unit, resname, &rateidx) == 0)
			found = true;
		if (found) {
			ads111x_setup_channel(sc, chan, gainidx, rateidx);
			++num_added;
		}
	}

	/* If any channels were configured via FDT or hints, we're done. */
	if (num_added > 0)
		return;

	/*
	 * No channel config; add all possible channels using default values,
	 * and let the user configure the ones they want on the fly via sysctl.
	 */
	for (chan = 0; chan < sc->chipinfo->numchan; ++chan) {
		gainidx = DEFAULT_GAINIDX;
		rateidx = DEFAULT_RATEIDX;
		ads111x_setup_channel(sc, chan, gainidx, rateidx);
	}
}

static const struct ads111x_chipinfo *
ads111x_find_chipinfo(device_t dev)
{
	const struct ads111x_chipinfo *info;
	const char *chiptype;
	int i;

#ifdef FDT
	if (ofw_bus_status_okay(dev)) {
		info = (struct ads111x_chipinfo*)
		    ofw_bus_search_compatible(dev, compat_data)->ocd_data;
		if (info != NULL)
			return (info);
	}
#endif

	/* For hinted devices, we must be told the chip type. */
	chiptype = NULL;
	resource_string_value(device_get_name(dev), device_get_unit(dev),
	    "type", &chiptype);
	if (chiptype != NULL) {
		for (i = 0; i < nitems(ads111x_chip_infos); ++i) {
			info = &ads111x_chip_infos[i];
			if (strcasecmp(chiptype, info->name) == 0)
				return (info);
		}
	}
	return (NULL);
}

static int
ads111x_probe(device_t dev)
{
	const struct ads111x_chipinfo *info;

	info = ads111x_find_chipinfo(dev);
	if (info != NULL) {
		device_set_desc(dev, info->name);
#ifdef FDT
		return (BUS_PROBE_DEFAULT);
#else
		return (BUS_PROBE_NOWILDCARD);
#endif
	}

	return (ENXIO);
}

static int
ads111x_attach(device_t dev)
{
	struct ads111x_softc *sc;
	struct sysctl_ctx_list *ctx;
	struct sysctl_oid *tree;
	int err;

	sc = device_get_softc(dev);
	sc->dev = dev;
	sc->addr = iicbus_get_addr(dev);
	sc->cfgword = ADS111x_CONF_DEFAULT;

	sc->chipinfo = ads111x_find_chipinfo(sc->dev);
	if (sc->chipinfo == NULL) {
		device_printf(dev,
		    "cannot get chipinfo (but it worked during probe)");
		return (ENXIO);
	}

	/* Set the default chip config. */
	if ((err = ads111x_write_2(sc, ADS111x_CONF, sc->cfgword)) != 0) {
		device_printf(dev, "cannot write chip config register\n");
		return (err);
	}

	/* Add the sysctl handler to set the chip configuration register.  */
	ctx = device_get_sysctl_ctx(dev);
	tree = device_get_sysctl_tree(dev);
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "config", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
	    ads111x_sysctl_config, "I", "configuration register word");
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "lo_thresh", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
	    ads111x_sysctl_lothresh, "I", "comparator low threshold");
	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
	    "hi_thresh", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
	    ads111x_sysctl_hithresh, "I", "comparator high threshold");

	/* Set up channels based on metadata or default config. */
	ads111x_add_channels(sc);

	sx_init(&sc->lock, "ads111x");

	return (0);
}

static int
ads111x_detach(device_t dev)
{
	struct ads111x_softc *sc;

	sc = device_get_softc(dev);

	sx_destroy(&sc->lock);
	return (0);
}

static device_method_t ads111x_methods[] = {
	DEVMETHOD(device_probe,		ads111x_probe),
	DEVMETHOD(device_attach,	ads111x_attach),
	DEVMETHOD(device_detach,	ads111x_detach),

	DEVMETHOD_END,
};

static driver_t ads111x_driver = {
	"ads111x",
	ads111x_methods,
	sizeof(struct ads111x_softc),
};
static devclass_t ads111x_devclass;

DRIVER_MODULE(ads111x, iicbus, ads111x_driver, ads111x_devclass, NULL, NULL);
#ifdef FDT
IICBUS_FDT_PNP_INFO(compat_data);
#endif
MODULE_VERSION(ads111x, 1);
MODULE_DEPEND(ads111x, iicbus, 1, 1, 1);