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