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