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>
44 #include <sys/module.h>
45 #include <machine/bus.h>
47 #include <dev/iicbus/iicbus.h>
48 #include <dev/iicbus/iiconf.h>
50 #include <dev/gpio/gpiobusvar.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
55 #include <dev/extres/regulator/regulator.h>
58 #include "iicbus_if.h"
59 #include "regdev_if.h"
61 MALLOC_DEFINE(M_AXP81X_REG, "AXP81x regulator", "AXP81x power regulator");
63 #define AXP_ICTYPE 0x03
64 #define AXP_POWERCTL2 0x12
65 #define AXP_POWERCTL2_DC1SW (1 << 7)
66 #define AXP_POWERBAT 0x32
67 #define AXP_POWERBAT_SHUTDOWN (1 << 7)
68 #define AXP_IRQEN1 0x40
69 #define AXP_IRQEN2 0x41
70 #define AXP_IRQEN3 0x42
71 #define AXP_IRQEN4 0x43
72 #define AXP_IRQEN5 0x44
73 #define AXP_IRQEN5_POKSIRQ (1 << 4)
74 #define AXP_IRQEN6 0x45
75 #define AXP_IRQSTAT5 0x4c
76 #define AXP_IRQSTAT5_POKSIRQ (1 << 4)
77 #define AXP_GPIO0_CTRL 0x90
78 #define AXP_GPIO1_CTRL 0x92
79 #define AXP_GPIO_FUNC (0x7 << 0)
80 #define AXP_GPIO_FUNC_SHIFT 0
81 #define AXP_GPIO_FUNC_DRVLO 0
82 #define AXP_GPIO_FUNC_DRVHI 1
83 #define AXP_GPIO_FUNC_INPUT 2
84 #define AXP_GPIO_SIGBIT 0x94
85 #define AXP_GPIO_PD 0x97
91 { "GPIO0", AXP_GPIO0_CTRL },
92 { "GPIO1", AXP_GPIO1_CTRL },
95 static struct ofw_compat_data compat_data[] = {
96 { "x-powers,axp813", 1 },
97 { "x-powers,axp818", 1 },
101 static struct resource_spec axp81x_spec[] = {
102 { SYS_RES_IRQ, 0, RF_ACTIVE },
106 struct axp81x_regdef {
118 static struct axp81x_regdef axp81x_regdefs[] = {
120 .id = AXP81X_REG_ID_DC1SW,
122 .enable_reg = AXP_POWERCTL2,
123 .enable_mask = AXP_POWERCTL2_DC1SW,
129 struct axp81x_reg_sc {
130 struct regnode *regnode;
132 struct axp81x_regdef *def;
134 struct regnode_std_param *param;
137 struct axp81x_softc {
138 struct resource *res;
146 struct axp81x_reg_sc **regs;
150 #define AXP_LOCK(sc) mtx_lock(&(sc)->mtx)
151 #define AXP_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
154 axp81x_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
156 struct axp81x_softc *sc;
157 struct iic_msg msg[2];
159 sc = device_get_softc(dev);
161 msg[0].slave = sc->addr;
162 msg[0].flags = IIC_M_WR;
166 msg[1].slave = sc->addr;
167 msg[1].flags = IIC_M_RD;
171 return (iicbus_transfer(dev, msg, 2));
175 axp81x_write(device_t dev, uint8_t reg, uint8_t val)
177 struct axp81x_softc *sc;
178 struct iic_msg msg[2];
180 sc = device_get_softc(dev);
182 msg[0].slave = sc->addr;
183 msg[0].flags = IIC_M_WR;
187 msg[1].slave = sc->addr;
188 msg[1].flags = IIC_M_WR;
192 return (iicbus_transfer(dev, msg, 2));
196 axp81x_regnode_init(struct regnode *regnode)
202 axp81x_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
204 struct axp81x_reg_sc *sc;
207 sc = regnode_get_softc(regnode);
209 axp81x_read(sc->base_dev, sc->def->enable_reg, &val, 1);
211 val |= sc->def->enable_mask;
213 val &= ~sc->def->enable_mask;
214 axp81x_write(sc->base_dev, sc->def->enable_reg, val);
222 axp81x_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
223 int max_uvolt, int *udelay)
229 axp81x_regnode_get_voltage(struct regnode *regnode, int *uvolt)
234 static regnode_method_t axp81x_regnode_methods[] = {
235 /* Regulator interface */
236 REGNODEMETHOD(regnode_init, axp81x_regnode_init),
237 REGNODEMETHOD(regnode_enable, axp81x_regnode_enable),
238 REGNODEMETHOD(regnode_set_voltage, axp81x_regnode_set_voltage),
239 REGNODEMETHOD(regnode_get_voltage, axp81x_regnode_get_voltage),
242 DEFINE_CLASS_1(axp81x_regnode, axp81x_regnode_class, axp81x_regnode_methods,
243 sizeof(struct axp81x_reg_sc), regnode_class);
246 axp81x_shutdown(void *devp, int howto)
250 if ((howto & RB_POWEROFF) == 0)
256 device_printf(dev, "Shutdown AXP81x\n");
258 axp81x_write(dev, AXP_POWERBAT, AXP_POWERBAT_SHUTDOWN);
262 axp81x_intr(void *arg)
264 struct axp81x_softc *sc;
270 sc = device_get_softc(dev);
272 error = axp81x_read(dev, AXP_IRQSTAT5, &val, 1);
277 if ((val & AXP_IRQSTAT5_POKSIRQ) != 0) {
279 device_printf(dev, "Power button pressed\n");
280 shutdown_nice(RB_POWEROFF);
283 axp81x_write(dev, AXP_IRQSTAT5, val);
288 axp81x_gpio_get_bus(device_t dev)
290 struct axp81x_softc *sc;
292 sc = device_get_softc(dev);
294 return (sc->gpiodev);
298 axp81x_gpio_pin_max(device_t dev, int *maxpin)
300 *maxpin = nitems(axp81x_pins) - 1;
306 axp81x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
308 if (pin >= nitems(axp81x_pins))
311 snprintf(name, GPIOMAXNAME, "%s", axp81x_pins[pin].name);
317 axp81x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
319 if (pin >= nitems(axp81x_pins))
322 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
328 axp81x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
330 struct axp81x_softc *sc;
334 if (pin >= nitems(axp81x_pins))
337 sc = device_get_softc(dev);
340 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
342 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
343 if (func == AXP_GPIO_FUNC_INPUT)
344 *flags = GPIO_PIN_INPUT;
345 else if (func == AXP_GPIO_FUNC_DRVLO ||
346 func == AXP_GPIO_FUNC_DRVHI)
347 *flags = GPIO_PIN_OUTPUT;
357 axp81x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
359 struct axp81x_softc *sc;
363 if (pin >= nitems(axp81x_pins))
366 sc = device_get_softc(dev);
369 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
371 data &= ~AXP_GPIO_FUNC;
372 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) {
373 if ((flags & GPIO_PIN_OUTPUT) == 0)
374 data |= AXP_GPIO_FUNC_INPUT;
376 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
384 axp81x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
386 struct axp81x_softc *sc;
390 if (pin >= nitems(axp81x_pins))
393 sc = device_get_softc(dev);
396 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
398 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
400 case AXP_GPIO_FUNC_DRVLO:
403 case AXP_GPIO_FUNC_DRVHI:
406 case AXP_GPIO_FUNC_INPUT:
407 error = axp81x_read(dev, AXP_GPIO_SIGBIT, &data, 1);
409 *val = (data & (1 << pin)) ? 1 : 0;
422 axp81x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
424 struct axp81x_softc *sc;
428 if (pin >= nitems(axp81x_pins))
431 sc = device_get_softc(dev);
434 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
436 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
438 case AXP_GPIO_FUNC_DRVLO:
439 case AXP_GPIO_FUNC_DRVHI:
440 data &= ~AXP_GPIO_FUNC;
441 data |= (val << AXP_GPIO_FUNC_SHIFT);
449 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
457 axp81x_gpio_pin_toggle(device_t dev, uint32_t pin)
459 struct axp81x_softc *sc;
463 if (pin >= nitems(axp81x_pins))
466 sc = device_get_softc(dev);
469 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
471 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
473 case AXP_GPIO_FUNC_DRVLO:
474 data &= ~AXP_GPIO_FUNC;
475 data |= (AXP_GPIO_FUNC_DRVHI << AXP_GPIO_FUNC_SHIFT);
477 case AXP_GPIO_FUNC_DRVHI:
478 data &= ~AXP_GPIO_FUNC;
479 data |= (AXP_GPIO_FUNC_DRVLO << AXP_GPIO_FUNC_SHIFT);
487 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
494 axp81x_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent,
495 int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
497 if (gpios[0] >= nitems(axp81x_pins))
507 axp81x_get_node(device_t dev, device_t bus)
509 return (ofw_bus_get_node(dev));
512 static struct axp81x_reg_sc *
513 axp81x_reg_attach(device_t dev, phandle_t node,
514 struct axp81x_regdef *def)
516 struct axp81x_reg_sc *reg_sc;
517 struct regnode_init_def initdef;
518 struct regnode *regnode;
520 memset(&initdef, 0, sizeof(initdef));
521 regulator_parse_ofw_stdparam(dev, node, &initdef);
522 initdef.id = def->id;
523 initdef.ofw_node = node;
524 regnode = regnode_create(dev, &axp81x_regnode_class, &initdef);
525 if (regnode == NULL) {
526 device_printf(dev, "cannot create regulator\n");
530 reg_sc = regnode_get_softc(regnode);
531 reg_sc->regnode = regnode;
532 reg_sc->base_dev = dev;
534 reg_sc->xref = OF_xref_from_node(node);
535 reg_sc->param = regnode_get_stdparam(regnode);
537 regnode_register(regnode);
543 axp81x_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
546 struct axp81x_softc *sc;
549 sc = device_get_softc(dev);
550 for (i = 0; i < sc->nregs; i++) {
551 if (sc->regs[i] == NULL)
553 if (sc->regs[i]->xref == xref) {
554 *num = sc->regs[i]->def->id;
563 axp81x_probe(device_t dev)
565 if (!ofw_bus_status_okay(dev))
568 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
571 device_set_desc(dev, "X-Powers AXP81x Power Management Unit");
573 return (BUS_PROBE_DEFAULT);
577 axp81x_attach(device_t dev)
579 struct axp81x_softc *sc;
580 struct axp81x_reg_sc *reg;
582 phandle_t rnode, child;
585 sc = device_get_softc(dev);
587 sc->addr = iicbus_get_addr(dev);
588 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
590 error = bus_alloc_resources(dev, axp81x_spec, &sc->res);
592 device_printf(dev, "cannot allocate resources for device\n");
597 axp81x_read(dev, AXP_ICTYPE, &chip_id, 1);
598 device_printf(dev, "chip ID 0x%02x\n", chip_id);
601 sc->nregs = nitems(axp81x_regdefs);
602 sc->regs = malloc(sizeof(struct axp81x_reg_sc *) * sc->nregs,
603 M_AXP81X_REG, M_WAITOK | M_ZERO);
605 /* Attach known regulators that exist in the DT */
606 rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators");
608 for (i = 0; i < sc->nregs; i++) {
609 child = ofw_bus_find_child(rnode,
610 axp81x_regdefs[i].name);
613 reg = axp81x_reg_attach(dev, child, &axp81x_regdefs[i]);
616 "cannot attach regulator %s\n",
617 axp81x_regdefs[i].name);
624 /* Enable IRQ on short power key press */
625 axp81x_write(dev, AXP_IRQEN1, 0);
626 axp81x_write(dev, AXP_IRQEN2, 0);
627 axp81x_write(dev, AXP_IRQEN3, 0);
628 axp81x_write(dev, AXP_IRQEN4, 0);
629 axp81x_write(dev, AXP_IRQEN5, AXP_IRQEN5_POKSIRQ);
630 axp81x_write(dev, AXP_IRQEN6, 0);
632 /* Install interrupt handler */
633 error = bus_setup_intr(dev, sc->res, INTR_TYPE_MISC | INTR_MPSAFE,
634 NULL, axp81x_intr, dev, &sc->ih);
636 device_printf(dev, "cannot setup interrupt handler\n");
640 EVENTHANDLER_REGISTER(shutdown_final, axp81x_shutdown, dev,
643 sc->gpiodev = gpiobus_attach_bus(dev);
648 static device_method_t axp81x_methods[] = {
649 /* Device interface */
650 DEVMETHOD(device_probe, axp81x_probe),
651 DEVMETHOD(device_attach, axp81x_attach),
654 DEVMETHOD(gpio_get_bus, axp81x_gpio_get_bus),
655 DEVMETHOD(gpio_pin_max, axp81x_gpio_pin_max),
656 DEVMETHOD(gpio_pin_getname, axp81x_gpio_pin_getname),
657 DEVMETHOD(gpio_pin_getcaps, axp81x_gpio_pin_getcaps),
658 DEVMETHOD(gpio_pin_getflags, axp81x_gpio_pin_getflags),
659 DEVMETHOD(gpio_pin_setflags, axp81x_gpio_pin_setflags),
660 DEVMETHOD(gpio_pin_get, axp81x_gpio_pin_get),
661 DEVMETHOD(gpio_pin_set, axp81x_gpio_pin_set),
662 DEVMETHOD(gpio_pin_toggle, axp81x_gpio_pin_toggle),
663 DEVMETHOD(gpio_map_gpios, axp81x_gpio_map_gpios),
665 /* Regdev interface */
666 DEVMETHOD(regdev_map, axp81x_regdev_map),
668 /* OFW bus interface */
669 DEVMETHOD(ofw_bus_get_node, axp81x_get_node),
674 static driver_t axp81x_driver = {
677 sizeof(struct axp81x_softc),
680 static devclass_t axp81x_devclass;
681 extern devclass_t ofwgpiobus_devclass, gpioc_devclass;
682 extern driver_t ofw_gpiobus_driver, gpioc_driver;
684 EARLY_DRIVER_MODULE(axp81x, iicbus, axp81x_driver, axp81x_devclass, 0, 0,
685 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
686 EARLY_DRIVER_MODULE(ofw_gpiobus, axp81x_pmu, ofw_gpiobus_driver,
687 ofwgpiobus_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
688 DRIVER_MODULE(gpioc, axp81x_pmu, gpioc_driver, gpioc_devclass, 0, 0);
689 MODULE_VERSION(axp81x, 1);
690 MODULE_DEPEND(axp81x, iicbus, 1, 1, 1);