2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
53 static DEFINE_MUTEX(regulator_list_mutex);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
59 static struct dentry *debugfs_root;
61 static struct class regulator_class;
64 * struct regulator_map
66 * Used to provide symbolic supply names to devices.
68 struct regulator_map {
69 struct list_head list;
70 const char *dev_name; /* The dev_name() for the consumer */
72 struct regulator_dev *regulator;
76 * struct regulator_enable_gpio
78 * Management for shared enable GPIO pin
80 struct regulator_enable_gpio {
81 struct list_head list;
82 struct gpio_desc *gpiod;
83 u32 enable_count; /* a number of enabled shared GPIO */
84 u32 request_count; /* a number of requested shared GPIO */
85 unsigned int ena_gpio_invert:1;
89 * struct regulator_supply_alias
91 * Used to map lookups for a supply onto an alternative device.
93 struct regulator_supply_alias {
94 struct list_head list;
95 struct device *src_dev;
96 const char *src_supply;
97 struct device *alias_dev;
98 const char *alias_supply;
101 static int _regulator_is_enabled(struct regulator_dev *rdev);
102 static int _regulator_disable(struct regulator_dev *rdev);
103 static int _regulator_get_voltage(struct regulator_dev *rdev);
104 static int _regulator_get_current_limit(struct regulator_dev *rdev);
105 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
106 static int _notifier_call_chain(struct regulator_dev *rdev,
107 unsigned long event, void *data);
108 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
109 int min_uV, int max_uV);
110 static struct regulator *create_regulator(struct regulator_dev *rdev,
112 const char *supply_name);
113 static void _regulator_put(struct regulator *regulator);
115 static struct regulator_dev *dev_to_rdev(struct device *dev)
117 return container_of(dev, struct regulator_dev, dev);
120 static const char *rdev_get_name(struct regulator_dev *rdev)
122 if (rdev->constraints && rdev->constraints->name)
123 return rdev->constraints->name;
124 else if (rdev->desc->name)
125 return rdev->desc->name;
130 static bool have_full_constraints(void)
132 return has_full_constraints || of_have_populated_dt();
135 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
137 if (rdev && rdev->supply)
138 return rdev->supply->rdev;
144 * regulator_lock_supply - lock a regulator and its supplies
145 * @rdev: regulator source
147 static void regulator_lock_supply(struct regulator_dev *rdev)
151 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
152 mutex_lock_nested(&rdev->mutex, i);
156 * regulator_unlock_supply - unlock a regulator and its supplies
157 * @rdev: regulator source
159 static void regulator_unlock_supply(struct regulator_dev *rdev)
161 struct regulator *supply;
164 mutex_unlock(&rdev->mutex);
165 supply = rdev->supply;
175 * of_get_regulator - get a regulator device node based on supply name
176 * @dev: Device pointer for the consumer (of regulator) device
177 * @supply: regulator supply name
179 * Extract the regulator device node corresponding to the supply name.
180 * returns the device node corresponding to the regulator if found, else
183 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
185 struct device_node *regnode = NULL;
186 char prop_name[32]; /* 32 is max size of property name */
188 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
190 snprintf(prop_name, 32, "%s-supply", supply);
191 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
194 dev_dbg(dev, "Looking up %s property in node %s failed",
195 prop_name, dev->of_node->full_name);
201 static int _regulator_can_change_status(struct regulator_dev *rdev)
203 if (!rdev->constraints)
206 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
212 /* Platform voltage constraint check */
213 static int regulator_check_voltage(struct regulator_dev *rdev,
214 int *min_uV, int *max_uV)
216 BUG_ON(*min_uV > *max_uV);
218 if (!rdev->constraints) {
219 rdev_err(rdev, "no constraints\n");
222 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
223 rdev_err(rdev, "voltage operation not allowed\n");
227 if (*max_uV > rdev->constraints->max_uV)
228 *max_uV = rdev->constraints->max_uV;
229 if (*min_uV < rdev->constraints->min_uV)
230 *min_uV = rdev->constraints->min_uV;
232 if (*min_uV > *max_uV) {
233 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
241 /* Make sure we select a voltage that suits the needs of all
242 * regulator consumers
244 static int regulator_check_consumers(struct regulator_dev *rdev,
245 int *min_uV, int *max_uV)
247 struct regulator *regulator;
249 list_for_each_entry(regulator, &rdev->consumer_list, list) {
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
254 if (!regulator->min_uV && !regulator->max_uV)
257 if (*max_uV > regulator->max_uV)
258 *max_uV = regulator->max_uV;
259 if (*min_uV < regulator->min_uV)
260 *min_uV = regulator->min_uV;
263 if (*min_uV > *max_uV) {
264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
272 /* current constraint check */
273 static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
276 BUG_ON(*min_uA > *max_uA);
278 if (!rdev->constraints) {
279 rdev_err(rdev, "no constraints\n");
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
283 rdev_err(rdev, "current operation not allowed\n");
287 if (*max_uA > rdev->constraints->max_uA)
288 *max_uA = rdev->constraints->max_uA;
289 if (*min_uA < rdev->constraints->min_uA)
290 *min_uA = rdev->constraints->min_uA;
292 if (*min_uA > *max_uA) {
293 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
301 /* operating mode constraint check */
302 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
305 case REGULATOR_MODE_FAST:
306 case REGULATOR_MODE_NORMAL:
307 case REGULATOR_MODE_IDLE:
308 case REGULATOR_MODE_STANDBY:
311 rdev_err(rdev, "invalid mode %x specified\n", *mode);
315 if (!rdev->constraints) {
316 rdev_err(rdev, "no constraints\n");
319 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
320 rdev_err(rdev, "mode operation not allowed\n");
324 /* The modes are bitmasks, the most power hungry modes having
325 * the lowest values. If the requested mode isn't supported
326 * try higher modes. */
328 if (rdev->constraints->valid_modes_mask & *mode)
336 /* dynamic regulator mode switching constraint check */
337 static int regulator_check_drms(struct regulator_dev *rdev)
339 if (!rdev->constraints) {
340 rdev_err(rdev, "no constraints\n");
343 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
344 rdev_dbg(rdev, "drms operation not allowed\n");
350 static ssize_t regulator_uV_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
356 mutex_lock(&rdev->mutex);
357 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
358 mutex_unlock(&rdev->mutex);
362 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
364 static ssize_t regulator_uA_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
367 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
371 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
373 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
376 struct regulator_dev *rdev = dev_get_drvdata(dev);
378 return sprintf(buf, "%s\n", rdev_get_name(rdev));
380 static DEVICE_ATTR_RO(name);
382 static ssize_t regulator_print_opmode(char *buf, int mode)
385 case REGULATOR_MODE_FAST:
386 return sprintf(buf, "fast\n");
387 case REGULATOR_MODE_NORMAL:
388 return sprintf(buf, "normal\n");
389 case REGULATOR_MODE_IDLE:
390 return sprintf(buf, "idle\n");
391 case REGULATOR_MODE_STANDBY:
392 return sprintf(buf, "standby\n");
394 return sprintf(buf, "unknown\n");
397 static ssize_t regulator_opmode_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
400 struct regulator_dev *rdev = dev_get_drvdata(dev);
402 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
404 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
406 static ssize_t regulator_print_state(char *buf, int state)
409 return sprintf(buf, "enabled\n");
411 return sprintf(buf, "disabled\n");
413 return sprintf(buf, "unknown\n");
416 static ssize_t regulator_state_show(struct device *dev,
417 struct device_attribute *attr, char *buf)
419 struct regulator_dev *rdev = dev_get_drvdata(dev);
422 mutex_lock(&rdev->mutex);
423 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
424 mutex_unlock(&rdev->mutex);
428 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
430 static ssize_t regulator_status_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
433 struct regulator_dev *rdev = dev_get_drvdata(dev);
437 status = rdev->desc->ops->get_status(rdev);
442 case REGULATOR_STATUS_OFF:
445 case REGULATOR_STATUS_ON:
448 case REGULATOR_STATUS_ERROR:
451 case REGULATOR_STATUS_FAST:
454 case REGULATOR_STATUS_NORMAL:
457 case REGULATOR_STATUS_IDLE:
460 case REGULATOR_STATUS_STANDBY:
463 case REGULATOR_STATUS_BYPASS:
466 case REGULATOR_STATUS_UNDEFINED:
473 return sprintf(buf, "%s\n", label);
475 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
477 static ssize_t regulator_min_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
480 struct regulator_dev *rdev = dev_get_drvdata(dev);
482 if (!rdev->constraints)
483 return sprintf(buf, "constraint not defined\n");
485 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
487 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
489 static ssize_t regulator_max_uA_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
492 struct regulator_dev *rdev = dev_get_drvdata(dev);
494 if (!rdev->constraints)
495 return sprintf(buf, "constraint not defined\n");
497 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
499 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
501 static ssize_t regulator_min_uV_show(struct device *dev,
502 struct device_attribute *attr, char *buf)
504 struct regulator_dev *rdev = dev_get_drvdata(dev);
506 if (!rdev->constraints)
507 return sprintf(buf, "constraint not defined\n");
509 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
511 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
513 static ssize_t regulator_max_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
516 struct regulator_dev *rdev = dev_get_drvdata(dev);
518 if (!rdev->constraints)
519 return sprintf(buf, "constraint not defined\n");
521 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
523 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
525 static ssize_t regulator_total_uA_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
528 struct regulator_dev *rdev = dev_get_drvdata(dev);
529 struct regulator *regulator;
532 mutex_lock(&rdev->mutex);
533 list_for_each_entry(regulator, &rdev->consumer_list, list)
534 uA += regulator->uA_load;
535 mutex_unlock(&rdev->mutex);
536 return sprintf(buf, "%d\n", uA);
538 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
540 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
543 struct regulator_dev *rdev = dev_get_drvdata(dev);
544 return sprintf(buf, "%d\n", rdev->use_count);
546 static DEVICE_ATTR_RO(num_users);
548 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
553 switch (rdev->desc->type) {
554 case REGULATOR_VOLTAGE:
555 return sprintf(buf, "voltage\n");
556 case REGULATOR_CURRENT:
557 return sprintf(buf, "current\n");
559 return sprintf(buf, "unknown\n");
561 static DEVICE_ATTR_RO(type);
563 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 struct regulator_dev *rdev = dev_get_drvdata(dev);
568 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
570 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
571 regulator_suspend_mem_uV_show, NULL);
573 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 struct regulator_dev *rdev = dev_get_drvdata(dev);
578 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
580 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
581 regulator_suspend_disk_uV_show, NULL);
583 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
586 struct regulator_dev *rdev = dev_get_drvdata(dev);
588 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
590 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
591 regulator_suspend_standby_uV_show, NULL);
593 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 struct regulator_dev *rdev = dev_get_drvdata(dev);
598 return regulator_print_opmode(buf,
599 rdev->constraints->state_mem.mode);
601 static DEVICE_ATTR(suspend_mem_mode, 0444,
602 regulator_suspend_mem_mode_show, NULL);
604 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
605 struct device_attribute *attr, char *buf)
607 struct regulator_dev *rdev = dev_get_drvdata(dev);
609 return regulator_print_opmode(buf,
610 rdev->constraints->state_disk.mode);
612 static DEVICE_ATTR(suspend_disk_mode, 0444,
613 regulator_suspend_disk_mode_show, NULL);
615 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct regulator_dev *rdev = dev_get_drvdata(dev);
620 return regulator_print_opmode(buf,
621 rdev->constraints->state_standby.mode);
623 static DEVICE_ATTR(suspend_standby_mode, 0444,
624 regulator_suspend_standby_mode_show, NULL);
626 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
627 struct device_attribute *attr, char *buf)
629 struct regulator_dev *rdev = dev_get_drvdata(dev);
631 return regulator_print_state(buf,
632 rdev->constraints->state_mem.enabled);
634 static DEVICE_ATTR(suspend_mem_state, 0444,
635 regulator_suspend_mem_state_show, NULL);
637 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
638 struct device_attribute *attr, char *buf)
640 struct regulator_dev *rdev = dev_get_drvdata(dev);
642 return regulator_print_state(buf,
643 rdev->constraints->state_disk.enabled);
645 static DEVICE_ATTR(suspend_disk_state, 0444,
646 regulator_suspend_disk_state_show, NULL);
648 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 struct regulator_dev *rdev = dev_get_drvdata(dev);
653 return regulator_print_state(buf,
654 rdev->constraints->state_standby.enabled);
656 static DEVICE_ATTR(suspend_standby_state, 0444,
657 regulator_suspend_standby_state_show, NULL);
659 static ssize_t regulator_bypass_show(struct device *dev,
660 struct device_attribute *attr, char *buf)
662 struct regulator_dev *rdev = dev_get_drvdata(dev);
667 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
676 return sprintf(buf, "%s\n", report);
678 static DEVICE_ATTR(bypass, 0444,
679 regulator_bypass_show, NULL);
681 /* Calculate the new optimum regulator operating mode based on the new total
682 * consumer load. All locks held by caller */
683 static int drms_uA_update(struct regulator_dev *rdev)
685 struct regulator *sibling;
686 int current_uA = 0, output_uV, input_uV, err;
689 lockdep_assert_held_once(&rdev->mutex);
692 * first check to see if we can set modes at all, otherwise just
693 * tell the consumer everything is OK.
695 err = regulator_check_drms(rdev);
699 if (!rdev->desc->ops->get_optimum_mode &&
700 !rdev->desc->ops->set_load)
703 if (!rdev->desc->ops->set_mode &&
704 !rdev->desc->ops->set_load)
707 /* get output voltage */
708 output_uV = _regulator_get_voltage(rdev);
709 if (output_uV <= 0) {
710 rdev_err(rdev, "invalid output voltage found\n");
714 /* get input voltage */
717 input_uV = regulator_get_voltage(rdev->supply);
719 input_uV = rdev->constraints->input_uV;
721 rdev_err(rdev, "invalid input voltage found\n");
725 /* calc total requested load */
726 list_for_each_entry(sibling, &rdev->consumer_list, list)
727 current_uA += sibling->uA_load;
729 current_uA += rdev->constraints->system_load;
731 if (rdev->desc->ops->set_load) {
732 /* set the optimum mode for our new total regulator load */
733 err = rdev->desc->ops->set_load(rdev, current_uA);
735 rdev_err(rdev, "failed to set load %d\n", current_uA);
737 /* now get the optimum mode for our new total regulator load */
738 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
739 output_uV, current_uA);
741 /* check the new mode is allowed */
742 err = regulator_mode_constrain(rdev, &mode);
744 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
745 current_uA, input_uV, output_uV);
749 err = rdev->desc->ops->set_mode(rdev, mode);
751 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
757 static int suspend_set_state(struct regulator_dev *rdev,
758 struct regulator_state *rstate)
762 /* If we have no suspend mode configration don't set anything;
763 * only warn if the driver implements set_suspend_voltage or
764 * set_suspend_mode callback.
766 if (!rstate->enabled && !rstate->disabled) {
767 if (rdev->desc->ops->set_suspend_voltage ||
768 rdev->desc->ops->set_suspend_mode)
769 rdev_warn(rdev, "No configuration\n");
773 if (rstate->enabled && rstate->disabled) {
774 rdev_err(rdev, "invalid configuration\n");
778 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
779 ret = rdev->desc->ops->set_suspend_enable(rdev);
780 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
781 ret = rdev->desc->ops->set_suspend_disable(rdev);
782 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
786 rdev_err(rdev, "failed to enabled/disable\n");
790 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
791 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
793 rdev_err(rdev, "failed to set voltage\n");
798 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
799 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
801 rdev_err(rdev, "failed to set mode\n");
808 /* locks held by caller */
809 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
811 if (!rdev->constraints)
815 case PM_SUSPEND_STANDBY:
816 return suspend_set_state(rdev,
817 &rdev->constraints->state_standby);
819 return suspend_set_state(rdev,
820 &rdev->constraints->state_mem);
822 return suspend_set_state(rdev,
823 &rdev->constraints->state_disk);
829 static void print_constraints(struct regulator_dev *rdev)
831 struct regulation_constraints *constraints = rdev->constraints;
833 size_t len = sizeof(buf) - 1;
837 if (constraints->min_uV && constraints->max_uV) {
838 if (constraints->min_uV == constraints->max_uV)
839 count += scnprintf(buf + count, len - count, "%d mV ",
840 constraints->min_uV / 1000);
842 count += scnprintf(buf + count, len - count,
844 constraints->min_uV / 1000,
845 constraints->max_uV / 1000);
848 if (!constraints->min_uV ||
849 constraints->min_uV != constraints->max_uV) {
850 ret = _regulator_get_voltage(rdev);
852 count += scnprintf(buf + count, len - count,
853 "at %d mV ", ret / 1000);
856 if (constraints->uV_offset)
857 count += scnprintf(buf + count, len - count, "%dmV offset ",
858 constraints->uV_offset / 1000);
860 if (constraints->min_uA && constraints->max_uA) {
861 if (constraints->min_uA == constraints->max_uA)
862 count += scnprintf(buf + count, len - count, "%d mA ",
863 constraints->min_uA / 1000);
865 count += scnprintf(buf + count, len - count,
867 constraints->min_uA / 1000,
868 constraints->max_uA / 1000);
871 if (!constraints->min_uA ||
872 constraints->min_uA != constraints->max_uA) {
873 ret = _regulator_get_current_limit(rdev);
875 count += scnprintf(buf + count, len - count,
876 "at %d mA ", ret / 1000);
879 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
880 count += scnprintf(buf + count, len - count, "fast ");
881 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
882 count += scnprintf(buf + count, len - count, "normal ");
883 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
884 count += scnprintf(buf + count, len - count, "idle ");
885 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
886 count += scnprintf(buf + count, len - count, "standby");
889 scnprintf(buf, len, "no parameters");
891 rdev_dbg(rdev, "%s\n", buf);
893 if ((constraints->min_uV != constraints->max_uV) &&
894 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
896 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
899 static int machine_constraints_voltage(struct regulator_dev *rdev,
900 struct regulation_constraints *constraints)
902 const struct regulator_ops *ops = rdev->desc->ops;
905 /* do we need to apply the constraint voltage */
906 if (rdev->constraints->apply_uV &&
907 rdev->constraints->min_uV == rdev->constraints->max_uV) {
908 int current_uV = _regulator_get_voltage(rdev);
909 if (current_uV < 0) {
911 "failed to get the current voltage(%d)\n",
915 if (current_uV < rdev->constraints->min_uV ||
916 current_uV > rdev->constraints->max_uV) {
917 ret = _regulator_do_set_voltage(
918 rdev, rdev->constraints->min_uV,
919 rdev->constraints->max_uV);
922 "failed to apply %duV constraint(%d)\n",
923 rdev->constraints->min_uV, ret);
929 /* constrain machine-level voltage specs to fit
930 * the actual range supported by this regulator.
932 if (ops->list_voltage && rdev->desc->n_voltages) {
933 int count = rdev->desc->n_voltages;
935 int min_uV = INT_MAX;
936 int max_uV = INT_MIN;
937 int cmin = constraints->min_uV;
938 int cmax = constraints->max_uV;
940 /* it's safe to autoconfigure fixed-voltage supplies
941 and the constraints are used by list_voltage. */
942 if (count == 1 && !cmin) {
945 constraints->min_uV = cmin;
946 constraints->max_uV = cmax;
949 /* voltage constraints are optional */
950 if ((cmin == 0) && (cmax == 0))
953 /* else require explicit machine-level constraints */
954 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
955 rdev_err(rdev, "invalid voltage constraints\n");
959 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
960 for (i = 0; i < count; i++) {
963 value = ops->list_voltage(rdev, i);
967 /* maybe adjust [min_uV..max_uV] */
968 if (value >= cmin && value < min_uV)
970 if (value <= cmax && value > max_uV)
974 /* final: [min_uV..max_uV] valid iff constraints valid */
975 if (max_uV < min_uV) {
977 "unsupportable voltage constraints %u-%uuV\n",
982 /* use regulator's subset of machine constraints */
983 if (constraints->min_uV < min_uV) {
984 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
985 constraints->min_uV, min_uV);
986 constraints->min_uV = min_uV;
988 if (constraints->max_uV > max_uV) {
989 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
990 constraints->max_uV, max_uV);
991 constraints->max_uV = max_uV;
998 static int machine_constraints_current(struct regulator_dev *rdev,
999 struct regulation_constraints *constraints)
1001 const struct regulator_ops *ops = rdev->desc->ops;
1004 if (!constraints->min_uA && !constraints->max_uA)
1007 if (constraints->min_uA > constraints->max_uA) {
1008 rdev_err(rdev, "Invalid current constraints\n");
1012 if (!ops->set_current_limit || !ops->get_current_limit) {
1013 rdev_warn(rdev, "Operation of current configuration missing\n");
1017 /* Set regulator current in constraints range */
1018 ret = ops->set_current_limit(rdev, constraints->min_uA,
1019 constraints->max_uA);
1021 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1028 static int _regulator_do_enable(struct regulator_dev *rdev);
1031 * set_machine_constraints - sets regulator constraints
1032 * @rdev: regulator source
1033 * @constraints: constraints to apply
1035 * Allows platform initialisation code to define and constrain
1036 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1037 * Constraints *must* be set by platform code in order for some
1038 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1041 static int set_machine_constraints(struct regulator_dev *rdev,
1042 const struct regulation_constraints *constraints)
1045 const struct regulator_ops *ops = rdev->desc->ops;
1048 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1051 rdev->constraints = kzalloc(sizeof(*constraints),
1053 if (!rdev->constraints)
1056 ret = machine_constraints_voltage(rdev, rdev->constraints);
1060 ret = machine_constraints_current(rdev, rdev->constraints);
1064 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1065 ret = ops->set_input_current_limit(rdev,
1066 rdev->constraints->ilim_uA);
1068 rdev_err(rdev, "failed to set input limit\n");
1073 /* do we need to setup our suspend state */
1074 if (rdev->constraints->initial_state) {
1075 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1077 rdev_err(rdev, "failed to set suspend state\n");
1082 if (rdev->constraints->initial_mode) {
1083 if (!ops->set_mode) {
1084 rdev_err(rdev, "no set_mode operation\n");
1088 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1090 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1095 /* If the constraints say the regulator should be on at this point
1096 * and we have control then make sure it is enabled.
1098 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1099 ret = _regulator_do_enable(rdev);
1100 if (ret < 0 && ret != -EINVAL) {
1101 rdev_err(rdev, "failed to enable\n");
1106 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1107 && ops->set_ramp_delay) {
1108 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1110 rdev_err(rdev, "failed to set ramp_delay\n");
1115 if (rdev->constraints->pull_down && ops->set_pull_down) {
1116 ret = ops->set_pull_down(rdev);
1118 rdev_err(rdev, "failed to set pull down\n");
1123 if (rdev->constraints->soft_start && ops->set_soft_start) {
1124 ret = ops->set_soft_start(rdev);
1126 rdev_err(rdev, "failed to set soft start\n");
1131 if (rdev->constraints->over_current_protection
1132 && ops->set_over_current_protection) {
1133 ret = ops->set_over_current_protection(rdev);
1135 rdev_err(rdev, "failed to set over current protection\n");
1140 print_constraints(rdev);
1145 * set_supply - set regulator supply regulator
1146 * @rdev: regulator name
1147 * @supply_rdev: supply regulator name
1149 * Called by platform initialisation code to set the supply regulator for this
1150 * regulator. This ensures that a regulators supply will also be enabled by the
1151 * core if it's child is enabled.
1153 static int set_supply(struct regulator_dev *rdev,
1154 struct regulator_dev *supply_rdev)
1158 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1160 if (!try_module_get(supply_rdev->owner))
1163 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1164 if (rdev->supply == NULL) {
1168 supply_rdev->open_count++;
1174 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1175 * @rdev: regulator source
1176 * @consumer_dev_name: dev_name() string for device supply applies to
1177 * @supply: symbolic name for supply
1179 * Allows platform initialisation code to map physical regulator
1180 * sources to symbolic names for supplies for use by devices. Devices
1181 * should use these symbolic names to request regulators, avoiding the
1182 * need to provide board-specific regulator names as platform data.
1184 static int set_consumer_device_supply(struct regulator_dev *rdev,
1185 const char *consumer_dev_name,
1188 struct regulator_map *node;
1194 if (consumer_dev_name != NULL)
1199 list_for_each_entry(node, ®ulator_map_list, list) {
1200 if (node->dev_name && consumer_dev_name) {
1201 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1203 } else if (node->dev_name || consumer_dev_name) {
1207 if (strcmp(node->supply, supply) != 0)
1210 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1212 dev_name(&node->regulator->dev),
1213 node->regulator->desc->name,
1215 dev_name(&rdev->dev), rdev_get_name(rdev));
1219 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1223 node->regulator = rdev;
1224 node->supply = supply;
1227 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1228 if (node->dev_name == NULL) {
1234 list_add(&node->list, ®ulator_map_list);
1238 static void unset_regulator_supplies(struct regulator_dev *rdev)
1240 struct regulator_map *node, *n;
1242 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1243 if (rdev == node->regulator) {
1244 list_del(&node->list);
1245 kfree(node->dev_name);
1251 #define REG_STR_SIZE 64
1253 static struct regulator *create_regulator(struct regulator_dev *rdev,
1255 const char *supply_name)
1257 struct regulator *regulator;
1258 char buf[REG_STR_SIZE];
1261 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1262 if (regulator == NULL)
1265 mutex_lock(&rdev->mutex);
1266 regulator->rdev = rdev;
1267 list_add(®ulator->list, &rdev->consumer_list);
1270 regulator->dev = dev;
1272 /* Add a link to the device sysfs entry */
1273 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1274 dev->kobj.name, supply_name);
1275 if (size >= REG_STR_SIZE)
1278 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1279 if (regulator->supply_name == NULL)
1282 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1285 rdev_dbg(rdev, "could not add device link %s err %d\n",
1286 dev->kobj.name, err);
1290 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1291 if (regulator->supply_name == NULL)
1295 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1297 if (!regulator->debugfs) {
1298 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1300 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1301 ®ulator->uA_load);
1302 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1303 ®ulator->min_uV);
1304 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1305 ®ulator->max_uV);
1309 * Check now if the regulator is an always on regulator - if
1310 * it is then we don't need to do nearly so much work for
1311 * enable/disable calls.
1313 if (!_regulator_can_change_status(rdev) &&
1314 _regulator_is_enabled(rdev))
1315 regulator->always_on = true;
1317 mutex_unlock(&rdev->mutex);
1320 list_del(®ulator->list);
1322 mutex_unlock(&rdev->mutex);
1326 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1328 if (rdev->constraints && rdev->constraints->enable_time)
1329 return rdev->constraints->enable_time;
1330 if (!rdev->desc->ops->enable_time)
1331 return rdev->desc->enable_time;
1332 return rdev->desc->ops->enable_time(rdev);
1335 static struct regulator_supply_alias *regulator_find_supply_alias(
1336 struct device *dev, const char *supply)
1338 struct regulator_supply_alias *map;
1340 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1341 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1347 static void regulator_supply_alias(struct device **dev, const char **supply)
1349 struct regulator_supply_alias *map;
1351 map = regulator_find_supply_alias(*dev, *supply);
1353 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1354 *supply, map->alias_supply,
1355 dev_name(map->alias_dev));
1356 *dev = map->alias_dev;
1357 *supply = map->alias_supply;
1361 static int of_node_match(struct device *dev, const void *data)
1363 return dev->of_node == data;
1366 static struct regulator_dev *of_find_regulator_by_node(struct device_node *np)
1370 dev = class_find_device(®ulator_class, NULL, np, of_node_match);
1372 return dev ? dev_to_rdev(dev) : NULL;
1375 static int regulator_match(struct device *dev, const void *data)
1377 struct regulator_dev *r = dev_to_rdev(dev);
1379 return strcmp(rdev_get_name(r), data) == 0;
1382 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1386 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1388 return dev ? dev_to_rdev(dev) : NULL;
1392 * regulator_dev_lookup - lookup a regulator device.
1393 * @dev: device for regulator "consumer".
1394 * @supply: Supply name or regulator ID.
1395 * @ret: 0 on success, -ENODEV if lookup fails permanently, -EPROBE_DEFER if
1396 * lookup could succeed in the future.
1398 * If successful, returns a struct regulator_dev that corresponds to the name
1399 * @supply and with the embedded struct device refcount incremented by one,
1400 * or NULL on failure. The refcount must be dropped by calling put_device().
1402 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1406 struct regulator_dev *r;
1407 struct device_node *node;
1408 struct regulator_map *map;
1409 const char *devname = NULL;
1411 regulator_supply_alias(&dev, &supply);
1413 /* first do a dt based lookup */
1414 if (dev && dev->of_node) {
1415 node = of_get_regulator(dev, supply);
1417 r = of_find_regulator_by_node(node);
1420 *ret = -EPROBE_DEFER;
1424 * If we couldn't even get the node then it's
1425 * not just that the device didn't register
1426 * yet, there's no node and we'll never
1433 /* if not found, try doing it non-dt way */
1435 devname = dev_name(dev);
1437 r = regulator_lookup_by_name(supply);
1441 mutex_lock(®ulator_list_mutex);
1442 list_for_each_entry(map, ®ulator_map_list, list) {
1443 /* If the mapping has a device set up it must match */
1444 if (map->dev_name &&
1445 (!devname || strcmp(map->dev_name, devname)))
1448 if (strcmp(map->supply, supply) == 0 &&
1449 get_device(&map->regulator->dev)) {
1450 mutex_unlock(®ulator_list_mutex);
1451 return map->regulator;
1454 mutex_unlock(®ulator_list_mutex);
1459 static int regulator_resolve_supply(struct regulator_dev *rdev)
1461 struct regulator_dev *r;
1462 struct device *dev = rdev->dev.parent;
1465 /* No supply to resovle? */
1466 if (!rdev->supply_name)
1469 /* Supply already resolved? */
1473 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1475 if (ret == -ENODEV) {
1477 * No supply was specified for this regulator and
1478 * there will never be one.
1483 /* Did the lookup explicitly defer for us? */
1484 if (ret == -EPROBE_DEFER)
1487 if (have_full_constraints()) {
1488 r = dummy_regulator_rdev;
1489 get_device(&r->dev);
1491 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1492 rdev->supply_name, rdev->desc->name);
1493 return -EPROBE_DEFER;
1497 /* Recursively resolve the supply of the supply */
1498 ret = regulator_resolve_supply(r);
1500 put_device(&r->dev);
1504 ret = set_supply(rdev, r);
1506 put_device(&r->dev);
1510 /* Cascade always-on state to supply */
1511 if (_regulator_is_enabled(rdev) && rdev->supply) {
1512 ret = regulator_enable(rdev->supply);
1514 _regulator_put(rdev->supply);
1522 /* Internal regulator request function */
1523 static struct regulator *_regulator_get(struct device *dev, const char *id,
1524 bool exclusive, bool allow_dummy)
1526 struct regulator_dev *rdev;
1527 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1528 const char *devname = NULL;
1532 pr_err("get() with no identifier\n");
1533 return ERR_PTR(-EINVAL);
1537 devname = dev_name(dev);
1539 if (have_full_constraints())
1542 ret = -EPROBE_DEFER;
1544 rdev = regulator_dev_lookup(dev, id, &ret);
1548 regulator = ERR_PTR(ret);
1551 * If we have return value from dev_lookup fail, we do not expect to
1552 * succeed, so, quit with appropriate error value
1554 if (ret && ret != -ENODEV)
1558 devname = "deviceless";
1561 * Assume that a regulator is physically present and enabled
1562 * even if it isn't hooked up and just provide a dummy.
1564 if (have_full_constraints() && allow_dummy) {
1565 pr_warn("%s supply %s not found, using dummy regulator\n",
1568 rdev = dummy_regulator_rdev;
1569 get_device(&rdev->dev);
1571 /* Don't log an error when called from regulator_get_optional() */
1572 } else if (!have_full_constraints() || exclusive) {
1573 dev_warn(dev, "dummy supplies not allowed\n");
1579 if (rdev->exclusive) {
1580 regulator = ERR_PTR(-EPERM);
1581 put_device(&rdev->dev);
1585 if (exclusive && rdev->open_count) {
1586 regulator = ERR_PTR(-EBUSY);
1587 put_device(&rdev->dev);
1591 ret = regulator_resolve_supply(rdev);
1593 regulator = ERR_PTR(ret);
1594 put_device(&rdev->dev);
1598 if (!try_module_get(rdev->owner)) {
1599 put_device(&rdev->dev);
1603 regulator = create_regulator(rdev, dev, id);
1604 if (regulator == NULL) {
1605 regulator = ERR_PTR(-ENOMEM);
1606 put_device(&rdev->dev);
1607 module_put(rdev->owner);
1613 rdev->exclusive = 1;
1615 ret = _regulator_is_enabled(rdev);
1617 rdev->use_count = 1;
1619 rdev->use_count = 0;
1626 * regulator_get - lookup and obtain a reference to a regulator.
1627 * @dev: device for regulator "consumer"
1628 * @id: Supply name or regulator ID.
1630 * Returns a struct regulator corresponding to the regulator producer,
1631 * or IS_ERR() condition containing errno.
1633 * Use of supply names configured via regulator_set_device_supply() is
1634 * strongly encouraged. It is recommended that the supply name used
1635 * should match the name used for the supply and/or the relevant
1636 * device pins in the datasheet.
1638 struct regulator *regulator_get(struct device *dev, const char *id)
1640 return _regulator_get(dev, id, false, true);
1642 EXPORT_SYMBOL_GPL(regulator_get);
1645 * regulator_get_exclusive - obtain exclusive access to a regulator.
1646 * @dev: device for regulator "consumer"
1647 * @id: Supply name or regulator ID.
1649 * Returns a struct regulator corresponding to the regulator producer,
1650 * or IS_ERR() condition containing errno. Other consumers will be
1651 * unable to obtain this regulator while this reference is held and the
1652 * use count for the regulator will be initialised to reflect the current
1653 * state of the regulator.
1655 * This is intended for use by consumers which cannot tolerate shared
1656 * use of the regulator such as those which need to force the
1657 * regulator off for correct operation of the hardware they are
1660 * Use of supply names configured via regulator_set_device_supply() is
1661 * strongly encouraged. It is recommended that the supply name used
1662 * should match the name used for the supply and/or the relevant
1663 * device pins in the datasheet.
1665 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1667 return _regulator_get(dev, id, true, false);
1669 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1672 * regulator_get_optional - obtain optional access to a regulator.
1673 * @dev: device for regulator "consumer"
1674 * @id: Supply name or regulator ID.
1676 * Returns a struct regulator corresponding to the regulator producer,
1677 * or IS_ERR() condition containing errno.
1679 * This is intended for use by consumers for devices which can have
1680 * some supplies unconnected in normal use, such as some MMC devices.
1681 * It can allow the regulator core to provide stub supplies for other
1682 * supplies requested using normal regulator_get() calls without
1683 * disrupting the operation of drivers that can handle absent
1686 * Use of supply names configured via regulator_set_device_supply() is
1687 * strongly encouraged. It is recommended that the supply name used
1688 * should match the name used for the supply and/or the relevant
1689 * device pins in the datasheet.
1691 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1693 return _regulator_get(dev, id, false, false);
1695 EXPORT_SYMBOL_GPL(regulator_get_optional);
1697 /* regulator_list_mutex lock held by regulator_put() */
1698 static void _regulator_put(struct regulator *regulator)
1700 struct regulator_dev *rdev;
1702 if (IS_ERR_OR_NULL(regulator))
1705 lockdep_assert_held_once(®ulator_list_mutex);
1707 rdev = regulator->rdev;
1709 debugfs_remove_recursive(regulator->debugfs);
1711 /* remove any sysfs entries */
1713 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1714 mutex_lock(&rdev->mutex);
1715 list_del(®ulator->list);
1718 rdev->exclusive = 0;
1719 put_device(&rdev->dev);
1720 mutex_unlock(&rdev->mutex);
1722 kfree(regulator->supply_name);
1725 module_put(rdev->owner);
1729 * regulator_put - "free" the regulator source
1730 * @regulator: regulator source
1732 * Note: drivers must ensure that all regulator_enable calls made on this
1733 * regulator source are balanced by regulator_disable calls prior to calling
1736 void regulator_put(struct regulator *regulator)
1738 mutex_lock(®ulator_list_mutex);
1739 _regulator_put(regulator);
1740 mutex_unlock(®ulator_list_mutex);
1742 EXPORT_SYMBOL_GPL(regulator_put);
1745 * regulator_register_supply_alias - Provide device alias for supply lookup
1747 * @dev: device that will be given as the regulator "consumer"
1748 * @id: Supply name or regulator ID
1749 * @alias_dev: device that should be used to lookup the supply
1750 * @alias_id: Supply name or regulator ID that should be used to lookup the
1753 * All lookups for id on dev will instead be conducted for alias_id on
1756 int regulator_register_supply_alias(struct device *dev, const char *id,
1757 struct device *alias_dev,
1758 const char *alias_id)
1760 struct regulator_supply_alias *map;
1762 map = regulator_find_supply_alias(dev, id);
1766 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1771 map->src_supply = id;
1772 map->alias_dev = alias_dev;
1773 map->alias_supply = alias_id;
1775 list_add(&map->list, ®ulator_supply_alias_list);
1777 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1778 id, dev_name(dev), alias_id, dev_name(alias_dev));
1782 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1785 * regulator_unregister_supply_alias - Remove device alias
1787 * @dev: device that will be given as the regulator "consumer"
1788 * @id: Supply name or regulator ID
1790 * Remove a lookup alias if one exists for id on dev.
1792 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1794 struct regulator_supply_alias *map;
1796 map = regulator_find_supply_alias(dev, id);
1798 list_del(&map->list);
1802 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1805 * regulator_bulk_register_supply_alias - register multiple aliases
1807 * @dev: device that will be given as the regulator "consumer"
1808 * @id: List of supply names or regulator IDs
1809 * @alias_dev: device that should be used to lookup the supply
1810 * @alias_id: List of supply names or regulator IDs that should be used to
1812 * @num_id: Number of aliases to register
1814 * @return 0 on success, an errno on failure.
1816 * This helper function allows drivers to register several supply
1817 * aliases in one operation. If any of the aliases cannot be
1818 * registered any aliases that were registered will be removed
1819 * before returning to the caller.
1821 int regulator_bulk_register_supply_alias(struct device *dev,
1822 const char *const *id,
1823 struct device *alias_dev,
1824 const char *const *alias_id,
1830 for (i = 0; i < num_id; ++i) {
1831 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1841 "Failed to create supply alias %s,%s -> %s,%s\n",
1842 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1845 regulator_unregister_supply_alias(dev, id[i]);
1849 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1852 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1854 * @dev: device that will be given as the regulator "consumer"
1855 * @id: List of supply names or regulator IDs
1856 * @num_id: Number of aliases to unregister
1858 * This helper function allows drivers to unregister several supply
1859 * aliases in one operation.
1861 void regulator_bulk_unregister_supply_alias(struct device *dev,
1862 const char *const *id,
1867 for (i = 0; i < num_id; ++i)
1868 regulator_unregister_supply_alias(dev, id[i]);
1870 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1873 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1874 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1875 const struct regulator_config *config)
1877 struct regulator_enable_gpio *pin;
1878 struct gpio_desc *gpiod;
1881 gpiod = gpio_to_desc(config->ena_gpio);
1883 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1884 if (pin->gpiod == gpiod) {
1885 rdev_dbg(rdev, "GPIO %d is already used\n",
1887 goto update_ena_gpio_to_rdev;
1891 ret = gpio_request_one(config->ena_gpio,
1892 GPIOF_DIR_OUT | config->ena_gpio_flags,
1893 rdev_get_name(rdev));
1897 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1899 gpio_free(config->ena_gpio);
1904 pin->ena_gpio_invert = config->ena_gpio_invert;
1905 list_add(&pin->list, ®ulator_ena_gpio_list);
1907 update_ena_gpio_to_rdev:
1908 pin->request_count++;
1909 rdev->ena_pin = pin;
1913 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1915 struct regulator_enable_gpio *pin, *n;
1920 /* Free the GPIO only in case of no use */
1921 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1922 if (pin->gpiod == rdev->ena_pin->gpiod) {
1923 if (pin->request_count <= 1) {
1924 pin->request_count = 0;
1925 gpiod_put(pin->gpiod);
1926 list_del(&pin->list);
1928 rdev->ena_pin = NULL;
1931 pin->request_count--;
1938 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1939 * @rdev: regulator_dev structure
1940 * @enable: enable GPIO at initial use?
1942 * GPIO is enabled in case of initial use. (enable_count is 0)
1943 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1945 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1947 struct regulator_enable_gpio *pin = rdev->ena_pin;
1953 /* Enable GPIO at initial use */
1954 if (pin->enable_count == 0)
1955 gpiod_set_value_cansleep(pin->gpiod,
1956 !pin->ena_gpio_invert);
1958 pin->enable_count++;
1960 if (pin->enable_count > 1) {
1961 pin->enable_count--;
1965 /* Disable GPIO if not used */
1966 if (pin->enable_count <= 1) {
1967 gpiod_set_value_cansleep(pin->gpiod,
1968 pin->ena_gpio_invert);
1969 pin->enable_count = 0;
1977 * _regulator_enable_delay - a delay helper function
1978 * @delay: time to delay in microseconds
1980 * Delay for the requested amount of time as per the guidelines in:
1982 * Documentation/timers/timers-howto.txt
1984 * The assumption here is that regulators will never be enabled in
1985 * atomic context and therefore sleeping functions can be used.
1987 static void _regulator_enable_delay(unsigned int delay)
1989 unsigned int ms = delay / 1000;
1990 unsigned int us = delay % 1000;
1994 * For small enough values, handle super-millisecond
1995 * delays in the usleep_range() call below.
2004 * Give the scheduler some room to coalesce with any other
2005 * wakeup sources. For delays shorter than 10 us, don't even
2006 * bother setting up high-resolution timers and just busy-
2010 usleep_range(us, us + 100);
2015 static int _regulator_do_enable(struct regulator_dev *rdev)
2019 /* Query before enabling in case configuration dependent. */
2020 ret = _regulator_get_enable_time(rdev);
2024 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2028 trace_regulator_enable(rdev_get_name(rdev));
2030 if (rdev->desc->off_on_delay) {
2031 /* if needed, keep a distance of off_on_delay from last time
2032 * this regulator was disabled.
2034 unsigned long start_jiffy = jiffies;
2035 unsigned long intended, max_delay, remaining;
2037 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2038 intended = rdev->last_off_jiffy + max_delay;
2040 if (time_before(start_jiffy, intended)) {
2041 /* calc remaining jiffies to deal with one-time
2043 * in case of multiple timer wrapping, either it can be
2044 * detected by out-of-range remaining, or it cannot be
2045 * detected and we gets a panelty of
2046 * _regulator_enable_delay().
2048 remaining = intended - start_jiffy;
2049 if (remaining <= max_delay)
2050 _regulator_enable_delay(
2051 jiffies_to_usecs(remaining));
2055 if (rdev->ena_pin) {
2056 if (!rdev->ena_gpio_state) {
2057 ret = regulator_ena_gpio_ctrl(rdev, true);
2060 rdev->ena_gpio_state = 1;
2062 } else if (rdev->desc->ops->enable) {
2063 ret = rdev->desc->ops->enable(rdev);
2070 /* Allow the regulator to ramp; it would be useful to extend
2071 * this for bulk operations so that the regulators can ramp
2073 trace_regulator_enable_delay(rdev_get_name(rdev));
2075 _regulator_enable_delay(delay);
2077 trace_regulator_enable_complete(rdev_get_name(rdev));
2082 /* locks held by regulator_enable() */
2083 static int _regulator_enable(struct regulator_dev *rdev)
2087 lockdep_assert_held_once(&rdev->mutex);
2089 /* check voltage and requested load before enabling */
2090 if (rdev->constraints &&
2091 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2092 drms_uA_update(rdev);
2094 if (rdev->use_count == 0) {
2095 /* The regulator may on if it's not switchable or left on */
2096 ret = _regulator_is_enabled(rdev);
2097 if (ret == -EINVAL || ret == 0) {
2098 if (!_regulator_can_change_status(rdev))
2101 ret = _regulator_do_enable(rdev);
2105 } else if (ret < 0) {
2106 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2109 /* Fallthrough on positive return values - already enabled */
2118 * regulator_enable - enable regulator output
2119 * @regulator: regulator source
2121 * Request that the regulator be enabled with the regulator output at
2122 * the predefined voltage or current value. Calls to regulator_enable()
2123 * must be balanced with calls to regulator_disable().
2125 * NOTE: the output value can be set by other drivers, boot loader or may be
2126 * hardwired in the regulator.
2128 int regulator_enable(struct regulator *regulator)
2130 struct regulator_dev *rdev = regulator->rdev;
2133 if (regulator->always_on)
2137 ret = regulator_enable(rdev->supply);
2142 mutex_lock(&rdev->mutex);
2143 ret = _regulator_enable(rdev);
2144 mutex_unlock(&rdev->mutex);
2146 if (ret != 0 && rdev->supply)
2147 regulator_disable(rdev->supply);
2151 EXPORT_SYMBOL_GPL(regulator_enable);
2153 static int _regulator_do_disable(struct regulator_dev *rdev)
2157 trace_regulator_disable(rdev_get_name(rdev));
2159 if (rdev->ena_pin) {
2160 if (rdev->ena_gpio_state) {
2161 ret = regulator_ena_gpio_ctrl(rdev, false);
2164 rdev->ena_gpio_state = 0;
2167 } else if (rdev->desc->ops->disable) {
2168 ret = rdev->desc->ops->disable(rdev);
2173 /* cares about last_off_jiffy only if off_on_delay is required by
2176 if (rdev->desc->off_on_delay)
2177 rdev->last_off_jiffy = jiffies;
2179 trace_regulator_disable_complete(rdev_get_name(rdev));
2184 /* locks held by regulator_disable() */
2185 static int _regulator_disable(struct regulator_dev *rdev)
2189 lockdep_assert_held_once(&rdev->mutex);
2191 if (WARN(rdev->use_count <= 0,
2192 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2195 /* are we the last user and permitted to disable ? */
2196 if (rdev->use_count == 1 &&
2197 (rdev->constraints && !rdev->constraints->always_on)) {
2199 /* we are last user */
2200 if (_regulator_can_change_status(rdev)) {
2201 ret = _notifier_call_chain(rdev,
2202 REGULATOR_EVENT_PRE_DISABLE,
2204 if (ret & NOTIFY_STOP_MASK)
2207 ret = _regulator_do_disable(rdev);
2209 rdev_err(rdev, "failed to disable\n");
2210 _notifier_call_chain(rdev,
2211 REGULATOR_EVENT_ABORT_DISABLE,
2215 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2219 rdev->use_count = 0;
2220 } else if (rdev->use_count > 1) {
2222 if (rdev->constraints &&
2223 (rdev->constraints->valid_ops_mask &
2224 REGULATOR_CHANGE_DRMS))
2225 drms_uA_update(rdev);
2234 * regulator_disable - disable regulator output
2235 * @regulator: regulator source
2237 * Disable the regulator output voltage or current. Calls to
2238 * regulator_enable() must be balanced with calls to
2239 * regulator_disable().
2241 * NOTE: this will only disable the regulator output if no other consumer
2242 * devices have it enabled, the regulator device supports disabling and
2243 * machine constraints permit this operation.
2245 int regulator_disable(struct regulator *regulator)
2247 struct regulator_dev *rdev = regulator->rdev;
2250 if (regulator->always_on)
2253 mutex_lock(&rdev->mutex);
2254 ret = _regulator_disable(rdev);
2255 mutex_unlock(&rdev->mutex);
2257 if (ret == 0 && rdev->supply)
2258 regulator_disable(rdev->supply);
2262 EXPORT_SYMBOL_GPL(regulator_disable);
2264 /* locks held by regulator_force_disable() */
2265 static int _regulator_force_disable(struct regulator_dev *rdev)
2269 lockdep_assert_held_once(&rdev->mutex);
2271 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2272 REGULATOR_EVENT_PRE_DISABLE, NULL);
2273 if (ret & NOTIFY_STOP_MASK)
2276 ret = _regulator_do_disable(rdev);
2278 rdev_err(rdev, "failed to force disable\n");
2279 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2280 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2284 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2285 REGULATOR_EVENT_DISABLE, NULL);
2291 * regulator_force_disable - force disable regulator output
2292 * @regulator: regulator source
2294 * Forcibly disable the regulator output voltage or current.
2295 * NOTE: this *will* disable the regulator output even if other consumer
2296 * devices have it enabled. This should be used for situations when device
2297 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2299 int regulator_force_disable(struct regulator *regulator)
2301 struct regulator_dev *rdev = regulator->rdev;
2304 mutex_lock(&rdev->mutex);
2305 regulator->uA_load = 0;
2306 ret = _regulator_force_disable(regulator->rdev);
2307 mutex_unlock(&rdev->mutex);
2310 while (rdev->open_count--)
2311 regulator_disable(rdev->supply);
2315 EXPORT_SYMBOL_GPL(regulator_force_disable);
2317 static void regulator_disable_work(struct work_struct *work)
2319 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2323 mutex_lock(&rdev->mutex);
2325 BUG_ON(!rdev->deferred_disables);
2327 count = rdev->deferred_disables;
2328 rdev->deferred_disables = 0;
2330 for (i = 0; i < count; i++) {
2331 ret = _regulator_disable(rdev);
2333 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2336 mutex_unlock(&rdev->mutex);
2339 for (i = 0; i < count; i++) {
2340 ret = regulator_disable(rdev->supply);
2343 "Supply disable failed: %d\n", ret);
2350 * regulator_disable_deferred - disable regulator output with delay
2351 * @regulator: regulator source
2352 * @ms: miliseconds until the regulator is disabled
2354 * Execute regulator_disable() on the regulator after a delay. This
2355 * is intended for use with devices that require some time to quiesce.
2357 * NOTE: this will only disable the regulator output if no other consumer
2358 * devices have it enabled, the regulator device supports disabling and
2359 * machine constraints permit this operation.
2361 int regulator_disable_deferred(struct regulator *regulator, int ms)
2363 struct regulator_dev *rdev = regulator->rdev;
2365 if (regulator->always_on)
2369 return regulator_disable(regulator);
2371 mutex_lock(&rdev->mutex);
2372 rdev->deferred_disables++;
2373 mutex_unlock(&rdev->mutex);
2375 queue_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2376 msecs_to_jiffies(ms));
2379 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2381 static int _regulator_is_enabled(struct regulator_dev *rdev)
2383 /* A GPIO control always takes precedence */
2385 return rdev->ena_gpio_state;
2387 /* If we don't know then assume that the regulator is always on */
2388 if (!rdev->desc->ops->is_enabled)
2391 return rdev->desc->ops->is_enabled(rdev);
2394 static int _regulator_list_voltage(struct regulator *regulator,
2395 unsigned selector, int lock)
2397 struct regulator_dev *rdev = regulator->rdev;
2398 const struct regulator_ops *ops = rdev->desc->ops;
2401 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2402 return rdev->desc->fixed_uV;
2404 if (ops->list_voltage) {
2405 if (selector >= rdev->desc->n_voltages)
2408 mutex_lock(&rdev->mutex);
2409 ret = ops->list_voltage(rdev, selector);
2411 mutex_unlock(&rdev->mutex);
2412 } else if (rdev->supply) {
2413 ret = _regulator_list_voltage(rdev->supply, selector, lock);
2419 if (ret < rdev->constraints->min_uV)
2421 else if (ret > rdev->constraints->max_uV)
2429 * regulator_is_enabled - is the regulator output enabled
2430 * @regulator: regulator source
2432 * Returns positive if the regulator driver backing the source/client
2433 * has requested that the device be enabled, zero if it hasn't, else a
2434 * negative errno code.
2436 * Note that the device backing this regulator handle can have multiple
2437 * users, so it might be enabled even if regulator_enable() was never
2438 * called for this particular source.
2440 int regulator_is_enabled(struct regulator *regulator)
2444 if (regulator->always_on)
2447 mutex_lock(®ulator->rdev->mutex);
2448 ret = _regulator_is_enabled(regulator->rdev);
2449 mutex_unlock(®ulator->rdev->mutex);
2453 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2456 * regulator_can_change_voltage - check if regulator can change voltage
2457 * @regulator: regulator source
2459 * Returns positive if the regulator driver backing the source/client
2460 * can change its voltage, false otherwise. Useful for detecting fixed
2461 * or dummy regulators and disabling voltage change logic in the client
2464 int regulator_can_change_voltage(struct regulator *regulator)
2466 struct regulator_dev *rdev = regulator->rdev;
2468 if (rdev->constraints &&
2469 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2470 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2473 if (rdev->desc->continuous_voltage_range &&
2474 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2475 rdev->constraints->min_uV != rdev->constraints->max_uV)
2481 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2484 * regulator_count_voltages - count regulator_list_voltage() selectors
2485 * @regulator: regulator source
2487 * Returns number of selectors, or negative errno. Selectors are
2488 * numbered starting at zero, and typically correspond to bitfields
2489 * in hardware registers.
2491 int regulator_count_voltages(struct regulator *regulator)
2493 struct regulator_dev *rdev = regulator->rdev;
2495 if (rdev->desc->n_voltages)
2496 return rdev->desc->n_voltages;
2501 return regulator_count_voltages(rdev->supply);
2503 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2506 * regulator_list_voltage - enumerate supported voltages
2507 * @regulator: regulator source
2508 * @selector: identify voltage to list
2509 * Context: can sleep
2511 * Returns a voltage that can be passed to @regulator_set_voltage(),
2512 * zero if this selector code can't be used on this system, or a
2515 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2517 return _regulator_list_voltage(regulator, selector, 1);
2519 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2522 * regulator_get_regmap - get the regulator's register map
2523 * @regulator: regulator source
2525 * Returns the register map for the given regulator, or an ERR_PTR value
2526 * if the regulator doesn't use regmap.
2528 struct regmap *regulator_get_regmap(struct regulator *regulator)
2530 struct regmap *map = regulator->rdev->regmap;
2532 return map ? map : ERR_PTR(-EOPNOTSUPP);
2536 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2537 * @regulator: regulator source
2538 * @vsel_reg: voltage selector register, output parameter
2539 * @vsel_mask: mask for voltage selector bitfield, output parameter
2541 * Returns the hardware register offset and bitmask used for setting the
2542 * regulator voltage. This might be useful when configuring voltage-scaling
2543 * hardware or firmware that can make I2C requests behind the kernel's back,
2546 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2547 * and 0 is returned, otherwise a negative errno is returned.
2549 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2551 unsigned *vsel_mask)
2553 struct regulator_dev *rdev = regulator->rdev;
2554 const struct regulator_ops *ops = rdev->desc->ops;
2556 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2559 *vsel_reg = rdev->desc->vsel_reg;
2560 *vsel_mask = rdev->desc->vsel_mask;
2564 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2567 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2568 * @regulator: regulator source
2569 * @selector: identify voltage to list
2571 * Converts the selector to a hardware-specific voltage selector that can be
2572 * directly written to the regulator registers. The address of the voltage
2573 * register can be determined by calling @regulator_get_hardware_vsel_register.
2575 * On error a negative errno is returned.
2577 int regulator_list_hardware_vsel(struct regulator *regulator,
2580 struct regulator_dev *rdev = regulator->rdev;
2581 const struct regulator_ops *ops = rdev->desc->ops;
2583 if (selector >= rdev->desc->n_voltages)
2585 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2590 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2593 * regulator_get_linear_step - return the voltage step size between VSEL values
2594 * @regulator: regulator source
2596 * Returns the voltage step size between VSEL values for linear
2597 * regulators, or return 0 if the regulator isn't a linear regulator.
2599 unsigned int regulator_get_linear_step(struct regulator *regulator)
2601 struct regulator_dev *rdev = regulator->rdev;
2603 return rdev->desc->uV_step;
2605 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2608 * regulator_is_supported_voltage - check if a voltage range can be supported
2610 * @regulator: Regulator to check.
2611 * @min_uV: Minimum required voltage in uV.
2612 * @max_uV: Maximum required voltage in uV.
2614 * Returns a boolean or a negative error code.
2616 int regulator_is_supported_voltage(struct regulator *regulator,
2617 int min_uV, int max_uV)
2619 struct regulator_dev *rdev = regulator->rdev;
2620 int i, voltages, ret;
2622 /* If we can't change voltage check the current voltage */
2623 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2624 ret = regulator_get_voltage(regulator);
2626 return min_uV <= ret && ret <= max_uV;
2631 /* Any voltage within constrains range is fine? */
2632 if (rdev->desc->continuous_voltage_range)
2633 return min_uV >= rdev->constraints->min_uV &&
2634 max_uV <= rdev->constraints->max_uV;
2636 ret = regulator_count_voltages(regulator);
2641 for (i = 0; i < voltages; i++) {
2642 ret = regulator_list_voltage(regulator, i);
2644 if (ret >= min_uV && ret <= max_uV)
2650 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2652 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2655 const struct regulator_desc *desc = rdev->desc;
2657 if (desc->ops->map_voltage)
2658 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2660 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2661 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2663 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2664 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2666 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2669 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2670 int min_uV, int max_uV,
2673 struct pre_voltage_change_data data;
2676 data.old_uV = _regulator_get_voltage(rdev);
2677 data.min_uV = min_uV;
2678 data.max_uV = max_uV;
2679 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2681 if (ret & NOTIFY_STOP_MASK)
2684 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2688 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2689 (void *)data.old_uV);
2694 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2695 int uV, unsigned selector)
2697 struct pre_voltage_change_data data;
2700 data.old_uV = _regulator_get_voltage(rdev);
2703 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2705 if (ret & NOTIFY_STOP_MASK)
2708 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2712 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2713 (void *)data.old_uV);
2718 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2719 int min_uV, int max_uV)
2724 unsigned int selector;
2725 int old_selector = -1;
2727 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2729 min_uV += rdev->constraints->uV_offset;
2730 max_uV += rdev->constraints->uV_offset;
2733 * If we can't obtain the old selector there is not enough
2734 * info to call set_voltage_time_sel().
2736 if (_regulator_is_enabled(rdev) &&
2737 rdev->desc->ops->set_voltage_time_sel &&
2738 rdev->desc->ops->get_voltage_sel) {
2739 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2740 if (old_selector < 0)
2741 return old_selector;
2744 if (rdev->desc->ops->set_voltage) {
2745 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2749 if (rdev->desc->ops->list_voltage)
2750 best_val = rdev->desc->ops->list_voltage(rdev,
2753 best_val = _regulator_get_voltage(rdev);
2756 } else if (rdev->desc->ops->set_voltage_sel) {
2757 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2759 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2760 if (min_uV <= best_val && max_uV >= best_val) {
2762 if (old_selector == selector)
2765 ret = _regulator_call_set_voltage_sel(
2766 rdev, best_val, selector);
2775 /* Call set_voltage_time_sel if successfully obtained old_selector */
2776 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2777 && old_selector != selector) {
2779 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2780 old_selector, selector);
2782 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2787 /* Insert any necessary delays */
2788 if (delay >= 1000) {
2789 mdelay(delay / 1000);
2790 udelay(delay % 1000);
2796 if (ret == 0 && best_val >= 0) {
2797 unsigned long data = best_val;
2799 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2803 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2808 static int regulator_set_voltage_unlocked(struct regulator *regulator,
2809 int min_uV, int max_uV)
2811 struct regulator_dev *rdev = regulator->rdev;
2813 int old_min_uV, old_max_uV;
2815 int best_supply_uV = 0;
2816 int supply_change_uV = 0;
2818 /* If we're setting the same range as last time the change
2819 * should be a noop (some cpufreq implementations use the same
2820 * voltage for multiple frequencies, for example).
2822 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2825 /* If we're trying to set a range that overlaps the current voltage,
2826 * return successfully even though the regulator does not support
2827 * changing the voltage.
2829 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2830 current_uV = _regulator_get_voltage(rdev);
2831 if (min_uV <= current_uV && current_uV <= max_uV) {
2832 regulator->min_uV = min_uV;
2833 regulator->max_uV = max_uV;
2839 if (!rdev->desc->ops->set_voltage &&
2840 !rdev->desc->ops->set_voltage_sel) {
2845 /* constraints check */
2846 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2850 /* restore original values in case of error */
2851 old_min_uV = regulator->min_uV;
2852 old_max_uV = regulator->max_uV;
2853 regulator->min_uV = min_uV;
2854 regulator->max_uV = max_uV;
2856 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2860 if (rdev->supply && (rdev->desc->min_dropout_uV ||
2861 !rdev->desc->ops->get_voltage)) {
2862 int current_supply_uV;
2865 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2871 best_supply_uV = _regulator_list_voltage(regulator, selector, 0);
2872 if (best_supply_uV < 0) {
2873 ret = best_supply_uV;
2877 best_supply_uV += rdev->desc->min_dropout_uV;
2879 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2880 if (current_supply_uV < 0) {
2881 ret = current_supply_uV;
2885 supply_change_uV = best_supply_uV - current_supply_uV;
2888 if (supply_change_uV > 0) {
2889 ret = regulator_set_voltage_unlocked(rdev->supply,
2890 best_supply_uV, INT_MAX);
2892 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
2898 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2902 if (supply_change_uV < 0) {
2903 ret = regulator_set_voltage_unlocked(rdev->supply,
2904 best_supply_uV, INT_MAX);
2906 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
2908 /* No need to fail here */
2915 regulator->min_uV = old_min_uV;
2916 regulator->max_uV = old_max_uV;
2922 * regulator_set_voltage - set regulator output voltage
2923 * @regulator: regulator source
2924 * @min_uV: Minimum required voltage in uV
2925 * @max_uV: Maximum acceptable voltage in uV
2927 * Sets a voltage regulator to the desired output voltage. This can be set
2928 * during any regulator state. IOW, regulator can be disabled or enabled.
2930 * If the regulator is enabled then the voltage will change to the new value
2931 * immediately otherwise if the regulator is disabled the regulator will
2932 * output at the new voltage when enabled.
2934 * NOTE: If the regulator is shared between several devices then the lowest
2935 * request voltage that meets the system constraints will be used.
2936 * Regulator system constraints must be set for this regulator before
2937 * calling this function otherwise this call will fail.
2939 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2943 regulator_lock_supply(regulator->rdev);
2945 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV);
2947 regulator_unlock_supply(regulator->rdev);
2951 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2954 * regulator_set_voltage_time - get raise/fall time
2955 * @regulator: regulator source
2956 * @old_uV: starting voltage in microvolts
2957 * @new_uV: target voltage in microvolts
2959 * Provided with the starting and ending voltage, this function attempts to
2960 * calculate the time in microseconds required to rise or fall to this new
2963 int regulator_set_voltage_time(struct regulator *regulator,
2964 int old_uV, int new_uV)
2966 struct regulator_dev *rdev = regulator->rdev;
2967 const struct regulator_ops *ops = rdev->desc->ops;
2973 /* Currently requires operations to do this */
2974 if (!ops->list_voltage || !ops->set_voltage_time_sel
2975 || !rdev->desc->n_voltages)
2978 for (i = 0; i < rdev->desc->n_voltages; i++) {
2979 /* We only look for exact voltage matches here */
2980 voltage = regulator_list_voltage(regulator, i);
2985 if (voltage == old_uV)
2987 if (voltage == new_uV)
2991 if (old_sel < 0 || new_sel < 0)
2994 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2996 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2999 * regulator_set_voltage_time_sel - get raise/fall time
3000 * @rdev: regulator source device
3001 * @old_selector: selector for starting voltage
3002 * @new_selector: selector for target voltage
3004 * Provided with the starting and target voltage selectors, this function
3005 * returns time in microseconds required to rise or fall to this new voltage
3007 * Drivers providing ramp_delay in regulation_constraints can use this as their
3008 * set_voltage_time_sel() operation.
3010 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3011 unsigned int old_selector,
3012 unsigned int new_selector)
3014 unsigned int ramp_delay = 0;
3015 int old_volt, new_volt;
3017 if (rdev->constraints->ramp_delay)
3018 ramp_delay = rdev->constraints->ramp_delay;
3019 else if (rdev->desc->ramp_delay)
3020 ramp_delay = rdev->desc->ramp_delay;
3022 if (ramp_delay == 0) {
3023 rdev_warn(rdev, "ramp_delay not set\n");
3028 if (!rdev->desc->ops->list_voltage)
3031 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3032 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3034 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
3036 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3039 * regulator_sync_voltage - re-apply last regulator output voltage
3040 * @regulator: regulator source
3042 * Re-apply the last configured voltage. This is intended to be used
3043 * where some external control source the consumer is cooperating with
3044 * has caused the configured voltage to change.
3046 int regulator_sync_voltage(struct regulator *regulator)
3048 struct regulator_dev *rdev = regulator->rdev;
3049 int ret, min_uV, max_uV;
3051 mutex_lock(&rdev->mutex);
3053 if (!rdev->desc->ops->set_voltage &&
3054 !rdev->desc->ops->set_voltage_sel) {
3059 /* This is only going to work if we've had a voltage configured. */
3060 if (!regulator->min_uV && !regulator->max_uV) {
3065 min_uV = regulator->min_uV;
3066 max_uV = regulator->max_uV;
3068 /* This should be a paranoia check... */
3069 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3073 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
3077 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3080 mutex_unlock(&rdev->mutex);
3083 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3085 static int _regulator_get_voltage(struct regulator_dev *rdev)
3089 if (rdev->desc->ops->get_voltage_sel) {
3090 sel = rdev->desc->ops->get_voltage_sel(rdev);
3093 ret = rdev->desc->ops->list_voltage(rdev, sel);
3094 } else if (rdev->desc->ops->get_voltage) {
3095 ret = rdev->desc->ops->get_voltage(rdev);
3096 } else if (rdev->desc->ops->list_voltage) {
3097 ret = rdev->desc->ops->list_voltage(rdev, 0);
3098 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3099 ret = rdev->desc->fixed_uV;
3100 } else if (rdev->supply) {
3101 ret = _regulator_get_voltage(rdev->supply->rdev);
3108 return ret - rdev->constraints->uV_offset;
3112 * regulator_get_voltage - get regulator output voltage
3113 * @regulator: regulator source
3115 * This returns the current regulator voltage in uV.
3117 * NOTE: If the regulator is disabled it will return the voltage value. This
3118 * function should not be used to determine regulator state.
3120 int regulator_get_voltage(struct regulator *regulator)
3124 regulator_lock_supply(regulator->rdev);
3126 ret = _regulator_get_voltage(regulator->rdev);
3128 regulator_unlock_supply(regulator->rdev);
3132 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3135 * regulator_set_current_limit - set regulator output current limit
3136 * @regulator: regulator source
3137 * @min_uA: Minimum supported current in uA
3138 * @max_uA: Maximum supported current in uA
3140 * Sets current sink to the desired output current. This can be set during
3141 * any regulator state. IOW, regulator can be disabled or enabled.
3143 * If the regulator is enabled then the current will change to the new value
3144 * immediately otherwise if the regulator is disabled the regulator will
3145 * output at the new current when enabled.
3147 * NOTE: Regulator system constraints must be set for this regulator before
3148 * calling this function otherwise this call will fail.
3150 int regulator_set_current_limit(struct regulator *regulator,
3151 int min_uA, int max_uA)
3153 struct regulator_dev *rdev = regulator->rdev;
3156 mutex_lock(&rdev->mutex);
3159 if (!rdev->desc->ops->set_current_limit) {
3164 /* constraints check */
3165 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3169 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3171 mutex_unlock(&rdev->mutex);
3174 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3176 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3180 mutex_lock(&rdev->mutex);
3183 if (!rdev->desc->ops->get_current_limit) {
3188 ret = rdev->desc->ops->get_current_limit(rdev);
3190 mutex_unlock(&rdev->mutex);
3195 * regulator_get_current_limit - get regulator output current
3196 * @regulator: regulator source
3198 * This returns the current supplied by the specified current sink in uA.
3200 * NOTE: If the regulator is disabled it will return the current value. This
3201 * function should not be used to determine regulator state.
3203 int regulator_get_current_limit(struct regulator *regulator)
3205 return _regulator_get_current_limit(regulator->rdev);
3207 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3210 * regulator_set_mode - set regulator operating mode
3211 * @regulator: regulator source
3212 * @mode: operating mode - one of the REGULATOR_MODE constants
3214 * Set regulator operating mode to increase regulator efficiency or improve
3215 * regulation performance.
3217 * NOTE: Regulator system constraints must be set for this regulator before
3218 * calling this function otherwise this call will fail.
3220 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3222 struct regulator_dev *rdev = regulator->rdev;
3224 int regulator_curr_mode;
3226 mutex_lock(&rdev->mutex);
3229 if (!rdev->desc->ops->set_mode) {
3234 /* return if the same mode is requested */
3235 if (rdev->desc->ops->get_mode) {
3236 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3237 if (regulator_curr_mode == mode) {
3243 /* constraints check */
3244 ret = regulator_mode_constrain(rdev, &mode);
3248 ret = rdev->desc->ops->set_mode(rdev, mode);
3250 mutex_unlock(&rdev->mutex);
3253 EXPORT_SYMBOL_GPL(regulator_set_mode);
3255 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3259 mutex_lock(&rdev->mutex);
3262 if (!rdev->desc->ops->get_mode) {
3267 ret = rdev->desc->ops->get_mode(rdev);
3269 mutex_unlock(&rdev->mutex);
3274 * regulator_get_mode - get regulator operating mode
3275 * @regulator: regulator source
3277 * Get the current regulator operating mode.
3279 unsigned int regulator_get_mode(struct regulator *regulator)
3281 return _regulator_get_mode(regulator->rdev);
3283 EXPORT_SYMBOL_GPL(regulator_get_mode);
3286 * regulator_set_load - set regulator load
3287 * @regulator: regulator source
3288 * @uA_load: load current
3290 * Notifies the regulator core of a new device load. This is then used by
3291 * DRMS (if enabled by constraints) to set the most efficient regulator
3292 * operating mode for the new regulator loading.
3294 * Consumer devices notify their supply regulator of the maximum power
3295 * they will require (can be taken from device datasheet in the power
3296 * consumption tables) when they change operational status and hence power
3297 * state. Examples of operational state changes that can affect power
3298 * consumption are :-
3300 * o Device is opened / closed.
3301 * o Device I/O is about to begin or has just finished.
3302 * o Device is idling in between work.
3304 * This information is also exported via sysfs to userspace.
3306 * DRMS will sum the total requested load on the regulator and change
3307 * to the most efficient operating mode if platform constraints allow.
3309 * On error a negative errno is returned.
3311 int regulator_set_load(struct regulator *regulator, int uA_load)
3313 struct regulator_dev *rdev = regulator->rdev;
3316 mutex_lock(&rdev->mutex);
3317 regulator->uA_load = uA_load;
3318 ret = drms_uA_update(rdev);
3319 mutex_unlock(&rdev->mutex);
3323 EXPORT_SYMBOL_GPL(regulator_set_load);
3326 * regulator_allow_bypass - allow the regulator to go into bypass mode
3328 * @regulator: Regulator to configure
3329 * @enable: enable or disable bypass mode
3331 * Allow the regulator to go into bypass mode if all other consumers
3332 * for the regulator also enable bypass mode and the machine
3333 * constraints allow this. Bypass mode means that the regulator is
3334 * simply passing the input directly to the output with no regulation.
3336 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3338 struct regulator_dev *rdev = regulator->rdev;
3341 if (!rdev->desc->ops->set_bypass)
3344 if (rdev->constraints &&
3345 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3348 mutex_lock(&rdev->mutex);
3350 if (enable && !regulator->bypass) {
3351 rdev->bypass_count++;
3353 if (rdev->bypass_count == rdev->open_count) {
3354 ret = rdev->desc->ops->set_bypass(rdev, enable);
3356 rdev->bypass_count--;
3359 } else if (!enable && regulator->bypass) {
3360 rdev->bypass_count--;
3362 if (rdev->bypass_count != rdev->open_count) {
3363 ret = rdev->desc->ops->set_bypass(rdev, enable);
3365 rdev->bypass_count++;
3370 regulator->bypass = enable;
3372 mutex_unlock(&rdev->mutex);
3376 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3379 * regulator_register_notifier - register regulator event notifier
3380 * @regulator: regulator source
3381 * @nb: notifier block
3383 * Register notifier block to receive regulator events.
3385 int regulator_register_notifier(struct regulator *regulator,
3386 struct notifier_block *nb)
3388 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3391 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3394 * regulator_unregister_notifier - unregister regulator event notifier
3395 * @regulator: regulator source
3396 * @nb: notifier block
3398 * Unregister regulator event notifier block.
3400 int regulator_unregister_notifier(struct regulator *regulator,
3401 struct notifier_block *nb)
3403 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3406 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3408 /* notify regulator consumers and downstream regulator consumers.
3409 * Note mutex must be held by caller.
3411 static int _notifier_call_chain(struct regulator_dev *rdev,
3412 unsigned long event, void *data)
3414 /* call rdev chain first */
3415 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3419 * regulator_bulk_get - get multiple regulator consumers
3421 * @dev: Device to supply
3422 * @num_consumers: Number of consumers to register
3423 * @consumers: Configuration of consumers; clients are stored here.
3425 * @return 0 on success, an errno on failure.
3427 * This helper function allows drivers to get several regulator
3428 * consumers in one operation. If any of the regulators cannot be
3429 * acquired then any regulators that were allocated will be freed
3430 * before returning to the caller.
3432 int regulator_bulk_get(struct device *dev, int num_consumers,
3433 struct regulator_bulk_data *consumers)
3438 for (i = 0; i < num_consumers; i++)
3439 consumers[i].consumer = NULL;
3441 for (i = 0; i < num_consumers; i++) {
3442 consumers[i].consumer = _regulator_get(dev,
3443 consumers[i].supply,
3445 !consumers[i].optional);
3446 if (IS_ERR(consumers[i].consumer)) {
3447 ret = PTR_ERR(consumers[i].consumer);
3448 dev_err(dev, "Failed to get supply '%s': %d\n",
3449 consumers[i].supply, ret);
3450 consumers[i].consumer = NULL;
3459 regulator_put(consumers[i].consumer);
3463 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3465 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3467 struct regulator_bulk_data *bulk = data;
3469 bulk->ret = regulator_enable(bulk->consumer);
3473 * regulator_bulk_enable - enable multiple regulator consumers
3475 * @num_consumers: Number of consumers
3476 * @consumers: Consumer data; clients are stored here.
3477 * @return 0 on success, an errno on failure
3479 * This convenience API allows consumers to enable multiple regulator
3480 * clients in a single API call. If any consumers cannot be enabled
3481 * then any others that were enabled will be disabled again prior to
3484 int regulator_bulk_enable(int num_consumers,
3485 struct regulator_bulk_data *consumers)
3487 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3491 for (i = 0; i < num_consumers; i++) {
3492 if (consumers[i].consumer->always_on)
3493 consumers[i].ret = 0;
3495 async_schedule_domain(regulator_bulk_enable_async,
3496 &consumers[i], &async_domain);
3499 async_synchronize_full_domain(&async_domain);
3501 /* If any consumer failed we need to unwind any that succeeded */
3502 for (i = 0; i < num_consumers; i++) {
3503 if (consumers[i].ret != 0) {
3504 ret = consumers[i].ret;
3512 for (i = 0; i < num_consumers; i++) {
3513 if (consumers[i].ret < 0)
3514 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3517 regulator_disable(consumers[i].consumer);
3522 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3525 * regulator_bulk_disable - disable multiple regulator consumers
3527 * @num_consumers: Number of consumers
3528 * @consumers: Consumer data; clients are stored here.
3529 * @return 0 on success, an errno on failure
3531 * This convenience API allows consumers to disable multiple regulator
3532 * clients in a single API call. If any consumers cannot be disabled
3533 * then any others that were disabled will be enabled again prior to
3536 int regulator_bulk_disable(int num_consumers,
3537 struct regulator_bulk_data *consumers)
3542 for (i = num_consumers - 1; i >= 0; --i) {
3543 ret = regulator_disable(consumers[i].consumer);
3551 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3552 for (++i; i < num_consumers; ++i) {
3553 r = regulator_enable(consumers[i].consumer);
3555 pr_err("Failed to reename %s: %d\n",
3556 consumers[i].supply, r);
3561 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3564 * regulator_bulk_force_disable - force disable multiple regulator consumers
3566 * @num_consumers: Number of consumers
3567 * @consumers: Consumer data; clients are stored here.
3568 * @return 0 on success, an errno on failure
3570 * This convenience API allows consumers to forcibly disable multiple regulator
3571 * clients in a single API call.
3572 * NOTE: This should be used for situations when device damage will
3573 * likely occur if the regulators are not disabled (e.g. over temp).
3574 * Although regulator_force_disable function call for some consumers can
3575 * return error numbers, the function is called for all consumers.
3577 int regulator_bulk_force_disable(int num_consumers,
3578 struct regulator_bulk_data *consumers)
3583 for (i = 0; i < num_consumers; i++)
3585 regulator_force_disable(consumers[i].consumer);
3587 for (i = 0; i < num_consumers; i++) {
3588 if (consumers[i].ret != 0) {
3589 ret = consumers[i].ret;
3598 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3601 * regulator_bulk_free - free multiple regulator consumers
3603 * @num_consumers: Number of consumers
3604 * @consumers: Consumer data; clients are stored here.
3606 * This convenience API allows consumers to free multiple regulator
3607 * clients in a single API call.
3609 void regulator_bulk_free(int num_consumers,
3610 struct regulator_bulk_data *consumers)
3614 for (i = 0; i < num_consumers; i++) {
3615 regulator_put(consumers[i].consumer);
3616 consumers[i].consumer = NULL;
3619 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3622 * regulator_notifier_call_chain - call regulator event notifier
3623 * @rdev: regulator source
3624 * @event: notifier block
3625 * @data: callback-specific data.
3627 * Called by regulator drivers to notify clients a regulator event has
3628 * occurred. We also notify regulator clients downstream.
3629 * Note lock must be held by caller.
3631 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3632 unsigned long event, void *data)
3634 lockdep_assert_held_once(&rdev->mutex);
3636 _notifier_call_chain(rdev, event, data);
3640 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3643 * regulator_mode_to_status - convert a regulator mode into a status
3645 * @mode: Mode to convert
3647 * Convert a regulator mode into a status.
3649 int regulator_mode_to_status(unsigned int mode)
3652 case REGULATOR_MODE_FAST:
3653 return REGULATOR_STATUS_FAST;
3654 case REGULATOR_MODE_NORMAL:
3655 return REGULATOR_STATUS_NORMAL;
3656 case REGULATOR_MODE_IDLE:
3657 return REGULATOR_STATUS_IDLE;
3658 case REGULATOR_MODE_STANDBY:
3659 return REGULATOR_STATUS_STANDBY;
3661 return REGULATOR_STATUS_UNDEFINED;
3664 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3666 static struct attribute *regulator_dev_attrs[] = {
3667 &dev_attr_name.attr,
3668 &dev_attr_num_users.attr,
3669 &dev_attr_type.attr,
3670 &dev_attr_microvolts.attr,
3671 &dev_attr_microamps.attr,
3672 &dev_attr_opmode.attr,
3673 &dev_attr_state.attr,
3674 &dev_attr_status.attr,
3675 &dev_attr_bypass.attr,
3676 &dev_attr_requested_microamps.attr,
3677 &dev_attr_min_microvolts.attr,
3678 &dev_attr_max_microvolts.attr,
3679 &dev_attr_min_microamps.attr,
3680 &dev_attr_max_microamps.attr,
3681 &dev_attr_suspend_standby_state.attr,
3682 &dev_attr_suspend_mem_state.attr,
3683 &dev_attr_suspend_disk_state.attr,
3684 &dev_attr_suspend_standby_microvolts.attr,
3685 &dev_attr_suspend_mem_microvolts.attr,
3686 &dev_attr_suspend_disk_microvolts.attr,
3687 &dev_attr_suspend_standby_mode.attr,
3688 &dev_attr_suspend_mem_mode.attr,
3689 &dev_attr_suspend_disk_mode.attr,
3694 * To avoid cluttering sysfs (and memory) with useless state, only
3695 * create attributes that can be meaningfully displayed.
3697 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3698 struct attribute *attr, int idx)
3700 struct device *dev = kobj_to_dev(kobj);
3701 struct regulator_dev *rdev = dev_to_rdev(dev);
3702 const struct regulator_ops *ops = rdev->desc->ops;
3703 umode_t mode = attr->mode;
3705 /* these three are always present */
3706 if (attr == &dev_attr_name.attr ||
3707 attr == &dev_attr_num_users.attr ||
3708 attr == &dev_attr_type.attr)
3711 /* some attributes need specific methods to be displayed */
3712 if (attr == &dev_attr_microvolts.attr) {
3713 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3714 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3715 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3716 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3721 if (attr == &dev_attr_microamps.attr)
3722 return ops->get_current_limit ? mode : 0;
3724 if (attr == &dev_attr_opmode.attr)
3725 return ops->get_mode ? mode : 0;
3727 if (attr == &dev_attr_state.attr)
3728 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3730 if (attr == &dev_attr_status.attr)
3731 return ops->get_status ? mode : 0;
3733 if (attr == &dev_attr_bypass.attr)
3734 return ops->get_bypass ? mode : 0;
3736 /* some attributes are type-specific */
3737 if (attr == &dev_attr_requested_microamps.attr)
3738 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3740 /* constraints need specific supporting methods */
3741 if (attr == &dev_attr_min_microvolts.attr ||
3742 attr == &dev_attr_max_microvolts.attr)
3743 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3745 if (attr == &dev_attr_min_microamps.attr ||
3746 attr == &dev_attr_max_microamps.attr)
3747 return ops->set_current_limit ? mode : 0;
3749 if (attr == &dev_attr_suspend_standby_state.attr ||
3750 attr == &dev_attr_suspend_mem_state.attr ||
3751 attr == &dev_attr_suspend_disk_state.attr)
3754 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3755 attr == &dev_attr_suspend_mem_microvolts.attr ||
3756 attr == &dev_attr_suspend_disk_microvolts.attr)
3757 return ops->set_suspend_voltage ? mode : 0;
3759 if (attr == &dev_attr_suspend_standby_mode.attr ||
3760 attr == &dev_attr_suspend_mem_mode.attr ||
3761 attr == &dev_attr_suspend_disk_mode.attr)
3762 return ops->set_suspend_mode ? mode : 0;
3767 static const struct attribute_group regulator_dev_group = {
3768 .attrs = regulator_dev_attrs,
3769 .is_visible = regulator_attr_is_visible,
3772 static const struct attribute_group *regulator_dev_groups[] = {
3773 ®ulator_dev_group,
3777 static void regulator_dev_release(struct device *dev)
3779 struct regulator_dev *rdev = dev_get_drvdata(dev);
3781 kfree(rdev->constraints);
3782 of_node_put(rdev->dev.of_node);
3786 static struct class regulator_class = {
3787 .name = "regulator",
3788 .dev_release = regulator_dev_release,
3789 .dev_groups = regulator_dev_groups,
3792 static void rdev_init_debugfs(struct regulator_dev *rdev)
3794 struct device *parent = rdev->dev.parent;
3795 const char *rname = rdev_get_name(rdev);
3796 char name[NAME_MAX];
3798 /* Avoid duplicate debugfs directory names */
3799 if (parent && rname == rdev->desc->name) {
3800 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3805 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3806 if (!rdev->debugfs) {
3807 rdev_warn(rdev, "Failed to create debugfs directory\n");
3811 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3813 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3815 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3816 &rdev->bypass_count);
3820 * regulator_register - register regulator
3821 * @regulator_desc: regulator to register
3822 * @cfg: runtime configuration for regulator
3824 * Called by regulator drivers to register a regulator.
3825 * Returns a valid pointer to struct regulator_dev on success
3826 * or an ERR_PTR() on error.
3828 struct regulator_dev *
3829 regulator_register(const struct regulator_desc *regulator_desc,
3830 const struct regulator_config *cfg)
3832 const struct regulation_constraints *constraints = NULL;
3833 const struct regulator_init_data *init_data;
3834 struct regulator_config *config = NULL;
3835 static atomic_t regulator_no = ATOMIC_INIT(-1);
3836 struct regulator_dev *rdev;
3840 if (regulator_desc == NULL || cfg == NULL)
3841 return ERR_PTR(-EINVAL);
3846 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3847 return ERR_PTR(-EINVAL);
3849 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3850 regulator_desc->type != REGULATOR_CURRENT)
3851 return ERR_PTR(-EINVAL);
3853 /* Only one of each should be implemented */
3854 WARN_ON(regulator_desc->ops->get_voltage &&
3855 regulator_desc->ops->get_voltage_sel);
3856 WARN_ON(regulator_desc->ops->set_voltage &&
3857 regulator_desc->ops->set_voltage_sel);
3859 /* If we're using selectors we must implement list_voltage. */
3860 if (regulator_desc->ops->get_voltage_sel &&
3861 !regulator_desc->ops->list_voltage) {
3862 return ERR_PTR(-EINVAL);
3864 if (regulator_desc->ops->set_voltage_sel &&
3865 !regulator_desc->ops->list_voltage) {
3866 return ERR_PTR(-EINVAL);
3869 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3871 return ERR_PTR(-ENOMEM);
3874 * Duplicate the config so the driver could override it after
3875 * parsing init data.
3877 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3878 if (config == NULL) {
3880 return ERR_PTR(-ENOMEM);
3883 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3884 &rdev->dev.of_node);
3886 init_data = config->init_data;
3887 rdev->dev.of_node = of_node_get(config->of_node);
3890 mutex_lock(®ulator_list_mutex);
3892 mutex_init(&rdev->mutex);
3893 rdev->reg_data = config->driver_data;
3894 rdev->owner = regulator_desc->owner;
3895 rdev->desc = regulator_desc;
3897 rdev->regmap = config->regmap;
3898 else if (dev_get_regmap(dev, NULL))
3899 rdev->regmap = dev_get_regmap(dev, NULL);
3900 else if (dev->parent)
3901 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3902 INIT_LIST_HEAD(&rdev->consumer_list);
3903 INIT_LIST_HEAD(&rdev->list);
3904 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3905 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3907 /* preform any regulator specific init */
3908 if (init_data && init_data->regulator_init) {
3909 ret = init_data->regulator_init(rdev->reg_data);
3914 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3915 gpio_is_valid(config->ena_gpio)) {
3916 ret = regulator_ena_gpio_request(rdev, config);
3918 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3919 config->ena_gpio, ret);
3924 /* register with sysfs */
3925 rdev->dev.class = ®ulator_class;
3926 rdev->dev.parent = dev;
3927 dev_set_name(&rdev->dev, "regulator.%lu",
3928 (unsigned long) atomic_inc_return(®ulator_no));
3929 ret = device_register(&rdev->dev);
3931 put_device(&rdev->dev);
3935 dev_set_drvdata(&rdev->dev, rdev);
3937 /* set regulator constraints */
3939 constraints = &init_data->constraints;
3941 ret = set_machine_constraints(rdev, constraints);
3945 if (init_data && init_data->supply_regulator)
3946 rdev->supply_name = init_data->supply_regulator;
3947 else if (regulator_desc->supply_name)
3948 rdev->supply_name = regulator_desc->supply_name;
3950 /* add consumers devices */
3952 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3953 ret = set_consumer_device_supply(rdev,
3954 init_data->consumer_supplies[i].dev_name,
3955 init_data->consumer_supplies[i].supply);
3957 dev_err(dev, "Failed to set supply %s\n",
3958 init_data->consumer_supplies[i].supply);
3959 goto unset_supplies;
3964 rdev_init_debugfs(rdev);
3966 mutex_unlock(®ulator_list_mutex);
3971 unset_regulator_supplies(rdev);
3974 regulator_ena_gpio_free(rdev);
3975 device_unregister(&rdev->dev);
3976 /* device core frees rdev */
3977 rdev = ERR_PTR(ret);
3981 regulator_ena_gpio_free(rdev);
3984 rdev = ERR_PTR(ret);
3987 EXPORT_SYMBOL_GPL(regulator_register);
3990 * regulator_unregister - unregister regulator
3991 * @rdev: regulator to unregister
3993 * Called by regulator drivers to unregister a regulator.
3995 void regulator_unregister(struct regulator_dev *rdev)
4001 while (rdev->use_count--)
4002 regulator_disable(rdev->supply);
4003 regulator_put(rdev->supply);
4005 mutex_lock(®ulator_list_mutex);
4006 debugfs_remove_recursive(rdev->debugfs);
4007 flush_work(&rdev->disable_work.work);
4008 WARN_ON(rdev->open_count);
4009 unset_regulator_supplies(rdev);
4010 list_del(&rdev->list);
4011 mutex_unlock(®ulator_list_mutex);
4012 regulator_ena_gpio_free(rdev);
4013 device_unregister(&rdev->dev);
4015 EXPORT_SYMBOL_GPL(regulator_unregister);
4017 static int _regulator_suspend_prepare(struct device *dev, void *data)
4019 struct regulator_dev *rdev = dev_to_rdev(dev);
4020 const suspend_state_t *state = data;
4023 mutex_lock(&rdev->mutex);
4024 ret = suspend_prepare(rdev, *state);
4025 mutex_unlock(&rdev->mutex);
4031 * regulator_suspend_prepare - prepare regulators for system wide suspend
4032 * @state: system suspend state
4034 * Configure each regulator with it's suspend operating parameters for state.
4035 * This will usually be called by machine suspend code prior to supending.
4037 int regulator_suspend_prepare(suspend_state_t state)
4039 /* ON is handled by regulator active state */
4040 if (state == PM_SUSPEND_ON)
4043 return class_for_each_device(®ulator_class, NULL, &state,
4044 _regulator_suspend_prepare);
4046 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
4048 static int _regulator_suspend_finish(struct device *dev, void *data)
4050 struct regulator_dev *rdev = dev_to_rdev(dev);
4053 mutex_lock(&rdev->mutex);
4054 if (rdev->use_count > 0 || rdev->constraints->always_on) {
4055 if (!_regulator_is_enabled(rdev)) {
4056 ret = _regulator_do_enable(rdev);
4059 "Failed to resume regulator %d\n",
4063 if (!have_full_constraints())
4065 if (!_regulator_is_enabled(rdev))
4068 ret = _regulator_do_disable(rdev);
4070 dev_err(dev, "Failed to suspend regulator %d\n", ret);
4073 mutex_unlock(&rdev->mutex);
4075 /* Keep processing regulators in spite of any errors */
4080 * regulator_suspend_finish - resume regulators from system wide suspend
4082 * Turn on regulators that might be turned off by regulator_suspend_prepare
4083 * and that should be turned on according to the regulators properties.
4085 int regulator_suspend_finish(void)
4087 return class_for_each_device(®ulator_class, NULL, NULL,
4088 _regulator_suspend_finish);
4090 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
4093 * regulator_has_full_constraints - the system has fully specified constraints
4095 * Calling this function will cause the regulator API to disable all
4096 * regulators which have a zero use count and don't have an always_on
4097 * constraint in a late_initcall.
4099 * The intention is that this will become the default behaviour in a
4100 * future kernel release so users are encouraged to use this facility
4103 void regulator_has_full_constraints(void)
4105 has_full_constraints = 1;
4107 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4110 * rdev_get_drvdata - get rdev regulator driver data
4113 * Get rdev regulator driver private data. This call can be used in the
4114 * regulator driver context.
4116 void *rdev_get_drvdata(struct regulator_dev *rdev)
4118 return rdev->reg_data;
4120 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4123 * regulator_get_drvdata - get regulator driver data
4124 * @regulator: regulator
4126 * Get regulator driver private data. This call can be used in the consumer
4127 * driver context when non API regulator specific functions need to be called.
4129 void *regulator_get_drvdata(struct regulator *regulator)
4131 return regulator->rdev->reg_data;
4133 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4136 * regulator_set_drvdata - set regulator driver data
4137 * @regulator: regulator
4140 void regulator_set_drvdata(struct regulator *regulator, void *data)
4142 regulator->rdev->reg_data = data;
4144 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4147 * regulator_get_id - get regulator ID
4150 int rdev_get_id(struct regulator_dev *rdev)
4152 return rdev->desc->id;
4154 EXPORT_SYMBOL_GPL(rdev_get_id);
4156 struct device *rdev_get_dev(struct regulator_dev *rdev)
4160 EXPORT_SYMBOL_GPL(rdev_get_dev);
4162 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4164 return reg_init_data->driver_data;
4166 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4168 #ifdef CONFIG_DEBUG_FS
4169 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4170 size_t count, loff_t *ppos)
4172 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4173 ssize_t len, ret = 0;
4174 struct regulator_map *map;
4179 list_for_each_entry(map, ®ulator_map_list, list) {
4180 len = snprintf(buf + ret, PAGE_SIZE - ret,
4182 rdev_get_name(map->regulator), map->dev_name,
4186 if (ret > PAGE_SIZE) {
4192 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4200 static const struct file_operations supply_map_fops = {
4201 #ifdef CONFIG_DEBUG_FS
4202 .read = supply_map_read_file,
4203 .llseek = default_llseek,
4207 #ifdef CONFIG_DEBUG_FS
4208 struct summary_data {
4210 struct regulator_dev *parent;
4214 static void regulator_summary_show_subtree(struct seq_file *s,
4215 struct regulator_dev *rdev,
4218 static int regulator_summary_show_children(struct device *dev, void *data)
4220 struct regulator_dev *rdev = dev_to_rdev(dev);
4221 struct summary_data *summary_data = data;
4223 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4224 regulator_summary_show_subtree(summary_data->s, rdev,
4225 summary_data->level + 1);
4230 static void regulator_summary_show_subtree(struct seq_file *s,
4231 struct regulator_dev *rdev,
4234 struct regulation_constraints *c;
4235 struct regulator *consumer;
4236 struct summary_data summary_data;
4241 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4243 30 - level * 3, rdev_get_name(rdev),
4244 rdev->use_count, rdev->open_count, rdev->bypass_count);
4246 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4247 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4249 c = rdev->constraints;
4251 switch (rdev->desc->type) {
4252 case REGULATOR_VOLTAGE:
4253 seq_printf(s, "%5dmV %5dmV ",
4254 c->min_uV / 1000, c->max_uV / 1000);
4256 case REGULATOR_CURRENT:
4257 seq_printf(s, "%5dmA %5dmA ",
4258 c->min_uA / 1000, c->max_uA / 1000);
4265 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4266 if (consumer->dev->class == ®ulator_class)
4269 seq_printf(s, "%*s%-*s ",
4270 (level + 1) * 3 + 1, "",
4271 30 - (level + 1) * 3, dev_name(consumer->dev));
4273 switch (rdev->desc->type) {
4274 case REGULATOR_VOLTAGE:
4275 seq_printf(s, "%37dmV %5dmV",
4276 consumer->min_uV / 1000,
4277 consumer->max_uV / 1000);
4279 case REGULATOR_CURRENT:
4287 summary_data.level = level;
4288 summary_data.parent = rdev;
4290 class_for_each_device(®ulator_class, NULL, &summary_data,
4291 regulator_summary_show_children);
4294 static int regulator_summary_show_roots(struct device *dev, void *data)
4296 struct regulator_dev *rdev = dev_to_rdev(dev);
4297 struct seq_file *s = data;
4300 regulator_summary_show_subtree(s, rdev, 0);
4305 static int regulator_summary_show(struct seq_file *s, void *data)
4307 seq_puts(s, " regulator use open bypass voltage current min max\n");
4308 seq_puts(s, "-------------------------------------------------------------------------------\n");
4310 class_for_each_device(®ulator_class, NULL, s,
4311 regulator_summary_show_roots);
4316 static int regulator_summary_open(struct inode *inode, struct file *file)
4318 return single_open(file, regulator_summary_show, inode->i_private);
4322 static const struct file_operations regulator_summary_fops = {
4323 #ifdef CONFIG_DEBUG_FS
4324 .open = regulator_summary_open,
4326 .llseek = seq_lseek,
4327 .release = single_release,
4331 static int __init regulator_init(void)
4335 ret = class_register(®ulator_class);
4337 debugfs_root = debugfs_create_dir("regulator", NULL);
4339 pr_warn("regulator: Failed to create debugfs directory\n");
4341 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4344 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4345 NULL, ®ulator_summary_fops);
4347 regulator_dummy_init();
4352 /* init early to allow our consumers to complete system booting */
4353 core_initcall(regulator_init);
4355 static int __init regulator_late_cleanup(struct device *dev, void *data)
4357 struct regulator_dev *rdev = dev_to_rdev(dev);
4358 const struct regulator_ops *ops = rdev->desc->ops;
4359 struct regulation_constraints *c = rdev->constraints;
4362 if (c && c->always_on)
4365 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4368 mutex_lock(&rdev->mutex);
4370 if (rdev->use_count)
4373 /* If we can't read the status assume it's on. */
4374 if (ops->is_enabled)
4375 enabled = ops->is_enabled(rdev);
4382 if (have_full_constraints()) {
4383 /* We log since this may kill the system if it goes
4385 rdev_info(rdev, "disabling\n");
4386 ret = _regulator_do_disable(rdev);
4388 rdev_err(rdev, "couldn't disable: %d\n", ret);
4390 /* The intention is that in future we will
4391 * assume that full constraints are provided
4392 * so warn even if we aren't going to do
4395 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4399 mutex_unlock(&rdev->mutex);
4404 static int __init regulator_init_complete(void)
4407 * Since DT doesn't provide an idiomatic mechanism for
4408 * enabling full constraints and since it's much more natural
4409 * with DT to provide them just assume that a DT enabled
4410 * system has full constraints.
4412 if (of_have_populated_dt())
4413 has_full_constraints = true;
4415 /* If we have a full configuration then disable any regulators
4416 * we have permission to change the status for and which are
4417 * not in use or always_on. This is effectively the default
4418 * for DT and ACPI as they have full constraints.
4420 class_for_each_device(®ulator_class, NULL, NULL,
4421 regulator_late_cleanup);
4425 late_initcall_sync(regulator_init_complete);