Index: head/sys/arm/ti/am335x/am335x_pmic.c =================================================================== --- head/sys/arm/ti/am335x/am335x_pmic.c (revision 282826) +++ head/sys/arm/ti/am335x/am335x_pmic.c (revision 282827) @@ -1,211 +1,278 @@ /*- * Copyright (c) 2012 Damjan Marion * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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. */ #include __FBSDID("$FreeBSD$"); /* * TPS65217 PMIC companion chip for AM335x SoC sitting on I2C bus */ #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #include #include #include #include "iicbus_if.h" #define TPS65217A 0x7 #define TPS65217B 0xF #define TPS65217C 0xE #define TPS65217D 0x6 /* TPS65217 Reisters */ -#define TPS65217_CHIPID_REG 0x00 -#define TPS65217_STATUS_REG 0x0A -#define TPS65217_STATUS_OFF (1U << 7) -#define TPS65217_STATUS_ACPWR (1U << 3) -#define TPS65217_STATUS_USBPWR (1U << 2) -#define TPS65217_STATUS_BT (1U << 0) +#define TPS65217_CHIPID_REG 0x00 +#define TPS65217_INT_REG 0x02 +#define TPS65217_INT_PBM (1U << 6) +#define TPS65217_INT_ACM (1U << 5) +#define TPS65217_INT_USBM (1U << 4) +#define TPS65217_INT_PBI (1U << 2) +#define TPS65217_INT_ACI (1U << 1) +#define TPS65217_INT_USBI (1U << 0) +#define TPS65217_STATUS_REG 0x0A +#define TPS65217_STATUS_OFF (1U << 7) +#define TPS65217_STATUS_ACPWR (1U << 3) +#define TPS65217_STATUS_USBPWR (1U << 2) +#define TPS65217_STATUS_BT (1U << 0) + #define MAX_IIC_DATA_SIZE 2 struct am335x_pmic_softc { device_t sc_dev; uint32_t sc_addr; struct intr_config_hook enum_hook; + struct resource *sc_irq_res; + void *sc_intrhand; }; static void am335x_pmic_shutdown(void *, int); static int am335x_pmic_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size) { struct am335x_pmic_softc *sc = device_get_softc(dev); struct iic_msg msg[] = { { sc->sc_addr, IIC_M_WR, 1, &addr }, { sc->sc_addr, IIC_M_RD, size, data }, }; return (iicbus_transfer(dev, msg, 2)); } static int am335x_pmic_write(device_t dev, uint8_t address, uint8_t *data, uint8_t size) { uint8_t buffer[MAX_IIC_DATA_SIZE + 1]; struct am335x_pmic_softc *sc = device_get_softc(dev); struct iic_msg msg[] = { { sc->sc_addr, IIC_M_WR, size + 1, buffer }, }; if (size > MAX_IIC_DATA_SIZE) return (ENOMEM); buffer[0] = address; memcpy(buffer + 1, data, size); return (iicbus_transfer(dev, msg, 1)); } +static void +am335x_pmic_intr(void *arg) +{ + struct am335x_pmic_softc *sc = (struct am335x_pmic_softc *)arg; + uint8_t int_reg, status_reg; + int rv; + char notify_buf[16]; + + THREAD_SLEEPING_OK(); + rv = am335x_pmic_read(sc->sc_dev, TPS65217_INT_REG, &int_reg, 1); + if (rv != 0) { + device_printf(sc->sc_dev, "Cannot read interrupt register\n"); + THREAD_NO_SLEEPING(); + return; + } + rv = am335x_pmic_read(sc->sc_dev, TPS65217_STATUS_REG, &status_reg, 1); + if (rv != 0) { + device_printf(sc->sc_dev, "Cannot read status register\n"); + THREAD_NO_SLEEPING(); + return; + } + THREAD_NO_SLEEPING(); + + if ((int_reg & TPS65217_INT_PBI) && (status_reg & TPS65217_STATUS_BT)) + shutdown_nice(RB_POWEROFF); + if (int_reg & TPS65217_INT_ACI) { + snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", + (status_reg & TPS65217_STATUS_ACPWR) ? 1 : 0); + devctl_notify_f("ACPI", "ACAD", "power", notify_buf, M_NOWAIT); + } +} + static int am335x_pmic_probe(device_t dev) { struct am335x_pmic_softc *sc; if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic")) return (ENXIO); sc = device_get_softc(dev); sc->sc_dev = dev; sc->sc_addr = iicbus_get_addr(dev); device_set_desc(dev, "TI TPS65217 Power Management IC"); return (0); } static void am335x_pmic_start(void *xdev) { struct am335x_pmic_softc *sc; device_t dev = (device_t)xdev; uint8_t reg; char name[20]; char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"}; + int rv; sc = device_get_softc(dev); am335x_pmic_read(dev, TPS65217_CHIPID_REG, ®, 1); switch (reg>>4) { case TPS65217A: sprintf(name, "TPS65217A ver 1.%u", reg & 0xF); break; case TPS65217B: sprintf(name, "TPS65217B ver 1.%u", reg & 0xF); break; case TPS65217C: sprintf(name, "TPS65217C ver 1.%u", reg & 0xF); break; case TPS65217D: sprintf(name, "TPS65217D ver 1.%u", reg & 0xF); break; default: sprintf(name, "Unknown PMIC"); } am335x_pmic_read(dev, TPS65217_STATUS_REG, ®, 1); device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]); EVENTHANDLER_REGISTER(shutdown_final, am335x_pmic_shutdown, dev, SHUTDOWN_PRI_LAST); config_intrhook_disestablish(&sc->enum_hook); + + /* Unmask all interrupts and clear pending status */ + reg = 0; + am335x_pmic_write(dev, TPS65217_INT_REG, ®, 1); + am335x_pmic_read(dev, TPS65217_INT_REG, ®, 1); + + if (sc->sc_irq_res != NULL) { + rv = bus_setup_intr(dev, sc->sc_irq_res, + INTR_TYPE_MISC | INTR_MPSAFE, NULL, am335x_pmic_intr, + sc, &sc->sc_intrhand); + if (rv != 0) + device_printf(dev, + "Unable to setup the irq handler.\n"); + } } static int am335x_pmic_attach(device_t dev) { struct am335x_pmic_softc *sc; + int rid; sc = device_get_softc(dev); + + rid = 0; + sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, + RF_ACTIVE); + if (!sc->sc_irq_res) { + device_printf(dev, "cannot allocate interrupt\n"); + /* return (ENXIO); */ + } sc->enum_hook.ich_func = am335x_pmic_start; sc->enum_hook.ich_arg = dev; if (config_intrhook_establish(&sc->enum_hook) != 0) return (ENOMEM); return (0); } static void am335x_pmic_shutdown(void *xdev, int howto) { device_t dev; uint8_t reg; if (!(howto & RB_POWEROFF)) return; dev = (device_t)xdev; /* Set the OFF bit on status register to start the shutdown sequence. */ reg = TPS65217_STATUS_OFF; am335x_pmic_write(dev, TPS65217_STATUS_REG, ®, 1); /* Toggle pmic_pwr_enable to shutdown the PMIC. */ am335x_rtc_pmic_pwr_toggle(); } static device_method_t am335x_pmic_methods[] = { DEVMETHOD(device_probe, am335x_pmic_probe), DEVMETHOD(device_attach, am335x_pmic_attach), {0, 0}, }; static driver_t am335x_pmic_driver = { "am335x_pmic", am335x_pmic_methods, sizeof(struct am335x_pmic_softc), }; static devclass_t am335x_pmic_devclass; DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0); MODULE_VERSION(am335x_pmic, 1); MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1); Index: head/sys/boot/fdt/dts/arm/beaglebone-black.dts =================================================================== --- head/sys/boot/fdt/dts/arm/beaglebone-black.dts (revision 282826) +++ head/sys/boot/fdt/dts/arm/beaglebone-black.dts (revision 282827) @@ -1,184 +1,186 @@ /*- * Copyright (c) 2012 Damjan Marion * 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 AND CONTRIBUTORS ``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 OR CONTRIBUTORS 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$ */ /dts-v1/; /include/ "am335x.dtsi" / { model = "beaglebone-black"; compatible = "beaglebone-black", "beaglebone", "ti,am335x"; aliases { soc = &SOC; uart0 = &uart0; }; memory { device_type = "memory"; reg = < 0x80000000 0x20000000 >; /* 512MB RAM */ }; am335x { scm@44e10000 { /* Set of triplets < padname, muxname, padstate> */ scm-pad-config = /* I2C0 */ "I2C0_SDA", "I2C0_SDA","i2c", "I2C0_SCL", "I2C0_SCL","i2c", /* I2C1 */ "SPI0_D1", "I2C1_SDA", "i2c", "SPI0_CS0", "I2C1_SCL", "i2c", /* I2C2 */ "UART1_CTSn", "I2C2_SDA", "i2c", "UART1_RTSn", "I2C2_SCL", "i2c", /* Ethernet */ "MII1_RX_ER", "gmii1_rxerr", "input_pulldown", "MII1_TX_EN", "gmii1_txen", "output", "MII1_RX_DV", "gmii1_rxdv", "input_pulldown", "MII1_TXD3", "gmii1_txd3", "output", "MII1_TXD2", "gmii1_txd2", "output", "MII1_TXD1", "gmii1_txd1", "output", "MII1_TXD0", "gmii1_txd0", "output", "MII1_TX_CLK", "gmii1_txclk", "input_pulldown", "MII1_RX_CLK", "gmii1_rxclk", "input_pulldown", "MII1_RXD3", "gmii1_rxd3", "input_pulldown", "MII1_RXD2", "gmii1_rxd2", "input_pulldown", "MII1_RXD1", "gmii1_rxd1", "input_pulldown", "MII1_RXD0", "gmii1_rxd0", "input_pulldown", "MDIO", "mdio_data", "input_pullup", "MDC", "mdio_clk", "output_pullup", /* MMCSD0 */ "MMC0_CMD", "mmc0_cmd", "input_pullup", "MMC0_CLK", "mmc0_clk", "input_pullup", "MMC0_DAT0", "mmc0_dat0", "input_pullup", "MMC0_DAT1", "mmc0_dat1", "input_pullup", "MMC0_DAT2", "mmc0_dat2", "input_pullup", "MMC0_DAT3", "mmc0_dat3", "input_pullup", /* MMC1 */ "GPMC_CSn1", "mmc1_clk", "input_pullup", "GPMC_CSn2", "mmc1_cmd", "input_pullup", "GPMC_CSn3", "gpio2_0", "output_pullup", /* Reset */ "GPMC_AD0", "mmc1_dat0", "input_pullup", "GPMC_AD1", "mmc1_dat1", "input_pullup", "GPMC_AD2", "mmc1_dat2", "input_pullup", "GPMC_AD3", "mmc1_dat3", "input_pullup", "GPMC_AD4", "mmc1_dat4", "input_pullup", "GPMC_AD5", "mmc1_dat5", "input_pullup", "GPMC_AD6", "mmc1_dat6", "input_pullup", "GPMC_AD7", "mmc1_dat7", "input_pullup", /* GPIO */ "ECAP0_IN_PWM0_OUT", "gpio0_7", "input_pulldown", "GPMC_AD10", "gpio0_26", "input_pulldown", "GPMC_AD11", "gpio0_27", "input_pulldown", "GPMC_AD12", "gpio1_12", "input_pulldown", "GPMC_AD13", "gpio1_13", "input_pulldown", "GPMC_AD14", "gpio1_14", "input_pulldown", "GPMC_AD15", "gpio1_15", "input_pulldown", "GPMC_A0", "gpio1_16", "input_pulldown", "GPMC_A1", "gpio1_17", "input_pulldown", "GPMC_A5", "gpio1_21", "output", /* User LED 1 */ "GPMC_A6", "gpio1_22", "output", /* User LED 2 */ "GPMC_A7", "gpio1_23", "output", /* User LED 3 */ "GPMC_A8", "gpio1_24", "output", /* User LED 4 */ "GPMC_BEn1", "gpio1_28", "input_pulldown", "GPMC_CSn0", "gpio1_29", "input_pulldown", "GPMC_CLK", "gpio2_1", "input_pulldown", "LCD_DATA0", "gpio2_6", "input_pulldown", "LCD_DATA1", "gpio2_7", "input_pulldown", "LCD_DATA2", "gpio2_8", "input_pulldown", "LCD_DATA3", "gpio2_9", "input_pulldown", "LCD_DATA4", "gpio2_10", "input_pulldown", "LCD_DATA5", "gpio2_11", "input_pulldown", "LCD_DATA6", "gpio2_12", "input_pulldown", "LCD_DATA7", "gpio2_13", "input_pulldown", "LCD_VSYNC", "gpio2_22", "input_pulldown", "LCD_HSYNC", "gpio2_23", "input_pulldown", "LCD_PCLK", "gpio2_24", "input_pulldown", "LCD_AC_BIAS_EN", "gpio2_25", "input_pulldown", "MCASP0_FSR", "gpio3_19", "input_pulldown", "MCASP0_AHCLKX", "gpio3_21", "input_pulldown", /* TIMERs */ "GPMC_ADVn_ALE", "timer4", "output", "GPMC_BEn0_CLE", "timer5", "output", "GPMC_WEn", "timer6", "output", "GPMC_OEn_REn", "timer7", "output", /* USB0 and USB1 */ "USB0_DRVVBUS", "USB0_DRVVBUS", "output", "USB1_DRVVBUS", "USB1_DRVVBUS", "output", /* PWM */ "GPMC_A2", "ehrpwm1A", "output", "GPMC_A3", "ehrpwm1B", "output", "GPMC_AD8", "ehrpwm2A", "output", "GPMC_AD9", "ehrpwm2B", "output"; }; mmchs1@481D8000 { bus-width = <8>; status = "okay"; non-removable; }; i2c@44e0b000 { pmic@48 { compatible = "ti,am335x-pmic"; reg = <0x48>; + interrupts = <7>; /* nNMI */ + interrupt-parent = <&AINTC>; }; }; }; leds { compatible = "gpio-leds"; led1 { gpios = <&GPIO 53 2 0>; name = "led1"; }; led2 { gpios = <&GPIO 54 2 0>; name = "led2"; }; led3 { gpios = <&GPIO 55 2 0>; name = "led3"; }; led4 { gpios = <&GPIO 56 2 0>; name = "led4"; }; }; chosen { stdin = "uart0"; stdout = "uart0"; }; };