1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // FAN53555 Fairchild Digitally Programmable TinyBuck Regulator Driver. 4 // 5 // Supported Part Numbers: 6 // FAN53555UC00X/01X/03X/04X/05X 7 // 8 // Copyright (c) 2012 Marvell Technology Ltd. 9 // Yunfan Zhang <yfzhang@marvell.com> 10 11 #include <linux/module.h> 12 #include <linux/param.h> 13 #include <linux/err.h> 14 #include <linux/platform_device.h> 15 #include <linux/regulator/driver.h> 16 #include <linux/regulator/machine.h> 17 #include <linux/regulator/of_regulator.h> 18 #include <linux/of_device.h> 19 #include <linux/i2c.h> 20 #include <linux/slab.h> 21 #include <linux/regmap.h> 22 #include <linux/regulator/fan53555.h> 23 24 /* Voltage setting */ 25 #define FAN53555_VSEL0 0x00 26 #define FAN53555_VSEL1 0x01 27 28 #define TCS4525_VSEL0 0x11 29 #define TCS4525_VSEL1 0x10 30 #define TCS4525_TIME 0x13 31 #define TCS4525_COMMAND 0x14 32 33 /* Control register */ 34 #define FAN53555_CONTROL 0x02 35 /* IC Type */ 36 #define FAN53555_ID1 0x03 37 /* IC mask version */ 38 #define FAN53555_ID2 0x04 39 /* Monitor register */ 40 #define FAN53555_MONITOR 0x05 41 42 /* VSEL bit definitions */ 43 #define VSEL_BUCK_EN (1 << 7) 44 #define VSEL_MODE (1 << 6) 45 /* Chip ID and Verison */ 46 #define DIE_ID 0x0F /* ID1 */ 47 #define DIE_REV 0x0F /* ID2 */ 48 /* Control bit definitions */ 49 #define CTL_OUTPUT_DISCHG (1 << 7) 50 #define CTL_SLEW_MASK (0x7 << 4) 51 #define CTL_SLEW_SHIFT 4 52 #define CTL_RESET (1 << 2) 53 #define CTL_MODE_VSEL0_MODE BIT(0) 54 #define CTL_MODE_VSEL1_MODE BIT(1) 55 56 #define FAN53555_NVOLTAGES 64 /* Numbers of voltages */ 57 #define FAN53526_NVOLTAGES 128 58 59 #define TCS_VSEL_NSEL_MASK 0x7f 60 #define TCS_VSEL0_MODE (1 << 7) 61 #define TCS_VSEL1_MODE (1 << 6) 62 63 #define TCS_SLEW_SHIFT 3 64 #define TCS_SLEW_MASK (0x3 < 3) 65 66 enum fan53555_vendor { 67 FAN53526_VENDOR_FAIRCHILD = 0, 68 FAN53555_VENDOR_FAIRCHILD, 69 FAN53555_VENDOR_SILERGY, 70 FAN53555_VENDOR_TCS, 71 }; 72 73 enum { 74 FAN53526_CHIP_ID_01 = 1, 75 }; 76 77 enum { 78 FAN53526_CHIP_REV_08 = 8, 79 }; 80 81 /* IC Type */ 82 enum { 83 FAN53555_CHIP_ID_00 = 0, 84 FAN53555_CHIP_ID_01, 85 FAN53555_CHIP_ID_02, 86 FAN53555_CHIP_ID_03, 87 FAN53555_CHIP_ID_04, 88 FAN53555_CHIP_ID_05, 89 FAN53555_CHIP_ID_08 = 8, 90 }; 91 92 /* IC mask revision */ 93 enum { 94 FAN53555_CHIP_REV_00 = 0x3, 95 FAN53555_CHIP_REV_13 = 0xf, 96 }; 97 98 enum { 99 SILERGY_SYR82X = 8, 100 SILERGY_SYR83X = 9, 101 }; 102 103 struct fan53555_device_info { 104 enum fan53555_vendor vendor; 105 struct device *dev; 106 struct regulator_desc desc; 107 struct regulator_init_data *regulator; 108 /* IC Type and Rev */ 109 int chip_id; 110 int chip_rev; 111 /* Voltage setting register */ 112 unsigned int vol_reg; 113 unsigned int sleep_reg; 114 /* Voltage range and step(linear) */ 115 unsigned int vsel_min; 116 unsigned int vsel_step; 117 unsigned int vsel_count; 118 /* Mode */ 119 unsigned int mode_reg; 120 unsigned int mode_mask; 121 /* Sleep voltage cache */ 122 unsigned int sleep_vol_cache; 123 /* Slew rate */ 124 unsigned int slew_reg; 125 unsigned int slew_mask; 126 unsigned int slew_shift; 127 unsigned int slew_rate; 128 }; 129 130 static int fan53555_set_suspend_voltage(struct regulator_dev *rdev, int uV) 131 { 132 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 133 int ret; 134 135 if (di->sleep_vol_cache == uV) 136 return 0; 137 ret = regulator_map_voltage_linear(rdev, uV, uV); 138 if (ret < 0) 139 return ret; 140 ret = regmap_update_bits(rdev->regmap, di->sleep_reg, 141 di->desc.vsel_mask, ret); 142 if (ret < 0) 143 return ret; 144 /* Cache the sleep voltage setting. 145 * Might not be the real voltage which is rounded */ 146 di->sleep_vol_cache = uV; 147 148 return 0; 149 } 150 151 static int fan53555_set_suspend_enable(struct regulator_dev *rdev) 152 { 153 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 154 155 return regmap_update_bits(rdev->regmap, di->sleep_reg, 156 VSEL_BUCK_EN, VSEL_BUCK_EN); 157 } 158 159 static int fan53555_set_suspend_disable(struct regulator_dev *rdev) 160 { 161 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 162 163 return regmap_update_bits(rdev->regmap, di->sleep_reg, 164 VSEL_BUCK_EN, 0); 165 } 166 167 static int fan53555_set_mode(struct regulator_dev *rdev, unsigned int mode) 168 { 169 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 170 171 switch (mode) { 172 case REGULATOR_MODE_FAST: 173 regmap_update_bits(rdev->regmap, di->mode_reg, 174 di->mode_mask, di->mode_mask); 175 break; 176 case REGULATOR_MODE_NORMAL: 177 regmap_update_bits(rdev->regmap, di->vol_reg, di->mode_mask, 0); 178 break; 179 default: 180 return -EINVAL; 181 } 182 return 0; 183 } 184 185 static unsigned int fan53555_get_mode(struct regulator_dev *rdev) 186 { 187 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 188 unsigned int val; 189 int ret = 0; 190 191 ret = regmap_read(rdev->regmap, di->mode_reg, &val); 192 if (ret < 0) 193 return ret; 194 if (val & di->mode_mask) 195 return REGULATOR_MODE_FAST; 196 else 197 return REGULATOR_MODE_NORMAL; 198 } 199 200 static const int slew_rates[] = { 201 64000, 202 32000, 203 16000, 204 8000, 205 4000, 206 2000, 207 1000, 208 500, 209 }; 210 211 static const int tcs_slew_rates[] = { 212 18700, 213 9300, 214 4600, 215 2300, 216 }; 217 218 static int fan53555_set_ramp(struct regulator_dev *rdev, int ramp) 219 { 220 struct fan53555_device_info *di = rdev_get_drvdata(rdev); 221 int regval = -1, i; 222 const int *slew_rate_t; 223 int slew_rate_n; 224 225 switch (di->vendor) { 226 case FAN53526_VENDOR_FAIRCHILD: 227 case FAN53555_VENDOR_FAIRCHILD: 228 case FAN53555_VENDOR_SILERGY: 229 slew_rate_t = slew_rates; 230 slew_rate_n = ARRAY_SIZE(slew_rates); 231 break; 232 case FAN53555_VENDOR_TCS: 233 slew_rate_t = tcs_slew_rates; 234 slew_rate_n = ARRAY_SIZE(tcs_slew_rates); 235 break; 236 default: 237 return -EINVAL; 238 } 239 240 for (i = 0; i < slew_rate_n; i++) { 241 if (ramp <= slew_rate_t[i]) 242 regval = i; 243 else 244 break; 245 } 246 247 if (regval < 0) { 248 dev_err(di->dev, "unsupported ramp value %d\n", ramp); 249 return -EINVAL; 250 } 251 252 return regmap_update_bits(rdev->regmap, di->slew_reg, 253 di->slew_mask, regval << di->slew_shift); 254 } 255 256 static const struct regulator_ops fan53555_regulator_ops = { 257 .set_voltage_sel = regulator_set_voltage_sel_regmap, 258 .get_voltage_sel = regulator_get_voltage_sel_regmap, 259 .set_voltage_time_sel = regulator_set_voltage_time_sel, 260 .map_voltage = regulator_map_voltage_linear, 261 .list_voltage = regulator_list_voltage_linear, 262 .set_suspend_voltage = fan53555_set_suspend_voltage, 263 .enable = regulator_enable_regmap, 264 .disable = regulator_disable_regmap, 265 .is_enabled = regulator_is_enabled_regmap, 266 .set_mode = fan53555_set_mode, 267 .get_mode = fan53555_get_mode, 268 .set_ramp_delay = fan53555_set_ramp, 269 .set_suspend_enable = fan53555_set_suspend_enable, 270 .set_suspend_disable = fan53555_set_suspend_disable, 271 }; 272 273 static int fan53526_voltages_setup_fairchild(struct fan53555_device_info *di) 274 { 275 /* Init voltage range and step */ 276 switch (di->chip_id) { 277 case FAN53526_CHIP_ID_01: 278 switch (di->chip_rev) { 279 case FAN53526_CHIP_REV_08: 280 di->vsel_min = 600000; 281 di->vsel_step = 6250; 282 break; 283 default: 284 dev_err(di->dev, 285 "Chip ID %d with rev %d not supported!\n", 286 di->chip_id, di->chip_rev); 287 return -EINVAL; 288 } 289 break; 290 default: 291 dev_err(di->dev, 292 "Chip ID %d not supported!\n", di->chip_id); 293 return -EINVAL; 294 } 295 296 di->vsel_count = FAN53526_NVOLTAGES; 297 298 return 0; 299 } 300 301 static int fan53555_voltages_setup_fairchild(struct fan53555_device_info *di) 302 { 303 /* Init voltage range and step */ 304 switch (di->chip_id) { 305 case FAN53555_CHIP_ID_00: 306 switch (di->chip_rev) { 307 case FAN53555_CHIP_REV_00: 308 di->vsel_min = 600000; 309 di->vsel_step = 10000; 310 break; 311 case FAN53555_CHIP_REV_13: 312 di->vsel_min = 800000; 313 di->vsel_step = 10000; 314 break; 315 default: 316 dev_err(di->dev, 317 "Chip ID %d with rev %d not supported!\n", 318 di->chip_id, di->chip_rev); 319 return -EINVAL; 320 } 321 break; 322 case FAN53555_CHIP_ID_01: 323 case FAN53555_CHIP_ID_03: 324 case FAN53555_CHIP_ID_05: 325 case FAN53555_CHIP_ID_08: 326 di->vsel_min = 600000; 327 di->vsel_step = 10000; 328 break; 329 case FAN53555_CHIP_ID_04: 330 di->vsel_min = 603000; 331 di->vsel_step = 12826; 332 break; 333 default: 334 dev_err(di->dev, 335 "Chip ID %d not supported!\n", di->chip_id); 336 return -EINVAL; 337 } 338 di->slew_reg = FAN53555_CONTROL; 339 di->slew_mask = CTL_SLEW_MASK; 340 di->slew_shift = CTL_SLEW_SHIFT; 341 di->vsel_count = FAN53555_NVOLTAGES; 342 343 return 0; 344 } 345 346 static int fan53555_voltages_setup_silergy(struct fan53555_device_info *di) 347 { 348 /* Init voltage range and step */ 349 switch (di->chip_id) { 350 case SILERGY_SYR82X: 351 case SILERGY_SYR83X: 352 di->vsel_min = 712500; 353 di->vsel_step = 12500; 354 break; 355 default: 356 dev_err(di->dev, 357 "Chip ID %d not supported!\n", di->chip_id); 358 return -EINVAL; 359 } 360 di->slew_reg = FAN53555_CONTROL; 361 di->slew_reg = FAN53555_CONTROL; 362 di->slew_mask = CTL_SLEW_MASK; 363 di->slew_shift = CTL_SLEW_SHIFT; 364 di->vsel_count = FAN53555_NVOLTAGES; 365 366 return 0; 367 } 368 369 static int fan53555_voltages_setup_tcs(struct fan53555_device_info *di) 370 { 371 di->slew_reg = TCS4525_TIME; 372 di->slew_mask = TCS_SLEW_MASK; 373 di->slew_shift = TCS_SLEW_MASK; 374 375 /* Init voltage range and step */ 376 di->vsel_min = 600000; 377 di->vsel_step = 6250; 378 di->vsel_count = FAN53526_NVOLTAGES; 379 380 return 0; 381 } 382 383 /* For 00,01,03,05 options: 384 * VOUT = 0.60V + NSELx * 10mV, from 0.60 to 1.23V. 385 * For 04 option: 386 * VOUT = 0.603V + NSELx * 12.826mV, from 0.603 to 1.411V. 387 * */ 388 static int fan53555_device_setup(struct fan53555_device_info *di, 389 struct fan53555_platform_data *pdata) 390 { 391 int ret = 0; 392 393 /* Setup voltage control register */ 394 switch (di->vendor) { 395 case FAN53526_VENDOR_FAIRCHILD: 396 case FAN53555_VENDOR_FAIRCHILD: 397 case FAN53555_VENDOR_SILERGY: 398 switch (pdata->sleep_vsel_id) { 399 case FAN53555_VSEL_ID_0: 400 di->sleep_reg = FAN53555_VSEL0; 401 di->vol_reg = FAN53555_VSEL1; 402 break; 403 case FAN53555_VSEL_ID_1: 404 di->sleep_reg = FAN53555_VSEL1; 405 di->vol_reg = FAN53555_VSEL0; 406 break; 407 default: 408 dev_err(di->dev, "Invalid VSEL ID!\n"); 409 return -EINVAL; 410 } 411 break; 412 case FAN53555_VENDOR_TCS: 413 switch (pdata->sleep_vsel_id) { 414 case FAN53555_VSEL_ID_0: 415 di->sleep_reg = TCS4525_VSEL0; 416 di->vol_reg = TCS4525_VSEL1; 417 break; 418 case FAN53555_VSEL_ID_1: 419 di->sleep_reg = TCS4525_VSEL1; 420 di->vol_reg = TCS4525_VSEL0; 421 break; 422 default: 423 dev_err(di->dev, "Invalid VSEL ID!\n"); 424 return -EINVAL; 425 } 426 break; 427 default: 428 dev_err(di->dev, "vendor %d not supported!\n", di->vendor); 429 return -EINVAL; 430 } 431 432 /* Setup mode control register */ 433 switch (di->vendor) { 434 case FAN53526_VENDOR_FAIRCHILD: 435 di->mode_reg = FAN53555_CONTROL; 436 437 switch (pdata->sleep_vsel_id) { 438 case FAN53555_VSEL_ID_0: 439 di->mode_mask = CTL_MODE_VSEL1_MODE; 440 break; 441 case FAN53555_VSEL_ID_1: 442 di->mode_mask = CTL_MODE_VSEL0_MODE; 443 break; 444 } 445 break; 446 case FAN53555_VENDOR_FAIRCHILD: 447 case FAN53555_VENDOR_SILERGY: 448 di->mode_reg = di->vol_reg; 449 di->mode_mask = VSEL_MODE; 450 break; 451 case FAN53555_VENDOR_TCS: 452 di->mode_reg = TCS4525_COMMAND; 453 454 switch (pdata->sleep_vsel_id) { 455 case FAN53555_VSEL_ID_0: 456 di->mode_mask = TCS_VSEL1_MODE; 457 break; 458 case FAN53555_VSEL_ID_1: 459 di->mode_mask = TCS_VSEL0_MODE; 460 break; 461 } 462 break; 463 default: 464 dev_err(di->dev, "vendor %d not supported!\n", di->vendor); 465 return -EINVAL; 466 } 467 468 /* Setup voltage range */ 469 switch (di->vendor) { 470 case FAN53526_VENDOR_FAIRCHILD: 471 ret = fan53526_voltages_setup_fairchild(di); 472 break; 473 case FAN53555_VENDOR_FAIRCHILD: 474 ret = fan53555_voltages_setup_fairchild(di); 475 break; 476 case FAN53555_VENDOR_SILERGY: 477 ret = fan53555_voltages_setup_silergy(di); 478 break; 479 case FAN53555_VENDOR_TCS: 480 ret = fan53555_voltages_setup_tcs(di); 481 break; 482 default: 483 dev_err(di->dev, "vendor %d not supported!\n", di->vendor); 484 return -EINVAL; 485 } 486 487 return ret; 488 } 489 490 static int fan53555_regulator_register(struct fan53555_device_info *di, 491 struct regulator_config *config) 492 { 493 struct regulator_desc *rdesc = &di->desc; 494 struct regulator_dev *rdev; 495 496 rdesc->name = "fan53555-reg"; 497 rdesc->supply_name = "vin"; 498 rdesc->ops = &fan53555_regulator_ops; 499 rdesc->type = REGULATOR_VOLTAGE; 500 rdesc->n_voltages = di->vsel_count; 501 rdesc->enable_reg = di->vol_reg; 502 rdesc->enable_mask = VSEL_BUCK_EN; 503 rdesc->min_uV = di->vsel_min; 504 rdesc->uV_step = di->vsel_step; 505 rdesc->vsel_reg = di->vol_reg; 506 rdesc->vsel_mask = di->vsel_count - 1; 507 rdesc->owner = THIS_MODULE; 508 509 rdev = devm_regulator_register(di->dev, &di->desc, config); 510 return PTR_ERR_OR_ZERO(rdev); 511 } 512 513 static const struct regmap_config fan53555_regmap_config = { 514 .reg_bits = 8, 515 .val_bits = 8, 516 }; 517 518 static struct fan53555_platform_data *fan53555_parse_dt(struct device *dev, 519 struct device_node *np, 520 const struct regulator_desc *desc) 521 { 522 struct fan53555_platform_data *pdata; 523 int ret; 524 u32 tmp; 525 526 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 527 if (!pdata) 528 return NULL; 529 530 pdata->regulator = of_get_regulator_init_data(dev, np, desc); 531 532 ret = of_property_read_u32(np, "fcs,suspend-voltage-selector", 533 &tmp); 534 if (!ret) 535 pdata->sleep_vsel_id = tmp; 536 537 return pdata; 538 } 539 540 static const struct of_device_id __maybe_unused fan53555_dt_ids[] = { 541 { 542 .compatible = "fcs,fan53526", 543 .data = (void *)FAN53526_VENDOR_FAIRCHILD, 544 }, { 545 .compatible = "fcs,fan53555", 546 .data = (void *)FAN53555_VENDOR_FAIRCHILD 547 }, { 548 .compatible = "silergy,syr827", 549 .data = (void *)FAN53555_VENDOR_SILERGY, 550 }, { 551 .compatible = "silergy,syr828", 552 .data = (void *)FAN53555_VENDOR_SILERGY, 553 }, { 554 .compatible = "tcs,tcs4525", 555 .data = (void *)FAN53555_VENDOR_TCS 556 }, 557 { } 558 }; 559 MODULE_DEVICE_TABLE(of, fan53555_dt_ids); 560 561 static int fan53555_regulator_probe(struct i2c_client *client, 562 const struct i2c_device_id *id) 563 { 564 struct device_node *np = client->dev.of_node; 565 struct fan53555_device_info *di; 566 struct fan53555_platform_data *pdata; 567 struct regulator_config config = { }; 568 struct regmap *regmap; 569 unsigned int val; 570 int ret; 571 572 di = devm_kzalloc(&client->dev, sizeof(struct fan53555_device_info), 573 GFP_KERNEL); 574 if (!di) 575 return -ENOMEM; 576 577 pdata = dev_get_platdata(&client->dev); 578 if (!pdata) 579 pdata = fan53555_parse_dt(&client->dev, np, &di->desc); 580 581 if (!pdata || !pdata->regulator) { 582 dev_err(&client->dev, "Platform data not found!\n"); 583 return -ENODEV; 584 } 585 586 di->regulator = pdata->regulator; 587 if (client->dev.of_node) { 588 di->vendor = 589 (unsigned long)of_device_get_match_data(&client->dev); 590 } else { 591 /* if no ramp constraint set, get the pdata ramp_delay */ 592 if (!di->regulator->constraints.ramp_delay) { 593 if (pdata->slew_rate >= ARRAY_SIZE(slew_rates)) { 594 dev_err(&client->dev, "Invalid slew_rate\n"); 595 return -EINVAL; 596 } 597 598 di->regulator->constraints.ramp_delay 599 = slew_rates[pdata->slew_rate]; 600 } 601 602 di->vendor = id->driver_data; 603 } 604 605 regmap = devm_regmap_init_i2c(client, &fan53555_regmap_config); 606 if (IS_ERR(regmap)) { 607 dev_err(&client->dev, "Failed to allocate regmap!\n"); 608 return PTR_ERR(regmap); 609 } 610 di->dev = &client->dev; 611 i2c_set_clientdata(client, di); 612 /* Get chip ID */ 613 ret = regmap_read(regmap, FAN53555_ID1, &val); 614 if (ret < 0) { 615 dev_err(&client->dev, "Failed to get chip ID!\n"); 616 return ret; 617 } 618 di->chip_id = val & DIE_ID; 619 /* Get chip revision */ 620 ret = regmap_read(regmap, FAN53555_ID2, &val); 621 if (ret < 0) { 622 dev_err(&client->dev, "Failed to get chip Rev!\n"); 623 return ret; 624 } 625 di->chip_rev = val & DIE_REV; 626 dev_info(&client->dev, "FAN53555 Option[%d] Rev[%d] Detected!\n", 627 di->chip_id, di->chip_rev); 628 /* Device init */ 629 ret = fan53555_device_setup(di, pdata); 630 if (ret < 0) { 631 dev_err(&client->dev, "Failed to setup device!\n"); 632 return ret; 633 } 634 /* Register regulator */ 635 config.dev = di->dev; 636 config.init_data = di->regulator; 637 config.regmap = regmap; 638 config.driver_data = di; 639 config.of_node = np; 640 641 ret = fan53555_regulator_register(di, &config); 642 if (ret < 0) 643 dev_err(&client->dev, "Failed to register regulator!\n"); 644 return ret; 645 646 } 647 648 static const struct i2c_device_id fan53555_id[] = { 649 { 650 .name = "fan53526", 651 .driver_data = FAN53526_VENDOR_FAIRCHILD 652 }, { 653 .name = "fan53555", 654 .driver_data = FAN53555_VENDOR_FAIRCHILD 655 }, { 656 .name = "syr827", 657 .driver_data = FAN53555_VENDOR_SILERGY 658 }, { 659 .name = "syr828", 660 .driver_data = FAN53555_VENDOR_SILERGY 661 }, { 662 .name = "tcs4525", 663 .driver_data = FAN53555_VENDOR_TCS 664 }, 665 { }, 666 }; 667 MODULE_DEVICE_TABLE(i2c, fan53555_id); 668 669 static struct i2c_driver fan53555_regulator_driver = { 670 .driver = { 671 .name = "fan53555-regulator", 672 .of_match_table = of_match_ptr(fan53555_dt_ids), 673 }, 674 .probe = fan53555_regulator_probe, 675 .id_table = fan53555_id, 676 }; 677 678 module_i2c_driver(fan53555_regulator_driver); 679 680 MODULE_AUTHOR("Yunfan Zhang <yfzhang@marvell.com>"); 681 MODULE_DESCRIPTION("FAN53555 regulator driver"); 682 MODULE_LICENSE("GPL v2"); 683