xref: /freebsd/sys/dev/gpio/gpioths.c (revision 1398c4c58a7270c1a3674f624f52660a37bffd42)
1662e30fcSMichael Zhilin /*-
2662e30fcSMichael Zhilin  * Copyright (c) 2016 Michael Zhilin <mizhka@freebsd.org>
3662e30fcSMichael Zhilin  * All rights reserved.
4662e30fcSMichael Zhilin  *
5662e30fcSMichael Zhilin  * Redistribution and use in source and binary forms, with or without
6662e30fcSMichael Zhilin  * modification, are permitted provided that the following conditions
7662e30fcSMichael Zhilin  * are met:
8662e30fcSMichael Zhilin  * 1. Redistributions of source code must retain the above copyright
9662e30fcSMichael Zhilin  *    notice, this list of conditions and the following disclaimer.
10662e30fcSMichael Zhilin  * 2. Redistributions in binary form must reproduce the above copyright
11662e30fcSMichael Zhilin  *    notice, this list of conditions and the following disclaimer in the
12662e30fcSMichael Zhilin  *    documentation and/or other materials provided with the distribution.
13662e30fcSMichael Zhilin  *
14662e30fcSMichael Zhilin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15662e30fcSMichael Zhilin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16662e30fcSMichael Zhilin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17662e30fcSMichael Zhilin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18662e30fcSMichael Zhilin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19662e30fcSMichael Zhilin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20662e30fcSMichael Zhilin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21662e30fcSMichael Zhilin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22662e30fcSMichael Zhilin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23662e30fcSMichael Zhilin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24662e30fcSMichael Zhilin  * SUCH DAMAGE.
25662e30fcSMichael Zhilin  */
26662e30fcSMichael Zhilin 
27662e30fcSMichael Zhilin /*
28*1398c4c5SIan Lepore  * GPIOTHS - Temp/Humidity sensor over GPIO.
29*1398c4c5SIan Lepore  *
30662e30fcSMichael Zhilin  * This is driver for Temperature & Humidity sensor which provides digital
31662e30fcSMichael Zhilin  * output over single-wire protocol from embedded 8-bit microcontroller.
32*1398c4c5SIan Lepore  * Note that uses a custom single-wire protocol, it is not One-wire(tm).
33662e30fcSMichael Zhilin  *
34ce508b36SIan Lepore  * This driver supports the following chips:
35ce508b36SIan Lepore  *   DHT11:  Temp   0c to 50c +-2.0c, Humidity 20% to  90% +-5%
36ce508b36SIan Lepore  *   DHT12:  Temp -20c to 60c +-0.5c, Humidity 20% to  95% +-5%
37ce508b36SIan Lepore  *   DHT21:  Temp -40c to 80c +-0.3c, Humidity  0% to 100% +-3%
38ce508b36SIan Lepore  *   DHT22:  Temp -40c to 80c +-0.3c, Humidity  0% to 100% +-2%
39ce508b36SIan Lepore  *   AM2301: Same as DHT21, but also supports i2c interface.
40ce508b36SIan Lepore  *   AM2302: Same as DHT22, but also supports i2c interface.
41ce508b36SIan Lepore  *
42662e30fcSMichael Zhilin  * Temp/Humidity sensor can't be discovered automatically, please specify hints
43662e30fcSMichael Zhilin  * as part of loader or kernel configuration:
44662e30fcSMichael Zhilin  *	hint.gpioths.0.at="gpiobus0"
45662e30fcSMichael Zhilin  *	hint.gpioths.0.pins=<PIN>
46*1398c4c5SIan Lepore  *
47*1398c4c5SIan Lepore  * Or configure via FDT data.
48662e30fcSMichael Zhilin  */
49662e30fcSMichael Zhilin 
50*1398c4c5SIan Lepore #include <sys/cdefs.h>
51*1398c4c5SIan Lepore __FBSDID("$FreeBSD$");
52*1398c4c5SIan Lepore 
53*1398c4c5SIan Lepore #include <sys/param.h>
54*1398c4c5SIan Lepore #include <sys/kernel.h>
55*1398c4c5SIan Lepore #include <sys/bus.h>
56*1398c4c5SIan Lepore #include <sys/gpio.h>
57*1398c4c5SIan Lepore #include <sys/module.h>
58*1398c4c5SIan Lepore #include <sys/errno.h>
59*1398c4c5SIan Lepore #include <sys/systm.h>
60*1398c4c5SIan Lepore #include <sys/sysctl.h>
61*1398c4c5SIan Lepore 
62*1398c4c5SIan Lepore #include <dev/gpio/gpiobusvar.h>
63*1398c4c5SIan Lepore 
64*1398c4c5SIan Lepore #ifdef FDT
65*1398c4c5SIan Lepore #include <dev/ofw/ofw_bus.h>
66*1398c4c5SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
67*1398c4c5SIan Lepore 
68*1398c4c5SIan Lepore static struct ofw_compat_data compat_data[] = {
69*1398c4c5SIan Lepore 	{"dht11",  true},
70*1398c4c5SIan Lepore 	{NULL,     false}
71*1398c4c5SIan Lepore };
72*1398c4c5SIan Lepore OFWBUS_PNP_INFO(compat_data);
73*1398c4c5SIan Lepore SIMPLEBUS_PNP_INFO(compat_data);
74*1398c4c5SIan Lepore #endif /* FDT */
75*1398c4c5SIan Lepore 
76*1398c4c5SIan Lepore #define	PIN_IDX 0			/* Use the first/only configured pin. */
77*1398c4c5SIan Lepore 
78662e30fcSMichael Zhilin #define	GPIOTHS_POLLTIME	5	/* in seconds */
79662e30fcSMichael Zhilin 
80662e30fcSMichael Zhilin #define	GPIOTHS_DHT_STARTCYCLE	20000	/* 20ms = 20000us */
81662e30fcSMichael Zhilin #define	GPIOTHS_DHT_TIMEOUT	1000	/* 1ms = 1000us */
82662e30fcSMichael Zhilin #define	GPIOTHS_DHT_CYCLES	41
83662e30fcSMichael Zhilin #define	GPIOTHS_DHT_ONEBYTEMASK	0xFF
84662e30fcSMichael Zhilin 
85662e30fcSMichael Zhilin struct gpioths_softc {
86662e30fcSMichael Zhilin 	device_t		 dev;
87*1398c4c5SIan Lepore 	gpio_pin_t		 pin;
88662e30fcSMichael Zhilin 	int			 temp;
89662e30fcSMichael Zhilin 	int			 hum;
90662e30fcSMichael Zhilin 	int			 fails;
91662e30fcSMichael Zhilin 	struct callout		 callout;
92662e30fcSMichael Zhilin };
93662e30fcSMichael Zhilin 
94662e30fcSMichael Zhilin static int
95662e30fcSMichael Zhilin gpioths_probe(device_t dev)
96662e30fcSMichael Zhilin {
97*1398c4c5SIan Lepore 	int rv;
98*1398c4c5SIan Lepore 
99*1398c4c5SIan Lepore 	/*
100*1398c4c5SIan Lepore 	 * By default we only bid to attach if specifically added by our parent
101*1398c4c5SIan Lepore 	 * (usually via hint.gpioths.#.at=busname).  On FDT systems we bid as
102*1398c4c5SIan Lepore 	 * the default driver based on being configured in the FDT data.
103*1398c4c5SIan Lepore 	 */
104*1398c4c5SIan Lepore 	rv = BUS_PROBE_NOWILDCARD;
105*1398c4c5SIan Lepore 
106*1398c4c5SIan Lepore #ifdef FDT
107*1398c4c5SIan Lepore 	if (ofw_bus_status_okay(dev) &&
108*1398c4c5SIan Lepore 	    ofw_bus_search_compatible(dev, compat_data)->ocd_data)
109*1398c4c5SIan Lepore 		rv = BUS_PROBE_DEFAULT;
110*1398c4c5SIan Lepore #endif
111*1398c4c5SIan Lepore 
112*1398c4c5SIan Lepore 	device_set_desc(dev, "DHT11/DHT22 Temperature and Humidity Sensor");
113*1398c4c5SIan Lepore 
114*1398c4c5SIan Lepore 	return (rv);
115662e30fcSMichael Zhilin }
116662e30fcSMichael Zhilin 
117662e30fcSMichael Zhilin static int
118*1398c4c5SIan Lepore gpioths_dht_timeuntil(struct gpioths_softc *sc, bool lev, uint32_t *time)
119662e30fcSMichael Zhilin {
120*1398c4c5SIan Lepore 	bool		cur_level;
121662e30fcSMichael Zhilin 	int		i;
122662e30fcSMichael Zhilin 
123662e30fcSMichael Zhilin 	for (i = 0; i < GPIOTHS_DHT_TIMEOUT; i++) {
124*1398c4c5SIan Lepore 		gpio_pin_is_active(sc->pin, &cur_level);
125662e30fcSMichael Zhilin 		if (cur_level == lev) {
126662e30fcSMichael Zhilin 			if (time != NULL)
127662e30fcSMichael Zhilin 				*time = i;
128662e30fcSMichael Zhilin 			return (0);
129662e30fcSMichael Zhilin 		}
130662e30fcSMichael Zhilin 		DELAY(1);
131662e30fcSMichael Zhilin 	}
132662e30fcSMichael Zhilin 
133662e30fcSMichael Zhilin 	/* Timeout */
134662e30fcSMichael Zhilin 	return (ETIMEDOUT);
135662e30fcSMichael Zhilin }
136662e30fcSMichael Zhilin 
137*1398c4c5SIan Lepore static void
138*1398c4c5SIan Lepore gpioths_dht_initread(struct gpioths_softc *sc)
139662e30fcSMichael Zhilin {
140662e30fcSMichael Zhilin 
141662e30fcSMichael Zhilin 	/*
142*1398c4c5SIan Lepore 	 * According to specifications we need to drive the data line low for at
143*1398c4c5SIan Lepore 	 * least 20ms then drive it high, to wake up the chip and signal it to
144*1398c4c5SIan Lepore 	 * send a measurement. After sending this start signal, we switch the
145*1398c4c5SIan Lepore 	 * pin back to input so the device can begin talking to us.
146662e30fcSMichael Zhilin 	 */
147*1398c4c5SIan Lepore 	gpio_pin_setflags(sc->pin, GPIO_PIN_OUTPUT);
148*1398c4c5SIan Lepore 	gpio_pin_set_active(sc->pin, false);
149662e30fcSMichael Zhilin 	DELAY(GPIOTHS_DHT_STARTCYCLE);
150*1398c4c5SIan Lepore 	gpio_pin_set_active(sc->pin, true);
151*1398c4c5SIan Lepore 	gpio_pin_setflags(sc->pin, GPIO_PIN_INPUT);
152662e30fcSMichael Zhilin }
153662e30fcSMichael Zhilin 
154662e30fcSMichael Zhilin static int
155*1398c4c5SIan Lepore gpioths_dht_readbytes(struct gpioths_softc *sc)
156662e30fcSMichael Zhilin {
157662e30fcSMichael Zhilin 	uint32_t		 calibrations[GPIOTHS_DHT_CYCLES];
158662e30fcSMichael Zhilin 	uint32_t		 intervals[GPIOTHS_DHT_CYCLES];
159662e30fcSMichael Zhilin 	uint32_t		 err, avglen, value;
160662e30fcSMichael Zhilin 	uint8_t			 crc, calc;
161ce508b36SIan Lepore 	int			 i, negmul, offset, size, tmphi, tmplo;
162662e30fcSMichael Zhilin 
163*1398c4c5SIan Lepore 	gpioths_dht_initread(sc);
164662e30fcSMichael Zhilin 
165*1398c4c5SIan Lepore 	err = gpioths_dht_timeuntil(sc, false, NULL);
166662e30fcSMichael Zhilin 	if (err) {
167*1398c4c5SIan Lepore 		device_printf(sc->dev, "err(START) = %d\n", err);
168662e30fcSMichael Zhilin 		goto error;
169662e30fcSMichael Zhilin 	}
170662e30fcSMichael Zhilin 
171662e30fcSMichael Zhilin 	/* reading - 41 cycles */
172662e30fcSMichael Zhilin 	for (i = 0; i < GPIOTHS_DHT_CYCLES; i++) {
173*1398c4c5SIan Lepore 		err = gpioths_dht_timeuntil(sc, true, &calibrations[i]);
174662e30fcSMichael Zhilin 		if (err) {
175*1398c4c5SIan Lepore 			device_printf(sc->dev, "err(CAL, %d) = %d\n", i, err);
176662e30fcSMichael Zhilin 			goto error;
177662e30fcSMichael Zhilin 		}
178*1398c4c5SIan Lepore 		err = gpioths_dht_timeuntil(sc, false, &intervals[i]);
179662e30fcSMichael Zhilin 		if (err) {
180*1398c4c5SIan Lepore 			device_printf(sc->dev, "err(INTERVAL, %d) = %d\n", i, err);
181662e30fcSMichael Zhilin 			goto error;
182662e30fcSMichael Zhilin 		}
183662e30fcSMichael Zhilin 	}
184662e30fcSMichael Zhilin 
185662e30fcSMichael Zhilin 	/* Calculate average data calibration cycle length */
186662e30fcSMichael Zhilin 	avglen = 0;
187662e30fcSMichael Zhilin 	for (i = 1; i < GPIOTHS_DHT_CYCLES; i++)
188662e30fcSMichael Zhilin 		avglen += calibrations[i];
189662e30fcSMichael Zhilin 
190662e30fcSMichael Zhilin 	avglen = avglen / (GPIOTHS_DHT_CYCLES - 1);
191662e30fcSMichael Zhilin 
192662e30fcSMichael Zhilin 	/* Calculate data */
193662e30fcSMichael Zhilin 	value = 0;
194662e30fcSMichael Zhilin 	offset = 1;
195662e30fcSMichael Zhilin 	size = sizeof(value) * 8;
196662e30fcSMichael Zhilin 	for (i = offset; i < size + offset; i++) {
197662e30fcSMichael Zhilin 		value <<= 1;
198662e30fcSMichael Zhilin 		if (intervals[i] > avglen)
199662e30fcSMichael Zhilin 			value += 1;
200662e30fcSMichael Zhilin 	}
201662e30fcSMichael Zhilin 
202662e30fcSMichael Zhilin 	/* Calculate CRC */
203662e30fcSMichael Zhilin 	crc = 0;
204662e30fcSMichael Zhilin 	offset = sizeof(value) * 8 + 1;
205662e30fcSMichael Zhilin 	size = sizeof(crc) * 8;
206662e30fcSMichael Zhilin 	for (i = offset;  i < size + offset; i++) {
207662e30fcSMichael Zhilin 		crc <<= 1;
208662e30fcSMichael Zhilin 		if (intervals[i] > avglen)
209662e30fcSMichael Zhilin 			crc += 1;
210662e30fcSMichael Zhilin 	}
211662e30fcSMichael Zhilin 
212662e30fcSMichael Zhilin 	calc = 0;
213662e30fcSMichael Zhilin 	for (i = 0; i < sizeof(value); i++)
214662e30fcSMichael Zhilin 		calc += (value >> (8*i)) & GPIOTHS_DHT_ONEBYTEMASK;
215662e30fcSMichael Zhilin 
216662e30fcSMichael Zhilin #ifdef GPIOTHS_DEBUG
217662e30fcSMichael Zhilin 	/* Debug bits */
218662e30fcSMichael Zhilin 	for (i = 0; i < GPIOTHS_DHT_CYCLES; i++)
219662e30fcSMichael Zhilin 		device_printf(dev, "%d: %d %d\n", i, calibrations[i],
220662e30fcSMichael Zhilin 		    intervals[i]);
221662e30fcSMichael Zhilin 
222662e30fcSMichael Zhilin 	device_printf(dev, "len=%d, data=%x, crc=%x/%x\n", avglen, value, crc,
223662e30fcSMichael Zhilin 	    calc);
224662e30fcSMichael Zhilin #endif /* GPIOTHS_DEBUG */
225662e30fcSMichael Zhilin 
226662e30fcSMichael Zhilin 	/* CRC check */
227662e30fcSMichael Zhilin 	if (calc != crc) {
228662e30fcSMichael Zhilin 		err = -1;
229662e30fcSMichael Zhilin 		goto error;
230662e30fcSMichael Zhilin 	}
231662e30fcSMichael Zhilin 
232ce508b36SIan Lepore 	/*
233ce508b36SIan Lepore 	 * For DHT11/12, the values are split into 8 bits of integer and 8 bits
234ce508b36SIan Lepore 	 * of fractional tenths.  On DHT11 the fraction bytes are always zero.
235ce508b36SIan Lepore 	 * On DHT12 the sign bit is in the high bit of the fraction byte.
236ce508b36SIan Lepore 	 *  - DHT11: 0HHHHHHH 00000000 00TTTTTT 00000000
237ce508b36SIan Lepore 	 *  - DHT12: 0HHHHHHH 0000hhhh 00TTTTTT s000tttt
238ce508b36SIan Lepore 	 *
239ce508b36SIan Lepore 	 * For DHT21/21, the values are are encoded in 16 bits each, with the
240ce508b36SIan Lepore 	 * temperature sign bit in the high bit.  The values are tenths of a
241ce508b36SIan Lepore 	 * degree C and tenths of a percent RH.
242ce508b36SIan Lepore 	 *  - DHT21: 000000HH HHHHHHHH s00000TT TTTTTTTT
243ce508b36SIan Lepore 	 *  - DHT22: 000000HH HHHHHHHH s00000TT TTTTTTTT
244ce508b36SIan Lepore 	 *
245ce508b36SIan Lepore 	 * For all devices, some bits are always zero because of the range of
246ce508b36SIan Lepore 	 * values supported by the device.
247ce508b36SIan Lepore 	 *
248ce508b36SIan Lepore 	 * We figure out how to decode things based on the high byte of the
249ce508b36SIan Lepore 	 * humidity.  A DHT21/22 cannot report a value greater than 3 in
250ce508b36SIan Lepore 	 * the upper bits of its 16-bit humidity.  A DHT11/12 should not report
251ce508b36SIan Lepore 	 * a value lower than 20.  To allow for the possibility that a device
252ce508b36SIan Lepore 	 * could report a value slightly out of its sensitivity range, we split
253*1398c4c5SIan Lepore 	 * the difference and say if the value is greater than 10 it must be a
254*1398c4c5SIan Lepore 	 * DHT11/12 (that would be a humidity over 256% on a DHT21/22).
255ce508b36SIan Lepore 	 */
256ce508b36SIan Lepore #define	DK_OFFSET 2731 /* Offset between K and C, in decikelvins. */
257ce508b36SIan Lepore 	if ((value >> 24) > 10) {
258ce508b36SIan Lepore 		/* DHT11 or DHT12 */
259ce508b36SIan Lepore 		tmphi = (value >> 8) & 0x3f;
260ce508b36SIan Lepore 		tmplo = value & 0x0f;
261ce508b36SIan Lepore 		negmul = (value & 0x80) ? -1 : 1;
262ce508b36SIan Lepore 		sc->temp = DK_OFFSET + (negmul * (tmphi * 10 + tmplo));
263ce508b36SIan Lepore 		sc->hum = (value >> 24) & 0x7f;
264ce508b36SIan Lepore 	} else {
265ce508b36SIan Lepore                 /* DHT21 or DHT22 */
266ce508b36SIan Lepore 		negmul = (value & 0x8000) ? -1 : 1;
267ce508b36SIan Lepore 		sc->temp = DK_OFFSET + (negmul * (value & 0x03ff));
268ce508b36SIan Lepore 		sc->hum = ((value >> 16) & 0x03ff) / 10;
269ce508b36SIan Lepore 	}
270ce508b36SIan Lepore 
271662e30fcSMichael Zhilin 	sc->fails = 0;
272662e30fcSMichael Zhilin 
273662e30fcSMichael Zhilin #ifdef GPIOTHS_DEBUG
274662e30fcSMichael Zhilin 	/* Debug bits */
275662e30fcSMichael Zhilin 	device_printf(dev, "fails=%d, temp=%d, hum=%d\n", sc->fails,
276662e30fcSMichael Zhilin 	    sc->temp, sc->hum);
277662e30fcSMichael Zhilin #endif /* GPIOTHS_DEBUG */
278662e30fcSMichael Zhilin 
279662e30fcSMichael Zhilin 	return (0);
280662e30fcSMichael Zhilin error:
281662e30fcSMichael Zhilin 	sc->fails++;
282662e30fcSMichael Zhilin 	return (err);
283662e30fcSMichael Zhilin }
284662e30fcSMichael Zhilin 
285662e30fcSMichael Zhilin static void
286662e30fcSMichael Zhilin gpioths_poll(void *arg)
287662e30fcSMichael Zhilin {
288662e30fcSMichael Zhilin 	struct gpioths_softc	*sc;
289662e30fcSMichael Zhilin 
290*1398c4c5SIan Lepore 	sc = (struct gpioths_softc *)arg;
291662e30fcSMichael Zhilin 
292*1398c4c5SIan Lepore 	gpioths_dht_readbytes(sc);
293662e30fcSMichael Zhilin 	callout_schedule(&sc->callout, GPIOTHS_POLLTIME * hz);
294662e30fcSMichael Zhilin }
295662e30fcSMichael Zhilin 
296662e30fcSMichael Zhilin static int
297662e30fcSMichael Zhilin gpioths_attach(device_t dev)
298662e30fcSMichael Zhilin {
299662e30fcSMichael Zhilin 	struct gpioths_softc	*sc;
300662e30fcSMichael Zhilin 	struct sysctl_ctx_list	*ctx;
301662e30fcSMichael Zhilin 	struct sysctl_oid	*tree;
302*1398c4c5SIan Lepore 	int err;
303662e30fcSMichael Zhilin 
304662e30fcSMichael Zhilin 	sc = device_get_softc(dev);
305662e30fcSMichael Zhilin 	ctx = device_get_sysctl_ctx(dev);
306662e30fcSMichael Zhilin 	tree = device_get_sysctl_tree(dev);
307662e30fcSMichael Zhilin 
308662e30fcSMichael Zhilin 	sc->dev = dev;
309662e30fcSMichael Zhilin 
310*1398c4c5SIan Lepore #ifdef FDT
311*1398c4c5SIan Lepore 	/* Try to configure our pin from fdt data on fdt-based systems. */
312*1398c4c5SIan Lepore 	err = gpio_pin_get_by_ofw_idx(dev, ofw_bus_get_node(dev), PIN_IDX,
313*1398c4c5SIan Lepore 	    &sc->pin);
314*1398c4c5SIan Lepore #else
315*1398c4c5SIan Lepore 	err = ENOENT;
316*1398c4c5SIan Lepore #endif
317*1398c4c5SIan Lepore 	/*
318*1398c4c5SIan Lepore 	 * If we didn't get configured by fdt data and our parent is gpiobus,
319*1398c4c5SIan Lepore 	 * see if we can be configured by the bus (allows hinted attachment even
320*1398c4c5SIan Lepore 	 * on fdt-based systems).
321*1398c4c5SIan Lepore 	 */
322*1398c4c5SIan Lepore 	if (err != 0 &&
323*1398c4c5SIan Lepore 	    strcmp("gpiobus", device_get_name(device_get_parent(dev))) == 0)
324*1398c4c5SIan Lepore 		err = gpio_pin_get_by_child_index(dev, PIN_IDX, &sc->pin);
325*1398c4c5SIan Lepore 
326*1398c4c5SIan Lepore 	/* If we didn't get configured by either method, whine and punt. */
327*1398c4c5SIan Lepore 	if (err != 0) {
328*1398c4c5SIan Lepore 		device_printf(sc->dev,
329*1398c4c5SIan Lepore 		    "cannot acquire gpio pin (config error)\n");
330*1398c4c5SIan Lepore 		return (err);
331*1398c4c5SIan Lepore 	}
332*1398c4c5SIan Lepore 
333*1398c4c5SIan Lepore 	/*
334*1398c4c5SIan Lepore 	 * Ensure we have control of our pin, and preset the data line to its
335*1398c4c5SIan Lepore 	 * idle condition (high).  Leave the line in input mode, relying on the
336*1398c4c5SIan Lepore 	 * external pullup to keep the line high while idle.
337*1398c4c5SIan Lepore 	 */
338*1398c4c5SIan Lepore 	err = gpio_pin_setflags(sc->pin, GPIO_PIN_OUTPUT);
339*1398c4c5SIan Lepore 	if (err != 0) {
340*1398c4c5SIan Lepore 		device_printf(dev, "gpio_pin_setflags(OUT) = %d\n", err);
341*1398c4c5SIan Lepore 		return (err);
342*1398c4c5SIan Lepore 	}
343*1398c4c5SIan Lepore 	err = gpio_pin_set_active(sc->pin, true);
344*1398c4c5SIan Lepore 	if (err != 0) {
345*1398c4c5SIan Lepore 		device_printf(dev, "gpio_pin_set_active(false) = %d\n", err);
346*1398c4c5SIan Lepore 		return (err);
347*1398c4c5SIan Lepore 	}
348*1398c4c5SIan Lepore 	err = gpio_pin_setflags(sc->pin, GPIO_PIN_INPUT);
349*1398c4c5SIan Lepore 	if (err != 0) {
350*1398c4c5SIan Lepore 		device_printf(dev, "gpio_pin_setflags(IN) = %d\n", err);
351*1398c4c5SIan Lepore 		return (err);
352*1398c4c5SIan Lepore 	}
353*1398c4c5SIan Lepore 
3549f8df20cSIan Lepore 	/*
3559f8df20cSIan Lepore 	 * Do an initial read so we have correct values for reporting before
3569f8df20cSIan Lepore 	 * registering the sysctls that can access those values.
3579f8df20cSIan Lepore 	 */
358*1398c4c5SIan Lepore 	gpioths_dht_readbytes(sc);
359662e30fcSMichael Zhilin 
36078e70ab9SIan Lepore 	sysctl_add_oid(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "temperature",				\
36178e70ab9SIan Lepore 	    CTLFLAG_RD | CTLTYPE_INT | CTLFLAG_MPSAFE,
362ce508b36SIan Lepore 	    &sc->temp, 0, sysctl_handle_int, "IK", "temperature", NULL);
363662e30fcSMichael Zhilin 
36478e70ab9SIan Lepore 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "humidity",
36578e70ab9SIan Lepore 	    CTLFLAG_RD, &sc->hum, 0, "relative humidity(%)");
366662e30fcSMichael Zhilin 
36778e70ab9SIan Lepore 	SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, "fails",
36878e70ab9SIan Lepore 	    CTLFLAG_RD, &sc->fails, 0,
36978e70ab9SIan Lepore 	    "failures since last successful read");
370662e30fcSMichael Zhilin 
3719f8df20cSIan Lepore 	callout_init(&sc->callout, 1);
372*1398c4c5SIan Lepore 	callout_reset(&sc->callout, GPIOTHS_POLLTIME * hz, gpioths_poll, sc);
3739f8df20cSIan Lepore 
374662e30fcSMichael Zhilin 	return (0);
375662e30fcSMichael Zhilin }
376662e30fcSMichael Zhilin 
377662e30fcSMichael Zhilin static int
378662e30fcSMichael Zhilin gpioths_detach(device_t dev)
379662e30fcSMichael Zhilin {
3809f8df20cSIan Lepore 	struct gpioths_softc	*sc;
3819f8df20cSIan Lepore 
3829f8df20cSIan Lepore 	sc = device_get_softc(dev);
383*1398c4c5SIan Lepore 	gpio_pin_release(sc->pin);
3849f8df20cSIan Lepore 	callout_drain(&sc->callout);
385662e30fcSMichael Zhilin 
386662e30fcSMichael Zhilin 	return (0);
387662e30fcSMichael Zhilin }
388662e30fcSMichael Zhilin 
389662e30fcSMichael Zhilin /* Driver bits */
390662e30fcSMichael Zhilin static device_method_t gpioths_methods[] = {
391662e30fcSMichael Zhilin 	/* Device interface */
392662e30fcSMichael Zhilin 	DEVMETHOD(device_probe,			gpioths_probe),
393662e30fcSMichael Zhilin 	DEVMETHOD(device_attach,		gpioths_attach),
394662e30fcSMichael Zhilin 	DEVMETHOD(device_detach,		gpioths_detach),
395662e30fcSMichael Zhilin 
396662e30fcSMichael Zhilin 	DEVMETHOD_END
397662e30fcSMichael Zhilin };
398662e30fcSMichael Zhilin 
39935e9bfc9SIan Lepore static devclass_t gpioths_devclass;
40035e9bfc9SIan Lepore 
401662e30fcSMichael Zhilin DEFINE_CLASS_0(gpioths, gpioths_driver, gpioths_methods, sizeof(struct gpioths_softc));
402*1398c4c5SIan Lepore 
403*1398c4c5SIan Lepore #ifdef FDT
404*1398c4c5SIan Lepore DRIVER_MODULE(gpioths, simplebus, gpioths_driver, gpioths_devclass, 0, 0);
405*1398c4c5SIan Lepore #endif
406*1398c4c5SIan Lepore 
407662e30fcSMichael Zhilin DRIVER_MODULE(gpioths, gpiobus, gpioths_driver, gpioths_devclass, 0, 0);
40835e9bfc9SIan Lepore MODULE_DEPEND(gpioths, gpiobus, 1, 1, 1);
409