fs: ldm: don't use own implementation of hex_to_bin()
[linux-2.6.git] / drivers / regulator / core.c
blob51cf2bb37438d92f9711bb385fde0f642e663c48
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/slab.h>
20 #include <linux/err.h>
21 #include <linux/mutex.h>
22 #include <linux/suspend.h>
23 #include <linux/delay.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/driver.h>
26 #include <linux/regulator/machine.h>
28 #include "dummy.h"
30 #define REGULATOR_VERSION "0.5"
32 static DEFINE_MUTEX(regulator_list_mutex);
33 static LIST_HEAD(regulator_list);
34 static LIST_HEAD(regulator_map_list);
35 static int has_full_constraints;
38 * struct regulator_map
40 * Used to provide symbolic supply names to devices.
42 struct regulator_map {
43 struct list_head list;
44 const char *dev_name; /* The dev_name() for the consumer */
45 const char *supply;
46 struct regulator_dev *regulator;
50 * struct regulator
52 * One for each consumer device.
54 struct regulator {
55 struct device *dev;
56 struct list_head list;
57 int uA_load;
58 int min_uV;
59 int max_uV;
60 char *supply_name;
61 struct device_attribute dev_attr;
62 struct regulator_dev *rdev;
65 static int _regulator_is_enabled(struct regulator_dev *rdev);
66 static int _regulator_disable(struct regulator_dev *rdev);
67 static int _regulator_get_voltage(struct regulator_dev *rdev);
68 static int _regulator_get_current_limit(struct regulator_dev *rdev);
69 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
70 static void _notifier_call_chain(struct regulator_dev *rdev,
71 unsigned long event, void *data);
73 static const char *rdev_get_name(struct regulator_dev *rdev)
75 if (rdev->constraints && rdev->constraints->name)
76 return rdev->constraints->name;
77 else if (rdev->desc->name)
78 return rdev->desc->name;
79 else
80 return "";
83 /* gets the regulator for a given consumer device */
84 static struct regulator *get_device_regulator(struct device *dev)
86 struct regulator *regulator = NULL;
87 struct regulator_dev *rdev;
89 mutex_lock(&regulator_list_mutex);
90 list_for_each_entry(rdev, &regulator_list, list) {
91 mutex_lock(&rdev->mutex);
92 list_for_each_entry(regulator, &rdev->consumer_list, list) {
93 if (regulator->dev == dev) {
94 mutex_unlock(&rdev->mutex);
95 mutex_unlock(&regulator_list_mutex);
96 return regulator;
99 mutex_unlock(&rdev->mutex);
101 mutex_unlock(&regulator_list_mutex);
102 return NULL;
105 /* Platform voltage constraint check */
106 static int regulator_check_voltage(struct regulator_dev *rdev,
107 int *min_uV, int *max_uV)
109 BUG_ON(*min_uV > *max_uV);
111 if (!rdev->constraints) {
112 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
113 rdev_get_name(rdev));
114 return -ENODEV;
116 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
117 printk(KERN_ERR "%s: operation not allowed for %s\n",
118 __func__, rdev_get_name(rdev));
119 return -EPERM;
122 if (*max_uV > rdev->constraints->max_uV)
123 *max_uV = rdev->constraints->max_uV;
124 if (*min_uV < rdev->constraints->min_uV)
125 *min_uV = rdev->constraints->min_uV;
127 if (*min_uV > *max_uV)
128 return -EINVAL;
130 return 0;
133 /* current constraint check */
134 static int regulator_check_current_limit(struct regulator_dev *rdev,
135 int *min_uA, int *max_uA)
137 BUG_ON(*min_uA > *max_uA);
139 if (!rdev->constraints) {
140 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
141 rdev_get_name(rdev));
142 return -ENODEV;
144 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
145 printk(KERN_ERR "%s: operation not allowed for %s\n",
146 __func__, rdev_get_name(rdev));
147 return -EPERM;
150 if (*max_uA > rdev->constraints->max_uA)
151 *max_uA = rdev->constraints->max_uA;
152 if (*min_uA < rdev->constraints->min_uA)
153 *min_uA = rdev->constraints->min_uA;
155 if (*min_uA > *max_uA)
156 return -EINVAL;
158 return 0;
161 /* operating mode constraint check */
162 static int regulator_check_mode(struct regulator_dev *rdev, int mode)
164 switch (mode) {
165 case REGULATOR_MODE_FAST:
166 case REGULATOR_MODE_NORMAL:
167 case REGULATOR_MODE_IDLE:
168 case REGULATOR_MODE_STANDBY:
169 break;
170 default:
171 return -EINVAL;
174 if (!rdev->constraints) {
175 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
176 rdev_get_name(rdev));
177 return -ENODEV;
179 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
180 printk(KERN_ERR "%s: operation not allowed for %s\n",
181 __func__, rdev_get_name(rdev));
182 return -EPERM;
184 if (!(rdev->constraints->valid_modes_mask & mode)) {
185 printk(KERN_ERR "%s: invalid mode %x for %s\n",
186 __func__, mode, rdev_get_name(rdev));
187 return -EINVAL;
189 return 0;
192 /* dynamic regulator mode switching constraint check */
193 static int regulator_check_drms(struct regulator_dev *rdev)
195 if (!rdev->constraints) {
196 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
197 rdev_get_name(rdev));
198 return -ENODEV;
200 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
201 printk(KERN_ERR "%s: operation not allowed for %s\n",
202 __func__, rdev_get_name(rdev));
203 return -EPERM;
205 return 0;
208 static ssize_t device_requested_uA_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 struct regulator *regulator;
213 regulator = get_device_regulator(dev);
214 if (regulator == NULL)
215 return 0;
217 return sprintf(buf, "%d\n", regulator->uA_load);
220 static ssize_t regulator_uV_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
223 struct regulator_dev *rdev = dev_get_drvdata(dev);
224 ssize_t ret;
226 mutex_lock(&rdev->mutex);
227 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
228 mutex_unlock(&rdev->mutex);
230 return ret;
232 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
234 static ssize_t regulator_uA_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
237 struct regulator_dev *rdev = dev_get_drvdata(dev);
239 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
241 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
243 static ssize_t regulator_name_show(struct device *dev,
244 struct device_attribute *attr, char *buf)
246 struct regulator_dev *rdev = dev_get_drvdata(dev);
248 return sprintf(buf, "%s\n", rdev_get_name(rdev));
251 static ssize_t regulator_print_opmode(char *buf, int mode)
253 switch (mode) {
254 case REGULATOR_MODE_FAST:
255 return sprintf(buf, "fast\n");
256 case REGULATOR_MODE_NORMAL:
257 return sprintf(buf, "normal\n");
258 case REGULATOR_MODE_IDLE:
259 return sprintf(buf, "idle\n");
260 case REGULATOR_MODE_STANDBY:
261 return sprintf(buf, "standby\n");
263 return sprintf(buf, "unknown\n");
266 static ssize_t regulator_opmode_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 struct regulator_dev *rdev = dev_get_drvdata(dev);
271 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
273 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
275 static ssize_t regulator_print_state(char *buf, int state)
277 if (state > 0)
278 return sprintf(buf, "enabled\n");
279 else if (state == 0)
280 return sprintf(buf, "disabled\n");
281 else
282 return sprintf(buf, "unknown\n");
285 static ssize_t regulator_state_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct regulator_dev *rdev = dev_get_drvdata(dev);
289 ssize_t ret;
291 mutex_lock(&rdev->mutex);
292 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
293 mutex_unlock(&rdev->mutex);
295 return ret;
297 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
299 static ssize_t regulator_status_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct regulator_dev *rdev = dev_get_drvdata(dev);
303 int status;
304 char *label;
306 status = rdev->desc->ops->get_status(rdev);
307 if (status < 0)
308 return status;
310 switch (status) {
311 case REGULATOR_STATUS_OFF:
312 label = "off";
313 break;
314 case REGULATOR_STATUS_ON:
315 label = "on";
316 break;
317 case REGULATOR_STATUS_ERROR:
318 label = "error";
319 break;
320 case REGULATOR_STATUS_FAST:
321 label = "fast";
322 break;
323 case REGULATOR_STATUS_NORMAL:
324 label = "normal";
325 break;
326 case REGULATOR_STATUS_IDLE:
327 label = "idle";
328 break;
329 case REGULATOR_STATUS_STANDBY:
330 label = "standby";
331 break;
332 default:
333 return -ERANGE;
336 return sprintf(buf, "%s\n", label);
338 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
340 static ssize_t regulator_min_uA_show(struct device *dev,
341 struct device_attribute *attr, char *buf)
343 struct regulator_dev *rdev = dev_get_drvdata(dev);
345 if (!rdev->constraints)
346 return sprintf(buf, "constraint not defined\n");
348 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
350 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
352 static ssize_t regulator_max_uA_show(struct device *dev,
353 struct device_attribute *attr, char *buf)
355 struct regulator_dev *rdev = dev_get_drvdata(dev);
357 if (!rdev->constraints)
358 return sprintf(buf, "constraint not defined\n");
360 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
362 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
364 static ssize_t regulator_min_uV_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 if (!rdev->constraints)
370 return sprintf(buf, "constraint not defined\n");
372 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
374 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
376 static ssize_t regulator_max_uV_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
381 if (!rdev->constraints)
382 return sprintf(buf, "constraint not defined\n");
384 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
386 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
388 static ssize_t regulator_total_uA_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct regulator_dev *rdev = dev_get_drvdata(dev);
392 struct regulator *regulator;
393 int uA = 0;
395 mutex_lock(&rdev->mutex);
396 list_for_each_entry(regulator, &rdev->consumer_list, list)
397 uA += regulator->uA_load;
398 mutex_unlock(&rdev->mutex);
399 return sprintf(buf, "%d\n", uA);
401 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
403 static ssize_t regulator_num_users_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
406 struct regulator_dev *rdev = dev_get_drvdata(dev);
407 return sprintf(buf, "%d\n", rdev->use_count);
410 static ssize_t regulator_type_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
413 struct regulator_dev *rdev = dev_get_drvdata(dev);
415 switch (rdev->desc->type) {
416 case REGULATOR_VOLTAGE:
417 return sprintf(buf, "voltage\n");
418 case REGULATOR_CURRENT:
419 return sprintf(buf, "current\n");
421 return sprintf(buf, "unknown\n");
424 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct regulator_dev *rdev = dev_get_drvdata(dev);
429 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
431 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
432 regulator_suspend_mem_uV_show, NULL);
434 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
437 struct regulator_dev *rdev = dev_get_drvdata(dev);
439 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
441 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
442 regulator_suspend_disk_uV_show, NULL);
444 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct regulator_dev *rdev = dev_get_drvdata(dev);
449 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
451 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
452 regulator_suspend_standby_uV_show, NULL);
454 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
457 struct regulator_dev *rdev = dev_get_drvdata(dev);
459 return regulator_print_opmode(buf,
460 rdev->constraints->state_mem.mode);
462 static DEVICE_ATTR(suspend_mem_mode, 0444,
463 regulator_suspend_mem_mode_show, NULL);
465 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 return regulator_print_opmode(buf,
471 rdev->constraints->state_disk.mode);
473 static DEVICE_ATTR(suspend_disk_mode, 0444,
474 regulator_suspend_disk_mode_show, NULL);
476 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
479 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 return regulator_print_opmode(buf,
482 rdev->constraints->state_standby.mode);
484 static DEVICE_ATTR(suspend_standby_mode, 0444,
485 regulator_suspend_standby_mode_show, NULL);
487 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
490 struct regulator_dev *rdev = dev_get_drvdata(dev);
492 return regulator_print_state(buf,
493 rdev->constraints->state_mem.enabled);
495 static DEVICE_ATTR(suspend_mem_state, 0444,
496 regulator_suspend_mem_state_show, NULL);
498 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
503 return regulator_print_state(buf,
504 rdev->constraints->state_disk.enabled);
506 static DEVICE_ATTR(suspend_disk_state, 0444,
507 regulator_suspend_disk_state_show, NULL);
509 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct regulator_dev *rdev = dev_get_drvdata(dev);
514 return regulator_print_state(buf,
515 rdev->constraints->state_standby.enabled);
517 static DEVICE_ATTR(suspend_standby_state, 0444,
518 regulator_suspend_standby_state_show, NULL);
522 * These are the only attributes are present for all regulators.
523 * Other attributes are a function of regulator functionality.
525 static struct device_attribute regulator_dev_attrs[] = {
526 __ATTR(name, 0444, regulator_name_show, NULL),
527 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
528 __ATTR(type, 0444, regulator_type_show, NULL),
529 __ATTR_NULL,
532 static void regulator_dev_release(struct device *dev)
534 struct regulator_dev *rdev = dev_get_drvdata(dev);
535 kfree(rdev);
538 static struct class regulator_class = {
539 .name = "regulator",
540 .dev_release = regulator_dev_release,
541 .dev_attrs = regulator_dev_attrs,
544 /* Calculate the new optimum regulator operating mode based on the new total
545 * consumer load. All locks held by caller */
546 static void drms_uA_update(struct regulator_dev *rdev)
548 struct regulator *sibling;
549 int current_uA = 0, output_uV, input_uV, err;
550 unsigned int mode;
552 err = regulator_check_drms(rdev);
553 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
554 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
555 return;
557 /* get output voltage */
558 output_uV = rdev->desc->ops->get_voltage(rdev);
559 if (output_uV <= 0)
560 return;
562 /* get input voltage */
563 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
564 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
565 else
566 input_uV = rdev->constraints->input_uV;
567 if (input_uV <= 0)
568 return;
570 /* calc total requested load */
571 list_for_each_entry(sibling, &rdev->consumer_list, list)
572 current_uA += sibling->uA_load;
574 /* now get the optimum mode for our new total regulator load */
575 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
576 output_uV, current_uA);
578 /* check the new mode is allowed */
579 err = regulator_check_mode(rdev, mode);
580 if (err == 0)
581 rdev->desc->ops->set_mode(rdev, mode);
584 static int suspend_set_state(struct regulator_dev *rdev,
585 struct regulator_state *rstate)
587 int ret = 0;
588 bool can_set_state;
590 can_set_state = rdev->desc->ops->set_suspend_enable &&
591 rdev->desc->ops->set_suspend_disable;
593 /* If we have no suspend mode configration don't set anything;
594 * only warn if the driver actually makes the suspend mode
595 * configurable.
597 if (!rstate->enabled && !rstate->disabled) {
598 if (can_set_state)
599 printk(KERN_WARNING "%s: No configuration for %s\n",
600 __func__, rdev_get_name(rdev));
601 return 0;
604 if (rstate->enabled && rstate->disabled) {
605 printk(KERN_ERR "%s: invalid configuration for %s\n",
606 __func__, rdev_get_name(rdev));
607 return -EINVAL;
610 if (!can_set_state) {
611 printk(KERN_ERR "%s: no way to set suspend state\n",
612 __func__);
613 return -EINVAL;
616 if (rstate->enabled)
617 ret = rdev->desc->ops->set_suspend_enable(rdev);
618 else
619 ret = rdev->desc->ops->set_suspend_disable(rdev);
620 if (ret < 0) {
621 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
622 return ret;
625 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
626 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
627 if (ret < 0) {
628 printk(KERN_ERR "%s: failed to set voltage\n",
629 __func__);
630 return ret;
634 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
635 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
636 if (ret < 0) {
637 printk(KERN_ERR "%s: failed to set mode\n", __func__);
638 return ret;
641 return ret;
644 /* locks held by caller */
645 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
647 if (!rdev->constraints)
648 return -EINVAL;
650 switch (state) {
651 case PM_SUSPEND_STANDBY:
652 return suspend_set_state(rdev,
653 &rdev->constraints->state_standby);
654 case PM_SUSPEND_MEM:
655 return suspend_set_state(rdev,
656 &rdev->constraints->state_mem);
657 case PM_SUSPEND_MAX:
658 return suspend_set_state(rdev,
659 &rdev->constraints->state_disk);
660 default:
661 return -EINVAL;
665 static void print_constraints(struct regulator_dev *rdev)
667 struct regulation_constraints *constraints = rdev->constraints;
668 char buf[80] = "";
669 int count = 0;
670 int ret;
672 if (constraints->min_uV && constraints->max_uV) {
673 if (constraints->min_uV == constraints->max_uV)
674 count += sprintf(buf + count, "%d mV ",
675 constraints->min_uV / 1000);
676 else
677 count += sprintf(buf + count, "%d <--> %d mV ",
678 constraints->min_uV / 1000,
679 constraints->max_uV / 1000);
682 if (!constraints->min_uV ||
683 constraints->min_uV != constraints->max_uV) {
684 ret = _regulator_get_voltage(rdev);
685 if (ret > 0)
686 count += sprintf(buf + count, "at %d mV ", ret / 1000);
689 if (constraints->min_uA && constraints->max_uA) {
690 if (constraints->min_uA == constraints->max_uA)
691 count += sprintf(buf + count, "%d mA ",
692 constraints->min_uA / 1000);
693 else
694 count += sprintf(buf + count, "%d <--> %d mA ",
695 constraints->min_uA / 1000,
696 constraints->max_uA / 1000);
699 if (!constraints->min_uA ||
700 constraints->min_uA != constraints->max_uA) {
701 ret = _regulator_get_current_limit(rdev);
702 if (ret > 0)
703 count += sprintf(buf + count, "at %d uA ", ret / 1000);
706 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
707 count += sprintf(buf + count, "fast ");
708 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
709 count += sprintf(buf + count, "normal ");
710 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
711 count += sprintf(buf + count, "idle ");
712 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
713 count += sprintf(buf + count, "standby");
715 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
718 static int machine_constraints_voltage(struct regulator_dev *rdev,
719 struct regulation_constraints *constraints)
721 struct regulator_ops *ops = rdev->desc->ops;
722 const char *name = rdev_get_name(rdev);
723 int ret;
725 /* do we need to apply the constraint voltage */
726 if (rdev->constraints->apply_uV &&
727 rdev->constraints->min_uV == rdev->constraints->max_uV &&
728 ops->set_voltage) {
729 ret = ops->set_voltage(rdev,
730 rdev->constraints->min_uV, rdev->constraints->max_uV);
731 if (ret < 0) {
732 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
733 __func__,
734 rdev->constraints->min_uV, name);
735 rdev->constraints = NULL;
736 return ret;
740 /* constrain machine-level voltage specs to fit
741 * the actual range supported by this regulator.
743 if (ops->list_voltage && rdev->desc->n_voltages) {
744 int count = rdev->desc->n_voltages;
745 int i;
746 int min_uV = INT_MAX;
747 int max_uV = INT_MIN;
748 int cmin = constraints->min_uV;
749 int cmax = constraints->max_uV;
751 /* it's safe to autoconfigure fixed-voltage supplies
752 and the constraints are used by list_voltage. */
753 if (count == 1 && !cmin) {
754 cmin = 1;
755 cmax = INT_MAX;
756 constraints->min_uV = cmin;
757 constraints->max_uV = cmax;
760 /* voltage constraints are optional */
761 if ((cmin == 0) && (cmax == 0))
762 return 0;
764 /* else require explicit machine-level constraints */
765 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
766 pr_err("%s: %s '%s' voltage constraints\n",
767 __func__, "invalid", name);
768 return -EINVAL;
771 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
772 for (i = 0; i < count; i++) {
773 int value;
775 value = ops->list_voltage(rdev, i);
776 if (value <= 0)
777 continue;
779 /* maybe adjust [min_uV..max_uV] */
780 if (value >= cmin && value < min_uV)
781 min_uV = value;
782 if (value <= cmax && value > max_uV)
783 max_uV = value;
786 /* final: [min_uV..max_uV] valid iff constraints valid */
787 if (max_uV < min_uV) {
788 pr_err("%s: %s '%s' voltage constraints\n",
789 __func__, "unsupportable", name);
790 return -EINVAL;
793 /* use regulator's subset of machine constraints */
794 if (constraints->min_uV < min_uV) {
795 pr_debug("%s: override '%s' %s, %d -> %d\n",
796 __func__, name, "min_uV",
797 constraints->min_uV, min_uV);
798 constraints->min_uV = min_uV;
800 if (constraints->max_uV > max_uV) {
801 pr_debug("%s: override '%s' %s, %d -> %d\n",
802 __func__, name, "max_uV",
803 constraints->max_uV, max_uV);
804 constraints->max_uV = max_uV;
808 return 0;
812 * set_machine_constraints - sets regulator constraints
813 * @rdev: regulator source
814 * @constraints: constraints to apply
816 * Allows platform initialisation code to define and constrain
817 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
818 * Constraints *must* be set by platform code in order for some
819 * regulator operations to proceed i.e. set_voltage, set_current_limit,
820 * set_mode.
822 static int set_machine_constraints(struct regulator_dev *rdev,
823 struct regulation_constraints *constraints)
825 int ret = 0;
826 const char *name;
827 struct regulator_ops *ops = rdev->desc->ops;
829 rdev->constraints = constraints;
831 name = rdev_get_name(rdev);
833 ret = machine_constraints_voltage(rdev, constraints);
834 if (ret != 0)
835 goto out;
837 /* do we need to setup our suspend state */
838 if (constraints->initial_state) {
839 ret = suspend_prepare(rdev, constraints->initial_state);
840 if (ret < 0) {
841 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
842 __func__, name);
843 rdev->constraints = NULL;
844 goto out;
848 if (constraints->initial_mode) {
849 if (!ops->set_mode) {
850 printk(KERN_ERR "%s: no set_mode operation for %s\n",
851 __func__, name);
852 ret = -EINVAL;
853 goto out;
856 ret = ops->set_mode(rdev, constraints->initial_mode);
857 if (ret < 0) {
858 printk(KERN_ERR
859 "%s: failed to set initial mode for %s: %d\n",
860 __func__, name, ret);
861 goto out;
865 /* If the constraints say the regulator should be on at this point
866 * and we have control then make sure it is enabled.
868 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
869 ret = ops->enable(rdev);
870 if (ret < 0) {
871 printk(KERN_ERR "%s: failed to enable %s\n",
872 __func__, name);
873 rdev->constraints = NULL;
874 goto out;
878 print_constraints(rdev);
879 out:
880 return ret;
884 * set_supply - set regulator supply regulator
885 * @rdev: regulator name
886 * @supply_rdev: supply regulator name
888 * Called by platform initialisation code to set the supply regulator for this
889 * regulator. This ensures that a regulators supply will also be enabled by the
890 * core if it's child is enabled.
892 static int set_supply(struct regulator_dev *rdev,
893 struct regulator_dev *supply_rdev)
895 int err;
897 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
898 "supply");
899 if (err) {
900 printk(KERN_ERR
901 "%s: could not add device link %s err %d\n",
902 __func__, supply_rdev->dev.kobj.name, err);
903 goto out;
905 rdev->supply = supply_rdev;
906 list_add(&rdev->slist, &supply_rdev->supply_list);
907 out:
908 return err;
912 * set_consumer_device_supply: Bind a regulator to a symbolic supply
913 * @rdev: regulator source
914 * @consumer_dev: device the supply applies to
915 * @consumer_dev_name: dev_name() string for device supply applies to
916 * @supply: symbolic name for supply
918 * Allows platform initialisation code to map physical regulator
919 * sources to symbolic names for supplies for use by devices. Devices
920 * should use these symbolic names to request regulators, avoiding the
921 * need to provide board-specific regulator names as platform data.
923 * Only one of consumer_dev and consumer_dev_name may be specified.
925 static int set_consumer_device_supply(struct regulator_dev *rdev,
926 struct device *consumer_dev, const char *consumer_dev_name,
927 const char *supply)
929 struct regulator_map *node;
930 int has_dev;
932 if (consumer_dev && consumer_dev_name)
933 return -EINVAL;
935 if (!consumer_dev_name && consumer_dev)
936 consumer_dev_name = dev_name(consumer_dev);
938 if (supply == NULL)
939 return -EINVAL;
941 if (consumer_dev_name != NULL)
942 has_dev = 1;
943 else
944 has_dev = 0;
946 list_for_each_entry(node, &regulator_map_list, list) {
947 if (consumer_dev_name != node->dev_name)
948 continue;
949 if (strcmp(node->supply, supply) != 0)
950 continue;
952 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
953 dev_name(&node->regulator->dev),
954 node->regulator->desc->name,
955 supply,
956 dev_name(&rdev->dev), rdev_get_name(rdev));
957 return -EBUSY;
960 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
961 if (node == NULL)
962 return -ENOMEM;
964 node->regulator = rdev;
965 node->supply = supply;
967 if (has_dev) {
968 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
969 if (node->dev_name == NULL) {
970 kfree(node);
971 return -ENOMEM;
975 list_add(&node->list, &regulator_map_list);
976 return 0;
979 static void unset_consumer_device_supply(struct regulator_dev *rdev,
980 const char *consumer_dev_name, struct device *consumer_dev)
982 struct regulator_map *node, *n;
984 if (consumer_dev && !consumer_dev_name)
985 consumer_dev_name = dev_name(consumer_dev);
987 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
988 if (rdev != node->regulator)
989 continue;
991 if (consumer_dev_name && node->dev_name &&
992 strcmp(consumer_dev_name, node->dev_name))
993 continue;
995 list_del(&node->list);
996 kfree(node->dev_name);
997 kfree(node);
998 return;
1002 static void unset_regulator_supplies(struct regulator_dev *rdev)
1004 struct regulator_map *node, *n;
1006 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1007 if (rdev == node->regulator) {
1008 list_del(&node->list);
1009 kfree(node->dev_name);
1010 kfree(node);
1011 return;
1016 #define REG_STR_SIZE 32
1018 static struct regulator *create_regulator(struct regulator_dev *rdev,
1019 struct device *dev,
1020 const char *supply_name)
1022 struct regulator *regulator;
1023 char buf[REG_STR_SIZE];
1024 int err, size;
1026 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1027 if (regulator == NULL)
1028 return NULL;
1030 mutex_lock(&rdev->mutex);
1031 regulator->rdev = rdev;
1032 list_add(&regulator->list, &rdev->consumer_list);
1034 if (dev) {
1035 /* create a 'requested_microamps_name' sysfs entry */
1036 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1037 supply_name);
1038 if (size >= REG_STR_SIZE)
1039 goto overflow_err;
1041 regulator->dev = dev;
1042 sysfs_attr_init(&regulator->dev_attr.attr);
1043 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1044 if (regulator->dev_attr.attr.name == NULL)
1045 goto attr_name_err;
1047 regulator->dev_attr.attr.owner = THIS_MODULE;
1048 regulator->dev_attr.attr.mode = 0444;
1049 regulator->dev_attr.show = device_requested_uA_show;
1050 err = device_create_file(dev, &regulator->dev_attr);
1051 if (err < 0) {
1052 printk(KERN_WARNING "%s: could not add regulator_dev"
1053 " load sysfs\n", __func__);
1054 goto attr_name_err;
1057 /* also add a link to the device sysfs entry */
1058 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1059 dev->kobj.name, supply_name);
1060 if (size >= REG_STR_SIZE)
1061 goto attr_err;
1063 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1064 if (regulator->supply_name == NULL)
1065 goto attr_err;
1067 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1068 buf);
1069 if (err) {
1070 printk(KERN_WARNING
1071 "%s: could not add device link %s err %d\n",
1072 __func__, dev->kobj.name, err);
1073 device_remove_file(dev, &regulator->dev_attr);
1074 goto link_name_err;
1077 mutex_unlock(&rdev->mutex);
1078 return regulator;
1079 link_name_err:
1080 kfree(regulator->supply_name);
1081 attr_err:
1082 device_remove_file(regulator->dev, &regulator->dev_attr);
1083 attr_name_err:
1084 kfree(regulator->dev_attr.attr.name);
1085 overflow_err:
1086 list_del(&regulator->list);
1087 kfree(regulator);
1088 mutex_unlock(&rdev->mutex);
1089 return NULL;
1092 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1094 if (!rdev->desc->ops->enable_time)
1095 return 0;
1096 return rdev->desc->ops->enable_time(rdev);
1099 /* Internal regulator request function */
1100 static struct regulator *_regulator_get(struct device *dev, const char *id,
1101 int exclusive)
1103 struct regulator_dev *rdev;
1104 struct regulator_map *map;
1105 struct regulator *regulator = ERR_PTR(-ENODEV);
1106 const char *devname = NULL;
1107 int ret;
1109 if (id == NULL) {
1110 printk(KERN_ERR "regulator: get() with no identifier\n");
1111 return regulator;
1114 if (dev)
1115 devname = dev_name(dev);
1117 mutex_lock(&regulator_list_mutex);
1119 list_for_each_entry(map, &regulator_map_list, list) {
1120 /* If the mapping has a device set up it must match */
1121 if (map->dev_name &&
1122 (!devname || strcmp(map->dev_name, devname)))
1123 continue;
1125 if (strcmp(map->supply, id) == 0) {
1126 rdev = map->regulator;
1127 goto found;
1131 #ifdef CONFIG_REGULATOR_DUMMY
1132 if (!devname)
1133 devname = "deviceless";
1135 /* If the board didn't flag that it was fully constrained then
1136 * substitute in a dummy regulator so consumers can continue.
1138 if (!has_full_constraints) {
1139 pr_warning("%s supply %s not found, using dummy regulator\n",
1140 devname, id);
1141 rdev = dummy_regulator_rdev;
1142 goto found;
1144 #endif
1146 mutex_unlock(&regulator_list_mutex);
1147 return regulator;
1149 found:
1150 if (rdev->exclusive) {
1151 regulator = ERR_PTR(-EPERM);
1152 goto out;
1155 if (exclusive && rdev->open_count) {
1156 regulator = ERR_PTR(-EBUSY);
1157 goto out;
1160 if (!try_module_get(rdev->owner))
1161 goto out;
1163 regulator = create_regulator(rdev, dev, id);
1164 if (regulator == NULL) {
1165 regulator = ERR_PTR(-ENOMEM);
1166 module_put(rdev->owner);
1169 rdev->open_count++;
1170 if (exclusive) {
1171 rdev->exclusive = 1;
1173 ret = _regulator_is_enabled(rdev);
1174 if (ret > 0)
1175 rdev->use_count = 1;
1176 else
1177 rdev->use_count = 0;
1180 out:
1181 mutex_unlock(&regulator_list_mutex);
1183 return regulator;
1187 * regulator_get - lookup and obtain a reference to a regulator.
1188 * @dev: device for regulator "consumer"
1189 * @id: Supply name or regulator ID.
1191 * Returns a struct regulator corresponding to the regulator producer,
1192 * or IS_ERR() condition containing errno.
1194 * Use of supply names configured via regulator_set_device_supply() is
1195 * strongly encouraged. It is recommended that the supply name used
1196 * should match the name used for the supply and/or the relevant
1197 * device pins in the datasheet.
1199 struct regulator *regulator_get(struct device *dev, const char *id)
1201 return _regulator_get(dev, id, 0);
1203 EXPORT_SYMBOL_GPL(regulator_get);
1206 * regulator_get_exclusive - obtain exclusive access to a regulator.
1207 * @dev: device for regulator "consumer"
1208 * @id: Supply name or regulator ID.
1210 * Returns a struct regulator corresponding to the regulator producer,
1211 * or IS_ERR() condition containing errno. Other consumers will be
1212 * unable to obtain this reference is held and the use count for the
1213 * regulator will be initialised to reflect the current state of the
1214 * regulator.
1216 * This is intended for use by consumers which cannot tolerate shared
1217 * use of the regulator such as those which need to force the
1218 * regulator off for correct operation of the hardware they are
1219 * controlling.
1221 * Use of supply names configured via regulator_set_device_supply() is
1222 * strongly encouraged. It is recommended that the supply name used
1223 * should match the name used for the supply and/or the relevant
1224 * device pins in the datasheet.
1226 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1228 return _regulator_get(dev, id, 1);
1230 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1233 * regulator_put - "free" the regulator source
1234 * @regulator: regulator source
1236 * Note: drivers must ensure that all regulator_enable calls made on this
1237 * regulator source are balanced by regulator_disable calls prior to calling
1238 * this function.
1240 void regulator_put(struct regulator *regulator)
1242 struct regulator_dev *rdev;
1244 if (regulator == NULL || IS_ERR(regulator))
1245 return;
1247 mutex_lock(&regulator_list_mutex);
1248 rdev = regulator->rdev;
1250 /* remove any sysfs entries */
1251 if (regulator->dev) {
1252 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1253 kfree(regulator->supply_name);
1254 device_remove_file(regulator->dev, &regulator->dev_attr);
1255 kfree(regulator->dev_attr.attr.name);
1257 list_del(&regulator->list);
1258 kfree(regulator);
1260 rdev->open_count--;
1261 rdev->exclusive = 0;
1263 module_put(rdev->owner);
1264 mutex_unlock(&regulator_list_mutex);
1266 EXPORT_SYMBOL_GPL(regulator_put);
1268 static int _regulator_can_change_status(struct regulator_dev *rdev)
1270 if (!rdev->constraints)
1271 return 0;
1273 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1274 return 1;
1275 else
1276 return 0;
1279 /* locks held by regulator_enable() */
1280 static int _regulator_enable(struct regulator_dev *rdev)
1282 int ret, delay;
1284 /* do we need to enable the supply regulator first */
1285 if (rdev->supply) {
1286 ret = _regulator_enable(rdev->supply);
1287 if (ret < 0) {
1288 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1289 __func__, rdev_get_name(rdev), ret);
1290 return ret;
1294 /* check voltage and requested load before enabling */
1295 if (rdev->constraints &&
1296 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1297 drms_uA_update(rdev);
1299 if (rdev->use_count == 0) {
1300 /* The regulator may on if it's not switchable or left on */
1301 ret = _regulator_is_enabled(rdev);
1302 if (ret == -EINVAL || ret == 0) {
1303 if (!_regulator_can_change_status(rdev))
1304 return -EPERM;
1306 if (!rdev->desc->ops->enable)
1307 return -EINVAL;
1309 /* Query before enabling in case configuration
1310 * dependant. */
1311 ret = _regulator_get_enable_time(rdev);
1312 if (ret >= 0) {
1313 delay = ret;
1314 } else {
1315 printk(KERN_WARNING
1316 "%s: enable_time() failed for %s: %d\n",
1317 __func__, rdev_get_name(rdev),
1318 ret);
1319 delay = 0;
1322 /* Allow the regulator to ramp; it would be useful
1323 * to extend this for bulk operations so that the
1324 * regulators can ramp together. */
1325 ret = rdev->desc->ops->enable(rdev);
1326 if (ret < 0)
1327 return ret;
1329 if (delay >= 1000)
1330 mdelay(delay / 1000);
1331 else if (delay)
1332 udelay(delay);
1334 } else if (ret < 0) {
1335 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1336 __func__, rdev_get_name(rdev), ret);
1337 return ret;
1339 /* Fallthrough on positive return values - already enabled */
1342 rdev->use_count++;
1344 return 0;
1348 * regulator_enable - enable regulator output
1349 * @regulator: regulator source
1351 * Request that the regulator be enabled with the regulator output at
1352 * the predefined voltage or current value. Calls to regulator_enable()
1353 * must be balanced with calls to regulator_disable().
1355 * NOTE: the output value can be set by other drivers, boot loader or may be
1356 * hardwired in the regulator.
1358 int regulator_enable(struct regulator *regulator)
1360 struct regulator_dev *rdev = regulator->rdev;
1361 int ret = 0;
1363 mutex_lock(&rdev->mutex);
1364 ret = _regulator_enable(rdev);
1365 mutex_unlock(&rdev->mutex);
1366 return ret;
1368 EXPORT_SYMBOL_GPL(regulator_enable);
1370 /* locks held by regulator_disable() */
1371 static int _regulator_disable(struct regulator_dev *rdev)
1373 int ret = 0;
1375 if (WARN(rdev->use_count <= 0,
1376 "unbalanced disables for %s\n",
1377 rdev_get_name(rdev)))
1378 return -EIO;
1380 /* are we the last user and permitted to disable ? */
1381 if (rdev->use_count == 1 &&
1382 (rdev->constraints && !rdev->constraints->always_on)) {
1384 /* we are last user */
1385 if (_regulator_can_change_status(rdev) &&
1386 rdev->desc->ops->disable) {
1387 ret = rdev->desc->ops->disable(rdev);
1388 if (ret < 0) {
1389 printk(KERN_ERR "%s: failed to disable %s\n",
1390 __func__, rdev_get_name(rdev));
1391 return ret;
1394 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1395 NULL);
1398 /* decrease our supplies ref count and disable if required */
1399 if (rdev->supply)
1400 _regulator_disable(rdev->supply);
1402 rdev->use_count = 0;
1403 } else if (rdev->use_count > 1) {
1405 if (rdev->constraints &&
1406 (rdev->constraints->valid_ops_mask &
1407 REGULATOR_CHANGE_DRMS))
1408 drms_uA_update(rdev);
1410 rdev->use_count--;
1412 return ret;
1416 * regulator_disable - disable regulator output
1417 * @regulator: regulator source
1419 * Disable the regulator output voltage or current. Calls to
1420 * regulator_enable() must be balanced with calls to
1421 * regulator_disable().
1423 * NOTE: this will only disable the regulator output if no other consumer
1424 * devices have it enabled, the regulator device supports disabling and
1425 * machine constraints permit this operation.
1427 int regulator_disable(struct regulator *regulator)
1429 struct regulator_dev *rdev = regulator->rdev;
1430 int ret = 0;
1432 mutex_lock(&rdev->mutex);
1433 ret = _regulator_disable(rdev);
1434 mutex_unlock(&rdev->mutex);
1435 return ret;
1437 EXPORT_SYMBOL_GPL(regulator_disable);
1439 /* locks held by regulator_force_disable() */
1440 static int _regulator_force_disable(struct regulator_dev *rdev)
1442 int ret = 0;
1444 /* force disable */
1445 if (rdev->desc->ops->disable) {
1446 /* ah well, who wants to live forever... */
1447 ret = rdev->desc->ops->disable(rdev);
1448 if (ret < 0) {
1449 printk(KERN_ERR "%s: failed to force disable %s\n",
1450 __func__, rdev_get_name(rdev));
1451 return ret;
1453 /* notify other consumers that power has been forced off */
1454 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1455 REGULATOR_EVENT_DISABLE, NULL);
1458 /* decrease our supplies ref count and disable if required */
1459 if (rdev->supply)
1460 _regulator_disable(rdev->supply);
1462 rdev->use_count = 0;
1463 return ret;
1467 * regulator_force_disable - force disable regulator output
1468 * @regulator: regulator source
1470 * Forcibly disable the regulator output voltage or current.
1471 * NOTE: this *will* disable the regulator output even if other consumer
1472 * devices have it enabled. This should be used for situations when device
1473 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1475 int regulator_force_disable(struct regulator *regulator)
1477 int ret;
1479 mutex_lock(&regulator->rdev->mutex);
1480 regulator->uA_load = 0;
1481 ret = _regulator_force_disable(regulator->rdev);
1482 mutex_unlock(&regulator->rdev->mutex);
1483 return ret;
1485 EXPORT_SYMBOL_GPL(regulator_force_disable);
1487 static int _regulator_is_enabled(struct regulator_dev *rdev)
1489 /* If we don't know then assume that the regulator is always on */
1490 if (!rdev->desc->ops->is_enabled)
1491 return 1;
1493 return rdev->desc->ops->is_enabled(rdev);
1497 * regulator_is_enabled - is the regulator output enabled
1498 * @regulator: regulator source
1500 * Returns positive if the regulator driver backing the source/client
1501 * has requested that the device be enabled, zero if it hasn't, else a
1502 * negative errno code.
1504 * Note that the device backing this regulator handle can have multiple
1505 * users, so it might be enabled even if regulator_enable() was never
1506 * called for this particular source.
1508 int regulator_is_enabled(struct regulator *regulator)
1510 int ret;
1512 mutex_lock(&regulator->rdev->mutex);
1513 ret = _regulator_is_enabled(regulator->rdev);
1514 mutex_unlock(&regulator->rdev->mutex);
1516 return ret;
1518 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1521 * regulator_count_voltages - count regulator_list_voltage() selectors
1522 * @regulator: regulator source
1524 * Returns number of selectors, or negative errno. Selectors are
1525 * numbered starting at zero, and typically correspond to bitfields
1526 * in hardware registers.
1528 int regulator_count_voltages(struct regulator *regulator)
1530 struct regulator_dev *rdev = regulator->rdev;
1532 return rdev->desc->n_voltages ? : -EINVAL;
1534 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1537 * regulator_list_voltage - enumerate supported voltages
1538 * @regulator: regulator source
1539 * @selector: identify voltage to list
1540 * Context: can sleep
1542 * Returns a voltage that can be passed to @regulator_set_voltage(),
1543 * zero if this selector code can't be used on this system, or a
1544 * negative errno.
1546 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1548 struct regulator_dev *rdev = regulator->rdev;
1549 struct regulator_ops *ops = rdev->desc->ops;
1550 int ret;
1552 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1553 return -EINVAL;
1555 mutex_lock(&rdev->mutex);
1556 ret = ops->list_voltage(rdev, selector);
1557 mutex_unlock(&rdev->mutex);
1559 if (ret > 0) {
1560 if (ret < rdev->constraints->min_uV)
1561 ret = 0;
1562 else if (ret > rdev->constraints->max_uV)
1563 ret = 0;
1566 return ret;
1568 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1571 * regulator_is_supported_voltage - check if a voltage range can be supported
1573 * @regulator: Regulator to check.
1574 * @min_uV: Minimum required voltage in uV.
1575 * @max_uV: Maximum required voltage in uV.
1577 * Returns a boolean or a negative error code.
1579 int regulator_is_supported_voltage(struct regulator *regulator,
1580 int min_uV, int max_uV)
1582 int i, voltages, ret;
1584 ret = regulator_count_voltages(regulator);
1585 if (ret < 0)
1586 return ret;
1587 voltages = ret;
1589 for (i = 0; i < voltages; i++) {
1590 ret = regulator_list_voltage(regulator, i);
1592 if (ret >= min_uV && ret <= max_uV)
1593 return 1;
1596 return 0;
1600 * regulator_set_voltage - set regulator output voltage
1601 * @regulator: regulator source
1602 * @min_uV: Minimum required voltage in uV
1603 * @max_uV: Maximum acceptable voltage in uV
1605 * Sets a voltage regulator to the desired output voltage. This can be set
1606 * during any regulator state. IOW, regulator can be disabled or enabled.
1608 * If the regulator is enabled then the voltage will change to the new value
1609 * immediately otherwise if the regulator is disabled the regulator will
1610 * output at the new voltage when enabled.
1612 * NOTE: If the regulator is shared between several devices then the lowest
1613 * request voltage that meets the system constraints will be used.
1614 * Regulator system constraints must be set for this regulator before
1615 * calling this function otherwise this call will fail.
1617 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1619 struct regulator_dev *rdev = regulator->rdev;
1620 int ret;
1622 mutex_lock(&rdev->mutex);
1624 /* sanity check */
1625 if (!rdev->desc->ops->set_voltage) {
1626 ret = -EINVAL;
1627 goto out;
1630 /* constraints check */
1631 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1632 if (ret < 0)
1633 goto out;
1634 regulator->min_uV = min_uV;
1635 regulator->max_uV = max_uV;
1636 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1638 out:
1639 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
1640 mutex_unlock(&rdev->mutex);
1641 return ret;
1643 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1645 static int _regulator_get_voltage(struct regulator_dev *rdev)
1647 /* sanity check */
1648 if (rdev->desc->ops->get_voltage)
1649 return rdev->desc->ops->get_voltage(rdev);
1650 else
1651 return -EINVAL;
1655 * regulator_get_voltage - get regulator output voltage
1656 * @regulator: regulator source
1658 * This returns the current regulator voltage in uV.
1660 * NOTE: If the regulator is disabled it will return the voltage value. This
1661 * function should not be used to determine regulator state.
1663 int regulator_get_voltage(struct regulator *regulator)
1665 int ret;
1667 mutex_lock(&regulator->rdev->mutex);
1669 ret = _regulator_get_voltage(regulator->rdev);
1671 mutex_unlock(&regulator->rdev->mutex);
1673 return ret;
1675 EXPORT_SYMBOL_GPL(regulator_get_voltage);
1678 * regulator_set_current_limit - set regulator output current limit
1679 * @regulator: regulator source
1680 * @min_uA: Minimuum supported current in uA
1681 * @max_uA: Maximum supported current in uA
1683 * Sets current sink to the desired output current. This can be set during
1684 * any regulator state. IOW, regulator can be disabled or enabled.
1686 * If the regulator is enabled then the current will change to the new value
1687 * immediately otherwise if the regulator is disabled the regulator will
1688 * output at the new current when enabled.
1690 * NOTE: Regulator system constraints must be set for this regulator before
1691 * calling this function otherwise this call will fail.
1693 int regulator_set_current_limit(struct regulator *regulator,
1694 int min_uA, int max_uA)
1696 struct regulator_dev *rdev = regulator->rdev;
1697 int ret;
1699 mutex_lock(&rdev->mutex);
1701 /* sanity check */
1702 if (!rdev->desc->ops->set_current_limit) {
1703 ret = -EINVAL;
1704 goto out;
1707 /* constraints check */
1708 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1709 if (ret < 0)
1710 goto out;
1712 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1713 out:
1714 mutex_unlock(&rdev->mutex);
1715 return ret;
1717 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1719 static int _regulator_get_current_limit(struct regulator_dev *rdev)
1721 int ret;
1723 mutex_lock(&rdev->mutex);
1725 /* sanity check */
1726 if (!rdev->desc->ops->get_current_limit) {
1727 ret = -EINVAL;
1728 goto out;
1731 ret = rdev->desc->ops->get_current_limit(rdev);
1732 out:
1733 mutex_unlock(&rdev->mutex);
1734 return ret;
1738 * regulator_get_current_limit - get regulator output current
1739 * @regulator: regulator source
1741 * This returns the current supplied by the specified current sink in uA.
1743 * NOTE: If the regulator is disabled it will return the current value. This
1744 * function should not be used to determine regulator state.
1746 int regulator_get_current_limit(struct regulator *regulator)
1748 return _regulator_get_current_limit(regulator->rdev);
1750 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1753 * regulator_set_mode - set regulator operating mode
1754 * @regulator: regulator source
1755 * @mode: operating mode - one of the REGULATOR_MODE constants
1757 * Set regulator operating mode to increase regulator efficiency or improve
1758 * regulation performance.
1760 * NOTE: Regulator system constraints must be set for this regulator before
1761 * calling this function otherwise this call will fail.
1763 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1765 struct regulator_dev *rdev = regulator->rdev;
1766 int ret;
1768 mutex_lock(&rdev->mutex);
1770 /* sanity check */
1771 if (!rdev->desc->ops->set_mode) {
1772 ret = -EINVAL;
1773 goto out;
1776 /* constraints check */
1777 ret = regulator_check_mode(rdev, mode);
1778 if (ret < 0)
1779 goto out;
1781 ret = rdev->desc->ops->set_mode(rdev, mode);
1782 out:
1783 mutex_unlock(&rdev->mutex);
1784 return ret;
1786 EXPORT_SYMBOL_GPL(regulator_set_mode);
1788 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1790 int ret;
1792 mutex_lock(&rdev->mutex);
1794 /* sanity check */
1795 if (!rdev->desc->ops->get_mode) {
1796 ret = -EINVAL;
1797 goto out;
1800 ret = rdev->desc->ops->get_mode(rdev);
1801 out:
1802 mutex_unlock(&rdev->mutex);
1803 return ret;
1807 * regulator_get_mode - get regulator operating mode
1808 * @regulator: regulator source
1810 * Get the current regulator operating mode.
1812 unsigned int regulator_get_mode(struct regulator *regulator)
1814 return _regulator_get_mode(regulator->rdev);
1816 EXPORT_SYMBOL_GPL(regulator_get_mode);
1819 * regulator_set_optimum_mode - set regulator optimum operating mode
1820 * @regulator: regulator source
1821 * @uA_load: load current
1823 * Notifies the regulator core of a new device load. This is then used by
1824 * DRMS (if enabled by constraints) to set the most efficient regulator
1825 * operating mode for the new regulator loading.
1827 * Consumer devices notify their supply regulator of the maximum power
1828 * they will require (can be taken from device datasheet in the power
1829 * consumption tables) when they change operational status and hence power
1830 * state. Examples of operational state changes that can affect power
1831 * consumption are :-
1833 * o Device is opened / closed.
1834 * o Device I/O is about to begin or has just finished.
1835 * o Device is idling in between work.
1837 * This information is also exported via sysfs to userspace.
1839 * DRMS will sum the total requested load on the regulator and change
1840 * to the most efficient operating mode if platform constraints allow.
1842 * Returns the new regulator mode or error.
1844 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1846 struct regulator_dev *rdev = regulator->rdev;
1847 struct regulator *consumer;
1848 int ret, output_uV, input_uV, total_uA_load = 0;
1849 unsigned int mode;
1851 mutex_lock(&rdev->mutex);
1853 regulator->uA_load = uA_load;
1854 ret = regulator_check_drms(rdev);
1855 if (ret < 0)
1856 goto out;
1857 ret = -EINVAL;
1859 /* sanity check */
1860 if (!rdev->desc->ops->get_optimum_mode)
1861 goto out;
1863 /* get output voltage */
1864 output_uV = rdev->desc->ops->get_voltage(rdev);
1865 if (output_uV <= 0) {
1866 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1867 __func__, rdev_get_name(rdev));
1868 goto out;
1871 /* get input voltage */
1872 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1873 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1874 else
1875 input_uV = rdev->constraints->input_uV;
1876 if (input_uV <= 0) {
1877 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1878 __func__, rdev_get_name(rdev));
1879 goto out;
1882 /* calc total requested load for this regulator */
1883 list_for_each_entry(consumer, &rdev->consumer_list, list)
1884 total_uA_load += consumer->uA_load;
1886 mode = rdev->desc->ops->get_optimum_mode(rdev,
1887 input_uV, output_uV,
1888 total_uA_load);
1889 ret = regulator_check_mode(rdev, mode);
1890 if (ret < 0) {
1891 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1892 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
1893 total_uA_load, input_uV, output_uV);
1894 goto out;
1897 ret = rdev->desc->ops->set_mode(rdev, mode);
1898 if (ret < 0) {
1899 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1900 __func__, mode, rdev_get_name(rdev));
1901 goto out;
1903 ret = mode;
1904 out:
1905 mutex_unlock(&rdev->mutex);
1906 return ret;
1908 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1911 * regulator_register_notifier - register regulator event notifier
1912 * @regulator: regulator source
1913 * @nb: notifier block
1915 * Register notifier block to receive regulator events.
1917 int regulator_register_notifier(struct regulator *regulator,
1918 struct notifier_block *nb)
1920 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1921 nb);
1923 EXPORT_SYMBOL_GPL(regulator_register_notifier);
1926 * regulator_unregister_notifier - unregister regulator event notifier
1927 * @regulator: regulator source
1928 * @nb: notifier block
1930 * Unregister regulator event notifier block.
1932 int regulator_unregister_notifier(struct regulator *regulator,
1933 struct notifier_block *nb)
1935 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1936 nb);
1938 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1940 /* notify regulator consumers and downstream regulator consumers.
1941 * Note mutex must be held by caller.
1943 static void _notifier_call_chain(struct regulator_dev *rdev,
1944 unsigned long event, void *data)
1946 struct regulator_dev *_rdev;
1948 /* call rdev chain first */
1949 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1951 /* now notify regulator we supply */
1952 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1953 mutex_lock(&_rdev->mutex);
1954 _notifier_call_chain(_rdev, event, data);
1955 mutex_unlock(&_rdev->mutex);
1960 * regulator_bulk_get - get multiple regulator consumers
1962 * @dev: Device to supply
1963 * @num_consumers: Number of consumers to register
1964 * @consumers: Configuration of consumers; clients are stored here.
1966 * @return 0 on success, an errno on failure.
1968 * This helper function allows drivers to get several regulator
1969 * consumers in one operation. If any of the regulators cannot be
1970 * acquired then any regulators that were allocated will be freed
1971 * before returning to the caller.
1973 int regulator_bulk_get(struct device *dev, int num_consumers,
1974 struct regulator_bulk_data *consumers)
1976 int i;
1977 int ret;
1979 for (i = 0; i < num_consumers; i++)
1980 consumers[i].consumer = NULL;
1982 for (i = 0; i < num_consumers; i++) {
1983 consumers[i].consumer = regulator_get(dev,
1984 consumers[i].supply);
1985 if (IS_ERR(consumers[i].consumer)) {
1986 ret = PTR_ERR(consumers[i].consumer);
1987 dev_err(dev, "Failed to get supply '%s': %d\n",
1988 consumers[i].supply, ret);
1989 consumers[i].consumer = NULL;
1990 goto err;
1994 return 0;
1996 err:
1997 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1998 regulator_put(consumers[i].consumer);
2000 return ret;
2002 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2005 * regulator_bulk_enable - enable multiple regulator consumers
2007 * @num_consumers: Number of consumers
2008 * @consumers: Consumer data; clients are stored here.
2009 * @return 0 on success, an errno on failure
2011 * This convenience API allows consumers to enable multiple regulator
2012 * clients in a single API call. If any consumers cannot be enabled
2013 * then any others that were enabled will be disabled again prior to
2014 * return.
2016 int regulator_bulk_enable(int num_consumers,
2017 struct regulator_bulk_data *consumers)
2019 int i;
2020 int ret;
2022 for (i = 0; i < num_consumers; i++) {
2023 ret = regulator_enable(consumers[i].consumer);
2024 if (ret != 0)
2025 goto err;
2028 return 0;
2030 err:
2031 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
2032 for (--i; i >= 0; --i)
2033 regulator_disable(consumers[i].consumer);
2035 return ret;
2037 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2040 * regulator_bulk_disable - disable multiple regulator consumers
2042 * @num_consumers: Number of consumers
2043 * @consumers: Consumer data; clients are stored here.
2044 * @return 0 on success, an errno on failure
2046 * This convenience API allows consumers to disable multiple regulator
2047 * clients in a single API call. If any consumers cannot be enabled
2048 * then any others that were disabled will be disabled again prior to
2049 * return.
2051 int regulator_bulk_disable(int num_consumers,
2052 struct regulator_bulk_data *consumers)
2054 int i;
2055 int ret;
2057 for (i = 0; i < num_consumers; i++) {
2058 ret = regulator_disable(consumers[i].consumer);
2059 if (ret != 0)
2060 goto err;
2063 return 0;
2065 err:
2066 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2067 ret);
2068 for (--i; i >= 0; --i)
2069 regulator_enable(consumers[i].consumer);
2071 return ret;
2073 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2076 * regulator_bulk_free - free multiple regulator consumers
2078 * @num_consumers: Number of consumers
2079 * @consumers: Consumer data; clients are stored here.
2081 * This convenience API allows consumers to free multiple regulator
2082 * clients in a single API call.
2084 void regulator_bulk_free(int num_consumers,
2085 struct regulator_bulk_data *consumers)
2087 int i;
2089 for (i = 0; i < num_consumers; i++) {
2090 regulator_put(consumers[i].consumer);
2091 consumers[i].consumer = NULL;
2094 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2097 * regulator_notifier_call_chain - call regulator event notifier
2098 * @rdev: regulator source
2099 * @event: notifier block
2100 * @data: callback-specific data.
2102 * Called by regulator drivers to notify clients a regulator event has
2103 * occurred. We also notify regulator clients downstream.
2104 * Note lock must be held by caller.
2106 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2107 unsigned long event, void *data)
2109 _notifier_call_chain(rdev, event, data);
2110 return NOTIFY_DONE;
2113 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2116 * regulator_mode_to_status - convert a regulator mode into a status
2118 * @mode: Mode to convert
2120 * Convert a regulator mode into a status.
2122 int regulator_mode_to_status(unsigned int mode)
2124 switch (mode) {
2125 case REGULATOR_MODE_FAST:
2126 return REGULATOR_STATUS_FAST;
2127 case REGULATOR_MODE_NORMAL:
2128 return REGULATOR_STATUS_NORMAL;
2129 case REGULATOR_MODE_IDLE:
2130 return REGULATOR_STATUS_IDLE;
2131 case REGULATOR_STATUS_STANDBY:
2132 return REGULATOR_STATUS_STANDBY;
2133 default:
2134 return 0;
2137 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2140 * To avoid cluttering sysfs (and memory) with useless state, only
2141 * create attributes that can be meaningfully displayed.
2143 static int add_regulator_attributes(struct regulator_dev *rdev)
2145 struct device *dev = &rdev->dev;
2146 struct regulator_ops *ops = rdev->desc->ops;
2147 int status = 0;
2149 /* some attributes need specific methods to be displayed */
2150 if (ops->get_voltage) {
2151 status = device_create_file(dev, &dev_attr_microvolts);
2152 if (status < 0)
2153 return status;
2155 if (ops->get_current_limit) {
2156 status = device_create_file(dev, &dev_attr_microamps);
2157 if (status < 0)
2158 return status;
2160 if (ops->get_mode) {
2161 status = device_create_file(dev, &dev_attr_opmode);
2162 if (status < 0)
2163 return status;
2165 if (ops->is_enabled) {
2166 status = device_create_file(dev, &dev_attr_state);
2167 if (status < 0)
2168 return status;
2170 if (ops->get_status) {
2171 status = device_create_file(dev, &dev_attr_status);
2172 if (status < 0)
2173 return status;
2176 /* some attributes are type-specific */
2177 if (rdev->desc->type == REGULATOR_CURRENT) {
2178 status = device_create_file(dev, &dev_attr_requested_microamps);
2179 if (status < 0)
2180 return status;
2183 /* all the other attributes exist to support constraints;
2184 * don't show them if there are no constraints, or if the
2185 * relevant supporting methods are missing.
2187 if (!rdev->constraints)
2188 return status;
2190 /* constraints need specific supporting methods */
2191 if (ops->set_voltage) {
2192 status = device_create_file(dev, &dev_attr_min_microvolts);
2193 if (status < 0)
2194 return status;
2195 status = device_create_file(dev, &dev_attr_max_microvolts);
2196 if (status < 0)
2197 return status;
2199 if (ops->set_current_limit) {
2200 status = device_create_file(dev, &dev_attr_min_microamps);
2201 if (status < 0)
2202 return status;
2203 status = device_create_file(dev, &dev_attr_max_microamps);
2204 if (status < 0)
2205 return status;
2208 /* suspend mode constraints need multiple supporting methods */
2209 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2210 return status;
2212 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2213 if (status < 0)
2214 return status;
2215 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2216 if (status < 0)
2217 return status;
2218 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2219 if (status < 0)
2220 return status;
2222 if (ops->set_suspend_voltage) {
2223 status = device_create_file(dev,
2224 &dev_attr_suspend_standby_microvolts);
2225 if (status < 0)
2226 return status;
2227 status = device_create_file(dev,
2228 &dev_attr_suspend_mem_microvolts);
2229 if (status < 0)
2230 return status;
2231 status = device_create_file(dev,
2232 &dev_attr_suspend_disk_microvolts);
2233 if (status < 0)
2234 return status;
2237 if (ops->set_suspend_mode) {
2238 status = device_create_file(dev,
2239 &dev_attr_suspend_standby_mode);
2240 if (status < 0)
2241 return status;
2242 status = device_create_file(dev,
2243 &dev_attr_suspend_mem_mode);
2244 if (status < 0)
2245 return status;
2246 status = device_create_file(dev,
2247 &dev_attr_suspend_disk_mode);
2248 if (status < 0)
2249 return status;
2252 return status;
2256 * regulator_register - register regulator
2257 * @regulator_desc: regulator to register
2258 * @dev: struct device for the regulator
2259 * @init_data: platform provided init data, passed through by driver
2260 * @driver_data: private regulator data
2262 * Called by regulator drivers to register a regulator.
2263 * Returns 0 on success.
2265 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2266 struct device *dev, struct regulator_init_data *init_data,
2267 void *driver_data)
2269 static atomic_t regulator_no = ATOMIC_INIT(0);
2270 struct regulator_dev *rdev;
2271 int ret, i;
2273 if (regulator_desc == NULL)
2274 return ERR_PTR(-EINVAL);
2276 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2277 return ERR_PTR(-EINVAL);
2279 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2280 regulator_desc->type != REGULATOR_CURRENT)
2281 return ERR_PTR(-EINVAL);
2283 if (!init_data)
2284 return ERR_PTR(-EINVAL);
2286 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2287 if (rdev == NULL)
2288 return ERR_PTR(-ENOMEM);
2290 mutex_lock(&regulator_list_mutex);
2292 mutex_init(&rdev->mutex);
2293 rdev->reg_data = driver_data;
2294 rdev->owner = regulator_desc->owner;
2295 rdev->desc = regulator_desc;
2296 INIT_LIST_HEAD(&rdev->consumer_list);
2297 INIT_LIST_HEAD(&rdev->supply_list);
2298 INIT_LIST_HEAD(&rdev->list);
2299 INIT_LIST_HEAD(&rdev->slist);
2300 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2302 /* preform any regulator specific init */
2303 if (init_data->regulator_init) {
2304 ret = init_data->regulator_init(rdev->reg_data);
2305 if (ret < 0)
2306 goto clean;
2309 /* register with sysfs */
2310 rdev->dev.class = &regulator_class;
2311 rdev->dev.parent = dev;
2312 dev_set_name(&rdev->dev, "regulator.%d",
2313 atomic_inc_return(&regulator_no) - 1);
2314 ret = device_register(&rdev->dev);
2315 if (ret != 0)
2316 goto clean;
2318 dev_set_drvdata(&rdev->dev, rdev);
2320 /* set regulator constraints */
2321 ret = set_machine_constraints(rdev, &init_data->constraints);
2322 if (ret < 0)
2323 goto scrub;
2325 /* add attributes supported by this regulator */
2326 ret = add_regulator_attributes(rdev);
2327 if (ret < 0)
2328 goto scrub;
2330 /* set supply regulator if it exists */
2331 if (init_data->supply_regulator_dev) {
2332 ret = set_supply(rdev,
2333 dev_get_drvdata(init_data->supply_regulator_dev));
2334 if (ret < 0)
2335 goto scrub;
2338 /* add consumers devices */
2339 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2340 ret = set_consumer_device_supply(rdev,
2341 init_data->consumer_supplies[i].dev,
2342 init_data->consumer_supplies[i].dev_name,
2343 init_data->consumer_supplies[i].supply);
2344 if (ret < 0) {
2345 for (--i; i >= 0; i--)
2346 unset_consumer_device_supply(rdev,
2347 init_data->consumer_supplies[i].dev_name,
2348 init_data->consumer_supplies[i].dev);
2349 goto scrub;
2353 list_add(&rdev->list, &regulator_list);
2354 out:
2355 mutex_unlock(&regulator_list_mutex);
2356 return rdev;
2358 scrub:
2359 device_unregister(&rdev->dev);
2360 /* device core frees rdev */
2361 rdev = ERR_PTR(ret);
2362 goto out;
2364 clean:
2365 kfree(rdev);
2366 rdev = ERR_PTR(ret);
2367 goto out;
2369 EXPORT_SYMBOL_GPL(regulator_register);
2372 * regulator_unregister - unregister regulator
2373 * @rdev: regulator to unregister
2375 * Called by regulator drivers to unregister a regulator.
2377 void regulator_unregister(struct regulator_dev *rdev)
2379 if (rdev == NULL)
2380 return;
2382 mutex_lock(&regulator_list_mutex);
2383 WARN_ON(rdev->open_count);
2384 unset_regulator_supplies(rdev);
2385 list_del(&rdev->list);
2386 if (rdev->supply)
2387 sysfs_remove_link(&rdev->dev.kobj, "supply");
2388 device_unregister(&rdev->dev);
2389 mutex_unlock(&regulator_list_mutex);
2391 EXPORT_SYMBOL_GPL(regulator_unregister);
2394 * regulator_suspend_prepare - prepare regulators for system wide suspend
2395 * @state: system suspend state
2397 * Configure each regulator with it's suspend operating parameters for state.
2398 * This will usually be called by machine suspend code prior to supending.
2400 int regulator_suspend_prepare(suspend_state_t state)
2402 struct regulator_dev *rdev;
2403 int ret = 0;
2405 /* ON is handled by regulator active state */
2406 if (state == PM_SUSPEND_ON)
2407 return -EINVAL;
2409 mutex_lock(&regulator_list_mutex);
2410 list_for_each_entry(rdev, &regulator_list, list) {
2412 mutex_lock(&rdev->mutex);
2413 ret = suspend_prepare(rdev, state);
2414 mutex_unlock(&rdev->mutex);
2416 if (ret < 0) {
2417 printk(KERN_ERR "%s: failed to prepare %s\n",
2418 __func__, rdev_get_name(rdev));
2419 goto out;
2422 out:
2423 mutex_unlock(&regulator_list_mutex);
2424 return ret;
2426 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2429 * regulator_has_full_constraints - the system has fully specified constraints
2431 * Calling this function will cause the regulator API to disable all
2432 * regulators which have a zero use count and don't have an always_on
2433 * constraint in a late_initcall.
2435 * The intention is that this will become the default behaviour in a
2436 * future kernel release so users are encouraged to use this facility
2437 * now.
2439 void regulator_has_full_constraints(void)
2441 has_full_constraints = 1;
2443 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2446 * rdev_get_drvdata - get rdev regulator driver data
2447 * @rdev: regulator
2449 * Get rdev regulator driver private data. This call can be used in the
2450 * regulator driver context.
2452 void *rdev_get_drvdata(struct regulator_dev *rdev)
2454 return rdev->reg_data;
2456 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2459 * regulator_get_drvdata - get regulator driver data
2460 * @regulator: regulator
2462 * Get regulator driver private data. This call can be used in the consumer
2463 * driver context when non API regulator specific functions need to be called.
2465 void *regulator_get_drvdata(struct regulator *regulator)
2467 return regulator->rdev->reg_data;
2469 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2472 * regulator_set_drvdata - set regulator driver data
2473 * @regulator: regulator
2474 * @data: data
2476 void regulator_set_drvdata(struct regulator *regulator, void *data)
2478 regulator->rdev->reg_data = data;
2480 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2483 * regulator_get_id - get regulator ID
2484 * @rdev: regulator
2486 int rdev_get_id(struct regulator_dev *rdev)
2488 return rdev->desc->id;
2490 EXPORT_SYMBOL_GPL(rdev_get_id);
2492 struct device *rdev_get_dev(struct regulator_dev *rdev)
2494 return &rdev->dev;
2496 EXPORT_SYMBOL_GPL(rdev_get_dev);
2498 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2500 return reg_init_data->driver_data;
2502 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2504 static int __init regulator_init(void)
2506 int ret;
2508 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2510 ret = class_register(&regulator_class);
2512 regulator_dummy_init();
2514 return ret;
2517 /* init early to allow our consumers to complete system booting */
2518 core_initcall(regulator_init);
2520 static int __init regulator_init_complete(void)
2522 struct regulator_dev *rdev;
2523 struct regulator_ops *ops;
2524 struct regulation_constraints *c;
2525 int enabled, ret;
2526 const char *name;
2528 mutex_lock(&regulator_list_mutex);
2530 /* If we have a full configuration then disable any regulators
2531 * which are not in use or always_on. This will become the
2532 * default behaviour in the future.
2534 list_for_each_entry(rdev, &regulator_list, list) {
2535 ops = rdev->desc->ops;
2536 c = rdev->constraints;
2538 name = rdev_get_name(rdev);
2540 if (!ops->disable || (c && c->always_on))
2541 continue;
2543 mutex_lock(&rdev->mutex);
2545 if (rdev->use_count)
2546 goto unlock;
2548 /* If we can't read the status assume it's on. */
2549 if (ops->is_enabled)
2550 enabled = ops->is_enabled(rdev);
2551 else
2552 enabled = 1;
2554 if (!enabled)
2555 goto unlock;
2557 if (has_full_constraints) {
2558 /* We log since this may kill the system if it
2559 * goes wrong. */
2560 printk(KERN_INFO "%s: disabling %s\n",
2561 __func__, name);
2562 ret = ops->disable(rdev);
2563 if (ret != 0) {
2564 printk(KERN_ERR
2565 "%s: couldn't disable %s: %d\n",
2566 __func__, name, ret);
2568 } else {
2569 /* The intention is that in future we will
2570 * assume that full constraints are provided
2571 * so warn even if we aren't going to do
2572 * anything here.
2574 printk(KERN_WARNING
2575 "%s: incomplete constraints, leaving %s on\n",
2576 __func__, name);
2579 unlock:
2580 mutex_unlock(&rdev->mutex);
2583 mutex_unlock(&regulator_list_mutex);
2585 return 0;
2587 late_initcall(regulator_init_complete);