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