Merge branch 'v4l_for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[zen-stable.git] / drivers / regulator / core.c
blob938398f3e869c57814fc130b9133ac435118214e
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 #define pr_fmt(fmt) "%s: " fmt, __func__
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/slab.h>
23 #include <linux/async.h>
24 #include <linux/err.h>
25 #include <linux/mutex.h>
26 #include <linux/suspend.h>
27 #include <linux/delay.h>
28 #include <linux/regulator/consumer.h>
29 #include <linux/regulator/driver.h>
30 #include <linux/regulator/machine.h>
31 #include <linux/module.h>
33 #define CREATE_TRACE_POINTS
34 #include <trace/events/regulator.h>
36 #include "dummy.h"
38 #define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_list);
51 static LIST_HEAD(regulator_map_list);
52 static bool has_full_constraints;
53 static bool board_wants_dummy_regulator;
55 #ifdef CONFIG_DEBUG_FS
56 static struct dentry *debugfs_root;
57 #endif
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
66 const char *dev_name; /* The dev_name() for the consumer */
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 char *supply_name;
83 struct device_attribute dev_attr;
84 struct regulator_dev *rdev;
85 #ifdef CONFIG_DEBUG_FS
86 struct dentry *debugfs;
87 #endif
90 static int _regulator_is_enabled(struct regulator_dev *rdev);
91 static int _regulator_disable(struct regulator_dev *rdev);
92 static int _regulator_get_voltage(struct regulator_dev *rdev);
93 static int _regulator_get_current_limit(struct regulator_dev *rdev);
94 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95 static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
97 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
99 static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
103 static const char *rdev_get_name(struct regulator_dev *rdev)
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
113 /* gets the regulator for a given consumer device */
114 static struct regulator *get_device_regulator(struct device *dev)
116 struct regulator *regulator = NULL;
117 struct regulator_dev *rdev;
119 mutex_lock(&regulator_list_mutex);
120 list_for_each_entry(rdev, &regulator_list, list) {
121 mutex_lock(&rdev->mutex);
122 list_for_each_entry(regulator, &rdev->consumer_list, list) {
123 if (regulator->dev == dev) {
124 mutex_unlock(&rdev->mutex);
125 mutex_unlock(&regulator_list_mutex);
126 return regulator;
129 mutex_unlock(&rdev->mutex);
131 mutex_unlock(&regulator_list_mutex);
132 return NULL;
135 /* Platform voltage constraint check */
136 static int regulator_check_voltage(struct regulator_dev *rdev,
137 int *min_uV, int *max_uV)
139 BUG_ON(*min_uV > *max_uV);
141 if (!rdev->constraints) {
142 rdev_err(rdev, "no constraints\n");
143 return -ENODEV;
145 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
146 rdev_err(rdev, "operation not allowed\n");
147 return -EPERM;
150 if (*max_uV > rdev->constraints->max_uV)
151 *max_uV = rdev->constraints->max_uV;
152 if (*min_uV < rdev->constraints->min_uV)
153 *min_uV = rdev->constraints->min_uV;
155 if (*min_uV > *max_uV) {
156 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
157 *min_uV, *max_uV);
158 return -EINVAL;
161 return 0;
164 /* Make sure we select a voltage that suits the needs of all
165 * regulator consumers
167 static int regulator_check_consumers(struct regulator_dev *rdev,
168 int *min_uV, int *max_uV)
170 struct regulator *regulator;
172 list_for_each_entry(regulator, &rdev->consumer_list, list) {
174 * Assume consumers that didn't say anything are OK
175 * with anything in the constraint range.
177 if (!regulator->min_uV && !regulator->max_uV)
178 continue;
180 if (*max_uV > regulator->max_uV)
181 *max_uV = regulator->max_uV;
182 if (*min_uV < regulator->min_uV)
183 *min_uV = regulator->min_uV;
186 if (*min_uV > *max_uV)
187 return -EINVAL;
189 return 0;
192 /* current constraint check */
193 static int regulator_check_current_limit(struct regulator_dev *rdev,
194 int *min_uA, int *max_uA)
196 BUG_ON(*min_uA > *max_uA);
198 if (!rdev->constraints) {
199 rdev_err(rdev, "no constraints\n");
200 return -ENODEV;
202 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
203 rdev_err(rdev, "operation not allowed\n");
204 return -EPERM;
207 if (*max_uA > rdev->constraints->max_uA)
208 *max_uA = rdev->constraints->max_uA;
209 if (*min_uA < rdev->constraints->min_uA)
210 *min_uA = rdev->constraints->min_uA;
212 if (*min_uA > *max_uA) {
213 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
214 *min_uA, *max_uA);
215 return -EINVAL;
218 return 0;
221 /* operating mode constraint check */
222 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
224 switch (*mode) {
225 case REGULATOR_MODE_FAST:
226 case REGULATOR_MODE_NORMAL:
227 case REGULATOR_MODE_IDLE:
228 case REGULATOR_MODE_STANDBY:
229 break;
230 default:
231 rdev_err(rdev, "invalid mode %x specified\n", *mode);
232 return -EINVAL;
235 if (!rdev->constraints) {
236 rdev_err(rdev, "no constraints\n");
237 return -ENODEV;
239 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
240 rdev_err(rdev, "operation not allowed\n");
241 return -EPERM;
244 /* The modes are bitmasks, the most power hungry modes having
245 * the lowest values. If the requested mode isn't supported
246 * try higher modes. */
247 while (*mode) {
248 if (rdev->constraints->valid_modes_mask & *mode)
249 return 0;
250 *mode /= 2;
253 return -EINVAL;
256 /* dynamic regulator mode switching constraint check */
257 static int regulator_check_drms(struct regulator_dev *rdev)
259 if (!rdev->constraints) {
260 rdev_err(rdev, "no constraints\n");
261 return -ENODEV;
263 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
264 rdev_err(rdev, "operation not allowed\n");
265 return -EPERM;
267 return 0;
270 static ssize_t device_requested_uA_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct regulator *regulator;
275 regulator = get_device_regulator(dev);
276 if (regulator == NULL)
277 return 0;
279 return sprintf(buf, "%d\n", regulator->uA_load);
282 static ssize_t regulator_uV_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
285 struct regulator_dev *rdev = dev_get_drvdata(dev);
286 ssize_t ret;
288 mutex_lock(&rdev->mutex);
289 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
290 mutex_unlock(&rdev->mutex);
292 return ret;
294 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
296 static ssize_t regulator_uA_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
299 struct regulator_dev *rdev = dev_get_drvdata(dev);
301 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
303 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
305 static ssize_t regulator_name_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
308 struct regulator_dev *rdev = dev_get_drvdata(dev);
310 return sprintf(buf, "%s\n", rdev_get_name(rdev));
313 static ssize_t regulator_print_opmode(char *buf, int mode)
315 switch (mode) {
316 case REGULATOR_MODE_FAST:
317 return sprintf(buf, "fast\n");
318 case REGULATOR_MODE_NORMAL:
319 return sprintf(buf, "normal\n");
320 case REGULATOR_MODE_IDLE:
321 return sprintf(buf, "idle\n");
322 case REGULATOR_MODE_STANDBY:
323 return sprintf(buf, "standby\n");
325 return sprintf(buf, "unknown\n");
328 static ssize_t regulator_opmode_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
331 struct regulator_dev *rdev = dev_get_drvdata(dev);
333 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
335 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
337 static ssize_t regulator_print_state(char *buf, int state)
339 if (state > 0)
340 return sprintf(buf, "enabled\n");
341 else if (state == 0)
342 return sprintf(buf, "disabled\n");
343 else
344 return sprintf(buf, "unknown\n");
347 static ssize_t regulator_state_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 ssize_t ret;
353 mutex_lock(&rdev->mutex);
354 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
355 mutex_unlock(&rdev->mutex);
357 return ret;
359 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
361 static ssize_t regulator_status_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 struct regulator_dev *rdev = dev_get_drvdata(dev);
365 int status;
366 char *label;
368 status = rdev->desc->ops->get_status(rdev);
369 if (status < 0)
370 return status;
372 switch (status) {
373 case REGULATOR_STATUS_OFF:
374 label = "off";
375 break;
376 case REGULATOR_STATUS_ON:
377 label = "on";
378 break;
379 case REGULATOR_STATUS_ERROR:
380 label = "error";
381 break;
382 case REGULATOR_STATUS_FAST:
383 label = "fast";
384 break;
385 case REGULATOR_STATUS_NORMAL:
386 label = "normal";
387 break;
388 case REGULATOR_STATUS_IDLE:
389 label = "idle";
390 break;
391 case REGULATOR_STATUS_STANDBY:
392 label = "standby";
393 break;
394 default:
395 return -ERANGE;
398 return sprintf(buf, "%s\n", label);
400 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
402 static ssize_t regulator_min_uA_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
405 struct regulator_dev *rdev = dev_get_drvdata(dev);
407 if (!rdev->constraints)
408 return sprintf(buf, "constraint not defined\n");
410 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
412 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
414 static ssize_t regulator_max_uA_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
417 struct regulator_dev *rdev = dev_get_drvdata(dev);
419 if (!rdev->constraints)
420 return sprintf(buf, "constraint not defined\n");
422 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
424 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
426 static ssize_t regulator_min_uV_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 if (!rdev->constraints)
432 return sprintf(buf, "constraint not defined\n");
434 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
436 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
438 static ssize_t regulator_max_uV_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct regulator_dev *rdev = dev_get_drvdata(dev);
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
446 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
448 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
450 static ssize_t regulator_total_uA_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 struct regulator *regulator;
455 int uA = 0;
457 mutex_lock(&rdev->mutex);
458 list_for_each_entry(regulator, &rdev->consumer_list, list)
459 uA += regulator->uA_load;
460 mutex_unlock(&rdev->mutex);
461 return sprintf(buf, "%d\n", uA);
463 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
465 static ssize_t regulator_num_users_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
469 return sprintf(buf, "%d\n", rdev->use_count);
472 static ssize_t regulator_type_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
475 struct regulator_dev *rdev = dev_get_drvdata(dev);
477 switch (rdev->desc->type) {
478 case REGULATOR_VOLTAGE:
479 return sprintf(buf, "voltage\n");
480 case REGULATOR_CURRENT:
481 return sprintf(buf, "current\n");
483 return sprintf(buf, "unknown\n");
486 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct regulator_dev *rdev = dev_get_drvdata(dev);
491 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
493 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
494 regulator_suspend_mem_uV_show, NULL);
496 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 struct regulator_dev *rdev = dev_get_drvdata(dev);
501 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
503 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
504 regulator_suspend_disk_uV_show, NULL);
506 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 struct regulator_dev *rdev = dev_get_drvdata(dev);
511 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
513 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
514 regulator_suspend_standby_uV_show, NULL);
516 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
519 struct regulator_dev *rdev = dev_get_drvdata(dev);
521 return regulator_print_opmode(buf,
522 rdev->constraints->state_mem.mode);
524 static DEVICE_ATTR(suspend_mem_mode, 0444,
525 regulator_suspend_mem_mode_show, NULL);
527 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
530 struct regulator_dev *rdev = dev_get_drvdata(dev);
532 return regulator_print_opmode(buf,
533 rdev->constraints->state_disk.mode);
535 static DEVICE_ATTR(suspend_disk_mode, 0444,
536 regulator_suspend_disk_mode_show, NULL);
538 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
541 struct regulator_dev *rdev = dev_get_drvdata(dev);
543 return regulator_print_opmode(buf,
544 rdev->constraints->state_standby.mode);
546 static DEVICE_ATTR(suspend_standby_mode, 0444,
547 regulator_suspend_standby_mode_show, NULL);
549 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
550 struct device_attribute *attr, char *buf)
552 struct regulator_dev *rdev = dev_get_drvdata(dev);
554 return regulator_print_state(buf,
555 rdev->constraints->state_mem.enabled);
557 static DEVICE_ATTR(suspend_mem_state, 0444,
558 regulator_suspend_mem_state_show, NULL);
560 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct regulator_dev *rdev = dev_get_drvdata(dev);
565 return regulator_print_state(buf,
566 rdev->constraints->state_disk.enabled);
568 static DEVICE_ATTR(suspend_disk_state, 0444,
569 regulator_suspend_disk_state_show, NULL);
571 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
574 struct regulator_dev *rdev = dev_get_drvdata(dev);
576 return regulator_print_state(buf,
577 rdev->constraints->state_standby.enabled);
579 static DEVICE_ATTR(suspend_standby_state, 0444,
580 regulator_suspend_standby_state_show, NULL);
584 * These are the only attributes are present for all regulators.
585 * Other attributes are a function of regulator functionality.
587 static struct device_attribute regulator_dev_attrs[] = {
588 __ATTR(name, 0444, regulator_name_show, NULL),
589 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
590 __ATTR(type, 0444, regulator_type_show, NULL),
591 __ATTR_NULL,
594 static void regulator_dev_release(struct device *dev)
596 struct regulator_dev *rdev = dev_get_drvdata(dev);
597 kfree(rdev);
600 static struct class regulator_class = {
601 .name = "regulator",
602 .dev_release = regulator_dev_release,
603 .dev_attrs = regulator_dev_attrs,
606 /* Calculate the new optimum regulator operating mode based on the new total
607 * consumer load. All locks held by caller */
608 static void drms_uA_update(struct regulator_dev *rdev)
610 struct regulator *sibling;
611 int current_uA = 0, output_uV, input_uV, err;
612 unsigned int mode;
614 err = regulator_check_drms(rdev);
615 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
616 (!rdev->desc->ops->get_voltage &&
617 !rdev->desc->ops->get_voltage_sel) ||
618 !rdev->desc->ops->set_mode)
619 return;
621 /* get output voltage */
622 output_uV = _regulator_get_voltage(rdev);
623 if (output_uV <= 0)
624 return;
626 /* get input voltage */
627 input_uV = 0;
628 if (rdev->supply)
629 input_uV = _regulator_get_voltage(rdev);
630 if (input_uV <= 0)
631 input_uV = rdev->constraints->input_uV;
632 if (input_uV <= 0)
633 return;
635 /* calc total requested load */
636 list_for_each_entry(sibling, &rdev->consumer_list, list)
637 current_uA += sibling->uA_load;
639 /* now get the optimum mode for our new total regulator load */
640 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
641 output_uV, current_uA);
643 /* check the new mode is allowed */
644 err = regulator_mode_constrain(rdev, &mode);
645 if (err == 0)
646 rdev->desc->ops->set_mode(rdev, mode);
649 static int suspend_set_state(struct regulator_dev *rdev,
650 struct regulator_state *rstate)
652 int ret = 0;
653 bool can_set_state;
655 can_set_state = rdev->desc->ops->set_suspend_enable &&
656 rdev->desc->ops->set_suspend_disable;
658 /* If we have no suspend mode configration don't set anything;
659 * only warn if the driver actually makes the suspend mode
660 * configurable.
662 if (!rstate->enabled && !rstate->disabled) {
663 if (can_set_state)
664 rdev_warn(rdev, "No configuration\n");
665 return 0;
668 if (rstate->enabled && rstate->disabled) {
669 rdev_err(rdev, "invalid configuration\n");
670 return -EINVAL;
673 if (!can_set_state) {
674 rdev_err(rdev, "no way to set suspend state\n");
675 return -EINVAL;
678 if (rstate->enabled)
679 ret = rdev->desc->ops->set_suspend_enable(rdev);
680 else
681 ret = rdev->desc->ops->set_suspend_disable(rdev);
682 if (ret < 0) {
683 rdev_err(rdev, "failed to enabled/disable\n");
684 return ret;
687 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
688 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
689 if (ret < 0) {
690 rdev_err(rdev, "failed to set voltage\n");
691 return ret;
695 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
696 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
697 if (ret < 0) {
698 rdev_err(rdev, "failed to set mode\n");
699 return ret;
702 return ret;
705 /* locks held by caller */
706 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
708 if (!rdev->constraints)
709 return -EINVAL;
711 switch (state) {
712 case PM_SUSPEND_STANDBY:
713 return suspend_set_state(rdev,
714 &rdev->constraints->state_standby);
715 case PM_SUSPEND_MEM:
716 return suspend_set_state(rdev,
717 &rdev->constraints->state_mem);
718 case PM_SUSPEND_MAX:
719 return suspend_set_state(rdev,
720 &rdev->constraints->state_disk);
721 default:
722 return -EINVAL;
726 static void print_constraints(struct regulator_dev *rdev)
728 struct regulation_constraints *constraints = rdev->constraints;
729 char buf[80] = "";
730 int count = 0;
731 int ret;
733 if (constraints->min_uV && constraints->max_uV) {
734 if (constraints->min_uV == constraints->max_uV)
735 count += sprintf(buf + count, "%d mV ",
736 constraints->min_uV / 1000);
737 else
738 count += sprintf(buf + count, "%d <--> %d mV ",
739 constraints->min_uV / 1000,
740 constraints->max_uV / 1000);
743 if (!constraints->min_uV ||
744 constraints->min_uV != constraints->max_uV) {
745 ret = _regulator_get_voltage(rdev);
746 if (ret > 0)
747 count += sprintf(buf + count, "at %d mV ", ret / 1000);
750 if (constraints->uV_offset)
751 count += sprintf(buf, "%dmV offset ",
752 constraints->uV_offset / 1000);
754 if (constraints->min_uA && constraints->max_uA) {
755 if (constraints->min_uA == constraints->max_uA)
756 count += sprintf(buf + count, "%d mA ",
757 constraints->min_uA / 1000);
758 else
759 count += sprintf(buf + count, "%d <--> %d mA ",
760 constraints->min_uA / 1000,
761 constraints->max_uA / 1000);
764 if (!constraints->min_uA ||
765 constraints->min_uA != constraints->max_uA) {
766 ret = _regulator_get_current_limit(rdev);
767 if (ret > 0)
768 count += sprintf(buf + count, "at %d mA ", ret / 1000);
771 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
772 count += sprintf(buf + count, "fast ");
773 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
774 count += sprintf(buf + count, "normal ");
775 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
776 count += sprintf(buf + count, "idle ");
777 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
778 count += sprintf(buf + count, "standby");
780 rdev_info(rdev, "%s\n", buf);
783 static int machine_constraints_voltage(struct regulator_dev *rdev,
784 struct regulation_constraints *constraints)
786 struct regulator_ops *ops = rdev->desc->ops;
787 int ret;
789 /* do we need to apply the constraint voltage */
790 if (rdev->constraints->apply_uV &&
791 rdev->constraints->min_uV == rdev->constraints->max_uV) {
792 ret = _regulator_do_set_voltage(rdev,
793 rdev->constraints->min_uV,
794 rdev->constraints->max_uV);
795 if (ret < 0) {
796 rdev_err(rdev, "failed to apply %duV constraint\n",
797 rdev->constraints->min_uV);
798 return ret;
802 /* constrain machine-level voltage specs to fit
803 * the actual range supported by this regulator.
805 if (ops->list_voltage && rdev->desc->n_voltages) {
806 int count = rdev->desc->n_voltages;
807 int i;
808 int min_uV = INT_MAX;
809 int max_uV = INT_MIN;
810 int cmin = constraints->min_uV;
811 int cmax = constraints->max_uV;
813 /* it's safe to autoconfigure fixed-voltage supplies
814 and the constraints are used by list_voltage. */
815 if (count == 1 && !cmin) {
816 cmin = 1;
817 cmax = INT_MAX;
818 constraints->min_uV = cmin;
819 constraints->max_uV = cmax;
822 /* voltage constraints are optional */
823 if ((cmin == 0) && (cmax == 0))
824 return 0;
826 /* else require explicit machine-level constraints */
827 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
828 rdev_err(rdev, "invalid voltage constraints\n");
829 return -EINVAL;
832 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
833 for (i = 0; i < count; i++) {
834 int value;
836 value = ops->list_voltage(rdev, i);
837 if (value <= 0)
838 continue;
840 /* maybe adjust [min_uV..max_uV] */
841 if (value >= cmin && value < min_uV)
842 min_uV = value;
843 if (value <= cmax && value > max_uV)
844 max_uV = value;
847 /* final: [min_uV..max_uV] valid iff constraints valid */
848 if (max_uV < min_uV) {
849 rdev_err(rdev, "unsupportable voltage constraints\n");
850 return -EINVAL;
853 /* use regulator's subset of machine constraints */
854 if (constraints->min_uV < min_uV) {
855 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
856 constraints->min_uV, min_uV);
857 constraints->min_uV = min_uV;
859 if (constraints->max_uV > max_uV) {
860 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
861 constraints->max_uV, max_uV);
862 constraints->max_uV = max_uV;
866 return 0;
870 * set_machine_constraints - sets regulator constraints
871 * @rdev: regulator source
872 * @constraints: constraints to apply
874 * Allows platform initialisation code to define and constrain
875 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
876 * Constraints *must* be set by platform code in order for some
877 * regulator operations to proceed i.e. set_voltage, set_current_limit,
878 * set_mode.
880 static int set_machine_constraints(struct regulator_dev *rdev,
881 const struct regulation_constraints *constraints)
883 int ret = 0;
884 struct regulator_ops *ops = rdev->desc->ops;
886 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
887 GFP_KERNEL);
888 if (!rdev->constraints)
889 return -ENOMEM;
891 ret = machine_constraints_voltage(rdev, rdev->constraints);
892 if (ret != 0)
893 goto out;
895 /* do we need to setup our suspend state */
896 if (constraints->initial_state) {
897 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
898 if (ret < 0) {
899 rdev_err(rdev, "failed to set suspend state\n");
900 goto out;
904 if (constraints->initial_mode) {
905 if (!ops->set_mode) {
906 rdev_err(rdev, "no set_mode operation\n");
907 ret = -EINVAL;
908 goto out;
911 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
912 if (ret < 0) {
913 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
914 goto out;
918 /* If the constraints say the regulator should be on at this point
919 * and we have control then make sure it is enabled.
921 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
922 ops->enable) {
923 ret = ops->enable(rdev);
924 if (ret < 0) {
925 rdev_err(rdev, "failed to enable\n");
926 goto out;
930 print_constraints(rdev);
931 return 0;
932 out:
933 kfree(rdev->constraints);
934 rdev->constraints = NULL;
935 return ret;
939 * set_supply - set regulator supply regulator
940 * @rdev: regulator name
941 * @supply_rdev: supply regulator name
943 * Called by platform initialisation code to set the supply regulator for this
944 * regulator. This ensures that a regulators supply will also be enabled by the
945 * core if it's child is enabled.
947 static int set_supply(struct regulator_dev *rdev,
948 struct regulator_dev *supply_rdev)
950 int err;
952 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
954 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
955 if (IS_ERR(rdev->supply)) {
956 err = PTR_ERR(rdev->supply);
957 rdev->supply = NULL;
958 return err;
961 return 0;
965 * set_consumer_device_supply - Bind a regulator to a symbolic supply
966 * @rdev: regulator source
967 * @consumer_dev: device the supply applies to
968 * @consumer_dev_name: dev_name() string for device supply applies to
969 * @supply: symbolic name for supply
971 * Allows platform initialisation code to map physical regulator
972 * sources to symbolic names for supplies for use by devices. Devices
973 * should use these symbolic names to request regulators, avoiding the
974 * need to provide board-specific regulator names as platform data.
976 * Only one of consumer_dev and consumer_dev_name may be specified.
978 static int set_consumer_device_supply(struct regulator_dev *rdev,
979 struct device *consumer_dev, const char *consumer_dev_name,
980 const char *supply)
982 struct regulator_map *node;
983 int has_dev;
985 if (consumer_dev && consumer_dev_name)
986 return -EINVAL;
988 if (!consumer_dev_name && consumer_dev)
989 consumer_dev_name = dev_name(consumer_dev);
991 if (supply == NULL)
992 return -EINVAL;
994 if (consumer_dev_name != NULL)
995 has_dev = 1;
996 else
997 has_dev = 0;
999 list_for_each_entry(node, &regulator_map_list, list) {
1000 if (node->dev_name && consumer_dev_name) {
1001 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1002 continue;
1003 } else if (node->dev_name || consumer_dev_name) {
1004 continue;
1007 if (strcmp(node->supply, supply) != 0)
1008 continue;
1010 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
1011 dev_name(&node->regulator->dev),
1012 node->regulator->desc->name,
1013 supply,
1014 dev_name(&rdev->dev), rdev_get_name(rdev));
1015 return -EBUSY;
1018 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1019 if (node == NULL)
1020 return -ENOMEM;
1022 node->regulator = rdev;
1023 node->supply = supply;
1025 if (has_dev) {
1026 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1027 if (node->dev_name == NULL) {
1028 kfree(node);
1029 return -ENOMEM;
1033 list_add(&node->list, &regulator_map_list);
1034 return 0;
1037 static void unset_regulator_supplies(struct regulator_dev *rdev)
1039 struct regulator_map *node, *n;
1041 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1042 if (rdev == node->regulator) {
1043 list_del(&node->list);
1044 kfree(node->dev_name);
1045 kfree(node);
1050 #define REG_STR_SIZE 64
1052 static struct regulator *create_regulator(struct regulator_dev *rdev,
1053 struct device *dev,
1054 const char *supply_name)
1056 struct regulator *regulator;
1057 char buf[REG_STR_SIZE];
1058 int err, size;
1060 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1061 if (regulator == NULL)
1062 return NULL;
1064 mutex_lock(&rdev->mutex);
1065 regulator->rdev = rdev;
1066 list_add(&regulator->list, &rdev->consumer_list);
1068 if (dev) {
1069 /* create a 'requested_microamps_name' sysfs entry */
1070 size = scnprintf(buf, REG_STR_SIZE,
1071 "microamps_requested_%s-%s",
1072 dev_name(dev), supply_name);
1073 if (size >= REG_STR_SIZE)
1074 goto overflow_err;
1076 regulator->dev = dev;
1077 sysfs_attr_init(&regulator->dev_attr.attr);
1078 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1079 if (regulator->dev_attr.attr.name == NULL)
1080 goto attr_name_err;
1082 regulator->dev_attr.attr.mode = 0444;
1083 regulator->dev_attr.show = device_requested_uA_show;
1084 err = device_create_file(dev, &regulator->dev_attr);
1085 if (err < 0) {
1086 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
1087 goto attr_name_err;
1090 /* also add a link to the device sysfs entry */
1091 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1092 dev->kobj.name, supply_name);
1093 if (size >= REG_STR_SIZE)
1094 goto attr_err;
1096 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1097 if (regulator->supply_name == NULL)
1098 goto attr_err;
1100 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1101 buf);
1102 if (err) {
1103 rdev_warn(rdev, "could not add device link %s err %d\n",
1104 dev->kobj.name, err);
1105 goto link_name_err;
1107 } else {
1108 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1109 if (regulator->supply_name == NULL)
1110 goto attr_err;
1113 #ifdef CONFIG_DEBUG_FS
1114 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1115 rdev->debugfs);
1116 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1117 rdev_warn(rdev, "Failed to create debugfs directory\n");
1118 regulator->debugfs = NULL;
1119 } else {
1120 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1121 &regulator->uA_load);
1122 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1123 &regulator->min_uV);
1124 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1125 &regulator->max_uV);
1127 #endif
1129 mutex_unlock(&rdev->mutex);
1130 return regulator;
1131 link_name_err:
1132 kfree(regulator->supply_name);
1133 attr_err:
1134 device_remove_file(regulator->dev, &regulator->dev_attr);
1135 attr_name_err:
1136 kfree(regulator->dev_attr.attr.name);
1137 overflow_err:
1138 list_del(&regulator->list);
1139 kfree(regulator);
1140 mutex_unlock(&rdev->mutex);
1141 return NULL;
1144 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1146 if (!rdev->desc->ops->enable_time)
1147 return 0;
1148 return rdev->desc->ops->enable_time(rdev);
1151 /* Internal regulator request function */
1152 static struct regulator *_regulator_get(struct device *dev, const char *id,
1153 int exclusive)
1155 struct regulator_dev *rdev;
1156 struct regulator_map *map;
1157 struct regulator *regulator = ERR_PTR(-ENODEV);
1158 const char *devname = NULL;
1159 int ret;
1161 if (id == NULL) {
1162 pr_err("get() with no identifier\n");
1163 return regulator;
1166 if (dev)
1167 devname = dev_name(dev);
1169 mutex_lock(&regulator_list_mutex);
1171 list_for_each_entry(map, &regulator_map_list, list) {
1172 /* If the mapping has a device set up it must match */
1173 if (map->dev_name &&
1174 (!devname || strcmp(map->dev_name, devname)))
1175 continue;
1177 if (strcmp(map->supply, id) == 0) {
1178 rdev = map->regulator;
1179 goto found;
1183 if (board_wants_dummy_regulator) {
1184 rdev = dummy_regulator_rdev;
1185 goto found;
1188 #ifdef CONFIG_REGULATOR_DUMMY
1189 if (!devname)
1190 devname = "deviceless";
1192 /* If the board didn't flag that it was fully constrained then
1193 * substitute in a dummy regulator so consumers can continue.
1195 if (!has_full_constraints) {
1196 pr_warn("%s supply %s not found, using dummy regulator\n",
1197 devname, id);
1198 rdev = dummy_regulator_rdev;
1199 goto found;
1201 #endif
1203 mutex_unlock(&regulator_list_mutex);
1204 return regulator;
1206 found:
1207 if (rdev->exclusive) {
1208 regulator = ERR_PTR(-EPERM);
1209 goto out;
1212 if (exclusive && rdev->open_count) {
1213 regulator = ERR_PTR(-EBUSY);
1214 goto out;
1217 if (!try_module_get(rdev->owner))
1218 goto out;
1220 regulator = create_regulator(rdev, dev, id);
1221 if (regulator == NULL) {
1222 regulator = ERR_PTR(-ENOMEM);
1223 module_put(rdev->owner);
1226 rdev->open_count++;
1227 if (exclusive) {
1228 rdev->exclusive = 1;
1230 ret = _regulator_is_enabled(rdev);
1231 if (ret > 0)
1232 rdev->use_count = 1;
1233 else
1234 rdev->use_count = 0;
1237 out:
1238 mutex_unlock(&regulator_list_mutex);
1240 return regulator;
1244 * regulator_get - lookup and obtain a reference to a regulator.
1245 * @dev: device for regulator "consumer"
1246 * @id: Supply name or regulator ID.
1248 * Returns a struct regulator corresponding to the regulator producer,
1249 * or IS_ERR() condition containing errno.
1251 * Use of supply names configured via regulator_set_device_supply() is
1252 * strongly encouraged. It is recommended that the supply name used
1253 * should match the name used for the supply and/or the relevant
1254 * device pins in the datasheet.
1256 struct regulator *regulator_get(struct device *dev, const char *id)
1258 return _regulator_get(dev, id, 0);
1260 EXPORT_SYMBOL_GPL(regulator_get);
1263 * regulator_get_exclusive - obtain exclusive access to a regulator.
1264 * @dev: device for regulator "consumer"
1265 * @id: Supply name or regulator ID.
1267 * Returns a struct regulator corresponding to the regulator producer,
1268 * or IS_ERR() condition containing errno. Other consumers will be
1269 * unable to obtain this reference is held and the use count for the
1270 * regulator will be initialised to reflect the current state of the
1271 * regulator.
1273 * This is intended for use by consumers which cannot tolerate shared
1274 * use of the regulator such as those which need to force the
1275 * regulator off for correct operation of the hardware they are
1276 * controlling.
1278 * Use of supply names configured via regulator_set_device_supply() is
1279 * strongly encouraged. It is recommended that the supply name used
1280 * should match the name used for the supply and/or the relevant
1281 * device pins in the datasheet.
1283 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1285 return _regulator_get(dev, id, 1);
1287 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1290 * regulator_put - "free" the regulator source
1291 * @regulator: regulator source
1293 * Note: drivers must ensure that all regulator_enable calls made on this
1294 * regulator source are balanced by regulator_disable calls prior to calling
1295 * this function.
1297 void regulator_put(struct regulator *regulator)
1299 struct regulator_dev *rdev;
1301 if (regulator == NULL || IS_ERR(regulator))
1302 return;
1304 mutex_lock(&regulator_list_mutex);
1305 rdev = regulator->rdev;
1307 #ifdef CONFIG_DEBUG_FS
1308 debugfs_remove_recursive(regulator->debugfs);
1309 #endif
1311 /* remove any sysfs entries */
1312 if (regulator->dev) {
1313 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1314 device_remove_file(regulator->dev, &regulator->dev_attr);
1315 kfree(regulator->dev_attr.attr.name);
1317 kfree(regulator->supply_name);
1318 list_del(&regulator->list);
1319 kfree(regulator);
1321 rdev->open_count--;
1322 rdev->exclusive = 0;
1324 module_put(rdev->owner);
1325 mutex_unlock(&regulator_list_mutex);
1327 EXPORT_SYMBOL_GPL(regulator_put);
1329 static int _regulator_can_change_status(struct regulator_dev *rdev)
1331 if (!rdev->constraints)
1332 return 0;
1334 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1335 return 1;
1336 else
1337 return 0;
1340 /* locks held by regulator_enable() */
1341 static int _regulator_enable(struct regulator_dev *rdev)
1343 int ret, delay;
1345 /* check voltage and requested load before enabling */
1346 if (rdev->constraints &&
1347 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1348 drms_uA_update(rdev);
1350 if (rdev->use_count == 0) {
1351 /* The regulator may on if it's not switchable or left on */
1352 ret = _regulator_is_enabled(rdev);
1353 if (ret == -EINVAL || ret == 0) {
1354 if (!_regulator_can_change_status(rdev))
1355 return -EPERM;
1357 if (!rdev->desc->ops->enable)
1358 return -EINVAL;
1360 /* Query before enabling in case configuration
1361 * dependent. */
1362 ret = _regulator_get_enable_time(rdev);
1363 if (ret >= 0) {
1364 delay = ret;
1365 } else {
1366 rdev_warn(rdev, "enable_time() failed: %d\n",
1367 ret);
1368 delay = 0;
1371 trace_regulator_enable(rdev_get_name(rdev));
1373 /* Allow the regulator to ramp; it would be useful
1374 * to extend this for bulk operations so that the
1375 * regulators can ramp together. */
1376 ret = rdev->desc->ops->enable(rdev);
1377 if (ret < 0)
1378 return ret;
1380 trace_regulator_enable_delay(rdev_get_name(rdev));
1382 if (delay >= 1000) {
1383 mdelay(delay / 1000);
1384 udelay(delay % 1000);
1385 } else if (delay) {
1386 udelay(delay);
1389 trace_regulator_enable_complete(rdev_get_name(rdev));
1391 } else if (ret < 0) {
1392 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1393 return ret;
1395 /* Fallthrough on positive return values - already enabled */
1398 rdev->use_count++;
1400 return 0;
1404 * regulator_enable - enable regulator output
1405 * @regulator: regulator source
1407 * Request that the regulator be enabled with the regulator output at
1408 * the predefined voltage or current value. Calls to regulator_enable()
1409 * must be balanced with calls to regulator_disable().
1411 * NOTE: the output value can be set by other drivers, boot loader or may be
1412 * hardwired in the regulator.
1414 int regulator_enable(struct regulator *regulator)
1416 struct regulator_dev *rdev = regulator->rdev;
1417 int ret = 0;
1419 if (rdev->supply) {
1420 ret = regulator_enable(rdev->supply);
1421 if (ret != 0)
1422 return ret;
1425 mutex_lock(&rdev->mutex);
1426 ret = _regulator_enable(rdev);
1427 mutex_unlock(&rdev->mutex);
1429 if (ret != 0 && rdev->supply)
1430 regulator_disable(rdev->supply);
1432 return ret;
1434 EXPORT_SYMBOL_GPL(regulator_enable);
1436 /* locks held by regulator_disable() */
1437 static int _regulator_disable(struct regulator_dev *rdev)
1439 int ret = 0;
1441 if (WARN(rdev->use_count <= 0,
1442 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1443 return -EIO;
1445 /* are we the last user and permitted to disable ? */
1446 if (rdev->use_count == 1 &&
1447 (rdev->constraints && !rdev->constraints->always_on)) {
1449 /* we are last user */
1450 if (_regulator_can_change_status(rdev) &&
1451 rdev->desc->ops->disable) {
1452 trace_regulator_disable(rdev_get_name(rdev));
1454 ret = rdev->desc->ops->disable(rdev);
1455 if (ret < 0) {
1456 rdev_err(rdev, "failed to disable\n");
1457 return ret;
1460 trace_regulator_disable_complete(rdev_get_name(rdev));
1462 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1463 NULL);
1466 rdev->use_count = 0;
1467 } else if (rdev->use_count > 1) {
1469 if (rdev->constraints &&
1470 (rdev->constraints->valid_ops_mask &
1471 REGULATOR_CHANGE_DRMS))
1472 drms_uA_update(rdev);
1474 rdev->use_count--;
1477 return ret;
1481 * regulator_disable - disable regulator output
1482 * @regulator: regulator source
1484 * Disable the regulator output voltage or current. Calls to
1485 * regulator_enable() must be balanced with calls to
1486 * regulator_disable().
1488 * NOTE: this will only disable the regulator output if no other consumer
1489 * devices have it enabled, the regulator device supports disabling and
1490 * machine constraints permit this operation.
1492 int regulator_disable(struct regulator *regulator)
1494 struct regulator_dev *rdev = regulator->rdev;
1495 int ret = 0;
1497 mutex_lock(&rdev->mutex);
1498 ret = _regulator_disable(rdev);
1499 mutex_unlock(&rdev->mutex);
1501 if (ret == 0 && rdev->supply)
1502 regulator_disable(rdev->supply);
1504 return ret;
1506 EXPORT_SYMBOL_GPL(regulator_disable);
1508 /* locks held by regulator_force_disable() */
1509 static int _regulator_force_disable(struct regulator_dev *rdev)
1511 int ret = 0;
1513 /* force disable */
1514 if (rdev->desc->ops->disable) {
1515 /* ah well, who wants to live forever... */
1516 ret = rdev->desc->ops->disable(rdev);
1517 if (ret < 0) {
1518 rdev_err(rdev, "failed to force disable\n");
1519 return ret;
1521 /* notify other consumers that power has been forced off */
1522 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1523 REGULATOR_EVENT_DISABLE, NULL);
1526 return ret;
1530 * regulator_force_disable - force disable regulator output
1531 * @regulator: regulator source
1533 * Forcibly disable the regulator output voltage or current.
1534 * NOTE: this *will* disable the regulator output even if other consumer
1535 * devices have it enabled. This should be used for situations when device
1536 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1538 int regulator_force_disable(struct regulator *regulator)
1540 struct regulator_dev *rdev = regulator->rdev;
1541 int ret;
1543 mutex_lock(&rdev->mutex);
1544 regulator->uA_load = 0;
1545 ret = _regulator_force_disable(regulator->rdev);
1546 mutex_unlock(&rdev->mutex);
1548 if (rdev->supply)
1549 while (rdev->open_count--)
1550 regulator_disable(rdev->supply);
1552 return ret;
1554 EXPORT_SYMBOL_GPL(regulator_force_disable);
1556 static void regulator_disable_work(struct work_struct *work)
1558 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1559 disable_work.work);
1560 int count, i, ret;
1562 mutex_lock(&rdev->mutex);
1564 BUG_ON(!rdev->deferred_disables);
1566 count = rdev->deferred_disables;
1567 rdev->deferred_disables = 0;
1569 for (i = 0; i < count; i++) {
1570 ret = _regulator_disable(rdev);
1571 if (ret != 0)
1572 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1575 mutex_unlock(&rdev->mutex);
1577 if (rdev->supply) {
1578 for (i = 0; i < count; i++) {
1579 ret = regulator_disable(rdev->supply);
1580 if (ret != 0) {
1581 rdev_err(rdev,
1582 "Supply disable failed: %d\n", ret);
1589 * regulator_disable_deferred - disable regulator output with delay
1590 * @regulator: regulator source
1591 * @ms: miliseconds until the regulator is disabled
1593 * Execute regulator_disable() on the regulator after a delay. This
1594 * is intended for use with devices that require some time to quiesce.
1596 * NOTE: this will only disable the regulator output if no other consumer
1597 * devices have it enabled, the regulator device supports disabling and
1598 * machine constraints permit this operation.
1600 int regulator_disable_deferred(struct regulator *regulator, int ms)
1602 struct regulator_dev *rdev = regulator->rdev;
1603 int ret;
1605 mutex_lock(&rdev->mutex);
1606 rdev->deferred_disables++;
1607 mutex_unlock(&rdev->mutex);
1609 ret = schedule_delayed_work(&rdev->disable_work,
1610 msecs_to_jiffies(ms));
1611 if (ret < 0)
1612 return ret;
1613 else
1614 return 0;
1616 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1618 static int _regulator_is_enabled(struct regulator_dev *rdev)
1620 /* If we don't know then assume that the regulator is always on */
1621 if (!rdev->desc->ops->is_enabled)
1622 return 1;
1624 return rdev->desc->ops->is_enabled(rdev);
1628 * regulator_is_enabled - is the regulator output enabled
1629 * @regulator: regulator source
1631 * Returns positive if the regulator driver backing the source/client
1632 * has requested that the device be enabled, zero if it hasn't, else a
1633 * negative errno code.
1635 * Note that the device backing this regulator handle can have multiple
1636 * users, so it might be enabled even if regulator_enable() was never
1637 * called for this particular source.
1639 int regulator_is_enabled(struct regulator *regulator)
1641 int ret;
1643 mutex_lock(&regulator->rdev->mutex);
1644 ret = _regulator_is_enabled(regulator->rdev);
1645 mutex_unlock(&regulator->rdev->mutex);
1647 return ret;
1649 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1652 * regulator_count_voltages - count regulator_list_voltage() selectors
1653 * @regulator: regulator source
1655 * Returns number of selectors, or negative errno. Selectors are
1656 * numbered starting at zero, and typically correspond to bitfields
1657 * in hardware registers.
1659 int regulator_count_voltages(struct regulator *regulator)
1661 struct regulator_dev *rdev = regulator->rdev;
1663 return rdev->desc->n_voltages ? : -EINVAL;
1665 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1668 * regulator_list_voltage - enumerate supported voltages
1669 * @regulator: regulator source
1670 * @selector: identify voltage to list
1671 * Context: can sleep
1673 * Returns a voltage that can be passed to @regulator_set_voltage(),
1674 * zero if this selector code can't be used on this system, or a
1675 * negative errno.
1677 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1679 struct regulator_dev *rdev = regulator->rdev;
1680 struct regulator_ops *ops = rdev->desc->ops;
1681 int ret;
1683 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1684 return -EINVAL;
1686 mutex_lock(&rdev->mutex);
1687 ret = ops->list_voltage(rdev, selector);
1688 mutex_unlock(&rdev->mutex);
1690 if (ret > 0) {
1691 if (ret < rdev->constraints->min_uV)
1692 ret = 0;
1693 else if (ret > rdev->constraints->max_uV)
1694 ret = 0;
1697 return ret;
1699 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1702 * regulator_is_supported_voltage - check if a voltage range can be supported
1704 * @regulator: Regulator to check.
1705 * @min_uV: Minimum required voltage in uV.
1706 * @max_uV: Maximum required voltage in uV.
1708 * Returns a boolean or a negative error code.
1710 int regulator_is_supported_voltage(struct regulator *regulator,
1711 int min_uV, int max_uV)
1713 int i, voltages, ret;
1715 ret = regulator_count_voltages(regulator);
1716 if (ret < 0)
1717 return ret;
1718 voltages = ret;
1720 for (i = 0; i < voltages; i++) {
1721 ret = regulator_list_voltage(regulator, i);
1723 if (ret >= min_uV && ret <= max_uV)
1724 return 1;
1727 return 0;
1730 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1731 int min_uV, int max_uV)
1733 int ret;
1734 int delay = 0;
1735 unsigned int selector;
1737 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1739 min_uV += rdev->constraints->uV_offset;
1740 max_uV += rdev->constraints->uV_offset;
1742 if (rdev->desc->ops->set_voltage) {
1743 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1744 &selector);
1746 if (rdev->desc->ops->list_voltage)
1747 selector = rdev->desc->ops->list_voltage(rdev,
1748 selector);
1749 else
1750 selector = -1;
1751 } else if (rdev->desc->ops->set_voltage_sel) {
1752 int best_val = INT_MAX;
1753 int i;
1755 selector = 0;
1757 /* Find the smallest voltage that falls within the specified
1758 * range.
1760 for (i = 0; i < rdev->desc->n_voltages; i++) {
1761 ret = rdev->desc->ops->list_voltage(rdev, i);
1762 if (ret < 0)
1763 continue;
1765 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1766 best_val = ret;
1767 selector = i;
1772 * If we can't obtain the old selector there is not enough
1773 * info to call set_voltage_time_sel().
1775 if (rdev->desc->ops->set_voltage_time_sel &&
1776 rdev->desc->ops->get_voltage_sel) {
1777 unsigned int old_selector = 0;
1779 ret = rdev->desc->ops->get_voltage_sel(rdev);
1780 if (ret < 0)
1781 return ret;
1782 old_selector = ret;
1783 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1784 old_selector, selector);
1787 if (best_val != INT_MAX) {
1788 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1789 selector = best_val;
1790 } else {
1791 ret = -EINVAL;
1793 } else {
1794 ret = -EINVAL;
1797 /* Insert any necessary delays */
1798 if (delay >= 1000) {
1799 mdelay(delay / 1000);
1800 udelay(delay % 1000);
1801 } else if (delay) {
1802 udelay(delay);
1805 if (ret == 0)
1806 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1807 NULL);
1809 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1811 return ret;
1815 * regulator_set_voltage - set regulator output voltage
1816 * @regulator: regulator source
1817 * @min_uV: Minimum required voltage in uV
1818 * @max_uV: Maximum acceptable voltage in uV
1820 * Sets a voltage regulator to the desired output voltage. This can be set
1821 * during any regulator state. IOW, regulator can be disabled or enabled.
1823 * If the regulator is enabled then the voltage will change to the new value
1824 * immediately otherwise if the regulator is disabled the regulator will
1825 * output at the new voltage when enabled.
1827 * NOTE: If the regulator is shared between several devices then the lowest
1828 * request voltage that meets the system constraints will be used.
1829 * Regulator system constraints must be set for this regulator before
1830 * calling this function otherwise this call will fail.
1832 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1834 struct regulator_dev *rdev = regulator->rdev;
1835 int ret = 0;
1837 mutex_lock(&rdev->mutex);
1839 /* If we're setting the same range as last time the change
1840 * should be a noop (some cpufreq implementations use the same
1841 * voltage for multiple frequencies, for example).
1843 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1844 goto out;
1846 /* sanity check */
1847 if (!rdev->desc->ops->set_voltage &&
1848 !rdev->desc->ops->set_voltage_sel) {
1849 ret = -EINVAL;
1850 goto out;
1853 /* constraints check */
1854 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1855 if (ret < 0)
1856 goto out;
1857 regulator->min_uV = min_uV;
1858 regulator->max_uV = max_uV;
1860 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1861 if (ret < 0)
1862 goto out;
1864 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1866 out:
1867 mutex_unlock(&rdev->mutex);
1868 return ret;
1870 EXPORT_SYMBOL_GPL(regulator_set_voltage);
1873 * regulator_set_voltage_time - get raise/fall time
1874 * @regulator: regulator source
1875 * @old_uV: starting voltage in microvolts
1876 * @new_uV: target voltage in microvolts
1878 * Provided with the starting and ending voltage, this function attempts to
1879 * calculate the time in microseconds required to rise or fall to this new
1880 * voltage.
1882 int regulator_set_voltage_time(struct regulator *regulator,
1883 int old_uV, int new_uV)
1885 struct regulator_dev *rdev = regulator->rdev;
1886 struct regulator_ops *ops = rdev->desc->ops;
1887 int old_sel = -1;
1888 int new_sel = -1;
1889 int voltage;
1890 int i;
1892 /* Currently requires operations to do this */
1893 if (!ops->list_voltage || !ops->set_voltage_time_sel
1894 || !rdev->desc->n_voltages)
1895 return -EINVAL;
1897 for (i = 0; i < rdev->desc->n_voltages; i++) {
1898 /* We only look for exact voltage matches here */
1899 voltage = regulator_list_voltage(regulator, i);
1900 if (voltage < 0)
1901 return -EINVAL;
1902 if (voltage == 0)
1903 continue;
1904 if (voltage == old_uV)
1905 old_sel = i;
1906 if (voltage == new_uV)
1907 new_sel = i;
1910 if (old_sel < 0 || new_sel < 0)
1911 return -EINVAL;
1913 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1915 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1918 * regulator_sync_voltage - re-apply last regulator output voltage
1919 * @regulator: regulator source
1921 * Re-apply the last configured voltage. This is intended to be used
1922 * where some external control source the consumer is cooperating with
1923 * has caused the configured voltage to change.
1925 int regulator_sync_voltage(struct regulator *regulator)
1927 struct regulator_dev *rdev = regulator->rdev;
1928 int ret, min_uV, max_uV;
1930 mutex_lock(&rdev->mutex);
1932 if (!rdev->desc->ops->set_voltage &&
1933 !rdev->desc->ops->set_voltage_sel) {
1934 ret = -EINVAL;
1935 goto out;
1938 /* This is only going to work if we've had a voltage configured. */
1939 if (!regulator->min_uV && !regulator->max_uV) {
1940 ret = -EINVAL;
1941 goto out;
1944 min_uV = regulator->min_uV;
1945 max_uV = regulator->max_uV;
1947 /* This should be a paranoia check... */
1948 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1949 if (ret < 0)
1950 goto out;
1952 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1953 if (ret < 0)
1954 goto out;
1956 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1958 out:
1959 mutex_unlock(&rdev->mutex);
1960 return ret;
1962 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1964 static int _regulator_get_voltage(struct regulator_dev *rdev)
1966 int sel, ret;
1968 if (rdev->desc->ops->get_voltage_sel) {
1969 sel = rdev->desc->ops->get_voltage_sel(rdev);
1970 if (sel < 0)
1971 return sel;
1972 ret = rdev->desc->ops->list_voltage(rdev, sel);
1973 } else if (rdev->desc->ops->get_voltage) {
1974 ret = rdev->desc->ops->get_voltage(rdev);
1975 } else {
1976 return -EINVAL;
1979 if (ret < 0)
1980 return ret;
1981 return ret - rdev->constraints->uV_offset;
1985 * regulator_get_voltage - get regulator output voltage
1986 * @regulator: regulator source
1988 * This returns the current regulator voltage in uV.
1990 * NOTE: If the regulator is disabled it will return the voltage value. This
1991 * function should not be used to determine regulator state.
1993 int regulator_get_voltage(struct regulator *regulator)
1995 int ret;
1997 mutex_lock(&regulator->rdev->mutex);
1999 ret = _regulator_get_voltage(regulator->rdev);
2001 mutex_unlock(&regulator->rdev->mutex);
2003 return ret;
2005 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2008 * regulator_set_current_limit - set regulator output current limit
2009 * @regulator: regulator source
2010 * @min_uA: Minimuum supported current in uA
2011 * @max_uA: Maximum supported current in uA
2013 * Sets current sink to the desired output current. This can be set during
2014 * any regulator state. IOW, regulator can be disabled or enabled.
2016 * If the regulator is enabled then the current will change to the new value
2017 * immediately otherwise if the regulator is disabled the regulator will
2018 * output at the new current when enabled.
2020 * NOTE: Regulator system constraints must be set for this regulator before
2021 * calling this function otherwise this call will fail.
2023 int regulator_set_current_limit(struct regulator *regulator,
2024 int min_uA, int max_uA)
2026 struct regulator_dev *rdev = regulator->rdev;
2027 int ret;
2029 mutex_lock(&rdev->mutex);
2031 /* sanity check */
2032 if (!rdev->desc->ops->set_current_limit) {
2033 ret = -EINVAL;
2034 goto out;
2037 /* constraints check */
2038 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2039 if (ret < 0)
2040 goto out;
2042 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2043 out:
2044 mutex_unlock(&rdev->mutex);
2045 return ret;
2047 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2049 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2051 int ret;
2053 mutex_lock(&rdev->mutex);
2055 /* sanity check */
2056 if (!rdev->desc->ops->get_current_limit) {
2057 ret = -EINVAL;
2058 goto out;
2061 ret = rdev->desc->ops->get_current_limit(rdev);
2062 out:
2063 mutex_unlock(&rdev->mutex);
2064 return ret;
2068 * regulator_get_current_limit - get regulator output current
2069 * @regulator: regulator source
2071 * This returns the current supplied by the specified current sink in uA.
2073 * NOTE: If the regulator is disabled it will return the current value. This
2074 * function should not be used to determine regulator state.
2076 int regulator_get_current_limit(struct regulator *regulator)
2078 return _regulator_get_current_limit(regulator->rdev);
2080 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2083 * regulator_set_mode - set regulator operating mode
2084 * @regulator: regulator source
2085 * @mode: operating mode - one of the REGULATOR_MODE constants
2087 * Set regulator operating mode to increase regulator efficiency or improve
2088 * regulation performance.
2090 * NOTE: Regulator system constraints must be set for this regulator before
2091 * calling this function otherwise this call will fail.
2093 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2095 struct regulator_dev *rdev = regulator->rdev;
2096 int ret;
2097 int regulator_curr_mode;
2099 mutex_lock(&rdev->mutex);
2101 /* sanity check */
2102 if (!rdev->desc->ops->set_mode) {
2103 ret = -EINVAL;
2104 goto out;
2107 /* return if the same mode is requested */
2108 if (rdev->desc->ops->get_mode) {
2109 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2110 if (regulator_curr_mode == mode) {
2111 ret = 0;
2112 goto out;
2116 /* constraints check */
2117 ret = regulator_mode_constrain(rdev, &mode);
2118 if (ret < 0)
2119 goto out;
2121 ret = rdev->desc->ops->set_mode(rdev, mode);
2122 out:
2123 mutex_unlock(&rdev->mutex);
2124 return ret;
2126 EXPORT_SYMBOL_GPL(regulator_set_mode);
2128 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2130 int ret;
2132 mutex_lock(&rdev->mutex);
2134 /* sanity check */
2135 if (!rdev->desc->ops->get_mode) {
2136 ret = -EINVAL;
2137 goto out;
2140 ret = rdev->desc->ops->get_mode(rdev);
2141 out:
2142 mutex_unlock(&rdev->mutex);
2143 return ret;
2147 * regulator_get_mode - get regulator operating mode
2148 * @regulator: regulator source
2150 * Get the current regulator operating mode.
2152 unsigned int regulator_get_mode(struct regulator *regulator)
2154 return _regulator_get_mode(regulator->rdev);
2156 EXPORT_SYMBOL_GPL(regulator_get_mode);
2159 * regulator_set_optimum_mode - set regulator optimum operating mode
2160 * @regulator: regulator source
2161 * @uA_load: load current
2163 * Notifies the regulator core of a new device load. This is then used by
2164 * DRMS (if enabled by constraints) to set the most efficient regulator
2165 * operating mode for the new regulator loading.
2167 * Consumer devices notify their supply regulator of the maximum power
2168 * they will require (can be taken from device datasheet in the power
2169 * consumption tables) when they change operational status and hence power
2170 * state. Examples of operational state changes that can affect power
2171 * consumption are :-
2173 * o Device is opened / closed.
2174 * o Device I/O is about to begin or has just finished.
2175 * o Device is idling in between work.
2177 * This information is also exported via sysfs to userspace.
2179 * DRMS will sum the total requested load on the regulator and change
2180 * to the most efficient operating mode if platform constraints allow.
2182 * Returns the new regulator mode or error.
2184 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2186 struct regulator_dev *rdev = regulator->rdev;
2187 struct regulator *consumer;
2188 int ret, output_uV, input_uV, total_uA_load = 0;
2189 unsigned int mode;
2191 mutex_lock(&rdev->mutex);
2194 * first check to see if we can set modes at all, otherwise just
2195 * tell the consumer everything is OK.
2197 regulator->uA_load = uA_load;
2198 ret = regulator_check_drms(rdev);
2199 if (ret < 0) {
2200 ret = 0;
2201 goto out;
2204 if (!rdev->desc->ops->get_optimum_mode)
2205 goto out;
2208 * we can actually do this so any errors are indicators of
2209 * potential real failure.
2211 ret = -EINVAL;
2213 /* get output voltage */
2214 output_uV = _regulator_get_voltage(rdev);
2215 if (output_uV <= 0) {
2216 rdev_err(rdev, "invalid output voltage found\n");
2217 goto out;
2220 /* get input voltage */
2221 input_uV = 0;
2222 if (rdev->supply)
2223 input_uV = regulator_get_voltage(rdev->supply);
2224 if (input_uV <= 0)
2225 input_uV = rdev->constraints->input_uV;
2226 if (input_uV <= 0) {
2227 rdev_err(rdev, "invalid input voltage found\n");
2228 goto out;
2231 /* calc total requested load for this regulator */
2232 list_for_each_entry(consumer, &rdev->consumer_list, list)
2233 total_uA_load += consumer->uA_load;
2235 mode = rdev->desc->ops->get_optimum_mode(rdev,
2236 input_uV, output_uV,
2237 total_uA_load);
2238 ret = regulator_mode_constrain(rdev, &mode);
2239 if (ret < 0) {
2240 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2241 total_uA_load, input_uV, output_uV);
2242 goto out;
2245 ret = rdev->desc->ops->set_mode(rdev, mode);
2246 if (ret < 0) {
2247 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2248 goto out;
2250 ret = mode;
2251 out:
2252 mutex_unlock(&rdev->mutex);
2253 return ret;
2255 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2258 * regulator_register_notifier - register regulator event notifier
2259 * @regulator: regulator source
2260 * @nb: notifier block
2262 * Register notifier block to receive regulator events.
2264 int regulator_register_notifier(struct regulator *regulator,
2265 struct notifier_block *nb)
2267 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2268 nb);
2270 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2273 * regulator_unregister_notifier - unregister regulator event notifier
2274 * @regulator: regulator source
2275 * @nb: notifier block
2277 * Unregister regulator event notifier block.
2279 int regulator_unregister_notifier(struct regulator *regulator,
2280 struct notifier_block *nb)
2282 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2283 nb);
2285 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2287 /* notify regulator consumers and downstream regulator consumers.
2288 * Note mutex must be held by caller.
2290 static void _notifier_call_chain(struct regulator_dev *rdev,
2291 unsigned long event, void *data)
2293 /* call rdev chain first */
2294 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
2298 * regulator_bulk_get - get multiple regulator consumers
2300 * @dev: Device to supply
2301 * @num_consumers: Number of consumers to register
2302 * @consumers: Configuration of consumers; clients are stored here.
2304 * @return 0 on success, an errno on failure.
2306 * This helper function allows drivers to get several regulator
2307 * consumers in one operation. If any of the regulators cannot be
2308 * acquired then any regulators that were allocated will be freed
2309 * before returning to the caller.
2311 int regulator_bulk_get(struct device *dev, int num_consumers,
2312 struct regulator_bulk_data *consumers)
2314 int i;
2315 int ret;
2317 for (i = 0; i < num_consumers; i++)
2318 consumers[i].consumer = NULL;
2320 for (i = 0; i < num_consumers; i++) {
2321 consumers[i].consumer = regulator_get(dev,
2322 consumers[i].supply);
2323 if (IS_ERR(consumers[i].consumer)) {
2324 ret = PTR_ERR(consumers[i].consumer);
2325 dev_err(dev, "Failed to get supply '%s': %d\n",
2326 consumers[i].supply, ret);
2327 consumers[i].consumer = NULL;
2328 goto err;
2332 return 0;
2334 err:
2335 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2336 regulator_put(consumers[i].consumer);
2338 return ret;
2340 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2342 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2344 struct regulator_bulk_data *bulk = data;
2346 bulk->ret = regulator_enable(bulk->consumer);
2350 * regulator_bulk_enable - enable multiple regulator consumers
2352 * @num_consumers: Number of consumers
2353 * @consumers: Consumer data; clients are stored here.
2354 * @return 0 on success, an errno on failure
2356 * This convenience API allows consumers to enable multiple regulator
2357 * clients in a single API call. If any consumers cannot be enabled
2358 * then any others that were enabled will be disabled again prior to
2359 * return.
2361 int regulator_bulk_enable(int num_consumers,
2362 struct regulator_bulk_data *consumers)
2364 LIST_HEAD(async_domain);
2365 int i;
2366 int ret = 0;
2368 for (i = 0; i < num_consumers; i++)
2369 async_schedule_domain(regulator_bulk_enable_async,
2370 &consumers[i], &async_domain);
2372 async_synchronize_full_domain(&async_domain);
2374 /* If any consumer failed we need to unwind any that succeeded */
2375 for (i = 0; i < num_consumers; i++) {
2376 if (consumers[i].ret != 0) {
2377 ret = consumers[i].ret;
2378 goto err;
2382 return 0;
2384 err:
2385 for (i = 0; i < num_consumers; i++)
2386 if (consumers[i].ret == 0)
2387 regulator_disable(consumers[i].consumer);
2388 else
2389 pr_err("Failed to enable %s: %d\n",
2390 consumers[i].supply, consumers[i].ret);
2392 return ret;
2394 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2397 * regulator_bulk_disable - disable multiple regulator consumers
2399 * @num_consumers: Number of consumers
2400 * @consumers: Consumer data; clients are stored here.
2401 * @return 0 on success, an errno on failure
2403 * This convenience API allows consumers to disable multiple regulator
2404 * clients in a single API call. If any consumers cannot be enabled
2405 * then any others that were disabled will be disabled again prior to
2406 * return.
2408 int regulator_bulk_disable(int num_consumers,
2409 struct regulator_bulk_data *consumers)
2411 int i;
2412 int ret;
2414 for (i = 0; i < num_consumers; i++) {
2415 ret = regulator_disable(consumers[i].consumer);
2416 if (ret != 0)
2417 goto err;
2420 return 0;
2422 err:
2423 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
2424 for (--i; i >= 0; --i)
2425 regulator_enable(consumers[i].consumer);
2427 return ret;
2429 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2432 * regulator_bulk_free - free multiple regulator consumers
2434 * @num_consumers: Number of consumers
2435 * @consumers: Consumer data; clients are stored here.
2437 * This convenience API allows consumers to free multiple regulator
2438 * clients in a single API call.
2440 void regulator_bulk_free(int num_consumers,
2441 struct regulator_bulk_data *consumers)
2443 int i;
2445 for (i = 0; i < num_consumers; i++) {
2446 regulator_put(consumers[i].consumer);
2447 consumers[i].consumer = NULL;
2450 EXPORT_SYMBOL_GPL(regulator_bulk_free);
2453 * regulator_notifier_call_chain - call regulator event notifier
2454 * @rdev: regulator source
2455 * @event: notifier block
2456 * @data: callback-specific data.
2458 * Called by regulator drivers to notify clients a regulator event has
2459 * occurred. We also notify regulator clients downstream.
2460 * Note lock must be held by caller.
2462 int regulator_notifier_call_chain(struct regulator_dev *rdev,
2463 unsigned long event, void *data)
2465 _notifier_call_chain(rdev, event, data);
2466 return NOTIFY_DONE;
2469 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2472 * regulator_mode_to_status - convert a regulator mode into a status
2474 * @mode: Mode to convert
2476 * Convert a regulator mode into a status.
2478 int regulator_mode_to_status(unsigned int mode)
2480 switch (mode) {
2481 case REGULATOR_MODE_FAST:
2482 return REGULATOR_STATUS_FAST;
2483 case REGULATOR_MODE_NORMAL:
2484 return REGULATOR_STATUS_NORMAL;
2485 case REGULATOR_MODE_IDLE:
2486 return REGULATOR_STATUS_IDLE;
2487 case REGULATOR_STATUS_STANDBY:
2488 return REGULATOR_STATUS_STANDBY;
2489 default:
2490 return 0;
2493 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2496 * To avoid cluttering sysfs (and memory) with useless state, only
2497 * create attributes that can be meaningfully displayed.
2499 static int add_regulator_attributes(struct regulator_dev *rdev)
2501 struct device *dev = &rdev->dev;
2502 struct regulator_ops *ops = rdev->desc->ops;
2503 int status = 0;
2505 /* some attributes need specific methods to be displayed */
2506 if (ops->get_voltage || ops->get_voltage_sel) {
2507 status = device_create_file(dev, &dev_attr_microvolts);
2508 if (status < 0)
2509 return status;
2511 if (ops->get_current_limit) {
2512 status = device_create_file(dev, &dev_attr_microamps);
2513 if (status < 0)
2514 return status;
2516 if (ops->get_mode) {
2517 status = device_create_file(dev, &dev_attr_opmode);
2518 if (status < 0)
2519 return status;
2521 if (ops->is_enabled) {
2522 status = device_create_file(dev, &dev_attr_state);
2523 if (status < 0)
2524 return status;
2526 if (ops->get_status) {
2527 status = device_create_file(dev, &dev_attr_status);
2528 if (status < 0)
2529 return status;
2532 /* some attributes are type-specific */
2533 if (rdev->desc->type == REGULATOR_CURRENT) {
2534 status = device_create_file(dev, &dev_attr_requested_microamps);
2535 if (status < 0)
2536 return status;
2539 /* all the other attributes exist to support constraints;
2540 * don't show them if there are no constraints, or if the
2541 * relevant supporting methods are missing.
2543 if (!rdev->constraints)
2544 return status;
2546 /* constraints need specific supporting methods */
2547 if (ops->set_voltage || ops->set_voltage_sel) {
2548 status = device_create_file(dev, &dev_attr_min_microvolts);
2549 if (status < 0)
2550 return status;
2551 status = device_create_file(dev, &dev_attr_max_microvolts);
2552 if (status < 0)
2553 return status;
2555 if (ops->set_current_limit) {
2556 status = device_create_file(dev, &dev_attr_min_microamps);
2557 if (status < 0)
2558 return status;
2559 status = device_create_file(dev, &dev_attr_max_microamps);
2560 if (status < 0)
2561 return status;
2564 /* suspend mode constraints need multiple supporting methods */
2565 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2566 return status;
2568 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2569 if (status < 0)
2570 return status;
2571 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2572 if (status < 0)
2573 return status;
2574 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2575 if (status < 0)
2576 return status;
2578 if (ops->set_suspend_voltage) {
2579 status = device_create_file(dev,
2580 &dev_attr_suspend_standby_microvolts);
2581 if (status < 0)
2582 return status;
2583 status = device_create_file(dev,
2584 &dev_attr_suspend_mem_microvolts);
2585 if (status < 0)
2586 return status;
2587 status = device_create_file(dev,
2588 &dev_attr_suspend_disk_microvolts);
2589 if (status < 0)
2590 return status;
2593 if (ops->set_suspend_mode) {
2594 status = device_create_file(dev,
2595 &dev_attr_suspend_standby_mode);
2596 if (status < 0)
2597 return status;
2598 status = device_create_file(dev,
2599 &dev_attr_suspend_mem_mode);
2600 if (status < 0)
2601 return status;
2602 status = device_create_file(dev,
2603 &dev_attr_suspend_disk_mode);
2604 if (status < 0)
2605 return status;
2608 return status;
2611 static void rdev_init_debugfs(struct regulator_dev *rdev)
2613 #ifdef CONFIG_DEBUG_FS
2614 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2615 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2616 rdev_warn(rdev, "Failed to create debugfs directory\n");
2617 rdev->debugfs = NULL;
2618 return;
2621 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2622 &rdev->use_count);
2623 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2624 &rdev->open_count);
2625 #endif
2629 * regulator_register - register regulator
2630 * @regulator_desc: regulator to register
2631 * @dev: struct device for the regulator
2632 * @init_data: platform provided init data, passed through by driver
2633 * @driver_data: private regulator data
2635 * Called by regulator drivers to register a regulator.
2636 * Returns 0 on success.
2638 struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
2639 struct device *dev, const struct regulator_init_data *init_data,
2640 void *driver_data)
2642 static atomic_t regulator_no = ATOMIC_INIT(0);
2643 struct regulator_dev *rdev;
2644 int ret, i;
2646 if (regulator_desc == NULL)
2647 return ERR_PTR(-EINVAL);
2649 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2650 return ERR_PTR(-EINVAL);
2652 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2653 regulator_desc->type != REGULATOR_CURRENT)
2654 return ERR_PTR(-EINVAL);
2656 if (!init_data)
2657 return ERR_PTR(-EINVAL);
2659 /* Only one of each should be implemented */
2660 WARN_ON(regulator_desc->ops->get_voltage &&
2661 regulator_desc->ops->get_voltage_sel);
2662 WARN_ON(regulator_desc->ops->set_voltage &&
2663 regulator_desc->ops->set_voltage_sel);
2665 /* If we're using selectors we must implement list_voltage. */
2666 if (regulator_desc->ops->get_voltage_sel &&
2667 !regulator_desc->ops->list_voltage) {
2668 return ERR_PTR(-EINVAL);
2670 if (regulator_desc->ops->set_voltage_sel &&
2671 !regulator_desc->ops->list_voltage) {
2672 return ERR_PTR(-EINVAL);
2675 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2676 if (rdev == NULL)
2677 return ERR_PTR(-ENOMEM);
2679 mutex_lock(&regulator_list_mutex);
2681 mutex_init(&rdev->mutex);
2682 rdev->reg_data = driver_data;
2683 rdev->owner = regulator_desc->owner;
2684 rdev->desc = regulator_desc;
2685 INIT_LIST_HEAD(&rdev->consumer_list);
2686 INIT_LIST_HEAD(&rdev->list);
2687 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2688 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
2690 /* preform any regulator specific init */
2691 if (init_data->regulator_init) {
2692 ret = init_data->regulator_init(rdev->reg_data);
2693 if (ret < 0)
2694 goto clean;
2697 /* register with sysfs */
2698 rdev->dev.class = &regulator_class;
2699 rdev->dev.parent = dev;
2700 dev_set_name(&rdev->dev, "regulator.%d",
2701 atomic_inc_return(&regulator_no) - 1);
2702 ret = device_register(&rdev->dev);
2703 if (ret != 0) {
2704 put_device(&rdev->dev);
2705 goto clean;
2708 dev_set_drvdata(&rdev->dev, rdev);
2710 /* set regulator constraints */
2711 ret = set_machine_constraints(rdev, &init_data->constraints);
2712 if (ret < 0)
2713 goto scrub;
2715 /* add attributes supported by this regulator */
2716 ret = add_regulator_attributes(rdev);
2717 if (ret < 0)
2718 goto scrub;
2720 if (init_data->supply_regulator) {
2721 struct regulator_dev *r;
2722 int found = 0;
2724 list_for_each_entry(r, &regulator_list, list) {
2725 if (strcmp(rdev_get_name(r),
2726 init_data->supply_regulator) == 0) {
2727 found = 1;
2728 break;
2732 if (!found) {
2733 dev_err(dev, "Failed to find supply %s\n",
2734 init_data->supply_regulator);
2735 ret = -ENODEV;
2736 goto scrub;
2739 ret = set_supply(rdev, r);
2740 if (ret < 0)
2741 goto scrub;
2744 /* add consumers devices */
2745 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2746 ret = set_consumer_device_supply(rdev,
2747 init_data->consumer_supplies[i].dev,
2748 init_data->consumer_supplies[i].dev_name,
2749 init_data->consumer_supplies[i].supply);
2750 if (ret < 0) {
2751 dev_err(dev, "Failed to set supply %s\n",
2752 init_data->consumer_supplies[i].supply);
2753 goto unset_supplies;
2757 list_add(&rdev->list, &regulator_list);
2759 rdev_init_debugfs(rdev);
2760 out:
2761 mutex_unlock(&regulator_list_mutex);
2762 return rdev;
2764 unset_supplies:
2765 unset_regulator_supplies(rdev);
2767 scrub:
2768 kfree(rdev->constraints);
2769 device_unregister(&rdev->dev);
2770 /* device core frees rdev */
2771 rdev = ERR_PTR(ret);
2772 goto out;
2774 clean:
2775 kfree(rdev);
2776 rdev = ERR_PTR(ret);
2777 goto out;
2779 EXPORT_SYMBOL_GPL(regulator_register);
2782 * regulator_unregister - unregister regulator
2783 * @rdev: regulator to unregister
2785 * Called by regulator drivers to unregister a regulator.
2787 void regulator_unregister(struct regulator_dev *rdev)
2789 if (rdev == NULL)
2790 return;
2792 mutex_lock(&regulator_list_mutex);
2793 #ifdef CONFIG_DEBUG_FS
2794 debugfs_remove_recursive(rdev->debugfs);
2795 #endif
2796 flush_work_sync(&rdev->disable_work.work);
2797 WARN_ON(rdev->open_count);
2798 unset_regulator_supplies(rdev);
2799 list_del(&rdev->list);
2800 if (rdev->supply)
2801 regulator_put(rdev->supply);
2802 kfree(rdev->constraints);
2803 device_unregister(&rdev->dev);
2804 mutex_unlock(&regulator_list_mutex);
2806 EXPORT_SYMBOL_GPL(regulator_unregister);
2809 * regulator_suspend_prepare - prepare regulators for system wide suspend
2810 * @state: system suspend state
2812 * Configure each regulator with it's suspend operating parameters for state.
2813 * This will usually be called by machine suspend code prior to supending.
2815 int regulator_suspend_prepare(suspend_state_t state)
2817 struct regulator_dev *rdev;
2818 int ret = 0;
2820 /* ON is handled by regulator active state */
2821 if (state == PM_SUSPEND_ON)
2822 return -EINVAL;
2824 mutex_lock(&regulator_list_mutex);
2825 list_for_each_entry(rdev, &regulator_list, list) {
2827 mutex_lock(&rdev->mutex);
2828 ret = suspend_prepare(rdev, state);
2829 mutex_unlock(&rdev->mutex);
2831 if (ret < 0) {
2832 rdev_err(rdev, "failed to prepare\n");
2833 goto out;
2836 out:
2837 mutex_unlock(&regulator_list_mutex);
2838 return ret;
2840 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2843 * regulator_suspend_finish - resume regulators from system wide suspend
2845 * Turn on regulators that might be turned off by regulator_suspend_prepare
2846 * and that should be turned on according to the regulators properties.
2848 int regulator_suspend_finish(void)
2850 struct regulator_dev *rdev;
2851 int ret = 0, error;
2853 mutex_lock(&regulator_list_mutex);
2854 list_for_each_entry(rdev, &regulator_list, list) {
2855 struct regulator_ops *ops = rdev->desc->ops;
2857 mutex_lock(&rdev->mutex);
2858 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2859 ops->enable) {
2860 error = ops->enable(rdev);
2861 if (error)
2862 ret = error;
2863 } else {
2864 if (!has_full_constraints)
2865 goto unlock;
2866 if (!ops->disable)
2867 goto unlock;
2868 if (ops->is_enabled && !ops->is_enabled(rdev))
2869 goto unlock;
2871 error = ops->disable(rdev);
2872 if (error)
2873 ret = error;
2875 unlock:
2876 mutex_unlock(&rdev->mutex);
2878 mutex_unlock(&regulator_list_mutex);
2879 return ret;
2881 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2884 * regulator_has_full_constraints - the system has fully specified constraints
2886 * Calling this function will cause the regulator API to disable all
2887 * regulators which have a zero use count and don't have an always_on
2888 * constraint in a late_initcall.
2890 * The intention is that this will become the default behaviour in a
2891 * future kernel release so users are encouraged to use this facility
2892 * now.
2894 void regulator_has_full_constraints(void)
2896 has_full_constraints = 1;
2898 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2901 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2903 * Calling this function will cause the regulator API to provide a
2904 * dummy regulator to consumers if no physical regulator is found,
2905 * allowing most consumers to proceed as though a regulator were
2906 * configured. This allows systems such as those with software
2907 * controllable regulators for the CPU core only to be brought up more
2908 * readily.
2910 void regulator_use_dummy_regulator(void)
2912 board_wants_dummy_regulator = true;
2914 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2917 * rdev_get_drvdata - get rdev regulator driver data
2918 * @rdev: regulator
2920 * Get rdev regulator driver private data. This call can be used in the
2921 * regulator driver context.
2923 void *rdev_get_drvdata(struct regulator_dev *rdev)
2925 return rdev->reg_data;
2927 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2930 * regulator_get_drvdata - get regulator driver data
2931 * @regulator: regulator
2933 * Get regulator driver private data. This call can be used in the consumer
2934 * driver context when non API regulator specific functions need to be called.
2936 void *regulator_get_drvdata(struct regulator *regulator)
2938 return regulator->rdev->reg_data;
2940 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2943 * regulator_set_drvdata - set regulator driver data
2944 * @regulator: regulator
2945 * @data: data
2947 void regulator_set_drvdata(struct regulator *regulator, void *data)
2949 regulator->rdev->reg_data = data;
2951 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2954 * regulator_get_id - get regulator ID
2955 * @rdev: regulator
2957 int rdev_get_id(struct regulator_dev *rdev)
2959 return rdev->desc->id;
2961 EXPORT_SYMBOL_GPL(rdev_get_id);
2963 struct device *rdev_get_dev(struct regulator_dev *rdev)
2965 return &rdev->dev;
2967 EXPORT_SYMBOL_GPL(rdev_get_dev);
2969 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2971 return reg_init_data->driver_data;
2973 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2975 #ifdef CONFIG_DEBUG_FS
2976 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
2977 size_t count, loff_t *ppos)
2979 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2980 ssize_t len, ret = 0;
2981 struct regulator_map *map;
2983 if (!buf)
2984 return -ENOMEM;
2986 list_for_each_entry(map, &regulator_map_list, list) {
2987 len = snprintf(buf + ret, PAGE_SIZE - ret,
2988 "%s -> %s.%s\n",
2989 rdev_get_name(map->regulator), map->dev_name,
2990 map->supply);
2991 if (len >= 0)
2992 ret += len;
2993 if (ret > PAGE_SIZE) {
2994 ret = PAGE_SIZE;
2995 break;
2999 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3001 kfree(buf);
3003 return ret;
3006 static const struct file_operations supply_map_fops = {
3007 .read = supply_map_read_file,
3008 .llseek = default_llseek,
3010 #endif
3012 static int __init regulator_init(void)
3014 int ret;
3016 ret = class_register(&regulator_class);
3018 #ifdef CONFIG_DEBUG_FS
3019 debugfs_root = debugfs_create_dir("regulator", NULL);
3020 if (IS_ERR(debugfs_root) || !debugfs_root) {
3021 pr_warn("regulator: Failed to create debugfs directory\n");
3022 debugfs_root = NULL;
3025 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3026 NULL, &supply_map_fops)))
3027 pr_warn("regulator: Failed to create supplies debugfs\n");
3028 #endif
3030 regulator_dummy_init();
3032 return ret;
3035 /* init early to allow our consumers to complete system booting */
3036 core_initcall(regulator_init);
3038 static int __init regulator_init_complete(void)
3040 struct regulator_dev *rdev;
3041 struct regulator_ops *ops;
3042 struct regulation_constraints *c;
3043 int enabled, ret;
3045 mutex_lock(&regulator_list_mutex);
3047 /* If we have a full configuration then disable any regulators
3048 * which are not in use or always_on. This will become the
3049 * default behaviour in the future.
3051 list_for_each_entry(rdev, &regulator_list, list) {
3052 ops = rdev->desc->ops;
3053 c = rdev->constraints;
3055 if (!ops->disable || (c && c->always_on))
3056 continue;
3058 mutex_lock(&rdev->mutex);
3060 if (rdev->use_count)
3061 goto unlock;
3063 /* If we can't read the status assume it's on. */
3064 if (ops->is_enabled)
3065 enabled = ops->is_enabled(rdev);
3066 else
3067 enabled = 1;
3069 if (!enabled)
3070 goto unlock;
3072 if (has_full_constraints) {
3073 /* We log since this may kill the system if it
3074 * goes wrong. */
3075 rdev_info(rdev, "disabling\n");
3076 ret = ops->disable(rdev);
3077 if (ret != 0) {
3078 rdev_err(rdev, "couldn't disable: %d\n", ret);
3080 } else {
3081 /* The intention is that in future we will
3082 * assume that full constraints are provided
3083 * so warn even if we aren't going to do
3084 * anything here.
3086 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3089 unlock:
3090 mutex_unlock(&rdev->mutex);
3093 mutex_unlock(&regulator_list_mutex);
3095 return 0;
3097 late_initcall(regulator_init_complete);