]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - sys/dev/gpio/gpioc.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / sys / dev / gpio / gpioc.c
1 /*-
2  * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/types.h>
33
34 #include <sys/bus.h>
35 #include <sys/conf.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>
43
44 #include <sys/gpio.h>
45 #include "gpio_if.h"
46
47 #undef GPIOC_DEBUG
48 #ifdef GPIOC_DEBUG
49 #define dprintf printf
50 #else
51 #define dprintf(x, arg...)
52 #endif
53
54 static int gpioc_probe(device_t dev);
55 static int gpioc_attach(device_t dev);
56 static int gpioc_detach(device_t dev);
57
58 static d_ioctl_t        gpioc_ioctl;
59
60 static struct cdevsw gpioc_cdevsw = {
61         .d_version      = D_VERSION,
62         .d_ioctl        = gpioc_ioctl,
63         .d_name         = "gpioc",
64 #if __FreeBSD_version >= 800039
65         .d_flags        = D_PSEUDO | D_NEEDMINOR
66 #endif
67 };
68
69 struct gpioc_softc {
70         device_t        sc_dev;         /* gpiocX dev */
71         device_t        sc_pdev;        /* gpioX dev */
72         struct cdev     *sc_ctl_dev;    /* controller device */
73         int             sc_unit;
74 };
75
76 static int
77 gpioc_probe(device_t dev)
78 {
79         device_set_desc(dev, "GPIO controller");
80         return (0);
81 }
82
83 static int
84 gpioc_attach(device_t dev)
85 {
86         struct gpioc_softc *sc = device_get_softc(dev);
87
88         sc->sc_dev = dev;
89         sc->sc_pdev = device_get_parent(dev);
90         sc->sc_unit = device_get_unit(dev);
91         sc->sc_ctl_dev = make_dev(&gpioc_cdevsw, sc->sc_unit,
92             UID_ROOT, GID_WHEEL, 0600, "gpioc%d", sc->sc_unit);
93         if (!sc->sc_ctl_dev) {
94                 printf("Failed to create gpioc%d", sc->sc_unit);
95                 return (ENXIO);
96         }
97         sc->sc_ctl_dev->si_drv1 = sc;
98
99         return (0);
100 }
101
102 static int
103 gpioc_detach(device_t dev)
104 {
105         struct gpioc_softc *sc = device_get_softc(dev);
106         int err;
107
108         if (sc->sc_ctl_dev);
109                 destroy_dev(sc->sc_ctl_dev);
110
111         if ((err = bus_generic_detach(dev)) != 0)
112                 return (err);
113
114         return (0);
115 }
116
117 static int 
118 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag, 
119     struct thread *td)
120 {
121         int max_pin, res;
122         struct gpioc_softc *sc = cdev->si_drv1;
123         struct gpio_pin pin;
124         struct gpio_req req;
125
126         switch (cmd) {
127                 case GPIOMAXPIN:
128                         max_pin = -1;
129                         res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
130                         bcopy(&max_pin, arg, sizeof(max_pin));
131                         break;
132                 case GPIOGETCONFIG:
133                         bcopy(arg, &pin, sizeof(pin));
134                         dprintf("get config pin %d\n", pin.gp_pin);
135                         res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
136                             &pin.gp_flags);
137                         /* Fail early */
138                         if (res)
139                                 break;
140                         GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
141                         GPIO_PIN_GETNAME(sc->sc_pdev, pin.gp_pin, pin.gp_name);
142                         bcopy(&pin, arg, sizeof(pin));
143                         break;
144                 case GPIOSETCONFIG:
145                         bcopy(arg, &pin, sizeof(pin));
146                         dprintf("set config pin %d\n", pin.gp_pin);
147                         res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
148                             pin.gp_flags);
149                         break;
150                 case GPIOGET:
151                         bcopy(arg, &req, sizeof(req));
152                         res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
153                             &req.gp_value);
154                         dprintf("read pin %d -> %d\n", 
155                             req.gp_pin, req.gp_value);
156                         bcopy(&req, arg, sizeof(req));
157                         break;
158                 case GPIOSET:
159                         bcopy(arg, &req, sizeof(req));
160                         res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin, 
161                             req.gp_value);
162                         dprintf("write pin %d -> %d\n", 
163                             req.gp_pin, req.gp_value);
164                         break;
165                 case GPIOTOGGLE:
166                         bcopy(arg, &req, sizeof(req));
167                         dprintf("toggle pin %d\n", 
168                             req.gp_pin);
169                         res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
170                         break;
171                 default:
172                         return (ENOTTY);
173                         break;
174         }
175
176         return (res);
177 }
178
179 static device_method_t gpioc_methods[] = {
180         /* Device interface */
181         DEVMETHOD(device_probe,         gpioc_probe),
182         DEVMETHOD(device_attach,        gpioc_attach),
183         DEVMETHOD(device_detach,        gpioc_detach),
184         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
185         DEVMETHOD(device_suspend,       bus_generic_suspend),
186         DEVMETHOD(device_resume,        bus_generic_resume),
187
188         { 0, 0 }
189 };
190
191 driver_t gpioc_driver = {
192         "gpioc",
193         gpioc_methods,
194         sizeof(struct gpioc_softc)
195 };
196
197 devclass_t      gpioc_devclass;
198
199 DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
200 MODULE_VERSION(gpioc, 1);