]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_gpio.c
MFV r333789: libpcap 1.9.0 (pre-release)
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_gpio.c
1 /*-
2  * Copyright 2013-2015 John Wehle <john@feith.com>
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 /*
28  * Amlogic aml8726 GPIO driver.
29  *
30  * Note: The OEN register is active * low *.  Setting a bit to zero
31  * enables the output driver, setting a bit to one disables the driver.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/resource.h>
46 #include <sys/rman.h>
47
48 #include <sys/gpio.h>
49
50 #include <machine/bus.h>
51
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54
55 #include <dev/gpio/gpiobusvar.h>
56
57 #include "gpio_if.h"
58
59
60 struct aml8726_gpio_softc {
61         device_t        dev;
62         struct resource *res[3];
63         struct mtx      mtx;
64         uint32_t        npins;
65         device_t        busdev;
66 };
67
68 static struct resource_spec aml8726_gpio_spec[] = {
69         { SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE }, /* oen */
70         { SYS_RES_MEMORY, 1, RF_ACTIVE | RF_SHAREABLE }, /* output */
71         { SYS_RES_MEMORY, 2, RF_ACTIVE }, /* input */
72         { -1, 0 }
73 };
74
75 #define AML_GPIO_LOCK(sc)               mtx_lock(&(sc)->mtx)
76 #define AML_GPIO_UNLOCK(sc)             mtx_unlock(&(sc)->mtx)
77 #define AML_GPIO_LOCK_INIT(sc)          \
78     mtx_init(&(sc)->mtx, device_get_nameunit((sc)->dev),        \
79     "gpio", MTX_DEF)
80 #define AML_GPIO_LOCK_DESTROY(sc)       mtx_destroy(&(sc)->mtx);
81
82 #define AML_GPIO_OE_N_REG               0
83 #define AML_GPIO_OUT_REG                1
84 #define AML_GPIO_IN_REG                 2
85
86 #define CSR_WRITE_4(sc, reg, val)       bus_write_4((sc)->res[reg], 0, (val))
87 #define CSR_READ_4(sc, reg)             bus_read_4((sc)->res[reg], 0)
88
89 static int
90 aml8726_gpio_probe(device_t dev)
91 {
92
93         if (!ofw_bus_status_okay(dev))
94                 return (ENXIO);
95
96         if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-gpio"))
97                 return (ENXIO);
98
99         device_set_desc(dev, "Amlogic aml8726 GPIO");
100
101         return (BUS_PROBE_DEFAULT);
102 }
103
104 static int
105 aml8726_gpio_attach(device_t dev)
106 {
107         struct aml8726_gpio_softc *sc = device_get_softc(dev);
108         phandle_t node;
109         pcell_t prop;
110
111         sc->dev = dev;
112
113         node = ofw_bus_get_node(dev);
114
115         if (OF_getencprop(node, "pin-count",
116             &prop, sizeof(prop)) <= 0) {
117                 device_printf(dev, "missing pin-count attribute in FDT\n");
118                 return (ENXIO);
119         }
120         sc->npins = prop;
121
122         if (sc->npins > 32)
123                 return (ENXIO);
124
125         if (bus_alloc_resources(dev, aml8726_gpio_spec, sc->res)) {
126                 device_printf(dev, "can not allocate resources for device\n");
127                 return (ENXIO);
128         }
129
130         /*
131          * The GPIOAO OUT bits occupy the upper word of the OEN register.
132          */
133         if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
134           if (sc->npins > 16) {
135                 device_printf(dev,
136                     "too many pins for overlapping OEN and OUT\n");
137                 bus_release_resources(dev, aml8726_gpio_spec, sc->res);
138                 return (ENXIO);
139                 }
140
141         AML_GPIO_LOCK_INIT(sc);
142
143         sc->busdev = gpiobus_attach_bus(dev);
144         if (sc->busdev == NULL) {
145                 AML_GPIO_LOCK_DESTROY(sc);
146                 bus_release_resources(dev, aml8726_gpio_spec, sc->res);
147                 return (ENXIO);
148         }
149
150         return (0);
151 }
152
153 static int
154 aml8726_gpio_detach(device_t dev)
155 {
156         struct aml8726_gpio_softc *sc = device_get_softc(dev);
157
158         gpiobus_detach_bus(dev);
159
160         AML_GPIO_LOCK_DESTROY(sc);
161
162         bus_release_resources(dev, aml8726_gpio_spec, sc->res);
163
164         return (0);
165 }
166
167 static device_t
168 aml8726_gpio_get_bus(device_t dev)
169 {
170         struct aml8726_gpio_softc *sc = device_get_softc(dev);
171
172         return (sc->busdev);
173 }
174
175 static int
176 aml8726_gpio_pin_max(device_t dev, int *maxpin)
177 {
178         struct aml8726_gpio_softc *sc = device_get_softc(dev);
179
180         *maxpin = (int)sc->npins;
181
182         return (0);
183 }
184
185 /* Get a specific pin's capabilities. */
186 static int
187 aml8726_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
188 {
189         struct aml8726_gpio_softc *sc = device_get_softc(dev);
190
191         if (pin >= sc->npins)
192                 return (EINVAL);
193
194         *caps = (GPIO_PIN_INPUT | GPIO_PIN_OUTPUT);
195
196         return (0);
197 }
198
199 /* Get a specific pin's name. */
200 static int
201 aml8726_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
202 {
203         struct aml8726_gpio_softc *sc = device_get_softc(dev);
204
205         if (pin >= sc->npins)
206                 return (EINVAL);
207
208         snprintf(name, GPIOMAXNAME, "%s.%u", ofw_bus_get_name(dev), pin);
209
210         return (0);
211 }
212
213 /* Get a specific pin's current in/out state. */
214 static int
215 aml8726_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
216 {
217         struct aml8726_gpio_softc *sc = device_get_softc(dev);
218         uint32_t mask = 1U << pin;
219
220         if (pin >= sc->npins)
221                 return (EINVAL);
222
223         if ((CSR_READ_4(sc, AML_GPIO_OE_N_REG) & mask) == 0) {
224                 /* output */
225                 *flags = GPIO_PIN_OUTPUT;
226         } else
227                 /* input */
228                 *flags = GPIO_PIN_INPUT;
229
230         return (0);
231 }
232
233 /* Set a specific pin's in/out state. */
234 static int
235 aml8726_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
236 {
237         struct aml8726_gpio_softc *sc = device_get_softc(dev);
238         uint32_t mask = 1U << pin;
239
240         if (pin >= sc->npins)
241                 return (EINVAL);
242
243         AML_GPIO_LOCK(sc);
244
245         if ((flags & GPIO_PIN_OUTPUT) != 0) {
246                 /* Output.  Turn on driver.  */
247                 CSR_WRITE_4(sc, AML_GPIO_OE_N_REG,
248                     (CSR_READ_4(sc, AML_GPIO_OE_N_REG) & ~mask));
249         } else {
250                 /* Input.  Turn off driver. */
251                 CSR_WRITE_4(sc, AML_GPIO_OE_N_REG,
252                     (CSR_READ_4(sc, AML_GPIO_OE_N_REG) | mask));
253         }
254                 
255         AML_GPIO_UNLOCK(sc);
256
257         return (0);
258 }
259
260 /* Set a specific output pin's value. */
261 static int
262 aml8726_gpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
263 {
264         struct aml8726_gpio_softc *sc = device_get_softc(dev);
265         uint32_t mask;
266
267         if (pin >= sc->npins || value > 1)
268                 return (EINVAL);
269
270         /*
271          * The GPIOAO OUT bits occupy the upper word of the OEN register.
272          */
273         if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
274                 pin += 16;
275
276         mask = 1U << pin;
277
278         AML_GPIO_LOCK(sc);
279
280         CSR_WRITE_4(sc, AML_GPIO_OUT_REG,
281             ((CSR_READ_4(sc, AML_GPIO_OUT_REG) & ~mask) | (value << pin)));
282
283         AML_GPIO_UNLOCK(sc);
284
285         return (0);
286 }
287
288 /* Get a specific pin's input value. */
289 static int
290 aml8726_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
291 {
292         struct aml8726_gpio_softc *sc = device_get_softc(dev);
293         uint32_t mask = 1U << pin;
294
295         if (pin >= sc->npins)
296                 return (EINVAL);
297
298         *value = (CSR_READ_4(sc, AML_GPIO_IN_REG) & mask) ? 1 : 0;
299
300         return (0);
301 }
302
303 /* Toggle a pin's output value. */
304 static int
305 aml8726_gpio_pin_toggle(device_t dev, uint32_t pin)
306 {
307         struct aml8726_gpio_softc *sc = device_get_softc(dev);
308         uint32_t mask;
309
310         if (pin >= sc->npins)
311                 return (EINVAL);
312
313         /*
314          * The GPIOAO OUT bits occupy the upper word of the OEN register.
315          */
316         if (rman_get_start(sc->res[1]) == rman_get_start(sc->res[0]))
317                 pin += 16;
318
319         mask = 1U << pin;
320
321         AML_GPIO_LOCK(sc);
322
323         CSR_WRITE_4(sc, AML_GPIO_OUT_REG,
324             CSR_READ_4(sc, AML_GPIO_OUT_REG) ^ mask);
325
326         AML_GPIO_UNLOCK(sc);
327
328         return (0);
329 }
330
331 static phandle_t
332 aml8726_gpio_get_node(device_t bus, device_t dev)
333 {
334
335         /* We only have one child, the GPIO bus, which needs our own node. */
336         return (ofw_bus_get_node(bus));
337 }
338
339 static device_method_t aml8726_gpio_methods[] = {
340         /* Device interface */
341         DEVMETHOD(device_probe,         aml8726_gpio_probe),
342         DEVMETHOD(device_attach,        aml8726_gpio_attach),
343         DEVMETHOD(device_detach,        aml8726_gpio_detach),
344
345         /* GPIO interface */
346         DEVMETHOD(gpio_get_bus,         aml8726_gpio_get_bus),
347         DEVMETHOD(gpio_pin_max,         aml8726_gpio_pin_max),
348         DEVMETHOD(gpio_pin_getname,     aml8726_gpio_pin_getname),
349         DEVMETHOD(gpio_pin_getflags,    aml8726_gpio_pin_getflags),
350         DEVMETHOD(gpio_pin_getcaps,     aml8726_gpio_pin_getcaps),
351         DEVMETHOD(gpio_pin_setflags,    aml8726_gpio_pin_setflags),
352         DEVMETHOD(gpio_pin_get,         aml8726_gpio_pin_get),
353         DEVMETHOD(gpio_pin_set,         aml8726_gpio_pin_set),
354         DEVMETHOD(gpio_pin_toggle,      aml8726_gpio_pin_toggle),
355
356         /* ofw_bus interface */
357         DEVMETHOD(ofw_bus_get_node,     aml8726_gpio_get_node),
358
359         DEVMETHOD_END
360 };
361
362 static driver_t aml8726_gpio_driver = {
363         "gpio",
364         aml8726_gpio_methods,
365         sizeof(struct aml8726_gpio_softc),
366 };
367
368 static devclass_t aml8726_gpio_devclass;
369
370 DRIVER_MODULE(aml8726_gpio, simplebus, aml8726_gpio_driver,
371     aml8726_gpio_devclass, 0, 0);