2 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
36 #include <sys/ioccom.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/queue.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
49 #define dprintf printf
51 #define dprintf(x, arg...)
54 static int gpioc_probe(device_t dev);
55 static int gpioc_attach(device_t dev);
56 static int gpioc_detach(device_t dev);
58 static d_ioctl_t gpioc_ioctl;
60 static struct cdevsw gpioc_cdevsw = {
61 .d_version = D_VERSION,
62 .d_ioctl = gpioc_ioctl,
67 device_t sc_dev; /* gpiocX dev */
68 device_t sc_pdev; /* gpioX dev */
69 struct cdev *sc_ctl_dev; /* controller device */
74 gpioc_probe(device_t dev)
76 device_set_desc(dev, "GPIO controller");
81 gpioc_attach(device_t dev)
83 struct gpioc_softc *sc = device_get_softc(dev);
86 sc->sc_pdev = device_get_parent(dev);
87 sc->sc_unit = device_get_unit(dev);
88 sc->sc_ctl_dev = make_dev(&gpioc_cdevsw, sc->sc_unit,
89 UID_ROOT, GID_WHEEL, 0600, "gpioc%d", sc->sc_unit);
90 if (!sc->sc_ctl_dev) {
91 printf("Failed to create gpioc%d", sc->sc_unit);
94 sc->sc_ctl_dev->si_drv1 = sc;
100 gpioc_detach(device_t dev)
102 struct gpioc_softc *sc = device_get_softc(dev);
106 destroy_dev(sc->sc_ctl_dev);
108 if ((err = bus_generic_detach(dev)) != 0)
115 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
119 struct gpioc_softc *sc = cdev->si_drv1;
126 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
127 bcopy(&max_pin, arg, sizeof(max_pin));
130 bcopy(arg, &pin, sizeof(pin));
131 dprintf("get config pin %d\n", pin.gp_pin);
132 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
137 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
138 GPIO_PIN_GETNAME(sc->sc_pdev, pin.gp_pin, pin.gp_name);
139 bcopy(&pin, arg, sizeof(pin));
142 bcopy(arg, &pin, sizeof(pin));
143 dprintf("set config pin %d\n", pin.gp_pin);
144 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
148 bcopy(arg, &req, sizeof(req));
149 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
151 dprintf("read pin %d -> %d\n",
152 req.gp_pin, req.gp_value);
153 bcopy(&req, arg, sizeof(req));
156 bcopy(arg, &req, sizeof(req));
157 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
159 dprintf("write pin %d -> %d\n",
160 req.gp_pin, req.gp_value);
163 bcopy(arg, &req, sizeof(req));
164 dprintf("toggle pin %d\n",
166 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
176 static device_method_t gpioc_methods[] = {
177 /* Device interface */
178 DEVMETHOD(device_probe, gpioc_probe),
179 DEVMETHOD(device_attach, gpioc_attach),
180 DEVMETHOD(device_detach, gpioc_detach),
181 DEVMETHOD(device_shutdown, bus_generic_shutdown),
182 DEVMETHOD(device_suspend, bus_generic_suspend),
183 DEVMETHOD(device_resume, bus_generic_resume),
188 driver_t gpioc_driver = {
191 sizeof(struct gpioc_softc)
194 devclass_t gpioc_devclass;
196 DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
197 MODULE_VERSION(gpioc, 1);