xref: /freebsd/sys/arm/ti/omap4/omap4_gpio.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1b6c7dacfSAndrew Turner /*-
2b6c7dacfSAndrew Turner  * Copyright (c) 2011 Ben Gray <ben.r.gray@gmail.com>.
3b6c7dacfSAndrew Turner  * Copyright (c) 2014 Andrew Turner <andrew@FreeBSD.org>
4b6c7dacfSAndrew Turner  * All rights reserved.
5b6c7dacfSAndrew Turner  *
6b6c7dacfSAndrew Turner  * Redistribution and use in source and binary forms, with or without
7b6c7dacfSAndrew Turner  * modification, are permitted provided that the following conditions
8b6c7dacfSAndrew Turner  * are met:
9b6c7dacfSAndrew Turner  * 1. Redistributions of source code must retain the above copyright
10b6c7dacfSAndrew Turner  *    notice, this list of conditions and the following disclaimer.
11b6c7dacfSAndrew Turner  * 2. Redistributions in binary form must reproduce the above copyright
12b6c7dacfSAndrew Turner  *    notice, this list of conditions and the following disclaimer in the
13b6c7dacfSAndrew Turner  *    documentation and/or other materials provided with the distribution.
14b6c7dacfSAndrew Turner  * 3. The name of the company nor the name of the author may be used to
15b6c7dacfSAndrew Turner  *    endorse or promote products derived from this software without specific
16b6c7dacfSAndrew Turner  *    prior written permission.
17b6c7dacfSAndrew Turner  *
18b6c7dacfSAndrew Turner  * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
19b6c7dacfSAndrew Turner  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20b6c7dacfSAndrew Turner  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21b6c7dacfSAndrew Turner  * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22b6c7dacfSAndrew Turner  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23b6c7dacfSAndrew Turner  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24b6c7dacfSAndrew Turner  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25b6c7dacfSAndrew Turner  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26b6c7dacfSAndrew Turner  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27b6c7dacfSAndrew Turner  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28b6c7dacfSAndrew Turner  */
29*fdafd315SWarner Losh 
30b6c7dacfSAndrew Turner #include <sys/param.h>
31b6c7dacfSAndrew Turner #include <sys/systm.h>
32b6c7dacfSAndrew Turner #include <sys/kernel.h>
33b6c7dacfSAndrew Turner #include <sys/module.h>
34b6c7dacfSAndrew Turner #include <sys/bus.h>
35b6c7dacfSAndrew Turner #include <sys/gpio.h>
36b6c7dacfSAndrew Turner 
37b6c7dacfSAndrew Turner #include <machine/bus.h>
382df5562dSSvatopluk Kraus #include <machine/intr.h>
39b6c7dacfSAndrew Turner 
40b6c7dacfSAndrew Turner #include <dev/ofw/ofw_bus.h>
41b6c7dacfSAndrew Turner #include <dev/ofw/ofw_bus_subr.h>
42b6c7dacfSAndrew Turner 
43b6c7dacfSAndrew Turner #include <arm/ti/ti_cpuid.h>
44b6c7dacfSAndrew Turner #include <arm/ti/ti_gpio.h>
455b03aba6SOleksandr Tymoshenko #include <arm/ti/ti_pinmux.h>
46b6c7dacfSAndrew Turner 
47b6c7dacfSAndrew Turner #include <arm/ti/omap4/omap4_scm_padconf.h>
48b6c7dacfSAndrew Turner 
49b6c7dacfSAndrew Turner #include "ti_gpio_if.h"
50b6c7dacfSAndrew Turner 
51b6c7dacfSAndrew Turner static struct ofw_compat_data compat_data[] = {
52b6c7dacfSAndrew Turner 	{"ti,omap4-gpio",	1},
53b6c7dacfSAndrew Turner 	{"ti,gpio",		1},
54b6c7dacfSAndrew Turner 	{NULL,			0},
55b6c7dacfSAndrew Turner };
56b6c7dacfSAndrew Turner 
57b6c7dacfSAndrew Turner static int
omap4_gpio_probe(device_t dev)58b6c7dacfSAndrew Turner omap4_gpio_probe(device_t dev)
59b6c7dacfSAndrew Turner {
60b6c7dacfSAndrew Turner 
61b6c7dacfSAndrew Turner 	if (!ofw_bus_status_okay(dev))
62b6c7dacfSAndrew Turner 		return (ENXIO);
63b6c7dacfSAndrew Turner 
64b6c7dacfSAndrew Turner 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
65b6c7dacfSAndrew Turner 		return (ENXIO);
66cbfebd9aSOlivier Houchard 	if (ti_chip() != CHIP_OMAP_4)
67cbfebd9aSOlivier Houchard 		return (ENXIO);
68b6c7dacfSAndrew Turner 
6924e34f57SGavin Atkinson 	device_set_desc(dev, "TI OMAP4 General Purpose I/O (GPIO)");
70b6c7dacfSAndrew Turner 
71b6c7dacfSAndrew Turner 	return (0);
72b6c7dacfSAndrew Turner }
73b6c7dacfSAndrew Turner 
74b6c7dacfSAndrew Turner static int
omap4_gpio_set_flags(device_t dev,uint32_t gpio,uint32_t flags)75b6c7dacfSAndrew Turner omap4_gpio_set_flags(device_t dev, uint32_t gpio, uint32_t flags)
76b6c7dacfSAndrew Turner {
77b6c7dacfSAndrew Turner 	unsigned int state = 0;
785b03aba6SOleksandr Tymoshenko 	struct ti_gpio_softc *sc;
795b03aba6SOleksandr Tymoshenko 
805b03aba6SOleksandr Tymoshenko 	sc = device_get_softc(dev);
81b6c7dacfSAndrew Turner 	/* First the SCM driver needs to be told to put the pad into GPIO mode */
82b6c7dacfSAndrew Turner 	if (flags & GPIO_PIN_OUTPUT)
83b6c7dacfSAndrew Turner 		state = PADCONF_PIN_OUTPUT;
84b6c7dacfSAndrew Turner 	else if (flags & GPIO_PIN_INPUT) {
85b6c7dacfSAndrew Turner 		if (flags & GPIO_PIN_PULLUP)
86b6c7dacfSAndrew Turner 			state = PADCONF_PIN_INPUT_PULLUP;
87b6c7dacfSAndrew Turner 		else if (flags & GPIO_PIN_PULLDOWN)
88b6c7dacfSAndrew Turner 			state = PADCONF_PIN_INPUT_PULLDOWN;
89b6c7dacfSAndrew Turner 		else
90b6c7dacfSAndrew Turner 			state = PADCONF_PIN_INPUT;
91b6c7dacfSAndrew Turner 	}
925b03aba6SOleksandr Tymoshenko 	return ti_pinmux_padconf_set_gpiomode((sc->sc_bank-1)*32 + gpio, state);
93b6c7dacfSAndrew Turner }
94b6c7dacfSAndrew Turner 
95b6c7dacfSAndrew Turner static int
omap4_gpio_get_flags(device_t dev,uint32_t gpio,uint32_t * flags)96b6c7dacfSAndrew Turner omap4_gpio_get_flags(device_t dev, uint32_t gpio, uint32_t *flags)
97b6c7dacfSAndrew Turner {
98b6c7dacfSAndrew Turner 	unsigned int state;
995b03aba6SOleksandr Tymoshenko 	struct ti_gpio_softc *sc;
1005b03aba6SOleksandr Tymoshenko 
1015b03aba6SOleksandr Tymoshenko 	sc = device_get_softc(dev);
102b6c7dacfSAndrew Turner 
103b6c7dacfSAndrew Turner 	/* Get the current pin state */
1045b03aba6SOleksandr Tymoshenko 	if (ti_pinmux_padconf_get_gpiomode((sc->sc_bank-1)*32 + gpio, &state) != 0) {
105b6c7dacfSAndrew Turner 		*flags = 0;
106b6c7dacfSAndrew Turner 		return (EINVAL);
107b6c7dacfSAndrew Turner 	} else {
108b6c7dacfSAndrew Turner 		switch (state) {
109b6c7dacfSAndrew Turner 			case PADCONF_PIN_OUTPUT:
110b6c7dacfSAndrew Turner 				*flags = GPIO_PIN_OUTPUT;
111b6c7dacfSAndrew Turner 				break;
112b6c7dacfSAndrew Turner 			case PADCONF_PIN_INPUT:
113b6c7dacfSAndrew Turner 				*flags = GPIO_PIN_INPUT;
114b6c7dacfSAndrew Turner 				break;
115b6c7dacfSAndrew Turner 			case PADCONF_PIN_INPUT_PULLUP:
116b6c7dacfSAndrew Turner 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLUP;
117b6c7dacfSAndrew Turner 				break;
118b6c7dacfSAndrew Turner 			case PADCONF_PIN_INPUT_PULLDOWN:
119b6c7dacfSAndrew Turner 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLDOWN;
120b6c7dacfSAndrew Turner 				break;
121b6c7dacfSAndrew Turner 			default:
122b6c7dacfSAndrew Turner 				*flags = 0;
123b6c7dacfSAndrew Turner 				break;
124b6c7dacfSAndrew Turner 		}
125b6c7dacfSAndrew Turner 	}
126b6c7dacfSAndrew Turner 
127b6c7dacfSAndrew Turner 	return (0);
128b6c7dacfSAndrew Turner }
129b6c7dacfSAndrew Turner 
130b6c7dacfSAndrew Turner static device_method_t omap4_gpio_methods[] = {
131b6c7dacfSAndrew Turner 	/* bus interface */
132b6c7dacfSAndrew Turner 	DEVMETHOD(device_probe, omap4_gpio_probe),
133b6c7dacfSAndrew Turner 
134b6c7dacfSAndrew Turner 	/* ti_gpio interface */
135b6c7dacfSAndrew Turner 	DEVMETHOD(ti_gpio_set_flags, omap4_gpio_set_flags),
136b6c7dacfSAndrew Turner 	DEVMETHOD(ti_gpio_get_flags, omap4_gpio_get_flags),
137b6c7dacfSAndrew Turner 
138b6c7dacfSAndrew Turner 	DEVMETHOD_END
139b6c7dacfSAndrew Turner };
140b6c7dacfSAndrew Turner 
141b6c7dacfSAndrew Turner extern driver_t ti_gpio_driver;
142b6c7dacfSAndrew Turner 
143b6c7dacfSAndrew Turner DEFINE_CLASS_1(gpio, omap4_gpio_driver, omap4_gpio_methods,
144b6c7dacfSAndrew Turner     sizeof(struct ti_gpio_softc), ti_gpio_driver);
1458537e671SJohn Baldwin DRIVER_MODULE(omap4_gpio, simplebus, omap4_gpio_driver, 0, 0);
146