1 /* $NetBSD: sy8106a.c,v 1.8 2021/01/27 02:29:48 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 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: sy8106a.c,v 1.8 2021/01/27 02:29:48 thorpej 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 VOUT1_SEL 0x01 45 #define SEL_GO __BIT(7) 46 #define SEL_VOLTAGE __BITS(6,0) 47 #define SEL_VOLTAGE_BASE 680000 /* uV */ 48 #define SEL_VOLTAGE_STEP 10000 /* uV */ 49 #define VOUT_COM 0x02 50 #define COM_DISABLE __BIT(0) 51 #define SYS_STATUS 0x06 52 53 struct sy8106a_softc { 54 device_t sc_dev; 55 i2c_tag_t sc_i2c; 56 i2c_addr_t sc_addr; 57 int sc_phandle; 58 59 u_int sc_ramp_delay; 60 }; 61 62 static const struct device_compatible_entry compat_data[] = { 63 { .compat = "silergy,sy8106a" }, 64 DEVICE_COMPAT_EOL 65 }; 66 67 static uint8_t 68 sy8106a_read(struct sy8106a_softc *sc, uint8_t reg, int flags) 69 { 70 uint8_t val = 0; 71 int error; 72 73 error = iic_exec(sc->sc_i2c, I2C_OP_READ_WITH_STOP, sc->sc_addr, 74 ®, 1, &val, 1, flags); 75 if (error != 0) 76 aprint_error_dev(sc->sc_dev, "error reading reg %#x: %d\n", reg, error); 77 78 return val; 79 } 80 81 static void 82 sy8106a_write(struct sy8106a_softc *sc, uint8_t reg, uint8_t val, int flags) 83 { 84 uint8_t buf[2]; 85 int error; 86 87 buf[0] = reg; 88 buf[1] = val; 89 90 error = iic_exec(sc->sc_i2c, I2C_OP_WRITE_WITH_STOP, sc->sc_addr, 91 NULL, 0, buf, 2, flags); 92 if (error != 0) 93 aprint_error_dev(sc->sc_dev, "error writing reg %#x: %d\n", reg, error); 94 } 95 96 #define I2C_READ(sc, reg) sy8106a_read((sc), (reg), 0) 97 #define I2C_WRITE(sc, reg, val) sy8106a_write((sc), (reg), (val), 0) 98 #define I2C_LOCK(sc) iic_acquire_bus((sc)->sc_i2c, 0) 99 #define I2C_UNLOCK(sc) iic_release_bus((sc)->sc_i2c, 0) 100 101 static int 102 sy8106a_acquire(device_t dev) 103 { 104 return 0; 105 } 106 107 static void 108 sy8106a_release(device_t dev) 109 { 110 } 111 112 static int 113 sy8106a_enable(device_t dev, bool enable) 114 { 115 struct sy8106a_softc * const sc = device_private(dev); 116 uint8_t val; 117 118 I2C_LOCK(sc); 119 val = I2C_READ(sc, VOUT_COM); 120 if (enable) 121 val &= ~COM_DISABLE; 122 else 123 val |= COM_DISABLE; 124 I2C_WRITE(sc, VOUT_COM, val); 125 I2C_UNLOCK(sc); 126 127 if (sc->sc_ramp_delay) 128 delay(sc->sc_ramp_delay); 129 130 return 0; 131 } 132 133 static int 134 sy8106a_set_voltage(device_t dev, u_int min_uvol, u_int max_uvol) 135 { 136 struct sy8106a_softc * const sc = device_private(dev); 137 uint8_t val, oval; 138 u_int cur_uvol; 139 140 I2C_LOCK(sc); 141 142 /* Get current voltage */ 143 oval = I2C_READ(sc, VOUT1_SEL); 144 cur_uvol = __SHIFTOUT(oval, SEL_VOLTAGE) * SEL_VOLTAGE_STEP + 145 SEL_VOLTAGE_BASE; 146 147 /* Set new voltage */ 148 val = SEL_GO | ((min_uvol - SEL_VOLTAGE_BASE) / SEL_VOLTAGE_STEP); 149 I2C_WRITE(sc, VOUT1_SEL, val); 150 151 I2C_UNLOCK(sc); 152 153 /* Time to delay is based on the number of voltage steps */ 154 if (sc->sc_ramp_delay) 155 delay((abs(cur_uvol - min_uvol) / SEL_VOLTAGE_STEP) * sc->sc_ramp_delay); 156 157 return 0; 158 } 159 160 static int 161 sy8106a_get_voltage(device_t dev, u_int *puvol) 162 { 163 struct sy8106a_softc * const sc = device_private(dev); 164 uint8_t val; 165 166 I2C_LOCK(sc); 167 val = I2C_READ(sc, VOUT1_SEL); 168 I2C_UNLOCK(sc); 169 170 *puvol = __SHIFTOUT(val, SEL_VOLTAGE) * SEL_VOLTAGE_STEP + 171 SEL_VOLTAGE_BASE; 172 173 return 0; 174 } 175 176 static struct fdtbus_regulator_controller_func sy8106a_funcs = { 177 .acquire = sy8106a_acquire, 178 .release = sy8106a_release, 179 .enable = sy8106a_enable, 180 .set_voltage = sy8106a_set_voltage, 181 .get_voltage = sy8106a_get_voltage, 182 }; 183 184 static int 185 sy8106a_match(device_t parent, cfdata_t match, void *aux) 186 { 187 struct i2c_attach_args *ia = aux; 188 int match_result; 189 190 if (iic_use_direct_match(ia, match, compat_data, &match_result)) 191 return match_result; 192 193 return 0; 194 } 195 196 static void 197 sy8106a_attach(device_t parent, device_t self, void *aux) 198 { 199 struct sy8106a_softc * const sc = device_private(self); 200 struct i2c_attach_args *ia = aux; 201 202 sc->sc_dev = self; 203 sc->sc_i2c = ia->ia_tag; 204 sc->sc_addr = ia->ia_addr; 205 sc->sc_phandle = ia->ia_cookie; 206 207 aprint_naive("\n"); 208 aprint_normal(": Silergy SY8106A regulator\n"); 209 210 of_getprop_uint32(sc->sc_phandle, "regulator-ramp-delay", 211 &sc->sc_ramp_delay); 212 213 fdtbus_register_regulator_controller(self, sc->sc_phandle, 214 &sy8106a_funcs); 215 } 216 217 CFATTACH_DECL_NEW(sy8106a, sizeof(struct sy8106a_softc), 218 sy8106a_match, sy8106a_attach, NULL, NULL); 219