2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * X-Powers AXP813/818 PMU for Allwinner SoCs
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/eventhandler.h>
41 #include <sys/kernel.h>
42 #include <sys/reboot.h>
45 #include <sys/module.h>
46 #include <machine/bus.h>
48 #include <dev/iicbus/iicbus.h>
49 #include <dev/iicbus/iiconf.h>
51 #include <dev/gpio/gpiobusvar.h>
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
56 #include "iicbus_if.h"
59 #define AXP_ICTYPE 0x03
60 #define AXP_POWERBAT 0x32
61 #define AXP_POWERBAT_SHUTDOWN (1 << 7)
62 #define AXP_IRQEN1 0x40
63 #define AXP_IRQEN2 0x41
64 #define AXP_IRQEN3 0x42
65 #define AXP_IRQEN4 0x43
66 #define AXP_IRQEN5 0x44
67 #define AXP_IRQEN5_POKSIRQ (1 << 4)
68 #define AXP_IRQEN6 0x45
69 #define AXP_IRQSTAT5 0x4c
70 #define AXP_IRQSTAT5_POKSIRQ (1 << 4)
71 #define AXP_GPIO0_CTRL 0x90
72 #define AXP_GPIO1_CTRL 0x92
73 #define AXP_GPIO_FUNC (0x7 << 0)
74 #define AXP_GPIO_FUNC_SHIFT 0
75 #define AXP_GPIO_FUNC_DRVLO 0
76 #define AXP_GPIO_FUNC_DRVHI 1
77 #define AXP_GPIO_FUNC_INPUT 2
78 #define AXP_GPIO_SIGBIT 0x94
79 #define AXP_GPIO_PD 0x97
85 { "GPIO0", AXP_GPIO0_CTRL },
86 { "GPIO1", AXP_GPIO1_CTRL },
89 static struct ofw_compat_data compat_data[] = {
90 { "x-powers,axp813", 1 },
91 { "x-powers,axp818", 1 },
95 static struct resource_spec axp81x_spec[] = {
96 { SYS_RES_IRQ, 0, RF_ACTIVE },
100 struct axp81x_softc {
101 struct resource *res;
109 #define AXP_LOCK(sc) mtx_lock(&(sc)->mtx)
110 #define AXP_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
113 axp81x_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
115 struct axp81x_softc *sc;
116 struct iic_msg msg[2];
118 sc = device_get_softc(dev);
120 msg[0].slave = sc->addr;
121 msg[0].flags = IIC_M_WR;
125 msg[1].slave = sc->addr;
126 msg[1].flags = IIC_M_RD;
130 return (iicbus_transfer(dev, msg, 2));
134 axp81x_write(device_t dev, uint8_t reg, uint8_t val)
136 struct axp81x_softc *sc;
137 struct iic_msg msg[2];
139 sc = device_get_softc(dev);
141 msg[0].slave = sc->addr;
142 msg[0].flags = IIC_M_WR;
146 msg[1].slave = sc->addr;
147 msg[1].flags = IIC_M_WR;
151 return (iicbus_transfer(dev, msg, 2));
155 axp81x_shutdown(void *devp, int howto)
159 if ((howto & RB_POWEROFF) == 0)
165 device_printf(dev, "Shutdown AXP81x\n");
167 axp81x_write(dev, AXP_POWERBAT, AXP_POWERBAT_SHUTDOWN);
171 axp81x_intr(void *arg)
173 struct axp81x_softc *sc;
179 sc = device_get_softc(dev);
181 error = axp81x_read(dev, AXP_IRQSTAT5, &val, 1);
186 if ((val & AXP_IRQSTAT5_POKSIRQ) != 0) {
188 device_printf(dev, "Power button pressed\n");
189 shutdown_nice(RB_POWEROFF);
192 axp81x_write(dev, AXP_IRQSTAT5, val);
197 axp81x_gpio_get_bus(device_t dev)
199 struct axp81x_softc *sc;
201 sc = device_get_softc(dev);
203 return (sc->gpiodev);
207 axp81x_gpio_pin_max(device_t dev, int *maxpin)
209 *maxpin = nitems(axp81x_pins) - 1;
215 axp81x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
217 if (pin >= nitems(axp81x_pins))
220 snprintf(name, GPIOMAXNAME, "%s", axp81x_pins[pin].name);
226 axp81x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
228 if (pin >= nitems(axp81x_pins))
231 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
237 axp81x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
239 struct axp81x_softc *sc;
243 if (pin >= nitems(axp81x_pins))
246 sc = device_get_softc(dev);
249 THREAD_SLEEPING_OK();
250 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
252 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
253 if (func == AXP_GPIO_FUNC_INPUT)
254 *flags = GPIO_PIN_INPUT;
255 else if (func == AXP_GPIO_FUNC_DRVLO ||
256 func == AXP_GPIO_FUNC_DRVHI)
257 *flags = GPIO_PIN_OUTPUT;
261 THREAD_NO_SLEEPING();
268 axp81x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
270 struct axp81x_softc *sc;
274 if (pin >= nitems(axp81x_pins))
277 sc = device_get_softc(dev);
280 THREAD_SLEEPING_OK();
281 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
283 data &= ~AXP_GPIO_FUNC;
284 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) {
285 if ((flags & GPIO_PIN_OUTPUT) == 0)
286 data |= AXP_GPIO_FUNC_INPUT;
288 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
290 THREAD_NO_SLEEPING();
297 axp81x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
299 struct axp81x_softc *sc;
303 if (pin >= nitems(axp81x_pins))
306 sc = device_get_softc(dev);
309 THREAD_SLEEPING_OK();
310 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
312 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
314 case AXP_GPIO_FUNC_DRVLO:
317 case AXP_GPIO_FUNC_DRVHI:
320 case AXP_GPIO_FUNC_INPUT:
321 error = axp81x_read(dev, AXP_GPIO_SIGBIT, &data, 1);
323 *val = (data & (1 << pin)) ? 1 : 0;
330 THREAD_NO_SLEEPING();
337 axp81x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
339 struct axp81x_softc *sc;
343 if (pin >= nitems(axp81x_pins))
346 sc = device_get_softc(dev);
349 THREAD_SLEEPING_OK();
350 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
352 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
354 case AXP_GPIO_FUNC_DRVLO:
355 case AXP_GPIO_FUNC_DRVHI:
356 data &= ~AXP_GPIO_FUNC;
357 data |= (val << AXP_GPIO_FUNC_SHIFT);
365 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
366 THREAD_NO_SLEEPING();
374 axp81x_gpio_pin_toggle(device_t dev, uint32_t pin)
376 struct axp81x_softc *sc;
380 if (pin >= nitems(axp81x_pins))
383 sc = device_get_softc(dev);
386 THREAD_SLEEPING_OK();
387 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
389 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
391 case AXP_GPIO_FUNC_DRVLO:
392 data &= ~AXP_GPIO_FUNC;
393 data |= (AXP_GPIO_FUNC_DRVHI << AXP_GPIO_FUNC_SHIFT);
395 case AXP_GPIO_FUNC_DRVHI:
396 data &= ~AXP_GPIO_FUNC;
397 data |= (AXP_GPIO_FUNC_DRVLO << AXP_GPIO_FUNC_SHIFT);
405 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
406 THREAD_NO_SLEEPING();
413 axp81x_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent,
414 int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
416 if (gpios[0] >= nitems(axp81x_pins))
426 axp81x_get_node(device_t dev, device_t bus)
428 return (ofw_bus_get_node(dev));
432 axp81x_probe(device_t dev)
434 if (!ofw_bus_status_okay(dev))
437 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
440 device_set_desc(dev, "X-Powers AXP81x Power Management Unit");
442 return (BUS_PROBE_DEFAULT);
446 axp81x_attach(device_t dev)
448 struct axp81x_softc *sc;
452 sc = device_get_softc(dev);
454 sc->addr = iicbus_get_addr(dev);
455 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
457 error = bus_alloc_resources(dev, axp81x_spec, &sc->res);
459 device_printf(dev, "cannot allocate resources for device\n");
464 axp81x_read(dev, AXP_ICTYPE, &chip_id, 1);
465 device_printf(dev, "chip ID 0x%02x\n", chip_id);
468 /* Enable IRQ on short power key press */
469 axp81x_write(dev, AXP_IRQEN1, 0);
470 axp81x_write(dev, AXP_IRQEN2, 0);
471 axp81x_write(dev, AXP_IRQEN3, 0);
472 axp81x_write(dev, AXP_IRQEN4, 0);
473 axp81x_write(dev, AXP_IRQEN5, AXP_IRQEN5_POKSIRQ);
474 axp81x_write(dev, AXP_IRQEN6, 0);
476 /* Install interrupt handler */
477 error = bus_setup_intr(dev, sc->res, INTR_TYPE_MISC | INTR_MPSAFE,
478 NULL, axp81x_intr, dev, &sc->ih);
480 device_printf(dev, "cannot setup interrupt handler\n");
484 EVENTHANDLER_REGISTER(shutdown_final, axp81x_shutdown, dev,
487 sc->gpiodev = gpiobus_attach_bus(dev);
492 static device_method_t axp81x_methods[] = {
493 /* Device interface */
494 DEVMETHOD(device_probe, axp81x_probe),
495 DEVMETHOD(device_attach, axp81x_attach),
498 DEVMETHOD(gpio_get_bus, axp81x_gpio_get_bus),
499 DEVMETHOD(gpio_pin_max, axp81x_gpio_pin_max),
500 DEVMETHOD(gpio_pin_getname, axp81x_gpio_pin_getname),
501 DEVMETHOD(gpio_pin_getcaps, axp81x_gpio_pin_getcaps),
502 DEVMETHOD(gpio_pin_getflags, axp81x_gpio_pin_getflags),
503 DEVMETHOD(gpio_pin_setflags, axp81x_gpio_pin_setflags),
504 DEVMETHOD(gpio_pin_get, axp81x_gpio_pin_get),
505 DEVMETHOD(gpio_pin_set, axp81x_gpio_pin_set),
506 DEVMETHOD(gpio_pin_toggle, axp81x_gpio_pin_toggle),
507 DEVMETHOD(gpio_map_gpios, axp81x_gpio_map_gpios),
509 /* OFW bus interface */
510 DEVMETHOD(ofw_bus_get_node, axp81x_get_node),
515 static driver_t axp81x_driver = {
518 sizeof(struct axp81x_softc),
521 static devclass_t axp81x_devclass;
522 extern devclass_t ofwgpiobus_devclass, gpioc_devclass;
523 extern driver_t ofw_gpiobus_driver, gpioc_driver;
525 DRIVER_MODULE(axp81x, iicbus, axp81x_driver, axp81x_devclass, 0, 0);
526 DRIVER_MODULE(ofw_gpiobus, axp81x_pmu, ofw_gpiobus_driver,
527 ofwgpiobus_devclass, 0, 0);
528 DRIVER_MODULE(gpioc, axp81x_pmu, gpioc_driver, gpioc_devclass, 0, 0);
529 MODULE_VERSION(axp81x, 1);
530 MODULE_DEPEND(axp81x, iicbus, 1, 1, 1);