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