regulator: Clean up kerneldoc warnings
[linux-2.6/libata-dev.git] / drivers / regulator / core.c
blobea12c68c327fad63eccd627c4ea07dd77569612e
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);
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 */
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; /* count of client enables */
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 switch (mode) {
178 case REGULATOR_MODE_FAST:
179 case REGULATOR_MODE_NORMAL:
180 case REGULATOR_MODE_IDLE:
181 case REGULATOR_MODE_STANDBY:
182 break;
183 default:
184 return -EINVAL;
187 if (!rdev->constraints) {
188 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189 rdev->desc->name);
190 return -ENODEV;
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);
195 return -EPERM;
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);
200 return -EINVAL;
202 return 0;
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__,
210 rdev->desc->name);
211 return -ENODEV;
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);
216 return -EPERM;
218 return 0;
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)
228 return 0;
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);
237 ssize_t ret;
239 mutex_lock(&rdev->mutex);
240 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241 mutex_unlock(&rdev->mutex);
243 return ret;
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);
260 const char *name;
262 if (rdev->constraints->name)
263 name = rdev->constraints->name;
264 else if (rdev->desc->name)
265 name = rdev->desc->name;
266 else
267 name = "";
269 return sprintf(buf, "%s\n", name);
272 static ssize_t regulator_print_opmode(char *buf, int mode)
274 switch (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)
298 if (state > 0)
299 return sprintf(buf, "enabled\n");
300 else if (state == 0)
301 return sprintf(buf, "disabled\n");
302 else
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_min_uA_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_uA);
325 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
327 static ssize_t regulator_max_uA_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
330 struct regulator_dev *rdev = dev_get_drvdata(dev);
332 if (!rdev->constraints)
333 return sprintf(buf, "constraint not defined\n");
335 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
337 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
339 static ssize_t regulator_min_uV_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
342 struct regulator_dev *rdev = dev_get_drvdata(dev);
344 if (!rdev->constraints)
345 return sprintf(buf, "constraint not defined\n");
347 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
349 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
351 static ssize_t regulator_max_uV_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
356 if (!rdev->constraints)
357 return sprintf(buf, "constraint not defined\n");
359 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
361 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
363 static ssize_t regulator_total_uA_show(struct device *dev,
364 struct device_attribute *attr, char *buf)
366 struct regulator_dev *rdev = dev_get_drvdata(dev);
367 struct regulator *regulator;
368 int uA = 0;
370 mutex_lock(&rdev->mutex);
371 list_for_each_entry(regulator, &rdev->consumer_list, list)
372 uA += regulator->uA_load;
373 mutex_unlock(&rdev->mutex);
374 return sprintf(buf, "%d\n", uA);
376 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
378 static ssize_t regulator_num_users_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 return sprintf(buf, "%d\n", rdev->use_count);
385 static ssize_t regulator_type_show(struct device *dev,
386 struct device_attribute *attr, char *buf)
388 struct regulator_dev *rdev = dev_get_drvdata(dev);
390 switch (rdev->desc->type) {
391 case REGULATOR_VOLTAGE:
392 return sprintf(buf, "voltage\n");
393 case REGULATOR_CURRENT:
394 return sprintf(buf, "current\n");
396 return sprintf(buf, "unknown\n");
399 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct regulator_dev *rdev = dev_get_drvdata(dev);
404 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
406 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
407 regulator_suspend_mem_uV_show, NULL);
409 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
412 struct regulator_dev *rdev = dev_get_drvdata(dev);
414 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
416 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
417 regulator_suspend_disk_uV_show, NULL);
419 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
420 struct device_attribute *attr, char *buf)
422 struct regulator_dev *rdev = dev_get_drvdata(dev);
424 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
426 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
427 regulator_suspend_standby_uV_show, NULL);
429 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 return regulator_print_opmode(buf,
435 rdev->constraints->state_mem.mode);
437 static DEVICE_ATTR(suspend_mem_mode, 0444,
438 regulator_suspend_mem_mode_show, NULL);
440 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
443 struct regulator_dev *rdev = dev_get_drvdata(dev);
445 return regulator_print_opmode(buf,
446 rdev->constraints->state_disk.mode);
448 static DEVICE_ATTR(suspend_disk_mode, 0444,
449 regulator_suspend_disk_mode_show, NULL);
451 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 return regulator_print_opmode(buf,
457 rdev->constraints->state_standby.mode);
459 static DEVICE_ATTR(suspend_standby_mode, 0444,
460 regulator_suspend_standby_mode_show, NULL);
462 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 return regulator_print_state(buf,
468 rdev->constraints->state_mem.enabled);
470 static DEVICE_ATTR(suspend_mem_state, 0444,
471 regulator_suspend_mem_state_show, NULL);
473 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 return regulator_print_state(buf,
479 rdev->constraints->state_disk.enabled);
481 static DEVICE_ATTR(suspend_disk_state, 0444,
482 regulator_suspend_disk_state_show, NULL);
484 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
485 struct device_attribute *attr, char *buf)
487 struct regulator_dev *rdev = dev_get_drvdata(dev);
489 return regulator_print_state(buf,
490 rdev->constraints->state_standby.enabled);
492 static DEVICE_ATTR(suspend_standby_state, 0444,
493 regulator_suspend_standby_state_show, NULL);
497 * These are the only attributes are present for all regulators.
498 * Other attributes are a function of regulator functionality.
500 static struct device_attribute regulator_dev_attrs[] = {
501 __ATTR(name, 0444, regulator_name_show, NULL),
502 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
503 __ATTR(type, 0444, regulator_type_show, NULL),
504 __ATTR_NULL,
507 static void regulator_dev_release(struct device *dev)
509 struct regulator_dev *rdev = dev_get_drvdata(dev);
510 kfree(rdev);
513 static struct class regulator_class = {
514 .name = "regulator",
515 .dev_release = regulator_dev_release,
516 .dev_attrs = regulator_dev_attrs,
519 /* Calculate the new optimum regulator operating mode based on the new total
520 * consumer load. All locks held by caller */
521 static void drms_uA_update(struct regulator_dev *rdev)
523 struct regulator *sibling;
524 int current_uA = 0, output_uV, input_uV, err;
525 unsigned int mode;
527 err = regulator_check_drms(rdev);
528 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
529 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
530 return;
532 /* get output voltage */
533 output_uV = rdev->desc->ops->get_voltage(rdev);
534 if (output_uV <= 0)
535 return;
537 /* get input voltage */
538 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
539 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
540 else
541 input_uV = rdev->constraints->input_uV;
542 if (input_uV <= 0)
543 return;
545 /* calc total requested load */
546 list_for_each_entry(sibling, &rdev->consumer_list, list)
547 current_uA += sibling->uA_load;
549 /* now get the optimum mode for our new total regulator load */
550 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
551 output_uV, current_uA);
553 /* check the new mode is allowed */
554 err = regulator_check_mode(rdev, mode);
555 if (err == 0)
556 rdev->desc->ops->set_mode(rdev, mode);
559 static int suspend_set_state(struct regulator_dev *rdev,
560 struct regulator_state *rstate)
562 int ret = 0;
564 /* enable & disable are mandatory for suspend control */
565 if (!rdev->desc->ops->set_suspend_enable ||
566 !rdev->desc->ops->set_suspend_disable) {
567 printk(KERN_ERR "%s: no way to set suspend state\n",
568 __func__);
569 return -EINVAL;
572 if (rstate->enabled)
573 ret = rdev->desc->ops->set_suspend_enable(rdev);
574 else
575 ret = rdev->desc->ops->set_suspend_disable(rdev);
576 if (ret < 0) {
577 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
578 return ret;
581 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
582 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
583 if (ret < 0) {
584 printk(KERN_ERR "%s: failed to set voltage\n",
585 __func__);
586 return ret;
590 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
591 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
592 if (ret < 0) {
593 printk(KERN_ERR "%s: failed to set mode\n", __func__);
594 return ret;
597 return ret;
600 /* locks held by caller */
601 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
603 if (!rdev->constraints)
604 return -EINVAL;
606 switch (state) {
607 case PM_SUSPEND_STANDBY:
608 return suspend_set_state(rdev,
609 &rdev->constraints->state_standby);
610 case PM_SUSPEND_MEM:
611 return suspend_set_state(rdev,
612 &rdev->constraints->state_mem);
613 case PM_SUSPEND_MAX:
614 return suspend_set_state(rdev,
615 &rdev->constraints->state_disk);
616 default:
617 return -EINVAL;
621 static void print_constraints(struct regulator_dev *rdev)
623 struct regulation_constraints *constraints = rdev->constraints;
624 char buf[80];
625 int count;
627 if (rdev->desc->type == REGULATOR_VOLTAGE) {
628 if (constraints->min_uV == constraints->max_uV)
629 count = sprintf(buf, "%d mV ",
630 constraints->min_uV / 1000);
631 else
632 count = sprintf(buf, "%d <--> %d mV ",
633 constraints->min_uV / 1000,
634 constraints->max_uV / 1000);
635 } else {
636 if (constraints->min_uA == constraints->max_uA)
637 count = sprintf(buf, "%d mA ",
638 constraints->min_uA / 1000);
639 else
640 count = sprintf(buf, "%d <--> %d mA ",
641 constraints->min_uA / 1000,
642 constraints->max_uA / 1000);
644 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
645 count += sprintf(buf + count, "fast ");
646 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
647 count += sprintf(buf + count, "normal ");
648 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
649 count += sprintf(buf + count, "idle ");
650 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
651 count += sprintf(buf + count, "standby");
653 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
657 * set_machine_constraints - sets regulator constraints
658 * @rdev: regulator source
660 * Allows platform initialisation code to define and constrain
661 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
662 * Constraints *must* be set by platform code in order for some
663 * regulator operations to proceed i.e. set_voltage, set_current_limit,
664 * set_mode.
666 static int set_machine_constraints(struct regulator_dev *rdev,
667 struct regulation_constraints *constraints)
669 int ret = 0;
670 const char *name;
671 struct regulator_ops *ops = rdev->desc->ops;
673 if (constraints->name)
674 name = constraints->name;
675 else if (rdev->desc->name)
676 name = rdev->desc->name;
677 else
678 name = "regulator";
680 rdev->constraints = constraints;
682 /* do we need to apply the constraint voltage */
683 if (rdev->constraints->apply_uV &&
684 rdev->constraints->min_uV == rdev->constraints->max_uV &&
685 ops->set_voltage) {
686 ret = ops->set_voltage(rdev,
687 rdev->constraints->min_uV, rdev->constraints->max_uV);
688 if (ret < 0) {
689 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
690 __func__,
691 rdev->constraints->min_uV, name);
692 rdev->constraints = NULL;
693 goto out;
697 /* are we enabled at boot time by firmware / bootloader */
698 if (rdev->constraints->boot_on)
699 rdev->use_count = 1;
701 /* do we need to setup our suspend state */
702 if (constraints->initial_state) {
703 ret = suspend_prepare(rdev, constraints->initial_state);
704 if (ret < 0) {
705 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
706 __func__, name);
707 rdev->constraints = NULL;
708 goto out;
712 /* if always_on is set then turn the regulator on if it's not
713 * already on. */
714 if (constraints->always_on && ops->enable &&
715 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
716 (!ops->is_enabled && !constraints->boot_on))) {
717 ret = ops->enable(rdev);
718 if (ret < 0) {
719 printk(KERN_ERR "%s: failed to enable %s\n",
720 __func__, name);
721 rdev->constraints = NULL;
722 goto out;
726 print_constraints(rdev);
727 out:
728 return ret;
732 * set_supply - set regulator supply regulator
733 * @rdev: regulator name
734 * @supply_rdev: supply regulator name
736 * Called by platform initialisation code to set the supply regulator for this
737 * regulator. This ensures that a regulators supply will also be enabled by the
738 * core if it's child is enabled.
740 static int set_supply(struct regulator_dev *rdev,
741 struct regulator_dev *supply_rdev)
743 int err;
745 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
746 "supply");
747 if (err) {
748 printk(KERN_ERR
749 "%s: could not add device link %s err %d\n",
750 __func__, supply_rdev->dev.kobj.name, err);
751 goto out;
753 rdev->supply = supply_rdev;
754 list_add(&rdev->slist, &supply_rdev->supply_list);
755 out:
756 return err;
760 * set_consumer_device_supply: Bind a regulator to a symbolic supply
761 * @rdev: regulator source
762 * @consumer_dev: device the supply applies to
763 * @supply: symbolic name for supply
765 * Allows platform initialisation code to map physical regulator
766 * sources to symbolic names for supplies for use by devices. Devices
767 * should use these symbolic names to request regulators, avoiding the
768 * need to provide board-specific regulator names as platform data.
770 static int set_consumer_device_supply(struct regulator_dev *rdev,
771 struct device *consumer_dev, const char *supply)
773 struct regulator_map *node;
775 if (supply == NULL)
776 return -EINVAL;
778 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
779 if (node == NULL)
780 return -ENOMEM;
782 node->regulator = rdev;
783 node->dev = consumer_dev;
784 node->supply = supply;
786 list_add(&node->list, &regulator_map_list);
787 return 0;
790 static void unset_consumer_device_supply(struct regulator_dev *rdev,
791 struct device *consumer_dev)
793 struct regulator_map *node, *n;
795 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
796 if (rdev == node->regulator &&
797 consumer_dev == node->dev) {
798 list_del(&node->list);
799 kfree(node);
800 return;
805 #define REG_STR_SIZE 32
807 static struct regulator *create_regulator(struct regulator_dev *rdev,
808 struct device *dev,
809 const char *supply_name)
811 struct regulator *regulator;
812 char buf[REG_STR_SIZE];
813 int err, size;
815 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
816 if (regulator == NULL)
817 return NULL;
819 mutex_lock(&rdev->mutex);
820 regulator->rdev = rdev;
821 list_add(&regulator->list, &rdev->consumer_list);
823 if (dev) {
824 /* create a 'requested_microamps_name' sysfs entry */
825 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
826 supply_name);
827 if (size >= REG_STR_SIZE)
828 goto overflow_err;
830 regulator->dev = dev;
831 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
832 if (regulator->dev_attr.attr.name == NULL)
833 goto attr_name_err;
835 regulator->dev_attr.attr.owner = THIS_MODULE;
836 regulator->dev_attr.attr.mode = 0444;
837 regulator->dev_attr.show = device_requested_uA_show;
838 err = device_create_file(dev, &regulator->dev_attr);
839 if (err < 0) {
840 printk(KERN_WARNING "%s: could not add regulator_dev"
841 " load sysfs\n", __func__);
842 goto attr_name_err;
845 /* also add a link to the device sysfs entry */
846 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
847 dev->kobj.name, supply_name);
848 if (size >= REG_STR_SIZE)
849 goto attr_err;
851 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
852 if (regulator->supply_name == NULL)
853 goto attr_err;
855 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
856 buf);
857 if (err) {
858 printk(KERN_WARNING
859 "%s: could not add device link %s err %d\n",
860 __func__, dev->kobj.name, err);
861 device_remove_file(dev, &regulator->dev_attr);
862 goto link_name_err;
865 mutex_unlock(&rdev->mutex);
866 return regulator;
867 link_name_err:
868 kfree(regulator->supply_name);
869 attr_err:
870 device_remove_file(regulator->dev, &regulator->dev_attr);
871 attr_name_err:
872 kfree(regulator->dev_attr.attr.name);
873 overflow_err:
874 list_del(&regulator->list);
875 kfree(regulator);
876 mutex_unlock(&rdev->mutex);
877 return NULL;
881 * regulator_get - lookup and obtain a reference to a regulator.
882 * @dev: device for regulator "consumer"
883 * @id: Supply name or regulator ID.
885 * Returns a struct regulator corresponding to the regulator producer,
886 * or IS_ERR() condition containing errno. Use of supply names
887 * configured via regulator_set_device_supply() is strongly
888 * encouraged.
890 struct regulator *regulator_get(struct device *dev, const char *id)
892 struct regulator_dev *rdev;
893 struct regulator_map *map;
894 struct regulator *regulator = ERR_PTR(-ENODEV);
896 if (id == NULL) {
897 printk(KERN_ERR "regulator: get() with no identifier\n");
898 return regulator;
901 mutex_lock(&regulator_list_mutex);
903 list_for_each_entry(map, &regulator_map_list, list) {
904 if (dev == map->dev &&
905 strcmp(map->supply, id) == 0) {
906 rdev = map->regulator;
907 goto found;
910 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
911 id);
912 mutex_unlock(&regulator_list_mutex);
913 return regulator;
915 found:
916 if (!try_module_get(rdev->owner))
917 goto out;
919 regulator = create_regulator(rdev, dev, id);
920 if (regulator == NULL) {
921 regulator = ERR_PTR(-ENOMEM);
922 module_put(rdev->owner);
925 out:
926 mutex_unlock(&regulator_list_mutex);
927 return regulator;
929 EXPORT_SYMBOL_GPL(regulator_get);
932 * regulator_put - "free" the regulator source
933 * @regulator: regulator source
935 * Note: drivers must ensure that all regulator_enable calls made on this
936 * regulator source are balanced by regulator_disable calls prior to calling
937 * this function.
939 void regulator_put(struct regulator *regulator)
941 struct regulator_dev *rdev;
943 if (regulator == NULL || IS_ERR(regulator))
944 return;
946 mutex_lock(&regulator_list_mutex);
947 rdev = regulator->rdev;
949 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
950 regulator->supply_name))
951 _regulator_disable(rdev);
953 /* remove any sysfs entries */
954 if (regulator->dev) {
955 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
956 kfree(regulator->supply_name);
957 device_remove_file(regulator->dev, &regulator->dev_attr);
958 kfree(regulator->dev_attr.attr.name);
960 list_del(&regulator->list);
961 kfree(regulator);
963 module_put(rdev->owner);
964 mutex_unlock(&regulator_list_mutex);
966 EXPORT_SYMBOL_GPL(regulator_put);
968 /* locks held by regulator_enable() */
969 static int _regulator_enable(struct regulator_dev *rdev)
971 int ret = -EINVAL;
973 if (!rdev->constraints) {
974 printk(KERN_ERR "%s: %s has no constraints\n",
975 __func__, rdev->desc->name);
976 return ret;
979 /* do we need to enable the supply regulator first */
980 if (rdev->supply) {
981 ret = _regulator_enable(rdev->supply);
982 if (ret < 0) {
983 printk(KERN_ERR "%s: failed to enable %s: %d\n",
984 __func__, rdev->desc->name, ret);
985 return ret;
989 /* check voltage and requested load before enabling */
990 if (rdev->desc->ops->enable) {
992 if (rdev->constraints &&
993 (rdev->constraints->valid_ops_mask &
994 REGULATOR_CHANGE_DRMS))
995 drms_uA_update(rdev);
997 ret = rdev->desc->ops->enable(rdev);
998 if (ret < 0) {
999 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1000 __func__, rdev->desc->name, ret);
1001 return ret;
1003 rdev->use_count++;
1004 return ret;
1007 return ret;
1011 * regulator_enable - enable regulator output
1012 * @regulator: regulator source
1014 * Enable the regulator output at the predefined voltage or current value.
1015 * NOTE: the output value can be set by other drivers, boot loader or may be
1016 * hardwired in the regulator. Calls to regulator_enable() must be balanced
1017 * with calls to regulator_disable().
1019 int regulator_enable(struct regulator *regulator)
1021 struct regulator_dev *rdev = regulator->rdev;
1022 int ret = 0;
1024 mutex_lock(&rdev->mutex);
1025 if (regulator->enabled == 0)
1026 ret = _regulator_enable(rdev);
1027 else if (regulator->enabled < 0)
1028 ret = -EIO;
1029 if (ret == 0)
1030 regulator->enabled++;
1031 mutex_unlock(&rdev->mutex);
1032 return ret;
1034 EXPORT_SYMBOL_GPL(regulator_enable);
1036 /* locks held by regulator_disable() */
1037 static int _regulator_disable(struct regulator_dev *rdev)
1039 int ret = 0;
1041 /* are we the last user and permitted to disable ? */
1042 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1044 /* we are last user */
1045 if (rdev->desc->ops->disable) {
1046 ret = rdev->desc->ops->disable(rdev);
1047 if (ret < 0) {
1048 printk(KERN_ERR "%s: failed to disable %s\n",
1049 __func__, rdev->desc->name);
1050 return ret;
1054 /* decrease our supplies ref count and disable if required */
1055 if (rdev->supply)
1056 _regulator_disable(rdev->supply);
1058 rdev->use_count = 0;
1059 } else if (rdev->use_count > 1) {
1061 if (rdev->constraints &&
1062 (rdev->constraints->valid_ops_mask &
1063 REGULATOR_CHANGE_DRMS))
1064 drms_uA_update(rdev);
1066 rdev->use_count--;
1068 return ret;
1072 * regulator_disable - disable regulator output
1073 * @regulator: regulator source
1075 * Disable the regulator output voltage or current.
1077 * NOTE: this will only disable the regulator output if no other consumer
1078 * devices have it enabled. Calls to regulator_enable() must be balanced with
1079 * calls to regulator_disable().
1081 int regulator_disable(struct regulator *regulator)
1083 struct regulator_dev *rdev = regulator->rdev;
1084 int ret = 0;
1086 mutex_lock(&rdev->mutex);
1087 if (regulator->enabled == 1) {
1088 ret = _regulator_disable(rdev);
1089 if (ret == 0)
1090 regulator->uA_load = 0;
1091 } else if (WARN(regulator->enabled <= 0,
1092 "unbalanced disables for supply %s\n",
1093 regulator->supply_name))
1094 ret = -EIO;
1095 if (ret == 0)
1096 regulator->enabled--;
1097 mutex_unlock(&rdev->mutex);
1098 return ret;
1100 EXPORT_SYMBOL_GPL(regulator_disable);
1102 /* locks held by regulator_force_disable() */
1103 static int _regulator_force_disable(struct regulator_dev *rdev)
1105 int ret = 0;
1107 /* force disable */
1108 if (rdev->desc->ops->disable) {
1109 /* ah well, who wants to live forever... */
1110 ret = rdev->desc->ops->disable(rdev);
1111 if (ret < 0) {
1112 printk(KERN_ERR "%s: failed to force disable %s\n",
1113 __func__, rdev->desc->name);
1114 return ret;
1116 /* notify other consumers that power has been forced off */
1117 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1118 NULL);
1121 /* decrease our supplies ref count and disable if required */
1122 if (rdev->supply)
1123 _regulator_disable(rdev->supply);
1125 rdev->use_count = 0;
1126 return ret;
1130 * regulator_force_disable - force disable regulator output
1131 * @regulator: regulator source
1133 * Forcibly disable the regulator output voltage or current.
1134 * NOTE: this *will* disable the regulator output even if other consumer
1135 * devices have it enabled. This should be used for situations when device
1136 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1138 int regulator_force_disable(struct regulator *regulator)
1140 int ret;
1142 mutex_lock(&regulator->rdev->mutex);
1143 regulator->enabled = 0;
1144 regulator->uA_load = 0;
1145 ret = _regulator_force_disable(regulator->rdev);
1146 mutex_unlock(&regulator->rdev->mutex);
1147 return ret;
1149 EXPORT_SYMBOL_GPL(regulator_force_disable);
1151 static int _regulator_is_enabled(struct regulator_dev *rdev)
1153 int ret;
1155 mutex_lock(&rdev->mutex);
1157 /* sanity check */
1158 if (!rdev->desc->ops->is_enabled) {
1159 ret = -EINVAL;
1160 goto out;
1163 ret = rdev->desc->ops->is_enabled(rdev);
1164 out:
1165 mutex_unlock(&rdev->mutex);
1166 return ret;
1170 * regulator_is_enabled - is the regulator output enabled
1171 * @regulator: regulator source
1173 * Returns positive if the regulator driver backing the source/client
1174 * has requested that the device be enabled, zero if it hasn't, else a
1175 * negative errno code.
1177 * Note that the device backing this regulator handle can have multiple
1178 * users, so it might be enabled even if regulator_enable() was never
1179 * called for this particular source.
1181 int regulator_is_enabled(struct regulator *regulator)
1183 return _regulator_is_enabled(regulator->rdev);
1185 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1188 * regulator_set_voltage - set regulator output voltage
1189 * @regulator: regulator source
1190 * @min_uV: Minimum required voltage in uV
1191 * @max_uV: Maximum acceptable voltage in uV
1193 * Sets a voltage regulator to the desired output voltage. This can be set
1194 * during any regulator state. IOW, regulator can be disabled or enabled.
1196 * If the regulator is enabled then the voltage will change to the new value
1197 * immediately otherwise if the regulator is disabled the regulator will
1198 * output at the new voltage when enabled.
1200 * NOTE: If the regulator is shared between several devices then the lowest
1201 * request voltage that meets the system constraints will be used.
1202 * Regulator system constraints must be set for this regulator before
1203 * calling this function otherwise this call will fail.
1205 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1207 struct regulator_dev *rdev = regulator->rdev;
1208 int ret;
1210 mutex_lock(&rdev->mutex);
1212 /* sanity check */
1213 if (!rdev->desc->ops->set_voltage) {
1214 ret = -EINVAL;
1215 goto out;
1218 /* constraints check */
1219 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1220 if (ret < 0)
1221 goto out;
1222 regulator->min_uV = min_uV;
1223 regulator->max_uV = max_uV;
1224 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1226 out:
1227 mutex_unlock(&rdev->mutex);
1228 return ret;
1230 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1232 static int _regulator_get_voltage(struct regulator_dev *rdev)
1234 /* sanity check */
1235 if (rdev->desc->ops->get_voltage)
1236 return rdev->desc->ops->get_voltage(rdev);
1237 else
1238 return -EINVAL;
1242 * regulator_get_voltage - get regulator output voltage
1243 * @regulator: regulator source
1245 * This returns the current regulator voltage in uV.
1247 * NOTE: If the regulator is disabled it will return the voltage value. This
1248 * function should not be used to determine regulator state.
1250 int regulator_get_voltage(struct regulator *regulator)
1252 int ret;
1254 mutex_lock(&regulator->rdev->mutex);
1256 ret = _regulator_get_voltage(regulator->rdev);
1258 mutex_unlock(&regulator->rdev->mutex);
1260 return ret;
1262 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1265 * regulator_set_current_limit - set regulator output current limit
1266 * @regulator: regulator source
1267 * @min_uA: Minimuum supported current in uA
1268 * @max_uA: Maximum supported current in uA
1270 * Sets current sink to the desired output current. This can be set during
1271 * any regulator state. IOW, regulator can be disabled or enabled.
1273 * If the regulator is enabled then the current will change to the new value
1274 * immediately otherwise if the regulator is disabled the regulator will
1275 * output at the new current when enabled.
1277 * NOTE: Regulator system constraints must be set for this regulator before
1278 * calling this function otherwise this call will fail.
1280 int regulator_set_current_limit(struct regulator *regulator,
1281 int min_uA, int max_uA)
1283 struct regulator_dev *rdev = regulator->rdev;
1284 int ret;
1286 mutex_lock(&rdev->mutex);
1288 /* sanity check */
1289 if (!rdev->desc->ops->set_current_limit) {
1290 ret = -EINVAL;
1291 goto out;
1294 /* constraints check */
1295 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1296 if (ret < 0)
1297 goto out;
1299 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1300 out:
1301 mutex_unlock(&rdev->mutex);
1302 return ret;
1304 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1306 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1308 int ret;
1310 mutex_lock(&rdev->mutex);
1312 /* sanity check */
1313 if (!rdev->desc->ops->get_current_limit) {
1314 ret = -EINVAL;
1315 goto out;
1318 ret = rdev->desc->ops->get_current_limit(rdev);
1319 out:
1320 mutex_unlock(&rdev->mutex);
1321 return ret;
1325 * regulator_get_current_limit - get regulator output current
1326 * @regulator: regulator source
1328 * This returns the current supplied by the specified current sink in uA.
1330 * NOTE: If the regulator is disabled it will return the current value. This
1331 * function should not be used to determine regulator state.
1333 int regulator_get_current_limit(struct regulator *regulator)
1335 return _regulator_get_current_limit(regulator->rdev);
1337 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1340 * regulator_set_mode - set regulator operating mode
1341 * @regulator: regulator source
1342 * @mode: operating mode - one of the REGULATOR_MODE constants
1344 * Set regulator operating mode to increase regulator efficiency or improve
1345 * regulation performance.
1347 * NOTE: Regulator system constraints must be set for this regulator before
1348 * calling this function otherwise this call will fail.
1350 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1352 struct regulator_dev *rdev = regulator->rdev;
1353 int ret;
1355 mutex_lock(&rdev->mutex);
1357 /* sanity check */
1358 if (!rdev->desc->ops->set_mode) {
1359 ret = -EINVAL;
1360 goto out;
1363 /* constraints check */
1364 ret = regulator_check_mode(rdev, mode);
1365 if (ret < 0)
1366 goto out;
1368 ret = rdev->desc->ops->set_mode(rdev, mode);
1369 out:
1370 mutex_unlock(&rdev->mutex);
1371 return ret;
1373 EXPORT_SYMBOL_GPL(regulator_set_mode);
1375 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1377 int ret;
1379 mutex_lock(&rdev->mutex);
1381 /* sanity check */
1382 if (!rdev->desc->ops->get_mode) {
1383 ret = -EINVAL;
1384 goto out;
1387 ret = rdev->desc->ops->get_mode(rdev);
1388 out:
1389 mutex_unlock(&rdev->mutex);
1390 return ret;
1394 * regulator_get_mode - get regulator operating mode
1395 * @regulator: regulator source
1397 * Get the current regulator operating mode.
1399 unsigned int regulator_get_mode(struct regulator *regulator)
1401 return _regulator_get_mode(regulator->rdev);
1403 EXPORT_SYMBOL_GPL(regulator_get_mode);
1406 * regulator_set_optimum_mode - set regulator optimum operating mode
1407 * @regulator: regulator source
1408 * @uA_load: load current
1410 * Notifies the regulator core of a new device load. This is then used by
1411 * DRMS (if enabled by constraints) to set the most efficient regulator
1412 * operating mode for the new regulator loading.
1414 * Consumer devices notify their supply regulator of the maximum power
1415 * they will require (can be taken from device datasheet in the power
1416 * consumption tables) when they change operational status and hence power
1417 * state. Examples of operational state changes that can affect power
1418 * consumption are :-
1420 * o Device is opened / closed.
1421 * o Device I/O is about to begin or has just finished.
1422 * o Device is idling in between work.
1424 * This information is also exported via sysfs to userspace.
1426 * DRMS will sum the total requested load on the regulator and change
1427 * to the most efficient operating mode if platform constraints allow.
1429 * Returns the new regulator mode or error.
1431 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1433 struct regulator_dev *rdev = regulator->rdev;
1434 struct regulator *consumer;
1435 int ret, output_uV, input_uV, total_uA_load = 0;
1436 unsigned int mode;
1438 mutex_lock(&rdev->mutex);
1440 regulator->uA_load = uA_load;
1441 ret = regulator_check_drms(rdev);
1442 if (ret < 0)
1443 goto out;
1444 ret = -EINVAL;
1446 /* sanity check */
1447 if (!rdev->desc->ops->get_optimum_mode)
1448 goto out;
1450 /* get output voltage */
1451 output_uV = rdev->desc->ops->get_voltage(rdev);
1452 if (output_uV <= 0) {
1453 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1454 __func__, rdev->desc->name);
1455 goto out;
1458 /* get input voltage */
1459 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1460 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1461 else
1462 input_uV = rdev->constraints->input_uV;
1463 if (input_uV <= 0) {
1464 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1465 __func__, rdev->desc->name);
1466 goto out;
1469 /* calc total requested load for this regulator */
1470 list_for_each_entry(consumer, &rdev->consumer_list, list)
1471 total_uA_load += consumer->uA_load;
1473 mode = rdev->desc->ops->get_optimum_mode(rdev,
1474 input_uV, output_uV,
1475 total_uA_load);
1476 ret = regulator_check_mode(rdev, mode);
1477 if (ret < 0) {
1478 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1479 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1480 total_uA_load, input_uV, output_uV);
1481 goto out;
1484 ret = rdev->desc->ops->set_mode(rdev, mode);
1485 if (ret < 0) {
1486 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1487 __func__, mode, rdev->desc->name);
1488 goto out;
1490 ret = mode;
1491 out:
1492 mutex_unlock(&rdev->mutex);
1493 return ret;
1495 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1498 * regulator_register_notifier - register regulator event notifier
1499 * @regulator: regulator source
1500 * @nb: notifier block
1502 * Register notifier block to receive regulator events.
1504 int regulator_register_notifier(struct regulator *regulator,
1505 struct notifier_block *nb)
1507 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1508 nb);
1510 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1513 * regulator_unregister_notifier - unregister regulator event notifier
1514 * @regulator: regulator source
1515 * @nb: notifier block
1517 * Unregister regulator event notifier block.
1519 int regulator_unregister_notifier(struct regulator *regulator,
1520 struct notifier_block *nb)
1522 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1523 nb);
1525 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1527 /* notify regulator consumers and downstream regulator consumers */
1528 static void _notifier_call_chain(struct regulator_dev *rdev,
1529 unsigned long event, void *data)
1531 struct regulator_dev *_rdev;
1533 /* call rdev chain first */
1534 mutex_lock(&rdev->mutex);
1535 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1536 mutex_unlock(&rdev->mutex);
1538 /* now notify regulator we supply */
1539 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1540 _notifier_call_chain(_rdev, event, data);
1544 * regulator_bulk_get - get multiple regulator consumers
1546 * @dev: Device to supply
1547 * @num_consumers: Number of consumers to register
1548 * @consumers: Configuration of consumers; clients are stored here.
1550 * @return 0 on success, an errno on failure.
1552 * This helper function allows drivers to get several regulator
1553 * consumers in one operation. If any of the regulators cannot be
1554 * acquired then any regulators that were allocated will be freed
1555 * before returning to the caller.
1557 int regulator_bulk_get(struct device *dev, int num_consumers,
1558 struct regulator_bulk_data *consumers)
1560 int i;
1561 int ret;
1563 for (i = 0; i < num_consumers; i++)
1564 consumers[i].consumer = NULL;
1566 for (i = 0; i < num_consumers; i++) {
1567 consumers[i].consumer = regulator_get(dev,
1568 consumers[i].supply);
1569 if (IS_ERR(consumers[i].consumer)) {
1570 dev_err(dev, "Failed to get supply '%s'\n",
1571 consumers[i].supply);
1572 ret = PTR_ERR(consumers[i].consumer);
1573 consumers[i].consumer = NULL;
1574 goto err;
1578 return 0;
1580 err:
1581 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1582 regulator_put(consumers[i].consumer);
1584 return ret;
1586 EXPORT_SYMBOL_GPL(regulator_bulk_get);
1589 * regulator_bulk_enable - enable multiple regulator consumers
1591 * @num_consumers: Number of consumers
1592 * @consumers: Consumer data; clients are stored here.
1593 * @return 0 on success, an errno on failure
1595 * This convenience API allows consumers to enable multiple regulator
1596 * clients in a single API call. If any consumers cannot be enabled
1597 * then any others that were enabled will be disabled again prior to
1598 * return.
1600 int regulator_bulk_enable(int num_consumers,
1601 struct regulator_bulk_data *consumers)
1603 int i;
1604 int ret;
1606 for (i = 0; i < num_consumers; i++) {
1607 ret = regulator_enable(consumers[i].consumer);
1608 if (ret != 0)
1609 goto err;
1612 return 0;
1614 err:
1615 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1616 for (i = 0; i < num_consumers; i++)
1617 regulator_disable(consumers[i].consumer);
1619 return ret;
1621 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1624 * regulator_bulk_disable - disable multiple regulator consumers
1626 * @num_consumers: Number of consumers
1627 * @consumers: Consumer data; clients are stored here.
1628 * @return 0 on success, an errno on failure
1630 * This convenience API allows consumers to disable multiple regulator
1631 * clients in a single API call. If any consumers cannot be enabled
1632 * then any others that were disabled will be disabled again prior to
1633 * return.
1635 int regulator_bulk_disable(int num_consumers,
1636 struct regulator_bulk_data *consumers)
1638 int i;
1639 int ret;
1641 for (i = 0; i < num_consumers; i++) {
1642 ret = regulator_disable(consumers[i].consumer);
1643 if (ret != 0)
1644 goto err;
1647 return 0;
1649 err:
1650 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1651 for (i = 0; i < num_consumers; i++)
1652 regulator_enable(consumers[i].consumer);
1654 return ret;
1656 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1659 * regulator_bulk_free - free multiple regulator consumers
1661 * @num_consumers: Number of consumers
1662 * @consumers: Consumer data; clients are stored here.
1664 * This convenience API allows consumers to free multiple regulator
1665 * clients in a single API call.
1667 void regulator_bulk_free(int num_consumers,
1668 struct regulator_bulk_data *consumers)
1670 int i;
1672 for (i = 0; i < num_consumers; i++) {
1673 regulator_put(consumers[i].consumer);
1674 consumers[i].consumer = NULL;
1677 EXPORT_SYMBOL_GPL(regulator_bulk_free);
1680 * regulator_notifier_call_chain - call regulator event notifier
1681 * @rdev: regulator source
1682 * @event: notifier block
1683 * @data: callback-specific data.
1685 * Called by regulator drivers to notify clients a regulator event has
1686 * occurred. We also notify regulator clients downstream.
1688 int regulator_notifier_call_chain(struct regulator_dev *rdev,
1689 unsigned long event, void *data)
1691 _notifier_call_chain(rdev, event, data);
1692 return NOTIFY_DONE;
1695 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1698 * To avoid cluttering sysfs (and memory) with useless state, only
1699 * create attributes that can be meaningfully displayed.
1701 static int add_regulator_attributes(struct regulator_dev *rdev)
1703 struct device *dev = &rdev->dev;
1704 struct regulator_ops *ops = rdev->desc->ops;
1705 int status = 0;
1707 /* some attributes need specific methods to be displayed */
1708 if (ops->get_voltage) {
1709 status = device_create_file(dev, &dev_attr_microvolts);
1710 if (status < 0)
1711 return status;
1713 if (ops->get_current_limit) {
1714 status = device_create_file(dev, &dev_attr_microamps);
1715 if (status < 0)
1716 return status;
1718 if (ops->get_mode) {
1719 status = device_create_file(dev, &dev_attr_opmode);
1720 if (status < 0)
1721 return status;
1723 if (ops->is_enabled) {
1724 status = device_create_file(dev, &dev_attr_state);
1725 if (status < 0)
1726 return status;
1729 /* some attributes are type-specific */
1730 if (rdev->desc->type == REGULATOR_CURRENT) {
1731 status = device_create_file(dev, &dev_attr_requested_microamps);
1732 if (status < 0)
1733 return status;
1736 /* all the other attributes exist to support constraints;
1737 * don't show them if there are no constraints, or if the
1738 * relevant supporting methods are missing.
1740 if (!rdev->constraints)
1741 return status;
1743 /* constraints need specific supporting methods */
1744 if (ops->set_voltage) {
1745 status = device_create_file(dev, &dev_attr_min_microvolts);
1746 if (status < 0)
1747 return status;
1748 status = device_create_file(dev, &dev_attr_max_microvolts);
1749 if (status < 0)
1750 return status;
1752 if (ops->set_current_limit) {
1753 status = device_create_file(dev, &dev_attr_min_microamps);
1754 if (status < 0)
1755 return status;
1756 status = device_create_file(dev, &dev_attr_max_microamps);
1757 if (status < 0)
1758 return status;
1761 /* suspend mode constraints need multiple supporting methods */
1762 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
1763 return status;
1765 status = device_create_file(dev, &dev_attr_suspend_standby_state);
1766 if (status < 0)
1767 return status;
1768 status = device_create_file(dev, &dev_attr_suspend_mem_state);
1769 if (status < 0)
1770 return status;
1771 status = device_create_file(dev, &dev_attr_suspend_disk_state);
1772 if (status < 0)
1773 return status;
1775 if (ops->set_suspend_voltage) {
1776 status = device_create_file(dev,
1777 &dev_attr_suspend_standby_microvolts);
1778 if (status < 0)
1779 return status;
1780 status = device_create_file(dev,
1781 &dev_attr_suspend_mem_microvolts);
1782 if (status < 0)
1783 return status;
1784 status = device_create_file(dev,
1785 &dev_attr_suspend_disk_microvolts);
1786 if (status < 0)
1787 return status;
1790 if (ops->set_suspend_mode) {
1791 status = device_create_file(dev,
1792 &dev_attr_suspend_standby_mode);
1793 if (status < 0)
1794 return status;
1795 status = device_create_file(dev,
1796 &dev_attr_suspend_mem_mode);
1797 if (status < 0)
1798 return status;
1799 status = device_create_file(dev,
1800 &dev_attr_suspend_disk_mode);
1801 if (status < 0)
1802 return status;
1805 return status;
1809 * regulator_register - register regulator
1810 * @regulator_desc: regulator to register
1811 * @dev: struct device for the regulator
1812 * @driver_data: private regulator data
1814 * Called by regulator drivers to register a regulator.
1815 * Returns 0 on success.
1817 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
1818 struct device *dev, void *driver_data)
1820 static atomic_t regulator_no = ATOMIC_INIT(0);
1821 struct regulator_dev *rdev;
1822 struct regulator_init_data *init_data = dev->platform_data;
1823 int ret, i;
1825 if (regulator_desc == NULL)
1826 return ERR_PTR(-EINVAL);
1828 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1829 return ERR_PTR(-EINVAL);
1831 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1832 !regulator_desc->type == REGULATOR_CURRENT)
1833 return ERR_PTR(-EINVAL);
1835 if (!init_data)
1836 return ERR_PTR(-EINVAL);
1838 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1839 if (rdev == NULL)
1840 return ERR_PTR(-ENOMEM);
1842 mutex_lock(&regulator_list_mutex);
1844 mutex_init(&rdev->mutex);
1845 rdev->reg_data = driver_data;
1846 rdev->owner = regulator_desc->owner;
1847 rdev->desc = regulator_desc;
1848 INIT_LIST_HEAD(&rdev->consumer_list);
1849 INIT_LIST_HEAD(&rdev->supply_list);
1850 INIT_LIST_HEAD(&rdev->list);
1851 INIT_LIST_HEAD(&rdev->slist);
1852 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1854 /* preform any regulator specific init */
1855 if (init_data->regulator_init) {
1856 ret = init_data->regulator_init(rdev->reg_data);
1857 if (ret < 0)
1858 goto clean;
1861 /* register with sysfs */
1862 rdev->dev.class = &regulator_class;
1863 rdev->dev.parent = dev;
1864 dev_set_name(&rdev->dev, "regulator.%d",
1865 atomic_inc_return(&regulator_no) - 1);
1866 ret = device_register(&rdev->dev);
1867 if (ret != 0)
1868 goto clean;
1870 dev_set_drvdata(&rdev->dev, rdev);
1872 /* set regulator constraints */
1873 ret = set_machine_constraints(rdev, &init_data->constraints);
1874 if (ret < 0)
1875 goto scrub;
1877 /* add attributes supported by this regulator */
1878 ret = add_regulator_attributes(rdev);
1879 if (ret < 0)
1880 goto scrub;
1882 /* set supply regulator if it exists */
1883 if (init_data->supply_regulator_dev) {
1884 ret = set_supply(rdev,
1885 dev_get_drvdata(init_data->supply_regulator_dev));
1886 if (ret < 0)
1887 goto scrub;
1890 /* add consumers devices */
1891 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1892 ret = set_consumer_device_supply(rdev,
1893 init_data->consumer_supplies[i].dev,
1894 init_data->consumer_supplies[i].supply);
1895 if (ret < 0) {
1896 for (--i; i >= 0; i--)
1897 unset_consumer_device_supply(rdev,
1898 init_data->consumer_supplies[i].dev);
1899 goto scrub;
1903 list_add(&rdev->list, &regulator_list);
1904 out:
1905 mutex_unlock(&regulator_list_mutex);
1906 return rdev;
1908 scrub:
1909 device_unregister(&rdev->dev);
1910 clean:
1911 kfree(rdev);
1912 rdev = ERR_PTR(ret);
1913 goto out;
1915 EXPORT_SYMBOL_GPL(regulator_register);
1918 * regulator_unregister - unregister regulator
1919 * @rdev: regulator to unregister
1921 * Called by regulator drivers to unregister a regulator.
1923 void regulator_unregister(struct regulator_dev *rdev)
1925 if (rdev == NULL)
1926 return;
1928 mutex_lock(&regulator_list_mutex);
1929 list_del(&rdev->list);
1930 if (rdev->supply)
1931 sysfs_remove_link(&rdev->dev.kobj, "supply");
1932 device_unregister(&rdev->dev);
1933 mutex_unlock(&regulator_list_mutex);
1935 EXPORT_SYMBOL_GPL(regulator_unregister);
1938 * regulator_suspend_prepare: prepare regulators for system wide suspend
1939 * @state: system suspend state
1941 * Configure each regulator with it's suspend operating parameters for state.
1942 * This will usually be called by machine suspend code prior to supending.
1944 int regulator_suspend_prepare(suspend_state_t state)
1946 struct regulator_dev *rdev;
1947 int ret = 0;
1949 /* ON is handled by regulator active state */
1950 if (state == PM_SUSPEND_ON)
1951 return -EINVAL;
1953 mutex_lock(&regulator_list_mutex);
1954 list_for_each_entry(rdev, &regulator_list, list) {
1956 mutex_lock(&rdev->mutex);
1957 ret = suspend_prepare(rdev, state);
1958 mutex_unlock(&rdev->mutex);
1960 if (ret < 0) {
1961 printk(KERN_ERR "%s: failed to prepare %s\n",
1962 __func__, rdev->desc->name);
1963 goto out;
1966 out:
1967 mutex_unlock(&regulator_list_mutex);
1968 return ret;
1970 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1973 * rdev_get_drvdata - get rdev regulator driver data
1974 * @rdev: regulator
1976 * Get rdev regulator driver private data. This call can be used in the
1977 * regulator driver context.
1979 void *rdev_get_drvdata(struct regulator_dev *rdev)
1981 return rdev->reg_data;
1983 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1986 * regulator_get_drvdata - get regulator driver data
1987 * @regulator: regulator
1989 * Get regulator driver private data. This call can be used in the consumer
1990 * driver context when non API regulator specific functions need to be called.
1992 void *regulator_get_drvdata(struct regulator *regulator)
1994 return regulator->rdev->reg_data;
1996 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1999 * regulator_set_drvdata - set regulator driver data
2000 * @regulator: regulator
2001 * @data: data
2003 void regulator_set_drvdata(struct regulator *regulator, void *data)
2005 regulator->rdev->reg_data = data;
2007 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2010 * regulator_get_id - get regulator ID
2011 * @rdev: regulator
2013 int rdev_get_id(struct regulator_dev *rdev)
2015 return rdev->desc->id;
2017 EXPORT_SYMBOL_GPL(rdev_get_id);
2019 struct device *rdev_get_dev(struct regulator_dev *rdev)
2021 return &rdev->dev;
2023 EXPORT_SYMBOL_GPL(rdev_get_dev);
2025 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2027 return reg_init_data->driver_data;
2029 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2031 static int __init regulator_init(void)
2033 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2034 return class_register(&regulator_class);
2037 /* init early to allow our consumers to complete system booting */
2038 core_initcall(regulator_init);