1 /*- 2 * Copyright (c) 2012 Damjan Marion <dmarion@Freebsd.org> 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$"); 29 /* 30 * TPS65217 PMIC companion chip for AM335x SoC sitting on I2C bus 31 */ 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/module.h> 36 #include <sys/clock.h> 37 #include <sys/time.h> 38 #include <sys/bus.h> 39 #include <sys/resource.h> 40 #include <sys/rman.h> 41 42 #include <dev/iicbus/iicbus.h> 43 #include <dev/iicbus/iiconf.h> 44 45 #include <dev/ofw/openfirm.h> 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include "iicbus_if.h" 50 51 #define TPS65217A 0x7 52 #define TPS65217B 0xF 53 54 /* TPS65217 Reisters */ 55 #define TPS65217_CHIPID_REG 0x00 56 #define TPS65217_STATUS_REG 0x0A 57 58 #define MAX_IIC_DATA_SIZE 2 59 60 61 struct am335x_pmic_softc { 62 device_t sc_dev; 63 uint32_t sc_addr; 64 struct intr_config_hook enum_hook; 65 }; 66 67 static int 68 am335x_pmic_read(device_t dev, uint8_t addr, uint8_t *data, uint8_t size) 69 { 70 struct am335x_pmic_softc *sc = device_get_softc(dev); 71 struct iic_msg msg[] = { 72 { sc->sc_addr, IIC_M_WR, 1, &addr }, 73 { sc->sc_addr, IIC_M_RD, size, data }, 74 }; 75 return (iicbus_transfer(dev, msg, 2)); 76 } 77 78 #ifdef notyet 79 static int 80 am335x_pmic_write(device_t dev, uint8_t address, uint8_t *data, uint8_t size) 81 { 82 uint8_t buffer[MAX_IIC_DATA_SIZE + 1]; 83 struct am335x_pmic_softc *sc = device_get_softc(dev); 84 struct iic_msg msg[] = { 85 { sc->sc_addr, IIC_M_WR, size + 1, buffer }, 86 }; 87 88 if (size > MAX_IIC_DATA_SIZE) 89 return (ENOMEM); 90 91 buffer[0] = address; 92 memcpy(buffer + 1, data, size); 93 94 return (iicbus_transfer(dev, msg, 1)); 95 } 96 #endif 97 98 static int 99 am335x_pmic_probe(device_t dev) 100 { 101 struct am335x_pmic_softc *sc; 102 103 if (!ofw_bus_is_compatible(dev, "ti,am335x-pmic")) 104 return (ENXIO); 105 106 sc = device_get_softc(dev); 107 sc->sc_dev = dev; 108 sc->sc_addr = iicbus_get_addr(dev); 109 110 device_set_desc(dev, "TI TPS65217 Power Management IC"); 111 112 return (0); 113 } 114 115 static void 116 am335x_pmic_start(void *xdev) 117 { 118 struct am335x_pmic_softc *sc; 119 device_t dev = (device_t)xdev; 120 uint8_t reg; 121 char name[20]; 122 char pwr[4][11] = {"Unknown", "USB", "AC", "USB and AC"}; 123 124 sc = device_get_softc(dev); 125 126 am335x_pmic_read(dev, TPS65217_CHIPID_REG, ®, 1); 127 switch (reg>>4) { 128 case TPS65217A: 129 sprintf(name, "TPS65217A ver 1.%u", reg & 0xF); 130 break; 131 case TPS65217B: 132 sprintf(name, "TPS65217B ver 1.%u", reg & 0xF); 133 break; 134 default: 135 sprintf(name, "Unknown PMIC"); 136 } 137 138 am335x_pmic_read(dev, TPS65217_STATUS_REG, ®, 1); 139 device_printf(dev, "%s powered by %s\n", name, pwr[(reg>>2)&0x03]); 140 141 config_intrhook_disestablish(&sc->enum_hook); 142 } 143 144 static int 145 am335x_pmic_attach(device_t dev) 146 { 147 struct am335x_pmic_softc *sc; 148 149 sc = device_get_softc(dev); 150 151 sc->enum_hook.ich_func = am335x_pmic_start; 152 sc->enum_hook.ich_arg = dev; 153 154 if (config_intrhook_establish(&sc->enum_hook) != 0) 155 return (ENOMEM); 156 157 return (0); 158 } 159 160 static device_method_t am335x_pmic_methods[] = { 161 DEVMETHOD(device_probe, am335x_pmic_probe), 162 DEVMETHOD(device_attach, am335x_pmic_attach), 163 {0, 0}, 164 }; 165 166 static driver_t am335x_pmic_driver = { 167 "am335x_pmic", 168 am335x_pmic_methods, 169 sizeof(struct am335x_pmic_softc), 170 }; 171 172 static devclass_t am335x_pmic_devclass; 173 174 DRIVER_MODULE(am335x_pmic, iicbus, am335x_pmic_driver, am335x_pmic_devclass, 0, 0); 175 MODULE_VERSION(am335x_pmic, 1); 176 MODULE_DEPEND(am335x_pmic, iicbus, 1, 1, 1); 177