hc
2024-05-16 8d2a02b24d66aa359e83eebc1ed3c0f85367a1cb
kernel/drivers/regulator/of_regulator.c
....@@ -1,13 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * OF helpers for regulator framework
34 *
45 * Copyright (C) 2011 Texas Instruments, Inc.
56 * 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.
117 */
128
139 #include <linux/module.h>
....@@ -20,11 +16,13 @@
2016 #include "internal.h"
2117
2218 static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
19
+ [PM_SUSPEND_STANDBY] = "regulator-state-standby",
2320 [PM_SUSPEND_MEM] = "regulator-state-mem",
2421 [PM_SUSPEND_MAX] = "regulator-state-disk",
2522 };
2623
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,
2826 struct regulator_init_data **init_data,
2927 const struct regulator_desc *desc)
3028 {
....@@ -33,7 +31,12 @@
3331 struct device_node *suspend_np;
3432 unsigned int mode;
3533 int ret, i, len;
34
+ int n_phandles;
3635 u32 pval;
36
+
37
+ n_phandles = of_count_phandle_with_args(np, "regulator-coupled-with",
38
+ NULL);
39
+ n_phandles = max(n_phandles, 0);
3740
3841 constraints->name = of_get_property(np, "regulator-name", NULL);
3942
....@@ -95,8 +98,8 @@
9598 if (!ret)
9699 constraints->settling_time_up = pval;
97100 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);
100103 constraints->settling_time_up = 0;
101104 }
102105
....@@ -105,8 +108,8 @@
105108 if (!ret)
106109 constraints->settling_time_down = pval;
107110 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);
110113 constraints->settling_time_down = 0;
111114 }
112115
....@@ -127,12 +130,12 @@
127130 if (desc && desc->of_map_mode) {
128131 mode = desc->of_map_mode(pval);
129132 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);
131134 else
132135 constraints->initial_mode = mode;
133136 } 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);
136139 }
137140 }
138141
....@@ -144,14 +147,14 @@
144147 ret = of_property_read_u32_index(np,
145148 "regulator-allowed-modes", i, &pval);
146149 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);
149152 break;
150153 }
151154 mode = desc->of_map_mode(pval);
152155 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);
155158 else
156159 constraints->valid_modes_mask |= mode;
157160 }
....@@ -159,16 +162,28 @@
159162 constraints->valid_ops_mask
160163 |= REGULATOR_CHANGE_MODE;
161164 } else {
162
- pr_warn("%s: mode mapping not defined\n", np->name);
165
+ pr_warn("%pOFn: mode mapping not defined\n", np);
163166 }
164167 }
165168
166169 if (!of_property_read_u32(np, "regulator-system-load", &pval))
167170 constraints->system_load = pval;
168171
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",
170185 &pval))
171
- constraints->max_spread = pval;
186
+ constraints->max_uV_step = pval;
172187
173188 constraints->over_current_protection = of_property_read_bool(np,
174189 "regulator-over-current-protection");
....@@ -181,29 +196,35 @@
181196 case PM_SUSPEND_MAX:
182197 suspend_state = &constraints->state_disk;
183198 break;
199
+ case PM_SUSPEND_STANDBY:
200
+ suspend_state = &constraints->state_standby;
201
+ break;
184202 case PM_SUSPEND_ON:
185203 case PM_SUSPEND_TO_IDLE:
186
- case PM_SUSPEND_STANDBY:
187204 default:
188205 continue;
189206 }
190207
191208 suspend_np = of_get_child_by_name(np, regulator_states[i]);
192
- if (!suspend_np || !suspend_state)
209
+ if (!suspend_np)
193210 continue;
211
+ if (!suspend_state) {
212
+ of_node_put(suspend_np);
213
+ continue;
214
+ }
194215
195216 if (!of_property_read_u32(suspend_np, "regulator-mode",
196217 &pval)) {
197218 if (desc && desc->of_map_mode) {
198219 mode = desc->of_map_mode(pval);
199220 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);
202223 else
203224 suspend_state->mode = mode;
204225 } 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);
207228 }
208229 }
209230
....@@ -239,6 +260,8 @@
239260 suspend_state = NULL;
240261 suspend_np = NULL;
241262 }
263
+
264
+ return 0;
242265 }
243266
244267 /**
....@@ -248,7 +271,7 @@
248271 * @desc: regulator description
249272 *
250273 * 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
252275 * alloc fails.
253276 */
254277 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
....@@ -264,7 +287,9 @@
264287 if (!init_data)
265288 return NULL; /* Out of memory? */
266289
267
- of_get_regulation_constraints(node, &init_data, desc);
290
+ if (of_get_regulation_constraints(dev, node, &init_data, desc))
291
+ return NULL;
292
+
268293 return init_data;
269294 }
270295 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
....@@ -349,8 +374,8 @@
349374 match->desc);
350375 if (!match->init_data) {
351376 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);
354379 of_node_put(child);
355380 return -EINVAL;
356381 }
....@@ -364,23 +389,25 @@
364389 }
365390 EXPORT_SYMBOL_GPL(of_regulator_match);
366391
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)
371395 {
372396 struct device_node *search, *child;
373
- struct regulator_init_data *init_data = NULL;
374397 const char *name;
375398
376399 if (!dev->of_node || !desc->of_match)
377400 return NULL;
378401
379
- if (desc->regulators_node)
402
+ if (desc->regulators_node) {
380403 search = of_get_child_by_name(dev->of_node,
381404 desc->regulators_node);
382
- else
405
+ } else {
383406 search = of_node_get(dev->of_node);
407
+
408
+ if (!strcmp(desc->of_match, search->name))
409
+ return search;
410
+ }
384411
385412 if (!search) {
386413 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
....@@ -390,50 +417,73 @@
390417
391418 for_each_available_child_of_node(search, child) {
392419 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;
405425 }
406426
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);
415430 }
416
-
417
- of_node_get(child);
418
- *node = child;
419
- break;
420431 }
421432
422433 of_node_put(search);
423434
424
- return init_data;
435
+ return NULL;
425436 }
426437
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)
428442 {
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;
430480 }
431481
432482 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
433483 {
434484 struct device *dev;
435485
436
- dev = class_find_device(&regulator_class, NULL, np, of_node_match);
486
+ dev = class_find_device_by_of_node(&regulator_class, np);
437487
438488 return dev ? dev_to_rdev(dev) : NULL;
439489 }
....@@ -455,7 +505,8 @@
455505
456506 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
457507 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)
459510 {
460511 int n_phandles, i;
461512 bool found = false;
....@@ -477,8 +528,10 @@
477528
478529 of_node_put(tmp);
479530
480
- if (found)
531
+ if (found) {
532
+ *index = i;
481533 break;
534
+ }
482535 }
483536
484537 return found;
....@@ -487,7 +540,7 @@
487540 /**
488541 * of_check_coupling_data - Parse rdev's coupling properties and check data
489542 * consistency
490
- * @rdev - pointer to regulator_dev whose data is checked
543
+ * @rdev: pointer to regulator_dev whose data is checked
491544 *
492545 * Function checks if all the following conditions are met:
493546 * - rdev's max_spread is greater than 0
....@@ -499,21 +552,22 @@
499552 */
500553 bool of_check_coupling_data(struct regulator_dev *rdev)
501554 {
502
- int max_spread = rdev->constraints->max_spread;
503555 struct device_node *node = rdev->dev.of_node;
504556 int n_phandles = of_get_n_coupled(rdev);
505557 struct device_node *c_node;
558
+ int index;
506559 int i;
507560 bool ret = true;
508561
509
- if (max_spread <= 0) {
510
- dev_err(&rdev->dev, "max_spread value invalid\n");
511
- return false;
512
- }
513
-
514562 /* iterate over rdev's phandles */
515563 for (i = 0; i < n_phandles; i++) {
564
+ int max_spread = rdev->constraints->max_spread[i];
516565 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
+ }
517571
518572 c_node = of_parse_phandle(node,
519573 "regulator-coupled-with", i);
....@@ -526,13 +580,19 @@
526580 NULL);
527581
528582 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");
530584 ret = false;
531585 goto clean;
532586 }
533587
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)) {
536596 ret = false;
537597 goto clean;
538598 }
....@@ -542,11 +602,6 @@
542602 "coupled regulators max_spread mismatch\n");
543603 ret = false;
544604 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;
550605 }
551606
552607 clean: