gpioow.c revision 1.8 1 /* $NetBSD: gpioow.c,v 1.8 2009/08/03 12:42:45 mbalmer 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 (at) 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.8 2009/08/03 12:42:45 mbalmer 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
32 #include <dev/gpio/gpiovar.h>
33
34 #include <dev/onewire/onewirevar.h>
35
36 #define GPIOOW_NPINS 1
37 #define GPIOOW_PIN_DATA 0
38
39 struct gpioow_softc {
40 void * sc_gpio;
41 struct gpio_pinmap sc_map;
42 int __map[GPIOOW_NPINS];
43
44 struct onewire_bus sc_ow_bus;
45 device_t sc_ow_dev;
46
47 int sc_data;
48 int sc_dying;
49 };
50
51 int gpioow_match(device_t, cfdata_t, void *);
52 void gpioow_attach(device_t, device_t, void *);
53 int gpioow_detach(device_t, int);
54 int gpioow_activate(device_t, enum devact);
55
56 int gpioow_ow_reset(void *);
57 int gpioow_ow_bit(void *, int);
58
59 void gpioow_bb_rx(void *);
60 void gpioow_bb_tx(void *);
61 int gpioow_bb_get(void *);
62 void gpioow_bb_set(void *, int);
63
64 CFATTACH_DECL_NEW(gpioow, sizeof(struct gpioow_softc),
65 gpioow_match, gpioow_attach, gpioow_detach, gpioow_activate);
66
67 extern struct cfdriver gpioow_cd;
68
69 static const struct onewire_bbops gpioow_bbops = {
70 gpioow_bb_rx,
71 gpioow_bb_tx,
72 gpioow_bb_get,
73 gpioow_bb_set
74 };
75
76 int
77 gpioow_match(device_t parent, cfdata_t cf,
78 void *aux)
79 {
80 struct gpio_attach_args *ga = aux;
81
82 if (strcmp(ga->ga_dvname, cf->cf_name))
83 return 0;
84
85 if (ga->ga_offset == -1)
86 return 0;
87
88 /* Check that we have enough pins */
89 if (gpio_npins(ga->ga_mask) != GPIOOW_NPINS) {
90 aprint_debug("%s: invalid pin mask 0x%02x/n", cf->cf_name,
91 ga->ga_mask);
92 return 0;
93 }
94 return 1;
95 }
96
97 void
98 gpioow_attach(device_t parent, device_t self, void *aux)
99 {
100 struct gpioow_softc *sc = device_private(self);
101 struct gpio_attach_args *ga = aux;
102 struct onewirebus_attach_args oba;
103 int caps;
104
105 /* Map pins */
106 sc->sc_gpio = ga->ga_gpio;
107 sc->sc_map.pm_map = sc->__map;
108 if (gpio_pin_map(sc->sc_gpio, ga->ga_offset, ga->ga_mask,
109 &sc->sc_map)) {
110 aprint_error(": can't map pins\n");
111 return;
112 }
113
114 /* Configure data pin */
115 caps = gpio_pin_caps(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA);
116 if (!(caps & GPIO_PIN_OUTPUT)) {
117 aprint_error(": data pin is unable to drive output\n");
118 goto fail;
119 }
120 if (!(caps & GPIO_PIN_INPUT)) {
121 aprint_error(": data pin is unable to read input\n");
122 goto fail;
123 }
124 aprint_normal(": DATA[%d]", sc->sc_map.pm_map[GPIOOW_PIN_DATA]);
125 sc->sc_data = GPIO_PIN_OUTPUT;
126 if (caps & GPIO_PIN_OPENDRAIN) {
127 aprint_normal(" open-drain");
128 sc->sc_data |= GPIO_PIN_OPENDRAIN;
129 } else if ((caps & GPIO_PIN_PUSHPULL) && (caps & GPIO_PIN_TRISTATE)) {
130 aprint_normal(" push-pull tri-state");
131 sc->sc_data |= GPIO_PIN_PUSHPULL;
132 }
133 if (caps & GPIO_PIN_PULLUP) {
134 aprint_normal(" pull-up");
135 sc->sc_data |= GPIO_PIN_PULLUP;
136 }
137 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA, sc->sc_data);
138
139 aprint_normal("\n");
140
141 /* Attach 1-Wire bus */
142 sc->sc_ow_bus.bus_cookie = sc;
143 sc->sc_ow_bus.bus_reset = gpioow_ow_reset;
144 sc->sc_ow_bus.bus_bit = gpioow_ow_bit;
145
146 memset(&oba, 0, sizeof(oba));
147 oba.oba_bus = &sc->sc_ow_bus;
148 sc->sc_ow_dev = config_found(self, &oba, onewirebus_print);
149
150 return;
151
152 fail:
153 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
154 }
155
156 int
157 gpioow_detach(device_t self, int flags)
158 {
159 struct gpioow_softc *sc = device_private(self);
160 int rv = 0;
161
162 gpio_pin_unmap(sc->sc_gpio, &sc->sc_map);
163
164 if (sc->sc_ow_dev != NULL)
165 rv = config_detach(sc->sc_ow_dev, flags);
166
167 return rv;
168 }
169
170 int
171 gpioow_activate(device_t self, enum devact act)
172 {
173 struct gpioow_softc *sc = device_private(self);
174 int rv = 0;
175
176 switch (act) {
177 case DVACT_ACTIVATE:
178 return (EOPNOTSUPP);
179 case DVACT_DEACTIVATE:
180 sc->sc_dying = 1;
181 if (sc->sc_ow_dev != NULL)
182 rv = config_deactivate(sc->sc_ow_dev);
183 break;
184 }
185
186 return (rv);
187 }
188
189 int
190 gpioow_ow_reset(void *arg)
191 {
192 return (onewire_bb_reset(&gpioow_bbops, arg));
193 }
194
195 int
196 gpioow_ow_bit(void *arg, int value)
197 {
198 return (onewire_bb_bit(&gpioow_bbops, arg, value));
199 }
200
201 void
202 gpioow_bb_rx(void *arg)
203 {
204 struct gpioow_softc *sc = arg;
205 int data = sc->sc_data;
206
207 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
208 data |= GPIO_PIN_INPUT;
209 if (data & GPIO_PIN_PUSHPULL)
210 data |= GPIO_PIN_TRISTATE;
211 if (sc->sc_data != data) {
212 sc->sc_data = data;
213 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
214 sc->sc_data);
215 }
216 }
217
218 void
219 gpioow_bb_tx(void *arg)
220 {
221 struct gpioow_softc *sc = arg;
222 int data = sc->sc_data;
223
224 data &= ~(GPIO_PIN_INPUT | GPIO_PIN_OUTPUT | GPIO_PIN_TRISTATE);
225 data |= GPIO_PIN_OUTPUT;
226 if (sc->sc_data != data) {
227 sc->sc_data = data;
228 gpio_pin_ctl(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
229 sc->sc_data);
230 }
231 }
232
233 int
234 gpioow_bb_get(void *arg)
235 {
236 struct gpioow_softc *sc = arg;
237
238 return (gpio_pin_read(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA) ==
239 GPIO_PIN_HIGH ? 1 : 0);
240 }
241
242 void
243 gpioow_bb_set(void *arg, int value)
244 {
245 struct gpioow_softc *sc = arg;
246
247 gpio_pin_write(sc->sc_gpio, &sc->sc_map, GPIOOW_PIN_DATA,
248 value ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
249 }
250