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