Index: sys/arm/allwinner/axp209.c =================================================================== --- sys/arm/allwinner/axp209.c +++ sys/arm/allwinner/axp209.c @@ -50,36 +50,26 @@ #include #include -#include "iicbus_if.h" - -/* Power State Register */ -#define AXP209_PSR 0x00 -#define AXP209_PSR_ACIN 0x80 -#define AXP209_PSR_ACIN_SHIFT 7 -#define AXP209_PSR_VBUS 0x20 -#define AXP209_PSR_VBUS_SHIFT 5 - -/* Shutdown and battery control */ -#define AXP209_SHUTBAT 0x32 -#define AXP209_SHUTBAT_SHUTDOWN 0x80 +#include -/* Temperature monitor */ -#define AXP209_TEMPMON 0x5e -#define AXP209_TEMPMON_H(a) ((a) << 4) -#define AXP209_TEMPMON_L(a) ((a) & 0xf) -#define AXP209_TEMPMON_MIN 1447 /* -144.7C */ - -#define AXP209_0C_TO_K 2732 +#include "iicbus_if.h" struct axp209_softc { + device_t dev; uint32_t addr; - struct intr_config_hook enum_hook; + struct resource * res[1]; + void * intrcookie; + struct intr_config_hook intr_hook; }; -enum axp209_sensor { - AXP209_TEMP +static struct resource_spec axp_res_spec[] = { + { SYS_RES_IRQ, 0, RF_ACTIVE }, + { -1, 0, 0 } }; +static int axp209_bootverbose = 0; +TUNABLE_INT("hw.axp209.bootverbose", &axp209_bootverbose); + static int axp209_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) { @@ -125,16 +115,75 @@ uint8_t data[2]; int val, error; - if (sensor != AXP209_TEMP) + switch (sensor) { + case AXP209_TEMP: + error = axp209_read(dev, AXP209_TEMPMON, data, 2); + if (error != 0) + return (error); + + /* Temperature is between -144.7C and 264.8C, step +0.1C */ + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) - + AXP209_TEMPMON_MIN + AXP209_0C_TO_K; + break; + case AXP209_ACVOLT: + error = axp209_read(dev, AXP209_ACIN_VOLTAGE, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_VOLT_STEP; + break; + case AXP209_ACCURRENT: + error = axp209_read(dev, AXP209_ACIN_CURRENT, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_ACCURRENT_STEP; + break; + case AXP209_VBUSVOLT: + error = axp209_read(dev, AXP209_VBUS_VOLTAGE, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_VOLT_STEP; + break; + case AXP209_VBUSCURRENT: + error = axp209_read(dev, AXP209_VBUS_CURRENT, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_VBUSCURRENT_STEP; + break; + case AXP209_BATVOLT: + error = axp209_read(dev, AXP209_BAT_VOLTAGE, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_BATVOLT_STEP; + break; + case AXP209_BATCHARGECURRENT: + error = axp209_read(dev, AXP209_BAT_CHARGE_CURRENT, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_H(data[0]) | AXP209_SENSOR_L(data[1])) * + AXP209_BATCURRENT_STEP; + break; + case AXP209_BATDISCHARGECURRENT: + error = axp209_read(dev, AXP209_BAT_DISCHARGE_CURRENT, data, 2); + if (error != 0) + return (error); + + val = (AXP209_SENSOR_BAT_H(data[0]) | + AXP209_SENSOR_BAT_L(data[1])) * AXP209_BATCURRENT_STEP; + break; + default: return (ENOENT); - - error = axp209_read(dev, AXP209_TEMPMON, data, 2); - if (error != 0) - return (error); - - /* Temperature is between -144.7C and 264.8C, step +0.1C */ - val = (AXP209_TEMPMON_H(data[0]) | AXP209_TEMPMON_L(data[1])) - - AXP209_TEMPMON_MIN + AXP209_0C_TO_K; + } return sysctl_handle_opaque(oidp, &val, sizeof(val), req); } @@ -154,6 +203,72 @@ axp209_write(dev, AXP209_SHUTBAT, AXP209_SHUTBAT_SHUTDOWN); } +static void +axp_intr(void *arg) +{ + struct axp209_softc *sc; + uint8_t reg; + + sc = arg; + + axp209_read(sc->dev, AXP209_IRQ1_STATUS, ®, 1); + if (reg) { + if (reg & AXP209_IRQ1_AC_OVERVOLT) + device_printf(sc->dev, "AC overvoltage\n"); + if (reg & AXP209_IRQ1_VBUS_OVERVOLT) + device_printf(sc->dev, "USB overvoltage\n"); + if (reg & AXP209_IRQ1_VBUS_LOW) + device_printf(sc->dev, "USB voltage is too low\n"); + if (bootverbose || axp209_bootverbose) { + if (reg & AXP209_IRQ1_AC_CONN) + device_printf(sc->dev, "AC plugged\n"); + if (reg & AXP209_IRQ1_AC_DISCONN) + device_printf(sc->dev, "AC unplugged\n"); + if (reg & AXP209_IRQ1_VBUS_CONN) + device_printf(sc->dev, "USB plugged\n"); + if (reg & AXP209_IRQ1_VBUS_DISCONN) + device_printf(sc->dev, "USB unplugged\n"); + } + axp209_write(sc->dev, AXP209_IRQ1_STATUS, AXP209_IRQ_ACK); + } + + axp209_read(sc->dev, AXP209_IRQ2_STATUS, ®, 1); + if (reg) { + if (bootverbose || axp209_bootverbose) { + if (reg & AXP209_IRQ2_BATT_CHARGED) + device_printf(sc->dev, "Battery charging complete\n"); + if (reg & AXP209_IRQ2_BATT_CHARGING) + device_printf(sc->dev, "Battery charging\n"); + if (reg & AXP209_IRQ2_BATT_CONN) + device_printf(sc->dev, "Battery connected\n"); + if (reg & AXP209_IRQ2_BATT_DISCONN) + device_printf(sc->dev, "Battery disconnected\n"); + if (reg & AXP209_IRQ2_BATT_TEMP_LOW) + device_printf(sc->dev, "Battery low temp\n"); + if (reg & AXP209_IRQ2_BATT_TEMP_OVER) + device_printf(sc->dev, "Battery high temp\n"); + } + axp209_write(sc->dev, AXP209_IRQ2_STATUS, AXP209_IRQ_ACK); + } + + axp209_read(sc->dev, AXP209_IRQ3_STATUS, ®, 1); + if (reg) { + if (reg & AXP209_IRQ3_PEK_SHORT) + shutdown_nice(RB_POWEROFF); + axp209_write(sc->dev, AXP209_IRQ3_STATUS, AXP209_IRQ_ACK); + } + + axp209_read(sc->dev, AXP209_IRQ4_STATUS, ®, 1); + if (reg) { + axp209_write(sc->dev, AXP209_IRQ4_STATUS, AXP209_IRQ_ACK); + } + + axp209_read(sc->dev, AXP209_IRQ5_STATUS, ®, 1); + if (reg) { + axp209_write(sc->dev, AXP209_IRQ5_STATUS, AXP209_IRQ_ACK); + } +} + static int axp209_probe(device_t dev) { @@ -169,19 +284,22 @@ return (BUS_PROBE_DEFAULT); } -static int -axp209_attach(device_t dev) +static void +axp209_start(void *pdev) { + device_t dev; struct axp209_softc *sc; const char *pwr_name[] = {"Battery", "AC", "USB", "AC and USB"}; uint8_t data; uint8_t pwr_src; - sc = device_get_softc(dev); + dev = pdev; + sc = device_get_softc(dev); sc->addr = iicbus_get_addr(dev); + sc->dev = dev; - if (bootverbose) { + if (bootverbose || axp209_bootverbose) { /* * Read the Power State register. * Shift the AC presence into bit 0. @@ -195,15 +313,113 @@ pwr_name[pwr_src]); } + /* Only enable interrupts that we are interested in */ + axp209_write(dev, AXP209_IRQ1_ENABLE, + AXP209_IRQ1_AC_OVERVOLT | + AXP209_IRQ1_AC_DISCONN | + AXP209_IRQ1_AC_CONN | + AXP209_IRQ1_VBUS_OVERVOLT | + AXP209_IRQ1_VBUS_DISCONN | + AXP209_IRQ1_VBUS_CONN); + axp209_write(dev, AXP209_IRQ2_ENABLE, + AXP209_IRQ2_BATT_CONN | + AXP209_IRQ2_BATT_DISCONN | + AXP209_IRQ2_BATT_CHARGE_ACCT_ON | + AXP209_IRQ2_BATT_CHARGE_ACCT_OFF | + AXP209_IRQ2_BATT_CHARGING | + AXP209_IRQ2_BATT_CHARGED | + AXP209_IRQ2_BATT_TEMP_OVER | + AXP209_IRQ2_BATT_TEMP_LOW); + axp209_write(dev, AXP209_IRQ3_ENABLE, + AXP209_IRQ3_PEK_SHORT | AXP209_IRQ3_PEK_LONG); + axp209_write(dev, AXP209_IRQ4_ENABLE, AXP209_IRQ4_APS_LOW_2); + axp209_write(dev, AXP209_IRQ5_ENABLE, 0x0); + EVENTHANDLER_REGISTER(shutdown_final, axp209_shutdown, dev, SHUTDOWN_PRI_LAST); + /* Enable ADC sensors */ + if (axp209_write(dev, AXP209_ADC_ENABLE1, + AXP209_ADC1_BATVOLT | AXP209_ADC1_BATCURRENT | + AXP209_ADC1_ACVOLT | AXP209_ADC1_ACCURRENT | + AXP209_ADC1_VBUSVOLT | AXP209_ADC1_VBUSCURRENT) != -1) { + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "acvolt", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_ACVOLT, axp209_sysctl, "I", + "AC Voltage (microVolt)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "accurrent", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_ACCURRENT, axp209_sysctl, "I", + "AC Current (microAmpere)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "vbusvolt", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_VBUSVOLT, axp209_sysctl, "I", + "VBUS Voltage (microVolt)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "vbuscurrent", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_VBUSCURRENT, axp209_sysctl, "I", + "VBUS Current (microAmpere)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "batvolt", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_BATVOLT, axp209_sysctl, "I", + "Battery Voltage (microVolt)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "batchargecurrent", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_BATCHARGECURRENT, axp209_sysctl, "I", + "Battery Charging Current (microAmpere)"); + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), + OID_AUTO, "batdischargecurrent", + CTLTYPE_INT | CTLFLAG_RD, + dev, AXP209_BATDISCHARGECURRENT, axp209_sysctl, "I", + "Battery Discharging Current (microAmpere)"); + } else { + device_printf(dev, "Couldn't enable ADC sensors\n"); + } + SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "temp", CTLTYPE_INT | CTLFLAG_RD, dev, AXP209_TEMP, axp209_sysctl, "IK", "Internal temperature"); + if ((bus_setup_intr(dev, sc->res[0], INTR_TYPE_MISC | INTR_MPSAFE, + NULL, axp_intr, sc, &sc->intrcookie))) + device_printf(dev, "unable to register interrupt handler\n"); + + config_intrhook_disestablish(&sc->intr_hook); +} + +static int +axp209_attach(device_t dev) +{ + struct axp209_softc *sc; + + sc = device_get_softc(dev); + + if (bus_alloc_resources(dev, axp_res_spec, sc->res) != 0) { + device_printf(dev, "can't allocate device resources\n"); + return (ENXIO); + } + + sc->intr_hook.ich_func = axp209_start; + sc->intr_hook.ich_arg = dev; + + if (config_intrhook_establish(&sc->intr_hook) != 0) + return (ENOMEM); + return (0); } Index: sys/arm/allwinner/axp209reg.h =================================================================== --- /dev/null +++ sys/arm/allwinner/axp209reg.h @@ -0,0 +1,146 @@ +/*- + * Copyright (c) 2016 Emmanuel Vadot + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#ifndef _AXP209REG_H_ +#define _AXP209REG_H_ + +/* Power State Register */ +#define AXP209_PSR 0x00 +#define AXP209_PSR_ACIN 0x80 +#define AXP209_PSR_ACIN_SHIFT 7 +#define AXP209_PSR_VBUS 0x20 +#define AXP209_PSR_VBUS_SHIFT 5 + +/* Shutdown and battery control */ +#define AXP209_SHUTBAT 0x32 +#define AXP209_SHUTBAT_SHUTDOWN 0x80 + +/* Voltage/Current Monitor */ +#define AXP209_ACIN_VOLTAGE 0x56 +#define AXP209_ACIN_CURRENT 0x58 +#define AXP209_VBUS_VOLTAGE 0x5A +#define AXP209_VBUS_CURRENT 0x5C +#define AXP209_BAT_VOLTAGE 0x78 +#define AXP209_BAT_CHARGE_CURRENT 0x7A +#define AXP209_BAT_DISCHARGE_CURRENT 0x7C + +#define AXP209_VOLT_STEP 1700 +#define AXP209_BATVOLT_STEP 1100 +#define AXP209_ACCURRENT_STEP 625 +#define AXP209_VBUSCURRENT_STEP 375 +#define AXP209_BATCURRENT_STEP 500 + +/* Temperature monitor */ +#define AXP209_TEMPMON 0x5e +#define AXP209_TEMPMON_MIN 1447 /* -144.7C */ + +/* Sensors conversion macros */ +#define AXP209_SENSOR_H(a) ((a) << 4) +#define AXP209_SENSOR_L(a) ((a) & 0xf) +#define AXP209_SENSOR_BAT_H(a) ((a) << 5) +#define AXP209_SENSOR_BAT_L(a) ((a) & 0x1f) + +#define AXP209_0C_TO_K 2732 + +/* ADC Sensors */ +#define AXP209_ADC_ENABLE1 0x82 +#define AXP209_ADC_ENABLE2 0x83 + +#define AXP209_ADC1_BATVOLT (1 << 7) +#define AXP209_ADC1_BATCURRENT (1 << 6) +#define AXP209_ADC1_ACVOLT (1 << 5) +#define AXP209_ADC1_ACCURRENT (1 << 4) +#define AXP209_ADC1_VBUSVOLT (1 << 3) +#define AXP209_ADC1_VBUSCURRENT (1 << 2) + +/* Interrupt related registers */ +#define AXP209_IRQ1_ENABLE 0x40 +#define AXP209_IRQ1_STATUS 0x48 +#define AXP209_IRQ1_AC_OVERVOLT (1 << 7) +#define AXP209_IRQ1_AC_CONN (1 << 6) +#define AXP209_IRQ1_AC_DISCONN (1 << 5) +#define AXP209_IRQ1_VBUS_OVERVOLT (1 << 4) +#define AXP209_IRQ1_VBUS_CONN (1 << 3) +#define AXP209_IRQ1_VBUS_DISCONN (1 << 2) +#define AXP209_IRQ1_VBUS_LOW (1 << 1) + +#define AXP209_IRQ2_ENABLE 0x41 +#define AXP209_IRQ2_STATUS 0x49 +#define AXP209_IRQ2_BATT_CONN (1 << 7) +#define AXP209_IRQ2_BATT_DISCONN (1 << 6) +#define AXP209_IRQ2_BATT_CHARGE_ACCT_ON (1 << 5) +#define AXP209_IRQ2_BATT_CHARGE_ACCT_OFF (1 << 4) +#define AXP209_IRQ2_BATT_CHARGING (1 << 3) +#define AXP209_IRQ2_BATT_CHARGED (1 << 2) +#define AXP209_IRQ2_BATT_TEMP_OVER (1 << 1) +#define AXP209_IRQ2_BATT_TEMP_LOW (1 << 0) + +#define AXP209_IRQ3_ENABLE 0x42 +#define AXP209_IRQ3_STATUS 0x4A +#define AXP209_IRQ3_TEMP_OVER (1 << 7) +#define AXP209_IRQ3_CHARGE_CURRENT_LOW (1 << 6) +#define AXP209_IRQ3_DCDC2_LOW (1 << 4) +#define AXP209_IRQ3_DCDC3_LOW (1 << 3) +#define AXP209_IRQ3_LDO3_LOW (1 << 2) +#define AXP209_IRQ3_PEK_SHORT (1 << 1) +#define AXP209_IRQ3_PEK_LONG (1 << 0) + +#define AXP209_IRQ4_ENABLE 0x43 +#define AXP209_IRQ4_STATUS 0x4B +#define AXP209_IRQ4_NOE_START (1 << 7) +#define AXP209_IRQ4_NOE_SHUT (1 << 6) +#define AXP209_IRQ4_VBUS_VALID (1 << 5) +#define AXP209_IRQ4_VBUS_INVALID (1 << 4) +#define AXP209_IRQ4_VBUS_SESSION (1 << 3) +#define AXP209_IRQ4_VBUS_SESSION_END (1 << 2) +#define AXP209_IRQ4_APS_LOW_1 (1 << 1) +#define AXP209_IRQ4_APS_LOW_2 (1 << 0) + +#define AXP209_IRQ5_ENABLE 0x44 +#define AXP209_IRQ5_STATUS 0x4C +#define AXP209_IRQ5_TIMER_EXPIRE (1 << 7) +#define AXP209_IRQ5_PEK_RISE_EDGE (1 << 6) +#define AXP209_IRQ5_PEK_FALL_EDGE (1 << 5) +#define AXP209_IRQ5_GPIO3 (1 << 3) +#define AXP209_IRQ5_GPIO2 (1 << 2) +#define AXP209_IRQ5_GPIO1 (1 << 1) +#define AXP209_IRQ5_GPIO0 (1 << 0) + +#define AXP209_IRQ_ACK 0xff + +enum axp209_sensor { + AXP209_TEMP, + AXP209_ACVOLT, + AXP209_ACCURRENT, + AXP209_VBUSVOLT, + AXP209_VBUSCURRENT, + AXP209_BATVOLT, + AXP209_BATCHARGECURRENT, + AXP209_BATDISCHARGECURRENT, +}; + +#endif /* _AXP209REG_H_ */