1 /* 2 * Copyright (c) 2014, Sony Mobile Communications AB. 3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 and 7 * only version 2 as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/module.h> 16 #include <linux/platform_device.h> 17 #include <linux/of.h> 18 #include <linux/of_device.h> 19 #include <linux/regulator/driver.h> 20 #include <linux/regulator/machine.h> 21 #include <linux/regulator/of_regulator.h> 22 #include <linux/mfd/qcom_rpm.h> 23 24 #include <dt-bindings/mfd/qcom-rpm.h> 25 26 #define MAX_REQUEST_LEN 2 27 28 struct request_member { 29 int word; 30 unsigned int mask; 31 int shift; 32 }; 33 34 struct rpm_reg_parts { 35 struct request_member mV; /* used if voltage is in mV */ 36 struct request_member uV; /* used if voltage is in uV */ 37 struct request_member ip; /* peak current in mA */ 38 struct request_member pd; /* pull down enable */ 39 struct request_member ia; /* average current in mA */ 40 struct request_member fm; /* force mode */ 41 struct request_member pm; /* power mode */ 42 struct request_member pc; /* pin control */ 43 struct request_member pf; /* pin function */ 44 struct request_member enable_state; /* NCP and switch */ 45 struct request_member comp_mode; /* NCP */ 46 struct request_member freq; /* frequency: NCP and SMPS */ 47 struct request_member freq_clk_src; /* clock source: SMPS */ 48 struct request_member hpm; /* switch: control OCP and SS */ 49 int request_len; 50 }; 51 52 #define FORCE_MODE_IS_2_BITS(reg) \ 53 (((reg)->parts->fm.mask >> (reg)->parts->fm.shift) == 3) 54 55 struct qcom_rpm_reg { 56 struct qcom_rpm *rpm; 57 58 struct mutex lock; 59 struct device *dev; 60 struct regulator_desc desc; 61 const struct rpm_reg_parts *parts; 62 63 int resource; 64 u32 val[MAX_REQUEST_LEN]; 65 66 int uV; 67 int is_enabled; 68 69 bool supports_force_mode_auto; 70 bool supports_force_mode_bypass; 71 }; 72 73 static const struct rpm_reg_parts rpm8660_ldo_parts = { 74 .request_len = 2, 75 .mV = { 0, 0x00000FFF, 0 }, 76 .ip = { 0, 0x00FFF000, 12 }, 77 .fm = { 0, 0x03000000, 24 }, 78 .pc = { 0, 0x3C000000, 26 }, 79 .pf = { 0, 0xC0000000, 30 }, 80 .pd = { 1, 0x00000001, 0 }, 81 .ia = { 1, 0x00001FFE, 1 }, 82 }; 83 84 static const struct rpm_reg_parts rpm8660_smps_parts = { 85 .request_len = 2, 86 .mV = { 0, 0x00000FFF, 0 }, 87 .ip = { 0, 0x00FFF000, 12 }, 88 .fm = { 0, 0x03000000, 24 }, 89 .pc = { 0, 0x3C000000, 26 }, 90 .pf = { 0, 0xC0000000, 30 }, 91 .pd = { 1, 0x00000001, 0 }, 92 .ia = { 1, 0x00001FFE, 1 }, 93 .freq = { 1, 0x001FE000, 13 }, 94 .freq_clk_src = { 1, 0x00600000, 21 }, 95 }; 96 97 static const struct rpm_reg_parts rpm8660_switch_parts = { 98 .request_len = 1, 99 .enable_state = { 0, 0x00000001, 0 }, 100 .pd = { 0, 0x00000002, 1 }, 101 .pc = { 0, 0x0000003C, 2 }, 102 .pf = { 0, 0x000000C0, 6 }, 103 .hpm = { 0, 0x00000300, 8 }, 104 }; 105 106 static const struct rpm_reg_parts rpm8660_ncp_parts = { 107 .request_len = 1, 108 .mV = { 0, 0x00000FFF, 0 }, 109 .enable_state = { 0, 0x00001000, 12 }, 110 .comp_mode = { 0, 0x00002000, 13 }, 111 .freq = { 0, 0x003FC000, 14 }, 112 }; 113 114 static const struct rpm_reg_parts rpm8960_ldo_parts = { 115 .request_len = 2, 116 .uV = { 0, 0x007FFFFF, 0 }, 117 .pd = { 0, 0x00800000, 23 }, 118 .pc = { 0, 0x0F000000, 24 }, 119 .pf = { 0, 0xF0000000, 28 }, 120 .ip = { 1, 0x000003FF, 0 }, 121 .ia = { 1, 0x000FFC00, 10 }, 122 .fm = { 1, 0x00700000, 20 }, 123 }; 124 125 static const struct rpm_reg_parts rpm8960_smps_parts = { 126 .request_len = 2, 127 .uV = { 0, 0x007FFFFF, 0 }, 128 .pd = { 0, 0x00800000, 23 }, 129 .pc = { 0, 0x0F000000, 24 }, 130 .pf = { 0, 0xF0000000, 28 }, 131 .ip = { 1, 0x000003FF, 0 }, 132 .ia = { 1, 0x000FFC00, 10 }, 133 .fm = { 1, 0x00700000, 20 }, 134 .pm = { 1, 0x00800000, 23 }, 135 .freq = { 1, 0x1F000000, 24 }, 136 .freq_clk_src = { 1, 0x60000000, 29 }, 137 }; 138 139 static const struct rpm_reg_parts rpm8960_switch_parts = { 140 .request_len = 1, 141 .enable_state = { 0, 0x00000001, 0 }, 142 .pd = { 0, 0x00000002, 1 }, 143 .pc = { 0, 0x0000003C, 2 }, 144 .pf = { 0, 0x000003C0, 6 }, 145 .hpm = { 0, 0x00000C00, 10 }, 146 }; 147 148 static const struct rpm_reg_parts rpm8960_ncp_parts = { 149 .request_len = 1, 150 .uV = { 0, 0x007FFFFF, 0 }, 151 .enable_state = { 0, 0x00800000, 23 }, 152 .comp_mode = { 0, 0x01000000, 24 }, 153 .freq = { 0, 0x3E000000, 25 }, 154 }; 155 156 /* 157 * Physically available PMIC regulator voltage ranges 158 */ 159 static const struct regulator_linear_range pldo_ranges[] = { 160 REGULATOR_LINEAR_RANGE( 750000, 0, 59, 12500), 161 REGULATOR_LINEAR_RANGE(1500000, 60, 123, 25000), 162 REGULATOR_LINEAR_RANGE(3100000, 124, 160, 50000), 163 }; 164 165 static const struct regulator_linear_range nldo_ranges[] = { 166 REGULATOR_LINEAR_RANGE( 750000, 0, 63, 12500), 167 }; 168 169 static const struct regulator_linear_range nldo1200_ranges[] = { 170 REGULATOR_LINEAR_RANGE( 375000, 0, 59, 6250), 171 REGULATOR_LINEAR_RANGE( 750000, 60, 123, 12500), 172 }; 173 174 static const struct regulator_linear_range smps_ranges[] = { 175 REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500), 176 REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500), 177 REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000), 178 }; 179 180 static const struct regulator_linear_range ftsmps_ranges[] = { 181 REGULATOR_LINEAR_RANGE( 350000, 0, 6, 50000), 182 REGULATOR_LINEAR_RANGE( 700000, 7, 63, 12500), 183 REGULATOR_LINEAR_RANGE(1500000, 64, 100, 50000), 184 }; 185 186 static const struct regulator_linear_range ncp_ranges[] = { 187 REGULATOR_LINEAR_RANGE(1500000, 0, 31, 50000), 188 }; 189 190 static int rpm_reg_write(struct qcom_rpm_reg *vreg, 191 const struct request_member *req, 192 const int value) 193 { 194 if (WARN_ON((value << req->shift) & ~req->mask)) 195 return -EINVAL; 196 197 vreg->val[req->word] &= ~req->mask; 198 vreg->val[req->word] |= value << req->shift; 199 200 return qcom_rpm_write(vreg->rpm, 201 vreg->resource, 202 vreg->val, 203 vreg->parts->request_len); 204 } 205 206 static int rpm_reg_set_mV_sel(struct regulator_dev *rdev, 207 unsigned selector) 208 { 209 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 210 const struct rpm_reg_parts *parts = vreg->parts; 211 const struct request_member *req = &parts->mV; 212 int ret = 0; 213 int uV; 214 215 if (req->mask == 0) 216 return -EINVAL; 217 218 uV = regulator_list_voltage_linear_range(rdev, selector); 219 if (uV < 0) 220 return uV; 221 222 mutex_lock(&vreg->lock); 223 vreg->uV = uV; 224 if (vreg->is_enabled) 225 ret = rpm_reg_write(vreg, req, vreg->uV / 1000); 226 mutex_unlock(&vreg->lock); 227 228 return ret; 229 } 230 231 static int rpm_reg_set_uV_sel(struct regulator_dev *rdev, 232 unsigned selector) 233 { 234 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 235 const struct rpm_reg_parts *parts = vreg->parts; 236 const struct request_member *req = &parts->uV; 237 int ret = 0; 238 int uV; 239 240 if (req->mask == 0) 241 return -EINVAL; 242 243 uV = regulator_list_voltage_linear_range(rdev, selector); 244 if (uV < 0) 245 return uV; 246 247 mutex_lock(&vreg->lock); 248 vreg->uV = uV; 249 if (vreg->is_enabled) 250 ret = rpm_reg_write(vreg, req, vreg->uV); 251 mutex_unlock(&vreg->lock); 252 253 return ret; 254 } 255 256 static int rpm_reg_get_voltage(struct regulator_dev *rdev) 257 { 258 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 259 260 return vreg->uV; 261 } 262 263 static int rpm_reg_mV_enable(struct regulator_dev *rdev) 264 { 265 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 266 const struct rpm_reg_parts *parts = vreg->parts; 267 const struct request_member *req = &parts->mV; 268 int ret; 269 270 if (req->mask == 0) 271 return -EINVAL; 272 273 mutex_lock(&vreg->lock); 274 ret = rpm_reg_write(vreg, req, vreg->uV / 1000); 275 if (!ret) 276 vreg->is_enabled = 1; 277 mutex_unlock(&vreg->lock); 278 279 return ret; 280 } 281 282 static int rpm_reg_uV_enable(struct regulator_dev *rdev) 283 { 284 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 285 const struct rpm_reg_parts *parts = vreg->parts; 286 const struct request_member *req = &parts->uV; 287 int ret; 288 289 if (req->mask == 0) 290 return -EINVAL; 291 292 mutex_lock(&vreg->lock); 293 ret = rpm_reg_write(vreg, req, vreg->uV); 294 if (!ret) 295 vreg->is_enabled = 1; 296 mutex_unlock(&vreg->lock); 297 298 return ret; 299 } 300 301 static int rpm_reg_switch_enable(struct regulator_dev *rdev) 302 { 303 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 304 const struct rpm_reg_parts *parts = vreg->parts; 305 const struct request_member *req = &parts->enable_state; 306 int ret; 307 308 if (req->mask == 0) 309 return -EINVAL; 310 311 mutex_lock(&vreg->lock); 312 ret = rpm_reg_write(vreg, req, 1); 313 if (!ret) 314 vreg->is_enabled = 1; 315 mutex_unlock(&vreg->lock); 316 317 return ret; 318 } 319 320 static int rpm_reg_mV_disable(struct regulator_dev *rdev) 321 { 322 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 323 const struct rpm_reg_parts *parts = vreg->parts; 324 const struct request_member *req = &parts->mV; 325 int ret; 326 327 if (req->mask == 0) 328 return -EINVAL; 329 330 mutex_lock(&vreg->lock); 331 ret = rpm_reg_write(vreg, req, 0); 332 if (!ret) 333 vreg->is_enabled = 0; 334 mutex_unlock(&vreg->lock); 335 336 return ret; 337 } 338 339 static int rpm_reg_uV_disable(struct regulator_dev *rdev) 340 { 341 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 342 const struct rpm_reg_parts *parts = vreg->parts; 343 const struct request_member *req = &parts->uV; 344 int ret; 345 346 if (req->mask == 0) 347 return -EINVAL; 348 349 mutex_lock(&vreg->lock); 350 ret = rpm_reg_write(vreg, req, 0); 351 if (!ret) 352 vreg->is_enabled = 0; 353 mutex_unlock(&vreg->lock); 354 355 return ret; 356 } 357 358 static int rpm_reg_switch_disable(struct regulator_dev *rdev) 359 { 360 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 361 const struct rpm_reg_parts *parts = vreg->parts; 362 const struct request_member *req = &parts->enable_state; 363 int ret; 364 365 if (req->mask == 0) 366 return -EINVAL; 367 368 mutex_lock(&vreg->lock); 369 ret = rpm_reg_write(vreg, req, 0); 370 if (!ret) 371 vreg->is_enabled = 0; 372 mutex_unlock(&vreg->lock); 373 374 return ret; 375 } 376 377 static int rpm_reg_is_enabled(struct regulator_dev *rdev) 378 { 379 struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev); 380 381 return vreg->is_enabled; 382 } 383 384 static struct regulator_ops uV_ops = { 385 .list_voltage = regulator_list_voltage_linear_range, 386 387 .set_voltage_sel = rpm_reg_set_uV_sel, 388 .get_voltage = rpm_reg_get_voltage, 389 390 .enable = rpm_reg_uV_enable, 391 .disable = rpm_reg_uV_disable, 392 .is_enabled = rpm_reg_is_enabled, 393 }; 394 395 static struct regulator_ops mV_ops = { 396 .list_voltage = regulator_list_voltage_linear_range, 397 398 .set_voltage_sel = rpm_reg_set_mV_sel, 399 .get_voltage = rpm_reg_get_voltage, 400 401 .enable = rpm_reg_mV_enable, 402 .disable = rpm_reg_mV_disable, 403 .is_enabled = rpm_reg_is_enabled, 404 }; 405 406 static struct regulator_ops switch_ops = { 407 .enable = rpm_reg_switch_enable, 408 .disable = rpm_reg_switch_disable, 409 .is_enabled = rpm_reg_is_enabled, 410 }; 411 412 /* 413 * PM8058 regulators 414 */ 415 static const struct qcom_rpm_reg pm8058_pldo = { 416 .desc.linear_ranges = pldo_ranges, 417 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges), 418 .desc.n_voltages = 161, 419 .desc.ops = &mV_ops, 420 .parts = &rpm8660_ldo_parts, 421 .supports_force_mode_auto = false, 422 .supports_force_mode_bypass = false, 423 }; 424 425 static const struct qcom_rpm_reg pm8058_nldo = { 426 .desc.linear_ranges = nldo_ranges, 427 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges), 428 .desc.n_voltages = 64, 429 .desc.ops = &mV_ops, 430 .parts = &rpm8660_ldo_parts, 431 .supports_force_mode_auto = false, 432 .supports_force_mode_bypass = false, 433 }; 434 435 static const struct qcom_rpm_reg pm8058_smps = { 436 .desc.linear_ranges = smps_ranges, 437 .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges), 438 .desc.n_voltages = 154, 439 .desc.ops = &mV_ops, 440 .parts = &rpm8660_smps_parts, 441 .supports_force_mode_auto = false, 442 .supports_force_mode_bypass = false, 443 }; 444 445 static const struct qcom_rpm_reg pm8058_ncp = { 446 .desc.linear_ranges = ncp_ranges, 447 .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges), 448 .desc.n_voltages = 32, 449 .desc.ops = &mV_ops, 450 .parts = &rpm8660_ncp_parts, 451 }; 452 453 static const struct qcom_rpm_reg pm8058_switch = { 454 .desc.ops = &switch_ops, 455 .parts = &rpm8660_switch_parts, 456 }; 457 458 /* 459 * PM8901 regulators 460 */ 461 static const struct qcom_rpm_reg pm8901_pldo = { 462 .desc.linear_ranges = pldo_ranges, 463 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges), 464 .desc.n_voltages = 161, 465 .desc.ops = &mV_ops, 466 .parts = &rpm8660_ldo_parts, 467 .supports_force_mode_auto = false, 468 .supports_force_mode_bypass = true, 469 }; 470 471 static const struct qcom_rpm_reg pm8901_nldo = { 472 .desc.linear_ranges = nldo_ranges, 473 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges), 474 .desc.n_voltages = 64, 475 .desc.ops = &mV_ops, 476 .parts = &rpm8660_ldo_parts, 477 .supports_force_mode_auto = false, 478 .supports_force_mode_bypass = true, 479 }; 480 481 static const struct qcom_rpm_reg pm8901_ftsmps = { 482 .desc.linear_ranges = ftsmps_ranges, 483 .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges), 484 .desc.n_voltages = 101, 485 .desc.ops = &mV_ops, 486 .parts = &rpm8660_smps_parts, 487 .supports_force_mode_auto = true, 488 .supports_force_mode_bypass = false, 489 }; 490 491 static const struct qcom_rpm_reg pm8901_switch = { 492 .desc.ops = &switch_ops, 493 .parts = &rpm8660_switch_parts, 494 }; 495 496 /* 497 * PM8921 regulators 498 */ 499 static const struct qcom_rpm_reg pm8921_pldo = { 500 .desc.linear_ranges = pldo_ranges, 501 .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges), 502 .desc.n_voltages = 161, 503 .desc.ops = &uV_ops, 504 .parts = &rpm8960_ldo_parts, 505 .supports_force_mode_auto = false, 506 .supports_force_mode_bypass = true, 507 }; 508 509 static const struct qcom_rpm_reg pm8921_nldo = { 510 .desc.linear_ranges = nldo_ranges, 511 .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges), 512 .desc.n_voltages = 64, 513 .desc.ops = &uV_ops, 514 .parts = &rpm8960_ldo_parts, 515 .supports_force_mode_auto = false, 516 .supports_force_mode_bypass = true, 517 }; 518 519 static const struct qcom_rpm_reg pm8921_nldo1200 = { 520 .desc.linear_ranges = nldo1200_ranges, 521 .desc.n_linear_ranges = ARRAY_SIZE(nldo1200_ranges), 522 .desc.n_voltages = 124, 523 .desc.ops = &uV_ops, 524 .parts = &rpm8960_ldo_parts, 525 .supports_force_mode_auto = false, 526 .supports_force_mode_bypass = true, 527 }; 528 529 static const struct qcom_rpm_reg pm8921_smps = { 530 .desc.linear_ranges = smps_ranges, 531 .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges), 532 .desc.n_voltages = 154, 533 .desc.ops = &uV_ops, 534 .parts = &rpm8960_smps_parts, 535 .supports_force_mode_auto = true, 536 .supports_force_mode_bypass = false, 537 }; 538 539 static const struct qcom_rpm_reg pm8921_ftsmps = { 540 .desc.linear_ranges = ftsmps_ranges, 541 .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges), 542 .desc.n_voltages = 101, 543 .desc.ops = &uV_ops, 544 .parts = &rpm8960_smps_parts, 545 .supports_force_mode_auto = true, 546 .supports_force_mode_bypass = false, 547 }; 548 549 static const struct qcom_rpm_reg pm8921_ncp = { 550 .desc.linear_ranges = ncp_ranges, 551 .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges), 552 .desc.n_voltages = 32, 553 .desc.ops = &uV_ops, 554 .parts = &rpm8960_ncp_parts, 555 }; 556 557 static const struct qcom_rpm_reg pm8921_switch = { 558 .desc.ops = &switch_ops, 559 .parts = &rpm8960_switch_parts, 560 }; 561 562 static const struct of_device_id rpm_of_match[] = { 563 { .compatible = "qcom,rpm-pm8058-pldo", .data = &pm8058_pldo }, 564 { .compatible = "qcom,rpm-pm8058-nldo", .data = &pm8058_nldo }, 565 { .compatible = "qcom,rpm-pm8058-smps", .data = &pm8058_smps }, 566 { .compatible = "qcom,rpm-pm8058-ncp", .data = &pm8058_ncp }, 567 { .compatible = "qcom,rpm-pm8058-switch", .data = &pm8058_switch }, 568 569 { .compatible = "qcom,rpm-pm8901-pldo", .data = &pm8901_pldo }, 570 { .compatible = "qcom,rpm-pm8901-nldo", .data = &pm8901_nldo }, 571 { .compatible = "qcom,rpm-pm8901-ftsmps", .data = &pm8901_ftsmps }, 572 { .compatible = "qcom,rpm-pm8901-switch", .data = &pm8901_switch }, 573 574 { .compatible = "qcom,rpm-pm8921-pldo", .data = &pm8921_pldo }, 575 { .compatible = "qcom,rpm-pm8921-nldo", .data = &pm8921_nldo }, 576 { .compatible = "qcom,rpm-pm8921-nldo1200", .data = &pm8921_nldo1200 }, 577 { .compatible = "qcom,rpm-pm8921-smps", .data = &pm8921_smps }, 578 { .compatible = "qcom,rpm-pm8921-ftsmps", .data = &pm8921_ftsmps }, 579 { .compatible = "qcom,rpm-pm8921-ncp", .data = &pm8921_ncp }, 580 { .compatible = "qcom,rpm-pm8921-switch", .data = &pm8921_switch }, 581 { } 582 }; 583 MODULE_DEVICE_TABLE(of, rpm_of_match); 584 585 static int rpm_reg_set(struct qcom_rpm_reg *vreg, 586 const struct request_member *req, 587 const int value) 588 { 589 if (req->mask == 0 || (value << req->shift) & ~req->mask) 590 return -EINVAL; 591 592 vreg->val[req->word] &= ~req->mask; 593 vreg->val[req->word] |= value << req->shift; 594 595 return 0; 596 } 597 598 static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg) 599 { 600 static const int freq_table[] = { 601 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000, 602 2400000, 2130000, 1920000, 1750000, 1600000, 1480000, 1370000, 603 1280000, 1200000, 604 605 }; 606 const char *key; 607 u32 freq; 608 int ret; 609 int i; 610 611 key = "qcom,switch-mode-frequency"; 612 ret = of_property_read_u32(dev->of_node, key, &freq); 613 if (ret) { 614 dev_err(dev, "regulator requires %s property\n", key); 615 return -EINVAL; 616 } 617 618 for (i = 0; i < ARRAY_SIZE(freq_table); i++) { 619 if (freq == freq_table[i]) { 620 rpm_reg_set(vreg, &vreg->parts->freq, i + 1); 621 return 0; 622 } 623 } 624 625 dev_err(dev, "invalid frequency %d\n", freq); 626 return -EINVAL; 627 } 628 629 static int rpm_reg_probe(struct platform_device *pdev) 630 { 631 struct regulator_init_data *initdata; 632 const struct qcom_rpm_reg *template; 633 const struct of_device_id *match; 634 struct regulator_config config = { }; 635 struct regulator_dev *rdev; 636 struct qcom_rpm_reg *vreg; 637 const char *key; 638 u32 force_mode; 639 bool pwm; 640 u32 val; 641 int ret; 642 643 match = of_match_device(rpm_of_match, &pdev->dev); 644 template = match->data; 645 646 initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node); 647 if (!initdata) 648 return -EINVAL; 649 650 vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL); 651 if (!vreg) { 652 dev_err(&pdev->dev, "failed to allocate vreg\n"); 653 return -ENOMEM; 654 } 655 memcpy(vreg, template, sizeof(*vreg)); 656 mutex_init(&vreg->lock); 657 vreg->dev = &pdev->dev; 658 vreg->desc.id = -1; 659 vreg->desc.owner = THIS_MODULE; 660 vreg->desc.type = REGULATOR_VOLTAGE; 661 vreg->desc.name = pdev->dev.of_node->name; 662 663 vreg->rpm = dev_get_drvdata(pdev->dev.parent); 664 if (!vreg->rpm) { 665 dev_err(&pdev->dev, "unable to retrieve handle to rpm\n"); 666 return -ENODEV; 667 } 668 669 key = "reg"; 670 ret = of_property_read_u32(pdev->dev.of_node, key, &val); 671 if (ret) { 672 dev_err(&pdev->dev, "failed to read %s\n", key); 673 return ret; 674 } 675 vreg->resource = val; 676 677 if ((vreg->parts->uV.mask || vreg->parts->mV.mask) && 678 (!initdata->constraints.min_uV || !initdata->constraints.max_uV)) { 679 dev_err(&pdev->dev, "no voltage specified for regulator\n"); 680 return -EINVAL; 681 } 682 683 key = "bias-pull-down"; 684 if (of_property_read_bool(pdev->dev.of_node, key)) { 685 ret = rpm_reg_set(vreg, &vreg->parts->pd, 1); 686 if (ret) { 687 dev_err(&pdev->dev, "%s is invalid", key); 688 return ret; 689 } 690 } 691 692 if (vreg->parts->freq.mask) { 693 ret = rpm_reg_of_parse_freq(&pdev->dev, vreg); 694 if (ret < 0) 695 return ret; 696 } 697 698 if (vreg->parts->pm.mask) { 699 key = "qcom,power-mode-hysteretic"; 700 pwm = !of_property_read_bool(pdev->dev.of_node, key); 701 702 ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm); 703 if (ret) { 704 dev_err(&pdev->dev, "failed to set power mode\n"); 705 return ret; 706 } 707 } 708 709 if (vreg->parts->fm.mask) { 710 force_mode = -1; 711 712 key = "qcom,force-mode"; 713 ret = of_property_read_u32(pdev->dev.of_node, key, &val); 714 if (ret == -EINVAL) { 715 val = QCOM_RPM_FORCE_MODE_NONE; 716 } else if (ret < 0) { 717 dev_err(&pdev->dev, "failed to read %s\n", key); 718 return ret; 719 } 720 721 /* 722 * If force-mode is encoded as 2 bits then the 723 * possible register values are: 724 * NONE, LPM, HPM 725 * otherwise: 726 * NONE, LPM, AUTO, HPM, BYPASS 727 */ 728 switch (val) { 729 case QCOM_RPM_FORCE_MODE_NONE: 730 force_mode = 0; 731 break; 732 case QCOM_RPM_FORCE_MODE_LPM: 733 force_mode = 1; 734 break; 735 case QCOM_RPM_FORCE_MODE_HPM: 736 if (FORCE_MODE_IS_2_BITS(vreg)) 737 force_mode = 2; 738 else 739 force_mode = 3; 740 break; 741 case QCOM_RPM_FORCE_MODE_AUTO: 742 if (vreg->supports_force_mode_auto) 743 force_mode = 2; 744 break; 745 case QCOM_RPM_FORCE_MODE_BYPASS: 746 if (vreg->supports_force_mode_bypass) 747 force_mode = 4; 748 break; 749 } 750 751 if (force_mode < 0) { 752 dev_err(&pdev->dev, "invalid force mode\n"); 753 return -EINVAL; 754 } 755 756 ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode); 757 if (ret) { 758 dev_err(&pdev->dev, "failed to set force mode\n"); 759 return ret; 760 } 761 } 762 763 config.dev = &pdev->dev; 764 config.init_data = initdata; 765 config.driver_data = vreg; 766 config.of_node = pdev->dev.of_node; 767 rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config); 768 if (IS_ERR(rdev)) { 769 dev_err(&pdev->dev, "can't register regulator\n"); 770 return PTR_ERR(rdev); 771 } 772 773 return 0; 774 } 775 776 static struct platform_driver rpm_reg_driver = { 777 .probe = rpm_reg_probe, 778 .driver = { 779 .name = "qcom_rpm_reg", 780 .owner = THIS_MODULE, 781 .of_match_table = of_match_ptr(rpm_of_match), 782 }, 783 }; 784 785 static int __init rpm_reg_init(void) 786 { 787 return platform_driver_register(&rpm_reg_driver); 788 } 789 subsys_initcall(rpm_reg_init); 790 791 static void __exit rpm_reg_exit(void) 792 { 793 platform_driver_unregister(&rpm_reg_driver); 794 } 795 module_exit(rpm_reg_exit) 796 797 MODULE_DESCRIPTION("Qualcomm RPM regulator driver"); 798 MODULE_LICENSE("GPL v2"); 799