axp209.c revision 295658
1/*- 2 * Copyright (c) 2015 Emmanuel Vadot <manu@bidouilliste.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, 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 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/arm/allwinner/axp209.c 295658 2016-02-16 11:51:54Z andrew $"); 29/* 30* X-Power AXP209 PMU for Allwinner SoCs 31*/ 32#include <sys/param.h> 33#include <sys/systm.h> 34#include <sys/eventhandler.h> 35#include <sys/kernel.h> 36#include <sys/module.h> 37#include <sys/clock.h> 38#include <sys/time.h> 39#include <sys/bus.h> 40#include <sys/proc.h> 41#include <sys/reboot.h> 42#include <sys/resource.h> 43#include <sys/rman.h> 44#include <sys/sysctl.h> 45 46#include <dev/iicbus/iicbus.h> 47#include <dev/iicbus/iiconf.h> 48 49#include <dev/ofw/openfirm.h> 50#include <dev/ofw/ofw_bus.h> 51#include <dev/ofw/ofw_bus_subr.h> 52 53#include "iicbus_if.h" 54 55/* Power State Register */ 56#define AXP209_PSR 0x00 57#define AXP209_PSR_ACIN 0x80 58#define AXP209_PSR_ACIN_SHIFT 7 59#define AXP209_PSR_VBUS 0x20 60#define AXP209_PSR_VBUS_SHIFT 5 61 62/* Shutdown and battery control */ 63#define AXP209_SHUTBAT 0x32 64#define AXP209_SHUTBAT_SHUTDOWN 0x80 65 66/* Temperature monitor */ 67#define AXP209_TEMPMON 0x5e 68#define AXP209_TEMPMON_H(a) ((a) << 4) 69#define AXP209_TEMPMON_L(a) ((a) & 0xf) 70#define AXP209_TEMPMON_MIN 1447 /* -144.7C */ 71 72#define AXP209_0C_TO_K 2732 73 74struct axp209_softc { 75 uint32_t addr; 76 struct intr_config_hook enum_hook; 77}; 78 79enum axp209_sensor { 80 AXP209_TEMP 81}; 82 83static int 84axp209_read(device_t dev, uint8_t reg, uint8_t *data, uint8_t size) 85{ 86 struct axp209_softc *sc = device_get_softc(dev); 87 struct iic_msg msg[2]; 88 89 msg[0].slave = sc->addr; 90 msg[0].flags = IIC_M_WR; 91 msg[0].len = 1; 92 msg[0].buf = ® 93 94 msg[1].slave = sc->addr; 95 msg[1].flags = IIC_M_RD; 96 msg[1].len = size; 97 msg[1].buf = data; 98 99 return (iicbus_transfer(dev, msg, 2)); 100} 101 102static int 103axp209_write(device_t dev, uint8_t reg, uint8_t data) 104{ 105 uint8_t buffer[2]; 106 struct axp209_softc *sc = device_get_softc(dev); 107 struct iic_msg msg; 108 109 buffer[0] = reg; 110 buffer[1] = data; 111 112 msg.slave = sc->addr; 113 msg.flags = IIC_M_WR; 114 msg.len = 2; 115 msg.buf = buffer; 116 117 return (iicbus_transfer(dev, &msg, 1)); 118} 119 120static int 121axp209_sysctl(SYSCTL_HANDLER_ARGS) 122{ 123 device_t dev = arg1; 124 enum axp209_sensor sensor = arg2; 125 uint8_t data[2]; 126 int val, error; 127 128 if (sensor != AXP209_TEMP) 129 return (ENOENT); 130 131 error = axp209_read(dev, AXP209_TEMPMON, data, 2); 132 if (error != 0) 133 return (error); 134 135 /* Temperature is between -144.7C and 264.8C, step +0.1C */ 136 val = (AXP209_TEMPMON_H(data[0]) | AXP209_TEMPMON_L(data[1])) - 137 AXP209_TEMPMON_MIN + AXP209_0C_TO_K; 138 139 return sysctl_handle_opaque(oidp, &val, sizeof(val), req); 140} 141 142static void 143axp209_shutdown(void *devp, int howto) 144{ 145 device_t dev; 146 147 if (!(howto & RB_POWEROFF)) 148 return; 149 dev = (device_t)devp; 150 151 if (bootverbose) 152 device_printf(dev, "Shutdown AXP209\n"); 153 154 axp209_write(dev, AXP209_SHUTBAT, AXP209_SHUTBAT_SHUTDOWN); 155} 156 157static int 158axp209_probe(device_t dev) 159{ 160 161 if (!ofw_bus_status_okay(dev)) 162 return (ENXIO); 163 164 if (!ofw_bus_is_compatible(dev, "x-powers,axp209")) 165 return (ENXIO); 166 167 device_set_desc(dev, "X-Power AXP209 Power Management Unit"); 168 169 return (BUS_PROBE_DEFAULT); 170} 171 172static int 173axp209_attach(device_t dev) 174{ 175 struct axp209_softc *sc; 176 const char *pwr_name[] = {"Battery", "AC", "USB", "AC and USB"}; 177 uint8_t data; 178 uint8_t pwr_src; 179 180 sc = device_get_softc(dev); 181 182 sc->addr = iicbus_get_addr(dev); 183 184 if (bootverbose) { 185 /* 186 * Read the Power State register. 187 * Shift the AC presence into bit 0. 188 * Shift the Battery presence into bit 1. 189 */ 190 axp209_read(dev, AXP209_PSR, &data, 1); 191 pwr_src = ((data & AXP209_PSR_ACIN) >> AXP209_PSR_ACIN_SHIFT) | 192 ((data & AXP209_PSR_VBUS) >> (AXP209_PSR_VBUS_SHIFT - 1)); 193 194 device_printf(dev, "AXP209 Powered by %s\n", 195 pwr_name[pwr_src]); 196 } 197 198 EVENTHANDLER_REGISTER(shutdown_final, axp209_shutdown, dev, 199 SHUTDOWN_PRI_LAST); 200 201 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 202 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), 203 OID_AUTO, "temp", 204 CTLTYPE_INT | CTLFLAG_RD, 205 dev, AXP209_TEMP, axp209_sysctl, "IK", "Internal temperature"); 206 207 return (0); 208} 209 210static device_method_t axp209_methods[] = { 211 DEVMETHOD(device_probe, axp209_probe), 212 DEVMETHOD(device_attach, axp209_attach), 213 {0, 0}, 214}; 215 216static driver_t axp209_driver = { 217 "axp209_pmu", 218 axp209_methods, 219 sizeof(struct axp209_softc), 220}; 221 222static devclass_t axp209_devclass; 223 224DRIVER_MODULE(axp209, iicbus, axp209_driver, axp209_devclass, 0, 0); 225MODULE_VERSION(axp209, 1); 226MODULE_DEPEND(axp209, iicbus, 1, 1, 1); 227