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