1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca> 5 * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 6 * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/param.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/mutex.h> 35 #include <sys/rman.h> 36 #include <machine/bus.h> 37 38 #include <dev/iicbus/iiconf.h> 39 #include <dev/iicbus/iicbus.h> 40 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <dev/extres/regulator/regulator.h> 45 46 #include "regdev_if.h" 47 48 /* Registers */ 49 #define FAN53555_VSEL0 0x00 50 #define FAN53555_VSEL1 0x01 51 #define FAN53555_VSEL_ENA (1 << 7) 52 #define FAN53555_VSEL_MODE (1 << 6) 53 #define FAN53555_VSEL_MASK 0x3f 54 #define FAN53555_CTRL 0x02 55 #define FAN53555_ID1 0x03 56 #define FAN53555_ID1_DIE_ID(x) ((x) & 0x0F) 57 #define FAN53555_ID2 0x04 58 #define FAN53555_ID2_DIE_REV(x) ((x) & 0x0F) 59 #define FAN53555_MON 0x05 60 61 #define TCS4525_VSEL0 0x11 62 #define TCS4525_VSEL1 0x10 63 #define TCS4525_CHIP_ID_12 12 64 65 #if 0 66 #define dprintf(sc, format, arg...) \ 67 device_printf(sc->base_dev, "%s: " format, __func__, arg) 68 #else 69 #define dprintf(sc, format, arg...) 70 #endif 71 72 enum fan53555_pmic_type { 73 FAN53555 = 1, 74 SYR827, 75 SYR828, 76 TCS4525, 77 }; 78 79 static struct ofw_compat_data compat_data[] = { 80 {"fcs,fan53555", FAN53555}, 81 {"silergy,syr827", SYR827}, 82 {"silergy,syr828", SYR828}, 83 {"tcs,tcs4525", TCS4525}, 84 {NULL, 0} 85 }; 86 87 struct fan53555_reg_sc { 88 struct regnode *regnode; 89 char *name; 90 device_t base_dev; 91 uint8_t live_reg; 92 uint8_t sleep_reg; 93 struct regulator_range *range; 94 struct regnode_std_param *param; 95 }; 96 97 struct fan53555_softc { 98 device_t dev; 99 uint8_t live_reg; 100 uint8_t sleep_reg; 101 }; 102 103 static struct regulator_range syr_8_range = 104 REG_RANGE_INIT( 0, 0x3F, 712500, 12500); 105 106 static struct regulator_range fan_0_0_range = 107 REG_RANGE_INIT( 0, 0x3F, 600000, 10000); 108 static struct regulator_range fan_0_13_range = 109 REG_RANGE_INIT( 0, 0x3F, 800000, 10000); 110 static struct regulator_range fan_1_range = 111 REG_RANGE_INIT( 0, 0x3F, 600000, 10000); 112 static struct regulator_range fan_4_range = 113 REG_RANGE_INIT( 0, 0x3F, 603000, 12826); 114 115 static struct regulator_range tcs_12_range = 116 REG_RANGE_INIT( 0, 0x3F, 800000, 6250); 117 118 static int 119 fan53555_read(device_t dev, uint8_t reg, uint8_t *val) 120 { 121 uint8_t addr; 122 int rv; 123 struct iic_msg msgs[2] = { 124 {0, IIC_M_WR | IIC_M_NOSTOP, 1, &addr}, 125 {0, IIC_M_RD, 1, val}, 126 }; 127 128 msgs[0].slave = iicbus_get_addr(dev); 129 msgs[1].slave = iicbus_get_addr(dev); 130 addr = reg; 131 132 rv = iicbus_transfer_excl(dev, msgs, 2, IIC_INTRWAIT); 133 if (rv != 0) { 134 device_printf(dev, "Error when reading reg 0x%02X, rv: %d\n", 135 reg, rv); 136 return (EIO); 137 } 138 139 return (0); 140 } 141 142 static int 143 fan53555_write(device_t dev, uint8_t reg, uint8_t val) 144 { 145 uint8_t data[2]; 146 int rv; 147 148 struct iic_msg msgs[1] = { 149 {0, IIC_M_WR, 2, data}, 150 }; 151 152 msgs[0].slave = iicbus_get_addr(dev); 153 data[0] = reg; 154 data[1] = val; 155 156 rv = iicbus_transfer_excl(dev, msgs, 1, IIC_INTRWAIT); 157 if (rv != 0) { 158 device_printf(dev, 159 "Error when writing reg 0x%02X, rv: %d\n", reg, rv); 160 return (EIO); 161 } 162 return (0); 163 } 164 165 static int 166 fan53555_read_sel(struct fan53555_reg_sc *sc, uint8_t *sel) 167 { 168 int rv; 169 170 rv = fan53555_read(sc->base_dev, sc->live_reg, sel); 171 if (rv != 0) 172 return (rv); 173 *sel &= FAN53555_VSEL_MASK; 174 return (0); 175 } 176 177 static int 178 fan53555_write_sel(struct fan53555_reg_sc *sc, uint8_t sel) 179 { 180 int rv; 181 uint8_t reg; 182 183 rv = fan53555_read(sc->base_dev, sc->live_reg, ®); 184 if (rv != 0) 185 return (rv); 186 reg &= ~FAN53555_VSEL_MASK; 187 reg |= sel; 188 189 rv = fan53555_write(sc->base_dev, sc->live_reg, reg); 190 if (rv != 0) 191 return (rv); 192 return (rv); 193 } 194 195 static int 196 fan53555_regnode_init(struct regnode *regnode) 197 { 198 return (0); 199 } 200 201 static int 202 fan53555_regnode_enable(struct regnode *regnode, bool enable, int *udelay) 203 { 204 struct fan53555_reg_sc *sc; 205 uint8_t val; 206 207 sc = regnode_get_softc(regnode); 208 209 dprintf(sc, "%sabling regulator %s\n", enable ? "En" : "Dis", 210 sc->name); 211 fan53555_read(sc->base_dev, sc->live_reg, &val); 212 if (enable) 213 val |=FAN53555_VSEL_ENA; 214 else 215 val &= ~FAN53555_VSEL_ENA; 216 fan53555_write(sc->base_dev, sc->live_reg, val); 217 218 *udelay = sc->param->enable_delay; 219 return (0); 220 } 221 222 223 static int 224 fan53555_regnode_set_voltage(struct regnode *regnode, int min_uvolt, 225 int max_uvolt, int *udelay) 226 { 227 struct fan53555_reg_sc *sc; 228 uint8_t sel; 229 int uvolt, rv; 230 231 sc = regnode_get_softc(regnode); 232 233 dprintf(sc, "Setting %s to %d<->%d uvolts\n", sc->name, min_uvolt, 234 max_uvolt); 235 rv = regulator_range_volt_to_sel8(sc->range, 1, min_uvolt, max_uvolt, 236 &sel); 237 if (rv != 0) 238 return (rv); 239 *udelay = sc->param->ramp_delay; 240 rv = fan53555_write_sel(sc, sel); 241 dprintf(sc, "Regulator %s writing sel: 0x%02X\n", sc->name, sel); 242 243 fan53555_read_sel(sc, &sel); 244 regulator_range_sel8_to_volt(sc->range, 1, sel, &uvolt); 245 dprintf(sc, "Regulator %s set to %d uvolt (sel: 0x%02X)\n", sc->name, 246 uvolt, sel); 247 248 return (rv); 249 } 250 251 static int 252 fan53555_regnode_get_voltage(struct regnode *regnode, int *uvolt) 253 { 254 struct fan53555_reg_sc *sc; 255 uint8_t sel; 256 int rv; 257 258 sc = regnode_get_softc(regnode); 259 260 rv = fan53555_read_sel(sc, &sel); 261 if (rv != 0) 262 return (rv); 263 rv = regulator_range_sel8_to_volt(sc->range, 1, sel, uvolt); 264 dprintf(sc, "Regulator %s is at %d uvolt ((sel: 0x%02X)\n", sc->name, 265 *uvolt, sel); 266 267 return (rv); 268 } 269 270 static regnode_method_t fan53555_regnode_methods[] = { 271 /* Regulator interface */ 272 REGNODEMETHOD(regnode_init, fan53555_regnode_init), 273 REGNODEMETHOD(regnode_enable, fan53555_regnode_enable), 274 REGNODEMETHOD(regnode_set_voltage, fan53555_regnode_set_voltage), 275 REGNODEMETHOD(regnode_get_voltage, fan53555_regnode_get_voltage), 276 REGNODEMETHOD_END 277 }; 278 DEFINE_CLASS_1(fan53555_regnode, fan53555_regnode_class, 279 fan53555_regnode_methods, sizeof(struct fan53555_reg_sc), regnode_class); 280 281 static struct regulator_range * 282 fan53555_get_range(struct fan53555_softc *sc, int type, uint8_t id, 283 uint8_t rev) 284 { 285 if (type == SYR827 || type == SYR828) { 286 switch (id) { 287 case 8: 288 return (&syr_8_range); 289 default: 290 return (NULL); 291 } 292 } 293 294 if (type == FAN53555) { 295 switch (id) { 296 case 0: 297 if (rev == 0) 298 return (&fan_0_0_range); 299 else if (rev == 13) 300 return (&fan_0_13_range); 301 else 302 return (NULL); 303 case 1: 304 case 3: 305 case 5: 306 case 8: 307 return (&fan_1_range); 308 case 4: 309 return (&fan_4_range); 310 default: 311 return (NULL); 312 } 313 } 314 315 if (type == TCS4525) { 316 switch (id) { 317 case TCS4525_CHIP_ID_12: 318 return (&tcs_12_range); 319 default: 320 return (NULL); 321 } 322 } 323 324 return (NULL); 325 } 326 327 static struct fan53555_reg_sc * 328 fan53555_reg_attach(struct fan53555_softc *sc, phandle_t node, int type) 329 { 330 struct fan53555_reg_sc *reg_sc; 331 struct regnode_init_def initdef; 332 struct regnode *regnode; 333 static struct regulator_range *range; 334 uint8_t id1, id2; 335 336 memset(&initdef, 0, sizeof(initdef)); 337 if (regulator_parse_ofw_stdparam(sc->dev, node, &initdef) != 0) { 338 device_printf(sc->dev, "cannot parse regulator FDT data\n"); 339 return (NULL); 340 } 341 342 if (fan53555_read(sc->dev, FAN53555_ID1, &id1) != 0) { 343 device_printf(sc->dev, "cannot read ID1\n"); 344 return (NULL); 345 } 346 347 if (fan53555_read(sc->dev, FAN53555_ID2, &id2) != 0) { 348 device_printf(sc->dev, "cannot read ID2\n"); 349 return (NULL); 350 } 351 dprintf(sc, "Device ID1: 0x%02X, ID2: 0x%02X\n", id1, id2); 352 353 range = fan53555_get_range(sc, type, FAN53555_ID1_DIE_ID(id1), 354 FAN53555_ID2_DIE_REV(id2)); 355 if (range == NULL) { 356 device_printf(sc->dev, 357 "cannot determine chip type (ID1: 0x%02X, ID2: 0x%02X)\n", 358 id1, id2); 359 return (NULL); 360 } 361 362 initdef.id = 1; 363 initdef.ofw_node = node; 364 365 regnode = regnode_create(sc->dev, &fan53555_regnode_class, &initdef); 366 if (regnode == NULL) { 367 device_printf(sc->dev, "cannot create regulator\n"); 368 return (NULL); 369 } 370 371 reg_sc = regnode_get_softc(regnode); 372 reg_sc->name = "fan53555"; 373 reg_sc->regnode = regnode; 374 reg_sc->base_dev = sc->dev; 375 reg_sc->param = regnode_get_stdparam(regnode); 376 reg_sc->range = range; 377 reg_sc->live_reg = sc->live_reg; 378 reg_sc->sleep_reg = sc->sleep_reg; 379 380 dprintf(sc->dev, "live_reg: %d, sleep_reg: %d\n", reg_sc->live_reg, 381 reg_sc->sleep_reg); 382 383 regnode_register(regnode); 384 385 if (bootverbose) { 386 int volt, rv; 387 regnode_topo_slock(); 388 rv = regnode_get_voltage(regnode, &volt); 389 if (rv == ENODEV) { 390 device_printf(sc->dev, 391 " Regulator %s: parent doesn't exist yet.\n", 392 regnode_get_name(regnode)); 393 } else if (rv != 0) { 394 device_printf(sc->dev, 395 " Regulator %s: voltage: INVALID!!!\n", 396 regnode_get_name(regnode)); 397 } else { 398 device_printf(sc->dev, 399 " Regulator %s: voltage: %d uV\n", 400 regnode_get_name(regnode), volt); 401 } 402 regnode_topo_unlock(); 403 } 404 405 return (reg_sc); 406 } 407 408 static int 409 fan53555_probe(device_t dev) 410 { 411 int type; 412 413 if (!ofw_bus_status_okay(dev)) 414 return (ENXIO); 415 416 type = ofw_bus_search_compatible(dev, compat_data)->ocd_data; 417 switch (type) { 418 case FAN53555: 419 device_set_desc(dev, "FAN53555 PMIC"); 420 break; 421 case SYR827: 422 device_set_desc(dev, "SYR827 PMIC"); 423 break; 424 case SYR828: 425 device_set_desc(dev, "SYR828 PMIC"); 426 break; 427 case TCS4525: 428 device_set_desc(dev, "TCS4525 PMIC"); 429 break; 430 default: 431 return (ENXIO); 432 } 433 434 return (BUS_PROBE_DEFAULT); 435 } 436 437 static int 438 fan53555_attach(device_t dev) 439 { 440 struct fan53555_softc *sc; 441 phandle_t node; 442 int type, susp_sel, rv; 443 444 sc = device_get_softc(dev); 445 sc->dev = dev; 446 node = ofw_bus_get_node(dev); 447 type = ofw_bus_search_compatible(dev, compat_data)->ocd_data; 448 449 rv = OF_getencprop(node, "fcs,suspend-voltage-selector", &susp_sel, 450 sizeof(susp_sel)); 451 if (rv <= 0) 452 susp_sel = 1; 453 454 switch (type) { 455 case FAN53555: 456 case SYR827: 457 case SYR828: 458 if (susp_sel == 1) { 459 sc->live_reg = FAN53555_VSEL0; 460 sc->sleep_reg = FAN53555_VSEL1; 461 } else { 462 sc->live_reg = FAN53555_VSEL1; 463 sc->sleep_reg = FAN53555_VSEL0; 464 } 465 break; 466 case TCS4525: 467 if (susp_sel == 1) { 468 sc->live_reg = TCS4525_VSEL0; 469 sc->sleep_reg = TCS4525_VSEL1; 470 } else { 471 sc->live_reg = TCS4525_VSEL1; 472 sc->sleep_reg = TCS4525_VSEL0; 473 } 474 break; 475 default: 476 return (ENXIO); 477 } 478 if (fan53555_reg_attach(sc, node, type) == NULL) 479 device_printf(dev, "cannot attach regulator.\n"); 480 481 return (0); 482 } 483 484 static int 485 fan53555_detach(device_t dev) 486 { 487 488 /* We cannot detach regulators */ 489 return (EBUSY); 490 } 491 492 static device_method_t fan53555_methods[] = { 493 DEVMETHOD(device_probe, fan53555_probe), 494 DEVMETHOD(device_attach, fan53555_attach), 495 DEVMETHOD(device_detach, fan53555_detach), 496 497 /* Regdev interface */ 498 DEVMETHOD(regdev_map, regdev_default_ofw_map), 499 500 DEVMETHOD_END 501 }; 502 503 static DEFINE_CLASS_0(fan53555_pmic, fan53555_driver, fan53555_methods, 504 sizeof(struct fan53555_softc)); 505 506 EARLY_DRIVER_MODULE(fan53555, iicbus, fan53555_driver, 0, 0, BUS_PASS_RESOURCE); 507 MODULE_VERSION(fan53555, 1); 508 MODULE_DEPEND(fan53555, iicbus, IICBUS_MINVER, IICBUS_PREFVER, IICBUS_MAXVER); 509 IICBUS_FDT_PNP_INFO(compat_data); 510