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