2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 Tom Jones <tj@enoti.me>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, 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
31 * Copyright (c) 2016 Mark Kettenis
33 * Permission to use, copy, modify, and distribute this software for any
34 * purpose with or without fee is hereby granted, provided that the above
35 * copyright notice and this permission notice appear in all copies.
37 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
38 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
39 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
40 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
42 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
43 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/clock.h>
53 #include <sys/kernel.h>
54 #include <sys/module.h>
55 #include <sys/endian.h>
57 #include <sys/types.h>
58 #include <sys/malloc.h>
60 #include <machine/bus.h>
61 #include <machine/resource.h>
63 #include <contrib/dev/acpica/include/acpi.h>
64 #include <contrib/dev/acpica/include/accommon.h>
66 #include <dev/acpica/acpivar.h>
67 #include <dev/gpio/gpiobusvar.h>
69 #include "opt_platform.h"
73 #include "chvgpio_reg.h"
76 * Macros for driver mutex locking
78 #define CHVGPIO_LOCK(_sc) mtx_lock_spin(&(_sc)->sc_mtx)
79 #define CHVGPIO_UNLOCK(_sc) mtx_unlock_spin(&(_sc)->sc_mtx)
80 #define CHVGPIO_LOCK_INIT(_sc) \
81 mtx_init(&_sc->sc_mtx, device_get_nameunit((_sc)->sc_dev), \
83 #define CHVGPIO_LOCK_DESTROY(_sc) mtx_destroy(&(_sc)->sc_mtx)
84 #define CHVGPIO_ASSERT_LOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_OWNED)
85 #define CHVGPIO_ASSERT_UNLOCKED(_sc) mtx_assert(&(_sc)->sc_mtx, MA_NOTOWNED)
87 struct chvgpio_softc {
92 ACPI_HANDLE sc_handle;
95 struct resource *sc_mem_res;
98 struct resource *sc_irq_res;
101 const char *sc_bank_prefix;
105 const char **sc_pin_names;
108 static void chvgpio_intr(void *);
109 static int chvgpio_probe(device_t);
110 static int chvgpio_attach(device_t);
111 static int chvgpio_detach(device_t);
114 chvgpio_pad_cfg0_offset(int pin)
116 return (CHVGPIO_PAD_CFG0 + 1024 * (pin / 15) + 8 * (pin % 15));
120 chvgpio_read_pad_cfg0(struct chvgpio_softc *sc, int pin)
122 return bus_read_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin));
126 chvgpio_write_pad_cfg0(struct chvgpio_softc *sc, int pin, uint32_t val)
128 bus_write_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin), val);
132 chvgpio_read_pad_cfg1(struct chvgpio_softc *sc, int pin)
134 return bus_read_4(sc->sc_mem_res, chvgpio_pad_cfg0_offset(pin) + 4);
138 chvgpio_get_bus(device_t dev)
140 struct chvgpio_softc *sc;
142 sc = device_get_softc(dev);
144 return (sc->sc_busdev);
148 chvgpio_pin_max(device_t dev, int *maxpin)
150 struct chvgpio_softc *sc;
152 sc = device_get_softc(dev);
154 *maxpin = sc->sc_npins - 1;
160 chvgpio_valid_pin(struct chvgpio_softc *sc, int pin)
164 if ((pin / 15) >= sc->sc_ngroups)
166 if ((pin % 15) >= sc->sc_pins[pin / 15])
172 chvgpio_pin_getname(device_t dev, uint32_t pin, char *name)
174 struct chvgpio_softc *sc;
176 sc = device_get_softc(dev);
177 if (chvgpio_valid_pin(sc, pin) != 0)
180 /* return pin name from datasheet */
181 snprintf(name, GPIOMAXNAME, "%s", sc->sc_pin_names[pin]);
182 name[GPIOMAXNAME - 1] = '\0';
187 chvgpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
189 struct chvgpio_softc *sc;
191 sc = device_get_softc(dev);
192 if (chvgpio_valid_pin(sc, pin) != 0)
196 if (chvgpio_valid_pin(sc, pin))
197 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
203 chvgpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
205 struct chvgpio_softc *sc;
208 sc = device_get_softc(dev);
209 if (chvgpio_valid_pin(sc, pin) != 0)
214 /* Get the current pin state */
216 val = chvgpio_read_pad_cfg0(sc, pin);
218 if (val & CHVGPIO_PAD_CFG0_GPIOCFG_GPIO ||
219 val & CHVGPIO_PAD_CFG0_GPIOCFG_GPO)
220 *flags |= GPIO_PIN_OUTPUT;
222 if (val & CHVGPIO_PAD_CFG0_GPIOCFG_GPIO ||
223 val & CHVGPIO_PAD_CFG0_GPIOCFG_GPI)
224 *flags |= GPIO_PIN_INPUT;
226 val = chvgpio_read_pad_cfg1(sc, pin);
233 chvgpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
235 struct chvgpio_softc *sc;
239 sc = device_get_softc(dev);
240 if (chvgpio_valid_pin(sc, pin) != 0)
243 allowed = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
246 * Only direction flag allowed
248 if (flags & ~allowed)
252 * Not both directions simultaneously
254 if ((flags & allowed) == allowed)
257 /* Set the GPIO mode and state */
259 val = chvgpio_read_pad_cfg0(sc, pin);
260 if (flags & GPIO_PIN_INPUT)
261 val = val & CHVGPIO_PAD_CFG0_GPIOCFG_GPI;
262 if (flags & GPIO_PIN_OUTPUT)
263 val = val & CHVGPIO_PAD_CFG0_GPIOCFG_GPO;
264 chvgpio_write_pad_cfg0(sc, pin, val);
271 chvgpio_pin_set(device_t dev, uint32_t pin, unsigned int value)
273 struct chvgpio_softc *sc;
276 sc = device_get_softc(dev);
277 if (chvgpio_valid_pin(sc, pin) != 0)
281 val = chvgpio_read_pad_cfg0(sc, pin);
282 if (value == GPIO_PIN_LOW)
283 val = val & ~CHVGPIO_PAD_CFG0_GPIOTXSTATE;
285 val = val | CHVGPIO_PAD_CFG0_GPIOTXSTATE;
286 chvgpio_write_pad_cfg0(sc, pin, val);
293 chvgpio_pin_get(device_t dev, uint32_t pin, unsigned int *value)
295 struct chvgpio_softc *sc;
298 sc = device_get_softc(dev);
299 if (chvgpio_valid_pin(sc, pin) != 0)
305 val = chvgpio_read_pad_cfg0(sc, pin);
306 if (val & CHVGPIO_PAD_CFG0_GPIORXSTATE)
307 *value = GPIO_PIN_HIGH;
309 *value = GPIO_PIN_LOW;
317 chvgpio_pin_toggle(device_t dev, uint32_t pin)
319 struct chvgpio_softc *sc;
322 sc = device_get_softc(dev);
323 if (chvgpio_valid_pin(sc, pin) != 0)
329 val = chvgpio_read_pad_cfg0(sc, pin);
330 val = val ^ CHVGPIO_PAD_CFG0_GPIOTXSTATE;
331 chvgpio_write_pad_cfg0(sc, pin, val);
338 static char *chvgpio_hids[] = {
344 chvgpio_probe(device_t dev)
346 if (acpi_disabled("chvgpio") ||
347 ACPI_ID_PROBE(device_get_parent(dev), dev, chvgpio_hids) == NULL)
350 device_set_desc(dev, "Intel Cherry View GPIO");
355 chvgpio_attach(device_t dev)
357 struct chvgpio_softc *sc;
363 sc = device_get_softc(dev);
365 sc->sc_handle = acpi_get_handle(dev);
367 status = acpi_GetInteger(sc->sc_handle, "_UID", &uid);
368 if (ACPI_FAILURE(status)) {
369 device_printf(dev, "failed to read _UID\n");
373 CHVGPIO_LOCK_INIT(sc);
377 sc->sc_bank_prefix = SW_BANK_PREFIX;
378 sc->sc_pins = chv_southwest_pins;
379 sc->sc_pin_names = chv_southwest_pin_names;
382 sc->sc_bank_prefix = N_BANK_PREFIX;
383 sc->sc_pins = chv_north_pins;
384 sc->sc_pin_names = chv_north_pin_names;
387 sc->sc_bank_prefix = E_BANK_PREFIX;
388 sc->sc_pins = chv_east_pins;
389 sc->sc_pin_names = chv_east_pin_names;
392 sc->sc_bank_prefix = SE_BANK_PREFIX;
393 sc->sc_pins = chv_southeast_pins;
394 sc->sc_pin_names = chv_southeast_pin_names;
397 device_printf(dev, "invalid _UID value: %d\n", uid);
401 for (i = 0; sc->sc_pins[i] >= 0; i++) {
402 sc->sc_npins += sc->sc_pins[i];
407 sc->sc_mem_res = bus_alloc_resource_any(sc->sc_dev, SYS_RES_MEMORY,
408 &sc->sc_mem_rid, RF_ACTIVE);
409 if (sc->sc_mem_res == NULL) {
410 CHVGPIO_LOCK_DESTROY(sc);
411 device_printf(dev, "can't allocate memory resource\n");
415 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
416 &sc->sc_irq_rid, RF_ACTIVE);
418 if (!sc->sc_irq_res) {
419 CHVGPIO_LOCK_DESTROY(sc);
420 bus_release_resource(dev, SYS_RES_MEMORY,
421 sc->sc_mem_rid, sc->sc_mem_res);
422 device_printf(dev, "can't allocate irq resource\n");
426 error = bus_setup_intr(sc->sc_dev, sc->sc_irq_res, INTR_TYPE_MISC | INTR_MPSAFE,
427 NULL, chvgpio_intr, sc, &sc->intr_handle);
431 device_printf(sc->sc_dev, "unable to setup irq: error %d\n", error);
432 CHVGPIO_LOCK_DESTROY(sc);
433 bus_release_resource(dev, SYS_RES_MEMORY,
434 sc->sc_mem_rid, sc->sc_mem_res);
435 bus_release_resource(dev, SYS_RES_IRQ,
436 sc->sc_irq_rid, sc->sc_irq_res);
440 /* Mask and ack all interrupts. */
441 bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_MASK, 0);
442 bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS, 0xffff);
444 sc->sc_busdev = gpiobus_attach_bus(dev);
445 if (sc->sc_busdev == NULL) {
446 CHVGPIO_LOCK_DESTROY(sc);
447 bus_release_resource(dev, SYS_RES_MEMORY,
448 sc->sc_mem_rid, sc->sc_mem_res);
449 bus_release_resource(dev, SYS_RES_IRQ,
450 sc->sc_irq_rid, sc->sc_irq_res);
458 chvgpio_intr(void *arg)
460 struct chvgpio_softc *sc = arg;
464 reg = bus_read_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS);
465 for (line = 0; line < 16; line++) {
466 if ((reg & (1 << line)) == 0)
468 bus_write_4(sc->sc_mem_res, CHVGPIO_INTERRUPT_STATUS, 1 << line);
473 chvgpio_detach(device_t dev)
475 struct chvgpio_softc *sc;
476 sc = device_get_softc(dev);
479 gpiobus_detach_bus(dev);
481 if (sc->intr_handle != NULL)
482 bus_teardown_intr(sc->sc_dev, sc->sc_irq_res, sc->intr_handle);
483 if (sc->sc_irq_res != NULL)
484 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid, sc->sc_irq_res);
485 if (sc->sc_mem_res != NULL)
486 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_mem_rid, sc->sc_mem_res);
488 CHVGPIO_LOCK_DESTROY(sc);
493 static device_method_t chvgpio_methods[] = {
494 DEVMETHOD(device_probe, chvgpio_probe),
495 DEVMETHOD(device_attach, chvgpio_attach),
496 DEVMETHOD(device_detach, chvgpio_detach),
499 DEVMETHOD(gpio_get_bus, chvgpio_get_bus),
500 DEVMETHOD(gpio_pin_max, chvgpio_pin_max),
501 DEVMETHOD(gpio_pin_getname, chvgpio_pin_getname),
502 DEVMETHOD(gpio_pin_getflags, chvgpio_pin_getflags),
503 DEVMETHOD(gpio_pin_getcaps, chvgpio_pin_getcaps),
504 DEVMETHOD(gpio_pin_setflags, chvgpio_pin_setflags),
505 DEVMETHOD(gpio_pin_get, chvgpio_pin_get),
506 DEVMETHOD(gpio_pin_set, chvgpio_pin_set),
507 DEVMETHOD(gpio_pin_toggle, chvgpio_pin_toggle),
512 static driver_t chvgpio_driver = {
514 .methods = chvgpio_methods,
515 .size = sizeof(struct chvgpio_softc)
518 static devclass_t chvgpio_devclass;
519 DRIVER_MODULE(chvgpio, acpi, chvgpio_driver, chvgpio_devclass, NULL , NULL);
520 MODULE_DEPEND(chvgpio, acpi, 1, 1, 1);
521 MODULE_DEPEND(chvgpio, gpiobus, 1, 1, 1);
523 MODULE_VERSION(chvgpio, 1);