xref: /openbsd-src/sys/dev/gpio/gpioow.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: gpioow.c,v 1.3 2008/11/24 12:12:12 mbalmer Exp $	*/
2 
3 /*
4  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * 1-Wire bus bit-banging through GPIO pin.
21  */
22 
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/gpio.h>
27 
28 #include <dev/gpio/gpiovar.h>
29 
30 #include <dev/onewire/onewirevar.h>
31 
32 #define GPIOOW_NPINS		1
33 #define GPIOOW_PIN_DATA		0
34 
35 struct gpioow_softc {
36 	struct device		sc_dev;
37 
38 	void *			sc_gpio;
39 	struct gpio_pinmap	sc_map;
40 	int			__map[GPIOOW_NPINS];
41 
42 	struct onewire_bus	sc_ow_bus;
43 	struct device *		sc_ow_dev;
44 
45 	int			sc_data;
46 	int			sc_dying;
47 };
48 
49 int	gpioow_match(struct device *, void *, void *);
50 void	gpioow_attach(struct device *, struct device *, void *);
51 int	gpioow_detach(struct device *, int);
52 int	gpioow_activate(struct device *, enum devact);
53 
54 int	gpioow_ow_reset(void *);
55 int	gpioow_ow_bit(void *, int);
56 
57 void	gpioow_bb_rx(void *);
58 void	gpioow_bb_tx(void *);
59 int	gpioow_bb_get(void *);
60 void	gpioow_bb_set(void *, int);
61 
62 struct cfattach gpioow_ca = {
63 	sizeof(struct gpioow_softc),
64 	gpioow_match,
65 	gpioow_attach,
66 	gpioow_detach,
67 	gpioow_activate
68 };
69 
70 struct cfdriver gpioow_cd = {
71 	NULL, "gpioow", DV_DULL
72 };
73 
74 static const struct onewire_bbops gpioow_bbops = {
75 	gpioow_bb_rx,
76 	gpioow_bb_tx,
77 	gpioow_bb_get,
78 	gpioow_bb_set
79 };
80 
81 int
82 gpioow_match(struct device *parent, void *match, void *aux)
83 {
84 	struct cfdata *cf = match;
85 	struct gpio_attach_args *ga = aux;
86 
87 	if (ga->ga_offset == -1)
88 		return 0;
89 
90 	return (strcmp(cf->cf_driver->cd_name, "gpioow") == 0);
91 }
92 
93 void
94 gpioow_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct gpioow_softc *sc = (struct gpioow_softc *)self;
97 	struct gpio_attach_args *ga = aux;
98 	struct onewirebus_attach_args oba;
99 	int caps;
100 
101 	/* Check that we have enough pins */
102 	if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
103 		printf(": invalid pin mask\n");
104 		return;
105 	}
106 
107 	/* Map pins */
108 	sc->sc_gpio = ga->ga_gpio;
109 	sc->sc_map.pm_map = sc->__map;
110 	if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
111 	    &sc->sc_map)) {
112 		printf(": can't map pins\n");
113 		return;
114 	}
115 
116 	/* Configure data pin */
117 	caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
118 	if (!(caps & GPIO_PIN_OUTPUT)) {
119 		printf(": data pin is unable to drive output\n");
120 		goto fail;
121 	}
122 	if (!(caps & GPIO_PIN_INPUT)) {
123 		printf(": data pin is unable to read input\n");
124 		goto fail;
125 	}
126 	printf(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
127 	sc->sc_data = GPIO_PIN_OUTPUT;
128 	if (caps & GPIO_PIN_OPENDRAIN) {
129 		printf(" open-drain");
130 		sc->sc_data |= GPIO_PIN_OPENDRAIN;
131 	} else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
132 		printf(" push-pull tri-state");
133 		sc->sc_data |= GPIO_PIN_PUSHPULL;
134 	}
135 	if (caps & GPIO_PIN_PULLUP) {
136 		printf(" pull-up");
137 		sc->sc_data |= GPIO_PIN_PULLUP;
138 	}
139 	gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
140 
141 	printf("\n");
142 
143 	/* Attach 1-Wire bus */
144 	sc->sc_ow_bus.bus_cookie = sc;
145 	sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
146 	sc->sc_ow_bus.bus_bit = gpioow_ow_bit;
147 
148 	bzero(&oba, sizeof(oba));
149 	oba.oba_bus = &sc->sc_ow_bus;
150 	sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
151 
152 	return;
153 
154 fail:
155 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
156 }
157 
158 int
159 gpioow_detach(struct device *self, int flags)
160 {
161 	struct gpioow_softc *sc = (struct gpioow_softc *)self;
162 	int rv = 0;
163 
164 	gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
165 
166 	if (sc->sc_ow_dev != NULL)
167 		rv = config_detach(sc->sc_ow_dev, flags);
168 
169 	return (rv);
170 }
171 
172 int
173 gpioow_activate(struct device *self, enum devact act)
174 {
175 	struct gpioow_softc *sc = (struct gpioow_softc *)self;
176 	int rv = 0;
177 
178 	switch (act) {
179 	case DVACT_ACTIVATE:
180 		break;
181 	case DVACT_DEACTIVATE:
182 		sc->sc_dying = 1;
183 		if (sc->sc_ow_dev != NULL)
184 			rv = config_deactivate(sc->sc_ow_dev);
185 		break;
186 	}
187 
188 	return (rv);
189 }
190 
191 int
192 gpioow_ow_reset(void *arg)
193 {
194 	return (onewire_bb_reset(&gpioow_bbops, arg));
195 }
196 
197 int
198 gpioow_ow_bit(void *arg, int value)
199 {
200 	return (onewire_bb_bit(&gpioow_bbops, arg, value));
201 }
202 
203 void
204 gpioow_bb_rx(void *arg)
205 {
206 	struct gpioow_softc *sc = arg;
207 	int data = sc->sc_data;
208 
209 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
210 	data |= GPIO_PIN_INPUT;
211 	if (data & GPIO_PIN_PUSHPULL)
212 		data |= GPIO_PIN_TRISTATE;
213 	if (sc->sc_data != data) {
214 		sc->sc_data = data;
215 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
216 		    sc->sc_data);
217 	}
218 }
219 
220 void
221 gpioow_bb_tx(void *arg)
222 {
223 	struct gpioow_softc *sc = arg;
224 	int data = sc->sc_data;
225 
226 	data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
227 	data |= GPIO_PIN_OUTPUT;
228 	if (sc->sc_data != data) {
229 		sc->sc_data = data;
230 		gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
231 		    sc->sc_data);
232 	}
233 }
234 
235 int
236 gpioow_bb_get(void *arg)
237 {
238 	struct gpioow_softc *sc = arg;
239 
240 	return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
241 	    GPIO_PIN_HIGH ? 1 : 0);
242 }
243 
244 void
245 gpioow_bb_set(void *arg, int value)
246 {
247 	struct gpioow_softc *sc = arg;
248 
249 	gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
250 	    value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
251 }
252