1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org> 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/kernel.h> 34 #include <sys/module.h> 35 36 #include <dev/ofw/ofw_bus.h> 37 #include <dev/ofw/ofw_bus_subr.h> 38 39 #include <dev/extres/syscon/syscon.h> 40 #include <dev/extres/regulator/regulator.h> 41 42 #include "syscon_if.h" 43 44 #define RK3288_GRF_IO_VSEL 0x380 45 #define RK3399_GRF_IO_VSEL 0xe640 46 #define RK3399_PMUGRF_SOC_CON0 0x180 47 #define RK3568_PMUGRF_IO_VSEL0 0x0140 48 #define RK3568_PMUGRF_IO_VSEL1 0x0144 49 #define RK3568_PMUGRF_IO_VSEL2 0x0148 50 51 #define MAX_1V8 1850000 52 53 enum rk_iodomain_type { 54 RK3328 = 1, 55 RK3399, 56 RK3568, 57 }; 58 59 struct rk_iodomain_supply { 60 char *name; 61 uint32_t bit; 62 }; 63 64 struct rk_iodomain_softc; 65 66 struct rk_iodomain_conf { 67 struct rk_iodomain_supply *supply; 68 int nsupply; 69 uint32_t grf_reg; 70 void (*init)(struct rk_iodomain_softc *sc); 71 enum rk_iodomain_type type; 72 }; 73 74 struct rk_iodomain_softc { 75 device_t dev; 76 struct syscon *grf; 77 phandle_t node; 78 struct rk_iodomain_conf *conf; 79 }; 80 81 static struct rk_iodomain_supply rk3288_supply[] = { 82 {"lcdc-supply", 0}, 83 {"dvp-supply", 1}, 84 {"flash0-supply", 2}, 85 {"flash1-supply", 3}, 86 {"wifi-supply", 4}, 87 {"bb-supply", 5}, 88 {"audio-supply", 6}, 89 {"sdcard-supply", 7}, 90 {"gpio30-supply", 8}, 91 {"gpio1830-supply", 9}, 92 }; 93 94 static struct rk_iodomain_conf rk3288_conf = { 95 .supply = rk3288_supply, 96 .nsupply = nitems(rk3288_supply), 97 .grf_reg = RK3288_GRF_IO_VSEL, 98 .type = RK3328, 99 }; 100 101 static struct rk_iodomain_supply rk3399_supply[] = { 102 {"bt656-supply", 0}, 103 {"audio-supply", 1}, 104 {"sdmmc-supply", 2}, 105 {"gpio1830-supply", 3}, 106 }; 107 108 static struct rk_iodomain_conf rk3399_conf = { 109 .supply = rk3399_supply, 110 .nsupply = nitems(rk3399_supply), 111 .grf_reg = RK3399_GRF_IO_VSEL, 112 .type = RK3399, 113 }; 114 115 static struct rk_iodomain_supply rk3399_pmu_supply[] = { 116 {"pmu1830-supply", 9}, 117 }; 118 119 static void rk3399_pmu_init(struct rk_iodomain_softc *sc); 120 static struct rk_iodomain_conf rk3399_pmu_conf = { 121 .supply = rk3399_pmu_supply, 122 .nsupply = nitems(rk3399_pmu_supply), 123 .grf_reg = RK3399_PMUGRF_SOC_CON0, 124 .init = rk3399_pmu_init, 125 .type = RK3399, 126 }; 127 128 static struct rk_iodomain_supply rk3568_pmu_supply[] = { 129 {"pmuio1-supply", 0}, 130 {"pmuio2-supply", 1}, 131 {"vccio1-supply", 1}, 132 {"vccio2-supply", 2}, 133 {"vccio3-supply", 3}, 134 {"vccio4-supply", 4}, 135 {"vccio5-supply", 5}, 136 {"vccio6-supply", 6}, 137 {"vccio7-supply", 7}, 138 }; 139 static struct rk_iodomain_conf rk3568_pmu_conf = { 140 .supply = rk3568_pmu_supply, 141 .nsupply = nitems(rk3568_pmu_supply), 142 .type = RK3568, 143 }; 144 145 static struct ofw_compat_data compat_data[] = { 146 {"rockchip,rk3288-io-voltage-domain", (uintptr_t)&rk3288_conf}, 147 {"rockchip,rk3399-io-voltage-domain", (uintptr_t)&rk3399_conf}, 148 {"rockchip,rk3399-pmu-io-voltage-domain", (uintptr_t)&rk3399_pmu_conf}, 149 {"rockchip,rk3568-pmu-io-voltage-domain", (uintptr_t)&rk3568_pmu_conf}, 150 {NULL, 0} 151 }; 152 153 static void 154 rk3399_pmu_init(struct rk_iodomain_softc *sc) 155 { 156 157 SYSCON_WRITE_4(sc->grf, RK3399_PMUGRF_SOC_CON0, 158 (1 << 8) | (1 << (8 + 16))); /* set pmu1830_volsel */ 159 } 160 161 static int 162 rk_iodomain_set(struct rk_iodomain_softc *sc) 163 { 164 regulator_t supply; 165 uint32_t reg = 0; 166 uint32_t mask = 0; 167 int uvolt, i; 168 169 for (i = 0; i < sc->conf->nsupply; i++) { 170 if (regulator_get_by_ofw_property(sc->dev, sc->node, 171 sc->conf->supply[i].name, &supply) != 0) { 172 device_printf(sc->dev, 173 "Cannot get property for regulator %s\n", 174 sc->conf->supply[i].name); 175 return (ENXIO); 176 } 177 178 if (regulator_get_voltage(supply, &uvolt) != 0) { 179 device_printf(sc->dev, 180 "Cannot get current voltage for regulator %s\n", 181 sc->conf->supply[i].name); 182 return (ENXIO); 183 } 184 185 if (sc->conf->type != RK3568) { 186 /* RK3328 and RK3399 iodomain */ 187 mask |= (1 << sc->conf->supply[i].bit) << 16; 188 if (uvolt == 1800000) 189 reg |= (1 << sc->conf->supply[i].bit); 190 else if (uvolt != 3000000) 191 device_printf(sc->dev, 192 "%s regulator is at %duV, ignoring\n", 193 sc->conf->supply[i].name, uvolt); 194 } else { 195 /* RK3568 iodomain */ 196 if (bootverbose) { 197 device_printf(sc->dev, 198 "Setting regulator %s voltage=%duV\n", 199 sc->conf->supply[i].name, uvolt); 200 } 201 switch(i) { 202 case 0: /* pmuio1 */ 203 break; 204 case 1: /* pmuio2 */ 205 SYSCON_WRITE_4(sc->grf, RK3568_PMUGRF_IO_VSEL2, 206 (1 << (sc->conf->supply[i].bit + 16)) | 207 (uvolt > MAX_1V8 ? 208 0 : 1 << sc->conf->supply[i].bit)); 209 SYSCON_WRITE_4(sc->grf, RK3568_PMUGRF_IO_VSEL2, 210 (1 << (sc->conf->supply[i].bit + 4 + 16)) | 211 (uvolt > MAX_1V8 ? 212 1 << (sc->conf->supply[i].bit + 4) : 0)); 213 case 3: /* vccio2 */ 214 break; 215 case 2: /* vccio1 */ 216 case 4: /* vccio3 */ 217 case 5: /* vccio4 */ 218 case 6: /* vccio5 */ 219 case 7: /* vccio6 */ 220 case 8: /* vccio7 */ 221 SYSCON_WRITE_4(sc->grf, RK3568_PMUGRF_IO_VSEL0, 222 (1 << (sc->conf->supply[i].bit + 16)) | 223 (uvolt > MAX_1V8 ? 224 0 : 1 << sc->conf->supply[i].bit)); 225 SYSCON_WRITE_4(sc->grf, RK3568_PMUGRF_IO_VSEL1, 226 (1 << (sc->conf->supply[i].bit + 16)) | 227 (uvolt > MAX_1V8 ? 228 1 << sc->conf->supply[i].bit : 0)); 229 break; 230 default: 231 device_printf(sc->dev, "Index out of range\n"); 232 } 233 } 234 } 235 if (sc->conf->type != RK3568) 236 SYSCON_WRITE_4(sc->grf, sc->conf->grf_reg, reg | mask); 237 if (sc->conf->init != NULL) 238 sc->conf->init(sc); 239 240 return (0); 241 } 242 243 static int 244 rk_iodomain_probe(device_t dev) 245 { 246 247 if (!ofw_bus_status_okay(dev)) 248 return (ENXIO); 249 250 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 251 return (ENXIO); 252 253 device_set_desc(dev, "RockChip IO Voltage Domain"); 254 return (BUS_PROBE_DEFAULT); 255 } 256 257 static int 258 rk_iodomain_attach(device_t dev) 259 { 260 struct rk_iodomain_softc *sc; 261 int rv; 262 263 sc = device_get_softc(dev); 264 sc->dev = dev; 265 sc->node = ofw_bus_get_node(dev); 266 267 rv = syscon_get_handle_default(dev, &sc->grf); 268 if (rv != 0) { 269 device_printf(dev, "Cannot get grf handle\n"); 270 return (ENXIO); 271 } 272 273 sc->conf = (struct rk_iodomain_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data; 274 rv = rk_iodomain_set(sc); 275 276 return (rv); 277 } 278 279 static int 280 rk_iodomain_detach(device_t dev) 281 { 282 283 return (0); 284 } 285 286 static device_method_t rk_iodomain_methods[] = { 287 /* Device interface */ 288 DEVMETHOD(device_probe, rk_iodomain_probe), 289 DEVMETHOD(device_attach, rk_iodomain_attach), 290 DEVMETHOD(device_detach, rk_iodomain_detach), 291 292 DEVMETHOD_END 293 }; 294 295 static driver_t rk_iodomain_driver = { 296 "rk_iodomain", 297 rk_iodomain_methods, 298 sizeof(struct rk_iodomain_softc), 299 }; 300 301 EARLY_DRIVER_MODULE(rk_iodomain, simplebus, rk_iodomain_driver, 0, 0, 302 BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST); 303