2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Ian Lepore.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Driver for Texas Instruments ADS101x and ADS111x family i2c ADC chips.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/endian.h>
40 #include <sys/kernel.h>
42 #include <sys/module.h>
44 #include <sys/sysctl.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
51 #include <dev/iicbus/iiconf.h>
52 #include <dev/iicbus/iicbus.h>
54 #include "iicbus_if.h"
57 * Chip registers, bit definitions, shifting and masking values.
59 #define ADS111x_CONV 0 /* Reg 0: Latest sample (ro) */
61 #define ADS111x_CONF 1 /* Reg 1: Config (rw) */
62 #define ADS111x_CONF_OS_SHIFT 15 /* Operational state */
63 #define ADS111x_CONF_MUX_SHIFT 12 /* Input mux setting */
64 #define ADS111x_CONF_GAIN_SHIFT 9 /* Programmable gain amp */
65 #define ADS111x_CONF_MODE_SHIFT 8 /* Operational mode */
66 #define ADS111x_CONF_RATE_SHIFT 5 /* Sample rate */
67 #define ADS111x_CONF_COMP_DISABLE 3 /* Comparator disable */
69 #define ADS111x_LOTHRESH 2 /* Compare lo threshold (rw) */
71 #define ADS111x_HITHRESH 3 /* Compare hi threshold (rw) */
74 * On config write, the operational-state bit starts a measurement, on read it
75 * indicates when the measurement process is complete/idle.
77 #define ADS111x_CONF_MEASURE (1u << ADS111x_CONF_OS_SHIFT)
78 #define ADS111x_CONF_IDLE (1u << ADS111x_CONF_OS_SHIFT)
81 * The default values for config items that are not per-channel. Mostly, this
82 * turns off the comparator on chips that have that feature, because this driver
83 * doesn't support it directly. However, the user is allowed to enable the
84 * comparator and we'll leave it alone if they do. That allows them connect the
85 * alert pin to something and use the feature without any help from this driver.
87 #define ADS111x_CONF_DEFAULT \
88 ((1 << ADS111x_CONF_MODE_SHIFT) | ADS111x_CONF_COMP_DISABLE)
89 #define ADS111x_CONF_USERMASK 0x001f
92 * Per-channel defaults. The chip only has one control register, and we load
93 * per-channel values into it every time we make a measurement on that channel.
94 * These are the default values for the control register from the datasheet, for
95 * values we maintain on a per-channel basis.
97 #define DEFAULT_GAINIDX 2
98 #define DEFAULT_RATEIDX 4
101 * Full-scale ranges for each available amplifier setting, in microvolts. The
102 * ADS1x13 chips are fixed-range, the other chips contain a programmable gain
103 * amplifier, and the full scale range is based on the amplifier setting.
105 static const u_int fixedranges[8] = {
106 2048000, 2048000, 2048000, 2048000, 2048000, 2048000, 2048000, 2048000,
108 static const u_int gainranges[8] = {
109 6144000, 4096000, 2048000, 1024000, 512000, 256000, 256000, 256000,
112 /* The highest value for the ADS101x chip is 0x7ff0, for ADS111x it's 0x7fff. */
113 #define ADS101x_RANGEDIV ((1 << 15) - 15)
114 #define ADS111x_RANGEDIV ((1 << 15) - 1)
116 /* Samples per second; varies based on chip type. */
117 static const u_int rates101x[8] = {128, 250, 490, 920, 1600, 2400, 3300, 3300};
118 static const u_int rates111x[8] = { 8, 16, 32, 64, 128, 250, 475, 860};
120 struct ads111x_channel {
121 u_int gainidx; /* Amplifier (full-scale range) config index */
122 u_int rateidx; /* Samples per second config index */
123 bool configured; /* Channel has been configured */
126 #define ADS111x_MAX_CHANNELS 8
128 struct ads111x_chipinfo {
130 const u_int *rangetab;
131 const u_int *ratetab;
136 static struct ads111x_chipinfo ads111x_chip_infos[] = {
137 { "ADS1013", fixedranges, rates101x, 1, ADS101x_RANGEDIV },
138 { "ADS1014", gainranges, rates101x, 1, ADS101x_RANGEDIV },
139 { "ADS1015", gainranges, rates101x, 8, ADS101x_RANGEDIV },
140 { "ADS1113", fixedranges, rates111x, 1, ADS111x_RANGEDIV },
141 { "ADS1114", gainranges, rates111x, 1, ADS111x_RANGEDIV },
142 { "ADS1115", gainranges, rates111x, 8, ADS111x_RANGEDIV },
146 static struct ofw_compat_data compat_data[] = {
147 {"ti,ads1013", (uintptr_t)&ads111x_chip_infos[0]},
148 {"ti,ads1014", (uintptr_t)&ads111x_chip_infos[1]},
149 {"ti,ads1015", (uintptr_t)&ads111x_chip_infos[2]},
150 {"ti,ads1113", (uintptr_t)&ads111x_chip_infos[3]},
151 {"ti,ads1114", (uintptr_t)&ads111x_chip_infos[4]},
152 {"ti,ads1115", (uintptr_t)&ads111x_chip_infos[5]},
153 {NULL, (uintptr_t)NULL},
155 IICBUS_FDT_PNP_INFO(compat_data);
158 struct ads111x_softc {
163 const struct ads111x_chipinfo
165 struct ads111x_channel
166 channels[ADS111x_MAX_CHANNELS];
170 ads111x_write_2(struct ads111x_softc *sc, int reg, int val)
173 struct iic_msg msgs[1];
176 slaveaddr = iicbus_get_addr(sc->dev);
179 be16enc(&data[1], val);
181 msgs[0].slave = slaveaddr;
182 msgs[0].flags = IIC_M_WR;
183 msgs[0].len = sizeof(data);
186 return (iicbus_transfer_excl(sc->dev, msgs, nitems(msgs), IIC_WAIT));
190 ads111x_read_2(struct ads111x_softc *sc, int reg, int *val)
195 err = iic2errno(iicdev_readfrom(sc->dev, reg, data, 2, IIC_WAIT));
197 *val = (int16_t)be16dec(data);
203 ads111x_sample_voltage(struct ads111x_softc *sc, int channum, int *voltage)
205 struct ads111x_channel *chan;
206 int err, cfgword, convword, rate, retries, waitns;
209 chan = &sc->channels[channum];
211 /* Ask the chip to do a one-shot measurement of the given channel. */
212 cfgword = sc->cfgword |
213 (1 << ADS111x_CONF_OS_SHIFT) |
214 (channum << ADS111x_CONF_MUX_SHIFT) |
215 (chan->gainidx << ADS111x_CONF_GAIN_SHIFT) |
216 (chan->rateidx << ADS111x_CONF_RATE_SHIFT);
217 if ((err = ads111x_write_2(sc, ADS111x_CONF, cfgword)) != 0)
221 * Calculate how long it will take to make the measurement at the
222 * current sampling rate (round up). The measurement averaging time
223 * ranges from 300us to 125ms, so we yield the cpu while waiting.
225 rate = sc->chipinfo->ratetab[chan->rateidx];
226 waitns = (1000000000 + rate - 1) / rate;
227 err = pause_sbt("ads111x", nstosbt(waitns), 0, C_PREL(2));
228 if (err != 0 && err != EWOULDBLOCK)
232 * In theory the measurement should be available now; we waited long
233 * enough. However, the chip times its averaging intervals using an
234 * internal 1 MHz oscillator which likely isn't running at the same rate
235 * as the system clock, so we have to double-check that the measurement
236 * is complete before reading the result. If it's not ready yet, yield
237 * the cpu again for 5% of the time we originally calculated.
239 * Unlike most i2c slaves, this device does not auto-increment the
240 * register number on reads, so we can't read both status and
241 * measurement in one operation.
243 for (retries = 5; ; --retries) {
245 return (EWOULDBLOCK);
246 if ((err = ads111x_read_2(sc, ADS111x_CONF, &cfgword)) != 0)
248 if (cfgword & ADS111x_CONF_IDLE)
250 pause_sbt("ads111x", nstosbt(waitns / 20), 0, C_PREL(2));
253 /* Retrieve the sample and convert it to microvolts. */
254 if ((err = ads111x_read_2(sc, ADS111x_CONV, &convword)) != 0)
256 fsrange = sc->chipinfo->rangetab[chan->gainidx];
257 *voltage = (int)((convword * fsrange ) / sc->chipinfo->rangediv);
263 ads111x_sysctl_gainidx(SYSCTL_HANDLER_ARGS)
265 struct ads111x_softc *sc;
266 int chan, err, gainidx;
271 gainidx = sc->channels[chan].gainidx;
272 err = sysctl_handle_int(oidp, &gainidx, 0, req);
273 if (err != 0 || req->newptr == NULL)
275 if (gainidx < 0 || gainidx > 7)
278 sc->channels[chan].gainidx = gainidx;
279 sx_xunlock(&sc->lock);
285 ads111x_sysctl_rateidx(SYSCTL_HANDLER_ARGS)
287 struct ads111x_softc *sc;
288 int chan, err, rateidx;
293 rateidx = sc->channels[chan].rateidx;
294 err = sysctl_handle_int(oidp, &rateidx, 0, req);
295 if (err != 0 || req->newptr == NULL)
297 if (rateidx < 0 || rateidx > 7)
300 sc->channels[chan].rateidx = rateidx;
301 sx_xunlock(&sc->lock);
307 ads111x_sysctl_voltage(SYSCTL_HANDLER_ARGS)
309 struct ads111x_softc *sc;
310 int chan, err, voltage;
315 if (req->oldptr != NULL) {
317 err = ads111x_sample_voltage(sc, chan, &voltage);
318 sx_xunlock(&sc->lock);
320 device_printf(sc->dev,
321 "conversion read failed, error %d\n", err);
325 err = sysctl_handle_int(oidp, &voltage, 0, req);
330 ads111x_sysctl_config(SYSCTL_HANDLER_ARGS)
332 struct ads111x_softc *sc;
336 config = sc->cfgword & ADS111x_CONF_USERMASK;
337 err = sysctl_handle_int(oidp, &config, 0, req);
338 if (err != 0 || req->newptr == NULL)
341 sc->cfgword = config & ADS111x_CONF_USERMASK;
342 err = ads111x_write_2(sc, ADS111x_CONF, sc->cfgword);
343 sx_xunlock(&sc->lock);
348 ads111x_sysctl_lothresh(SYSCTL_HANDLER_ARGS)
350 struct ads111x_softc *sc;
354 if ((err = ads111x_read_2(sc, ADS111x_LOTHRESH, &thresh)) != 0)
356 err = sysctl_handle_int(oidp, &thresh, 0, req);
357 if (err != 0 || req->newptr == NULL)
360 err = ads111x_write_2(sc, ADS111x_CONF, thresh);
361 sx_xunlock(&sc->lock);
367 ads111x_sysctl_hithresh(SYSCTL_HANDLER_ARGS)
369 struct ads111x_softc *sc;
373 if ((err = ads111x_read_2(sc, ADS111x_HITHRESH, &thresh)) != 0)
375 err = sysctl_handle_int(oidp, &thresh, 0, req);
376 if (err != 0 || req->newptr == NULL)
379 err = ads111x_write_2(sc, ADS111x_CONF, thresh);
380 sx_xunlock(&sc->lock);
386 ads111x_setup_channel(struct ads111x_softc *sc, int chan, int gainidx, int rateidx)
388 struct ads111x_channel *c;
389 struct sysctl_ctx_list *ctx;
390 struct sysctl_oid *chantree, *devtree;
393 c = &sc->channels[chan];
394 c->gainidx = gainidx;
395 c->rateidx = rateidx;
398 * If setting up the channel for the first time, create channel's
399 * sysctl entries. We might have already configured the channel if
400 * config data for it exists in both FDT and hints.
406 ctx = device_get_sysctl_ctx(sc->dev);
407 devtree = device_get_sysctl_tree(sc->dev);
408 snprintf(chanstr, sizeof(chanstr), "%d", chan);
409 chantree = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(devtree), OID_AUTO,
410 chanstr, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "channel data");
411 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
412 "gain_index", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
413 sc, chan, ads111x_sysctl_gainidx, "I",
414 "programmable gain amp setting, 0-7");
415 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
416 "rate_index", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
417 sc, chan, ads111x_sysctl_rateidx, "I", "sample rate setting, 0-7");
418 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(chantree), OID_AUTO,
420 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_NEEDGIANT, sc,
421 chan, ads111x_sysctl_voltage, "I", "sampled voltage in microvolts");
423 c->configured = true;
427 ads111x_add_channels(struct ads111x_softc *sc)
430 uint32_t chan, gainidx, num_added, rateidx, unit;
434 phandle_t child, node;
436 /* Configure any channels that have FDT data. */
438 node = ofw_bus_get_node(sc->dev);
439 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
440 if (OF_getencprop(child, "reg", &chan, sizeof(chan)) == -1)
442 if (chan >= ADS111x_MAX_CHANNELS)
444 gainidx = DEFAULT_GAINIDX;
445 rateidx = DEFAULT_RATEIDX;
446 OF_getencprop(child, "ti,gain", &gainidx, sizeof(gainidx));
447 OF_getencprop(child, "ti,datarate", &rateidx, sizeof(rateidx));
448 ads111x_setup_channel(sc, chan, gainidx, rateidx);
455 /* Configure any channels that have hint data. */
456 name = device_get_name(sc->dev);
457 unit = device_get_unit(sc->dev);
458 for (chan = 0; chan < sc->chipinfo->numchan; ++chan) {
460 gainidx = DEFAULT_GAINIDX;
461 rateidx = DEFAULT_RATEIDX;
462 if (resource_int_value(name, unit, "gain_index", &gainidx) == 0)
464 if (resource_int_value(name, unit, "rate_index", &gainidx) == 0)
467 ads111x_setup_channel(sc, chan, gainidx, rateidx);
472 /* If any channels were configured via FDT or hints, we're done. */
477 * No channel config; add all possible channels using default values,
478 * and let the user configure the ones they want on the fly via sysctl.
480 for (chan = 0; chan < sc->chipinfo->numchan; ++chan) {
481 gainidx = DEFAULT_GAINIDX;
482 rateidx = DEFAULT_RATEIDX;
483 ads111x_setup_channel(sc, chan, gainidx, rateidx);
487 static const struct ads111x_chipinfo *
488 ads111x_find_chipinfo(device_t dev)
490 const struct ads111x_chipinfo *info;
491 const char *chiptype;
495 if (ofw_bus_status_okay(dev)) {
496 info = (struct ads111x_chipinfo*)
497 ofw_bus_search_compatible(dev, compat_data)->ocd_data;
503 /* For hinted devices, we must be told the chip type. */
505 resource_string_value(device_get_name(dev), device_get_unit(dev),
507 if (chiptype != NULL) {
508 for (i = 0; i < nitems(ads111x_chip_infos); ++i) {
509 info = &ads111x_chip_infos[i];
510 if (strcasecmp(chiptype, info->name) == 0)
518 ads111x_probe(device_t dev)
520 const struct ads111x_chipinfo *info;
522 info = ads111x_find_chipinfo(dev);
524 device_set_desc(dev, info->name);
525 return (BUS_PROBE_DEFAULT);
532 ads111x_attach(device_t dev)
534 struct ads111x_softc *sc;
535 struct sysctl_ctx_list *ctx;
536 struct sysctl_oid *tree;
539 sc = device_get_softc(dev);
541 sc->addr = iicbus_get_addr(dev);
542 sc->cfgword = ADS111x_CONF_DEFAULT;
544 sc->chipinfo = ads111x_find_chipinfo(sc->dev);
545 if (sc->chipinfo == NULL) {
547 "cannot get chipinfo (but it worked during probe)");
551 /* Set the default chip config. */
552 if ((err = ads111x_write_2(sc, ADS111x_CONF, sc->cfgword)) != 0) {
553 device_printf(dev, "cannot write chip config register\n");
557 /* Add the sysctl handler to set the chip configuration register. */
558 ctx = device_get_sysctl_ctx(dev);
559 tree = device_get_sysctl_tree(dev);
560 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
561 "config", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
562 ads111x_sysctl_config, "I", "configuration register word");
563 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
564 "lo_thresh", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
565 ads111x_sysctl_lothresh, "I", "comparator low threshold");
566 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
567 "hi_thresh", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, sc, 0,
568 ads111x_sysctl_hithresh, "I", "comparator high threshold");
570 /* Set up channels based on metadata or default config. */
571 ads111x_add_channels(sc);
573 sx_init(&sc->lock, "ads111x");
579 ads111x_detach(device_t dev)
581 struct ads111x_softc *sc;
583 sc = device_get_softc(dev);
585 sx_destroy(&sc->lock);
589 static device_method_t ads111x_methods[] = {
590 DEVMETHOD(device_probe, ads111x_probe),
591 DEVMETHOD(device_attach, ads111x_attach),
592 DEVMETHOD(device_detach, ads111x_detach),
597 static driver_t ads111x_driver = {
600 sizeof(struct ads111x_softc),
602 static devclass_t ads111x_devclass;
604 DRIVER_MODULE(ads111x, iicbus, ads111x_driver, ads111x_devclass, NULL, NULL);
605 MODULE_VERSION(ads111x, 1);
606 MODULE_DEPEND(ads111x, iicbus, 1, 1, 1);