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_POWERCTL1 0x10
65 #define AXP_POWERCTL1_DCDC2 (1 << 1)
66 #define AXP_POWERCTL2 0x12
67 #define AXP_POWERCTL2_DC1SW (1 << 7)
68 #define AXP_VOLTCTL_DCDC2 0x21
69 #define AXP_VOLTCTL_STATUS (1 << 7)
70 #define AXP_VOLTCTL_MASK 0x7f
71 #define AXP_POWERBAT 0x32
72 #define AXP_POWERBAT_SHUTDOWN (1 << 7)
73 #define AXP_IRQEN1 0x40
74 #define AXP_IRQEN2 0x41
75 #define AXP_IRQEN3 0x42
76 #define AXP_IRQEN4 0x43
77 #define AXP_IRQEN5 0x44
78 #define AXP_IRQEN5_POKSIRQ (1 << 4)
79 #define AXP_IRQEN6 0x45
80 #define AXP_IRQSTAT5 0x4c
81 #define AXP_IRQSTAT5_POKSIRQ (1 << 4)
82 #define AXP_GPIO0_CTRL 0x90
83 #define AXP_GPIO1_CTRL 0x92
84 #define AXP_GPIO_FUNC (0x7 << 0)
85 #define AXP_GPIO_FUNC_SHIFT 0
86 #define AXP_GPIO_FUNC_DRVLO 0
87 #define AXP_GPIO_FUNC_DRVHI 1
88 #define AXP_GPIO_FUNC_INPUT 2
89 #define AXP_GPIO_SIGBIT 0x94
90 #define AXP_GPIO_PD 0x97
96 { "GPIO0", AXP_GPIO0_CTRL },
97 { "GPIO1", AXP_GPIO1_CTRL },
100 static struct ofw_compat_data compat_data[] = {
101 { "x-powers,axp813", 1 },
102 { "x-powers,axp818", 1 },
106 static struct resource_spec axp81x_spec[] = {
107 { SYS_RES_IRQ, 0, RF_ACTIVE },
111 struct axp81x_regdef {
131 static struct axp81x_regdef axp81x_regdefs[] = {
133 .id = AXP81X_REG_ID_DC1SW,
135 .enable_reg = AXP_POWERCTL2,
136 .enable_mask = AXP_POWERCTL2_DC1SW,
139 .id = AXP81X_REG_ID_DCDC2,
141 .enable_reg = AXP_POWERCTL1,
142 .enable_mask = AXP_POWERCTL1_DCDC2,
143 .voltage_reg = AXP_VOLTCTL_DCDC2,
147 .voltage_nstep1 = 70,
155 struct axp81x_reg_sc {
156 struct regnode *regnode;
158 struct axp81x_regdef *def;
160 struct regnode_std_param *param;
163 struct axp81x_softc {
164 struct resource *res;
172 struct axp81x_reg_sc **regs;
176 #define AXP_LOCK(sc) mtx_lock(&(sc)->mtx)
177 #define AXP_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
180 axp81x_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size)
182 struct axp81x_softc *sc;
183 struct iic_msg msg[2];
185 sc = device_get_softc(dev);
187 msg[0].slave = sc->addr;
188 msg[0].flags = IIC_M_WR;
192 msg[1].slave = sc->addr;
193 msg[1].flags = IIC_M_RD;
197 return (iicbus_transfer(dev, msg, 2));
201 axp81x_write(device_t dev, uint8_t reg, uint8_t val)
203 struct axp81x_softc *sc;
204 struct iic_msg msg[2];
206 sc = device_get_softc(dev);
208 msg[0].slave = sc->addr;
209 msg[0].flags = IIC_M_WR;
213 msg[1].slave = sc->addr;
214 msg[1].flags = IIC_M_WR;
218 return (iicbus_transfer(dev, msg, 2));
222 axp81x_regnode_init(struct regnode *regnode)
228 axp81x_regnode_enable(struct regnode *regnode, bool enable, int *udelay)
230 struct axp81x_reg_sc *sc;
233 sc = regnode_get_softc(regnode);
235 axp81x_read(sc->base_dev, sc->def->enable_reg, &val, 1);
237 val |= sc->def->enable_mask;
239 val &= ~sc->def->enable_mask;
240 axp81x_write(sc->base_dev, sc->def->enable_reg, val);
248 axp81x_regnode_reg_to_voltage(struct axp81x_reg_sc *sc, uint8_t val, int *uv)
250 if (val < sc->def->voltage_nstep1)
251 *uv = sc->def->voltage_min + val * sc->def->voltage_step1;
253 *uv = sc->def->voltage_min +
254 (sc->def->voltage_nstep1 * sc->def->voltage_step1) +
255 ((val - sc->def->voltage_nstep1) * sc->def->voltage_step2);
260 axp81x_regnode_voltage_to_reg(struct axp81x_reg_sc *sc, int min_uvolt,
261 int max_uvolt, uint8_t *val)
267 uvolt = sc->def->voltage_min * 1000;
269 for (nstep = 0; nstep < sc->def->voltage_nstep1 && uvolt < min_uvolt;
272 uvolt += (sc->def->voltage_step1 * 1000);
274 for (nstep = 0; nstep < sc->def->voltage_nstep2 && uvolt < min_uvolt;
277 uvolt += (sc->def->voltage_step2 * 1000);
279 if (uvolt > max_uvolt)
287 axp81x_regnode_set_voltage(struct regnode *regnode, int min_uvolt,
288 int max_uvolt, int *udelay)
290 struct axp81x_reg_sc *sc;
293 sc = regnode_get_softc(regnode);
295 if (!sc->def->voltage_step1 || !sc->def->voltage_step2)
298 if (axp81x_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0)
301 axp81x_write(sc->base_dev, sc->def->voltage_reg, val);
309 axp81x_regnode_get_voltage(struct regnode *regnode, int *uvolt)
311 struct axp81x_reg_sc *sc;
314 sc = regnode_get_softc(regnode);
316 if (!sc->def->voltage_step1 || !sc->def->voltage_step2)
319 axp81x_read(sc->base_dev, sc->def->voltage_reg, &val, 1);
320 axp81x_regnode_reg_to_voltage(sc, val & AXP_VOLTCTL_MASK, uvolt);
325 static regnode_method_t axp81x_regnode_methods[] = {
326 /* Regulator interface */
327 REGNODEMETHOD(regnode_init, axp81x_regnode_init),
328 REGNODEMETHOD(regnode_enable, axp81x_regnode_enable),
329 REGNODEMETHOD(regnode_set_voltage, axp81x_regnode_set_voltage),
330 REGNODEMETHOD(regnode_get_voltage, axp81x_regnode_get_voltage),
333 DEFINE_CLASS_1(axp81x_regnode, axp81x_regnode_class, axp81x_regnode_methods,
334 sizeof(struct axp81x_reg_sc), regnode_class);
337 axp81x_shutdown(void *devp, int howto)
341 if ((howto & RB_POWEROFF) == 0)
347 device_printf(dev, "Shutdown AXP81x\n");
349 axp81x_write(dev, AXP_POWERBAT, AXP_POWERBAT_SHUTDOWN);
353 axp81x_intr(void *arg)
355 struct axp81x_softc *sc;
361 sc = device_get_softc(dev);
363 error = axp81x_read(dev, AXP_IRQSTAT5, &val, 1);
368 if ((val & AXP_IRQSTAT5_POKSIRQ) != 0) {
370 device_printf(dev, "Power button pressed\n");
371 shutdown_nice(RB_POWEROFF);
374 axp81x_write(dev, AXP_IRQSTAT5, val);
379 axp81x_gpio_get_bus(device_t dev)
381 struct axp81x_softc *sc;
383 sc = device_get_softc(dev);
385 return (sc->gpiodev);
389 axp81x_gpio_pin_max(device_t dev, int *maxpin)
391 *maxpin = nitems(axp81x_pins) - 1;
397 axp81x_gpio_pin_getname(device_t dev, uint32_t pin, char *name)
399 if (pin >= nitems(axp81x_pins))
402 snprintf(name, GPIOMAXNAME, "%s", axp81x_pins[pin].name);
408 axp81x_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps)
410 if (pin >= nitems(axp81x_pins))
413 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT;
419 axp81x_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags)
421 struct axp81x_softc *sc;
425 if (pin >= nitems(axp81x_pins))
428 sc = device_get_softc(dev);
431 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
433 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
434 if (func == AXP_GPIO_FUNC_INPUT)
435 *flags = GPIO_PIN_INPUT;
436 else if (func == AXP_GPIO_FUNC_DRVLO ||
437 func == AXP_GPIO_FUNC_DRVHI)
438 *flags = GPIO_PIN_OUTPUT;
448 axp81x_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags)
450 struct axp81x_softc *sc;
454 if (pin >= nitems(axp81x_pins))
457 sc = device_get_softc(dev);
460 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
462 data &= ~AXP_GPIO_FUNC;
463 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) {
464 if ((flags & GPIO_PIN_OUTPUT) == 0)
465 data |= AXP_GPIO_FUNC_INPUT;
467 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
475 axp81x_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val)
477 struct axp81x_softc *sc;
481 if (pin >= nitems(axp81x_pins))
484 sc = device_get_softc(dev);
487 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
489 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
491 case AXP_GPIO_FUNC_DRVLO:
494 case AXP_GPIO_FUNC_DRVHI:
497 case AXP_GPIO_FUNC_INPUT:
498 error = axp81x_read(dev, AXP_GPIO_SIGBIT, &data, 1);
500 *val = (data & (1 << pin)) ? 1 : 0;
513 axp81x_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val)
515 struct axp81x_softc *sc;
519 if (pin >= nitems(axp81x_pins))
522 sc = device_get_softc(dev);
525 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
527 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
529 case AXP_GPIO_FUNC_DRVLO:
530 case AXP_GPIO_FUNC_DRVHI:
531 data &= ~AXP_GPIO_FUNC;
532 data |= (val << AXP_GPIO_FUNC_SHIFT);
540 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
548 axp81x_gpio_pin_toggle(device_t dev, uint32_t pin)
550 struct axp81x_softc *sc;
554 if (pin >= nitems(axp81x_pins))
557 sc = device_get_softc(dev);
560 error = axp81x_read(dev, axp81x_pins[pin].ctrl_reg, &data, 1);
562 func = (data & AXP_GPIO_FUNC) >> AXP_GPIO_FUNC_SHIFT;
564 case AXP_GPIO_FUNC_DRVLO:
565 data &= ~AXP_GPIO_FUNC;
566 data |= (AXP_GPIO_FUNC_DRVHI << AXP_GPIO_FUNC_SHIFT);
568 case AXP_GPIO_FUNC_DRVHI:
569 data &= ~AXP_GPIO_FUNC;
570 data |= (AXP_GPIO_FUNC_DRVLO << AXP_GPIO_FUNC_SHIFT);
578 error = axp81x_write(dev, axp81x_pins[pin].ctrl_reg, data);
585 axp81x_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent,
586 int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags)
588 if (gpios[0] >= nitems(axp81x_pins))
598 axp81x_get_node(device_t dev, device_t bus)
600 return (ofw_bus_get_node(dev));
603 static struct axp81x_reg_sc *
604 axp81x_reg_attach(device_t dev, phandle_t node,
605 struct axp81x_regdef *def)
607 struct axp81x_reg_sc *reg_sc;
608 struct regnode_init_def initdef;
609 struct regnode *regnode;
611 memset(&initdef, 0, sizeof(initdef));
612 regulator_parse_ofw_stdparam(dev, node, &initdef);
613 if (initdef.std_param.min_uvolt == 0)
614 initdef.std_param.min_uvolt = def->voltage_min * 1000;
615 if (initdef.std_param.max_uvolt == 0)
616 initdef.std_param.max_uvolt = def->voltage_max * 1000;
617 initdef.id = def->id;
618 initdef.ofw_node = node;
619 regnode = regnode_create(dev, &axp81x_regnode_class, &initdef);
620 if (regnode == NULL) {
621 device_printf(dev, "cannot create regulator\n");
625 reg_sc = regnode_get_softc(regnode);
626 reg_sc->regnode = regnode;
627 reg_sc->base_dev = dev;
629 reg_sc->xref = OF_xref_from_node(node);
630 reg_sc->param = regnode_get_stdparam(regnode);
632 regnode_register(regnode);
638 axp81x_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells,
641 struct axp81x_softc *sc;
644 sc = device_get_softc(dev);
645 for (i = 0; i < sc->nregs; i++) {
646 if (sc->regs[i] == NULL)
648 if (sc->regs[i]->xref == xref) {
649 *num = sc->regs[i]->def->id;
658 axp81x_probe(device_t dev)
660 if (!ofw_bus_status_okay(dev))
663 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
666 device_set_desc(dev, "X-Powers AXP81x Power Management Unit");
668 return (BUS_PROBE_DEFAULT);
672 axp81x_attach(device_t dev)
674 struct axp81x_softc *sc;
675 struct axp81x_reg_sc *reg;
677 phandle_t rnode, child;
680 sc = device_get_softc(dev);
682 sc->addr = iicbus_get_addr(dev);
683 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
685 error = bus_alloc_resources(dev, axp81x_spec, &sc->res);
687 device_printf(dev, "cannot allocate resources for device\n");
692 axp81x_read(dev, AXP_ICTYPE, &chip_id, 1);
693 device_printf(dev, "chip ID 0x%02x\n", chip_id);
696 sc->nregs = nitems(axp81x_regdefs);
697 sc->regs = malloc(sizeof(struct axp81x_reg_sc *) * sc->nregs,
698 M_AXP81X_REG, M_WAITOK | M_ZERO);
700 /* Attach known regulators that exist in the DT */
701 rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators");
703 for (i = 0; i < sc->nregs; i++) {
704 child = ofw_bus_find_child(rnode,
705 axp81x_regdefs[i].name);
708 reg = axp81x_reg_attach(dev, child, &axp81x_regdefs[i]);
711 "cannot attach regulator %s\n",
712 axp81x_regdefs[i].name);
719 /* Enable IRQ on short power key press */
720 axp81x_write(dev, AXP_IRQEN1, 0);
721 axp81x_write(dev, AXP_IRQEN2, 0);
722 axp81x_write(dev, AXP_IRQEN3, 0);
723 axp81x_write(dev, AXP_IRQEN4, 0);
724 axp81x_write(dev, AXP_IRQEN5, AXP_IRQEN5_POKSIRQ);
725 axp81x_write(dev, AXP_IRQEN6, 0);
727 /* Install interrupt handler */
728 error = bus_setup_intr(dev, sc->res, INTR_TYPE_MISC | INTR_MPSAFE,
729 NULL, axp81x_intr, dev, &sc->ih);
731 device_printf(dev, "cannot setup interrupt handler\n");
735 EVENTHANDLER_REGISTER(shutdown_final, axp81x_shutdown, dev,
738 sc->gpiodev = gpiobus_attach_bus(dev);
743 static device_method_t axp81x_methods[] = {
744 /* Device interface */
745 DEVMETHOD(device_probe, axp81x_probe),
746 DEVMETHOD(device_attach, axp81x_attach),
749 DEVMETHOD(gpio_get_bus, axp81x_gpio_get_bus),
750 DEVMETHOD(gpio_pin_max, axp81x_gpio_pin_max),
751 DEVMETHOD(gpio_pin_getname, axp81x_gpio_pin_getname),
752 DEVMETHOD(gpio_pin_getcaps, axp81x_gpio_pin_getcaps),
753 DEVMETHOD(gpio_pin_getflags, axp81x_gpio_pin_getflags),
754 DEVMETHOD(gpio_pin_setflags, axp81x_gpio_pin_setflags),
755 DEVMETHOD(gpio_pin_get, axp81x_gpio_pin_get),
756 DEVMETHOD(gpio_pin_set, axp81x_gpio_pin_set),
757 DEVMETHOD(gpio_pin_toggle, axp81x_gpio_pin_toggle),
758 DEVMETHOD(gpio_map_gpios, axp81x_gpio_map_gpios),
760 /* Regdev interface */
761 DEVMETHOD(regdev_map, axp81x_regdev_map),
763 /* OFW bus interface */
764 DEVMETHOD(ofw_bus_get_node, axp81x_get_node),
769 static driver_t axp81x_driver = {
772 sizeof(struct axp81x_softc),
775 static devclass_t axp81x_devclass;
776 extern devclass_t ofwgpiobus_devclass, gpioc_devclass;
777 extern driver_t ofw_gpiobus_driver, gpioc_driver;
779 EARLY_DRIVER_MODULE(axp81x, iicbus, axp81x_driver, axp81x_devclass, 0, 0,
780 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
781 EARLY_DRIVER_MODULE(ofw_gpiobus, axp81x_pmu, ofw_gpiobus_driver,
782 ofwgpiobus_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
783 DRIVER_MODULE(gpioc, axp81x_pmu, gpioc_driver, gpioc_devclass, 0, 0);
784 MODULE_VERSION(axp81x, 1);
785 MODULE_DEPEND(axp81x, iicbus, 1, 1, 1);