xref: /freebsd/sys/arm/ti/am335x/am335x_gpio.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/resource.h>
37 #include <sys/rman.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 
41 #include <machine/bus.h>
42 #include <machine/cpu.h>
43 #include <machine/cpufunc.h>
44 #include <machine/resource.h>
45 #include <machine/intr.h>
46 #include <sys/gpio.h>
47 
48 #include <dev/fdt/fdt_common.h>
49 #include <dev/ofw/ofw_bus.h>
50 #include <dev/ofw/ofw_bus_subr.h>
51 
52 #include <arm/ti/ti_cpuid.h>
53 #include <arm/ti/ti_gpio.h>
54 #include <arm/ti/ti_pinmux.h>
55 
56 #include <arm/ti/am335x/am335x_scm_padconf.h>
57 
58 #include "ti_gpio_if.h"
59 
60 static struct ofw_compat_data compat_data[] = {
61 	{"ti,am335x-gpio",	1},
62 	/* Linux uses ti,omap4-gpio on am335x so we need to support it */
63 	{"ti,omap4-gpio",	1},
64 	{"ti,gpio",		1},
65 	{NULL,			0},
66 };
67 
68 static int
69 am335x_gpio_probe(device_t dev)
70 {
71 	if (ti_chip() != CHIP_AM335X)
72 		return (ENXIO);
73 
74 	if (!ofw_bus_status_okay(dev))
75 		return (ENXIO);
76 
77 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
78 		return (ENXIO);
79 
80 	device_set_desc(dev, "TI AM335x General Purpose I/O (GPIO)");
81 
82 	return (0);
83 }
84 
85 static int
86 am335x_gpio_set_flags(device_t dev, uint32_t gpio, uint32_t flags)
87 {
88 	unsigned int state = 0;
89 	struct ti_gpio_softc *sc = device_get_softc(dev);
90 
91 	if (flags & GPIO_PIN_OUTPUT) {
92 		if (flags & GPIO_PIN_PULLUP)
93 			state = PADCONF_OUTPUT_PULLUP;
94 		else
95 			state = PADCONF_OUTPUT;
96 	} else if (flags & GPIO_PIN_INPUT) {
97 		if (flags & GPIO_PIN_PULLUP)
98 			state = PADCONF_INPUT_PULLUP;
99 		else if (flags & GPIO_PIN_PULLDOWN)
100 			state = PADCONF_INPUT_PULLDOWN;
101 		else
102 			state = PADCONF_INPUT;
103 	}
104 	return ti_pinmux_padconf_set_gpiomode(sc->sc_bank*32 + gpio, state);
105 }
106 
107 static int
108 am335x_gpio_get_flags(device_t dev, uint32_t gpio, uint32_t *flags)
109 {
110 	unsigned int state;
111 	struct ti_gpio_softc *sc = device_get_softc(dev);
112 
113 	if (ti_pinmux_padconf_get_gpiomode(sc->sc_bank*32 + gpio, &state) != 0) {
114 		*flags = 0;
115 		return (EINVAL);
116 	} else {
117 		switch (state) {
118 			case PADCONF_OUTPUT:
119 				*flags = GPIO_PIN_OUTPUT;
120 				break;
121 			case PADCONF_OUTPUT_PULLUP:
122 				*flags = GPIO_PIN_OUTPUT | GPIO_PIN_PULLUP;
123 				break;
124 			case PADCONF_INPUT:
125 				*flags = GPIO_PIN_INPUT;
126 				break;
127 			case PADCONF_INPUT_PULLUP:
128 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLUP;
129 				break;
130 			case PADCONF_INPUT_PULLDOWN:
131 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLDOWN;
132 				break;
133 			default:
134 				*flags = 0;
135 				break;
136 		}
137 	}
138 
139 	return (0);
140 }
141 
142 static device_method_t am335x_gpio_methods[] = {
143 	/* bus interface */
144 	DEVMETHOD(device_probe, am335x_gpio_probe),
145 
146 	/* ti_gpio interface */
147 	DEVMETHOD(ti_gpio_set_flags, am335x_gpio_set_flags),
148 	DEVMETHOD(ti_gpio_get_flags, am335x_gpio_get_flags),
149 
150 	DEVMETHOD_END
151 };
152 
153 extern driver_t ti_gpio_driver;
154 static devclass_t am335x_gpio_devclass;
155 
156 DEFINE_CLASS_1(gpio, am335x_gpio_driver, am335x_gpio_methods,
157     sizeof(struct ti_gpio_softc), ti_gpio_driver);
158 DRIVER_MODULE(am335x_gpio, simplebus, am335x_gpio_driver, am335x_gpio_devclass,
159     0, 0);
160