Home | History | Annotate | Line # | Download | only in acpi
qcomgpio.c revision 1.1
      1 /* $NetBSD: qcomgpio.c,v 1.1 2024/12/08 20:49:14 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jared McNeill <jmcneill (at) invisible.ca>.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: qcomgpio.c,v 1.1 2024/12/08 20:49:14 jmcneill Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/bus.h>
     37 #include <sys/cpu.h>
     38 #include <sys/device.h>
     39 #include <sys/gpio.h>
     40 #include <sys/queue.h>
     41 #include <sys/kmem.h>
     42 #include <sys/mutex.h>
     43 
     44 #include <dev/acpi/acpireg.h>
     45 #include <dev/acpi/acpivar.h>
     46 #include <dev/acpi/acpi_intr.h>
     47 #include <dev/acpi/acpi_event.h>
     48 #include <dev/acpi/acpi_gpio.h>
     49 #include <dev/acpi/qcomgpioreg.h>
     50 
     51 #include <dev/gpio/gpiovar.h>
     52 
     53 typedef enum {
     54 	QCOMGPIO_X1E,
     55 } qcomgpio_type;
     56 
     57 struct qcomgpio_config {
     58 	u_int	num_pins;
     59 	int	(*translate)(ACPI_INTEGER);
     60 };
     61 
     62 struct qcomgpio_intr_handler {
     63 	int	(*ih_func)(void *);
     64 	void	*ih_arg;
     65 	int	ih_pin;
     66 	LIST_ENTRY(qcomgpio_intr_handler) ih_list;
     67 };
     68 
     69 struct qcomgpio_softc {
     70 	device_t			sc_dev;
     71 	device_t			sc_gpiodev;
     72 	bus_space_handle_t		sc_bsh;
     73 	bus_space_tag_t			sc_bst;
     74 	const struct qcomgpio_config	*sc_config;
     75 	struct gpio_chipset_tag		sc_gc;
     76 	gpio_pin_t			*sc_pins;
     77 	LIST_HEAD(, qcomgpio_intr_handler) sc_intrs;
     78 	kmutex_t			sc_lock;
     79 };
     80 
     81 #define RD4(sc, reg)		\
     82 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     83 #define WR4(sc, reg, val)	\
     84 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     85 
     86 static int	qcomgpio_match(device_t, cfdata_t, void *);
     87 static void	qcomgpio_attach(device_t, device_t, void *);
     88 
     89 static int	qcomgpio_pin_read(void *, int);
     90 static void	qcomgpio_pin_write(void *, int, int);
     91 static void	qcomgpio_pin_ctl(void *, int, int);
     92 static void *	qcomgpio_intr_establish(void *, int, int, int,
     93 					int (*)(void *), void *);
     94 static void	qcomgpio_intr_disestablish(void *, void *);
     95 static bool	qcomgpio_intr_str(void *, int, int, char *, size_t);
     96 static void	qcomgpio_intr_mask(void *, void *);
     97 static void	qcomgpio_intr_unmask(void *, void *);
     98 
     99 static int	qcomgpio_acpi_translate(void *, ACPI_INTEGER, void **);
    100 static void	qcomgpio_register_event(void *, struct acpi_event *,
    101 					ACPI_RESOURCE_GPIO *);
    102 static int	qcomgpio_intr(void *);
    103 
    104 CFATTACH_DECL_NEW(qcomgpio, sizeof(struct qcomgpio_softc),
    105     qcomgpio_match, qcomgpio_attach, NULL, NULL);
    106 
    107 static int
    108 qcomgpio_x1e_translate(ACPI_INTEGER val)
    109 {
    110 	switch (val) {
    111 	case 0x33:
    112 		return 51;
    113 	case 0x180:
    114 		return 67;
    115 	case 0x380:
    116 		return 3;
    117 	default:
    118 		return -1;
    119 	}
    120 }
    121 
    122 static struct qcomgpio_config qcomgpio_x1e_config = {
    123 	.num_pins = 239,
    124 	.translate = qcomgpio_x1e_translate,
    125 };
    126 
    127 static const struct device_compatible_entry compat_data[] = {
    128 	{ .compat = "QCOM0C0C",	.data = &qcomgpio_x1e_config },
    129 	DEVICE_COMPAT_EOL
    130 };
    131 
    132 static int
    133 qcomgpio_match(device_t parent, cfdata_t cf, void *aux)
    134 {
    135 	struct acpi_attach_args *aa = aux;
    136 
    137 	return acpi_compatible_match(aa, compat_data);
    138 }
    139 
    140 static void
    141 qcomgpio_attach(device_t parent, device_t self, void *aux)
    142 {
    143 	struct qcomgpio_softc * const sc = device_private(self);
    144 	struct acpi_attach_args *aa = aux;
    145 	struct gpiobus_attach_args gba;
    146 	ACPI_HANDLE hdl = aa->aa_node->ad_handle;
    147 	struct acpi_resources res;
    148 	struct acpi_mem *mem;
    149 	struct acpi_irq *irq;
    150 	ACPI_STATUS rv;
    151 	int error, pin;
    152 	void *ih;
    153 
    154 	sc->sc_dev = self;
    155 	sc->sc_config = acpi_compatible_lookup(aa, compat_data)->data;
    156 	sc->sc_bst = aa->aa_memt;
    157 	KASSERT(sc->sc_config != NULL);
    158 	LIST_INIT(&sc->sc_intrs);
    159 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    160 
    161 	rv = acpi_resource_parse(sc->sc_dev, hdl, "_CRS",
    162 	    &res, &acpi_resource_parse_ops_default);
    163 	if (ACPI_FAILURE(rv)) {
    164 		return;
    165 	}
    166 
    167 	mem = acpi_res_mem(&res, 0);
    168 	if (mem == NULL) {
    169 		aprint_error_dev(self, "couldn't find mem resource\n");
    170 		goto done;
    171 	}
    172 
    173 	irq = acpi_res_irq(&res, 0);
    174 	if (irq == NULL) {
    175 		aprint_error_dev(self, "couldn't find irq resource\n");
    176 		goto done;
    177 	}
    178 
    179 	error = bus_space_map(sc->sc_bst, mem->ar_base, mem->ar_length, 0,
    180 	    &sc->sc_bsh);
    181 	if (error) {
    182 		aprint_error_dev(self, "couldn't map registers\n");
    183 		goto done;
    184 	}
    185 
    186 	sc->sc_pins = kmem_zalloc(sizeof(*sc->sc_pins) *
    187 	    sc->sc_config->num_pins, KM_SLEEP);
    188 	for (pin = 0; pin < sc->sc_config->num_pins; pin++) {
    189 		sc->sc_pins[pin].pin_num = pin;
    190 		sc->sc_pins[pin].pin_caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
    191 		sc->sc_pins[pin].pin_intrcaps =
    192 		    GPIO_INTR_POS_EDGE | GPIO_INTR_NEG_EDGE |
    193 		    GPIO_INTR_DOUBLE_EDGE | GPIO_INTR_HIGH_LEVEL |
    194 		    GPIO_INTR_LOW_LEVEL | GPIO_INTR_MPSAFE;
    195 		/* It's not safe to read all pins, so leave pin state unknown */
    196 		sc->sc_pins[pin].pin_state = 0;
    197 	}
    198 
    199 	sc->sc_gc.gp_cookie = sc;
    200 	sc->sc_gc.gp_pin_read = qcomgpio_pin_read;
    201 	sc->sc_gc.gp_pin_write = qcomgpio_pin_write;
    202 	sc->sc_gc.gp_pin_ctl = qcomgpio_pin_ctl;
    203 	sc->sc_gc.gp_intr_establish = qcomgpio_intr_establish;
    204 	sc->sc_gc.gp_intr_disestablish = qcomgpio_intr_disestablish;
    205 	sc->sc_gc.gp_intr_str = qcomgpio_intr_str;
    206 	sc->sc_gc.gp_intr_mask = qcomgpio_intr_mask;
    207 	sc->sc_gc.gp_intr_unmask = qcomgpio_intr_unmask;
    208 
    209 	rv = acpi_event_create_gpio(self, hdl, qcomgpio_register_event, sc);
    210 	if (ACPI_FAILURE(rv)) {
    211 		if (rv != AE_NOT_FOUND) {
    212 			aprint_error_dev(self, "failed to create events: %s\n",
    213 			    AcpiFormatException(rv));
    214 		}
    215 		goto done;
    216 	}
    217 
    218 	ih = acpi_intr_establish(self, (uint64_t)(uintptr_t)hdl,
    219 	    IPL_VM, false, qcomgpio_intr, sc, device_xname(self));
    220 	if (ih == NULL) {
    221 		aprint_error_dev(self, "couldn't establish interrupt\n");
    222 		goto done;
    223 	}
    224 
    225 	memset(&gba, 0, sizeof(gba));
    226 	gba.gba_gc = &sc->sc_gc;
    227 	gba.gba_pins = sc->sc_pins;
    228 	gba.gba_npins = sc->sc_config->num_pins;
    229 	sc->sc_gpiodev = config_found(self, &gba, gpiobus_print,
    230 	    CFARGS(.iattr = "gpiobus"));
    231 	if (sc->sc_gpiodev != NULL) {
    232 		acpi_gpio_register(aa->aa_node, self,
    233 		    qcomgpio_acpi_translate, sc);
    234 	}
    235 
    236 done:
    237 	acpi_resource_cleanup(&res);
    238 }
    239 
    240 static int
    241 qcomgpio_acpi_translate(void *priv, ACPI_INTEGER pin, void **gpiop)
    242 {
    243 	struct qcomgpio_softc * const sc = priv;
    244 	int xpin;
    245 
    246 	xpin = sc->sc_config->translate(pin);
    247 
    248 	aprint_debug_dev(sc->sc_dev, "translate %#lx -> %u\n", pin, xpin);
    249 
    250 	if (gpiop != NULL) {
    251 		if (sc->sc_gpiodev != NULL) {
    252 			*gpiop = device_private(sc->sc_gpiodev);
    253 		} else {
    254 			device_printf(sc->sc_dev,
    255 			    "no gpiodev for pin %#lx -> %u\n", pin, xpin);
    256 			xpin = -1;
    257 		}
    258 	}
    259 
    260 	return xpin;
    261 }
    262 
    263 static int
    264 qcomgpio_acpi_event(void *priv)
    265 {
    266 	struct acpi_event * const ev = priv;
    267 
    268 	acpi_event_notify(ev);
    269 
    270 	return 1;
    271 }
    272 
    273 static void
    274 qcomgpio_register_event(void *priv, struct acpi_event *ev,
    275     ACPI_RESOURCE_GPIO *gpio)
    276 {
    277 	struct qcomgpio_softc * const sc = priv;
    278 	int irqmode;
    279 	void *ih;
    280 
    281 	const int pin = qcomgpio_acpi_translate(sc, gpio->PinTable[0], NULL);
    282 
    283 	if (pin < 0) {
    284 		aprint_error_dev(sc->sc_dev,
    285 		    "ignoring event for pin %#x (out of range)\n",
    286 		    gpio->PinTable[0]);
    287 		return;
    288 	}
    289 
    290 	if (gpio->Triggering == ACPI_LEVEL_SENSITIVE) {
    291 		irqmode = gpio->Polarity == ACPI_ACTIVE_HIGH ?
    292 		    GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL;
    293 	} else {
    294 		KASSERT(gpio->Triggering == ACPI_EDGE_SENSITIVE);
    295 		if (gpio->Polarity == ACPI_ACTIVE_LOW) {
    296 			irqmode = GPIO_INTR_NEG_EDGE;
    297 		} else if (gpio->Polarity == ACPI_ACTIVE_HIGH) {
    298 			irqmode = GPIO_INTR_POS_EDGE;
    299 		} else {
    300 			KASSERT(gpio->Polarity == ACPI_ACTIVE_BOTH);
    301 			irqmode = GPIO_INTR_DOUBLE_EDGE;
    302 		}
    303 	}
    304 
    305 	ih = qcomgpio_intr_establish(sc, pin, IPL_VM, irqmode,
    306 	    qcomgpio_acpi_event, ev);
    307 	if (ih == NULL) {
    308 		aprint_error_dev(sc->sc_dev,
    309 		    "couldn't register event for pin %#x\n",
    310 		    gpio->PinTable[0]);
    311 	}
    312 }
    313 
    314 static int
    315 qcomgpio_pin_read(void *priv, int pin)
    316 {
    317 	struct qcomgpio_softc * const sc = priv;
    318 	uint32_t val;
    319 
    320 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    321 		return 0;
    322 	}
    323 
    324 	val = RD4(sc, TLMM_GPIO_IN_OUT(pin));
    325 	return (val & TLMM_GPIO_IN_OUT_GPIO_IN) != 0;
    326 }
    327 
    328 static void
    329 qcomgpio_pin_write(void *priv, int pin, int pinval)
    330 {
    331 	struct qcomgpio_softc * const sc = priv;
    332 	uint32_t val;
    333 
    334 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    335 		return;
    336 	}
    337 
    338 	val = RD4(sc, TLMM_GPIO_IN_OUT(pin));
    339 	if (pinval) {
    340 		val |= TLMM_GPIO_IN_OUT_GPIO_OUT;
    341 	} else {
    342 		val &= ~TLMM_GPIO_IN_OUT_GPIO_OUT;
    343 	}
    344 	WR4(sc, TLMM_GPIO_IN_OUT(pin), val);
    345 }
    346 
    347 static void
    348 qcomgpio_pin_ctl(void *priv, int pin, int flags)
    349 {
    350 	/* Nothing to do here, as firmware has already configured pins. */
    351 }
    352 
    353 static void *
    354 qcomgpio_intr_establish(void *priv, int pin, int ipl, int irqmode,
    355 			int (*func)(void *), void *arg)
    356 {
    357 	struct qcomgpio_softc * const sc = priv;
    358 	struct qcomgpio_intr_handler *qih, *qihp;
    359 	uint32_t dect, pol;
    360 	uint32_t val;
    361 
    362 	if (pin < 0 || pin >= sc->sc_config->num_pins) {
    363 		return NULL;
    364 	}
    365 	if (ipl != IPL_VM) {
    366 		device_printf(sc->sc_dev, "%s: only IPL_VM supported\n",
    367 		    __func__);
    368 		return NULL;
    369 	}
    370 
    371 	qih = kmem_alloc(sizeof(*qih), KM_SLEEP);
    372 	qih->ih_func = func;
    373 	qih->ih_arg = arg;
    374 	qih->ih_pin = pin;
    375 
    376 	mutex_enter(&sc->sc_lock);
    377 
    378 	LIST_FOREACH(qihp, &sc->sc_intrs, ih_list) {
    379 		if (qihp->ih_pin == qih->ih_pin) {
    380 			mutex_exit(&sc->sc_lock);
    381 			kmem_free(qih, sizeof(*qih));
    382 			device_printf(sc->sc_dev,
    383 			    "%s: pin %d already establish\n", __func__, pin);
    384 			return NULL;
    385 		}
    386 	}
    387 
    388 	LIST_INSERT_HEAD(&sc->sc_intrs, qih, ih_list);
    389 
    390 	if ((irqmode & GPIO_INTR_LEVEL_MASK) != 0) {
    391 		dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_LEVEL;
    392 		pol = (irqmode & GPIO_INTR_HIGH_LEVEL) != 0 ?
    393 		    TLMM_GPIO_INTR_CFG_INTR_POL_CTL : 0;
    394 	} else {
    395 		KASSERT((irqmode & GPIO_INTR_EDGE_MASK) != 0);
    396 		if ((irqmode & GPIO_INTR_NEG_EDGE) != 0) {
    397 			dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_NEG;
    398 			pol = TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
    399 		} else if ((irqmode & GPIO_INTR_POS_EDGE) != 0) {
    400 			dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_POS;
    401 			pol = TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
    402 		} else {
    403 			KASSERT((irqmode & GPIO_INTR_DOUBLE_EDGE) != 0);
    404 			dect = TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_EDGE_BOTH;
    405 			pol = 0;
    406 		}
    407 	}
    408 
    409 	val = RD4(sc, TLMM_GPIO_INTR_CFG(pin));
    410 	val &= ~TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK;
    411 	val |= __SHIFTIN(dect, TLMM_GPIO_INTR_CFG_INTR_DECT_CTL_MASK);
    412 	val &= ~TLMM_GPIO_INTR_CFG_INTR_POL_CTL;
    413 	val |= pol;
    414 	val &= ~TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK;
    415 	val |= __SHIFTIN(TLMM_GPIO_INTR_CFG_TARGET_PROC_RPM,
    416 			 TLMM_GPIO_INTR_CFG_TARGET_PROC_MASK);
    417 	val |= TLMM_GPIO_INTR_CFG_INTR_RAW_STATUS_EN;
    418 	val |= TLMM_GPIO_INTR_CFG_INTR_ENABLE;
    419 	WR4(sc, TLMM_GPIO_INTR_CFG(pin), val);
    420 
    421 	mutex_exit(&sc->sc_lock);
    422 
    423 	return qih;
    424 }
    425 
    426 static void
    427 qcomgpio_intr_disestablish(void *priv, void *ih)
    428 {
    429 	struct qcomgpio_softc * const sc = priv;
    430 	struct qcomgpio_intr_handler *qih = ih;
    431 	uint32_t val;
    432 
    433 	mutex_enter(&sc->sc_lock);
    434 
    435 	LIST_REMOVE(qih, ih_list);
    436 
    437 	val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
    438 	val &= ~TLMM_GPIO_INTR_CFG_INTR_ENABLE;
    439 	WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
    440 
    441 	mutex_exit(&sc->sc_lock);
    442 
    443 	kmem_free(qih, sizeof(*qih));
    444 }
    445 
    446 static bool
    447 qcomgpio_intr_str(void *priv, int pin, int irqmode, char *buf, size_t buflen)
    448 {
    449 	struct qcomgpio_softc * const sc = priv;
    450 	int rv;
    451 
    452 	rv = snprintf(buf, buflen, "%s pin %d", device_xname(sc->sc_dev), pin);
    453 
    454 	return rv < buflen;
    455 }
    456 
    457 static void
    458 qcomgpio_intr_mask(void *priv, void *ih)
    459 {
    460 	struct qcomgpio_softc * const sc = priv;
    461 	struct qcomgpio_intr_handler *qih = ih;
    462 	uint32_t val;
    463 
    464 	val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
    465 	val &= ~TLMM_GPIO_INTR_CFG_INTR_ENABLE;
    466 	WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
    467 }
    468 
    469 static void
    470 qcomgpio_intr_unmask(void *priv, void *ih)
    471 {
    472 	struct qcomgpio_softc * const sc = priv;
    473 	struct qcomgpio_intr_handler *qih = ih;
    474 	uint32_t val;
    475 
    476 	val = RD4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin));
    477 	val |= TLMM_GPIO_INTR_CFG_INTR_ENABLE;
    478 	WR4(sc, TLMM_GPIO_INTR_CFG(qih->ih_pin), val);
    479 }
    480 
    481 static int
    482 qcomgpio_intr(void *priv)
    483 {
    484 	struct qcomgpio_softc * const sc = priv;
    485 	struct qcomgpio_intr_handler *qih;
    486 	int rv = 0;
    487 
    488 	mutex_enter(&sc->sc_lock);
    489 
    490 	LIST_FOREACH(qih, &sc->sc_intrs, ih_list) {
    491 		const int pin = qih->ih_pin;
    492 		uint32_t val;
    493 
    494 		val = RD4(sc, TLMM_GPIO_INTR_STATUS(pin));
    495 		if ((val & TLMM_GPIO_INTR_STATUS_INTR_STATUS) != 0) {
    496 			rv |= qih->ih_func(qih->ih_arg);
    497 
    498 			val &= ~TLMM_GPIO_INTR_STATUS_INTR_STATUS;
    499 			WR4(sc, TLMM_GPIO_INTR_STATUS(pin), val);
    500 		}
    501 	}
    502 
    503 	mutex_exit(&sc->sc_lock);
    504 
    505 	return rv;
    506 }
    507