Commit | Line | Data |
---|---|---|
414c70cb LG |
1 | /* |
2 | * core.c -- Voltage/Current Regulator framework. | |
3 | * | |
4 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. | |
a5766f11 | 5 | * Copyright 2008 SlimLogic Ltd. |
414c70cb | 6 | * |
a5766f11 | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
414c70cb LG |
8 | * |
9 | * This program is free software; you can redistribute it and/or modify it | |
10 | * under the terms of the GNU General Public License as published by the | |
11 | * Free Software Foundation; either version 2 of the License, or (at your | |
12 | * option) any later version. | |
13 | * | |
14 | */ | |
15 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/init.h> | |
1130e5b3 | 18 | #include <linux/debugfs.h> |
414c70cb | 19 | #include <linux/device.h> |
5a0e3ad6 | 20 | #include <linux/slab.h> |
f21e0e81 | 21 | #include <linux/async.h> |
414c70cb LG |
22 | #include <linux/err.h> |
23 | #include <linux/mutex.h> | |
24 | #include <linux/suspend.h> | |
31aae2be | 25 | #include <linux/delay.h> |
65f73508 | 26 | #include <linux/gpio.h> |
778b28b4 | 27 | #include <linux/gpio/consumer.h> |
69511a45 | 28 | #include <linux/of.h> |
65b19ce6 | 29 | #include <linux/regmap.h> |
69511a45 | 30 | #include <linux/regulator/of_regulator.h> |
414c70cb LG |
31 | #include <linux/regulator/consumer.h> |
32 | #include <linux/regulator/driver.h> | |
33 | #include <linux/regulator/machine.h> | |
65602c32 | 34 | #include <linux/module.h> |
414c70cb | 35 | |
02fa3ec0 MB |
36 | #define CREATE_TRACE_POINTS |
37 | #include <trace/events/regulator.h> | |
38 | ||
34abbd68 | 39 | #include "dummy.h" |
0cdfcc0f | 40 | #include "internal.h" |
34abbd68 | 41 | |
7d51a0db MB |
42 | #define rdev_crit(rdev, fmt, ...) \ |
43 | pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
5da84fd9 JP |
44 | #define rdev_err(rdev, fmt, ...) \ |
45 | pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
46 | #define rdev_warn(rdev, fmt, ...) \ | |
47 | pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
48 | #define rdev_info(rdev, fmt, ...) \ | |
49 | pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
50 | #define rdev_dbg(rdev, fmt, ...) \ | |
51 | pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
52 | ||
414c70cb | 53 | static DEFINE_MUTEX(regulator_list_mutex); |
414c70cb | 54 | static LIST_HEAD(regulator_map_list); |
f19b00da | 55 | static LIST_HEAD(regulator_ena_gpio_list); |
a06ccd9c | 56 | static LIST_HEAD(regulator_supply_alias_list); |
21cf891a | 57 | static bool has_full_constraints; |
414c70cb | 58 | |
1130e5b3 | 59 | static struct dentry *debugfs_root; |
1130e5b3 | 60 | |
8dc5390d | 61 | /* |
414c70cb LG |
62 | * struct regulator_map |
63 | * | |
64 | * Used to provide symbolic supply names to devices. | |
65 | */ | |
66 | struct regulator_map { | |
67 | struct list_head list; | |
40f9244f | 68 | const char *dev_name; /* The dev_name() for the consumer */ |
414c70cb | 69 | const char *supply; |
a5766f11 | 70 | struct regulator_dev *regulator; |
414c70cb LG |
71 | }; |
72 | ||
f19b00da KM |
73 | /* |
74 | * struct regulator_enable_gpio | |
75 | * | |
76 | * Management for shared enable GPIO pin | |
77 | */ | |
78 | struct regulator_enable_gpio { | |
79 | struct list_head list; | |
778b28b4 | 80 | struct gpio_desc *gpiod; |
f19b00da KM |
81 | u32 enable_count; /* a number of enabled shared GPIO */ |
82 | u32 request_count; /* a number of requested shared GPIO */ | |
83 | unsigned int ena_gpio_invert:1; | |
84 | }; | |
85 | ||
a06ccd9c CK |
86 | /* |
87 | * struct regulator_supply_alias | |
88 | * | |
89 | * Used to map lookups for a supply onto an alternative device. | |
90 | */ | |
91 | struct regulator_supply_alias { | |
92 | struct list_head list; | |
93 | struct device *src_dev; | |
94 | const char *src_supply; | |
95 | struct device *alias_dev; | |
96 | const char *alias_supply; | |
97 | }; | |
98 | ||
414c70cb | 99 | static int _regulator_is_enabled(struct regulator_dev *rdev); |
3801b86a | 100 | static int _regulator_disable(struct regulator_dev *rdev); |
414c70cb LG |
101 | static int _regulator_get_voltage(struct regulator_dev *rdev); |
102 | static int _regulator_get_current_limit(struct regulator_dev *rdev); | |
103 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); | |
7179569a | 104 | static int _notifier_call_chain(struct regulator_dev *rdev, |
414c70cb | 105 | unsigned long event, void *data); |
75790251 MB |
106 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
107 | int min_uV, int max_uV); | |
3801b86a MB |
108 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
109 | struct device *dev, | |
110 | const char *supply_name); | |
36a1f1b6 | 111 | static void _regulator_put(struct regulator *regulator); |
414c70cb | 112 | |
1083c393 MB |
113 | static const char *rdev_get_name(struct regulator_dev *rdev) |
114 | { | |
115 | if (rdev->constraints && rdev->constraints->name) | |
116 | return rdev->constraints->name; | |
117 | else if (rdev->desc->name) | |
118 | return rdev->desc->name; | |
119 | else | |
120 | return ""; | |
121 | } | |
122 | ||
87b28417 MB |
123 | static bool have_full_constraints(void) |
124 | { | |
75bc9641 | 125 | return has_full_constraints || of_have_populated_dt(); |
87b28417 MB |
126 | } |
127 | ||
8a34e979 WP |
128 | static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) |
129 | { | |
130 | if (!rdev->constraints) { | |
131 | rdev_err(rdev, "no constraints\n"); | |
132 | return false; | |
133 | } | |
134 | ||
135 | if (rdev->constraints->valid_ops_mask & ops) | |
136 | return true; | |
137 | ||
138 | return false; | |
139 | } | |
140 | ||
70a7fb80 TR |
141 | static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev) |
142 | { | |
143 | if (rdev && rdev->supply) | |
144 | return rdev->supply->rdev; | |
145 | ||
146 | return NULL; | |
147 | } | |
148 | ||
66cf9a7e MP |
149 | /** |
150 | * regulator_lock_nested - lock a single regulator | |
151 | * @rdev: regulator source | |
152 | * @subclass: mutex subclass used for lockdep | |
153 | * | |
154 | * This function can be called many times by one task on | |
155 | * a single regulator and its mutex will be locked only | |
156 | * once. If a task, which is calling this function is other | |
157 | * than the one, which initially locked the mutex, it will | |
158 | * wait on mutex. | |
159 | */ | |
160 | static void regulator_lock_nested(struct regulator_dev *rdev, | |
161 | unsigned int subclass) | |
162 | { | |
163 | if (!mutex_trylock(&rdev->mutex)) { | |
164 | if (rdev->mutex_owner == current) { | |
165 | rdev->ref_cnt++; | |
166 | return; | |
167 | } | |
168 | mutex_lock_nested(&rdev->mutex, subclass); | |
169 | } | |
170 | ||
171 | rdev->ref_cnt = 1; | |
172 | rdev->mutex_owner = current; | |
173 | } | |
174 | ||
175 | static inline void regulator_lock(struct regulator_dev *rdev) | |
176 | { | |
177 | regulator_lock_nested(rdev, 0); | |
178 | } | |
179 | ||
180 | /** | |
181 | * regulator_unlock - unlock a single regulator | |
182 | * @rdev: regulator_source | |
183 | * | |
184 | * This function unlocks the mutex when the | |
185 | * reference counter reaches 0. | |
186 | */ | |
187 | static void regulator_unlock(struct regulator_dev *rdev) | |
188 | { | |
189 | if (rdev->ref_cnt != 0) { | |
190 | rdev->ref_cnt--; | |
191 | ||
192 | if (!rdev->ref_cnt) { | |
193 | rdev->mutex_owner = NULL; | |
194 | mutex_unlock(&rdev->mutex); | |
195 | } | |
196 | } | |
197 | } | |
198 | ||
38de19fa MB |
199 | /** |
200 | * regulator_lock_supply - lock a regulator and its supplies | |
201 | * @rdev: regulator source | |
202 | */ | |
203 | static void regulator_lock_supply(struct regulator_dev *rdev) | |
9f01cd4a | 204 | { |
fa731ac7 | 205 | int i; |
9f01cd4a | 206 | |
38de19fa MB |
207 | for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++) |
208 | regulator_lock_nested(rdev, i); | |
9f01cd4a SH |
209 | } |
210 | ||
211 | /** | |
38de19fa MB |
212 | * regulator_unlock_supply - unlock a regulator and its supplies |
213 | * @rdev: regulator source | |
9f01cd4a | 214 | */ |
38de19fa | 215 | static void regulator_unlock_supply(struct regulator_dev *rdev) |
9f01cd4a | 216 | { |
38de19fa | 217 | struct regulator *supply; |
9f01cd4a | 218 | |
38de19fa MB |
219 | while (1) { |
220 | regulator_unlock(rdev); | |
221 | supply = rdev->supply; | |
456e7cdf | 222 | |
38de19fa MB |
223 | if (!rdev->supply) |
224 | return; | |
9f01cd4a | 225 | |
38de19fa | 226 | rdev = supply->rdev; |
9f01cd4a SH |
227 | } |
228 | } | |
229 | ||
69511a45 RN |
230 | /** |
231 | * of_get_regulator - get a regulator device node based on supply name | |
232 | * @dev: Device pointer for the consumer (of regulator) device | |
233 | * @supply: regulator supply name | |
234 | * | |
235 | * Extract the regulator device node corresponding to the supply name. | |
167d41dc | 236 | * returns the device node corresponding to the regulator if found, else |
69511a45 RN |
237 | * returns NULL. |
238 | */ | |
239 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) | |
240 | { | |
241 | struct device_node *regnode = NULL; | |
242 | char prop_name[32]; /* 32 is max size of property name */ | |
243 | ||
244 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); | |
245 | ||
246 | snprintf(prop_name, 32, "%s-supply", supply); | |
247 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); | |
248 | ||
249 | if (!regnode) { | |
7799167b RH |
250 | dev_dbg(dev, "Looking up %s property in node %pOF failed\n", |
251 | prop_name, dev->of_node); | |
69511a45 RN |
252 | return NULL; |
253 | } | |
254 | return regnode; | |
255 | } | |
256 | ||
414c70cb LG |
257 | /* Platform voltage constraint check */ |
258 | static int regulator_check_voltage(struct regulator_dev *rdev, | |
259 | int *min_uV, int *max_uV) | |
260 | { | |
261 | BUG_ON(*min_uV > *max_uV); | |
262 | ||
8a34e979 | 263 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
7ebcf26c | 264 | rdev_err(rdev, "voltage operation not allowed\n"); |
414c70cb LG |
265 | return -EPERM; |
266 | } | |
267 | ||
268 | if (*max_uV > rdev->constraints->max_uV) | |
269 | *max_uV = rdev->constraints->max_uV; | |
270 | if (*min_uV < rdev->constraints->min_uV) | |
271 | *min_uV = rdev->constraints->min_uV; | |
272 | ||
89f425ed MB |
273 | if (*min_uV > *max_uV) { |
274 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", | |
54abd335 | 275 | *min_uV, *max_uV); |
414c70cb | 276 | return -EINVAL; |
89f425ed | 277 | } |
414c70cb LG |
278 | |
279 | return 0; | |
280 | } | |
281 | ||
f7efad10 CZ |
282 | /* return 0 if the state is valid */ |
283 | static int regulator_check_states(suspend_state_t state) | |
284 | { | |
285 | return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE); | |
286 | } | |
287 | ||
05fda3b1 TP |
288 | /* Make sure we select a voltage that suits the needs of all |
289 | * regulator consumers | |
290 | */ | |
291 | static int regulator_check_consumers(struct regulator_dev *rdev, | |
c360a6df CZ |
292 | int *min_uV, int *max_uV, |
293 | suspend_state_t state) | |
05fda3b1 TP |
294 | { |
295 | struct regulator *regulator; | |
c360a6df | 296 | struct regulator_voltage *voltage; |
05fda3b1 TP |
297 | |
298 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
c360a6df | 299 | voltage = ®ulator->voltage[state]; |
4aa922c0 MB |
300 | /* |
301 | * Assume consumers that didn't say anything are OK | |
302 | * with anything in the constraint range. | |
303 | */ | |
c360a6df | 304 | if (!voltage->min_uV && !voltage->max_uV) |
4aa922c0 MB |
305 | continue; |
306 | ||
c360a6df CZ |
307 | if (*max_uV > voltage->max_uV) |
308 | *max_uV = voltage->max_uV; | |
309 | if (*min_uV < voltage->min_uV) | |
310 | *min_uV = voltage->min_uV; | |
05fda3b1 TP |
311 | } |
312 | ||
dd8004af | 313 | if (*min_uV > *max_uV) { |
9c7b4e8a RD |
314 | rdev_err(rdev, "Restricting voltage, %u-%uuV\n", |
315 | *min_uV, *max_uV); | |
05fda3b1 | 316 | return -EINVAL; |
dd8004af | 317 | } |
05fda3b1 TP |
318 | |
319 | return 0; | |
320 | } | |
321 | ||
414c70cb LG |
322 | /* current constraint check */ |
323 | static int regulator_check_current_limit(struct regulator_dev *rdev, | |
324 | int *min_uA, int *max_uA) | |
325 | { | |
326 | BUG_ON(*min_uA > *max_uA); | |
327 | ||
8a34e979 | 328 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) { |
7ebcf26c | 329 | rdev_err(rdev, "current operation not allowed\n"); |
414c70cb LG |
330 | return -EPERM; |
331 | } | |
332 | ||
333 | if (*max_uA > rdev->constraints->max_uA) | |
334 | *max_uA = rdev->constraints->max_uA; | |
335 | if (*min_uA < rdev->constraints->min_uA) | |
336 | *min_uA = rdev->constraints->min_uA; | |
337 | ||
89f425ed MB |
338 | if (*min_uA > *max_uA) { |
339 | rdev_err(rdev, "unsupportable current range: %d-%duA\n", | |
54abd335 | 340 | *min_uA, *max_uA); |
414c70cb | 341 | return -EINVAL; |
89f425ed | 342 | } |
414c70cb LG |
343 | |
344 | return 0; | |
345 | } | |
346 | ||
347 | /* operating mode constraint check */ | |
109c75af CK |
348 | static int regulator_mode_constrain(struct regulator_dev *rdev, |
349 | unsigned int *mode) | |
414c70cb | 350 | { |
2c608234 | 351 | switch (*mode) { |
e573520b DB |
352 | case REGULATOR_MODE_FAST: |
353 | case REGULATOR_MODE_NORMAL: | |
354 | case REGULATOR_MODE_IDLE: | |
355 | case REGULATOR_MODE_STANDBY: | |
356 | break; | |
357 | default: | |
89f425ed | 358 | rdev_err(rdev, "invalid mode %x specified\n", *mode); |
e573520b DB |
359 | return -EINVAL; |
360 | } | |
361 | ||
8a34e979 | 362 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) { |
7ebcf26c | 363 | rdev_err(rdev, "mode operation not allowed\n"); |
414c70cb LG |
364 | return -EPERM; |
365 | } | |
2c608234 MB |
366 | |
367 | /* The modes are bitmasks, the most power hungry modes having | |
368 | * the lowest values. If the requested mode isn't supported | |
369 | * try higher modes. */ | |
370 | while (*mode) { | |
371 | if (rdev->constraints->valid_modes_mask & *mode) | |
372 | return 0; | |
373 | *mode /= 2; | |
414c70cb | 374 | } |
2c608234 MB |
375 | |
376 | return -EINVAL; | |
414c70cb LG |
377 | } |
378 | ||
f7efad10 CZ |
379 | static inline struct regulator_state * |
380 | regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state) | |
381 | { | |
382 | if (rdev->constraints == NULL) | |
383 | return NULL; | |
384 | ||
385 | switch (state) { | |
386 | case PM_SUSPEND_STANDBY: | |
387 | return &rdev->constraints->state_standby; | |
388 | case PM_SUSPEND_MEM: | |
389 | return &rdev->constraints->state_mem; | |
390 | case PM_SUSPEND_MAX: | |
391 | return &rdev->constraints->state_disk; | |
392 | default: | |
393 | return NULL; | |
394 | } | |
395 | } | |
396 | ||
414c70cb LG |
397 | static ssize_t regulator_uV_show(struct device *dev, |
398 | struct device_attribute *attr, char *buf) | |
399 | { | |
a5766f11 | 400 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
401 | ssize_t ret; |
402 | ||
66cf9a7e | 403 | regulator_lock(rdev); |
414c70cb | 404 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
66cf9a7e | 405 | regulator_unlock(rdev); |
414c70cb LG |
406 | |
407 | return ret; | |
408 | } | |
7ad68e2f | 409 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
414c70cb LG |
410 | |
411 | static ssize_t regulator_uA_show(struct device *dev, | |
412 | struct device_attribute *attr, char *buf) | |
413 | { | |
a5766f11 | 414 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
415 | |
416 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); | |
417 | } | |
7ad68e2f | 418 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); |
414c70cb | 419 | |
587cea27 GKH |
420 | static ssize_t name_show(struct device *dev, struct device_attribute *attr, |
421 | char *buf) | |
bc558a60 MB |
422 | { |
423 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
bc558a60 | 424 | |
1083c393 | 425 | return sprintf(buf, "%s\n", rdev_get_name(rdev)); |
bc558a60 | 426 | } |
587cea27 | 427 | static DEVICE_ATTR_RO(name); |
bc558a60 | 428 | |
4fca9545 | 429 | static ssize_t regulator_print_opmode(char *buf, int mode) |
414c70cb | 430 | { |
414c70cb LG |
431 | switch (mode) { |
432 | case REGULATOR_MODE_FAST: | |
433 | return sprintf(buf, "fast\n"); | |
434 | case REGULATOR_MODE_NORMAL: | |
435 | return sprintf(buf, "normal\n"); | |
436 | case REGULATOR_MODE_IDLE: | |
437 | return sprintf(buf, "idle\n"); | |
438 | case REGULATOR_MODE_STANDBY: | |
439 | return sprintf(buf, "standby\n"); | |
440 | } | |
441 | return sprintf(buf, "unknown\n"); | |
442 | } | |
443 | ||
4fca9545 DB |
444 | static ssize_t regulator_opmode_show(struct device *dev, |
445 | struct device_attribute *attr, char *buf) | |
414c70cb | 446 | { |
a5766f11 | 447 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 448 | |
4fca9545 DB |
449 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); |
450 | } | |
7ad68e2f | 451 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); |
4fca9545 DB |
452 | |
453 | static ssize_t regulator_print_state(char *buf, int state) | |
454 | { | |
414c70cb LG |
455 | if (state > 0) |
456 | return sprintf(buf, "enabled\n"); | |
457 | else if (state == 0) | |
458 | return sprintf(buf, "disabled\n"); | |
459 | else | |
460 | return sprintf(buf, "unknown\n"); | |
461 | } | |
462 | ||
4fca9545 DB |
463 | static ssize_t regulator_state_show(struct device *dev, |
464 | struct device_attribute *attr, char *buf) | |
465 | { | |
466 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
9332546f MB |
467 | ssize_t ret; |
468 | ||
66cf9a7e | 469 | regulator_lock(rdev); |
9332546f | 470 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); |
66cf9a7e | 471 | regulator_unlock(rdev); |
4fca9545 | 472 | |
9332546f | 473 | return ret; |
4fca9545 | 474 | } |
7ad68e2f | 475 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
4fca9545 | 476 | |
853116a1 DB |
477 | static ssize_t regulator_status_show(struct device *dev, |
478 | struct device_attribute *attr, char *buf) | |
479 | { | |
480 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
481 | int status; | |
482 | char *label; | |
483 | ||
484 | status = rdev->desc->ops->get_status(rdev); | |
485 | if (status < 0) | |
486 | return status; | |
487 | ||
488 | switch (status) { | |
489 | case REGULATOR_STATUS_OFF: | |
490 | label = "off"; | |
491 | break; | |
492 | case REGULATOR_STATUS_ON: | |
493 | label = "on"; | |
494 | break; | |
495 | case REGULATOR_STATUS_ERROR: | |
496 | label = "error"; | |
497 | break; | |
498 | case REGULATOR_STATUS_FAST: | |
499 | label = "fast"; | |
500 | break; | |
501 | case REGULATOR_STATUS_NORMAL: | |
502 | label = "normal"; | |
503 | break; | |
504 | case REGULATOR_STATUS_IDLE: | |
505 | label = "idle"; | |
506 | break; | |
507 | case REGULATOR_STATUS_STANDBY: | |
508 | label = "standby"; | |
509 | break; | |
f59c8f9f MB |
510 | case REGULATOR_STATUS_BYPASS: |
511 | label = "bypass"; | |
512 | break; | |
1beaf762 KG |
513 | case REGULATOR_STATUS_UNDEFINED: |
514 | label = "undefined"; | |
515 | break; | |
853116a1 DB |
516 | default: |
517 | return -ERANGE; | |
518 | } | |
519 | ||
520 | return sprintf(buf, "%s\n", label); | |
521 | } | |
522 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); | |
523 | ||
414c70cb LG |
524 | static ssize_t regulator_min_uA_show(struct device *dev, |
525 | struct device_attribute *attr, char *buf) | |
526 | { | |
a5766f11 | 527 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
528 | |
529 | if (!rdev->constraints) | |
530 | return sprintf(buf, "constraint not defined\n"); | |
531 | ||
532 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | |
533 | } | |
7ad68e2f | 534 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
414c70cb LG |
535 | |
536 | static ssize_t regulator_max_uA_show(struct device *dev, | |
537 | struct device_attribute *attr, char *buf) | |
538 | { | |
a5766f11 | 539 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
540 | |
541 | if (!rdev->constraints) | |
542 | return sprintf(buf, "constraint not defined\n"); | |
543 | ||
544 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | |
545 | } | |
7ad68e2f | 546 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
414c70cb LG |
547 | |
548 | static ssize_t regulator_min_uV_show(struct device *dev, | |
549 | struct device_attribute *attr, char *buf) | |
550 | { | |
a5766f11 | 551 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
552 | |
553 | if (!rdev->constraints) | |
554 | return sprintf(buf, "constraint not defined\n"); | |
555 | ||
556 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | |
557 | } | |
7ad68e2f | 558 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
414c70cb LG |
559 | |
560 | static ssize_t regulator_max_uV_show(struct device *dev, | |
561 | struct device_attribute *attr, char *buf) | |
562 | { | |
a5766f11 | 563 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
564 | |
565 | if (!rdev->constraints) | |
566 | return sprintf(buf, "constraint not defined\n"); | |
567 | ||
568 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | |
569 | } | |
7ad68e2f | 570 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
414c70cb LG |
571 | |
572 | static ssize_t regulator_total_uA_show(struct device *dev, | |
573 | struct device_attribute *attr, char *buf) | |
574 | { | |
a5766f11 | 575 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
576 | struct regulator *regulator; |
577 | int uA = 0; | |
578 | ||
66cf9a7e | 579 | regulator_lock(rdev); |
414c70cb | 580 | list_for_each_entry(regulator, &rdev->consumer_list, list) |
fa2984d4 | 581 | uA += regulator->uA_load; |
66cf9a7e | 582 | regulator_unlock(rdev); |
414c70cb LG |
583 | return sprintf(buf, "%d\n", uA); |
584 | } | |
7ad68e2f | 585 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
414c70cb | 586 | |
587cea27 GKH |
587 | static ssize_t num_users_show(struct device *dev, struct device_attribute *attr, |
588 | char *buf) | |
414c70cb | 589 | { |
a5766f11 | 590 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
591 | return sprintf(buf, "%d\n", rdev->use_count); |
592 | } | |
587cea27 | 593 | static DEVICE_ATTR_RO(num_users); |
414c70cb | 594 | |
587cea27 GKH |
595 | static ssize_t type_show(struct device *dev, struct device_attribute *attr, |
596 | char *buf) | |
414c70cb | 597 | { |
a5766f11 | 598 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
599 | |
600 | switch (rdev->desc->type) { | |
601 | case REGULATOR_VOLTAGE: | |
602 | return sprintf(buf, "voltage\n"); | |
603 | case REGULATOR_CURRENT: | |
604 | return sprintf(buf, "current\n"); | |
605 | } | |
606 | return sprintf(buf, "unknown\n"); | |
607 | } | |
587cea27 | 608 | static DEVICE_ATTR_RO(type); |
414c70cb LG |
609 | |
610 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | |
611 | struct device_attribute *attr, char *buf) | |
612 | { | |
a5766f11 | 613 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 614 | |
414c70cb LG |
615 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
616 | } | |
7ad68e2f DB |
617 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
618 | regulator_suspend_mem_uV_show, NULL); | |
414c70cb LG |
619 | |
620 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | |
621 | struct device_attribute *attr, char *buf) | |
622 | { | |
a5766f11 | 623 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 624 | |
414c70cb LG |
625 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
626 | } | |
7ad68e2f DB |
627 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
628 | regulator_suspend_disk_uV_show, NULL); | |
414c70cb LG |
629 | |
630 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | |
631 | struct device_attribute *attr, char *buf) | |
632 | { | |
a5766f11 | 633 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 634 | |
414c70cb LG |
635 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
636 | } | |
7ad68e2f DB |
637 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
638 | regulator_suspend_standby_uV_show, NULL); | |
414c70cb | 639 | |
414c70cb LG |
640 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
641 | struct device_attribute *attr, char *buf) | |
642 | { | |
a5766f11 | 643 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 644 | |
4fca9545 DB |
645 | return regulator_print_opmode(buf, |
646 | rdev->constraints->state_mem.mode); | |
414c70cb | 647 | } |
7ad68e2f DB |
648 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
649 | regulator_suspend_mem_mode_show, NULL); | |
414c70cb LG |
650 | |
651 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | |
652 | struct device_attribute *attr, char *buf) | |
653 | { | |
a5766f11 | 654 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 655 | |
4fca9545 DB |
656 | return regulator_print_opmode(buf, |
657 | rdev->constraints->state_disk.mode); | |
414c70cb | 658 | } |
7ad68e2f DB |
659 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
660 | regulator_suspend_disk_mode_show, NULL); | |
414c70cb LG |
661 | |
662 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | |
663 | struct device_attribute *attr, char *buf) | |
664 | { | |
a5766f11 | 665 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 666 | |
4fca9545 DB |
667 | return regulator_print_opmode(buf, |
668 | rdev->constraints->state_standby.mode); | |
414c70cb | 669 | } |
7ad68e2f DB |
670 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
671 | regulator_suspend_standby_mode_show, NULL); | |
414c70cb LG |
672 | |
673 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | |
674 | struct device_attribute *attr, char *buf) | |
675 | { | |
a5766f11 | 676 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 677 | |
4fca9545 DB |
678 | return regulator_print_state(buf, |
679 | rdev->constraints->state_mem.enabled); | |
414c70cb | 680 | } |
7ad68e2f DB |
681 | static DEVICE_ATTR(suspend_mem_state, 0444, |
682 | regulator_suspend_mem_state_show, NULL); | |
414c70cb LG |
683 | |
684 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | |
685 | struct device_attribute *attr, char *buf) | |
686 | { | |
a5766f11 | 687 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 688 | |
4fca9545 DB |
689 | return regulator_print_state(buf, |
690 | rdev->constraints->state_disk.enabled); | |
414c70cb | 691 | } |
7ad68e2f DB |
692 | static DEVICE_ATTR(suspend_disk_state, 0444, |
693 | regulator_suspend_disk_state_show, NULL); | |
414c70cb LG |
694 | |
695 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | |
696 | struct device_attribute *attr, char *buf) | |
697 | { | |
a5766f11 | 698 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 699 | |
4fca9545 DB |
700 | return regulator_print_state(buf, |
701 | rdev->constraints->state_standby.enabled); | |
414c70cb | 702 | } |
7ad68e2f DB |
703 | static DEVICE_ATTR(suspend_standby_state, 0444, |
704 | regulator_suspend_standby_state_show, NULL); | |
705 | ||
f59c8f9f MB |
706 | static ssize_t regulator_bypass_show(struct device *dev, |
707 | struct device_attribute *attr, char *buf) | |
708 | { | |
709 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
710 | const char *report; | |
711 | bool bypass; | |
712 | int ret; | |
713 | ||
714 | ret = rdev->desc->ops->get_bypass(rdev, &bypass); | |
715 | ||
716 | if (ret != 0) | |
717 | report = "unknown"; | |
718 | else if (bypass) | |
719 | report = "enabled"; | |
720 | else | |
721 | report = "disabled"; | |
722 | ||
723 | return sprintf(buf, "%s\n", report); | |
724 | } | |
725 | static DEVICE_ATTR(bypass, 0444, | |
726 | regulator_bypass_show, NULL); | |
bc558a60 | 727 | |
414c70cb LG |
728 | /* Calculate the new optimum regulator operating mode based on the new total |
729 | * consumer load. All locks held by caller */ | |
8460ef38 | 730 | static int drms_uA_update(struct regulator_dev *rdev) |
414c70cb LG |
731 | { |
732 | struct regulator *sibling; | |
733 | int current_uA = 0, output_uV, input_uV, err; | |
734 | unsigned int mode; | |
735 | ||
70cfef26 KK |
736 | lockdep_assert_held_once(&rdev->mutex); |
737 | ||
8460ef38 BA |
738 | /* |
739 | * first check to see if we can set modes at all, otherwise just | |
740 | * tell the consumer everything is OK. | |
741 | */ | |
8a34e979 | 742 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
8460ef38 BA |
743 | return 0; |
744 | ||
8f4490e0 BA |
745 | if (!rdev->desc->ops->get_optimum_mode && |
746 | !rdev->desc->ops->set_load) | |
8460ef38 BA |
747 | return 0; |
748 | ||
8f4490e0 BA |
749 | if (!rdev->desc->ops->set_mode && |
750 | !rdev->desc->ops->set_load) | |
8460ef38 | 751 | return -EINVAL; |
414c70cb | 752 | |
414c70cb LG |
753 | /* calc total requested load */ |
754 | list_for_each_entry(sibling, &rdev->consumer_list, list) | |
fa2984d4 | 755 | current_uA += sibling->uA_load; |
414c70cb | 756 | |
22a10bca SB |
757 | current_uA += rdev->constraints->system_load; |
758 | ||
8f4490e0 BA |
759 | if (rdev->desc->ops->set_load) { |
760 | /* set the optimum mode for our new total regulator load */ | |
761 | err = rdev->desc->ops->set_load(rdev, current_uA); | |
762 | if (err < 0) | |
763 | rdev_err(rdev, "failed to set load %d\n", current_uA); | |
764 | } else { | |
57776617 JP |
765 | /* get output voltage */ |
766 | output_uV = _regulator_get_voltage(rdev); | |
767 | if (output_uV <= 0) { | |
768 | rdev_err(rdev, "invalid output voltage found\n"); | |
769 | return -EINVAL; | |
770 | } | |
771 | ||
772 | /* get input voltage */ | |
773 | input_uV = 0; | |
774 | if (rdev->supply) | |
775 | input_uV = regulator_get_voltage(rdev->supply); | |
776 | if (input_uV <= 0) | |
777 | input_uV = rdev->constraints->input_uV; | |
778 | if (input_uV <= 0) { | |
779 | rdev_err(rdev, "invalid input voltage found\n"); | |
780 | return -EINVAL; | |
781 | } | |
782 | ||
8f4490e0 BA |
783 | /* now get the optimum mode for our new total regulator load */ |
784 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, | |
785 | output_uV, current_uA); | |
786 | ||
787 | /* check the new mode is allowed */ | |
788 | err = regulator_mode_constrain(rdev, &mode); | |
789 | if (err < 0) { | |
790 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", | |
791 | current_uA, input_uV, output_uV); | |
792 | return err; | |
793 | } | |
414c70cb | 794 | |
8f4490e0 BA |
795 | err = rdev->desc->ops->set_mode(rdev, mode); |
796 | if (err < 0) | |
797 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); | |
8460ef38 BA |
798 | } |
799 | ||
8460ef38 | 800 | return err; |
414c70cb LG |
801 | } |
802 | ||
803 | static int suspend_set_state(struct regulator_dev *rdev, | |
f7efad10 | 804 | suspend_state_t state) |
414c70cb LG |
805 | { |
806 | int ret = 0; | |
f7efad10 CZ |
807 | struct regulator_state *rstate; |
808 | ||
809 | rstate = regulator_get_suspend_state(rdev, state); | |
810 | if (rstate == NULL) | |
57a0dd18 | 811 | return 0; |
638f85c5 MB |
812 | |
813 | /* If we have no suspend mode configration don't set anything; | |
8ac0e95d AL |
814 | * only warn if the driver implements set_suspend_voltage or |
815 | * set_suspend_mode callback. | |
638f85c5 | 816 | */ |
72069f99 CZ |
817 | if (rstate->enabled != ENABLE_IN_SUSPEND && |
818 | rstate->enabled != DISABLE_IN_SUSPEND) { | |
8ac0e95d AL |
819 | if (rdev->desc->ops->set_suspend_voltage || |
820 | rdev->desc->ops->set_suspend_mode) | |
5da84fd9 | 821 | rdev_warn(rdev, "No configuration\n"); |
638f85c5 MB |
822 | return 0; |
823 | } | |
824 | ||
72069f99 CZ |
825 | if (rstate->enabled == ENABLE_IN_SUSPEND && |
826 | rdev->desc->ops->set_suspend_enable) | |
414c70cb | 827 | ret = rdev->desc->ops->set_suspend_enable(rdev); |
72069f99 CZ |
828 | else if (rstate->enabled == DISABLE_IN_SUSPEND && |
829 | rdev->desc->ops->set_suspend_disable) | |
414c70cb | 830 | ret = rdev->desc->ops->set_suspend_disable(rdev); |
8ac0e95d AL |
831 | else /* OK if set_suspend_enable or set_suspend_disable is NULL */ |
832 | ret = 0; | |
833 | ||
414c70cb | 834 | if (ret < 0) { |
5da84fd9 | 835 | rdev_err(rdev, "failed to enabled/disable\n"); |
414c70cb LG |
836 | return ret; |
837 | } | |
838 | ||
839 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { | |
840 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); | |
841 | if (ret < 0) { | |
5da84fd9 | 842 | rdev_err(rdev, "failed to set voltage\n"); |
414c70cb LG |
843 | return ret; |
844 | } | |
845 | } | |
846 | ||
847 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { | |
848 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); | |
849 | if (ret < 0) { | |
5da84fd9 | 850 | rdev_err(rdev, "failed to set mode\n"); |
414c70cb LG |
851 | return ret; |
852 | } | |
853 | } | |
414c70cb | 854 | |
f7efad10 | 855 | return ret; |
414c70cb LG |
856 | } |
857 | ||
858 | static void print_constraints(struct regulator_dev *rdev) | |
859 | { | |
860 | struct regulation_constraints *constraints = rdev->constraints; | |
a7068e39 | 861 | char buf[160] = ""; |
5751a99f | 862 | size_t len = sizeof(buf) - 1; |
8f031b48 MB |
863 | int count = 0; |
864 | int ret; | |
414c70cb | 865 | |
8f031b48 | 866 | if (constraints->min_uV && constraints->max_uV) { |
414c70cb | 867 | if (constraints->min_uV == constraints->max_uV) |
5751a99f SW |
868 | count += scnprintf(buf + count, len - count, "%d mV ", |
869 | constraints->min_uV / 1000); | |
414c70cb | 870 | else |
5751a99f SW |
871 | count += scnprintf(buf + count, len - count, |
872 | "%d <--> %d mV ", | |
873 | constraints->min_uV / 1000, | |
874 | constraints->max_uV / 1000); | |
8f031b48 MB |
875 | } |
876 | ||
877 | if (!constraints->min_uV || | |
878 | constraints->min_uV != constraints->max_uV) { | |
879 | ret = _regulator_get_voltage(rdev); | |
880 | if (ret > 0) | |
5751a99f SW |
881 | count += scnprintf(buf + count, len - count, |
882 | "at %d mV ", ret / 1000); | |
8f031b48 MB |
883 | } |
884 | ||
bf5892a8 | 885 | if (constraints->uV_offset) |
5751a99f SW |
886 | count += scnprintf(buf + count, len - count, "%dmV offset ", |
887 | constraints->uV_offset / 1000); | |
bf5892a8 | 888 | |
8f031b48 | 889 | if (constraints->min_uA && constraints->max_uA) { |
414c70cb | 890 | if (constraints->min_uA == constraints->max_uA) |
5751a99f SW |
891 | count += scnprintf(buf + count, len - count, "%d mA ", |
892 | constraints->min_uA / 1000); | |
414c70cb | 893 | else |
5751a99f SW |
894 | count += scnprintf(buf + count, len - count, |
895 | "%d <--> %d mA ", | |
896 | constraints->min_uA / 1000, | |
897 | constraints->max_uA / 1000); | |
8f031b48 MB |
898 | } |
899 | ||
900 | if (!constraints->min_uA || | |
901 | constraints->min_uA != constraints->max_uA) { | |
902 | ret = _regulator_get_current_limit(rdev); | |
903 | if (ret > 0) | |
5751a99f SW |
904 | count += scnprintf(buf + count, len - count, |
905 | "at %d mA ", ret / 1000); | |
414c70cb | 906 | } |
8f031b48 | 907 | |
414c70cb | 908 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
5751a99f | 909 | count += scnprintf(buf + count, len - count, "fast "); |
414c70cb | 910 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) |
5751a99f | 911 | count += scnprintf(buf + count, len - count, "normal "); |
414c70cb | 912 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
5751a99f | 913 | count += scnprintf(buf + count, len - count, "idle "); |
414c70cb | 914 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
5751a99f | 915 | count += scnprintf(buf + count, len - count, "standby"); |
414c70cb | 916 | |
215b8b05 | 917 | if (!count) |
5751a99f | 918 | scnprintf(buf, len, "no parameters"); |
215b8b05 | 919 | |
194dbaef | 920 | rdev_dbg(rdev, "%s\n", buf); |
4a682922 MB |
921 | |
922 | if ((constraints->min_uV != constraints->max_uV) && | |
8a34e979 | 923 | !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) |
4a682922 MB |
924 | rdev_warn(rdev, |
925 | "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); | |
414c70cb LG |
926 | } |
927 | ||
e79055d6 | 928 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
1083c393 | 929 | struct regulation_constraints *constraints) |
a5766f11 | 930 | { |
272e2315 | 931 | const struct regulator_ops *ops = rdev->desc->ops; |
af5866c9 MB |
932 | int ret; |
933 | ||
934 | /* do we need to apply the constraint voltage */ | |
935 | if (rdev->constraints->apply_uV && | |
fa93fd4e MB |
936 | rdev->constraints->min_uV && rdev->constraints->max_uV) { |
937 | int target_min, target_max; | |
064d5cd1 | 938 | int current_uV = _regulator_get_voltage(rdev); |
84b3a7c9 DA |
939 | |
940 | if (current_uV == -ENOTRECOVERABLE) { | |
941 | /* This regulator can't be read and must be initted */ | |
942 | rdev_info(rdev, "Setting %d-%duV\n", | |
943 | rdev->constraints->min_uV, | |
944 | rdev->constraints->max_uV); | |
945 | _regulator_do_set_voltage(rdev, | |
946 | rdev->constraints->min_uV, | |
947 | rdev->constraints->max_uV); | |
948 | current_uV = _regulator_get_voltage(rdev); | |
949 | } | |
950 | ||
064d5cd1 | 951 | if (current_uV < 0) { |
69d58839 NM |
952 | rdev_err(rdev, |
953 | "failed to get the current voltage(%d)\n", | |
954 | current_uV); | |
064d5cd1 AB |
955 | return current_uV; |
956 | } | |
fa93fd4e MB |
957 | |
958 | /* | |
959 | * If we're below the minimum voltage move up to the | |
960 | * minimum voltage, if we're above the maximum voltage | |
961 | * then move down to the maximum. | |
962 | */ | |
963 | target_min = current_uV; | |
964 | target_max = current_uV; | |
965 | ||
966 | if (current_uV < rdev->constraints->min_uV) { | |
967 | target_min = rdev->constraints->min_uV; | |
968 | target_max = rdev->constraints->min_uV; | |
969 | } | |
970 | ||
971 | if (current_uV > rdev->constraints->max_uV) { | |
972 | target_min = rdev->constraints->max_uV; | |
973 | target_max = rdev->constraints->max_uV; | |
974 | } | |
975 | ||
976 | if (target_min != current_uV || target_max != current_uV) { | |
45a91e8f MB |
977 | rdev_info(rdev, "Bringing %duV into %d-%duV\n", |
978 | current_uV, target_min, target_max); | |
064d5cd1 | 979 | ret = _regulator_do_set_voltage( |
fa93fd4e | 980 | rdev, target_min, target_max); |
064d5cd1 AB |
981 | if (ret < 0) { |
982 | rdev_err(rdev, | |
fa93fd4e MB |
983 | "failed to apply %d-%duV constraint(%d)\n", |
984 | target_min, target_max, ret); | |
064d5cd1 AB |
985 | return ret; |
986 | } | |
75790251 | 987 | } |
af5866c9 | 988 | } |
e06f5b4f | 989 | |
4367cfdc DB |
990 | /* constrain machine-level voltage specs to fit |
991 | * the actual range supported by this regulator. | |
992 | */ | |
993 | if (ops->list_voltage && rdev->desc->n_voltages) { | |
994 | int count = rdev->desc->n_voltages; | |
995 | int i; | |
996 | int min_uV = INT_MAX; | |
997 | int max_uV = INT_MIN; | |
998 | int cmin = constraints->min_uV; | |
999 | int cmax = constraints->max_uV; | |
1000 | ||
3e590918 MB |
1001 | /* it's safe to autoconfigure fixed-voltage supplies |
1002 | and the constraints are used by list_voltage. */ | |
4367cfdc | 1003 | if (count == 1 && !cmin) { |
3e590918 | 1004 | cmin = 1; |
4367cfdc | 1005 | cmax = INT_MAX; |
3e590918 MB |
1006 | constraints->min_uV = cmin; |
1007 | constraints->max_uV = cmax; | |
4367cfdc DB |
1008 | } |
1009 | ||
3e2b9abd MB |
1010 | /* voltage constraints are optional */ |
1011 | if ((cmin == 0) && (cmax == 0)) | |
e79055d6 | 1012 | return 0; |
3e2b9abd | 1013 | |
4367cfdc | 1014 | /* else require explicit machine-level constraints */ |
3e2b9abd | 1015 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
5da84fd9 | 1016 | rdev_err(rdev, "invalid voltage constraints\n"); |
e79055d6 | 1017 | return -EINVAL; |
4367cfdc DB |
1018 | } |
1019 | ||
1020 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ | |
1021 | for (i = 0; i < count; i++) { | |
1022 | int value; | |
1023 | ||
1024 | value = ops->list_voltage(rdev, i); | |
1025 | if (value <= 0) | |
1026 | continue; | |
1027 | ||
1028 | /* maybe adjust [min_uV..max_uV] */ | |
1029 | if (value >= cmin && value < min_uV) | |
1030 | min_uV = value; | |
1031 | if (value <= cmax && value > max_uV) | |
1032 | max_uV = value; | |
1033 | } | |
1034 | ||
1035 | /* final: [min_uV..max_uV] valid iff constraints valid */ | |
1036 | if (max_uV < min_uV) { | |
fff15bef MB |
1037 | rdev_err(rdev, |
1038 | "unsupportable voltage constraints %u-%uuV\n", | |
1039 | min_uV, max_uV); | |
e79055d6 | 1040 | return -EINVAL; |
4367cfdc DB |
1041 | } |
1042 | ||
1043 | /* use regulator's subset of machine constraints */ | |
1044 | if (constraints->min_uV < min_uV) { | |
5da84fd9 JP |
1045 | rdev_dbg(rdev, "override min_uV, %d -> %d\n", |
1046 | constraints->min_uV, min_uV); | |
4367cfdc DB |
1047 | constraints->min_uV = min_uV; |
1048 | } | |
1049 | if (constraints->max_uV > max_uV) { | |
5da84fd9 JP |
1050 | rdev_dbg(rdev, "override max_uV, %d -> %d\n", |
1051 | constraints->max_uV, max_uV); | |
4367cfdc DB |
1052 | constraints->max_uV = max_uV; |
1053 | } | |
1054 | } | |
1055 | ||
e79055d6 MB |
1056 | return 0; |
1057 | } | |
1058 | ||
f8c1700d LD |
1059 | static int machine_constraints_current(struct regulator_dev *rdev, |
1060 | struct regulation_constraints *constraints) | |
1061 | { | |
272e2315 | 1062 | const struct regulator_ops *ops = rdev->desc->ops; |
f8c1700d LD |
1063 | int ret; |
1064 | ||
1065 | if (!constraints->min_uA && !constraints->max_uA) | |
1066 | return 0; | |
1067 | ||
1068 | if (constraints->min_uA > constraints->max_uA) { | |
1069 | rdev_err(rdev, "Invalid current constraints\n"); | |
1070 | return -EINVAL; | |
1071 | } | |
1072 | ||
1073 | if (!ops->set_current_limit || !ops->get_current_limit) { | |
1074 | rdev_warn(rdev, "Operation of current configuration missing\n"); | |
1075 | return 0; | |
1076 | } | |
1077 | ||
1078 | /* Set regulator current in constraints range */ | |
1079 | ret = ops->set_current_limit(rdev, constraints->min_uA, | |
1080 | constraints->max_uA); | |
1081 | if (ret < 0) { | |
1082 | rdev_err(rdev, "Failed to set current constraint, %d\n", ret); | |
1083 | return ret; | |
1084 | } | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
30c21971 MP |
1089 | static int _regulator_do_enable(struct regulator_dev *rdev); |
1090 | ||
e79055d6 MB |
1091 | /** |
1092 | * set_machine_constraints - sets regulator constraints | |
1093 | * @rdev: regulator source | |
1094 | * @constraints: constraints to apply | |
1095 | * | |
1096 | * Allows platform initialisation code to define and constrain | |
1097 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: | |
1098 | * Constraints *must* be set by platform code in order for some | |
1099 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | |
1100 | * set_mode. | |
1101 | */ | |
1102 | static int set_machine_constraints(struct regulator_dev *rdev, | |
f8c12fe3 | 1103 | const struct regulation_constraints *constraints) |
e79055d6 MB |
1104 | { |
1105 | int ret = 0; | |
272e2315 | 1106 | const struct regulator_ops *ops = rdev->desc->ops; |
e79055d6 | 1107 | |
9a8f5e07 MB |
1108 | if (constraints) |
1109 | rdev->constraints = kmemdup(constraints, sizeof(*constraints), | |
1110 | GFP_KERNEL); | |
1111 | else | |
1112 | rdev->constraints = kzalloc(sizeof(*constraints), | |
1113 | GFP_KERNEL); | |
f8c12fe3 MB |
1114 | if (!rdev->constraints) |
1115 | return -ENOMEM; | |
af5866c9 | 1116 | |
f8c12fe3 | 1117 | ret = machine_constraints_voltage(rdev, rdev->constraints); |
e79055d6 | 1118 | if (ret != 0) |
6333ef46 | 1119 | return ret; |
e79055d6 | 1120 | |
f8c1700d | 1121 | ret = machine_constraints_current(rdev, rdev->constraints); |
e79055d6 | 1122 | if (ret != 0) |
6333ef46 | 1123 | return ret; |
e79055d6 | 1124 | |
36e4f839 SB |
1125 | if (rdev->constraints->ilim_uA && ops->set_input_current_limit) { |
1126 | ret = ops->set_input_current_limit(rdev, | |
1127 | rdev->constraints->ilim_uA); | |
1128 | if (ret < 0) { | |
1129 | rdev_err(rdev, "failed to set input limit\n"); | |
6333ef46 | 1130 | return ret; |
36e4f839 SB |
1131 | } |
1132 | } | |
1133 | ||
a5766f11 | 1134 | /* do we need to setup our suspend state */ |
9a8f5e07 | 1135 | if (rdev->constraints->initial_state) { |
f7efad10 | 1136 | ret = suspend_set_state(rdev, rdev->constraints->initial_state); |
e06f5b4f | 1137 | if (ret < 0) { |
5da84fd9 | 1138 | rdev_err(rdev, "failed to set suspend state\n"); |
6333ef46 | 1139 | return ret; |
e06f5b4f MB |
1140 | } |
1141 | } | |
a5766f11 | 1142 | |
9a8f5e07 | 1143 | if (rdev->constraints->initial_mode) { |
a308466c | 1144 | if (!ops->set_mode) { |
5da84fd9 | 1145 | rdev_err(rdev, "no set_mode operation\n"); |
6333ef46 | 1146 | return -EINVAL; |
a308466c MB |
1147 | } |
1148 | ||
f8c12fe3 | 1149 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
a308466c | 1150 | if (ret < 0) { |
5da84fd9 | 1151 | rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
6333ef46 | 1152 | return ret; |
a308466c MB |
1153 | } |
1154 | } | |
1155 | ||
cacf90f2 MB |
1156 | /* If the constraints say the regulator should be on at this point |
1157 | * and we have control then make sure it is enabled. | |
1158 | */ | |
30c21971 MP |
1159 | if (rdev->constraints->always_on || rdev->constraints->boot_on) { |
1160 | ret = _regulator_do_enable(rdev); | |
1161 | if (ret < 0 && ret != -EINVAL) { | |
5da84fd9 | 1162 | rdev_err(rdev, "failed to enable\n"); |
6333ef46 | 1163 | return ret; |
e5fda26c MB |
1164 | } |
1165 | } | |
1166 | ||
1653ccf4 YSB |
1167 | if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable) |
1168 | && ops->set_ramp_delay) { | |
6f0b2c69 YSB |
1169 | ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay); |
1170 | if (ret < 0) { | |
1171 | rdev_err(rdev, "failed to set ramp_delay\n"); | |
6333ef46 | 1172 | return ret; |
6f0b2c69 YSB |
1173 | } |
1174 | } | |
1175 | ||
23c779b9 SB |
1176 | if (rdev->constraints->pull_down && ops->set_pull_down) { |
1177 | ret = ops->set_pull_down(rdev); | |
1178 | if (ret < 0) { | |
1179 | rdev_err(rdev, "failed to set pull down\n"); | |
6333ef46 | 1180 | return ret; |
23c779b9 SB |
1181 | } |
1182 | } | |
1183 | ||
57f66b78 SB |
1184 | if (rdev->constraints->soft_start && ops->set_soft_start) { |
1185 | ret = ops->set_soft_start(rdev); | |
1186 | if (ret < 0) { | |
1187 | rdev_err(rdev, "failed to set soft start\n"); | |
6333ef46 | 1188 | return ret; |
57f66b78 SB |
1189 | } |
1190 | } | |
1191 | ||
3a003bae SB |
1192 | if (rdev->constraints->over_current_protection |
1193 | && ops->set_over_current_protection) { | |
1194 | ret = ops->set_over_current_protection(rdev); | |
1195 | if (ret < 0) { | |
1196 | rdev_err(rdev, "failed to set over current protection\n"); | |
6333ef46 | 1197 | return ret; |
3a003bae SB |
1198 | } |
1199 | } | |
1200 | ||
670666b9 LD |
1201 | if (rdev->constraints->active_discharge && ops->set_active_discharge) { |
1202 | bool ad_state = (rdev->constraints->active_discharge == | |
1203 | REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false; | |
1204 | ||
1205 | ret = ops->set_active_discharge(rdev, ad_state); | |
1206 | if (ret < 0) { | |
1207 | rdev_err(rdev, "failed to set active discharge\n"); | |
1208 | return ret; | |
1209 | } | |
1210 | } | |
1211 | ||
a5766f11 | 1212 | print_constraints(rdev); |
1a6958e7 | 1213 | return 0; |
a5766f11 LG |
1214 | } |
1215 | ||
1216 | /** | |
1217 | * set_supply - set regulator supply regulator | |
69279fb9 MB |
1218 | * @rdev: regulator name |
1219 | * @supply_rdev: supply regulator name | |
a5766f11 LG |
1220 | * |
1221 | * Called by platform initialisation code to set the supply regulator for this | |
1222 | * regulator. This ensures that a regulators supply will also be enabled by the | |
1223 | * core if it's child is enabled. | |
1224 | */ | |
1225 | static int set_supply(struct regulator_dev *rdev, | |
3801b86a | 1226 | struct regulator_dev *supply_rdev) |
a5766f11 LG |
1227 | { |
1228 | int err; | |
1229 | ||
3801b86a MB |
1230 | rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); |
1231 | ||
e2c09ae7 JMC |
1232 | if (!try_module_get(supply_rdev->owner)) |
1233 | return -ENODEV; | |
1234 | ||
3801b86a | 1235 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); |
32c78de8 AL |
1236 | if (rdev->supply == NULL) { |
1237 | err = -ENOMEM; | |
3801b86a | 1238 | return err; |
a5766f11 | 1239 | } |
57ad526a | 1240 | supply_rdev->open_count++; |
3801b86a MB |
1241 | |
1242 | return 0; | |
a5766f11 LG |
1243 | } |
1244 | ||
1245 | /** | |
06c63f93 | 1246 | * set_consumer_device_supply - Bind a regulator to a symbolic supply |
69279fb9 | 1247 | * @rdev: regulator source |
40f9244f | 1248 | * @consumer_dev_name: dev_name() string for device supply applies to |
69279fb9 | 1249 | * @supply: symbolic name for supply |
a5766f11 LG |
1250 | * |
1251 | * Allows platform initialisation code to map physical regulator | |
1252 | * sources to symbolic names for supplies for use by devices. Devices | |
1253 | * should use these symbolic names to request regulators, avoiding the | |
1254 | * need to provide board-specific regulator names as platform data. | |
1255 | */ | |
1256 | static int set_consumer_device_supply(struct regulator_dev *rdev, | |
737f360d MB |
1257 | const char *consumer_dev_name, |
1258 | const char *supply) | |
a5766f11 LG |
1259 | { |
1260 | struct regulator_map *node; | |
9ed2099e | 1261 | int has_dev; |
a5766f11 LG |
1262 | |
1263 | if (supply == NULL) | |
1264 | return -EINVAL; | |
1265 | ||
9ed2099e MB |
1266 | if (consumer_dev_name != NULL) |
1267 | has_dev = 1; | |
1268 | else | |
1269 | has_dev = 0; | |
1270 | ||
6001e13c | 1271 | list_for_each_entry(node, ®ulator_map_list, list) { |
23b5cc2a JN |
1272 | if (node->dev_name && consumer_dev_name) { |
1273 | if (strcmp(node->dev_name, consumer_dev_name) != 0) | |
1274 | continue; | |
1275 | } else if (node->dev_name || consumer_dev_name) { | |
6001e13c | 1276 | continue; |
23b5cc2a JN |
1277 | } |
1278 | ||
6001e13c DB |
1279 | if (strcmp(node->supply, supply) != 0) |
1280 | continue; | |
1281 | ||
737f360d MB |
1282 | pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", |
1283 | consumer_dev_name, | |
1284 | dev_name(&node->regulator->dev), | |
1285 | node->regulator->desc->name, | |
1286 | supply, | |
1287 | dev_name(&rdev->dev), rdev_get_name(rdev)); | |
6001e13c DB |
1288 | return -EBUSY; |
1289 | } | |
1290 | ||
9ed2099e | 1291 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
a5766f11 LG |
1292 | if (node == NULL) |
1293 | return -ENOMEM; | |
1294 | ||
1295 | node->regulator = rdev; | |
a5766f11 LG |
1296 | node->supply = supply; |
1297 | ||
9ed2099e MB |
1298 | if (has_dev) { |
1299 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); | |
1300 | if (node->dev_name == NULL) { | |
1301 | kfree(node); | |
1302 | return -ENOMEM; | |
1303 | } | |
40f9244f MB |
1304 | } |
1305 | ||
a5766f11 LG |
1306 | list_add(&node->list, ®ulator_map_list); |
1307 | return 0; | |
1308 | } | |
1309 | ||
0f1d747b MR |
1310 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
1311 | { | |
1312 | struct regulator_map *node, *n; | |
1313 | ||
1314 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | |
1315 | if (rdev == node->regulator) { | |
1316 | list_del(&node->list); | |
40f9244f | 1317 | kfree(node->dev_name); |
0f1d747b | 1318 | kfree(node); |
0f1d747b MR |
1319 | } |
1320 | } | |
1321 | } | |
1322 | ||
2d80a91b RF |
1323 | #ifdef CONFIG_DEBUG_FS |
1324 | static ssize_t constraint_flags_read_file(struct file *file, | |
1325 | char __user *user_buf, | |
1326 | size_t count, loff_t *ppos) | |
1327 | { | |
1328 | const struct regulator *regulator = file->private_data; | |
1329 | const struct regulation_constraints *c = regulator->rdev->constraints; | |
1330 | char *buf; | |
1331 | ssize_t ret; | |
1332 | ||
1333 | if (!c) | |
1334 | return 0; | |
1335 | ||
1336 | buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1337 | if (!buf) | |
1338 | return -ENOMEM; | |
1339 | ||
1340 | ret = snprintf(buf, PAGE_SIZE, | |
1341 | "always_on: %u\n" | |
1342 | "boot_on: %u\n" | |
1343 | "apply_uV: %u\n" | |
1344 | "ramp_disable: %u\n" | |
1345 | "soft_start: %u\n" | |
1346 | "pull_down: %u\n" | |
1347 | "over_current_protection: %u\n", | |
1348 | c->always_on, | |
1349 | c->boot_on, | |
1350 | c->apply_uV, | |
1351 | c->ramp_disable, | |
1352 | c->soft_start, | |
1353 | c->pull_down, | |
1354 | c->over_current_protection); | |
1355 | ||
1356 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
1357 | kfree(buf); | |
1358 | ||
1359 | return ret; | |
1360 | } | |
1361 | ||
1362 | #endif | |
1363 | ||
1364 | static const struct file_operations constraint_flags_fops = { | |
1365 | #ifdef CONFIG_DEBUG_FS | |
1366 | .open = simple_open, | |
1367 | .read = constraint_flags_read_file, | |
1368 | .llseek = default_llseek, | |
1369 | #endif | |
1370 | }; | |
1371 | ||
f5726ae3 | 1372 | #define REG_STR_SIZE 64 |
414c70cb LG |
1373 | |
1374 | static struct regulator *create_regulator(struct regulator_dev *rdev, | |
1375 | struct device *dev, | |
1376 | const char *supply_name) | |
1377 | { | |
1378 | struct regulator *regulator; | |
1379 | char buf[REG_STR_SIZE]; | |
1380 | int err, size; | |
1381 | ||
1382 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | |
1383 | if (regulator == NULL) | |
1384 | return NULL; | |
1385 | ||
66cf9a7e | 1386 | regulator_lock(rdev); |
414c70cb LG |
1387 | regulator->rdev = rdev; |
1388 | list_add(®ulator->list, &rdev->consumer_list); | |
1389 | ||
1390 | if (dev) { | |
e2c98eaf SG |
1391 | regulator->dev = dev; |
1392 | ||
222cc7b1 | 1393 | /* Add a link to the device sysfs entry */ |
b7cd1b13 BG |
1394 | size = snprintf(buf, REG_STR_SIZE, "%s-%s", |
1395 | dev->kobj.name, supply_name); | |
414c70cb | 1396 | if (size >= REG_STR_SIZE) |
222cc7b1 | 1397 | goto overflow_err; |
414c70cb LG |
1398 | |
1399 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | |
1400 | if (regulator->supply_name == NULL) | |
222cc7b1 | 1401 | goto overflow_err; |
414c70cb | 1402 | |
ff268b56 | 1403 | err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj, |
414c70cb LG |
1404 | buf); |
1405 | if (err) { | |
ff268b56 | 1406 | rdev_dbg(rdev, "could not add device link %s err %d\n", |
5da84fd9 | 1407 | dev->kobj.name, err); |
222cc7b1 | 1408 | /* non-fatal */ |
414c70cb | 1409 | } |
5de70519 | 1410 | } else { |
0630b614 | 1411 | regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL); |
5de70519 | 1412 | if (regulator->supply_name == NULL) |
222cc7b1 | 1413 | goto overflow_err; |
5de70519 MB |
1414 | } |
1415 | ||
5de70519 MB |
1416 | regulator->debugfs = debugfs_create_dir(regulator->supply_name, |
1417 | rdev->debugfs); | |
24751434 | 1418 | if (!regulator->debugfs) { |
ad3a942b | 1419 | rdev_dbg(rdev, "Failed to create debugfs directory\n"); |
5de70519 MB |
1420 | } else { |
1421 | debugfs_create_u32("uA_load", 0444, regulator->debugfs, | |
1422 | ®ulator->uA_load); | |
1423 | debugfs_create_u32("min_uV", 0444, regulator->debugfs, | |
c360a6df | 1424 | ®ulator->voltage[PM_SUSPEND_ON].min_uV); |
5de70519 | 1425 | debugfs_create_u32("max_uV", 0444, regulator->debugfs, |
c360a6df | 1426 | ®ulator->voltage[PM_SUSPEND_ON].max_uV); |
2d80a91b RF |
1427 | debugfs_create_file("constraint_flags", 0444, |
1428 | regulator->debugfs, regulator, | |
1429 | &constraint_flags_fops); | |
414c70cb | 1430 | } |
5de70519 | 1431 | |
6492bc1b MB |
1432 | /* |
1433 | * Check now if the regulator is an always on regulator - if | |
1434 | * it is then we don't need to do nearly so much work for | |
1435 | * enable/disable calls. | |
1436 | */ | |
8a34e979 | 1437 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) && |
6492bc1b MB |
1438 | _regulator_is_enabled(rdev)) |
1439 | regulator->always_on = true; | |
1440 | ||
66cf9a7e | 1441 | regulator_unlock(rdev); |
414c70cb | 1442 | return regulator; |
414c70cb LG |
1443 | overflow_err: |
1444 | list_del(®ulator->list); | |
1445 | kfree(regulator); | |
66cf9a7e | 1446 | regulator_unlock(rdev); |
414c70cb LG |
1447 | return NULL; |
1448 | } | |
1449 | ||
31aae2be MB |
1450 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
1451 | { | |
00c877c6 LD |
1452 | if (rdev->constraints && rdev->constraints->enable_time) |
1453 | return rdev->constraints->enable_time; | |
31aae2be | 1454 | if (!rdev->desc->ops->enable_time) |
79511ed3 | 1455 | return rdev->desc->enable_time; |
31aae2be MB |
1456 | return rdev->desc->ops->enable_time(rdev); |
1457 | } | |
1458 | ||
a06ccd9c CK |
1459 | static struct regulator_supply_alias *regulator_find_supply_alias( |
1460 | struct device *dev, const char *supply) | |
1461 | { | |
1462 | struct regulator_supply_alias *map; | |
1463 | ||
1464 | list_for_each_entry(map, ®ulator_supply_alias_list, list) | |
1465 | if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0) | |
1466 | return map; | |
1467 | ||
1468 | return NULL; | |
1469 | } | |
1470 | ||
1471 | static void regulator_supply_alias(struct device **dev, const char **supply) | |
1472 | { | |
1473 | struct regulator_supply_alias *map; | |
1474 | ||
1475 | map = regulator_find_supply_alias(*dev, *supply); | |
1476 | if (map) { | |
1477 | dev_dbg(*dev, "Mapping supply %s to %s,%s\n", | |
1478 | *supply, map->alias_supply, | |
1479 | dev_name(map->alias_dev)); | |
1480 | *dev = map->alias_dev; | |
1481 | *supply = map->alias_supply; | |
1482 | } | |
1483 | } | |
1484 | ||
85f3b431 TV |
1485 | static int regulator_match(struct device *dev, const void *data) |
1486 | { | |
1487 | struct regulator_dev *r = dev_to_rdev(dev); | |
1488 | ||
1489 | return strcmp(rdev_get_name(r), data) == 0; | |
1490 | } | |
1491 | ||
1492 | static struct regulator_dev *regulator_lookup_by_name(const char *name) | |
1493 | { | |
1494 | struct device *dev; | |
1495 | ||
1496 | dev = class_find_device(®ulator_class, NULL, name, regulator_match); | |
1497 | ||
1498 | return dev ? dev_to_rdev(dev) : NULL; | |
1499 | } | |
1500 | ||
1501 | /** | |
1502 | * regulator_dev_lookup - lookup a regulator device. | |
1503 | * @dev: device for regulator "consumer". | |
1504 | * @supply: Supply name or regulator ID. | |
85f3b431 TV |
1505 | * |
1506 | * If successful, returns a struct regulator_dev that corresponds to the name | |
163478da DT |
1507 | * @supply and with the embedded struct device refcount incremented by one. |
1508 | * The refcount must be dropped by calling put_device(). | |
1509 | * On failure one of the following ERR-PTR-encoded values is returned: | |
1510 | * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed | |
1511 | * in the future. | |
85f3b431 | 1512 | */ |
69511a45 | 1513 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, |
163478da | 1514 | const char *supply) |
69511a45 | 1515 | { |
06217197 | 1516 | struct regulator_dev *r = NULL; |
69511a45 | 1517 | struct device_node *node; |
576ca436 MB |
1518 | struct regulator_map *map; |
1519 | const char *devname = NULL; | |
69511a45 | 1520 | |
a06ccd9c CK |
1521 | regulator_supply_alias(&dev, &supply); |
1522 | ||
69511a45 RN |
1523 | /* first do a dt based lookup */ |
1524 | if (dev && dev->of_node) { | |
1525 | node = of_get_regulator(dev, supply); | |
6d191a5f | 1526 | if (node) { |
85f3b431 TV |
1527 | r = of_find_regulator_by_node(node); |
1528 | if (r) | |
1529 | return r; | |
163478da | 1530 | |
6d191a5f | 1531 | /* |
163478da DT |
1532 | * We have a node, but there is no device. |
1533 | * assume it has not registered yet. | |
6d191a5f | 1534 | */ |
163478da | 1535 | return ERR_PTR(-EPROBE_DEFER); |
6d191a5f | 1536 | } |
69511a45 RN |
1537 | } |
1538 | ||
1539 | /* if not found, try doing it non-dt way */ | |
576ca436 MB |
1540 | if (dev) |
1541 | devname = dev_name(dev); | |
1542 | ||
85f3b431 | 1543 | mutex_lock(®ulator_list_mutex); |
576ca436 MB |
1544 | list_for_each_entry(map, ®ulator_map_list, list) { |
1545 | /* If the mapping has a device set up it must match */ | |
1546 | if (map->dev_name && | |
1547 | (!devname || strcmp(map->dev_name, devname))) | |
1548 | continue; | |
1549 | ||
85f3b431 TV |
1550 | if (strcmp(map->supply, supply) == 0 && |
1551 | get_device(&map->regulator->dev)) { | |
163478da DT |
1552 | r = map->regulator; |
1553 | break; | |
85f3b431 | 1554 | } |
576ca436 | 1555 | } |
85f3b431 | 1556 | mutex_unlock(®ulator_list_mutex); |
576ca436 | 1557 | |
06217197 CK |
1558 | if (r) |
1559 | return r; | |
1560 | ||
1561 | r = regulator_lookup_by_name(supply); | |
163478da DT |
1562 | if (r) |
1563 | return r; | |
1564 | ||
1565 | return ERR_PTR(-ENODEV); | |
69511a45 RN |
1566 | } |
1567 | ||
6261b06d BA |
1568 | static int regulator_resolve_supply(struct regulator_dev *rdev) |
1569 | { | |
1570 | struct regulator_dev *r; | |
1571 | struct device *dev = rdev->dev.parent; | |
1572 | int ret; | |
1573 | ||
1574 | /* No supply to resovle? */ | |
1575 | if (!rdev->supply_name) | |
1576 | return 0; | |
1577 | ||
1578 | /* Supply already resolved? */ | |
1579 | if (rdev->supply) | |
1580 | return 0; | |
1581 | ||
163478da DT |
1582 | r = regulator_dev_lookup(dev, rdev->supply_name); |
1583 | if (IS_ERR(r)) { | |
1584 | ret = PTR_ERR(r); | |
1585 | ||
06423121 MB |
1586 | /* Did the lookup explicitly defer for us? */ |
1587 | if (ret == -EPROBE_DEFER) | |
1588 | return ret; | |
1589 | ||
9f7e25ed MB |
1590 | if (have_full_constraints()) { |
1591 | r = dummy_regulator_rdev; | |
85f3b431 | 1592 | get_device(&r->dev); |
9f7e25ed MB |
1593 | } else { |
1594 | dev_err(dev, "Failed to resolve %s-supply for %s\n", | |
1595 | rdev->supply_name, rdev->desc->name); | |
1596 | return -EPROBE_DEFER; | |
1597 | } | |
6261b06d BA |
1598 | } |
1599 | ||
66d228a2 JH |
1600 | /* |
1601 | * If the supply's parent device is not the same as the | |
1602 | * regulator's parent device, then ensure the parent device | |
1603 | * is bound before we resolve the supply, in case the parent | |
1604 | * device get probe deferred and unregisters the supply. | |
1605 | */ | |
1606 | if (r->dev.parent && r->dev.parent != rdev->dev.parent) { | |
1607 | if (!device_is_bound(r->dev.parent)) { | |
1608 | put_device(&r->dev); | |
1609 | return -EPROBE_DEFER; | |
1610 | } | |
1611 | } | |
1612 | ||
6261b06d BA |
1613 | /* Recursively resolve the supply of the supply */ |
1614 | ret = regulator_resolve_supply(r); | |
85f3b431 TV |
1615 | if (ret < 0) { |
1616 | put_device(&r->dev); | |
6261b06d | 1617 | return ret; |
85f3b431 | 1618 | } |
6261b06d BA |
1619 | |
1620 | ret = set_supply(rdev, r); | |
85f3b431 TV |
1621 | if (ret < 0) { |
1622 | put_device(&r->dev); | |
6261b06d | 1623 | return ret; |
85f3b431 | 1624 | } |
6261b06d BA |
1625 | |
1626 | /* Cascade always-on state to supply */ | |
95a293c7 | 1627 | if (_regulator_is_enabled(rdev)) { |
6261b06d | 1628 | ret = regulator_enable(rdev->supply); |
36a1f1b6 | 1629 | if (ret < 0) { |
9f8df6ad | 1630 | _regulator_put(rdev->supply); |
8e5356a7 | 1631 | rdev->supply = NULL; |
6261b06d | 1632 | return ret; |
36a1f1b6 | 1633 | } |
6261b06d BA |
1634 | } |
1635 | ||
1636 | return 0; | |
1637 | } | |
1638 | ||
5ffbd136 | 1639 | /* Internal regulator request function */ |
a8bd42a9 DT |
1640 | struct regulator *_regulator_get(struct device *dev, const char *id, |
1641 | enum regulator_get_type get_type) | |
414c70cb LG |
1642 | { |
1643 | struct regulator_dev *rdev; | |
7d245afa | 1644 | struct regulator *regulator; |
a4d7641f | 1645 | const char *devname = dev ? dev_name(dev) : "deviceless"; |
317b5684 | 1646 | int ret; |
414c70cb | 1647 | |
a8bd42a9 DT |
1648 | if (get_type >= MAX_GET_TYPE) { |
1649 | dev_err(dev, "invalid type %d in %s\n", get_type, __func__); | |
1650 | return ERR_PTR(-EINVAL); | |
1651 | } | |
1652 | ||
414c70cb | 1653 | if (id == NULL) { |
5da84fd9 | 1654 | pr_err("get() with no identifier\n"); |
043c998f | 1655 | return ERR_PTR(-EINVAL); |
414c70cb LG |
1656 | } |
1657 | ||
163478da | 1658 | rdev = regulator_dev_lookup(dev, id); |
a4d7641f DT |
1659 | if (IS_ERR(rdev)) { |
1660 | ret = PTR_ERR(rdev); | |
1e4b545c | 1661 | |
a4d7641f DT |
1662 | /* |
1663 | * If regulator_dev_lookup() fails with error other | |
1664 | * than -ENODEV our job here is done, we simply return it. | |
1665 | */ | |
1666 | if (ret != -ENODEV) | |
1667 | return ERR_PTR(ret); | |
34abbd68 | 1668 | |
a4d7641f DT |
1669 | if (!have_full_constraints()) { |
1670 | dev_warn(dev, | |
1671 | "incomplete constraints, dummy supplies not allowed\n"); | |
1672 | return ERR_PTR(-ENODEV); | |
1673 | } | |
4ddfebd3 | 1674 | |
a4d7641f DT |
1675 | switch (get_type) { |
1676 | case NORMAL_GET: | |
1677 | /* | |
1678 | * Assume that a regulator is physically present and | |
1679 | * enabled, even if it isn't hooked up, and just | |
1680 | * provide a dummy. | |
1681 | */ | |
1682 | dev_warn(dev, | |
1683 | "%s supply %s not found, using dummy regulator\n", | |
1684 | devname, id); | |
1685 | rdev = dummy_regulator_rdev; | |
1686 | get_device(&rdev->dev); | |
1687 | break; | |
34abbd68 | 1688 | |
a4d7641f DT |
1689 | case EXCLUSIVE_GET: |
1690 | dev_warn(dev, | |
1691 | "dummy supplies not allowed for exclusive requests\n"); | |
1692 | /* fall through */ | |
34abbd68 | 1693 | |
a4d7641f DT |
1694 | default: |
1695 | return ERR_PTR(-ENODEV); | |
1696 | } | |
34abbd68 | 1697 | } |
34abbd68 | 1698 | |
5ffbd136 MB |
1699 | if (rdev->exclusive) { |
1700 | regulator = ERR_PTR(-EPERM); | |
85f3b431 TV |
1701 | put_device(&rdev->dev); |
1702 | return regulator; | |
5ffbd136 MB |
1703 | } |
1704 | ||
a8bd42a9 | 1705 | if (get_type == EXCLUSIVE_GET && rdev->open_count) { |
5ffbd136 | 1706 | regulator = ERR_PTR(-EBUSY); |
85f3b431 TV |
1707 | put_device(&rdev->dev); |
1708 | return regulator; | |
5ffbd136 MB |
1709 | } |
1710 | ||
6261b06d BA |
1711 | ret = regulator_resolve_supply(rdev); |
1712 | if (ret < 0) { | |
1713 | regulator = ERR_PTR(ret); | |
85f3b431 TV |
1714 | put_device(&rdev->dev); |
1715 | return regulator; | |
6261b06d BA |
1716 | } |
1717 | ||
85f3b431 | 1718 | if (!try_module_get(rdev->owner)) { |
7d245afa | 1719 | regulator = ERR_PTR(-EPROBE_DEFER); |
85f3b431 TV |
1720 | put_device(&rdev->dev); |
1721 | return regulator; | |
1722 | } | |
a5766f11 | 1723 | |
414c70cb LG |
1724 | regulator = create_regulator(rdev, dev, id); |
1725 | if (regulator == NULL) { | |
1726 | regulator = ERR_PTR(-ENOMEM); | |
85f3b431 | 1727 | put_device(&rdev->dev); |
414c70cb | 1728 | module_put(rdev->owner); |
85f3b431 | 1729 | return regulator; |
414c70cb LG |
1730 | } |
1731 | ||
5ffbd136 | 1732 | rdev->open_count++; |
a8bd42a9 | 1733 | if (get_type == EXCLUSIVE_GET) { |
5ffbd136 MB |
1734 | rdev->exclusive = 1; |
1735 | ||
1736 | ret = _regulator_is_enabled(rdev); | |
1737 | if (ret > 0) | |
1738 | rdev->use_count = 1; | |
1739 | else | |
1740 | rdev->use_count = 0; | |
1741 | } | |
1742 | ||
414c70cb LG |
1743 | return regulator; |
1744 | } | |
5ffbd136 MB |
1745 | |
1746 | /** | |
1747 | * regulator_get - lookup and obtain a reference to a regulator. | |
1748 | * @dev: device for regulator "consumer" | |
1749 | * @id: Supply name or regulator ID. | |
1750 | * | |
1751 | * Returns a struct regulator corresponding to the regulator producer, | |
1752 | * or IS_ERR() condition containing errno. | |
1753 | * | |
1754 | * Use of supply names configured via regulator_set_device_supply() is | |
1755 | * strongly encouraged. It is recommended that the supply name used | |
1756 | * should match the name used for the supply and/or the relevant | |
1757 | * device pins in the datasheet. | |
1758 | */ | |
1759 | struct regulator *regulator_get(struct device *dev, const char *id) | |
1760 | { | |
a8bd42a9 | 1761 | return _regulator_get(dev, id, NORMAL_GET); |
5ffbd136 | 1762 | } |
414c70cb LG |
1763 | EXPORT_SYMBOL_GPL(regulator_get); |
1764 | ||
5ffbd136 MB |
1765 | /** |
1766 | * regulator_get_exclusive - obtain exclusive access to a regulator. | |
1767 | * @dev: device for regulator "consumer" | |
1768 | * @id: Supply name or regulator ID. | |
1769 | * | |
1770 | * Returns a struct regulator corresponding to the regulator producer, | |
1771 | * or IS_ERR() condition containing errno. Other consumers will be | |
69c3f723 SB |
1772 | * unable to obtain this regulator while this reference is held and the |
1773 | * use count for the regulator will be initialised to reflect the current | |
1774 | * state of the regulator. | |
5ffbd136 MB |
1775 | * |
1776 | * This is intended for use by consumers which cannot tolerate shared | |
1777 | * use of the regulator such as those which need to force the | |
1778 | * regulator off for correct operation of the hardware they are | |
1779 | * controlling. | |
1780 | * | |
1781 | * Use of supply names configured via regulator_set_device_supply() is | |
1782 | * strongly encouraged. It is recommended that the supply name used | |
1783 | * should match the name used for the supply and/or the relevant | |
1784 | * device pins in the datasheet. | |
1785 | */ | |
1786 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) | |
1787 | { | |
a8bd42a9 | 1788 | return _regulator_get(dev, id, EXCLUSIVE_GET); |
5ffbd136 MB |
1789 | } |
1790 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); | |
1791 | ||
de1dd9fd MB |
1792 | /** |
1793 | * regulator_get_optional - obtain optional access to a regulator. | |
1794 | * @dev: device for regulator "consumer" | |
1795 | * @id: Supply name or regulator ID. | |
1796 | * | |
1797 | * Returns a struct regulator corresponding to the regulator producer, | |
69c3f723 | 1798 | * or IS_ERR() condition containing errno. |
de1dd9fd MB |
1799 | * |
1800 | * This is intended for use by consumers for devices which can have | |
1801 | * some supplies unconnected in normal use, such as some MMC devices. | |
1802 | * It can allow the regulator core to provide stub supplies for other | |
1803 | * supplies requested using normal regulator_get() calls without | |
1804 | * disrupting the operation of drivers that can handle absent | |
1805 | * supplies. | |
1806 | * | |
1807 | * Use of supply names configured via regulator_set_device_supply() is | |
1808 | * strongly encouraged. It is recommended that the supply name used | |
1809 | * should match the name used for the supply and/or the relevant | |
1810 | * device pins in the datasheet. | |
1811 | */ | |
1812 | struct regulator *regulator_get_optional(struct device *dev, const char *id) | |
1813 | { | |
a8bd42a9 | 1814 | return _regulator_get(dev, id, OPTIONAL_GET); |
de1dd9fd MB |
1815 | } |
1816 | EXPORT_SYMBOL_GPL(regulator_get_optional); | |
1817 | ||
83b0302d | 1818 | /* regulator_list_mutex lock held by regulator_put() */ |
23ff2f0f | 1819 | static void _regulator_put(struct regulator *regulator) |
414c70cb LG |
1820 | { |
1821 | struct regulator_dev *rdev; | |
1822 | ||
93576842 | 1823 | if (IS_ERR_OR_NULL(regulator)) |
414c70cb LG |
1824 | return; |
1825 | ||
70cfef26 KK |
1826 | lockdep_assert_held_once(®ulator_list_mutex); |
1827 | ||
414c70cb LG |
1828 | rdev = regulator->rdev; |
1829 | ||
5de70519 | 1830 | debugfs_remove_recursive(regulator->debugfs); |
5de70519 | 1831 | |
414c70cb | 1832 | /* remove any sysfs entries */ |
e2c98eaf | 1833 | if (regulator->dev) |
414c70cb | 1834 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
66cf9a7e | 1835 | regulator_lock(rdev); |
414c70cb | 1836 | list_del(®ulator->list); |
414c70cb | 1837 | |
5ffbd136 MB |
1838 | rdev->open_count--; |
1839 | rdev->exclusive = 0; | |
85f3b431 | 1840 | put_device(&rdev->dev); |
66cf9a7e | 1841 | regulator_unlock(rdev); |
5ffbd136 | 1842 | |
0630b614 | 1843 | kfree_const(regulator->supply_name); |
1768514e MB |
1844 | kfree(regulator); |
1845 | ||
414c70cb | 1846 | module_put(rdev->owner); |
23ff2f0f CK |
1847 | } |
1848 | ||
1849 | /** | |
1850 | * regulator_put - "free" the regulator source | |
1851 | * @regulator: regulator source | |
1852 | * | |
1853 | * Note: drivers must ensure that all regulator_enable calls made on this | |
1854 | * regulator source are balanced by regulator_disable calls prior to calling | |
1855 | * this function. | |
1856 | */ | |
1857 | void regulator_put(struct regulator *regulator) | |
1858 | { | |
1859 | mutex_lock(®ulator_list_mutex); | |
1860 | _regulator_put(regulator); | |
414c70cb LG |
1861 | mutex_unlock(®ulator_list_mutex); |
1862 | } | |
1863 | EXPORT_SYMBOL_GPL(regulator_put); | |
1864 | ||
a06ccd9c CK |
1865 | /** |
1866 | * regulator_register_supply_alias - Provide device alias for supply lookup | |
1867 | * | |
1868 | * @dev: device that will be given as the regulator "consumer" | |
1869 | * @id: Supply name or regulator ID | |
1870 | * @alias_dev: device that should be used to lookup the supply | |
1871 | * @alias_id: Supply name or regulator ID that should be used to lookup the | |
1872 | * supply | |
1873 | * | |
1874 | * All lookups for id on dev will instead be conducted for alias_id on | |
1875 | * alias_dev. | |
1876 | */ | |
1877 | int regulator_register_supply_alias(struct device *dev, const char *id, | |
1878 | struct device *alias_dev, | |
1879 | const char *alias_id) | |
1880 | { | |
1881 | struct regulator_supply_alias *map; | |
1882 | ||
1883 | map = regulator_find_supply_alias(dev, id); | |
1884 | if (map) | |
1885 | return -EEXIST; | |
1886 | ||
1887 | map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL); | |
1888 | if (!map) | |
1889 | return -ENOMEM; | |
1890 | ||
1891 | map->src_dev = dev; | |
1892 | map->src_supply = id; | |
1893 | map->alias_dev = alias_dev; | |
1894 | map->alias_supply = alias_id; | |
1895 | ||
1896 | list_add(&map->list, ®ulator_supply_alias_list); | |
1897 | ||
1898 | pr_info("Adding alias for supply %s,%s -> %s,%s\n", | |
1899 | id, dev_name(dev), alias_id, dev_name(alias_dev)); | |
1900 | ||
1901 | return 0; | |
1902 | } | |
1903 | EXPORT_SYMBOL_GPL(regulator_register_supply_alias); | |
1904 | ||
1905 | /** | |
1906 | * regulator_unregister_supply_alias - Remove device alias | |
1907 | * | |
1908 | * @dev: device that will be given as the regulator "consumer" | |
1909 | * @id: Supply name or regulator ID | |
1910 | * | |
1911 | * Remove a lookup alias if one exists for id on dev. | |
1912 | */ | |
1913 | void regulator_unregister_supply_alias(struct device *dev, const char *id) | |
1914 | { | |
1915 | struct regulator_supply_alias *map; | |
1916 | ||
1917 | map = regulator_find_supply_alias(dev, id); | |
1918 | if (map) { | |
1919 | list_del(&map->list); | |
1920 | kfree(map); | |
1921 | } | |
1922 | } | |
1923 | EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias); | |
1924 | ||
1925 | /** | |
1926 | * regulator_bulk_register_supply_alias - register multiple aliases | |
1927 | * | |
1928 | * @dev: device that will be given as the regulator "consumer" | |
1929 | * @id: List of supply names or regulator IDs | |
1930 | * @alias_dev: device that should be used to lookup the supply | |
1931 | * @alias_id: List of supply names or regulator IDs that should be used to | |
1932 | * lookup the supply | |
1933 | * @num_id: Number of aliases to register | |
1934 | * | |
1935 | * @return 0 on success, an errno on failure. | |
1936 | * | |
1937 | * This helper function allows drivers to register several supply | |
1938 | * aliases in one operation. If any of the aliases cannot be | |
1939 | * registered any aliases that were registered will be removed | |
1940 | * before returning to the caller. | |
1941 | */ | |
9f8c0fe9 LJ |
1942 | int regulator_bulk_register_supply_alias(struct device *dev, |
1943 | const char *const *id, | |
a06ccd9c | 1944 | struct device *alias_dev, |
9f8c0fe9 | 1945 | const char *const *alias_id, |
a06ccd9c CK |
1946 | int num_id) |
1947 | { | |
1948 | int i; | |
1949 | int ret; | |
1950 | ||
1951 | for (i = 0; i < num_id; ++i) { | |
1952 | ret = regulator_register_supply_alias(dev, id[i], alias_dev, | |
1953 | alias_id[i]); | |
1954 | if (ret < 0) | |
1955 | goto err; | |
1956 | } | |
1957 | ||
1958 | return 0; | |
1959 | ||
1960 | err: | |
1961 | dev_err(dev, | |
1962 | "Failed to create supply alias %s,%s -> %s,%s\n", | |
1963 | id[i], dev_name(dev), alias_id[i], dev_name(alias_dev)); | |
1964 | ||
1965 | while (--i >= 0) | |
1966 | regulator_unregister_supply_alias(dev, id[i]); | |
1967 | ||
1968 | return ret; | |
1969 | } | |
1970 | EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias); | |
1971 | ||
1972 | /** | |
1973 | * regulator_bulk_unregister_supply_alias - unregister multiple aliases | |
1974 | * | |
1975 | * @dev: device that will be given as the regulator "consumer" | |
1976 | * @id: List of supply names or regulator IDs | |
1977 | * @num_id: Number of aliases to unregister | |
1978 | * | |
1979 | * This helper function allows drivers to unregister several supply | |
1980 | * aliases in one operation. | |
1981 | */ | |
1982 | void regulator_bulk_unregister_supply_alias(struct device *dev, | |
9f8c0fe9 | 1983 | const char *const *id, |
a06ccd9c CK |
1984 | int num_id) |
1985 | { | |
1986 | int i; | |
1987 | ||
1988 | for (i = 0; i < num_id; ++i) | |
1989 | regulator_unregister_supply_alias(dev, id[i]); | |
1990 | } | |
1991 | EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias); | |
1992 | ||
1993 | ||
f19b00da KM |
1994 | /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */ |
1995 | static int regulator_ena_gpio_request(struct regulator_dev *rdev, | |
1996 | const struct regulator_config *config) | |
1997 | { | |
1998 | struct regulator_enable_gpio *pin; | |
778b28b4 | 1999 | struct gpio_desc *gpiod; |
f19b00da KM |
2000 | int ret; |
2001 | ||
e45e290a LW |
2002 | if (config->ena_gpiod) |
2003 | gpiod = config->ena_gpiod; | |
2004 | else | |
2005 | gpiod = gpio_to_desc(config->ena_gpio); | |
778b28b4 | 2006 | |
f19b00da | 2007 | list_for_each_entry(pin, ®ulator_ena_gpio_list, list) { |
778b28b4 | 2008 | if (pin->gpiod == gpiod) { |
f19b00da KM |
2009 | rdev_dbg(rdev, "GPIO %d is already used\n", |
2010 | config->ena_gpio); | |
2011 | goto update_ena_gpio_to_rdev; | |
2012 | } | |
2013 | } | |
2014 | ||
e45e290a LW |
2015 | if (!config->ena_gpiod) { |
2016 | ret = gpio_request_one(config->ena_gpio, | |
2017 | GPIOF_DIR_OUT | config->ena_gpio_flags, | |
2018 | rdev_get_name(rdev)); | |
2019 | if (ret) | |
2020 | return ret; | |
2021 | } | |
f19b00da KM |
2022 | |
2023 | pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL); | |
2024 | if (pin == NULL) { | |
e45e290a LW |
2025 | if (!config->ena_gpiod) |
2026 | gpio_free(config->ena_gpio); | |
f19b00da KM |
2027 | return -ENOMEM; |
2028 | } | |
2029 | ||
778b28b4 | 2030 | pin->gpiod = gpiod; |
f19b00da KM |
2031 | pin->ena_gpio_invert = config->ena_gpio_invert; |
2032 | list_add(&pin->list, ®ulator_ena_gpio_list); | |
2033 | ||
2034 | update_ena_gpio_to_rdev: | |
2035 | pin->request_count++; | |
2036 | rdev->ena_pin = pin; | |
2037 | return 0; | |
2038 | } | |
2039 | ||
2040 | static void regulator_ena_gpio_free(struct regulator_dev *rdev) | |
2041 | { | |
2042 | struct regulator_enable_gpio *pin, *n; | |
2043 | ||
2044 | if (!rdev->ena_pin) | |
2045 | return; | |
2046 | ||
2047 | /* Free the GPIO only in case of no use */ | |
2048 | list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) { | |
778b28b4 | 2049 | if (pin->gpiod == rdev->ena_pin->gpiod) { |
f19b00da KM |
2050 | if (pin->request_count <= 1) { |
2051 | pin->request_count = 0; | |
778b28b4 | 2052 | gpiod_put(pin->gpiod); |
f19b00da KM |
2053 | list_del(&pin->list); |
2054 | kfree(pin); | |
60a2362f SWK |
2055 | rdev->ena_pin = NULL; |
2056 | return; | |
f19b00da KM |
2057 | } else { |
2058 | pin->request_count--; | |
2059 | } | |
2060 | } | |
2061 | } | |
2062 | } | |
2063 | ||
967cfb18 | 2064 | /** |
31d6eebf RD |
2065 | * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control |
2066 | * @rdev: regulator_dev structure | |
2067 | * @enable: enable GPIO at initial use? | |
2068 | * | |
967cfb18 KM |
2069 | * GPIO is enabled in case of initial use. (enable_count is 0) |
2070 | * GPIO is disabled when it is not shared any more. (enable_count <= 1) | |
2071 | */ | |
2072 | static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable) | |
2073 | { | |
2074 | struct regulator_enable_gpio *pin = rdev->ena_pin; | |
2075 | ||
2076 | if (!pin) | |
2077 | return -EINVAL; | |
2078 | ||
2079 | if (enable) { | |
2080 | /* Enable GPIO at initial use */ | |
2081 | if (pin->enable_count == 0) | |
778b28b4 RK |
2082 | gpiod_set_value_cansleep(pin->gpiod, |
2083 | !pin->ena_gpio_invert); | |
967cfb18 KM |
2084 | |
2085 | pin->enable_count++; | |
2086 | } else { | |
2087 | if (pin->enable_count > 1) { | |
2088 | pin->enable_count--; | |
2089 | return 0; | |
2090 | } | |
2091 | ||
2092 | /* Disable GPIO if not used */ | |
2093 | if (pin->enable_count <= 1) { | |
778b28b4 RK |
2094 | gpiod_set_value_cansleep(pin->gpiod, |
2095 | pin->ena_gpio_invert); | |
967cfb18 KM |
2096 | pin->enable_count = 0; |
2097 | } | |
2098 | } | |
2099 | ||
2100 | return 0; | |
2101 | } | |
2102 | ||
79fd1141 GX |
2103 | /** |
2104 | * _regulator_enable_delay - a delay helper function | |
2105 | * @delay: time to delay in microseconds | |
2106 | * | |
2107 | * Delay for the requested amount of time as per the guidelines in: | |
2108 | * | |
2109 | * Documentation/timers/timers-howto.txt | |
2110 | * | |
2111 | * The assumption here is that regulators will never be enabled in | |
2112 | * atomic context and therefore sleeping functions can be used. | |
2113 | */ | |
2114 | static void _regulator_enable_delay(unsigned int delay) | |
2115 | { | |
2116 | unsigned int ms = delay / 1000; | |
2117 | unsigned int us = delay % 1000; | |
2118 | ||
2119 | if (ms > 0) { | |
2120 | /* | |
2121 | * For small enough values, handle super-millisecond | |
2122 | * delays in the usleep_range() call below. | |
2123 | */ | |
2124 | if (ms < 20) | |
2125 | us += ms * 1000; | |
2126 | else | |
2127 | msleep(ms); | |
2128 | } | |
2129 | ||
2130 | /* | |
2131 | * Give the scheduler some room to coalesce with any other | |
2132 | * wakeup sources. For delays shorter than 10 us, don't even | |
2133 | * bother setting up high-resolution timers and just busy- | |
2134 | * loop. | |
2135 | */ | |
2136 | if (us >= 10) | |
2137 | usleep_range(us, us + 100); | |
2138 | else | |
2139 | udelay(us); | |
2140 | } | |
2141 | ||
5c5659d0 MB |
2142 | static int _regulator_do_enable(struct regulator_dev *rdev) |
2143 | { | |
2144 | int ret, delay; | |
2145 | ||
2146 | /* Query before enabling in case configuration dependent. */ | |
2147 | ret = _regulator_get_enable_time(rdev); | |
2148 | if (ret >= 0) { | |
2149 | delay = ret; | |
2150 | } else { | |
2151 | rdev_warn(rdev, "enable_time() failed: %d\n", ret); | |
2152 | delay = 0; | |
2153 | } | |
2154 | ||
2155 | trace_regulator_enable(rdev_get_name(rdev)); | |
2156 | ||
871f5650 GX |
2157 | if (rdev->desc->off_on_delay) { |
2158 | /* if needed, keep a distance of off_on_delay from last time | |
2159 | * this regulator was disabled. | |
2160 | */ | |
2161 | unsigned long start_jiffy = jiffies; | |
2162 | unsigned long intended, max_delay, remaining; | |
2163 | ||
2164 | max_delay = usecs_to_jiffies(rdev->desc->off_on_delay); | |
2165 | intended = rdev->last_off_jiffy + max_delay; | |
2166 | ||
2167 | if (time_before(start_jiffy, intended)) { | |
2168 | /* calc remaining jiffies to deal with one-time | |
2169 | * timer wrapping. | |
2170 | * in case of multiple timer wrapping, either it can be | |
2171 | * detected by out-of-range remaining, or it cannot be | |
2172 | * detected and we gets a panelty of | |
2173 | * _regulator_enable_delay(). | |
2174 | */ | |
2175 | remaining = intended - start_jiffy; | |
2176 | if (remaining <= max_delay) | |
2177 | _regulator_enable_delay( | |
2178 | jiffies_to_usecs(remaining)); | |
2179 | } | |
2180 | } | |
2181 | ||
967cfb18 | 2182 | if (rdev->ena_pin) { |
29d62ec5 DA |
2183 | if (!rdev->ena_gpio_state) { |
2184 | ret = regulator_ena_gpio_ctrl(rdev, true); | |
2185 | if (ret < 0) | |
2186 | return ret; | |
2187 | rdev->ena_gpio_state = 1; | |
2188 | } | |
65f73508 | 2189 | } else if (rdev->desc->ops->enable) { |
5c5659d0 MB |
2190 | ret = rdev->desc->ops->enable(rdev); |
2191 | if (ret < 0) | |
2192 | return ret; | |
2193 | } else { | |
2194 | return -EINVAL; | |
2195 | } | |
2196 | ||
2197 | /* Allow the regulator to ramp; it would be useful to extend | |
2198 | * this for bulk operations so that the regulators can ramp | |
2199 | * together. */ | |
2200 | trace_regulator_enable_delay(rdev_get_name(rdev)); | |
2201 | ||
79fd1141 | 2202 | _regulator_enable_delay(delay); |
5c5659d0 MB |
2203 | |
2204 | trace_regulator_enable_complete(rdev_get_name(rdev)); | |
2205 | ||
2206 | return 0; | |
2207 | } | |
2208 | ||
414c70cb LG |
2209 | /* locks held by regulator_enable() */ |
2210 | static int _regulator_enable(struct regulator_dev *rdev) | |
2211 | { | |
5c5659d0 | 2212 | int ret; |
414c70cb | 2213 | |
70cfef26 KK |
2214 | lockdep_assert_held_once(&rdev->mutex); |
2215 | ||
414c70cb | 2216 | /* check voltage and requested load before enabling */ |
8a34e979 | 2217 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
9a2372fa | 2218 | drms_uA_update(rdev); |
414c70cb | 2219 | |
9a2372fa MB |
2220 | if (rdev->use_count == 0) { |
2221 | /* The regulator may on if it's not switchable or left on */ | |
2222 | ret = _regulator_is_enabled(rdev); | |
2223 | if (ret == -EINVAL || ret == 0) { | |
8a34e979 WP |
2224 | if (!regulator_ops_is_valid(rdev, |
2225 | REGULATOR_CHANGE_STATUS)) | |
9a2372fa MB |
2226 | return -EPERM; |
2227 | ||
5c5659d0 | 2228 | ret = _regulator_do_enable(rdev); |
31aae2be MB |
2229 | if (ret < 0) |
2230 | return ret; | |
2231 | ||
264b88c9 HG |
2232 | _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE, |
2233 | NULL); | |
a7433cff | 2234 | } else if (ret < 0) { |
5da84fd9 | 2235 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
414c70cb LG |
2236 | return ret; |
2237 | } | |
a7433cff | 2238 | /* Fallthrough on positive return values - already enabled */ |
414c70cb LG |
2239 | } |
2240 | ||
9a2372fa MB |
2241 | rdev->use_count++; |
2242 | ||
2243 | return 0; | |
414c70cb LG |
2244 | } |
2245 | ||
2246 | /** | |
2247 | * regulator_enable - enable regulator output | |
2248 | * @regulator: regulator source | |
2249 | * | |
cf7bbcdf MB |
2250 | * Request that the regulator be enabled with the regulator output at |
2251 | * the predefined voltage or current value. Calls to regulator_enable() | |
2252 | * must be balanced with calls to regulator_disable(). | |
2253 | * | |
414c70cb | 2254 | * NOTE: the output value can be set by other drivers, boot loader or may be |
cf7bbcdf | 2255 | * hardwired in the regulator. |
414c70cb LG |
2256 | */ |
2257 | int regulator_enable(struct regulator *regulator) | |
2258 | { | |
412aec61 DB |
2259 | struct regulator_dev *rdev = regulator->rdev; |
2260 | int ret = 0; | |
414c70cb | 2261 | |
6492bc1b MB |
2262 | if (regulator->always_on) |
2263 | return 0; | |
2264 | ||
3801b86a MB |
2265 | if (rdev->supply) { |
2266 | ret = regulator_enable(rdev->supply); | |
2267 | if (ret != 0) | |
2268 | return ret; | |
2269 | } | |
2270 | ||
38de19fa | 2271 | mutex_lock(&rdev->mutex); |
cd94b505 | 2272 | ret = _regulator_enable(rdev); |
38de19fa | 2273 | mutex_unlock(&rdev->mutex); |
3801b86a | 2274 | |
d1685e4e | 2275 | if (ret != 0 && rdev->supply) |
3801b86a MB |
2276 | regulator_disable(rdev->supply); |
2277 | ||
414c70cb LG |
2278 | return ret; |
2279 | } | |
2280 | EXPORT_SYMBOL_GPL(regulator_enable); | |
2281 | ||
5c5659d0 MB |
2282 | static int _regulator_do_disable(struct regulator_dev *rdev) |
2283 | { | |
2284 | int ret; | |
2285 | ||
2286 | trace_regulator_disable(rdev_get_name(rdev)); | |
2287 | ||
967cfb18 | 2288 | if (rdev->ena_pin) { |
29d62ec5 DA |
2289 | if (rdev->ena_gpio_state) { |
2290 | ret = regulator_ena_gpio_ctrl(rdev, false); | |
2291 | if (ret < 0) | |
2292 | return ret; | |
2293 | rdev->ena_gpio_state = 0; | |
2294 | } | |
5c5659d0 MB |
2295 | |
2296 | } else if (rdev->desc->ops->disable) { | |
2297 | ret = rdev->desc->ops->disable(rdev); | |
2298 | if (ret != 0) | |
2299 | return ret; | |
2300 | } | |
2301 | ||
871f5650 GX |
2302 | /* cares about last_off_jiffy only if off_on_delay is required by |
2303 | * device. | |
2304 | */ | |
2305 | if (rdev->desc->off_on_delay) | |
2306 | rdev->last_off_jiffy = jiffies; | |
2307 | ||
5c5659d0 MB |
2308 | trace_regulator_disable_complete(rdev_get_name(rdev)); |
2309 | ||
5c5659d0 MB |
2310 | return 0; |
2311 | } | |
2312 | ||
414c70cb | 2313 | /* locks held by regulator_disable() */ |
3801b86a | 2314 | static int _regulator_disable(struct regulator_dev *rdev) |
414c70cb LG |
2315 | { |
2316 | int ret = 0; | |
2317 | ||
70cfef26 KK |
2318 | lockdep_assert_held_once(&rdev->mutex); |
2319 | ||
cd94b505 | 2320 | if (WARN(rdev->use_count <= 0, |
43e7ee33 | 2321 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
cd94b505 DB |
2322 | return -EIO; |
2323 | ||
414c70cb | 2324 | /* are we the last user and permitted to disable ? */ |
60ef66fc MB |
2325 | if (rdev->use_count == 1 && |
2326 | (rdev->constraints && !rdev->constraints->always_on)) { | |
414c70cb LG |
2327 | |
2328 | /* we are last user */ | |
8a34e979 | 2329 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { |
a1c8a551 RF |
2330 | ret = _notifier_call_chain(rdev, |
2331 | REGULATOR_EVENT_PRE_DISABLE, | |
2332 | NULL); | |
2333 | if (ret & NOTIFY_STOP_MASK) | |
2334 | return -EINVAL; | |
2335 | ||
5c5659d0 | 2336 | ret = _regulator_do_disable(rdev); |
414c70cb | 2337 | if (ret < 0) { |
5da84fd9 | 2338 | rdev_err(rdev, "failed to disable\n"); |
a1c8a551 RF |
2339 | _notifier_call_chain(rdev, |
2340 | REGULATOR_EVENT_ABORT_DISABLE, | |
2341 | NULL); | |
414c70cb LG |
2342 | return ret; |
2343 | } | |
66fda75f MP |
2344 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
2345 | NULL); | |
414c70cb LG |
2346 | } |
2347 | ||
414c70cb LG |
2348 | rdev->use_count = 0; |
2349 | } else if (rdev->use_count > 1) { | |
8a34e979 | 2350 | if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) |
414c70cb LG |
2351 | drms_uA_update(rdev); |
2352 | ||
2353 | rdev->use_count--; | |
2354 | } | |
3801b86a | 2355 | |
414c70cb LG |
2356 | return ret; |
2357 | } | |
2358 | ||
2359 | /** | |
2360 | * regulator_disable - disable regulator output | |
2361 | * @regulator: regulator source | |
2362 | * | |
cf7bbcdf MB |
2363 | * Disable the regulator output voltage or current. Calls to |
2364 | * regulator_enable() must be balanced with calls to | |
2365 | * regulator_disable(). | |
69279fb9 | 2366 | * |
414c70cb | 2367 | * NOTE: this will only disable the regulator output if no other consumer |
cf7bbcdf MB |
2368 | * devices have it enabled, the regulator device supports disabling and |
2369 | * machine constraints permit this operation. | |
414c70cb LG |
2370 | */ |
2371 | int regulator_disable(struct regulator *regulator) | |
2372 | { | |
412aec61 DB |
2373 | struct regulator_dev *rdev = regulator->rdev; |
2374 | int ret = 0; | |
414c70cb | 2375 | |
6492bc1b MB |
2376 | if (regulator->always_on) |
2377 | return 0; | |
2378 | ||
38de19fa | 2379 | mutex_lock(&rdev->mutex); |
3801b86a | 2380 | ret = _regulator_disable(rdev); |
38de19fa | 2381 | mutex_unlock(&rdev->mutex); |
8cbf811d | 2382 | |
3801b86a MB |
2383 | if (ret == 0 && rdev->supply) |
2384 | regulator_disable(rdev->supply); | |
8cbf811d | 2385 | |
414c70cb LG |
2386 | return ret; |
2387 | } | |
2388 | EXPORT_SYMBOL_GPL(regulator_disable); | |
2389 | ||
2390 | /* locks held by regulator_force_disable() */ | |
3801b86a | 2391 | static int _regulator_force_disable(struct regulator_dev *rdev) |
414c70cb LG |
2392 | { |
2393 | int ret = 0; | |
2394 | ||
70cfef26 KK |
2395 | lockdep_assert_held_once(&rdev->mutex); |
2396 | ||
a1c8a551 RF |
2397 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2398 | REGULATOR_EVENT_PRE_DISABLE, NULL); | |
2399 | if (ret & NOTIFY_STOP_MASK) | |
2400 | return -EINVAL; | |
2401 | ||
66fda75f MP |
2402 | ret = _regulator_do_disable(rdev); |
2403 | if (ret < 0) { | |
2404 | rdev_err(rdev, "failed to force disable\n"); | |
a1c8a551 RF |
2405 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2406 | REGULATOR_EVENT_ABORT_DISABLE, NULL); | |
66fda75f | 2407 | return ret; |
414c70cb LG |
2408 | } |
2409 | ||
66fda75f MP |
2410 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
2411 | REGULATOR_EVENT_DISABLE, NULL); | |
2412 | ||
2413 | return 0; | |
414c70cb LG |
2414 | } |
2415 | ||
2416 | /** | |
2417 | * regulator_force_disable - force disable regulator output | |
2418 | * @regulator: regulator source | |
2419 | * | |
2420 | * Forcibly disable the regulator output voltage or current. | |
2421 | * NOTE: this *will* disable the regulator output even if other consumer | |
2422 | * devices have it enabled. This should be used for situations when device | |
2423 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | |
2424 | */ | |
2425 | int regulator_force_disable(struct regulator *regulator) | |
2426 | { | |
82d15839 | 2427 | struct regulator_dev *rdev = regulator->rdev; |
414c70cb LG |
2428 | int ret; |
2429 | ||
38de19fa | 2430 | mutex_lock(&rdev->mutex); |
414c70cb | 2431 | regulator->uA_load = 0; |
3801b86a | 2432 | ret = _regulator_force_disable(regulator->rdev); |
38de19fa | 2433 | mutex_unlock(&rdev->mutex); |
8cbf811d | 2434 | |
3801b86a MB |
2435 | if (rdev->supply) |
2436 | while (rdev->open_count--) | |
2437 | regulator_disable(rdev->supply); | |
8cbf811d | 2438 | |
414c70cb LG |
2439 | return ret; |
2440 | } | |
2441 | EXPORT_SYMBOL_GPL(regulator_force_disable); | |
2442 | ||
da07ecd9 MB |
2443 | static void regulator_disable_work(struct work_struct *work) |
2444 | { | |
2445 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, | |
2446 | disable_work.work); | |
2447 | int count, i, ret; | |
2448 | ||
66cf9a7e | 2449 | regulator_lock(rdev); |
da07ecd9 MB |
2450 | |
2451 | BUG_ON(!rdev->deferred_disables); | |
2452 | ||
2453 | count = rdev->deferred_disables; | |
2454 | rdev->deferred_disables = 0; | |
2455 | ||
c9ccaa0c TR |
2456 | /* |
2457 | * Workqueue functions queue the new work instance while the previous | |
2458 | * work instance is being processed. Cancel the queued work instance | |
2459 | * as the work instance under processing does the job of the queued | |
2460 | * work instance. | |
2461 | */ | |
2462 | cancel_delayed_work(&rdev->disable_work); | |
2463 | ||
da07ecd9 MB |
2464 | for (i = 0; i < count; i++) { |
2465 | ret = _regulator_disable(rdev); | |
2466 | if (ret != 0) | |
2467 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); | |
2468 | } | |
2469 | ||
66cf9a7e | 2470 | regulator_unlock(rdev); |
da07ecd9 MB |
2471 | |
2472 | if (rdev->supply) { | |
2473 | for (i = 0; i < count; i++) { | |
2474 | ret = regulator_disable(rdev->supply); | |
2475 | if (ret != 0) { | |
2476 | rdev_err(rdev, | |
2477 | "Supply disable failed: %d\n", ret); | |
2478 | } | |
2479 | } | |
2480 | } | |
2481 | } | |
2482 | ||
2483 | /** | |
2484 | * regulator_disable_deferred - disable regulator output with delay | |
2485 | * @regulator: regulator source | |
2486 | * @ms: miliseconds until the regulator is disabled | |
2487 | * | |
2488 | * Execute regulator_disable() on the regulator after a delay. This | |
2489 | * is intended for use with devices that require some time to quiesce. | |
2490 | * | |
2491 | * NOTE: this will only disable the regulator output if no other consumer | |
2492 | * devices have it enabled, the regulator device supports disabling and | |
2493 | * machine constraints permit this operation. | |
2494 | */ | |
2495 | int regulator_disable_deferred(struct regulator *regulator, int ms) | |
2496 | { | |
2497 | struct regulator_dev *rdev = regulator->rdev; | |
2498 | ||
6492bc1b MB |
2499 | if (regulator->always_on) |
2500 | return 0; | |
2501 | ||
2b5a24a0 MB |
2502 | if (!ms) |
2503 | return regulator_disable(regulator); | |
2504 | ||
66cf9a7e | 2505 | regulator_lock(rdev); |
da07ecd9 | 2506 | rdev->deferred_disables++; |
c9ccaa0c TR |
2507 | mod_delayed_work(system_power_efficient_wq, &rdev->disable_work, |
2508 | msecs_to_jiffies(ms)); | |
66cf9a7e | 2509 | regulator_unlock(rdev); |
da07ecd9 | 2510 | |
70dc6daf | 2511 | return 0; |
da07ecd9 MB |
2512 | } |
2513 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); | |
2514 | ||
414c70cb LG |
2515 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
2516 | { | |
65f73508 | 2517 | /* A GPIO control always takes precedence */ |
7b74d149 | 2518 | if (rdev->ena_pin) |
65f73508 MB |
2519 | return rdev->ena_gpio_state; |
2520 | ||
9a7f6a4c | 2521 | /* If we don't know then assume that the regulator is always on */ |
9332546f | 2522 | if (!rdev->desc->ops->is_enabled) |
9a7f6a4c | 2523 | return 1; |
414c70cb | 2524 | |
9332546f | 2525 | return rdev->desc->ops->is_enabled(rdev); |
414c70cb LG |
2526 | } |
2527 | ||
3d67fe95 MP |
2528 | static int _regulator_list_voltage(struct regulator_dev *rdev, |
2529 | unsigned selector, int lock) | |
3a40cfc3 | 2530 | { |
3a40cfc3 SH |
2531 | const struct regulator_ops *ops = rdev->desc->ops; |
2532 | int ret; | |
2533 | ||
2534 | if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector) | |
2535 | return rdev->desc->fixed_uV; | |
2536 | ||
2537 | if (ops->list_voltage) { | |
2538 | if (selector >= rdev->desc->n_voltages) | |
2539 | return -EINVAL; | |
2540 | if (lock) | |
66cf9a7e | 2541 | regulator_lock(rdev); |
3a40cfc3 SH |
2542 | ret = ops->list_voltage(rdev, selector); |
2543 | if (lock) | |
66cf9a7e | 2544 | regulator_unlock(rdev); |
fd086045 | 2545 | } else if (rdev->is_switch && rdev->supply) { |
3d67fe95 MP |
2546 | ret = _regulator_list_voltage(rdev->supply->rdev, |
2547 | selector, lock); | |
3a40cfc3 SH |
2548 | } else { |
2549 | return -EINVAL; | |
2550 | } | |
2551 | ||
2552 | if (ret > 0) { | |
2553 | if (ret < rdev->constraints->min_uV) | |
2554 | ret = 0; | |
2555 | else if (ret > rdev->constraints->max_uV) | |
2556 | ret = 0; | |
2557 | } | |
2558 | ||
2559 | return ret; | |
2560 | } | |
2561 | ||
414c70cb LG |
2562 | /** |
2563 | * regulator_is_enabled - is the regulator output enabled | |
2564 | * @regulator: regulator source | |
2565 | * | |
412aec61 DB |
2566 | * Returns positive if the regulator driver backing the source/client |
2567 | * has requested that the device be enabled, zero if it hasn't, else a | |
2568 | * negative errno code. | |
2569 | * | |
2570 | * Note that the device backing this regulator handle can have multiple | |
2571 | * users, so it might be enabled even if regulator_enable() was never | |
2572 | * called for this particular source. | |
414c70cb LG |
2573 | */ |
2574 | int regulator_is_enabled(struct regulator *regulator) | |
2575 | { | |
9332546f MB |
2576 | int ret; |
2577 | ||
6492bc1b MB |
2578 | if (regulator->always_on) |
2579 | return 1; | |
2580 | ||
38de19fa | 2581 | mutex_lock(®ulator->rdev->mutex); |
9332546f | 2582 | ret = _regulator_is_enabled(regulator->rdev); |
38de19fa | 2583 | mutex_unlock(®ulator->rdev->mutex); |
9332546f MB |
2584 | |
2585 | return ret; | |
414c70cb LG |
2586 | } |
2587 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | |
2588 | ||
4367cfdc DB |
2589 | /** |
2590 | * regulator_count_voltages - count regulator_list_voltage() selectors | |
2591 | * @regulator: regulator source | |
2592 | * | |
2593 | * Returns number of selectors, or negative errno. Selectors are | |
2594 | * numbered starting at zero, and typically correspond to bitfields | |
2595 | * in hardware registers. | |
2596 | */ | |
2597 | int regulator_count_voltages(struct regulator *regulator) | |
2598 | { | |
2599 | struct regulator_dev *rdev = regulator->rdev; | |
2600 | ||
26988efe JMC |
2601 | if (rdev->desc->n_voltages) |
2602 | return rdev->desc->n_voltages; | |
2603 | ||
fd086045 | 2604 | if (!rdev->is_switch || !rdev->supply) |
26988efe JMC |
2605 | return -EINVAL; |
2606 | ||
2607 | return regulator_count_voltages(rdev->supply); | |
4367cfdc DB |
2608 | } |
2609 | EXPORT_SYMBOL_GPL(regulator_count_voltages); | |
2610 | ||
2611 | /** | |
2612 | * regulator_list_voltage - enumerate supported voltages | |
2613 | * @regulator: regulator source | |
2614 | * @selector: identify voltage to list | |
2615 | * Context: can sleep | |
2616 | * | |
2617 | * Returns a voltage that can be passed to @regulator_set_voltage(), | |
88393161 | 2618 | * zero if this selector code can't be used on this system, or a |
4367cfdc DB |
2619 | * negative errno. |
2620 | */ | |
2621 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) | |
2622 | { | |
3d67fe95 | 2623 | return _regulator_list_voltage(regulator->rdev, selector, 1); |
4367cfdc DB |
2624 | } |
2625 | EXPORT_SYMBOL_GPL(regulator_list_voltage); | |
2626 | ||
04eca28c TT |
2627 | /** |
2628 | * regulator_get_regmap - get the regulator's register map | |
2629 | * @regulator: regulator source | |
2630 | * | |
2631 | * Returns the register map for the given regulator, or an ERR_PTR value | |
2632 | * if the regulator doesn't use regmap. | |
2633 | */ | |
2634 | struct regmap *regulator_get_regmap(struct regulator *regulator) | |
2635 | { | |
2636 | struct regmap *map = regulator->rdev->regmap; | |
2637 | ||
2638 | return map ? map : ERR_PTR(-EOPNOTSUPP); | |
2639 | } | |
2640 | ||
2641 | /** | |
2642 | * regulator_get_hardware_vsel_register - get the HW voltage selector register | |
2643 | * @regulator: regulator source | |
2644 | * @vsel_reg: voltage selector register, output parameter | |
2645 | * @vsel_mask: mask for voltage selector bitfield, output parameter | |
2646 | * | |
2647 | * Returns the hardware register offset and bitmask used for setting the | |
2648 | * regulator voltage. This might be useful when configuring voltage-scaling | |
2649 | * hardware or firmware that can make I2C requests behind the kernel's back, | |
2650 | * for example. | |
2651 | * | |
2652 | * On success, the output parameters @vsel_reg and @vsel_mask are filled in | |
2653 | * and 0 is returned, otherwise a negative errno is returned. | |
2654 | */ | |
2655 | int regulator_get_hardware_vsel_register(struct regulator *regulator, | |
2656 | unsigned *vsel_reg, | |
2657 | unsigned *vsel_mask) | |
2658 | { | |
39f5460d GX |
2659 | struct regulator_dev *rdev = regulator->rdev; |
2660 | const struct regulator_ops *ops = rdev->desc->ops; | |
04eca28c TT |
2661 | |
2662 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | |
2663 | return -EOPNOTSUPP; | |
2664 | ||
0d5c8633 CIK |
2665 | *vsel_reg = rdev->desc->vsel_reg; |
2666 | *vsel_mask = rdev->desc->vsel_mask; | |
04eca28c TT |
2667 | |
2668 | return 0; | |
2669 | } | |
2670 | EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register); | |
2671 | ||
2672 | /** | |
2673 | * regulator_list_hardware_vsel - get the HW-specific register value for a selector | |
2674 | * @regulator: regulator source | |
2675 | * @selector: identify voltage to list | |
2676 | * | |
2677 | * Converts the selector to a hardware-specific voltage selector that can be | |
2678 | * directly written to the regulator registers. The address of the voltage | |
2679 | * register can be determined by calling @regulator_get_hardware_vsel_register. | |
2680 | * | |
2681 | * On error a negative errno is returned. | |
2682 | */ | |
2683 | int regulator_list_hardware_vsel(struct regulator *regulator, | |
2684 | unsigned selector) | |
2685 | { | |
39f5460d GX |
2686 | struct regulator_dev *rdev = regulator->rdev; |
2687 | const struct regulator_ops *ops = rdev->desc->ops; | |
04eca28c TT |
2688 | |
2689 | if (selector >= rdev->desc->n_voltages) | |
2690 | return -EINVAL; | |
2691 | if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap) | |
2692 | return -EOPNOTSUPP; | |
2693 | ||
2694 | return selector; | |
2695 | } | |
2696 | EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel); | |
2697 | ||
2a668a8b PW |
2698 | /** |
2699 | * regulator_get_linear_step - return the voltage step size between VSEL values | |
2700 | * @regulator: regulator source | |
2701 | * | |
2702 | * Returns the voltage step size between VSEL values for linear | |
2703 | * regulators, or return 0 if the regulator isn't a linear regulator. | |
2704 | */ | |
2705 | unsigned int regulator_get_linear_step(struct regulator *regulator) | |
2706 | { | |
2707 | struct regulator_dev *rdev = regulator->rdev; | |
2708 | ||
2709 | return rdev->desc->uV_step; | |
2710 | } | |
2711 | EXPORT_SYMBOL_GPL(regulator_get_linear_step); | |
2712 | ||
a7a1ad90 MB |
2713 | /** |
2714 | * regulator_is_supported_voltage - check if a voltage range can be supported | |
2715 | * | |
2716 | * @regulator: Regulator to check. | |
2717 | * @min_uV: Minimum required voltage in uV. | |
2718 | * @max_uV: Maximum required voltage in uV. | |
2719 | * | |
2720 | * Returns a boolean or a negative error code. | |
2721 | */ | |
2722 | int regulator_is_supported_voltage(struct regulator *regulator, | |
2723 | int min_uV, int max_uV) | |
2724 | { | |
c5f3939b | 2725 | struct regulator_dev *rdev = regulator->rdev; |
a7a1ad90 MB |
2726 | int i, voltages, ret; |
2727 | ||
c5f3939b | 2728 | /* If we can't change voltage check the current voltage */ |
8a34e979 | 2729 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
c5f3939b MB |
2730 | ret = regulator_get_voltage(regulator); |
2731 | if (ret >= 0) | |
0d25d09d | 2732 | return min_uV <= ret && ret <= max_uV; |
c5f3939b MB |
2733 | else |
2734 | return ret; | |
2735 | } | |
2736 | ||
bd7a2b60 PM |
2737 | /* Any voltage within constrains range is fine? */ |
2738 | if (rdev->desc->continuous_voltage_range) | |
2739 | return min_uV >= rdev->constraints->min_uV && | |
2740 | max_uV <= rdev->constraints->max_uV; | |
2741 | ||
a7a1ad90 MB |
2742 | ret = regulator_count_voltages(regulator); |
2743 | if (ret < 0) | |
2744 | return ret; | |
2745 | voltages = ret; | |
2746 | ||
2747 | for (i = 0; i < voltages; i++) { | |
2748 | ret = regulator_list_voltage(regulator, i); | |
2749 | ||
2750 | if (ret >= min_uV && ret <= max_uV) | |
2751 | return 1; | |
2752 | } | |
2753 | ||
2754 | return 0; | |
2755 | } | |
a398eaa2 | 2756 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
a7a1ad90 | 2757 | |
a204f41e SH |
2758 | static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV, |
2759 | int max_uV) | |
2760 | { | |
2761 | const struct regulator_desc *desc = rdev->desc; | |
2762 | ||
2763 | if (desc->ops->map_voltage) | |
2764 | return desc->ops->map_voltage(rdev, min_uV, max_uV); | |
2765 | ||
2766 | if (desc->ops->list_voltage == regulator_list_voltage_linear) | |
2767 | return regulator_map_voltage_linear(rdev, min_uV, max_uV); | |
2768 | ||
2769 | if (desc->ops->list_voltage == regulator_list_voltage_linear_range) | |
2770 | return regulator_map_voltage_linear_range(rdev, min_uV, max_uV); | |
2771 | ||
2772 | return regulator_map_voltage_iterate(rdev, min_uV, max_uV); | |
2773 | } | |
2774 | ||
7179569a HS |
2775 | static int _regulator_call_set_voltage(struct regulator_dev *rdev, |
2776 | int min_uV, int max_uV, | |
2777 | unsigned *selector) | |
2778 | { | |
2779 | struct pre_voltage_change_data data; | |
2780 | int ret; | |
2781 | ||
2782 | data.old_uV = _regulator_get_voltage(rdev); | |
2783 | data.min_uV = min_uV; | |
2784 | data.max_uV = max_uV; | |
2785 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | |
2786 | &data); | |
2787 | if (ret & NOTIFY_STOP_MASK) | |
2788 | return -EINVAL; | |
2789 | ||
2790 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector); | |
2791 | if (ret >= 0) | |
2792 | return ret; | |
2793 | ||
2794 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | |
2795 | (void *)data.old_uV); | |
2796 | ||
2797 | return ret; | |
2798 | } | |
2799 | ||
2800 | static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev, | |
2801 | int uV, unsigned selector) | |
2802 | { | |
2803 | struct pre_voltage_change_data data; | |
2804 | int ret; | |
2805 | ||
2806 | data.old_uV = _regulator_get_voltage(rdev); | |
2807 | data.min_uV = uV; | |
2808 | data.max_uV = uV; | |
2809 | ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE, | |
2810 | &data); | |
2811 | if (ret & NOTIFY_STOP_MASK) | |
2812 | return -EINVAL; | |
2813 | ||
2814 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); | |
2815 | if (ret >= 0) | |
2816 | return ret; | |
2817 | ||
2818 | _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE, | |
2819 | (void *)data.old_uV); | |
2820 | ||
2821 | return ret; | |
2822 | } | |
2823 | ||
73e705bf MK |
2824 | static int _regulator_set_voltage_time(struct regulator_dev *rdev, |
2825 | int old_uV, int new_uV) | |
2826 | { | |
2827 | unsigned int ramp_delay = 0; | |
2828 | ||
2829 | if (rdev->constraints->ramp_delay) | |
2830 | ramp_delay = rdev->constraints->ramp_delay; | |
2831 | else if (rdev->desc->ramp_delay) | |
2832 | ramp_delay = rdev->desc->ramp_delay; | |
d6c1dc3f LD |
2833 | else if (rdev->constraints->settling_time) |
2834 | return rdev->constraints->settling_time; | |
3ffad468 MK |
2835 | else if (rdev->constraints->settling_time_up && |
2836 | (new_uV > old_uV)) | |
2837 | return rdev->constraints->settling_time_up; | |
2838 | else if (rdev->constraints->settling_time_down && | |
2839 | (new_uV < old_uV)) | |
2840 | return rdev->constraints->settling_time_down; | |
73e705bf MK |
2841 | |
2842 | if (ramp_delay == 0) { | |
ba14fa1a | 2843 | rdev_dbg(rdev, "ramp_delay not set\n"); |
73e705bf MK |
2844 | return 0; |
2845 | } | |
2846 | ||
2847 | return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay); | |
2848 | } | |
2849 | ||
75790251 MB |
2850 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
2851 | int min_uV, int max_uV) | |
2852 | { | |
2853 | int ret; | |
77af1b26 | 2854 | int delay = 0; |
e113d792 | 2855 | int best_val = 0; |
75790251 | 2856 | unsigned int selector; |
eba41a5e | 2857 | int old_selector = -1; |
57995a48 | 2858 | const struct regulator_ops *ops = rdev->desc->ops; |
73e705bf | 2859 | int old_uV = _regulator_get_voltage(rdev); |
75790251 MB |
2860 | |
2861 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); | |
2862 | ||
bf5892a8 MB |
2863 | min_uV += rdev->constraints->uV_offset; |
2864 | max_uV += rdev->constraints->uV_offset; | |
2865 | ||
eba41a5e AL |
2866 | /* |
2867 | * If we can't obtain the old selector there is not enough | |
2868 | * info to call set_voltage_time_sel(). | |
2869 | */ | |
8b7485ef | 2870 | if (_regulator_is_enabled(rdev) && |
57995a48 MK |
2871 | ops->set_voltage_time_sel && ops->get_voltage_sel) { |
2872 | old_selector = ops->get_voltage_sel(rdev); | |
eba41a5e AL |
2873 | if (old_selector < 0) |
2874 | return old_selector; | |
2875 | } | |
2876 | ||
57995a48 | 2877 | if (ops->set_voltage) { |
7179569a HS |
2878 | ret = _regulator_call_set_voltage(rdev, min_uV, max_uV, |
2879 | &selector); | |
e113d792 MB |
2880 | |
2881 | if (ret >= 0) { | |
57995a48 MK |
2882 | if (ops->list_voltage) |
2883 | best_val = ops->list_voltage(rdev, | |
2884 | selector); | |
e113d792 MB |
2885 | else |
2886 | best_val = _regulator_get_voltage(rdev); | |
2887 | } | |
2888 | ||
57995a48 | 2889 | } else if (ops->set_voltage_sel) { |
a204f41e | 2890 | ret = regulator_map_voltage(rdev, min_uV, max_uV); |
e843fc46 | 2891 | if (ret >= 0) { |
57995a48 | 2892 | best_val = ops->list_voltage(rdev, ret); |
e113d792 MB |
2893 | if (min_uV <= best_val && max_uV >= best_val) { |
2894 | selector = ret; | |
c66a566a AL |
2895 | if (old_selector == selector) |
2896 | ret = 0; | |
2897 | else | |
7179569a HS |
2898 | ret = _regulator_call_set_voltage_sel( |
2899 | rdev, best_val, selector); | |
e113d792 MB |
2900 | } else { |
2901 | ret = -EINVAL; | |
2902 | } | |
e8eef82b | 2903 | } |
75790251 MB |
2904 | } else { |
2905 | ret = -EINVAL; | |
2906 | } | |
e8eef82b | 2907 | |
31dfe686 MK |
2908 | if (ret) |
2909 | goto out; | |
77af1b26 | 2910 | |
73e705bf MK |
2911 | if (ops->set_voltage_time_sel) { |
2912 | /* | |
2913 | * Call set_voltage_time_sel if successfully obtained | |
2914 | * old_selector | |
2915 | */ | |
2916 | if (old_selector >= 0 && old_selector != selector) | |
2917 | delay = ops->set_voltage_time_sel(rdev, old_selector, | |
2918 | selector); | |
2919 | } else { | |
2920 | if (old_uV != best_val) { | |
2921 | if (ops->set_voltage_time) | |
2922 | delay = ops->set_voltage_time(rdev, old_uV, | |
2923 | best_val); | |
2924 | else | |
2925 | delay = _regulator_set_voltage_time(rdev, | |
2926 | old_uV, | |
2927 | best_val); | |
e8eef82b | 2928 | } |
73e705bf | 2929 | } |
75790251 | 2930 | |
73e705bf MK |
2931 | if (delay < 0) { |
2932 | rdev_warn(rdev, "failed to get delay: %d\n", delay); | |
2933 | delay = 0; | |
77af1b26 LW |
2934 | } |
2935 | ||
73e705bf MK |
2936 | /* Insert any necessary delays */ |
2937 | if (delay >= 1000) { | |
2938 | mdelay(delay / 1000); | |
2939 | udelay(delay % 1000); | |
2940 | } else if (delay) { | |
2941 | udelay(delay); | |
77af1b26 LW |
2942 | } |
2943 | ||
31dfe686 | 2944 | if (best_val >= 0) { |
2f6c797f AL |
2945 | unsigned long data = best_val; |
2946 | ||
ded06a52 | 2947 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
2f6c797f AL |
2948 | (void *)data); |
2949 | } | |
ded06a52 | 2950 | |
31dfe686 | 2951 | out: |
eba41a5e | 2952 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); |
75790251 MB |
2953 | |
2954 | return ret; | |
2955 | } | |
2956 | ||
f7efad10 CZ |
2957 | static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev, |
2958 | int min_uV, int max_uV, suspend_state_t state) | |
2959 | { | |
2960 | struct regulator_state *rstate; | |
2961 | int uV, sel; | |
2962 | ||
2963 | rstate = regulator_get_suspend_state(rdev, state); | |
2964 | if (rstate == NULL) | |
2965 | return -EINVAL; | |
2966 | ||
2967 | if (min_uV < rstate->min_uV) | |
2968 | min_uV = rstate->min_uV; | |
2969 | if (max_uV > rstate->max_uV) | |
2970 | max_uV = rstate->max_uV; | |
2971 | ||
2972 | sel = regulator_map_voltage(rdev, min_uV, max_uV); | |
2973 | if (sel < 0) | |
2974 | return sel; | |
2975 | ||
2976 | uV = rdev->desc->ops->list_voltage(rdev, sel); | |
2977 | if (uV >= min_uV && uV <= max_uV) | |
2978 | rstate->uV = uV; | |
2979 | ||
2980 | return 0; | |
2981 | } | |
2982 | ||
a9f226bc | 2983 | static int regulator_set_voltage_unlocked(struct regulator *regulator, |
c360a6df CZ |
2984 | int min_uV, int max_uV, |
2985 | suspend_state_t state) | |
414c70cb LG |
2986 | { |
2987 | struct regulator_dev *rdev = regulator->rdev; | |
c360a6df | 2988 | struct regulator_voltage *voltage = ®ulator->voltage[state]; |
95a3c23a | 2989 | int ret = 0; |
92d7a558 | 2990 | int old_min_uV, old_max_uV; |
c00dc359 | 2991 | int current_uV; |
38de19fa MB |
2992 | int best_supply_uV = 0; |
2993 | int supply_change_uV = 0; | |
414c70cb | 2994 | |
95a3c23a MB |
2995 | /* If we're setting the same range as last time the change |
2996 | * should be a noop (some cpufreq implementations use the same | |
2997 | * voltage for multiple frequencies, for example). | |
2998 | */ | |
c360a6df | 2999 | if (voltage->min_uV == min_uV && voltage->max_uV == max_uV) |
95a3c23a MB |
3000 | goto out; |
3001 | ||
c00dc359 | 3002 | /* If we're trying to set a range that overlaps the current voltage, |
d3fb9800 | 3003 | * return successfully even though the regulator does not support |
c00dc359 BA |
3004 | * changing the voltage. |
3005 | */ | |
8a34e979 | 3006 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { |
c00dc359 BA |
3007 | current_uV = _regulator_get_voltage(rdev); |
3008 | if (min_uV <= current_uV && current_uV <= max_uV) { | |
c360a6df CZ |
3009 | voltage->min_uV = min_uV; |
3010 | voltage->max_uV = max_uV; | |
c00dc359 BA |
3011 | goto out; |
3012 | } | |
3013 | } | |
3014 | ||
414c70cb | 3015 | /* sanity check */ |
e8eef82b MB |
3016 | if (!rdev->desc->ops->set_voltage && |
3017 | !rdev->desc->ops->set_voltage_sel) { | |
414c70cb LG |
3018 | ret = -EINVAL; |
3019 | goto out; | |
3020 | } | |
3021 | ||
3022 | /* constraints check */ | |
3023 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
3024 | if (ret < 0) | |
3025 | goto out; | |
0d25d09d | 3026 | |
92d7a558 | 3027 | /* restore original values in case of error */ |
c360a6df CZ |
3028 | old_min_uV = voltage->min_uV; |
3029 | old_max_uV = voltage->max_uV; | |
3030 | voltage->min_uV = min_uV; | |
3031 | voltage->max_uV = max_uV; | |
3a93f2a9 | 3032 | |
c360a6df | 3033 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state); |
05fda3b1 | 3034 | if (ret < 0) |
92d7a558 | 3035 | goto out2; |
05fda3b1 | 3036 | |
43fc99f2 MB |
3037 | if (rdev->supply && |
3038 | regulator_ops_is_valid(rdev->supply->rdev, | |
3039 | REGULATOR_CHANGE_VOLTAGE) && | |
2c2874b1 TR |
3040 | (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage || |
3041 | rdev->desc->ops->get_voltage_sel))) { | |
fc42112c SH |
3042 | int current_supply_uV; |
3043 | int selector; | |
3044 | ||
3045 | selector = regulator_map_voltage(rdev, min_uV, max_uV); | |
3046 | if (selector < 0) { | |
3047 | ret = selector; | |
38de19fa | 3048 | goto out2; |
fc42112c SH |
3049 | } |
3050 | ||
00cb9f4f | 3051 | best_supply_uV = _regulator_list_voltage(rdev, selector, 0); |
fc42112c SH |
3052 | if (best_supply_uV < 0) { |
3053 | ret = best_supply_uV; | |
38de19fa | 3054 | goto out2; |
fc42112c SH |
3055 | } |
3056 | ||
3057 | best_supply_uV += rdev->desc->min_dropout_uV; | |
3058 | ||
3059 | current_supply_uV = _regulator_get_voltage(rdev->supply->rdev); | |
3060 | if (current_supply_uV < 0) { | |
3061 | ret = current_supply_uV; | |
38de19fa | 3062 | goto out2; |
fc42112c SH |
3063 | } |
3064 | ||
3065 | supply_change_uV = best_supply_uV - current_supply_uV; | |
3066 | } | |
3067 | ||
3068 | if (supply_change_uV > 0) { | |
3069 | ret = regulator_set_voltage_unlocked(rdev->supply, | |
c360a6df | 3070 | best_supply_uV, INT_MAX, state); |
fc42112c SH |
3071 | if (ret) { |
3072 | dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n", | |
3073 | ret); | |
38de19fa | 3074 | goto out2; |
fc42112c SH |
3075 | } |
3076 | } | |
3077 | ||
f7efad10 CZ |
3078 | if (state == PM_SUSPEND_ON) |
3079 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | |
3080 | else | |
3081 | ret = _regulator_do_set_suspend_voltage(rdev, min_uV, | |
3082 | max_uV, state); | |
92d7a558 | 3083 | if (ret < 0) |
38de19fa | 3084 | goto out2; |
0d25d09d | 3085 | |
fc42112c SH |
3086 | if (supply_change_uV < 0) { |
3087 | ret = regulator_set_voltage_unlocked(rdev->supply, | |
c360a6df | 3088 | best_supply_uV, INT_MAX, state); |
fc42112c SH |
3089 | if (ret) |
3090 | dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n", | |
3091 | ret); | |
3092 | /* No need to fail here */ | |
3093 | ret = 0; | |
3094 | } | |
3095 | ||
414c70cb | 3096 | out: |
a9f226bc | 3097 | return ret; |
38de19fa MB |
3098 | out2: |
3099 | voltage->min_uV = old_min_uV; | |
3100 | voltage->max_uV = old_max_uV; | |
69686176 | 3101 | |
69686176 MP |
3102 | return ret; |
3103 | } | |
3104 | ||
a9f226bc SH |
3105 | /** |
3106 | * regulator_set_voltage - set regulator output voltage | |
3107 | * @regulator: regulator source | |
3108 | * @min_uV: Minimum required voltage in uV | |
3109 | * @max_uV: Maximum acceptable voltage in uV | |
3110 | * | |
3111 | * Sets a voltage regulator to the desired output voltage. This can be set | |
3112 | * during any regulator state. IOW, regulator can be disabled or enabled. | |
3113 | * | |
3114 | * If the regulator is enabled then the voltage will change to the new value | |
3115 | * immediately otherwise if the regulator is disabled the regulator will | |
3116 | * output at the new voltage when enabled. | |
3117 | * | |
3118 | * NOTE: If the regulator is shared between several devices then the lowest | |
3119 | * request voltage that meets the system constraints will be used. | |
3120 | * Regulator system constraints must be set for this regulator before | |
3121 | * calling this function otherwise this call will fail. | |
3122 | */ | |
3123 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | |
3124 | { | |
3125 | int ret = 0; | |
3126 | ||
38de19fa | 3127 | regulator_lock_supply(regulator->rdev); |
a9f226bc | 3128 | |
c360a6df CZ |
3129 | ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV, |
3130 | PM_SUSPEND_ON); | |
a9f226bc | 3131 | |
38de19fa | 3132 | regulator_unlock_supply(regulator->rdev); |
a9f226bc | 3133 | |
414c70cb LG |
3134 | return ret; |
3135 | } | |
3136 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | |
3137 | ||
f7efad10 CZ |
3138 | static inline int regulator_suspend_toggle(struct regulator_dev *rdev, |
3139 | suspend_state_t state, bool en) | |
3140 | { | |
3141 | struct regulator_state *rstate; | |
3142 | ||
3143 | rstate = regulator_get_suspend_state(rdev, state); | |
3144 | if (rstate == NULL) | |
3145 | return -EINVAL; | |
3146 | ||
3147 | if (!rstate->changeable) | |
3148 | return -EPERM; | |
3149 | ||
3150 | rstate->enabled = en; | |
3151 | ||
3152 | return 0; | |
3153 | } | |
3154 | ||
3155 | int regulator_suspend_enable(struct regulator_dev *rdev, | |
3156 | suspend_state_t state) | |
3157 | { | |
3158 | return regulator_suspend_toggle(rdev, state, true); | |
3159 | } | |
3160 | EXPORT_SYMBOL_GPL(regulator_suspend_enable); | |
3161 | ||
3162 | int regulator_suspend_disable(struct regulator_dev *rdev, | |
3163 | suspend_state_t state) | |
3164 | { | |
3165 | struct regulator *regulator; | |
3166 | struct regulator_voltage *voltage; | |
3167 | ||
3168 | /* | |
3169 | * if any consumer wants this regulator device keeping on in | |
3170 | * suspend states, don't set it as disabled. | |
3171 | */ | |
3172 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
3173 | voltage = ®ulator->voltage[state]; | |
3174 | if (voltage->min_uV || voltage->max_uV) | |
3175 | return 0; | |
3176 | } | |
3177 | ||
3178 | return regulator_suspend_toggle(rdev, state, false); | |
3179 | } | |
3180 | EXPORT_SYMBOL_GPL(regulator_suspend_disable); | |
3181 | ||
3182 | static int _regulator_set_suspend_voltage(struct regulator *regulator, | |
3183 | int min_uV, int max_uV, | |
3184 | suspend_state_t state) | |
3185 | { | |
3186 | struct regulator_dev *rdev = regulator->rdev; | |
3187 | struct regulator_state *rstate; | |
3188 | ||
3189 | rstate = regulator_get_suspend_state(rdev, state); | |
3190 | if (rstate == NULL) | |
3191 | return -EINVAL; | |
3192 | ||
3193 | if (rstate->min_uV == rstate->max_uV) { | |
3194 | rdev_err(rdev, "The suspend voltage can't be changed!\n"); | |
3195 | return -EPERM; | |
3196 | } | |
3197 | ||
3198 | return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state); | |
3199 | } | |
3200 | ||
3201 | int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV, | |
3202 | int max_uV, suspend_state_t state) | |
3203 | { | |
3204 | int ret = 0; | |
3205 | ||
3206 | /* PM_SUSPEND_ON is handled by regulator_set_voltage() */ | |
3207 | if (regulator_check_states(state) || state == PM_SUSPEND_ON) | |
3208 | return -EINVAL; | |
3209 | ||
38de19fa | 3210 | regulator_lock_supply(regulator->rdev); |
f7efad10 CZ |
3211 | |
3212 | ret = _regulator_set_suspend_voltage(regulator, min_uV, | |
3213 | max_uV, state); | |
3214 | ||
38de19fa | 3215 | regulator_unlock_supply(regulator->rdev); |
f7efad10 CZ |
3216 | |
3217 | return ret; | |
3218 | } | |
3219 | EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage); | |
3220 | ||
88cd222b LW |
3221 | /** |
3222 | * regulator_set_voltage_time - get raise/fall time | |
3223 | * @regulator: regulator source | |
3224 | * @old_uV: starting voltage in microvolts | |
3225 | * @new_uV: target voltage in microvolts | |
3226 | * | |
3227 | * Provided with the starting and ending voltage, this function attempts to | |
3228 | * calculate the time in microseconds required to rise or fall to this new | |
3229 | * voltage. | |
3230 | */ | |
3231 | int regulator_set_voltage_time(struct regulator *regulator, | |
3232 | int old_uV, int new_uV) | |
3233 | { | |
272e2315 GX |
3234 | struct regulator_dev *rdev = regulator->rdev; |
3235 | const struct regulator_ops *ops = rdev->desc->ops; | |
88cd222b LW |
3236 | int old_sel = -1; |
3237 | int new_sel = -1; | |
3238 | int voltage; | |
3239 | int i; | |
3240 | ||
73e705bf MK |
3241 | if (ops->set_voltage_time) |
3242 | return ops->set_voltage_time(rdev, old_uV, new_uV); | |
3243 | else if (!ops->set_voltage_time_sel) | |
3244 | return _regulator_set_voltage_time(rdev, old_uV, new_uV); | |
3245 | ||
88cd222b | 3246 | /* Currently requires operations to do this */ |
73e705bf | 3247 | if (!ops->list_voltage || !rdev->desc->n_voltages) |
88cd222b LW |
3248 | return -EINVAL; |
3249 | ||
3250 | for (i = 0; i < rdev->desc->n_voltages; i++) { | |
3251 | /* We only look for exact voltage matches here */ | |
3252 | voltage = regulator_list_voltage(regulator, i); | |
3253 | if (voltage < 0) | |
3254 | return -EINVAL; | |
3255 | if (voltage == 0) | |
3256 | continue; | |
3257 | if (voltage == old_uV) | |
3258 | old_sel = i; | |
3259 | if (voltage == new_uV) | |
3260 | new_sel = i; | |
3261 | } | |
3262 | ||
3263 | if (old_sel < 0 || new_sel < 0) | |
3264 | return -EINVAL; | |
3265 | ||
3266 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); | |
3267 | } | |
3268 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); | |
3269 | ||
98a175b6 | 3270 | /** |
296c6566 RD |
3271 | * regulator_set_voltage_time_sel - get raise/fall time |
3272 | * @rdev: regulator source device | |
98a175b6 YSB |
3273 | * @old_selector: selector for starting voltage |
3274 | * @new_selector: selector for target voltage | |
3275 | * | |
3276 | * Provided with the starting and target voltage selectors, this function | |
3277 | * returns time in microseconds required to rise or fall to this new voltage | |
3278 | * | |
f11d08c3 | 3279 | * Drivers providing ramp_delay in regulation_constraints can use this as their |
398715ab | 3280 | * set_voltage_time_sel() operation. |
98a175b6 YSB |
3281 | */ |
3282 | int regulator_set_voltage_time_sel(struct regulator_dev *rdev, | |
3283 | unsigned int old_selector, | |
3284 | unsigned int new_selector) | |
3285 | { | |
f11d08c3 | 3286 | int old_volt, new_volt; |
398715ab | 3287 | |
f11d08c3 AL |
3288 | /* sanity check */ |
3289 | if (!rdev->desc->ops->list_voltage) | |
3290 | return -EINVAL; | |
398715ab | 3291 | |
f11d08c3 AL |
3292 | old_volt = rdev->desc->ops->list_voltage(rdev, old_selector); |
3293 | new_volt = rdev->desc->ops->list_voltage(rdev, new_selector); | |
3294 | ||
73e705bf MK |
3295 | if (rdev->desc->ops->set_voltage_time) |
3296 | return rdev->desc->ops->set_voltage_time(rdev, old_volt, | |
3297 | new_volt); | |
3298 | else | |
3299 | return _regulator_set_voltage_time(rdev, old_volt, new_volt); | |
98a175b6 | 3300 | } |
b19dbf71 | 3301 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel); |
98a175b6 | 3302 | |
606a2562 MB |
3303 | /** |
3304 | * regulator_sync_voltage - re-apply last regulator output voltage | |
3305 | * @regulator: regulator source | |
3306 | * | |
3307 | * Re-apply the last configured voltage. This is intended to be used | |
3308 | * where some external control source the consumer is cooperating with | |
3309 | * has caused the configured voltage to change. | |
3310 | */ | |
3311 | int regulator_sync_voltage(struct regulator *regulator) | |
3312 | { | |
3313 | struct regulator_dev *rdev = regulator->rdev; | |
c360a6df | 3314 | struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON]; |
606a2562 MB |
3315 | int ret, min_uV, max_uV; |
3316 | ||
66cf9a7e | 3317 | regulator_lock(rdev); |
606a2562 MB |
3318 | |
3319 | if (!rdev->desc->ops->set_voltage && | |
3320 | !rdev->desc->ops->set_voltage_sel) { | |
3321 | ret = -EINVAL; | |
3322 | goto out; | |
3323 | } | |
3324 | ||
3325 | /* This is only going to work if we've had a voltage configured. */ | |
c360a6df | 3326 | if (!voltage->min_uV && !voltage->max_uV) { |
606a2562 MB |
3327 | ret = -EINVAL; |
3328 | goto out; | |
3329 | } | |
3330 | ||
c360a6df CZ |
3331 | min_uV = voltage->min_uV; |
3332 | max_uV = voltage->max_uV; | |
606a2562 MB |
3333 | |
3334 | /* This should be a paranoia check... */ | |
3335 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
3336 | if (ret < 0) | |
3337 | goto out; | |
3338 | ||
c360a6df | 3339 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0); |
606a2562 MB |
3340 | if (ret < 0) |
3341 | goto out; | |
3342 | ||
3343 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | |
3344 | ||
3345 | out: | |
66cf9a7e | 3346 | regulator_unlock(rdev); |
606a2562 MB |
3347 | return ret; |
3348 | } | |
3349 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); | |
3350 | ||
414c70cb LG |
3351 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
3352 | { | |
bf5892a8 | 3353 | int sel, ret; |
fef95019 MB |
3354 | bool bypassed; |
3355 | ||
3356 | if (rdev->desc->ops->get_bypass) { | |
3357 | ret = rdev->desc->ops->get_bypass(rdev, &bypassed); | |
3358 | if (ret < 0) | |
3359 | return ret; | |
3360 | if (bypassed) { | |
3361 | /* if bypassed the regulator must have a supply */ | |
45389c47 JH |
3362 | if (!rdev->supply) { |
3363 | rdev_err(rdev, | |
3364 | "bypassed regulator has no supply!\n"); | |
3365 | return -EPROBE_DEFER; | |
3366 | } | |
fef95019 MB |
3367 | |
3368 | return _regulator_get_voltage(rdev->supply->rdev); | |
3369 | } | |
3370 | } | |
476c2d83 MB |
3371 | |
3372 | if (rdev->desc->ops->get_voltage_sel) { | |
3373 | sel = rdev->desc->ops->get_voltage_sel(rdev); | |
3374 | if (sel < 0) | |
3375 | return sel; | |
bf5892a8 | 3376 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
cb220d16 | 3377 | } else if (rdev->desc->ops->get_voltage) { |
bf5892a8 | 3378 | ret = rdev->desc->ops->get_voltage(rdev); |
f7df20ec MB |
3379 | } else if (rdev->desc->ops->list_voltage) { |
3380 | ret = rdev->desc->ops->list_voltage(rdev, 0); | |
5a523605 LD |
3381 | } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) { |
3382 | ret = rdev->desc->fixed_uV; | |
e303996e | 3383 | } else if (rdev->supply) { |
d9b96d35 | 3384 | ret = _regulator_get_voltage(rdev->supply->rdev); |
cb220d16 | 3385 | } else { |
414c70cb | 3386 | return -EINVAL; |
cb220d16 | 3387 | } |
bf5892a8 | 3388 | |
cb220d16 AL |
3389 | if (ret < 0) |
3390 | return ret; | |
bf5892a8 | 3391 | return ret - rdev->constraints->uV_offset; |
414c70cb LG |
3392 | } |
3393 | ||
3394 | /** | |
3395 | * regulator_get_voltage - get regulator output voltage | |
3396 | * @regulator: regulator source | |
3397 | * | |
3398 | * This returns the current regulator voltage in uV. | |
3399 | * | |
3400 | * NOTE: If the regulator is disabled it will return the voltage value. This | |
3401 | * function should not be used to determine regulator state. | |
3402 | */ | |
3403 | int regulator_get_voltage(struct regulator *regulator) | |
3404 | { | |
3405 | int ret; | |
3406 | ||
38de19fa | 3407 | regulator_lock_supply(regulator->rdev); |
414c70cb LG |
3408 | |
3409 | ret = _regulator_get_voltage(regulator->rdev); | |
3410 | ||
38de19fa | 3411 | regulator_unlock_supply(regulator->rdev); |
414c70cb LG |
3412 | |
3413 | return ret; | |
3414 | } | |
3415 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | |
3416 | ||
3417 | /** | |
3418 | * regulator_set_current_limit - set regulator output current limit | |
3419 | * @regulator: regulator source | |
ce0d10f8 | 3420 | * @min_uA: Minimum supported current in uA |
414c70cb LG |
3421 | * @max_uA: Maximum supported current in uA |
3422 | * | |
3423 | * Sets current sink to the desired output current. This can be set during | |
3424 | * any regulator state. IOW, regulator can be disabled or enabled. | |
3425 | * | |
3426 | * If the regulator is enabled then the current will change to the new value | |
3427 | * immediately otherwise if the regulator is disabled the regulator will | |
3428 | * output at the new current when enabled. | |
3429 | * | |
3430 | * NOTE: Regulator system constraints must be set for this regulator before | |
3431 | * calling this function otherwise this call will fail. | |
3432 | */ | |
3433 | int regulator_set_current_limit(struct regulator *regulator, | |
3434 | int min_uA, int max_uA) | |
3435 | { | |
3436 | struct regulator_dev *rdev = regulator->rdev; | |
3437 | int ret; | |
3438 | ||
66cf9a7e | 3439 | regulator_lock(rdev); |
414c70cb LG |
3440 | |
3441 | /* sanity check */ | |
3442 | if (!rdev->desc->ops->set_current_limit) { | |
3443 | ret = -EINVAL; | |
3444 | goto out; | |
3445 | } | |
3446 | ||
3447 | /* constraints check */ | |
3448 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | |
3449 | if (ret < 0) | |
3450 | goto out; | |
3451 | ||
3452 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | |
3453 | out: | |
66cf9a7e | 3454 | regulator_unlock(rdev); |
414c70cb LG |
3455 | return ret; |
3456 | } | |
3457 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | |
3458 | ||
3459 | static int _regulator_get_current_limit(struct regulator_dev *rdev) | |
3460 | { | |
3461 | int ret; | |
3462 | ||
66cf9a7e | 3463 | regulator_lock(rdev); |
414c70cb LG |
3464 | |
3465 | /* sanity check */ | |
3466 | if (!rdev->desc->ops->get_current_limit) { | |
3467 | ret = -EINVAL; | |
3468 | goto out; | |
3469 | } | |
3470 | ||
3471 | ret = rdev->desc->ops->get_current_limit(rdev); | |
3472 | out: | |
66cf9a7e | 3473 | regulator_unlock(rdev); |
414c70cb LG |
3474 | return ret; |
3475 | } | |
3476 | ||
3477 | /** | |
3478 | * regulator_get_current_limit - get regulator output current | |
3479 | * @regulator: regulator source | |
3480 | * | |
3481 | * This returns the current supplied by the specified current sink in uA. | |
3482 | * | |
3483 | * NOTE: If the regulator is disabled it will return the current value. This | |
3484 | * function should not be used to determine regulator state. | |
3485 | */ | |
3486 | int regulator_get_current_limit(struct regulator *regulator) | |
3487 | { | |
3488 | return _regulator_get_current_limit(regulator->rdev); | |
3489 | } | |
3490 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | |
3491 | ||
3492 | /** | |
3493 | * regulator_set_mode - set regulator operating mode | |
3494 | * @regulator: regulator source | |
3495 | * @mode: operating mode - one of the REGULATOR_MODE constants | |
3496 | * | |
3497 | * Set regulator operating mode to increase regulator efficiency or improve | |
3498 | * regulation performance. | |
3499 | * | |
3500 | * NOTE: Regulator system constraints must be set for this regulator before | |
3501 | * calling this function otherwise this call will fail. | |
3502 | */ | |
3503 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | |
3504 | { | |
3505 | struct regulator_dev *rdev = regulator->rdev; | |
3506 | int ret; | |
500b4ac9 | 3507 | int regulator_curr_mode; |
414c70cb | 3508 | |
66cf9a7e | 3509 | regulator_lock(rdev); |
414c70cb LG |
3510 | |
3511 | /* sanity check */ | |
3512 | if (!rdev->desc->ops->set_mode) { | |
3513 | ret = -EINVAL; | |
3514 | goto out; | |
3515 | } | |
3516 | ||
500b4ac9 SI |
3517 | /* return if the same mode is requested */ |
3518 | if (rdev->desc->ops->get_mode) { | |
3519 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); | |
3520 | if (regulator_curr_mode == mode) { | |
3521 | ret = 0; | |
3522 | goto out; | |
3523 | } | |
3524 | } | |
3525 | ||
414c70cb | 3526 | /* constraints check */ |
22c51b47 | 3527 | ret = regulator_mode_constrain(rdev, &mode); |
414c70cb LG |
3528 | if (ret < 0) |
3529 | goto out; | |
3530 | ||
3531 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
3532 | out: | |
66cf9a7e | 3533 | regulator_unlock(rdev); |
414c70cb LG |
3534 | return ret; |
3535 | } | |
3536 | EXPORT_SYMBOL_GPL(regulator_set_mode); | |
3537 | ||
3538 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) | |
3539 | { | |
3540 | int ret; | |
3541 | ||
66cf9a7e | 3542 | regulator_lock(rdev); |
414c70cb LG |
3543 | |
3544 | /* sanity check */ | |
3545 | if (!rdev->desc->ops->get_mode) { | |
3546 | ret = -EINVAL; | |
3547 | goto out; | |
3548 | } | |
3549 | ||
3550 | ret = rdev->desc->ops->get_mode(rdev); | |
3551 | out: | |
66cf9a7e | 3552 | regulator_unlock(rdev); |
414c70cb LG |
3553 | return ret; |
3554 | } | |
3555 | ||
3556 | /** | |
3557 | * regulator_get_mode - get regulator operating mode | |
3558 | * @regulator: regulator source | |
3559 | * | |
3560 | * Get the current regulator operating mode. | |
3561 | */ | |
3562 | unsigned int regulator_get_mode(struct regulator *regulator) | |
3563 | { | |
3564 | return _regulator_get_mode(regulator->rdev); | |
3565 | } | |
3566 | EXPORT_SYMBOL_GPL(regulator_get_mode); | |
3567 | ||
1b5b4221 AH |
3568 | static int _regulator_get_error_flags(struct regulator_dev *rdev, |
3569 | unsigned int *flags) | |
3570 | { | |
3571 | int ret; | |
3572 | ||
66cf9a7e | 3573 | regulator_lock(rdev); |
1b5b4221 AH |
3574 | |
3575 | /* sanity check */ | |
3576 | if (!rdev->desc->ops->get_error_flags) { | |
3577 | ret = -EINVAL; | |
3578 | goto out; | |
3579 | } | |
3580 | ||
3581 | ret = rdev->desc->ops->get_error_flags(rdev, flags); | |
3582 | out: | |
66cf9a7e | 3583 | regulator_unlock(rdev); |
1b5b4221 AH |
3584 | return ret; |
3585 | } | |
3586 | ||
3587 | /** | |
3588 | * regulator_get_error_flags - get regulator error information | |
3589 | * @regulator: regulator source | |
3590 | * @flags: pointer to store error flags | |
3591 | * | |
3592 | * Get the current regulator error information. | |
3593 | */ | |
3594 | int regulator_get_error_flags(struct regulator *regulator, | |
3595 | unsigned int *flags) | |
3596 | { | |
3597 | return _regulator_get_error_flags(regulator->rdev, flags); | |
3598 | } | |
3599 | EXPORT_SYMBOL_GPL(regulator_get_error_flags); | |
3600 | ||
414c70cb | 3601 | /** |
e39ce48f | 3602 | * regulator_set_load - set regulator load |
414c70cb LG |
3603 | * @regulator: regulator source |
3604 | * @uA_load: load current | |
3605 | * | |
3606 | * Notifies the regulator core of a new device load. This is then used by | |
3607 | * DRMS (if enabled by constraints) to set the most efficient regulator | |
3608 | * operating mode for the new regulator loading. | |
3609 | * | |
3610 | * Consumer devices notify their supply regulator of the maximum power | |
3611 | * they will require (can be taken from device datasheet in the power | |
3612 | * consumption tables) when they change operational status and hence power | |
3613 | * state. Examples of operational state changes that can affect power | |
3614 | * consumption are :- | |
3615 | * | |
3616 | * o Device is opened / closed. | |
3617 | * o Device I/O is about to begin or has just finished. | |
3618 | * o Device is idling in between work. | |
3619 | * | |
3620 | * This information is also exported via sysfs to userspace. | |
3621 | * | |
3622 | * DRMS will sum the total requested load on the regulator and change | |
3623 | * to the most efficient operating mode if platform constraints allow. | |
3624 | * | |
e39ce48f | 3625 | * On error a negative errno is returned. |
414c70cb | 3626 | */ |
e39ce48f | 3627 | int regulator_set_load(struct regulator *regulator, int uA_load) |
414c70cb LG |
3628 | { |
3629 | struct regulator_dev *rdev = regulator->rdev; | |
8460ef38 | 3630 | int ret; |
d92d95b6 | 3631 | |
66cf9a7e | 3632 | regulator_lock(rdev); |
414c70cb | 3633 | regulator->uA_load = uA_load; |
8460ef38 | 3634 | ret = drms_uA_update(rdev); |
66cf9a7e | 3635 | regulator_unlock(rdev); |
8460ef38 | 3636 | |
414c70cb LG |
3637 | return ret; |
3638 | } | |
e39ce48f | 3639 | EXPORT_SYMBOL_GPL(regulator_set_load); |
414c70cb | 3640 | |
f59c8f9f MB |
3641 | /** |
3642 | * regulator_allow_bypass - allow the regulator to go into bypass mode | |
3643 | * | |
3644 | * @regulator: Regulator to configure | |
9345dfb8 | 3645 | * @enable: enable or disable bypass mode |
f59c8f9f MB |
3646 | * |
3647 | * Allow the regulator to go into bypass mode if all other consumers | |
3648 | * for the regulator also enable bypass mode and the machine | |
3649 | * constraints allow this. Bypass mode means that the regulator is | |
3650 | * simply passing the input directly to the output with no regulation. | |
3651 | */ | |
3652 | int regulator_allow_bypass(struct regulator *regulator, bool enable) | |
3653 | { | |
3654 | struct regulator_dev *rdev = regulator->rdev; | |
3655 | int ret = 0; | |
3656 | ||
3657 | if (!rdev->desc->ops->set_bypass) | |
3658 | return 0; | |
3659 | ||
8a34e979 | 3660 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS)) |
f59c8f9f MB |
3661 | return 0; |
3662 | ||
66cf9a7e | 3663 | regulator_lock(rdev); |
f59c8f9f MB |
3664 | |
3665 | if (enable && !regulator->bypass) { | |
3666 | rdev->bypass_count++; | |
3667 | ||
3668 | if (rdev->bypass_count == rdev->open_count) { | |
3669 | ret = rdev->desc->ops->set_bypass(rdev, enable); | |
3670 | if (ret != 0) | |
3671 | rdev->bypass_count--; | |
3672 | } | |
3673 | ||
3674 | } else if (!enable && regulator->bypass) { | |
3675 | rdev->bypass_count--; | |
3676 | ||
3677 | if (rdev->bypass_count != rdev->open_count) { | |
3678 | ret = rdev->desc->ops->set_bypass(rdev, enable); | |
3679 | if (ret != 0) | |
3680 | rdev->bypass_count++; | |
3681 | } | |
3682 | } | |
3683 | ||
3684 | if (ret == 0) | |
3685 | regulator->bypass = enable; | |
3686 | ||
66cf9a7e | 3687 | regulator_unlock(rdev); |
f59c8f9f MB |
3688 | |
3689 | return ret; | |
3690 | } | |
3691 | EXPORT_SYMBOL_GPL(regulator_allow_bypass); | |
3692 | ||
414c70cb LG |
3693 | /** |
3694 | * regulator_register_notifier - register regulator event notifier | |
3695 | * @regulator: regulator source | |
69279fb9 | 3696 | * @nb: notifier block |
414c70cb LG |
3697 | * |
3698 | * Register notifier block to receive regulator events. | |
3699 | */ | |
3700 | int regulator_register_notifier(struct regulator *regulator, | |
3701 | struct notifier_block *nb) | |
3702 | { | |
3703 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | |
3704 | nb); | |
3705 | } | |
3706 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | |
3707 | ||
3708 | /** | |
3709 | * regulator_unregister_notifier - unregister regulator event notifier | |
3710 | * @regulator: regulator source | |
69279fb9 | 3711 | * @nb: notifier block |
414c70cb LG |
3712 | * |
3713 | * Unregister regulator event notifier block. | |
3714 | */ | |
3715 | int regulator_unregister_notifier(struct regulator *regulator, | |
3716 | struct notifier_block *nb) | |
3717 | { | |
3718 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | |
3719 | nb); | |
3720 | } | |
3721 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | |
3722 | ||
b136fb44 JC |
3723 | /* notify regulator consumers and downstream regulator consumers. |
3724 | * Note mutex must be held by caller. | |
3725 | */ | |
7179569a | 3726 | static int _notifier_call_chain(struct regulator_dev *rdev, |
414c70cb LG |
3727 | unsigned long event, void *data) |
3728 | { | |
414c70cb | 3729 | /* call rdev chain first */ |
7179569a | 3730 | return blocking_notifier_call_chain(&rdev->notifier, event, data); |
414c70cb LG |
3731 | } |
3732 | ||
3733 | /** | |
3734 | * regulator_bulk_get - get multiple regulator consumers | |
3735 | * | |
3736 | * @dev: Device to supply | |
3737 | * @num_consumers: Number of consumers to register | |
3738 | * @consumers: Configuration of consumers; clients are stored here. | |
3739 | * | |
3740 | * @return 0 on success, an errno on failure. | |
3741 | * | |
3742 | * This helper function allows drivers to get several regulator | |
3743 | * consumers in one operation. If any of the regulators cannot be | |
3744 | * acquired then any regulators that were allocated will be freed | |
3745 | * before returning to the caller. | |
3746 | */ | |
3747 | int regulator_bulk_get(struct device *dev, int num_consumers, | |
3748 | struct regulator_bulk_data *consumers) | |
3749 | { | |
3750 | int i; | |
3751 | int ret; | |
3752 | ||
3753 | for (i = 0; i < num_consumers; i++) | |
3754 | consumers[i].consumer = NULL; | |
3755 | ||
3756 | for (i = 0; i < num_consumers; i++) { | |
565f9b07 BA |
3757 | consumers[i].consumer = regulator_get(dev, |
3758 | consumers[i].supply); | |
414c70cb | 3759 | if (IS_ERR(consumers[i].consumer)) { |
414c70cb | 3760 | ret = PTR_ERR(consumers[i].consumer); |
5b307627 MB |
3761 | dev_err(dev, "Failed to get supply '%s': %d\n", |
3762 | consumers[i].supply, ret); | |
414c70cb LG |
3763 | consumers[i].consumer = NULL; |
3764 | goto err; | |
3765 | } | |
3766 | } | |
3767 | ||
3768 | return 0; | |
3769 | ||
3770 | err: | |
b29c7690 | 3771 | while (--i >= 0) |
414c70cb LG |
3772 | regulator_put(consumers[i].consumer); |
3773 | ||
3774 | return ret; | |
3775 | } | |
3776 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | |
3777 | ||
f21e0e81 MB |
3778 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
3779 | { | |
3780 | struct regulator_bulk_data *bulk = data; | |
3781 | ||
3782 | bulk->ret = regulator_enable(bulk->consumer); | |
3783 | } | |
3784 | ||
414c70cb LG |
3785 | /** |
3786 | * regulator_bulk_enable - enable multiple regulator consumers | |
3787 | * | |
3788 | * @num_consumers: Number of consumers | |
3789 | * @consumers: Consumer data; clients are stored here. | |
3790 | * @return 0 on success, an errno on failure | |
3791 | * | |
3792 | * This convenience API allows consumers to enable multiple regulator | |
3793 | * clients in a single API call. If any consumers cannot be enabled | |
3794 | * then any others that were enabled will be disabled again prior to | |
3795 | * return. | |
3796 | */ | |
3797 | int regulator_bulk_enable(int num_consumers, | |
3798 | struct regulator_bulk_data *consumers) | |
3799 | { | |
2955b47d | 3800 | ASYNC_DOMAIN_EXCLUSIVE(async_domain); |
414c70cb | 3801 | int i; |
f21e0e81 | 3802 | int ret = 0; |
414c70cb | 3803 | |
6492bc1b MB |
3804 | for (i = 0; i < num_consumers; i++) { |
3805 | if (consumers[i].consumer->always_on) | |
3806 | consumers[i].ret = 0; | |
3807 | else | |
3808 | async_schedule_domain(regulator_bulk_enable_async, | |
3809 | &consumers[i], &async_domain); | |
3810 | } | |
f21e0e81 MB |
3811 | |
3812 | async_synchronize_full_domain(&async_domain); | |
3813 | ||
3814 | /* If any consumer failed we need to unwind any that succeeded */ | |
414c70cb | 3815 | for (i = 0; i < num_consumers; i++) { |
f21e0e81 MB |
3816 | if (consumers[i].ret != 0) { |
3817 | ret = consumers[i].ret; | |
414c70cb | 3818 | goto err; |
f21e0e81 | 3819 | } |
414c70cb LG |
3820 | } |
3821 | ||
3822 | return 0; | |
3823 | ||
3824 | err: | |
fbe31057 AH |
3825 | for (i = 0; i < num_consumers; i++) { |
3826 | if (consumers[i].ret < 0) | |
3827 | pr_err("Failed to enable %s: %d\n", consumers[i].supply, | |
3828 | consumers[i].ret); | |
3829 | else | |
3830 | regulator_disable(consumers[i].consumer); | |
3831 | } | |
414c70cb LG |
3832 | |
3833 | return ret; | |
3834 | } | |
3835 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | |
3836 | ||
3837 | /** | |
3838 | * regulator_bulk_disable - disable multiple regulator consumers | |
3839 | * | |
3840 | * @num_consumers: Number of consumers | |
3841 | * @consumers: Consumer data; clients are stored here. | |
3842 | * @return 0 on success, an errno on failure | |
3843 | * | |
3844 | * This convenience API allows consumers to disable multiple regulator | |
49e22632 SN |
3845 | * clients in a single API call. If any consumers cannot be disabled |
3846 | * then any others that were disabled will be enabled again prior to | |
414c70cb LG |
3847 | * return. |
3848 | */ | |
3849 | int regulator_bulk_disable(int num_consumers, | |
3850 | struct regulator_bulk_data *consumers) | |
3851 | { | |
3852 | int i; | |
01e86f49 | 3853 | int ret, r; |
414c70cb | 3854 | |
49e22632 | 3855 | for (i = num_consumers - 1; i >= 0; --i) { |
414c70cb LG |
3856 | ret = regulator_disable(consumers[i].consumer); |
3857 | if (ret != 0) | |
3858 | goto err; | |
3859 | } | |
3860 | ||
3861 | return 0; | |
3862 | ||
3863 | err: | |
5da84fd9 | 3864 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
01e86f49 MB |
3865 | for (++i; i < num_consumers; ++i) { |
3866 | r = regulator_enable(consumers[i].consumer); | |
3867 | if (r != 0) | |
d1642ea7 | 3868 | pr_err("Failed to re-enable %s: %d\n", |
01e86f49 MB |
3869 | consumers[i].supply, r); |
3870 | } | |
414c70cb LG |
3871 | |
3872 | return ret; | |
3873 | } | |
3874 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | |
3875 | ||
e1de2f42 DK |
3876 | /** |
3877 | * regulator_bulk_force_disable - force disable multiple regulator consumers | |
3878 | * | |
3879 | * @num_consumers: Number of consumers | |
3880 | * @consumers: Consumer data; clients are stored here. | |
3881 | * @return 0 on success, an errno on failure | |
3882 | * | |
3883 | * This convenience API allows consumers to forcibly disable multiple regulator | |
3884 | * clients in a single API call. | |
3885 | * NOTE: This should be used for situations when device damage will | |
3886 | * likely occur if the regulators are not disabled (e.g. over temp). | |
3887 | * Although regulator_force_disable function call for some consumers can | |
3888 | * return error numbers, the function is called for all consumers. | |
3889 | */ | |
3890 | int regulator_bulk_force_disable(int num_consumers, | |
3891 | struct regulator_bulk_data *consumers) | |
3892 | { | |
3893 | int i; | |
b8c77ff6 | 3894 | int ret = 0; |
e1de2f42 | 3895 | |
b8c77ff6 | 3896 | for (i = 0; i < num_consumers; i++) { |
e1de2f42 DK |
3897 | consumers[i].ret = |
3898 | regulator_force_disable(consumers[i].consumer); | |
3899 | ||
b8c77ff6 DT |
3900 | /* Store first error for reporting */ |
3901 | if (consumers[i].ret && !ret) | |
e1de2f42 | 3902 | ret = consumers[i].ret; |
e1de2f42 DK |
3903 | } |
3904 | ||
e1de2f42 DK |
3905 | return ret; |
3906 | } | |
3907 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); | |
3908 | ||
414c70cb LG |
3909 | /** |
3910 | * regulator_bulk_free - free multiple regulator consumers | |
3911 | * | |
3912 | * @num_consumers: Number of consumers | |
3913 | * @consumers: Consumer data; clients are stored here. | |
3914 | * | |
3915 | * This convenience API allows consumers to free multiple regulator | |
3916 | * clients in a single API call. | |
3917 | */ | |
3918 | void regulator_bulk_free(int num_consumers, | |
3919 | struct regulator_bulk_data *consumers) | |
3920 | { | |
3921 | int i; | |
3922 | ||
3923 | for (i = 0; i < num_consumers; i++) { | |
3924 | regulator_put(consumers[i].consumer); | |
3925 | consumers[i].consumer = NULL; | |
3926 | } | |
3927 | } | |
3928 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | |
3929 | ||
3930 | /** | |
3931 | * regulator_notifier_call_chain - call regulator event notifier | |
69279fb9 | 3932 | * @rdev: regulator source |
414c70cb | 3933 | * @event: notifier block |
69279fb9 | 3934 | * @data: callback-specific data. |
414c70cb LG |
3935 | * |
3936 | * Called by regulator drivers to notify clients a regulator event has | |
3937 | * occurred. We also notify regulator clients downstream. | |
b136fb44 | 3938 | * Note lock must be held by caller. |
414c70cb LG |
3939 | */ |
3940 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | |
3941 | unsigned long event, void *data) | |
3942 | { | |
70cfef26 KK |
3943 | lockdep_assert_held_once(&rdev->mutex); |
3944 | ||
414c70cb LG |
3945 | _notifier_call_chain(rdev, event, data); |
3946 | return NOTIFY_DONE; | |
3947 | ||
3948 | } | |
3949 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | |
3950 | ||
be721979 MB |
3951 | /** |
3952 | * regulator_mode_to_status - convert a regulator mode into a status | |
3953 | * | |
3954 | * @mode: Mode to convert | |
3955 | * | |
3956 | * Convert a regulator mode into a status. | |
3957 | */ | |
3958 | int regulator_mode_to_status(unsigned int mode) | |
3959 | { | |
3960 | switch (mode) { | |
3961 | case REGULATOR_MODE_FAST: | |
3962 | return REGULATOR_STATUS_FAST; | |
3963 | case REGULATOR_MODE_NORMAL: | |
3964 | return REGULATOR_STATUS_NORMAL; | |
3965 | case REGULATOR_MODE_IDLE: | |
3966 | return REGULATOR_STATUS_IDLE; | |
03ffcf3d | 3967 | case REGULATOR_MODE_STANDBY: |
be721979 MB |
3968 | return REGULATOR_STATUS_STANDBY; |
3969 | default: | |
1beaf762 | 3970 | return REGULATOR_STATUS_UNDEFINED; |
be721979 MB |
3971 | } |
3972 | } | |
3973 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); | |
3974 | ||
39f802d6 TI |
3975 | static struct attribute *regulator_dev_attrs[] = { |
3976 | &dev_attr_name.attr, | |
3977 | &dev_attr_num_users.attr, | |
3978 | &dev_attr_type.attr, | |
3979 | &dev_attr_microvolts.attr, | |
3980 | &dev_attr_microamps.attr, | |
3981 | &dev_attr_opmode.attr, | |
3982 | &dev_attr_state.attr, | |
3983 | &dev_attr_status.attr, | |
3984 | &dev_attr_bypass.attr, | |
3985 | &dev_attr_requested_microamps.attr, | |
3986 | &dev_attr_min_microvolts.attr, | |
3987 | &dev_attr_max_microvolts.attr, | |
3988 | &dev_attr_min_microamps.attr, | |
3989 | &dev_attr_max_microamps.attr, | |
3990 | &dev_attr_suspend_standby_state.attr, | |
3991 | &dev_attr_suspend_mem_state.attr, | |
3992 | &dev_attr_suspend_disk_state.attr, | |
3993 | &dev_attr_suspend_standby_microvolts.attr, | |
3994 | &dev_attr_suspend_mem_microvolts.attr, | |
3995 | &dev_attr_suspend_disk_microvolts.attr, | |
3996 | &dev_attr_suspend_standby_mode.attr, | |
3997 | &dev_attr_suspend_mem_mode.attr, | |
3998 | &dev_attr_suspend_disk_mode.attr, | |
3999 | NULL | |
4000 | }; | |
4001 | ||
7ad68e2f DB |
4002 | /* |
4003 | * To avoid cluttering sysfs (and memory) with useless state, only | |
4004 | * create attributes that can be meaningfully displayed. | |
4005 | */ | |
39f802d6 TI |
4006 | static umode_t regulator_attr_is_visible(struct kobject *kobj, |
4007 | struct attribute *attr, int idx) | |
7ad68e2f | 4008 | { |
39f802d6 | 4009 | struct device *dev = kobj_to_dev(kobj); |
83080a14 | 4010 | struct regulator_dev *rdev = dev_to_rdev(dev); |
272e2315 | 4011 | const struct regulator_ops *ops = rdev->desc->ops; |
39f802d6 TI |
4012 | umode_t mode = attr->mode; |
4013 | ||
4014 | /* these three are always present */ | |
4015 | if (attr == &dev_attr_name.attr || | |
4016 | attr == &dev_attr_num_users.attr || | |
4017 | attr == &dev_attr_type.attr) | |
4018 | return mode; | |
7ad68e2f DB |
4019 | |
4020 | /* some attributes need specific methods to be displayed */ | |
39f802d6 TI |
4021 | if (attr == &dev_attr_microvolts.attr) { |
4022 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || | |
4023 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) || | |
4024 | (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) || | |
4025 | (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1)) | |
4026 | return mode; | |
4027 | return 0; | |
f59c8f9f | 4028 | } |
7ad68e2f | 4029 | |
39f802d6 TI |
4030 | if (attr == &dev_attr_microamps.attr) |
4031 | return ops->get_current_limit ? mode : 0; | |
4032 | ||
4033 | if (attr == &dev_attr_opmode.attr) | |
4034 | return ops->get_mode ? mode : 0; | |
4035 | ||
4036 | if (attr == &dev_attr_state.attr) | |
4037 | return (rdev->ena_pin || ops->is_enabled) ? mode : 0; | |
4038 | ||
4039 | if (attr == &dev_attr_status.attr) | |
4040 | return ops->get_status ? mode : 0; | |
4041 | ||
4042 | if (attr == &dev_attr_bypass.attr) | |
4043 | return ops->get_bypass ? mode : 0; | |
4044 | ||
7ad68e2f | 4045 | /* some attributes are type-specific */ |
39f802d6 TI |
4046 | if (attr == &dev_attr_requested_microamps.attr) |
4047 | return rdev->desc->type == REGULATOR_CURRENT ? mode : 0; | |
7ad68e2f | 4048 | |
7ad68e2f | 4049 | /* constraints need specific supporting methods */ |
39f802d6 TI |
4050 | if (attr == &dev_attr_min_microvolts.attr || |
4051 | attr == &dev_attr_max_microvolts.attr) | |
4052 | return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0; | |
4053 | ||
4054 | if (attr == &dev_attr_min_microamps.attr || | |
4055 | attr == &dev_attr_max_microamps.attr) | |
4056 | return ops->set_current_limit ? mode : 0; | |
4057 | ||
4058 | if (attr == &dev_attr_suspend_standby_state.attr || | |
4059 | attr == &dev_attr_suspend_mem_state.attr || | |
4060 | attr == &dev_attr_suspend_disk_state.attr) | |
4061 | return mode; | |
4062 | ||
4063 | if (attr == &dev_attr_suspend_standby_microvolts.attr || | |
4064 | attr == &dev_attr_suspend_mem_microvolts.attr || | |
4065 | attr == &dev_attr_suspend_disk_microvolts.attr) | |
4066 | return ops->set_suspend_voltage ? mode : 0; | |
4067 | ||
4068 | if (attr == &dev_attr_suspend_standby_mode.attr || | |
4069 | attr == &dev_attr_suspend_mem_mode.attr || | |
4070 | attr == &dev_attr_suspend_disk_mode.attr) | |
4071 | return ops->set_suspend_mode ? mode : 0; | |
4072 | ||
4073 | return mode; | |
4074 | } | |
4075 | ||
4076 | static const struct attribute_group regulator_dev_group = { | |
4077 | .attrs = regulator_dev_attrs, | |
4078 | .is_visible = regulator_attr_is_visible, | |
4079 | }; | |
4080 | ||
4081 | static const struct attribute_group *regulator_dev_groups[] = { | |
4082 | ®ulator_dev_group, | |
4083 | NULL | |
4084 | }; | |
7ad68e2f | 4085 | |
39f802d6 TI |
4086 | static void regulator_dev_release(struct device *dev) |
4087 | { | |
4088 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
29f5f486 MB |
4089 | |
4090 | kfree(rdev->constraints); | |
4091 | of_node_put(rdev->dev.of_node); | |
39f802d6 | 4092 | kfree(rdev); |
7ad68e2f DB |
4093 | } |
4094 | ||
1130e5b3 MB |
4095 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
4096 | { | |
a9eaa813 GR |
4097 | struct device *parent = rdev->dev.parent; |
4098 | const char *rname = rdev_get_name(rdev); | |
4099 | char name[NAME_MAX]; | |
4100 | ||
4101 | /* Avoid duplicate debugfs directory names */ | |
4102 | if (parent && rname == rdev->desc->name) { | |
4103 | snprintf(name, sizeof(name), "%s-%s", dev_name(parent), | |
4104 | rname); | |
4105 | rname = name; | |
4106 | } | |
4107 | ||
4108 | rdev->debugfs = debugfs_create_dir(rname, debugfs_root); | |
24751434 | 4109 | if (!rdev->debugfs) { |
1130e5b3 | 4110 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
1130e5b3 MB |
4111 | return; |
4112 | } | |
4113 | ||
4114 | debugfs_create_u32("use_count", 0444, rdev->debugfs, | |
4115 | &rdev->use_count); | |
4116 | debugfs_create_u32("open_count", 0444, rdev->debugfs, | |
4117 | &rdev->open_count); | |
f59c8f9f MB |
4118 | debugfs_create_u32("bypass_count", 0444, rdev->debugfs, |
4119 | &rdev->bypass_count); | |
1130e5b3 MB |
4120 | } |
4121 | ||
5e3ca2b3 JMC |
4122 | static int regulator_register_resolve_supply(struct device *dev, void *data) |
4123 | { | |
7ddede6a JH |
4124 | struct regulator_dev *rdev = dev_to_rdev(dev); |
4125 | ||
4126 | if (regulator_resolve_supply(rdev)) | |
4127 | rdev_dbg(rdev, "unable to resolve supply\n"); | |
4128 | ||
4129 | return 0; | |
5e3ca2b3 JMC |
4130 | } |
4131 | ||
d3d64537 MP |
4132 | static int regulator_fill_coupling_array(struct regulator_dev *rdev) |
4133 | { | |
4134 | struct coupling_desc *c_desc = &rdev->coupling_desc; | |
4135 | int n_coupled = c_desc->n_coupled; | |
4136 | struct regulator_dev *c_rdev; | |
4137 | int i; | |
4138 | ||
4139 | for (i = 1; i < n_coupled; i++) { | |
4140 | /* already resolved */ | |
4141 | if (c_desc->coupled_rdevs[i]) | |
4142 | continue; | |
4143 | ||
4144 | c_rdev = of_parse_coupled_regulator(rdev, i - 1); | |
4145 | ||
4146 | if (c_rdev) { | |
4147 | c_desc->coupled_rdevs[i] = c_rdev; | |
4148 | c_desc->n_resolved++; | |
4149 | } | |
4150 | } | |
4151 | ||
4152 | if (rdev->coupling_desc.n_resolved < n_coupled) | |
4153 | return -1; | |
4154 | else | |
4155 | return 0; | |
4156 | } | |
4157 | ||
4158 | static int regulator_register_fill_coupling_array(struct device *dev, | |
4159 | void *data) | |
4160 | { | |
4161 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
4162 | ||
4163 | if (!IS_ENABLED(CONFIG_OF)) | |
4164 | return 0; | |
4165 | ||
4166 | if (regulator_fill_coupling_array(rdev)) | |
4167 | rdev_dbg(rdev, "unable to resolve coupling\n"); | |
4168 | ||
4169 | return 0; | |
4170 | } | |
4171 | ||
4172 | static int regulator_resolve_coupling(struct regulator_dev *rdev) | |
4173 | { | |
4174 | int n_phandles; | |
4175 | ||
4176 | if (!IS_ENABLED(CONFIG_OF)) | |
4177 | n_phandles = 0; | |
4178 | else | |
4179 | n_phandles = of_get_n_coupled(rdev); | |
4180 | ||
4181 | if (n_phandles + 1 > MAX_COUPLED) { | |
4182 | rdev_err(rdev, "too many regulators coupled\n"); | |
4183 | return -EPERM; | |
4184 | } | |
4185 | ||
4186 | /* | |
4187 | * Every regulator should always have coupling descriptor filled with | |
4188 | * at least pointer to itself. | |
4189 | */ | |
4190 | rdev->coupling_desc.coupled_rdevs[0] = rdev; | |
4191 | rdev->coupling_desc.n_coupled = n_phandles + 1; | |
4192 | rdev->coupling_desc.n_resolved++; | |
4193 | ||
4194 | /* regulator isn't coupled */ | |
4195 | if (n_phandles == 0) | |
4196 | return 0; | |
4197 | ||
4198 | /* regulator, which can't change its voltage, can't be coupled */ | |
4199 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) { | |
4200 | rdev_err(rdev, "voltage operation not allowed\n"); | |
4201 | return -EPERM; | |
4202 | } | |
4203 | ||
4204 | if (rdev->constraints->max_spread <= 0) { | |
4205 | rdev_err(rdev, "wrong max_spread value\n"); | |
4206 | return -EPERM; | |
4207 | } | |
4208 | ||
4209 | if (!of_check_coupling_data(rdev)) | |
4210 | return -EPERM; | |
4211 | ||
4212 | /* | |
4213 | * After everything has been checked, try to fill rdevs array | |
4214 | * with pointers to regulators parsed from device tree. If some | |
4215 | * regulators are not registered yet, retry in late init call | |
4216 | */ | |
4217 | regulator_fill_coupling_array(rdev); | |
4218 | ||
4219 | return 0; | |
4220 | } | |
4221 | ||
414c70cb LG |
4222 | /** |
4223 | * regulator_register - register regulator | |
69279fb9 | 4224 | * @regulator_desc: regulator to register |
f47531b1 | 4225 | * @cfg: runtime configuration for regulator |
414c70cb LG |
4226 | * |
4227 | * Called by regulator drivers to register a regulator. | |
0384618a AL |
4228 | * Returns a valid pointer to struct regulator_dev on success |
4229 | * or an ERR_PTR() on error. | |
414c70cb | 4230 | */ |
65f26846 MB |
4231 | struct regulator_dev * |
4232 | regulator_register(const struct regulator_desc *regulator_desc, | |
1b3de223 | 4233 | const struct regulator_config *cfg) |
414c70cb | 4234 | { |
9a8f5e07 | 4235 | const struct regulation_constraints *constraints = NULL; |
c172708d | 4236 | const struct regulator_init_data *init_data; |
1b3de223 | 4237 | struct regulator_config *config = NULL; |
72dca06f | 4238 | static atomic_t regulator_no = ATOMIC_INIT(-1); |
414c70cb | 4239 | struct regulator_dev *rdev; |
32c8fad4 | 4240 | struct device *dev; |
a5766f11 | 4241 | int ret, i; |
414c70cb | 4242 | |
1b3de223 | 4243 | if (regulator_desc == NULL || cfg == NULL) |
414c70cb LG |
4244 | return ERR_PTR(-EINVAL); |
4245 | ||
1b3de223 | 4246 | dev = cfg->dev; |
dcf70112 | 4247 | WARN_ON(!dev); |
32c8fad4 | 4248 | |
414c70cb LG |
4249 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
4250 | return ERR_PTR(-EINVAL); | |
4251 | ||
cd78dfc6 DL |
4252 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
4253 | regulator_desc->type != REGULATOR_CURRENT) | |
414c70cb LG |
4254 | return ERR_PTR(-EINVAL); |
4255 | ||
476c2d83 MB |
4256 | /* Only one of each should be implemented */ |
4257 | WARN_ON(regulator_desc->ops->get_voltage && | |
4258 | regulator_desc->ops->get_voltage_sel); | |
e8eef82b MB |
4259 | WARN_ON(regulator_desc->ops->set_voltage && |
4260 | regulator_desc->ops->set_voltage_sel); | |
476c2d83 MB |
4261 | |
4262 | /* If we're using selectors we must implement list_voltage. */ | |
4263 | if (regulator_desc->ops->get_voltage_sel && | |
4264 | !regulator_desc->ops->list_voltage) { | |
4265 | return ERR_PTR(-EINVAL); | |
4266 | } | |
e8eef82b MB |
4267 | if (regulator_desc->ops->set_voltage_sel && |
4268 | !regulator_desc->ops->list_voltage) { | |
4269 | return ERR_PTR(-EINVAL); | |
4270 | } | |
476c2d83 | 4271 | |
414c70cb LG |
4272 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
4273 | if (rdev == NULL) | |
4274 | return ERR_PTR(-ENOMEM); | |
4275 | ||
1b3de223 KK |
4276 | /* |
4277 | * Duplicate the config so the driver could override it after | |
4278 | * parsing init data. | |
4279 | */ | |
4280 | config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL); | |
4281 | if (config == NULL) { | |
4282 | kfree(rdev); | |
4283 | return ERR_PTR(-ENOMEM); | |
4284 | } | |
4285 | ||
bfa21a0d | 4286 | init_data = regulator_of_get_init_data(dev, regulator_desc, config, |
a0c7b164 MB |
4287 | &rdev->dev.of_node); |
4288 | if (!init_data) { | |
4289 | init_data = config->init_data; | |
4290 | rdev->dev.of_node = of_node_get(config->of_node); | |
4291 | } | |
4292 | ||
414c70cb | 4293 | mutex_init(&rdev->mutex); |
c172708d | 4294 | rdev->reg_data = config->driver_data; |
414c70cb LG |
4295 | rdev->owner = regulator_desc->owner; |
4296 | rdev->desc = regulator_desc; | |
3a4b0a07 MB |
4297 | if (config->regmap) |
4298 | rdev->regmap = config->regmap; | |
52b84dac | 4299 | else if (dev_get_regmap(dev, NULL)) |
3a4b0a07 | 4300 | rdev->regmap = dev_get_regmap(dev, NULL); |
52b84dac AC |
4301 | else if (dev->parent) |
4302 | rdev->regmap = dev_get_regmap(dev->parent, NULL); | |
414c70cb | 4303 | INIT_LIST_HEAD(&rdev->consumer_list); |
414c70cb | 4304 | INIT_LIST_HEAD(&rdev->list); |
414c70cb | 4305 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
da07ecd9 | 4306 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
414c70cb | 4307 | |
a5766f11 | 4308 | /* preform any regulator specific init */ |
9a8f5e07 | 4309 | if (init_data && init_data->regulator_init) { |
a5766f11 | 4310 | ret = init_data->regulator_init(rdev->reg_data); |
4fca9545 DB |
4311 | if (ret < 0) |
4312 | goto clean; | |
a5766f11 LG |
4313 | } |
4314 | ||
e45e290a LW |
4315 | if (config->ena_gpiod || |
4316 | ((config->ena_gpio || config->ena_gpio_initialized) && | |
4317 | gpio_is_valid(config->ena_gpio))) { | |
45389c47 | 4318 | mutex_lock(®ulator_list_mutex); |
daad134d | 4319 | ret = regulator_ena_gpio_request(rdev, config); |
45389c47 | 4320 | mutex_unlock(®ulator_list_mutex); |
daad134d KA |
4321 | if (ret != 0) { |
4322 | rdev_err(rdev, "Failed to request enable GPIO%d: %d\n", | |
4323 | config->ena_gpio, ret); | |
32165230 | 4324 | goto clean; |
daad134d KA |
4325 | } |
4326 | } | |
4327 | ||
a5766f11 | 4328 | /* register with sysfs */ |
414c70cb | 4329 | rdev->dev.class = ®ulator_class; |
a5766f11 | 4330 | rdev->dev.parent = dev; |
72dca06f | 4331 | dev_set_name(&rdev->dev, "regulator.%lu", |
39138818 | 4332 | (unsigned long) atomic_inc_return(®ulator_no)); |
a5766f11 | 4333 | |
74f544c1 | 4334 | /* set regulator constraints */ |
9a8f5e07 MB |
4335 | if (init_data) |
4336 | constraints = &init_data->constraints; | |
4337 | ||
9a8f5e07 | 4338 | if (init_data && init_data->supply_regulator) |
6261b06d | 4339 | rdev->supply_name = init_data->supply_regulator; |
69511a45 | 4340 | else if (regulator_desc->supply_name) |
6261b06d | 4341 | rdev->supply_name = regulator_desc->supply_name; |
0178f3e2 | 4342 | |
45389c47 JH |
4343 | /* |
4344 | * Attempt to resolve the regulator supply, if specified, | |
4345 | * but don't return an error if we fail because we will try | |
4346 | * to resolve it again later as more regulators are added. | |
4347 | */ | |
4348 | if (regulator_resolve_supply(rdev)) | |
4349 | rdev_dbg(rdev, "unable to resolve supply\n"); | |
4350 | ||
4351 | ret = set_machine_constraints(rdev, constraints); | |
4352 | if (ret < 0) | |
4353 | goto wash; | |
4354 | ||
d3d64537 MP |
4355 | mutex_lock(®ulator_list_mutex); |
4356 | ret = regulator_resolve_coupling(rdev); | |
4357 | mutex_unlock(®ulator_list_mutex); | |
4358 | ||
4359 | if (ret != 0) | |
4360 | goto wash; | |
4361 | ||
a5766f11 | 4362 | /* add consumers devices */ |
9a8f5e07 | 4363 | if (init_data) { |
45389c47 | 4364 | mutex_lock(®ulator_list_mutex); |
9a8f5e07 MB |
4365 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
4366 | ret = set_consumer_device_supply(rdev, | |
9a8f5e07 | 4367 | init_data->consumer_supplies[i].dev_name, |
23c2f041 | 4368 | init_data->consumer_supplies[i].supply); |
9a8f5e07 | 4369 | if (ret < 0) { |
45389c47 | 4370 | mutex_unlock(®ulator_list_mutex); |
9a8f5e07 MB |
4371 | dev_err(dev, "Failed to set supply %s\n", |
4372 | init_data->consumer_supplies[i].supply); | |
4373 | goto unset_supplies; | |
4374 | } | |
23c2f041 | 4375 | } |
45389c47 | 4376 | mutex_unlock(®ulator_list_mutex); |
414c70cb | 4377 | } |
a5766f11 | 4378 | |
fd086045 MK |
4379 | if (!rdev->desc->ops->get_voltage && |
4380 | !rdev->desc->ops->list_voltage && | |
4381 | !rdev->desc->fixed_uV) | |
4382 | rdev->is_switch = true; | |
4383 | ||
c438b9d0 JH |
4384 | ret = device_register(&rdev->dev); |
4385 | if (ret != 0) { | |
4386 | put_device(&rdev->dev); | |
4387 | goto unset_supplies; | |
4388 | } | |
4389 | ||
4390 | dev_set_drvdata(&rdev->dev, rdev); | |
1130e5b3 | 4391 | rdev_init_debugfs(rdev); |
5e3ca2b3 JMC |
4392 | |
4393 | /* try to resolve regulators supply since a new one was registered */ | |
4394 | class_for_each_device(®ulator_class, NULL, NULL, | |
4395 | regulator_register_resolve_supply); | |
1b3de223 | 4396 | kfree(config); |
414c70cb | 4397 | return rdev; |
4fca9545 | 4398 | |
d4033b54 | 4399 | unset_supplies: |
45389c47 | 4400 | mutex_lock(®ulator_list_mutex); |
d4033b54 | 4401 | unset_regulator_supplies(rdev); |
45389c47 | 4402 | mutex_unlock(®ulator_list_mutex); |
32165230 | 4403 | wash: |
469b640e | 4404 | kfree(rdev->constraints); |
45389c47 | 4405 | mutex_lock(®ulator_list_mutex); |
32165230 | 4406 | regulator_ena_gpio_free(rdev); |
45389c47 | 4407 | mutex_unlock(®ulator_list_mutex); |
4fca9545 DB |
4408 | clean: |
4409 | kfree(rdev); | |
a2151374 JH |
4410 | kfree(config); |
4411 | return ERR_PTR(ret); | |
414c70cb LG |
4412 | } |
4413 | EXPORT_SYMBOL_GPL(regulator_register); | |
4414 | ||
4415 | /** | |
4416 | * regulator_unregister - unregister regulator | |
69279fb9 | 4417 | * @rdev: regulator to unregister |
414c70cb LG |
4418 | * |
4419 | * Called by regulator drivers to unregister a regulator. | |
4420 | */ | |
4421 | void regulator_unregister(struct regulator_dev *rdev) | |
4422 | { | |
4423 | if (rdev == NULL) | |
4424 | return; | |
4425 | ||
891636ea MB |
4426 | if (rdev->supply) { |
4427 | while (rdev->use_count--) | |
4428 | regulator_disable(rdev->supply); | |
e032b376 | 4429 | regulator_put(rdev->supply); |
891636ea | 4430 | } |
414c70cb | 4431 | mutex_lock(®ulator_list_mutex); |
1130e5b3 | 4432 | debugfs_remove_recursive(rdev->debugfs); |
43829731 | 4433 | flush_work(&rdev->disable_work.work); |
6bf87d17 | 4434 | WARN_ON(rdev->open_count); |
0f1d747b | 4435 | unset_regulator_supplies(rdev); |
414c70cb | 4436 | list_del(&rdev->list); |
f19b00da | 4437 | regulator_ena_gpio_free(rdev); |
2c0a303a | 4438 | mutex_unlock(®ulator_list_mutex); |
58fb5cf5 | 4439 | device_unregister(&rdev->dev); |
414c70cb LG |
4440 | } |
4441 | EXPORT_SYMBOL_GPL(regulator_unregister); | |
4442 | ||
f7efad10 | 4443 | #ifdef CONFIG_SUSPEND |
0380cf7d | 4444 | static int _regulator_suspend(struct device *dev, void *data) |
85f3b431 TV |
4445 | { |
4446 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
f7efad10 | 4447 | suspend_state_t *state = data; |
85f3b431 TV |
4448 | int ret; |
4449 | ||
66cf9a7e | 4450 | regulator_lock(rdev); |
f7efad10 | 4451 | ret = suspend_set_state(rdev, *state); |
66cf9a7e | 4452 | regulator_unlock(rdev); |
85f3b431 TV |
4453 | |
4454 | return ret; | |
4455 | } | |
4456 | ||
414c70cb | 4457 | /** |
0380cf7d | 4458 | * regulator_suspend - prepare regulators for system wide suspend |
414c70cb LG |
4459 | * @state: system suspend state |
4460 | * | |
4461 | * Configure each regulator with it's suspend operating parameters for state. | |
414c70cb | 4462 | */ |
0380cf7d | 4463 | static int regulator_suspend(struct device *dev) |
414c70cb | 4464 | { |
f7efad10 | 4465 | suspend_state_t state = pm_suspend_target_state; |
414c70cb | 4466 | |
85f3b431 | 4467 | return class_for_each_device(®ulator_class, NULL, &state, |
0380cf7d | 4468 | _regulator_suspend); |
85f3b431 | 4469 | } |
d3e4eccb | 4470 | |
0380cf7d | 4471 | static int _regulator_resume(struct device *dev, void *data) |
85f3b431 | 4472 | { |
f7efad10 | 4473 | int ret = 0; |
85f3b431 | 4474 | struct regulator_dev *rdev = dev_to_rdev(dev); |
f7efad10 CZ |
4475 | suspend_state_t *state = data; |
4476 | struct regulator_state *rstate; | |
4477 | ||
4478 | rstate = regulator_get_suspend_state(rdev, *state); | |
4479 | if (rstate == NULL) | |
35b5f14e | 4480 | return 0; |
414c70cb | 4481 | |
66cf9a7e | 4482 | regulator_lock(rdev); |
85f3b431 | 4483 | |
0380cf7d | 4484 | if (rdev->desc->ops->resume && |
f7efad10 CZ |
4485 | (rstate->enabled == ENABLE_IN_SUSPEND || |
4486 | rstate->enabled == DISABLE_IN_SUSPEND)) | |
0380cf7d | 4487 | ret = rdev->desc->ops->resume(rdev); |
f7efad10 | 4488 | |
66cf9a7e | 4489 | regulator_unlock(rdev); |
85f3b431 | 4490 | |
f7efad10 | 4491 | return ret; |
414c70cb | 4492 | } |
414c70cb | 4493 | |
0380cf7d | 4494 | static int regulator_resume(struct device *dev) |
7a32b589 | 4495 | { |
f7efad10 CZ |
4496 | suspend_state_t state = pm_suspend_target_state; |
4497 | ||
4498 | return class_for_each_device(®ulator_class, NULL, &state, | |
0380cf7d | 4499 | _regulator_resume); |
7a32b589 | 4500 | } |
7a32b589 | 4501 | |
f7efad10 CZ |
4502 | #else /* !CONFIG_SUSPEND */ |
4503 | ||
0380cf7d | 4504 | #define regulator_suspend NULL |
4505 | #define regulator_resume NULL | |
f7efad10 CZ |
4506 | |
4507 | #endif /* !CONFIG_SUSPEND */ | |
4508 | ||
4509 | #ifdef CONFIG_PM | |
4510 | static const struct dev_pm_ops __maybe_unused regulator_pm_ops = { | |
0380cf7d | 4511 | .suspend = regulator_suspend, |
4512 | .resume = regulator_resume, | |
f7efad10 CZ |
4513 | }; |
4514 | #endif | |
4515 | ||
285c22de | 4516 | struct class regulator_class = { |
f7efad10 CZ |
4517 | .name = "regulator", |
4518 | .dev_release = regulator_dev_release, | |
4519 | .dev_groups = regulator_dev_groups, | |
4520 | #ifdef CONFIG_PM | |
4521 | .pm = ®ulator_pm_ops, | |
4522 | #endif | |
4523 | }; | |
ca725561 MB |
4524 | /** |
4525 | * regulator_has_full_constraints - the system has fully specified constraints | |
4526 | * | |
4527 | * Calling this function will cause the regulator API to disable all | |
4528 | * regulators which have a zero use count and don't have an always_on | |
4529 | * constraint in a late_initcall. | |
4530 | * | |
4531 | * The intention is that this will become the default behaviour in a | |
4532 | * future kernel release so users are encouraged to use this facility | |
4533 | * now. | |
4534 | */ | |
4535 | void regulator_has_full_constraints(void) | |
4536 | { | |
4537 | has_full_constraints = 1; | |
4538 | } | |
4539 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); | |
4540 | ||
414c70cb LG |
4541 | /** |
4542 | * rdev_get_drvdata - get rdev regulator driver data | |
69279fb9 | 4543 | * @rdev: regulator |
414c70cb LG |
4544 | * |
4545 | * Get rdev regulator driver private data. This call can be used in the | |
4546 | * regulator driver context. | |
4547 | */ | |
4548 | void *rdev_get_drvdata(struct regulator_dev *rdev) | |
4549 | { | |
4550 | return rdev->reg_data; | |
4551 | } | |
4552 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | |
4553 | ||
4554 | /** | |
4555 | * regulator_get_drvdata - get regulator driver data | |
4556 | * @regulator: regulator | |
4557 | * | |
4558 | * Get regulator driver private data. This call can be used in the consumer | |
4559 | * driver context when non API regulator specific functions need to be called. | |
4560 | */ | |
4561 | void *regulator_get_drvdata(struct regulator *regulator) | |
4562 | { | |
4563 | return regulator->rdev->reg_data; | |
4564 | } | |
4565 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | |
4566 | ||
4567 | /** | |
4568 | * regulator_set_drvdata - set regulator driver data | |
4569 | * @regulator: regulator | |
4570 | * @data: data | |
4571 | */ | |
4572 | void regulator_set_drvdata(struct regulator *regulator, void *data) | |
4573 | { | |
4574 | regulator->rdev->reg_data = data; | |
4575 | } | |
4576 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | |
4577 | ||
4578 | /** | |
4579 | * regulator_get_id - get regulator ID | |
69279fb9 | 4580 | * @rdev: regulator |
414c70cb LG |
4581 | */ |
4582 | int rdev_get_id(struct regulator_dev *rdev) | |
4583 | { | |
4584 | return rdev->desc->id; | |
4585 | } | |
4586 | EXPORT_SYMBOL_GPL(rdev_get_id); | |
4587 | ||
a5766f11 LG |
4588 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
4589 | { | |
4590 | return &rdev->dev; | |
4591 | } | |
4592 | EXPORT_SYMBOL_GPL(rdev_get_dev); | |
4593 | ||
4594 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) | |
4595 | { | |
4596 | return reg_init_data->driver_data; | |
4597 | } | |
4598 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | |
4599 | ||
ba55a974 | 4600 | #ifdef CONFIG_DEBUG_FS |
dbc55955 | 4601 | static int supply_map_show(struct seq_file *sf, void *data) |
ba55a974 | 4602 | { |
ba55a974 MB |
4603 | struct regulator_map *map; |
4604 | ||
ba55a974 | 4605 | list_for_each_entry(map, ®ulator_map_list, list) { |
dbc55955 HZ |
4606 | seq_printf(sf, "%s -> %s.%s\n", |
4607 | rdev_get_name(map->regulator), map->dev_name, | |
4608 | map->supply); | |
ba55a974 MB |
4609 | } |
4610 | ||
dbc55955 HZ |
4611 | return 0; |
4612 | } | |
ba55a974 | 4613 | |
dbc55955 HZ |
4614 | static int supply_map_open(struct inode *inode, struct file *file) |
4615 | { | |
4616 | return single_open(file, supply_map_show, inode->i_private); | |
ba55a974 | 4617 | } |
24751434 | 4618 | #endif |
ba55a974 MB |
4619 | |
4620 | static const struct file_operations supply_map_fops = { | |
24751434 | 4621 | #ifdef CONFIG_DEBUG_FS |
dbc55955 HZ |
4622 | .open = supply_map_open, |
4623 | .read = seq_read, | |
4624 | .llseek = seq_lseek, | |
4625 | .release = single_release, | |
ba55a974 | 4626 | #endif |
24751434 | 4627 | }; |
ba55a974 | 4628 | |
7c225ec9 | 4629 | #ifdef CONFIG_DEBUG_FS |
85f3b431 TV |
4630 | struct summary_data { |
4631 | struct seq_file *s; | |
4632 | struct regulator_dev *parent; | |
4633 | int level; | |
4634 | }; | |
4635 | ||
4636 | static void regulator_summary_show_subtree(struct seq_file *s, | |
4637 | struct regulator_dev *rdev, | |
4638 | int level); | |
4639 | ||
4640 | static int regulator_summary_show_children(struct device *dev, void *data) | |
4641 | { | |
4642 | struct regulator_dev *rdev = dev_to_rdev(dev); | |
4643 | struct summary_data *summary_data = data; | |
4644 | ||
4645 | if (rdev->supply && rdev->supply->rdev == summary_data->parent) | |
4646 | regulator_summary_show_subtree(summary_data->s, rdev, | |
4647 | summary_data->level + 1); | |
4648 | ||
4649 | return 0; | |
4650 | } | |
4651 | ||
7c225ec9 HS |
4652 | static void regulator_summary_show_subtree(struct seq_file *s, |
4653 | struct regulator_dev *rdev, | |
4654 | int level) | |
4655 | { | |
7c225ec9 HS |
4656 | struct regulation_constraints *c; |
4657 | struct regulator *consumer; | |
85f3b431 | 4658 | struct summary_data summary_data; |
7c225ec9 HS |
4659 | |
4660 | if (!rdev) | |
4661 | return; | |
4662 | ||
7c225ec9 HS |
4663 | seq_printf(s, "%*s%-*s %3d %4d %6d ", |
4664 | level * 3 + 1, "", | |
4665 | 30 - level * 3, rdev_get_name(rdev), | |
4666 | rdev->use_count, rdev->open_count, rdev->bypass_count); | |
4667 | ||
23296099 HS |
4668 | seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000); |
4669 | seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000); | |
7c225ec9 HS |
4670 | |
4671 | c = rdev->constraints; | |
4672 | if (c) { | |
4673 | switch (rdev->desc->type) { | |
4674 | case REGULATOR_VOLTAGE: | |
4675 | seq_printf(s, "%5dmV %5dmV ", | |
4676 | c->min_uV / 1000, c->max_uV / 1000); | |
4677 | break; | |
4678 | case REGULATOR_CURRENT: | |
4679 | seq_printf(s, "%5dmA %5dmA ", | |
4680 | c->min_uA / 1000, c->max_uA / 1000); | |
4681 | break; | |
4682 | } | |
4683 | } | |
4684 | ||
4685 | seq_puts(s, "\n"); | |
4686 | ||
4687 | list_for_each_entry(consumer, &rdev->consumer_list, list) { | |
e42a46b6 | 4688 | if (consumer->dev && consumer->dev->class == ®ulator_class) |
7c225ec9 HS |
4689 | continue; |
4690 | ||
4691 | seq_printf(s, "%*s%-*s ", | |
4692 | (level + 1) * 3 + 1, "", | |
e42a46b6 LC |
4693 | 30 - (level + 1) * 3, |
4694 | consumer->dev ? dev_name(consumer->dev) : "deviceless"); | |
7c225ec9 HS |
4695 | |
4696 | switch (rdev->desc->type) { | |
4697 | case REGULATOR_VOLTAGE: | |
23296099 | 4698 | seq_printf(s, "%37dmV %5dmV", |
c360a6df CZ |
4699 | consumer->voltage[PM_SUSPEND_ON].min_uV / 1000, |
4700 | consumer->voltage[PM_SUSPEND_ON].max_uV / 1000); | |
7c225ec9 HS |
4701 | break; |
4702 | case REGULATOR_CURRENT: | |
7c225ec9 HS |
4703 | break; |
4704 | } | |
4705 | ||
4706 | seq_puts(s, "\n"); | |
4707 | } | |
4708 | ||
85f3b431 TV |
4709 | summary_data.s = s; |
4710 | summary_data.level = level; | |
4711 | summary_data.parent = rdev; | |
7c225ec9 | 4712 | |
85f3b431 TV |
4713 | class_for_each_device(®ulator_class, NULL, &summary_data, |
4714 | regulator_summary_show_children); | |
7c225ec9 HS |
4715 | } |
4716 | ||
85f3b431 | 4717 | static int regulator_summary_show_roots(struct device *dev, void *data) |
7c225ec9 | 4718 | { |
85f3b431 TV |
4719 | struct regulator_dev *rdev = dev_to_rdev(dev); |
4720 | struct seq_file *s = data; | |
7c225ec9 | 4721 | |
85f3b431 TV |
4722 | if (!rdev->supply) |
4723 | regulator_summary_show_subtree(s, rdev, 0); | |
7c225ec9 | 4724 | |
85f3b431 TV |
4725 | return 0; |
4726 | } | |
7c225ec9 | 4727 | |
85f3b431 TV |
4728 | static int regulator_summary_show(struct seq_file *s, void *data) |
4729 | { | |
4730 | seq_puts(s, " regulator use open bypass voltage current min max\n"); | |
4731 | seq_puts(s, "-------------------------------------------------------------------------------\n"); | |
7c225ec9 | 4732 | |
85f3b431 TV |
4733 | class_for_each_device(®ulator_class, NULL, s, |
4734 | regulator_summary_show_roots); | |
7c225ec9 HS |
4735 | |
4736 | return 0; | |
4737 | } | |
4738 | ||
4739 | static int regulator_summary_open(struct inode *inode, struct file *file) | |
4740 | { | |
4741 | return single_open(file, regulator_summary_show, inode->i_private); | |
4742 | } | |
4743 | #endif | |
4744 | ||
4745 | static const struct file_operations regulator_summary_fops = { | |
4746 | #ifdef CONFIG_DEBUG_FS | |
4747 | .open = regulator_summary_open, | |
4748 | .read = seq_read, | |
4749 | .llseek = seq_lseek, | |
4750 | .release = single_release, | |
4751 | #endif | |
4752 | }; | |
4753 | ||
414c70cb LG |
4754 | static int __init regulator_init(void) |
4755 | { | |
34abbd68 MB |
4756 | int ret; |
4757 | ||
34abbd68 MB |
4758 | ret = class_register(®ulator_class); |
4759 | ||
1130e5b3 | 4760 | debugfs_root = debugfs_create_dir("regulator", NULL); |
24751434 | 4761 | if (!debugfs_root) |
1130e5b3 | 4762 | pr_warn("regulator: Failed to create debugfs directory\n"); |
ba55a974 | 4763 | |
f4d562c6 MB |
4764 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
4765 | &supply_map_fops); | |
1130e5b3 | 4766 | |
7c225ec9 | 4767 | debugfs_create_file("regulator_summary", 0444, debugfs_root, |
85f3b431 | 4768 | NULL, ®ulator_summary_fops); |
7c225ec9 | 4769 | |
34abbd68 MB |
4770 | regulator_dummy_init(); |
4771 | ||
4772 | return ret; | |
414c70cb LG |
4773 | } |
4774 | ||
4775 | /* init early to allow our consumers to complete system booting */ | |
4776 | core_initcall(regulator_init); | |
ca725561 | 4777 | |
609ca5f3 | 4778 | static int __init regulator_late_cleanup(struct device *dev, void *data) |
ca725561 | 4779 | { |
609ca5f3 MB |
4780 | struct regulator_dev *rdev = dev_to_rdev(dev); |
4781 | const struct regulator_ops *ops = rdev->desc->ops; | |
4782 | struct regulation_constraints *c = rdev->constraints; | |
ca725561 | 4783 | int enabled, ret; |
ca725561 | 4784 | |
609ca5f3 MB |
4785 | if (c && c->always_on) |
4786 | return 0; | |
4787 | ||
8a34e979 | 4788 | if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) |
609ca5f3 MB |
4789 | return 0; |
4790 | ||
66cf9a7e | 4791 | regulator_lock(rdev); |
609ca5f3 MB |
4792 | |
4793 | if (rdev->use_count) | |
4794 | goto unlock; | |
4795 | ||
4796 | /* If we can't read the status assume it's on. */ | |
4797 | if (ops->is_enabled) | |
4798 | enabled = ops->is_enabled(rdev); | |
4799 | else | |
4800 | enabled = 1; | |
4801 | ||
4802 | if (!enabled) | |
4803 | goto unlock; | |
4804 | ||
4805 | if (have_full_constraints()) { | |
4806 | /* We log since this may kill the system if it goes | |
4807 | * wrong. */ | |
4808 | rdev_info(rdev, "disabling\n"); | |
4809 | ret = _regulator_do_disable(rdev); | |
4810 | if (ret != 0) | |
4811 | rdev_err(rdev, "couldn't disable: %d\n", ret); | |
4812 | } else { | |
4813 | /* The intention is that in future we will | |
4814 | * assume that full constraints are provided | |
4815 | * so warn even if we aren't going to do | |
4816 | * anything here. | |
4817 | */ | |
4818 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); | |
4819 | } | |
4820 | ||
4821 | unlock: | |
66cf9a7e | 4822 | regulator_unlock(rdev); |
609ca5f3 MB |
4823 | |
4824 | return 0; | |
4825 | } | |
4826 | ||
4827 | static int __init regulator_init_complete(void) | |
4828 | { | |
86f5fcfc MB |
4829 | /* |
4830 | * Since DT doesn't provide an idiomatic mechanism for | |
4831 | * enabling full constraints and since it's much more natural | |
4832 | * with DT to provide them just assume that a DT enabled | |
4833 | * system has full constraints. | |
4834 | */ | |
4835 | if (of_have_populated_dt()) | |
4836 | has_full_constraints = true; | |
4837 | ||
3827b64d JMC |
4838 | /* |
4839 | * Regulators may had failed to resolve their input supplies | |
4840 | * when were registered, either because the input supply was | |
4841 | * not registered yet or because its parent device was not | |
4842 | * bound yet. So attempt to resolve the input supplies for | |
4843 | * pending regulators before trying to disable unused ones. | |
4844 | */ | |
4845 | class_for_each_device(®ulator_class, NULL, NULL, | |
4846 | regulator_register_resolve_supply); | |
4847 | ||
ca725561 | 4848 | /* If we have a full configuration then disable any regulators |
e9535834 MB |
4849 | * we have permission to change the status for and which are |
4850 | * not in use or always_on. This is effectively the default | |
4851 | * for DT and ACPI as they have full constraints. | |
ca725561 | 4852 | */ |
609ca5f3 MB |
4853 | class_for_each_device(®ulator_class, NULL, NULL, |
4854 | regulator_late_cleanup); | |
ca725561 | 4855 | |
d3d64537 MP |
4856 | class_for_each_device(®ulator_class, NULL, NULL, |
4857 | regulator_register_fill_coupling_array); | |
4858 | ||
ca725561 MB |
4859 | return 0; |
4860 | } | |
fd482a3e | 4861 | late_initcall_sync(regulator_init_complete); |