Linux 5.2-rc1
[linux-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
7a67eb1d
Y
374static struct
375device_node *regulator_of_get_init_node(struct device *dev,
376 const struct regulator_desc *desc)
a0c7b164
MB
377{
378 struct device_node *search, *child;
a0c7b164
MB
379 const char *name;
380
381 if (!dev->of_node || !desc->of_match)
382 return NULL;
383
eba9473f 384 if (desc->regulators_node) {
a0c7b164
MB
385 search = of_get_child_by_name(dev->of_node,
386 desc->regulators_node);
eba9473f 387 } else {
423a1164 388 search = of_node_get(dev->of_node);
a0c7b164 389
eba9473f
CK
390 if (!strcmp(desc->of_match, search->name))
391 return search;
392 }
393
a0c7b164 394 if (!search) {
7de79a1d
MB
395 dev_dbg(dev, "Failed to find regulator container node '%s'\n",
396 desc->regulators_node);
a0c7b164
MB
397 return NULL;
398 }
399
130daa3f 400 for_each_available_child_of_node(search, child) {
a0c7b164
MB
401 name = of_get_property(child, "regulator-compatible", NULL);
402 if (!name)
403 name = child->name;
404
925c85e2
CK
405 if (!strcmp(desc->of_match, name))
406 return of_node_get(child);
407 }
a0c7b164 408
925c85e2 409 of_node_put(search);
a0c7b164 410
925c85e2
CK
411 return NULL;
412}
bfa21a0d 413
925c85e2
CK
414struct regulator_init_data *regulator_of_get_init_data(struct device *dev,
415 const struct regulator_desc *desc,
416 struct regulator_config *config,
417 struct device_node **node)
418{
419 struct device_node *child;
420 struct regulator_init_data *init_data = NULL;
421
422 child = regulator_of_get_init_node(dev, desc);
423 if (!child)
424 return NULL;
425
426 init_data = of_get_regulator_init_data(dev, child, desc);
427 if (!init_data) {
428 dev_err(dev, "failed to parse DT for regulator %pOFn\n", child);
429 goto error;
a0c7b164
MB
430 }
431
925c85e2
CK
432 if (desc->of_parse_cb && desc->of_parse_cb(child, desc, config)) {
433 dev_err(dev,
434 "driver callback failed to parse DT for regulator %pOFn\n",
435 child);
436 goto error;
437 }
438
439 *node = child;
a0c7b164
MB
440
441 return init_data;
925c85e2
CK
442
443error:
444 of_node_put(child);
445
446 return NULL;
a0c7b164 447}
148096af
MP
448
449static int of_node_match(struct device *dev, const void *data)
450{
451 return dev->of_node == data;
452}
453
454struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
455{
456 struct device *dev;
457
458 dev = class_find_device(&regulator_class, NULL, np, of_node_match);
459
460 return dev ? dev_to_rdev(dev) : NULL;
461}
a085a31a
MP
462
463/*
464 * Returns number of regulators coupled with rdev.
465 */
466int of_get_n_coupled(struct regulator_dev *rdev)
467{
468 struct device_node *node = rdev->dev.of_node;
469 int n_phandles;
470
471 n_phandles = of_count_phandle_with_args(node,
472 "regulator-coupled-with",
473 NULL);
474
475 return (n_phandles > 0) ? n_phandles : 0;
476}
477
478/* Looks for "to_find" device_node in src's "regulator-coupled-with" property */
479static bool of_coupling_find_node(struct device_node *src,
480 struct device_node *to_find)
481{
482 int n_phandles, i;
483 bool found = false;
484
485 n_phandles = of_count_phandle_with_args(src,
486 "regulator-coupled-with",
487 NULL);
488
489 for (i = 0; i < n_phandles; i++) {
490 struct device_node *tmp = of_parse_phandle(src,
491 "regulator-coupled-with", i);
492
493 if (!tmp)
494 break;
495
496 /* found */
497 if (tmp == to_find)
498 found = true;
499
500 of_node_put(tmp);
501
502 if (found)
503 break;
504 }
505
506 return found;
507}
508
509/**
510 * of_check_coupling_data - Parse rdev's coupling properties and check data
511 * consistency
512 * @rdev - pointer to regulator_dev whose data is checked
513 *
514 * Function checks if all the following conditions are met:
515 * - rdev's max_spread is greater than 0
516 * - all coupled regulators have the same max_spread
517 * - all coupled regulators have the same number of regulator_dev phandles
518 * - all regulators are linked to each other
519 *
520 * Returns true if all conditions are met.
521 */
522bool of_check_coupling_data(struct regulator_dev *rdev)
523{
524 int max_spread = rdev->constraints->max_spread;
525 struct device_node *node = rdev->dev.of_node;
526 int n_phandles = of_get_n_coupled(rdev);
527 struct device_node *c_node;
528 int i;
529 bool ret = true;
530
531 if (max_spread <= 0) {
532 dev_err(&rdev->dev, "max_spread value invalid\n");
533 return false;
534 }
535
536 /* iterate over rdev's phandles */
537 for (i = 0; i < n_phandles; i++) {
538 int c_max_spread, c_n_phandles;
539
540 c_node = of_parse_phandle(node,
541 "regulator-coupled-with", i);
542
543 if (!c_node)
544 ret = false;
545
546 c_n_phandles = of_count_phandle_with_args(c_node,
547 "regulator-coupled-with",
548 NULL);
549
550 if (c_n_phandles != n_phandles) {
48f1b4ef 551 dev_err(&rdev->dev, "number of coupled reg phandles mismatch\n");
a085a31a
MP
552 ret = false;
553 goto clean;
554 }
555
556 if (of_property_read_u32(c_node, "regulator-coupled-max-spread",
557 &c_max_spread)) {
558 ret = false;
559 goto clean;
560 }
561
562 if (c_max_spread != max_spread) {
563 dev_err(&rdev->dev,
564 "coupled regulators max_spread mismatch\n");
565 ret = false;
566 goto clean;
567 }
568
569 if (!of_coupling_find_node(c_node, node)) {
570 dev_err(&rdev->dev, "missing 2-way linking for coupled regulators\n");
571 ret = false;
572 }
573
574clean:
575 of_node_put(c_node);
576 if (!ret)
577 break;
578 }
579
580 return ret;
581}
582
583/**
584 * of_parse_coupled regulator - Get regulator_dev pointer from rdev's property
585 * @rdev: Pointer to regulator_dev, whose DTS is used as a source to parse
586 * "regulator-coupled-with" property
587 * @index: Index in phandles array
588 *
589 * Returns the regulator_dev pointer parsed from DTS. If it has not been yet
590 * registered, returns NULL
591 */
592struct regulator_dev *of_parse_coupled_regulator(struct regulator_dev *rdev,
593 int index)
594{
595 struct device_node *node = rdev->dev.of_node;
596 struct device_node *c_node;
597 struct regulator_dev *c_rdev;
598
599 c_node = of_parse_phandle(node, "regulator-coupled-with", index);
600 if (!c_node)
601 return NULL;
602
603 c_rdev = of_find_regulator_by_node(c_node);
604
605 of_node_put(c_node);
606
607 return c_rdev;
608}