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