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