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);
33 * struct regulator_dev
35 * Voltage / Current regulator class device. One for each regulator.
37 struct regulator_dev {
38 struct regulator_desc *desc;
41 /* lists we belong to */
42 struct list_head list; /* list of all regulators */
43 struct list_head slist; /* list of supplied regulators */
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 */
53 struct regulation_constraints *constraints;
54 struct regulator_dev *supply; /* for tree */
56 void *reg_data; /* regulator_dev data */
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
68 struct regulator_dev *regulator;
74 * One for each consumer device.
78 struct list_head list;
82 int enabled; /* count of client enables */
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(®ulator_list_mutex);
103 list_for_each_entry(rdev, ®ulator_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(®ulator_list_mutex);
112 mutex_unlock(&rdev->mutex);
114 mutex_unlock(®ulator_list_mutex);
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__,
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);
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)
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__,
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);
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)
174 /* operating mode constraint check */
175 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
178 case REGULATOR_MODE_FAST:
179 case REGULATOR_MODE_NORMAL:
180 case REGULATOR_MODE_IDLE:
181 case REGULATOR_MODE_STANDBY:
187 if (!rdev->constraints) {
188 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
192 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193 printk(KERN_ERR "%s: operation not allowed for %s\n",
194 __func__, rdev->desc->name);
197 if (!(rdev->constraints->valid_modes_mask & mode)) {
198 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199 __func__, mode, rdev->desc->name);
205 /* dynamic regulator mode switching constraint check */
206 static int regulator_check_drms(struct regulator_dev *rdev)
208 if (!rdev->constraints) {
209 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
213 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214 printk(KERN_ERR "%s: operation not allowed for %s\n",
215 __func__, rdev->desc->name);
221 static ssize_t device_requested_uA_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
224 struct regulator *regulator;
226 regulator = get_device_regulator(dev);
227 if (regulator == NULL)
230 return sprintf(buf, "%d\n", regulator->uA_load);
233 static ssize_t regulator_uV_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
236 struct regulator_dev *rdev = dev_get_drvdata(dev);
239 mutex_lock(&rdev->mutex);
240 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241 mutex_unlock(&rdev->mutex);
245 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
247 static ssize_t regulator_uA_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
250 struct regulator_dev *rdev = dev_get_drvdata(dev);
252 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
254 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
256 static ssize_t regulator_name_show(struct device *dev,
257 struct device_attribute *attr, char *buf)
259 struct regulator_dev *rdev = dev_get_drvdata(dev);
262 if (rdev->constraints->name)
263 name = rdev->constraints->name;
264 else if (rdev->desc->name)
265 name = rdev->desc->name;
269 return sprintf(buf, "%s\n", name);
272 static ssize_t regulator_print_opmode(char *buf, int mode)
275 case REGULATOR_MODE_FAST:
276 return sprintf(buf, "fast\n");
277 case REGULATOR_MODE_NORMAL:
278 return sprintf(buf, "normal\n");
279 case REGULATOR_MODE_IDLE:
280 return sprintf(buf, "idle\n");
281 case REGULATOR_MODE_STANDBY:
282 return sprintf(buf, "standby\n");
284 return sprintf(buf, "unknown\n");
287 static ssize_t regulator_opmode_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
290 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
294 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
296 static ssize_t regulator_print_state(char *buf, int state)
299 return sprintf(buf, "enabled\n");
301 return sprintf(buf, "disabled\n");
303 return sprintf(buf, "unknown\n");
306 static ssize_t regulator_state_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
309 struct regulator_dev *rdev = dev_get_drvdata(dev);
311 return regulator_print_state(buf, _regulator_is_enabled(rdev));
313 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
315 static ssize_t regulator_status_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct regulator_dev *rdev = dev_get_drvdata(dev);
322 status = rdev->desc->ops->get_status(rdev);
327 case REGULATOR_STATUS_OFF:
330 case REGULATOR_STATUS_ON:
333 case REGULATOR_STATUS_ERROR:
336 case REGULATOR_STATUS_FAST:
339 case REGULATOR_STATUS_NORMAL:
342 case REGULATOR_STATUS_IDLE:
345 case REGULATOR_STATUS_STANDBY:
352 return sprintf(buf, "%s\n", label);
354 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
356 static ssize_t regulator_min_uA_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct regulator_dev *rdev = dev_get_drvdata(dev);
361 if (!rdev->constraints)
362 return sprintf(buf, "constraint not defined\n");
364 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
366 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
368 static ssize_t regulator_max_uA_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
373 if (!rdev->constraints)
374 return sprintf(buf, "constraint not defined\n");
376 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
378 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
380 static ssize_t regulator_min_uV_show(struct device *dev,
381 struct device_attribute *attr, char *buf)
383 struct regulator_dev *rdev = dev_get_drvdata(dev);
385 if (!rdev->constraints)
386 return sprintf(buf, "constraint not defined\n");
388 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
390 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
392 static ssize_t regulator_max_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, "constraint not defined\n");
400 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
402 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
404 static ssize_t regulator_total_uA_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
407 struct regulator_dev *rdev = dev_get_drvdata(dev);
408 struct regulator *regulator;
411 mutex_lock(&rdev->mutex);
412 list_for_each_entry(regulator, &rdev->consumer_list, list)
413 uA += regulator->uA_load;
414 mutex_unlock(&rdev->mutex);
415 return sprintf(buf, "%d\n", uA);
417 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
419 static ssize_t regulator_num_users_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct regulator_dev *rdev = dev_get_drvdata(dev);
423 return sprintf(buf, "%d\n", rdev->use_count);
426 static ssize_t regulator_type_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 switch (rdev->desc->type) {
432 case REGULATOR_VOLTAGE:
433 return sprintf(buf, "voltage\n");
434 case REGULATOR_CURRENT:
435 return sprintf(buf, "current\n");
437 return sprintf(buf, "unknown\n");
440 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
447 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
448 regulator_suspend_mem_uV_show, NULL);
450 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
457 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
458 regulator_suspend_disk_uV_show, NULL);
460 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 struct regulator_dev *rdev = dev_get_drvdata(dev);
465 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
467 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
468 regulator_suspend_standby_uV_show, NULL);
470 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
473 struct regulator_dev *rdev = dev_get_drvdata(dev);
475 return regulator_print_opmode(buf,
476 rdev->constraints->state_mem.mode);
478 static DEVICE_ATTR(suspend_mem_mode, 0444,
479 regulator_suspend_mem_mode_show, NULL);
481 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
484 struct regulator_dev *rdev = dev_get_drvdata(dev);
486 return regulator_print_opmode(buf,
487 rdev->constraints->state_disk.mode);
489 static DEVICE_ATTR(suspend_disk_mode, 0444,
490 regulator_suspend_disk_mode_show, NULL);
492 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
497 return regulator_print_opmode(buf,
498 rdev->constraints->state_standby.mode);
500 static DEVICE_ATTR(suspend_standby_mode, 0444,
501 regulator_suspend_standby_mode_show, NULL);
503 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
506 struct regulator_dev *rdev = dev_get_drvdata(dev);
508 return regulator_print_state(buf,
509 rdev->constraints->state_mem.enabled);
511 static DEVICE_ATTR(suspend_mem_state, 0444,
512 regulator_suspend_mem_state_show, NULL);
514 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
517 struct regulator_dev *rdev = dev_get_drvdata(dev);
519 return regulator_print_state(buf,
520 rdev->constraints->state_disk.enabled);
522 static DEVICE_ATTR(suspend_disk_state, 0444,
523 regulator_suspend_disk_state_show, NULL);
525 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
530 return regulator_print_state(buf,
531 rdev->constraints->state_standby.enabled);
533 static DEVICE_ATTR(suspend_standby_state, 0444,
534 regulator_suspend_standby_state_show, NULL);
538 * These are the only attributes are present for all regulators.
539 * Other attributes are a function of regulator functionality.
541 static struct device_attribute regulator_dev_attrs[] = {
542 __ATTR(name, 0444, regulator_name_show, NULL),
543 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
544 __ATTR(type, 0444, regulator_type_show, NULL),
548 static void regulator_dev_release(struct device *dev)
550 struct regulator_dev *rdev = dev_get_drvdata(dev);
554 static struct class regulator_class = {
556 .dev_release = regulator_dev_release,
557 .dev_attrs = regulator_dev_attrs,
560 /* Calculate the new optimum regulator operating mode based on the new total
561 * consumer load. All locks held by caller */
562 static void drms_uA_update(struct regulator_dev *rdev)
564 struct regulator *sibling;
565 int current_uA = 0, output_uV, input_uV, err;
568 err = regulator_check_drms(rdev);
569 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
570 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
573 /* get output voltage */
574 output_uV = rdev->desc->ops->get_voltage(rdev);
578 /* get input voltage */
579 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
580 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
582 input_uV = rdev->constraints->input_uV;
586 /* calc total requested load */
587 list_for_each_entry(sibling, &rdev->consumer_list, list)
588 current_uA += sibling->uA_load;
590 /* now get the optimum mode for our new total regulator load */
591 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
592 output_uV, current_uA);
594 /* check the new mode is allowed */
595 err = regulator_check_mode(rdev, mode);
597 rdev->desc->ops->set_mode(rdev, mode);
600 static int suspend_set_state(struct regulator_dev *rdev,
601 struct regulator_state *rstate)
605 /* enable & disable are mandatory for suspend control */
606 if (!rdev->desc->ops->set_suspend_enable ||
607 !rdev->desc->ops->set_suspend_disable) {
608 printk(KERN_ERR "%s: no way to set suspend state\n",
614 ret = rdev->desc->ops->set_suspend_enable(rdev);
616 ret = rdev->desc->ops->set_suspend_disable(rdev);
618 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
622 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
623 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
625 printk(KERN_ERR "%s: failed to set voltage\n",
631 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
632 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
634 printk(KERN_ERR "%s: failed to set mode\n", __func__);
641 /* locks held by caller */
642 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
644 if (!rdev->constraints)
648 case PM_SUSPEND_STANDBY:
649 return suspend_set_state(rdev,
650 &rdev->constraints->state_standby);
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_mem);
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_disk);
662 static void print_constraints(struct regulator_dev *rdev)
664 struct regulation_constraints *constraints = rdev->constraints;
668 if (rdev->desc->type == REGULATOR_VOLTAGE) {
669 if (constraints->min_uV == constraints->max_uV)
670 count = sprintf(buf, "%d mV ",
671 constraints->min_uV / 1000);
673 count = sprintf(buf, "%d <--> %d mV ",
674 constraints->min_uV / 1000,
675 constraints->max_uV / 1000);
677 if (constraints->min_uA == constraints->max_uA)
678 count = sprintf(buf, "%d mA ",
679 constraints->min_uA / 1000);
681 count = sprintf(buf, "%d <--> %d mA ",
682 constraints->min_uA / 1000,
683 constraints->max_uA / 1000);
685 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
686 count += sprintf(buf + count, "fast ");
687 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
688 count += sprintf(buf + count, "normal ");
689 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
690 count += sprintf(buf + count, "idle ");
691 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
692 count += sprintf(buf + count, "standby");
694 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
698 * set_machine_constraints - sets regulator constraints
699 * @rdev: regulator source
700 * @constraints: constraints to apply
702 * Allows platform initialisation code to define and constrain
703 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
704 * Constraints *must* be set by platform code in order for some
705 * regulator operations to proceed i.e. set_voltage, set_current_limit,
708 static int set_machine_constraints(struct regulator_dev *rdev,
709 struct regulation_constraints *constraints)
713 struct regulator_ops *ops = rdev->desc->ops;
715 if (constraints->name)
716 name = constraints->name;
717 else if (rdev->desc->name)
718 name = rdev->desc->name;
722 /* constrain machine-level voltage specs to fit
723 * the actual range supported by this regulator.
725 if (ops->list_voltage && rdev->desc->n_voltages) {
726 int count = rdev->desc->n_voltages;
728 int min_uV = INT_MAX;
729 int max_uV = INT_MIN;
730 int cmin = constraints->min_uV;
731 int cmax = constraints->max_uV;
733 /* it's safe to autoconfigure fixed-voltage supplies */
734 if (count == 1 && !cmin) {
739 /* else require explicit machine-level constraints */
740 else if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
741 pr_err("%s: %s '%s' voltage constraints\n",
742 __func__, "invalid", name);
747 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
748 for (i = 0; i < count; i++) {
751 value = ops->list_voltage(rdev, i);
755 /* maybe adjust [min_uV..max_uV] */
756 if (value >= cmin && value < min_uV)
758 if (value <= cmax && value > max_uV)
762 /* final: [min_uV..max_uV] valid iff constraints valid */
763 if (max_uV < min_uV) {
764 pr_err("%s: %s '%s' voltage constraints\n",
765 __func__, "unsupportable", name);
770 /* use regulator's subset of machine constraints */
771 if (constraints->min_uV < min_uV) {
772 pr_debug("%s: override '%s' %s, %d -> %d\n",
773 __func__, name, "min_uV",
774 constraints->min_uV, min_uV);
775 constraints->min_uV = min_uV;
777 if (constraints->max_uV > max_uV) {
778 pr_debug("%s: override '%s' %s, %d -> %d\n",
779 __func__, name, "max_uV",
780 constraints->max_uV, max_uV);
781 constraints->max_uV = max_uV;
785 rdev->constraints = constraints;
787 /* do we need to apply the constraint voltage */
788 if (rdev->constraints->apply_uV &&
789 rdev->constraints->min_uV == rdev->constraints->max_uV &&
791 ret = ops->set_voltage(rdev,
792 rdev->constraints->min_uV, rdev->constraints->max_uV);
794 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
796 rdev->constraints->min_uV, name);
797 rdev->constraints = NULL;
802 /* are we enabled at boot time by firmware / bootloader */
803 if (rdev->constraints->boot_on)
806 /* do we need to setup our suspend state */
807 if (constraints->initial_state) {
808 ret = suspend_prepare(rdev, constraints->initial_state);
810 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
812 rdev->constraints = NULL;
817 /* if always_on is set then turn the regulator on if it's not
819 if (constraints->always_on && ops->enable &&
820 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
821 (!ops->is_enabled && !constraints->boot_on))) {
822 ret = ops->enable(rdev);
824 printk(KERN_ERR "%s: failed to enable %s\n",
826 rdev->constraints = NULL;
831 print_constraints(rdev);
837 * set_supply - set regulator supply regulator
838 * @rdev: regulator name
839 * @supply_rdev: supply regulator name
841 * Called by platform initialisation code to set the supply regulator for this
842 * regulator. This ensures that a regulators supply will also be enabled by the
843 * core if it's child is enabled.
845 static int set_supply(struct regulator_dev *rdev,
846 struct regulator_dev *supply_rdev)
850 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
854 "%s: could not add device link %s err %d\n",
855 __func__, supply_rdev->dev.kobj.name, err);
858 rdev->supply = supply_rdev;
859 list_add(&rdev->slist, &supply_rdev->supply_list);
865 * set_consumer_device_supply: Bind a regulator to a symbolic supply
866 * @rdev: regulator source
867 * @consumer_dev: device the supply applies to
868 * @supply: symbolic name for supply
870 * Allows platform initialisation code to map physical regulator
871 * sources to symbolic names for supplies for use by devices. Devices
872 * should use these symbolic names to request regulators, avoiding the
873 * need to provide board-specific regulator names as platform data.
875 static int set_consumer_device_supply(struct regulator_dev *rdev,
876 struct device *consumer_dev, const char *supply)
878 struct regulator_map *node;
883 list_for_each_entry(node, ®ulator_map_list, list) {
884 if (consumer_dev != node->dev)
886 if (strcmp(node->supply, supply) != 0)
889 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
890 dev_name(&node->regulator->dev),
891 node->regulator->desc->name,
893 dev_name(&rdev->dev), rdev->desc->name);
897 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
901 node->regulator = rdev;
902 node->dev = consumer_dev;
903 node->supply = supply;
905 list_add(&node->list, ®ulator_map_list);
909 static void unset_consumer_device_supply(struct regulator_dev *rdev,
910 struct device *consumer_dev)
912 struct regulator_map *node, *n;
914 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
915 if (rdev == node->regulator &&
916 consumer_dev == node->dev) {
917 list_del(&node->list);
924 #define REG_STR_SIZE 32
926 static struct regulator *create_regulator(struct regulator_dev *rdev,
928 const char *supply_name)
930 struct regulator *regulator;
931 char buf[REG_STR_SIZE];
934 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
935 if (regulator == NULL)
938 mutex_lock(&rdev->mutex);
939 regulator->rdev = rdev;
940 list_add(®ulator->list, &rdev->consumer_list);
943 /* create a 'requested_microamps_name' sysfs entry */
944 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
946 if (size >= REG_STR_SIZE)
949 regulator->dev = dev;
950 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
951 if (regulator->dev_attr.attr.name == NULL)
954 regulator->dev_attr.attr.owner = THIS_MODULE;
955 regulator->dev_attr.attr.mode = 0444;
956 regulator->dev_attr.show = device_requested_uA_show;
957 err = device_create_file(dev, ®ulator->dev_attr);
959 printk(KERN_WARNING "%s: could not add regulator_dev"
960 " load sysfs\n", __func__);
964 /* also add a link to the device sysfs entry */
965 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
966 dev->kobj.name, supply_name);
967 if (size >= REG_STR_SIZE)
970 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
971 if (regulator->supply_name == NULL)
974 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
978 "%s: could not add device link %s err %d\n",
979 __func__, dev->kobj.name, err);
980 device_remove_file(dev, ®ulator->dev_attr);
984 mutex_unlock(&rdev->mutex);
987 kfree(regulator->supply_name);
989 device_remove_file(regulator->dev, ®ulator->dev_attr);
991 kfree(regulator->dev_attr.attr.name);
993 list_del(®ulator->list);
995 mutex_unlock(&rdev->mutex);
1000 * regulator_get - lookup and obtain a reference to a regulator.
1001 * @dev: device for regulator "consumer"
1002 * @id: Supply name or regulator ID.
1004 * Returns a struct regulator corresponding to the regulator producer,
1005 * or IS_ERR() condition containing errno. Use of supply names
1006 * configured via regulator_set_device_supply() is strongly
1009 struct regulator *regulator_get(struct device *dev, const char *id)
1011 struct regulator_dev *rdev;
1012 struct regulator_map *map;
1013 struct regulator *regulator = ERR_PTR(-ENODEV);
1016 printk(KERN_ERR "regulator: get() with no identifier\n");
1020 mutex_lock(®ulator_list_mutex);
1022 list_for_each_entry(map, ®ulator_map_list, list) {
1023 if (dev == map->dev &&
1024 strcmp(map->supply, id) == 0) {
1025 rdev = map->regulator;
1029 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
1031 mutex_unlock(®ulator_list_mutex);
1035 if (!try_module_get(rdev->owner))
1038 regulator = create_regulator(rdev, dev, id);
1039 if (regulator == NULL) {
1040 regulator = ERR_PTR(-ENOMEM);
1041 module_put(rdev->owner);
1045 mutex_unlock(®ulator_list_mutex);
1048 EXPORT_SYMBOL_GPL(regulator_get);
1051 * regulator_put - "free" the regulator source
1052 * @regulator: regulator source
1054 * Note: drivers must ensure that all regulator_enable calls made on this
1055 * regulator source are balanced by regulator_disable calls prior to calling
1058 void regulator_put(struct regulator *regulator)
1060 struct regulator_dev *rdev;
1062 if (regulator == NULL || IS_ERR(regulator))
1065 mutex_lock(®ulator_list_mutex);
1066 rdev = regulator->rdev;
1068 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
1069 regulator->supply_name))
1070 _regulator_disable(rdev);
1072 /* remove any sysfs entries */
1073 if (regulator->dev) {
1074 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1075 kfree(regulator->supply_name);
1076 device_remove_file(regulator->dev, ®ulator->dev_attr);
1077 kfree(regulator->dev_attr.attr.name);
1079 list_del(®ulator->list);
1082 module_put(rdev->owner);
1083 mutex_unlock(®ulator_list_mutex);
1085 EXPORT_SYMBOL_GPL(regulator_put);
1087 /* locks held by regulator_enable() */
1088 static int _regulator_enable(struct regulator_dev *rdev)
1092 if (!rdev->constraints) {
1093 printk(KERN_ERR "%s: %s has no constraints\n",
1094 __func__, rdev->desc->name);
1098 /* do we need to enable the supply regulator first */
1100 ret = _regulator_enable(rdev->supply);
1102 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1103 __func__, rdev->desc->name, ret);
1108 /* check voltage and requested load before enabling */
1109 if (rdev->desc->ops->enable) {
1111 if (rdev->constraints &&
1112 (rdev->constraints->valid_ops_mask &
1113 REGULATOR_CHANGE_DRMS))
1114 drms_uA_update(rdev);
1116 ret = rdev->desc->ops->enable(rdev);
1118 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1119 __func__, rdev->desc->name, ret);
1130 * regulator_enable - enable regulator output
1131 * @regulator: regulator source
1133 * Request that the regulator be enabled with the regulator output at
1134 * the predefined voltage or current value. Calls to regulator_enable()
1135 * must be balanced with calls to regulator_disable().
1137 * NOTE: the output value can be set by other drivers, boot loader or may be
1138 * hardwired in the regulator.
1140 int regulator_enable(struct regulator *regulator)
1142 struct regulator_dev *rdev = regulator->rdev;
1145 mutex_lock(&rdev->mutex);
1146 if (regulator->enabled == 0)
1147 ret = _regulator_enable(rdev);
1148 else if (regulator->enabled < 0)
1151 regulator->enabled++;
1152 mutex_unlock(&rdev->mutex);
1155 EXPORT_SYMBOL_GPL(regulator_enable);
1157 /* locks held by regulator_disable() */
1158 static int _regulator_disable(struct regulator_dev *rdev)
1162 /* are we the last user and permitted to disable ? */
1163 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1165 /* we are last user */
1166 if (rdev->desc->ops->disable) {
1167 ret = rdev->desc->ops->disable(rdev);
1169 printk(KERN_ERR "%s: failed to disable %s\n",
1170 __func__, rdev->desc->name);
1175 /* decrease our supplies ref count and disable if required */
1177 _regulator_disable(rdev->supply);
1179 rdev->use_count = 0;
1180 } else if (rdev->use_count > 1) {
1182 if (rdev->constraints &&
1183 (rdev->constraints->valid_ops_mask &
1184 REGULATOR_CHANGE_DRMS))
1185 drms_uA_update(rdev);
1193 * regulator_disable - disable regulator output
1194 * @regulator: regulator source
1196 * Disable the regulator output voltage or current. Calls to
1197 * regulator_enable() must be balanced with calls to
1198 * regulator_disable().
1200 * NOTE: this will only disable the regulator output if no other consumer
1201 * devices have it enabled, the regulator device supports disabling and
1202 * machine constraints permit this operation.
1204 int regulator_disable(struct regulator *regulator)
1206 struct regulator_dev *rdev = regulator->rdev;
1209 mutex_lock(&rdev->mutex);
1210 if (regulator->enabled == 1) {
1211 ret = _regulator_disable(rdev);
1213 regulator->uA_load = 0;
1214 } else if (WARN(regulator->enabled <= 0,
1215 "unbalanced disables for supply %s\n",
1216 regulator->supply_name))
1219 regulator->enabled--;
1220 mutex_unlock(&rdev->mutex);
1223 EXPORT_SYMBOL_GPL(regulator_disable);
1225 /* locks held by regulator_force_disable() */
1226 static int _regulator_force_disable(struct regulator_dev *rdev)
1231 if (rdev->desc->ops->disable) {
1232 /* ah well, who wants to live forever... */
1233 ret = rdev->desc->ops->disable(rdev);
1235 printk(KERN_ERR "%s: failed to force disable %s\n",
1236 __func__, rdev->desc->name);
1239 /* notify other consumers that power has been forced off */
1240 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1244 /* decrease our supplies ref count and disable if required */
1246 _regulator_disable(rdev->supply);
1248 rdev->use_count = 0;
1253 * regulator_force_disable - force disable regulator output
1254 * @regulator: regulator source
1256 * Forcibly disable the regulator output voltage or current.
1257 * NOTE: this *will* disable the regulator output even if other consumer
1258 * devices have it enabled. This should be used for situations when device
1259 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1261 int regulator_force_disable(struct regulator *regulator)
1265 mutex_lock(®ulator->rdev->mutex);
1266 regulator->enabled = 0;
1267 regulator->uA_load = 0;
1268 ret = _regulator_force_disable(regulator->rdev);
1269 mutex_unlock(®ulator->rdev->mutex);
1272 EXPORT_SYMBOL_GPL(regulator_force_disable);
1274 static int _regulator_is_enabled(struct regulator_dev *rdev)
1278 mutex_lock(&rdev->mutex);
1281 if (!rdev->desc->ops->is_enabled) {
1286 ret = rdev->desc->ops->is_enabled(rdev);
1288 mutex_unlock(&rdev->mutex);
1293 * regulator_is_enabled - is the regulator output enabled
1294 * @regulator: regulator source
1296 * Returns positive if the regulator driver backing the source/client
1297 * has requested that the device be enabled, zero if it hasn't, else a
1298 * negative errno code.
1300 * Note that the device backing this regulator handle can have multiple
1301 * users, so it might be enabled even if regulator_enable() was never
1302 * called for this particular source.
1304 int regulator_is_enabled(struct regulator *regulator)
1306 return _regulator_is_enabled(regulator->rdev);
1308 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1311 * regulator_count_voltages - count regulator_list_voltage() selectors
1312 * @regulator: regulator source
1314 * Returns number of selectors, or negative errno. Selectors are
1315 * numbered starting at zero, and typically correspond to bitfields
1316 * in hardware registers.
1318 int regulator_count_voltages(struct regulator *regulator)
1320 struct regulator_dev *rdev = regulator->rdev;
1322 return rdev->desc->n_voltages ? : -EINVAL;
1324 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1327 * regulator_list_voltage - enumerate supported voltages
1328 * @regulator: regulator source
1329 * @selector: identify voltage to list
1330 * Context: can sleep
1332 * Returns a voltage that can be passed to @regulator_set_voltage(),
1333 * zero if this selector code can't be used on this sytem, or a
1336 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1338 struct regulator_dev *rdev = regulator->rdev;
1339 struct regulator_ops *ops = rdev->desc->ops;
1342 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1345 mutex_lock(&rdev->mutex);
1346 ret = ops->list_voltage(rdev, selector);
1347 mutex_unlock(&rdev->mutex);
1350 if (ret < rdev->constraints->min_uV)
1352 else if (ret > rdev->constraints->max_uV)
1358 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1361 * regulator_set_voltage - set regulator output voltage
1362 * @regulator: regulator source
1363 * @min_uV: Minimum required voltage in uV
1364 * @max_uV: Maximum acceptable voltage in uV
1366 * Sets a voltage regulator to the desired output voltage. This can be set
1367 * during any regulator state. IOW, regulator can be disabled or enabled.
1369 * If the regulator is enabled then the voltage will change to the new value
1370 * immediately otherwise if the regulator is disabled the regulator will
1371 * output at the new voltage when enabled.
1373 * NOTE: If the regulator is shared between several devices then the lowest
1374 * request voltage that meets the system constraints will be used.
1375 * Regulator system constraints must be set for this regulator before
1376 * calling this function otherwise this call will fail.
1378 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1380 struct regulator_dev *rdev = regulator->rdev;
1383 mutex_lock(&rdev->mutex);
1386 if (!rdev->desc->ops->set_voltage) {
1391 /* constraints check */
1392 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1395 regulator->min_uV = min_uV;
1396 regulator->max_uV = max_uV;
1397 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1400 mutex_unlock(&rdev->mutex);
1403 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1405 static int _regulator_get_voltage(struct regulator_dev *rdev)
1408 if (rdev->desc->ops->get_voltage)
1409 return rdev->desc->ops->get_voltage(rdev);
1415 * regulator_get_voltage - get regulator output voltage
1416 * @regulator: regulator source
1418 * This returns the current regulator voltage in uV.
1420 * NOTE: If the regulator is disabled it will return the voltage value. This
1421 * function should not be used to determine regulator state.
1423 int regulator_get_voltage(struct regulator *regulator)
1427 mutex_lock(®ulator->rdev->mutex);
1429 ret = _regulator_get_voltage(regulator->rdev);
1431 mutex_unlock(®ulator->rdev->mutex);
1435 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1438 * regulator_set_current_limit - set regulator output current limit
1439 * @regulator: regulator source
1440 * @min_uA: Minimuum supported current in uA
1441 * @max_uA: Maximum supported current in uA
1443 * Sets current sink to the desired output current. This can be set during
1444 * any regulator state. IOW, regulator can be disabled or enabled.
1446 * If the regulator is enabled then the current will change to the new value
1447 * immediately otherwise if the regulator is disabled the regulator will
1448 * output at the new current when enabled.
1450 * NOTE: Regulator system constraints must be set for this regulator before
1451 * calling this function otherwise this call will fail.
1453 int regulator_set_current_limit(struct regulator *regulator,
1454 int min_uA, int max_uA)
1456 struct regulator_dev *rdev = regulator->rdev;
1459 mutex_lock(&rdev->mutex);
1462 if (!rdev->desc->ops->set_current_limit) {
1467 /* constraints check */
1468 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1472 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1474 mutex_unlock(&rdev->mutex);
1477 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1479 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1483 mutex_lock(&rdev->mutex);
1486 if (!rdev->desc->ops->get_current_limit) {
1491 ret = rdev->desc->ops->get_current_limit(rdev);
1493 mutex_unlock(&rdev->mutex);
1498 * regulator_get_current_limit - get regulator output current
1499 * @regulator: regulator source
1501 * This returns the current supplied by the specified current sink in uA.
1503 * NOTE: If the regulator is disabled it will return the current value. This
1504 * function should not be used to determine regulator state.
1506 int regulator_get_current_limit(struct regulator *regulator)
1508 return _regulator_get_current_limit(regulator->rdev);
1510 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1513 * regulator_set_mode - set regulator operating mode
1514 * @regulator: regulator source
1515 * @mode: operating mode - one of the REGULATOR_MODE constants
1517 * Set regulator operating mode to increase regulator efficiency or improve
1518 * regulation performance.
1520 * NOTE: Regulator system constraints must be set for this regulator before
1521 * calling this function otherwise this call will fail.
1523 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1525 struct regulator_dev *rdev = regulator->rdev;
1528 mutex_lock(&rdev->mutex);
1531 if (!rdev->desc->ops->set_mode) {
1536 /* constraints check */
1537 ret = regulator_check_mode(rdev, mode);
1541 ret = rdev->desc->ops->set_mode(rdev, mode);
1543 mutex_unlock(&rdev->mutex);
1546 EXPORT_SYMBOL_GPL(regulator_set_mode);
1548 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1552 mutex_lock(&rdev->mutex);
1555 if (!rdev->desc->ops->get_mode) {
1560 ret = rdev->desc->ops->get_mode(rdev);
1562 mutex_unlock(&rdev->mutex);
1567 * regulator_get_mode - get regulator operating mode
1568 * @regulator: regulator source
1570 * Get the current regulator operating mode.
1572 unsigned int regulator_get_mode(struct regulator *regulator)
1574 return _regulator_get_mode(regulator->rdev);
1576 EXPORT_SYMBOL_GPL(regulator_get_mode);
1579 * regulator_set_optimum_mode - set regulator optimum operating mode
1580 * @regulator: regulator source
1581 * @uA_load: load current
1583 * Notifies the regulator core of a new device load. This is then used by
1584 * DRMS (if enabled by constraints) to set the most efficient regulator
1585 * operating mode for the new regulator loading.
1587 * Consumer devices notify their supply regulator of the maximum power
1588 * they will require (can be taken from device datasheet in the power
1589 * consumption tables) when they change operational status and hence power
1590 * state. Examples of operational state changes that can affect power
1591 * consumption are :-
1593 * o Device is opened / closed.
1594 * o Device I/O is about to begin or has just finished.
1595 * o Device is idling in between work.
1597 * This information is also exported via sysfs to userspace.
1599 * DRMS will sum the total requested load on the regulator and change
1600 * to the most efficient operating mode if platform constraints allow.
1602 * Returns the new regulator mode or error.
1604 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1606 struct regulator_dev *rdev = regulator->rdev;
1607 struct regulator *consumer;
1608 int ret, output_uV, input_uV, total_uA_load = 0;
1611 mutex_lock(&rdev->mutex);
1613 regulator->uA_load = uA_load;
1614 ret = regulator_check_drms(rdev);
1620 if (!rdev->desc->ops->get_optimum_mode)
1623 /* get output voltage */
1624 output_uV = rdev->desc->ops->get_voltage(rdev);
1625 if (output_uV <= 0) {
1626 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1627 __func__, rdev->desc->name);
1631 /* get input voltage */
1632 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1633 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1635 input_uV = rdev->constraints->input_uV;
1636 if (input_uV <= 0) {
1637 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1638 __func__, rdev->desc->name);
1642 /* calc total requested load for this regulator */
1643 list_for_each_entry(consumer, &rdev->consumer_list, list)
1644 total_uA_load += consumer->uA_load;
1646 mode = rdev->desc->ops->get_optimum_mode(rdev,
1647 input_uV, output_uV,
1649 ret = regulator_check_mode(rdev, mode);
1651 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1652 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1653 total_uA_load, input_uV, output_uV);
1657 ret = rdev->desc->ops->set_mode(rdev, mode);
1659 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1660 __func__, mode, rdev->desc->name);
1665 mutex_unlock(&rdev->mutex);
1668 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1671 * regulator_register_notifier - register regulator event notifier
1672 * @regulator: regulator source
1673 * @nb: notifier block
1675 * Register notifier block to receive regulator events.
1677 int regulator_register_notifier(struct regulator *regulator,
1678 struct notifier_block *nb)
1680 return blocking_notifier_chain_register(®ulator->rdev->notifier,
1683 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1686 * regulator_unregister_notifier - unregister regulator event notifier
1687 * @regulator: regulator source
1688 * @nb: notifier block
1690 * Unregister regulator event notifier block.
1692 int regulator_unregister_notifier(struct regulator *regulator,
1693 struct notifier_block *nb)
1695 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
1698 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1700 /* notify regulator consumers and downstream regulator consumers */
1701 static void _notifier_call_chain(struct regulator_dev *rdev,
1702 unsigned long event, void *data)
1704 struct regulator_dev *_rdev;
1706 /* call rdev chain first */
1707 mutex_lock(&rdev->mutex);
1708 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1709 mutex_unlock(&rdev->mutex);
1711 /* now notify regulator we supply */
1712 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1713 _notifier_call_chain(_rdev, event, data);
1717 * regulator_bulk_get - get multiple regulator consumers
1719 * @dev: Device to supply
1720 * @num_consumers: Number of consumers to register
1721 * @consumers: Configuration of consumers; clients are stored here.
1723 * @return 0 on success, an errno on failure.
1725 * This helper function allows drivers to get several regulator
1726 * consumers in one operation. If any of the regulators cannot be
1727 * acquired then any regulators that were allocated will be freed
1728 * before returning to the caller.
1730 int regulator_bulk_get(struct device *dev, int num_consumers,
1731 struct regulator_bulk_data *consumers)
1736 for (i = 0; i < num_consumers; i++)
1737 consumers[i].consumer = NULL;
1739 for (i = 0; i < num_consumers; i++) {
1740 consumers[i].consumer = regulator_get(dev,
1741 consumers[i].supply);
1742 if (IS_ERR(consumers[i].consumer)) {
1743 dev_err(dev, "Failed to get supply '%s'\n",
1744 consumers[i].supply);
1745 ret = PTR_ERR(consumers[i].consumer);
1746 consumers[i].consumer = NULL;
1754 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1755 regulator_put(consumers[i].consumer);
1759 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1762 * regulator_bulk_enable - enable multiple regulator consumers
1764 * @num_consumers: Number of consumers
1765 * @consumers: Consumer data; clients are stored here.
1766 * @return 0 on success, an errno on failure
1768 * This convenience API allows consumers to enable multiple regulator
1769 * clients in a single API call. If any consumers cannot be enabled
1770 * then any others that were enabled will be disabled again prior to
1773 int regulator_bulk_enable(int num_consumers,
1774 struct regulator_bulk_data *consumers)
1779 for (i = 0; i < num_consumers; i++) {
1780 ret = regulator_enable(consumers[i].consumer);
1788 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1789 for (i = 0; i < num_consumers; i++)
1790 regulator_disable(consumers[i].consumer);
1794 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1797 * regulator_bulk_disable - disable multiple regulator consumers
1799 * @num_consumers: Number of consumers
1800 * @consumers: Consumer data; clients are stored here.
1801 * @return 0 on success, an errno on failure
1803 * This convenience API allows consumers to disable multiple regulator
1804 * clients in a single API call. If any consumers cannot be enabled
1805 * then any others that were disabled will be disabled again prior to
1808 int regulator_bulk_disable(int num_consumers,
1809 struct regulator_bulk_data *consumers)
1814 for (i = 0; i < num_consumers; i++) {
1815 ret = regulator_disable(consumers[i].consumer);
1823 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1824 for (i = 0; i < num_consumers; i++)
1825 regulator_enable(consumers[i].consumer);
1829 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1832 * regulator_bulk_free - free multiple regulator consumers
1834 * @num_consumers: Number of consumers
1835 * @consumers: Consumer data; clients are stored here.
1837 * This convenience API allows consumers to free multiple regulator
1838 * clients in a single API call.
1840 void regulator_bulk_free(int num_consumers,
1841 struct regulator_bulk_data *consumers)
1845 for (i = 0; i < num_consumers; i++) {
1846 regulator_put(consumers[i].consumer);
1847 consumers[i].consumer = NULL;
1850 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1853 * regulator_notifier_call_chain - call regulator event notifier
1854 * @rdev: regulator source
1855 * @event: notifier block
1856 * @data: callback-specific data.
1858 * Called by regulator drivers to notify clients a regulator event has
1859 * occurred. We also notify regulator clients downstream.
1861 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1862 unsigned long event, void *data)
1864 _notifier_call_chain(rdev, event, data);
1868 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1871 * To avoid cluttering sysfs (and memory) with useless state, only
1872 * create attributes that can be meaningfully displayed.
1874 static int add_regulator_attributes(struct regulator_dev *rdev)
1876 struct device *dev = &rdev->dev;
1877 struct regulator_ops *ops = rdev->desc->ops;
1880 /* some attributes need specific methods to be displayed */
1881 if (ops->get_voltage) {
1882 status = device_create_file(dev, &dev_attr_microvolts);
1886 if (ops->get_current_limit) {
1887 status = device_create_file(dev, &dev_attr_microamps);
1891 if (ops->get_mode) {
1892 status = device_create_file(dev, &dev_attr_opmode);
1896 if (ops->is_enabled) {
1897 status = device_create_file(dev, &dev_attr_state);
1901 if (ops->get_status) {
1902 status = device_create_file(dev, &dev_attr_status);
1907 /* some attributes are type-specific */
1908 if (rdev->desc->type == REGULATOR_CURRENT) {
1909 status = device_create_file(dev, &dev_attr_requested_microamps);
1914 /* all the other attributes exist to support constraints;
1915 * don't show them if there are no constraints, or if the
1916 * relevant supporting methods are missing.
1918 if (!rdev->constraints)
1921 /* constraints need specific supporting methods */
1922 if (ops->set_voltage) {
1923 status = device_create_file(dev, &dev_attr_min_microvolts);
1926 status = device_create_file(dev, &dev_attr_max_microvolts);
1930 if (ops->set_current_limit) {
1931 status = device_create_file(dev, &dev_attr_min_microamps);
1934 status = device_create_file(dev, &dev_attr_max_microamps);
1939 /* suspend mode constraints need multiple supporting methods */
1940 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1943 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1946 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1949 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1953 if (ops->set_suspend_voltage) {
1954 status = device_create_file(dev,
1955 &dev_attr_suspend_standby_microvolts);
1958 status = device_create_file(dev,
1959 &dev_attr_suspend_mem_microvolts);
1962 status = device_create_file(dev,
1963 &dev_attr_suspend_disk_microvolts);
1968 if (ops->set_suspend_mode) {
1969 status = device_create_file(dev,
1970 &dev_attr_suspend_standby_mode);
1973 status = device_create_file(dev,
1974 &dev_attr_suspend_mem_mode);
1977 status = device_create_file(dev,
1978 &dev_attr_suspend_disk_mode);
1987 * regulator_register - register regulator
1988 * @regulator_desc: regulator to register
1989 * @dev: struct device for the regulator
1990 * @driver_data: private regulator data
1992 * Called by regulator drivers to register a regulator.
1993 * Returns 0 on success.
1995 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1996 struct device *dev, void *driver_data)
1998 static atomic_t regulator_no = ATOMIC_INIT(0);
1999 struct regulator_dev *rdev;
2000 struct regulator_init_data *init_data = dev->platform_data;
2003 if (regulator_desc == NULL)
2004 return ERR_PTR(-EINVAL);
2006 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2007 return ERR_PTR(-EINVAL);
2009 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
2010 !regulator_desc->type == REGULATOR_CURRENT)
2011 return ERR_PTR(-EINVAL);
2014 return ERR_PTR(-EINVAL);
2016 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2018 return ERR_PTR(-ENOMEM);
2020 mutex_lock(®ulator_list_mutex);
2022 mutex_init(&rdev->mutex);
2023 rdev->reg_data = driver_data;
2024 rdev->owner = regulator_desc->owner;
2025 rdev->desc = regulator_desc;
2026 INIT_LIST_HEAD(&rdev->consumer_list);
2027 INIT_LIST_HEAD(&rdev->supply_list);
2028 INIT_LIST_HEAD(&rdev->list);
2029 INIT_LIST_HEAD(&rdev->slist);
2030 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2032 /* preform any regulator specific init */
2033 if (init_data->regulator_init) {
2034 ret = init_data->regulator_init(rdev->reg_data);
2039 /* register with sysfs */
2040 rdev->dev.class = ®ulator_class;
2041 rdev->dev.parent = dev;
2042 dev_set_name(&rdev->dev, "regulator.%d",
2043 atomic_inc_return(®ulator_no) - 1);
2044 ret = device_register(&rdev->dev);
2048 dev_set_drvdata(&rdev->dev, rdev);
2050 /* set regulator constraints */
2051 ret = set_machine_constraints(rdev, &init_data->constraints);
2055 /* add attributes supported by this regulator */
2056 ret = add_regulator_attributes(rdev);
2060 /* set supply regulator if it exists */
2061 if (init_data->supply_regulator_dev) {
2062 ret = set_supply(rdev,
2063 dev_get_drvdata(init_data->supply_regulator_dev));
2068 /* add consumers devices */
2069 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2070 ret = set_consumer_device_supply(rdev,
2071 init_data->consumer_supplies[i].dev,
2072 init_data->consumer_supplies[i].supply);
2074 for (--i; i >= 0; i--)
2075 unset_consumer_device_supply(rdev,
2076 init_data->consumer_supplies[i].dev);
2081 list_add(&rdev->list, ®ulator_list);
2083 mutex_unlock(®ulator_list_mutex);
2087 device_unregister(&rdev->dev);
2090 rdev = ERR_PTR(ret);
2093 EXPORT_SYMBOL_GPL(regulator_register);
2096 * regulator_unregister - unregister regulator
2097 * @rdev: regulator to unregister
2099 * Called by regulator drivers to unregister a regulator.
2101 void regulator_unregister(struct regulator_dev *rdev)
2106 mutex_lock(®ulator_list_mutex);
2107 list_del(&rdev->list);
2109 sysfs_remove_link(&rdev->dev.kobj, "supply");
2110 device_unregister(&rdev->dev);
2111 mutex_unlock(®ulator_list_mutex);
2113 EXPORT_SYMBOL_GPL(regulator_unregister);
2116 * regulator_suspend_prepare - prepare regulators for system wide suspend
2117 * @state: system suspend state
2119 * Configure each regulator with it's suspend operating parameters for state.
2120 * This will usually be called by machine suspend code prior to supending.
2122 int regulator_suspend_prepare(suspend_state_t state)
2124 struct regulator_dev *rdev;
2127 /* ON is handled by regulator active state */
2128 if (state == PM_SUSPEND_ON)
2131 mutex_lock(®ulator_list_mutex);
2132 list_for_each_entry(rdev, ®ulator_list, list) {
2134 mutex_lock(&rdev->mutex);
2135 ret = suspend_prepare(rdev, state);
2136 mutex_unlock(&rdev->mutex);
2139 printk(KERN_ERR "%s: failed to prepare %s\n",
2140 __func__, rdev->desc->name);
2145 mutex_unlock(®ulator_list_mutex);
2148 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2151 * rdev_get_drvdata - get rdev regulator driver data
2154 * Get rdev regulator driver private data. This call can be used in the
2155 * regulator driver context.
2157 void *rdev_get_drvdata(struct regulator_dev *rdev)
2159 return rdev->reg_data;
2161 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2164 * regulator_get_drvdata - get regulator driver data
2165 * @regulator: regulator
2167 * Get regulator driver private data. This call can be used in the consumer
2168 * driver context when non API regulator specific functions need to be called.
2170 void *regulator_get_drvdata(struct regulator *regulator)
2172 return regulator->rdev->reg_data;
2174 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2177 * regulator_set_drvdata - set regulator driver data
2178 * @regulator: regulator
2181 void regulator_set_drvdata(struct regulator *regulator, void *data)
2183 regulator->rdev->reg_data = data;
2185 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2188 * regulator_get_id - get regulator ID
2191 int rdev_get_id(struct regulator_dev *rdev)
2193 return rdev->desc->id;
2195 EXPORT_SYMBOL_GPL(rdev_get_id);
2197 struct device *rdev_get_dev(struct regulator_dev *rdev)
2201 EXPORT_SYMBOL_GPL(rdev_get_dev);
2203 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2205 return reg_init_data->driver_data;
2207 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2209 static int __init regulator_init(void)
2211 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2212 return class_register(®ulator_class);
2215 /* init early to allow our consumers to complete system booting */
2216 core_initcall(regulator_init);