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