OSDN Git Service

treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 180
[uclinux-h8/linux.git] / drivers / regulator / of_regulator.c
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 of_get_regulation_constraints(struct device_node *np,
25                                         struct regulator_init_data **init_data,
26                                         const struct regulator_desc *desc)
27 {
28         struct regulation_constraints *constraints = &(*init_data)->constraints;
29         struct regulator_state *suspend_state;
30         struct device_node *suspend_np;
31         unsigned int mode;
32         int ret, i, len;
33         u32 pval;
34
35         constraints->name = of_get_property(np, "regulator-name", NULL);
36
37         if (!of_property_read_u32(np, "regulator-min-microvolt", &pval))
38                 constraints->min_uV = pval;
39
40         if (!of_property_read_u32(np, "regulator-max-microvolt", &pval))
41                 constraints->max_uV = pval;
42
43         /* Voltage change possible? */
44         if (constraints->min_uV != constraints->max_uV)
45                 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
46
47         /* Do we have a voltage range, if so try to apply it? */
48         if (constraints->min_uV && constraints->max_uV)
49                 constraints->apply_uV = true;
50
51         if (!of_property_read_u32(np, "regulator-microvolt-offset", &pval))
52                 constraints->uV_offset = pval;
53         if (!of_property_read_u32(np, "regulator-min-microamp", &pval))
54                 constraints->min_uA = pval;
55         if (!of_property_read_u32(np, "regulator-max-microamp", &pval))
56                 constraints->max_uA = pval;
57
58         if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
59                                   &pval))
60                 constraints->ilim_uA = pval;
61
62         /* Current change possible? */
63         if (constraints->min_uA != constraints->max_uA)
64                 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
65
66         constraints->boot_on = of_property_read_bool(np, "regulator-boot-on");
67         constraints->always_on = of_property_read_bool(np, "regulator-always-on");
68         if (!constraints->always_on) /* status change should be possible. */
69                 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
70
71         constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
72
73         if (of_property_read_bool(np, "regulator-allow-bypass"))
74                 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
75
76         if (of_property_read_bool(np, "regulator-allow-set-load"))
77                 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
78
79         ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
80         if (!ret) {
81                 if (pval)
82                         constraints->ramp_delay = pval;
83                 else
84                         constraints->ramp_disable = true;
85         }
86
87         ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
88         if (!ret)
89                 constraints->settling_time = pval;
90
91         ret = of_property_read_u32(np, "regulator-settling-time-up-us", &pval);
92         if (!ret)
93                 constraints->settling_time_up = pval;
94         if (constraints->settling_time_up && constraints->settling_time) {
95                 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
96                         np);
97                 constraints->settling_time_up = 0;
98         }
99
100         ret = of_property_read_u32(np, "regulator-settling-time-down-us",
101                                    &pval);
102         if (!ret)
103                 constraints->settling_time_down = pval;
104         if (constraints->settling_time_down && constraints->settling_time) {
105                 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
106                         np);
107                 constraints->settling_time_down = 0;
108         }
109
110         ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
111         if (!ret)
112                 constraints->enable_time = pval;
113
114         constraints->soft_start = of_property_read_bool(np,
115                                         "regulator-soft-start");
116         ret = of_property_read_u32(np, "regulator-active-discharge", &pval);
117         if (!ret) {
118                 constraints->active_discharge =
119                                 (pval) ? REGULATOR_ACTIVE_DISCHARGE_ENABLE :
120                                         REGULATOR_ACTIVE_DISCHARGE_DISABLE;
121         }
122
123         if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
124                 if (desc && desc->of_map_mode) {
125                         mode = desc->of_map_mode(pval);
126                         if (mode == REGULATOR_MODE_INVALID)
127                                 pr_err("%pOFn: invalid mode %u\n", np, pval);
128                         else
129                                 constraints->initial_mode = mode;
130                 } else {
131                         pr_warn("%pOFn: mapping for mode %d not defined\n",
132                                 np, pval);
133                 }
134         }
135
136         len = of_property_count_elems_of_size(np, "regulator-allowed-modes",
137                                                 sizeof(u32));
138         if (len > 0) {
139                 if (desc && desc->of_map_mode) {
140                         for (i = 0; i < len; i++) {
141                                 ret = of_property_read_u32_index(np,
142                                         "regulator-allowed-modes", i, &pval);
143                                 if (ret) {
144                                         pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
145                                                 np, i, ret);
146                                         break;
147                                 }
148                                 mode = desc->of_map_mode(pval);
149                                 if (mode == REGULATOR_MODE_INVALID)
150                                         pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
151                                                 np, pval);
152                                 else
153                                         constraints->valid_modes_mask |= mode;
154                         }
155                         if (constraints->valid_modes_mask)
156                                 constraints->valid_ops_mask
157                                         |= REGULATOR_CHANGE_MODE;
158                 } else {
159                         pr_warn("%pOFn: mode mapping not defined\n", np);
160                 }
161         }
162
163         if (!of_property_read_u32(np, "regulator-system-load", &pval))
164                 constraints->system_load = pval;
165
166         if (!of_property_read_u32(np, "regulator-coupled-max-spread",
167                                   &pval))
168                 constraints->max_spread = pval;
169
170         if (!of_property_read_u32(np, "regulator-max-step-microvolt",
171                                   &pval))
172                 constraints->max_uV_step = pval;
173
174         constraints->over_current_protection = of_property_read_bool(np,
175                                         "regulator-over-current-protection");
176
177         for (i = 0; i < ARRAY_SIZE(regulator_states); i++) {
178                 switch (i) {
179                 case PM_SUSPEND_MEM:
180                         suspend_state = &constraints->state_mem;
181                         break;
182                 case PM_SUSPEND_MAX:
183                         suspend_state = &constraints->state_disk;
184                         break;
185                 case PM_SUSPEND_STANDBY:
186                         suspend_state = &constraints->state_standby;
187                         break;
188                 case PM_SUSPEND_ON:
189                 case PM_SUSPEND_TO_IDLE:
190                 default:
191                         continue;
192                 }
193
194                 suspend_np = of_get_child_by_name(np, regulator_states[i]);
195                 if (!suspend_np || !suspend_state)
196                         continue;
197
198                 if (!of_property_read_u32(suspend_np, "regulator-mode",
199                                           &pval)) {
200                         if (desc && desc->of_map_mode) {
201                                 mode = desc->of_map_mode(pval);
202                                 if (mode == REGULATOR_MODE_INVALID)
203                                         pr_err("%pOFn: invalid mode %u\n",
204                                                np, pval);
205                                 else
206                                         suspend_state->mode = mode;
207                         } else {
208                                 pr_warn("%pOFn: mapping for mode %d not defined\n",
209                                         np, pval);
210                         }
211                 }
212
213                 if (of_property_read_bool(suspend_np,
214                                         "regulator-on-in-suspend"))
215                         suspend_state->enabled = ENABLE_IN_SUSPEND;
216                 else if (of_property_read_bool(suspend_np,
217                                         "regulator-off-in-suspend"))
218                         suspend_state->enabled = DISABLE_IN_SUSPEND;
219
220                 if (!of_property_read_u32(np, "regulator-suspend-min-microvolt",
221                                           &pval))
222                         suspend_state->min_uV = pval;
223
224                 if (!of_property_read_u32(np, "regulator-suspend-max-microvolt",
225                                           &pval))
226                         suspend_state->max_uV = pval;
227
228                 if (!of_property_read_u32(suspend_np,
229                                         "regulator-suspend-microvolt", &pval))
230                         suspend_state->uV = pval;
231                 else /* otherwise use min_uV as default suspend voltage */
232                         suspend_state->uV = suspend_state->min_uV;
233
234                 if (of_property_read_bool(suspend_np,
235                                         "regulator-changeable-in-suspend"))
236                         suspend_state->changeable = true;
237
238                 if (i == PM_SUSPEND_MEM)
239                         constraints->initial_state = PM_SUSPEND_MEM;
240
241                 of_node_put(suspend_np);
242                 suspend_state = NULL;
243                 suspend_np = NULL;
244         }
245 }
246
247 /**
248  * of_get_regulator_init_data - extract regulator_init_data structure info
249  * @dev: device requesting for regulator_init_data
250  * @node: regulator device node
251  * @desc: regulator description
252  *
253  * Populates regulator_init_data structure by extracting data from device
254  * tree node, returns a pointer to the populated structure or NULL if memory
255  * alloc fails.
256  */
257 struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
258                                           struct device_node *node,
259                                           const struct regulator_desc *desc)
260 {
261         struct regulator_init_data *init_data;
262
263         if (!node)
264                 return NULL;
265
266         init_data = devm_kzalloc(dev, sizeof(*init_data), GFP_KERNEL);
267         if (!init_data)
268                 return NULL; /* Out of memory? */
269
270         of_get_regulation_constraints(node, &init_data, desc);
271         return init_data;
272 }
273 EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
274
275 struct devm_of_regulator_matches {
276         struct of_regulator_match *matches;
277         unsigned int num_matches;
278 };
279
280 static void devm_of_regulator_put_matches(struct device *dev, void *res)
281 {
282         struct devm_of_regulator_matches *devm_matches = res;
283         int i;
284
285         for (i = 0; i < devm_matches->num_matches; i++)
286                 of_node_put(devm_matches->matches[i].of_node);
287 }
288
289 /**
290  * of_regulator_match - extract multiple regulator init data from device tree.
291  * @dev: device requesting the data
292  * @node: parent device node of the regulators
293  * @matches: match table for the regulators
294  * @num_matches: number of entries in match table
295  *
296  * This function uses a match table specified by the regulator driver to
297  * parse regulator init data from the device tree. @node is expected to
298  * contain a set of child nodes, each providing the init data for one
299  * regulator. The data parsed from a child node will be matched to a regulator
300  * based on either the deprecated property regulator-compatible if present,
301  * or otherwise the child node's name. Note that the match table is modified
302  * in place and an additional of_node reference is taken for each matched
303  * regulator.
304  *
305  * Returns the number of matches found or a negative error code on failure.
306  */
307 int of_regulator_match(struct device *dev, struct device_node *node,
308                        struct of_regulator_match *matches,
309                        unsigned int num_matches)
310 {
311         unsigned int count = 0;
312         unsigned int i;
313         const char *name;
314         struct device_node *child;
315         struct devm_of_regulator_matches *devm_matches;
316
317         if (!dev || !node)
318                 return -EINVAL;
319
320         devm_matches = devres_alloc(devm_of_regulator_put_matches,
321                                     sizeof(struct devm_of_regulator_matches),
322                                     GFP_KERNEL);
323         if (!devm_matches)
324                 return -ENOMEM;
325
326         devm_matches->matches = matches;
327         devm_matches->num_matches = num_matches;
328
329         devres_add(dev, devm_matches);
330
331         for (i = 0; i < num_matches; i++) {
332                 struct of_regulator_match *match = &matches[i];
333                 match->init_data = NULL;
334                 match->of_node = NULL;
335         }
336
337         for_each_child_of_node(node, child) {
338                 name = of_get_property(child,
339                                         "regulator-compatible", NULL);
340                 if (!name)
341                         name = child->name;
342                 for (i = 0; i < num_matches; i++) {
343                         struct of_regulator_match *match = &matches[i];
344                         if (match->of_node)
345                                 continue;
346
347                         if (strcmp(match->name, name))
348                                 continue;
349
350                         match->init_data =
351                                 of_get_regulator_init_data(dev, child,
352                                                            match->desc);
353                         if (!match->init_data) {
354                                 dev_err(dev,
355                                         "failed to parse DT for regulator %pOFn\n",
356                                         child);
357                                 of_node_put(child);
358                                 return -EINVAL;
359                         }
360                         match->of_node = of_node_get(child);
361                         count++;
362                         break;
363                 }
364         }
365
366         return count;
367 }
368 EXPORT_SYMBOL_GPL(of_regulator_match);
369
370 static struct
371 device_node *regulator_of_get_init_node(struct device *dev,
372                                         const struct regulator_desc *desc)
373 {
374         struct device_node *search, *child;
375         const char *name;
376
377         if (!dev->of_node || !desc->of_match)
378                 return NULL;
379
380         if (desc->regulators_node) {
381                 search = of_get_child_by_name(dev->of_node,
382                                               desc->regulators_node);
383         } else {
384                 search = of_node_get(dev->of_node);
385
386                 if (!strcmp(desc->of_match, search->name))
387                         return search;
388         }
389
390         if (!search) {
391                 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
392                         desc->regulators_node);
393                 return NULL;
394         }
395
396         for_each_available_child_of_node(search, child) {
397                 name = of_get_property(child, "regulator-compatible", NULL);
398                 if (!name)
399                         name = child->name;
400
401                 if (!strcmp(desc->of_match, name))
402                         return of_node_get(child);
403         }
404
405         of_node_put(search);
406
407         return NULL;
408 }
409
410 struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
411                                             const struct regulator_desc *desc,
412                                             struct regulator_config *config,
413                                             struct device_node **node)
414 {
415         struct device_node *child;
416         struct regulator_init_data *init_data = NULL;
417
418         child = regulator_of_get_init_node(dev, desc);
419         if (!child)
420                 return NULL;
421
422         init_data = of_get_regulator_init_data(dev, child, desc);
423         if (!init_data) {
424                 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
425                 goto error;
426         }
427
428         if (desc->of_parse_cb && desc->of_parse_cb(child, desc, config)) {
429                 dev_err(dev,
430                         "driver callback failed to parse DT for regulator %pOFn\n",
431                         child);
432                 goto error;
433         }
434
435         *node = child;
436
437         return init_data;
438
439 error:
440         of_node_put(child);
441
442         return NULL;
443 }
444
445 static int of_node_match(struct device *dev, const void *data)
446 {
447         return dev->of_node == data;
448 }
449
450 struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
451 {
452         struct device *dev;
453
454         dev = class_find_device(&regulator_class, NULL, np, of_node_match);
455
456         return dev ? dev_to_rdev(dev) : NULL;
457 }
458
459 /*
460  * Returns number of regulators coupled with rdev.
461  */
462 int of_get_n_coupled(struct regulator_dev *rdev)
463 {
464         struct device_node *node = rdev->dev.of_node;
465         int n_phandles;
466
467         n_phandles = of_count_phandle_with_args(node,
468                                                 "regulator-coupled-with",
469                                                 NULL);
470
471         return (n_phandles > 0) ? n_phandles : 0;
472 }
473
474 /* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
475 static bool of_coupling_find_node(struct device_node *src,
476                                   struct device_node *to_find)
477 {
478         int n_phandles, i;
479         bool found = false;
480
481         n_phandles = of_count_phandle_with_args(src,
482                                                 "regulator-coupled-with",
483                                                 NULL);
484
485         for (i = 0; i < n_phandles; i++) {
486                 struct device_node *tmp = of_parse_phandle(src,
487                                            "regulator-coupled-with", i);
488
489                 if (!tmp)
490                         break;
491
492                 /* found */
493                 if (tmp == to_find)
494                         found = true;
495
496                 of_node_put(tmp);
497
498                 if (found)
499                         break;
500         }
501
502         return found;
503 }
504
505 /**
506  * of_check_coupling_data - Parse rdev's coupling properties and check data
507  *                          consistency
508  * @rdev - pointer to regulator_dev whose data is checked
509  *
510  * Function checks if all the following conditions are met:
511  * - rdev's max_spread is greater than 0
512  * - all coupled regulators have the same max_spread
513  * - all coupled regulators have the same number of regulator_dev phandles
514  * - all regulators are linked to each other
515  *
516  * Returns true if all conditions are met.
517  */
518 bool of_check_coupling_data(struct regulator_dev *rdev)
519 {
520         int max_spread = rdev->constraints->max_spread;
521         struct device_node *node = rdev->dev.of_node;
522         int n_phandles = of_get_n_coupled(rdev);
523         struct device_node *c_node;
524         int i;
525         bool ret = true;
526
527         if (max_spread <= 0) {
528                 dev_err(&rdev->dev, "max_spread value invalid\n");
529                 return false;
530         }
531
532         /* iterate over rdev's phandles */
533         for (i = 0; i < n_phandles; i++) {
534                 int c_max_spread, c_n_phandles;
535
536                 c_node = of_parse_phandle(node,
537                                           "regulator-coupled-with", i);
538
539                 if (!c_node)
540                         ret = false;
541
542                 c_n_phandles = of_count_phandle_with_args(c_node,
543                                                           "regulator-coupled-with",
544                                                           NULL);
545
546                 if (c_n_phandles != n_phandles) {
547                         dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
548                         ret = false;
549                         goto clean;
550                 }
551
552                 if (of_property_read_u32(c_node, "regulator-coupled-max-spread",
553                                          &c_max_spread)) {
554                         ret = false;
555                         goto clean;
556                 }
557
558                 if (c_max_spread != max_spread) {
559                         dev_err(&rdev->dev,
560                                 "coupled regulators max_spread mismatch\n");
561                         ret = false;
562                         goto clean;
563                 }
564
565                 if (!of_coupling_find_node(c_node, node)) {
566                         dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
567                         ret = false;
568                 }
569
570 clean:
571                 of_node_put(c_node);
572                 if (!ret)
573                         break;
574         }
575
576         return ret;
577 }
578
579 /**
580  * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
581  * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
582  *        "regulator-coupled-with" property
583  * @index: Index in phandles array
584  *
585  * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
586  * registered, returns NULL
587  */
588 struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
589                                                  int index)
590 {
591         struct device_node *node = rdev->dev.of_node;
592         struct device_node *c_node;
593         struct regulator_dev *c_rdev;
594
595         c_node = of_parse_phandle(node, "regulator-coupled-with", index);
596         if (!c_node)
597                 return NULL;
598
599         c_rdev = of_find_regulator_by_node(c_node);
600
601         of_node_put(c_node);
602
603         return c_rdev;
604 }