Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-2.6-block.git] / drivers / regulator / of_regulator.c
CommitLineData
8f446e6f
RN
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
e69af5e9 13#include <linux/module.h>
8f446e6f
RN
14#include <linux/slab.h>
15#include <linux/of.h>
16#include <linux/regulator/machine.h>
a0c7b164 17#include <linux/regulator/driver.h>
1c8fa58f 18#include <linux/regulator/of_regulator.h>
8f446e6f 19
a0c7b164
MB
20#include "internal.h"
21
f32fa89c 22static const char *const regulator_states[PM_SUSPEND_MAX + 1] = {
f2b40769 23 [PM_SUSPEND_STANDBY] = "regulator-state-standby",
40e20d68
CC
24 [PM_SUSPEND_MEM] = "regulator-state-mem",
25 [PM_SUSPEND_MAX] = "regulator-state-disk",
26};
27
8f446e6f 28static void of_get_regulation_constraints(struct device_node *np,
5e5e3a42
JMC
29 struct regulator_init_data **init_data,
30 const struct regulator_desc *desc)
8f446e6f 31{
8f446e6f 32 struct regulation_constraints *constraints = &(*init_data)->constraints;
40e20d68
CC
33 struct regulator_state *suspend_state;
34 struct device_node *suspend_np;
02f37039 35 unsigned int mode;
54557ad9 36 int ret, i, len;
00c877c6 37 u32 pval;
8f446e6f
RN
38
39 constraints->name = of_get_property(np, "regulator-name", NULL);
40
a34785f1
LD
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;
8f446e6f
RN
46
47 /* Voltage change possible? */
45fa2038 48 if (constraints->min_uV != constraints->max_uV)
8f446e6f 49 constraints->valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE;
45fa2038
MB
50
51 /* Do we have a voltage range, if so try to apply it? */
52 if (constraints->min_uV && constraints->max_uV)
ab62aa93 53 constraints->apply_uV = true;
8f446e6f 54
1e050eab
SS
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;
8f446e6f 61
36e4f839
SB
62 if (!of_property_read_u32(np, "regulator-input-current-limit-microamp",
63 &pval))
64 constraints->ilim_uA = pval;
65
8f446e6f
RN
66 /* Current change possible? */
67 if (constraints->min_uA != constraints->max_uA)
68 constraints->valid_ops_mask |= REGULATOR_CHANGE_CURRENT;
69
1e050eab
SS
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. */
8f446e6f 73 constraints->valid_ops_mask |= REGULATOR_CHANGE_STATUS;
6f0b2c69 74
23c779b9
SB
75 constraints->pull_down = of_property_read_bool(np, "regulator-pull-down");
76
93134c7b
KVA
77 if (of_property_read_bool(np, "regulator-allow-bypass"))
78 constraints->valid_ops_mask |= REGULATOR_CHANGE_BYPASS;
79
b263d203
BA
80 if (of_property_read_bool(np, "regulator-allow-set-load"))
81 constraints->valid_ops_mask |= REGULATOR_CHANGE_DRMS;
82
1e050eab
SS
83 ret = of_property_read_u32(np, "regulator-ramp-delay", &pval);
84 if (!ret) {
85 if (pval)
86 constraints->ramp_delay = pval;
1653ccf4
YSB
87 else
88 constraints->ramp_disable = true;
89 }
00c877c6 90
d6c1dc3f
LD
91 ret = of_property_read_u32(np, "regulator-settling-time-us", &pval);
92 if (!ret)
93 constraints->settling_time = pval;
94
3ffad468
MK
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) {
0c9721a5
RH
99 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-up-us'\n",
100 np);
3ffad468
MK
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) {
0c9721a5
RH
109 pr_warn("%pOFn: ambiguous configuration for settling time, ignoring 'regulator-settling-time-down-us'\n",
110 np);
3ffad468
MK
111 constraints->settling_time_down = 0;
112 }
113
00c877c6
LD
114 ret = of_property_read_u32(np, "regulator-enable-ramp-delay", &pval);
115 if (!ret)
116 constraints->enable_time = pval;
40e20d68 117
57f66b78
SB
118 constraints->soft_start = of_property_read_bool(np,
119 "regulator-soft-start");
670666b9
LD
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 }
57f66b78 126
5e5e3a42
JMC
127 if (!of_property_read_u32(np, "regulator-initial-mode", &pval)) {
128 if (desc && desc->of_map_mode) {
02f37039
DA
129 mode = desc->of_map_mode(pval);
130 if (mode == REGULATOR_MODE_INVALID)
0c9721a5 131 pr_err("%pOFn: invalid mode %u\n", np, pval);
5e5e3a42 132 else
02f37039 133 constraints->initial_mode = mode;
5e5e3a42 134 } else {
0c9721a5
RH
135 pr_warn("%pOFn: mapping for mode %d not defined\n",
136 np, pval);
5e5e3a42
JMC
137 }
138 }
139
54557ad9
DC
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) {
0c9721a5
RH
148 pr_err("%pOFn: couldn't read allowed modes index %d, ret=%d\n",
149 np, i, ret);
54557ad9
DC
150 break;
151 }
152 mode = desc->of_map_mode(pval);
153 if (mode == REGULATOR_MODE_INVALID)
0c9721a5
RH
154 pr_err("%pOFn: invalid regulator-allowed-modes element %u\n",
155 np, pval);
54557ad9
DC
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 {
0c9721a5 163 pr_warn("%pOFn: mode mapping not defined\n", np);
54557ad9
DC
164 }
165 }
166
22a10bca
SB
167 if (!of_property_read_u32(np, "regulator-system-load", &pval))
168 constraints->system_load = pval;
169
a085a31a
MP
170 if (!of_property_read_u32(np, "regulator-coupled-max-spread",
171 &pval))
172 constraints->max_spread = pval;
173
85254bcf
DO
174 if (!of_property_read_u32(np, "regulator-max-step-microvolt",
175 &pval))
176 constraints->max_uV_step = pval;
177
3a003bae
SB
178 constraints->over_current_protection = of_property_read_bool(np,
179 "regulator-over-current-protection");
180
40e20d68
CC
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;
f2b40769
AS
189 case PM_SUSPEND_STANDBY:
190 suspend_state = &constraints->state_standby;
191 break;
40e20d68 192 case PM_SUSPEND_ON:
690cbb90 193 case PM_SUSPEND_TO_IDLE:
40e20d68
CC
194 default:
195 continue;
7cf225b9 196 }
40e20d68
CC
197
198 suspend_np = of_get_child_by_name(np, regulator_states[i]);
199 if (!suspend_np || !suspend_state)
200 continue;
201
5e5e3a42
JMC
202 if (!of_property_read_u32(suspend_np, "regulator-mode",
203 &pval)) {
204 if (desc && desc->of_map_mode) {
02f37039
DA
205 mode = desc->of_map_mode(pval);
206 if (mode == REGULATOR_MODE_INVALID)
0c9721a5
RH
207 pr_err("%pOFn: invalid mode %u\n",
208 np, pval);
5e5e3a42 209 else
02f37039 210 suspend_state->mode = mode;
5e5e3a42 211 } else {
0c9721a5
RH
212 pr_warn("%pOFn: mapping for mode %d not defined\n",
213 np, pval);
5e5e3a42
JMC
214 }
215 }
216
40e20d68
CC
217 if (of_property_read_bool(suspend_np,
218 "regulator-on-in-suspend"))
72069f99 219 suspend_state->enabled = ENABLE_IN_SUSPEND;
40e20d68
CC
220 else if (of_property_read_bool(suspend_np,
221 "regulator-off-in-suspend"))
72069f99 222 suspend_state->enabled = DISABLE_IN_SUSPEND;
40e20d68 223
f7efad10
CZ
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;
40e20d68 231
8cbcaea8
DA
232 if (!of_property_read_u32(suspend_np,
233 "regulator-suspend-microvolt", &pval))
234 suspend_state->uV = pval;
f7efad10
CZ
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;
8cbcaea8 241
a0f78bc8
K
242 if (i == PM_SUSPEND_MEM)
243 constraints->initial_state = PM_SUSPEND_MEM;
244
4eafec83 245 of_node_put(suspend_np);
40e20d68
CC
246 suspend_state = NULL;
247 suspend_np = NULL;
248 }
8f446e6f
RN
249}
250
251/**
252 * of_get_regulator_init_data - extract regulator_init_data structure info
253 * @dev: device requesting for regulator_init_data
072e78b1
JMC
254 * @node: regulator device node
255 * @desc: regulator description
8f446e6f
RN
256 *
257 * Populates regulator_init_data structure by extracting data from device
48f1b4ef 258 * tree node, returns a pointer to the populated structure or NULL if memory
8f446e6f
RN
259 * alloc fails.
260 */
d9a861cc 261struct regulator_init_data *of_get_regulator_init_data(struct device *dev,
072e78b1
JMC
262 struct device_node *node,
263 const struct regulator_desc *desc)
8f446e6f
RN
264{
265 struct regulator_init_data *init_data;
266
d9a861cc 267 if (!node)
8f446e6f
RN
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
5e5e3a42 274 of_get_regulation_constraints(node, &init_data, desc);
8f446e6f
RN
275 return init_data;
276}
e69af5e9 277EXPORT_SYMBOL_GPL(of_get_regulator_init_data);
1c8fa58f 278
13b3fde8
CK
279struct devm_of_regulator_matches {
280 struct of_regulator_match *matches;
281 unsigned int num_matches;
282};
283
284static 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
1c8fa58f 293/**
13511def 294 * of_regulator_match - extract multiple regulator init data from device tree.
1c8fa58f
TR
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 *
13511def
SW
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
bd0dda74
CK
306 * in place and an additional of_node reference is taken for each matched
307 * regulator.
1c8fa58f
TR
308 *
309 * Returns the number of matches found or a negative error code on failure.
310 */
311int 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;
13511def 317 const char *name;
5260cd2b 318 struct device_node *child;
13b3fde8 319 struct devm_of_regulator_matches *devm_matches;
1c8fa58f
TR
320
321 if (!dev || !node)
322 return -EINVAL;
323
13b3fde8
CK
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
a2f95c36
SW
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
5260cd2b 341 for_each_child_of_node(node, child) {
13511def 342 name = of_get_property(child,
5260cd2b 343 "regulator-compatible", NULL);
13511def
SW
344 if (!name)
345 name = child->name;
5260cd2b
LD
346 for (i = 0; i < num_matches; i++) {
347 struct of_regulator_match *match = &matches[i];
348 if (match->of_node)
349 continue;
350
13511def 351 if (strcmp(match->name, name))
5260cd2b
LD
352 continue;
353
354 match->init_data =
75d6b2fa
JMC
355 of_get_regulator_init_data(dev, child,
356 match->desc);
5260cd2b
LD
357 if (!match->init_data) {
358 dev_err(dev,
0c9721a5
RH
359 "failed to parse DT for regulator %pOFn\n",
360 child);
30966861 361 of_node_put(child);
5260cd2b
LD
362 return -EINVAL;
363 }
bd0dda74 364 match->of_node = of_node_get(child);
5260cd2b
LD
365 count++;
366 break;
1c8fa58f 367 }
1c8fa58f
TR
368 }
369
370 return count;
371}
372EXPORT_SYMBOL_GPL(of_regulator_match);
a0c7b164 373
925c85e2
CK
374struct device_node *regulator_of_get_init_node(struct device *dev,
375 const struct regulator_desc *desc)
a0c7b164
MB
376{
377 struct device_node *search, *child;
a0c7b164
MB
378 const char *name;
379
380 if (!dev->of_node || !desc->of_match)
381 return NULL;
382
eba9473f 383 if (desc->regulators_node) {
a0c7b164
MB
384 search = of_get_child_by_name(dev->of_node,
385 desc->regulators_node);
eba9473f 386 } else {
423a1164 387 search = of_node_get(dev->of_node);
a0c7b164 388
eba9473f
CK
389 if (!strcmp(desc->of_match, search->name))
390 return search;
391 }
392
a0c7b164 393 if (!search) {
7de79a1d
MB
394 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
395 desc->regulators_node);
a0c7b164
MB
396 return NULL;
397 }
398
130daa3f 399 for_each_available_child_of_node(search, child) {
a0c7b164
MB
400 name = of_get_property(child, "regulator-compatible", NULL);
401 if (!name)
402 name = child->name;
403
925c85e2
CK
404 if (!strcmp(desc->of_match, name))
405 return of_node_get(child);
406 }
a0c7b164 407
925c85e2 408 of_node_put(search);
a0c7b164 409
925c85e2
CK
410 return NULL;
411}
bfa21a0d 412
925c85e2
CK
413struct 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;
a0c7b164
MB
429 }
430
925c85e2
CK
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;
a0c7b164
MB
439
440 return init_data;
925c85e2
CK
441
442error:
443 of_node_put(child);
444
445 return NULL;
a0c7b164 446}
148096af
MP
447
448static int of_node_match(struct device *dev, const void *data)
449{
450 return dev->of_node == data;
451}
452
453struct 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}
a085a31a
MP
461
462/*
463 * Returns number of regulators coupled with rdev.
464 */
465int 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 */
478static 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 */
521bool 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) {
48f1b4ef 550 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
a085a31a
MP
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
573clean:
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 */
591struct 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}