2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Oleksandr Tymoshenko <gonzo@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/ioccom.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <dev/gpio/gpiobusvar.h>
45 #include "gpiobus_if.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)
84 struct gpioc_softc *sc;
85 struct make_dev_args devargs;
87 sc = device_get_softc(dev);
89 sc->sc_pdev = device_get_parent(dev);
90 sc->sc_unit = device_get_unit(dev);
91 make_dev_args_init(&devargs);
92 devargs.mda_devsw = &gpioc_cdevsw;
93 devargs.mda_uid = UID_ROOT;
94 devargs.mda_gid = GID_WHEEL;
95 devargs.mda_mode = 0600;
96 devargs.mda_si_drv1 = sc;
97 err = make_dev_s(&devargs, &sc->sc_ctl_dev, "gpioc%d", sc->sc_unit);
99 printf("Failed to create gpioc%d", sc->sc_unit);
107 gpioc_detach(device_t dev)
109 struct gpioc_softc *sc = device_get_softc(dev);
113 destroy_dev(sc->sc_ctl_dev);
115 if ((err = bus_generic_detach(dev)) != 0)
122 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
127 struct gpioc_softc *sc = cdev->si_drv1;
130 struct gpio_access_32 *a32;
131 struct gpio_config_32 *c32;
134 bus = GPIO_GET_BUS(sc->sc_pdev);
140 res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
141 bcopy(&max_pin, arg, sizeof(max_pin));
144 bcopy(arg, &pin, sizeof(pin));
145 dprintf("get config pin %d\n", pin.gp_pin);
146 res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
151 GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
152 GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name);
153 bcopy(&pin, arg, sizeof(pin));
156 bcopy(arg, &pin, sizeof(pin));
157 dprintf("set config pin %d\n", pin.gp_pin);
158 res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps);
160 res = gpio_check_flags(caps, pin.gp_flags);
162 res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
166 bcopy(arg, &req, sizeof(req));
167 res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
169 dprintf("read pin %d -> %d\n",
170 req.gp_pin, req.gp_value);
171 bcopy(&req, arg, sizeof(req));
174 bcopy(arg, &req, sizeof(req));
175 res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
177 dprintf("write pin %d -> %d\n",
178 req.gp_pin, req.gp_value);
181 bcopy(arg, &req, sizeof(req));
182 dprintf("toggle pin %d\n",
184 res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
187 bcopy(arg, &pin, sizeof(pin));
188 dprintf("set name on pin %d\n", pin.gp_pin);
189 res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin,
193 a32 = (struct gpio_access_32 *)arg;
194 res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin,
195 a32->clear_pins, a32->change_pins, &a32->orig_pins);
198 c32 = (struct gpio_config_32 *)arg;
199 res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin,
200 c32->num_pins, c32->pin_flags);
210 static device_method_t gpioc_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, gpioc_probe),
213 DEVMETHOD(device_attach, gpioc_attach),
214 DEVMETHOD(device_detach, gpioc_detach),
215 DEVMETHOD(device_shutdown, bus_generic_shutdown),
216 DEVMETHOD(device_suspend, bus_generic_suspend),
217 DEVMETHOD(device_resume, bus_generic_resume),
222 driver_t gpioc_driver = {
225 sizeof(struct gpioc_softc)
228 devclass_t gpioc_devclass;
230 DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
231 MODULE_VERSION(gpioc, 1);