Home | History | Annotate | Line # | Download | only in sunxi
      1 /* $NetBSD: sunxi_gpio.c,v 1.41 2026/11/22 14:29:23 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2017 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 "opt_soc.h"
     30 
     31 #include <sys/cdefs.h>
     32 __KERNEL_RCSID(0, "$NetBSD: sunxi_gpio.c,v 1.41 2026/11/22 14:29:23 jmcneill Exp $");
     33 
     34 #include <sys/param.h>
     35 #include <sys/bus.h>
     36 #include <sys/device.h>
     37 #include <sys/intr.h>
     38 #include <sys/systm.h>
     39 #include <sys/mutex.h>
     40 #include <sys/kmem.h>
     41 #include <sys/gpio.h>
     42 #include <sys/bitops.h>
     43 #include <sys/lwp.h>
     44 
     45 #include <dev/fdt/fdtvar.h>
     46 #include <dev/gpio/gpiovar.h>
     47 
     48 #include <arm/sunxi/sunxi_gpio.h>
     49 
     50 #define	SUNXI_GPIO_MAX_EINT_BANK	5
     51 #define	SUNXI_GPIO_MAX_EINT		32
     52 
     53 #define	SUNXI_GPIO_MAX_BANK		26
     54 
     55 #define	SUNXI_GPIO_PORT(port)		(0x24 * (port))
     56 #define SUNXI_GPIO_CFG(port, pin)	(SUNXI_GPIO_PORT(port) + 0x00 + (0x4 * ((pin) / 8)))
     57 #define  SUNXI_GPIO_CFG_PINMASK(pin)	(0x7U << (((pin) % 8) * 4))
     58 #define	SUNXI_GPIO_DATA(port)		(SUNXI_GPIO_PORT(port) + 0x10)
     59 #define	SUNXI_GPIO_DRV(port, pin)	(SUNXI_GPIO_PORT(port) + 0x14 + (0x4 * ((pin) / 16)))
     60 #define  SUNXI_GPIO_DRV_PINMASK(pin)	(0x3U << (((pin) % 16) * 2))
     61 #define	SUNXI_GPIO_PULL(port, pin)	(SUNXI_GPIO_PORT(port) + 0x1c + (0x4 * ((pin) / 16)))
     62 #define	 SUNXI_GPIO_PULL_DISABLE	0
     63 #define	 SUNXI_GPIO_PULL_UP		1
     64 #define	 SUNXI_GPIO_PULL_DOWN		2
     65 #define  SUNXI_GPIO_PULL_PINMASK(pin)	(0x3U << (((pin) % 16) * 2))
     66 #define	SUNXI_GPIO_INT_CFG(bank, eint)	(0x200 + (0x20 * (bank)) + (0x4 * ((eint) / 8)))
     67 #define	 SUNXI_GPIO_INT_MODEMASK(eint)	(0xfU << (((eint) % 8) * 4))
     68 #define	  SUNXI_GPIO_INT_MODE_POS_EDGE		0x0
     69 #define	  SUNXI_GPIO_INT_MODE_NEG_EDGE		0x1
     70 #define	  SUNXI_GPIO_INT_MODE_HIGH_LEVEL	0x2
     71 #define	  SUNXI_GPIO_INT_MODE_LOW_LEVEL		0x3
     72 #define	  SUNXI_GPIO_INT_MODE_DOUBLE_EDGE	0x4
     73 #define	SUNXI_GPIO_INT_CTL(bank)	(0x210 + 0x20 * (bank))
     74 #define	SUNXI_GPIO_INT_STATUS(bank)	(0x214 + 0x20 * (bank))
     75 #define	SUNXI_GPIO_INT_DEBOUNCE(bank)	(0x218 + 0x20 * (bank))
     76 #define	  SUNXI_GPIO_INT_DEBOUNCE_CLK_PRESCALE	__BITS(6,4)
     77 #define	  SUNXI_GPIO_INT_DEBOUNCE_CLK_SEL	__BIT(0)
     78 #define	SUNXI_GPIO_GRP_CONFIG(bank)	(0x300 + 0x4 * (bank))
     79 #define	 SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK	0xf
     80 
     81 static const struct device_compatible_entry compat_data[] = {
     82 #ifdef SOC_SUN4I_A10
     83 	{ .compat = "allwinner,sun4i-a10-pinctrl",
     84 	  .data = &sun4i_a10_padconf },
     85 #endif
     86 #ifdef SOC_SUN5I_A13
     87 	{ .compat = "allwinner,sun5i-a13-pinctrl",
     88 	  .data = &sun5i_a13_padconf },
     89 	{ .compat = "nextthing,gr8-pinctrl",
     90 	  .data = &sun5i_a13_padconf },
     91 #endif
     92 #ifdef SOC_SUN6I_A31
     93 	{ .compat = "allwinner,sun6i-a31-pinctrl",
     94 	  .data = &sun6i_a31_padconf },
     95 	{ .compat = "allwinner,sun6i-a31-r-pinctrl",
     96 	  .data = &sun6i_a31_r_padconf },
     97 #endif
     98 #ifdef SOC_SUN7I_A20
     99 	{ .compat = "allwinner,sun7i-a20-pinctrl",
    100 	  .data = &sun7i_a20_padconf },
    101 #endif
    102 #ifdef SOC_SUN8I_A83T
    103 	{ .compat = "allwinner,sun8i-a83t-pinctrl",
    104 	  .data = &sun8i_a83t_padconf },
    105 	{ .compat = "allwinner,sun8i-a83t-r-pinctrl",
    106 	  .data = &sun8i_a83t_r_padconf },
    107 #endif
    108 #ifdef SOC_SUN8I_H3
    109 	{ .compat = "allwinner,sun8i-h3-pinctrl",
    110 	  .data = &sun8i_h3_padconf },
    111 	{ .compat = "allwinner,sun8i-h3-r-pinctrl",
    112 	  .data = &sun8i_h3_r_padconf },
    113 #endif
    114 #ifdef SOC_SUN8I_V3S
    115 	{ .compat = "allwinner,sun8i-v3s-pinctrl",
    116 	  .data = &sun8i_v3s_padconf },
    117 #endif
    118 #ifdef SOC_SUN9I_A80
    119 	{ .compat = "allwinner,sun9i-a80-pinctrl",
    120 	  .data = &sun9i_a80_padconf },
    121 	{ .compat = "allwinner,sun9i-a80-r-pinctrl",
    122 	  .data = &sun9i_a80_r_padconf },
    123 #endif
    124 #ifdef SOC_SUN20I_D1
    125 	{ .compat = "allwinner,sun20i-d1-pinctrl",
    126 	  .data = &sun20i_d1_padconf },
    127 #endif
    128 #ifdef SOC_SUN50I_A64
    129 	{ .compat = "allwinner,sun50i-a64-pinctrl",
    130 	  .data = &sun50i_a64_padconf },
    131 	{ .compat = "allwinner,sun50i-a64-r-pinctrl",
    132 	  .data = &sun50i_a64_r_padconf },
    133 #endif
    134 #ifdef SOC_SUN50I_H5
    135 	{ .compat = "allwinner,sun50i-h5-pinctrl",
    136 	  .data = &sun8i_h3_padconf },
    137 	{ .compat = "allwinner,sun50i-h5-r-pinctrl",
    138 	  .data = &sun8i_h3_r_padconf },
    139 #endif
    140 #ifdef SOC_SUN50I_H6
    141 	{ .compat = "allwinner,sun50i-h6-pinctrl",
    142 	  .data = &sun50i_h6_padconf },
    143 	{ .compat = "allwinner,sun50i-h6-r-pinctrl",
    144 	  .data = &sun50i_h6_r_padconf },
    145 #endif
    146 #ifdef SOC_SUN60I_A733
    147 	{ .compat = "allwinner,sun60iw2-pinctrl",
    148 	  .data = &sun60i_a733_padconf },
    149 	{ .compat = "allwinner,sun60iw2-r-pinctrl",
    150 	  .data = &sun60i_a733_r_padconf },
    151 #endif
    152 	DEVICE_COMPAT_EOL
    153 };
    154 
    155 struct sunxi_gpio_eint {
    156 	int (*eint_func)(void *);
    157 	void *eint_arg;
    158 	bool eint_mpsafe;
    159 	int eint_bank;
    160 	int eint_num;
    161 };
    162 
    163 struct sunxi_gpio_softc {
    164 	device_t sc_dev;
    165 	bus_space_tag_t sc_bst;
    166 	bus_space_handle_t sc_bsh;
    167 	int sc_phandle;
    168 	const struct sunxi_gpio_padconf *sc_padconf;
    169 	kmutex_t sc_lock;
    170 
    171 	struct gpio_chipset_tag sc_gp;
    172 	gpio_pin_t *sc_pins;
    173 	device_t sc_gpiodev;
    174 
    175 	struct fdtbus_regulator *sc_pin_supply[SUNXI_GPIO_MAX_BANK];
    176 
    177 	u_int sc_eint_bank_max;
    178 
    179 	void *sc_ih;
    180 	struct sunxi_gpio_eint sc_eint[SUNXI_GPIO_MAX_EINT_BANK][SUNXI_GPIO_MAX_EINT];
    181 };
    182 
    183 struct sunxi_gpio_pin {
    184 	struct sunxi_gpio_softc *pin_sc;
    185 	const struct sunxi_gpio_pins *pin_def;
    186 	int pin_flags;
    187 	bool pin_actlo;
    188 };
    189 
    190 #define GPIO_READ(sc, reg) 		\
    191     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    192 #define GPIO_WRITE(sc, reg, val) 	\
    193     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    194 
    195 static int	sunxi_gpio_match(device_t, cfdata_t, void *);
    196 static void	sunxi_gpio_attach(device_t, device_t, void *);
    197 
    198 CFATTACH_DECL_NEW(sunxi_gpio, sizeof(struct sunxi_gpio_softc),
    199 	sunxi_gpio_match, sunxi_gpio_attach, NULL, NULL);
    200 
    201 static const struct sunxi_gpio_pins *
    202 sunxi_gpio_lookup(struct sunxi_gpio_softc *sc, uint8_t port, uint8_t pin)
    203 {
    204 	const struct sunxi_gpio_pins *pin_def;
    205 	u_int n;
    206 
    207 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    208 		pin_def = &sc->sc_padconf->pins[n];
    209 		if (pin_def->port == port && pin_def->pin == pin)
    210 			return pin_def;
    211 	}
    212 
    213 	return NULL;
    214 }
    215 
    216 static const struct sunxi_gpio_pins *
    217 sunxi_gpio_lookup_byname(struct sunxi_gpio_softc *sc, const char *name)
    218 {
    219 	const struct sunxi_gpio_pins *pin_def;
    220 	u_int n;
    221 
    222 	for (n = 0; n < sc->sc_padconf->npins; n++) {
    223 		pin_def = &sc->sc_padconf->pins[n];
    224 		if (strcmp(pin_def->name, name) == 0)
    225 			return pin_def;
    226 	}
    227 
    228 	return NULL;
    229 }
    230 
    231 static int
    232 sunxi_gpio_setfunc(struct sunxi_gpio_softc *sc,
    233     const struct sunxi_gpio_pins *pin_def, const char *func)
    234 {
    235 	uint32_t cfg;
    236 	u_int n;
    237 
    238 	KASSERT(mutex_owned(&sc->sc_lock));
    239 
    240 	const bus_size_t cfg_reg = SUNXI_GPIO_CFG(pin_def->port, pin_def->pin);
    241 	const uint32_t cfg_mask = SUNXI_GPIO_CFG_PINMASK(pin_def->pin);
    242 
    243 	for (n = 0; n < SUNXI_GPIO_MAXFUNC; n++) {
    244 		if (pin_def->functions[n] == NULL)
    245 			continue;
    246 		if (strcmp(pin_def->functions[n], func) == 0) {
    247 			cfg = GPIO_READ(sc, cfg_reg);
    248 			cfg &= ~cfg_mask;
    249 			cfg |= __SHIFTIN(n, cfg_mask);
    250 #ifdef SUNXI_GPIO_DEBUG
    251 			device_printf(sc->sc_dev, "P%c%02d cfg %08x -> %08x\n",
    252 			    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, cfg_reg), cfg);
    253 #endif
    254 			GPIO_WRITE(sc, cfg_reg, cfg);
    255 			return 0;
    256 		}
    257 	}
    258 
    259 	/* Function not found */
    260 	device_printf(sc->sc_dev, "function '%s' not supported on P%c%02d\n",
    261 	    func, pin_def->port + 'A', pin_def->pin);
    262 
    263 	return ENXIO;
    264 }
    265 
    266 static int
    267 sunxi_gpio_setpull(struct sunxi_gpio_softc *sc,
    268     const struct sunxi_gpio_pins *pin_def, int flags)
    269 {
    270 	uint32_t pull;
    271 
    272 	KASSERT(mutex_owned(&sc->sc_lock));
    273 
    274 	const bus_size_t pull_reg = SUNXI_GPIO_PULL(pin_def->port, pin_def->pin);
    275 	const uint32_t pull_mask = SUNXI_GPIO_PULL_PINMASK(pin_def->pin);
    276 
    277 	pull = GPIO_READ(sc, pull_reg);
    278 	pull &= ~pull_mask;
    279 	if (flags & GPIO_PIN_PULLUP)
    280 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_UP, pull_mask);
    281 	else if (flags & GPIO_PIN_PULLDOWN)
    282 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DOWN, pull_mask);
    283 	else
    284 		pull |= __SHIFTIN(SUNXI_GPIO_PULL_DISABLE, pull_mask);
    285 #ifdef SUNXI_GPIO_DEBUG
    286 	device_printf(sc->sc_dev, "P%c%02d pull %08x -> %08x\n",
    287 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, pull_reg), pull);
    288 #endif
    289 	GPIO_WRITE(sc, pull_reg, pull);
    290 
    291 	return 0;
    292 }
    293 
    294 static int
    295 sunxi_gpio_setdrv(struct sunxi_gpio_softc *sc,
    296     const struct sunxi_gpio_pins *pin_def, int drive_strength)
    297 {
    298 	uint32_t drv;
    299 
    300 	KASSERT(mutex_owned(&sc->sc_lock));
    301 
    302 	if (drive_strength < 10 || drive_strength > 40)
    303 		return EINVAL;
    304 
    305 	const bus_size_t drv_reg = SUNXI_GPIO_DRV(pin_def->port, pin_def->pin);
    306 	const uint32_t drv_mask = SUNXI_GPIO_DRV_PINMASK(pin_def->pin);
    307 
    308 	drv = GPIO_READ(sc, drv_reg);
    309 	drv &= ~drv_mask;
    310 	drv |= __SHIFTIN((drive_strength / 10) - 1, drv_mask);
    311 #ifdef SUNXI_GPIO_DEBUG
    312 	device_printf(sc->sc_dev, "P%c%02d drv %08x -> %08x\n",
    313 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, drv_reg), drv);
    314 #endif
    315 	GPIO_WRITE(sc, drv_reg, drv);
    316 
    317 	return 0;
    318 }
    319 
    320 static int
    321 sunxi_gpio_ctl(struct sunxi_gpio_softc *sc, const struct sunxi_gpio_pins *pin_def,
    322     int flags)
    323 {
    324 	KASSERT(mutex_owned(&sc->sc_lock));
    325 
    326 	if (flags & GPIO_PIN_INPUT)
    327 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_in");
    328 	if (flags & GPIO_PIN_OUTPUT)
    329 		return sunxi_gpio_setfunc(sc, pin_def, "gpio_out");
    330 
    331 	return EINVAL;
    332 }
    333 
    334 static void *
    335 sunxi_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
    336 {
    337 	struct sunxi_gpio_softc * const sc = device_private(dev);
    338 	const struct sunxi_gpio_pins *pin_def;
    339 	struct sunxi_gpio_pin *gpin;
    340 	const u_int *gpio = data;
    341 	int error;
    342 
    343 	if (len != 16)
    344 		return NULL;
    345 
    346 	const uint8_t port = be32toh(gpio[1]) & 0xff;
    347 	const uint8_t pin = be32toh(gpio[2]) & 0xff;
    348 	const bool actlo = be32toh(gpio[3]) & 1;
    349 
    350 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    351 	if (pin_def == NULL)
    352 		return NULL;
    353 
    354 	mutex_enter(&sc->sc_lock);
    355 	error = sunxi_gpio_ctl(sc, pin_def, flags);
    356 	mutex_exit(&sc->sc_lock);
    357 
    358 	if (error != 0)
    359 		return NULL;
    360 
    361 	gpin = kmem_zalloc(sizeof(*gpin), KM_SLEEP);
    362 	gpin->pin_sc = sc;
    363 	gpin->pin_def = pin_def;
    364 	gpin->pin_flags = flags;
    365 	gpin->pin_actlo = actlo;
    366 
    367 	return gpin;
    368 }
    369 
    370 static void
    371 sunxi_gpio_release(device_t dev, void *priv)
    372 {
    373 	struct sunxi_gpio_softc * const sc = device_private(dev);
    374 	struct sunxi_gpio_pin *pin = priv;
    375 
    376 	mutex_enter(&sc->sc_lock);
    377 	sunxi_gpio_ctl(pin->pin_sc, pin->pin_def, GPIO_PIN_INPUT);
    378 	mutex_exit(&sc->sc_lock);
    379 
    380 	kmem_free(pin, sizeof(*pin));
    381 }
    382 
    383 static int
    384 sunxi_gpio_read(device_t dev, void *priv, bool raw)
    385 {
    386 	struct sunxi_gpio_softc * const sc = device_private(dev);
    387 	struct sunxi_gpio_pin *pin = priv;
    388 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    389 	uint32_t data;
    390 	int val;
    391 
    392 	KASSERT(sc == pin->pin_sc);
    393 
    394 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    395 	const uint32_t data_mask = __BIT(pin_def->pin);
    396 
    397 	/* No lock required for reads */
    398 	data = GPIO_READ(sc, data_reg);
    399 	val = __SHIFTOUT(data, data_mask);
    400 	if (!raw && pin->pin_actlo)
    401 		val = !val;
    402 
    403 #ifdef SUNXI_GPIO_DEBUG
    404 	device_printf(dev, "P%c%02d rd %08x (%d %d)\n",
    405 	    pin_def->port + 'A', pin_def->pin, data,
    406 	    __SHIFTOUT(val, data_mask), val);
    407 #endif
    408 
    409 	return val;
    410 }
    411 
    412 static void
    413 sunxi_gpio_write(device_t dev, void *priv, int val, bool raw)
    414 {
    415 	struct sunxi_gpio_softc * const sc = device_private(dev);
    416 	struct sunxi_gpio_pin *pin = priv;
    417 	const struct sunxi_gpio_pins *pin_def = pin->pin_def;
    418 	uint32_t data;
    419 
    420 	KASSERT(sc == pin->pin_sc);
    421 
    422 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    423 	const uint32_t data_mask = __BIT(pin_def->pin);
    424 
    425 	if (!raw && pin->pin_actlo)
    426 		val = !val;
    427 
    428 	mutex_enter(&sc->sc_lock);
    429 	data = GPIO_READ(sc, data_reg);
    430 	data &= ~data_mask;
    431 	data |= __SHIFTIN(val, data_mask);
    432 #ifdef SUNXI_GPIO_DEBUG
    433 	device_printf(dev, "P%c%02d wr %08x -> %08x\n",
    434 	    pin_def->port + 'A', pin_def->pin, GPIO_READ(sc, data_reg), data);
    435 #endif
    436 	GPIO_WRITE(sc, data_reg, data);
    437 	mutex_exit(&sc->sc_lock);
    438 }
    439 
    440 static struct fdtbus_gpio_controller_func sunxi_gpio_funcs = {
    441 	.acquire = sunxi_gpio_acquire,
    442 	.release = sunxi_gpio_release,
    443 	.read = sunxi_gpio_read,
    444 	.write = sunxi_gpio_write,
    445 };
    446 
    447 static int
    448 sunxi_gpio_intr(void *priv)
    449 {
    450 	struct sunxi_gpio_softc * const sc = priv;
    451 	struct sunxi_gpio_eint *eint;
    452 	uint32_t status, bit;
    453 	u_int bank;
    454 	int ret = 0;
    455 
    456 	for (bank = 0; bank <= sc->sc_eint_bank_max; bank++) {
    457 		status = GPIO_READ(sc, SUNXI_GPIO_INT_STATUS(bank));
    458 		if (status == 0)
    459 			continue;
    460 		GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(bank), status);
    461 
    462 		while ((bit = ffs32(status)) != 0) {
    463 			status &= ~__BIT(bit - 1);
    464 			eint = &sc->sc_eint[bank][bit - 1];
    465 			if (eint->eint_func == NULL)
    466 				continue;
    467 			if (!eint->eint_mpsafe)
    468 				KERNEL_LOCK(1, curlwp);
    469 			ret |= eint->eint_func(eint->eint_arg);
    470 			if (!eint->eint_mpsafe)
    471 				KERNEL_UNLOCK_ONE(curlwp);
    472 		}
    473 	}
    474 
    475 	return ret;
    476 }
    477 
    478 static void *
    479 sunxi_intr_enable(struct sunxi_gpio_softc *sc,
    480     const struct sunxi_gpio_pins *pin_def, u_int mode, bool mpsafe,
    481     int (*func)(void *), void *arg)
    482 {
    483 	uint32_t val;
    484 	struct sunxi_gpio_eint *eint;
    485 
    486 	if (pin_def->functions[pin_def->eint_func] == NULL ||
    487 	    strcmp(pin_def->functions[pin_def->eint_func], "irq") != 0)
    488 		return NULL;
    489 
    490 	KASSERT(pin_def->eint_num < SUNXI_GPIO_MAX_EINT);
    491 
    492 	mutex_enter(&sc->sc_lock);
    493 
    494 	eint = &sc->sc_eint[pin_def->eint_bank][pin_def->eint_num];
    495 	if (eint->eint_func != NULL) {
    496 		mutex_exit(&sc->sc_lock);
    497 		return NULL;	/* in use */
    498 	}
    499 
    500 	/* Set function */
    501 	if (sunxi_gpio_setfunc(sc, pin_def, "irq") != 0) {
    502 		mutex_exit(&sc->sc_lock);
    503 		return NULL;
    504 	}
    505 
    506 	eint->eint_func = func;
    507 	eint->eint_arg = arg;
    508 	eint->eint_mpsafe = mpsafe;
    509 	eint->eint_bank = pin_def->eint_bank;
    510 	eint->eint_num = pin_def->eint_num;
    511 
    512 	/* Configure eint mode */
    513 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num));
    514 	val &= ~SUNXI_GPIO_INT_MODEMASK(eint->eint_num);
    515 	val |= __SHIFTIN(mode, SUNXI_GPIO_INT_MODEMASK(eint->eint_num));
    516 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CFG(eint->eint_bank, eint->eint_num), val);
    517 
    518 	val = SUNXI_GPIO_INT_DEBOUNCE_CLK_SEL;
    519 	GPIO_WRITE(sc, SUNXI_GPIO_INT_DEBOUNCE(eint->eint_bank), val);
    520 
    521 	/* Enable eint */
    522 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
    523 	val |= __BIT(eint->eint_num);
    524 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
    525 
    526 	mutex_exit(&sc->sc_lock);
    527 
    528 	return eint;
    529 }
    530 
    531 static void
    532 sunxi_intr_disable(struct sunxi_gpio_softc *sc, struct sunxi_gpio_eint *eint)
    533 {
    534 	uint32_t val;
    535 
    536 	KASSERT(eint->eint_func != NULL);
    537 
    538 	mutex_enter(&sc->sc_lock);
    539 
    540 	/* Disable eint */
    541 	val = GPIO_READ(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank));
    542 	val &= ~__BIT(eint->eint_num);
    543 	GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(eint->eint_bank), val);
    544 	GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(eint->eint_bank), __BIT(eint->eint_num));
    545 
    546 	eint->eint_func = NULL;
    547 	eint->eint_arg = NULL;
    548 	eint->eint_mpsafe = false;
    549 
    550 	mutex_exit(&sc->sc_lock);
    551 }
    552 
    553 static void *
    554 sunxi_fdt_intr_establish(device_t dev, u_int *specifier, int ipl, int flags,
    555     int (*func)(void *), void *arg, const char *xname)
    556 {
    557 	struct sunxi_gpio_softc * const sc = device_private(dev);
    558 	bool mpsafe = (flags & FDT_INTR_MPSAFE) != 0;
    559 	const struct sunxi_gpio_pins *pin_def;
    560 	u_int mode;
    561 
    562 	if (ipl != IPL_VM) {
    563 		aprint_error_dev(dev, "%s: wrong IPL %d (expected %d)\n",
    564 		    __func__, ipl, IPL_VM);
    565 		return NULL;
    566 	}
    567 
    568 	/* 1st cell is the bank */
    569 	/* 2nd cell is the pin */
    570 	/* 3rd cell is flags */
    571 	const u_int port = be32toh(specifier[0]);
    572 	const u_int pin = be32toh(specifier[1]);
    573 	const u_int type = be32toh(specifier[2]) & 0xf;
    574 
    575 	switch (type) {
    576 	case FDT_INTR_TYPE_POS_EDGE:
    577 		mode = SUNXI_GPIO_INT_MODE_POS_EDGE;
    578 		break;
    579 	case FDT_INTR_TYPE_NEG_EDGE:
    580 		mode = SUNXI_GPIO_INT_MODE_NEG_EDGE;
    581 		break;
    582 	case FDT_INTR_TYPE_DOUBLE_EDGE:
    583 		mode = SUNXI_GPIO_INT_MODE_DOUBLE_EDGE;
    584 		break;
    585 	case FDT_INTR_TYPE_HIGH_LEVEL:
    586 		mode = SUNXI_GPIO_INT_MODE_HIGH_LEVEL;
    587 		break;
    588 	case FDT_INTR_TYPE_LOW_LEVEL:
    589 		mode = SUNXI_GPIO_INT_MODE_LOW_LEVEL;
    590 		break;
    591 	default:
    592 		aprint_error_dev(dev, "%s: unsupported irq type 0x%x\n",
    593 		    __func__, type);
    594 		return NULL;
    595 	}
    596 
    597 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    598 	if (pin_def == NULL)
    599 		return NULL;
    600 
    601 	return sunxi_intr_enable(sc, pin_def, mode, mpsafe, func, arg);
    602 }
    603 
    604 static void
    605 sunxi_fdt_intr_disestablish(device_t dev, void *ih)
    606 {
    607 	struct sunxi_gpio_softc * const sc = device_private(dev);
    608 	struct sunxi_gpio_eint * const eint = ih;
    609 
    610 	sunxi_intr_disable(sc, eint);
    611 }
    612 
    613 static bool
    614 sunxi_fdt_intrstr(device_t dev, u_int *specifier, char *buf, size_t buflen)
    615 {
    616 	struct sunxi_gpio_softc * const sc = device_private(dev);
    617 	const struct sunxi_gpio_pins *pin_def;
    618 
    619 	/* 1st cell is the bank */
    620 	/* 2nd cell is the pin */
    621 	/* 3rd cell is flags */
    622 	if (!specifier)
    623 		return false;
    624 	const u_int port = be32toh(specifier[0]);
    625 	const u_int pin = be32toh(specifier[1]);
    626 
    627 	pin_def = sunxi_gpio_lookup(sc, port, pin);
    628 	if (pin_def == NULL)
    629 		return false;
    630 
    631 	snprintf(buf, buflen, "GPIO %s", pin_def->name);
    632 
    633 	return true;
    634 }
    635 
    636 static struct fdtbus_interrupt_controller_func sunxi_gpio_intrfuncs = {
    637 	.establish = sunxi_fdt_intr_establish,
    638 	.disestablish = sunxi_fdt_intr_disestablish,
    639 	.intrstr = sunxi_fdt_intrstr,
    640 };
    641 
    642 static void *
    643 sunxi_gpio_intr_establish(void *vsc, int pin, int ipl, int irqmode,
    644     int (*func)(void *), void *arg)
    645 {
    646 	struct sunxi_gpio_softc * const sc = vsc;
    647 	bool mpsafe = (irqmode & GPIO_INTR_MPSAFE) != 0;
    648 	int type = irqmode & GPIO_INTR_MODE_MASK;
    649 	const struct sunxi_gpio_pins *pin_def;
    650 	u_int mode;
    651 
    652 	switch (type) {
    653 	case GPIO_INTR_POS_EDGE:
    654 		mode = SUNXI_GPIO_INT_MODE_POS_EDGE;
    655 		break;
    656 	case GPIO_INTR_NEG_EDGE:
    657 		mode = SUNXI_GPIO_INT_MODE_NEG_EDGE;
    658 		break;
    659 	case GPIO_INTR_DOUBLE_EDGE:
    660 		mode = SUNXI_GPIO_INT_MODE_DOUBLE_EDGE;
    661 		break;
    662 	case GPIO_INTR_HIGH_LEVEL:
    663 		mode = SUNXI_GPIO_INT_MODE_HIGH_LEVEL;
    664 		break;
    665 	case GPIO_INTR_LOW_LEVEL:
    666 		mode = SUNXI_GPIO_INT_MODE_LOW_LEVEL;
    667 		break;
    668 	default:
    669 		aprint_error_dev(sc->sc_dev, "%s: unsupported irq type 0x%x\n",
    670 				 __func__, type);
    671 		return NULL;
    672 	}
    673 
    674 	if (pin < 0 || pin >= sc->sc_padconf->npins)
    675 		return NULL;
    676 	pin_def = &sc->sc_padconf->pins[pin];
    677 
    678 	return sunxi_intr_enable(sc, pin_def, mode, mpsafe, func, arg);
    679 }
    680 
    681 static void
    682 sunxi_gpio_intr_disestablish(void *vsc, void *ih)
    683 {
    684 	struct sunxi_gpio_softc * const sc = vsc;
    685 	struct sunxi_gpio_eint * const eint = ih;
    686 
    687 	sunxi_intr_disable(sc, eint);
    688 }
    689 
    690 static bool
    691 sunxi_gpio_intrstr(void *vsc, int pin, int irqmode, char *buf, size_t buflen)
    692 {
    693 	struct sunxi_gpio_softc * const sc = vsc;
    694 	const struct sunxi_gpio_pins *pin_def;
    695 
    696 	if (pin < 0 || pin >= sc->sc_padconf->npins)
    697 		return NULL;
    698 	pin_def = &sc->sc_padconf->pins[pin];
    699 
    700 	snprintf(buf, buflen, "GPIO %s", pin_def->name);
    701 
    702 	return true;
    703 }
    704 
    705 static const char *
    706 sunxi_pinctrl_parse_function(int phandle)
    707 {
    708 	const char *function;
    709 
    710 	function = fdtbus_pinctrl_parse_function(phandle);
    711 	if (function != NULL)
    712 		return function;
    713 
    714 	return fdtbus_get_string(phandle, "allwinner,function");
    715 }
    716 
    717 static const char *
    718 sunxi_pinctrl_parse_pins(int phandle, int *pins_len)
    719 {
    720 	const char *pins;
    721 	int len;
    722 
    723 	pins = fdtbus_pinctrl_parse_pins(phandle, pins_len);
    724 	if (pins != NULL)
    725 		return pins;
    726 
    727 	len = OF_getproplen(phandle, "allwinner,pins");
    728 	if (len > 0) {
    729 		*pins_len = len;
    730 		return fdtbus_get_prop(phandle, "allwinner,pins", pins_len);
    731 	}
    732 
    733 	return NULL;
    734 }
    735 
    736 static int
    737 sunxi_pinctrl_parse_bias(int phandle)
    738 {
    739 	u_int pull;
    740 	int bias;
    741 
    742 	bias = fdtbus_pinctrl_parse_bias(phandle, NULL);
    743 	if (bias != -1)
    744 		return bias;
    745 
    746 	if (of_getprop_uint32(phandle, "allwinner,pull", &pull) == 0) {
    747 		switch (pull) {
    748 		case 0:
    749 			bias = 0;
    750 			break;
    751 		case 1:
    752 			bias = GPIO_PIN_PULLUP;
    753 			break;
    754 		case 2:
    755 			bias = GPIO_PIN_PULLDOWN;
    756 			break;
    757 		}
    758 	}
    759 
    760 	return bias;
    761 }
    762 
    763 static int
    764 sunxi_pinctrl_parse_drive_strength(int phandle)
    765 {
    766 	int val;
    767 
    768 	val = fdtbus_pinctrl_parse_drive_strength(phandle);
    769 	if (val != -1)
    770 		return val;
    771 
    772 	if (of_getprop_uint32(phandle, "allwinner,drive", &val) == 0)
    773 		return (val + 1) * 10;
    774 
    775 	return -1;
    776 }
    777 
    778 static void
    779 sunxi_pinctrl_enable_regulator(struct sunxi_gpio_softc *sc,
    780     const struct sunxi_gpio_pins *pin_def)
    781 {
    782 	char supply_prop[16];
    783 	uint32_t val;
    784 	u_int uvol;
    785 	int error;
    786 
    787 	const char c = tolower(pin_def->name[1]);
    788 	if (c < 'a' || c > 'z')
    789 		return;
    790 	const int index = c - 'a';
    791 
    792 	if (sc->sc_pin_supply[index] != NULL) {
    793 		/* Already enabled */
    794 		return;
    795 	}
    796 
    797 	snprintf(supply_prop, sizeof(supply_prop), "vcc-p%c-supply", c);
    798 	sc->sc_pin_supply[index] = fdtbus_regulator_acquire(sc->sc_phandle, supply_prop);
    799 	if (sc->sc_pin_supply[index] == NULL)
    800 		return;
    801 
    802 	aprint_debug_dev(sc->sc_dev, "enable \"%s\"\n", supply_prop);
    803 	error = fdtbus_regulator_enable(sc->sc_pin_supply[index]);
    804 	if (error != 0)
    805 		aprint_error_dev(sc->sc_dev, "failed to enable %s: %d\n", supply_prop, error);
    806 
    807 	if (sc->sc_padconf->has_io_bias_config) {
    808 		error = fdtbus_regulator_get_voltage(sc->sc_pin_supply[index], &uvol);
    809 		if (error != 0) {
    810 			aprint_error_dev(sc->sc_dev, "failed to get %s voltage: %d\n",
    811 			    supply_prop, error);
    812 			uvol = 0;
    813 		}
    814 		if (uvol != 0) {
    815 			if (uvol <= 1800000)
    816 				val = 0x0;	/* 1.8V */
    817 			else if (uvol <= 2500000)
    818 				val = 0x6;	/* 2.5V */
    819 			else if (uvol <= 2800000)
    820 				val = 0x9;	/* 2.8V */
    821 			else if (uvol <= 3000000)
    822 				val = 0xa;	/* 3.0V */
    823 			else
    824 				val = 0xd;	/* 3.3V */
    825 
    826 			aprint_debug_dev(sc->sc_dev, "set io bias config for port %d to 0x%x\n",
    827 			    pin_def->port, val);
    828 			val = GPIO_READ(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port));
    829 			val &= ~SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK;
    830 			val |= __SHIFTIN(val, SUNXI_GPIO_GRP_IO_BIAS_CONFIGMASK);
    831 			GPIO_WRITE(sc, SUNXI_GPIO_GRP_CONFIG(pin_def->port), val);
    832 		}
    833 	}
    834 }
    835 
    836 static int
    837 sunxi_pinctrl_set_config(device_t dev, const void *data, size_t len)
    838 {
    839 	struct sunxi_gpio_softc * const sc = device_private(dev);
    840 	const struct sunxi_gpio_pins *pin_def;
    841 	int pins_len;
    842 
    843 	if (len != 4)
    844 		return -1;
    845 
    846 	const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
    847 
    848 	/*
    849 	 * Required: pins, function
    850 	 * Optional: bias, drive strength
    851 	 */
    852 
    853 	const char *function = sunxi_pinctrl_parse_function(phandle);
    854 	if (function == NULL)
    855 		return -1;
    856 	const char *pins = sunxi_pinctrl_parse_pins(phandle, &pins_len);
    857 	if (pins == NULL)
    858 		return -1;
    859 
    860 	const int bias = sunxi_pinctrl_parse_bias(phandle);
    861 	const int drive_strength = sunxi_pinctrl_parse_drive_strength(phandle);
    862 
    863 	mutex_enter(&sc->sc_lock);
    864 
    865 	for (; pins_len > 0;
    866 	    pins_len -= strlen(pins) + 1, pins += strlen(pins) + 1) {
    867 		pin_def = sunxi_gpio_lookup_byname(sc, pins);
    868 		if (pin_def == NULL) {
    869 			aprint_error_dev(dev, "unknown pin name '%s'\n", pins);
    870 			continue;
    871 		}
    872 		if (sunxi_gpio_setfunc(sc, pin_def, function) != 0)
    873 			continue;
    874 
    875 		if (bias != -1)
    876 			sunxi_gpio_setpull(sc, pin_def, bias);
    877 
    878 		if (drive_strength != -1)
    879 			sunxi_gpio_setdrv(sc, pin_def, drive_strength);
    880 
    881 		sunxi_pinctrl_enable_regulator(sc, pin_def);
    882 	}
    883 
    884 	mutex_exit(&sc->sc_lock);
    885 
    886 	return 0;
    887 }
    888 
    889 static struct fdtbus_pinctrl_controller_func sunxi_pinctrl_funcs = {
    890 	.set_config = sunxi_pinctrl_set_config,
    891 };
    892 
    893 static int
    894 sunxi_gpio_pin_read(void *priv, int pin)
    895 {
    896 	struct sunxi_gpio_softc * const sc = priv;
    897 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    898 	uint32_t data;
    899 	int val;
    900 
    901 	KASSERT(pin < sc->sc_padconf->npins);
    902 
    903 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    904 	const uint32_t data_mask = __BIT(pin_def->pin);
    905 
    906 	/* No lock required for reads */
    907 	data = GPIO_READ(sc, data_reg);
    908 	val = __SHIFTOUT(data, data_mask);
    909 
    910 	return val;
    911 }
    912 
    913 static void
    914 sunxi_gpio_pin_write(void *priv, int pin, int val)
    915 {
    916 	struct sunxi_gpio_softc * const sc = priv;
    917 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    918 	uint32_t data;
    919 
    920 	KASSERT(pin < sc->sc_padconf->npins);
    921 
    922 	const bus_size_t data_reg = SUNXI_GPIO_DATA(pin_def->port);
    923 	const uint32_t data_mask = __BIT(pin_def->pin);
    924 
    925 	mutex_enter(&sc->sc_lock);
    926 	data = GPIO_READ(sc, data_reg);
    927 	if (val)
    928 		data |= data_mask;
    929 	else
    930 		data &= ~data_mask;
    931 	GPIO_WRITE(sc, data_reg, data);
    932 	mutex_exit(&sc->sc_lock);
    933 }
    934 
    935 static void
    936 sunxi_gpio_pin_ctl(void *priv, int pin, int flags)
    937 {
    938 	struct sunxi_gpio_softc * const sc = priv;
    939 	const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[pin];
    940 
    941 	KASSERT(pin < sc->sc_padconf->npins);
    942 
    943 	mutex_enter(&sc->sc_lock);
    944 	sunxi_gpio_ctl(sc, pin_def, flags);
    945 	sunxi_gpio_setpull(sc, pin_def, flags);
    946 	mutex_exit(&sc->sc_lock);
    947 }
    948 
    949 static void
    950 sunxi_gpio_attach_ports(struct sunxi_gpio_softc *sc)
    951 {
    952 	const struct sunxi_gpio_pins *pin_def;
    953 	struct gpio_chipset_tag *gp = &sc->sc_gp;
    954 	struct gpiobus_attach_args gba;
    955 	u_int pin;
    956 
    957 	gp->gp_cookie = sc;
    958 	gp->gp_pin_read = sunxi_gpio_pin_read;
    959 	gp->gp_pin_write = sunxi_gpio_pin_write;
    960 	gp->gp_pin_ctl = sunxi_gpio_pin_ctl;
    961 	gp->gp_intr_establish = sunxi_gpio_intr_establish;
    962 	gp->gp_intr_disestablish = sunxi_gpio_intr_disestablish;
    963 	gp->gp_intr_str = sunxi_gpio_intrstr;
    964 
    965 	const u_int npins = sc->sc_padconf->npins;
    966 	sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) * npins, KM_SLEEP);
    967 
    968 	for (pin = 0; pin < sc->sc_padconf->npins; pin++) {
    969 		pin_def = &sc->sc_padconf->pins[pin];
    970 		sc->sc_pins[pin].pin_num = pin;
    971 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT |
    972 		    GPIO_PIN_PULLUP | GPIO_PIN_PULLDOWN;
    973 		if (pin_def->functions[pin_def->eint_func] != NULL &&
    974 		    strcmp(pin_def->functions[pin_def->eint_func], "irq") == 0) {
    975 			sc->sc_pins[pin].pin_intrcaps =
    976 			    GPIO_INTR_POS_EDGE | GPIO_INTR_NEG_EDGE |
    977 			    GPIO_INTR_HIGH_LEVEL | GPIO_INTR_LOW_LEVEL |
    978 			    GPIO_INTR_DOUBLE_EDGE | GPIO_INTR_MPSAFE;
    979 		}
    980 		sc->sc_pins[pin].pin_state = sunxi_gpio_pin_read(sc, pin);
    981 		strlcpy(sc->sc_pins[pin].pin_defname, pin_def->name,
    982 		    sizeof(sc->sc_pins[pin].pin_defname));
    983 	}
    984 
    985 	memset(&gba, 0, sizeof(gba));
    986 	gba.gba_gc = gp;
    987 	gba.gba_pins = sc->sc_pins;
    988 	gba.gba_npins = npins;
    989 	sc->sc_gpiodev = config_found(sc->sc_dev, &gba, NULL, CFARGS_NONE);
    990 }
    991 
    992 static int
    993 sunxi_gpio_match(device_t parent, cfdata_t cf, void *aux)
    994 {
    995 	struct fdt_attach_args * const faa = aux;
    996 
    997 	return of_compatible_match(faa->faa_phandle, compat_data);
    998 }
    999 
   1000 static void
   1001 sunxi_gpio_attach(device_t parent, device_t self, void *aux)
   1002 {
   1003 	struct sunxi_gpio_softc * const sc = device_private(self);
   1004 	struct fdt_attach_args * const faa = aux;
   1005 	const int phandle = faa->faa_phandle;
   1006 	char intrstr[128];
   1007 	struct fdtbus_reset *rst;
   1008 	struct clk *clk;
   1009 	bus_addr_t addr;
   1010 	bus_size_t size;
   1011 	int child;
   1012 
   1013 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
   1014 		aprint_error(": couldn't get registers\n");
   1015 		return;
   1016 	}
   1017 
   1018 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL)
   1019 		if (clk_enable(clk) != 0) {
   1020 			aprint_error(": couldn't enable clock\n");
   1021 			return;
   1022 		}
   1023 
   1024 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
   1025 		if (fdtbus_reset_deassert(rst) != 0) {
   1026 			aprint_error(": couldn't de-assert reset\n");
   1027 			return;
   1028 		}
   1029 
   1030 	sc->sc_dev = self;
   1031 	sc->sc_phandle = phandle;
   1032 	sc->sc_bst = faa->faa_bst;
   1033 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
   1034 		aprint_error(": couldn't map registers\n");
   1035 		return;
   1036 	}
   1037 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
   1038 	sc->sc_padconf = of_compatible_lookup(phandle, compat_data)->data;
   1039 
   1040 	aprint_naive("\n");
   1041 	aprint_normal(": PIO\n");
   1042 
   1043 	fdtbus_register_gpio_controller(self, phandle, &sunxi_gpio_funcs);
   1044 
   1045 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
   1046 		bool is_valid =
   1047 		    (of_hasprop(child, "function") && of_hasprop(child, "pins")) ||
   1048 		    (of_hasprop(child, "allwinner,function") && of_hasprop(child, "allwinner,pins"));
   1049 		if (!is_valid)
   1050 			continue;
   1051 		fdtbus_register_pinctrl_config(self, child, &sunxi_pinctrl_funcs);
   1052 	}
   1053 
   1054 	sunxi_gpio_attach_ports(sc);
   1055 
   1056 	/* Disable all external interrupts */
   1057 	for (int i = 0; i < sc->sc_padconf->npins; i++) {
   1058 		const struct sunxi_gpio_pins *pin_def = &sc->sc_padconf->pins[i];
   1059 		if (pin_def->eint_func == 0)
   1060 			continue;
   1061 		GPIO_WRITE(sc, SUNXI_GPIO_INT_CTL(pin_def->eint_bank), __BIT(pin_def->eint_num));
   1062 		GPIO_WRITE(sc, SUNXI_GPIO_INT_STATUS(pin_def->eint_bank), __BIT(pin_def->eint_num));
   1063 
   1064 		if (sc->sc_eint_bank_max < pin_def->eint_bank)
   1065 			sc->sc_eint_bank_max = pin_def->eint_bank;
   1066 	}
   1067 	KASSERT(sc->sc_eint_bank_max < SUNXI_GPIO_MAX_EINT_BANK);
   1068 
   1069 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
   1070 		aprint_error_dev(self, "failed to decode interrupt\n");
   1071 		return;
   1072 	}
   1073 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
   1074 	    FDT_INTR_MPSAFE, sunxi_gpio_intr, sc, device_xname(self));
   1075 	if (sc->sc_ih == NULL) {
   1076 		aprint_error_dev(self, "failed to establish interrupt on %s\n",
   1077 		    intrstr);
   1078 		return;
   1079 	}
   1080 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
   1081 	fdtbus_register_interrupt_controller(self, phandle,
   1082 	    &sunxi_gpio_intrfuncs);
   1083 }
   1084