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