xref: /netbsd-src/sys/dev/gpio/gpioow.c (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 /* $NetBSD: gpioow.c,v 1.17 2019/12/06 07:12:38 maxv Exp $ */
2 /*	$OpenBSD: gpioow.c,v 1.1 2006/03/04 16:27:03 grange Exp $	*/
3 
4 /*
5  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include <sys/cdefs.h>
21 __KERNEL_RCSID(0, "$NetBSD: gpioow.c,v 1.17 2019/12/06 07:12:38 maxv Exp $");
22 
23 /*
24  * 1-Wire bus bit-banging through GPIO pin.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/device.h>
30 #include <sys/gpio.h>
31 #include <sys/module.h>
32 
33 #include <dev/gpio/gpiovar.h>
34 
35 #include <dev/onewire/onewirevar.h>
36 
37 #include "ioconf.h"
38 
39 #define GPIOOW_NPINS		1
40 #define GPIOOW_PIN_DATA		0
41 
42 struct gpioow_softc {
43 	void *			sc_gpio;
44 	struct gpio_pinmap	sc_map;
45 	int			_map[GPIOOW_NPINS];
46 
47 	struct onewire_bus	sc_ow_bus;
48 	device_t		sc_ow_dev;
49 
50 	int			sc_data;
51 	int			sc_dying;
52 };
53 
54 static int	gpioow_match(device_t, cfdata_t, void *);
55 static void	gpioow_attach(device_t, device_t, void *);
56 static int	gpioow_detach(device_t, int);
57 static int	gpioow_activate(device_t, enum devact);
58 
59 static int	gpioow_ow_reset(void *);
60 static int	gpioow_ow_read_bit(void *);
61 static void	gpioow_ow_write_bit(void *, int);
62 
63 static void	gpioow_bb_rx(void *);
64 static void	gpioow_bb_tx(void *);
65 static int	gpioow_bb_get(void *);
66 static void	gpioow_bb_set(void *, int);
67 
68 CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc),
69 	gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate);
70 
71 static const struct onewire_bbops gpioow_bbops = {
72 	gpioow_bb_rx,
73 	gpioow_bb_tx,
74 	gpioow_bb_get,
75 	gpioow_bb_set
76 };
77 
78 static int
79 gpioow_match(device_t parent, cfdata_t cf, void *aux)
80 {
81 	struct gpio_attach_args *ga = aux;
82 
83 	if (strcmp(ga->ga_dvname, cf->cf_name))
84 		return 0;
85 
86 	if (ga->ga_offset == -1)
87 		return 0;
88 
89 	/* Check that we have enough pins */
90 	if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
91 		aprint_debug("%s: invalid pin mask 0x%02x\n", cf->cf_name,
92 		    ga->ga_mask);
93 		return 0;
94 	}
95 	return 1;
96 }
97 
98 static void
99 gpioow_attach(device_t parent, device_t self, void *aux)
100 {
101 	struct gpioow_softc *sc = device_private(self);
102 	struct gpio_attach_args *ga = aux;
103 	struct onewirebus_attach_args oba;
104 	int caps;
105 
106 	/* Map pins */
107 	sc->sc_gpio = ga->ga_gpio;
108 	sc->sc_map.pm_map = sc->_map;
109 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
110 	    &sc->sc_map)) {
111 		aprint_error(": can't map pins\n");
112 		goto finish;
113 	}
114 
115 	/* Configure data pin */
116 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
117 	if (!(caps & GPIO_PIN_OUTPUT)) {
118 		aprint_error(": data pin is unable to drive output\n");
119 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
120 		goto finish;
121 	}
122 	if (!(caps & GPIO_PIN_INPUT)) {
123 		aprint_error(": data pin is unable to read input\n");
124 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
125 		goto finish;
126 	}
127 	aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
128 	sc->sc_data = GPIO_PIN_OUTPUT;
129 	if (caps & GPIO_PIN_OPENDRAIN) {
130 		aprint_normal(" open-drain");
131 		sc->sc_data |= GPIO_PIN_OPENDRAIN;
132 	} else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
133 		aprint_normal(" push-pull tri-state");
134 		sc->sc_data |= GPIO_PIN_PUSHPULL;
135 	}
136 	if (caps & GPIO_PIN_PULLUP) {
137 		aprint_normal(" pull-up");
138 		sc->sc_data |= GPIO_PIN_PULLUP;
139 	}
140 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
141 
142 	aprint_normal("\n");
143 
144 	/* Attach 1-Wire bus */
145 	sc->sc_ow_bus.bus_cookie = sc;
146 	sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
147 	sc->sc_ow_bus.bus_read_bit = gpioow_ow_read_bit;
148 	sc->sc_ow_bus.bus_write_bit = gpioow_ow_write_bit;
149 
150 	memset(&oba, 0, sizeof(oba));
151 	oba.oba_bus = &sc->sc_ow_bus;
152 	sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
153 
154 	if (!pmf_device_register(self, NULL, NULL))
155 		aprint_error("%s: could not establish power handler\n",
156 		    device_xname(self));
157 finish:
158 	return;
159 }
160 
161 static int
162 gpioow_detach(device_t self, int flags)
163 {
164 	struct gpioow_softc *sc = device_private(self);
165 	int rv = 0;
166 
167 	if (sc->sc_ow_dev != NULL)
168 		rv = config_detach(sc->sc_ow_dev, flags);
169 
170 	if (!rv) {
171 		gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
172 		pmf_device_deregister(self);
173 	}
174 	return rv;
175 }
176 
177 static int
178 gpioow_activate(device_t self, enum devact act)
179 {
180 	struct gpioow_softc *sc = device_private(self);
181 
182 	switch (act) {
183 	case DVACT_DEACTIVATE:
184 		sc->sc_dying = 1;
185 		return 0;
186 	default:
187 		return EOPNOTSUPP;
188 	}
189 }
190 
191 static int
192 gpioow_ow_reset(void *arg)
193 {
194 	return (onewire_bb_reset(&gpioow_bbops, arg));
195 }
196 
197 static int
198 gpioow_ow_read_bit(void *arg)
199 {
200 	return (onewire_bb_read_bit(&gpioow_bbops, arg));
201 }
202 
203 static void
204 gpioow_ow_write_bit(void *arg, int value)
205 {
206 	onewire_bb_write_bit(&gpioow_bbops, arg, value);
207 }
208 
209 static void
210 gpioow_bb_rx(void *arg)
211 {
212 	struct gpioow_softc *sc = arg;
213 	int data = sc->sc_data;
214 
215 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
216 	data |= GPIO_PIN_INPUT;
217 	if (data & GPIO_PIN_PUSHPULL)
218 		data |= GPIO_PIN_TRISTATE;
219 	if (sc->sc_data != data) {
220 		sc->sc_data = data;
221 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
222 		    sc->sc_data);
223 	}
224 }
225 
226 static void
227 gpioow_bb_tx(void *arg)
228 {
229 	struct gpioow_softc *sc = arg;
230 	int data = sc->sc_data;
231 
232 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
233 	data |= GPIO_PIN_OUTPUT;
234 	if (sc->sc_data != data) {
235 		sc->sc_data = data;
236 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
237 		    sc->sc_data);
238 	}
239 }
240 
241 static int
242 gpioow_bb_get(void *arg)
243 {
244 	struct gpioow_softc *sc = arg;
245 
246 	return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
247 	    GPIO_PIN_HIGH ? 1 : 0);
248 }
249 
250 static void
251 gpioow_bb_set(void *arg, int value)
252 {
253 	struct gpioow_softc *sc = arg;
254 
255 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
256 	    value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
257 }
258 
259 MODULE(MODULE_CLASS_DRIVER, gpioow, "gpio,onewire");
260 
261 #ifdef _MODULE
262 #include "ioconf.c"
263 #endif
264 
265 static int
266 gpioow_modcmd(modcmd_t cmd, void *opaque)
267 {
268 	int error;
269 
270 	error = 0;
271 	switch (cmd) {
272 	case MODULE_CMD_INIT:
273 #ifdef _MODULE
274 		error = config_init_component(cfdriver_ioconf_gpioow,
275 		    cfattach_ioconf_gpioow, cfdata_ioconf_gpioow);
276 		if (error)
277 			aprint_error("%s: unable to init component\n",
278 			    gpioow_cd.cd_name);
279 #endif
280 		break;
281 	case MODULE_CMD_FINI:
282 #ifdef _MODULE
283 		config_fini_component(cfdriver_ioconf_gpioow,
284 		    cfattach_ioconf_gpioow, cfdata_ioconf_gpioow);
285 #endif
286 		break;
287 	default:
288 		error = ENOTTY;
289 	}
290 	return error;
291 }
292