drivers/rtc/rtc-m48t59.c: use dev_get_platdata()
[linux-2.6.git] / drivers / regulator / core.c
blob6382f0af353bc257e3ee6c3b545f75c0c1f2904e
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/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/of.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
38 #include "dummy.h"
39 #include "internal.h"
41 #define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
59 static struct dentry *debugfs_root;
62 * struct regulator_map
64 * Used to provide symbolic supply names to devices.
66 struct regulator_map {
67 struct list_head list;
68 const char *dev_name; /* The dev_name() for the consumer */
69 const char *supply;
70 struct regulator_dev *regulator;
74 * struct regulator_enable_gpio
76 * Management for shared enable GPIO pin
78 struct regulator_enable_gpio {
79 struct list_head list;
80 int gpio;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
87 * struct regulator_supply_alias
89 * Used to map lookups for a supply onto an alternative device.
91 struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
99 static int _regulator_is_enabled(struct regulator_dev *rdev);
100 static int _regulator_disable(struct regulator_dev *rdev);
101 static int _regulator_get_voltage(struct regulator_dev *rdev);
102 static int _regulator_get_current_limit(struct regulator_dev *rdev);
103 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104 static void _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108 static struct regulator *create_regulator(struct regulator_dev *rdev,
109 struct device *dev,
110 const char *supply_name);
112 static const char *rdev_get_name(struct regulator_dev *rdev)
114 if (rdev->constraints && rdev->constraints->name)
115 return rdev->constraints->name;
116 else if (rdev->desc->name)
117 return rdev->desc->name;
118 else
119 return "";
123 * of_get_regulator - get a regulator device node based on supply name
124 * @dev: Device pointer for the consumer (of regulator) device
125 * @supply: regulator supply name
127 * Extract the regulator device node corresponding to the supply name.
128 * returns the device node corresponding to the regulator if found, else
129 * returns NULL.
131 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
133 struct device_node *regnode = NULL;
134 char prop_name[32]; /* 32 is max size of property name */
136 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
138 snprintf(prop_name, 32, "%s-supply", supply);
139 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
141 if (!regnode) {
142 dev_dbg(dev, "Looking up %s property in node %s failed",
143 prop_name, dev->of_node->full_name);
144 return NULL;
146 return regnode;
149 static int _regulator_can_change_status(struct regulator_dev *rdev)
151 if (!rdev->constraints)
152 return 0;
154 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
155 return 1;
156 else
157 return 0;
160 /* Platform voltage constraint check */
161 static int regulator_check_voltage(struct regulator_dev *rdev,
162 int *min_uV, int *max_uV)
164 BUG_ON(*min_uV > *max_uV);
166 if (!rdev->constraints) {
167 rdev_err(rdev, "no constraints\n");
168 return -ENODEV;
170 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
171 rdev_err(rdev, "operation not allowed\n");
172 return -EPERM;
175 if (*max_uV > rdev->constraints->max_uV)
176 *max_uV = rdev->constraints->max_uV;
177 if (*min_uV < rdev->constraints->min_uV)
178 *min_uV = rdev->constraints->min_uV;
180 if (*min_uV > *max_uV) {
181 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
182 *min_uV, *max_uV);
183 return -EINVAL;
186 return 0;
189 /* Make sure we select a voltage that suits the needs of all
190 * regulator consumers
192 static int regulator_check_consumers(struct regulator_dev *rdev,
193 int *min_uV, int *max_uV)
195 struct regulator *regulator;
197 list_for_each_entry(regulator, &rdev->consumer_list, list) {
199 * Assume consumers that didn't say anything are OK
200 * with anything in the constraint range.
202 if (!regulator->min_uV && !regulator->max_uV)
203 continue;
205 if (*max_uV > regulator->max_uV)
206 *max_uV = regulator->max_uV;
207 if (*min_uV < regulator->min_uV)
208 *min_uV = regulator->min_uV;
211 if (*min_uV > *max_uV) {
212 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
213 *min_uV, *max_uV);
214 return -EINVAL;
217 return 0;
220 /* current constraint check */
221 static int regulator_check_current_limit(struct regulator_dev *rdev,
222 int *min_uA, int *max_uA)
224 BUG_ON(*min_uA > *max_uA);
226 if (!rdev->constraints) {
227 rdev_err(rdev, "no constraints\n");
228 return -ENODEV;
230 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
231 rdev_err(rdev, "operation not allowed\n");
232 return -EPERM;
235 if (*max_uA > rdev->constraints->max_uA)
236 *max_uA = rdev->constraints->max_uA;
237 if (*min_uA < rdev->constraints->min_uA)
238 *min_uA = rdev->constraints->min_uA;
240 if (*min_uA > *max_uA) {
241 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
242 *min_uA, *max_uA);
243 return -EINVAL;
246 return 0;
249 /* operating mode constraint check */
250 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
252 switch (*mode) {
253 case REGULATOR_MODE_FAST:
254 case REGULATOR_MODE_NORMAL:
255 case REGULATOR_MODE_IDLE:
256 case REGULATOR_MODE_STANDBY:
257 break;
258 default:
259 rdev_err(rdev, "invalid mode %x specified\n", *mode);
260 return -EINVAL;
263 if (!rdev->constraints) {
264 rdev_err(rdev, "no constraints\n");
265 return -ENODEV;
267 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
268 rdev_err(rdev, "operation not allowed\n");
269 return -EPERM;
272 /* The modes are bitmasks, the most power hungry modes having
273 * the lowest values. If the requested mode isn't supported
274 * try higher modes. */
275 while (*mode) {
276 if (rdev->constraints->valid_modes_mask & *mode)
277 return 0;
278 *mode /= 2;
281 return -EINVAL;
284 /* dynamic regulator mode switching constraint check */
285 static int regulator_check_drms(struct regulator_dev *rdev)
287 if (!rdev->constraints) {
288 rdev_err(rdev, "no constraints\n");
289 return -ENODEV;
291 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
292 rdev_err(rdev, "operation not allowed\n");
293 return -EPERM;
295 return 0;
298 static ssize_t regulator_uV_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
301 struct regulator_dev *rdev = dev_get_drvdata(dev);
302 ssize_t ret;
304 mutex_lock(&rdev->mutex);
305 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
306 mutex_unlock(&rdev->mutex);
308 return ret;
310 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
312 static ssize_t regulator_uA_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 struct regulator_dev *rdev = dev_get_drvdata(dev);
317 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
319 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
321 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
322 char *buf)
324 struct regulator_dev *rdev = dev_get_drvdata(dev);
326 return sprintf(buf, "%s\n", rdev_get_name(rdev));
328 static DEVICE_ATTR_RO(name);
330 static ssize_t regulator_print_opmode(char *buf, int mode)
332 switch (mode) {
333 case REGULATOR_MODE_FAST:
334 return sprintf(buf, "fast\n");
335 case REGULATOR_MODE_NORMAL:
336 return sprintf(buf, "normal\n");
337 case REGULATOR_MODE_IDLE:
338 return sprintf(buf, "idle\n");
339 case REGULATOR_MODE_STANDBY:
340 return sprintf(buf, "standby\n");
342 return sprintf(buf, "unknown\n");
345 static ssize_t regulator_opmode_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct regulator_dev *rdev = dev_get_drvdata(dev);
350 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
352 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
354 static ssize_t regulator_print_state(char *buf, int state)
356 if (state > 0)
357 return sprintf(buf, "enabled\n");
358 else if (state == 0)
359 return sprintf(buf, "disabled\n");
360 else
361 return sprintf(buf, "unknown\n");
364 static ssize_t regulator_state_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
368 ssize_t ret;
370 mutex_lock(&rdev->mutex);
371 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
372 mutex_unlock(&rdev->mutex);
374 return ret;
376 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
378 static ssize_t regulator_status_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 int status;
383 char *label;
385 status = rdev->desc->ops->get_status(rdev);
386 if (status < 0)
387 return status;
389 switch (status) {
390 case REGULATOR_STATUS_OFF:
391 label = "off";
392 break;
393 case REGULATOR_STATUS_ON:
394 label = "on";
395 break;
396 case REGULATOR_STATUS_ERROR:
397 label = "error";
398 break;
399 case REGULATOR_STATUS_FAST:
400 label = "fast";
401 break;
402 case REGULATOR_STATUS_NORMAL:
403 label = "normal";
404 break;
405 case REGULATOR_STATUS_IDLE:
406 label = "idle";
407 break;
408 case REGULATOR_STATUS_STANDBY:
409 label = "standby";
410 break;
411 case REGULATOR_STATUS_BYPASS:
412 label = "bypass";
413 break;
414 case REGULATOR_STATUS_UNDEFINED:
415 label = "undefined";
416 break;
417 default:
418 return -ERANGE;
421 return sprintf(buf, "%s\n", label);
423 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
425 static ssize_t regulator_min_uA_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
428 struct regulator_dev *rdev = dev_get_drvdata(dev);
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
433 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
435 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
437 static ssize_t regulator_max_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
440 struct regulator_dev *rdev = dev_get_drvdata(dev);
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
445 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
447 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
449 static ssize_t regulator_min_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
457 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
459 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
461 static ssize_t regulator_max_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct regulator_dev *rdev = dev_get_drvdata(dev);
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
469 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
471 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
473 static ssize_t regulator_total_uA_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
477 struct regulator *regulator;
478 int uA = 0;
480 mutex_lock(&rdev->mutex);
481 list_for_each_entry(regulator, &rdev->consumer_list, list)
482 uA += regulator->uA_load;
483 mutex_unlock(&rdev->mutex);
484 return sprintf(buf, "%d\n", uA);
486 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
488 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
489 char *buf)
491 struct regulator_dev *rdev = dev_get_drvdata(dev);
492 return sprintf(buf, "%d\n", rdev->use_count);
494 static DEVICE_ATTR_RO(num_users);
496 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
497 char *buf)
499 struct regulator_dev *rdev = dev_get_drvdata(dev);
501 switch (rdev->desc->type) {
502 case REGULATOR_VOLTAGE:
503 return sprintf(buf, "voltage\n");
504 case REGULATOR_CURRENT:
505 return sprintf(buf, "current\n");
507 return sprintf(buf, "unknown\n");
509 static DEVICE_ATTR_RO(type);
511 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
514 struct regulator_dev *rdev = dev_get_drvdata(dev);
516 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
518 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
519 regulator_suspend_mem_uV_show, NULL);
521 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
522 struct device_attribute *attr, char *buf)
524 struct regulator_dev *rdev = dev_get_drvdata(dev);
526 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
528 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
529 regulator_suspend_disk_uV_show, NULL);
531 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
534 struct regulator_dev *rdev = dev_get_drvdata(dev);
536 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
538 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
539 regulator_suspend_standby_uV_show, NULL);
541 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
544 struct regulator_dev *rdev = dev_get_drvdata(dev);
546 return regulator_print_opmode(buf,
547 rdev->constraints->state_mem.mode);
549 static DEVICE_ATTR(suspend_mem_mode, 0444,
550 regulator_suspend_mem_mode_show, NULL);
552 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
553 struct device_attribute *attr, char *buf)
555 struct regulator_dev *rdev = dev_get_drvdata(dev);
557 return regulator_print_opmode(buf,
558 rdev->constraints->state_disk.mode);
560 static DEVICE_ATTR(suspend_disk_mode, 0444,
561 regulator_suspend_disk_mode_show, NULL);
563 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 struct regulator_dev *rdev = dev_get_drvdata(dev);
568 return regulator_print_opmode(buf,
569 rdev->constraints->state_standby.mode);
571 static DEVICE_ATTR(suspend_standby_mode, 0444,
572 regulator_suspend_standby_mode_show, NULL);
574 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
577 struct regulator_dev *rdev = dev_get_drvdata(dev);
579 return regulator_print_state(buf,
580 rdev->constraints->state_mem.enabled);
582 static DEVICE_ATTR(suspend_mem_state, 0444,
583 regulator_suspend_mem_state_show, NULL);
585 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
588 struct regulator_dev *rdev = dev_get_drvdata(dev);
590 return regulator_print_state(buf,
591 rdev->constraints->state_disk.enabled);
593 static DEVICE_ATTR(suspend_disk_state, 0444,
594 regulator_suspend_disk_state_show, NULL);
596 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
597 struct device_attribute *attr, char *buf)
599 struct regulator_dev *rdev = dev_get_drvdata(dev);
601 return regulator_print_state(buf,
602 rdev->constraints->state_standby.enabled);
604 static DEVICE_ATTR(suspend_standby_state, 0444,
605 regulator_suspend_standby_state_show, NULL);
607 static ssize_t regulator_bypass_show(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct regulator_dev *rdev = dev_get_drvdata(dev);
611 const char *report;
612 bool bypass;
613 int ret;
615 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
617 if (ret != 0)
618 report = "unknown";
619 else if (bypass)
620 report = "enabled";
621 else
622 report = "disabled";
624 return sprintf(buf, "%s\n", report);
626 static DEVICE_ATTR(bypass, 0444,
627 regulator_bypass_show, NULL);
630 * These are the only attributes are present for all regulators.
631 * Other attributes are a function of regulator functionality.
633 static struct attribute *regulator_dev_attrs[] = {
634 &dev_attr_name.attr,
635 &dev_attr_num_users.attr,
636 &dev_attr_type.attr,
637 NULL,
639 ATTRIBUTE_GROUPS(regulator_dev);
641 static void regulator_dev_release(struct device *dev)
643 struct regulator_dev *rdev = dev_get_drvdata(dev);
644 kfree(rdev);
647 static struct class regulator_class = {
648 .name = "regulator",
649 .dev_release = regulator_dev_release,
650 .dev_groups = regulator_dev_groups,
653 /* Calculate the new optimum regulator operating mode based on the new total
654 * consumer load. All locks held by caller */
655 static void drms_uA_update(struct regulator_dev *rdev)
657 struct regulator *sibling;
658 int current_uA = 0, output_uV, input_uV, err;
659 unsigned int mode;
661 err = regulator_check_drms(rdev);
662 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
663 (!rdev->desc->ops->get_voltage &&
664 !rdev->desc->ops->get_voltage_sel) ||
665 !rdev->desc->ops->set_mode)
666 return;
668 /* get output voltage */
669 output_uV = _regulator_get_voltage(rdev);
670 if (output_uV <= 0)
671 return;
673 /* get input voltage */
674 input_uV = 0;
675 if (rdev->supply)
676 input_uV = regulator_get_voltage(rdev->supply);
677 if (input_uV <= 0)
678 input_uV = rdev->constraints->input_uV;
679 if (input_uV <= 0)
680 return;
682 /* calc total requested load */
683 list_for_each_entry(sibling, &rdev->consumer_list, list)
684 current_uA += sibling->uA_load;
686 /* now get the optimum mode for our new total regulator load */
687 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
688 output_uV, current_uA);
690 /* check the new mode is allowed */
691 err = regulator_mode_constrain(rdev, &mode);
692 if (err == 0)
693 rdev->desc->ops->set_mode(rdev, mode);
696 static int suspend_set_state(struct regulator_dev *rdev,
697 struct regulator_state *rstate)
699 int ret = 0;
701 /* If we have no suspend mode configration don't set anything;
702 * only warn if the driver implements set_suspend_voltage or
703 * set_suspend_mode callback.
705 if (!rstate->enabled && !rstate->disabled) {
706 if (rdev->desc->ops->set_suspend_voltage ||
707 rdev->desc->ops->set_suspend_mode)
708 rdev_warn(rdev, "No configuration\n");
709 return 0;
712 if (rstate->enabled && rstate->disabled) {
713 rdev_err(rdev, "invalid configuration\n");
714 return -EINVAL;
717 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
718 ret = rdev->desc->ops->set_suspend_enable(rdev);
719 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
720 ret = rdev->desc->ops->set_suspend_disable(rdev);
721 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
722 ret = 0;
724 if (ret < 0) {
725 rdev_err(rdev, "failed to enabled/disable\n");
726 return ret;
729 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
730 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
731 if (ret < 0) {
732 rdev_err(rdev, "failed to set voltage\n");
733 return ret;
737 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
738 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
739 if (ret < 0) {
740 rdev_err(rdev, "failed to set mode\n");
741 return ret;
744 return ret;
747 /* locks held by caller */
748 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750 if (!rdev->constraints)
751 return -EINVAL;
753 switch (state) {
754 case PM_SUSPEND_STANDBY:
755 return suspend_set_state(rdev,
756 &rdev->constraints->state_standby);
757 case PM_SUSPEND_MEM:
758 return suspend_set_state(rdev,
759 &rdev->constraints->state_mem);
760 case PM_SUSPEND_MAX:
761 return suspend_set_state(rdev,
762 &rdev->constraints->state_disk);
763 default:
764 return -EINVAL;
768 static void print_constraints(struct regulator_dev *rdev)
770 struct regulation_constraints *constraints = rdev->constraints;
771 char buf[80] = "";
772 int count = 0;
773 int ret;
775 if (constraints->min_uV && constraints->max_uV) {
776 if (constraints->min_uV == constraints->max_uV)
777 count += sprintf(buf + count, "%d mV ",
778 constraints->min_uV / 1000);
779 else
780 count += sprintf(buf + count, "%d <--> %d mV ",
781 constraints->min_uV / 1000,
782 constraints->max_uV / 1000);
785 if (!constraints->min_uV ||
786 constraints->min_uV != constraints->max_uV) {
787 ret = _regulator_get_voltage(rdev);
788 if (ret > 0)
789 count += sprintf(buf + count, "at %d mV ", ret / 1000);
792 if (constraints->uV_offset)
793 count += sprintf(buf, "%dmV offset ",
794 constraints->uV_offset / 1000);
796 if (constraints->min_uA && constraints->max_uA) {
797 if (constraints->min_uA == constraints->max_uA)
798 count += sprintf(buf + count, "%d mA ",
799 constraints->min_uA / 1000);
800 else
801 count += sprintf(buf + count, "%d <--> %d mA ",
802 constraints->min_uA / 1000,
803 constraints->max_uA / 1000);
806 if (!constraints->min_uA ||
807 constraints->min_uA != constraints->max_uA) {
808 ret = _regulator_get_current_limit(rdev);
809 if (ret > 0)
810 count += sprintf(buf + count, "at %d mA ", ret / 1000);
813 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
814 count += sprintf(buf + count, "fast ");
815 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
816 count += sprintf(buf + count, "normal ");
817 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
818 count += sprintf(buf + count, "idle ");
819 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
820 count += sprintf(buf + count, "standby");
822 if (!count)
823 sprintf(buf, "no parameters");
825 rdev_info(rdev, "%s\n", buf);
827 if ((constraints->min_uV != constraints->max_uV) &&
828 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
829 rdev_warn(rdev,
830 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
833 static int machine_constraints_voltage(struct regulator_dev *rdev,
834 struct regulation_constraints *constraints)
836 struct regulator_ops *ops = rdev->desc->ops;
837 int ret;
839 /* do we need to apply the constraint voltage */
840 if (rdev->constraints->apply_uV &&
841 rdev->constraints->min_uV == rdev->constraints->max_uV) {
842 ret = _regulator_do_set_voltage(rdev,
843 rdev->constraints->min_uV,
844 rdev->constraints->max_uV);
845 if (ret < 0) {
846 rdev_err(rdev, "failed to apply %duV constraint\n",
847 rdev->constraints->min_uV);
848 return ret;
852 /* constrain machine-level voltage specs to fit
853 * the actual range supported by this regulator.
855 if (ops->list_voltage && rdev->desc->n_voltages) {
856 int count = rdev->desc->n_voltages;
857 int i;
858 int min_uV = INT_MAX;
859 int max_uV = INT_MIN;
860 int cmin = constraints->min_uV;
861 int cmax = constraints->max_uV;
863 /* it's safe to autoconfigure fixed-voltage supplies
864 and the constraints are used by list_voltage. */
865 if (count == 1 && !cmin) {
866 cmin = 1;
867 cmax = INT_MAX;
868 constraints->min_uV = cmin;
869 constraints->max_uV = cmax;
872 /* voltage constraints are optional */
873 if ((cmin == 0) && (cmax == 0))
874 return 0;
876 /* else require explicit machine-level constraints */
877 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
878 rdev_err(rdev, "invalid voltage constraints\n");
879 return -EINVAL;
882 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
883 for (i = 0; i < count; i++) {
884 int value;
886 value = ops->list_voltage(rdev, i);
887 if (value <= 0)
888 continue;
890 /* maybe adjust [min_uV..max_uV] */
891 if (value >= cmin && value < min_uV)
892 min_uV = value;
893 if (value <= cmax && value > max_uV)
894 max_uV = value;
897 /* final: [min_uV..max_uV] valid iff constraints valid */
898 if (max_uV < min_uV) {
899 rdev_err(rdev,
900 "unsupportable voltage constraints %u-%uuV\n",
901 min_uV, max_uV);
902 return -EINVAL;
905 /* use regulator's subset of machine constraints */
906 if (constraints->min_uV < min_uV) {
907 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
908 constraints->min_uV, min_uV);
909 constraints->min_uV = min_uV;
911 if (constraints->max_uV > max_uV) {
912 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
913 constraints->max_uV, max_uV);
914 constraints->max_uV = max_uV;
918 return 0;
921 static int machine_constraints_current(struct regulator_dev *rdev,
922 struct regulation_constraints *constraints)
924 struct regulator_ops *ops = rdev->desc->ops;
925 int ret;
927 if (!constraints->min_uA && !constraints->max_uA)
928 return 0;
930 if (constraints->min_uA > constraints->max_uA) {
931 rdev_err(rdev, "Invalid current constraints\n");
932 return -EINVAL;
935 if (!ops->set_current_limit || !ops->get_current_limit) {
936 rdev_warn(rdev, "Operation of current configuration missing\n");
937 return 0;
940 /* Set regulator current in constraints range */
941 ret = ops->set_current_limit(rdev, constraints->min_uA,
942 constraints->max_uA);
943 if (ret < 0) {
944 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
945 return ret;
948 return 0;
952 * set_machine_constraints - sets regulator constraints
953 * @rdev: regulator source
954 * @constraints: constraints to apply
956 * Allows platform initialisation code to define and constrain
957 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
958 * Constraints *must* be set by platform code in order for some
959 * regulator operations to proceed i.e. set_voltage, set_current_limit,
960 * set_mode.
962 static int set_machine_constraints(struct regulator_dev *rdev,
963 const struct regulation_constraints *constraints)
965 int ret = 0;
966 struct regulator_ops *ops = rdev->desc->ops;
968 if (constraints)
969 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
970 GFP_KERNEL);
971 else
972 rdev->constraints = kzalloc(sizeof(*constraints),
973 GFP_KERNEL);
974 if (!rdev->constraints)
975 return -ENOMEM;
977 ret = machine_constraints_voltage(rdev, rdev->constraints);
978 if (ret != 0)
979 goto out;
981 ret = machine_constraints_current(rdev, rdev->constraints);
982 if (ret != 0)
983 goto out;
985 /* do we need to setup our suspend state */
986 if (rdev->constraints->initial_state) {
987 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
988 if (ret < 0) {
989 rdev_err(rdev, "failed to set suspend state\n");
990 goto out;
994 if (rdev->constraints->initial_mode) {
995 if (!ops->set_mode) {
996 rdev_err(rdev, "no set_mode operation\n");
997 ret = -EINVAL;
998 goto out;
1001 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1002 if (ret < 0) {
1003 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1004 goto out;
1008 /* If the constraints say the regulator should be on at this point
1009 * and we have control then make sure it is enabled.
1011 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
1012 ops->enable) {
1013 ret = ops->enable(rdev);
1014 if (ret < 0) {
1015 rdev_err(rdev, "failed to enable\n");
1016 goto out;
1020 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1021 && ops->set_ramp_delay) {
1022 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1023 if (ret < 0) {
1024 rdev_err(rdev, "failed to set ramp_delay\n");
1025 goto out;
1029 print_constraints(rdev);
1030 return 0;
1031 out:
1032 kfree(rdev->constraints);
1033 rdev->constraints = NULL;
1034 return ret;
1038 * set_supply - set regulator supply regulator
1039 * @rdev: regulator name
1040 * @supply_rdev: supply regulator name
1042 * Called by platform initialisation code to set the supply regulator for this
1043 * regulator. This ensures that a regulators supply will also be enabled by the
1044 * core if it's child is enabled.
1046 static int set_supply(struct regulator_dev *rdev,
1047 struct regulator_dev *supply_rdev)
1049 int err;
1051 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1053 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1054 if (rdev->supply == NULL) {
1055 err = -ENOMEM;
1056 return err;
1058 supply_rdev->open_count++;
1060 return 0;
1064 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1065 * @rdev: regulator source
1066 * @consumer_dev_name: dev_name() string for device supply applies to
1067 * @supply: symbolic name for supply
1069 * Allows platform initialisation code to map physical regulator
1070 * sources to symbolic names for supplies for use by devices. Devices
1071 * should use these symbolic names to request regulators, avoiding the
1072 * need to provide board-specific regulator names as platform data.
1074 static int set_consumer_device_supply(struct regulator_dev *rdev,
1075 const char *consumer_dev_name,
1076 const char *supply)
1078 struct regulator_map *node;
1079 int has_dev;
1081 if (supply == NULL)
1082 return -EINVAL;
1084 if (consumer_dev_name != NULL)
1085 has_dev = 1;
1086 else
1087 has_dev = 0;
1089 list_for_each_entry(node, &regulator_map_list, list) {
1090 if (node->dev_name && consumer_dev_name) {
1091 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1092 continue;
1093 } else if (node->dev_name || consumer_dev_name) {
1094 continue;
1097 if (strcmp(node->supply, supply) != 0)
1098 continue;
1100 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1101 consumer_dev_name,
1102 dev_name(&node->regulator->dev),
1103 node->regulator->desc->name,
1104 supply,
1105 dev_name(&rdev->dev), rdev_get_name(rdev));
1106 return -EBUSY;
1109 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1110 if (node == NULL)
1111 return -ENOMEM;
1113 node->regulator = rdev;
1114 node->supply = supply;
1116 if (has_dev) {
1117 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1118 if (node->dev_name == NULL) {
1119 kfree(node);
1120 return -ENOMEM;
1124 list_add(&node->list, &regulator_map_list);
1125 return 0;
1128 static void unset_regulator_supplies(struct regulator_dev *rdev)
1130 struct regulator_map *node, *n;
1132 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1133 if (rdev == node->regulator) {
1134 list_del(&node->list);
1135 kfree(node->dev_name);
1136 kfree(node);
1141 #define REG_STR_SIZE 64
1143 static struct regulator *create_regulator(struct regulator_dev *rdev,
1144 struct device *dev,
1145 const char *supply_name)
1147 struct regulator *regulator;
1148 char buf[REG_STR_SIZE];
1149 int err, size;
1151 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1152 if (regulator == NULL)
1153 return NULL;
1155 mutex_lock(&rdev->mutex);
1156 regulator->rdev = rdev;
1157 list_add(&regulator->list, &rdev->consumer_list);
1159 if (dev) {
1160 regulator->dev = dev;
1162 /* Add a link to the device sysfs entry */
1163 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1164 dev->kobj.name, supply_name);
1165 if (size >= REG_STR_SIZE)
1166 goto overflow_err;
1168 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1169 if (regulator->supply_name == NULL)
1170 goto overflow_err;
1172 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1173 buf);
1174 if (err) {
1175 rdev_warn(rdev, "could not add device link %s err %d\n",
1176 dev->kobj.name, err);
1177 /* non-fatal */
1179 } else {
1180 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1181 if (regulator->supply_name == NULL)
1182 goto overflow_err;
1185 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1186 rdev->debugfs);
1187 if (!regulator->debugfs) {
1188 rdev_warn(rdev, "Failed to create debugfs directory\n");
1189 } else {
1190 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1191 &regulator->uA_load);
1192 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1193 &regulator->min_uV);
1194 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1195 &regulator->max_uV);
1199 * Check now if the regulator is an always on regulator - if
1200 * it is then we don't need to do nearly so much work for
1201 * enable/disable calls.
1203 if (!_regulator_can_change_status(rdev) &&
1204 _regulator_is_enabled(rdev))
1205 regulator->always_on = true;
1207 mutex_unlock(&rdev->mutex);
1208 return regulator;
1209 overflow_err:
1210 list_del(&regulator->list);
1211 kfree(regulator);
1212 mutex_unlock(&rdev->mutex);
1213 return NULL;
1216 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1218 if (rdev->constraints && rdev->constraints->enable_time)
1219 return rdev->constraints->enable_time;
1220 if (!rdev->desc->ops->enable_time)
1221 return rdev->desc->enable_time;
1222 return rdev->desc->ops->enable_time(rdev);
1225 static struct regulator_supply_alias *regulator_find_supply_alias(
1226 struct device *dev, const char *supply)
1228 struct regulator_supply_alias *map;
1230 list_for_each_entry(map, &regulator_supply_alias_list, list)
1231 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1232 return map;
1234 return NULL;
1237 static void regulator_supply_alias(struct device **dev, const char **supply)
1239 struct regulator_supply_alias *map;
1241 map = regulator_find_supply_alias(*dev, *supply);
1242 if (map) {
1243 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1244 *supply, map->alias_supply,
1245 dev_name(map->alias_dev));
1246 *dev = map->alias_dev;
1247 *supply = map->alias_supply;
1251 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1252 const char *supply,
1253 int *ret)
1255 struct regulator_dev *r;
1256 struct device_node *node;
1257 struct regulator_map *map;
1258 const char *devname = NULL;
1260 regulator_supply_alias(&dev, &supply);
1262 /* first do a dt based lookup */
1263 if (dev && dev->of_node) {
1264 node = of_get_regulator(dev, supply);
1265 if (node) {
1266 list_for_each_entry(r, &regulator_list, list)
1267 if (r->dev.parent &&
1268 node == r->dev.of_node)
1269 return r;
1270 } else {
1272 * If we couldn't even get the node then it's
1273 * not just that the device didn't register
1274 * yet, there's no node and we'll never
1275 * succeed.
1277 *ret = -ENODEV;
1281 /* if not found, try doing it non-dt way */
1282 if (dev)
1283 devname = dev_name(dev);
1285 list_for_each_entry(r, &regulator_list, list)
1286 if (strcmp(rdev_get_name(r), supply) == 0)
1287 return r;
1289 list_for_each_entry(map, &regulator_map_list, list) {
1290 /* If the mapping has a device set up it must match */
1291 if (map->dev_name &&
1292 (!devname || strcmp(map->dev_name, devname)))
1293 continue;
1295 if (strcmp(map->supply, supply) == 0)
1296 return map->regulator;
1300 return NULL;
1303 /* Internal regulator request function */
1304 static struct regulator *_regulator_get(struct device *dev, const char *id,
1305 bool exclusive, bool allow_dummy)
1307 struct regulator_dev *rdev;
1308 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1309 const char *devname = NULL;
1310 int ret = -EPROBE_DEFER;
1312 if (id == NULL) {
1313 pr_err("get() with no identifier\n");
1314 return ERR_PTR(-EINVAL);
1317 if (dev)
1318 devname = dev_name(dev);
1320 mutex_lock(&regulator_list_mutex);
1322 rdev = regulator_dev_lookup(dev, id, &ret);
1323 if (rdev)
1324 goto found;
1326 regulator = ERR_PTR(ret);
1329 * If we have return value from dev_lookup fail, we do not expect to
1330 * succeed, so, quit with appropriate error value
1332 if (ret && ret != -ENODEV) {
1333 goto out;
1336 if (!devname)
1337 devname = "deviceless";
1340 * Assume that a regulator is physically present and enabled
1341 * even if it isn't hooked up and just provide a dummy.
1343 if (has_full_constraints && allow_dummy) {
1344 pr_warn("%s supply %s not found, using dummy regulator\n",
1345 devname, id);
1347 rdev = dummy_regulator_rdev;
1348 goto found;
1349 } else {
1350 dev_err(dev, "dummy supplies not allowed\n");
1353 mutex_unlock(&regulator_list_mutex);
1354 return regulator;
1356 found:
1357 if (rdev->exclusive) {
1358 regulator = ERR_PTR(-EPERM);
1359 goto out;
1362 if (exclusive && rdev->open_count) {
1363 regulator = ERR_PTR(-EBUSY);
1364 goto out;
1367 if (!try_module_get(rdev->owner))
1368 goto out;
1370 regulator = create_regulator(rdev, dev, id);
1371 if (regulator == NULL) {
1372 regulator = ERR_PTR(-ENOMEM);
1373 module_put(rdev->owner);
1374 goto out;
1377 rdev->open_count++;
1378 if (exclusive) {
1379 rdev->exclusive = 1;
1381 ret = _regulator_is_enabled(rdev);
1382 if (ret > 0)
1383 rdev->use_count = 1;
1384 else
1385 rdev->use_count = 0;
1388 out:
1389 mutex_unlock(&regulator_list_mutex);
1391 return regulator;
1395 * regulator_get - lookup and obtain a reference to a regulator.
1396 * @dev: device for regulator "consumer"
1397 * @id: Supply name or regulator ID.
1399 * Returns a struct regulator corresponding to the regulator producer,
1400 * or IS_ERR() condition containing errno.
1402 * Use of supply names configured via regulator_set_device_supply() is
1403 * strongly encouraged. It is recommended that the supply name used
1404 * should match the name used for the supply and/or the relevant
1405 * device pins in the datasheet.
1407 struct regulator *regulator_get(struct device *dev, const char *id)
1409 return _regulator_get(dev, id, false, true);
1411 EXPORT_SYMBOL_GPL(regulator_get);
1414 * regulator_get_exclusive - obtain exclusive access to a regulator.
1415 * @dev: device for regulator "consumer"
1416 * @id: Supply name or regulator ID.
1418 * Returns a struct regulator corresponding to the regulator producer,
1419 * or IS_ERR() condition containing errno. Other consumers will be
1420 * unable to obtain this reference is held and the use count for the
1421 * regulator will be initialised to reflect the current state of the
1422 * regulator.
1424 * This is intended for use by consumers which cannot tolerate shared
1425 * use of the regulator such as those which need to force the
1426 * regulator off for correct operation of the hardware they are
1427 * controlling.
1429 * Use of supply names configured via regulator_set_device_supply() is
1430 * strongly encouraged. It is recommended that the supply name used
1431 * should match the name used for the supply and/or the relevant
1432 * device pins in the datasheet.
1434 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1436 return _regulator_get(dev, id, true, false);
1438 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1441 * regulator_get_optional - obtain optional access to a regulator.
1442 * @dev: device for regulator "consumer"
1443 * @id: Supply name or regulator ID.
1445 * Returns a struct regulator corresponding to the regulator producer,
1446 * or IS_ERR() condition containing errno. Other consumers will be
1447 * unable to obtain this reference is held and the use count for the
1448 * regulator will be initialised to reflect the current state of the
1449 * regulator.
1451 * This is intended for use by consumers for devices which can have
1452 * some supplies unconnected in normal use, such as some MMC devices.
1453 * It can allow the regulator core to provide stub supplies for other
1454 * supplies requested using normal regulator_get() calls without
1455 * disrupting the operation of drivers that can handle absent
1456 * supplies.
1458 * Use of supply names configured via regulator_set_device_supply() is
1459 * strongly encouraged. It is recommended that the supply name used
1460 * should match the name used for the supply and/or the relevant
1461 * device pins in the datasheet.
1463 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1465 return _regulator_get(dev, id, false, false);
1467 EXPORT_SYMBOL_GPL(regulator_get_optional);
1469 /* Locks held by regulator_put() */
1470 static void _regulator_put(struct regulator *regulator)
1472 struct regulator_dev *rdev;
1474 if (regulator == NULL || IS_ERR(regulator))
1475 return;
1477 rdev = regulator->rdev;
1479 debugfs_remove_recursive(regulator->debugfs);
1481 /* remove any sysfs entries */
1482 if (regulator->dev)
1483 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1484 kfree(regulator->supply_name);
1485 list_del(&regulator->list);
1486 kfree(regulator);
1488 rdev->open_count--;
1489 rdev->exclusive = 0;
1491 module_put(rdev->owner);
1495 * regulator_put - "free" the regulator source
1496 * @regulator: regulator source
1498 * Note: drivers must ensure that all regulator_enable calls made on this
1499 * regulator source are balanced by regulator_disable calls prior to calling
1500 * this function.
1502 void regulator_put(struct regulator *regulator)
1504 mutex_lock(&regulator_list_mutex);
1505 _regulator_put(regulator);
1506 mutex_unlock(&regulator_list_mutex);
1508 EXPORT_SYMBOL_GPL(regulator_put);
1511 * regulator_register_supply_alias - Provide device alias for supply lookup
1513 * @dev: device that will be given as the regulator "consumer"
1514 * @id: Supply name or regulator ID
1515 * @alias_dev: device that should be used to lookup the supply
1516 * @alias_id: Supply name or regulator ID that should be used to lookup the
1517 * supply
1519 * All lookups for id on dev will instead be conducted for alias_id on
1520 * alias_dev.
1522 int regulator_register_supply_alias(struct device *dev, const char *id,
1523 struct device *alias_dev,
1524 const char *alias_id)
1526 struct regulator_supply_alias *map;
1528 map = regulator_find_supply_alias(dev, id);
1529 if (map)
1530 return -EEXIST;
1532 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1533 if (!map)
1534 return -ENOMEM;
1536 map->src_dev = dev;
1537 map->src_supply = id;
1538 map->alias_dev = alias_dev;
1539 map->alias_supply = alias_id;
1541 list_add(&map->list, &regulator_supply_alias_list);
1543 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1544 id, dev_name(dev), alias_id, dev_name(alias_dev));
1546 return 0;
1548 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1551 * regulator_unregister_supply_alias - Remove device alias
1553 * @dev: device that will be given as the regulator "consumer"
1554 * @id: Supply name or regulator ID
1556 * Remove a lookup alias if one exists for id on dev.
1558 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1560 struct regulator_supply_alias *map;
1562 map = regulator_find_supply_alias(dev, id);
1563 if (map) {
1564 list_del(&map->list);
1565 kfree(map);
1568 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1571 * regulator_bulk_register_supply_alias - register multiple aliases
1573 * @dev: device that will be given as the regulator "consumer"
1574 * @id: List of supply names or regulator IDs
1575 * @alias_dev: device that should be used to lookup the supply
1576 * @alias_id: List of supply names or regulator IDs that should be used to
1577 * lookup the supply
1578 * @num_id: Number of aliases to register
1580 * @return 0 on success, an errno on failure.
1582 * This helper function allows drivers to register several supply
1583 * aliases in one operation. If any of the aliases cannot be
1584 * registered any aliases that were registered will be removed
1585 * before returning to the caller.
1587 int regulator_bulk_register_supply_alias(struct device *dev, const char **id,
1588 struct device *alias_dev,
1589 const char **alias_id,
1590 int num_id)
1592 int i;
1593 int ret;
1595 for (i = 0; i < num_id; ++i) {
1596 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1597 alias_id[i]);
1598 if (ret < 0)
1599 goto err;
1602 return 0;
1604 err:
1605 dev_err(dev,
1606 "Failed to create supply alias %s,%s -> %s,%s\n",
1607 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1609 while (--i >= 0)
1610 regulator_unregister_supply_alias(dev, id[i]);
1612 return ret;
1614 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1617 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1619 * @dev: device that will be given as the regulator "consumer"
1620 * @id: List of supply names or regulator IDs
1621 * @num_id: Number of aliases to unregister
1623 * This helper function allows drivers to unregister several supply
1624 * aliases in one operation.
1626 void regulator_bulk_unregister_supply_alias(struct device *dev,
1627 const char **id,
1628 int num_id)
1630 int i;
1632 for (i = 0; i < num_id; ++i)
1633 regulator_unregister_supply_alias(dev, id[i]);
1635 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1638 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1639 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1640 const struct regulator_config *config)
1642 struct regulator_enable_gpio *pin;
1643 int ret;
1645 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1646 if (pin->gpio == config->ena_gpio) {
1647 rdev_dbg(rdev, "GPIO %d is already used\n",
1648 config->ena_gpio);
1649 goto update_ena_gpio_to_rdev;
1653 ret = gpio_request_one(config->ena_gpio,
1654 GPIOF_DIR_OUT | config->ena_gpio_flags,
1655 rdev_get_name(rdev));
1656 if (ret)
1657 return ret;
1659 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1660 if (pin == NULL) {
1661 gpio_free(config->ena_gpio);
1662 return -ENOMEM;
1665 pin->gpio = config->ena_gpio;
1666 pin->ena_gpio_invert = config->ena_gpio_invert;
1667 list_add(&pin->list, &regulator_ena_gpio_list);
1669 update_ena_gpio_to_rdev:
1670 pin->request_count++;
1671 rdev->ena_pin = pin;
1672 return 0;
1675 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1677 struct regulator_enable_gpio *pin, *n;
1679 if (!rdev->ena_pin)
1680 return;
1682 /* Free the GPIO only in case of no use */
1683 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1684 if (pin->gpio == rdev->ena_pin->gpio) {
1685 if (pin->request_count <= 1) {
1686 pin->request_count = 0;
1687 gpio_free(pin->gpio);
1688 list_del(&pin->list);
1689 kfree(pin);
1690 } else {
1691 pin->request_count--;
1698 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1699 * @rdev: regulator_dev structure
1700 * @enable: enable GPIO at initial use?
1702 * GPIO is enabled in case of initial use. (enable_count is 0)
1703 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1705 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1707 struct regulator_enable_gpio *pin = rdev->ena_pin;
1709 if (!pin)
1710 return -EINVAL;
1712 if (enable) {
1713 /* Enable GPIO at initial use */
1714 if (pin->enable_count == 0)
1715 gpio_set_value_cansleep(pin->gpio,
1716 !pin->ena_gpio_invert);
1718 pin->enable_count++;
1719 } else {
1720 if (pin->enable_count > 1) {
1721 pin->enable_count--;
1722 return 0;
1725 /* Disable GPIO if not used */
1726 if (pin->enable_count <= 1) {
1727 gpio_set_value_cansleep(pin->gpio,
1728 pin->ena_gpio_invert);
1729 pin->enable_count = 0;
1733 return 0;
1736 static int _regulator_do_enable(struct regulator_dev *rdev)
1738 int ret, delay;
1740 /* Query before enabling in case configuration dependent. */
1741 ret = _regulator_get_enable_time(rdev);
1742 if (ret >= 0) {
1743 delay = ret;
1744 } else {
1745 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1746 delay = 0;
1749 trace_regulator_enable(rdev_get_name(rdev));
1751 if (rdev->ena_pin) {
1752 ret = regulator_ena_gpio_ctrl(rdev, true);
1753 if (ret < 0)
1754 return ret;
1755 rdev->ena_gpio_state = 1;
1756 } else if (rdev->desc->ops->enable) {
1757 ret = rdev->desc->ops->enable(rdev);
1758 if (ret < 0)
1759 return ret;
1760 } else {
1761 return -EINVAL;
1764 /* Allow the regulator to ramp; it would be useful to extend
1765 * this for bulk operations so that the regulators can ramp
1766 * together. */
1767 trace_regulator_enable_delay(rdev_get_name(rdev));
1770 * Delay for the requested amount of time as per the guidelines in:
1772 * Documentation/timers/timers-howto.txt
1774 * The assumption here is that regulators will never be enabled in
1775 * atomic context and therefore sleeping functions can be used.
1777 if (delay) {
1778 unsigned int ms = delay / 1000;
1779 unsigned int us = delay % 1000;
1781 if (ms > 0) {
1783 * For small enough values, handle super-millisecond
1784 * delays in the usleep_range() call below.
1786 if (ms < 20)
1787 us += ms * 1000;
1788 else
1789 msleep(ms);
1793 * Give the scheduler some room to coalesce with any other
1794 * wakeup sources. For delays shorter than 10 us, don't even
1795 * bother setting up high-resolution timers and just busy-
1796 * loop.
1798 if (us >= 10)
1799 usleep_range(us, us + 100);
1800 else
1801 udelay(us);
1804 trace_regulator_enable_complete(rdev_get_name(rdev));
1806 return 0;
1809 /* locks held by regulator_enable() */
1810 static int _regulator_enable(struct regulator_dev *rdev)
1812 int ret;
1814 /* check voltage and requested load before enabling */
1815 if (rdev->constraints &&
1816 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1817 drms_uA_update(rdev);
1819 if (rdev->use_count == 0) {
1820 /* The regulator may on if it's not switchable or left on */
1821 ret = _regulator_is_enabled(rdev);
1822 if (ret == -EINVAL || ret == 0) {
1823 if (!_regulator_can_change_status(rdev))
1824 return -EPERM;
1826 ret = _regulator_do_enable(rdev);
1827 if (ret < 0)
1828 return ret;
1830 } else if (ret < 0) {
1831 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1832 return ret;
1834 /* Fallthrough on positive return values - already enabled */
1837 rdev->use_count++;
1839 return 0;
1843 * regulator_enable - enable regulator output
1844 * @regulator: regulator source
1846 * Request that the regulator be enabled with the regulator output at
1847 * the predefined voltage or current value. Calls to regulator_enable()
1848 * must be balanced with calls to regulator_disable().
1850 * NOTE: the output value can be set by other drivers, boot loader or may be
1851 * hardwired in the regulator.
1853 int regulator_enable(struct regulator *regulator)
1855 struct regulator_dev *rdev = regulator->rdev;
1856 int ret = 0;
1858 if (regulator->always_on)
1859 return 0;
1861 if (rdev->supply) {
1862 ret = regulator_enable(rdev->supply);
1863 if (ret != 0)
1864 return ret;
1867 mutex_lock(&rdev->mutex);
1868 ret = _regulator_enable(rdev);
1869 mutex_unlock(&rdev->mutex);
1871 if (ret != 0 && rdev->supply)
1872 regulator_disable(rdev->supply);
1874 return ret;
1876 EXPORT_SYMBOL_GPL(regulator_enable);
1878 static int _regulator_do_disable(struct regulator_dev *rdev)
1880 int ret;
1882 trace_regulator_disable(rdev_get_name(rdev));
1884 if (rdev->ena_pin) {
1885 ret = regulator_ena_gpio_ctrl(rdev, false);
1886 if (ret < 0)
1887 return ret;
1888 rdev->ena_gpio_state = 0;
1890 } else if (rdev->desc->ops->disable) {
1891 ret = rdev->desc->ops->disable(rdev);
1892 if (ret != 0)
1893 return ret;
1896 trace_regulator_disable_complete(rdev_get_name(rdev));
1898 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1899 NULL);
1900 return 0;
1903 /* locks held by regulator_disable() */
1904 static int _regulator_disable(struct regulator_dev *rdev)
1906 int ret = 0;
1908 if (WARN(rdev->use_count <= 0,
1909 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1910 return -EIO;
1912 /* are we the last user and permitted to disable ? */
1913 if (rdev->use_count == 1 &&
1914 (rdev->constraints && !rdev->constraints->always_on)) {
1916 /* we are last user */
1917 if (_regulator_can_change_status(rdev)) {
1918 ret = _regulator_do_disable(rdev);
1919 if (ret < 0) {
1920 rdev_err(rdev, "failed to disable\n");
1921 return ret;
1925 rdev->use_count = 0;
1926 } else if (rdev->use_count > 1) {
1928 if (rdev->constraints &&
1929 (rdev->constraints->valid_ops_mask &
1930 REGULATOR_CHANGE_DRMS))
1931 drms_uA_update(rdev);
1933 rdev->use_count--;
1936 return ret;
1940 * regulator_disable - disable regulator output
1941 * @regulator: regulator source
1943 * Disable the regulator output voltage or current. Calls to
1944 * regulator_enable() must be balanced with calls to
1945 * regulator_disable().
1947 * NOTE: this will only disable the regulator output if no other consumer
1948 * devices have it enabled, the regulator device supports disabling and
1949 * machine constraints permit this operation.
1951 int regulator_disable(struct regulator *regulator)
1953 struct regulator_dev *rdev = regulator->rdev;
1954 int ret = 0;
1956 if (regulator->always_on)
1957 return 0;
1959 mutex_lock(&rdev->mutex);
1960 ret = _regulator_disable(rdev);
1961 mutex_unlock(&rdev->mutex);
1963 if (ret == 0 && rdev->supply)
1964 regulator_disable(rdev->supply);
1966 return ret;
1968 EXPORT_SYMBOL_GPL(regulator_disable);
1970 /* locks held by regulator_force_disable() */
1971 static int _regulator_force_disable(struct regulator_dev *rdev)
1973 int ret = 0;
1975 /* force disable */
1976 if (rdev->desc->ops->disable) {
1977 /* ah well, who wants to live forever... */
1978 ret = rdev->desc->ops->disable(rdev);
1979 if (ret < 0) {
1980 rdev_err(rdev, "failed to force disable\n");
1981 return ret;
1983 /* notify other consumers that power has been forced off */
1984 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1985 REGULATOR_EVENT_DISABLE, NULL);
1988 return ret;
1992 * regulator_force_disable - force disable regulator output
1993 * @regulator: regulator source
1995 * Forcibly disable the regulator output voltage or current.
1996 * NOTE: this *will* disable the regulator output even if other consumer
1997 * devices have it enabled. This should be used for situations when device
1998 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2000 int regulator_force_disable(struct regulator *regulator)
2002 struct regulator_dev *rdev = regulator->rdev;
2003 int ret;
2005 mutex_lock(&rdev->mutex);
2006 regulator->uA_load = 0;
2007 ret = _regulator_force_disable(regulator->rdev);
2008 mutex_unlock(&rdev->mutex);
2010 if (rdev->supply)
2011 while (rdev->open_count--)
2012 regulator_disable(rdev->supply);
2014 return ret;
2016 EXPORT_SYMBOL_GPL(regulator_force_disable);
2018 static void regulator_disable_work(struct work_struct *work)
2020 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2021 disable_work.work);
2022 int count, i, ret;
2024 mutex_lock(&rdev->mutex);
2026 BUG_ON(!rdev->deferred_disables);
2028 count = rdev->deferred_disables;
2029 rdev->deferred_disables = 0;
2031 for (i = 0; i < count; i++) {
2032 ret = _regulator_disable(rdev);
2033 if (ret != 0)
2034 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2037 mutex_unlock(&rdev->mutex);
2039 if (rdev->supply) {
2040 for (i = 0; i < count; i++) {
2041 ret = regulator_disable(rdev->supply);
2042 if (ret != 0) {
2043 rdev_err(rdev,
2044 "Supply disable failed: %d\n", ret);
2051 * regulator_disable_deferred - disable regulator output with delay
2052 * @regulator: regulator source
2053 * @ms: miliseconds until the regulator is disabled
2055 * Execute regulator_disable() on the regulator after a delay. This
2056 * is intended for use with devices that require some time to quiesce.
2058 * NOTE: this will only disable the regulator output if no other consumer
2059 * devices have it enabled, the regulator device supports disabling and
2060 * machine constraints permit this operation.
2062 int regulator_disable_deferred(struct regulator *regulator, int ms)
2064 struct regulator_dev *rdev = regulator->rdev;
2065 int ret;
2067 if (regulator->always_on)
2068 return 0;
2070 if (!ms)
2071 return regulator_disable(regulator);
2073 mutex_lock(&rdev->mutex);
2074 rdev->deferred_disables++;
2075 mutex_unlock(&rdev->mutex);
2077 ret = queue_delayed_work(system_power_efficient_wq,
2078 &rdev->disable_work,
2079 msecs_to_jiffies(ms));
2080 if (ret < 0)
2081 return ret;
2082 else
2083 return 0;
2085 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2087 static int _regulator_is_enabled(struct regulator_dev *rdev)
2089 /* A GPIO control always takes precedence */
2090 if (rdev->ena_pin)
2091 return rdev->ena_gpio_state;
2093 /* If we don't know then assume that the regulator is always on */
2094 if (!rdev->desc->ops->is_enabled)
2095 return 1;
2097 return rdev->desc->ops->is_enabled(rdev);
2101 * regulator_is_enabled - is the regulator output enabled
2102 * @regulator: regulator source
2104 * Returns positive if the regulator driver backing the source/client
2105 * has requested that the device be enabled, zero if it hasn't, else a
2106 * negative errno code.
2108 * Note that the device backing this regulator handle can have multiple
2109 * users, so it might be enabled even if regulator_enable() was never
2110 * called for this particular source.
2112 int regulator_is_enabled(struct regulator *regulator)
2114 int ret;
2116 if (regulator->always_on)
2117 return 1;
2119 mutex_lock(&regulator->rdev->mutex);
2120 ret = _regulator_is_enabled(regulator->rdev);
2121 mutex_unlock(&regulator->rdev->mutex);
2123 return ret;
2125 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2128 * regulator_can_change_voltage - check if regulator can change voltage
2129 * @regulator: regulator source
2131 * Returns positive if the regulator driver backing the source/client
2132 * can change its voltage, false otherwise. Usefull for detecting fixed
2133 * or dummy regulators and disabling voltage change logic in the client
2134 * driver.
2136 int regulator_can_change_voltage(struct regulator *regulator)
2138 struct regulator_dev *rdev = regulator->rdev;
2140 if (rdev->constraints &&
2141 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2142 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2143 return 1;
2145 if (rdev->desc->continuous_voltage_range &&
2146 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2147 rdev->constraints->min_uV != rdev->constraints->max_uV)
2148 return 1;
2151 return 0;
2153 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2156 * regulator_count_voltages - count regulator_list_voltage() selectors
2157 * @regulator: regulator source
2159 * Returns number of selectors, or negative errno. Selectors are
2160 * numbered starting at zero, and typically correspond to bitfields
2161 * in hardware registers.
2163 int regulator_count_voltages(struct regulator *regulator)
2165 struct regulator_dev *rdev = regulator->rdev;
2167 return rdev->desc->n_voltages ? : -EINVAL;
2169 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2172 * regulator_list_voltage - enumerate supported voltages
2173 * @regulator: regulator source
2174 * @selector: identify voltage to list
2175 * Context: can sleep
2177 * Returns a voltage that can be passed to @regulator_set_voltage(),
2178 * zero if this selector code can't be used on this system, or a
2179 * negative errno.
2181 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2183 struct regulator_dev *rdev = regulator->rdev;
2184 struct regulator_ops *ops = rdev->desc->ops;
2185 int ret;
2187 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2188 return -EINVAL;
2190 mutex_lock(&rdev->mutex);
2191 ret = ops->list_voltage(rdev, selector);
2192 mutex_unlock(&rdev->mutex);
2194 if (ret > 0) {
2195 if (ret < rdev->constraints->min_uV)
2196 ret = 0;
2197 else if (ret > rdev->constraints->max_uV)
2198 ret = 0;
2201 return ret;
2203 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2206 * regulator_get_linear_step - return the voltage step size between VSEL values
2207 * @regulator: regulator source
2209 * Returns the voltage step size between VSEL values for linear
2210 * regulators, or return 0 if the regulator isn't a linear regulator.
2212 unsigned int regulator_get_linear_step(struct regulator *regulator)
2214 struct regulator_dev *rdev = regulator->rdev;
2216 return rdev->desc->uV_step;
2218 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2221 * regulator_is_supported_voltage - check if a voltage range can be supported
2223 * @regulator: Regulator to check.
2224 * @min_uV: Minimum required voltage in uV.
2225 * @max_uV: Maximum required voltage in uV.
2227 * Returns a boolean or a negative error code.
2229 int regulator_is_supported_voltage(struct regulator *regulator,
2230 int min_uV, int max_uV)
2232 struct regulator_dev *rdev = regulator->rdev;
2233 int i, voltages, ret;
2235 /* If we can't change voltage check the current voltage */
2236 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2237 ret = regulator_get_voltage(regulator);
2238 if (ret >= 0)
2239 return (min_uV <= ret && ret <= max_uV);
2240 else
2241 return ret;
2244 /* Any voltage within constrains range is fine? */
2245 if (rdev->desc->continuous_voltage_range)
2246 return min_uV >= rdev->constraints->min_uV &&
2247 max_uV <= rdev->constraints->max_uV;
2249 ret = regulator_count_voltages(regulator);
2250 if (ret < 0)
2251 return ret;
2252 voltages = ret;
2254 for (i = 0; i < voltages; i++) {
2255 ret = regulator_list_voltage(regulator, i);
2257 if (ret >= min_uV && ret <= max_uV)
2258 return 1;
2261 return 0;
2263 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2265 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2266 int min_uV, int max_uV)
2268 int ret;
2269 int delay = 0;
2270 int best_val = 0;
2271 unsigned int selector;
2272 int old_selector = -1;
2274 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2276 min_uV += rdev->constraints->uV_offset;
2277 max_uV += rdev->constraints->uV_offset;
2280 * If we can't obtain the old selector there is not enough
2281 * info to call set_voltage_time_sel().
2283 if (_regulator_is_enabled(rdev) &&
2284 rdev->desc->ops->set_voltage_time_sel &&
2285 rdev->desc->ops->get_voltage_sel) {
2286 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2287 if (old_selector < 0)
2288 return old_selector;
2291 if (rdev->desc->ops->set_voltage) {
2292 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2293 &selector);
2295 if (ret >= 0) {
2296 if (rdev->desc->ops->list_voltage)
2297 best_val = rdev->desc->ops->list_voltage(rdev,
2298 selector);
2299 else
2300 best_val = _regulator_get_voltage(rdev);
2303 } else if (rdev->desc->ops->set_voltage_sel) {
2304 if (rdev->desc->ops->map_voltage) {
2305 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2306 max_uV);
2307 } else {
2308 if (rdev->desc->ops->list_voltage ==
2309 regulator_list_voltage_linear)
2310 ret = regulator_map_voltage_linear(rdev,
2311 min_uV, max_uV);
2312 else
2313 ret = regulator_map_voltage_iterate(rdev,
2314 min_uV, max_uV);
2317 if (ret >= 0) {
2318 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2319 if (min_uV <= best_val && max_uV >= best_val) {
2320 selector = ret;
2321 if (old_selector == selector)
2322 ret = 0;
2323 else
2324 ret = rdev->desc->ops->set_voltage_sel(
2325 rdev, ret);
2326 } else {
2327 ret = -EINVAL;
2330 } else {
2331 ret = -EINVAL;
2334 /* Call set_voltage_time_sel if successfully obtained old_selector */
2335 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2336 && old_selector != selector) {
2338 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2339 old_selector, selector);
2340 if (delay < 0) {
2341 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2342 delay);
2343 delay = 0;
2346 /* Insert any necessary delays */
2347 if (delay >= 1000) {
2348 mdelay(delay / 1000);
2349 udelay(delay % 1000);
2350 } else if (delay) {
2351 udelay(delay);
2355 if (ret == 0 && best_val >= 0) {
2356 unsigned long data = best_val;
2358 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2359 (void *)data);
2362 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2364 return ret;
2368 * regulator_set_voltage - set regulator output voltage
2369 * @regulator: regulator source
2370 * @min_uV: Minimum required voltage in uV
2371 * @max_uV: Maximum acceptable voltage in uV
2373 * Sets a voltage regulator to the desired output voltage. This can be set
2374 * during any regulator state. IOW, regulator can be disabled or enabled.
2376 * If the regulator is enabled then the voltage will change to the new value
2377 * immediately otherwise if the regulator is disabled the regulator will
2378 * output at the new voltage when enabled.
2380 * NOTE: If the regulator is shared between several devices then the lowest
2381 * request voltage that meets the system constraints will be used.
2382 * Regulator system constraints must be set for this regulator before
2383 * calling this function otherwise this call will fail.
2385 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2387 struct regulator_dev *rdev = regulator->rdev;
2388 int ret = 0;
2389 int old_min_uV, old_max_uV;
2391 mutex_lock(&rdev->mutex);
2393 /* If we're setting the same range as last time the change
2394 * should be a noop (some cpufreq implementations use the same
2395 * voltage for multiple frequencies, for example).
2397 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2398 goto out;
2400 /* sanity check */
2401 if (!rdev->desc->ops->set_voltage &&
2402 !rdev->desc->ops->set_voltage_sel) {
2403 ret = -EINVAL;
2404 goto out;
2407 /* constraints check */
2408 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2409 if (ret < 0)
2410 goto out;
2412 /* restore original values in case of error */
2413 old_min_uV = regulator->min_uV;
2414 old_max_uV = regulator->max_uV;
2415 regulator->min_uV = min_uV;
2416 regulator->max_uV = max_uV;
2418 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2419 if (ret < 0)
2420 goto out2;
2422 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2423 if (ret < 0)
2424 goto out2;
2426 out:
2427 mutex_unlock(&rdev->mutex);
2428 return ret;
2429 out2:
2430 regulator->min_uV = old_min_uV;
2431 regulator->max_uV = old_max_uV;
2432 mutex_unlock(&rdev->mutex);
2433 return ret;
2435 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2438 * regulator_set_voltage_time - get raise/fall time
2439 * @regulator: regulator source
2440 * @old_uV: starting voltage in microvolts
2441 * @new_uV: target voltage in microvolts
2443 * Provided with the starting and ending voltage, this function attempts to
2444 * calculate the time in microseconds required to rise or fall to this new
2445 * voltage.
2447 int regulator_set_voltage_time(struct regulator *regulator,
2448 int old_uV, int new_uV)
2450 struct regulator_dev *rdev = regulator->rdev;
2451 struct regulator_ops *ops = rdev->desc->ops;
2452 int old_sel = -1;
2453 int new_sel = -1;
2454 int voltage;
2455 int i;
2457 /* Currently requires operations to do this */
2458 if (!ops->list_voltage || !ops->set_voltage_time_sel
2459 || !rdev->desc->n_voltages)
2460 return -EINVAL;
2462 for (i = 0; i < rdev->desc->n_voltages; i++) {
2463 /* We only look for exact voltage matches here */
2464 voltage = regulator_list_voltage(regulator, i);
2465 if (voltage < 0)
2466 return -EINVAL;
2467 if (voltage == 0)
2468 continue;
2469 if (voltage == old_uV)
2470 old_sel = i;
2471 if (voltage == new_uV)
2472 new_sel = i;
2475 if (old_sel < 0 || new_sel < 0)
2476 return -EINVAL;
2478 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2480 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2483 * regulator_set_voltage_time_sel - get raise/fall time
2484 * @rdev: regulator source device
2485 * @old_selector: selector for starting voltage
2486 * @new_selector: selector for target voltage
2488 * Provided with the starting and target voltage selectors, this function
2489 * returns time in microseconds required to rise or fall to this new voltage
2491 * Drivers providing ramp_delay in regulation_constraints can use this as their
2492 * set_voltage_time_sel() operation.
2494 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2495 unsigned int old_selector,
2496 unsigned int new_selector)
2498 unsigned int ramp_delay = 0;
2499 int old_volt, new_volt;
2501 if (rdev->constraints->ramp_delay)
2502 ramp_delay = rdev->constraints->ramp_delay;
2503 else if (rdev->desc->ramp_delay)
2504 ramp_delay = rdev->desc->ramp_delay;
2506 if (ramp_delay == 0) {
2507 rdev_warn(rdev, "ramp_delay not set\n");
2508 return 0;
2511 /* sanity check */
2512 if (!rdev->desc->ops->list_voltage)
2513 return -EINVAL;
2515 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2516 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2518 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2520 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2523 * regulator_sync_voltage - re-apply last regulator output voltage
2524 * @regulator: regulator source
2526 * Re-apply the last configured voltage. This is intended to be used
2527 * where some external control source the consumer is cooperating with
2528 * has caused the configured voltage to change.
2530 int regulator_sync_voltage(struct regulator *regulator)
2532 struct regulator_dev *rdev = regulator->rdev;
2533 int ret, min_uV, max_uV;
2535 mutex_lock(&rdev->mutex);
2537 if (!rdev->desc->ops->set_voltage &&
2538 !rdev->desc->ops->set_voltage_sel) {
2539 ret = -EINVAL;
2540 goto out;
2543 /* This is only going to work if we've had a voltage configured. */
2544 if (!regulator->min_uV && !regulator->max_uV) {
2545 ret = -EINVAL;
2546 goto out;
2549 min_uV = regulator->min_uV;
2550 max_uV = regulator->max_uV;
2552 /* This should be a paranoia check... */
2553 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2554 if (ret < 0)
2555 goto out;
2557 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2558 if (ret < 0)
2559 goto out;
2561 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2563 out:
2564 mutex_unlock(&rdev->mutex);
2565 return ret;
2567 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2569 static int _regulator_get_voltage(struct regulator_dev *rdev)
2571 int sel, ret;
2573 if (rdev->desc->ops->get_voltage_sel) {
2574 sel = rdev->desc->ops->get_voltage_sel(rdev);
2575 if (sel < 0)
2576 return sel;
2577 ret = rdev->desc->ops->list_voltage(rdev, sel);
2578 } else if (rdev->desc->ops->get_voltage) {
2579 ret = rdev->desc->ops->get_voltage(rdev);
2580 } else if (rdev->desc->ops->list_voltage) {
2581 ret = rdev->desc->ops->list_voltage(rdev, 0);
2582 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2583 ret = rdev->desc->fixed_uV;
2584 } else {
2585 return -EINVAL;
2588 if (ret < 0)
2589 return ret;
2590 return ret - rdev->constraints->uV_offset;
2594 * regulator_get_voltage - get regulator output voltage
2595 * @regulator: regulator source
2597 * This returns the current regulator voltage in uV.
2599 * NOTE: If the regulator is disabled it will return the voltage value. This
2600 * function should not be used to determine regulator state.
2602 int regulator_get_voltage(struct regulator *regulator)
2604 int ret;
2606 mutex_lock(&regulator->rdev->mutex);
2608 ret = _regulator_get_voltage(regulator->rdev);
2610 mutex_unlock(&regulator->rdev->mutex);
2612 return ret;
2614 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2617 * regulator_set_current_limit - set regulator output current limit
2618 * @regulator: regulator source
2619 * @min_uA: Minimum supported current in uA
2620 * @max_uA: Maximum supported current in uA
2622 * Sets current sink to the desired output current. This can be set during
2623 * any regulator state. IOW, regulator can be disabled or enabled.
2625 * If the regulator is enabled then the current will change to the new value
2626 * immediately otherwise if the regulator is disabled the regulator will
2627 * output at the new current when enabled.
2629 * NOTE: Regulator system constraints must be set for this regulator before
2630 * calling this function otherwise this call will fail.
2632 int regulator_set_current_limit(struct regulator *regulator,
2633 int min_uA, int max_uA)
2635 struct regulator_dev *rdev = regulator->rdev;
2636 int ret;
2638 mutex_lock(&rdev->mutex);
2640 /* sanity check */
2641 if (!rdev->desc->ops->set_current_limit) {
2642 ret = -EINVAL;
2643 goto out;
2646 /* constraints check */
2647 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2648 if (ret < 0)
2649 goto out;
2651 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2652 out:
2653 mutex_unlock(&rdev->mutex);
2654 return ret;
2656 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2658 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2660 int ret;
2662 mutex_lock(&rdev->mutex);
2664 /* sanity check */
2665 if (!rdev->desc->ops->get_current_limit) {
2666 ret = -EINVAL;
2667 goto out;
2670 ret = rdev->desc->ops->get_current_limit(rdev);
2671 out:
2672 mutex_unlock(&rdev->mutex);
2673 return ret;
2677 * regulator_get_current_limit - get regulator output current
2678 * @regulator: regulator source
2680 * This returns the current supplied by the specified current sink in uA.
2682 * NOTE: If the regulator is disabled it will return the current value. This
2683 * function should not be used to determine regulator state.
2685 int regulator_get_current_limit(struct regulator *regulator)
2687 return _regulator_get_current_limit(regulator->rdev);
2689 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2692 * regulator_set_mode - set regulator operating mode
2693 * @regulator: regulator source
2694 * @mode: operating mode - one of the REGULATOR_MODE constants
2696 * Set regulator operating mode to increase regulator efficiency or improve
2697 * regulation performance.
2699 * NOTE: Regulator system constraints must be set for this regulator before
2700 * calling this function otherwise this call will fail.
2702 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2704 struct regulator_dev *rdev = regulator->rdev;
2705 int ret;
2706 int regulator_curr_mode;
2708 mutex_lock(&rdev->mutex);
2710 /* sanity check */
2711 if (!rdev->desc->ops->set_mode) {
2712 ret = -EINVAL;
2713 goto out;
2716 /* return if the same mode is requested */
2717 if (rdev->desc->ops->get_mode) {
2718 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2719 if (regulator_curr_mode == mode) {
2720 ret = 0;
2721 goto out;
2725 /* constraints check */
2726 ret = regulator_mode_constrain(rdev, &mode);
2727 if (ret < 0)
2728 goto out;
2730 ret = rdev->desc->ops->set_mode(rdev, mode);
2731 out:
2732 mutex_unlock(&rdev->mutex);
2733 return ret;
2735 EXPORT_SYMBOL_GPL(regulator_set_mode);
2737 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2739 int ret;
2741 mutex_lock(&rdev->mutex);
2743 /* sanity check */
2744 if (!rdev->desc->ops->get_mode) {
2745 ret = -EINVAL;
2746 goto out;
2749 ret = rdev->desc->ops->get_mode(rdev);
2750 out:
2751 mutex_unlock(&rdev->mutex);
2752 return ret;
2756 * regulator_get_mode - get regulator operating mode
2757 * @regulator: regulator source
2759 * Get the current regulator operating mode.
2761 unsigned int regulator_get_mode(struct regulator *regulator)
2763 return _regulator_get_mode(regulator->rdev);
2765 EXPORT_SYMBOL_GPL(regulator_get_mode);
2768 * regulator_set_optimum_mode - set regulator optimum operating mode
2769 * @regulator: regulator source
2770 * @uA_load: load current
2772 * Notifies the regulator core of a new device load. This is then used by
2773 * DRMS (if enabled by constraints) to set the most efficient regulator
2774 * operating mode for the new regulator loading.
2776 * Consumer devices notify their supply regulator of the maximum power
2777 * they will require (can be taken from device datasheet in the power
2778 * consumption tables) when they change operational status and hence power
2779 * state. Examples of operational state changes that can affect power
2780 * consumption are :-
2782 * o Device is opened / closed.
2783 * o Device I/O is about to begin or has just finished.
2784 * o Device is idling in between work.
2786 * This information is also exported via sysfs to userspace.
2788 * DRMS will sum the total requested load on the regulator and change
2789 * to the most efficient operating mode if platform constraints allow.
2791 * Returns the new regulator mode or error.
2793 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2795 struct regulator_dev *rdev = regulator->rdev;
2796 struct regulator *consumer;
2797 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2798 unsigned int mode;
2800 if (rdev->supply)
2801 input_uV = regulator_get_voltage(rdev->supply);
2803 mutex_lock(&rdev->mutex);
2806 * first check to see if we can set modes at all, otherwise just
2807 * tell the consumer everything is OK.
2809 regulator->uA_load = uA_load;
2810 ret = regulator_check_drms(rdev);
2811 if (ret < 0) {
2812 ret = 0;
2813 goto out;
2816 if (!rdev->desc->ops->get_optimum_mode)
2817 goto out;
2820 * we can actually do this so any errors are indicators of
2821 * potential real failure.
2823 ret = -EINVAL;
2825 if (!rdev->desc->ops->set_mode)
2826 goto out;
2828 /* get output voltage */
2829 output_uV = _regulator_get_voltage(rdev);
2830 if (output_uV <= 0) {
2831 rdev_err(rdev, "invalid output voltage found\n");
2832 goto out;
2835 /* No supply? Use constraint voltage */
2836 if (input_uV <= 0)
2837 input_uV = rdev->constraints->input_uV;
2838 if (input_uV <= 0) {
2839 rdev_err(rdev, "invalid input voltage found\n");
2840 goto out;
2843 /* calc total requested load for this regulator */
2844 list_for_each_entry(consumer, &rdev->consumer_list, list)
2845 total_uA_load += consumer->uA_load;
2847 mode = rdev->desc->ops->get_optimum_mode(rdev,
2848 input_uV, output_uV,
2849 total_uA_load);
2850 ret = regulator_mode_constrain(rdev, &mode);
2851 if (ret < 0) {
2852 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2853 total_uA_load, input_uV, output_uV);
2854 goto out;
2857 ret = rdev->desc->ops->set_mode(rdev, mode);
2858 if (ret < 0) {
2859 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2860 goto out;
2862 ret = mode;
2863 out:
2864 mutex_unlock(&rdev->mutex);
2865 return ret;
2867 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2870 * regulator_allow_bypass - allow the regulator to go into bypass mode
2872 * @regulator: Regulator to configure
2873 * @enable: enable or disable bypass mode
2875 * Allow the regulator to go into bypass mode if all other consumers
2876 * for the regulator also enable bypass mode and the machine
2877 * constraints allow this. Bypass mode means that the regulator is
2878 * simply passing the input directly to the output with no regulation.
2880 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2882 struct regulator_dev *rdev = regulator->rdev;
2883 int ret = 0;
2885 if (!rdev->desc->ops->set_bypass)
2886 return 0;
2888 if (rdev->constraints &&
2889 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2890 return 0;
2892 mutex_lock(&rdev->mutex);
2894 if (enable && !regulator->bypass) {
2895 rdev->bypass_count++;
2897 if (rdev->bypass_count == rdev->open_count) {
2898 ret = rdev->desc->ops->set_bypass(rdev, enable);
2899 if (ret != 0)
2900 rdev->bypass_count--;
2903 } else if (!enable && regulator->bypass) {
2904 rdev->bypass_count--;
2906 if (rdev->bypass_count != rdev->open_count) {
2907 ret = rdev->desc->ops->set_bypass(rdev, enable);
2908 if (ret != 0)
2909 rdev->bypass_count++;
2913 if (ret == 0)
2914 regulator->bypass = enable;
2916 mutex_unlock(&rdev->mutex);
2918 return ret;
2920 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2923 * regulator_register_notifier - register regulator event notifier
2924 * @regulator: regulator source
2925 * @nb: notifier block
2927 * Register notifier block to receive regulator events.
2929 int regulator_register_notifier(struct regulator *regulator,
2930 struct notifier_block *nb)
2932 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2933 nb);
2935 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2938 * regulator_unregister_notifier - unregister regulator event notifier
2939 * @regulator: regulator source
2940 * @nb: notifier block
2942 * Unregister regulator event notifier block.
2944 int regulator_unregister_notifier(struct regulator *regulator,
2945 struct notifier_block *nb)
2947 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2948 nb);
2950 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2952 /* notify regulator consumers and downstream regulator consumers.
2953 * Note mutex must be held by caller.
2955 static void _notifier_call_chain(struct regulator_dev *rdev,
2956 unsigned long event, void *data)
2958 /* call rdev chain first */
2959 blocking_notifier_call_chain(&rdev->notifier, event, data);
2963 * regulator_bulk_get - get multiple regulator consumers
2965 * @dev: Device to supply
2966 * @num_consumers: Number of consumers to register
2967 * @consumers: Configuration of consumers; clients are stored here.
2969 * @return 0 on success, an errno on failure.
2971 * This helper function allows drivers to get several regulator
2972 * consumers in one operation. If any of the regulators cannot be
2973 * acquired then any regulators that were allocated will be freed
2974 * before returning to the caller.
2976 int regulator_bulk_get(struct device *dev, int num_consumers,
2977 struct regulator_bulk_data *consumers)
2979 int i;
2980 int ret;
2982 for (i = 0; i < num_consumers; i++)
2983 consumers[i].consumer = NULL;
2985 for (i = 0; i < num_consumers; i++) {
2986 consumers[i].consumer = regulator_get(dev,
2987 consumers[i].supply);
2988 if (IS_ERR(consumers[i].consumer)) {
2989 ret = PTR_ERR(consumers[i].consumer);
2990 dev_err(dev, "Failed to get supply '%s': %d\n",
2991 consumers[i].supply, ret);
2992 consumers[i].consumer = NULL;
2993 goto err;
2997 return 0;
2999 err:
3000 while (--i >= 0)
3001 regulator_put(consumers[i].consumer);
3003 return ret;
3005 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3007 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3009 struct regulator_bulk_data *bulk = data;
3011 bulk->ret = regulator_enable(bulk->consumer);
3015 * regulator_bulk_enable - enable multiple regulator consumers
3017 * @num_consumers: Number of consumers
3018 * @consumers: Consumer data; clients are stored here.
3019 * @return 0 on success, an errno on failure
3021 * This convenience API allows consumers to enable multiple regulator
3022 * clients in a single API call. If any consumers cannot be enabled
3023 * then any others that were enabled will be disabled again prior to
3024 * return.
3026 int regulator_bulk_enable(int num_consumers,
3027 struct regulator_bulk_data *consumers)
3029 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3030 int i;
3031 int ret = 0;
3033 for (i = 0; i < num_consumers; i++) {
3034 if (consumers[i].consumer->always_on)
3035 consumers[i].ret = 0;
3036 else
3037 async_schedule_domain(regulator_bulk_enable_async,
3038 &consumers[i], &async_domain);
3041 async_synchronize_full_domain(&async_domain);
3043 /* If any consumer failed we need to unwind any that succeeded */
3044 for (i = 0; i < num_consumers; i++) {
3045 if (consumers[i].ret != 0) {
3046 ret = consumers[i].ret;
3047 goto err;
3051 return 0;
3053 err:
3054 for (i = 0; i < num_consumers; i++) {
3055 if (consumers[i].ret < 0)
3056 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3057 consumers[i].ret);
3058 else
3059 regulator_disable(consumers[i].consumer);
3062 return ret;
3064 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3067 * regulator_bulk_disable - disable multiple regulator consumers
3069 * @num_consumers: Number of consumers
3070 * @consumers: Consumer data; clients are stored here.
3071 * @return 0 on success, an errno on failure
3073 * This convenience API allows consumers to disable multiple regulator
3074 * clients in a single API call. If any consumers cannot be disabled
3075 * then any others that were disabled will be enabled again prior to
3076 * return.
3078 int regulator_bulk_disable(int num_consumers,
3079 struct regulator_bulk_data *consumers)
3081 int i;
3082 int ret, r;
3084 for (i = num_consumers - 1; i >= 0; --i) {
3085 ret = regulator_disable(consumers[i].consumer);
3086 if (ret != 0)
3087 goto err;
3090 return 0;
3092 err:
3093 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3094 for (++i; i < num_consumers; ++i) {
3095 r = regulator_enable(consumers[i].consumer);
3096 if (r != 0)
3097 pr_err("Failed to reename %s: %d\n",
3098 consumers[i].supply, r);
3101 return ret;
3103 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3106 * regulator_bulk_force_disable - force disable multiple regulator consumers
3108 * @num_consumers: Number of consumers
3109 * @consumers: Consumer data; clients are stored here.
3110 * @return 0 on success, an errno on failure
3112 * This convenience API allows consumers to forcibly disable multiple regulator
3113 * clients in a single API call.
3114 * NOTE: This should be used for situations when device damage will
3115 * likely occur if the regulators are not disabled (e.g. over temp).
3116 * Although regulator_force_disable function call for some consumers can
3117 * return error numbers, the function is called for all consumers.
3119 int regulator_bulk_force_disable(int num_consumers,
3120 struct regulator_bulk_data *consumers)
3122 int i;
3123 int ret;
3125 for (i = 0; i < num_consumers; i++)
3126 consumers[i].ret =
3127 regulator_force_disable(consumers[i].consumer);
3129 for (i = 0; i < num_consumers; i++) {
3130 if (consumers[i].ret != 0) {
3131 ret = consumers[i].ret;
3132 goto out;
3136 return 0;
3137 out:
3138 return ret;
3140 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3143 * regulator_bulk_free - free multiple regulator consumers
3145 * @num_consumers: Number of consumers
3146 * @consumers: Consumer data; clients are stored here.
3148 * This convenience API allows consumers to free multiple regulator
3149 * clients in a single API call.
3151 void regulator_bulk_free(int num_consumers,
3152 struct regulator_bulk_data *consumers)
3154 int i;
3156 for (i = 0; i < num_consumers; i++) {
3157 regulator_put(consumers[i].consumer);
3158 consumers[i].consumer = NULL;
3161 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3164 * regulator_notifier_call_chain - call regulator event notifier
3165 * @rdev: regulator source
3166 * @event: notifier block
3167 * @data: callback-specific data.
3169 * Called by regulator drivers to notify clients a regulator event has
3170 * occurred. We also notify regulator clients downstream.
3171 * Note lock must be held by caller.
3173 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3174 unsigned long event, void *data)
3176 _notifier_call_chain(rdev, event, data);
3177 return NOTIFY_DONE;
3180 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3183 * regulator_mode_to_status - convert a regulator mode into a status
3185 * @mode: Mode to convert
3187 * Convert a regulator mode into a status.
3189 int regulator_mode_to_status(unsigned int mode)
3191 switch (mode) {
3192 case REGULATOR_MODE_FAST:
3193 return REGULATOR_STATUS_FAST;
3194 case REGULATOR_MODE_NORMAL:
3195 return REGULATOR_STATUS_NORMAL;
3196 case REGULATOR_MODE_IDLE:
3197 return REGULATOR_STATUS_IDLE;
3198 case REGULATOR_MODE_STANDBY:
3199 return REGULATOR_STATUS_STANDBY;
3200 default:
3201 return REGULATOR_STATUS_UNDEFINED;
3204 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3207 * To avoid cluttering sysfs (and memory) with useless state, only
3208 * create attributes that can be meaningfully displayed.
3210 static int add_regulator_attributes(struct regulator_dev *rdev)
3212 struct device *dev = &rdev->dev;
3213 struct regulator_ops *ops = rdev->desc->ops;
3214 int status = 0;
3216 /* some attributes need specific methods to be displayed */
3217 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3218 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3219 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3220 (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1))) {
3221 status = device_create_file(dev, &dev_attr_microvolts);
3222 if (status < 0)
3223 return status;
3225 if (ops->get_current_limit) {
3226 status = device_create_file(dev, &dev_attr_microamps);
3227 if (status < 0)
3228 return status;
3230 if (ops->get_mode) {
3231 status = device_create_file(dev, &dev_attr_opmode);
3232 if (status < 0)
3233 return status;
3235 if (rdev->ena_pin || ops->is_enabled) {
3236 status = device_create_file(dev, &dev_attr_state);
3237 if (status < 0)
3238 return status;
3240 if (ops->get_status) {
3241 status = device_create_file(dev, &dev_attr_status);
3242 if (status < 0)
3243 return status;
3245 if (ops->get_bypass) {
3246 status = device_create_file(dev, &dev_attr_bypass);
3247 if (status < 0)
3248 return status;
3251 /* some attributes are type-specific */
3252 if (rdev->desc->type == REGULATOR_CURRENT) {
3253 status = device_create_file(dev, &dev_attr_requested_microamps);
3254 if (status < 0)
3255 return status;
3258 /* all the other attributes exist to support constraints;
3259 * don't show them if there are no constraints, or if the
3260 * relevant supporting methods are missing.
3262 if (!rdev->constraints)
3263 return status;
3265 /* constraints need specific supporting methods */
3266 if (ops->set_voltage || ops->set_voltage_sel) {
3267 status = device_create_file(dev, &dev_attr_min_microvolts);
3268 if (status < 0)
3269 return status;
3270 status = device_create_file(dev, &dev_attr_max_microvolts);
3271 if (status < 0)
3272 return status;
3274 if (ops->set_current_limit) {
3275 status = device_create_file(dev, &dev_attr_min_microamps);
3276 if (status < 0)
3277 return status;
3278 status = device_create_file(dev, &dev_attr_max_microamps);
3279 if (status < 0)
3280 return status;
3283 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3284 if (status < 0)
3285 return status;
3286 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3287 if (status < 0)
3288 return status;
3289 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3290 if (status < 0)
3291 return status;
3293 if (ops->set_suspend_voltage) {
3294 status = device_create_file(dev,
3295 &dev_attr_suspend_standby_microvolts);
3296 if (status < 0)
3297 return status;
3298 status = device_create_file(dev,
3299 &dev_attr_suspend_mem_microvolts);
3300 if (status < 0)
3301 return status;
3302 status = device_create_file(dev,
3303 &dev_attr_suspend_disk_microvolts);
3304 if (status < 0)
3305 return status;
3308 if (ops->set_suspend_mode) {
3309 status = device_create_file(dev,
3310 &dev_attr_suspend_standby_mode);
3311 if (status < 0)
3312 return status;
3313 status = device_create_file(dev,
3314 &dev_attr_suspend_mem_mode);
3315 if (status < 0)
3316 return status;
3317 status = device_create_file(dev,
3318 &dev_attr_suspend_disk_mode);
3319 if (status < 0)
3320 return status;
3323 return status;
3326 static void rdev_init_debugfs(struct regulator_dev *rdev)
3328 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3329 if (!rdev->debugfs) {
3330 rdev_warn(rdev, "Failed to create debugfs directory\n");
3331 return;
3334 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3335 &rdev->use_count);
3336 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3337 &rdev->open_count);
3338 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3339 &rdev->bypass_count);
3343 * regulator_register - register regulator
3344 * @regulator_desc: regulator to register
3345 * @config: runtime configuration for regulator
3347 * Called by regulator drivers to register a regulator.
3348 * Returns a valid pointer to struct regulator_dev on success
3349 * or an ERR_PTR() on error.
3351 struct regulator_dev *
3352 regulator_register(const struct regulator_desc *regulator_desc,
3353 const struct regulator_config *config)
3355 const struct regulation_constraints *constraints = NULL;
3356 const struct regulator_init_data *init_data;
3357 static atomic_t regulator_no = ATOMIC_INIT(0);
3358 struct regulator_dev *rdev;
3359 struct device *dev;
3360 int ret, i;
3361 const char *supply = NULL;
3363 if (regulator_desc == NULL || config == NULL)
3364 return ERR_PTR(-EINVAL);
3366 dev = config->dev;
3367 WARN_ON(!dev);
3369 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3370 return ERR_PTR(-EINVAL);
3372 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3373 regulator_desc->type != REGULATOR_CURRENT)
3374 return ERR_PTR(-EINVAL);
3376 /* Only one of each should be implemented */
3377 WARN_ON(regulator_desc->ops->get_voltage &&
3378 regulator_desc->ops->get_voltage_sel);
3379 WARN_ON(regulator_desc->ops->set_voltage &&
3380 regulator_desc->ops->set_voltage_sel);
3382 /* If we're using selectors we must implement list_voltage. */
3383 if (regulator_desc->ops->get_voltage_sel &&
3384 !regulator_desc->ops->list_voltage) {
3385 return ERR_PTR(-EINVAL);
3387 if (regulator_desc->ops->set_voltage_sel &&
3388 !regulator_desc->ops->list_voltage) {
3389 return ERR_PTR(-EINVAL);
3392 init_data = config->init_data;
3394 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3395 if (rdev == NULL)
3396 return ERR_PTR(-ENOMEM);
3398 mutex_lock(&regulator_list_mutex);
3400 mutex_init(&rdev->mutex);
3401 rdev->reg_data = config->driver_data;
3402 rdev->owner = regulator_desc->owner;
3403 rdev->desc = regulator_desc;
3404 if (config->regmap)
3405 rdev->regmap = config->regmap;
3406 else if (dev_get_regmap(dev, NULL))
3407 rdev->regmap = dev_get_regmap(dev, NULL);
3408 else if (dev->parent)
3409 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3410 INIT_LIST_HEAD(&rdev->consumer_list);
3411 INIT_LIST_HEAD(&rdev->list);
3412 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3413 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3415 /* preform any regulator specific init */
3416 if (init_data && init_data->regulator_init) {
3417 ret = init_data->regulator_init(rdev->reg_data);
3418 if (ret < 0)
3419 goto clean;
3422 /* register with sysfs */
3423 rdev->dev.class = &regulator_class;
3424 rdev->dev.of_node = config->of_node;
3425 rdev->dev.parent = dev;
3426 dev_set_name(&rdev->dev, "regulator.%d",
3427 atomic_inc_return(&regulator_no) - 1);
3428 ret = device_register(&rdev->dev);
3429 if (ret != 0) {
3430 put_device(&rdev->dev);
3431 goto clean;
3434 dev_set_drvdata(&rdev->dev, rdev);
3436 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3437 ret = regulator_ena_gpio_request(rdev, config);
3438 if (ret != 0) {
3439 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3440 config->ena_gpio, ret);
3441 goto wash;
3444 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3445 rdev->ena_gpio_state = 1;
3447 if (config->ena_gpio_invert)
3448 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3451 /* set regulator constraints */
3452 if (init_data)
3453 constraints = &init_data->constraints;
3455 ret = set_machine_constraints(rdev, constraints);
3456 if (ret < 0)
3457 goto scrub;
3459 /* add attributes supported by this regulator */
3460 ret = add_regulator_attributes(rdev);
3461 if (ret < 0)
3462 goto scrub;
3464 if (init_data && init_data->supply_regulator)
3465 supply = init_data->supply_regulator;
3466 else if (regulator_desc->supply_name)
3467 supply = regulator_desc->supply_name;
3469 if (supply) {
3470 struct regulator_dev *r;
3472 r = regulator_dev_lookup(dev, supply, &ret);
3474 if (ret == -ENODEV) {
3476 * No supply was specified for this regulator and
3477 * there will never be one.
3479 ret = 0;
3480 goto add_dev;
3481 } else if (!r) {
3482 dev_err(dev, "Failed to find supply %s\n", supply);
3483 ret = -EPROBE_DEFER;
3484 goto scrub;
3487 ret = set_supply(rdev, r);
3488 if (ret < 0)
3489 goto scrub;
3491 /* Enable supply if rail is enabled */
3492 if (_regulator_is_enabled(rdev)) {
3493 ret = regulator_enable(rdev->supply);
3494 if (ret < 0)
3495 goto scrub;
3499 add_dev:
3500 /* add consumers devices */
3501 if (init_data) {
3502 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3503 ret = set_consumer_device_supply(rdev,
3504 init_data->consumer_supplies[i].dev_name,
3505 init_data->consumer_supplies[i].supply);
3506 if (ret < 0) {
3507 dev_err(dev, "Failed to set supply %s\n",
3508 init_data->consumer_supplies[i].supply);
3509 goto unset_supplies;
3514 list_add(&rdev->list, &regulator_list);
3516 rdev_init_debugfs(rdev);
3517 out:
3518 mutex_unlock(&regulator_list_mutex);
3519 return rdev;
3521 unset_supplies:
3522 unset_regulator_supplies(rdev);
3524 scrub:
3525 if (rdev->supply)
3526 _regulator_put(rdev->supply);
3527 regulator_ena_gpio_free(rdev);
3528 kfree(rdev->constraints);
3529 wash:
3530 device_unregister(&rdev->dev);
3531 /* device core frees rdev */
3532 rdev = ERR_PTR(ret);
3533 goto out;
3535 clean:
3536 kfree(rdev);
3537 rdev = ERR_PTR(ret);
3538 goto out;
3540 EXPORT_SYMBOL_GPL(regulator_register);
3543 * regulator_unregister - unregister regulator
3544 * @rdev: regulator to unregister
3546 * Called by regulator drivers to unregister a regulator.
3548 void regulator_unregister(struct regulator_dev *rdev)
3550 if (rdev == NULL)
3551 return;
3553 if (rdev->supply) {
3554 while (rdev->use_count--)
3555 regulator_disable(rdev->supply);
3556 regulator_put(rdev->supply);
3558 mutex_lock(&regulator_list_mutex);
3559 debugfs_remove_recursive(rdev->debugfs);
3560 flush_work(&rdev->disable_work.work);
3561 WARN_ON(rdev->open_count);
3562 unset_regulator_supplies(rdev);
3563 list_del(&rdev->list);
3564 kfree(rdev->constraints);
3565 regulator_ena_gpio_free(rdev);
3566 device_unregister(&rdev->dev);
3567 mutex_unlock(&regulator_list_mutex);
3569 EXPORT_SYMBOL_GPL(regulator_unregister);
3572 * regulator_suspend_prepare - prepare regulators for system wide suspend
3573 * @state: system suspend state
3575 * Configure each regulator with it's suspend operating parameters for state.
3576 * This will usually be called by machine suspend code prior to supending.
3578 int regulator_suspend_prepare(suspend_state_t state)
3580 struct regulator_dev *rdev;
3581 int ret = 0;
3583 /* ON is handled by regulator active state */
3584 if (state == PM_SUSPEND_ON)
3585 return -EINVAL;
3587 mutex_lock(&regulator_list_mutex);
3588 list_for_each_entry(rdev, &regulator_list, list) {
3590 mutex_lock(&rdev->mutex);
3591 ret = suspend_prepare(rdev, state);
3592 mutex_unlock(&rdev->mutex);
3594 if (ret < 0) {
3595 rdev_err(rdev, "failed to prepare\n");
3596 goto out;
3599 out:
3600 mutex_unlock(&regulator_list_mutex);
3601 return ret;
3603 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3606 * regulator_suspend_finish - resume regulators from system wide suspend
3608 * Turn on regulators that might be turned off by regulator_suspend_prepare
3609 * and that should be turned on according to the regulators properties.
3611 int regulator_suspend_finish(void)
3613 struct regulator_dev *rdev;
3614 int ret = 0, error;
3616 mutex_lock(&regulator_list_mutex);
3617 list_for_each_entry(rdev, &regulator_list, list) {
3618 struct regulator_ops *ops = rdev->desc->ops;
3620 mutex_lock(&rdev->mutex);
3621 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3622 ops->enable) {
3623 error = ops->enable(rdev);
3624 if (error)
3625 ret = error;
3626 } else {
3627 if (!has_full_constraints)
3628 goto unlock;
3629 if (!ops->disable)
3630 goto unlock;
3631 if (!_regulator_is_enabled(rdev))
3632 goto unlock;
3634 error = ops->disable(rdev);
3635 if (error)
3636 ret = error;
3638 unlock:
3639 mutex_unlock(&rdev->mutex);
3641 mutex_unlock(&regulator_list_mutex);
3642 return ret;
3644 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3647 * regulator_has_full_constraints - the system has fully specified constraints
3649 * Calling this function will cause the regulator API to disable all
3650 * regulators which have a zero use count and don't have an always_on
3651 * constraint in a late_initcall.
3653 * The intention is that this will become the default behaviour in a
3654 * future kernel release so users are encouraged to use this facility
3655 * now.
3657 void regulator_has_full_constraints(void)
3659 has_full_constraints = 1;
3661 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3664 * rdev_get_drvdata - get rdev regulator driver data
3665 * @rdev: regulator
3667 * Get rdev regulator driver private data. This call can be used in the
3668 * regulator driver context.
3670 void *rdev_get_drvdata(struct regulator_dev *rdev)
3672 return rdev->reg_data;
3674 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3677 * regulator_get_drvdata - get regulator driver data
3678 * @regulator: regulator
3680 * Get regulator driver private data. This call can be used in the consumer
3681 * driver context when non API regulator specific functions need to be called.
3683 void *regulator_get_drvdata(struct regulator *regulator)
3685 return regulator->rdev->reg_data;
3687 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3690 * regulator_set_drvdata - set regulator driver data
3691 * @regulator: regulator
3692 * @data: data
3694 void regulator_set_drvdata(struct regulator *regulator, void *data)
3696 regulator->rdev->reg_data = data;
3698 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3701 * regulator_get_id - get regulator ID
3702 * @rdev: regulator
3704 int rdev_get_id(struct regulator_dev *rdev)
3706 return rdev->desc->id;
3708 EXPORT_SYMBOL_GPL(rdev_get_id);
3710 struct device *rdev_get_dev(struct regulator_dev *rdev)
3712 return &rdev->dev;
3714 EXPORT_SYMBOL_GPL(rdev_get_dev);
3716 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3718 return reg_init_data->driver_data;
3720 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3722 #ifdef CONFIG_DEBUG_FS
3723 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3724 size_t count, loff_t *ppos)
3726 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3727 ssize_t len, ret = 0;
3728 struct regulator_map *map;
3730 if (!buf)
3731 return -ENOMEM;
3733 list_for_each_entry(map, &regulator_map_list, list) {
3734 len = snprintf(buf + ret, PAGE_SIZE - ret,
3735 "%s -> %s.%s\n",
3736 rdev_get_name(map->regulator), map->dev_name,
3737 map->supply);
3738 if (len >= 0)
3739 ret += len;
3740 if (ret > PAGE_SIZE) {
3741 ret = PAGE_SIZE;
3742 break;
3746 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3748 kfree(buf);
3750 return ret;
3752 #endif
3754 static const struct file_operations supply_map_fops = {
3755 #ifdef CONFIG_DEBUG_FS
3756 .read = supply_map_read_file,
3757 .llseek = default_llseek,
3758 #endif
3761 static int __init regulator_init(void)
3763 int ret;
3765 ret = class_register(&regulator_class);
3767 debugfs_root = debugfs_create_dir("regulator", NULL);
3768 if (!debugfs_root)
3769 pr_warn("regulator: Failed to create debugfs directory\n");
3771 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3772 &supply_map_fops);
3774 regulator_dummy_init();
3776 return ret;
3779 /* init early to allow our consumers to complete system booting */
3780 core_initcall(regulator_init);
3782 static int __init regulator_init_complete(void)
3784 struct regulator_dev *rdev;
3785 struct regulator_ops *ops;
3786 struct regulation_constraints *c;
3787 int enabled, ret;
3790 * Since DT doesn't provide an idiomatic mechanism for
3791 * enabling full constraints and since it's much more natural
3792 * with DT to provide them just assume that a DT enabled
3793 * system has full constraints.
3795 if (of_have_populated_dt())
3796 has_full_constraints = true;
3798 mutex_lock(&regulator_list_mutex);
3800 /* If we have a full configuration then disable any regulators
3801 * which are not in use or always_on. This will become the
3802 * default behaviour in the future.
3804 list_for_each_entry(rdev, &regulator_list, list) {
3805 ops = rdev->desc->ops;
3806 c = rdev->constraints;
3808 if (!ops->disable || (c && c->always_on))
3809 continue;
3811 mutex_lock(&rdev->mutex);
3813 if (rdev->use_count)
3814 goto unlock;
3816 /* If we can't read the status assume it's on. */
3817 if (ops->is_enabled)
3818 enabled = ops->is_enabled(rdev);
3819 else
3820 enabled = 1;
3822 if (!enabled)
3823 goto unlock;
3825 if (has_full_constraints) {
3826 /* We log since this may kill the system if it
3827 * goes wrong. */
3828 rdev_info(rdev, "disabling\n");
3829 ret = ops->disable(rdev);
3830 if (ret != 0) {
3831 rdev_err(rdev, "couldn't disable: %d\n", ret);
3833 } else {
3834 /* The intention is that in future we will
3835 * assume that full constraints are provided
3836 * so warn even if we aren't going to do
3837 * anything here.
3839 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3842 unlock:
3843 mutex_unlock(&rdev->mutex);
3846 mutex_unlock(&regulator_list_mutex);
3848 return 0;
3850 late_initcall(regulator_init_complete);