gpio.c revision 1.11 1 /* $NetBSD: gpio.c,v 1.11 2006/11/16 01:32:50 christos Exp $ */
2 /* $OpenBSD: gpio.c,v 1.6 2006/01/14 12:33:49 grange Exp $ */
3
4 /*
5 * Copyright (c) 2004, 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: gpio.c,v 1.11 2006/11/16 01:32:50 christos Exp $");
22
23 /*
24 * General Purpose Input/Output framework.
25 */
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/conf.h>
30 #include <sys/device.h>
31 #include <sys/ioctl.h>
32 #include <sys/gpio.h>
33 #include <sys/vnode.h>
34
35 #include <dev/gpio/gpiovar.h>
36
37 #include "locators.h"
38
39 struct gpio_softc {
40 struct device sc_dev;
41
42 gpio_chipset_tag_t sc_gc; /* our GPIO controller */
43 gpio_pin_t *sc_pins; /* pins array */
44 int sc_npins; /* total number of pins */
45
46 int sc_opened;
47 int sc_dying;
48 };
49
50 int gpio_match(struct device *, struct cfdata *, void *);
51 void gpio_attach(struct device *, struct device *, void *);
52 int gpio_detach(struct device *, int);
53 int gpio_activate(struct device *, enum devact);
54 int gpio_search(struct device *, struct cfdata *, const int *, void *);
55 int gpio_print(void *, const char *);
56
57 CFATTACH_DECL(gpio, sizeof(struct gpio_softc),
58 gpio_match, gpio_attach, gpio_detach, gpio_activate);
59
60 dev_type_open(gpioopen);
61 dev_type_close(gpioclose);
62 dev_type_ioctl(gpioioctl);
63
64 const struct cdevsw gpio_cdevsw = {
65 gpioopen, gpioclose, noread, nowrite, gpioioctl,
66 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
67 };
68
69 extern struct cfdriver gpio_cd;
70
71 int
72 gpio_match(struct device *parent, struct cfdata *cf,
73 void *aux)
74 {
75
76 return (1);
77 }
78
79 void
80 gpio_attach(struct device *parent, struct device *self, void *aux)
81 {
82 struct gpio_softc *sc = device_private(self);
83 struct gpiobus_attach_args *gba = aux;
84
85 sc->sc_gc = gba->gba_gc;
86 sc->sc_pins = gba->gba_pins;
87 sc->sc_npins = gba->gba_npins;
88
89 printf(": %d pins\n", sc->sc_npins);
90
91 /*
92 * Attach all devices that can be connected to the GPIO pins
93 * described in the kernel configuration file.
94 */
95 config_search_ia(gpio_search, self, "gpio", sc);
96 }
97
98 int
99 gpio_detach(struct device *self, int flags)
100 {
101 #if 0
102 int maj, mn;
103
104 /* Locate the major number */
105 for (maj = 0; maj < nchrdev; maj++)
106 if (cdevsw[maj].d_open == gpioopen)
107 break;
108
109 /* Nuke the vnodes for any open instances (calls close) */
110 mn = device_unit(self);
111 vdevgone(maj, mn, mn, VCHR);
112 #endif
113
114 return (0);
115 }
116
117 int
118 gpio_activate(struct device *self, enum devact act)
119 {
120 struct gpio_softc *sc = device_private(self);
121
122 switch (act) {
123 case DVACT_ACTIVATE:
124 return (EOPNOTSUPP);
125 case DVACT_DEACTIVATE:
126 sc->sc_dying = 1;
127 break;
128 }
129
130 return (0);
131 }
132
133 int
134 gpio_search(struct device *parent, struct cfdata *cf,
135 const int *ldesc, void *aux)
136 {
137 struct gpio_attach_args ga;
138
139 ga.ga_gpio = aux;
140 ga.ga_offset = cf->cf_loc[GPIOCF_OFFSET];
141 ga.ga_mask = cf->cf_loc[GPIOCF_MASK];
142
143 if (config_match(parent, cf, &ga) > 0)
144 config_attach(parent, cf, &ga, gpio_print);
145
146 return (0);
147 }
148
149 int
150 gpio_print(void *aux, const char *pnp)
151 {
152 struct gpio_attach_args *ga = aux;
153 int i;
154
155 printf(" pins");
156 for (i = 0; i < 32; i++)
157 if (ga->ga_mask & (1 << i))
158 printf(" %d", ga->ga_offset + i);
159
160 return (UNCONF);
161 }
162
163 int
164 gpiobus_print(void *aux, const char *pnp)
165 {
166 #if 0
167 struct gpiobus_attach_args *gba = aux;
168 #endif
169 if (pnp != NULL)
170 printf("%s at %s", "gpiobus", pnp);
171
172 return (UNCONF);
173 }
174
175 int
176 gpio_pin_map(void *gpio, int offset, u_int32_t mask, struct gpio_pinmap *map)
177 {
178 struct gpio_softc *sc = gpio;
179 int npins, pin, i;
180
181 npins = gpio_npins(mask);
182 if (npins > sc->sc_npins)
183 return (1);
184
185 for (npins = 0, i = 0; i < 32; i++)
186 if (mask & (1 << i)) {
187 pin = offset + i;
188 if (pin < 0 || pin >= sc->sc_npins)
189 return (1);
190 if (sc->sc_pins[pin].pin_mapped)
191 return (1);
192 sc->sc_pins[pin].pin_mapped = 1;
193 map->pm_map[npins++] = pin;
194 }
195 map->pm_size = npins;
196
197 return (0);
198 }
199
200 void
201 gpio_pin_unmap(void *gpio, struct gpio_pinmap *map)
202 {
203 struct gpio_softc *sc = gpio;
204 int pin, i;
205
206 for (i = 0; i < map->pm_size; i++) {
207 pin = map->pm_map[i];
208 sc->sc_pins[pin].pin_mapped = 0;
209 }
210 }
211
212 int
213 gpio_pin_read(void *gpio, struct gpio_pinmap *map, int pin)
214 {
215 struct gpio_softc *sc = gpio;
216
217 return (gpiobus_pin_read(sc->sc_gc, map->pm_map[pin]));
218 }
219
220 void
221 gpio_pin_write(void *gpio, struct gpio_pinmap *map, int pin, int value)
222 {
223 struct gpio_softc *sc = gpio;
224
225 return (gpiobus_pin_write(sc->sc_gc, map->pm_map[pin], value));
226 }
227
228 void
229 gpio_pin_ctl(void *gpio, struct gpio_pinmap *map, int pin, int flags)
230 {
231 struct gpio_softc *sc = gpio;
232
233 return (gpiobus_pin_ctl(sc->sc_gc, map->pm_map[pin], flags));
234 }
235
236 int
237 gpio_pin_caps(void *gpio, struct gpio_pinmap *map, int pin)
238 {
239 struct gpio_softc *sc = gpio;
240
241 return (sc->sc_pins[map->pm_map[pin]].pin_caps);
242 }
243
244 int
245 gpio_npins(u_int32_t mask)
246 {
247 int npins, i;
248
249 for (npins = 0, i = 0; i < 32; i++)
250 if (mask & (1 << i))
251 npins++;
252
253 return (npins);
254 }
255
256 int
257 gpioopen(dev_t dev, int flag, int mode,
258 struct lwp *l)
259 {
260 struct gpio_softc *sc;
261
262 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
263 if (sc == NULL)
264 return (ENXIO);
265
266 if (sc->sc_opened)
267 return (EBUSY);
268 sc->sc_opened = 1;
269
270 return (0);
271 }
272
273 int
274 gpioclose(dev_t dev, int flag, int mode,
275 struct lwp *l)
276 {
277 struct gpio_softc *sc;
278
279 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
280 sc->sc_opened = 0;
281
282 return (0);
283 }
284
285 int
286 gpioioctl(dev_t dev, u_long cmd, caddr_t data, int flag,
287 struct lwp *l)
288 {
289 struct gpio_softc *sc;
290 gpio_chipset_tag_t gc;
291 struct gpio_info *info;
292 struct gpio_pin_op *op;
293 struct gpio_pin_ctl *ctl;
294 int pin, value, flags;
295
296 sc = (struct gpio_softc *)device_lookup(&gpio_cd, minor(dev));
297 gc = sc->sc_gc;
298
299 switch (cmd) {
300 case GPIOINFO:
301 info = (struct gpio_info *)data;
302
303 info->gpio_npins = sc->sc_npins;
304 break;
305 case GPIOPINREAD:
306 op = (struct gpio_pin_op *)data;
307
308 pin = op->gp_pin;
309 if (pin < 0 || pin >= sc->sc_npins)
310 return (EINVAL);
311
312 /* return read value */
313 op->gp_value = gpiobus_pin_read(gc, pin);
314 break;
315 case GPIOPINWRITE:
316 op = (struct gpio_pin_op *)data;
317
318 pin = op->gp_pin;
319 if (pin < 0 || pin >= sc->sc_npins)
320 return (EINVAL);
321 if (sc->sc_pins[pin].pin_mapped)
322 return (EBUSY);
323
324 value = op->gp_value;
325 if (value != GPIO_PIN_LOW && value != GPIO_PIN_HIGH)
326 return (EINVAL);
327
328 gpiobus_pin_write(gc, pin, value);
329 /* return old value */
330 op->gp_value = sc->sc_pins[pin].pin_state;
331 /* update current value */
332 sc->sc_pins[pin].pin_state = value;
333 break;
334 case GPIOPINTOGGLE:
335 op = (struct gpio_pin_op *)data;
336
337 pin = op->gp_pin;
338 if (pin < 0 || pin >= sc->sc_npins)
339 return (EINVAL);
340 if (sc->sc_pins[pin].pin_mapped)
341 return (EBUSY);
342
343 value = (sc->sc_pins[pin].pin_state == GPIO_PIN_LOW ?
344 GPIO_PIN_HIGH : GPIO_PIN_LOW);
345 gpiobus_pin_write(gc, pin, value);
346 /* return old value */
347 op->gp_value = sc->sc_pins[pin].pin_state;
348 /* update current value */
349 sc->sc_pins[pin].pin_state = value;
350 break;
351 case GPIOPINCTL:
352 ctl = (struct gpio_pin_ctl *)data;
353
354 pin = ctl->gp_pin;
355 if (pin < 0 || pin >= sc->sc_npins)
356 return (EINVAL);
357 if (sc->sc_pins[pin].pin_mapped)
358 return (EBUSY);
359
360 flags = ctl->gp_flags;
361 /* check that the controller supports all requested flags */
362 if ((flags & sc->sc_pins[pin].pin_caps) != flags)
363 return (ENODEV);
364
365 ctl->gp_caps = sc->sc_pins[pin].pin_caps;
366 /* return old value */
367 ctl->gp_flags = sc->sc_pins[pin].pin_flags;
368 if (flags > 0) {
369 gpiobus_pin_ctl(gc, pin, flags);
370 /* update current value */
371 sc->sc_pins[pin].pin_flags = flags;
372 }
373 break;
374 default:
375 return (ENOTTY);
376 }
377
378 return (0);
379 }
380