1 /*- 2 * Copyright (c) 2015-2016 Emmanuel Vadot <manu@freebsd.org> 3 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 /* 31 * X-Power AXP209 PMU for Allwinner SoCs 32 */ 33 #include <sys/param.h> 34 #include <sys/systm.h> 35 #include <sys/eventhandler.h> 36 #include <sys/kernel.h> 37 #include <sys/module.h> 38 #include <sys/clock.h> 39 #include <sys/time.h> 40 #include <sys/bus.h> 41 #include <sys/proc.h> 42 #include <sys/gpio.h> 43 #include <sys/reboot.h> 44 #include <sys/resource.h> 45 #include <sys/rman.h> 46 #include <sys/sysctl.h> 47 48 #include <dev/iicbus/iicbus.h> 49 #include <dev/iicbus/iiconf.h> 50 51 #include <dev/gpio/gpiobusvar.h> 52 53 #include <dev/ofw/ofw_bus.h> 54 #include <dev/ofw/ofw_bus_subr.h> 55 56 #include <dev/extres/regulator/regulator.h> 57 58 #include <arm/allwinner/axp209reg.h> 59 60 #include "iicbus_if.h" 61 #include "gpio_if.h" 62 #include "regdev_if.h" 63 64 MALLOC_DEFINE(M_AXP209_REG, "Axp209 regulator", "Axp209 power regulator"); 65 66 struct axp209_regdef { 67 intptr_t id; 68 char *name; 69 uint8_t enable_reg; 70 uint8_t enable_mask; 71 uint8_t voltage_reg; 72 uint8_t voltage_mask; 73 uint8_t voltage_shift; 74 int voltage_min; 75 int voltage_max; 76 int voltage_step; 77 int voltage_nstep; 78 }; 79 80 static struct axp209_regdef axp209_regdefs[] = { 81 { 82 .id = AXP209_REG_ID_DCDC2, 83 .name = "dcdc2", 84 .enable_reg = AXP209_POWERCTL, 85 .enable_mask = AXP209_POWERCTL_DCDC2, 86 .voltage_reg = AXP209_REG_DCDC2_VOLTAGE, 87 .voltage_mask = 0x3f, 88 .voltage_min = 700, 89 .voltage_max = 2275, 90 .voltage_step = 25, 91 .voltage_nstep = 64, 92 }, 93 { 94 .id = AXP209_REG_ID_DCDC3, 95 .name = "dcdc3", 96 .enable_reg = AXP209_POWERCTL, 97 .enable_mask = AXP209_POWERCTL_DCDC3, 98 .voltage_reg = AXP209_REG_DCDC3_VOLTAGE, 99 .voltage_mask = 0x7f, 100 .voltage_min = 700, 101 .voltage_max = 3500, 102 .voltage_step = 25, 103 .voltage_nstep = 128, 104 }, 105 { 106 .id = AXP209_REG_ID_LDO2, 107 .name = "ldo2", 108 .enable_reg = AXP209_POWERCTL, 109 .enable_mask = AXP209_POWERCTL_LDO2, 110 .voltage_reg = AXP209_REG_LDO24_VOLTAGE, 111 .voltage_mask = 0xf0, 112 .voltage_shift = 4, 113 .voltage_min = 1800, 114 .voltage_max = 3300, 115 .voltage_step = 100, 116 .voltage_nstep = 16, 117 }, 118 { 119 .id = AXP209_REG_ID_LDO3, 120 .name = "ldo3", 121 .enable_reg = AXP209_POWERCTL, 122 .enable_mask = AXP209_POWERCTL_LDO3, 123 .voltage_reg = AXP209_REG_LDO3_VOLTAGE, 124 .voltage_mask = 0x7f, 125 .voltage_min = 700, 126 .voltage_max = 2275, 127 .voltage_step = 25, 128 .voltage_nstep = 128, 129 }, 130 }; 131 132 struct axp209_reg_sc { 133 struct regnode *regnode; 134 device_t base_dev; 135 struct axp209_regdef *def; 136 phandle_t xref; 137 struct regnode_std_param *param; 138 }; 139 140 struct axp209_softc { 141 device_t dev; 142 uint32_t addr; 143 struct resource * res[1]; 144 void * intrcookie; 145 struct intr_config_hook intr_hook; 146 device_t gpiodev; 147 struct mtx mtx; 148 149 /* Regulators */ 150 struct axp209_reg_sc **regs; 151 int nregs; 152 }; 153 154 /* GPIO3 is different, don't expose it for now */ 155 static const struct { 156 const char *name; 157 uint8_t ctrl_reg; 158 } axp209_pins[] = { 159 { "GPIO0", AXP209_GPIO0_CTRL }, 160 { "GPIO1", AXP209_GPIO1_CTRL }, 161 { "GPIO2", AXP209_GPIO2_CTRL }, 162 }; 163 164 static struct resource_spec axp_res_spec[] = { 165 { SYS_RES_IRQ, 0, RF_ACTIVE }, 166 { -1, 0, 0 } 167 }; 168 169 #define AXP_LOCK(sc) mtx_lock(&(sc)->mtx) 170 #define AXP_UNLOCK(sc) mtx_unlock(&(sc)->mtx) 171 172 static int 173 axp209_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 174 { 175 struct axp209_softc *sc = device_get_softc(dev); 176 struct iic_msg msg[2]; 177 178 msg[0].slave = sc->addr; 179 msg[0].flags = IIC_M_WR; 180 msg[0].len = 1; 181 msg[0].buf = ® 182 183 msg[1].slave = sc->addr; 184 msg[1].flags = IIC_M_RD; 185 msg[1].len = size; 186 msg[1].buf = data; 187 188 return (iicbus_transfer(dev, msg, 2)); 189 } 190 191 static int 192 axp209_write(device_t dev, uint8_t reg, uint8_t data) 193 { 194 uint8_t buffer[2]; 195 struct axp209_softc *sc = device_get_softc(dev); 196 struct iic_msg msg; 197 198 buffer[0] = reg; 199 buffer[1] = data; 200 201 msg.slave = sc->addr; 202 msg.flags = IIC_M_WR; 203 msg.len = 2; 204 msg.buf = buffer; 205 206 return (iicbus_transfer(dev, &msg, 1)); 207 } 208 209 static int 210 axp209_regnode_init(struct regnode *regnode) 211 { 212 return (0); 213 } 214 215 static int 216 axp209_regnode_enable(struct regnode *regnode, bool enable, int *udelay) 217 { 218 struct axp209_reg_sc *sc; 219 uint8_t val; 220 221 sc = regnode_get_softc(regnode); 222 223 axp209_read(sc->base_dev, sc->def->enable_reg, &val, 1); 224 if (enable) 225 val |= sc->def->enable_mask; 226 else 227 val &= ~sc->def->enable_mask; 228 axp209_write(sc->base_dev, sc->def->enable_reg, val); 229 230 *udelay = 0; 231 232 return (0); 233 } 234 235 static void 236 axp209_regnode_reg_to_voltage(struct axp209_reg_sc *sc, uint8_t val, int *uv) 237 { 238 if (val < sc->def->voltage_nstep) 239 *uv = sc->def->voltage_min + val * sc->def->voltage_step; 240 else 241 *uv = sc->def->voltage_min + 242 (sc->def->voltage_nstep * sc->def->voltage_step); 243 *uv *= 1000; 244 } 245 246 static int 247 axp209_regnode_voltage_to_reg(struct axp209_reg_sc *sc, int min_uvolt, 248 int max_uvolt, uint8_t *val) 249 { 250 uint8_t nval; 251 int nstep, uvolt; 252 253 nval = 0; 254 uvolt = sc->def->voltage_min * 1000; 255 256 for (nstep = 0; nstep < sc->def->voltage_nstep && uvolt < min_uvolt; 257 nstep++) { 258 ++nval; 259 uvolt += (sc->def->voltage_step * 1000); 260 } 261 if (uvolt > max_uvolt) 262 return (EINVAL); 263 264 *val = nval; 265 return (0); 266 } 267 268 static int 269 axp209_regnode_set_voltage(struct regnode *regnode, int min_uvolt, 270 int max_uvolt, int *udelay) 271 { 272 struct axp209_reg_sc *sc; 273 uint8_t val; 274 275 sc = regnode_get_softc(regnode); 276 277 if (!sc->def->voltage_step) 278 return (ENXIO); 279 280 if (axp209_regnode_voltage_to_reg(sc, min_uvolt, max_uvolt, &val) != 0) 281 return (ERANGE); 282 283 axp209_write(sc->base_dev, sc->def->voltage_reg, val); 284 285 *udelay = 0; 286 287 return (0); 288 } 289 290 static int 291 axp209_regnode_get_voltage(struct regnode *regnode, int *uvolt) 292 { 293 struct axp209_reg_sc *sc; 294 uint8_t val; 295 296 sc = regnode_get_softc(regnode); 297 298 if (!sc->def->voltage_step) 299 return (ENXIO); 300 301 axp209_read(sc->base_dev, sc->def->voltage_reg, &val, 1); 302 axp209_regnode_reg_to_voltage(sc, val & sc->def->voltage_mask, uvolt); 303 304 return (0); 305 } 306 307 static regnode_method_t axp209_regnode_methods[] = { 308 /* Regulator interface */ 309 REGNODEMETHOD(regnode_init, axp209_regnode_init), 310 REGNODEMETHOD(regnode_enable, axp209_regnode_enable), 311 REGNODEMETHOD(regnode_set_voltage, axp209_regnode_set_voltage), 312 REGNODEMETHOD(regnode_get_voltage, axp209_regnode_get_voltage), 313 REGNODEMETHOD_END 314 }; 315 DEFINE_CLASS_1(axp209_regnode, axp209_regnode_class, axp209_regnode_methods, 316 sizeof(struct axp209_reg_sc), regnode_class); 317 318 static int 319 axp209_sysctl(SYSCTL_HANDLER_ARGS) 320 { 321 device_t dev = arg1; 322 enum axp209_sensor sensor = arg2; 323 uint8_t data[2]; 324 int val, error; 325 326 switch (sensor) { 327 case AXP209_TEMP: 328 error = axp209_read(dev, AXP209_TEMPMON, data, 2); 329 if (error != 0) 330 return (error); 331 332 /* Temperature is between -144.7C and 264.8C, step +0.1C */ 333 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) - 334 AXP209_TEMPMON_MIN + AXP209_0C_TO_K; 335 break; 336 case AXP209_ACVOLT: 337 error = axp209_read(dev, AXP209_ACIN_VOLTAGE, data, 2); 338 if (error != 0) 339 return (error); 340 341 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 342 AXP209_VOLT_STEP; 343 break; 344 case AXP209_ACCURRENT: 345 error = axp209_read(dev, AXP209_ACIN_CURRENT, data, 2); 346 if (error != 0) 347 return (error); 348 349 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 350 AXP209_ACCURRENT_STEP; 351 break; 352 case AXP209_VBUSVOLT: 353 error = axp209_read(dev, AXP209_VBUS_VOLTAGE, data, 2); 354 if (error != 0) 355 return (error); 356 357 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 358 AXP209_VOLT_STEP; 359 break; 360 case AXP209_VBUSCURRENT: 361 error = axp209_read(dev, AXP209_VBUS_CURRENT, data, 2); 362 if (error != 0) 363 return (error); 364 365 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 366 AXP209_VBUSCURRENT_STEP; 367 break; 368 case AXP209_BATVOLT: 369 error = axp209_read(dev, AXP209_BAT_VOLTAGE, data, 2); 370 if (error != 0) 371 return (error); 372 373 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 374 AXP209_BATVOLT_STEP; 375 break; 376 case AXP209_BATCHARGECURRENT: 377 error = axp209_read(dev, AXP209_BAT_CHARGE_CURRENT, data, 2); 378 if (error != 0) 379 return (error); 380 381 val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * 382 AXP209_BATCURRENT_STEP; 383 break; 384 case AXP209_BATDISCHARGECURRENT: 385 error = axp209_read(dev, AXP209_BAT_DISCHARGE_CURRENT, data, 2); 386 if (error != 0) 387 return (error); 388 389 val = (AXP209_SENSOR_BAT_H(data[0]) | 390 AXP209_SENSOR_BAT_L(data[1])) * AXP209_BATCURRENT_STEP; 391 break; 392 default: 393 return (ENOENT); 394 } 395 396 return sysctl_handle_opaque(oidp, &val, sizeof(val), req); 397 } 398 399 static void 400 axp209_shutdown(void *devp, int howto) 401 { 402 device_t dev; 403 404 if (!(howto & RB_POWEROFF)) 405 return; 406 dev = (device_t)devp; 407 408 if (bootverbose) 409 device_printf(dev, "Shutdown AXP209\n"); 410 411 axp209_write(dev, AXP209_SHUTBAT, AXP209_SHUTBAT_SHUTDOWN); 412 } 413 414 static void 415 axp_intr(void *arg) 416 { 417 struct axp209_softc *sc; 418 uint8_t reg; 419 420 sc = arg; 421 422 axp209_read(sc->dev, AXP209_IRQ1_STATUS, ®, 1); 423 if (reg) { 424 if (reg & AXP209_IRQ1_AC_OVERVOLT) 425 devctl_notify("PMU", "AC", "overvoltage", NULL); 426 if (reg & AXP209_IRQ1_VBUS_OVERVOLT) 427 devctl_notify("PMU", "USB", "overvoltage", NULL); 428 if (reg & AXP209_IRQ1_VBUS_LOW) 429 devctl_notify("PMU", "USB", "undervoltage", NULL); 430 if (reg & AXP209_IRQ1_AC_CONN) 431 devctl_notify("PMU", "AC", "plugged", NULL); 432 if (reg & AXP209_IRQ1_AC_DISCONN) 433 devctl_notify("PMU", "AC", "unplugged", NULL); 434 if (reg & AXP209_IRQ1_VBUS_CONN) 435 devctl_notify("PMU", "USB", "plugged", NULL); 436 if (reg & AXP209_IRQ1_VBUS_DISCONN) 437 devctl_notify("PMU", "USB", "unplugged", NULL); 438 axp209_write(sc->dev, AXP209_IRQ1_STATUS, AXP209_IRQ_ACK); 439 } 440 441 axp209_read(sc->dev, AXP209_IRQ2_STATUS, ®, 1); 442 if (reg) { 443 if (reg & AXP209_IRQ2_BATT_CHARGED) 444 devctl_notify("PMU", "Battery", "charged", NULL); 445 if (reg & AXP209_IRQ2_BATT_CHARGING) 446 devctl_notify("PMU", "Battery", "charging", NULL); 447 if (reg & AXP209_IRQ2_BATT_CONN) 448 devctl_notify("PMU", "Battery", "connected", NULL); 449 if (reg & AXP209_IRQ2_BATT_DISCONN) 450 devctl_notify("PMU", "Battery", "disconnected", NULL); 451 if (reg & AXP209_IRQ2_BATT_TEMP_LOW) 452 devctl_notify("PMU", "Battery", "low temp", NULL); 453 if (reg & AXP209_IRQ2_BATT_TEMP_OVER) 454 devctl_notify("PMU", "Battery", "high temp", NULL); 455 axp209_write(sc->dev, AXP209_IRQ2_STATUS, AXP209_IRQ_ACK); 456 } 457 458 axp209_read(sc->dev, AXP209_IRQ3_STATUS, ®, 1); 459 if (reg) { 460 if (reg & AXP209_IRQ3_PEK_SHORT) 461 shutdown_nice(RB_POWEROFF); 462 axp209_write(sc->dev, AXP209_IRQ3_STATUS, AXP209_IRQ_ACK); 463 } 464 465 axp209_read(sc->dev, AXP209_IRQ4_STATUS, ®, 1); 466 if (reg) { 467 axp209_write(sc->dev, AXP209_IRQ4_STATUS, AXP209_IRQ_ACK); 468 } 469 470 axp209_read(sc->dev, AXP209_IRQ5_STATUS, ®, 1); 471 if (reg) { 472 axp209_write(sc->dev, AXP209_IRQ5_STATUS, AXP209_IRQ_ACK); 473 } 474 } 475 476 static device_t 477 axp209_gpio_get_bus(device_t dev) 478 { 479 struct axp209_softc *sc; 480 481 sc = device_get_softc(dev); 482 483 return (sc->gpiodev); 484 } 485 486 static int 487 axp209_gpio_pin_max(device_t dev, int *maxpin) 488 { 489 *maxpin = nitems(axp209_pins) - 1; 490 491 return (0); 492 } 493 494 static int 495 axp209_gpio_pin_getname(device_t dev, uint32_t pin, char *name) 496 { 497 if (pin >= nitems(axp209_pins)) 498 return (EINVAL); 499 500 snprintf(name, GPIOMAXNAME, "%s", axp209_pins[pin].name); 501 502 return (0); 503 } 504 505 static int 506 axp209_gpio_pin_getcaps(device_t dev, uint32_t pin, uint32_t *caps) 507 { 508 if (pin >= nitems(axp209_pins)) 509 return (EINVAL); 510 511 *caps = GPIO_PIN_INPUT | GPIO_PIN_OUTPUT; 512 513 return (0); 514 } 515 516 static int 517 axp209_gpio_pin_getflags(device_t dev, uint32_t pin, uint32_t *flags) 518 { 519 struct axp209_softc *sc; 520 uint8_t data, func; 521 int error; 522 523 if (pin >= nitems(axp209_pins)) 524 return (EINVAL); 525 526 sc = device_get_softc(dev); 527 528 AXP_LOCK(sc); 529 error = axp209_read(dev, axp209_pins[pin].ctrl_reg, &data, 1); 530 if (error == 0) { 531 func = data & AXP209_GPIO_FUNC_MASK; 532 if (func == AXP209_GPIO_FUNC_INPUT) 533 *flags = GPIO_PIN_INPUT; 534 else if (func == AXP209_GPIO_FUNC_DRVLO || 535 func == AXP209_GPIO_FUNC_DRVHI) 536 *flags = GPIO_PIN_OUTPUT; 537 else 538 *flags = 0; 539 } 540 AXP_UNLOCK(sc); 541 542 return (error); 543 } 544 545 static int 546 axp209_gpio_pin_setflags(device_t dev, uint32_t pin, uint32_t flags) 547 { 548 struct axp209_softc *sc; 549 uint8_t data; 550 int error; 551 552 if (pin >= nitems(axp209_pins)) 553 return (EINVAL); 554 555 sc = device_get_softc(dev); 556 557 AXP_LOCK(sc); 558 error = axp209_read(dev, axp209_pins[pin].ctrl_reg, &data, 1); 559 if (error == 0) { 560 data &= ~AXP209_GPIO_FUNC_MASK; 561 if ((flags & (GPIO_PIN_INPUT|GPIO_PIN_OUTPUT)) != 0) { 562 if ((flags & GPIO_PIN_OUTPUT) == 0) 563 data |= AXP209_GPIO_FUNC_INPUT; 564 } 565 error = axp209_write(dev, axp209_pins[pin].ctrl_reg, data); 566 } 567 AXP_UNLOCK(sc); 568 569 return (error); 570 } 571 572 static int 573 axp209_gpio_pin_get(device_t dev, uint32_t pin, unsigned int *val) 574 { 575 struct axp209_softc *sc; 576 uint8_t data, func; 577 int error; 578 579 if (pin >= nitems(axp209_pins)) 580 return (EINVAL); 581 582 sc = device_get_softc(dev); 583 584 AXP_LOCK(sc); 585 error = axp209_read(dev, axp209_pins[pin].ctrl_reg, &data, 1); 586 if (error == 0) { 587 func = data & AXP209_GPIO_FUNC_MASK; 588 switch (func) { 589 case AXP209_GPIO_FUNC_DRVLO: 590 *val = 0; 591 break; 592 case AXP209_GPIO_FUNC_DRVHI: 593 *val = 1; 594 break; 595 case AXP209_GPIO_FUNC_INPUT: 596 error = axp209_read(dev, AXP209_GPIO_STATUS, &data, 1); 597 if (error == 0) 598 *val = (data & AXP209_GPIO_DATA(pin)) ? 1 : 0; 599 break; 600 default: 601 error = EIO; 602 break; 603 } 604 } 605 AXP_UNLOCK(sc); 606 607 return (error); 608 } 609 610 static int 611 axp209_gpio_pin_set(device_t dev, uint32_t pin, unsigned int val) 612 { 613 struct axp209_softc *sc; 614 uint8_t data, func; 615 int error; 616 617 if (pin >= nitems(axp209_pins)) 618 return (EINVAL); 619 620 sc = device_get_softc(dev); 621 622 AXP_LOCK(sc); 623 error = axp209_read(dev, axp209_pins[pin].ctrl_reg, &data, 1); 624 if (error == 0) { 625 func = data & AXP209_GPIO_FUNC_MASK; 626 switch (func) { 627 case AXP209_GPIO_FUNC_DRVLO: 628 case AXP209_GPIO_FUNC_DRVHI: 629 /* GPIO2 can't be set to 1 */ 630 if (pin == 2 && val == 1) { 631 error = EINVAL; 632 break; 633 } 634 data &= ~AXP209_GPIO_FUNC_MASK; 635 data |= val; 636 break; 637 default: 638 error = EIO; 639 break; 640 } 641 } 642 if (error == 0) 643 error = axp209_write(dev, axp209_pins[pin].ctrl_reg, data); 644 AXP_UNLOCK(sc); 645 646 return (error); 647 } 648 649 650 static int 651 axp209_gpio_pin_toggle(device_t dev, uint32_t pin) 652 { 653 struct axp209_softc *sc; 654 uint8_t data, func; 655 int error; 656 657 if (pin >= nitems(axp209_pins)) 658 return (EINVAL); 659 660 sc = device_get_softc(dev); 661 662 AXP_LOCK(sc); 663 error = axp209_read(dev, axp209_pins[pin].ctrl_reg, &data, 1); 664 if (error == 0) { 665 func = data & AXP209_GPIO_FUNC_MASK; 666 switch (func) { 667 case AXP209_GPIO_FUNC_DRVLO: 668 /* Pin 2 can't be set to 1*/ 669 if (pin == 2) { 670 error = EINVAL; 671 break; 672 } 673 data &= ~AXP209_GPIO_FUNC_MASK; 674 data |= AXP209_GPIO_FUNC_DRVHI; 675 break; 676 case AXP209_GPIO_FUNC_DRVHI: 677 data &= ~AXP209_GPIO_FUNC_MASK; 678 data |= AXP209_GPIO_FUNC_DRVLO; 679 break; 680 default: 681 error = EIO; 682 break; 683 } 684 } 685 if (error == 0) 686 error = axp209_write(dev, axp209_pins[pin].ctrl_reg, data); 687 AXP_UNLOCK(sc); 688 689 return (error); 690 } 691 692 static int 693 axp209_gpio_map_gpios(device_t bus, phandle_t dev, phandle_t gparent, 694 int gcells, pcell_t *gpios, uint32_t *pin, uint32_t *flags) 695 { 696 if (gpios[0] >= nitems(axp209_pins)) 697 return (EINVAL); 698 699 *pin = gpios[0]; 700 *flags = gpios[1]; 701 702 return (0); 703 } 704 705 static phandle_t 706 axp209_get_node(device_t dev, device_t bus) 707 { 708 return (ofw_bus_get_node(dev)); 709 } 710 711 static struct axp209_reg_sc * 712 axp209_reg_attach(device_t dev, phandle_t node, 713 struct axp209_regdef *def) 714 { 715 struct axp209_reg_sc *reg_sc; 716 struct regnode_init_def initdef; 717 struct regnode *regnode; 718 719 memset(&initdef, 0, sizeof(initdef)); 720 if (regulator_parse_ofw_stdparam(dev, node, &initdef) != 0) { 721 device_printf(dev, "cannot create regulator\n"); 722 return (NULL); 723 } 724 if (initdef.std_param.min_uvolt == 0) 725 initdef.std_param.min_uvolt = def->voltage_min * 1000; 726 if (initdef.std_param.max_uvolt == 0) 727 initdef.std_param.max_uvolt = def->voltage_max * 1000; 728 initdef.id = def->id; 729 initdef.ofw_node = node; 730 regnode = regnode_create(dev, &axp209_regnode_class, &initdef); 731 if (regnode == NULL) { 732 device_printf(dev, "cannot create regulator\n"); 733 return (NULL); 734 } 735 736 reg_sc = regnode_get_softc(regnode); 737 reg_sc->regnode = regnode; 738 reg_sc->base_dev = dev; 739 reg_sc->def = def; 740 reg_sc->xref = OF_xref_from_node(node); 741 reg_sc->param = regnode_get_stdparam(regnode); 742 743 regnode_register(regnode); 744 745 return (reg_sc); 746 } 747 748 static int 749 axp209_regdev_map(device_t dev, phandle_t xref, int ncells, pcell_t *cells, 750 intptr_t *num) 751 { 752 struct axp209_softc *sc; 753 int i; 754 755 sc = device_get_softc(dev); 756 for (i = 0; i < sc->nregs; i++) { 757 if (sc->regs[i] == NULL) 758 continue; 759 if (sc->regs[i]->xref == xref) { 760 *num = sc->regs[i]->def->id; 761 return (0); 762 } 763 } 764 765 return (ENXIO); 766 } 767 768 static void 769 axp209_start(void *pdev) 770 { 771 device_t dev; 772 struct axp209_softc *sc; 773 const char *pwr_name[] = {"Battery", "AC", "USB", "AC and USB"}; 774 uint8_t data; 775 uint8_t pwr_src; 776 777 dev = pdev; 778 779 sc = device_get_softc(dev); 780 sc->addr = iicbus_get_addr(dev); 781 sc->dev = dev; 782 783 if (bootverbose) { 784 /* 785 * Read the Power State register. 786 * Shift the AC presence into bit 0. 787 * Shift the Battery presence into bit 1. 788 */ 789 axp209_read(dev, AXP209_PSR, &data, 1); 790 pwr_src = ((data & AXP209_PSR_ACIN) >> AXP209_PSR_ACIN_SHIFT) | 791 ((data & AXP209_PSR_VBUS) >> (AXP209_PSR_VBUS_SHIFT - 1)); 792 793 device_printf(dev, "AXP209 Powered by %s\n", 794 pwr_name[pwr_src]); 795 } 796 797 /* Only enable interrupts that we are interested in */ 798 axp209_write(dev, AXP209_IRQ1_ENABLE, 799 AXP209_IRQ1_AC_OVERVOLT | 800 AXP209_IRQ1_AC_DISCONN | 801 AXP209_IRQ1_AC_CONN | 802 AXP209_IRQ1_VBUS_OVERVOLT | 803 AXP209_IRQ1_VBUS_DISCONN | 804 AXP209_IRQ1_VBUS_CONN); 805 axp209_write(dev, AXP209_IRQ2_ENABLE, 806 AXP209_IRQ2_BATT_CONN | 807 AXP209_IRQ2_BATT_DISCONN | 808 AXP209_IRQ2_BATT_CHARGE_ACCT_ON | 809 AXP209_IRQ2_BATT_CHARGE_ACCT_OFF | 810 AXP209_IRQ2_BATT_CHARGING | 811 AXP209_IRQ2_BATT_CHARGED | 812 AXP209_IRQ2_BATT_TEMP_OVER | 813 AXP209_IRQ2_BATT_TEMP_LOW); 814 axp209_write(dev, AXP209_IRQ3_ENABLE, 815 AXP209_IRQ3_PEK_SHORT | AXP209_IRQ3_PEK_LONG); 816 axp209_write(dev, AXP209_IRQ4_ENABLE, AXP209_IRQ4_APS_LOW_2); 817 axp209_write(dev, AXP209_IRQ5_ENABLE, 0x0); 818 819 EVENTHANDLER_REGISTER(shutdown_final, axp209_shutdown, dev, 820 SHUTDOWN_PRI_LAST); 821 822 /* Enable ADC sensors */ 823 if (axp209_write(dev, AXP209_ADC_ENABLE1, 824 AXP209_ADC1_BATVOLT | AXP209_ADC1_BATCURRENT | 825 AXP209_ADC1_ACVOLT | AXP209_ADC1_ACCURRENT | 826 AXP209_ADC1_VBUSVOLT | AXP209_ADC1_VBUSCURRENT) != -1) { 827 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 828 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 829 OID_AUTO, "acvolt", 830 CTLTYPE_INT | CTLFLAG_RD, 831 dev, AXP209_ACVOLT, axp209_sysctl, "I", 832 "AC Voltage (microVolt)"); 833 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 834 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 835 OID_AUTO, "accurrent", 836 CTLTYPE_INT | CTLFLAG_RD, 837 dev, AXP209_ACCURRENT, axp209_sysctl, "I", 838 "AC Current (microAmpere)"); 839 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 840 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 841 OID_AUTO, "vbusvolt", 842 CTLTYPE_INT | CTLFLAG_RD, 843 dev, AXP209_VBUSVOLT, axp209_sysctl, "I", 844 "VBUS Voltage (microVolt)"); 845 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 846 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 847 OID_AUTO, "vbuscurrent", 848 CTLTYPE_INT | CTLFLAG_RD, 849 dev, AXP209_VBUSCURRENT, axp209_sysctl, "I", 850 "VBUS Current (microAmpere)"); 851 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 852 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 853 OID_AUTO, "batvolt", 854 CTLTYPE_INT | CTLFLAG_RD, 855 dev, AXP209_BATVOLT, axp209_sysctl, "I", 856 "Battery Voltage (microVolt)"); 857 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 858 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 859 OID_AUTO, "batchargecurrent", 860 CTLTYPE_INT | CTLFLAG_RD, 861 dev, AXP209_BATCHARGECURRENT, axp209_sysctl, "I", 862 "Battery Charging Current (microAmpere)"); 863 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 864 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 865 OID_AUTO, "batdischargecurrent", 866 CTLTYPE_INT | CTLFLAG_RD, 867 dev, AXP209_BATDISCHARGECURRENT, axp209_sysctl, "I", 868 "Battery Discharging Current (microAmpere)"); 869 } else { 870 device_printf(dev, "Couldn't enable ADC sensors\n"); 871 } 872 873 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 874 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 875 OID_AUTO, "temp", 876 CTLTYPE_INT | CTLFLAG_RD, 877 dev, AXP209_TEMP, axp209_sysctl, "IK", "Internal temperature"); 878 879 if ((bus_setup_intr(dev, sc->res[0], INTR_TYPE_MISC | INTR_MPSAFE, 880 NULL, axp_intr, sc, &sc->intrcookie))) 881 device_printf(dev, "unable to register interrupt handler\n"); 882 883 config_intrhook_disestablish(&sc->intr_hook); 884 } 885 886 static int 887 axp209_probe(device_t dev) 888 { 889 890 if (!ofw_bus_status_okay(dev)) 891 return (ENXIO); 892 893 if (!ofw_bus_is_compatible(dev, "x-powers,axp209")) 894 return (ENXIO); 895 896 device_set_desc(dev, "X-Powers AXP209 Power Management Unit"); 897 898 return (BUS_PROBE_DEFAULT); 899 } 900 901 static int 902 axp209_attach(device_t dev) 903 { 904 struct axp209_softc *sc; 905 struct axp209_reg_sc *reg; 906 phandle_t rnode, child; 907 int i; 908 909 sc = device_get_softc(dev); 910 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 911 912 if (bus_alloc_resources(dev, axp_res_spec, sc->res) != 0) { 913 device_printf(dev, "can't allocate device resources\n"); 914 return (ENXIO); 915 } 916 917 sc->intr_hook.ich_func = axp209_start; 918 sc->intr_hook.ich_arg = dev; 919 920 if (config_intrhook_establish(&sc->intr_hook) != 0) 921 return (ENOMEM); 922 923 sc->nregs = nitems(axp209_regdefs); 924 sc->regs = malloc(sizeof(struct axp209_reg_sc *) * sc->nregs, 925 M_AXP209_REG, M_WAITOK | M_ZERO); 926 927 /* Attach known regulators that exist in the DT */ 928 rnode = ofw_bus_find_child(ofw_bus_get_node(dev), "regulators"); 929 if (rnode > 0) { 930 for (i = 0; i < sc->nregs; i++) { 931 child = ofw_bus_find_child(rnode, 932 axp209_regdefs[i].name); 933 if (child == 0) 934 continue; 935 reg = axp209_reg_attach(dev, child, &axp209_regdefs[i]); 936 if (reg == NULL) { 937 device_printf(dev, 938 "cannot attach regulator %s\n", 939 axp209_regdefs[i].name); 940 continue; 941 } 942 sc->regs[i] = reg; 943 } 944 } 945 946 sc->gpiodev = gpiobus_attach_bus(dev); 947 948 return (0); 949 } 950 951 static device_method_t axp209_methods[] = { 952 DEVMETHOD(device_probe, axp209_probe), 953 DEVMETHOD(device_attach, axp209_attach), 954 955 /* GPIO interface */ 956 DEVMETHOD(gpio_get_bus, axp209_gpio_get_bus), 957 DEVMETHOD(gpio_pin_max, axp209_gpio_pin_max), 958 DEVMETHOD(gpio_pin_getname, axp209_gpio_pin_getname), 959 DEVMETHOD(gpio_pin_getcaps, axp209_gpio_pin_getcaps), 960 DEVMETHOD(gpio_pin_getflags, axp209_gpio_pin_getflags), 961 DEVMETHOD(gpio_pin_setflags, axp209_gpio_pin_setflags), 962 DEVMETHOD(gpio_pin_get, axp209_gpio_pin_get), 963 DEVMETHOD(gpio_pin_set, axp209_gpio_pin_set), 964 DEVMETHOD(gpio_pin_toggle, axp209_gpio_pin_toggle), 965 DEVMETHOD(gpio_map_gpios, axp209_gpio_map_gpios), 966 967 /* Regdev interface */ 968 DEVMETHOD(regdev_map, axp209_regdev_map), 969 970 /* OFW bus interface */ 971 DEVMETHOD(ofw_bus_get_node, axp209_get_node), 972 973 DEVMETHOD_END 974 }; 975 976 static driver_t axp209_driver = { 977 "axp209_pmu", 978 axp209_methods, 979 sizeof(struct axp209_softc), 980 }; 981 982 static devclass_t axp209_devclass; 983 extern devclass_t ofwgpiobus_devclass, gpioc_devclass; 984 extern driver_t ofw_gpiobus_driver, gpioc_driver; 985 986 EARLY_DRIVER_MODULE(axp209, iicbus, axp209_driver, axp209_devclass, 987 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); 988 EARLY_DRIVER_MODULE(ofw_gpiobus, axp209_pmu, ofw_gpiobus_driver, 989 ofwgpiobus_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE); 990 DRIVER_MODULE(gpioc, axp209_pmu, gpioc_driver, gpioc_devclass, 991 0, 0); 992 MODULE_VERSION(axp209, 1); 993 MODULE_DEPEND(axp209, iicbus, 1, 1, 1); 994