xref: /freebsd/sys/arm/ti/omap4/omap4_gpio.c (revision 3fc36ee018bb836bd1796067cf4ef8683f166ebc)
1 /*-
2  * Copyright (c) 2011 Ben Gray <ben.r.gray@gmail.com>.
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  * 3. The name of the company nor the name of the author may be used to
15  *    endorse or promote products derived from this software without specific
16  *    prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BEN GRAY ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL BEN GRAY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/gpio.h>
38 
39 #include <machine/bus.h>
40 #include <machine/intr.h>
41 
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <arm/ti/ti_cpuid.h>
47 #include <arm/ti/ti_gpio.h>
48 #include <arm/ti/ti_pinmux.h>
49 
50 #include <arm/ti/omap4/omap4_scm_padconf.h>
51 
52 #include "ti_gpio_if.h"
53 
54 static struct ofw_compat_data compat_data[] = {
55 	{"ti,omap4-gpio",	1},
56 	{"ti,gpio",		1},
57 	{NULL,			0},
58 };
59 
60 static int
61 omap4_gpio_probe(device_t dev)
62 {
63 	if (ti_chip() != CHIP_OMAP_4)
64 		return (ENXIO);
65 
66 	if (!ofw_bus_status_okay(dev))
67 		return (ENXIO);
68 
69 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
70 		return (ENXIO);
71 
72 	device_set_desc(dev, "TI OMAP4 General Purpose I/O (GPIO)");
73 
74 	return (0);
75 }
76 
77 static int
78 omap4_gpio_set_flags(device_t dev, uint32_t gpio, uint32_t flags)
79 {
80 	unsigned int state = 0;
81 	struct ti_gpio_softc *sc;
82 
83 	sc = device_get_softc(dev);
84 	/* First the SCM driver needs to be told to put the pad into GPIO mode */
85 	if (flags & GPIO_PIN_OUTPUT)
86 		state = PADCONF_PIN_OUTPUT;
87 	else if (flags & GPIO_PIN_INPUT) {
88 		if (flags & GPIO_PIN_PULLUP)
89 			state = PADCONF_PIN_INPUT_PULLUP;
90 		else if (flags & GPIO_PIN_PULLDOWN)
91 			state = PADCONF_PIN_INPUT_PULLDOWN;
92 		else
93 			state = PADCONF_PIN_INPUT;
94 	}
95 	return ti_pinmux_padconf_set_gpiomode((sc->sc_bank-1)*32 + gpio, state);
96 }
97 
98 static int
99 omap4_gpio_get_flags(device_t dev, uint32_t gpio, uint32_t *flags)
100 {
101 	unsigned int state;
102 	struct ti_gpio_softc *sc;
103 
104 	sc = device_get_softc(dev);
105 
106 	/* Get the current pin state */
107 	if (ti_pinmux_padconf_get_gpiomode((sc->sc_bank-1)*32 + gpio, &state) != 0) {
108 		*flags = 0;
109 		return (EINVAL);
110 	} else {
111 		switch (state) {
112 			case PADCONF_PIN_OUTPUT:
113 				*flags = GPIO_PIN_OUTPUT;
114 				break;
115 			case PADCONF_PIN_INPUT:
116 				*flags = GPIO_PIN_INPUT;
117 				break;
118 			case PADCONF_PIN_INPUT_PULLUP:
119 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLUP;
120 				break;
121 			case PADCONF_PIN_INPUT_PULLDOWN:
122 				*flags = GPIO_PIN_INPUT | GPIO_PIN_PULLDOWN;
123 				break;
124 			default:
125 				*flags = 0;
126 				break;
127 		}
128 	}
129 
130 	return (0);
131 }
132 
133 static device_method_t omap4_gpio_methods[] = {
134 	/* bus interface */
135 	DEVMETHOD(device_probe, omap4_gpio_probe),
136 
137 	/* ti_gpio interface */
138 	DEVMETHOD(ti_gpio_set_flags, omap4_gpio_set_flags),
139 	DEVMETHOD(ti_gpio_get_flags, omap4_gpio_get_flags),
140 
141 	DEVMETHOD_END
142 };
143 
144 extern driver_t ti_gpio_driver;
145 static devclass_t omap4_gpio_devclass;
146 
147 DEFINE_CLASS_1(gpio, omap4_gpio_driver, omap4_gpio_methods,
148     sizeof(struct ti_gpio_softc), ti_gpio_driver);
149 DRIVER_MODULE(omap4_gpio, simplebus, omap4_gpio_driver, omap4_gpio_devclass,
150     0, 0);
151