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