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