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 | ||
1d7372e1 | 16 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
c5e28ed7 | 17 | |
414c70cb LG |
18 | #include <linux/kernel.h> |
19 | #include <linux/init.h> | |
1130e5b3 | 20 | #include <linux/debugfs.h> |
414c70cb | 21 | #include <linux/device.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
f21e0e81 | 23 | #include <linux/async.h> |
414c70cb LG |
24 | #include <linux/err.h> |
25 | #include <linux/mutex.h> | |
26 | #include <linux/suspend.h> | |
31aae2be | 27 | #include <linux/delay.h> |
69511a45 RN |
28 | #include <linux/of.h> |
29 | #include <linux/regulator/of_regulator.h> | |
414c70cb LG |
30 | #include <linux/regulator/consumer.h> |
31 | #include <linux/regulator/driver.h> | |
32 | #include <linux/regulator/machine.h> | |
65602c32 | 33 | #include <linux/module.h> |
414c70cb | 34 | |
02fa3ec0 MB |
35 | #define CREATE_TRACE_POINTS |
36 | #include <trace/events/regulator.h> | |
37 | ||
34abbd68 MB |
38 | #include "dummy.h" |
39 | ||
7d51a0db MB |
40 | #define rdev_crit(rdev, fmt, ...) \ |
41 | pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
5da84fd9 JP |
42 | #define rdev_err(rdev, fmt, ...) \ |
43 | pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
44 | #define rdev_warn(rdev, fmt, ...) \ | |
45 | pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
46 | #define rdev_info(rdev, fmt, ...) \ | |
47 | pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
48 | #define rdev_dbg(rdev, fmt, ...) \ | |
49 | pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) | |
50 | ||
414c70cb LG |
51 | static DEFINE_MUTEX(regulator_list_mutex); |
52 | static LIST_HEAD(regulator_list); | |
53 | static LIST_HEAD(regulator_map_list); | |
21cf891a | 54 | static bool has_full_constraints; |
688fe99a | 55 | static bool board_wants_dummy_regulator; |
414c70cb | 56 | |
1130e5b3 MB |
57 | #ifdef CONFIG_DEBUG_FS |
58 | static struct dentry *debugfs_root; | |
59 | #endif | |
60 | ||
8dc5390d | 61 | /* |
414c70cb LG |
62 | * struct regulator_map |
63 | * | |
64 | * Used to provide symbolic supply names to devices. | |
65 | */ | |
66 | struct regulator_map { | |
67 | struct list_head list; | |
40f9244f | 68 | const char *dev_name; /* The dev_name() for the consumer */ |
414c70cb | 69 | const char *supply; |
a5766f11 | 70 | struct regulator_dev *regulator; |
414c70cb LG |
71 | }; |
72 | ||
414c70cb LG |
73 | /* |
74 | * struct regulator | |
75 | * | |
76 | * One for each consumer device. | |
77 | */ | |
78 | struct regulator { | |
79 | struct device *dev; | |
80 | struct list_head list; | |
81 | int uA_load; | |
82 | int min_uV; | |
83 | int max_uV; | |
414c70cb LG |
84 | char *supply_name; |
85 | struct device_attribute dev_attr; | |
86 | struct regulator_dev *rdev; | |
5de70519 MB |
87 | #ifdef CONFIG_DEBUG_FS |
88 | struct dentry *debugfs; | |
89 | #endif | |
414c70cb LG |
90 | }; |
91 | ||
92 | static int _regulator_is_enabled(struct regulator_dev *rdev); | |
3801b86a | 93 | static int _regulator_disable(struct regulator_dev *rdev); |
414c70cb LG |
94 | static int _regulator_get_voltage(struct regulator_dev *rdev); |
95 | static int _regulator_get_current_limit(struct regulator_dev *rdev); | |
96 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); | |
97 | static void _notifier_call_chain(struct regulator_dev *rdev, | |
98 | unsigned long event, void *data); | |
75790251 MB |
99 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
100 | int min_uV, int max_uV); | |
3801b86a MB |
101 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
102 | struct device *dev, | |
103 | const char *supply_name); | |
414c70cb | 104 | |
1083c393 MB |
105 | static const char *rdev_get_name(struct regulator_dev *rdev) |
106 | { | |
107 | if (rdev->constraints && rdev->constraints->name) | |
108 | return rdev->constraints->name; | |
109 | else if (rdev->desc->name) | |
110 | return rdev->desc->name; | |
111 | else | |
112 | return ""; | |
113 | } | |
114 | ||
414c70cb LG |
115 | /* gets the regulator for a given consumer device */ |
116 | static struct regulator *get_device_regulator(struct device *dev) | |
117 | { | |
118 | struct regulator *regulator = NULL; | |
119 | struct regulator_dev *rdev; | |
120 | ||
121 | mutex_lock(®ulator_list_mutex); | |
122 | list_for_each_entry(rdev, ®ulator_list, list) { | |
123 | mutex_lock(&rdev->mutex); | |
124 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
125 | if (regulator->dev == dev) { | |
126 | mutex_unlock(&rdev->mutex); | |
127 | mutex_unlock(®ulator_list_mutex); | |
128 | return regulator; | |
129 | } | |
130 | } | |
131 | mutex_unlock(&rdev->mutex); | |
132 | } | |
133 | mutex_unlock(®ulator_list_mutex); | |
134 | return NULL; | |
135 | } | |
136 | ||
69511a45 RN |
137 | /** |
138 | * of_get_regulator - get a regulator device node based on supply name | |
139 | * @dev: Device pointer for the consumer (of regulator) device | |
140 | * @supply: regulator supply name | |
141 | * | |
142 | * Extract the regulator device node corresponding to the supply name. | |
143 | * retruns the device node corresponding to the regulator if found, else | |
144 | * returns NULL. | |
145 | */ | |
146 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) | |
147 | { | |
148 | struct device_node *regnode = NULL; | |
149 | char prop_name[32]; /* 32 is max size of property name */ | |
150 | ||
151 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); | |
152 | ||
153 | snprintf(prop_name, 32, "%s-supply", supply); | |
154 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); | |
155 | ||
156 | if (!regnode) { | |
157 | dev_warn(dev, "%s property in node %s references invalid phandle", | |
158 | prop_name, dev->of_node->full_name); | |
159 | return NULL; | |
160 | } | |
161 | return regnode; | |
162 | } | |
163 | ||
414c70cb LG |
164 | /* Platform voltage constraint check */ |
165 | static int regulator_check_voltage(struct regulator_dev *rdev, | |
166 | int *min_uV, int *max_uV) | |
167 | { | |
168 | BUG_ON(*min_uV > *max_uV); | |
169 | ||
170 | if (!rdev->constraints) { | |
5da84fd9 | 171 | rdev_err(rdev, "no constraints\n"); |
414c70cb LG |
172 | return -ENODEV; |
173 | } | |
174 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { | |
5da84fd9 | 175 | rdev_err(rdev, "operation not allowed\n"); |
414c70cb LG |
176 | return -EPERM; |
177 | } | |
178 | ||
179 | if (*max_uV > rdev->constraints->max_uV) | |
180 | *max_uV = rdev->constraints->max_uV; | |
181 | if (*min_uV < rdev->constraints->min_uV) | |
182 | *min_uV = rdev->constraints->min_uV; | |
183 | ||
89f425ed MB |
184 | if (*min_uV > *max_uV) { |
185 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", | |
54abd335 | 186 | *min_uV, *max_uV); |
414c70cb | 187 | return -EINVAL; |
89f425ed | 188 | } |
414c70cb LG |
189 | |
190 | return 0; | |
191 | } | |
192 | ||
05fda3b1 TP |
193 | /* Make sure we select a voltage that suits the needs of all |
194 | * regulator consumers | |
195 | */ | |
196 | static int regulator_check_consumers(struct regulator_dev *rdev, | |
197 | int *min_uV, int *max_uV) | |
198 | { | |
199 | struct regulator *regulator; | |
200 | ||
201 | list_for_each_entry(regulator, &rdev->consumer_list, list) { | |
4aa922c0 MB |
202 | /* |
203 | * Assume consumers that didn't say anything are OK | |
204 | * with anything in the constraint range. | |
205 | */ | |
206 | if (!regulator->min_uV && !regulator->max_uV) | |
207 | continue; | |
208 | ||
05fda3b1 TP |
209 | if (*max_uV > regulator->max_uV) |
210 | *max_uV = regulator->max_uV; | |
211 | if (*min_uV < regulator->min_uV) | |
212 | *min_uV = regulator->min_uV; | |
213 | } | |
214 | ||
215 | if (*min_uV > *max_uV) | |
216 | return -EINVAL; | |
217 | ||
218 | return 0; | |
219 | } | |
220 | ||
414c70cb LG |
221 | /* current constraint check */ |
222 | static int regulator_check_current_limit(struct regulator_dev *rdev, | |
223 | int *min_uA, int *max_uA) | |
224 | { | |
225 | BUG_ON(*min_uA > *max_uA); | |
226 | ||
227 | if (!rdev->constraints) { | |
5da84fd9 | 228 | rdev_err(rdev, "no constraints\n"); |
414c70cb LG |
229 | return -ENODEV; |
230 | } | |
231 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { | |
5da84fd9 | 232 | rdev_err(rdev, "operation not allowed\n"); |
414c70cb LG |
233 | return -EPERM; |
234 | } | |
235 | ||
236 | if (*max_uA > rdev->constraints->max_uA) | |
237 | *max_uA = rdev->constraints->max_uA; | |
238 | if (*min_uA < rdev->constraints->min_uA) | |
239 | *min_uA = rdev->constraints->min_uA; | |
240 | ||
89f425ed MB |
241 | if (*min_uA > *max_uA) { |
242 | rdev_err(rdev, "unsupportable current range: %d-%duA\n", | |
54abd335 | 243 | *min_uA, *max_uA); |
414c70cb | 244 | return -EINVAL; |
89f425ed | 245 | } |
414c70cb LG |
246 | |
247 | return 0; | |
248 | } | |
249 | ||
250 | /* operating mode constraint check */ | |
2c608234 | 251 | static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode) |
414c70cb | 252 | { |
2c608234 | 253 | switch (*mode) { |
e573520b DB |
254 | case REGULATOR_MODE_FAST: |
255 | case REGULATOR_MODE_NORMAL: | |
256 | case REGULATOR_MODE_IDLE: | |
257 | case REGULATOR_MODE_STANDBY: | |
258 | break; | |
259 | default: | |
89f425ed | 260 | rdev_err(rdev, "invalid mode %x specified\n", *mode); |
e573520b DB |
261 | return -EINVAL; |
262 | } | |
263 | ||
414c70cb | 264 | if (!rdev->constraints) { |
5da84fd9 | 265 | rdev_err(rdev, "no constraints\n"); |
414c70cb LG |
266 | return -ENODEV; |
267 | } | |
268 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { | |
5da84fd9 | 269 | rdev_err(rdev, "operation not allowed\n"); |
414c70cb LG |
270 | return -EPERM; |
271 | } | |
2c608234 MB |
272 | |
273 | /* The modes are bitmasks, the most power hungry modes having | |
274 | * the lowest values. If the requested mode isn't supported | |
275 | * try higher modes. */ | |
276 | while (*mode) { | |
277 | if (rdev->constraints->valid_modes_mask & *mode) | |
278 | return 0; | |
279 | *mode /= 2; | |
414c70cb | 280 | } |
2c608234 MB |
281 | |
282 | return -EINVAL; | |
414c70cb LG |
283 | } |
284 | ||
285 | /* dynamic regulator mode switching constraint check */ | |
286 | static int regulator_check_drms(struct regulator_dev *rdev) | |
287 | { | |
288 | if (!rdev->constraints) { | |
5da84fd9 | 289 | rdev_err(rdev, "no constraints\n"); |
414c70cb LG |
290 | return -ENODEV; |
291 | } | |
292 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { | |
5da84fd9 | 293 | rdev_err(rdev, "operation not allowed\n"); |
414c70cb LG |
294 | return -EPERM; |
295 | } | |
296 | return 0; | |
297 | } | |
298 | ||
299 | static ssize_t device_requested_uA_show(struct device *dev, | |
300 | struct device_attribute *attr, char *buf) | |
301 | { | |
302 | struct regulator *regulator; | |
303 | ||
304 | regulator = get_device_regulator(dev); | |
305 | if (regulator == NULL) | |
306 | return 0; | |
307 | ||
308 | return sprintf(buf, "%d\n", regulator->uA_load); | |
309 | } | |
310 | ||
311 | static ssize_t regulator_uV_show(struct device *dev, | |
312 | struct device_attribute *attr, char *buf) | |
313 | { | |
a5766f11 | 314 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
315 | ssize_t ret; |
316 | ||
317 | mutex_lock(&rdev->mutex); | |
318 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); | |
319 | mutex_unlock(&rdev->mutex); | |
320 | ||
321 | return ret; | |
322 | } | |
7ad68e2f | 323 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
414c70cb LG |
324 | |
325 | static ssize_t regulator_uA_show(struct device *dev, | |
326 | struct device_attribute *attr, char *buf) | |
327 | { | |
a5766f11 | 328 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
329 | |
330 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); | |
331 | } | |
7ad68e2f | 332 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); |
414c70cb | 333 | |
bc558a60 MB |
334 | static ssize_t regulator_name_show(struct device *dev, |
335 | struct device_attribute *attr, char *buf) | |
336 | { | |
337 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
bc558a60 | 338 | |
1083c393 | 339 | return sprintf(buf, "%s\n", rdev_get_name(rdev)); |
bc558a60 MB |
340 | } |
341 | ||
4fca9545 | 342 | static ssize_t regulator_print_opmode(char *buf, int mode) |
414c70cb | 343 | { |
414c70cb LG |
344 | switch (mode) { |
345 | case REGULATOR_MODE_FAST: | |
346 | return sprintf(buf, "fast\n"); | |
347 | case REGULATOR_MODE_NORMAL: | |
348 | return sprintf(buf, "normal\n"); | |
349 | case REGULATOR_MODE_IDLE: | |
350 | return sprintf(buf, "idle\n"); | |
351 | case REGULATOR_MODE_STANDBY: | |
352 | return sprintf(buf, "standby\n"); | |
353 | } | |
354 | return sprintf(buf, "unknown\n"); | |
355 | } | |
356 | ||
4fca9545 DB |
357 | static ssize_t regulator_opmode_show(struct device *dev, |
358 | struct device_attribute *attr, char *buf) | |
414c70cb | 359 | { |
a5766f11 | 360 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 361 | |
4fca9545 DB |
362 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); |
363 | } | |
7ad68e2f | 364 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); |
4fca9545 DB |
365 | |
366 | static ssize_t regulator_print_state(char *buf, int state) | |
367 | { | |
414c70cb LG |
368 | if (state > 0) |
369 | return sprintf(buf, "enabled\n"); | |
370 | else if (state == 0) | |
371 | return sprintf(buf, "disabled\n"); | |
372 | else | |
373 | return sprintf(buf, "unknown\n"); | |
374 | } | |
375 | ||
4fca9545 DB |
376 | static ssize_t regulator_state_show(struct device *dev, |
377 | struct device_attribute *attr, char *buf) | |
378 | { | |
379 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
9332546f MB |
380 | ssize_t ret; |
381 | ||
382 | mutex_lock(&rdev->mutex); | |
383 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); | |
384 | mutex_unlock(&rdev->mutex); | |
4fca9545 | 385 | |
9332546f | 386 | return ret; |
4fca9545 | 387 | } |
7ad68e2f | 388 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
4fca9545 | 389 | |
853116a1 DB |
390 | static ssize_t regulator_status_show(struct device *dev, |
391 | struct device_attribute *attr, char *buf) | |
392 | { | |
393 | struct regulator_dev *rdev = dev_get_drvdata(dev); | |
394 | int status; | |
395 | char *label; | |
396 | ||
397 | status = rdev->desc->ops->get_status(rdev); | |
398 | if (status < 0) | |
399 | return status; | |
400 | ||
401 | switch (status) { | |
402 | case REGULATOR_STATUS_OFF: | |
403 | label = "off"; | |
404 | break; | |
405 | case REGULATOR_STATUS_ON: | |
406 | label = "on"; | |
407 | break; | |
408 | case REGULATOR_STATUS_ERROR: | |
409 | label = "error"; | |
410 | break; | |
411 | case REGULATOR_STATUS_FAST: | |
412 | label = "fast"; | |
413 | break; | |
414 | case REGULATOR_STATUS_NORMAL: | |
415 | label = "normal"; | |
416 | break; | |
417 | case REGULATOR_STATUS_IDLE: | |
418 | label = "idle"; | |
419 | break; | |
420 | case REGULATOR_STATUS_STANDBY: | |
421 | label = "standby"; | |
422 | break; | |
423 | default: | |
424 | return -ERANGE; | |
425 | } | |
426 | ||
427 | return sprintf(buf, "%s\n", label); | |
428 | } | |
429 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); | |
430 | ||
414c70cb LG |
431 | static ssize_t regulator_min_uA_show(struct device *dev, |
432 | struct device_attribute *attr, char *buf) | |
433 | { | |
a5766f11 | 434 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
435 | |
436 | if (!rdev->constraints) | |
437 | return sprintf(buf, "constraint not defined\n"); | |
438 | ||
439 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); | |
440 | } | |
7ad68e2f | 441 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
414c70cb LG |
442 | |
443 | static ssize_t regulator_max_uA_show(struct device *dev, | |
444 | struct device_attribute *attr, char *buf) | |
445 | { | |
a5766f11 | 446 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
447 | |
448 | if (!rdev->constraints) | |
449 | return sprintf(buf, "constraint not defined\n"); | |
450 | ||
451 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); | |
452 | } | |
7ad68e2f | 453 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
414c70cb LG |
454 | |
455 | static ssize_t regulator_min_uV_show(struct device *dev, | |
456 | struct device_attribute *attr, char *buf) | |
457 | { | |
a5766f11 | 458 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
459 | |
460 | if (!rdev->constraints) | |
461 | return sprintf(buf, "constraint not defined\n"); | |
462 | ||
463 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); | |
464 | } | |
7ad68e2f | 465 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
414c70cb LG |
466 | |
467 | static ssize_t regulator_max_uV_show(struct device *dev, | |
468 | struct device_attribute *attr, char *buf) | |
469 | { | |
a5766f11 | 470 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
471 | |
472 | if (!rdev->constraints) | |
473 | return sprintf(buf, "constraint not defined\n"); | |
474 | ||
475 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); | |
476 | } | |
7ad68e2f | 477 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
414c70cb LG |
478 | |
479 | static ssize_t regulator_total_uA_show(struct device *dev, | |
480 | struct device_attribute *attr, char *buf) | |
481 | { | |
a5766f11 | 482 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
483 | struct regulator *regulator; |
484 | int uA = 0; | |
485 | ||
486 | mutex_lock(&rdev->mutex); | |
487 | list_for_each_entry(regulator, &rdev->consumer_list, list) | |
fa2984d4 | 488 | uA += regulator->uA_load; |
414c70cb LG |
489 | mutex_unlock(&rdev->mutex); |
490 | return sprintf(buf, "%d\n", uA); | |
491 | } | |
7ad68e2f | 492 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
414c70cb LG |
493 | |
494 | static ssize_t regulator_num_users_show(struct device *dev, | |
495 | struct device_attribute *attr, char *buf) | |
496 | { | |
a5766f11 | 497 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
498 | return sprintf(buf, "%d\n", rdev->use_count); |
499 | } | |
500 | ||
501 | static ssize_t regulator_type_show(struct device *dev, | |
502 | struct device_attribute *attr, char *buf) | |
503 | { | |
a5766f11 | 504 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
505 | |
506 | switch (rdev->desc->type) { | |
507 | case REGULATOR_VOLTAGE: | |
508 | return sprintf(buf, "voltage\n"); | |
509 | case REGULATOR_CURRENT: | |
510 | return sprintf(buf, "current\n"); | |
511 | } | |
512 | return sprintf(buf, "unknown\n"); | |
513 | } | |
514 | ||
515 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, | |
516 | struct device_attribute *attr, char *buf) | |
517 | { | |
a5766f11 | 518 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 519 | |
414c70cb LG |
520 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
521 | } | |
7ad68e2f DB |
522 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
523 | regulator_suspend_mem_uV_show, NULL); | |
414c70cb LG |
524 | |
525 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, | |
526 | struct device_attribute *attr, char *buf) | |
527 | { | |
a5766f11 | 528 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 529 | |
414c70cb LG |
530 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
531 | } | |
7ad68e2f DB |
532 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
533 | regulator_suspend_disk_uV_show, NULL); | |
414c70cb LG |
534 | |
535 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, | |
536 | struct device_attribute *attr, char *buf) | |
537 | { | |
a5766f11 | 538 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 539 | |
414c70cb LG |
540 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
541 | } | |
7ad68e2f DB |
542 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
543 | regulator_suspend_standby_uV_show, NULL); | |
414c70cb | 544 | |
414c70cb LG |
545 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
546 | struct device_attribute *attr, char *buf) | |
547 | { | |
a5766f11 | 548 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 549 | |
4fca9545 DB |
550 | return regulator_print_opmode(buf, |
551 | rdev->constraints->state_mem.mode); | |
414c70cb | 552 | } |
7ad68e2f DB |
553 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
554 | regulator_suspend_mem_mode_show, NULL); | |
414c70cb LG |
555 | |
556 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, | |
557 | struct device_attribute *attr, char *buf) | |
558 | { | |
a5766f11 | 559 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 560 | |
4fca9545 DB |
561 | return regulator_print_opmode(buf, |
562 | rdev->constraints->state_disk.mode); | |
414c70cb | 563 | } |
7ad68e2f DB |
564 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
565 | regulator_suspend_disk_mode_show, NULL); | |
414c70cb LG |
566 | |
567 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, | |
568 | struct device_attribute *attr, char *buf) | |
569 | { | |
a5766f11 | 570 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 571 | |
4fca9545 DB |
572 | return regulator_print_opmode(buf, |
573 | rdev->constraints->state_standby.mode); | |
414c70cb | 574 | } |
7ad68e2f DB |
575 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
576 | regulator_suspend_standby_mode_show, NULL); | |
414c70cb LG |
577 | |
578 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, | |
579 | struct device_attribute *attr, char *buf) | |
580 | { | |
a5766f11 | 581 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 582 | |
4fca9545 DB |
583 | return regulator_print_state(buf, |
584 | rdev->constraints->state_mem.enabled); | |
414c70cb | 585 | } |
7ad68e2f DB |
586 | static DEVICE_ATTR(suspend_mem_state, 0444, |
587 | regulator_suspend_mem_state_show, NULL); | |
414c70cb LG |
588 | |
589 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, | |
590 | struct device_attribute *attr, char *buf) | |
591 | { | |
a5766f11 | 592 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 593 | |
4fca9545 DB |
594 | return regulator_print_state(buf, |
595 | rdev->constraints->state_disk.enabled); | |
414c70cb | 596 | } |
7ad68e2f DB |
597 | static DEVICE_ATTR(suspend_disk_state, 0444, |
598 | regulator_suspend_disk_state_show, NULL); | |
414c70cb LG |
599 | |
600 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, | |
601 | struct device_attribute *attr, char *buf) | |
602 | { | |
a5766f11 | 603 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb | 604 | |
4fca9545 DB |
605 | return regulator_print_state(buf, |
606 | rdev->constraints->state_standby.enabled); | |
414c70cb | 607 | } |
7ad68e2f DB |
608 | static DEVICE_ATTR(suspend_standby_state, 0444, |
609 | regulator_suspend_standby_state_show, NULL); | |
610 | ||
bc558a60 | 611 | |
7ad68e2f DB |
612 | /* |
613 | * These are the only attributes are present for all regulators. | |
614 | * Other attributes are a function of regulator functionality. | |
615 | */ | |
414c70cb | 616 | static struct device_attribute regulator_dev_attrs[] = { |
bc558a60 | 617 | __ATTR(name, 0444, regulator_name_show, NULL), |
414c70cb LG |
618 | __ATTR(num_users, 0444, regulator_num_users_show, NULL), |
619 | __ATTR(type, 0444, regulator_type_show, NULL), | |
414c70cb LG |
620 | __ATTR_NULL, |
621 | }; | |
622 | ||
623 | static void regulator_dev_release(struct device *dev) | |
624 | { | |
a5766f11 | 625 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
414c70cb LG |
626 | kfree(rdev); |
627 | } | |
628 | ||
629 | static struct class regulator_class = { | |
630 | .name = "regulator", | |
631 | .dev_release = regulator_dev_release, | |
632 | .dev_attrs = regulator_dev_attrs, | |
633 | }; | |
634 | ||
635 | /* Calculate the new optimum regulator operating mode based on the new total | |
636 | * consumer load. All locks held by caller */ | |
637 | static void drms_uA_update(struct regulator_dev *rdev) | |
638 | { | |
639 | struct regulator *sibling; | |
640 | int current_uA = 0, output_uV, input_uV, err; | |
641 | unsigned int mode; | |
642 | ||
643 | err = regulator_check_drms(rdev); | |
644 | if (err < 0 || !rdev->desc->ops->get_optimum_mode || | |
476c2d83 MB |
645 | (!rdev->desc->ops->get_voltage && |
646 | !rdev->desc->ops->get_voltage_sel) || | |
647 | !rdev->desc->ops->set_mode) | |
036de8ef | 648 | return; |
414c70cb LG |
649 | |
650 | /* get output voltage */ | |
1bf5a1f8 | 651 | output_uV = _regulator_get_voltage(rdev); |
414c70cb LG |
652 | if (output_uV <= 0) |
653 | return; | |
654 | ||
655 | /* get input voltage */ | |
1bf5a1f8 MB |
656 | input_uV = 0; |
657 | if (rdev->supply) | |
658 | input_uV = _regulator_get_voltage(rdev); | |
659 | if (input_uV <= 0) | |
414c70cb LG |
660 | input_uV = rdev->constraints->input_uV; |
661 | if (input_uV <= 0) | |
662 | return; | |
663 | ||
664 | /* calc total requested load */ | |
665 | list_for_each_entry(sibling, &rdev->consumer_list, list) | |
fa2984d4 | 666 | current_uA += sibling->uA_load; |
414c70cb LG |
667 | |
668 | /* now get the optimum mode for our new total regulator load */ | |
669 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, | |
670 | output_uV, current_uA); | |
671 | ||
672 | /* check the new mode is allowed */ | |
2c608234 | 673 | err = regulator_mode_constrain(rdev, &mode); |
414c70cb LG |
674 | if (err == 0) |
675 | rdev->desc->ops->set_mode(rdev, mode); | |
676 | } | |
677 | ||
678 | static int suspend_set_state(struct regulator_dev *rdev, | |
679 | struct regulator_state *rstate) | |
680 | { | |
681 | int ret = 0; | |
638f85c5 MB |
682 | bool can_set_state; |
683 | ||
684 | can_set_state = rdev->desc->ops->set_suspend_enable && | |
685 | rdev->desc->ops->set_suspend_disable; | |
686 | ||
687 | /* If we have no suspend mode configration don't set anything; | |
688 | * only warn if the driver actually makes the suspend mode | |
689 | * configurable. | |
690 | */ | |
691 | if (!rstate->enabled && !rstate->disabled) { | |
692 | if (can_set_state) | |
5da84fd9 | 693 | rdev_warn(rdev, "No configuration\n"); |
638f85c5 MB |
694 | return 0; |
695 | } | |
696 | ||
697 | if (rstate->enabled && rstate->disabled) { | |
5da84fd9 | 698 | rdev_err(rdev, "invalid configuration\n"); |
638f85c5 MB |
699 | return -EINVAL; |
700 | } | |
414c70cb | 701 | |
638f85c5 | 702 | if (!can_set_state) { |
5da84fd9 | 703 | rdev_err(rdev, "no way to set suspend state\n"); |
414c70cb | 704 | return -EINVAL; |
a5766f11 | 705 | } |
414c70cb LG |
706 | |
707 | if (rstate->enabled) | |
708 | ret = rdev->desc->ops->set_suspend_enable(rdev); | |
709 | else | |
710 | ret = rdev->desc->ops->set_suspend_disable(rdev); | |
711 | if (ret < 0) { | |
5da84fd9 | 712 | rdev_err(rdev, "failed to enabled/disable\n"); |
414c70cb LG |
713 | return ret; |
714 | } | |
715 | ||
716 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { | |
717 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); | |
718 | if (ret < 0) { | |
5da84fd9 | 719 | rdev_err(rdev, "failed to set voltage\n"); |
414c70cb LG |
720 | return ret; |
721 | } | |
722 | } | |
723 | ||
724 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { | |
725 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); | |
726 | if (ret < 0) { | |
5da84fd9 | 727 | rdev_err(rdev, "failed to set mode\n"); |
414c70cb LG |
728 | return ret; |
729 | } | |
730 | } | |
731 | return ret; | |
732 | } | |
733 | ||
734 | /* locks held by caller */ | |
735 | static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) | |
736 | { | |
737 | if (!rdev->constraints) | |
738 | return -EINVAL; | |
739 | ||
740 | switch (state) { | |
741 | case PM_SUSPEND_STANDBY: | |
742 | return suspend_set_state(rdev, | |
743 | &rdev->constraints->state_standby); | |
744 | case PM_SUSPEND_MEM: | |
745 | return suspend_set_state(rdev, | |
746 | &rdev->constraints->state_mem); | |
747 | case PM_SUSPEND_MAX: | |
748 | return suspend_set_state(rdev, | |
749 | &rdev->constraints->state_disk); | |
750 | default: | |
751 | return -EINVAL; | |
752 | } | |
753 | } | |
754 | ||
755 | static void print_constraints(struct regulator_dev *rdev) | |
756 | { | |
757 | struct regulation_constraints *constraints = rdev->constraints; | |
973e9a27 | 758 | char buf[80] = ""; |
8f031b48 MB |
759 | int count = 0; |
760 | int ret; | |
414c70cb | 761 | |
8f031b48 | 762 | if (constraints->min_uV && constraints->max_uV) { |
414c70cb | 763 | if (constraints->min_uV == constraints->max_uV) |
8f031b48 MB |
764 | count += sprintf(buf + count, "%d mV ", |
765 | constraints->min_uV / 1000); | |
414c70cb | 766 | else |
8f031b48 MB |
767 | count += sprintf(buf + count, "%d <--> %d mV ", |
768 | constraints->min_uV / 1000, | |
769 | constraints->max_uV / 1000); | |
770 | } | |
771 | ||
772 | if (!constraints->min_uV || | |
773 | constraints->min_uV != constraints->max_uV) { | |
774 | ret = _regulator_get_voltage(rdev); | |
775 | if (ret > 0) | |
776 | count += sprintf(buf + count, "at %d mV ", ret / 1000); | |
777 | } | |
778 | ||
bf5892a8 MB |
779 | if (constraints->uV_offset) |
780 | count += sprintf(buf, "%dmV offset ", | |
781 | constraints->uV_offset / 1000); | |
782 | ||
8f031b48 | 783 | if (constraints->min_uA && constraints->max_uA) { |
414c70cb | 784 | if (constraints->min_uA == constraints->max_uA) |
8f031b48 MB |
785 | count += sprintf(buf + count, "%d mA ", |
786 | constraints->min_uA / 1000); | |
414c70cb | 787 | else |
8f031b48 MB |
788 | count += sprintf(buf + count, "%d <--> %d mA ", |
789 | constraints->min_uA / 1000, | |
790 | constraints->max_uA / 1000); | |
791 | } | |
792 | ||
793 | if (!constraints->min_uA || | |
794 | constraints->min_uA != constraints->max_uA) { | |
795 | ret = _regulator_get_current_limit(rdev); | |
796 | if (ret > 0) | |
e4a6376b | 797 | count += sprintf(buf + count, "at %d mA ", ret / 1000); |
414c70cb | 798 | } |
8f031b48 | 799 | |
414c70cb LG |
800 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
801 | count += sprintf(buf + count, "fast "); | |
802 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) | |
803 | count += sprintf(buf + count, "normal "); | |
804 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) | |
805 | count += sprintf(buf + count, "idle "); | |
806 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) | |
807 | count += sprintf(buf + count, "standby"); | |
808 | ||
13ce29f8 | 809 | rdev_info(rdev, "%s\n", buf); |
414c70cb LG |
810 | } |
811 | ||
e79055d6 | 812 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
1083c393 | 813 | struct regulation_constraints *constraints) |
a5766f11 | 814 | { |
e5fda26c | 815 | struct regulator_ops *ops = rdev->desc->ops; |
af5866c9 MB |
816 | int ret; |
817 | ||
818 | /* do we need to apply the constraint voltage */ | |
819 | if (rdev->constraints->apply_uV && | |
75790251 MB |
820 | rdev->constraints->min_uV == rdev->constraints->max_uV) { |
821 | ret = _regulator_do_set_voltage(rdev, | |
822 | rdev->constraints->min_uV, | |
823 | rdev->constraints->max_uV); | |
824 | if (ret < 0) { | |
825 | rdev_err(rdev, "failed to apply %duV constraint\n", | |
826 | rdev->constraints->min_uV); | |
75790251 MB |
827 | return ret; |
828 | } | |
af5866c9 | 829 | } |
e06f5b4f | 830 | |
4367cfdc DB |
831 | /* constrain machine-level voltage specs to fit |
832 | * the actual range supported by this regulator. | |
833 | */ | |
834 | if (ops->list_voltage && rdev->desc->n_voltages) { | |
835 | int count = rdev->desc->n_voltages; | |
836 | int i; | |
837 | int min_uV = INT_MAX; | |
838 | int max_uV = INT_MIN; | |
839 | int cmin = constraints->min_uV; | |
840 | int cmax = constraints->max_uV; | |
841 | ||
3e590918 MB |
842 | /* it's safe to autoconfigure fixed-voltage supplies |
843 | and the constraints are used by list_voltage. */ | |
4367cfdc | 844 | if (count == 1 && !cmin) { |
3e590918 | 845 | cmin = 1; |
4367cfdc | 846 | cmax = INT_MAX; |
3e590918 MB |
847 | constraints->min_uV = cmin; |
848 | constraints->max_uV = cmax; | |
4367cfdc DB |
849 | } |
850 | ||
3e2b9abd MB |
851 | /* voltage constraints are optional */ |
852 | if ((cmin == 0) && (cmax == 0)) | |
e79055d6 | 853 | return 0; |
3e2b9abd | 854 | |
4367cfdc | 855 | /* else require explicit machine-level constraints */ |
3e2b9abd | 856 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
5da84fd9 | 857 | rdev_err(rdev, "invalid voltage constraints\n"); |
e79055d6 | 858 | return -EINVAL; |
4367cfdc DB |
859 | } |
860 | ||
861 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ | |
862 | for (i = 0; i < count; i++) { | |
863 | int value; | |
864 | ||
865 | value = ops->list_voltage(rdev, i); | |
866 | if (value <= 0) | |
867 | continue; | |
868 | ||
869 | /* maybe adjust [min_uV..max_uV] */ | |
870 | if (value >= cmin && value < min_uV) | |
871 | min_uV = value; | |
872 | if (value <= cmax && value > max_uV) | |
873 | max_uV = value; | |
874 | } | |
875 | ||
876 | /* final: [min_uV..max_uV] valid iff constraints valid */ | |
877 | if (max_uV < min_uV) { | |
5da84fd9 | 878 | rdev_err(rdev, "unsupportable voltage constraints\n"); |
e79055d6 | 879 | return -EINVAL; |
4367cfdc DB |
880 | } |
881 | ||
882 | /* use regulator's subset of machine constraints */ | |
883 | if (constraints->min_uV < min_uV) { | |
5da84fd9 JP |
884 | rdev_dbg(rdev, "override min_uV, %d -> %d\n", |
885 | constraints->min_uV, min_uV); | |
4367cfdc DB |
886 | constraints->min_uV = min_uV; |
887 | } | |
888 | if (constraints->max_uV > max_uV) { | |
5da84fd9 JP |
889 | rdev_dbg(rdev, "override max_uV, %d -> %d\n", |
890 | constraints->max_uV, max_uV); | |
4367cfdc DB |
891 | constraints->max_uV = max_uV; |
892 | } | |
893 | } | |
894 | ||
e79055d6 MB |
895 | return 0; |
896 | } | |
897 | ||
898 | /** | |
899 | * set_machine_constraints - sets regulator constraints | |
900 | * @rdev: regulator source | |
901 | * @constraints: constraints to apply | |
902 | * | |
903 | * Allows platform initialisation code to define and constrain | |
904 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: | |
905 | * Constraints *must* be set by platform code in order for some | |
906 | * regulator operations to proceed i.e. set_voltage, set_current_limit, | |
907 | * set_mode. | |
908 | */ | |
909 | static int set_machine_constraints(struct regulator_dev *rdev, | |
f8c12fe3 | 910 | const struct regulation_constraints *constraints) |
e79055d6 MB |
911 | { |
912 | int ret = 0; | |
e79055d6 MB |
913 | struct regulator_ops *ops = rdev->desc->ops; |
914 | ||
9a8f5e07 MB |
915 | if (constraints) |
916 | rdev->constraints = kmemdup(constraints, sizeof(*constraints), | |
917 | GFP_KERNEL); | |
918 | else | |
919 | rdev->constraints = kzalloc(sizeof(*constraints), | |
920 | GFP_KERNEL); | |
f8c12fe3 MB |
921 | if (!rdev->constraints) |
922 | return -ENOMEM; | |
af5866c9 | 923 | |
f8c12fe3 | 924 | ret = machine_constraints_voltage(rdev, rdev->constraints); |
e79055d6 MB |
925 | if (ret != 0) |
926 | goto out; | |
927 | ||
a5766f11 | 928 | /* do we need to setup our suspend state */ |
9a8f5e07 | 929 | if (rdev->constraints->initial_state) { |
f8c12fe3 | 930 | ret = suspend_prepare(rdev, rdev->constraints->initial_state); |
e06f5b4f | 931 | if (ret < 0) { |
5da84fd9 | 932 | rdev_err(rdev, "failed to set suspend state\n"); |
e06f5b4f MB |
933 | goto out; |
934 | } | |
935 | } | |
a5766f11 | 936 | |
9a8f5e07 | 937 | if (rdev->constraints->initial_mode) { |
a308466c | 938 | if (!ops->set_mode) { |
5da84fd9 | 939 | rdev_err(rdev, "no set_mode operation\n"); |
a308466c MB |
940 | ret = -EINVAL; |
941 | goto out; | |
942 | } | |
943 | ||
f8c12fe3 | 944 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
a308466c | 945 | if (ret < 0) { |
5da84fd9 | 946 | rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
a308466c MB |
947 | goto out; |
948 | } | |
949 | } | |
950 | ||
cacf90f2 MB |
951 | /* If the constraints say the regulator should be on at this point |
952 | * and we have control then make sure it is enabled. | |
953 | */ | |
f8c12fe3 MB |
954 | if ((rdev->constraints->always_on || rdev->constraints->boot_on) && |
955 | ops->enable) { | |
e5fda26c MB |
956 | ret = ops->enable(rdev); |
957 | if (ret < 0) { | |
5da84fd9 | 958 | rdev_err(rdev, "failed to enable\n"); |
e5fda26c MB |
959 | goto out; |
960 | } | |
961 | } | |
962 | ||
a5766f11 | 963 | print_constraints(rdev); |
1a6958e7 | 964 | return 0; |
a5766f11 | 965 | out: |
1a6958e7 AL |
966 | kfree(rdev->constraints); |
967 | rdev->constraints = NULL; | |
a5766f11 LG |
968 | return ret; |
969 | } | |
970 | ||
971 | /** | |
972 | * set_supply - set regulator supply regulator | |
69279fb9 MB |
973 | * @rdev: regulator name |
974 | * @supply_rdev: supply regulator name | |
a5766f11 LG |
975 | * |
976 | * Called by platform initialisation code to set the supply regulator for this | |
977 | * regulator. This ensures that a regulators supply will also be enabled by the | |
978 | * core if it's child is enabled. | |
979 | */ | |
980 | static int set_supply(struct regulator_dev *rdev, | |
3801b86a | 981 | struct regulator_dev *supply_rdev) |
a5766f11 LG |
982 | { |
983 | int err; | |
984 | ||
3801b86a MB |
985 | rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); |
986 | ||
987 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); | |
32c78de8 AL |
988 | if (rdev->supply == NULL) { |
989 | err = -ENOMEM; | |
3801b86a | 990 | return err; |
a5766f11 | 991 | } |
3801b86a MB |
992 | |
993 | return 0; | |
a5766f11 LG |
994 | } |
995 | ||
996 | /** | |
06c63f93 | 997 | * set_consumer_device_supply - Bind a regulator to a symbolic supply |
69279fb9 MB |
998 | * @rdev: regulator source |
999 | * @consumer_dev: device the supply applies to | |
40f9244f | 1000 | * @consumer_dev_name: dev_name() string for device supply applies to |
69279fb9 | 1001 | * @supply: symbolic name for supply |
a5766f11 LG |
1002 | * |
1003 | * Allows platform initialisation code to map physical regulator | |
1004 | * sources to symbolic names for supplies for use by devices. Devices | |
1005 | * should use these symbolic names to request regulators, avoiding the | |
1006 | * need to provide board-specific regulator names as platform data. | |
40f9244f MB |
1007 | * |
1008 | * Only one of consumer_dev and consumer_dev_name may be specified. | |
a5766f11 LG |
1009 | */ |
1010 | static int set_consumer_device_supply(struct regulator_dev *rdev, | |
40f9244f MB |
1011 | struct device *consumer_dev, const char *consumer_dev_name, |
1012 | const char *supply) | |
a5766f11 LG |
1013 | { |
1014 | struct regulator_map *node; | |
9ed2099e | 1015 | int has_dev; |
a5766f11 | 1016 | |
40f9244f MB |
1017 | if (consumer_dev && consumer_dev_name) |
1018 | return -EINVAL; | |
1019 | ||
1020 | if (!consumer_dev_name && consumer_dev) | |
1021 | consumer_dev_name = dev_name(consumer_dev); | |
1022 | ||
a5766f11 LG |
1023 | if (supply == NULL) |
1024 | return -EINVAL; | |
1025 | ||
9ed2099e MB |
1026 | if (consumer_dev_name != NULL) |
1027 | has_dev = 1; | |
1028 | else | |
1029 | has_dev = 0; | |
1030 | ||
6001e13c | 1031 | list_for_each_entry(node, ®ulator_map_list, list) { |
23b5cc2a JN |
1032 | if (node->dev_name && consumer_dev_name) { |
1033 | if (strcmp(node->dev_name, consumer_dev_name) != 0) | |
1034 | continue; | |
1035 | } else if (node->dev_name || consumer_dev_name) { | |
6001e13c | 1036 | continue; |
23b5cc2a JN |
1037 | } |
1038 | ||
6001e13c DB |
1039 | if (strcmp(node->supply, supply) != 0) |
1040 | continue; | |
1041 | ||
1042 | dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n", | |
5da84fd9 JP |
1043 | dev_name(&node->regulator->dev), |
1044 | node->regulator->desc->name, | |
1045 | supply, | |
1046 | dev_name(&rdev->dev), rdev_get_name(rdev)); | |
6001e13c DB |
1047 | return -EBUSY; |
1048 | } | |
1049 | ||
9ed2099e | 1050 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
a5766f11 LG |
1051 | if (node == NULL) |
1052 | return -ENOMEM; | |
1053 | ||
1054 | node->regulator = rdev; | |
a5766f11 LG |
1055 | node->supply = supply; |
1056 | ||
9ed2099e MB |
1057 | if (has_dev) { |
1058 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); | |
1059 | if (node->dev_name == NULL) { | |
1060 | kfree(node); | |
1061 | return -ENOMEM; | |
1062 | } | |
40f9244f MB |
1063 | } |
1064 | ||
a5766f11 LG |
1065 | list_add(&node->list, ®ulator_map_list); |
1066 | return 0; | |
1067 | } | |
1068 | ||
0f1d747b MR |
1069 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
1070 | { | |
1071 | struct regulator_map *node, *n; | |
1072 | ||
1073 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { | |
1074 | if (rdev == node->regulator) { | |
1075 | list_del(&node->list); | |
40f9244f | 1076 | kfree(node->dev_name); |
0f1d747b | 1077 | kfree(node); |
0f1d747b MR |
1078 | } |
1079 | } | |
1080 | } | |
1081 | ||
f5726ae3 | 1082 | #define REG_STR_SIZE 64 |
414c70cb LG |
1083 | |
1084 | static struct regulator *create_regulator(struct regulator_dev *rdev, | |
1085 | struct device *dev, | |
1086 | const char *supply_name) | |
1087 | { | |
1088 | struct regulator *regulator; | |
1089 | char buf[REG_STR_SIZE]; | |
1090 | int err, size; | |
1091 | ||
1092 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); | |
1093 | if (regulator == NULL) | |
1094 | return NULL; | |
1095 | ||
1096 | mutex_lock(&rdev->mutex); | |
1097 | regulator->rdev = rdev; | |
1098 | list_add(®ulator->list, &rdev->consumer_list); | |
1099 | ||
1100 | if (dev) { | |
1101 | /* create a 'requested_microamps_name' sysfs entry */ | |
e0eaedef MB |
1102 | size = scnprintf(buf, REG_STR_SIZE, |
1103 | "microamps_requested_%s-%s", | |
1104 | dev_name(dev), supply_name); | |
414c70cb LG |
1105 | if (size >= REG_STR_SIZE) |
1106 | goto overflow_err; | |
1107 | ||
1108 | regulator->dev = dev; | |
4f26a2ab | 1109 | sysfs_attr_init(®ulator->dev_attr.attr); |
414c70cb LG |
1110 | regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL); |
1111 | if (regulator->dev_attr.attr.name == NULL) | |
1112 | goto attr_name_err; | |
1113 | ||
414c70cb LG |
1114 | regulator->dev_attr.attr.mode = 0444; |
1115 | regulator->dev_attr.show = device_requested_uA_show; | |
1116 | err = device_create_file(dev, ®ulator->dev_attr); | |
1117 | if (err < 0) { | |
5da84fd9 | 1118 | rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n"); |
414c70cb LG |
1119 | goto attr_name_err; |
1120 | } | |
1121 | ||
1122 | /* also add a link to the device sysfs entry */ | |
1123 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", | |
1124 | dev->kobj.name, supply_name); | |
1125 | if (size >= REG_STR_SIZE) | |
1126 | goto attr_err; | |
1127 | ||
1128 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); | |
1129 | if (regulator->supply_name == NULL) | |
1130 | goto attr_err; | |
1131 | ||
1132 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, | |
1133 | buf); | |
1134 | if (err) { | |
5da84fd9 JP |
1135 | rdev_warn(rdev, "could not add device link %s err %d\n", |
1136 | dev->kobj.name, err); | |
414c70cb LG |
1137 | goto link_name_err; |
1138 | } | |
5de70519 MB |
1139 | } else { |
1140 | regulator->supply_name = kstrdup(supply_name, GFP_KERNEL); | |
1141 | if (regulator->supply_name == NULL) | |
1142 | goto attr_err; | |
1143 | } | |
1144 | ||
1145 | #ifdef CONFIG_DEBUG_FS | |
1146 | regulator->debugfs = debugfs_create_dir(regulator->supply_name, | |
1147 | rdev->debugfs); | |
1148 | if (IS_ERR_OR_NULL(regulator->debugfs)) { | |
1149 | rdev_warn(rdev, "Failed to create debugfs directory\n"); | |
1150 | regulator->debugfs = NULL; | |
1151 | } else { | |
1152 | debugfs_create_u32("uA_load", 0444, regulator->debugfs, | |
1153 | ®ulator->uA_load); | |
1154 | debugfs_create_u32("min_uV", 0444, regulator->debugfs, | |
1155 | ®ulator->min_uV); | |
1156 | debugfs_create_u32("max_uV", 0444, regulator->debugfs, | |
1157 | ®ulator->max_uV); | |
414c70cb | 1158 | } |
5de70519 MB |
1159 | #endif |
1160 | ||
414c70cb LG |
1161 | mutex_unlock(&rdev->mutex); |
1162 | return regulator; | |
1163 | link_name_err: | |
1164 | kfree(regulator->supply_name); | |
1165 | attr_err: | |
1166 | device_remove_file(regulator->dev, ®ulator->dev_attr); | |
1167 | attr_name_err: | |
1168 | kfree(regulator->dev_attr.attr.name); | |
1169 | overflow_err: | |
1170 | list_del(®ulator->list); | |
1171 | kfree(regulator); | |
1172 | mutex_unlock(&rdev->mutex); | |
1173 | return NULL; | |
1174 | } | |
1175 | ||
31aae2be MB |
1176 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
1177 | { | |
1178 | if (!rdev->desc->ops->enable_time) | |
1179 | return 0; | |
1180 | return rdev->desc->ops->enable_time(rdev); | |
1181 | } | |
1182 | ||
69511a45 RN |
1183 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, |
1184 | const char *supply) | |
1185 | { | |
1186 | struct regulator_dev *r; | |
1187 | struct device_node *node; | |
1188 | ||
1189 | /* first do a dt based lookup */ | |
1190 | if (dev && dev->of_node) { | |
1191 | node = of_get_regulator(dev, supply); | |
1192 | if (node) | |
1193 | list_for_each_entry(r, ®ulator_list, list) | |
1194 | if (r->dev.parent && | |
1195 | node == r->dev.of_node) | |
1196 | return r; | |
1197 | } | |
1198 | ||
1199 | /* if not found, try doing it non-dt way */ | |
1200 | list_for_each_entry(r, ®ulator_list, list) | |
1201 | if (strcmp(rdev_get_name(r), supply) == 0) | |
1202 | return r; | |
1203 | ||
1204 | return NULL; | |
1205 | } | |
1206 | ||
5ffbd136 MB |
1207 | /* Internal regulator request function */ |
1208 | static struct regulator *_regulator_get(struct device *dev, const char *id, | |
1209 | int exclusive) | |
414c70cb LG |
1210 | { |
1211 | struct regulator_dev *rdev; | |
1212 | struct regulator_map *map; | |
1213 | struct regulator *regulator = ERR_PTR(-ENODEV); | |
40f9244f | 1214 | const char *devname = NULL; |
5ffbd136 | 1215 | int ret; |
414c70cb LG |
1216 | |
1217 | if (id == NULL) { | |
5da84fd9 | 1218 | pr_err("get() with no identifier\n"); |
414c70cb LG |
1219 | return regulator; |
1220 | } | |
1221 | ||
40f9244f MB |
1222 | if (dev) |
1223 | devname = dev_name(dev); | |
1224 | ||
414c70cb LG |
1225 | mutex_lock(®ulator_list_mutex); |
1226 | ||
69511a45 RN |
1227 | rdev = regulator_dev_lookup(dev, id); |
1228 | if (rdev) | |
1229 | goto found; | |
1230 | ||
414c70cb | 1231 | list_for_each_entry(map, ®ulator_map_list, list) { |
40f9244f MB |
1232 | /* If the mapping has a device set up it must match */ |
1233 | if (map->dev_name && | |
1234 | (!devname || strcmp(map->dev_name, devname))) | |
1235 | continue; | |
1236 | ||
1237 | if (strcmp(map->supply, id) == 0) { | |
a5766f11 | 1238 | rdev = map->regulator; |
414c70cb | 1239 | goto found; |
a5766f11 | 1240 | } |
414c70cb | 1241 | } |
34abbd68 | 1242 | |
688fe99a MB |
1243 | if (board_wants_dummy_regulator) { |
1244 | rdev = dummy_regulator_rdev; | |
1245 | goto found; | |
1246 | } | |
1247 | ||
34abbd68 MB |
1248 | #ifdef CONFIG_REGULATOR_DUMMY |
1249 | if (!devname) | |
1250 | devname = "deviceless"; | |
1251 | ||
1252 | /* If the board didn't flag that it was fully constrained then | |
1253 | * substitute in a dummy regulator so consumers can continue. | |
1254 | */ | |
1255 | if (!has_full_constraints) { | |
5da84fd9 JP |
1256 | pr_warn("%s supply %s not found, using dummy regulator\n", |
1257 | devname, id); | |
34abbd68 MB |
1258 | rdev = dummy_regulator_rdev; |
1259 | goto found; | |
1260 | } | |
1261 | #endif | |
1262 | ||
414c70cb LG |
1263 | mutex_unlock(®ulator_list_mutex); |
1264 | return regulator; | |
1265 | ||
1266 | found: | |
5ffbd136 MB |
1267 | if (rdev->exclusive) { |
1268 | regulator = ERR_PTR(-EPERM); | |
1269 | goto out; | |
1270 | } | |
1271 | ||
1272 | if (exclusive && rdev->open_count) { | |
1273 | regulator = ERR_PTR(-EBUSY); | |
1274 | goto out; | |
1275 | } | |
1276 | ||
a5766f11 LG |
1277 | if (!try_module_get(rdev->owner)) |
1278 | goto out; | |
1279 | ||
414c70cb LG |
1280 | regulator = create_regulator(rdev, dev, id); |
1281 | if (regulator == NULL) { | |
1282 | regulator = ERR_PTR(-ENOMEM); | |
1283 | module_put(rdev->owner); | |
bcda4321 | 1284 | goto out; |
414c70cb LG |
1285 | } |
1286 | ||
5ffbd136 MB |
1287 | rdev->open_count++; |
1288 | if (exclusive) { | |
1289 | rdev->exclusive = 1; | |
1290 | ||
1291 | ret = _regulator_is_enabled(rdev); | |
1292 | if (ret > 0) | |
1293 | rdev->use_count = 1; | |
1294 | else | |
1295 | rdev->use_count = 0; | |
1296 | } | |
1297 | ||
a5766f11 | 1298 | out: |
414c70cb | 1299 | mutex_unlock(®ulator_list_mutex); |
5ffbd136 | 1300 | |
414c70cb LG |
1301 | return regulator; |
1302 | } | |
5ffbd136 MB |
1303 | |
1304 | /** | |
1305 | * regulator_get - lookup and obtain a reference to a regulator. | |
1306 | * @dev: device for regulator "consumer" | |
1307 | * @id: Supply name or regulator ID. | |
1308 | * | |
1309 | * Returns a struct regulator corresponding to the regulator producer, | |
1310 | * or IS_ERR() condition containing errno. | |
1311 | * | |
1312 | * Use of supply names configured via regulator_set_device_supply() is | |
1313 | * strongly encouraged. It is recommended that the supply name used | |
1314 | * should match the name used for the supply and/or the relevant | |
1315 | * device pins in the datasheet. | |
1316 | */ | |
1317 | struct regulator *regulator_get(struct device *dev, const char *id) | |
1318 | { | |
1319 | return _regulator_get(dev, id, 0); | |
1320 | } | |
414c70cb LG |
1321 | EXPORT_SYMBOL_GPL(regulator_get); |
1322 | ||
5ffbd136 MB |
1323 | /** |
1324 | * regulator_get_exclusive - obtain exclusive access to a regulator. | |
1325 | * @dev: device for regulator "consumer" | |
1326 | * @id: Supply name or regulator ID. | |
1327 | * | |
1328 | * Returns a struct regulator corresponding to the regulator producer, | |
1329 | * or IS_ERR() condition containing errno. Other consumers will be | |
1330 | * unable to obtain this reference is held and the use count for the | |
1331 | * regulator will be initialised to reflect the current state of the | |
1332 | * regulator. | |
1333 | * | |
1334 | * This is intended for use by consumers which cannot tolerate shared | |
1335 | * use of the regulator such as those which need to force the | |
1336 | * regulator off for correct operation of the hardware they are | |
1337 | * controlling. | |
1338 | * | |
1339 | * Use of supply names configured via regulator_set_device_supply() is | |
1340 | * strongly encouraged. It is recommended that the supply name used | |
1341 | * should match the name used for the supply and/or the relevant | |
1342 | * device pins in the datasheet. | |
1343 | */ | |
1344 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) | |
1345 | { | |
1346 | return _regulator_get(dev, id, 1); | |
1347 | } | |
1348 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); | |
1349 | ||
414c70cb LG |
1350 | /** |
1351 | * regulator_put - "free" the regulator source | |
1352 | * @regulator: regulator source | |
1353 | * | |
1354 | * Note: drivers must ensure that all regulator_enable calls made on this | |
1355 | * regulator source are balanced by regulator_disable calls prior to calling | |
1356 | * this function. | |
1357 | */ | |
1358 | void regulator_put(struct regulator *regulator) | |
1359 | { | |
1360 | struct regulator_dev *rdev; | |
1361 | ||
1362 | if (regulator == NULL || IS_ERR(regulator)) | |
1363 | return; | |
1364 | ||
414c70cb LG |
1365 | mutex_lock(®ulator_list_mutex); |
1366 | rdev = regulator->rdev; | |
1367 | ||
5de70519 MB |
1368 | #ifdef CONFIG_DEBUG_FS |
1369 | debugfs_remove_recursive(regulator->debugfs); | |
1370 | #endif | |
1371 | ||
414c70cb LG |
1372 | /* remove any sysfs entries */ |
1373 | if (regulator->dev) { | |
1374 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); | |
414c70cb LG |
1375 | device_remove_file(regulator->dev, ®ulator->dev_attr); |
1376 | kfree(regulator->dev_attr.attr.name); | |
1377 | } | |
5de70519 | 1378 | kfree(regulator->supply_name); |
414c70cb LG |
1379 | list_del(®ulator->list); |
1380 | kfree(regulator); | |
1381 | ||
5ffbd136 MB |
1382 | rdev->open_count--; |
1383 | rdev->exclusive = 0; | |
1384 | ||
414c70cb LG |
1385 | module_put(rdev->owner); |
1386 | mutex_unlock(®ulator_list_mutex); | |
1387 | } | |
1388 | EXPORT_SYMBOL_GPL(regulator_put); | |
1389 | ||
9a2372fa MB |
1390 | static int _regulator_can_change_status(struct regulator_dev *rdev) |
1391 | { | |
1392 | if (!rdev->constraints) | |
1393 | return 0; | |
1394 | ||
1395 | if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) | |
1396 | return 1; | |
1397 | else | |
1398 | return 0; | |
1399 | } | |
1400 | ||
414c70cb LG |
1401 | /* locks held by regulator_enable() */ |
1402 | static int _regulator_enable(struct regulator_dev *rdev) | |
1403 | { | |
31aae2be | 1404 | int ret, delay; |
414c70cb | 1405 | |
414c70cb | 1406 | /* check voltage and requested load before enabling */ |
9a2372fa MB |
1407 | if (rdev->constraints && |
1408 | (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) | |
1409 | drms_uA_update(rdev); | |
414c70cb | 1410 | |
9a2372fa MB |
1411 | if (rdev->use_count == 0) { |
1412 | /* The regulator may on if it's not switchable or left on */ | |
1413 | ret = _regulator_is_enabled(rdev); | |
1414 | if (ret == -EINVAL || ret == 0) { | |
1415 | if (!_regulator_can_change_status(rdev)) | |
1416 | return -EPERM; | |
1417 | ||
31aae2be | 1418 | if (!rdev->desc->ops->enable) |
9a2372fa | 1419 | return -EINVAL; |
31aae2be MB |
1420 | |
1421 | /* Query before enabling in case configuration | |
25985edc | 1422 | * dependent. */ |
31aae2be MB |
1423 | ret = _regulator_get_enable_time(rdev); |
1424 | if (ret >= 0) { | |
1425 | delay = ret; | |
1426 | } else { | |
5da84fd9 | 1427 | rdev_warn(rdev, "enable_time() failed: %d\n", |
1d7372e1 | 1428 | ret); |
31aae2be | 1429 | delay = 0; |
9a2372fa | 1430 | } |
31aae2be | 1431 | |
02fa3ec0 MB |
1432 | trace_regulator_enable(rdev_get_name(rdev)); |
1433 | ||
31aae2be MB |
1434 | /* Allow the regulator to ramp; it would be useful |
1435 | * to extend this for bulk operations so that the | |
1436 | * regulators can ramp together. */ | |
1437 | ret = rdev->desc->ops->enable(rdev); | |
1438 | if (ret < 0) | |
1439 | return ret; | |
1440 | ||
02fa3ec0 MB |
1441 | trace_regulator_enable_delay(rdev_get_name(rdev)); |
1442 | ||
e36c1df8 | 1443 | if (delay >= 1000) { |
31aae2be | 1444 | mdelay(delay / 1000); |
e36c1df8 AL |
1445 | udelay(delay % 1000); |
1446 | } else if (delay) { | |
31aae2be | 1447 | udelay(delay); |
e36c1df8 | 1448 | } |
31aae2be | 1449 | |
02fa3ec0 MB |
1450 | trace_regulator_enable_complete(rdev_get_name(rdev)); |
1451 | ||
a7433cff | 1452 | } else if (ret < 0) { |
5da84fd9 | 1453 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
414c70cb LG |
1454 | return ret; |
1455 | } | |
a7433cff | 1456 | /* Fallthrough on positive return values - already enabled */ |
414c70cb LG |
1457 | } |
1458 | ||
9a2372fa MB |
1459 | rdev->use_count++; |
1460 | ||
1461 | return 0; | |
414c70cb LG |
1462 | } |
1463 | ||
1464 | /** | |
1465 | * regulator_enable - enable regulator output | |
1466 | * @regulator: regulator source | |
1467 | * | |
cf7bbcdf MB |
1468 | * Request that the regulator be enabled with the regulator output at |
1469 | * the predefined voltage or current value. Calls to regulator_enable() | |
1470 | * must be balanced with calls to regulator_disable(). | |
1471 | * | |
414c70cb | 1472 | * NOTE: the output value can be set by other drivers, boot loader or may be |
cf7bbcdf | 1473 | * hardwired in the regulator. |
414c70cb LG |
1474 | */ |
1475 | int regulator_enable(struct regulator *regulator) | |
1476 | { | |
412aec61 DB |
1477 | struct regulator_dev *rdev = regulator->rdev; |
1478 | int ret = 0; | |
414c70cb | 1479 | |
3801b86a MB |
1480 | if (rdev->supply) { |
1481 | ret = regulator_enable(rdev->supply); | |
1482 | if (ret != 0) | |
1483 | return ret; | |
1484 | } | |
1485 | ||
412aec61 | 1486 | mutex_lock(&rdev->mutex); |
cd94b505 | 1487 | ret = _regulator_enable(rdev); |
412aec61 | 1488 | mutex_unlock(&rdev->mutex); |
3801b86a | 1489 | |
d1685e4e | 1490 | if (ret != 0 && rdev->supply) |
3801b86a MB |
1491 | regulator_disable(rdev->supply); |
1492 | ||
414c70cb LG |
1493 | return ret; |
1494 | } | |
1495 | EXPORT_SYMBOL_GPL(regulator_enable); | |
1496 | ||
1497 | /* locks held by regulator_disable() */ | |
3801b86a | 1498 | static int _regulator_disable(struct regulator_dev *rdev) |
414c70cb LG |
1499 | { |
1500 | int ret = 0; | |
1501 | ||
cd94b505 | 1502 | if (WARN(rdev->use_count <= 0, |
43e7ee33 | 1503 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
cd94b505 DB |
1504 | return -EIO; |
1505 | ||
414c70cb | 1506 | /* are we the last user and permitted to disable ? */ |
60ef66fc MB |
1507 | if (rdev->use_count == 1 && |
1508 | (rdev->constraints && !rdev->constraints->always_on)) { | |
414c70cb LG |
1509 | |
1510 | /* we are last user */ | |
9a2372fa MB |
1511 | if (_regulator_can_change_status(rdev) && |
1512 | rdev->desc->ops->disable) { | |
02fa3ec0 MB |
1513 | trace_regulator_disable(rdev_get_name(rdev)); |
1514 | ||
414c70cb LG |
1515 | ret = rdev->desc->ops->disable(rdev); |
1516 | if (ret < 0) { | |
5da84fd9 | 1517 | rdev_err(rdev, "failed to disable\n"); |
414c70cb LG |
1518 | return ret; |
1519 | } | |
84b68263 | 1520 | |
02fa3ec0 MB |
1521 | trace_regulator_disable_complete(rdev_get_name(rdev)); |
1522 | ||
84b68263 MB |
1523 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
1524 | NULL); | |
414c70cb LG |
1525 | } |
1526 | ||
414c70cb LG |
1527 | rdev->use_count = 0; |
1528 | } else if (rdev->use_count > 1) { | |
1529 | ||
1530 | if (rdev->constraints && | |
1531 | (rdev->constraints->valid_ops_mask & | |
1532 | REGULATOR_CHANGE_DRMS)) | |
1533 | drms_uA_update(rdev); | |
1534 | ||
1535 | rdev->use_count--; | |
1536 | } | |
3801b86a | 1537 | |
414c70cb LG |
1538 | return ret; |
1539 | } | |
1540 | ||
1541 | /** | |
1542 | * regulator_disable - disable regulator output | |
1543 | * @regulator: regulator source | |
1544 | * | |
cf7bbcdf MB |
1545 | * Disable the regulator output voltage or current. Calls to |
1546 | * regulator_enable() must be balanced with calls to | |
1547 | * regulator_disable(). | |
69279fb9 | 1548 | * |
414c70cb | 1549 | * NOTE: this will only disable the regulator output if no other consumer |
cf7bbcdf MB |
1550 | * devices have it enabled, the regulator device supports disabling and |
1551 | * machine constraints permit this operation. | |
414c70cb LG |
1552 | */ |
1553 | int regulator_disable(struct regulator *regulator) | |
1554 | { | |
412aec61 DB |
1555 | struct regulator_dev *rdev = regulator->rdev; |
1556 | int ret = 0; | |
414c70cb | 1557 | |
412aec61 | 1558 | mutex_lock(&rdev->mutex); |
3801b86a | 1559 | ret = _regulator_disable(rdev); |
412aec61 | 1560 | mutex_unlock(&rdev->mutex); |
8cbf811d | 1561 | |
3801b86a MB |
1562 | if (ret == 0 && rdev->supply) |
1563 | regulator_disable(rdev->supply); | |
8cbf811d | 1564 | |
414c70cb LG |
1565 | return ret; |
1566 | } | |
1567 | EXPORT_SYMBOL_GPL(regulator_disable); | |
1568 | ||
1569 | /* locks held by regulator_force_disable() */ | |
3801b86a | 1570 | static int _regulator_force_disable(struct regulator_dev *rdev) |
414c70cb LG |
1571 | { |
1572 | int ret = 0; | |
1573 | ||
1574 | /* force disable */ | |
1575 | if (rdev->desc->ops->disable) { | |
1576 | /* ah well, who wants to live forever... */ | |
1577 | ret = rdev->desc->ops->disable(rdev); | |
1578 | if (ret < 0) { | |
5da84fd9 | 1579 | rdev_err(rdev, "failed to force disable\n"); |
414c70cb LG |
1580 | return ret; |
1581 | } | |
1582 | /* notify other consumers that power has been forced off */ | |
84b68263 MB |
1583 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
1584 | REGULATOR_EVENT_DISABLE, NULL); | |
414c70cb LG |
1585 | } |
1586 | ||
414c70cb LG |
1587 | return ret; |
1588 | } | |
1589 | ||
1590 | /** | |
1591 | * regulator_force_disable - force disable regulator output | |
1592 | * @regulator: regulator source | |
1593 | * | |
1594 | * Forcibly disable the regulator output voltage or current. | |
1595 | * NOTE: this *will* disable the regulator output even if other consumer | |
1596 | * devices have it enabled. This should be used for situations when device | |
1597 | * damage will likely occur if the regulator is not disabled (e.g. over temp). | |
1598 | */ | |
1599 | int regulator_force_disable(struct regulator *regulator) | |
1600 | { | |
82d15839 | 1601 | struct regulator_dev *rdev = regulator->rdev; |
414c70cb LG |
1602 | int ret; |
1603 | ||
82d15839 | 1604 | mutex_lock(&rdev->mutex); |
414c70cb | 1605 | regulator->uA_load = 0; |
3801b86a | 1606 | ret = _regulator_force_disable(regulator->rdev); |
82d15839 | 1607 | mutex_unlock(&rdev->mutex); |
8cbf811d | 1608 | |
3801b86a MB |
1609 | if (rdev->supply) |
1610 | while (rdev->open_count--) | |
1611 | regulator_disable(rdev->supply); | |
8cbf811d | 1612 | |
414c70cb LG |
1613 | return ret; |
1614 | } | |
1615 | EXPORT_SYMBOL_GPL(regulator_force_disable); | |
1616 | ||
da07ecd9 MB |
1617 | static void regulator_disable_work(struct work_struct *work) |
1618 | { | |
1619 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, | |
1620 | disable_work.work); | |
1621 | int count, i, ret; | |
1622 | ||
1623 | mutex_lock(&rdev->mutex); | |
1624 | ||
1625 | BUG_ON(!rdev->deferred_disables); | |
1626 | ||
1627 | count = rdev->deferred_disables; | |
1628 | rdev->deferred_disables = 0; | |
1629 | ||
1630 | for (i = 0; i < count; i++) { | |
1631 | ret = _regulator_disable(rdev); | |
1632 | if (ret != 0) | |
1633 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); | |
1634 | } | |
1635 | ||
1636 | mutex_unlock(&rdev->mutex); | |
1637 | ||
1638 | if (rdev->supply) { | |
1639 | for (i = 0; i < count; i++) { | |
1640 | ret = regulator_disable(rdev->supply); | |
1641 | if (ret != 0) { | |
1642 | rdev_err(rdev, | |
1643 | "Supply disable failed: %d\n", ret); | |
1644 | } | |
1645 | } | |
1646 | } | |
1647 | } | |
1648 | ||
1649 | /** | |
1650 | * regulator_disable_deferred - disable regulator output with delay | |
1651 | * @regulator: regulator source | |
1652 | * @ms: miliseconds until the regulator is disabled | |
1653 | * | |
1654 | * Execute regulator_disable() on the regulator after a delay. This | |
1655 | * is intended for use with devices that require some time to quiesce. | |
1656 | * | |
1657 | * NOTE: this will only disable the regulator output if no other consumer | |
1658 | * devices have it enabled, the regulator device supports disabling and | |
1659 | * machine constraints permit this operation. | |
1660 | */ | |
1661 | int regulator_disable_deferred(struct regulator *regulator, int ms) | |
1662 | { | |
1663 | struct regulator_dev *rdev = regulator->rdev; | |
aa59802d | 1664 | int ret; |
da07ecd9 MB |
1665 | |
1666 | mutex_lock(&rdev->mutex); | |
1667 | rdev->deferred_disables++; | |
1668 | mutex_unlock(&rdev->mutex); | |
1669 | ||
aa59802d MB |
1670 | ret = schedule_delayed_work(&rdev->disable_work, |
1671 | msecs_to_jiffies(ms)); | |
1672 | if (ret < 0) | |
1673 | return ret; | |
1674 | else | |
1675 | return 0; | |
da07ecd9 MB |
1676 | } |
1677 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); | |
1678 | ||
414c70cb LG |
1679 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
1680 | { | |
9a7f6a4c | 1681 | /* If we don't know then assume that the regulator is always on */ |
9332546f | 1682 | if (!rdev->desc->ops->is_enabled) |
9a7f6a4c | 1683 | return 1; |
414c70cb | 1684 | |
9332546f | 1685 | return rdev->desc->ops->is_enabled(rdev); |
414c70cb LG |
1686 | } |
1687 | ||
1688 | /** | |
1689 | * regulator_is_enabled - is the regulator output enabled | |
1690 | * @regulator: regulator source | |
1691 | * | |
412aec61 DB |
1692 | * Returns positive if the regulator driver backing the source/client |
1693 | * has requested that the device be enabled, zero if it hasn't, else a | |
1694 | * negative errno code. | |
1695 | * | |
1696 | * Note that the device backing this regulator handle can have multiple | |
1697 | * users, so it might be enabled even if regulator_enable() was never | |
1698 | * called for this particular source. | |
414c70cb LG |
1699 | */ |
1700 | int regulator_is_enabled(struct regulator *regulator) | |
1701 | { | |
9332546f MB |
1702 | int ret; |
1703 | ||
1704 | mutex_lock(®ulator->rdev->mutex); | |
1705 | ret = _regulator_is_enabled(regulator->rdev); | |
1706 | mutex_unlock(®ulator->rdev->mutex); | |
1707 | ||
1708 | return ret; | |
414c70cb LG |
1709 | } |
1710 | EXPORT_SYMBOL_GPL(regulator_is_enabled); | |
1711 | ||
4367cfdc DB |
1712 | /** |
1713 | * regulator_count_voltages - count regulator_list_voltage() selectors | |
1714 | * @regulator: regulator source | |
1715 | * | |
1716 | * Returns number of selectors, or negative errno. Selectors are | |
1717 | * numbered starting at zero, and typically correspond to bitfields | |
1718 | * in hardware registers. | |
1719 | */ | |
1720 | int regulator_count_voltages(struct regulator *regulator) | |
1721 | { | |
1722 | struct regulator_dev *rdev = regulator->rdev; | |
1723 | ||
1724 | return rdev->desc->n_voltages ? : -EINVAL; | |
1725 | } | |
1726 | EXPORT_SYMBOL_GPL(regulator_count_voltages); | |
1727 | ||
1728 | /** | |
1729 | * regulator_list_voltage - enumerate supported voltages | |
1730 | * @regulator: regulator source | |
1731 | * @selector: identify voltage to list | |
1732 | * Context: can sleep | |
1733 | * | |
1734 | * Returns a voltage that can be passed to @regulator_set_voltage(), | |
88393161 | 1735 | * zero if this selector code can't be used on this system, or a |
4367cfdc DB |
1736 | * negative errno. |
1737 | */ | |
1738 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) | |
1739 | { | |
1740 | struct regulator_dev *rdev = regulator->rdev; | |
1741 | struct regulator_ops *ops = rdev->desc->ops; | |
1742 | int ret; | |
1743 | ||
1744 | if (!ops->list_voltage || selector >= rdev->desc->n_voltages) | |
1745 | return -EINVAL; | |
1746 | ||
1747 | mutex_lock(&rdev->mutex); | |
1748 | ret = ops->list_voltage(rdev, selector); | |
1749 | mutex_unlock(&rdev->mutex); | |
1750 | ||
1751 | if (ret > 0) { | |
1752 | if (ret < rdev->constraints->min_uV) | |
1753 | ret = 0; | |
1754 | else if (ret > rdev->constraints->max_uV) | |
1755 | ret = 0; | |
1756 | } | |
1757 | ||
1758 | return ret; | |
1759 | } | |
1760 | EXPORT_SYMBOL_GPL(regulator_list_voltage); | |
1761 | ||
a7a1ad90 MB |
1762 | /** |
1763 | * regulator_is_supported_voltage - check if a voltage range can be supported | |
1764 | * | |
1765 | * @regulator: Regulator to check. | |
1766 | * @min_uV: Minimum required voltage in uV. | |
1767 | * @max_uV: Maximum required voltage in uV. | |
1768 | * | |
1769 | * Returns a boolean or a negative error code. | |
1770 | */ | |
1771 | int regulator_is_supported_voltage(struct regulator *regulator, | |
1772 | int min_uV, int max_uV) | |
1773 | { | |
1774 | int i, voltages, ret; | |
1775 | ||
1776 | ret = regulator_count_voltages(regulator); | |
1777 | if (ret < 0) | |
1778 | return ret; | |
1779 | voltages = ret; | |
1780 | ||
1781 | for (i = 0; i < voltages; i++) { | |
1782 | ret = regulator_list_voltage(regulator, i); | |
1783 | ||
1784 | if (ret >= min_uV && ret <= max_uV) | |
1785 | return 1; | |
1786 | } | |
1787 | ||
1788 | return 0; | |
1789 | } | |
a398eaa2 | 1790 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
a7a1ad90 | 1791 | |
75790251 MB |
1792 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
1793 | int min_uV, int max_uV) | |
1794 | { | |
1795 | int ret; | |
77af1b26 | 1796 | int delay = 0; |
75790251 MB |
1797 | unsigned int selector; |
1798 | ||
1799 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); | |
1800 | ||
bf5892a8 MB |
1801 | min_uV += rdev->constraints->uV_offset; |
1802 | max_uV += rdev->constraints->uV_offset; | |
1803 | ||
75790251 MB |
1804 | if (rdev->desc->ops->set_voltage) { |
1805 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, | |
1806 | &selector); | |
1807 | ||
1808 | if (rdev->desc->ops->list_voltage) | |
1809 | selector = rdev->desc->ops->list_voltage(rdev, | |
1810 | selector); | |
1811 | else | |
1812 | selector = -1; | |
e8eef82b MB |
1813 | } else if (rdev->desc->ops->set_voltage_sel) { |
1814 | int best_val = INT_MAX; | |
1815 | int i; | |
1816 | ||
1817 | selector = 0; | |
1818 | ||
1819 | /* Find the smallest voltage that falls within the specified | |
1820 | * range. | |
1821 | */ | |
1822 | for (i = 0; i < rdev->desc->n_voltages; i++) { | |
1823 | ret = rdev->desc->ops->list_voltage(rdev, i); | |
1824 | if (ret < 0) | |
1825 | continue; | |
1826 | ||
1827 | if (ret < best_val && ret >= min_uV && ret <= max_uV) { | |
1828 | best_val = ret; | |
1829 | selector = i; | |
1830 | } | |
1831 | } | |
1832 | ||
77af1b26 LW |
1833 | /* |
1834 | * If we can't obtain the old selector there is not enough | |
1835 | * info to call set_voltage_time_sel(). | |
1836 | */ | |
1837 | if (rdev->desc->ops->set_voltage_time_sel && | |
1838 | rdev->desc->ops->get_voltage_sel) { | |
1839 | unsigned int old_selector = 0; | |
1840 | ||
1841 | ret = rdev->desc->ops->get_voltage_sel(rdev); | |
1842 | if (ret < 0) | |
1843 | return ret; | |
1844 | old_selector = ret; | |
1845 | delay = rdev->desc->ops->set_voltage_time_sel(rdev, | |
1846 | old_selector, selector); | |
1847 | } | |
1848 | ||
e8eef82b MB |
1849 | if (best_val != INT_MAX) { |
1850 | ret = rdev->desc->ops->set_voltage_sel(rdev, selector); | |
1851 | selector = best_val; | |
1852 | } else { | |
1853 | ret = -EINVAL; | |
1854 | } | |
75790251 MB |
1855 | } else { |
1856 | ret = -EINVAL; | |
1857 | } | |
1858 | ||
77af1b26 LW |
1859 | /* Insert any necessary delays */ |
1860 | if (delay >= 1000) { | |
1861 | mdelay(delay / 1000); | |
1862 | udelay(delay % 1000); | |
1863 | } else if (delay) { | |
1864 | udelay(delay); | |
1865 | } | |
1866 | ||
ded06a52 MB |
1867 | if (ret == 0) |
1868 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, | |
1869 | NULL); | |
1870 | ||
75790251 MB |
1871 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector); |
1872 | ||
1873 | return ret; | |
1874 | } | |
1875 | ||
414c70cb LG |
1876 | /** |
1877 | * regulator_set_voltage - set regulator output voltage | |
1878 | * @regulator: regulator source | |
1879 | * @min_uV: Minimum required voltage in uV | |
1880 | * @max_uV: Maximum acceptable voltage in uV | |
1881 | * | |
1882 | * Sets a voltage regulator to the desired output voltage. This can be set | |
1883 | * during any regulator state. IOW, regulator can be disabled or enabled. | |
1884 | * | |
1885 | * If the regulator is enabled then the voltage will change to the new value | |
1886 | * immediately otherwise if the regulator is disabled the regulator will | |
1887 | * output at the new voltage when enabled. | |
1888 | * | |
1889 | * NOTE: If the regulator is shared between several devices then the lowest | |
1890 | * request voltage that meets the system constraints will be used. | |
69279fb9 | 1891 | * Regulator system constraints must be set for this regulator before |
414c70cb LG |
1892 | * calling this function otherwise this call will fail. |
1893 | */ | |
1894 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) | |
1895 | { | |
1896 | struct regulator_dev *rdev = regulator->rdev; | |
95a3c23a | 1897 | int ret = 0; |
414c70cb LG |
1898 | |
1899 | mutex_lock(&rdev->mutex); | |
1900 | ||
95a3c23a MB |
1901 | /* If we're setting the same range as last time the change |
1902 | * should be a noop (some cpufreq implementations use the same | |
1903 | * voltage for multiple frequencies, for example). | |
1904 | */ | |
1905 | if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) | |
1906 | goto out; | |
1907 | ||
414c70cb | 1908 | /* sanity check */ |
e8eef82b MB |
1909 | if (!rdev->desc->ops->set_voltage && |
1910 | !rdev->desc->ops->set_voltage_sel) { | |
414c70cb LG |
1911 | ret = -EINVAL; |
1912 | goto out; | |
1913 | } | |
1914 | ||
1915 | /* constraints check */ | |
1916 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
1917 | if (ret < 0) | |
1918 | goto out; | |
1919 | regulator->min_uV = min_uV; | |
1920 | regulator->max_uV = max_uV; | |
3a93f2a9 | 1921 | |
05fda3b1 TP |
1922 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
1923 | if (ret < 0) | |
1924 | goto out; | |
1925 | ||
75790251 | 1926 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
02fa3ec0 | 1927 | |
414c70cb LG |
1928 | out: |
1929 | mutex_unlock(&rdev->mutex); | |
1930 | return ret; | |
1931 | } | |
1932 | EXPORT_SYMBOL_GPL(regulator_set_voltage); | |
1933 | ||
88cd222b LW |
1934 | /** |
1935 | * regulator_set_voltage_time - get raise/fall time | |
1936 | * @regulator: regulator source | |
1937 | * @old_uV: starting voltage in microvolts | |
1938 | * @new_uV: target voltage in microvolts | |
1939 | * | |
1940 | * Provided with the starting and ending voltage, this function attempts to | |
1941 | * calculate the time in microseconds required to rise or fall to this new | |
1942 | * voltage. | |
1943 | */ | |
1944 | int regulator_set_voltage_time(struct regulator *regulator, | |
1945 | int old_uV, int new_uV) | |
1946 | { | |
1947 | struct regulator_dev *rdev = regulator->rdev; | |
1948 | struct regulator_ops *ops = rdev->desc->ops; | |
1949 | int old_sel = -1; | |
1950 | int new_sel = -1; | |
1951 | int voltage; | |
1952 | int i; | |
1953 | ||
1954 | /* Currently requires operations to do this */ | |
1955 | if (!ops->list_voltage || !ops->set_voltage_time_sel | |
1956 | || !rdev->desc->n_voltages) | |
1957 | return -EINVAL; | |
1958 | ||
1959 | for (i = 0; i < rdev->desc->n_voltages; i++) { | |
1960 | /* We only look for exact voltage matches here */ | |
1961 | voltage = regulator_list_voltage(regulator, i); | |
1962 | if (voltage < 0) | |
1963 | return -EINVAL; | |
1964 | if (voltage == 0) | |
1965 | continue; | |
1966 | if (voltage == old_uV) | |
1967 | old_sel = i; | |
1968 | if (voltage == new_uV) | |
1969 | new_sel = i; | |
1970 | } | |
1971 | ||
1972 | if (old_sel < 0 || new_sel < 0) | |
1973 | return -EINVAL; | |
1974 | ||
1975 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); | |
1976 | } | |
1977 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); | |
1978 | ||
606a2562 MB |
1979 | /** |
1980 | * regulator_sync_voltage - re-apply last regulator output voltage | |
1981 | * @regulator: regulator source | |
1982 | * | |
1983 | * Re-apply the last configured voltage. This is intended to be used | |
1984 | * where some external control source the consumer is cooperating with | |
1985 | * has caused the configured voltage to change. | |
1986 | */ | |
1987 | int regulator_sync_voltage(struct regulator *regulator) | |
1988 | { | |
1989 | struct regulator_dev *rdev = regulator->rdev; | |
1990 | int ret, min_uV, max_uV; | |
1991 | ||
1992 | mutex_lock(&rdev->mutex); | |
1993 | ||
1994 | if (!rdev->desc->ops->set_voltage && | |
1995 | !rdev->desc->ops->set_voltage_sel) { | |
1996 | ret = -EINVAL; | |
1997 | goto out; | |
1998 | } | |
1999 | ||
2000 | /* This is only going to work if we've had a voltage configured. */ | |
2001 | if (!regulator->min_uV && !regulator->max_uV) { | |
2002 | ret = -EINVAL; | |
2003 | goto out; | |
2004 | } | |
2005 | ||
2006 | min_uV = regulator->min_uV; | |
2007 | max_uV = regulator->max_uV; | |
2008 | ||
2009 | /* This should be a paranoia check... */ | |
2010 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); | |
2011 | if (ret < 0) | |
2012 | goto out; | |
2013 | ||
2014 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); | |
2015 | if (ret < 0) | |
2016 | goto out; | |
2017 | ||
2018 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); | |
2019 | ||
2020 | out: | |
2021 | mutex_unlock(&rdev->mutex); | |
2022 | return ret; | |
2023 | } | |
2024 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); | |
2025 | ||
414c70cb LG |
2026 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
2027 | { | |
bf5892a8 | 2028 | int sel, ret; |
476c2d83 MB |
2029 | |
2030 | if (rdev->desc->ops->get_voltage_sel) { | |
2031 | sel = rdev->desc->ops->get_voltage_sel(rdev); | |
2032 | if (sel < 0) | |
2033 | return sel; | |
bf5892a8 | 2034 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
cb220d16 | 2035 | } else if (rdev->desc->ops->get_voltage) { |
bf5892a8 | 2036 | ret = rdev->desc->ops->get_voltage(rdev); |
cb220d16 | 2037 | } else { |
414c70cb | 2038 | return -EINVAL; |
cb220d16 | 2039 | } |
bf5892a8 | 2040 | |
cb220d16 AL |
2041 | if (ret < 0) |
2042 | return ret; | |
bf5892a8 | 2043 | return ret - rdev->constraints->uV_offset; |
414c70cb LG |
2044 | } |
2045 | ||
2046 | /** | |
2047 | * regulator_get_voltage - get regulator output voltage | |
2048 | * @regulator: regulator source | |
2049 | * | |
2050 | * This returns the current regulator voltage in uV. | |
2051 | * | |
2052 | * NOTE: If the regulator is disabled it will return the voltage value. This | |
2053 | * function should not be used to determine regulator state. | |
2054 | */ | |
2055 | int regulator_get_voltage(struct regulator *regulator) | |
2056 | { | |
2057 | int ret; | |
2058 | ||
2059 | mutex_lock(®ulator->rdev->mutex); | |
2060 | ||
2061 | ret = _regulator_get_voltage(regulator->rdev); | |
2062 | ||
2063 | mutex_unlock(®ulator->rdev->mutex); | |
2064 | ||
2065 | return ret; | |
2066 | } | |
2067 | EXPORT_SYMBOL_GPL(regulator_get_voltage); | |
2068 | ||
2069 | /** | |
2070 | * regulator_set_current_limit - set regulator output current limit | |
2071 | * @regulator: regulator source | |
2072 | * @min_uA: Minimuum supported current in uA | |
2073 | * @max_uA: Maximum supported current in uA | |
2074 | * | |
2075 | * Sets current sink to the desired output current. This can be set during | |
2076 | * any regulator state. IOW, regulator can be disabled or enabled. | |
2077 | * | |
2078 | * If the regulator is enabled then the current will change to the new value | |
2079 | * immediately otherwise if the regulator is disabled the regulator will | |
2080 | * output at the new current when enabled. | |
2081 | * | |
2082 | * NOTE: Regulator system constraints must be set for this regulator before | |
2083 | * calling this function otherwise this call will fail. | |
2084 | */ | |
2085 | int regulator_set_current_limit(struct regulator *regulator, | |
2086 | int min_uA, int max_uA) | |
2087 | { | |
2088 | struct regulator_dev *rdev = regulator->rdev; | |
2089 | int ret; | |
2090 | ||
2091 | mutex_lock(&rdev->mutex); | |
2092 | ||
2093 | /* sanity check */ | |
2094 | if (!rdev->desc->ops->set_current_limit) { | |
2095 | ret = -EINVAL; | |
2096 | goto out; | |
2097 | } | |
2098 | ||
2099 | /* constraints check */ | |
2100 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); | |
2101 | if (ret < 0) | |
2102 | goto out; | |
2103 | ||
2104 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); | |
2105 | out: | |
2106 | mutex_unlock(&rdev->mutex); | |
2107 | return ret; | |
2108 | } | |
2109 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); | |
2110 | ||
2111 | static int _regulator_get_current_limit(struct regulator_dev *rdev) | |
2112 | { | |
2113 | int ret; | |
2114 | ||
2115 | mutex_lock(&rdev->mutex); | |
2116 | ||
2117 | /* sanity check */ | |
2118 | if (!rdev->desc->ops->get_current_limit) { | |
2119 | ret = -EINVAL; | |
2120 | goto out; | |
2121 | } | |
2122 | ||
2123 | ret = rdev->desc->ops->get_current_limit(rdev); | |
2124 | out: | |
2125 | mutex_unlock(&rdev->mutex); | |
2126 | return ret; | |
2127 | } | |
2128 | ||
2129 | /** | |
2130 | * regulator_get_current_limit - get regulator output current | |
2131 | * @regulator: regulator source | |
2132 | * | |
2133 | * This returns the current supplied by the specified current sink in uA. | |
2134 | * | |
2135 | * NOTE: If the regulator is disabled it will return the current value. This | |
2136 | * function should not be used to determine regulator state. | |
2137 | */ | |
2138 | int regulator_get_current_limit(struct regulator *regulator) | |
2139 | { | |
2140 | return _regulator_get_current_limit(regulator->rdev); | |
2141 | } | |
2142 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); | |
2143 | ||
2144 | /** | |
2145 | * regulator_set_mode - set regulator operating mode | |
2146 | * @regulator: regulator source | |
2147 | * @mode: operating mode - one of the REGULATOR_MODE constants | |
2148 | * | |
2149 | * Set regulator operating mode to increase regulator efficiency or improve | |
2150 | * regulation performance. | |
2151 | * | |
2152 | * NOTE: Regulator system constraints must be set for this regulator before | |
2153 | * calling this function otherwise this call will fail. | |
2154 | */ | |
2155 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) | |
2156 | { | |
2157 | struct regulator_dev *rdev = regulator->rdev; | |
2158 | int ret; | |
500b4ac9 | 2159 | int regulator_curr_mode; |
414c70cb LG |
2160 | |
2161 | mutex_lock(&rdev->mutex); | |
2162 | ||
2163 | /* sanity check */ | |
2164 | if (!rdev->desc->ops->set_mode) { | |
2165 | ret = -EINVAL; | |
2166 | goto out; | |
2167 | } | |
2168 | ||
500b4ac9 SI |
2169 | /* return if the same mode is requested */ |
2170 | if (rdev->desc->ops->get_mode) { | |
2171 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); | |
2172 | if (regulator_curr_mode == mode) { | |
2173 | ret = 0; | |
2174 | goto out; | |
2175 | } | |
2176 | } | |
2177 | ||
414c70cb | 2178 | /* constraints check */ |
22c51b47 | 2179 | ret = regulator_mode_constrain(rdev, &mode); |
414c70cb LG |
2180 | if (ret < 0) |
2181 | goto out; | |
2182 | ||
2183 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
2184 | out: | |
2185 | mutex_unlock(&rdev->mutex); | |
2186 | return ret; | |
2187 | } | |
2188 | EXPORT_SYMBOL_GPL(regulator_set_mode); | |
2189 | ||
2190 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) | |
2191 | { | |
2192 | int ret; | |
2193 | ||
2194 | mutex_lock(&rdev->mutex); | |
2195 | ||
2196 | /* sanity check */ | |
2197 | if (!rdev->desc->ops->get_mode) { | |
2198 | ret = -EINVAL; | |
2199 | goto out; | |
2200 | } | |
2201 | ||
2202 | ret = rdev->desc->ops->get_mode(rdev); | |
2203 | out: | |
2204 | mutex_unlock(&rdev->mutex); | |
2205 | return ret; | |
2206 | } | |
2207 | ||
2208 | /** | |
2209 | * regulator_get_mode - get regulator operating mode | |
2210 | * @regulator: regulator source | |
2211 | * | |
2212 | * Get the current regulator operating mode. | |
2213 | */ | |
2214 | unsigned int regulator_get_mode(struct regulator *regulator) | |
2215 | { | |
2216 | return _regulator_get_mode(regulator->rdev); | |
2217 | } | |
2218 | EXPORT_SYMBOL_GPL(regulator_get_mode); | |
2219 | ||
2220 | /** | |
2221 | * regulator_set_optimum_mode - set regulator optimum operating mode | |
2222 | * @regulator: regulator source | |
2223 | * @uA_load: load current | |
2224 | * | |
2225 | * Notifies the regulator core of a new device load. This is then used by | |
2226 | * DRMS (if enabled by constraints) to set the most efficient regulator | |
2227 | * operating mode for the new regulator loading. | |
2228 | * | |
2229 | * Consumer devices notify their supply regulator of the maximum power | |
2230 | * they will require (can be taken from device datasheet in the power | |
2231 | * consumption tables) when they change operational status and hence power | |
2232 | * state. Examples of operational state changes that can affect power | |
2233 | * consumption are :- | |
2234 | * | |
2235 | * o Device is opened / closed. | |
2236 | * o Device I/O is about to begin or has just finished. | |
2237 | * o Device is idling in between work. | |
2238 | * | |
2239 | * This information is also exported via sysfs to userspace. | |
2240 | * | |
2241 | * DRMS will sum the total requested load on the regulator and change | |
2242 | * to the most efficient operating mode if platform constraints allow. | |
2243 | * | |
2244 | * Returns the new regulator mode or error. | |
2245 | */ | |
2246 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) | |
2247 | { | |
2248 | struct regulator_dev *rdev = regulator->rdev; | |
2249 | struct regulator *consumer; | |
2250 | int ret, output_uV, input_uV, total_uA_load = 0; | |
2251 | unsigned int mode; | |
2252 | ||
2253 | mutex_lock(&rdev->mutex); | |
2254 | ||
a4b41483 MB |
2255 | /* |
2256 | * first check to see if we can set modes at all, otherwise just | |
2257 | * tell the consumer everything is OK. | |
2258 | */ | |
414c70cb LG |
2259 | regulator->uA_load = uA_load; |
2260 | ret = regulator_check_drms(rdev); | |
a4b41483 MB |
2261 | if (ret < 0) { |
2262 | ret = 0; | |
414c70cb | 2263 | goto out; |
a4b41483 | 2264 | } |
414c70cb | 2265 | |
414c70cb LG |
2266 | if (!rdev->desc->ops->get_optimum_mode) |
2267 | goto out; | |
2268 | ||
a4b41483 MB |
2269 | /* |
2270 | * we can actually do this so any errors are indicators of | |
2271 | * potential real failure. | |
2272 | */ | |
2273 | ret = -EINVAL; | |
2274 | ||
414c70cb | 2275 | /* get output voltage */ |
1bf5a1f8 | 2276 | output_uV = _regulator_get_voltage(rdev); |
414c70cb | 2277 | if (output_uV <= 0) { |
5da84fd9 | 2278 | rdev_err(rdev, "invalid output voltage found\n"); |
414c70cb LG |
2279 | goto out; |
2280 | } | |
2281 | ||
2282 | /* get input voltage */ | |
1bf5a1f8 MB |
2283 | input_uV = 0; |
2284 | if (rdev->supply) | |
3801b86a | 2285 | input_uV = regulator_get_voltage(rdev->supply); |
1bf5a1f8 | 2286 | if (input_uV <= 0) |
414c70cb LG |
2287 | input_uV = rdev->constraints->input_uV; |
2288 | if (input_uV <= 0) { | |
5da84fd9 | 2289 | rdev_err(rdev, "invalid input voltage found\n"); |
414c70cb LG |
2290 | goto out; |
2291 | } | |
2292 | ||
2293 | /* calc total requested load for this regulator */ | |
2294 | list_for_each_entry(consumer, &rdev->consumer_list, list) | |
fa2984d4 | 2295 | total_uA_load += consumer->uA_load; |
414c70cb LG |
2296 | |
2297 | mode = rdev->desc->ops->get_optimum_mode(rdev, | |
2298 | input_uV, output_uV, | |
2299 | total_uA_load); | |
2c608234 | 2300 | ret = regulator_mode_constrain(rdev, &mode); |
e573520b | 2301 | if (ret < 0) { |
5da84fd9 JP |
2302 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", |
2303 | total_uA_load, input_uV, output_uV); | |
414c70cb LG |
2304 | goto out; |
2305 | } | |
2306 | ||
2307 | ret = rdev->desc->ops->set_mode(rdev, mode); | |
e573520b | 2308 | if (ret < 0) { |
5da84fd9 | 2309 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); |
414c70cb LG |
2310 | goto out; |
2311 | } | |
2312 | ret = mode; | |
2313 | out: | |
2314 | mutex_unlock(&rdev->mutex); | |
2315 | return ret; | |
2316 | } | |
2317 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); | |
2318 | ||
2319 | /** | |
2320 | * regulator_register_notifier - register regulator event notifier | |
2321 | * @regulator: regulator source | |
69279fb9 | 2322 | * @nb: notifier block |
414c70cb LG |
2323 | * |
2324 | * Register notifier block to receive regulator events. | |
2325 | */ | |
2326 | int regulator_register_notifier(struct regulator *regulator, | |
2327 | struct notifier_block *nb) | |
2328 | { | |
2329 | return blocking_notifier_chain_register(®ulator->rdev->notifier, | |
2330 | nb); | |
2331 | } | |
2332 | EXPORT_SYMBOL_GPL(regulator_register_notifier); | |
2333 | ||
2334 | /** | |
2335 | * regulator_unregister_notifier - unregister regulator event notifier | |
2336 | * @regulator: regulator source | |
69279fb9 | 2337 | * @nb: notifier block |
414c70cb LG |
2338 | * |
2339 | * Unregister regulator event notifier block. | |
2340 | */ | |
2341 | int regulator_unregister_notifier(struct regulator *regulator, | |
2342 | struct notifier_block *nb) | |
2343 | { | |
2344 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, | |
2345 | nb); | |
2346 | } | |
2347 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); | |
2348 | ||
b136fb44 JC |
2349 | /* notify regulator consumers and downstream regulator consumers. |
2350 | * Note mutex must be held by caller. | |
2351 | */ | |
414c70cb LG |
2352 | static void _notifier_call_chain(struct regulator_dev *rdev, |
2353 | unsigned long event, void *data) | |
2354 | { | |
414c70cb | 2355 | /* call rdev chain first */ |
414c70cb | 2356 | blocking_notifier_call_chain(&rdev->notifier, event, NULL); |
414c70cb LG |
2357 | } |
2358 | ||
2359 | /** | |
2360 | * regulator_bulk_get - get multiple regulator consumers | |
2361 | * | |
2362 | * @dev: Device to supply | |
2363 | * @num_consumers: Number of consumers to register | |
2364 | * @consumers: Configuration of consumers; clients are stored here. | |
2365 | * | |
2366 | * @return 0 on success, an errno on failure. | |
2367 | * | |
2368 | * This helper function allows drivers to get several regulator | |
2369 | * consumers in one operation. If any of the regulators cannot be | |
2370 | * acquired then any regulators that were allocated will be freed | |
2371 | * before returning to the caller. | |
2372 | */ | |
2373 | int regulator_bulk_get(struct device *dev, int num_consumers, | |
2374 | struct regulator_bulk_data *consumers) | |
2375 | { | |
2376 | int i; | |
2377 | int ret; | |
2378 | ||
2379 | for (i = 0; i < num_consumers; i++) | |
2380 | consumers[i].consumer = NULL; | |
2381 | ||
2382 | for (i = 0; i < num_consumers; i++) { | |
2383 | consumers[i].consumer = regulator_get(dev, | |
2384 | consumers[i].supply); | |
2385 | if (IS_ERR(consumers[i].consumer)) { | |
414c70cb | 2386 | ret = PTR_ERR(consumers[i].consumer); |
5b307627 MB |
2387 | dev_err(dev, "Failed to get supply '%s': %d\n", |
2388 | consumers[i].supply, ret); | |
414c70cb LG |
2389 | consumers[i].consumer = NULL; |
2390 | goto err; | |
2391 | } | |
2392 | } | |
2393 | ||
2394 | return 0; | |
2395 | ||
2396 | err: | |
2397 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) | |
2398 | regulator_put(consumers[i].consumer); | |
2399 | ||
2400 | return ret; | |
2401 | } | |
2402 | EXPORT_SYMBOL_GPL(regulator_bulk_get); | |
2403 | ||
f21e0e81 MB |
2404 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
2405 | { | |
2406 | struct regulator_bulk_data *bulk = data; | |
2407 | ||
2408 | bulk->ret = regulator_enable(bulk->consumer); | |
2409 | } | |
2410 | ||
414c70cb LG |
2411 | /** |
2412 | * regulator_bulk_enable - enable multiple regulator consumers | |
2413 | * | |
2414 | * @num_consumers: Number of consumers | |
2415 | * @consumers: Consumer data; clients are stored here. | |
2416 | * @return 0 on success, an errno on failure | |
2417 | * | |
2418 | * This convenience API allows consumers to enable multiple regulator | |
2419 | * clients in a single API call. If any consumers cannot be enabled | |
2420 | * then any others that were enabled will be disabled again prior to | |
2421 | * return. | |
2422 | */ | |
2423 | int regulator_bulk_enable(int num_consumers, | |
2424 | struct regulator_bulk_data *consumers) | |
2425 | { | |
f21e0e81 | 2426 | LIST_HEAD(async_domain); |
414c70cb | 2427 | int i; |
f21e0e81 | 2428 | int ret = 0; |
414c70cb | 2429 | |
f21e0e81 MB |
2430 | for (i = 0; i < num_consumers; i++) |
2431 | async_schedule_domain(regulator_bulk_enable_async, | |
2432 | &consumers[i], &async_domain); | |
2433 | ||
2434 | async_synchronize_full_domain(&async_domain); | |
2435 | ||
2436 | /* If any consumer failed we need to unwind any that succeeded */ | |
414c70cb | 2437 | for (i = 0; i < num_consumers; i++) { |
f21e0e81 MB |
2438 | if (consumers[i].ret != 0) { |
2439 | ret = consumers[i].ret; | |
414c70cb | 2440 | goto err; |
f21e0e81 | 2441 | } |
414c70cb LG |
2442 | } |
2443 | ||
2444 | return 0; | |
2445 | ||
2446 | err: | |
f21e0e81 MB |
2447 | for (i = 0; i < num_consumers; i++) |
2448 | if (consumers[i].ret == 0) | |
2449 | regulator_disable(consumers[i].consumer); | |
2450 | else | |
2451 | pr_err("Failed to enable %s: %d\n", | |
2452 | consumers[i].supply, consumers[i].ret); | |
414c70cb LG |
2453 | |
2454 | return ret; | |
2455 | } | |
2456 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); | |
2457 | ||
2458 | /** | |
2459 | * regulator_bulk_disable - disable multiple regulator consumers | |
2460 | * | |
2461 | * @num_consumers: Number of consumers | |
2462 | * @consumers: Consumer data; clients are stored here. | |
2463 | * @return 0 on success, an errno on failure | |
2464 | * | |
2465 | * This convenience API allows consumers to disable multiple regulator | |
2466 | * clients in a single API call. If any consumers cannot be enabled | |
2467 | * then any others that were disabled will be disabled again prior to | |
2468 | * return. | |
2469 | */ | |
2470 | int regulator_bulk_disable(int num_consumers, | |
2471 | struct regulator_bulk_data *consumers) | |
2472 | { | |
2473 | int i; | |
2474 | int ret; | |
2475 | ||
2476 | for (i = 0; i < num_consumers; i++) { | |
2477 | ret = regulator_disable(consumers[i].consumer); | |
2478 | if (ret != 0) | |
2479 | goto err; | |
2480 | } | |
2481 | ||
2482 | return 0; | |
2483 | ||
2484 | err: | |
5da84fd9 | 2485 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
eb143ac1 | 2486 | for (--i; i >= 0; --i) |
414c70cb LG |
2487 | regulator_enable(consumers[i].consumer); |
2488 | ||
2489 | return ret; | |
2490 | } | |
2491 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); | |
2492 | ||
e1de2f42 DK |
2493 | /** |
2494 | * regulator_bulk_force_disable - force disable multiple regulator consumers | |
2495 | * | |
2496 | * @num_consumers: Number of consumers | |
2497 | * @consumers: Consumer data; clients are stored here. | |
2498 | * @return 0 on success, an errno on failure | |
2499 | * | |
2500 | * This convenience API allows consumers to forcibly disable multiple regulator | |
2501 | * clients in a single API call. | |
2502 | * NOTE: This should be used for situations when device damage will | |
2503 | * likely occur if the regulators are not disabled (e.g. over temp). | |
2504 | * Although regulator_force_disable function call for some consumers can | |
2505 | * return error numbers, the function is called for all consumers. | |
2506 | */ | |
2507 | int regulator_bulk_force_disable(int num_consumers, | |
2508 | struct regulator_bulk_data *consumers) | |
2509 | { | |
2510 | int i; | |
2511 | int ret; | |
2512 | ||
2513 | for (i = 0; i < num_consumers; i++) | |
2514 | consumers[i].ret = | |
2515 | regulator_force_disable(consumers[i].consumer); | |
2516 | ||
2517 | for (i = 0; i < num_consumers; i++) { | |
2518 | if (consumers[i].ret != 0) { | |
2519 | ret = consumers[i].ret; | |
2520 | goto out; | |
2521 | } | |
2522 | } | |
2523 | ||
2524 | return 0; | |
2525 | out: | |
2526 | return ret; | |
2527 | } | |
2528 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); | |
2529 | ||
414c70cb LG |
2530 | /** |
2531 | * regulator_bulk_free - free multiple regulator consumers | |
2532 | * | |
2533 | * @num_consumers: Number of consumers | |
2534 | * @consumers: Consumer data; clients are stored here. | |
2535 | * | |
2536 | * This convenience API allows consumers to free multiple regulator | |
2537 | * clients in a single API call. | |
2538 | */ | |
2539 | void regulator_bulk_free(int num_consumers, | |
2540 | struct regulator_bulk_data *consumers) | |
2541 | { | |
2542 | int i; | |
2543 | ||
2544 | for (i = 0; i < num_consumers; i++) { | |
2545 | regulator_put(consumers[i].consumer); | |
2546 | consumers[i].consumer = NULL; | |
2547 | } | |
2548 | } | |
2549 | EXPORT_SYMBOL_GPL(regulator_bulk_free); | |
2550 | ||
2551 | /** | |
2552 | * regulator_notifier_call_chain - call regulator event notifier | |
69279fb9 | 2553 | * @rdev: regulator source |
414c70cb | 2554 | * @event: notifier block |
69279fb9 | 2555 | * @data: callback-specific data. |
414c70cb LG |
2556 | * |
2557 | * Called by regulator drivers to notify clients a regulator event has | |
2558 | * occurred. We also notify regulator clients downstream. | |
b136fb44 | 2559 | * Note lock must be held by caller. |
414c70cb LG |
2560 | */ |
2561 | int regulator_notifier_call_chain(struct regulator_dev *rdev, | |
2562 | unsigned long event, void *data) | |
2563 | { | |
2564 | _notifier_call_chain(rdev, event, data); | |
2565 | return NOTIFY_DONE; | |
2566 | ||
2567 | } | |
2568 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); | |
2569 | ||
be721979 MB |
2570 | /** |
2571 | * regulator_mode_to_status - convert a regulator mode into a status | |
2572 | * | |
2573 | * @mode: Mode to convert | |
2574 | * | |
2575 | * Convert a regulator mode into a status. | |
2576 | */ | |
2577 | int regulator_mode_to_status(unsigned int mode) | |
2578 | { | |
2579 | switch (mode) { | |
2580 | case REGULATOR_MODE_FAST: | |
2581 | return REGULATOR_STATUS_FAST; | |
2582 | case REGULATOR_MODE_NORMAL: | |
2583 | return REGULATOR_STATUS_NORMAL; | |
2584 | case REGULATOR_MODE_IDLE: | |
2585 | return REGULATOR_STATUS_IDLE; | |
2586 | case REGULATOR_STATUS_STANDBY: | |
2587 | return REGULATOR_STATUS_STANDBY; | |
2588 | default: | |
2589 | return 0; | |
2590 | } | |
2591 | } | |
2592 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); | |
2593 | ||
7ad68e2f DB |
2594 | /* |
2595 | * To avoid cluttering sysfs (and memory) with useless state, only | |
2596 | * create attributes that can be meaningfully displayed. | |
2597 | */ | |
2598 | static int add_regulator_attributes(struct regulator_dev *rdev) | |
2599 | { | |
2600 | struct device *dev = &rdev->dev; | |
2601 | struct regulator_ops *ops = rdev->desc->ops; | |
2602 | int status = 0; | |
2603 | ||
2604 | /* some attributes need specific methods to be displayed */ | |
4c78899b MB |
2605 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || |
2606 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) { | |
7ad68e2f DB |
2607 | status = device_create_file(dev, &dev_attr_microvolts); |
2608 | if (status < 0) | |
2609 | return status; | |
2610 | } | |
2611 | if (ops->get_current_limit) { | |
2612 | status = device_create_file(dev, &dev_attr_microamps); | |
2613 | if (status < 0) | |
2614 | return status; | |
2615 | } | |
2616 | if (ops->get_mode) { | |
2617 | status = device_create_file(dev, &dev_attr_opmode); | |
2618 | if (status < 0) | |
2619 | return status; | |
2620 | } | |
2621 | if (ops->is_enabled) { | |
2622 | status = device_create_file(dev, &dev_attr_state); | |
2623 | if (status < 0) | |
2624 | return status; | |
2625 | } | |
853116a1 DB |
2626 | if (ops->get_status) { |
2627 | status = device_create_file(dev, &dev_attr_status); | |
2628 | if (status < 0) | |
2629 | return status; | |
2630 | } | |
7ad68e2f DB |
2631 | |
2632 | /* some attributes are type-specific */ | |
2633 | if (rdev->desc->type == REGULATOR_CURRENT) { | |
2634 | status = device_create_file(dev, &dev_attr_requested_microamps); | |
2635 | if (status < 0) | |
2636 | return status; | |
2637 | } | |
2638 | ||
2639 | /* all the other attributes exist to support constraints; | |
2640 | * don't show them if there are no constraints, or if the | |
2641 | * relevant supporting methods are missing. | |
2642 | */ | |
2643 | if (!rdev->constraints) | |
2644 | return status; | |
2645 | ||
2646 | /* constraints need specific supporting methods */ | |
e8eef82b | 2647 | if (ops->set_voltage || ops->set_voltage_sel) { |
7ad68e2f DB |
2648 | status = device_create_file(dev, &dev_attr_min_microvolts); |
2649 | if (status < 0) | |
2650 | return status; | |
2651 | status = device_create_file(dev, &dev_attr_max_microvolts); | |
2652 | if (status < 0) | |
2653 | return status; | |
2654 | } | |
2655 | if (ops->set_current_limit) { | |
2656 | status = device_create_file(dev, &dev_attr_min_microamps); | |
2657 | if (status < 0) | |
2658 | return status; | |
2659 | status = device_create_file(dev, &dev_attr_max_microamps); | |
2660 | if (status < 0) | |
2661 | return status; | |
2662 | } | |
2663 | ||
2664 | /* suspend mode constraints need multiple supporting methods */ | |
2665 | if (!(ops->set_suspend_enable && ops->set_suspend_disable)) | |
2666 | return status; | |
2667 | ||
2668 | status = device_create_file(dev, &dev_attr_suspend_standby_state); | |
2669 | if (status < 0) | |
2670 | return status; | |
2671 | status = device_create_file(dev, &dev_attr_suspend_mem_state); | |
2672 | if (status < 0) | |
2673 | return status; | |
2674 | status = device_create_file(dev, &dev_attr_suspend_disk_state); | |
2675 | if (status < 0) | |
2676 | return status; | |
2677 | ||
2678 | if (ops->set_suspend_voltage) { | |
2679 | status = device_create_file(dev, | |
2680 | &dev_attr_suspend_standby_microvolts); | |
2681 | if (status < 0) | |
2682 | return status; | |
2683 | status = device_create_file(dev, | |
2684 | &dev_attr_suspend_mem_microvolts); | |
2685 | if (status < 0) | |
2686 | return status; | |
2687 | status = device_create_file(dev, | |
2688 | &dev_attr_suspend_disk_microvolts); | |
2689 | if (status < 0) | |
2690 | return status; | |
2691 | } | |
2692 | ||
2693 | if (ops->set_suspend_mode) { | |
2694 | status = device_create_file(dev, | |
2695 | &dev_attr_suspend_standby_mode); | |
2696 | if (status < 0) | |
2697 | return status; | |
2698 | status = device_create_file(dev, | |
2699 | &dev_attr_suspend_mem_mode); | |
2700 | if (status < 0) | |
2701 | return status; | |
2702 | status = device_create_file(dev, | |
2703 | &dev_attr_suspend_disk_mode); | |
2704 | if (status < 0) | |
2705 | return status; | |
2706 | } | |
2707 | ||
2708 | return status; | |
2709 | } | |
2710 | ||
1130e5b3 MB |
2711 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
2712 | { | |
2713 | #ifdef CONFIG_DEBUG_FS | |
2714 | rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); | |
2715 | if (IS_ERR(rdev->debugfs) || !rdev->debugfs) { | |
2716 | rdev_warn(rdev, "Failed to create debugfs directory\n"); | |
2717 | rdev->debugfs = NULL; | |
2718 | return; | |
2719 | } | |
2720 | ||
2721 | debugfs_create_u32("use_count", 0444, rdev->debugfs, | |
2722 | &rdev->use_count); | |
2723 | debugfs_create_u32("open_count", 0444, rdev->debugfs, | |
2724 | &rdev->open_count); | |
2725 | #endif | |
2726 | } | |
2727 | ||
414c70cb LG |
2728 | /** |
2729 | * regulator_register - register regulator | |
69279fb9 MB |
2730 | * @regulator_desc: regulator to register |
2731 | * @dev: struct device for the regulator | |
0527100f | 2732 | * @init_data: platform provided init data, passed through by driver |
69279fb9 | 2733 | * @driver_data: private regulator data |
4a7cbb56 MB |
2734 | * @of_node: OpenFirmware node to parse for device tree bindings (may be |
2735 | * NULL). | |
414c70cb LG |
2736 | * |
2737 | * Called by regulator drivers to register a regulator. | |
2738 | * Returns 0 on success. | |
2739 | */ | |
2740 | struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc, | |
f8c12fe3 | 2741 | struct device *dev, const struct regulator_init_data *init_data, |
2c043bcb | 2742 | void *driver_data, struct device_node *of_node) |
414c70cb | 2743 | { |
9a8f5e07 | 2744 | const struct regulation_constraints *constraints = NULL; |
414c70cb LG |
2745 | static atomic_t regulator_no = ATOMIC_INIT(0); |
2746 | struct regulator_dev *rdev; | |
a5766f11 | 2747 | int ret, i; |
69511a45 | 2748 | const char *supply = NULL; |
414c70cb LG |
2749 | |
2750 | if (regulator_desc == NULL) | |
2751 | return ERR_PTR(-EINVAL); | |
2752 | ||
2753 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) | |
2754 | return ERR_PTR(-EINVAL); | |
2755 | ||
cd78dfc6 DL |
2756 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
2757 | regulator_desc->type != REGULATOR_CURRENT) | |
414c70cb LG |
2758 | return ERR_PTR(-EINVAL); |
2759 | ||
476c2d83 MB |
2760 | /* Only one of each should be implemented */ |
2761 | WARN_ON(regulator_desc->ops->get_voltage && | |
2762 | regulator_desc->ops->get_voltage_sel); | |
e8eef82b MB |
2763 | WARN_ON(regulator_desc->ops->set_voltage && |
2764 | regulator_desc->ops->set_voltage_sel); | |
476c2d83 MB |
2765 | |
2766 | /* If we're using selectors we must implement list_voltage. */ | |
2767 | if (regulator_desc->ops->get_voltage_sel && | |
2768 | !regulator_desc->ops->list_voltage) { | |
2769 | return ERR_PTR(-EINVAL); | |
2770 | } | |
e8eef82b MB |
2771 | if (regulator_desc->ops->set_voltage_sel && |
2772 | !regulator_desc->ops->list_voltage) { | |
2773 | return ERR_PTR(-EINVAL); | |
2774 | } | |
476c2d83 | 2775 | |
414c70cb LG |
2776 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
2777 | if (rdev == NULL) | |
2778 | return ERR_PTR(-ENOMEM); | |
2779 | ||
2780 | mutex_lock(®ulator_list_mutex); | |
2781 | ||
2782 | mutex_init(&rdev->mutex); | |
a5766f11 | 2783 | rdev->reg_data = driver_data; |
414c70cb LG |
2784 | rdev->owner = regulator_desc->owner; |
2785 | rdev->desc = regulator_desc; | |
2786 | INIT_LIST_HEAD(&rdev->consumer_list); | |
414c70cb | 2787 | INIT_LIST_HEAD(&rdev->list); |
414c70cb | 2788 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
da07ecd9 | 2789 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
414c70cb | 2790 | |
a5766f11 | 2791 | /* preform any regulator specific init */ |
9a8f5e07 | 2792 | if (init_data && init_data->regulator_init) { |
a5766f11 | 2793 | ret = init_data->regulator_init(rdev->reg_data); |
4fca9545 DB |
2794 | if (ret < 0) |
2795 | goto clean; | |
a5766f11 LG |
2796 | } |
2797 | ||
a5766f11 | 2798 | /* register with sysfs */ |
414c70cb | 2799 | rdev->dev.class = ®ulator_class; |
2c043bcb | 2800 | rdev->dev.of_node = of_node; |
a5766f11 | 2801 | rdev->dev.parent = dev; |
812460a9 KS |
2802 | dev_set_name(&rdev->dev, "regulator.%d", |
2803 | atomic_inc_return(®ulator_no) - 1); | |
a5766f11 | 2804 | ret = device_register(&rdev->dev); |
ad7725cb VK |
2805 | if (ret != 0) { |
2806 | put_device(&rdev->dev); | |
4fca9545 | 2807 | goto clean; |
ad7725cb | 2808 | } |
a5766f11 LG |
2809 | |
2810 | dev_set_drvdata(&rdev->dev, rdev); | |
2811 | ||
74f544c1 | 2812 | /* set regulator constraints */ |
9a8f5e07 MB |
2813 | if (init_data) |
2814 | constraints = &init_data->constraints; | |
2815 | ||
2816 | ret = set_machine_constraints(rdev, constraints); | |
74f544c1 MR |
2817 | if (ret < 0) |
2818 | goto scrub; | |
2819 | ||
7ad68e2f DB |
2820 | /* add attributes supported by this regulator */ |
2821 | ret = add_regulator_attributes(rdev); | |
2822 | if (ret < 0) | |
2823 | goto scrub; | |
2824 | ||
9a8f5e07 | 2825 | if (init_data && init_data->supply_regulator) |
69511a45 RN |
2826 | supply = init_data->supply_regulator; |
2827 | else if (regulator_desc->supply_name) | |
2828 | supply = regulator_desc->supply_name; | |
2829 | ||
2830 | if (supply) { | |
0178f3e2 | 2831 | struct regulator_dev *r; |
0178f3e2 | 2832 | |
69511a45 | 2833 | r = regulator_dev_lookup(dev, supply); |
0178f3e2 | 2834 | |
69511a45 RN |
2835 | if (!r) { |
2836 | dev_err(dev, "Failed to find supply %s\n", supply); | |
7727da22 | 2837 | ret = -ENODEV; |
0178f3e2 MB |
2838 | goto scrub; |
2839 | } | |
2840 | ||
2841 | ret = set_supply(rdev, r); | |
2842 | if (ret < 0) | |
2843 | goto scrub; | |
b2296bd4 LD |
2844 | |
2845 | /* Enable supply if rail is enabled */ | |
2846 | if (rdev->desc->ops->is_enabled && | |
2847 | rdev->desc->ops->is_enabled(rdev)) { | |
2848 | ret = regulator_enable(rdev->supply); | |
2849 | if (ret < 0) | |
2850 | goto scrub; | |
2851 | } | |
0178f3e2 MB |
2852 | } |
2853 | ||
a5766f11 | 2854 | /* add consumers devices */ |
9a8f5e07 MB |
2855 | if (init_data) { |
2856 | for (i = 0; i < init_data->num_consumer_supplies; i++) { | |
2857 | ret = set_consumer_device_supply(rdev, | |
2858 | init_data->consumer_supplies[i].dev, | |
2859 | init_data->consumer_supplies[i].dev_name, | |
23c2f041 | 2860 | init_data->consumer_supplies[i].supply); |
9a8f5e07 MB |
2861 | if (ret < 0) { |
2862 | dev_err(dev, "Failed to set supply %s\n", | |
2863 | init_data->consumer_supplies[i].supply); | |
2864 | goto unset_supplies; | |
2865 | } | |
23c2f041 | 2866 | } |
414c70cb | 2867 | } |
a5766f11 LG |
2868 | |
2869 | list_add(&rdev->list, ®ulator_list); | |
1130e5b3 MB |
2870 | |
2871 | rdev_init_debugfs(rdev); | |
a5766f11 | 2872 | out: |
414c70cb LG |
2873 | mutex_unlock(®ulator_list_mutex); |
2874 | return rdev; | |
4fca9545 | 2875 | |
d4033b54 JN |
2876 | unset_supplies: |
2877 | unset_regulator_supplies(rdev); | |
2878 | ||
4fca9545 | 2879 | scrub: |
1a6958e7 | 2880 | kfree(rdev->constraints); |
4fca9545 | 2881 | device_unregister(&rdev->dev); |
53032daf PW |
2882 | /* device core frees rdev */ |
2883 | rdev = ERR_PTR(ret); | |
2884 | goto out; | |
2885 | ||
4fca9545 DB |
2886 | clean: |
2887 | kfree(rdev); | |
2888 | rdev = ERR_PTR(ret); | |
2889 | goto out; | |
414c70cb LG |
2890 | } |
2891 | EXPORT_SYMBOL_GPL(regulator_register); | |
2892 | ||
2893 | /** | |
2894 | * regulator_unregister - unregister regulator | |
69279fb9 | 2895 | * @rdev: regulator to unregister |
414c70cb LG |
2896 | * |
2897 | * Called by regulator drivers to unregister a regulator. | |
2898 | */ | |
2899 | void regulator_unregister(struct regulator_dev *rdev) | |
2900 | { | |
2901 | if (rdev == NULL) | |
2902 | return; | |
2903 | ||
2904 | mutex_lock(®ulator_list_mutex); | |
1130e5b3 MB |
2905 | #ifdef CONFIG_DEBUG_FS |
2906 | debugfs_remove_recursive(rdev->debugfs); | |
2907 | #endif | |
da07ecd9 | 2908 | flush_work_sync(&rdev->disable_work.work); |
6bf87d17 | 2909 | WARN_ON(rdev->open_count); |
0f1d747b | 2910 | unset_regulator_supplies(rdev); |
414c70cb LG |
2911 | list_del(&rdev->list); |
2912 | if (rdev->supply) | |
3801b86a | 2913 | regulator_put(rdev->supply); |
f8c12fe3 | 2914 | kfree(rdev->constraints); |
58fb5cf5 | 2915 | device_unregister(&rdev->dev); |
414c70cb LG |
2916 | mutex_unlock(®ulator_list_mutex); |
2917 | } | |
2918 | EXPORT_SYMBOL_GPL(regulator_unregister); | |
2919 | ||
414c70cb | 2920 | /** |
cf7bbcdf | 2921 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
414c70cb LG |
2922 | * @state: system suspend state |
2923 | * | |
2924 | * Configure each regulator with it's suspend operating parameters for state. | |
2925 | * This will usually be called by machine suspend code prior to supending. | |
2926 | */ | |
2927 | int regulator_suspend_prepare(suspend_state_t state) | |
2928 | { | |
2929 | struct regulator_dev *rdev; | |
2930 | int ret = 0; | |
2931 | ||
2932 | /* ON is handled by regulator active state */ | |
2933 | if (state == PM_SUSPEND_ON) | |
2934 | return -EINVAL; | |
2935 | ||
2936 | mutex_lock(®ulator_list_mutex); | |
2937 | list_for_each_entry(rdev, ®ulator_list, list) { | |
2938 | ||
2939 | mutex_lock(&rdev->mutex); | |
2940 | ret = suspend_prepare(rdev, state); | |
2941 | mutex_unlock(&rdev->mutex); | |
2942 | ||
2943 | if (ret < 0) { | |
5da84fd9 | 2944 | rdev_err(rdev, "failed to prepare\n"); |
414c70cb LG |
2945 | goto out; |
2946 | } | |
2947 | } | |
2948 | out: | |
2949 | mutex_unlock(®ulator_list_mutex); | |
2950 | return ret; | |
2951 | } | |
2952 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); | |
2953 | ||
7a32b589 MH |
2954 | /** |
2955 | * regulator_suspend_finish - resume regulators from system wide suspend | |
2956 | * | |
2957 | * Turn on regulators that might be turned off by regulator_suspend_prepare | |
2958 | * and that should be turned on according to the regulators properties. | |
2959 | */ | |
2960 | int regulator_suspend_finish(void) | |
2961 | { | |
2962 | struct regulator_dev *rdev; | |
2963 | int ret = 0, error; | |
2964 | ||
2965 | mutex_lock(®ulator_list_mutex); | |
2966 | list_for_each_entry(rdev, ®ulator_list, list) { | |
2967 | struct regulator_ops *ops = rdev->desc->ops; | |
2968 | ||
2969 | mutex_lock(&rdev->mutex); | |
2970 | if ((rdev->use_count > 0 || rdev->constraints->always_on) && | |
2971 | ops->enable) { | |
2972 | error = ops->enable(rdev); | |
2973 | if (error) | |
2974 | ret = error; | |
2975 | } else { | |
2976 | if (!has_full_constraints) | |
2977 | goto unlock; | |
2978 | if (!ops->disable) | |
2979 | goto unlock; | |
2980 | if (ops->is_enabled && !ops->is_enabled(rdev)) | |
2981 | goto unlock; | |
2982 | ||
2983 | error = ops->disable(rdev); | |
2984 | if (error) | |
2985 | ret = error; | |
2986 | } | |
2987 | unlock: | |
2988 | mutex_unlock(&rdev->mutex); | |
2989 | } | |
2990 | mutex_unlock(®ulator_list_mutex); | |
2991 | return ret; | |
2992 | } | |
2993 | EXPORT_SYMBOL_GPL(regulator_suspend_finish); | |
2994 | ||
ca725561 MB |
2995 | /** |
2996 | * regulator_has_full_constraints - the system has fully specified constraints | |
2997 | * | |
2998 | * Calling this function will cause the regulator API to disable all | |
2999 | * regulators which have a zero use count and don't have an always_on | |
3000 | * constraint in a late_initcall. | |
3001 | * | |
3002 | * The intention is that this will become the default behaviour in a | |
3003 | * future kernel release so users are encouraged to use this facility | |
3004 | * now. | |
3005 | */ | |
3006 | void regulator_has_full_constraints(void) | |
3007 | { | |
3008 | has_full_constraints = 1; | |
3009 | } | |
3010 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); | |
3011 | ||
688fe99a MB |
3012 | /** |
3013 | * regulator_use_dummy_regulator - Provide a dummy regulator when none is found | |
3014 | * | |
3015 | * Calling this function will cause the regulator API to provide a | |
3016 | * dummy regulator to consumers if no physical regulator is found, | |
3017 | * allowing most consumers to proceed as though a regulator were | |
3018 | * configured. This allows systems such as those with software | |
3019 | * controllable regulators for the CPU core only to be brought up more | |
3020 | * readily. | |
3021 | */ | |
3022 | void regulator_use_dummy_regulator(void) | |
3023 | { | |
3024 | board_wants_dummy_regulator = true; | |
3025 | } | |
3026 | EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); | |
3027 | ||
414c70cb LG |
3028 | /** |
3029 | * rdev_get_drvdata - get rdev regulator driver data | |
69279fb9 | 3030 | * @rdev: regulator |
414c70cb LG |
3031 | * |
3032 | * Get rdev regulator driver private data. This call can be used in the | |
3033 | * regulator driver context. | |
3034 | */ | |
3035 | void *rdev_get_drvdata(struct regulator_dev *rdev) | |
3036 | { | |
3037 | return rdev->reg_data; | |
3038 | } | |
3039 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); | |
3040 | ||
3041 | /** | |
3042 | * regulator_get_drvdata - get regulator driver data | |
3043 | * @regulator: regulator | |
3044 | * | |
3045 | * Get regulator driver private data. This call can be used in the consumer | |
3046 | * driver context when non API regulator specific functions need to be called. | |
3047 | */ | |
3048 | void *regulator_get_drvdata(struct regulator *regulator) | |
3049 | { | |
3050 | return regulator->rdev->reg_data; | |
3051 | } | |
3052 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); | |
3053 | ||
3054 | /** | |
3055 | * regulator_set_drvdata - set regulator driver data | |
3056 | * @regulator: regulator | |
3057 | * @data: data | |
3058 | */ | |
3059 | void regulator_set_drvdata(struct regulator *regulator, void *data) | |
3060 | { | |
3061 | regulator->rdev->reg_data = data; | |
3062 | } | |
3063 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); | |
3064 | ||
3065 | /** | |
3066 | * regulator_get_id - get regulator ID | |
69279fb9 | 3067 | * @rdev: regulator |
414c70cb LG |
3068 | */ |
3069 | int rdev_get_id(struct regulator_dev *rdev) | |
3070 | { | |
3071 | return rdev->desc->id; | |
3072 | } | |
3073 | EXPORT_SYMBOL_GPL(rdev_get_id); | |
3074 | ||
a5766f11 LG |
3075 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
3076 | { | |
3077 | return &rdev->dev; | |
3078 | } | |
3079 | EXPORT_SYMBOL_GPL(rdev_get_dev); | |
3080 | ||
3081 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) | |
3082 | { | |
3083 | return reg_init_data->driver_data; | |
3084 | } | |
3085 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); | |
3086 | ||
ba55a974 MB |
3087 | #ifdef CONFIG_DEBUG_FS |
3088 | static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, | |
3089 | size_t count, loff_t *ppos) | |
3090 | { | |
3091 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
3092 | ssize_t len, ret = 0; | |
3093 | struct regulator_map *map; | |
3094 | ||
3095 | if (!buf) | |
3096 | return -ENOMEM; | |
3097 | ||
3098 | list_for_each_entry(map, ®ulator_map_list, list) { | |
3099 | len = snprintf(buf + ret, PAGE_SIZE - ret, | |
3100 | "%s -> %s.%s\n", | |
3101 | rdev_get_name(map->regulator), map->dev_name, | |
3102 | map->supply); | |
3103 | if (len >= 0) | |
3104 | ret += len; | |
3105 | if (ret > PAGE_SIZE) { | |
3106 | ret = PAGE_SIZE; | |
3107 | break; | |
3108 | } | |
3109 | } | |
3110 | ||
3111 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); | |
3112 | ||
3113 | kfree(buf); | |
3114 | ||
3115 | return ret; | |
3116 | } | |
3117 | ||
3118 | static const struct file_operations supply_map_fops = { | |
3119 | .read = supply_map_read_file, | |
3120 | .llseek = default_llseek, | |
3121 | }; | |
3122 | #endif | |
3123 | ||
414c70cb LG |
3124 | static int __init regulator_init(void) |
3125 | { | |
34abbd68 MB |
3126 | int ret; |
3127 | ||
34abbd68 MB |
3128 | ret = class_register(®ulator_class); |
3129 | ||
1130e5b3 MB |
3130 | #ifdef CONFIG_DEBUG_FS |
3131 | debugfs_root = debugfs_create_dir("regulator", NULL); | |
3132 | if (IS_ERR(debugfs_root) || !debugfs_root) { | |
3133 | pr_warn("regulator: Failed to create debugfs directory\n"); | |
3134 | debugfs_root = NULL; | |
3135 | } | |
ba55a974 MB |
3136 | |
3137 | if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root, | |
3138 | NULL, &supply_map_fops))) | |
3139 | pr_warn("regulator: Failed to create supplies debugfs\n"); | |
1130e5b3 MB |
3140 | #endif |
3141 | ||
34abbd68 MB |
3142 | regulator_dummy_init(); |
3143 | ||
3144 | return ret; | |
414c70cb LG |
3145 | } |
3146 | ||
3147 | /* init early to allow our consumers to complete system booting */ | |
3148 | core_initcall(regulator_init); | |
ca725561 MB |
3149 | |
3150 | static int __init regulator_init_complete(void) | |
3151 | { | |
3152 | struct regulator_dev *rdev; | |
3153 | struct regulator_ops *ops; | |
3154 | struct regulation_constraints *c; | |
3155 | int enabled, ret; | |
ca725561 MB |
3156 | |
3157 | mutex_lock(®ulator_list_mutex); | |
3158 | ||
3159 | /* If we have a full configuration then disable any regulators | |
3160 | * which are not in use or always_on. This will become the | |
3161 | * default behaviour in the future. | |
3162 | */ | |
3163 | list_for_each_entry(rdev, ®ulator_list, list) { | |
3164 | ops = rdev->desc->ops; | |
3165 | c = rdev->constraints; | |
3166 | ||
f25e0b4f | 3167 | if (!ops->disable || (c && c->always_on)) |
ca725561 MB |
3168 | continue; |
3169 | ||
3170 | mutex_lock(&rdev->mutex); | |
3171 | ||
3172 | if (rdev->use_count) | |
3173 | goto unlock; | |
3174 | ||
3175 | /* If we can't read the status assume it's on. */ | |
3176 | if (ops->is_enabled) | |
3177 | enabled = ops->is_enabled(rdev); | |
3178 | else | |
3179 | enabled = 1; | |
3180 | ||
3181 | if (!enabled) | |
3182 | goto unlock; | |
3183 | ||
3184 | if (has_full_constraints) { | |
3185 | /* We log since this may kill the system if it | |
3186 | * goes wrong. */ | |
5da84fd9 | 3187 | rdev_info(rdev, "disabling\n"); |
ca725561 MB |
3188 | ret = ops->disable(rdev); |
3189 | if (ret != 0) { | |
5da84fd9 | 3190 | rdev_err(rdev, "couldn't disable: %d\n", ret); |
ca725561 MB |
3191 | } |
3192 | } else { | |
3193 | /* The intention is that in future we will | |
3194 | * assume that full constraints are provided | |
3195 | * so warn even if we aren't going to do | |
3196 | * anything here. | |
3197 | */ | |
5da84fd9 | 3198 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); |
ca725561 MB |
3199 | } |
3200 | ||
3201 | unlock: | |
3202 | mutex_unlock(&rdev->mutex); | |
3203 | } | |
3204 | ||
3205 | mutex_unlock(®ulator_list_mutex); | |
3206 | ||
3207 | return 0; | |
3208 | } | |
3209 | late_initcall(regulator_init_complete); |