1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * OF helpers for regulator framework 4 * 5 * Copyright (C) 2011 Texas Instruments, Inc. 6 * Rajendra Nayak <rnayak@ti.com> 7 */ 8 9 #include <linux/module.h> 10 #include <linux/slab.h> 11 #include <linux/of.h> 12 #include <linux/regulator/machine.h> 13 #include <linux/regulator/driver.h> 14 #include <linux/regulator/of_regulator.h> 15 16 #include "internal.h" 17 18 static const char *const regulator_states[PM_SUSPEND_MAX + 1] = { 19 [PM_SUSPEND_STANDBY] = "regulator-state-standby", 20 [PM_SUSPEND_MEM] = "regulator-state-mem", 21 [PM_SUSPEND_MAX] = "regulator-state-disk", 22 }; 23 24 static void fill_limit(int *limit, int val) 25 { 26 if (val) 27 if (val == 1) 28 *limit = REGULATOR_NOTIF_LIMIT_ENABLE; 29 else 30 *limit = val; 31 else 32 *limit = REGULATOR_NOTIF_LIMIT_DISABLE; 33 } 34 35 static void of_get_regulator_prot_limits(struct device_node *np, 36 struct regulation_constraints *constraints) 37 { 38 u32 pval; 39 int i; 40 static const char *const props[] = { 41 "regulator-oc-%s-microamp", 42 "regulator-ov-%s-microvolt", 43 "regulator-temp-%s-kelvin", 44 "regulator-uv-%s-microvolt", 45 }; 46 struct notification_limit *limits[] = { 47 &constraints->over_curr_limits, 48 &constraints->over_voltage_limits, 49 &constraints->temp_limits, 50 &constraints->under_voltage_limits, 51 }; 52 bool set[4] = {0}; 53 54 /* Protection limits: */ 55 for (i = 0; i < ARRAY_SIZE(props); i++) { 56 char prop[255]; 57 bool found; 58 int j; 59 static const char *const lvl[] = { 60 "protection", "error", "warn" 61 }; 62 int *l[] = { 63 &limits[i]->prot, &limits[i]->err, &limits[i]->warn, 64 }; 65 66 for (j = 0; j < ARRAY_SIZE(lvl); j++) { 67 snprintf(prop, 255, props[i], lvl[j]); 68 found = !of_property_read_u32(np, prop, &pval); 69 if (found) 70 fill_limit(l[j], pval); 71 set[i] |= found; 72 } 73 } 74 constraints->over_current_detection = set[0]; 75 constraints->over_voltage_detection = set[1]; 76 constraints->over_temp_detection = set[2]; 77 constraints->under_voltage_detection = set[3]; 78 } 79 80 static int of_get_regulation_constraints(struct device *dev, 81 struct device_node *np, 82 struct regulator_init_data **init_data, 83 const struct regulator_desc *desc) 84 { 85 struct regulation_constraints *constraints = &(*init_data)->constraints; 86 struct regulator_state *suspend_state; 87 struct device_node *suspend_np; 88 unsigned int mode; 89 int ret, i, len; 90 int n_phandles; 91 u32 pval; 92 93 n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with", 94 NULL); 95 n_phandles = max(n_phandles, 0); 96 97 constraints->name = of_get_property(np, "regulator-name", NULL); 98 99 if (!of_property_read_u32(np, "regulator-min-microvolt", &pval)) 100 constraints->min_uV = pval; 101 102 if (!of_property_read_u32(np, "regulator-max-microvolt", &pval)) 103 constraints->max_uV = pval; 104 105 /* Voltage change possible? */ 106 if (constraints->min_uV != constraints->max_uV) 107 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE; 108 109 /* Do we have a voltage range, if so try to apply it? */ 110 if (constraints->min_uV && constraints->max_uV) 111 constraints->apply_uV = true; 112 113 if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval)) 114 constraints->uV_offset = pval; 115 if (!of_property_read_u32(np, "regulator-min-microamp", &pval)) 116 constraints->min_uA = pval; 117 if (!of_property_read_u32(np, "regulator-max-microamp", &pval)) 118 constraints->max_uA = pval; 119 120 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp", 121 &pval)) 122 constraints->ilim_uA = pval; 123 124 /* Current change possible? */ 125 if (constraints->min_uA != constraints->max_uA) 126 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT; 127 128 constraints->boot_on = of_property_read_bool(np, "regulator-boot-on"); 129 constraints->always_on = of_property_read_bool(np, "regulator-always-on"); 130 if (!constraints->always_on) /* status change should be possible. */ 131 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS; 132 133 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down"); 134 constraints->system_critical = of_property_read_bool(np, 135 "system-critical-regulator"); 136 137 if (of_property_read_bool(np, "regulator-allow-bypass")) 138 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS; 139 140 if (of_property_read_bool(np, "regulator-allow-set-load")) 141 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS; 142 143 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval); 144 if (!ret) { 145 if (pval) 146 constraints->ramp_delay = pval; 147 else 148 constraints->ramp_disable = true; 149 } 150 151 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval); 152 if (!ret) 153 constraints->settling_time = pval; 154 155 ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval); 156 if (!ret) 157 constraints->settling_time_up = pval; 158 if (constraints->settling_time_up && constraints->settling_time) { 159 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n", 160 np); 161 constraints->settling_time_up = 0; 162 } 163 164 ret = of_property_read_u32(np, "regulator-settling-time-down-us", 165 &pval); 166 if (!ret) 167 constraints->settling_time_down = pval; 168 if (constraints->settling_time_down && constraints->settling_time) { 169 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n", 170 np); 171 constraints->settling_time_down = 0; 172 } 173 174 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval); 175 if (!ret) 176 constraints->enable_time = pval; 177 178 ret = of_property_read_u32(np, "regulator-uv-survival-time-ms", &pval); 179 if (!ret) 180 constraints->uv_less_critical_window_ms = pval; 181 else 182 constraints->uv_less_critical_window_ms = 183 REGULATOR_DEF_UV_LESS_CRITICAL_WINDOW_MS; 184 185 constraints->soft_start = of_property_read_bool(np, 186 "regulator-soft-start"); 187 ret = of_property_read_u32(np, "regulator-active-discharge", &pval); 188 if (!ret) { 189 constraints->active_discharge = 190 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE : 191 REGULATOR_ACTIVE_DISCHARGE_DISABLE; 192 } 193 194 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) { 195 if (desc && desc->of_map_mode) { 196 mode = desc->of_map_mode(pval); 197 if (mode == REGULATOR_MODE_INVALID) 198 pr_err("%pOFn: invalid mode %u\n", np, pval); 199 else 200 constraints->initial_mode = mode; 201 } else { 202 pr_warn("%pOFn: mapping for mode %d not defined\n", 203 np, pval); 204 } 205 } 206 207 len = of_property_count_elems_of_size(np, "regulator-allowed-modes", 208 sizeof(u32)); 209 if (len > 0) { 210 if (desc && desc->of_map_mode) { 211 for (i = 0; i < len; i++) { 212 ret = of_property_read_u32_index(np, 213 "regulator-allowed-modes", i, &pval); 214 if (ret) { 215 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n", 216 np, i, ret); 217 break; 218 } 219 mode = desc->of_map_mode(pval); 220 if (mode == REGULATOR_MODE_INVALID) 221 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n", 222 np, pval); 223 else 224 constraints->valid_modes_mask |= mode; 225 } 226 if (constraints->valid_modes_mask) 227 constraints->valid_ops_mask 228 |= REGULATOR_CHANGE_MODE; 229 } else { 230 pr_warn("%pOFn: mode mapping not defined\n", np); 231 } 232 } 233 234 if (!of_property_read_u32(np, "regulator-system-load", &pval)) 235 constraints->system_load = pval; 236 237 if (n_phandles) { 238 constraints->max_spread = devm_kzalloc(dev, 239 sizeof(*constraints->max_spread) * n_phandles, 240 GFP_KERNEL); 241 242 if (!constraints->max_spread) 243 return -ENOMEM; 244 245 of_property_read_u32_array(np, "regulator-coupled-max-spread", 246 constraints->max_spread, n_phandles); 247 } 248 249 if (!of_property_read_u32(np, "regulator-max-step-microvolt", 250 &pval)) 251 constraints->max_uV_step = pval; 252 253 constraints->over_current_protection = of_property_read_bool(np, 254 "regulator-over-current-protection"); 255 256 of_get_regulator_prot_limits(np, constraints); 257 258 for (i = 0; i < ARRAY_SIZE(regulator_states); i++) { 259 switch (i) { 260 case PM_SUSPEND_MEM: 261 suspend_state = &constraints->state_mem; 262 break; 263 case PM_SUSPEND_MAX: 264 suspend_state = &constraints->state_disk; 265 break; 266 case PM_SUSPEND_STANDBY: 267 suspend_state = &constraints->state_standby; 268 break; 269 case PM_SUSPEND_ON: 270 case PM_SUSPEND_TO_IDLE: 271 default: 272 continue; 273 } 274 275 suspend_np = of_get_child_by_name(np, regulator_states[i]); 276 if (!suspend_np) 277 continue; 278 if (!suspend_state) { 279 of_node_put(suspend_np); 280 continue; 281 } 282 283 if (!of_property_read_u32(suspend_np, "regulator-mode", 284 &pval)) { 285 if (desc && desc->of_map_mode) { 286 mode = desc->of_map_mode(pval); 287 if (mode == REGULATOR_MODE_INVALID) 288 pr_err("%pOFn: invalid mode %u\n", 289 np, pval); 290 else 291 suspend_state->mode = mode; 292 } else { 293 pr_warn("%pOFn: mapping for mode %d not defined\n", 294 np, pval); 295 } 296 } 297 298 if (of_property_read_bool(suspend_np, 299 "regulator-on-in-suspend")) 300 suspend_state->enabled = ENABLE_IN_SUSPEND; 301 else if (of_property_read_bool(suspend_np, 302 "regulator-off-in-suspend")) 303 suspend_state->enabled = DISABLE_IN_SUSPEND; 304 305 if (!of_property_read_u32(suspend_np, 306 "regulator-suspend-min-microvolt", &pval)) 307 suspend_state->min_uV = pval; 308 309 if (!of_property_read_u32(suspend_np, 310 "regulator-suspend-max-microvolt", &pval)) 311 suspend_state->max_uV = pval; 312 313 if (!of_property_read_u32(suspend_np, 314 "regulator-suspend-microvolt", &pval)) 315 suspend_state->uV = pval; 316 else /* otherwise use min_uV as default suspend voltage */ 317 suspend_state->uV = suspend_state->min_uV; 318 319 if (of_property_read_bool(suspend_np, 320 "regulator-changeable-in-suspend")) 321 suspend_state->changeable = true; 322 323 if (i == PM_SUSPEND_MEM) 324 constraints->initial_state = PM_SUSPEND_MEM; 325 326 of_node_put(suspend_np); 327 suspend_state = NULL; 328 suspend_np = NULL; 329 } 330 331 return 0; 332 } 333 334 /** 335 * of_get_regulator_init_data - extract regulator_init_data structure info 336 * @dev: device requesting for regulator_init_data 337 * @node: regulator device node 338 * @desc: regulator description 339 * 340 * Populates regulator_init_data structure by extracting data from device 341 * tree node. 342 * 343 * Return: Pointer to a populated &struct regulator_init_data or NULL if 344 * memory allocation fails. 345 */ 346 struct regulator_init_data *of_get_regulator_init_data(struct device *dev, 347 struct device_node *node, 348 const struct regulator_desc *desc) 349 { 350 struct regulator_init_data *init_data; 351 352 if (!node) 353 return NULL; 354 355 init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL); 356 if (!init_data) 357 return NULL; /* Out of memory? */ 358 359 if (of_get_regulation_constraints(dev, node, &init_data, desc)) 360 return NULL; 361 362 return init_data; 363 } 364 EXPORT_SYMBOL_GPL(of_get_regulator_init_data); 365 366 struct devm_of_regulator_matches { 367 struct of_regulator_match *matches; 368 unsigned int num_matches; 369 }; 370 371 static void devm_of_regulator_put_matches(struct device *dev, void *res) 372 { 373 struct devm_of_regulator_matches *devm_matches = res; 374 int i; 375 376 for (i = 0; i < devm_matches->num_matches; i++) 377 of_node_put(devm_matches->matches[i].of_node); 378 } 379 380 /** 381 * of_regulator_match - extract multiple regulator init data from device tree. 382 * @dev: device requesting the data 383 * @node: parent device node of the regulators 384 * @matches: match table for the regulators 385 * @num_matches: number of entries in match table 386 * 387 * This function uses a match table specified by the regulator driver to 388 * parse regulator init data from the device tree. @node is expected to 389 * contain a set of child nodes, each providing the init data for one 390 * regulator. The data parsed from a child node will be matched to a regulator 391 * based on either the deprecated property regulator-compatible if present, 392 * or otherwise the child node's name. Note that the match table is modified 393 * in place and an additional of_node reference is taken for each matched 394 * regulator. 395 * 396 * Return: The number of matches found or a negative error number on failure. 397 */ 398 int of_regulator_match(struct device *dev, struct device_node *node, 399 struct of_regulator_match *matches, 400 unsigned int num_matches) 401 { 402 unsigned int count = 0; 403 unsigned int i; 404 const char *name; 405 struct device_node *child; 406 struct devm_of_regulator_matches *devm_matches; 407 408 if (!dev || !node) 409 return -EINVAL; 410 411 devm_matches = devres_alloc(devm_of_regulator_put_matches, 412 sizeof(struct devm_of_regulator_matches), 413 GFP_KERNEL); 414 if (!devm_matches) 415 return -ENOMEM; 416 417 devm_matches->matches = matches; 418 devm_matches->num_matches = num_matches; 419 420 devres_add(dev, devm_matches); 421 422 for (i = 0; i < num_matches; i++) { 423 struct of_regulator_match *match = &matches[i]; 424 match->init_data = NULL; 425 match->of_node = NULL; 426 } 427 428 for_each_child_of_node(node, child) { 429 name = of_get_property(child, 430 "regulator-compatible", NULL); 431 if (!name) 432 name = child->name; 433 for (i = 0; i < num_matches; i++) { 434 struct of_regulator_match *match = &matches[i]; 435 if (match->of_node) 436 continue; 437 438 if (strcmp(match->name, name)) 439 continue; 440 441 match->init_data = 442 of_get_regulator_init_data(dev, child, 443 match->desc); 444 if (!match->init_data) { 445 dev_err(dev, 446 "failed to parse DT for regulator %pOFn\n", 447 child); 448 of_node_put(child); 449 return -EINVAL; 450 } 451 match->of_node = of_node_get(child); 452 count++; 453 break; 454 } 455 } 456 457 return count; 458 } 459 EXPORT_SYMBOL_GPL(of_regulator_match); 460 461 static struct 462 device_node *regulator_of_get_init_node(struct device *dev, 463 const struct regulator_desc *desc) 464 { 465 struct device_node *search, *child; 466 const char *name; 467 468 if (!dev->of_node || !desc->of_match) 469 return NULL; 470 471 if (desc->regulators_node) { 472 search = of_get_child_by_name(dev->of_node, 473 desc->regulators_node); 474 } else { 475 search = of_node_get(dev->of_node); 476 477 if (!strcmp(desc->of_match, search->name)) 478 return search; 479 } 480 481 if (!search) { 482 dev_dbg(dev, "Failed to find regulator container node '%s'\n", 483 desc->regulators_node); 484 return NULL; 485 } 486 487 for_each_available_child_of_node(search, child) { 488 name = of_get_property(child, "regulator-compatible", NULL); 489 if (!name) { 490 if (!desc->of_match_full_name) 491 name = child->name; 492 else 493 name = child->full_name; 494 } 495 496 if (!strcmp(desc->of_match, name)) { 497 of_node_put(search); 498 /* 499 * 'of_node_get(child)' is already performed by the 500 * for_each loop. 501 */ 502 return child; 503 } 504 } 505 506 of_node_put(search); 507 508 return NULL; 509 } 510 511 struct regulator_init_data *regulator_of_get_init_data(struct device *dev, 512 const struct regulator_desc *desc, 513 struct regulator_config *config, 514 struct device_node **node) 515 { 516 struct device_node *child; 517 struct regulator_init_data *init_data = NULL; 518 519 child = regulator_of_get_init_node(config->dev, desc); 520 if (!child) 521 return NULL; 522 523 init_data = of_get_regulator_init_data(dev, child, desc); 524 if (!init_data) { 525 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child); 526 goto error; 527 } 528 529 if (desc->of_parse_cb) { 530 int ret; 531 532 ret = desc->of_parse_cb(child, desc, config); 533 if (ret) { 534 if (ret == -EPROBE_DEFER) { 535 of_node_put(child); 536 return ERR_PTR(-EPROBE_DEFER); 537 } 538 dev_err(dev, 539 "driver callback failed to parse DT for regulator %pOFn\n", 540 child); 541 goto error; 542 } 543 } 544 545 *node = child; 546 547 return init_data; 548 549 error: 550 of_node_put(child); 551 552 return NULL; 553 } 554 555 /** 556 * of_get_child_regulator - get a child regulator device node 557 * based on supply name 558 * @parent: Parent device node 559 * @prop_name: Combination regulator supply name and "-supply" 560 * 561 * Traverse all child nodes. 562 * Extract the child regulator device node corresponding to the supply name. 563 * 564 * Return: Pointer to the &struct device_node corresponding to the regulator 565 * if found, or %NULL if not found. 566 */ 567 static struct device_node *of_get_child_regulator(struct device_node *parent, 568 const char *prop_name) 569 { 570 struct device_node *regnode = NULL; 571 struct device_node *child = NULL; 572 573 for_each_child_of_node(parent, child) { 574 regnode = of_parse_phandle(child, prop_name, 0); 575 if (regnode) 576 goto err_node_put; 577 578 regnode = of_get_child_regulator(child, prop_name); 579 if (regnode) 580 goto err_node_put; 581 } 582 return NULL; 583 584 err_node_put: 585 of_node_put(child); 586 return regnode; 587 } 588 589 /** 590 * of_get_regulator - get a regulator device node based on supply name 591 * @dev: Device pointer for the consumer (of regulator) device 592 * @supply: regulator supply name 593 * 594 * Extract the regulator device node corresponding to the supply name. 595 * 596 * Return: Pointer to the &struct device_node corresponding to the regulator 597 * if found, or %NULL if not found. 598 */ 599 static struct device_node *of_get_regulator(struct device *dev, const char *supply) 600 { 601 struct device_node *regnode = NULL; 602 char prop_name[64]; /* 64 is max size of property name */ 603 604 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); 605 606 snprintf(prop_name, 64, "%s-supply", supply); 607 regnode = of_parse_phandle(dev->of_node, prop_name, 0); 608 if (regnode) 609 return regnode; 610 611 regnode = of_get_child_regulator(dev->of_node, prop_name); 612 if (regnode) 613 return regnode; 614 615 dev_dbg(dev, "Looking up %s property in node %pOF failed\n", prop_name, dev->of_node); 616 return NULL; 617 } 618 619 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np) 620 { 621 struct device *dev; 622 623 dev = class_find_device_by_of_node(®ulator_class, np); 624 625 return dev ? dev_to_rdev(dev) : NULL; 626 } 627 628 /** 629 * of_regulator_dev_lookup - lookup a regulator device with device tree only 630 * @dev: Device pointer for regulator supply lookup. 631 * @supply: Supply name or regulator ID. 632 * 633 * Return: Pointer to the &struct regulator_dev on success, or ERR_PTR() 634 * encoded value on error. 635 * 636 * If successful, returns a pointer to the &struct regulator_dev that 637 * corresponds to the name @supply and with the embedded &struct device 638 * refcount incremented by one. The refcount must be dropped by calling 639 * put_device(). 640 * 641 * On failure one of the following ERR_PTR() encoded values is returned: 642 * * -%ENODEV if lookup fails permanently. 643 * * -%EPROBE_DEFER if lookup could succeed in the future. 644 */ 645 struct regulator_dev *of_regulator_dev_lookup(struct device *dev, 646 const char *supply) 647 { 648 struct regulator_dev *r; 649 struct device_node *node; 650 651 node = of_get_regulator(dev, supply); 652 if (node) { 653 r = of_find_regulator_by_node(node); 654 of_node_put(node); 655 if (r) 656 return r; 657 658 /* 659 * We have a node, but there is no device. 660 * assume it has not registered yet. 661 */ 662 return ERR_PTR(-EPROBE_DEFER); 663 } 664 665 return ERR_PTR(-ENODEV); 666 } 667 668 /* 669 * Returns number of regulators coupled with rdev. 670 */ 671 int of_get_n_coupled(struct regulator_dev *rdev) 672 { 673 struct device_node *node = rdev->dev.of_node; 674 int n_phandles; 675 676 n_phandles = of_count_phandle_with_args(node, 677 "regulator-coupled-with", 678 NULL); 679 680 return (n_phandles > 0) ? n_phandles : 0; 681 } 682 683 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */ 684 static bool of_coupling_find_node(struct device_node *src, 685 struct device_node *to_find, 686 int *index) 687 { 688 int n_phandles, i; 689 bool found = false; 690 691 n_phandles = of_count_phandle_with_args(src, 692 "regulator-coupled-with", 693 NULL); 694 695 for (i = 0; i < n_phandles; i++) { 696 struct device_node *tmp = of_parse_phandle(src, 697 "regulator-coupled-with", i); 698 699 if (!tmp) 700 break; 701 702 /* found */ 703 if (tmp == to_find) 704 found = true; 705 706 of_node_put(tmp); 707 708 if (found) { 709 *index = i; 710 break; 711 } 712 } 713 714 return found; 715 } 716 717 /** 718 * of_check_coupling_data - Parse rdev's coupling properties and check data 719 * consistency 720 * @rdev: pointer to regulator_dev whose data is checked 721 * 722 * Function checks if all the following conditions are met: 723 * - rdev's max_spread is greater than 0 724 * - all coupled regulators have the same max_spread 725 * - all coupled regulators have the same number of regulator_dev phandles 726 * - all regulators are linked to each other 727 * 728 * Return: True if all conditions are met; false otherwise. 729 */ 730 bool of_check_coupling_data(struct regulator_dev *rdev) 731 { 732 struct device_node *node = rdev->dev.of_node; 733 int n_phandles = of_get_n_coupled(rdev); 734 struct device_node *c_node; 735 int index; 736 int i; 737 bool ret = true; 738 739 /* iterate over rdev's phandles */ 740 for (i = 0; i < n_phandles; i++) { 741 int max_spread = rdev->constraints->max_spread[i]; 742 int c_max_spread, c_n_phandles; 743 744 if (max_spread <= 0) { 745 dev_err(&rdev->dev, "max_spread value invalid\n"); 746 return false; 747 } 748 749 c_node = of_parse_phandle(node, 750 "regulator-coupled-with", i); 751 752 if (!c_node) 753 ret = false; 754 755 c_n_phandles = of_count_phandle_with_args(c_node, 756 "regulator-coupled-with", 757 NULL); 758 759 if (c_n_phandles != n_phandles) { 760 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n"); 761 ret = false; 762 goto clean; 763 } 764 765 if (!of_coupling_find_node(c_node, node, &index)) { 766 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n"); 767 ret = false; 768 goto clean; 769 } 770 771 if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread", 772 index, &c_max_spread)) { 773 ret = false; 774 goto clean; 775 } 776 777 if (c_max_spread != max_spread) { 778 dev_err(&rdev->dev, 779 "coupled regulators max_spread mismatch\n"); 780 ret = false; 781 goto clean; 782 } 783 784 clean: 785 of_node_put(c_node); 786 if (!ret) 787 break; 788 } 789 790 return ret; 791 } 792 793 /** 794 * of_parse_coupled_regulator() - Get regulator_dev pointer from rdev's property 795 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse 796 * "regulator-coupled-with" property 797 * @index: Index in phandles array 798 * 799 * Return: Pointer to the &struct regulator_dev parsed from DTS, or %NULL if 800 * it has not yet been registered. 801 */ 802 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev, 803 int index) 804 { 805 struct device_node *node = rdev->dev.of_node; 806 struct device_node *c_node; 807 struct regulator_dev *c_rdev; 808 809 c_node = of_parse_phandle(node, "regulator-coupled-with", index); 810 if (!c_node) 811 return NULL; 812 813 c_rdev = of_find_regulator_by_node(c_node); 814 815 of_node_put(c_node); 816 817 return c_rdev; 818 } 819 820 /* 821 * Check if name is a supply name according to the '*-supply' pattern 822 * return 0 if false 823 * return length of supply name without the -supply 824 */ 825 static int is_supply_name(const char *name) 826 { 827 int strs, i; 828 829 strs = strlen(name); 830 /* string need to be at minimum len(x-supply) */ 831 if (strs < 8) 832 return 0; 833 for (i = strs - 6; i > 0; i--) { 834 /* find first '-' and check if right part is supply */ 835 if (name[i] != '-') 836 continue; 837 if (strcmp(name + i + 1, "supply") != 0) 838 return 0; 839 return i; 840 } 841 return 0; 842 } 843 844 /** 845 * of_regulator_bulk_get_all - get multiple regulator consumers 846 * 847 * @dev: Device to supply 848 * @np: device node to search for consumers 849 * @consumers: Configuration of consumers; clients are stored here. 850 * 851 * This helper function allows drivers to get several regulator 852 * consumers in one operation. If any of the regulators cannot be 853 * acquired then any regulators that were allocated will be freed 854 * before returning to the caller, and @consumers will not be 855 * changed. 856 * 857 * Return: Number of regulators on success, or a negative error number 858 * on failure. 859 */ 860 int of_regulator_bulk_get_all(struct device *dev, struct device_node *np, 861 struct regulator_bulk_data **consumers) 862 { 863 int num_consumers = 0; 864 struct regulator *tmp; 865 struct regulator_bulk_data *_consumers = NULL; 866 struct property *prop; 867 int i, n = 0, ret; 868 char name[64]; 869 870 /* 871 * first pass: get numbers of xxx-supply 872 * second pass: fill consumers 873 */ 874 restart: 875 for_each_property_of_node(np, prop) { 876 i = is_supply_name(prop->name); 877 if (i == 0) 878 continue; 879 if (!_consumers) { 880 num_consumers++; 881 continue; 882 } else { 883 memcpy(name, prop->name, i); 884 name[i] = '\0'; 885 tmp = regulator_get(dev, name); 886 if (IS_ERR(tmp)) { 887 ret = PTR_ERR(tmp); 888 goto error; 889 } 890 _consumers[n].consumer = tmp; 891 n++; 892 continue; 893 } 894 } 895 if (_consumers) { 896 *consumers = _consumers; 897 return num_consumers; 898 } 899 if (num_consumers == 0) 900 return 0; 901 _consumers = kmalloc_array(num_consumers, 902 sizeof(struct regulator_bulk_data), 903 GFP_KERNEL); 904 if (!_consumers) 905 return -ENOMEM; 906 goto restart; 907 908 error: 909 while (--n >= 0) 910 regulator_put(_consumers[n].consumer); 911 kfree(_consumers); 912 return ret; 913 } 914 EXPORT_SYMBOL_GPL(of_regulator_bulk_get_all); 915