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>
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>
40 #define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51 static DEFINE_MUTEX(regulator_list_mutex);
52 static LIST_HEAD(regulator_list);
53 static LIST_HEAD(regulator_map_list);
54 static LIST_HEAD(regulator_ena_gpio_list);
55 static bool has_full_constraints;
56 static bool board_wants_dummy_regulator;
58 static struct dentry *debugfs_root;
61 * struct regulator_map
63 * Used to provide symbolic supply names to devices.
65 struct regulator_map {
66 struct list_head list;
67 const char *dev_name; /* The dev_name() for the consumer */
69 struct regulator_dev *regulator;
73 * struct regulator_enable_gpio
75 * Management for shared enable GPIO pin
77 struct regulator_enable_gpio {
78 struct list_head list;
80 u32 enable_count; /* a number of enabled shared GPIO */
81 u32 request_count; /* a number of requested shared GPIO */
82 unsigned int ena_gpio_invert:1;
88 * One for each consumer device.
92 struct list_head list;
93 unsigned int always_on:1;
94 unsigned int bypass:1;
99 struct device_attribute dev_attr;
100 struct regulator_dev *rdev;
101 struct dentry *debugfs;
104 static int _regulator_is_enabled(struct regulator_dev *rdev);
105 static int _regulator_disable(struct regulator_dev *rdev);
106 static int _regulator_get_voltage(struct regulator_dev *rdev);
107 static int _regulator_get_current_limit(struct regulator_dev *rdev);
108 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
109 static void _notifier_call_chain(struct regulator_dev *rdev,
110 unsigned long event, void *data);
111 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
112 int min_uV, int max_uV);
113 static struct regulator *create_regulator(struct regulator_dev *rdev,
115 const char *supply_name);
117 static const char *rdev_get_name(struct regulator_dev *rdev)
119 if (rdev->constraints && rdev->constraints->name)
120 return rdev->constraints->name;
121 else if (rdev->desc->name)
122 return rdev->desc->name;
128 * of_get_regulator - get a regulator device node based on supply name
129 * @dev: Device pointer for the consumer (of regulator) device
130 * @supply: regulator supply name
132 * Extract the regulator device node corresponding to the supply name.
133 * returns the device node corresponding to the regulator if found, else
136 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
138 struct device_node *regnode = NULL;
139 char prop_name[32]; /* 32 is max size of property name */
141 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
143 snprintf(prop_name, 32, "%s-supply", supply);
144 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
147 dev_dbg(dev, "Looking up %s property in node %s failed",
148 prop_name, dev->of_node->full_name);
154 static int _regulator_can_change_status(struct regulator_dev *rdev)
156 if (!rdev->constraints)
159 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
165 /* Platform voltage constraint check */
166 static int regulator_check_voltage(struct regulator_dev *rdev,
167 int *min_uV, int *max_uV)
169 BUG_ON(*min_uV > *max_uV);
171 if (!rdev->constraints) {
172 rdev_err(rdev, "no constraints\n");
175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
176 rdev_err(rdev, "operation not allowed\n");
180 if (*max_uV > rdev->constraints->max_uV)
181 *max_uV = rdev->constraints->max_uV;
182 if (*min_uV < rdev->constraints->min_uV)
183 *min_uV = rdev->constraints->min_uV;
185 if (*min_uV > *max_uV) {
186 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
194 /* Make sure we select a voltage that suits the needs of all
195 * regulator consumers
197 static int regulator_check_consumers(struct regulator_dev *rdev,
198 int *min_uV, int *max_uV)
200 struct regulator *regulator;
202 list_for_each_entry(regulator, &rdev->consumer_list, list) {
204 * Assume consumers that didn't say anything are OK
205 * with anything in the constraint range.
207 if (!regulator->min_uV && !regulator->max_uV)
210 if (*max_uV > regulator->max_uV)
211 *max_uV = regulator->max_uV;
212 if (*min_uV < regulator->min_uV)
213 *min_uV = regulator->min_uV;
216 if (*min_uV > *max_uV) {
217 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
225 /* current constraint check */
226 static int regulator_check_current_limit(struct regulator_dev *rdev,
227 int *min_uA, int *max_uA)
229 BUG_ON(*min_uA > *max_uA);
231 if (!rdev->constraints) {
232 rdev_err(rdev, "no constraints\n");
235 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
236 rdev_err(rdev, "operation not allowed\n");
240 if (*max_uA > rdev->constraints->max_uA)
241 *max_uA = rdev->constraints->max_uA;
242 if (*min_uA < rdev->constraints->min_uA)
243 *min_uA = rdev->constraints->min_uA;
245 if (*min_uA > *max_uA) {
246 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
254 /* operating mode constraint check */
255 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
258 case REGULATOR_MODE_FAST:
259 case REGULATOR_MODE_NORMAL:
260 case REGULATOR_MODE_IDLE:
261 case REGULATOR_MODE_STANDBY:
264 rdev_err(rdev, "invalid mode %x specified\n", *mode);
268 if (!rdev->constraints) {
269 rdev_err(rdev, "no constraints\n");
272 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
273 rdev_err(rdev, "operation not allowed\n");
277 /* The modes are bitmasks, the most power hungry modes having
278 * the lowest values. If the requested mode isn't supported
279 * try higher modes. */
281 if (rdev->constraints->valid_modes_mask & *mode)
289 /* dynamic regulator mode switching constraint check */
290 static int regulator_check_drms(struct regulator_dev *rdev)
292 if (!rdev->constraints) {
293 rdev_err(rdev, "no constraints\n");
296 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
297 rdev_err(rdev, "operation not allowed\n");
303 static ssize_t regulator_uV_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct regulator_dev *rdev = dev_get_drvdata(dev);
309 mutex_lock(&rdev->mutex);
310 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
311 mutex_unlock(&rdev->mutex);
315 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
317 static ssize_t regulator_uA_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 struct regulator_dev *rdev = dev_get_drvdata(dev);
322 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
324 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
326 static ssize_t regulator_name_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
329 struct regulator_dev *rdev = dev_get_drvdata(dev);
331 return sprintf(buf, "%s\n", rdev_get_name(rdev));
334 static ssize_t regulator_print_opmode(char *buf, int mode)
337 case REGULATOR_MODE_FAST:
338 return sprintf(buf, "fast\n");
339 case REGULATOR_MODE_NORMAL:
340 return sprintf(buf, "normal\n");
341 case REGULATOR_MODE_IDLE:
342 return sprintf(buf, "idle\n");
343 case REGULATOR_MODE_STANDBY:
344 return sprintf(buf, "standby\n");
346 return sprintf(buf, "unknown\n");
349 static ssize_t regulator_opmode_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
352 struct regulator_dev *rdev = dev_get_drvdata(dev);
354 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
356 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
358 static ssize_t regulator_print_state(char *buf, int state)
361 return sprintf(buf, "enabled\n");
363 return sprintf(buf, "disabled\n");
365 return sprintf(buf, "unknown\n");
368 static ssize_t regulator_state_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
374 mutex_lock(&rdev->mutex);
375 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
376 mutex_unlock(&rdev->mutex);
380 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
382 static ssize_t regulator_status_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
389 status = rdev->desc->ops->get_status(rdev);
394 case REGULATOR_STATUS_OFF:
397 case REGULATOR_STATUS_ON:
400 case REGULATOR_STATUS_ERROR:
403 case REGULATOR_STATUS_FAST:
406 case REGULATOR_STATUS_NORMAL:
409 case REGULATOR_STATUS_IDLE:
412 case REGULATOR_STATUS_STANDBY:
415 case REGULATOR_STATUS_BYPASS:
418 case REGULATOR_STATUS_UNDEFINED:
425 return sprintf(buf, "%s\n", label);
427 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
429 static ssize_t regulator_min_uA_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
432 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 if (!rdev->constraints)
435 return sprintf(buf, "constraint not defined\n");
437 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
439 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
441 static ssize_t regulator_max_uA_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
444 struct regulator_dev *rdev = dev_get_drvdata(dev);
446 if (!rdev->constraints)
447 return sprintf(buf, "constraint not defined\n");
449 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
451 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
453 static ssize_t regulator_min_uV_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
456 struct regulator_dev *rdev = dev_get_drvdata(dev);
458 if (!rdev->constraints)
459 return sprintf(buf, "constraint not defined\n");
461 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
463 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
465 static ssize_t regulator_max_uV_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct regulator_dev *rdev = dev_get_drvdata(dev);
470 if (!rdev->constraints)
471 return sprintf(buf, "constraint not defined\n");
473 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
475 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
477 static ssize_t regulator_total_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
481 struct regulator *regulator;
484 mutex_lock(&rdev->mutex);
485 list_for_each_entry(regulator, &rdev->consumer_list, list)
486 uA += regulator->uA_load;
487 mutex_unlock(&rdev->mutex);
488 return sprintf(buf, "%d\n", uA);
490 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
492 static ssize_t regulator_num_users_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
495 struct regulator_dev *rdev = dev_get_drvdata(dev);
496 return sprintf(buf, "%d\n", rdev->use_count);
499 static ssize_t regulator_type_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct regulator_dev *rdev = dev_get_drvdata(dev);
504 switch (rdev->desc->type) {
505 case REGULATOR_VOLTAGE:
506 return sprintf(buf, "voltage\n");
507 case REGULATOR_CURRENT:
508 return sprintf(buf, "current\n");
510 return sprintf(buf, "unknown\n");
513 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct regulator_dev *rdev = dev_get_drvdata(dev);
518 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
520 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
521 regulator_suspend_mem_uV_show, NULL);
523 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 struct regulator_dev *rdev = dev_get_drvdata(dev);
528 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
530 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
531 regulator_suspend_disk_uV_show, NULL);
533 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 struct regulator_dev *rdev = dev_get_drvdata(dev);
538 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
540 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
541 regulator_suspend_standby_uV_show, NULL);
543 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct regulator_dev *rdev = dev_get_drvdata(dev);
548 return regulator_print_opmode(buf,
549 rdev->constraints->state_mem.mode);
551 static DEVICE_ATTR(suspend_mem_mode, 0444,
552 regulator_suspend_mem_mode_show, NULL);
554 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
557 struct regulator_dev *rdev = dev_get_drvdata(dev);
559 return regulator_print_opmode(buf,
560 rdev->constraints->state_disk.mode);
562 static DEVICE_ATTR(suspend_disk_mode, 0444,
563 regulator_suspend_disk_mode_show, NULL);
565 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
568 struct regulator_dev *rdev = dev_get_drvdata(dev);
570 return regulator_print_opmode(buf,
571 rdev->constraints->state_standby.mode);
573 static DEVICE_ATTR(suspend_standby_mode, 0444,
574 regulator_suspend_standby_mode_show, NULL);
576 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
579 struct regulator_dev *rdev = dev_get_drvdata(dev);
581 return regulator_print_state(buf,
582 rdev->constraints->state_mem.enabled);
584 static DEVICE_ATTR(suspend_mem_state, 0444,
585 regulator_suspend_mem_state_show, NULL);
587 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct regulator_dev *rdev = dev_get_drvdata(dev);
592 return regulator_print_state(buf,
593 rdev->constraints->state_disk.enabled);
595 static DEVICE_ATTR(suspend_disk_state, 0444,
596 regulator_suspend_disk_state_show, NULL);
598 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
601 struct regulator_dev *rdev = dev_get_drvdata(dev);
603 return regulator_print_state(buf,
604 rdev->constraints->state_standby.enabled);
606 static DEVICE_ATTR(suspend_standby_state, 0444,
607 regulator_suspend_standby_state_show, NULL);
609 static ssize_t regulator_bypass_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
612 struct regulator_dev *rdev = dev_get_drvdata(dev);
617 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
626 return sprintf(buf, "%s\n", report);
628 static DEVICE_ATTR(bypass, 0444,
629 regulator_bypass_show, NULL);
632 * These are the only attributes are present for all regulators.
633 * Other attributes are a function of regulator functionality.
635 static struct device_attribute regulator_dev_attrs[] = {
636 __ATTR(name, 0444, regulator_name_show, NULL),
637 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
638 __ATTR(type, 0444, regulator_type_show, NULL),
642 static void regulator_dev_release(struct device *dev)
644 struct regulator_dev *rdev = dev_get_drvdata(dev);
648 static struct class regulator_class = {
650 .dev_release = regulator_dev_release,
651 .dev_attrs = regulator_dev_attrs,
654 /* Calculate the new optimum regulator operating mode based on the new total
655 * consumer load. All locks held by caller */
656 static void drms_uA_update(struct regulator_dev *rdev)
658 struct regulator *sibling;
659 int current_uA = 0, output_uV, input_uV, err;
662 err = regulator_check_drms(rdev);
663 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
664 (!rdev->desc->ops->get_voltage &&
665 !rdev->desc->ops->get_voltage_sel) ||
666 !rdev->desc->ops->set_mode)
669 /* get output voltage */
670 output_uV = _regulator_get_voltage(rdev);
674 /* get input voltage */
677 input_uV = regulator_get_voltage(rdev->supply);
679 input_uV = rdev->constraints->input_uV;
683 /* calc total requested load */
684 list_for_each_entry(sibling, &rdev->consumer_list, list)
685 current_uA += sibling->uA_load;
687 /* now get the optimum mode for our new total regulator load */
688 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 output_uV, current_uA);
691 /* check the new mode is allowed */
692 err = regulator_mode_constrain(rdev, &mode);
694 rdev->desc->ops->set_mode(rdev, mode);
697 static int suspend_set_state(struct regulator_dev *rdev,
698 struct regulator_state *rstate)
702 /* If we have no suspend mode configration don't set anything;
703 * only warn if the driver implements set_suspend_voltage or
704 * set_suspend_mode callback.
706 if (!rstate->enabled && !rstate->disabled) {
707 if (rdev->desc->ops->set_suspend_voltage ||
708 rdev->desc->ops->set_suspend_mode)
709 rdev_warn(rdev, "No configuration\n");
713 if (rstate->enabled && rstate->disabled) {
714 rdev_err(rdev, "invalid configuration\n");
718 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
719 ret = rdev->desc->ops->set_suspend_enable(rdev);
720 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
721 ret = rdev->desc->ops->set_suspend_disable(rdev);
722 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
726 rdev_err(rdev, "failed to enabled/disable\n");
730 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
733 rdev_err(rdev, "failed to set voltage\n");
738 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
741 rdev_err(rdev, "failed to set mode\n");
748 /* locks held by caller */
749 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
751 if (!rdev->constraints)
755 case PM_SUSPEND_STANDBY:
756 return suspend_set_state(rdev,
757 &rdev->constraints->state_standby);
759 return suspend_set_state(rdev,
760 &rdev->constraints->state_mem);
762 return suspend_set_state(rdev,
763 &rdev->constraints->state_disk);
769 static void print_constraints(struct regulator_dev *rdev)
771 struct regulation_constraints *constraints = rdev->constraints;
776 if (constraints->min_uV && constraints->max_uV) {
777 if (constraints->min_uV == constraints->max_uV)
778 count += sprintf(buf + count, "%d mV ",
779 constraints->min_uV / 1000);
781 count += sprintf(buf + count, "%d <--> %d mV ",
782 constraints->min_uV / 1000,
783 constraints->max_uV / 1000);
786 if (!constraints->min_uV ||
787 constraints->min_uV != constraints->max_uV) {
788 ret = _regulator_get_voltage(rdev);
790 count += sprintf(buf + count, "at %d mV ", ret / 1000);
793 if (constraints->uV_offset)
794 count += sprintf(buf, "%dmV offset ",
795 constraints->uV_offset / 1000);
797 if (constraints->min_uA && constraints->max_uA) {
798 if (constraints->min_uA == constraints->max_uA)
799 count += sprintf(buf + count, "%d mA ",
800 constraints->min_uA / 1000);
802 count += sprintf(buf + count, "%d <--> %d mA ",
803 constraints->min_uA / 1000,
804 constraints->max_uA / 1000);
807 if (!constraints->min_uA ||
808 constraints->min_uA != constraints->max_uA) {
809 ret = _regulator_get_current_limit(rdev);
811 count += sprintf(buf + count, "at %d mA ", ret / 1000);
814 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 count += sprintf(buf + count, "fast ");
816 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 count += sprintf(buf + count, "normal ");
818 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 count += sprintf(buf + count, "idle ");
820 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 count += sprintf(buf + count, "standby");
824 sprintf(buf, "no parameters");
826 rdev_info(rdev, "%s\n", buf);
828 if ((constraints->min_uV != constraints->max_uV) &&
829 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
831 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
834 static int machine_constraints_voltage(struct regulator_dev *rdev,
835 struct regulation_constraints *constraints)
837 struct regulator_ops *ops = rdev->desc->ops;
840 /* do we need to apply the constraint voltage */
841 if (rdev->constraints->apply_uV &&
842 rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 ret = _regulator_do_set_voltage(rdev,
844 rdev->constraints->min_uV,
845 rdev->constraints->max_uV);
847 rdev_err(rdev, "failed to apply %duV constraint\n",
848 rdev->constraints->min_uV);
853 /* constrain machine-level voltage specs to fit
854 * the actual range supported by this regulator.
856 if (ops->list_voltage && rdev->desc->n_voltages) {
857 int count = rdev->desc->n_voltages;
859 int min_uV = INT_MAX;
860 int max_uV = INT_MIN;
861 int cmin = constraints->min_uV;
862 int cmax = constraints->max_uV;
864 /* it's safe to autoconfigure fixed-voltage supplies
865 and the constraints are used by list_voltage. */
866 if (count == 1 && !cmin) {
869 constraints->min_uV = cmin;
870 constraints->max_uV = cmax;
873 /* voltage constraints are optional */
874 if ((cmin == 0) && (cmax == 0))
877 /* else require explicit machine-level constraints */
878 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
879 rdev_err(rdev, "invalid voltage constraints\n");
883 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 for (i = 0; i < count; i++) {
887 value = ops->list_voltage(rdev, i);
891 /* maybe adjust [min_uV..max_uV] */
892 if (value >= cmin && value < min_uV)
894 if (value <= cmax && value > max_uV)
898 /* final: [min_uV..max_uV] valid iff constraints valid */
899 if (max_uV < min_uV) {
901 "unsupportable voltage constraints %u-%uuV\n",
906 /* use regulator's subset of machine constraints */
907 if (constraints->min_uV < min_uV) {
908 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 constraints->min_uV, min_uV);
910 constraints->min_uV = min_uV;
912 if (constraints->max_uV > max_uV) {
913 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 constraints->max_uV, max_uV);
915 constraints->max_uV = max_uV;
922 static int _regulator_do_enable(struct regulator_dev *rdev);
925 * set_machine_constraints - sets regulator constraints
926 * @rdev: regulator source
927 * @constraints: constraints to apply
929 * Allows platform initialisation code to define and constrain
930 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
931 * Constraints *must* be set by platform code in order for some
932 * regulator operations to proceed i.e. set_voltage, set_current_limit,
935 static int set_machine_constraints(struct regulator_dev *rdev,
936 const struct regulation_constraints *constraints)
939 struct regulator_ops *ops = rdev->desc->ops;
942 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
945 rdev->constraints = kzalloc(sizeof(*constraints),
947 if (!rdev->constraints)
950 ret = machine_constraints_voltage(rdev, rdev->constraints);
954 /* do we need to setup our suspend state */
955 if (rdev->constraints->initial_state) {
956 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
958 rdev_err(rdev, "failed to set suspend state\n");
963 if (rdev->constraints->initial_mode) {
964 if (!ops->set_mode) {
965 rdev_err(rdev, "no set_mode operation\n");
970 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
972 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
977 /* If the constraints say the regulator should be on at this point
978 * and we have control then make sure it is enabled.
980 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
981 ret = _regulator_do_enable(rdev);
982 if (ret < 0 && ret != -EINVAL) {
983 rdev_err(rdev, "failed to enable\n");
988 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
989 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
991 rdev_err(rdev, "failed to set ramp_delay\n");
996 print_constraints(rdev);
999 kfree(rdev->constraints);
1000 rdev->constraints = NULL;
1005 * set_supply - set regulator supply regulator
1006 * @rdev: regulator name
1007 * @supply_rdev: supply regulator name
1009 * Called by platform initialisation code to set the supply regulator for this
1010 * regulator. This ensures that a regulators supply will also be enabled by the
1011 * core if it's child is enabled.
1013 static int set_supply(struct regulator_dev *rdev,
1014 struct regulator_dev *supply_rdev)
1018 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1020 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1021 if (rdev->supply == NULL) {
1025 supply_rdev->open_count++;
1031 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1032 * @rdev: regulator source
1033 * @consumer_dev_name: dev_name() string for device supply applies to
1034 * @supply: symbolic name for supply
1036 * Allows platform initialisation code to map physical regulator
1037 * sources to symbolic names for supplies for use by devices. Devices
1038 * should use these symbolic names to request regulators, avoiding the
1039 * need to provide board-specific regulator names as platform data.
1041 static int set_consumer_device_supply(struct regulator_dev *rdev,
1042 const char *consumer_dev_name,
1045 struct regulator_map *node;
1051 if (consumer_dev_name != NULL)
1056 list_for_each_entry(node, ®ulator_map_list, list) {
1057 if (node->dev_name && consumer_dev_name) {
1058 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1060 } else if (node->dev_name || consumer_dev_name) {
1064 if (strcmp(node->supply, supply) != 0)
1067 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1069 dev_name(&node->regulator->dev),
1070 node->regulator->desc->name,
1072 dev_name(&rdev->dev), rdev_get_name(rdev));
1076 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1080 node->regulator = rdev;
1081 node->supply = supply;
1084 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1085 if (node->dev_name == NULL) {
1091 list_add(&node->list, ®ulator_map_list);
1095 static void unset_regulator_supplies(struct regulator_dev *rdev)
1097 struct regulator_map *node, *n;
1099 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1100 if (rdev == node->regulator) {
1101 list_del(&node->list);
1102 kfree(node->dev_name);
1108 #define REG_STR_SIZE 64
1110 static struct regulator *create_regulator(struct regulator_dev *rdev,
1112 const char *supply_name)
1114 struct regulator *regulator;
1115 char buf[REG_STR_SIZE];
1118 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1119 if (regulator == NULL)
1122 mutex_lock(&rdev->mutex);
1123 regulator->rdev = rdev;
1124 list_add(®ulator->list, &rdev->consumer_list);
1127 regulator->dev = dev;
1129 /* Add a link to the device sysfs entry */
1130 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1131 dev->kobj.name, supply_name);
1132 if (size >= REG_STR_SIZE)
1135 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1136 if (regulator->supply_name == NULL)
1139 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1142 rdev_warn(rdev, "could not add device link %s err %d\n",
1143 dev->kobj.name, err);
1147 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1148 if (regulator->supply_name == NULL)
1152 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1154 if (!regulator->debugfs) {
1155 rdev_warn(rdev, "Failed to create debugfs directory\n");
1157 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1158 ®ulator->uA_load);
1159 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1160 ®ulator->min_uV);
1161 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1162 ®ulator->max_uV);
1166 * Check now if the regulator is an always on regulator - if
1167 * it is then we don't need to do nearly so much work for
1168 * enable/disable calls.
1170 if (!_regulator_can_change_status(rdev) &&
1171 _regulator_is_enabled(rdev))
1172 regulator->always_on = true;
1174 mutex_unlock(&rdev->mutex);
1177 list_del(®ulator->list);
1179 mutex_unlock(&rdev->mutex);
1183 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1185 if (!rdev->desc->ops->enable_time)
1186 return rdev->desc->enable_time;
1187 return rdev->desc->ops->enable_time(rdev);
1190 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1194 struct regulator_dev *r;
1195 struct device_node *node;
1196 struct regulator_map *map;
1197 const char *devname = NULL;
1199 /* first do a dt based lookup */
1200 if (dev && dev->of_node) {
1201 node = of_get_regulator(dev, supply);
1203 list_for_each_entry(r, ®ulator_list, list)
1204 if (r->dev.parent &&
1205 node == r->dev.of_node)
1209 * If we couldn't even get the node then it's
1210 * not just that the device didn't register
1211 * yet, there's no node and we'll never
1218 /* if not found, try doing it non-dt way */
1220 devname = dev_name(dev);
1222 list_for_each_entry(r, ®ulator_list, list)
1223 if (strcmp(rdev_get_name(r), supply) == 0)
1226 list_for_each_entry(map, ®ulator_map_list, list) {
1227 /* If the mapping has a device set up it must match */
1228 if (map->dev_name &&
1229 (!devname || strcmp(map->dev_name, devname)))
1232 if (strcmp(map->supply, supply) == 0)
1233 return map->regulator;
1240 /* Internal regulator request function */
1241 static struct regulator *_regulator_get(struct device *dev, const char *id,
1244 struct regulator_dev *rdev;
1245 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1246 const char *devname = NULL;
1250 pr_err("get() with no identifier\n");
1255 devname = dev_name(dev);
1257 mutex_lock(®ulator_list_mutex);
1259 rdev = regulator_dev_lookup(dev, id, &ret);
1264 * If we have return value from dev_lookup fail, we do not expect to
1265 * succeed, so, quit with appropriate error value
1268 regulator = ERR_PTR(ret);
1272 if (board_wants_dummy_regulator) {
1273 rdev = dummy_regulator_rdev;
1277 #ifdef CONFIG_REGULATOR_DUMMY
1279 devname = "deviceless";
1281 /* If the board didn't flag that it was fully constrained then
1282 * substitute in a dummy regulator so consumers can continue.
1284 if (!has_full_constraints) {
1285 pr_warn("%s supply %s not found, using dummy regulator\n",
1287 rdev = dummy_regulator_rdev;
1292 mutex_unlock(®ulator_list_mutex);
1296 if (rdev->exclusive) {
1297 regulator = ERR_PTR(-EPERM);
1301 if (exclusive && rdev->open_count) {
1302 regulator = ERR_PTR(-EBUSY);
1306 if (!try_module_get(rdev->owner))
1309 regulator = create_regulator(rdev, dev, id);
1310 if (regulator == NULL) {
1311 regulator = ERR_PTR(-ENOMEM);
1312 module_put(rdev->owner);
1318 rdev->exclusive = 1;
1320 ret = _regulator_is_enabled(rdev);
1322 rdev->use_count = 1;
1324 rdev->use_count = 0;
1328 mutex_unlock(®ulator_list_mutex);
1334 * regulator_get - lookup and obtain a reference to a regulator.
1335 * @dev: device for regulator "consumer"
1336 * @id: Supply name or regulator ID.
1338 * Returns a struct regulator corresponding to the regulator producer,
1339 * or IS_ERR() condition containing errno.
1341 * Use of supply names configured via regulator_set_device_supply() is
1342 * strongly encouraged. It is recommended that the supply name used
1343 * should match the name used for the supply and/or the relevant
1344 * device pins in the datasheet.
1346 struct regulator *regulator_get(struct device *dev, const char *id)
1348 return _regulator_get(dev, id, 0);
1350 EXPORT_SYMBOL_GPL(regulator_get);
1352 static void devm_regulator_release(struct device *dev, void *res)
1354 regulator_put(*(struct regulator **)res);
1358 * devm_regulator_get - Resource managed regulator_get()
1359 * @dev: device for regulator "consumer"
1360 * @id: Supply name or regulator ID.
1362 * Managed regulator_get(). Regulators returned from this function are
1363 * automatically regulator_put() on driver detach. See regulator_get() for more
1366 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1368 struct regulator **ptr, *regulator;
1370 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1372 return ERR_PTR(-ENOMEM);
1374 regulator = regulator_get(dev, id);
1375 if (!IS_ERR(regulator)) {
1377 devres_add(dev, ptr);
1384 EXPORT_SYMBOL_GPL(devm_regulator_get);
1387 * regulator_get_exclusive - obtain exclusive access to a regulator.
1388 * @dev: device for regulator "consumer"
1389 * @id: Supply name or regulator ID.
1391 * Returns a struct regulator corresponding to the regulator producer,
1392 * or IS_ERR() condition containing errno. Other consumers will be
1393 * unable to obtain this reference is held and the use count for the
1394 * regulator will be initialised to reflect the current state of the
1397 * This is intended for use by consumers which cannot tolerate shared
1398 * use of the regulator such as those which need to force the
1399 * regulator off for correct operation of the hardware they are
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_exclusive(struct device *dev, const char *id)
1409 return _regulator_get(dev, id, 1);
1411 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1413 /* regulator_list_mutex lock held by regulator_put() */
1414 static void _regulator_put(struct regulator *regulator)
1416 struct regulator_dev *rdev;
1418 if (regulator == NULL || IS_ERR(regulator))
1421 rdev = regulator->rdev;
1423 debugfs_remove_recursive(regulator->debugfs);
1425 /* remove any sysfs entries */
1427 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1428 mutex_lock(&rdev->mutex);
1429 kfree(regulator->supply_name);
1430 list_del(®ulator->list);
1434 rdev->exclusive = 0;
1435 mutex_unlock(&rdev->mutex);
1437 module_put(rdev->owner);
1441 * regulator_put - "free" the regulator source
1442 * @regulator: regulator source
1444 * Note: drivers must ensure that all regulator_enable calls made on this
1445 * regulator source are balanced by regulator_disable calls prior to calling
1448 void regulator_put(struct regulator *regulator)
1450 mutex_lock(®ulator_list_mutex);
1451 _regulator_put(regulator);
1452 mutex_unlock(®ulator_list_mutex);
1454 EXPORT_SYMBOL_GPL(regulator_put);
1456 static int devm_regulator_match(struct device *dev, void *res, void *data)
1458 struct regulator **r = res;
1467 * devm_regulator_put - Resource managed regulator_put()
1468 * @regulator: regulator to free
1470 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1471 * this function will not need to be called and the resource management
1472 * code will ensure that the resource is freed.
1474 void devm_regulator_put(struct regulator *regulator)
1478 rc = devres_release(regulator->dev, devm_regulator_release,
1479 devm_regulator_match, regulator);
1483 EXPORT_SYMBOL_GPL(devm_regulator_put);
1485 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1486 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1487 const struct regulator_config *config)
1489 struct regulator_enable_gpio *pin;
1492 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1493 if (pin->gpio == config->ena_gpio) {
1494 rdev_dbg(rdev, "GPIO %d is already used\n",
1496 goto update_ena_gpio_to_rdev;
1500 ret = gpio_request_one(config->ena_gpio,
1501 GPIOF_DIR_OUT | config->ena_gpio_flags,
1502 rdev_get_name(rdev));
1506 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1508 gpio_free(config->ena_gpio);
1512 pin->gpio = config->ena_gpio;
1513 pin->ena_gpio_invert = config->ena_gpio_invert;
1514 list_add(&pin->list, ®ulator_ena_gpio_list);
1516 update_ena_gpio_to_rdev:
1517 pin->request_count++;
1518 rdev->ena_pin = pin;
1522 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1524 struct regulator_enable_gpio *pin, *n;
1529 /* Free the GPIO only in case of no use */
1530 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1531 if (pin->gpio == rdev->ena_pin->gpio) {
1532 if (pin->request_count <= 1) {
1533 pin->request_count = 0;
1534 gpio_free(pin->gpio);
1535 list_del(&pin->list);
1538 pin->request_count--;
1545 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1546 * @rdev: regulator_dev structure
1547 * @enable: enable GPIO at initial use?
1549 * GPIO is enabled in case of initial use. (enable_count is 0)
1550 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1552 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1554 struct regulator_enable_gpio *pin = rdev->ena_pin;
1560 /* Enable GPIO at initial use */
1561 if (pin->enable_count == 0)
1562 gpio_set_value_cansleep(pin->gpio,
1563 !pin->ena_gpio_invert);
1565 pin->enable_count++;
1567 if (pin->enable_count > 1) {
1568 pin->enable_count--;
1572 /* Disable GPIO if not used */
1573 if (pin->enable_count <= 1) {
1574 gpio_set_value_cansleep(pin->gpio,
1575 pin->ena_gpio_invert);
1576 pin->enable_count = 0;
1583 static int _regulator_do_enable(struct regulator_dev *rdev)
1587 /* Query before enabling in case configuration dependent. */
1588 ret = _regulator_get_enable_time(rdev);
1592 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1596 trace_regulator_enable(rdev_get_name(rdev));
1598 if (rdev->ena_pin) {
1599 ret = regulator_ena_gpio_ctrl(rdev, true);
1602 rdev->ena_gpio_state = 1;
1603 } else if (rdev->desc->ops->enable) {
1604 ret = rdev->desc->ops->enable(rdev);
1611 /* Allow the regulator to ramp; it would be useful to extend
1612 * this for bulk operations so that the regulators can ramp
1614 trace_regulator_enable_delay(rdev_get_name(rdev));
1616 if (delay >= 1000) {
1617 mdelay(delay / 1000);
1618 udelay(delay % 1000);
1623 trace_regulator_enable_complete(rdev_get_name(rdev));
1628 /* locks held by regulator_enable() */
1629 static int _regulator_enable(struct regulator_dev *rdev)
1633 /* check voltage and requested load before enabling */
1634 if (rdev->constraints &&
1635 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1636 drms_uA_update(rdev);
1638 if (rdev->use_count == 0) {
1639 /* The regulator may on if it's not switchable or left on */
1640 ret = _regulator_is_enabled(rdev);
1641 if (ret == -EINVAL || ret == 0) {
1642 if (!_regulator_can_change_status(rdev))
1645 ret = _regulator_do_enable(rdev);
1649 } else if (ret < 0) {
1650 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1653 /* Fallthrough on positive return values - already enabled */
1662 * regulator_enable - enable regulator output
1663 * @regulator: regulator source
1665 * Request that the regulator be enabled with the regulator output at
1666 * the predefined voltage or current value. Calls to regulator_enable()
1667 * must be balanced with calls to regulator_disable().
1669 * NOTE: the output value can be set by other drivers, boot loader or may be
1670 * hardwired in the regulator.
1672 int regulator_enable(struct regulator *regulator)
1674 struct regulator_dev *rdev = regulator->rdev;
1677 if (regulator->always_on)
1681 ret = regulator_enable(rdev->supply);
1686 mutex_lock(&rdev->mutex);
1687 ret = _regulator_enable(rdev);
1688 mutex_unlock(&rdev->mutex);
1690 if (ret != 0 && rdev->supply)
1691 regulator_disable(rdev->supply);
1695 EXPORT_SYMBOL_GPL(regulator_enable);
1697 static int _regulator_do_disable(struct regulator_dev *rdev)
1701 trace_regulator_disable(rdev_get_name(rdev));
1703 if (rdev->ena_pin) {
1704 ret = regulator_ena_gpio_ctrl(rdev, false);
1707 rdev->ena_gpio_state = 0;
1709 } else if (rdev->desc->ops->disable) {
1710 ret = rdev->desc->ops->disable(rdev);
1715 trace_regulator_disable_complete(rdev_get_name(rdev));
1720 /* locks held by regulator_disable() */
1721 static int _regulator_disable(struct regulator_dev *rdev)
1725 if (WARN(rdev->use_count <= 0,
1726 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1729 /* are we the last user and permitted to disable ? */
1730 if (rdev->use_count == 1 &&
1731 (rdev->constraints && !rdev->constraints->always_on)) {
1733 /* we are last user */
1734 if (_regulator_can_change_status(rdev)) {
1735 ret = _regulator_do_disable(rdev);
1737 rdev_err(rdev, "failed to disable\n");
1740 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1744 rdev->use_count = 0;
1745 } else if (rdev->use_count > 1) {
1747 if (rdev->constraints &&
1748 (rdev->constraints->valid_ops_mask &
1749 REGULATOR_CHANGE_DRMS))
1750 drms_uA_update(rdev);
1759 * regulator_disable - disable regulator output
1760 * @regulator: regulator source
1762 * Disable the regulator output voltage or current. Calls to
1763 * regulator_enable() must be balanced with calls to
1764 * regulator_disable().
1766 * NOTE: this will only disable the regulator output if no other consumer
1767 * devices have it enabled, the regulator device supports disabling and
1768 * machine constraints permit this operation.
1770 int regulator_disable(struct regulator *regulator)
1772 struct regulator_dev *rdev = regulator->rdev;
1775 if (regulator->always_on)
1778 mutex_lock(&rdev->mutex);
1779 ret = _regulator_disable(rdev);
1780 mutex_unlock(&rdev->mutex);
1782 if (ret == 0 && rdev->supply)
1783 regulator_disable(rdev->supply);
1787 EXPORT_SYMBOL_GPL(regulator_disable);
1789 /* locks held by regulator_force_disable() */
1790 static int _regulator_force_disable(struct regulator_dev *rdev)
1794 ret = _regulator_do_disable(rdev);
1796 rdev_err(rdev, "failed to force disable\n");
1800 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1801 REGULATOR_EVENT_DISABLE, NULL);
1807 * regulator_force_disable - force disable regulator output
1808 * @regulator: regulator source
1810 * Forcibly disable the regulator output voltage or current.
1811 * NOTE: this *will* disable the regulator output even if other consumer
1812 * devices have it enabled. This should be used for situations when device
1813 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1815 int regulator_force_disable(struct regulator *regulator)
1817 struct regulator_dev *rdev = regulator->rdev;
1820 mutex_lock(&rdev->mutex);
1821 regulator->uA_load = 0;
1822 ret = _regulator_force_disable(regulator->rdev);
1823 mutex_unlock(&rdev->mutex);
1826 while (rdev->open_count--)
1827 regulator_disable(rdev->supply);
1831 EXPORT_SYMBOL_GPL(regulator_force_disable);
1833 static void regulator_disable_work(struct work_struct *work)
1835 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1839 mutex_lock(&rdev->mutex);
1841 BUG_ON(!rdev->deferred_disables);
1843 count = rdev->deferred_disables;
1844 rdev->deferred_disables = 0;
1846 for (i = 0; i < count; i++) {
1847 ret = _regulator_disable(rdev);
1849 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1852 mutex_unlock(&rdev->mutex);
1855 for (i = 0; i < count; i++) {
1856 ret = regulator_disable(rdev->supply);
1859 "Supply disable failed: %d\n", ret);
1866 * regulator_disable_deferred - disable regulator output with delay
1867 * @regulator: regulator source
1868 * @ms: miliseconds until the regulator is disabled
1870 * Execute regulator_disable() on the regulator after a delay. This
1871 * is intended for use with devices that require some time to quiesce.
1873 * NOTE: this will only disable the regulator output if no other consumer
1874 * devices have it enabled, the regulator device supports disabling and
1875 * machine constraints permit this operation.
1877 int regulator_disable_deferred(struct regulator *regulator, int ms)
1879 struct regulator_dev *rdev = regulator->rdev;
1882 if (regulator->always_on)
1886 return regulator_disable(regulator);
1888 mutex_lock(&rdev->mutex);
1889 rdev->deferred_disables++;
1890 mutex_unlock(&rdev->mutex);
1892 ret = schedule_delayed_work(&rdev->disable_work,
1893 msecs_to_jiffies(ms));
1899 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1902 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1904 * @rdev: regulator to operate on
1906 * Regulators that use regmap for their register I/O can set the
1907 * enable_reg and enable_mask fields in their descriptor and then use
1908 * this as their is_enabled operation, saving some code.
1910 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1915 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1919 if (rdev->desc->enable_is_inverted)
1920 return (val & rdev->desc->enable_mask) == 0;
1922 return (val & rdev->desc->enable_mask) != 0;
1924 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1927 * regulator_enable_regmap - standard enable() for regmap users
1929 * @rdev: regulator to operate on
1931 * Regulators that use regmap for their register I/O can set the
1932 * enable_reg and enable_mask fields in their descriptor and then use
1933 * this as their enable() operation, saving some code.
1935 int regulator_enable_regmap(struct regulator_dev *rdev)
1939 if (rdev->desc->enable_is_inverted)
1942 val = rdev->desc->enable_mask;
1944 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1945 rdev->desc->enable_mask, val);
1947 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1950 * regulator_disable_regmap - standard disable() for regmap users
1952 * @rdev: regulator to operate on
1954 * Regulators that use regmap for their register I/O can set the
1955 * enable_reg and enable_mask fields in their descriptor and then use
1956 * this as their disable() operation, saving some code.
1958 int regulator_disable_regmap(struct regulator_dev *rdev)
1962 if (rdev->desc->enable_is_inverted)
1963 val = rdev->desc->enable_mask;
1967 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1968 rdev->desc->enable_mask, val);
1970 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1972 static int _regulator_is_enabled(struct regulator_dev *rdev)
1974 /* A GPIO control always takes precedence */
1976 return rdev->ena_gpio_state;
1978 /* If we don't know then assume that the regulator is always on */
1979 if (!rdev->desc->ops->is_enabled)
1982 return rdev->desc->ops->is_enabled(rdev);
1986 * regulator_is_enabled - is the regulator output enabled
1987 * @regulator: regulator source
1989 * Returns positive if the regulator driver backing the source/client
1990 * has requested that the device be enabled, zero if it hasn't, else a
1991 * negative errno code.
1993 * Note that the device backing this regulator handle can have multiple
1994 * users, so it might be enabled even if regulator_enable() was never
1995 * called for this particular source.
1997 int regulator_is_enabled(struct regulator *regulator)
2001 if (regulator->always_on)
2004 mutex_lock(®ulator->rdev->mutex);
2005 ret = _regulator_is_enabled(regulator->rdev);
2006 mutex_unlock(®ulator->rdev->mutex);
2010 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2013 * regulator_can_change_voltage - check if regulator can change voltage
2014 * @regulator: regulator source
2016 * Returns positive if the regulator driver backing the source/client
2017 * can change its voltage, false otherwise. Usefull for detecting fixed
2018 * or dummy regulators and disabling voltage change logic in the client
2021 int regulator_can_change_voltage(struct regulator *regulator)
2023 struct regulator_dev *rdev = regulator->rdev;
2025 if (rdev->constraints &&
2026 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2027 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2030 if (rdev->desc->continuous_voltage_range &&
2031 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2032 rdev->constraints->min_uV != rdev->constraints->max_uV)
2038 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2041 * regulator_count_voltages - count regulator_list_voltage() selectors
2042 * @regulator: regulator source
2044 * Returns number of selectors, or negative errno. Selectors are
2045 * numbered starting at zero, and typically correspond to bitfields
2046 * in hardware registers.
2048 int regulator_count_voltages(struct regulator *regulator)
2050 struct regulator_dev *rdev = regulator->rdev;
2052 return rdev->desc->n_voltages ? : -EINVAL;
2054 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2057 * regulator_list_voltage_linear - List voltages with simple calculation
2059 * @rdev: Regulator device
2060 * @selector: Selector to convert into a voltage
2062 * Regulators with a simple linear mapping between voltages and
2063 * selectors can set min_uV and uV_step in the regulator descriptor
2064 * and then use this function as their list_voltage() operation,
2066 int regulator_list_voltage_linear(struct regulator_dev *rdev,
2067 unsigned int selector)
2069 if (selector >= rdev->desc->n_voltages)
2071 if (selector < rdev->desc->linear_min_sel)
2074 selector -= rdev->desc->linear_min_sel;
2076 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2078 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2081 * regulator_list_voltage_table - List voltages with table based mapping
2083 * @rdev: Regulator device
2084 * @selector: Selector to convert into a voltage
2086 * Regulators with table based mapping between voltages and
2087 * selectors can set volt_table in the regulator descriptor
2088 * and then use this function as their list_voltage() operation.
2090 int regulator_list_voltage_table(struct regulator_dev *rdev,
2091 unsigned int selector)
2093 if (!rdev->desc->volt_table) {
2094 BUG_ON(!rdev->desc->volt_table);
2098 if (selector >= rdev->desc->n_voltages)
2101 return rdev->desc->volt_table[selector];
2103 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2106 * regulator_list_voltage - enumerate supported voltages
2107 * @regulator: regulator source
2108 * @selector: identify voltage to list
2109 * Context: can sleep
2111 * Returns a voltage that can be passed to @regulator_set_voltage(),
2112 * zero if this selector code can't be used on this system, or a
2115 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2117 struct regulator_dev *rdev = regulator->rdev;
2118 struct regulator_ops *ops = rdev->desc->ops;
2121 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2124 mutex_lock(&rdev->mutex);
2125 ret = ops->list_voltage(rdev, selector);
2126 mutex_unlock(&rdev->mutex);
2129 if (ret < rdev->constraints->min_uV)
2131 else if (ret > rdev->constraints->max_uV)
2137 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2140 * regulator_is_supported_voltage - check if a voltage range can be supported
2142 * @regulator: Regulator to check.
2143 * @min_uV: Minimum required voltage in uV.
2144 * @max_uV: Maximum required voltage in uV.
2146 * Returns a boolean or a negative error code.
2148 int regulator_is_supported_voltage(struct regulator *regulator,
2149 int min_uV, int max_uV)
2151 struct regulator_dev *rdev = regulator->rdev;
2152 int i, voltages, ret;
2154 /* If we can't change voltage check the current voltage */
2155 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2156 ret = regulator_get_voltage(regulator);
2158 return (min_uV <= ret && ret <= max_uV);
2163 /* Any voltage within constrains range is fine? */
2164 if (rdev->desc->continuous_voltage_range)
2165 return min_uV >= rdev->constraints->min_uV &&
2166 max_uV <= rdev->constraints->max_uV;
2168 ret = regulator_count_voltages(regulator);
2173 for (i = 0; i < voltages; i++) {
2174 ret = regulator_list_voltage(regulator, i);
2176 if (ret >= min_uV && ret <= max_uV)
2182 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2185 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2187 * @rdev: regulator to operate on
2189 * Regulators that use regmap for their register I/O can set the
2190 * vsel_reg and vsel_mask fields in their descriptor and then use this
2191 * as their get_voltage_vsel operation, saving some code.
2193 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2198 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2202 val &= rdev->desc->vsel_mask;
2203 val >>= ffs(rdev->desc->vsel_mask) - 1;
2207 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2210 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2212 * @rdev: regulator to operate on
2213 * @sel: Selector to set
2215 * Regulators that use regmap for their register I/O can set the
2216 * vsel_reg and vsel_mask fields in their descriptor and then use this
2217 * as their set_voltage_vsel operation, saving some code.
2219 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2223 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2225 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2226 rdev->desc->vsel_mask, sel);
2230 if (rdev->desc->apply_bit)
2231 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2232 rdev->desc->apply_bit,
2233 rdev->desc->apply_bit);
2236 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2239 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2241 * @rdev: Regulator to operate on
2242 * @min_uV: Lower bound for voltage
2243 * @max_uV: Upper bound for voltage
2245 * Drivers implementing set_voltage_sel() and list_voltage() can use
2246 * this as their map_voltage() operation. It will find a suitable
2247 * voltage by calling list_voltage() until it gets something in bounds
2248 * for the requested voltages.
2250 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2251 int min_uV, int max_uV)
2253 int best_val = INT_MAX;
2257 /* Find the smallest voltage that falls within the specified
2260 for (i = 0; i < rdev->desc->n_voltages; i++) {
2261 ret = rdev->desc->ops->list_voltage(rdev, i);
2265 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2271 if (best_val != INT_MAX)
2276 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2279 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2281 * @rdev: Regulator to operate on
2282 * @min_uV: Lower bound for voltage
2283 * @max_uV: Upper bound for voltage
2285 * Drivers that have ascendant voltage list can use this as their
2286 * map_voltage() operation.
2288 int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2289 int min_uV, int max_uV)
2293 for (i = 0; i < rdev->desc->n_voltages; i++) {
2294 ret = rdev->desc->ops->list_voltage(rdev, i);
2301 if (ret >= min_uV && ret <= max_uV)
2307 EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2310 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2312 * @rdev: Regulator to operate on
2313 * @min_uV: Lower bound for voltage
2314 * @max_uV: Upper bound for voltage
2316 * Drivers providing min_uV and uV_step in their regulator_desc can
2317 * use this as their map_voltage() operation.
2319 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2320 int min_uV, int max_uV)
2324 /* Allow uV_step to be 0 for fixed voltage */
2325 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2326 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2332 if (!rdev->desc->uV_step) {
2333 BUG_ON(!rdev->desc->uV_step);
2337 if (min_uV < rdev->desc->min_uV)
2338 min_uV = rdev->desc->min_uV;
2340 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2344 ret += rdev->desc->linear_min_sel;
2346 /* Map back into a voltage to verify we're still in bounds */
2347 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2348 if (voltage < min_uV || voltage > max_uV)
2353 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2355 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2356 int min_uV, int max_uV)
2361 unsigned int selector;
2362 int old_selector = -1;
2364 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2366 min_uV += rdev->constraints->uV_offset;
2367 max_uV += rdev->constraints->uV_offset;
2370 * If we can't obtain the old selector there is not enough
2371 * info to call set_voltage_time_sel().
2373 if (_regulator_is_enabled(rdev) &&
2374 rdev->desc->ops->set_voltage_time_sel &&
2375 rdev->desc->ops->get_voltage_sel) {
2376 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2377 if (old_selector < 0)
2378 return old_selector;
2381 if (rdev->desc->ops->set_voltage) {
2382 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2386 if (rdev->desc->ops->list_voltage)
2387 best_val = rdev->desc->ops->list_voltage(rdev,
2390 best_val = _regulator_get_voltage(rdev);
2393 } else if (rdev->desc->ops->set_voltage_sel) {
2394 if (rdev->desc->ops->map_voltage) {
2395 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2398 if (rdev->desc->ops->list_voltage ==
2399 regulator_list_voltage_linear)
2400 ret = regulator_map_voltage_linear(rdev,
2403 ret = regulator_map_voltage_iterate(rdev,
2408 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2409 if (min_uV <= best_val && max_uV >= best_val) {
2411 if (old_selector == selector)
2414 ret = rdev->desc->ops->set_voltage_sel(
2424 /* Call set_voltage_time_sel if successfully obtained old_selector */
2425 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2426 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
2428 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2429 old_selector, selector);
2431 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2436 /* Insert any necessary delays */
2437 if (delay >= 1000) {
2438 mdelay(delay / 1000);
2439 udelay(delay % 1000);
2445 if (ret == 0 && best_val >= 0) {
2446 unsigned long data = best_val;
2448 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2452 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2458 * regulator_set_voltage - set regulator output voltage
2459 * @regulator: regulator source
2460 * @min_uV: Minimum required voltage in uV
2461 * @max_uV: Maximum acceptable voltage in uV
2463 * Sets a voltage regulator to the desired output voltage. This can be set
2464 * during any regulator state. IOW, regulator can be disabled or enabled.
2466 * If the regulator is enabled then the voltage will change to the new value
2467 * immediately otherwise if the regulator is disabled the regulator will
2468 * output at the new voltage when enabled.
2470 * NOTE: If the regulator is shared between several devices then the lowest
2471 * request voltage that meets the system constraints will be used.
2472 * Regulator system constraints must be set for this regulator before
2473 * calling this function otherwise this call will fail.
2475 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2477 struct regulator_dev *rdev = regulator->rdev;
2479 int old_min_uV, old_max_uV;
2481 mutex_lock(&rdev->mutex);
2483 /* If we're setting the same range as last time the change
2484 * should be a noop (some cpufreq implementations use the same
2485 * voltage for multiple frequencies, for example).
2487 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2491 if (!rdev->desc->ops->set_voltage &&
2492 !rdev->desc->ops->set_voltage_sel) {
2497 /* constraints check */
2498 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2502 /* restore original values in case of error */
2503 old_min_uV = regulator->min_uV;
2504 old_max_uV = regulator->max_uV;
2505 regulator->min_uV = min_uV;
2506 regulator->max_uV = max_uV;
2508 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2512 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2517 mutex_unlock(&rdev->mutex);
2520 regulator->min_uV = old_min_uV;
2521 regulator->max_uV = old_max_uV;
2522 mutex_unlock(&rdev->mutex);
2525 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2528 * regulator_set_voltage_time - get raise/fall time
2529 * @regulator: regulator source
2530 * @old_uV: starting voltage in microvolts
2531 * @new_uV: target voltage in microvolts
2533 * Provided with the starting and ending voltage, this function attempts to
2534 * calculate the time in microseconds required to rise or fall to this new
2537 int regulator_set_voltage_time(struct regulator *regulator,
2538 int old_uV, int new_uV)
2540 struct regulator_dev *rdev = regulator->rdev;
2541 struct regulator_ops *ops = rdev->desc->ops;
2547 /* Currently requires operations to do this */
2548 if (!ops->list_voltage || !ops->set_voltage_time_sel
2549 || !rdev->desc->n_voltages)
2552 for (i = 0; i < rdev->desc->n_voltages; i++) {
2553 /* We only look for exact voltage matches here */
2554 voltage = regulator_list_voltage(regulator, i);
2559 if (voltage == old_uV)
2561 if (voltage == new_uV)
2565 if (old_sel < 0 || new_sel < 0)
2568 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2570 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2573 * regulator_set_voltage_time_sel - get raise/fall time
2574 * @rdev: regulator source device
2575 * @old_selector: selector for starting voltage
2576 * @new_selector: selector for target voltage
2578 * Provided with the starting and target voltage selectors, this function
2579 * returns time in microseconds required to rise or fall to this new voltage
2581 * Drivers providing ramp_delay in regulation_constraints can use this as their
2582 * set_voltage_time_sel() operation.
2584 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2585 unsigned int old_selector,
2586 unsigned int new_selector)
2588 unsigned int ramp_delay = 0;
2589 int old_volt, new_volt;
2591 if (rdev->constraints->ramp_delay)
2592 ramp_delay = rdev->constraints->ramp_delay;
2593 else if (rdev->desc->ramp_delay)
2594 ramp_delay = rdev->desc->ramp_delay;
2596 if (ramp_delay == 0) {
2597 rdev_warn(rdev, "ramp_delay not set\n");
2602 if (!rdev->desc->ops->list_voltage)
2605 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2606 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2608 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2610 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2613 * regulator_sync_voltage - re-apply last regulator output voltage
2614 * @regulator: regulator source
2616 * Re-apply the last configured voltage. This is intended to be used
2617 * where some external control source the consumer is cooperating with
2618 * has caused the configured voltage to change.
2620 int regulator_sync_voltage(struct regulator *regulator)
2622 struct regulator_dev *rdev = regulator->rdev;
2623 int ret, min_uV, max_uV;
2625 mutex_lock(&rdev->mutex);
2627 if (!rdev->desc->ops->set_voltage &&
2628 !rdev->desc->ops->set_voltage_sel) {
2633 /* This is only going to work if we've had a voltage configured. */
2634 if (!regulator->min_uV && !regulator->max_uV) {
2639 min_uV = regulator->min_uV;
2640 max_uV = regulator->max_uV;
2642 /* This should be a paranoia check... */
2643 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2647 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2651 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2654 mutex_unlock(&rdev->mutex);
2657 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2659 static int _regulator_get_voltage(struct regulator_dev *rdev)
2663 if (rdev->desc->ops->get_voltage_sel) {
2664 sel = rdev->desc->ops->get_voltage_sel(rdev);
2667 ret = rdev->desc->ops->list_voltage(rdev, sel);
2668 } else if (rdev->desc->ops->get_voltage) {
2669 ret = rdev->desc->ops->get_voltage(rdev);
2670 } else if (rdev->desc->ops->list_voltage) {
2671 ret = rdev->desc->ops->list_voltage(rdev, 0);
2678 return ret - rdev->constraints->uV_offset;
2682 * regulator_get_voltage - get regulator output voltage
2683 * @regulator: regulator source
2685 * This returns the current regulator voltage in uV.
2687 * NOTE: If the regulator is disabled it will return the voltage value. This
2688 * function should not be used to determine regulator state.
2690 int regulator_get_voltage(struct regulator *regulator)
2694 mutex_lock(®ulator->rdev->mutex);
2696 ret = _regulator_get_voltage(regulator->rdev);
2698 mutex_unlock(®ulator->rdev->mutex);
2702 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2705 * regulator_set_current_limit - set regulator output current limit
2706 * @regulator: regulator source
2707 * @min_uA: Minimum supported current in uA
2708 * @max_uA: Maximum supported current in uA
2710 * Sets current sink to the desired output current. This can be set during
2711 * any regulator state. IOW, regulator can be disabled or enabled.
2713 * If the regulator is enabled then the current will change to the new value
2714 * immediately otherwise if the regulator is disabled the regulator will
2715 * output at the new current when enabled.
2717 * NOTE: Regulator system constraints must be set for this regulator before
2718 * calling this function otherwise this call will fail.
2720 int regulator_set_current_limit(struct regulator *regulator,
2721 int min_uA, int max_uA)
2723 struct regulator_dev *rdev = regulator->rdev;
2726 mutex_lock(&rdev->mutex);
2729 if (!rdev->desc->ops->set_current_limit) {
2734 /* constraints check */
2735 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2739 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2741 mutex_unlock(&rdev->mutex);
2744 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2746 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2750 mutex_lock(&rdev->mutex);
2753 if (!rdev->desc->ops->get_current_limit) {
2758 ret = rdev->desc->ops->get_current_limit(rdev);
2760 mutex_unlock(&rdev->mutex);
2765 * regulator_get_current_limit - get regulator output current
2766 * @regulator: regulator source
2768 * This returns the current supplied by the specified current sink in uA.
2770 * NOTE: If the regulator is disabled it will return the current value. This
2771 * function should not be used to determine regulator state.
2773 int regulator_get_current_limit(struct regulator *regulator)
2775 return _regulator_get_current_limit(regulator->rdev);
2777 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2780 * regulator_set_mode - set regulator operating mode
2781 * @regulator: regulator source
2782 * @mode: operating mode - one of the REGULATOR_MODE constants
2784 * Set regulator operating mode to increase regulator efficiency or improve
2785 * regulation performance.
2787 * NOTE: Regulator system constraints must be set for this regulator before
2788 * calling this function otherwise this call will fail.
2790 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2792 struct regulator_dev *rdev = regulator->rdev;
2794 int regulator_curr_mode;
2796 mutex_lock(&rdev->mutex);
2799 if (!rdev->desc->ops->set_mode) {
2804 /* return if the same mode is requested */
2805 if (rdev->desc->ops->get_mode) {
2806 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2807 if (regulator_curr_mode == mode) {
2813 /* constraints check */
2814 ret = regulator_mode_constrain(rdev, &mode);
2818 ret = rdev->desc->ops->set_mode(rdev, mode);
2820 mutex_unlock(&rdev->mutex);
2823 EXPORT_SYMBOL_GPL(regulator_set_mode);
2825 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2829 mutex_lock(&rdev->mutex);
2832 if (!rdev->desc->ops->get_mode) {
2837 ret = rdev->desc->ops->get_mode(rdev);
2839 mutex_unlock(&rdev->mutex);
2844 * regulator_get_mode - get regulator operating mode
2845 * @regulator: regulator source
2847 * Get the current regulator operating mode.
2849 unsigned int regulator_get_mode(struct regulator *regulator)
2851 return _regulator_get_mode(regulator->rdev);
2853 EXPORT_SYMBOL_GPL(regulator_get_mode);
2856 * regulator_set_optimum_mode - set regulator optimum operating mode
2857 * @regulator: regulator source
2858 * @uA_load: load current
2860 * Notifies the regulator core of a new device load. This is then used by
2861 * DRMS (if enabled by constraints) to set the most efficient regulator
2862 * operating mode for the new regulator loading.
2864 * Consumer devices notify their supply regulator of the maximum power
2865 * they will require (can be taken from device datasheet in the power
2866 * consumption tables) when they change operational status and hence power
2867 * state. Examples of operational state changes that can affect power
2868 * consumption are :-
2870 * o Device is opened / closed.
2871 * o Device I/O is about to begin or has just finished.
2872 * o Device is idling in between work.
2874 * This information is also exported via sysfs to userspace.
2876 * DRMS will sum the total requested load on the regulator and change
2877 * to the most efficient operating mode if platform constraints allow.
2879 * Returns the new regulator mode or error.
2881 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2883 struct regulator_dev *rdev = regulator->rdev;
2884 struct regulator *consumer;
2885 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2889 input_uV = regulator_get_voltage(rdev->supply);
2891 mutex_lock(&rdev->mutex);
2894 * first check to see if we can set modes at all, otherwise just
2895 * tell the consumer everything is OK.
2897 regulator->uA_load = uA_load;
2898 ret = regulator_check_drms(rdev);
2904 if (!rdev->desc->ops->get_optimum_mode)
2908 * we can actually do this so any errors are indicators of
2909 * potential real failure.
2913 if (!rdev->desc->ops->set_mode)
2916 /* get output voltage */
2917 output_uV = _regulator_get_voltage(rdev);
2918 if (output_uV <= 0) {
2919 rdev_err(rdev, "invalid output voltage found\n");
2923 /* No supply? Use constraint voltage */
2925 input_uV = rdev->constraints->input_uV;
2926 if (input_uV <= 0) {
2927 rdev_err(rdev, "invalid input voltage found\n");
2931 /* calc total requested load for this regulator */
2932 list_for_each_entry(consumer, &rdev->consumer_list, list)
2933 total_uA_load += consumer->uA_load;
2935 mode = rdev->desc->ops->get_optimum_mode(rdev,
2936 input_uV, output_uV,
2938 ret = regulator_mode_constrain(rdev, &mode);
2940 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2941 total_uA_load, input_uV, output_uV);
2945 ret = rdev->desc->ops->set_mode(rdev, mode);
2947 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2952 mutex_unlock(&rdev->mutex);
2955 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2958 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2960 * @rdev: device to operate on.
2961 * @enable: state to set.
2963 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2968 val = rdev->desc->bypass_mask;
2972 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2973 rdev->desc->bypass_mask, val);
2975 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2978 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2980 * @rdev: device to operate on.
2981 * @enable: current state.
2983 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2988 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2992 *enable = val & rdev->desc->bypass_mask;
2996 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2999 * regulator_allow_bypass - allow the regulator to go into bypass mode
3001 * @regulator: Regulator to configure
3002 * @enable: enable or disable bypass mode
3004 * Allow the regulator to go into bypass mode if all other consumers
3005 * for the regulator also enable bypass mode and the machine
3006 * constraints allow this. Bypass mode means that the regulator is
3007 * simply passing the input directly to the output with no regulation.
3009 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3011 struct regulator_dev *rdev = regulator->rdev;
3014 if (!rdev->desc->ops->set_bypass)
3017 if (rdev->constraints &&
3018 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3021 mutex_lock(&rdev->mutex);
3023 if (enable && !regulator->bypass) {
3024 rdev->bypass_count++;
3026 if (rdev->bypass_count == rdev->open_count) {
3027 ret = rdev->desc->ops->set_bypass(rdev, enable);
3029 rdev->bypass_count--;
3032 } else if (!enable && regulator->bypass) {
3033 rdev->bypass_count--;
3035 if (rdev->bypass_count != rdev->open_count) {
3036 ret = rdev->desc->ops->set_bypass(rdev, enable);
3038 rdev->bypass_count++;
3043 regulator->bypass = enable;
3045 mutex_unlock(&rdev->mutex);
3049 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3052 * regulator_register_notifier - register regulator event notifier
3053 * @regulator: regulator source
3054 * @nb: notifier block
3056 * Register notifier block to receive regulator events.
3058 int regulator_register_notifier(struct regulator *regulator,
3059 struct notifier_block *nb)
3061 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3064 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3067 * regulator_unregister_notifier - unregister regulator event notifier
3068 * @regulator: regulator source
3069 * @nb: notifier block
3071 * Unregister regulator event notifier block.
3073 int regulator_unregister_notifier(struct regulator *regulator,
3074 struct notifier_block *nb)
3076 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3079 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3081 /* notify regulator consumers and downstream regulator consumers.
3082 * Note mutex must be held by caller.
3084 static void _notifier_call_chain(struct regulator_dev *rdev,
3085 unsigned long event, void *data)
3087 /* call rdev chain first */
3088 blocking_notifier_call_chain(&rdev->notifier, event, data);
3092 * regulator_bulk_get - get multiple regulator consumers
3094 * @dev: Device to supply
3095 * @num_consumers: Number of consumers to register
3096 * @consumers: Configuration of consumers; clients are stored here.
3098 * @return 0 on success, an errno on failure.
3100 * This helper function allows drivers to get several regulator
3101 * consumers in one operation. If any of the regulators cannot be
3102 * acquired then any regulators that were allocated will be freed
3103 * before returning to the caller.
3105 int regulator_bulk_get(struct device *dev, int num_consumers,
3106 struct regulator_bulk_data *consumers)
3111 for (i = 0; i < num_consumers; i++)
3112 consumers[i].consumer = NULL;
3114 for (i = 0; i < num_consumers; i++) {
3115 consumers[i].consumer = regulator_get(dev,
3116 consumers[i].supply);
3117 if (IS_ERR(consumers[i].consumer)) {
3118 ret = PTR_ERR(consumers[i].consumer);
3119 dev_err(dev, "Failed to get supply '%s': %d\n",
3120 consumers[i].supply, ret);
3121 consumers[i].consumer = NULL;
3130 regulator_put(consumers[i].consumer);
3134 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3137 * devm_regulator_bulk_get - managed get multiple regulator consumers
3139 * @dev: Device to supply
3140 * @num_consumers: Number of consumers to register
3141 * @consumers: Configuration of consumers; clients are stored here.
3143 * @return 0 on success, an errno on failure.
3145 * This helper function allows drivers to get several regulator
3146 * consumers in one operation with management, the regulators will
3147 * automatically be freed when the device is unbound. If any of the
3148 * regulators cannot be acquired then any regulators that were
3149 * allocated will be freed before returning to the caller.
3151 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3152 struct regulator_bulk_data *consumers)
3157 for (i = 0; i < num_consumers; i++)
3158 consumers[i].consumer = NULL;
3160 for (i = 0; i < num_consumers; i++) {
3161 consumers[i].consumer = devm_regulator_get(dev,
3162 consumers[i].supply);
3163 if (IS_ERR(consumers[i].consumer)) {
3164 ret = PTR_ERR(consumers[i].consumer);
3165 dev_err(dev, "Failed to get supply '%s': %d\n",
3166 consumers[i].supply, ret);
3167 consumers[i].consumer = NULL;
3175 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3176 devm_regulator_put(consumers[i].consumer);
3180 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3182 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3184 struct regulator_bulk_data *bulk = data;
3186 bulk->ret = regulator_enable(bulk->consumer);
3190 * regulator_bulk_enable - enable multiple regulator consumers
3192 * @num_consumers: Number of consumers
3193 * @consumers: Consumer data; clients are stored here.
3194 * @return 0 on success, an errno on failure
3196 * This convenience API allows consumers to enable multiple regulator
3197 * clients in a single API call. If any consumers cannot be enabled
3198 * then any others that were enabled will be disabled again prior to
3201 int regulator_bulk_enable(int num_consumers,
3202 struct regulator_bulk_data *consumers)
3204 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3208 for (i = 0; i < num_consumers; i++) {
3209 if (consumers[i].consumer->always_on)
3210 consumers[i].ret = 0;
3212 async_schedule_domain(regulator_bulk_enable_async,
3213 &consumers[i], &async_domain);
3216 async_synchronize_full_domain(&async_domain);
3218 /* If any consumer failed we need to unwind any that succeeded */
3219 for (i = 0; i < num_consumers; i++) {
3220 if (consumers[i].ret != 0) {
3221 ret = consumers[i].ret;
3229 for (i = 0; i < num_consumers; i++) {
3230 if (consumers[i].ret < 0)
3231 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3234 regulator_disable(consumers[i].consumer);
3239 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3242 * regulator_bulk_disable - disable multiple regulator consumers
3244 * @num_consumers: Number of consumers
3245 * @consumers: Consumer data; clients are stored here.
3246 * @return 0 on success, an errno on failure
3248 * This convenience API allows consumers to disable multiple regulator
3249 * clients in a single API call. If any consumers cannot be disabled
3250 * then any others that were disabled will be enabled again prior to
3253 int regulator_bulk_disable(int num_consumers,
3254 struct regulator_bulk_data *consumers)
3259 for (i = num_consumers - 1; i >= 0; --i) {
3260 ret = regulator_disable(consumers[i].consumer);
3268 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3269 for (++i; i < num_consumers; ++i) {
3270 r = regulator_enable(consumers[i].consumer);
3272 pr_err("Failed to reename %s: %d\n",
3273 consumers[i].supply, r);
3278 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3281 * regulator_bulk_force_disable - force disable multiple regulator consumers
3283 * @num_consumers: Number of consumers
3284 * @consumers: Consumer data; clients are stored here.
3285 * @return 0 on success, an errno on failure
3287 * This convenience API allows consumers to forcibly disable multiple regulator
3288 * clients in a single API call.
3289 * NOTE: This should be used for situations when device damage will
3290 * likely occur if the regulators are not disabled (e.g. over temp).
3291 * Although regulator_force_disable function call for some consumers can
3292 * return error numbers, the function is called for all consumers.
3294 int regulator_bulk_force_disable(int num_consumers,
3295 struct regulator_bulk_data *consumers)
3300 for (i = 0; i < num_consumers; i++)
3302 regulator_force_disable(consumers[i].consumer);
3304 for (i = 0; i < num_consumers; i++) {
3305 if (consumers[i].ret != 0) {
3306 ret = consumers[i].ret;
3315 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3318 * regulator_bulk_free - free multiple regulator consumers
3320 * @num_consumers: Number of consumers
3321 * @consumers: Consumer data; clients are stored here.
3323 * This convenience API allows consumers to free multiple regulator
3324 * clients in a single API call.
3326 void regulator_bulk_free(int num_consumers,
3327 struct regulator_bulk_data *consumers)
3331 for (i = 0; i < num_consumers; i++) {
3332 regulator_put(consumers[i].consumer);
3333 consumers[i].consumer = NULL;
3336 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3339 * regulator_notifier_call_chain - call regulator event notifier
3340 * @rdev: regulator source
3341 * @event: notifier block
3342 * @data: callback-specific data.
3344 * Called by regulator drivers to notify clients a regulator event has
3345 * occurred. We also notify regulator clients downstream.
3346 * Note lock must be held by caller.
3348 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3349 unsigned long event, void *data)
3351 _notifier_call_chain(rdev, event, data);
3355 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3358 * regulator_mode_to_status - convert a regulator mode into a status
3360 * @mode: Mode to convert
3362 * Convert a regulator mode into a status.
3364 int regulator_mode_to_status(unsigned int mode)
3367 case REGULATOR_MODE_FAST:
3368 return REGULATOR_STATUS_FAST;
3369 case REGULATOR_MODE_NORMAL:
3370 return REGULATOR_STATUS_NORMAL;
3371 case REGULATOR_MODE_IDLE:
3372 return REGULATOR_STATUS_IDLE;
3373 case REGULATOR_MODE_STANDBY:
3374 return REGULATOR_STATUS_STANDBY;
3376 return REGULATOR_STATUS_UNDEFINED;
3379 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3382 * To avoid cluttering sysfs (and memory) with useless state, only
3383 * create attributes that can be meaningfully displayed.
3385 static int add_regulator_attributes(struct regulator_dev *rdev)
3387 struct device *dev = &rdev->dev;
3388 struct regulator_ops *ops = rdev->desc->ops;
3391 /* some attributes need specific methods to be displayed */
3392 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3393 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3394 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3395 status = device_create_file(dev, &dev_attr_microvolts);
3399 if (ops->get_current_limit) {
3400 status = device_create_file(dev, &dev_attr_microamps);
3404 if (ops->get_mode) {
3405 status = device_create_file(dev, &dev_attr_opmode);
3409 if (rdev->ena_pin || ops->is_enabled) {
3410 status = device_create_file(dev, &dev_attr_state);
3414 if (ops->get_status) {
3415 status = device_create_file(dev, &dev_attr_status);
3419 if (ops->get_bypass) {
3420 status = device_create_file(dev, &dev_attr_bypass);
3425 /* some attributes are type-specific */
3426 if (rdev->desc->type == REGULATOR_CURRENT) {
3427 status = device_create_file(dev, &dev_attr_requested_microamps);
3432 /* all the other attributes exist to support constraints;
3433 * don't show them if there are no constraints, or if the
3434 * relevant supporting methods are missing.
3436 if (!rdev->constraints)
3439 /* constraints need specific supporting methods */
3440 if (ops->set_voltage || ops->set_voltage_sel) {
3441 status = device_create_file(dev, &dev_attr_min_microvolts);
3444 status = device_create_file(dev, &dev_attr_max_microvolts);
3448 if (ops->set_current_limit) {
3449 status = device_create_file(dev, &dev_attr_min_microamps);
3452 status = device_create_file(dev, &dev_attr_max_microamps);
3457 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3460 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3463 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3467 if (ops->set_suspend_voltage) {
3468 status = device_create_file(dev,
3469 &dev_attr_suspend_standby_microvolts);
3472 status = device_create_file(dev,
3473 &dev_attr_suspend_mem_microvolts);
3476 status = device_create_file(dev,
3477 &dev_attr_suspend_disk_microvolts);
3482 if (ops->set_suspend_mode) {
3483 status = device_create_file(dev,
3484 &dev_attr_suspend_standby_mode);
3487 status = device_create_file(dev,
3488 &dev_attr_suspend_mem_mode);
3491 status = device_create_file(dev,
3492 &dev_attr_suspend_disk_mode);
3500 static void rdev_init_debugfs(struct regulator_dev *rdev)
3502 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3503 if (!rdev->debugfs) {
3504 rdev_warn(rdev, "Failed to create debugfs directory\n");
3508 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3510 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3512 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3513 &rdev->bypass_count);
3517 * regulator_register - register regulator
3518 * @regulator_desc: regulator to register
3519 * @config: runtime configuration for regulator
3521 * Called by regulator drivers to register a regulator.
3522 * Returns a valid pointer to struct regulator_dev on success
3523 * or an ERR_PTR() on error.
3525 struct regulator_dev *
3526 regulator_register(const struct regulator_desc *regulator_desc,
3527 const struct regulator_config *config)
3529 const struct regulation_constraints *constraints = NULL;
3530 const struct regulator_init_data *init_data;
3531 static atomic_t regulator_no = ATOMIC_INIT(0);
3532 struct regulator_dev *rdev;
3535 const char *supply = NULL;
3537 if (regulator_desc == NULL || config == NULL)
3538 return ERR_PTR(-EINVAL);
3543 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3544 return ERR_PTR(-EINVAL);
3546 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3547 regulator_desc->type != REGULATOR_CURRENT)
3548 return ERR_PTR(-EINVAL);
3550 /* Only one of each should be implemented */
3551 WARN_ON(regulator_desc->ops->get_voltage &&
3552 regulator_desc->ops->get_voltage_sel);
3553 WARN_ON(regulator_desc->ops->set_voltage &&
3554 regulator_desc->ops->set_voltage_sel);
3556 /* If we're using selectors we must implement list_voltage. */
3557 if (regulator_desc->ops->get_voltage_sel &&
3558 !regulator_desc->ops->list_voltage) {
3559 return ERR_PTR(-EINVAL);
3561 if (regulator_desc->ops->set_voltage_sel &&
3562 !regulator_desc->ops->list_voltage) {
3563 return ERR_PTR(-EINVAL);
3566 init_data = config->init_data;
3568 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3570 return ERR_PTR(-ENOMEM);
3572 mutex_lock(®ulator_list_mutex);
3574 mutex_init(&rdev->mutex);
3575 rdev->reg_data = config->driver_data;
3576 rdev->owner = regulator_desc->owner;
3577 rdev->desc = regulator_desc;
3579 rdev->regmap = config->regmap;
3580 else if (dev_get_regmap(dev, NULL))
3581 rdev->regmap = dev_get_regmap(dev, NULL);
3582 else if (dev->parent)
3583 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3584 INIT_LIST_HEAD(&rdev->consumer_list);
3585 INIT_LIST_HEAD(&rdev->list);
3586 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3587 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3589 /* preform any regulator specific init */
3590 if (init_data && init_data->regulator_init) {
3591 ret = init_data->regulator_init(rdev->reg_data);
3596 /* register with sysfs */
3597 rdev->dev.class = ®ulator_class;
3598 rdev->dev.of_node = config->of_node;
3599 rdev->dev.parent = dev;
3600 dev_set_name(&rdev->dev, "regulator.%d",
3601 atomic_inc_return(®ulator_no) - 1);
3602 ret = device_register(&rdev->dev);
3604 put_device(&rdev->dev);
3608 dev_set_drvdata(&rdev->dev, rdev);
3610 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3611 ret = regulator_ena_gpio_request(rdev, config);
3613 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3614 config->ena_gpio, ret);
3618 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3619 rdev->ena_gpio_state = 1;
3621 if (config->ena_gpio_invert)
3622 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3625 /* set regulator constraints */
3627 constraints = &init_data->constraints;
3629 ret = set_machine_constraints(rdev, constraints);
3633 /* add attributes supported by this regulator */
3634 ret = add_regulator_attributes(rdev);
3638 if (init_data && init_data->supply_regulator)
3639 supply = init_data->supply_regulator;
3640 else if (regulator_desc->supply_name)
3641 supply = regulator_desc->supply_name;
3644 struct regulator_dev *r;
3646 r = regulator_dev_lookup(dev, supply, &ret);
3648 if (ret == -ENODEV) {
3650 * No supply was specified for this regulator and
3651 * there will never be one.
3656 dev_err(dev, "Failed to find supply %s\n", supply);
3657 ret = -EPROBE_DEFER;
3661 ret = set_supply(rdev, r);
3665 /* Enable supply if rail is enabled */
3666 if (_regulator_is_enabled(rdev)) {
3667 ret = regulator_enable(rdev->supply);
3674 /* add consumers devices */
3676 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3677 ret = set_consumer_device_supply(rdev,
3678 init_data->consumer_supplies[i].dev_name,
3679 init_data->consumer_supplies[i].supply);
3681 dev_err(dev, "Failed to set supply %s\n",
3682 init_data->consumer_supplies[i].supply);
3683 goto unset_supplies;
3688 list_add(&rdev->list, ®ulator_list);
3690 rdev_init_debugfs(rdev);
3692 mutex_unlock(®ulator_list_mutex);
3696 unset_regulator_supplies(rdev);
3700 _regulator_put(rdev->supply);
3701 regulator_ena_gpio_free(rdev);
3702 kfree(rdev->constraints);
3704 device_unregister(&rdev->dev);
3705 /* device core frees rdev */
3706 rdev = ERR_PTR(ret);
3711 rdev = ERR_PTR(ret);
3714 EXPORT_SYMBOL_GPL(regulator_register);
3717 * regulator_unregister - unregister regulator
3718 * @rdev: regulator to unregister
3720 * Called by regulator drivers to unregister a regulator.
3722 void regulator_unregister(struct regulator_dev *rdev)
3728 regulator_put(rdev->supply);
3729 mutex_lock(®ulator_list_mutex);
3730 debugfs_remove_recursive(rdev->debugfs);
3731 flush_work(&rdev->disable_work.work);
3732 WARN_ON(rdev->open_count);
3733 unset_regulator_supplies(rdev);
3734 list_del(&rdev->list);
3735 kfree(rdev->constraints);
3736 regulator_ena_gpio_free(rdev);
3737 device_unregister(&rdev->dev);
3738 mutex_unlock(®ulator_list_mutex);
3740 EXPORT_SYMBOL_GPL(regulator_unregister);
3743 * regulator_suspend_prepare - prepare regulators for system wide suspend
3744 * @state: system suspend state
3746 * Configure each regulator with it's suspend operating parameters for state.
3747 * This will usually be called by machine suspend code prior to supending.
3749 int regulator_suspend_prepare(suspend_state_t state)
3751 struct regulator_dev *rdev;
3754 /* ON is handled by regulator active state */
3755 if (state == PM_SUSPEND_ON)
3758 mutex_lock(®ulator_list_mutex);
3759 list_for_each_entry(rdev, ®ulator_list, list) {
3761 mutex_lock(&rdev->mutex);
3762 ret = suspend_prepare(rdev, state);
3763 mutex_unlock(&rdev->mutex);
3766 rdev_err(rdev, "failed to prepare\n");
3771 mutex_unlock(®ulator_list_mutex);
3774 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3777 * regulator_suspend_finish - resume regulators from system wide suspend
3779 * Turn on regulators that might be turned off by regulator_suspend_prepare
3780 * and that should be turned on according to the regulators properties.
3782 int regulator_suspend_finish(void)
3784 struct regulator_dev *rdev;
3787 mutex_lock(®ulator_list_mutex);
3788 list_for_each_entry(rdev, ®ulator_list, list) {
3789 mutex_lock(&rdev->mutex);
3790 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3791 error = _regulator_do_enable(rdev);
3795 if (!has_full_constraints)
3797 if (!_regulator_is_enabled(rdev))
3800 error = _regulator_do_disable(rdev);
3805 mutex_unlock(&rdev->mutex);
3807 mutex_unlock(®ulator_list_mutex);
3810 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3813 * regulator_has_full_constraints - the system has fully specified constraints
3815 * Calling this function will cause the regulator API to disable all
3816 * regulators which have a zero use count and don't have an always_on
3817 * constraint in a late_initcall.
3819 * The intention is that this will become the default behaviour in a
3820 * future kernel release so users are encouraged to use this facility
3823 void regulator_has_full_constraints(void)
3825 has_full_constraints = 1;
3827 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3830 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3832 * Calling this function will cause the regulator API to provide a
3833 * dummy regulator to consumers if no physical regulator is found,
3834 * allowing most consumers to proceed as though a regulator were
3835 * configured. This allows systems such as those with software
3836 * controllable regulators for the CPU core only to be brought up more
3839 void regulator_use_dummy_regulator(void)
3841 board_wants_dummy_regulator = true;
3843 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3846 * rdev_get_drvdata - get rdev regulator driver data
3849 * Get rdev regulator driver private data. This call can be used in the
3850 * regulator driver context.
3852 void *rdev_get_drvdata(struct regulator_dev *rdev)
3854 return rdev->reg_data;
3856 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3859 * regulator_get_drvdata - get regulator driver data
3860 * @regulator: regulator
3862 * Get regulator driver private data. This call can be used in the consumer
3863 * driver context when non API regulator specific functions need to be called.
3865 void *regulator_get_drvdata(struct regulator *regulator)
3867 return regulator->rdev->reg_data;
3869 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3872 * regulator_set_drvdata - set regulator driver data
3873 * @regulator: regulator
3876 void regulator_set_drvdata(struct regulator *regulator, void *data)
3878 regulator->rdev->reg_data = data;
3880 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3883 * regulator_get_id - get regulator ID
3886 int rdev_get_id(struct regulator_dev *rdev)
3888 return rdev->desc->id;
3890 EXPORT_SYMBOL_GPL(rdev_get_id);
3892 struct device *rdev_get_dev(struct regulator_dev *rdev)
3896 EXPORT_SYMBOL_GPL(rdev_get_dev);
3898 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3900 return reg_init_data->driver_data;
3902 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3904 #ifdef CONFIG_DEBUG_FS
3905 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3906 size_t count, loff_t *ppos)
3908 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3909 ssize_t len, ret = 0;
3910 struct regulator_map *map;
3915 list_for_each_entry(map, ®ulator_map_list, list) {
3916 len = snprintf(buf + ret, PAGE_SIZE - ret,
3918 rdev_get_name(map->regulator), map->dev_name,
3922 if (ret > PAGE_SIZE) {
3928 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3936 static const struct file_operations supply_map_fops = {
3937 #ifdef CONFIG_DEBUG_FS
3938 .read = supply_map_read_file,
3939 .llseek = default_llseek,
3943 static int __init regulator_init(void)
3947 ret = class_register(®ulator_class);
3949 debugfs_root = debugfs_create_dir("regulator", NULL);
3951 pr_warn("regulator: Failed to create debugfs directory\n");
3953 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3956 regulator_dummy_init();
3961 /* init early to allow our consumers to complete system booting */
3962 core_initcall(regulator_init);
3964 static int __init regulator_init_complete(void)
3966 struct regulator_dev *rdev;
3967 struct regulator_ops *ops;
3968 struct regulation_constraints *c;
3972 * Since DT doesn't provide an idiomatic mechanism for
3973 * enabling full constraints and since it's much more natural
3974 * with DT to provide them just assume that a DT enabled
3975 * system has full constraints.
3977 if (of_have_populated_dt())
3978 has_full_constraints = true;
3980 mutex_lock(®ulator_list_mutex);
3982 /* If we have a full configuration then disable any regulators
3983 * which are not in use or always_on. This will become the
3984 * default behaviour in the future.
3986 list_for_each_entry(rdev, ®ulator_list, list) {
3987 ops = rdev->desc->ops;
3988 c = rdev->constraints;
3990 if (c && c->always_on)
3993 mutex_lock(&rdev->mutex);
3995 if (rdev->use_count)
3998 /* If we can't read the status assume it's on. */
3999 if (ops->is_enabled)
4000 enabled = ops->is_enabled(rdev);
4007 if (has_full_constraints) {
4008 /* We log since this may kill the system if it
4010 rdev_info(rdev, "disabling\n");
4011 ret = _regulator_do_disable(rdev);
4013 rdev_err(rdev, "couldn't disable: %d\n", ret);
4016 /* The intention is that in future we will
4017 * assume that full constraints are provided
4018 * so warn even if we aren't going to do
4021 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4025 mutex_unlock(&rdev->mutex);
4028 mutex_unlock(®ulator_list_mutex);
4032 late_initcall(regulator_init_complete);