block: fix current kernel-doc warnings
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / regulator / core.c
blob02a774424e8de2bbade4eafeaadf6b8160fc05ef
1 /*
2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
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.
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>
26 #define REGULATOR_VERSION "0.5"
28 static DEFINE_MUTEX(regulator_list_mutex);
29 static LIST_HEAD(regulator_list);
30 static LIST_HEAD(regulator_map_list);
32 /**
33 * struct regulator_dev
35 * Voltage / Current regulator class device. One for each regulator.
37 struct regulator_dev {
38 struct regulator_desc *desc;
39 int use_count;
41 /* lists we belong to */
42 struct list_head list; /* list of all regulators */
43 struct list_head slist; /* list of supplied regulators */
45 /* lists we own */
46 struct list_head consumer_list; /* consumers we supply */
47 struct list_head supply_list; /* regulators we supply */
49 struct blocking_notifier_head notifier;
50 struct mutex mutex; /* consumer lock */
51 struct module *owner;
52 struct device dev;
53 struct regulation_constraints *constraints;
54 struct regulator_dev *supply; /* for tree */
56 void *reg_data; /* regulator_dev data */
59 /**
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
66 struct device *dev;
67 const char *supply;
68 struct regulator_dev *regulator;
72 * struct regulator
74 * One for each consumer device.
76 struct regulator {
77 struct device *dev;
78 struct list_head list;
79 int uA_load;
80 int min_uV;
81 int max_uV;
82 int enabled; /* client has called enabled */
83 char *supply_name;
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
88 static int _regulator_is_enabled(struct regulator_dev *rdev);
89 static int _regulator_disable(struct regulator_dev *rdev);
90 static int _regulator_get_voltage(struct regulator_dev *rdev);
91 static int _regulator_get_current_limit(struct regulator_dev *rdev);
92 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93 static void _notifier_call_chain(struct regulator_dev *rdev,
94 unsigned long event, void *data);
96 /* gets the regulator for a given consumer device */
97 static struct regulator *get_device_regulator(struct device *dev)
99 struct regulator *regulator = NULL;
100 struct regulator_dev *rdev;
102 mutex_lock(&regulator_list_mutex);
103 list_for_each_entry(rdev, &regulator_list, list) {
104 mutex_lock(&rdev->mutex);
105 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106 if (regulator->dev == dev) {
107 mutex_unlock(&rdev->mutex);
108 mutex_unlock(&regulator_list_mutex);
109 return regulator;
112 mutex_unlock(&rdev->mutex);
114 mutex_unlock(&regulator_list_mutex);
115 return NULL;
118 /* Platform voltage constraint check */
119 static int regulator_check_voltage(struct regulator_dev *rdev,
120 int *min_uV, int *max_uV)
122 BUG_ON(*min_uV > *max_uV);
124 if (!rdev->constraints) {
125 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126 rdev->desc->name);
127 return -ENODEV;
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130 printk(KERN_ERR "%s: operation not allowed for %s\n",
131 __func__, rdev->desc->name);
132 return -EPERM;
135 if (*max_uV > rdev->constraints->max_uV)
136 *max_uV = rdev->constraints->max_uV;
137 if (*min_uV < rdev->constraints->min_uV)
138 *min_uV = rdev->constraints->min_uV;
140 if (*min_uV > *max_uV)
141 return -EINVAL;
143 return 0;
146 /* current constraint check */
147 static int regulator_check_current_limit(struct regulator_dev *rdev,
148 int *min_uA, int *max_uA)
150 BUG_ON(*min_uA > *max_uA);
152 if (!rdev->constraints) {
153 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154 rdev->desc->name);
155 return -ENODEV;
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158 printk(KERN_ERR "%s: operation not allowed for %s\n",
159 __func__, rdev->desc->name);
160 return -EPERM;
163 if (*max_uA > rdev->constraints->max_uA)
164 *max_uA = rdev->constraints->max_uA;
165 if (*min_uA < rdev->constraints->min_uA)
166 *min_uA = rdev->constraints->min_uA;
168 if (*min_uA > *max_uA)
169 return -EINVAL;
171 return 0;
174 /* operating mode constraint check */
175 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
177 if (!rdev->constraints) {
178 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
179 rdev->desc->name);
180 return -ENODEV;
182 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
183 printk(KERN_ERR "%s: operation not allowed for %s\n",
184 __func__, rdev->desc->name);
185 return -EPERM;
187 if (!(rdev->constraints->valid_modes_mask & mode)) {
188 printk(KERN_ERR "%s: invalid mode %x for %s\n",
189 __func__, mode, rdev->desc->name);
190 return -EINVAL;
192 return 0;
195 /* dynamic regulator mode switching constraint check */
196 static int regulator_check_drms(struct regulator_dev *rdev)
198 if (!rdev->constraints) {
199 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
200 rdev->desc->name);
201 return -ENODEV;
203 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
204 printk(KERN_ERR "%s: operation not allowed for %s\n",
205 __func__, rdev->desc->name);
206 return -EPERM;
208 return 0;
211 static ssize_t device_requested_uA_show(struct device *dev,
212 struct device_attribute *attr, char *buf)
214 struct regulator *regulator;
216 regulator = get_device_regulator(dev);
217 if (regulator == NULL)
218 return 0;
220 return sprintf(buf, "%d\n", regulator->uA_load);
223 static ssize_t regulator_uV_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
226 struct regulator_dev *rdev = dev_get_drvdata(dev);
227 ssize_t ret;
229 mutex_lock(&rdev->mutex);
230 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
231 mutex_unlock(&rdev->mutex);
233 return ret;
236 static ssize_t regulator_uA_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
239 struct regulator_dev *rdev = dev_get_drvdata(dev);
241 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
244 static ssize_t regulator_name_show(struct device *dev,
245 struct device_attribute *attr, char *buf)
247 struct regulator_dev *rdev = dev_get_drvdata(dev);
248 const char *name;
250 if (rdev->constraints->name)
251 name = rdev->constraints->name;
252 else if (rdev->desc->name)
253 name = rdev->desc->name;
254 else
255 name = "";
257 return sprintf(buf, "%s\n", name);
260 static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
263 struct regulator_dev *rdev = dev_get_drvdata(dev);
264 int mode = _regulator_get_mode(rdev);
266 switch (mode) {
267 case REGULATOR_MODE_FAST:
268 return sprintf(buf, "fast\n");
269 case REGULATOR_MODE_NORMAL:
270 return sprintf(buf, "normal\n");
271 case REGULATOR_MODE_IDLE:
272 return sprintf(buf, "idle\n");
273 case REGULATOR_MODE_STANDBY:
274 return sprintf(buf, "standby\n");
276 return sprintf(buf, "unknown\n");
279 static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
283 int state = _regulator_is_enabled(rdev);
285 if (state > 0)
286 return sprintf(buf, "enabled\n");
287 else if (state == 0)
288 return sprintf(buf, "disabled\n");
289 else
290 return sprintf(buf, "unknown\n");
293 static ssize_t regulator_min_uA_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct regulator_dev *rdev = dev_get_drvdata(dev);
298 if (!rdev->constraints)
299 return sprintf(buf, "constraint not defined\n");
301 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
304 static ssize_t regulator_max_uA_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
309 if (!rdev->constraints)
310 return sprintf(buf, "constraint not defined\n");
312 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
315 static ssize_t regulator_min_uV_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
320 if (!rdev->constraints)
321 return sprintf(buf, "constraint not defined\n");
323 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
326 static ssize_t regulator_max_uV_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct regulator_dev *rdev = dev_get_drvdata(dev);
331 if (!rdev->constraints)
332 return sprintf(buf, "constraint not defined\n");
334 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
337 static ssize_t regulator_total_uA_show(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct regulator_dev *rdev = dev_get_drvdata(dev);
341 struct regulator *regulator;
342 int uA = 0;
344 mutex_lock(&rdev->mutex);
345 list_for_each_entry(regulator, &rdev->consumer_list, list)
346 uA += regulator->uA_load;
347 mutex_unlock(&rdev->mutex);
348 return sprintf(buf, "%d\n", uA);
351 static ssize_t regulator_num_users_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
355 return sprintf(buf, "%d\n", rdev->use_count);
358 static ssize_t regulator_type_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
361 struct regulator_dev *rdev = dev_get_drvdata(dev);
363 switch (rdev->desc->type) {
364 case REGULATOR_VOLTAGE:
365 return sprintf(buf, "voltage\n");
366 case REGULATOR_CURRENT:
367 return sprintf(buf, "current\n");
369 return sprintf(buf, "unknown\n");
372 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
375 struct regulator_dev *rdev = dev_get_drvdata(dev);
377 if (!rdev->constraints)
378 return sprintf(buf, "not defined\n");
379 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
382 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
387 if (!rdev->constraints)
388 return sprintf(buf, "not defined\n");
389 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
392 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
397 if (!rdev->constraints)
398 return sprintf(buf, "not defined\n");
399 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
402 static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
403 unsigned int mode, char *buf)
405 switch (mode) {
406 case REGULATOR_MODE_FAST:
407 return sprintf(buf, "fast\n");
408 case REGULATOR_MODE_NORMAL:
409 return sprintf(buf, "normal\n");
410 case REGULATOR_MODE_IDLE:
411 return sprintf(buf, "idle\n");
412 case REGULATOR_MODE_STANDBY:
413 return sprintf(buf, "standby\n");
415 return sprintf(buf, "unknown\n");
418 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
421 struct regulator_dev *rdev = dev_get_drvdata(dev);
423 if (!rdev->constraints)
424 return sprintf(buf, "not defined\n");
425 return suspend_opmode_show(rdev,
426 rdev->constraints->state_mem.mode, buf);
429 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 if (!rdev->constraints)
435 return sprintf(buf, "not defined\n");
436 return suspend_opmode_show(rdev,
437 rdev->constraints->state_disk.mode, buf);
440 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 if (!rdev->constraints)
446 return sprintf(buf, "not defined\n");
447 return suspend_opmode_show(rdev,
448 rdev->constraints->state_standby.mode, buf);
451 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 if (!rdev->constraints)
457 return sprintf(buf, "not defined\n");
459 if (rdev->constraints->state_mem.enabled)
460 return sprintf(buf, "enabled\n");
461 else
462 return sprintf(buf, "disabled\n");
465 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 if (!rdev->constraints)
471 return sprintf(buf, "not defined\n");
473 if (rdev->constraints->state_disk.enabled)
474 return sprintf(buf, "enabled\n");
475 else
476 return sprintf(buf, "disabled\n");
479 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
480 struct device_attribute *attr, char *buf)
482 struct regulator_dev *rdev = dev_get_drvdata(dev);
484 if (!rdev->constraints)
485 return sprintf(buf, "not defined\n");
487 if (rdev->constraints->state_standby.enabled)
488 return sprintf(buf, "enabled\n");
489 else
490 return sprintf(buf, "disabled\n");
493 static struct device_attribute regulator_dev_attrs[] = {
494 __ATTR(name, 0444, regulator_name_show, NULL),
495 __ATTR(microvolts, 0444, regulator_uV_show, NULL),
496 __ATTR(microamps, 0444, regulator_uA_show, NULL),
497 __ATTR(opmode, 0444, regulator_opmode_show, NULL),
498 __ATTR(state, 0444, regulator_state_show, NULL),
499 __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
500 __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
501 __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
502 __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
503 __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
504 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
505 __ATTR(type, 0444, regulator_type_show, NULL),
506 __ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL),
508 __ATTR(suspend_disk_microvolts, 0444,
509 regulator_suspend_disk_uV_show, NULL),
510 __ATTR(suspend_standby_microvolts, 0444,
511 regulator_suspend_standby_uV_show, NULL),
512 __ATTR(suspend_mem_mode, 0444,
513 regulator_suspend_mem_mode_show, NULL),
514 __ATTR(suspend_disk_mode, 0444,
515 regulator_suspend_disk_mode_show, NULL),
516 __ATTR(suspend_standby_mode, 0444,
517 regulator_suspend_standby_mode_show, NULL),
518 __ATTR(suspend_mem_state, 0444,
519 regulator_suspend_mem_state_show, NULL),
520 __ATTR(suspend_disk_state, 0444,
521 regulator_suspend_disk_state_show, NULL),
522 __ATTR(suspend_standby_state, 0444,
523 regulator_suspend_standby_state_show, NULL),
524 __ATTR_NULL,
527 static void regulator_dev_release(struct device *dev)
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
530 kfree(rdev);
533 static struct class regulator_class = {
534 .name = "regulator",
535 .dev_release = regulator_dev_release,
536 .dev_attrs = regulator_dev_attrs,
539 /* Calculate the new optimum regulator operating mode based on the new total
540 * consumer load. All locks held by caller */
541 static void drms_uA_update(struct regulator_dev *rdev)
543 struct regulator *sibling;
544 int current_uA = 0, output_uV, input_uV, err;
545 unsigned int mode;
547 err = regulator_check_drms(rdev);
548 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
549 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
550 return;
552 /* get output voltage */
553 output_uV = rdev->desc->ops->get_voltage(rdev);
554 if (output_uV <= 0)
555 return;
557 /* get input voltage */
558 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
559 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
560 else
561 input_uV = rdev->constraints->input_uV;
562 if (input_uV <= 0)
563 return;
565 /* calc total requested load */
566 list_for_each_entry(sibling, &rdev->consumer_list, list)
567 current_uA += sibling->uA_load;
569 /* now get the optimum mode for our new total regulator load */
570 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
571 output_uV, current_uA);
573 /* check the new mode is allowed */
574 err = regulator_check_mode(rdev, mode);
575 if (err == 0)
576 rdev->desc->ops->set_mode(rdev, mode);
579 static int suspend_set_state(struct regulator_dev *rdev,
580 struct regulator_state *rstate)
582 int ret = 0;
584 /* enable & disable are mandatory for suspend control */
585 if (!rdev->desc->ops->set_suspend_enable ||
586 !rdev->desc->ops->set_suspend_disable) {
587 printk(KERN_ERR "%s: no way to set suspend state\n",
588 __func__);
589 return -EINVAL;
592 if (rstate->enabled)
593 ret = rdev->desc->ops->set_suspend_enable(rdev);
594 else
595 ret = rdev->desc->ops->set_suspend_disable(rdev);
596 if (ret < 0) {
597 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
598 return ret;
601 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
602 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
603 if (ret < 0) {
604 printk(KERN_ERR "%s: failed to set voltage\n",
605 __func__);
606 return ret;
610 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
611 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
612 if (ret < 0) {
613 printk(KERN_ERR "%s: failed to set mode\n", __func__);
614 return ret;
617 return ret;
620 /* locks held by caller */
621 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
623 if (!rdev->constraints)
624 return -EINVAL;
626 switch (state) {
627 case PM_SUSPEND_STANDBY:
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_standby);
630 case PM_SUSPEND_MEM:
631 return suspend_set_state(rdev,
632 &rdev->constraints->state_mem);
633 case PM_SUSPEND_MAX:
634 return suspend_set_state(rdev,
635 &rdev->constraints->state_disk);
636 default:
637 return -EINVAL;
641 static void print_constraints(struct regulator_dev *rdev)
643 struct regulation_constraints *constraints = rdev->constraints;
644 char buf[80];
645 int count;
647 if (rdev->desc->type == REGULATOR_VOLTAGE) {
648 if (constraints->min_uV == constraints->max_uV)
649 count = sprintf(buf, "%d mV ",
650 constraints->min_uV / 1000);
651 else
652 count = sprintf(buf, "%d <--> %d mV ",
653 constraints->min_uV / 1000,
654 constraints->max_uV / 1000);
655 } else {
656 if (constraints->min_uA == constraints->max_uA)
657 count = sprintf(buf, "%d mA ",
658 constraints->min_uA / 1000);
659 else
660 count = sprintf(buf, "%d <--> %d mA ",
661 constraints->min_uA / 1000,
662 constraints->max_uA / 1000);
664 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
665 count += sprintf(buf + count, "fast ");
666 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
667 count += sprintf(buf + count, "normal ");
668 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
669 count += sprintf(buf + count, "idle ");
670 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
671 count += sprintf(buf + count, "standby");
673 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
677 * set_machine_constraints - sets regulator constraints
678 * @regulator: regulator source
680 * Allows platform initialisation code to define and constrain
681 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
682 * Constraints *must* be set by platform code in order for some
683 * regulator operations to proceed i.e. set_voltage, set_current_limit,
684 * set_mode.
686 static int set_machine_constraints(struct regulator_dev *rdev,
687 struct regulation_constraints *constraints)
689 int ret = 0;
690 const char *name;
691 struct regulator_ops *ops = rdev->desc->ops;
693 if (constraints->name)
694 name = constraints->name;
695 else if (rdev->desc->name)
696 name = rdev->desc->name;
697 else
698 name = "regulator";
700 rdev->constraints = constraints;
702 /* do we need to apply the constraint voltage */
703 if (rdev->constraints->apply_uV &&
704 rdev->constraints->min_uV == rdev->constraints->max_uV &&
705 ops->set_voltage) {
706 ret = ops->set_voltage(rdev,
707 rdev->constraints->min_uV, rdev->constraints->max_uV);
708 if (ret < 0) {
709 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
710 __func__,
711 rdev->constraints->min_uV, name);
712 rdev->constraints = NULL;
713 goto out;
717 /* are we enabled at boot time by firmware / bootloader */
718 if (rdev->constraints->boot_on)
719 rdev->use_count = 1;
721 /* do we need to setup our suspend state */
722 if (constraints->initial_state) {
723 ret = suspend_prepare(rdev, constraints->initial_state);
724 if (ret < 0) {
725 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
726 __func__, name);
727 rdev->constraints = NULL;
728 goto out;
732 /* if always_on is set then turn the regulator on if it's not
733 * already on. */
734 if (constraints->always_on && ops->enable &&
735 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
736 (!ops->is_enabled && !constraints->boot_on))) {
737 ret = ops->enable(rdev);
738 if (ret < 0) {
739 printk(KERN_ERR "%s: failed to enable %s\n",
740 __func__, name);
741 rdev->constraints = NULL;
742 goto out;
746 print_constraints(rdev);
747 out:
748 return ret;
752 * set_supply - set regulator supply regulator
753 * @regulator: regulator name
754 * @supply: supply regulator name
756 * Called by platform initialisation code to set the supply regulator for this
757 * regulator. This ensures that a regulators supply will also be enabled by the
758 * core if it's child is enabled.
760 static int set_supply(struct regulator_dev *rdev,
761 struct regulator_dev *supply_rdev)
763 int err;
765 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
766 "supply");
767 if (err) {
768 printk(KERN_ERR
769 "%s: could not add device link %s err %d\n",
770 __func__, supply_rdev->dev.kobj.name, err);
771 goto out;
773 rdev->supply = supply_rdev;
774 list_add(&rdev->slist, &supply_rdev->supply_list);
775 out:
776 return err;
780 * set_consumer_device_supply: Bind a regulator to a symbolic supply
781 * @regulator: regulator source
782 * @dev: device the supply applies to
783 * @supply: symbolic name for supply
785 * Allows platform initialisation code to map physical regulator
786 * sources to symbolic names for supplies for use by devices. Devices
787 * should use these symbolic names to request regulators, avoiding the
788 * need to provide board-specific regulator names as platform data.
790 static int set_consumer_device_supply(struct regulator_dev *rdev,
791 struct device *consumer_dev, const char *supply)
793 struct regulator_map *node;
795 if (supply == NULL)
796 return -EINVAL;
798 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
799 if (node == NULL)
800 return -ENOMEM;
802 node->regulator = rdev;
803 node->dev = consumer_dev;
804 node->supply = supply;
806 list_add(&node->list, &regulator_map_list);
807 return 0;
810 static void unset_consumer_device_supply(struct regulator_dev *rdev,
811 struct device *consumer_dev)
813 struct regulator_map *node, *n;
815 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
816 if (rdev == node->regulator &&
817 consumer_dev == node->dev) {
818 list_del(&node->list);
819 kfree(node);
820 return;
825 #define REG_STR_SIZE 32
827 static struct regulator *create_regulator(struct regulator_dev *rdev,
828 struct device *dev,
829 const char *supply_name)
831 struct regulator *regulator;
832 char buf[REG_STR_SIZE];
833 int err, size;
835 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
836 if (regulator == NULL)
837 return NULL;
839 mutex_lock(&rdev->mutex);
840 regulator->rdev = rdev;
841 list_add(&regulator->list, &rdev->consumer_list);
843 if (dev) {
844 /* create a 'requested_microamps_name' sysfs entry */
845 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
846 supply_name);
847 if (size >= REG_STR_SIZE)
848 goto overflow_err;
850 regulator->dev = dev;
851 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
852 if (regulator->dev_attr.attr.name == NULL)
853 goto attr_name_err;
855 regulator->dev_attr.attr.owner = THIS_MODULE;
856 regulator->dev_attr.attr.mode = 0444;
857 regulator->dev_attr.show = device_requested_uA_show;
858 err = device_create_file(dev, &regulator->dev_attr);
859 if (err < 0) {
860 printk(KERN_WARNING "%s: could not add regulator_dev"
861 " load sysfs\n", __func__);
862 goto attr_name_err;
865 /* also add a link to the device sysfs entry */
866 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
867 dev->kobj.name, supply_name);
868 if (size >= REG_STR_SIZE)
869 goto attr_err;
871 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
872 if (regulator->supply_name == NULL)
873 goto attr_err;
875 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
876 buf);
877 if (err) {
878 printk(KERN_WARNING
879 "%s: could not add device link %s err %d\n",
880 __func__, dev->kobj.name, err);
881 device_remove_file(dev, &regulator->dev_attr);
882 goto link_name_err;
885 mutex_unlock(&rdev->mutex);
886 return regulator;
887 link_name_err:
888 kfree(regulator->supply_name);
889 attr_err:
890 device_remove_file(regulator->dev, &regulator->dev_attr);
891 attr_name_err:
892 kfree(regulator->dev_attr.attr.name);
893 overflow_err:
894 list_del(&regulator->list);
895 kfree(regulator);
896 mutex_unlock(&rdev->mutex);
897 return NULL;
901 * regulator_get - lookup and obtain a reference to a regulator.
902 * @dev: device for regulator "consumer"
903 * @id: Supply name or regulator ID.
905 * Returns a struct regulator corresponding to the regulator producer,
906 * or IS_ERR() condition containing errno. Use of supply names
907 * configured via regulator_set_device_supply() is strongly
908 * encouraged.
910 struct regulator *regulator_get(struct device *dev, const char *id)
912 struct regulator_dev *rdev;
913 struct regulator_map *map;
914 struct regulator *regulator = ERR_PTR(-ENODEV);
916 if (id == NULL) {
917 printk(KERN_ERR "regulator: get() with no identifier\n");
918 return regulator;
921 mutex_lock(&regulator_list_mutex);
923 list_for_each_entry(map, &regulator_map_list, list) {
924 if (dev == map->dev &&
925 strcmp(map->supply, id) == 0) {
926 rdev = map->regulator;
927 goto found;
930 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
931 id);
932 mutex_unlock(&regulator_list_mutex);
933 return regulator;
935 found:
936 if (!try_module_get(rdev->owner))
937 goto out;
939 regulator = create_regulator(rdev, dev, id);
940 if (regulator == NULL) {
941 regulator = ERR_PTR(-ENOMEM);
942 module_put(rdev->owner);
945 out:
946 mutex_unlock(&regulator_list_mutex);
947 return regulator;
949 EXPORT_SYMBOL_GPL(regulator_get);
952 * regulator_put - "free" the regulator source
953 * @regulator: regulator source
955 * Note: drivers must ensure that all regulator_enable calls made on this
956 * regulator source are balanced by regulator_disable calls prior to calling
957 * this function.
959 void regulator_put(struct regulator *regulator)
961 struct regulator_dev *rdev;
963 if (regulator == NULL || IS_ERR(regulator))
964 return;
966 if (regulator->enabled) {
967 printk(KERN_WARNING "Releasing supply %s while enabled\n",
968 regulator->supply_name);
969 WARN_ON(regulator->enabled);
970 regulator_disable(regulator);
973 mutex_lock(&regulator_list_mutex);
974 rdev = regulator->rdev;
976 /* remove any sysfs entries */
977 if (regulator->dev) {
978 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
979 kfree(regulator->supply_name);
980 device_remove_file(regulator->dev, &regulator->dev_attr);
981 kfree(regulator->dev_attr.attr.name);
983 list_del(&regulator->list);
984 kfree(regulator);
986 module_put(rdev->owner);
987 mutex_unlock(&regulator_list_mutex);
989 EXPORT_SYMBOL_GPL(regulator_put);
991 /* locks held by regulator_enable() */
992 static int _regulator_enable(struct regulator_dev *rdev)
994 int ret = -EINVAL;
996 if (!rdev->constraints) {
997 printk(KERN_ERR "%s: %s has no constraints\n",
998 __func__, rdev->desc->name);
999 return ret;
1002 /* do we need to enable the supply regulator first */
1003 if (rdev->supply) {
1004 ret = _regulator_enable(rdev->supply);
1005 if (ret < 0) {
1006 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1007 __func__, rdev->desc->name, ret);
1008 return ret;
1012 /* check voltage and requested load before enabling */
1013 if (rdev->desc->ops->enable) {
1015 if (rdev->constraints &&
1016 (rdev->constraints->valid_ops_mask &
1017 REGULATOR_CHANGE_DRMS))
1018 drms_uA_update(rdev);
1020 ret = rdev->desc->ops->enable(rdev);
1021 if (ret < 0) {
1022 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1023 __func__, rdev->desc->name, ret);
1024 return ret;
1026 rdev->use_count++;
1027 return ret;
1030 return ret;
1034 * regulator_enable - enable regulator output
1035 * @regulator: regulator source
1037 * Enable the regulator output at the predefined voltage or current value.
1038 * NOTE: the output value can be set by other drivers, boot loader or may be
1039 * hardwired in the regulator.
1040 * NOTE: calls to regulator_enable() must be balanced with calls to
1041 * regulator_disable().
1043 int regulator_enable(struct regulator *regulator)
1045 int ret;
1047 if (regulator->enabled) {
1048 printk(KERN_CRIT "Regulator %s already enabled\n",
1049 regulator->supply_name);
1050 WARN_ON(regulator->enabled);
1051 return 0;
1054 mutex_lock(&regulator->rdev->mutex);
1055 regulator->enabled = 1;
1056 ret = _regulator_enable(regulator->rdev);
1057 if (ret != 0)
1058 regulator->enabled = 0;
1059 mutex_unlock(&regulator->rdev->mutex);
1060 return ret;
1062 EXPORT_SYMBOL_GPL(regulator_enable);
1064 /* locks held by regulator_disable() */
1065 static int _regulator_disable(struct regulator_dev *rdev)
1067 int ret = 0;
1069 /* are we the last user and permitted to disable ? */
1070 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1072 /* we are last user */
1073 if (rdev->desc->ops->disable) {
1074 ret = rdev->desc->ops->disable(rdev);
1075 if (ret < 0) {
1076 printk(KERN_ERR "%s: failed to disable %s\n",
1077 __func__, rdev->desc->name);
1078 return ret;
1082 /* decrease our supplies ref count and disable if required */
1083 if (rdev->supply)
1084 _regulator_disable(rdev->supply);
1086 rdev->use_count = 0;
1087 } else if (rdev->use_count > 1) {
1089 if (rdev->constraints &&
1090 (rdev->constraints->valid_ops_mask &
1091 REGULATOR_CHANGE_DRMS))
1092 drms_uA_update(rdev);
1094 rdev->use_count--;
1096 return ret;
1100 * regulator_disable - disable regulator output
1101 * @regulator: regulator source
1103 * Disable the regulator output voltage or current.
1104 * NOTE: this will only disable the regulator output if no other consumer
1105 * devices have it enabled.
1106 * NOTE: calls to regulator_enable() must be balanced with calls to
1107 * regulator_disable().
1109 int regulator_disable(struct regulator *regulator)
1111 int ret;
1113 if (!regulator->enabled) {
1114 printk(KERN_ERR "%s: not in use by this consumer\n",
1115 __func__);
1116 return 0;
1119 mutex_lock(&regulator->rdev->mutex);
1120 regulator->enabled = 0;
1121 regulator->uA_load = 0;
1122 ret = _regulator_disable(regulator->rdev);
1123 mutex_unlock(&regulator->rdev->mutex);
1124 return ret;
1126 EXPORT_SYMBOL_GPL(regulator_disable);
1128 /* locks held by regulator_force_disable() */
1129 static int _regulator_force_disable(struct regulator_dev *rdev)
1131 int ret = 0;
1133 /* force disable */
1134 if (rdev->desc->ops->disable) {
1135 /* ah well, who wants to live forever... */
1136 ret = rdev->desc->ops->disable(rdev);
1137 if (ret < 0) {
1138 printk(KERN_ERR "%s: failed to force disable %s\n",
1139 __func__, rdev->desc->name);
1140 return ret;
1142 /* notify other consumers that power has been forced off */
1143 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1144 NULL);
1147 /* decrease our supplies ref count and disable if required */
1148 if (rdev->supply)
1149 _regulator_disable(rdev->supply);
1151 rdev->use_count = 0;
1152 return ret;
1156 * regulator_force_disable - force disable regulator output
1157 * @regulator: regulator source
1159 * Forcibly disable the regulator output voltage or current.
1160 * NOTE: this *will* disable the regulator output even if other consumer
1161 * devices have it enabled. This should be used for situations when device
1162 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1164 int regulator_force_disable(struct regulator *regulator)
1166 int ret;
1168 mutex_lock(&regulator->rdev->mutex);
1169 regulator->enabled = 0;
1170 regulator->uA_load = 0;
1171 ret = _regulator_force_disable(regulator->rdev);
1172 mutex_unlock(&regulator->rdev->mutex);
1173 return ret;
1175 EXPORT_SYMBOL_GPL(regulator_force_disable);
1177 static int _regulator_is_enabled(struct regulator_dev *rdev)
1179 int ret;
1181 mutex_lock(&rdev->mutex);
1183 /* sanity check */
1184 if (!rdev->desc->ops->is_enabled) {
1185 ret = -EINVAL;
1186 goto out;
1189 ret = rdev->desc->ops->is_enabled(rdev);
1190 out:
1191 mutex_unlock(&rdev->mutex);
1192 return ret;
1196 * regulator_is_enabled - is the regulator output enabled
1197 * @regulator: regulator source
1199 * Returns zero for disabled otherwise return number of enable requests.
1201 int regulator_is_enabled(struct regulator *regulator)
1203 return _regulator_is_enabled(regulator->rdev);
1205 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1208 * regulator_set_voltage - set regulator output voltage
1209 * @regulator: regulator source
1210 * @min_uV: Minimum required voltage in uV
1211 * @max_uV: Maximum acceptable voltage in uV
1213 * Sets a voltage regulator to the desired output voltage. This can be set
1214 * during any regulator state. IOW, regulator can be disabled or enabled.
1216 * If the regulator is enabled then the voltage will change to the new value
1217 * immediately otherwise if the regulator is disabled the regulator will
1218 * output at the new voltage when enabled.
1220 * NOTE: If the regulator is shared between several devices then the lowest
1221 * request voltage that meets the system constraints will be used.
1222 * NOTE: Regulator system constraints must be set for this regulator before
1223 * calling this function otherwise this call will fail.
1225 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1227 struct regulator_dev *rdev = regulator->rdev;
1228 int ret;
1230 mutex_lock(&rdev->mutex);
1232 /* sanity check */
1233 if (!rdev->desc->ops->set_voltage) {
1234 ret = -EINVAL;
1235 goto out;
1238 /* constraints check */
1239 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1240 if (ret < 0)
1241 goto out;
1242 regulator->min_uV = min_uV;
1243 regulator->max_uV = max_uV;
1244 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1246 out:
1247 mutex_unlock(&rdev->mutex);
1248 return ret;
1250 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1252 static int _regulator_get_voltage(struct regulator_dev *rdev)
1254 /* sanity check */
1255 if (rdev->desc->ops->get_voltage)
1256 return rdev->desc->ops->get_voltage(rdev);
1257 else
1258 return -EINVAL;
1262 * regulator_get_voltage - get regulator output voltage
1263 * @regulator: regulator source
1265 * This returns the current regulator voltage in uV.
1267 * NOTE: If the regulator is disabled it will return the voltage value. This
1268 * function should not be used to determine regulator state.
1270 int regulator_get_voltage(struct regulator *regulator)
1272 int ret;
1274 mutex_lock(&regulator->rdev->mutex);
1276 ret = _regulator_get_voltage(regulator->rdev);
1278 mutex_unlock(&regulator->rdev->mutex);
1280 return ret;
1282 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1285 * regulator_set_current_limit - set regulator output current limit
1286 * @regulator: regulator source
1287 * @min_uA: Minimuum supported current in uA
1288 * @max_uA: Maximum supported current in uA
1290 * Sets current sink to the desired output current. This can be set during
1291 * any regulator state. IOW, regulator can be disabled or enabled.
1293 * If the regulator is enabled then the current will change to the new value
1294 * immediately otherwise if the regulator is disabled the regulator will
1295 * output at the new current when enabled.
1297 * NOTE: Regulator system constraints must be set for this regulator before
1298 * calling this function otherwise this call will fail.
1300 int regulator_set_current_limit(struct regulator *regulator,
1301 int min_uA, int max_uA)
1303 struct regulator_dev *rdev = regulator->rdev;
1304 int ret;
1306 mutex_lock(&rdev->mutex);
1308 /* sanity check */
1309 if (!rdev->desc->ops->set_current_limit) {
1310 ret = -EINVAL;
1311 goto out;
1314 /* constraints check */
1315 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1316 if (ret < 0)
1317 goto out;
1319 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1320 out:
1321 mutex_unlock(&rdev->mutex);
1322 return ret;
1324 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1326 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1328 int ret;
1330 mutex_lock(&rdev->mutex);
1332 /* sanity check */
1333 if (!rdev->desc->ops->get_current_limit) {
1334 ret = -EINVAL;
1335 goto out;
1338 ret = rdev->desc->ops->get_current_limit(rdev);
1339 out:
1340 mutex_unlock(&rdev->mutex);
1341 return ret;
1345 * regulator_get_current_limit - get regulator output current
1346 * @regulator: regulator source
1348 * This returns the current supplied by the specified current sink in uA.
1350 * NOTE: If the regulator is disabled it will return the current value. This
1351 * function should not be used to determine regulator state.
1353 int regulator_get_current_limit(struct regulator *regulator)
1355 return _regulator_get_current_limit(regulator->rdev);
1357 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1360 * regulator_set_mode - set regulator operating mode
1361 * @regulator: regulator source
1362 * @mode: operating mode - one of the REGULATOR_MODE constants
1364 * Set regulator operating mode to increase regulator efficiency or improve
1365 * regulation performance.
1367 * NOTE: Regulator system constraints must be set for this regulator before
1368 * calling this function otherwise this call will fail.
1370 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1372 struct regulator_dev *rdev = regulator->rdev;
1373 int ret;
1375 mutex_lock(&rdev->mutex);
1377 /* sanity check */
1378 if (!rdev->desc->ops->set_mode) {
1379 ret = -EINVAL;
1380 goto out;
1383 /* constraints check */
1384 ret = regulator_check_mode(rdev, mode);
1385 if (ret < 0)
1386 goto out;
1388 ret = rdev->desc->ops->set_mode(rdev, mode);
1389 out:
1390 mutex_unlock(&rdev->mutex);
1391 return ret;
1393 EXPORT_SYMBOL_GPL(regulator_set_mode);
1395 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1397 int ret;
1399 mutex_lock(&rdev->mutex);
1401 /* sanity check */
1402 if (!rdev->desc->ops->get_mode) {
1403 ret = -EINVAL;
1404 goto out;
1407 ret = rdev->desc->ops->get_mode(rdev);
1408 out:
1409 mutex_unlock(&rdev->mutex);
1410 return ret;
1414 * regulator_get_mode - get regulator operating mode
1415 * @regulator: regulator source
1417 * Get the current regulator operating mode.
1419 unsigned int regulator_get_mode(struct regulator *regulator)
1421 return _regulator_get_mode(regulator->rdev);
1423 EXPORT_SYMBOL_GPL(regulator_get_mode);
1426 * regulator_set_optimum_mode - set regulator optimum operating mode
1427 * @regulator: regulator source
1428 * @uA_load: load current
1430 * Notifies the regulator core of a new device load. This is then used by
1431 * DRMS (if enabled by constraints) to set the most efficient regulator
1432 * operating mode for the new regulator loading.
1434 * Consumer devices notify their supply regulator of the maximum power
1435 * they will require (can be taken from device datasheet in the power
1436 * consumption tables) when they change operational status and hence power
1437 * state. Examples of operational state changes that can affect power
1438 * consumption are :-
1440 * o Device is opened / closed.
1441 * o Device I/O is about to begin or has just finished.
1442 * o Device is idling in between work.
1444 * This information is also exported via sysfs to userspace.
1446 * DRMS will sum the total requested load on the regulator and change
1447 * to the most efficient operating mode if platform constraints allow.
1449 * Returns the new regulator mode or error.
1451 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1453 struct regulator_dev *rdev = regulator->rdev;
1454 struct regulator *consumer;
1455 int ret, output_uV, input_uV, total_uA_load = 0;
1456 unsigned int mode;
1458 mutex_lock(&rdev->mutex);
1460 regulator->uA_load = uA_load;
1461 ret = regulator_check_drms(rdev);
1462 if (ret < 0)
1463 goto out;
1464 ret = -EINVAL;
1466 /* sanity check */
1467 if (!rdev->desc->ops->get_optimum_mode)
1468 goto out;
1470 /* get output voltage */
1471 output_uV = rdev->desc->ops->get_voltage(rdev);
1472 if (output_uV <= 0) {
1473 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1474 __func__, rdev->desc->name);
1475 goto out;
1478 /* get input voltage */
1479 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1480 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1481 else
1482 input_uV = rdev->constraints->input_uV;
1483 if (input_uV <= 0) {
1484 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1485 __func__, rdev->desc->name);
1486 goto out;
1489 /* calc total requested load for this regulator */
1490 list_for_each_entry(consumer, &rdev->consumer_list, list)
1491 total_uA_load += consumer->uA_load;
1493 mode = rdev->desc->ops->get_optimum_mode(rdev,
1494 input_uV, output_uV,
1495 total_uA_load);
1496 if (ret <= 0) {
1497 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1498 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1499 total_uA_load, input_uV, output_uV);
1500 goto out;
1503 ret = rdev->desc->ops->set_mode(rdev, mode);
1504 if (ret <= 0) {
1505 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1506 __func__, mode, rdev->desc->name);
1507 goto out;
1509 ret = mode;
1510 out:
1511 mutex_unlock(&rdev->mutex);
1512 return ret;
1514 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1517 * regulator_register_notifier - register regulator event notifier
1518 * @regulator: regulator source
1519 * @notifier_block: notifier block
1521 * Register notifier block to receive regulator events.
1523 int regulator_register_notifier(struct regulator *regulator,
1524 struct notifier_block *nb)
1526 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1527 nb);
1529 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1532 * regulator_unregister_notifier - unregister regulator event notifier
1533 * @regulator: regulator source
1534 * @notifier_block: notifier block
1536 * Unregister regulator event notifier block.
1538 int regulator_unregister_notifier(struct regulator *regulator,
1539 struct notifier_block *nb)
1541 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1542 nb);
1544 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1546 /* notify regulator consumers and downstream regulator consumers */
1547 static void _notifier_call_chain(struct regulator_dev *rdev,
1548 unsigned long event, void *data)
1550 struct regulator_dev *_rdev;
1552 /* call rdev chain first */
1553 mutex_lock(&rdev->mutex);
1554 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1555 mutex_unlock(&rdev->mutex);
1557 /* now notify regulator we supply */
1558 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1559 _notifier_call_chain(_rdev, event, data);
1563 * regulator_bulk_get - get multiple regulator consumers
1565 * @dev: Device to supply
1566 * @num_consumers: Number of consumers to register
1567 * @consumers: Configuration of consumers; clients are stored here.
1569 * @return 0 on success, an errno on failure.
1571 * This helper function allows drivers to get several regulator
1572 * consumers in one operation. If any of the regulators cannot be
1573 * acquired then any regulators that were allocated will be freed
1574 * before returning to the caller.
1576 int regulator_bulk_get(struct device *dev, int num_consumers,
1577 struct regulator_bulk_data *consumers)
1579 int i;
1580 int ret;
1582 for (i = 0; i < num_consumers; i++)
1583 consumers[i].consumer = NULL;
1585 for (i = 0; i < num_consumers; i++) {
1586 consumers[i].consumer = regulator_get(dev,
1587 consumers[i].supply);
1588 if (IS_ERR(consumers[i].consumer)) {
1589 dev_err(dev, "Failed to get supply '%s'\n",
1590 consumers[i].supply);
1591 ret = PTR_ERR(consumers[i].consumer);
1592 consumers[i].consumer = NULL;
1593 goto err;
1597 return 0;
1599 err:
1600 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1601 regulator_put(consumers[i].consumer);
1603 return ret;
1605 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1608 * regulator_bulk_enable - enable multiple regulator consumers
1610 * @num_consumers: Number of consumers
1611 * @consumers: Consumer data; clients are stored here.
1612 * @return 0 on success, an errno on failure
1614 * This convenience API allows consumers to enable multiple regulator
1615 * clients in a single API call. If any consumers cannot be enabled
1616 * then any others that were enabled will be disabled again prior to
1617 * return.
1619 int regulator_bulk_enable(int num_consumers,
1620 struct regulator_bulk_data *consumers)
1622 int i;
1623 int ret;
1625 for (i = 0; i < num_consumers; i++) {
1626 ret = regulator_enable(consumers[i].consumer);
1627 if (ret != 0)
1628 goto err;
1631 return 0;
1633 err:
1634 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1635 for (i = 0; i < num_consumers; i++)
1636 regulator_disable(consumers[i].consumer);
1638 return ret;
1640 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1643 * regulator_bulk_disable - disable multiple regulator consumers
1645 * @num_consumers: Number of consumers
1646 * @consumers: Consumer data; clients are stored here.
1647 * @return 0 on success, an errno on failure
1649 * This convenience API allows consumers to disable multiple regulator
1650 * clients in a single API call. If any consumers cannot be enabled
1651 * then any others that were disabled will be disabled again prior to
1652 * return.
1654 int regulator_bulk_disable(int num_consumers,
1655 struct regulator_bulk_data *consumers)
1657 int i;
1658 int ret;
1660 for (i = 0; i < num_consumers; i++) {
1661 ret = regulator_disable(consumers[i].consumer);
1662 if (ret != 0)
1663 goto err;
1666 return 0;
1668 err:
1669 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1670 for (i = 0; i < num_consumers; i++)
1671 regulator_enable(consumers[i].consumer);
1673 return ret;
1675 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1678 * regulator_bulk_free - free multiple regulator consumers
1680 * @num_consumers: Number of consumers
1681 * @consumers: Consumer data; clients are stored here.
1683 * This convenience API allows consumers to free multiple regulator
1684 * clients in a single API call.
1686 void regulator_bulk_free(int num_consumers,
1687 struct regulator_bulk_data *consumers)
1689 int i;
1691 for (i = 0; i < num_consumers; i++) {
1692 regulator_put(consumers[i].consumer);
1693 consumers[i].consumer = NULL;
1696 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1699 * regulator_notifier_call_chain - call regulator event notifier
1700 * @regulator: regulator source
1701 * @event: notifier block
1702 * @data:
1704 * Called by regulator drivers to notify clients a regulator event has
1705 * occurred. We also notify regulator clients downstream.
1707 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1708 unsigned long event, void *data)
1710 _notifier_call_chain(rdev, event, data);
1711 return NOTIFY_DONE;
1714 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1717 * regulator_register - register regulator
1718 * @regulator: regulator source
1719 * @reg_data: private regulator data
1721 * Called by regulator drivers to register a regulator.
1722 * Returns 0 on success.
1724 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1725 struct device *dev, void *driver_data)
1727 static atomic_t regulator_no = ATOMIC_INIT(0);
1728 struct regulator_dev *rdev;
1729 struct regulator_init_data *init_data = dev->platform_data;
1730 int ret, i;
1732 if (regulator_desc == NULL)
1733 return ERR_PTR(-EINVAL);
1735 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1736 return ERR_PTR(-EINVAL);
1738 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1739 !regulator_desc->type == REGULATOR_CURRENT)
1740 return ERR_PTR(-EINVAL);
1742 if (!init_data)
1743 return ERR_PTR(-EINVAL);
1745 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1746 if (rdev == NULL)
1747 return ERR_PTR(-ENOMEM);
1749 mutex_lock(&regulator_list_mutex);
1751 mutex_init(&rdev->mutex);
1752 rdev->reg_data = driver_data;
1753 rdev->owner = regulator_desc->owner;
1754 rdev->desc = regulator_desc;
1755 INIT_LIST_HEAD(&rdev->consumer_list);
1756 INIT_LIST_HEAD(&rdev->supply_list);
1757 INIT_LIST_HEAD(&rdev->list);
1758 INIT_LIST_HEAD(&rdev->slist);
1759 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1761 /* preform any regulator specific init */
1762 if (init_data->regulator_init) {
1763 ret = init_data->regulator_init(rdev->reg_data);
1764 if (ret < 0) {
1765 kfree(rdev);
1766 rdev = ERR_PTR(ret);
1767 goto out;
1771 /* set regulator constraints */
1772 ret = set_machine_constraints(rdev, &init_data->constraints);
1773 if (ret < 0) {
1774 kfree(rdev);
1775 rdev = ERR_PTR(ret);
1776 goto out;
1779 /* register with sysfs */
1780 rdev->dev.class = &regulator_class;
1781 rdev->dev.parent = dev;
1782 snprintf(rdev->dev.bus_id, sizeof(rdev->dev.bus_id),
1783 "regulator.%d", atomic_inc_return(&regulator_no) - 1);
1784 ret = device_register(&rdev->dev);
1785 if (ret != 0) {
1786 kfree(rdev);
1787 rdev = ERR_PTR(ret);
1788 goto out;
1791 dev_set_drvdata(&rdev->dev, rdev);
1793 /* set supply regulator if it exists */
1794 if (init_data->supply_regulator_dev) {
1795 ret = set_supply(rdev,
1796 dev_get_drvdata(init_data->supply_regulator_dev));
1797 if (ret < 0) {
1798 device_unregister(&rdev->dev);
1799 kfree(rdev);
1800 rdev = ERR_PTR(ret);
1801 goto out;
1805 /* add consumers devices */
1806 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1807 ret = set_consumer_device_supply(rdev,
1808 init_data->consumer_supplies[i].dev,
1809 init_data->consumer_supplies[i].supply);
1810 if (ret < 0) {
1811 for (--i; i >= 0; i--)
1812 unset_consumer_device_supply(rdev,
1813 init_data->consumer_supplies[i].dev);
1814 device_unregister(&rdev->dev);
1815 kfree(rdev);
1816 rdev = ERR_PTR(ret);
1817 goto out;
1821 list_add(&rdev->list, &regulator_list);
1822 out:
1823 mutex_unlock(&regulator_list_mutex);
1824 return rdev;
1826 EXPORT_SYMBOL_GPL(regulator_register);
1829 * regulator_unregister - unregister regulator
1830 * @regulator: regulator source
1832 * Called by regulator drivers to unregister a regulator.
1834 void regulator_unregister(struct regulator_dev *rdev)
1836 if (rdev == NULL)
1837 return;
1839 mutex_lock(&regulator_list_mutex);
1840 list_del(&rdev->list);
1841 if (rdev->supply)
1842 sysfs_remove_link(&rdev->dev.kobj, "supply");
1843 device_unregister(&rdev->dev);
1844 mutex_unlock(&regulator_list_mutex);
1846 EXPORT_SYMBOL_GPL(regulator_unregister);
1849 * regulator_suspend_prepare: prepare regulators for system wide suspend
1850 * @state: system suspend state
1852 * Configure each regulator with it's suspend operating parameters for state.
1853 * This will usually be called by machine suspend code prior to supending.
1855 int regulator_suspend_prepare(suspend_state_t state)
1857 struct regulator_dev *rdev;
1858 int ret = 0;
1860 /* ON is handled by regulator active state */
1861 if (state == PM_SUSPEND_ON)
1862 return -EINVAL;
1864 mutex_lock(&regulator_list_mutex);
1865 list_for_each_entry(rdev, &regulator_list, list) {
1867 mutex_lock(&rdev->mutex);
1868 ret = suspend_prepare(rdev, state);
1869 mutex_unlock(&rdev->mutex);
1871 if (ret < 0) {
1872 printk(KERN_ERR "%s: failed to prepare %s\n",
1873 __func__, rdev->desc->name);
1874 goto out;
1877 out:
1878 mutex_unlock(&regulator_list_mutex);
1879 return ret;
1881 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1884 * rdev_get_drvdata - get rdev regulator driver data
1885 * @regulator: regulator
1887 * Get rdev regulator driver private data. This call can be used in the
1888 * regulator driver context.
1890 void *rdev_get_drvdata(struct regulator_dev *rdev)
1892 return rdev->reg_data;
1894 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1897 * regulator_get_drvdata - get regulator driver data
1898 * @regulator: regulator
1900 * Get regulator driver private data. This call can be used in the consumer
1901 * driver context when non API regulator specific functions need to be called.
1903 void *regulator_get_drvdata(struct regulator *regulator)
1905 return regulator->rdev->reg_data;
1907 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1910 * regulator_set_drvdata - set regulator driver data
1911 * @regulator: regulator
1912 * @data: data
1914 void regulator_set_drvdata(struct regulator *regulator, void *data)
1916 regulator->rdev->reg_data = data;
1918 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1921 * regulator_get_id - get regulator ID
1922 * @regulator: regulator
1924 int rdev_get_id(struct regulator_dev *rdev)
1926 return rdev->desc->id;
1928 EXPORT_SYMBOL_GPL(rdev_get_id);
1930 struct device *rdev_get_dev(struct regulator_dev *rdev)
1932 return &rdev->dev;
1934 EXPORT_SYMBOL_GPL(rdev_get_dev);
1936 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1938 return reg_init_data->driver_data;
1940 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1942 static int __init regulator_init(void)
1944 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1945 return class_register(&regulator_class);
1948 /* init early to allow our consumers to complete system booting */
1949 core_initcall(regulator_init);