OSDN Git Service

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