xref: /freebsd/sys/dev/gpio/gpioc.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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/bus.h>
33 #include <sys/conf.h>
34 #include <sys/gpio.h>
35 #include <sys/ioccom.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 
40 #include <dev/gpio/gpiobusvar.h>
41 
42 #include "gpio_if.h"
43 #include "gpiobus_if.h"
44 
45 #undef GPIOC_DEBUG
46 #ifdef GPIOC_DEBUG
47 #define dprintf printf
48 #else
49 #define dprintf(x, arg...)
50 #endif
51 
52 static int gpioc_probe(device_t dev);
53 static int gpioc_attach(device_t dev);
54 static int gpioc_detach(device_t dev);
55 
56 static d_ioctl_t	gpioc_ioctl;
57 
58 static struct cdevsw gpioc_cdevsw = {
59 	.d_version	= D_VERSION,
60 	.d_ioctl	= gpioc_ioctl,
61 	.d_name		= "gpioc",
62 };
63 
64 struct gpioc_softc {
65 	device_t	sc_dev;		/* gpiocX dev */
66 	device_t	sc_pdev;	/* gpioX dev */
67 	struct cdev	*sc_ctl_dev;	/* controller device */
68 	int		sc_unit;
69 };
70 
71 static int
72 gpioc_probe(device_t dev)
73 {
74 	device_set_desc(dev, "GPIO controller");
75 	return (0);
76 }
77 
78 static int
79 gpioc_attach(device_t dev)
80 {
81 	int err;
82 	struct gpioc_softc *sc;
83 	struct make_dev_args devargs;
84 
85 	sc = device_get_softc(dev);
86 	sc->sc_dev = dev;
87 	sc->sc_pdev = device_get_parent(dev);
88 	sc->sc_unit = device_get_unit(dev);
89 	make_dev_args_init(&devargs);
90 	devargs.mda_devsw = &gpioc_cdevsw;
91 	devargs.mda_uid = UID_ROOT;
92 	devargs.mda_gid = GID_WHEEL;
93 	devargs.mda_mode = 0600;
94 	devargs.mda_si_drv1 = sc;
95 	err = make_dev_s(&devargs, &sc->sc_ctl_dev, "gpioc%d", sc->sc_unit);
96 	if (err != 0) {
97 		printf("Failed to create gpioc%d", sc->sc_unit);
98 		return (ENXIO);
99 	}
100 
101 	return (0);
102 }
103 
104 static int
105 gpioc_detach(device_t dev)
106 {
107 	struct gpioc_softc *sc = device_get_softc(dev);
108 	int err;
109 
110 	if (sc->sc_ctl_dev)
111 		destroy_dev(sc->sc_ctl_dev);
112 
113 	if ((err = bus_generic_detach(dev)) != 0)
114 		return (err);
115 
116 	return (0);
117 }
118 
119 static int
120 gpioc_ioctl(struct cdev *cdev, u_long cmd, caddr_t arg, int fflag,
121     struct thread *td)
122 {
123 	device_t bus;
124 	int max_pin, res;
125 	struct gpioc_softc *sc = cdev->si_drv1;
126 	struct gpio_pin pin;
127 	struct gpio_req req;
128 	struct gpio_access_32 *a32;
129 	struct gpio_config_32 *c32;
130 	uint32_t caps;
131 
132 	bus = GPIO_GET_BUS(sc->sc_pdev);
133 	if (bus == NULL)
134 		return (EINVAL);
135 	switch (cmd) {
136 		case GPIOMAXPIN:
137 			max_pin = -1;
138 			res = GPIO_PIN_MAX(sc->sc_pdev, &max_pin);
139 			bcopy(&max_pin, arg, sizeof(max_pin));
140 			break;
141 		case GPIOGETCONFIG:
142 			bcopy(arg, &pin, sizeof(pin));
143 			dprintf("get config pin %d\n", pin.gp_pin);
144 			res = GPIO_PIN_GETFLAGS(sc->sc_pdev, pin.gp_pin,
145 			    &pin.gp_flags);
146 			/* Fail early */
147 			if (res)
148 				break;
149 			GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &pin.gp_caps);
150 			GPIOBUS_PIN_GETNAME(bus, pin.gp_pin, pin.gp_name);
151 			bcopy(&pin, arg, sizeof(pin));
152 			break;
153 		case GPIOSETCONFIG:
154 			bcopy(arg, &pin, sizeof(pin));
155 			dprintf("set config pin %d\n", pin.gp_pin);
156 			res = GPIO_PIN_GETCAPS(sc->sc_pdev, pin.gp_pin, &caps);
157 			if (res == 0)
158 				res = gpio_check_flags(caps, pin.gp_flags);
159 			if (res == 0)
160 				res = GPIO_PIN_SETFLAGS(sc->sc_pdev, pin.gp_pin,
161 				    pin.gp_flags);
162 			break;
163 		case GPIOGET:
164 			bcopy(arg, &req, sizeof(req));
165 			res = GPIO_PIN_GET(sc->sc_pdev, req.gp_pin,
166 			    &req.gp_value);
167 			dprintf("read pin %d -> %d\n",
168 			    req.gp_pin, req.gp_value);
169 			bcopy(&req, arg, sizeof(req));
170 			break;
171 		case GPIOSET:
172 			bcopy(arg, &req, sizeof(req));
173 			res = GPIO_PIN_SET(sc->sc_pdev, req.gp_pin,
174 			    req.gp_value);
175 			dprintf("write pin %d -> %d\n",
176 			    req.gp_pin, req.gp_value);
177 			break;
178 		case GPIOTOGGLE:
179 			bcopy(arg, &req, sizeof(req));
180 			dprintf("toggle pin %d\n",
181 			    req.gp_pin);
182 			res = GPIO_PIN_TOGGLE(sc->sc_pdev, req.gp_pin);
183 			break;
184 		case GPIOSETNAME:
185 			bcopy(arg, &pin, sizeof(pin));
186 			dprintf("set name on pin %d\n", pin.gp_pin);
187 			res = GPIOBUS_PIN_SETNAME(bus, pin.gp_pin,
188 			    pin.gp_name);
189 			break;
190 		case GPIOACCESS32:
191 			a32 = (struct gpio_access_32 *)arg;
192 			res = GPIO_PIN_ACCESS_32(sc->sc_pdev, a32->first_pin,
193 			    a32->clear_pins, a32->orig_pins, &a32->orig_pins);
194 			break;
195 		case GPIOCONFIG32:
196 			c32 = (struct gpio_config_32 *)arg;
197 			res = GPIO_PIN_CONFIG_32(sc->sc_pdev, c32->first_pin,
198 			    c32->num_pins, c32->pin_flags);
199 			break;
200 		default:
201 			return (ENOTTY);
202 			break;
203 	}
204 
205 	return (res);
206 }
207 
208 static device_method_t gpioc_methods[] = {
209 	/* Device interface */
210 	DEVMETHOD(device_probe,		gpioc_probe),
211 	DEVMETHOD(device_attach,	gpioc_attach),
212 	DEVMETHOD(device_detach,	gpioc_detach),
213 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
214 	DEVMETHOD(device_suspend,	bus_generic_suspend),
215 	DEVMETHOD(device_resume,	bus_generic_resume),
216 
217 	DEVMETHOD_END
218 };
219 
220 driver_t gpioc_driver = {
221 	"gpioc",
222 	gpioc_methods,
223 	sizeof(struct gpioc_softc)
224 };
225 
226 devclass_t	gpioc_devclass;
227 
228 DRIVER_MODULE(gpioc, gpio, gpioc_driver, gpioc_devclass, 0, 0);
229 MODULE_VERSION(gpioc, 1);
230