1 /* $NetBSD: fan53555.c,v 1.2 2018/08/29 11:08:30 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: fan53555.c,v 1.2 2018/08/29 11:08:30 jmcneill Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/device.h> 36 #include <sys/conf.h> 37 #include <sys/bus.h> 38 #include <sys/kmem.h> 39 40 #include <dev/i2c/i2cvar.h> 41 42 #include <dev/fdt/fdtvar.h> 43 44 #define VSEL0_REG 0x00 45 #define VSEL1_REG 0x01 46 #define VSEL_BUCK_EN __BIT(7) 47 #define VSEL_MODE __BIT(6) 48 #define VSEL_NSEL __BITS(5,0) 49 #define CONTROL_REG 0x02 50 #define CONTROL_OUTPUT_DISCHARGE __BIT(7) 51 #define CONTROL_SLEW __BITS(6,4) 52 #define CONTROL_RESET __BIT(2) 53 #define ID1_REG 0x03 54 #define ID1_VENDOR __BITS(7,5) 55 #define ID1_DIE_ID __BITS(3,0) 56 #define SILERGY_DIE_ID_SYR82X 8 57 #define ID2_REG 0x04 58 #define ID2_DIE_REV __BITS(3,0) 59 #define MONITOR_REG 0x05 60 #define MONITOR_PGOOD __BIT(7) 61 62 struct fan53555_softc { 63 device_t sc_dev; 64 i2c_tag_t sc_i2c; 65 i2c_addr_t sc_addr; 66 int sc_phandle; 67 68 u_int sc_suspend_reg; 69 u_int sc_runtime_reg; 70 71 u_int sc_base; 72 u_int sc_step; 73 74 u_int sc_ramp_delay; 75 u_int sc_suspend_voltage_selector; 76 }; 77 78 enum fan53555_vendor { 79 FAN_VENDOR_FAIRCHILD = 1, 80 FAN_VENDOR_SILERGY, 81 }; 82 83 /* 84 * Transition slew rates. 85 * Array index is reg value, value is slew rate in uV / us 86 */ 87 static const int fan53555_slew_rates[] = { 88 64000, 32000, 16000, 8000, 4000, 2000, 1000, 500 89 }; 90 91 static const struct device_compatible_entry compat_data[] = { 92 { "silergy,syr827", FAN_VENDOR_SILERGY }, 93 { "silergy,syr828", FAN_VENDOR_SILERGY }, 94 { NULL, 0 } 95 }; 96 97 static uint8_t 98 fan53555_read(struct fan53555_softc *sc, uint8_t reg, int flags) 99 { 100 uint8_t val = 0; 101 int error; 102 103 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, 104 ®, 1, &val, 1, flags); 105 if (error != 0) 106 aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error); 107 108 return val; 109 } 110 111 static void 112 fan53555_write(struct fan53555_softc *sc, uint8_t reg, uint8_t val, int flags) 113 { 114 uint8_t buf[2]; 115 int error; 116 117 buf[0] = reg; 118 buf[1] = val; 119 120 error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 121 NULL, 0, buf, 2, flags); 122 if (error != 0) 123 aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error); 124 } 125 126 #define I2C_READ(sc, reg) fan53555_read((sc), (reg), I2C_F_POLL) 127 #define I2C_WRITE(sc, reg, val) fan53555_write((sc), (reg), (val), I2C_F_POLL) 128 #define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, I2C_F_POLL) 129 #define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, I2C_F_POLL) 130 131 static int 132 fan53555_acquire(device_t dev) 133 { 134 return 0; 135 } 136 137 static void 138 fan53555_release(device_t dev) 139 { 140 } 141 142 static int 143 fan53555_enable(device_t dev, bool enable) 144 { 145 struct fan53555_softc * const sc = device_private(dev); 146 uint8_t val; 147 148 I2C_LOCK(sc); 149 val = I2C_READ(sc, sc->sc_runtime_reg); 150 if (enable) 151 val |= VSEL_BUCK_EN; 152 else 153 val &= ~VSEL_BUCK_EN; 154 I2C_WRITE(sc, sc->sc_runtime_reg, val); 155 I2C_UNLOCK(sc); 156 157 if (sc->sc_ramp_delay) 158 delay(sc->sc_ramp_delay); 159 160 return 0; 161 } 162 163 static int 164 fan53555_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol) 165 { 166 struct fan53555_softc * const sc = device_private(dev); 167 uint8_t val, oval; 168 u_int cur_uvol; 169 170 I2C_LOCK(sc); 171 172 /* Get current voltage */ 173 oval = I2C_READ(sc, sc->sc_runtime_reg); 174 cur_uvol = __SHIFTOUT(oval, VSEL_NSEL) * sc->sc_step + sc->sc_base; 175 176 /* Set new voltage */ 177 val = oval & ~VSEL_NSEL; 178 val |= __SHIFTIN((min_uvol - sc->sc_base) / sc->sc_step, VSEL_NSEL); 179 I2C_WRITE(sc, sc->sc_runtime_reg, val); 180 181 I2C_UNLOCK(sc); 182 183 /* Time to delay is based on the number of voltage steps */ 184 if (sc->sc_ramp_delay) 185 delay((abs(cur_uvol - min_uvol) / sc->sc_step) * sc->sc_ramp_delay); 186 187 return 0; 188 } 189 190 static int 191 fan53555_get_voltage(device_t dev, u_int *puvol) 192 { 193 struct fan53555_softc * const sc = device_private(dev); 194 uint8_t val; 195 196 I2C_LOCK(sc); 197 val = I2C_READ(sc, sc->sc_runtime_reg); 198 I2C_UNLOCK(sc); 199 200 *puvol = __SHIFTOUT(val, VSEL_NSEL) * sc->sc_step + sc->sc_base; 201 202 return 0; 203 } 204 205 static struct fdtbus_regulator_controller_func fan53555_funcs = { 206 .acquire = fan53555_acquire, 207 .release = fan53555_release, 208 .enable = fan53555_enable, 209 .set_voltage = fan53555_set_voltage, 210 .get_voltage = fan53555_get_voltage, 211 }; 212 213 static int 214 fan53555_init(struct fan53555_softc *sc, enum fan53555_vendor vendor) 215 { 216 uint8_t id1, control; 217 int n; 218 219 I2C_LOCK(sc); 220 id1 = I2C_READ(sc, ID1_REG); 221 I2C_UNLOCK(sc); 222 223 const u_int die_id = __SHIFTOUT(id1, ID1_DIE_ID); 224 225 aprint_naive("\n"); 226 switch (vendor) { 227 case FAN_VENDOR_SILERGY: 228 switch (die_id) { 229 case SILERGY_DIE_ID_SYR82X: 230 aprint_normal(": Silergy SYR82X\n"); 231 sc->sc_base = 712500; 232 sc->sc_step = 12500; 233 break; 234 default: 235 aprint_error(": Unsupported Silergy chip (0x%x)\n", die_id); 236 return ENXIO; 237 } 238 break; 239 default: 240 aprint_error(": Unsupported vendor (%d)\n", vendor); 241 return ENXIO; 242 } 243 244 of_getprop_uint32(sc->sc_phandle, "suspend_voltage_selector", 245 &sc->sc_suspend_voltage_selector); 246 switch (sc->sc_suspend_voltage_selector) { 247 case 0: 248 sc->sc_suspend_reg = VSEL0_REG; 249 sc->sc_runtime_reg = VSEL1_REG; 250 break; 251 case 1: 252 sc->sc_suspend_reg = VSEL1_REG; 253 sc->sc_runtime_reg = VSEL0_REG; 254 break; 255 default: 256 aprint_error_dev(sc->sc_dev, "unsupported 'fcs,suspend-voltage-selector' value %u\n", sc->sc_suspend_voltage_selector); 257 return EINVAL; 258 } 259 260 of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay", 261 &sc->sc_ramp_delay); 262 if (sc->sc_ramp_delay) { 263 for (n = 0; n < __arraycount(fan53555_slew_rates); n++) 264 if (sc->sc_ramp_delay > fan53555_slew_rates[n]) 265 break; 266 if (n == __arraycount(fan53555_slew_rates)) { 267 aprint_error_dev(sc->sc_dev, "unsupported 'regulator-ramp-delay' value %u\n", sc->sc_ramp_delay); 268 return EINVAL; 269 } 270 I2C_LOCK(sc); 271 control = I2C_READ(sc, CONTROL_REG); 272 control &= ~CONTROL_SLEW; 273 control |= __SHIFTIN(fan53555_slew_rates[n], CONTROL_SLEW); 274 I2C_WRITE(sc, CONTROL_REG, control); 275 I2C_UNLOCK(sc); 276 } 277 278 return 0; 279 } 280 281 static int 282 fan53555_match(device_t parent, cfdata_t match, void *aux) 283 { 284 struct i2c_attach_args *ia = aux; 285 int match_result; 286 287 if (iic_use_direct_match(ia, match, compat_data, &match_result)) 288 return match_result; 289 290 return 0; 291 } 292 293 static void 294 fan53555_attach(device_t parent, device_t self, void *aux) 295 { 296 struct fan53555_softc * const sc = device_private(self); 297 const struct device_compatible_entry *compat; 298 struct i2c_attach_args *ia = aux; 299 300 sc->sc_dev = self; 301 sc->sc_i2c = ia->ia_tag; 302 sc->sc_addr = ia->ia_addr; 303 sc->sc_phandle = ia->ia_cookie; 304 305 iic_compatible_match(ia, compat_data, &compat); 306 307 if (fan53555_init(sc, compat->data) != 0) 308 return; 309 310 fdtbus_register_regulator_controller(self, sc->sc_phandle, 311 &fan53555_funcs); 312 } 313 314 CFATTACH_DECL_NEW(fan53555reg, sizeof(struct fan53555_softc), 315 fan53555_match, fan53555_attach, NULL, NULL); 316