Home | History | Annotate | Line # | Download | only in ti
ti_gpio.c revision 1.1
      1 /* $NetBSD: ti_gpio.c,v 1.1 2019/10/28 22:21:35 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ti_gpio.c,v 1.1 2019/10/28 22:21:35 jmcneill Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/mutex.h>
     38 #include <sys/kmem.h>
     39 #include <sys/gpio.h>
     40 #include <sys/bitops.h>
     41 
     42 #include <dev/fdt/fdtvar.h>
     43 #include <dev/gpio/gpiovar.h>
     44 
     45 #include <arm/ti/ti_prcm.h>
     46 
     47 #define	GPIO_OE				0x134
     48 #define	GPIO_DATAIN			0x138
     49 #define	GPIO_CLEARDATAOUT		0x190
     50 #define	GPIO_SETDATAOUT			0x194
     51 
     52 static const char * const compatible[] = {
     53 	"ti,omap4-gpio",
     54 	NULL
     55 };
     56 
     57 struct ti_gpio_softc {
     58 	device_t sc_dev;
     59 	bus_space_tag_t sc_bst;
     60 	bus_space_handle_t sc_bsh;
     61 	kmutex_t sc_lock;
     62 
     63 	struct gpio_chipset_tag sc_gp;
     64 	gpio_pin_t sc_pins[32];
     65 	device_t sc_gpiodev;
     66 };
     67 
     68 struct ti_gpio_pin {
     69 	struct ti_gpio_softc *pin_sc;
     70 	u_int pin_nr;
     71 	int pin_flags;
     72 	bool pin_actlo;
     73 };
     74 
     75 #define RD4(sc, reg) 		\
     76     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     77 #define WR4(sc, reg, val) 	\
     78     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     79 
     80 static int	ti_gpio_match(device_t, cfdata_t, void *);
     81 static void	ti_gpio_attach(device_t, device_t, void *);
     82 
     83 CFATTACH_DECL_NEW(ti_gpio, sizeof(struct ti_gpio_softc),
     84 	ti_gpio_match, ti_gpio_attach, NULL, NULL);
     85 
     86 static int
     87 ti_gpio_ctl(struct ti_gpio_softc *sc, u_int pin, int flags)
     88 {
     89 	uint32_t oe;
     90 
     91 	KASSERT(mutex_owned(&sc->sc_lock));
     92 
     93 	oe = RD4(sc, GPIO_OE);
     94 	if (flags & GPIO_PIN_INPUT)
     95 		oe |= __BIT(pin);
     96 	else if (flags & GPIO_PIN_OUTPUT)
     97 		oe &= ~__BIT(pin);
     98 	WR4(sc, GPIO_OE, oe);
     99 
    100 	return 0;
    101 }
    102 
    103 static void *
    104 ti_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    105 {
    106 	struct ti_gpio_softc * const sc = device_private(dev);
    107 	struct ti_gpio_pin *gpin;
    108 	const u_int *gpio = data;
    109 	int error;
    110 
    111 	if (len != 12)
    112 		return NULL;
    113 
    114 	const uint8_t pin = be32toh(gpio[1]) & 0xff;
    115 	const bool actlo = be32toh(gpio[2]) & 1;
    116 
    117 	if (pin >= __arraycount(sc->sc_pins))
    118 		return NULL;
    119 
    120 	mutex_enter(&sc->sc_lock);
    121 	error = ti_gpio_ctl(sc, pin, flags);
    122 	mutex_exit(&sc->sc_lock);
    123 
    124 	if (error != 0)
    125 		return NULL;
    126 
    127 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    128 	gpin->pin_sc = sc;
    129 	gpin->pin_nr = pin;
    130 	gpin->pin_flags = flags;
    131 	gpin->pin_actlo = actlo;
    132 
    133 	return gpin;
    134 }
    135 
    136 static void
    137 ti_gpio_release(device_t dev, void *priv)
    138 {
    139 	struct ti_gpio_softc * const sc = device_private(dev);
    140 	struct ti_gpio_pin *pin = priv;
    141 
    142 	mutex_enter(&sc->sc_lock);
    143 	ti_gpio_ctl(pin->pin_sc, pin->pin_nr, GPIO_PIN_INPUT);
    144 	mutex_exit(&sc->sc_lock);
    145 
    146 	kmem_free(pin, sizeof(*pin));
    147 }
    148 
    149 static int
    150 ti_gpio_read(device_t dev, void *priv, bool raw)
    151 {
    152 	struct ti_gpio_softc * const sc = device_private(dev);
    153 	struct ti_gpio_pin *pin = priv;
    154 	uint32_t data;
    155 	int val;
    156 
    157 	KASSERT(sc == pin->pin_sc);
    158 
    159 	const uint32_t data_mask = __BIT(pin->pin_nr);
    160 
    161 	/* No lock required for reads */
    162 	data = RD4(sc, GPIO_DATAIN);
    163 	val = __SHIFTOUT(data, data_mask);
    164 	if (!raw && pin->pin_actlo)
    165 		val = !val;
    166 
    167 	return val;
    168 }
    169 
    170 static void
    171 ti_gpio_write(device_t dev, void *priv, int val, bool raw)
    172 {
    173 	struct ti_gpio_softc * const sc = device_private(dev);
    174 	struct ti_gpio_pin *pin = priv;
    175 
    176 	KASSERT(sc == pin->pin_sc);
    177 
    178 	const uint32_t data_mask = __BIT(pin->pin_nr);
    179 
    180 	if (!raw && pin->pin_actlo)
    181 		val = !val;
    182 
    183 	const u_int data_reg = val ? GPIO_SETDATAOUT : GPIO_CLEARDATAOUT;
    184 
    185 	WR4(sc, data_reg, data_mask);
    186 }
    187 
    188 static struct fdtbus_gpio_controller_func ti_gpio_funcs = {
    189 	.acquire = ti_gpio_acquire,
    190 	.release = ti_gpio_release,
    191 	.read = ti_gpio_read,
    192 	.write = ti_gpio_write,
    193 };
    194 
    195 static int
    196 ti_gpio_pin_read(void *priv, int pin)
    197 {
    198 	struct ti_gpio_softc * const sc = priv;
    199 	uint32_t data;
    200 	int val;
    201 
    202 	KASSERT(pin < __arraycount(sc->sc_pins));
    203 
    204 	const uint32_t data_mask = __BIT(pin);
    205 
    206 	data = RD4(sc, GPIO_DATAIN);
    207 	val = __SHIFTOUT(data, data_mask);
    208 
    209 	return val;
    210 }
    211 
    212 static void
    213 ti_gpio_pin_write(void *priv, int pin, int val)
    214 {
    215 	struct ti_gpio_softc * const sc = priv;
    216 
    217 	KASSERT(pin < __arraycount(sc->sc_pins));
    218 
    219 	const u_int data_reg = val ? GPIO_SETDATAOUT : GPIO_CLEARDATAOUT;
    220 	const uint32_t data_mask = __BIT(pin);
    221 
    222 	WR4(sc, data_reg, data_mask);
    223 }
    224 
    225 static void
    226 ti_gpio_pin_ctl(void *priv, int pin, int flags)
    227 {
    228 	struct ti_gpio_softc * const sc = priv;
    229 
    230 	KASSERT(pin < __arraycount(sc->sc_pins));
    231 
    232 	mutex_enter(&sc->sc_lock);
    233 	ti_gpio_ctl(sc, pin, flags);
    234 	mutex_exit(&sc->sc_lock);
    235 }
    236 
    237 static void
    238 ti_gpio_attach_ports(struct ti_gpio_softc *sc)
    239 {
    240 	struct gpio_chipset_tag *gp = &sc->sc_gp;
    241 	struct gpiobus_attach_args gba;
    242 	u_int pin;
    243 
    244 	gp->gp_cookie = sc;
    245 	gp->gp_pin_read = ti_gpio_pin_read;
    246 	gp->gp_pin_write = ti_gpio_pin_write;
    247 	gp->gp_pin_ctl = ti_gpio_pin_ctl;
    248 
    249 	for (pin = 0; pin < __arraycount(sc->sc_pins); pin++) {
    250 		sc->sc_pins[pin].pin_num = pin;
    251 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
    252 		sc->sc_pins[pin].pin_state = ti_gpio_pin_read(sc, pin);
    253 	}
    254 
    255 	memset(&gba, 0, sizeof(gba));
    256 	gba.gba_gc = gp;
    257 	gba.gba_pins = sc->sc_pins;
    258 	gba.gba_npins = __arraycount(sc->sc_pins);
    259 	sc->sc_gpiodev = config_found_ia(sc->sc_dev, "gpiobus", &gba, NULL);
    260 }
    261 
    262 static int
    263 ti_gpio_match(device_t parent, cfdata_t cf, void *aux)
    264 {
    265 	struct fdt_attach_args * const faa = aux;
    266 
    267 	return of_match_compatible(faa->faa_phandle, compatible);
    268 }
    269 
    270 static void
    271 ti_gpio_attach(device_t parent, device_t self, void *aux)
    272 {
    273 	struct ti_gpio_softc * const sc = device_private(self);
    274 	struct fdt_attach_args * const faa = aux;
    275 	const int phandle = faa->faa_phandle;
    276 	bus_addr_t addr;
    277 	bus_size_t size;
    278 
    279 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    280 		aprint_error(": couldn't get registers\n");
    281 		return;
    282 	}
    283 	if (ti_prcm_enable_hwmod(OF_parent(phandle), 0) != 0) {
    284 		aprint_error(": couldn't enable module\n");
    285 		return;
    286 	}
    287 
    288 	sc->sc_dev = self;
    289 	sc->sc_bst = faa->faa_bst;
    290 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    291 		aprint_error(": couldn't map registers\n");
    292 		return;
    293 	}
    294 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    295 
    296 	aprint_naive("\n");
    297 	aprint_normal(": GPIO (%s)\n", fdtbus_get_string(OF_parent(phandle), "ti,hwmods"));
    298 
    299 	fdtbus_register_gpio_controller(self, phandle, &ti_gpio_funcs);
    300 
    301 	ti_gpio_attach_ports(sc);
    302 }
    303