xref: /freebsd/sys/arm/ti/am335x/am335x_gpio.c (revision 315ee00f)
1 /*-
2  * Copyright (c) 2012 Damjan Marion <dmarion@FreeBSD.org>
3  * Copyright (c) 2014 Andrew Turner <andrew@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/bus.h>
34 #include <sys/resource.h>
35 #include <sys/rman.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <machine/intr.h>
42 #include <sys/gpio.h>
43 
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #include <arm/ti/ti_cpuid.h>
48 #include <arm/ti/ti_gpio.h>
49 #include <arm/ti/ti_pinmux.h>
50 
51 #include <arm/ti/am335x/am335x_scm_padconf.h>
52 
53 #include "ti_gpio_if.h"
54 
55 static struct ofw_compat_data compat_data[] = {
56 	{"ti,am335x-gpio",	1},
57 	/* Linux uses ti,omap4-gpio on am335x so we need to support it */
58 	{"ti,omap4-gpio",	1},
59 	{"ti,gpio",		1},
60 	{NULL,			0},
61 };
62 
63 static int
64 am335x_gpio_probe(device_t dev)
65 {
66 
67 	if (!ofw_bus_status_okay(dev))
68 		return (ENXIO);
69 
70 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
71 		return (ENXIO);
72 
73 	if (ti_chip() != CHIP_AM335X)
74 		return (ENXIO);
75 
76 	device_set_desc(dev, "TI AM335x General Purpose I/O (GPIO)");
77 
78 	return (0);
79 }
80 
81 static int
82 am335x_gpio_set_flags(device_t dev, uint32_t gpio, uint32_t flags)
83 {
84 	unsigned int state = 0;
85 	struct ti_gpio_softc *sc = device_get_softc(dev);
86 
87 	if (flags & GPIO_PIN_OUTPUT) {
88 		if (flags & GPIO_PIN_PULLUP)
89 			state = PADCONF_OUTPUT_PULLUP;
90 		else
91 			state = PADCONF_OUTPUT;
92 	} else if (flags & GPIO_PIN_INPUT) {
93 		if (flags & GPIO_PIN_PULLUP)
94 			state = PADCONF_INPUT_PULLUP;
95 		else if (flags & GPIO_PIN_PULLDOWN)
96 			state = PADCONF_INPUT_PULLDOWN;
97 		else
98 			state = PADCONF_INPUT;
99 	}
100 	return ti_pinmux_padconf_set_gpiomode(sc->sc_bank*32 + gpio, state);
101 }
102 
103 static int
104 am335x_gpio_get_flags(device_t dev, uint32_t gpio, uint32_t *flags)
105 {
106 	unsigned int state;
107 	struct ti_gpio_softc *sc = device_get_softc(dev);
108 
109 	if (ti_pinmux_padconf_get_gpiomode(sc->sc_bank*32 + gpio, &state) != 0) {
110 		*flags = 0;
111 		return (EINVAL);
112 	} else {
113 		switch (state) {
114 			case PADCONF_OUTPUT:
115 				*flags = GPIO_PIN_OUTPUT;
116 				break;
117 			case PADCONF_OUTPUT_PULLUP:
118 				*flags = GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP;
119 				break;
120 			case PADCONF_INPUT:
121 				*flags = GPIO_PIN_INPUT;
122 				break;
123 			case PADCONF_INPUT_PULLUP:
124 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLUP;
125 				break;
126 			case PADCONF_INPUT_PULLDOWN:
127 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLDOWN;
128 				break;
129 			default:
130 				*flags = 0;
131 				break;
132 		}
133 	}
134 
135 	return (0);
136 }
137 
138 static device_method_t am335x_gpio_methods[] = {
139 	/* bus interface */
140 	DEVMETHOD(device_probe, am335x_gpio_probe),
141 
142 	/* ti_gpio interface */
143 	DEVMETHOD(ti_gpio_set_flags, am335x_gpio_set_flags),
144 	DEVMETHOD(ti_gpio_get_flags, am335x_gpio_get_flags),
145 
146 	DEVMETHOD_END
147 };
148 
149 extern driver_t ti_gpio_driver;
150 
151 DEFINE_CLASS_1(gpio, am335x_gpio_driver, am335x_gpio_methods,
152     sizeof(struct ti_gpio_softc), ti_gpio_driver);
153 DRIVER_MODULE(am335x_gpio, simplebus, am335x_gpio_driver, 0, 0);
154 MODULE_DEPEND(am335x_gpio, ti_sysc, 1, 1, 1);
155