.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * OF helpers for regulator framework |
---|
3 | 4 | * |
---|
4 | 5 | * Copyright (C) 2011 Texas Instruments, Inc. |
---|
5 | 6 | * Rajendra Nayak <rnayak@ti.com> |
---|
6 | | - * |
---|
7 | | - * This program is free software; you can redistribute it and/or modify |
---|
8 | | - * it under the terms of the GNU General Public License as published by |
---|
9 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
10 | | - * (at your option) any later version. |
---|
11 | 7 | */ |
---|
12 | 8 | |
---|
13 | 9 | #include <linux/module.h> |
---|
.. | .. |
---|
20 | 16 | #include "internal.h" |
---|
21 | 17 | |
---|
22 | 18 | static const char *const regulator_states[PM_SUSPEND_MAX + 1] = { |
---|
| 19 | + [PM_SUSPEND_STANDBY] = "regulator-state-standby", |
---|
23 | 20 | [PM_SUSPEND_MEM] = "regulator-state-mem", |
---|
24 | 21 | [PM_SUSPEND_MAX] = "regulator-state-disk", |
---|
25 | 22 | }; |
---|
26 | 23 | |
---|
27 | | -static void of_get_regulation_constraints(struct device_node *np, |
---|
| 24 | +static int of_get_regulation_constraints(struct device *dev, |
---|
| 25 | + struct device_node *np, |
---|
28 | 26 | struct regulator_init_data **init_data, |
---|
29 | 27 | const struct regulator_desc *desc) |
---|
30 | 28 | { |
---|
.. | .. |
---|
33 | 31 | struct device_node *suspend_np; |
---|
34 | 32 | unsigned int mode; |
---|
35 | 33 | int ret, i, len; |
---|
| 34 | + int n_phandles; |
---|
36 | 35 | u32 pval; |
---|
| 36 | + |
---|
| 37 | + n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with", |
---|
| 38 | + NULL); |
---|
| 39 | + n_phandles = max(n_phandles, 0); |
---|
37 | 40 | |
---|
38 | 41 | constraints->name = of_get_property(np, "regulator-name", NULL); |
---|
39 | 42 | |
---|
.. | .. |
---|
95 | 98 | if (!ret) |
---|
96 | 99 | constraints->settling_time_up = pval; |
---|
97 | 100 | if (constraints->settling_time_up && constraints->settling_time) { |
---|
98 | | - pr_warn("%s: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n", |
---|
99 | | - np->name); |
---|
| 101 | + pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n", |
---|
| 102 | + np); |
---|
100 | 103 | constraints->settling_time_up = 0; |
---|
101 | 104 | } |
---|
102 | 105 | |
---|
.. | .. |
---|
105 | 108 | if (!ret) |
---|
106 | 109 | constraints->settling_time_down = pval; |
---|
107 | 110 | if (constraints->settling_time_down && constraints->settling_time) { |
---|
108 | | - pr_warn("%s: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n", |
---|
109 | | - np->name); |
---|
| 111 | + pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n", |
---|
| 112 | + np); |
---|
110 | 113 | constraints->settling_time_down = 0; |
---|
111 | 114 | } |
---|
112 | 115 | |
---|
.. | .. |
---|
127 | 130 | if (desc && desc->of_map_mode) { |
---|
128 | 131 | mode = desc->of_map_mode(pval); |
---|
129 | 132 | if (mode == REGULATOR_MODE_INVALID) |
---|
130 | | - pr_err("%s: invalid mode %u\n", np->name, pval); |
---|
| 133 | + pr_err("%pOFn: invalid mode %u\n", np, pval); |
---|
131 | 134 | else |
---|
132 | 135 | constraints->initial_mode = mode; |
---|
133 | 136 | } else { |
---|
134 | | - pr_warn("%s: mapping for mode %d not defined\n", |
---|
135 | | - np->name, pval); |
---|
| 137 | + pr_warn("%pOFn: mapping for mode %d not defined\n", |
---|
| 138 | + np, pval); |
---|
136 | 139 | } |
---|
137 | 140 | } |
---|
138 | 141 | |
---|
.. | .. |
---|
144 | 147 | ret = of_property_read_u32_index(np, |
---|
145 | 148 | "regulator-allowed-modes", i, &pval); |
---|
146 | 149 | if (ret) { |
---|
147 | | - pr_err("%s: couldn't read allowed modes index %d, ret=%d\n", |
---|
148 | | - np->name, i, ret); |
---|
| 150 | + pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n", |
---|
| 151 | + np, i, ret); |
---|
149 | 152 | break; |
---|
150 | 153 | } |
---|
151 | 154 | mode = desc->of_map_mode(pval); |
---|
152 | 155 | if (mode == REGULATOR_MODE_INVALID) |
---|
153 | | - pr_err("%s: invalid regulator-allowed-modes element %u\n", |
---|
154 | | - np->name, pval); |
---|
| 156 | + pr_err("%pOFn: invalid regulator-allowed-modes element %u\n", |
---|
| 157 | + np, pval); |
---|
155 | 158 | else |
---|
156 | 159 | constraints->valid_modes_mask |= mode; |
---|
157 | 160 | } |
---|
.. | .. |
---|
159 | 162 | constraints->valid_ops_mask |
---|
160 | 163 | |= REGULATOR_CHANGE_MODE; |
---|
161 | 164 | } else { |
---|
162 | | - pr_warn("%s: mode mapping not defined\n", np->name); |
---|
| 165 | + pr_warn("%pOFn: mode mapping not defined\n", np); |
---|
163 | 166 | } |
---|
164 | 167 | } |
---|
165 | 168 | |
---|
166 | 169 | if (!of_property_read_u32(np, "regulator-system-load", &pval)) |
---|
167 | 170 | constraints->system_load = pval; |
---|
168 | 171 | |
---|
169 | | - if (!of_property_read_u32(np, "regulator-coupled-max-spread", |
---|
| 172 | + if (n_phandles) { |
---|
| 173 | + constraints->max_spread = devm_kzalloc(dev, |
---|
| 174 | + sizeof(*constraints->max_spread) * n_phandles, |
---|
| 175 | + GFP_KERNEL); |
---|
| 176 | + |
---|
| 177 | + if (!constraints->max_spread) |
---|
| 178 | + return -ENOMEM; |
---|
| 179 | + |
---|
| 180 | + of_property_read_u32_array(np, "regulator-coupled-max-spread", |
---|
| 181 | + constraints->max_spread, n_phandles); |
---|
| 182 | + } |
---|
| 183 | + |
---|
| 184 | + if (!of_property_read_u32(np, "regulator-max-step-microvolt", |
---|
170 | 185 | &pval)) |
---|
171 | | - constraints->max_spread = pval; |
---|
| 186 | + constraints->max_uV_step = pval; |
---|
172 | 187 | |
---|
173 | 188 | constraints->over_current_protection = of_property_read_bool(np, |
---|
174 | 189 | "regulator-over-current-protection"); |
---|
.. | .. |
---|
181 | 196 | case PM_SUSPEND_MAX: |
---|
182 | 197 | suspend_state = &constraints->state_disk; |
---|
183 | 198 | break; |
---|
| 199 | + case PM_SUSPEND_STANDBY: |
---|
| 200 | + suspend_state = &constraints->state_standby; |
---|
| 201 | + break; |
---|
184 | 202 | case PM_SUSPEND_ON: |
---|
185 | 203 | case PM_SUSPEND_TO_IDLE: |
---|
186 | | - case PM_SUSPEND_STANDBY: |
---|
187 | 204 | default: |
---|
188 | 205 | continue; |
---|
189 | 206 | } |
---|
190 | 207 | |
---|
191 | 208 | suspend_np = of_get_child_by_name(np, regulator_states[i]); |
---|
192 | | - if (!suspend_np || !suspend_state) |
---|
| 209 | + if (!suspend_np) |
---|
193 | 210 | continue; |
---|
| 211 | + if (!suspend_state) { |
---|
| 212 | + of_node_put(suspend_np); |
---|
| 213 | + continue; |
---|
| 214 | + } |
---|
194 | 215 | |
---|
195 | 216 | if (!of_property_read_u32(suspend_np, "regulator-mode", |
---|
196 | 217 | &pval)) { |
---|
197 | 218 | if (desc && desc->of_map_mode) { |
---|
198 | 219 | mode = desc->of_map_mode(pval); |
---|
199 | 220 | if (mode == REGULATOR_MODE_INVALID) |
---|
200 | | - pr_err("%s: invalid mode %u\n", |
---|
201 | | - np->name, pval); |
---|
| 221 | + pr_err("%pOFn: invalid mode %u\n", |
---|
| 222 | + np, pval); |
---|
202 | 223 | else |
---|
203 | 224 | suspend_state->mode = mode; |
---|
204 | 225 | } else { |
---|
205 | | - pr_warn("%s: mapping for mode %d not defined\n", |
---|
206 | | - np->name, pval); |
---|
| 226 | + pr_warn("%pOFn: mapping for mode %d not defined\n", |
---|
| 227 | + np, pval); |
---|
207 | 228 | } |
---|
208 | 229 | } |
---|
209 | 230 | |
---|
.. | .. |
---|
239 | 260 | suspend_state = NULL; |
---|
240 | 261 | suspend_np = NULL; |
---|
241 | 262 | } |
---|
| 263 | + |
---|
| 264 | + return 0; |
---|
242 | 265 | } |
---|
243 | 266 | |
---|
244 | 267 | /** |
---|
.. | .. |
---|
248 | 271 | * @desc: regulator description |
---|
249 | 272 | * |
---|
250 | 273 | * Populates regulator_init_data structure by extracting data from device |
---|
251 | | - * tree node, returns a pointer to the populated struture or NULL if memory |
---|
| 274 | + * tree node, returns a pointer to the populated structure or NULL if memory |
---|
252 | 275 | * alloc fails. |
---|
253 | 276 | */ |
---|
254 | 277 | struct regulator_init_data *of_get_regulator_init_data(struct device *dev, |
---|
.. | .. |
---|
264 | 287 | if (!init_data) |
---|
265 | 288 | return NULL; /* Out of memory? */ |
---|
266 | 289 | |
---|
267 | | - of_get_regulation_constraints(node, &init_data, desc); |
---|
| 290 | + if (of_get_regulation_constraints(dev, node, &init_data, desc)) |
---|
| 291 | + return NULL; |
---|
| 292 | + |
---|
268 | 293 | return init_data; |
---|
269 | 294 | } |
---|
270 | 295 | EXPORT_SYMBOL_GPL(of_get_regulator_init_data); |
---|
.. | .. |
---|
349 | 374 | match->desc); |
---|
350 | 375 | if (!match->init_data) { |
---|
351 | 376 | dev_err(dev, |
---|
352 | | - "failed to parse DT for regulator %s\n", |
---|
353 | | - child->name); |
---|
| 377 | + "failed to parse DT for regulator %pOFn\n", |
---|
| 378 | + child); |
---|
354 | 379 | of_node_put(child); |
---|
355 | 380 | return -EINVAL; |
---|
356 | 381 | } |
---|
.. | .. |
---|
364 | 389 | } |
---|
365 | 390 | EXPORT_SYMBOL_GPL(of_regulator_match); |
---|
366 | 391 | |
---|
367 | | -struct regulator_init_data *regulator_of_get_init_data(struct device *dev, |
---|
368 | | - const struct regulator_desc *desc, |
---|
369 | | - struct regulator_config *config, |
---|
370 | | - struct device_node **node) |
---|
| 392 | +static struct |
---|
| 393 | +device_node *regulator_of_get_init_node(struct device *dev, |
---|
| 394 | + const struct regulator_desc *desc) |
---|
371 | 395 | { |
---|
372 | 396 | struct device_node *search, *child; |
---|
373 | | - struct regulator_init_data *init_data = NULL; |
---|
374 | 397 | const char *name; |
---|
375 | 398 | |
---|
376 | 399 | if (!dev->of_node || !desc->of_match) |
---|
377 | 400 | return NULL; |
---|
378 | 401 | |
---|
379 | | - if (desc->regulators_node) |
---|
| 402 | + if (desc->regulators_node) { |
---|
380 | 403 | search = of_get_child_by_name(dev->of_node, |
---|
381 | 404 | desc->regulators_node); |
---|
382 | | - else |
---|
| 405 | + } else { |
---|
383 | 406 | search = of_node_get(dev->of_node); |
---|
| 407 | + |
---|
| 408 | + if (!strcmp(desc->of_match, search->name)) |
---|
| 409 | + return search; |
---|
| 410 | + } |
---|
384 | 411 | |
---|
385 | 412 | if (!search) { |
---|
386 | 413 | dev_dbg(dev, "Failed to find regulator container node '%s'\n", |
---|
.. | .. |
---|
390 | 417 | |
---|
391 | 418 | for_each_available_child_of_node(search, child) { |
---|
392 | 419 | name = of_get_property(child, "regulator-compatible", NULL); |
---|
393 | | - if (!name) |
---|
394 | | - name = child->name; |
---|
395 | | - |
---|
396 | | - if (strcmp(desc->of_match, name)) |
---|
397 | | - continue; |
---|
398 | | - |
---|
399 | | - init_data = of_get_regulator_init_data(dev, child, desc); |
---|
400 | | - if (!init_data) { |
---|
401 | | - dev_err(dev, |
---|
402 | | - "failed to parse DT for regulator %s\n", |
---|
403 | | - child->name); |
---|
404 | | - break; |
---|
| 420 | + if (!name) { |
---|
| 421 | + if (!desc->of_match_full_name) |
---|
| 422 | + name = child->name; |
---|
| 423 | + else |
---|
| 424 | + name = child->full_name; |
---|
405 | 425 | } |
---|
406 | 426 | |
---|
407 | | - if (desc->of_parse_cb) { |
---|
408 | | - if (desc->of_parse_cb(child, desc, config)) { |
---|
409 | | - dev_err(dev, |
---|
410 | | - "driver callback failed to parse DT for regulator %s\n", |
---|
411 | | - child->name); |
---|
412 | | - init_data = NULL; |
---|
413 | | - break; |
---|
414 | | - } |
---|
| 427 | + if (!strcmp(desc->of_match, name)) { |
---|
| 428 | + of_node_put(search); |
---|
| 429 | + return of_node_get(child); |
---|
415 | 430 | } |
---|
416 | | - |
---|
417 | | - of_node_get(child); |
---|
418 | | - *node = child; |
---|
419 | | - break; |
---|
420 | 431 | } |
---|
421 | 432 | |
---|
422 | 433 | of_node_put(search); |
---|
423 | 434 | |
---|
424 | | - return init_data; |
---|
| 435 | + return NULL; |
---|
425 | 436 | } |
---|
426 | 437 | |
---|
427 | | -static int of_node_match(struct device *dev, const void *data) |
---|
| 438 | +struct regulator_init_data *regulator_of_get_init_data(struct device *dev, |
---|
| 439 | + const struct regulator_desc *desc, |
---|
| 440 | + struct regulator_config *config, |
---|
| 441 | + struct device_node **node) |
---|
428 | 442 | { |
---|
429 | | - return dev->of_node == data; |
---|
| 443 | + struct device_node *child; |
---|
| 444 | + struct regulator_init_data *init_data = NULL; |
---|
| 445 | + |
---|
| 446 | + child = regulator_of_get_init_node(dev, desc); |
---|
| 447 | + if (!child) |
---|
| 448 | + return NULL; |
---|
| 449 | + |
---|
| 450 | + init_data = of_get_regulator_init_data(dev, child, desc); |
---|
| 451 | + if (!init_data) { |
---|
| 452 | + dev_err(dev, "failed to parse DT for regulator %pOFn\n", child); |
---|
| 453 | + goto error; |
---|
| 454 | + } |
---|
| 455 | + |
---|
| 456 | + if (desc->of_parse_cb) { |
---|
| 457 | + int ret; |
---|
| 458 | + |
---|
| 459 | + ret = desc->of_parse_cb(child, desc, config); |
---|
| 460 | + if (ret) { |
---|
| 461 | + if (ret == -EPROBE_DEFER) { |
---|
| 462 | + of_node_put(child); |
---|
| 463 | + return ERR_PTR(-EPROBE_DEFER); |
---|
| 464 | + } |
---|
| 465 | + dev_err(dev, |
---|
| 466 | + "driver callback failed to parse DT for regulator %pOFn\n", |
---|
| 467 | + child); |
---|
| 468 | + goto error; |
---|
| 469 | + } |
---|
| 470 | + } |
---|
| 471 | + |
---|
| 472 | + *node = child; |
---|
| 473 | + |
---|
| 474 | + return init_data; |
---|
| 475 | + |
---|
| 476 | +error: |
---|
| 477 | + of_node_put(child); |
---|
| 478 | + |
---|
| 479 | + return NULL; |
---|
430 | 480 | } |
---|
431 | 481 | |
---|
432 | 482 | struct regulator_dev *of_find_regulator_by_node(struct device_node *np) |
---|
433 | 483 | { |
---|
434 | 484 | struct device *dev; |
---|
435 | 485 | |
---|
436 | | - dev = class_find_device(®ulator_class, NULL, np, of_node_match); |
---|
| 486 | + dev = class_find_device_by_of_node(®ulator_class, np); |
---|
437 | 487 | |
---|
438 | 488 | return dev ? dev_to_rdev(dev) : NULL; |
---|
439 | 489 | } |
---|
.. | .. |
---|
455 | 505 | |
---|
456 | 506 | /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */ |
---|
457 | 507 | static bool of_coupling_find_node(struct device_node *src, |
---|
458 | | - struct device_node *to_find) |
---|
| 508 | + struct device_node *to_find, |
---|
| 509 | + int *index) |
---|
459 | 510 | { |
---|
460 | 511 | int n_phandles, i; |
---|
461 | 512 | bool found = false; |
---|
.. | .. |
---|
477 | 528 | |
---|
478 | 529 | of_node_put(tmp); |
---|
479 | 530 | |
---|
480 | | - if (found) |
---|
| 531 | + if (found) { |
---|
| 532 | + *index = i; |
---|
481 | 533 | break; |
---|
| 534 | + } |
---|
482 | 535 | } |
---|
483 | 536 | |
---|
484 | 537 | return found; |
---|
.. | .. |
---|
487 | 540 | /** |
---|
488 | 541 | * of_check_coupling_data - Parse rdev's coupling properties and check data |
---|
489 | 542 | * consistency |
---|
490 | | - * @rdev - pointer to regulator_dev whose data is checked |
---|
| 543 | + * @rdev: pointer to regulator_dev whose data is checked |
---|
491 | 544 | * |
---|
492 | 545 | * Function checks if all the following conditions are met: |
---|
493 | 546 | * - rdev's max_spread is greater than 0 |
---|
.. | .. |
---|
499 | 552 | */ |
---|
500 | 553 | bool of_check_coupling_data(struct regulator_dev *rdev) |
---|
501 | 554 | { |
---|
502 | | - int max_spread = rdev->constraints->max_spread; |
---|
503 | 555 | struct device_node *node = rdev->dev.of_node; |
---|
504 | 556 | int n_phandles = of_get_n_coupled(rdev); |
---|
505 | 557 | struct device_node *c_node; |
---|
| 558 | + int index; |
---|
506 | 559 | int i; |
---|
507 | 560 | bool ret = true; |
---|
508 | 561 | |
---|
509 | | - if (max_spread <= 0) { |
---|
510 | | - dev_err(&rdev->dev, "max_spread value invalid\n"); |
---|
511 | | - return false; |
---|
512 | | - } |
---|
513 | | - |
---|
514 | 562 | /* iterate over rdev's phandles */ |
---|
515 | 563 | for (i = 0; i < n_phandles; i++) { |
---|
| 564 | + int max_spread = rdev->constraints->max_spread[i]; |
---|
516 | 565 | int c_max_spread, c_n_phandles; |
---|
| 566 | + |
---|
| 567 | + if (max_spread <= 0) { |
---|
| 568 | + dev_err(&rdev->dev, "max_spread value invalid\n"); |
---|
| 569 | + return false; |
---|
| 570 | + } |
---|
517 | 571 | |
---|
518 | 572 | c_node = of_parse_phandle(node, |
---|
519 | 573 | "regulator-coupled-with", i); |
---|
.. | .. |
---|
526 | 580 | NULL); |
---|
527 | 581 | |
---|
528 | 582 | if (c_n_phandles != n_phandles) { |
---|
529 | | - dev_err(&rdev->dev, "number of couped reg phandles mismatch\n"); |
---|
| 583 | + dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n"); |
---|
530 | 584 | ret = false; |
---|
531 | 585 | goto clean; |
---|
532 | 586 | } |
---|
533 | 587 | |
---|
534 | | - if (of_property_read_u32(c_node, "regulator-coupled-max-spread", |
---|
535 | | - &c_max_spread)) { |
---|
| 588 | + if (!of_coupling_find_node(c_node, node, &index)) { |
---|
| 589 | + dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n"); |
---|
| 590 | + ret = false; |
---|
| 591 | + goto clean; |
---|
| 592 | + } |
---|
| 593 | + |
---|
| 594 | + if (of_property_read_u32_index(c_node, "regulator-coupled-max-spread", |
---|
| 595 | + index, &c_max_spread)) { |
---|
536 | 596 | ret = false; |
---|
537 | 597 | goto clean; |
---|
538 | 598 | } |
---|
.. | .. |
---|
542 | 602 | "coupled regulators max_spread mismatch\n"); |
---|
543 | 603 | ret = false; |
---|
544 | 604 | goto clean; |
---|
545 | | - } |
---|
546 | | - |
---|
547 | | - if (!of_coupling_find_node(c_node, node)) { |
---|
548 | | - dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n"); |
---|
549 | | - ret = false; |
---|
550 | 605 | } |
---|
551 | 606 | |
---|
552 | 607 | clean: |
---|