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