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_list);
55 static LIST_HEAD(regulator_map_list);
56 static LIST_HEAD(regulator_ena_gpio_list);
57 static LIST_HEAD(regulator_supply_alias_list);
58 static bool has_full_constraints;
60 static struct dentry *debugfs_root;
63 * struct regulator_map
65 * Used to provide symbolic supply names to devices.
67 struct regulator_map {
68 struct list_head list;
69 const char *dev_name; /* The dev_name() for the consumer */
71 struct regulator_dev *regulator;
75 * struct regulator_enable_gpio
77 * Management for shared enable GPIO pin
79 struct regulator_enable_gpio {
80 struct list_head list;
81 struct gpio_desc *gpiod;
82 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
88 * struct regulator_supply_alias
90 * Used to map lookups for a supply onto an alternative device.
92 struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
100 static int _regulator_is_enabled(struct regulator_dev *rdev);
101 static int _regulator_disable(struct regulator_dev *rdev);
102 static int _regulator_get_voltage(struct regulator_dev *rdev);
103 static int _regulator_get_current_limit(struct regulator_dev *rdev);
104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
105 static int _notifier_call_chain(struct regulator_dev *rdev,
106 unsigned long event, void *data);
107 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
109 static struct regulator *create_regulator(struct regulator_dev *rdev,
111 const char *supply_name);
112 static void _regulator_put(struct regulator *regulator);
114 static struct regulator_dev *dev_to_rdev(struct device *dev)
116 return container_of(dev, struct regulator_dev, dev);
119 static const char *rdev_get_name(struct regulator_dev *rdev)
121 if (rdev->constraints && rdev->constraints->name)
122 return rdev->constraints->name;
123 else if (rdev->desc->name)
124 return rdev->desc->name;
129 static bool have_full_constraints(void)
131 return has_full_constraints || of_have_populated_dt();
135 * of_get_regulator - get a regulator device node based on supply name
136 * @dev: Device pointer for the consumer (of regulator) device
137 * @supply: regulator supply name
139 * Extract the regulator device node corresponding to the supply name.
140 * returns the device node corresponding to the regulator if found, else
143 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
145 struct device_node *regnode = NULL;
146 char prop_name[32]; /* 32 is max size of property name */
148 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
150 snprintf(prop_name, 32, "%s-supply", supply);
151 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
154 dev_dbg(dev, "Looking up %s property in node %s failed",
155 prop_name, dev->of_node->full_name);
161 static int _regulator_can_change_status(struct regulator_dev *rdev)
163 if (!rdev->constraints)
166 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
172 /* Platform voltage constraint check */
173 static int regulator_check_voltage(struct regulator_dev *rdev,
174 int *min_uV, int *max_uV)
176 BUG_ON(*min_uV > *max_uV);
178 if (!rdev->constraints) {
179 rdev_err(rdev, "no constraints\n");
182 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
183 rdev_err(rdev, "operation not allowed\n");
187 if (*max_uV > rdev->constraints->max_uV)
188 *max_uV = rdev->constraints->max_uV;
189 if (*min_uV < rdev->constraints->min_uV)
190 *min_uV = rdev->constraints->min_uV;
192 if (*min_uV > *max_uV) {
193 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
201 /* Make sure we select a voltage that suits the needs of all
202 * regulator consumers
204 static int regulator_check_consumers(struct regulator_dev *rdev,
205 int *min_uV, int *max_uV)
207 struct regulator *regulator;
209 list_for_each_entry(regulator, &rdev->consumer_list, list) {
211 * Assume consumers that didn't say anything are OK
212 * with anything in the constraint range.
214 if (!regulator->min_uV && !regulator->max_uV)
217 if (*max_uV > regulator->max_uV)
218 *max_uV = regulator->max_uV;
219 if (*min_uV < regulator->min_uV)
220 *min_uV = regulator->min_uV;
223 if (*min_uV > *max_uV) {
224 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
232 /* current constraint check */
233 static int regulator_check_current_limit(struct regulator_dev *rdev,
234 int *min_uA, int *max_uA)
236 BUG_ON(*min_uA > *max_uA);
238 if (!rdev->constraints) {
239 rdev_err(rdev, "no constraints\n");
242 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
243 rdev_err(rdev, "operation not allowed\n");
247 if (*max_uA > rdev->constraints->max_uA)
248 *max_uA = rdev->constraints->max_uA;
249 if (*min_uA < rdev->constraints->min_uA)
250 *min_uA = rdev->constraints->min_uA;
252 if (*min_uA > *max_uA) {
253 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
261 /* operating mode constraint check */
262 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
265 case REGULATOR_MODE_FAST:
266 case REGULATOR_MODE_NORMAL:
267 case REGULATOR_MODE_IDLE:
268 case REGULATOR_MODE_STANDBY:
271 rdev_err(rdev, "invalid mode %x specified\n", *mode);
275 if (!rdev->constraints) {
276 rdev_err(rdev, "no constraints\n");
279 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
280 rdev_err(rdev, "operation not allowed\n");
284 /* The modes are bitmasks, the most power hungry modes having
285 * the lowest values. If the requested mode isn't supported
286 * try higher modes. */
288 if (rdev->constraints->valid_modes_mask & *mode)
296 /* dynamic regulator mode switching constraint check */
297 static int regulator_check_drms(struct regulator_dev *rdev)
299 if (!rdev->constraints) {
300 rdev_err(rdev, "no constraints\n");
303 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
304 rdev_dbg(rdev, "operation not allowed\n");
310 static ssize_t regulator_uV_show(struct device *dev,
311 struct device_attribute *attr, char *buf)
313 struct regulator_dev *rdev = dev_get_drvdata(dev);
316 mutex_lock(&rdev->mutex);
317 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
318 mutex_unlock(&rdev->mutex);
322 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
324 static ssize_t regulator_uA_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
327 struct regulator_dev *rdev = dev_get_drvdata(dev);
329 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
331 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
333 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
336 struct regulator_dev *rdev = dev_get_drvdata(dev);
338 return sprintf(buf, "%s\n", rdev_get_name(rdev));
340 static DEVICE_ATTR_RO(name);
342 static ssize_t regulator_print_opmode(char *buf, int mode)
345 case REGULATOR_MODE_FAST:
346 return sprintf(buf, "fast\n");
347 case REGULATOR_MODE_NORMAL:
348 return sprintf(buf, "normal\n");
349 case REGULATOR_MODE_IDLE:
350 return sprintf(buf, "idle\n");
351 case REGULATOR_MODE_STANDBY:
352 return sprintf(buf, "standby\n");
354 return sprintf(buf, "unknown\n");
357 static ssize_t regulator_opmode_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
360 struct regulator_dev *rdev = dev_get_drvdata(dev);
362 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
364 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
366 static ssize_t regulator_print_state(char *buf, int state)
369 return sprintf(buf, "enabled\n");
371 return sprintf(buf, "disabled\n");
373 return sprintf(buf, "unknown\n");
376 static ssize_t regulator_state_show(struct device *dev,
377 struct device_attribute *attr, char *buf)
379 struct regulator_dev *rdev = dev_get_drvdata(dev);
382 mutex_lock(&rdev->mutex);
383 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
384 mutex_unlock(&rdev->mutex);
388 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
390 static ssize_t regulator_status_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
393 struct regulator_dev *rdev = dev_get_drvdata(dev);
397 status = rdev->desc->ops->get_status(rdev);
402 case REGULATOR_STATUS_OFF:
405 case REGULATOR_STATUS_ON:
408 case REGULATOR_STATUS_ERROR:
411 case REGULATOR_STATUS_FAST:
414 case REGULATOR_STATUS_NORMAL:
417 case REGULATOR_STATUS_IDLE:
420 case REGULATOR_STATUS_STANDBY:
423 case REGULATOR_STATUS_BYPASS:
426 case REGULATOR_STATUS_UNDEFINED:
433 return sprintf(buf, "%s\n", label);
435 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
437 static ssize_t regulator_min_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
440 struct regulator_dev *rdev = dev_get_drvdata(dev);
442 if (!rdev->constraints)
443 return sprintf(buf, "constraint not defined\n");
445 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
447 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
449 static ssize_t regulator_max_uA_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
452 struct regulator_dev *rdev = dev_get_drvdata(dev);
454 if (!rdev->constraints)
455 return sprintf(buf, "constraint not defined\n");
457 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
459 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
461 static ssize_t regulator_min_uV_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct regulator_dev *rdev = dev_get_drvdata(dev);
466 if (!rdev->constraints)
467 return sprintf(buf, "constraint not defined\n");
469 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
471 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
473 static ssize_t regulator_max_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
476 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 if (!rdev->constraints)
479 return sprintf(buf, "constraint not defined\n");
481 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
483 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
485 static ssize_t regulator_total_uA_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
488 struct regulator_dev *rdev = dev_get_drvdata(dev);
489 struct regulator *regulator;
492 mutex_lock(&rdev->mutex);
493 list_for_each_entry(regulator, &rdev->consumer_list, list)
494 uA += regulator->uA_load;
495 mutex_unlock(&rdev->mutex);
496 return sprintf(buf, "%d\n", uA);
498 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
500 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
503 struct regulator_dev *rdev = dev_get_drvdata(dev);
504 return sprintf(buf, "%d\n", rdev->use_count);
506 static DEVICE_ATTR_RO(num_users);
508 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
511 struct regulator_dev *rdev = dev_get_drvdata(dev);
513 switch (rdev->desc->type) {
514 case REGULATOR_VOLTAGE:
515 return sprintf(buf, "voltage\n");
516 case REGULATOR_CURRENT:
517 return sprintf(buf, "current\n");
519 return sprintf(buf, "unknown\n");
521 static DEVICE_ATTR_RO(type);
523 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
526 struct regulator_dev *rdev = dev_get_drvdata(dev);
528 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
530 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
531 regulator_suspend_mem_uV_show, NULL);
533 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
536 struct regulator_dev *rdev = dev_get_drvdata(dev);
538 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
540 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
541 regulator_suspend_disk_uV_show, NULL);
543 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
546 struct regulator_dev *rdev = dev_get_drvdata(dev);
548 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
550 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
551 regulator_suspend_standby_uV_show, NULL);
553 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
556 struct regulator_dev *rdev = dev_get_drvdata(dev);
558 return regulator_print_opmode(buf,
559 rdev->constraints->state_mem.mode);
561 static DEVICE_ATTR(suspend_mem_mode, 0444,
562 regulator_suspend_mem_mode_show, NULL);
564 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
567 struct regulator_dev *rdev = dev_get_drvdata(dev);
569 return regulator_print_opmode(buf,
570 rdev->constraints->state_disk.mode);
572 static DEVICE_ATTR(suspend_disk_mode, 0444,
573 regulator_suspend_disk_mode_show, NULL);
575 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
578 struct regulator_dev *rdev = dev_get_drvdata(dev);
580 return regulator_print_opmode(buf,
581 rdev->constraints->state_standby.mode);
583 static DEVICE_ATTR(suspend_standby_mode, 0444,
584 regulator_suspend_standby_mode_show, NULL);
586 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
589 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 return regulator_print_state(buf,
592 rdev->constraints->state_mem.enabled);
594 static DEVICE_ATTR(suspend_mem_state, 0444,
595 regulator_suspend_mem_state_show, NULL);
597 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
600 struct regulator_dev *rdev = dev_get_drvdata(dev);
602 return regulator_print_state(buf,
603 rdev->constraints->state_disk.enabled);
605 static DEVICE_ATTR(suspend_disk_state, 0444,
606 regulator_suspend_disk_state_show, NULL);
608 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct regulator_dev *rdev = dev_get_drvdata(dev);
613 return regulator_print_state(buf,
614 rdev->constraints->state_standby.enabled);
616 static DEVICE_ATTR(suspend_standby_state, 0444,
617 regulator_suspend_standby_state_show, NULL);
619 static ssize_t regulator_bypass_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
622 struct regulator_dev *rdev = dev_get_drvdata(dev);
627 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
636 return sprintf(buf, "%s\n", report);
638 static DEVICE_ATTR(bypass, 0444,
639 regulator_bypass_show, NULL);
641 /* Calculate the new optimum regulator operating mode based on the new total
642 * consumer load. All locks held by caller */
643 static int drms_uA_update(struct regulator_dev *rdev)
645 struct regulator *sibling;
646 int current_uA = 0, output_uV, input_uV, err;
649 lockdep_assert_held_once(&rdev->mutex);
652 * first check to see if we can set modes at all, otherwise just
653 * tell the consumer everything is OK.
655 err = regulator_check_drms(rdev);
659 if (!rdev->desc->ops->get_optimum_mode &&
660 !rdev->desc->ops->set_load)
663 if (!rdev->desc->ops->set_mode &&
664 !rdev->desc->ops->set_load)
667 /* get output voltage */
668 output_uV = _regulator_get_voltage(rdev);
669 if (output_uV <= 0) {
670 rdev_err(rdev, "invalid output voltage found\n");
674 /* get input voltage */
677 input_uV = regulator_get_voltage(rdev->supply);
679 input_uV = rdev->constraints->input_uV;
681 rdev_err(rdev, "invalid input voltage found\n");
685 /* calc total requested load */
686 list_for_each_entry(sibling, &rdev->consumer_list, list)
687 current_uA += sibling->uA_load;
689 current_uA += rdev->constraints->system_load;
691 if (rdev->desc->ops->set_load) {
692 /* set the optimum mode for our new total regulator load */
693 err = rdev->desc->ops->set_load(rdev, current_uA);
695 rdev_err(rdev, "failed to set load %d\n", current_uA);
697 /* now get the optimum mode for our new total regulator load */
698 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
699 output_uV, current_uA);
701 /* check the new mode is allowed */
702 err = regulator_mode_constrain(rdev, &mode);
704 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
705 current_uA, input_uV, output_uV);
709 err = rdev->desc->ops->set_mode(rdev, mode);
711 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
717 static int suspend_set_state(struct regulator_dev *rdev,
718 struct regulator_state *rstate)
722 /* If we have no suspend mode configration don't set anything;
723 * only warn if the driver implements set_suspend_voltage or
724 * set_suspend_mode callback.
726 if (!rstate->enabled && !rstate->disabled) {
727 if (rdev->desc->ops->set_suspend_voltage ||
728 rdev->desc->ops->set_suspend_mode)
729 rdev_warn(rdev, "No configuration\n");
733 if (rstate->enabled && rstate->disabled) {
734 rdev_err(rdev, "invalid configuration\n");
738 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
739 ret = rdev->desc->ops->set_suspend_enable(rdev);
740 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
741 ret = rdev->desc->ops->set_suspend_disable(rdev);
742 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
746 rdev_err(rdev, "failed to enabled/disable\n");
750 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
751 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
753 rdev_err(rdev, "failed to set voltage\n");
758 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
759 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
761 rdev_err(rdev, "failed to set mode\n");
768 /* locks held by caller */
769 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
771 lockdep_assert_held_once(&rdev->mutex);
773 if (!rdev->constraints)
777 case PM_SUSPEND_STANDBY:
778 return suspend_set_state(rdev,
779 &rdev->constraints->state_standby);
781 return suspend_set_state(rdev,
782 &rdev->constraints->state_mem);
784 return suspend_set_state(rdev,
785 &rdev->constraints->state_disk);
791 static void print_constraints(struct regulator_dev *rdev)
793 struct regulation_constraints *constraints = rdev->constraints;
795 size_t len = sizeof(buf) - 1;
799 if (constraints->min_uV && constraints->max_uV) {
800 if (constraints->min_uV == constraints->max_uV)
801 count += scnprintf(buf + count, len - count, "%d mV ",
802 constraints->min_uV / 1000);
804 count += scnprintf(buf + count, len - count,
806 constraints->min_uV / 1000,
807 constraints->max_uV / 1000);
810 if (!constraints->min_uV ||
811 constraints->min_uV != constraints->max_uV) {
812 ret = _regulator_get_voltage(rdev);
814 count += scnprintf(buf + count, len - count,
815 "at %d mV ", ret / 1000);
818 if (constraints->uV_offset)
819 count += scnprintf(buf + count, len - count, "%dmV offset ",
820 constraints->uV_offset / 1000);
822 if (constraints->min_uA && constraints->max_uA) {
823 if (constraints->min_uA == constraints->max_uA)
824 count += scnprintf(buf + count, len - count, "%d mA ",
825 constraints->min_uA / 1000);
827 count += scnprintf(buf + count, len - count,
829 constraints->min_uA / 1000,
830 constraints->max_uA / 1000);
833 if (!constraints->min_uA ||
834 constraints->min_uA != constraints->max_uA) {
835 ret = _regulator_get_current_limit(rdev);
837 count += scnprintf(buf + count, len - count,
838 "at %d mA ", ret / 1000);
841 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
842 count += scnprintf(buf + count, len - count, "fast ");
843 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
844 count += scnprintf(buf + count, len - count, "normal ");
845 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
846 count += scnprintf(buf + count, len - count, "idle ");
847 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
848 count += scnprintf(buf + count, len - count, "standby");
851 scnprintf(buf, len, "no parameters");
853 rdev_dbg(rdev, "%s\n", buf);
855 if ((constraints->min_uV != constraints->max_uV) &&
856 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
858 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
861 static int machine_constraints_voltage(struct regulator_dev *rdev,
862 struct regulation_constraints *constraints)
864 const struct regulator_ops *ops = rdev->desc->ops;
867 /* do we need to apply the constraint voltage */
868 if (rdev->constraints->apply_uV &&
869 rdev->constraints->min_uV == rdev->constraints->max_uV) {
870 int current_uV = _regulator_get_voltage(rdev);
871 if (current_uV < 0) {
873 "failed to get the current voltage(%d)\n",
877 if (current_uV < rdev->constraints->min_uV ||
878 current_uV > rdev->constraints->max_uV) {
879 ret = _regulator_do_set_voltage(
880 rdev, rdev->constraints->min_uV,
881 rdev->constraints->max_uV);
884 "failed to apply %duV constraint(%d)\n",
885 rdev->constraints->min_uV, ret);
891 /* constrain machine-level voltage specs to fit
892 * the actual range supported by this regulator.
894 if (ops->list_voltage && rdev->desc->n_voltages) {
895 int count = rdev->desc->n_voltages;
897 int min_uV = INT_MAX;
898 int max_uV = INT_MIN;
899 int cmin = constraints->min_uV;
900 int cmax = constraints->max_uV;
902 /* it's safe to autoconfigure fixed-voltage supplies
903 and the constraints are used by list_voltage. */
904 if (count == 1 && !cmin) {
907 constraints->min_uV = cmin;
908 constraints->max_uV = cmax;
911 /* voltage constraints are optional */
912 if ((cmin == 0) && (cmax == 0))
915 /* else require explicit machine-level constraints */
916 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
917 rdev_err(rdev, "invalid voltage constraints\n");
921 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
922 for (i = 0; i < count; i++) {
925 value = ops->list_voltage(rdev, i);
929 /* maybe adjust [min_uV..max_uV] */
930 if (value >= cmin && value < min_uV)
932 if (value <= cmax && value > max_uV)
936 /* final: [min_uV..max_uV] valid iff constraints valid */
937 if (max_uV < min_uV) {
939 "unsupportable voltage constraints %u-%uuV\n",
944 /* use regulator's subset of machine constraints */
945 if (constraints->min_uV < min_uV) {
946 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
947 constraints->min_uV, min_uV);
948 constraints->min_uV = min_uV;
950 if (constraints->max_uV > max_uV) {
951 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
952 constraints->max_uV, max_uV);
953 constraints->max_uV = max_uV;
960 static int machine_constraints_current(struct regulator_dev *rdev,
961 struct regulation_constraints *constraints)
963 const struct regulator_ops *ops = rdev->desc->ops;
966 if (!constraints->min_uA && !constraints->max_uA)
969 if (constraints->min_uA > constraints->max_uA) {
970 rdev_err(rdev, "Invalid current constraints\n");
974 if (!ops->set_current_limit || !ops->get_current_limit) {
975 rdev_warn(rdev, "Operation of current configuration missing\n");
979 /* Set regulator current in constraints range */
980 ret = ops->set_current_limit(rdev, constraints->min_uA,
981 constraints->max_uA);
983 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
990 static int _regulator_do_enable(struct regulator_dev *rdev);
993 * set_machine_constraints - sets regulator constraints
994 * @rdev: regulator source
995 * @constraints: constraints to apply
997 * Allows platform initialisation code to define and constrain
998 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
999 * Constraints *must* be set by platform code in order for some
1000 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1003 static int set_machine_constraints(struct regulator_dev *rdev,
1004 const struct regulation_constraints *constraints)
1007 const struct regulator_ops *ops = rdev->desc->ops;
1010 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1013 rdev->constraints = kzalloc(sizeof(*constraints),
1015 if (!rdev->constraints)
1018 ret = machine_constraints_voltage(rdev, rdev->constraints);
1022 ret = machine_constraints_current(rdev, rdev->constraints);
1026 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1027 ret = ops->set_input_current_limit(rdev,
1028 rdev->constraints->ilim_uA);
1030 rdev_err(rdev, "failed to set input limit\n");
1035 /* do we need to setup our suspend state */
1036 if (rdev->constraints->initial_state) {
1037 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
1039 rdev_err(rdev, "failed to set suspend state\n");
1044 if (rdev->constraints->initial_mode) {
1045 if (!ops->set_mode) {
1046 rdev_err(rdev, "no set_mode operation\n");
1051 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1053 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1058 /* If the constraints say the regulator should be on at this point
1059 * and we have control then make sure it is enabled.
1061 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1062 ret = _regulator_do_enable(rdev);
1063 if (ret < 0 && ret != -EINVAL) {
1064 rdev_err(rdev, "failed to enable\n");
1069 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1070 && ops->set_ramp_delay) {
1071 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1073 rdev_err(rdev, "failed to set ramp_delay\n");
1078 if (rdev->constraints->pull_down && ops->set_pull_down) {
1079 ret = ops->set_pull_down(rdev);
1081 rdev_err(rdev, "failed to set pull down\n");
1086 if (rdev->constraints->soft_start && ops->set_soft_start) {
1087 ret = ops->set_soft_start(rdev);
1089 rdev_err(rdev, "failed to set soft start\n");
1094 if (rdev->constraints->over_current_protection
1095 && ops->set_over_current_protection) {
1096 ret = ops->set_over_current_protection(rdev);
1098 rdev_err(rdev, "failed to set over current protection\n");
1103 print_constraints(rdev);
1106 kfree(rdev->constraints);
1107 rdev->constraints = NULL;
1112 * set_supply - set regulator supply regulator
1113 * @rdev: regulator name
1114 * @supply_rdev: supply regulator name
1116 * Called by platform initialisation code to set the supply regulator for this
1117 * regulator. This ensures that a regulators supply will also be enabled by the
1118 * core if it's child is enabled.
1120 static int set_supply(struct regulator_dev *rdev,
1121 struct regulator_dev *supply_rdev)
1125 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1127 if (!try_module_get(supply_rdev->owner))
1130 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1131 if (rdev->supply == NULL) {
1135 supply_rdev->open_count++;
1141 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1142 * @rdev: regulator source
1143 * @consumer_dev_name: dev_name() string for device supply applies to
1144 * @supply: symbolic name for supply
1146 * Allows platform initialisation code to map physical regulator
1147 * sources to symbolic names for supplies for use by devices. Devices
1148 * should use these symbolic names to request regulators, avoiding the
1149 * need to provide board-specific regulator names as platform data.
1151 static int set_consumer_device_supply(struct regulator_dev *rdev,
1152 const char *consumer_dev_name,
1155 struct regulator_map *node;
1161 if (consumer_dev_name != NULL)
1166 list_for_each_entry(node, ®ulator_map_list, list) {
1167 if (node->dev_name && consumer_dev_name) {
1168 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1170 } else if (node->dev_name || consumer_dev_name) {
1174 if (strcmp(node->supply, supply) != 0)
1177 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1179 dev_name(&node->regulator->dev),
1180 node->regulator->desc->name,
1182 dev_name(&rdev->dev), rdev_get_name(rdev));
1186 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1190 node->regulator = rdev;
1191 node->supply = supply;
1194 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1195 if (node->dev_name == NULL) {
1201 list_add(&node->list, ®ulator_map_list);
1205 static void unset_regulator_supplies(struct regulator_dev *rdev)
1207 struct regulator_map *node, *n;
1209 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1210 if (rdev == node->regulator) {
1211 list_del(&node->list);
1212 kfree(node->dev_name);
1218 #define REG_STR_SIZE 64
1220 static struct regulator *create_regulator(struct regulator_dev *rdev,
1222 const char *supply_name)
1224 struct regulator *regulator;
1225 char buf[REG_STR_SIZE];
1228 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1229 if (regulator == NULL)
1232 mutex_lock(&rdev->mutex);
1233 regulator->rdev = rdev;
1234 list_add(®ulator->list, &rdev->consumer_list);
1237 regulator->dev = dev;
1239 /* Add a link to the device sysfs entry */
1240 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1241 dev->kobj.name, supply_name);
1242 if (size >= REG_STR_SIZE)
1245 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1246 if (regulator->supply_name == NULL)
1249 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1252 rdev_dbg(rdev, "could not add device link %s err %d\n",
1253 dev->kobj.name, err);
1257 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1258 if (regulator->supply_name == NULL)
1262 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1264 if (!regulator->debugfs) {
1265 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1267 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1268 ®ulator->uA_load);
1269 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1270 ®ulator->min_uV);
1271 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1272 ®ulator->max_uV);
1276 * Check now if the regulator is an always on regulator - if
1277 * it is then we don't need to do nearly so much work for
1278 * enable/disable calls.
1280 if (!_regulator_can_change_status(rdev) &&
1281 _regulator_is_enabled(rdev))
1282 regulator->always_on = true;
1284 mutex_unlock(&rdev->mutex);
1287 list_del(®ulator->list);
1289 mutex_unlock(&rdev->mutex);
1293 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1295 if (rdev->constraints && rdev->constraints->enable_time)
1296 return rdev->constraints->enable_time;
1297 if (!rdev->desc->ops->enable_time)
1298 return rdev->desc->enable_time;
1299 return rdev->desc->ops->enable_time(rdev);
1302 static struct regulator_supply_alias *regulator_find_supply_alias(
1303 struct device *dev, const char *supply)
1305 struct regulator_supply_alias *map;
1307 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1308 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1314 static void regulator_supply_alias(struct device **dev, const char **supply)
1316 struct regulator_supply_alias *map;
1318 map = regulator_find_supply_alias(*dev, *supply);
1320 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1321 *supply, map->alias_supply,
1322 dev_name(map->alias_dev));
1323 *dev = map->alias_dev;
1324 *supply = map->alias_supply;
1328 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1332 struct regulator_dev *r;
1333 struct device_node *node;
1334 struct regulator_map *map;
1335 const char *devname = NULL;
1337 regulator_supply_alias(&dev, &supply);
1339 /* first do a dt based lookup */
1340 if (dev && dev->of_node) {
1341 node = of_get_regulator(dev, supply);
1343 list_for_each_entry(r, ®ulator_list, list)
1344 if (r->dev.parent &&
1345 node == r->dev.of_node)
1347 *ret = -EPROBE_DEFER;
1351 * If we couldn't even get the node then it's
1352 * not just that the device didn't register
1353 * yet, there's no node and we'll never
1360 /* if not found, try doing it non-dt way */
1362 devname = dev_name(dev);
1364 list_for_each_entry(r, ®ulator_list, list)
1365 if (strcmp(rdev_get_name(r), supply) == 0)
1368 list_for_each_entry(map, ®ulator_map_list, list) {
1369 /* If the mapping has a device set up it must match */
1370 if (map->dev_name &&
1371 (!devname || strcmp(map->dev_name, devname)))
1374 if (strcmp(map->supply, supply) == 0)
1375 return map->regulator;
1382 static int regulator_resolve_supply(struct regulator_dev *rdev)
1384 struct regulator_dev *r;
1385 struct device *dev = rdev->dev.parent;
1388 /* No supply to resovle? */
1389 if (!rdev->supply_name)
1392 /* Supply already resolved? */
1396 r = regulator_dev_lookup(dev, rdev->supply_name, &ret);
1398 if (ret == -ENODEV) {
1400 * No supply was specified for this regulator and
1401 * there will never be one.
1406 if (have_full_constraints()) {
1407 r = dummy_regulator_rdev;
1409 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1410 rdev->supply_name, rdev->desc->name);
1411 return -EPROBE_DEFER;
1415 /* Recursively resolve the supply of the supply */
1416 ret = regulator_resolve_supply(r);
1420 ret = set_supply(rdev, r);
1424 /* Cascade always-on state to supply */
1425 if (_regulator_is_enabled(rdev) && rdev->supply) {
1426 ret = regulator_enable(rdev->supply);
1428 _regulator_put(rdev->supply);
1436 /* Internal regulator request function */
1437 static struct regulator *_regulator_get(struct device *dev, const char *id,
1438 bool exclusive, bool allow_dummy)
1440 struct regulator_dev *rdev;
1441 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1442 const char *devname = NULL;
1446 pr_err("get() with no identifier\n");
1447 return ERR_PTR(-EINVAL);
1451 devname = dev_name(dev);
1453 if (have_full_constraints())
1456 ret = -EPROBE_DEFER;
1458 mutex_lock(®ulator_list_mutex);
1460 rdev = regulator_dev_lookup(dev, id, &ret);
1464 regulator = ERR_PTR(ret);
1467 * If we have return value from dev_lookup fail, we do not expect to
1468 * succeed, so, quit with appropriate error value
1470 if (ret && ret != -ENODEV)
1474 devname = "deviceless";
1477 * Assume that a regulator is physically present and enabled
1478 * even if it isn't hooked up and just provide a dummy.
1480 if (have_full_constraints() && allow_dummy) {
1481 pr_warn("%s supply %s not found, using dummy regulator\n",
1484 rdev = dummy_regulator_rdev;
1486 /* Don't log an error when called from regulator_get_optional() */
1487 } else if (!have_full_constraints() || exclusive) {
1488 dev_warn(dev, "dummy supplies not allowed\n");
1491 mutex_unlock(®ulator_list_mutex);
1495 if (rdev->exclusive) {
1496 regulator = ERR_PTR(-EPERM);
1500 if (exclusive && rdev->open_count) {
1501 regulator = ERR_PTR(-EBUSY);
1505 ret = regulator_resolve_supply(rdev);
1507 regulator = ERR_PTR(ret);
1511 if (!try_module_get(rdev->owner))
1514 regulator = create_regulator(rdev, dev, id);
1515 if (regulator == NULL) {
1516 regulator = ERR_PTR(-ENOMEM);
1517 module_put(rdev->owner);
1523 rdev->exclusive = 1;
1525 ret = _regulator_is_enabled(rdev);
1527 rdev->use_count = 1;
1529 rdev->use_count = 0;
1533 mutex_unlock(®ulator_list_mutex);
1539 * regulator_get - lookup and obtain a reference to a regulator.
1540 * @dev: device for regulator "consumer"
1541 * @id: Supply name or regulator ID.
1543 * Returns a struct regulator corresponding to the regulator producer,
1544 * or IS_ERR() condition containing errno.
1546 * Use of supply names configured via regulator_set_device_supply() is
1547 * strongly encouraged. It is recommended that the supply name used
1548 * should match the name used for the supply and/or the relevant
1549 * device pins in the datasheet.
1551 struct regulator *regulator_get(struct device *dev, const char *id)
1553 return _regulator_get(dev, id, false, true);
1555 EXPORT_SYMBOL_GPL(regulator_get);
1558 * regulator_get_exclusive - obtain exclusive access to a regulator.
1559 * @dev: device for regulator "consumer"
1560 * @id: Supply name or regulator ID.
1562 * Returns a struct regulator corresponding to the regulator producer,
1563 * or IS_ERR() condition containing errno. Other consumers will be
1564 * unable to obtain this regulator while this reference is held and the
1565 * use count for the regulator will be initialised to reflect the current
1566 * state of the regulator.
1568 * This is intended for use by consumers which cannot tolerate shared
1569 * use of the regulator such as those which need to force the
1570 * regulator off for correct operation of the hardware they are
1573 * Use of supply names configured via regulator_set_device_supply() is
1574 * strongly encouraged. It is recommended that the supply name used
1575 * should match the name used for the supply and/or the relevant
1576 * device pins in the datasheet.
1578 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1580 return _regulator_get(dev, id, true, false);
1582 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1585 * regulator_get_optional - obtain optional access to a regulator.
1586 * @dev: device for regulator "consumer"
1587 * @id: Supply name or regulator ID.
1589 * Returns a struct regulator corresponding to the regulator producer,
1590 * or IS_ERR() condition containing errno.
1592 * This is intended for use by consumers for devices which can have
1593 * some supplies unconnected in normal use, such as some MMC devices.
1594 * It can allow the regulator core to provide stub supplies for other
1595 * supplies requested using normal regulator_get() calls without
1596 * disrupting the operation of drivers that can handle absent
1599 * Use of supply names configured via regulator_set_device_supply() is
1600 * strongly encouraged. It is recommended that the supply name used
1601 * should match the name used for the supply and/or the relevant
1602 * device pins in the datasheet.
1604 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1606 return _regulator_get(dev, id, false, false);
1608 EXPORT_SYMBOL_GPL(regulator_get_optional);
1610 /* regulator_list_mutex lock held by regulator_put() */
1611 static void _regulator_put(struct regulator *regulator)
1613 struct regulator_dev *rdev;
1615 if (IS_ERR_OR_NULL(regulator))
1618 lockdep_assert_held_once(®ulator_list_mutex);
1620 rdev = regulator->rdev;
1622 debugfs_remove_recursive(regulator->debugfs);
1624 /* remove any sysfs entries */
1626 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1627 mutex_lock(&rdev->mutex);
1628 list_del(®ulator->list);
1631 rdev->exclusive = 0;
1632 mutex_unlock(&rdev->mutex);
1634 kfree(regulator->supply_name);
1637 module_put(rdev->owner);
1641 * regulator_put - "free" the regulator source
1642 * @regulator: regulator source
1644 * Note: drivers must ensure that all regulator_enable calls made on this
1645 * regulator source are balanced by regulator_disable calls prior to calling
1648 void regulator_put(struct regulator *regulator)
1650 mutex_lock(®ulator_list_mutex);
1651 _regulator_put(regulator);
1652 mutex_unlock(®ulator_list_mutex);
1654 EXPORT_SYMBOL_GPL(regulator_put);
1657 * regulator_register_supply_alias - Provide device alias for supply lookup
1659 * @dev: device that will be given as the regulator "consumer"
1660 * @id: Supply name or regulator ID
1661 * @alias_dev: device that should be used to lookup the supply
1662 * @alias_id: Supply name or regulator ID that should be used to lookup the
1665 * All lookups for id on dev will instead be conducted for alias_id on
1668 int regulator_register_supply_alias(struct device *dev, const char *id,
1669 struct device *alias_dev,
1670 const char *alias_id)
1672 struct regulator_supply_alias *map;
1674 map = regulator_find_supply_alias(dev, id);
1678 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1683 map->src_supply = id;
1684 map->alias_dev = alias_dev;
1685 map->alias_supply = alias_id;
1687 list_add(&map->list, ®ulator_supply_alias_list);
1689 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1690 id, dev_name(dev), alias_id, dev_name(alias_dev));
1694 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1697 * regulator_unregister_supply_alias - Remove device alias
1699 * @dev: device that will be given as the regulator "consumer"
1700 * @id: Supply name or regulator ID
1702 * Remove a lookup alias if one exists for id on dev.
1704 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1706 struct regulator_supply_alias *map;
1708 map = regulator_find_supply_alias(dev, id);
1710 list_del(&map->list);
1714 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1717 * regulator_bulk_register_supply_alias - register multiple aliases
1719 * @dev: device that will be given as the regulator "consumer"
1720 * @id: List of supply names or regulator IDs
1721 * @alias_dev: device that should be used to lookup the supply
1722 * @alias_id: List of supply names or regulator IDs that should be used to
1724 * @num_id: Number of aliases to register
1726 * @return 0 on success, an errno on failure.
1728 * This helper function allows drivers to register several supply
1729 * aliases in one operation. If any of the aliases cannot be
1730 * registered any aliases that were registered will be removed
1731 * before returning to the caller.
1733 int regulator_bulk_register_supply_alias(struct device *dev,
1734 const char *const *id,
1735 struct device *alias_dev,
1736 const char *const *alias_id,
1742 for (i = 0; i < num_id; ++i) {
1743 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1753 "Failed to create supply alias %s,%s -> %s,%s\n",
1754 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1757 regulator_unregister_supply_alias(dev, id[i]);
1761 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1764 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1766 * @dev: device that will be given as the regulator "consumer"
1767 * @id: List of supply names or regulator IDs
1768 * @num_id: Number of aliases to unregister
1770 * This helper function allows drivers to unregister several supply
1771 * aliases in one operation.
1773 void regulator_bulk_unregister_supply_alias(struct device *dev,
1774 const char *const *id,
1779 for (i = 0; i < num_id; ++i)
1780 regulator_unregister_supply_alias(dev, id[i]);
1782 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1785 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1786 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1787 const struct regulator_config *config)
1789 struct regulator_enable_gpio *pin;
1790 struct gpio_desc *gpiod;
1793 gpiod = gpio_to_desc(config->ena_gpio);
1795 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1796 if (pin->gpiod == gpiod) {
1797 rdev_dbg(rdev, "GPIO %d is already used\n",
1799 goto update_ena_gpio_to_rdev;
1803 ret = gpio_request_one(config->ena_gpio,
1804 GPIOF_DIR_OUT | config->ena_gpio_flags,
1805 rdev_get_name(rdev));
1809 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1811 gpio_free(config->ena_gpio);
1816 pin->ena_gpio_invert = config->ena_gpio_invert;
1817 list_add(&pin->list, ®ulator_ena_gpio_list);
1819 update_ena_gpio_to_rdev:
1820 pin->request_count++;
1821 rdev->ena_pin = pin;
1825 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1827 struct regulator_enable_gpio *pin, *n;
1832 /* Free the GPIO only in case of no use */
1833 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1834 if (pin->gpiod == rdev->ena_pin->gpiod) {
1835 if (pin->request_count <= 1) {
1836 pin->request_count = 0;
1837 gpiod_put(pin->gpiod);
1838 list_del(&pin->list);
1840 rdev->ena_pin = NULL;
1843 pin->request_count--;
1850 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1851 * @rdev: regulator_dev structure
1852 * @enable: enable GPIO at initial use?
1854 * GPIO is enabled in case of initial use. (enable_count is 0)
1855 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1857 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1859 struct regulator_enable_gpio *pin = rdev->ena_pin;
1865 /* Enable GPIO at initial use */
1866 if (pin->enable_count == 0)
1867 gpiod_set_value_cansleep(pin->gpiod,
1868 !pin->ena_gpio_invert);
1870 pin->enable_count++;
1872 if (pin->enable_count > 1) {
1873 pin->enable_count--;
1877 /* Disable GPIO if not used */
1878 if (pin->enable_count <= 1) {
1879 gpiod_set_value_cansleep(pin->gpiod,
1880 pin->ena_gpio_invert);
1881 pin->enable_count = 0;
1889 * _regulator_enable_delay - a delay helper function
1890 * @delay: time to delay in microseconds
1892 * Delay for the requested amount of time as per the guidelines in:
1894 * Documentation/timers/timers-howto.txt
1896 * The assumption here is that regulators will never be enabled in
1897 * atomic context and therefore sleeping functions can be used.
1899 static void _regulator_enable_delay(unsigned int delay)
1901 unsigned int ms = delay / 1000;
1902 unsigned int us = delay % 1000;
1906 * For small enough values, handle super-millisecond
1907 * delays in the usleep_range() call below.
1916 * Give the scheduler some room to coalesce with any other
1917 * wakeup sources. For delays shorter than 10 us, don't even
1918 * bother setting up high-resolution timers and just busy-
1922 usleep_range(us, us + 100);
1927 static int _regulator_do_enable(struct regulator_dev *rdev)
1931 /* Query before enabling in case configuration dependent. */
1932 ret = _regulator_get_enable_time(rdev);
1936 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1940 trace_regulator_enable(rdev_get_name(rdev));
1942 if (rdev->desc->off_on_delay) {
1943 /* if needed, keep a distance of off_on_delay from last time
1944 * this regulator was disabled.
1946 unsigned long start_jiffy = jiffies;
1947 unsigned long intended, max_delay, remaining;
1949 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
1950 intended = rdev->last_off_jiffy + max_delay;
1952 if (time_before(start_jiffy, intended)) {
1953 /* calc remaining jiffies to deal with one-time
1955 * in case of multiple timer wrapping, either it can be
1956 * detected by out-of-range remaining, or it cannot be
1957 * detected and we gets a panelty of
1958 * _regulator_enable_delay().
1960 remaining = intended - start_jiffy;
1961 if (remaining <= max_delay)
1962 _regulator_enable_delay(
1963 jiffies_to_usecs(remaining));
1967 if (rdev->ena_pin) {
1968 if (!rdev->ena_gpio_state) {
1969 ret = regulator_ena_gpio_ctrl(rdev, true);
1972 rdev->ena_gpio_state = 1;
1974 } else if (rdev->desc->ops->enable) {
1975 ret = rdev->desc->ops->enable(rdev);
1982 /* Allow the regulator to ramp; it would be useful to extend
1983 * this for bulk operations so that the regulators can ramp
1985 trace_regulator_enable_delay(rdev_get_name(rdev));
1987 _regulator_enable_delay(delay);
1989 trace_regulator_enable_complete(rdev_get_name(rdev));
1994 /* locks held by regulator_enable() */
1995 static int _regulator_enable(struct regulator_dev *rdev)
1999 lockdep_assert_held_once(&rdev->mutex);
2001 /* check voltage and requested load before enabling */
2002 if (rdev->constraints &&
2003 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
2004 drms_uA_update(rdev);
2006 if (rdev->use_count == 0) {
2007 /* The regulator may on if it's not switchable or left on */
2008 ret = _regulator_is_enabled(rdev);
2009 if (ret == -EINVAL || ret == 0) {
2010 if (!_regulator_can_change_status(rdev))
2013 ret = _regulator_do_enable(rdev);
2017 } else if (ret < 0) {
2018 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2021 /* Fallthrough on positive return values - already enabled */
2030 * regulator_enable - enable regulator output
2031 * @regulator: regulator source
2033 * Request that the regulator be enabled with the regulator output at
2034 * the predefined voltage or current value. Calls to regulator_enable()
2035 * must be balanced with calls to regulator_disable().
2037 * NOTE: the output value can be set by other drivers, boot loader or may be
2038 * hardwired in the regulator.
2040 int regulator_enable(struct regulator *regulator)
2042 struct regulator_dev *rdev = regulator->rdev;
2045 if (regulator->always_on)
2049 ret = regulator_enable(rdev->supply);
2054 mutex_lock(&rdev->mutex);
2055 ret = _regulator_enable(rdev);
2056 mutex_unlock(&rdev->mutex);
2058 if (ret != 0 && rdev->supply)
2059 regulator_disable(rdev->supply);
2063 EXPORT_SYMBOL_GPL(regulator_enable);
2065 static int _regulator_do_disable(struct regulator_dev *rdev)
2069 trace_regulator_disable(rdev_get_name(rdev));
2071 if (rdev->ena_pin) {
2072 if (rdev->ena_gpio_state) {
2073 ret = regulator_ena_gpio_ctrl(rdev, false);
2076 rdev->ena_gpio_state = 0;
2079 } else if (rdev->desc->ops->disable) {
2080 ret = rdev->desc->ops->disable(rdev);
2085 /* cares about last_off_jiffy only if off_on_delay is required by
2088 if (rdev->desc->off_on_delay)
2089 rdev->last_off_jiffy = jiffies;
2091 trace_regulator_disable_complete(rdev_get_name(rdev));
2096 /* locks held by regulator_disable() */
2097 static int _regulator_disable(struct regulator_dev *rdev)
2101 lockdep_assert_held_once(&rdev->mutex);
2103 if (WARN(rdev->use_count <= 0,
2104 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2107 /* are we the last user and permitted to disable ? */
2108 if (rdev->use_count == 1 &&
2109 (rdev->constraints && !rdev->constraints->always_on)) {
2111 /* we are last user */
2112 if (_regulator_can_change_status(rdev)) {
2113 ret = _notifier_call_chain(rdev,
2114 REGULATOR_EVENT_PRE_DISABLE,
2116 if (ret & NOTIFY_STOP_MASK)
2119 ret = _regulator_do_disable(rdev);
2121 rdev_err(rdev, "failed to disable\n");
2122 _notifier_call_chain(rdev,
2123 REGULATOR_EVENT_ABORT_DISABLE,
2127 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2131 rdev->use_count = 0;
2132 } else if (rdev->use_count > 1) {
2134 if (rdev->constraints &&
2135 (rdev->constraints->valid_ops_mask &
2136 REGULATOR_CHANGE_DRMS))
2137 drms_uA_update(rdev);
2146 * regulator_disable - disable regulator output
2147 * @regulator: regulator source
2149 * Disable the regulator output voltage or current. Calls to
2150 * regulator_enable() must be balanced with calls to
2151 * regulator_disable().
2153 * NOTE: this will only disable the regulator output if no other consumer
2154 * devices have it enabled, the regulator device supports disabling and
2155 * machine constraints permit this operation.
2157 int regulator_disable(struct regulator *regulator)
2159 struct regulator_dev *rdev = regulator->rdev;
2162 if (regulator->always_on)
2165 mutex_lock(&rdev->mutex);
2166 ret = _regulator_disable(rdev);
2167 mutex_unlock(&rdev->mutex);
2169 if (ret == 0 && rdev->supply)
2170 regulator_disable(rdev->supply);
2174 EXPORT_SYMBOL_GPL(regulator_disable);
2176 /* locks held by regulator_force_disable() */
2177 static int _regulator_force_disable(struct regulator_dev *rdev)
2181 lockdep_assert_held_once(&rdev->mutex);
2183 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2184 REGULATOR_EVENT_PRE_DISABLE, NULL);
2185 if (ret & NOTIFY_STOP_MASK)
2188 ret = _regulator_do_disable(rdev);
2190 rdev_err(rdev, "failed to force disable\n");
2191 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2192 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2196 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2197 REGULATOR_EVENT_DISABLE, NULL);
2203 * regulator_force_disable - force disable regulator output
2204 * @regulator: regulator source
2206 * Forcibly disable the regulator output voltage or current.
2207 * NOTE: this *will* disable the regulator output even if other consumer
2208 * devices have it enabled. This should be used for situations when device
2209 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2211 int regulator_force_disable(struct regulator *regulator)
2213 struct regulator_dev *rdev = regulator->rdev;
2216 mutex_lock(&rdev->mutex);
2217 regulator->uA_load = 0;
2218 ret = _regulator_force_disable(regulator->rdev);
2219 mutex_unlock(&rdev->mutex);
2222 while (rdev->open_count--)
2223 regulator_disable(rdev->supply);
2227 EXPORT_SYMBOL_GPL(regulator_force_disable);
2229 static void regulator_disable_work(struct work_struct *work)
2231 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2235 mutex_lock(&rdev->mutex);
2237 BUG_ON(!rdev->deferred_disables);
2239 count = rdev->deferred_disables;
2240 rdev->deferred_disables = 0;
2242 for (i = 0; i < count; i++) {
2243 ret = _regulator_disable(rdev);
2245 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2248 mutex_unlock(&rdev->mutex);
2251 for (i = 0; i < count; i++) {
2252 ret = regulator_disable(rdev->supply);
2255 "Supply disable failed: %d\n", ret);
2262 * regulator_disable_deferred - disable regulator output with delay
2263 * @regulator: regulator source
2264 * @ms: miliseconds until the regulator is disabled
2266 * Execute regulator_disable() on the regulator after a delay. This
2267 * is intended for use with devices that require some time to quiesce.
2269 * NOTE: this will only disable the regulator output if no other consumer
2270 * devices have it enabled, the regulator device supports disabling and
2271 * machine constraints permit this operation.
2273 int regulator_disable_deferred(struct regulator *regulator, int ms)
2275 struct regulator_dev *rdev = regulator->rdev;
2278 if (regulator->always_on)
2282 return regulator_disable(regulator);
2284 mutex_lock(&rdev->mutex);
2285 rdev->deferred_disables++;
2286 mutex_unlock(&rdev->mutex);
2288 ret = queue_delayed_work(system_power_efficient_wq,
2289 &rdev->disable_work,
2290 msecs_to_jiffies(ms));
2296 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2298 static int _regulator_is_enabled(struct regulator_dev *rdev)
2300 /* A GPIO control always takes precedence */
2302 return rdev->ena_gpio_state;
2304 /* If we don't know then assume that the regulator is always on */
2305 if (!rdev->desc->ops->is_enabled)
2308 return rdev->desc->ops->is_enabled(rdev);
2312 * regulator_is_enabled - is the regulator output enabled
2313 * @regulator: regulator source
2315 * Returns positive if the regulator driver backing the source/client
2316 * has requested that the device be enabled, zero if it hasn't, else a
2317 * negative errno code.
2319 * Note that the device backing this regulator handle can have multiple
2320 * users, so it might be enabled even if regulator_enable() was never
2321 * called for this particular source.
2323 int regulator_is_enabled(struct regulator *regulator)
2327 if (regulator->always_on)
2330 mutex_lock(®ulator->rdev->mutex);
2331 ret = _regulator_is_enabled(regulator->rdev);
2332 mutex_unlock(®ulator->rdev->mutex);
2336 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2339 * regulator_can_change_voltage - check if regulator can change voltage
2340 * @regulator: regulator source
2342 * Returns positive if the regulator driver backing the source/client
2343 * can change its voltage, false otherwise. Useful for detecting fixed
2344 * or dummy regulators and disabling voltage change logic in the client
2347 int regulator_can_change_voltage(struct regulator *regulator)
2349 struct regulator_dev *rdev = regulator->rdev;
2351 if (rdev->constraints &&
2352 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2353 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2356 if (rdev->desc->continuous_voltage_range &&
2357 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2358 rdev->constraints->min_uV != rdev->constraints->max_uV)
2364 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2367 * regulator_count_voltages - count regulator_list_voltage() selectors
2368 * @regulator: regulator source
2370 * Returns number of selectors, or negative errno. Selectors are
2371 * numbered starting at zero, and typically correspond to bitfields
2372 * in hardware registers.
2374 int regulator_count_voltages(struct regulator *regulator)
2376 struct regulator_dev *rdev = regulator->rdev;
2378 if (rdev->desc->n_voltages)
2379 return rdev->desc->n_voltages;
2384 return regulator_count_voltages(rdev->supply);
2386 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2389 * regulator_list_voltage - enumerate supported voltages
2390 * @regulator: regulator source
2391 * @selector: identify voltage to list
2392 * Context: can sleep
2394 * Returns a voltage that can be passed to @regulator_set_voltage(),
2395 * zero if this selector code can't be used on this system, or a
2398 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2400 struct regulator_dev *rdev = regulator->rdev;
2401 const struct regulator_ops *ops = rdev->desc->ops;
2404 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2405 return rdev->desc->fixed_uV;
2407 if (ops->list_voltage) {
2408 if (selector >= rdev->desc->n_voltages)
2410 mutex_lock(&rdev->mutex);
2411 ret = ops->list_voltage(rdev, selector);
2412 mutex_unlock(&rdev->mutex);
2413 } else if (rdev->supply) {
2414 ret = regulator_list_voltage(rdev->supply, selector);
2420 if (ret < rdev->constraints->min_uV)
2422 else if (ret > rdev->constraints->max_uV)
2428 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2431 * regulator_get_regmap - get the regulator's register map
2432 * @regulator: regulator source
2434 * Returns the register map for the given regulator, or an ERR_PTR value
2435 * if the regulator doesn't use regmap.
2437 struct regmap *regulator_get_regmap(struct regulator *regulator)
2439 struct regmap *map = regulator->rdev->regmap;
2441 return map ? map : ERR_PTR(-EOPNOTSUPP);
2445 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2446 * @regulator: regulator source
2447 * @vsel_reg: voltage selector register, output parameter
2448 * @vsel_mask: mask for voltage selector bitfield, output parameter
2450 * Returns the hardware register offset and bitmask used for setting the
2451 * regulator voltage. This might be useful when configuring voltage-scaling
2452 * hardware or firmware that can make I2C requests behind the kernel's back,
2455 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2456 * and 0 is returned, otherwise a negative errno is returned.
2458 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2460 unsigned *vsel_mask)
2462 struct regulator_dev *rdev = regulator->rdev;
2463 const struct regulator_ops *ops = rdev->desc->ops;
2465 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2468 *vsel_reg = rdev->desc->vsel_reg;
2469 *vsel_mask = rdev->desc->vsel_mask;
2473 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2476 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2477 * @regulator: regulator source
2478 * @selector: identify voltage to list
2480 * Converts the selector to a hardware-specific voltage selector that can be
2481 * directly written to the regulator registers. The address of the voltage
2482 * register can be determined by calling @regulator_get_hardware_vsel_register.
2484 * On error a negative errno is returned.
2486 int regulator_list_hardware_vsel(struct regulator *regulator,
2489 struct regulator_dev *rdev = regulator->rdev;
2490 const struct regulator_ops *ops = rdev->desc->ops;
2492 if (selector >= rdev->desc->n_voltages)
2494 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2499 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2502 * regulator_get_linear_step - return the voltage step size between VSEL values
2503 * @regulator: regulator source
2505 * Returns the voltage step size between VSEL values for linear
2506 * regulators, or return 0 if the regulator isn't a linear regulator.
2508 unsigned int regulator_get_linear_step(struct regulator *regulator)
2510 struct regulator_dev *rdev = regulator->rdev;
2512 return rdev->desc->uV_step;
2514 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2517 * regulator_is_supported_voltage - check if a voltage range can be supported
2519 * @regulator: Regulator to check.
2520 * @min_uV: Minimum required voltage in uV.
2521 * @max_uV: Maximum required voltage in uV.
2523 * Returns a boolean or a negative error code.
2525 int regulator_is_supported_voltage(struct regulator *regulator,
2526 int min_uV, int max_uV)
2528 struct regulator_dev *rdev = regulator->rdev;
2529 int i, voltages, ret;
2531 /* If we can't change voltage check the current voltage */
2532 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2533 ret = regulator_get_voltage(regulator);
2535 return min_uV <= ret && ret <= max_uV;
2540 /* Any voltage within constrains range is fine? */
2541 if (rdev->desc->continuous_voltage_range)
2542 return min_uV >= rdev->constraints->min_uV &&
2543 max_uV <= rdev->constraints->max_uV;
2545 ret = regulator_count_voltages(regulator);
2550 for (i = 0; i < voltages; i++) {
2551 ret = regulator_list_voltage(regulator, i);
2553 if (ret >= min_uV && ret <= max_uV)
2559 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2561 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2562 int min_uV, int max_uV,
2565 struct pre_voltage_change_data data;
2568 data.old_uV = _regulator_get_voltage(rdev);
2569 data.min_uV = min_uV;
2570 data.max_uV = max_uV;
2571 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2573 if (ret & NOTIFY_STOP_MASK)
2576 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2580 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2581 (void *)data.old_uV);
2586 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2587 int uV, unsigned selector)
2589 struct pre_voltage_change_data data;
2592 data.old_uV = _regulator_get_voltage(rdev);
2595 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2597 if (ret & NOTIFY_STOP_MASK)
2600 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2604 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2605 (void *)data.old_uV);
2610 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2611 int min_uV, int max_uV)
2616 unsigned int selector;
2617 int old_selector = -1;
2619 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2621 min_uV += rdev->constraints->uV_offset;
2622 max_uV += rdev->constraints->uV_offset;
2625 * If we can't obtain the old selector there is not enough
2626 * info to call set_voltage_time_sel().
2628 if (_regulator_is_enabled(rdev) &&
2629 rdev->desc->ops->set_voltage_time_sel &&
2630 rdev->desc->ops->get_voltage_sel) {
2631 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2632 if (old_selector < 0)
2633 return old_selector;
2636 if (rdev->desc->ops->set_voltage) {
2637 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2641 if (rdev->desc->ops->list_voltage)
2642 best_val = rdev->desc->ops->list_voltage(rdev,
2645 best_val = _regulator_get_voltage(rdev);
2648 } else if (rdev->desc->ops->set_voltage_sel) {
2649 if (rdev->desc->ops->map_voltage) {
2650 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2653 if (rdev->desc->ops->list_voltage ==
2654 regulator_list_voltage_linear)
2655 ret = regulator_map_voltage_linear(rdev,
2657 else if (rdev->desc->ops->list_voltage ==
2658 regulator_list_voltage_linear_range)
2659 ret = regulator_map_voltage_linear_range(rdev,
2662 ret = regulator_map_voltage_iterate(rdev,
2667 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2668 if (min_uV <= best_val && max_uV >= best_val) {
2670 if (old_selector == selector)
2673 ret = _regulator_call_set_voltage_sel(
2674 rdev, best_val, selector);
2683 /* Call set_voltage_time_sel if successfully obtained old_selector */
2684 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2685 && old_selector != selector) {
2687 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2688 old_selector, selector);
2690 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2695 /* Insert any necessary delays */
2696 if (delay >= 1000) {
2697 mdelay(delay / 1000);
2698 udelay(delay % 1000);
2704 if (ret == 0 && best_val >= 0) {
2705 unsigned long data = best_val;
2707 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2711 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2717 * regulator_set_voltage - set regulator output voltage
2718 * @regulator: regulator source
2719 * @min_uV: Minimum required voltage in uV
2720 * @max_uV: Maximum acceptable voltage in uV
2722 * Sets a voltage regulator to the desired output voltage. This can be set
2723 * during any regulator state. IOW, regulator can be disabled or enabled.
2725 * If the regulator is enabled then the voltage will change to the new value
2726 * immediately otherwise if the regulator is disabled the regulator will
2727 * output at the new voltage when enabled.
2729 * NOTE: If the regulator is shared between several devices then the lowest
2730 * request voltage that meets the system constraints will be used.
2731 * Regulator system constraints must be set for this regulator before
2732 * calling this function otherwise this call will fail.
2734 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2736 struct regulator_dev *rdev = regulator->rdev;
2738 int old_min_uV, old_max_uV;
2741 mutex_lock(&rdev->mutex);
2743 /* If we're setting the same range as last time the change
2744 * should be a noop (some cpufreq implementations use the same
2745 * voltage for multiple frequencies, for example).
2747 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2750 /* If we're trying to set a range that overlaps the current voltage,
2751 * return successfully even though the regulator does not support
2752 * changing the voltage.
2754 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2755 current_uV = _regulator_get_voltage(rdev);
2756 if (min_uV <= current_uV && current_uV <= max_uV) {
2757 regulator->min_uV = min_uV;
2758 regulator->max_uV = max_uV;
2764 if (!rdev->desc->ops->set_voltage &&
2765 !rdev->desc->ops->set_voltage_sel) {
2770 /* constraints check */
2771 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2775 /* restore original values in case of error */
2776 old_min_uV = regulator->min_uV;
2777 old_max_uV = regulator->max_uV;
2778 regulator->min_uV = min_uV;
2779 regulator->max_uV = max_uV;
2781 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2785 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2790 mutex_unlock(&rdev->mutex);
2793 regulator->min_uV = old_min_uV;
2794 regulator->max_uV = old_max_uV;
2795 mutex_unlock(&rdev->mutex);
2798 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2801 * regulator_set_voltage_time - get raise/fall time
2802 * @regulator: regulator source
2803 * @old_uV: starting voltage in microvolts
2804 * @new_uV: target voltage in microvolts
2806 * Provided with the starting and ending voltage, this function attempts to
2807 * calculate the time in microseconds required to rise or fall to this new
2810 int regulator_set_voltage_time(struct regulator *regulator,
2811 int old_uV, int new_uV)
2813 struct regulator_dev *rdev = regulator->rdev;
2814 const struct regulator_ops *ops = rdev->desc->ops;
2820 /* Currently requires operations to do this */
2821 if (!ops->list_voltage || !ops->set_voltage_time_sel
2822 || !rdev->desc->n_voltages)
2825 for (i = 0; i < rdev->desc->n_voltages; i++) {
2826 /* We only look for exact voltage matches here */
2827 voltage = regulator_list_voltage(regulator, i);
2832 if (voltage == old_uV)
2834 if (voltage == new_uV)
2838 if (old_sel < 0 || new_sel < 0)
2841 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2843 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2846 * regulator_set_voltage_time_sel - get raise/fall time
2847 * @rdev: regulator source device
2848 * @old_selector: selector for starting voltage
2849 * @new_selector: selector for target voltage
2851 * Provided with the starting and target voltage selectors, this function
2852 * returns time in microseconds required to rise or fall to this new voltage
2854 * Drivers providing ramp_delay in regulation_constraints can use this as their
2855 * set_voltage_time_sel() operation.
2857 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2858 unsigned int old_selector,
2859 unsigned int new_selector)
2861 unsigned int ramp_delay = 0;
2862 int old_volt, new_volt;
2864 if (rdev->constraints->ramp_delay)
2865 ramp_delay = rdev->constraints->ramp_delay;
2866 else if (rdev->desc->ramp_delay)
2867 ramp_delay = rdev->desc->ramp_delay;
2869 if (ramp_delay == 0) {
2870 rdev_warn(rdev, "ramp_delay not set\n");
2875 if (!rdev->desc->ops->list_voltage)
2878 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2879 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2881 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2883 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2886 * regulator_sync_voltage - re-apply last regulator output voltage
2887 * @regulator: regulator source
2889 * Re-apply the last configured voltage. This is intended to be used
2890 * where some external control source the consumer is cooperating with
2891 * has caused the configured voltage to change.
2893 int regulator_sync_voltage(struct regulator *regulator)
2895 struct regulator_dev *rdev = regulator->rdev;
2896 int ret, min_uV, max_uV;
2898 mutex_lock(&rdev->mutex);
2900 if (!rdev->desc->ops->set_voltage &&
2901 !rdev->desc->ops->set_voltage_sel) {
2906 /* This is only going to work if we've had a voltage configured. */
2907 if (!regulator->min_uV && !regulator->max_uV) {
2912 min_uV = regulator->min_uV;
2913 max_uV = regulator->max_uV;
2915 /* This should be a paranoia check... */
2916 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2920 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2924 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2927 mutex_unlock(&rdev->mutex);
2930 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2932 static int _regulator_get_voltage(struct regulator_dev *rdev)
2936 if (rdev->desc->ops->get_voltage_sel) {
2937 sel = rdev->desc->ops->get_voltage_sel(rdev);
2940 ret = rdev->desc->ops->list_voltage(rdev, sel);
2941 } else if (rdev->desc->ops->get_voltage) {
2942 ret = rdev->desc->ops->get_voltage(rdev);
2943 } else if (rdev->desc->ops->list_voltage) {
2944 ret = rdev->desc->ops->list_voltage(rdev, 0);
2945 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
2946 ret = rdev->desc->fixed_uV;
2947 } else if (rdev->supply) {
2948 ret = regulator_get_voltage(rdev->supply);
2955 return ret - rdev->constraints->uV_offset;
2959 * regulator_get_voltage - get regulator output voltage
2960 * @regulator: regulator source
2962 * This returns the current regulator voltage in uV.
2964 * NOTE: If the regulator is disabled it will return the voltage value. This
2965 * function should not be used to determine regulator state.
2967 int regulator_get_voltage(struct regulator *regulator)
2971 mutex_lock(®ulator->rdev->mutex);
2973 ret = _regulator_get_voltage(regulator->rdev);
2975 mutex_unlock(®ulator->rdev->mutex);
2979 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2982 * regulator_set_current_limit - set regulator output current limit
2983 * @regulator: regulator source
2984 * @min_uA: Minimum supported current in uA
2985 * @max_uA: Maximum supported current in uA
2987 * Sets current sink to the desired output current. This can be set during
2988 * any regulator state. IOW, regulator can be disabled or enabled.
2990 * If the regulator is enabled then the current will change to the new value
2991 * immediately otherwise if the regulator is disabled the regulator will
2992 * output at the new current when enabled.
2994 * NOTE: Regulator system constraints must be set for this regulator before
2995 * calling this function otherwise this call will fail.
2997 int regulator_set_current_limit(struct regulator *regulator,
2998 int min_uA, int max_uA)
3000 struct regulator_dev *rdev = regulator->rdev;
3003 mutex_lock(&rdev->mutex);
3006 if (!rdev->desc->ops->set_current_limit) {
3011 /* constraints check */
3012 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3016 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3018 mutex_unlock(&rdev->mutex);
3021 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3023 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3027 mutex_lock(&rdev->mutex);
3030 if (!rdev->desc->ops->get_current_limit) {
3035 ret = rdev->desc->ops->get_current_limit(rdev);
3037 mutex_unlock(&rdev->mutex);
3042 * regulator_get_current_limit - get regulator output current
3043 * @regulator: regulator source
3045 * This returns the current supplied by the specified current sink in uA.
3047 * NOTE: If the regulator is disabled it will return the current value. This
3048 * function should not be used to determine regulator state.
3050 int regulator_get_current_limit(struct regulator *regulator)
3052 return _regulator_get_current_limit(regulator->rdev);
3054 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3057 * regulator_set_mode - set regulator operating mode
3058 * @regulator: regulator source
3059 * @mode: operating mode - one of the REGULATOR_MODE constants
3061 * Set regulator operating mode to increase regulator efficiency or improve
3062 * regulation performance.
3064 * NOTE: Regulator system constraints must be set for this regulator before
3065 * calling this function otherwise this call will fail.
3067 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3069 struct regulator_dev *rdev = regulator->rdev;
3071 int regulator_curr_mode;
3073 mutex_lock(&rdev->mutex);
3076 if (!rdev->desc->ops->set_mode) {
3081 /* return if the same mode is requested */
3082 if (rdev->desc->ops->get_mode) {
3083 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3084 if (regulator_curr_mode == mode) {
3090 /* constraints check */
3091 ret = regulator_mode_constrain(rdev, &mode);
3095 ret = rdev->desc->ops->set_mode(rdev, mode);
3097 mutex_unlock(&rdev->mutex);
3100 EXPORT_SYMBOL_GPL(regulator_set_mode);
3102 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3106 mutex_lock(&rdev->mutex);
3109 if (!rdev->desc->ops->get_mode) {
3114 ret = rdev->desc->ops->get_mode(rdev);
3116 mutex_unlock(&rdev->mutex);
3121 * regulator_get_mode - get regulator operating mode
3122 * @regulator: regulator source
3124 * Get the current regulator operating mode.
3126 unsigned int regulator_get_mode(struct regulator *regulator)
3128 return _regulator_get_mode(regulator->rdev);
3130 EXPORT_SYMBOL_GPL(regulator_get_mode);
3133 * regulator_set_load - set regulator load
3134 * @regulator: regulator source
3135 * @uA_load: load current
3137 * Notifies the regulator core of a new device load. This is then used by
3138 * DRMS (if enabled by constraints) to set the most efficient regulator
3139 * operating mode for the new regulator loading.
3141 * Consumer devices notify their supply regulator of the maximum power
3142 * they will require (can be taken from device datasheet in the power
3143 * consumption tables) when they change operational status and hence power
3144 * state. Examples of operational state changes that can affect power
3145 * consumption are :-
3147 * o Device is opened / closed.
3148 * o Device I/O is about to begin or has just finished.
3149 * o Device is idling in between work.
3151 * This information is also exported via sysfs to userspace.
3153 * DRMS will sum the total requested load on the regulator and change
3154 * to the most efficient operating mode if platform constraints allow.
3156 * On error a negative errno is returned.
3158 int regulator_set_load(struct regulator *regulator, int uA_load)
3160 struct regulator_dev *rdev = regulator->rdev;
3163 mutex_lock(&rdev->mutex);
3164 regulator->uA_load = uA_load;
3165 ret = drms_uA_update(rdev);
3166 mutex_unlock(&rdev->mutex);
3170 EXPORT_SYMBOL_GPL(regulator_set_load);
3173 * regulator_allow_bypass - allow the regulator to go into bypass mode
3175 * @regulator: Regulator to configure
3176 * @enable: enable or disable bypass mode
3178 * Allow the regulator to go into bypass mode if all other consumers
3179 * for the regulator also enable bypass mode and the machine
3180 * constraints allow this. Bypass mode means that the regulator is
3181 * simply passing the input directly to the output with no regulation.
3183 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3185 struct regulator_dev *rdev = regulator->rdev;
3188 if (!rdev->desc->ops->set_bypass)
3191 if (rdev->constraints &&
3192 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3195 mutex_lock(&rdev->mutex);
3197 if (enable && !regulator->bypass) {
3198 rdev->bypass_count++;
3200 if (rdev->bypass_count == rdev->open_count) {
3201 ret = rdev->desc->ops->set_bypass(rdev, enable);
3203 rdev->bypass_count--;
3206 } else if (!enable && regulator->bypass) {
3207 rdev->bypass_count--;
3209 if (rdev->bypass_count != rdev->open_count) {
3210 ret = rdev->desc->ops->set_bypass(rdev, enable);
3212 rdev->bypass_count++;
3217 regulator->bypass = enable;
3219 mutex_unlock(&rdev->mutex);
3223 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3226 * regulator_register_notifier - register regulator event notifier
3227 * @regulator: regulator source
3228 * @nb: notifier block
3230 * Register notifier block to receive regulator events.
3232 int regulator_register_notifier(struct regulator *regulator,
3233 struct notifier_block *nb)
3235 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3238 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3241 * regulator_unregister_notifier - unregister regulator event notifier
3242 * @regulator: regulator source
3243 * @nb: notifier block
3245 * Unregister regulator event notifier block.
3247 int regulator_unregister_notifier(struct regulator *regulator,
3248 struct notifier_block *nb)
3250 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3253 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3255 /* notify regulator consumers and downstream regulator consumers.
3256 * Note mutex must be held by caller.
3258 static int _notifier_call_chain(struct regulator_dev *rdev,
3259 unsigned long event, void *data)
3261 /* call rdev chain first */
3262 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3266 * regulator_bulk_get - get multiple regulator consumers
3268 * @dev: Device to supply
3269 * @num_consumers: Number of consumers to register
3270 * @consumers: Configuration of consumers; clients are stored here.
3272 * @return 0 on success, an errno on failure.
3274 * This helper function allows drivers to get several regulator
3275 * consumers in one operation. If any of the regulators cannot be
3276 * acquired then any regulators that were allocated will be freed
3277 * before returning to the caller.
3279 int regulator_bulk_get(struct device *dev, int num_consumers,
3280 struct regulator_bulk_data *consumers)
3285 for (i = 0; i < num_consumers; i++)
3286 consumers[i].consumer = NULL;
3288 for (i = 0; i < num_consumers; i++) {
3289 consumers[i].consumer = regulator_get(dev,
3290 consumers[i].supply);
3291 if (IS_ERR(consumers[i].consumer)) {
3292 ret = PTR_ERR(consumers[i].consumer);
3293 dev_err(dev, "Failed to get supply '%s': %d\n",
3294 consumers[i].supply, ret);
3295 consumers[i].consumer = NULL;
3304 regulator_put(consumers[i].consumer);
3308 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3310 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3312 struct regulator_bulk_data *bulk = data;
3314 bulk->ret = regulator_enable(bulk->consumer);
3318 * regulator_bulk_enable - enable multiple regulator consumers
3320 * @num_consumers: Number of consumers
3321 * @consumers: Consumer data; clients are stored here.
3322 * @return 0 on success, an errno on failure
3324 * This convenience API allows consumers to enable multiple regulator
3325 * clients in a single API call. If any consumers cannot be enabled
3326 * then any others that were enabled will be disabled again prior to
3329 int regulator_bulk_enable(int num_consumers,
3330 struct regulator_bulk_data *consumers)
3332 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3336 for (i = 0; i < num_consumers; i++) {
3337 if (consumers[i].consumer->always_on)
3338 consumers[i].ret = 0;
3340 async_schedule_domain(regulator_bulk_enable_async,
3341 &consumers[i], &async_domain);
3344 async_synchronize_full_domain(&async_domain);
3346 /* If any consumer failed we need to unwind any that succeeded */
3347 for (i = 0; i < num_consumers; i++) {
3348 if (consumers[i].ret != 0) {
3349 ret = consumers[i].ret;
3357 for (i = 0; i < num_consumers; i++) {
3358 if (consumers[i].ret < 0)
3359 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3362 regulator_disable(consumers[i].consumer);
3367 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3370 * regulator_bulk_disable - disable multiple regulator consumers
3372 * @num_consumers: Number of consumers
3373 * @consumers: Consumer data; clients are stored here.
3374 * @return 0 on success, an errno on failure
3376 * This convenience API allows consumers to disable multiple regulator
3377 * clients in a single API call. If any consumers cannot be disabled
3378 * then any others that were disabled will be enabled again prior to
3381 int regulator_bulk_disable(int num_consumers,
3382 struct regulator_bulk_data *consumers)
3387 for (i = num_consumers - 1; i >= 0; --i) {
3388 ret = regulator_disable(consumers[i].consumer);
3396 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3397 for (++i; i < num_consumers; ++i) {
3398 r = regulator_enable(consumers[i].consumer);
3400 pr_err("Failed to reename %s: %d\n",
3401 consumers[i].supply, r);
3406 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3409 * regulator_bulk_force_disable - force disable multiple regulator consumers
3411 * @num_consumers: Number of consumers
3412 * @consumers: Consumer data; clients are stored here.
3413 * @return 0 on success, an errno on failure
3415 * This convenience API allows consumers to forcibly disable multiple regulator
3416 * clients in a single API call.
3417 * NOTE: This should be used for situations when device damage will
3418 * likely occur if the regulators are not disabled (e.g. over temp).
3419 * Although regulator_force_disable function call for some consumers can
3420 * return error numbers, the function is called for all consumers.
3422 int regulator_bulk_force_disable(int num_consumers,
3423 struct regulator_bulk_data *consumers)
3428 for (i = 0; i < num_consumers; i++)
3430 regulator_force_disable(consumers[i].consumer);
3432 for (i = 0; i < num_consumers; i++) {
3433 if (consumers[i].ret != 0) {
3434 ret = consumers[i].ret;
3443 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3446 * regulator_bulk_free - free multiple regulator consumers
3448 * @num_consumers: Number of consumers
3449 * @consumers: Consumer data; clients are stored here.
3451 * This convenience API allows consumers to free multiple regulator
3452 * clients in a single API call.
3454 void regulator_bulk_free(int num_consumers,
3455 struct regulator_bulk_data *consumers)
3459 for (i = 0; i < num_consumers; i++) {
3460 regulator_put(consumers[i].consumer);
3461 consumers[i].consumer = NULL;
3464 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3467 * regulator_notifier_call_chain - call regulator event notifier
3468 * @rdev: regulator source
3469 * @event: notifier block
3470 * @data: callback-specific data.
3472 * Called by regulator drivers to notify clients a regulator event has
3473 * occurred. We also notify regulator clients downstream.
3474 * Note lock must be held by caller.
3476 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3477 unsigned long event, void *data)
3479 lockdep_assert_held_once(&rdev->mutex);
3481 _notifier_call_chain(rdev, event, data);
3485 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3488 * regulator_mode_to_status - convert a regulator mode into a status
3490 * @mode: Mode to convert
3492 * Convert a regulator mode into a status.
3494 int regulator_mode_to_status(unsigned int mode)
3497 case REGULATOR_MODE_FAST:
3498 return REGULATOR_STATUS_FAST;
3499 case REGULATOR_MODE_NORMAL:
3500 return REGULATOR_STATUS_NORMAL;
3501 case REGULATOR_MODE_IDLE:
3502 return REGULATOR_STATUS_IDLE;
3503 case REGULATOR_MODE_STANDBY:
3504 return REGULATOR_STATUS_STANDBY;
3506 return REGULATOR_STATUS_UNDEFINED;
3509 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3511 static struct attribute *regulator_dev_attrs[] = {
3512 &dev_attr_name.attr,
3513 &dev_attr_num_users.attr,
3514 &dev_attr_type.attr,
3515 &dev_attr_microvolts.attr,
3516 &dev_attr_microamps.attr,
3517 &dev_attr_opmode.attr,
3518 &dev_attr_state.attr,
3519 &dev_attr_status.attr,
3520 &dev_attr_bypass.attr,
3521 &dev_attr_requested_microamps.attr,
3522 &dev_attr_min_microvolts.attr,
3523 &dev_attr_max_microvolts.attr,
3524 &dev_attr_min_microamps.attr,
3525 &dev_attr_max_microamps.attr,
3526 &dev_attr_suspend_standby_state.attr,
3527 &dev_attr_suspend_mem_state.attr,
3528 &dev_attr_suspend_disk_state.attr,
3529 &dev_attr_suspend_standby_microvolts.attr,
3530 &dev_attr_suspend_mem_microvolts.attr,
3531 &dev_attr_suspend_disk_microvolts.attr,
3532 &dev_attr_suspend_standby_mode.attr,
3533 &dev_attr_suspend_mem_mode.attr,
3534 &dev_attr_suspend_disk_mode.attr,
3539 * To avoid cluttering sysfs (and memory) with useless state, only
3540 * create attributes that can be meaningfully displayed.
3542 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3543 struct attribute *attr, int idx)
3545 struct device *dev = kobj_to_dev(kobj);
3546 struct regulator_dev *rdev = container_of(dev, struct regulator_dev, dev);
3547 const struct regulator_ops *ops = rdev->desc->ops;
3548 umode_t mode = attr->mode;
3550 /* these three are always present */
3551 if (attr == &dev_attr_name.attr ||
3552 attr == &dev_attr_num_users.attr ||
3553 attr == &dev_attr_type.attr)
3556 /* some attributes need specific methods to be displayed */
3557 if (attr == &dev_attr_microvolts.attr) {
3558 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3559 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3560 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3561 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3566 if (attr == &dev_attr_microamps.attr)
3567 return ops->get_current_limit ? mode : 0;
3569 if (attr == &dev_attr_opmode.attr)
3570 return ops->get_mode ? mode : 0;
3572 if (attr == &dev_attr_state.attr)
3573 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3575 if (attr == &dev_attr_status.attr)
3576 return ops->get_status ? mode : 0;
3578 if (attr == &dev_attr_bypass.attr)
3579 return ops->get_bypass ? mode : 0;
3581 /* some attributes are type-specific */
3582 if (attr == &dev_attr_requested_microamps.attr)
3583 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3585 /* constraints need specific supporting methods */
3586 if (attr == &dev_attr_min_microvolts.attr ||
3587 attr == &dev_attr_max_microvolts.attr)
3588 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3590 if (attr == &dev_attr_min_microamps.attr ||
3591 attr == &dev_attr_max_microamps.attr)
3592 return ops->set_current_limit ? mode : 0;
3594 if (attr == &dev_attr_suspend_standby_state.attr ||
3595 attr == &dev_attr_suspend_mem_state.attr ||
3596 attr == &dev_attr_suspend_disk_state.attr)
3599 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
3600 attr == &dev_attr_suspend_mem_microvolts.attr ||
3601 attr == &dev_attr_suspend_disk_microvolts.attr)
3602 return ops->set_suspend_voltage ? mode : 0;
3604 if (attr == &dev_attr_suspend_standby_mode.attr ||
3605 attr == &dev_attr_suspend_mem_mode.attr ||
3606 attr == &dev_attr_suspend_disk_mode.attr)
3607 return ops->set_suspend_mode ? mode : 0;
3612 static const struct attribute_group regulator_dev_group = {
3613 .attrs = regulator_dev_attrs,
3614 .is_visible = regulator_attr_is_visible,
3617 static const struct attribute_group *regulator_dev_groups[] = {
3618 ®ulator_dev_group,
3622 static void regulator_dev_release(struct device *dev)
3624 struct regulator_dev *rdev = dev_get_drvdata(dev);
3626 kfree(rdev->constraints);
3627 of_node_put(rdev->dev.of_node);
3631 static struct class regulator_class = {
3632 .name = "regulator",
3633 .dev_release = regulator_dev_release,
3634 .dev_groups = regulator_dev_groups,
3637 static void rdev_init_debugfs(struct regulator_dev *rdev)
3639 struct device *parent = rdev->dev.parent;
3640 const char *rname = rdev_get_name(rdev);
3641 char name[NAME_MAX];
3643 /* Avoid duplicate debugfs directory names */
3644 if (parent && rname == rdev->desc->name) {
3645 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
3650 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
3651 if (!rdev->debugfs) {
3652 rdev_warn(rdev, "Failed to create debugfs directory\n");
3656 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3658 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3660 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3661 &rdev->bypass_count);
3665 * regulator_register - register regulator
3666 * @regulator_desc: regulator to register
3667 * @cfg: runtime configuration for regulator
3669 * Called by regulator drivers to register a regulator.
3670 * Returns a valid pointer to struct regulator_dev on success
3671 * or an ERR_PTR() on error.
3673 struct regulator_dev *
3674 regulator_register(const struct regulator_desc *regulator_desc,
3675 const struct regulator_config *cfg)
3677 const struct regulation_constraints *constraints = NULL;
3678 const struct regulator_init_data *init_data;
3679 struct regulator_config *config = NULL;
3680 static atomic_t regulator_no = ATOMIC_INIT(-1);
3681 struct regulator_dev *rdev;
3685 if (regulator_desc == NULL || cfg == NULL)
3686 return ERR_PTR(-EINVAL);
3691 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3692 return ERR_PTR(-EINVAL);
3694 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3695 regulator_desc->type != REGULATOR_CURRENT)
3696 return ERR_PTR(-EINVAL);
3698 /* Only one of each should be implemented */
3699 WARN_ON(regulator_desc->ops->get_voltage &&
3700 regulator_desc->ops->get_voltage_sel);
3701 WARN_ON(regulator_desc->ops->set_voltage &&
3702 regulator_desc->ops->set_voltage_sel);
3704 /* If we're using selectors we must implement list_voltage. */
3705 if (regulator_desc->ops->get_voltage_sel &&
3706 !regulator_desc->ops->list_voltage) {
3707 return ERR_PTR(-EINVAL);
3709 if (regulator_desc->ops->set_voltage_sel &&
3710 !regulator_desc->ops->list_voltage) {
3711 return ERR_PTR(-EINVAL);
3714 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3716 return ERR_PTR(-ENOMEM);
3719 * Duplicate the config so the driver could override it after
3720 * parsing init data.
3722 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
3723 if (config == NULL) {
3725 return ERR_PTR(-ENOMEM);
3728 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
3729 &rdev->dev.of_node);
3731 init_data = config->init_data;
3732 rdev->dev.of_node = of_node_get(config->of_node);
3735 mutex_lock(®ulator_list_mutex);
3737 mutex_init(&rdev->mutex);
3738 rdev->reg_data = config->driver_data;
3739 rdev->owner = regulator_desc->owner;
3740 rdev->desc = regulator_desc;
3742 rdev->regmap = config->regmap;
3743 else if (dev_get_regmap(dev, NULL))
3744 rdev->regmap = dev_get_regmap(dev, NULL);
3745 else if (dev->parent)
3746 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3747 INIT_LIST_HEAD(&rdev->consumer_list);
3748 INIT_LIST_HEAD(&rdev->list);
3749 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3750 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3752 /* preform any regulator specific init */
3753 if (init_data && init_data->regulator_init) {
3754 ret = init_data->regulator_init(rdev->reg_data);
3759 /* register with sysfs */
3760 rdev->dev.class = ®ulator_class;
3761 rdev->dev.parent = dev;
3762 dev_set_name(&rdev->dev, "regulator.%lu",
3763 (unsigned long) atomic_inc_return(®ulator_no));
3764 ret = device_register(&rdev->dev);
3766 put_device(&rdev->dev);
3770 dev_set_drvdata(&rdev->dev, rdev);
3772 if ((config->ena_gpio || config->ena_gpio_initialized) &&
3773 gpio_is_valid(config->ena_gpio)) {
3774 ret = regulator_ena_gpio_request(rdev, config);
3776 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3777 config->ena_gpio, ret);
3782 /* set regulator constraints */
3784 constraints = &init_data->constraints;
3786 ret = set_machine_constraints(rdev, constraints);
3790 if (init_data && init_data->supply_regulator)
3791 rdev->supply_name = init_data->supply_regulator;
3792 else if (regulator_desc->supply_name)
3793 rdev->supply_name = regulator_desc->supply_name;
3795 /* add consumers devices */
3797 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3798 ret = set_consumer_device_supply(rdev,
3799 init_data->consumer_supplies[i].dev_name,
3800 init_data->consumer_supplies[i].supply);
3802 dev_err(dev, "Failed to set supply %s\n",
3803 init_data->consumer_supplies[i].supply);
3804 goto unset_supplies;
3809 list_add(&rdev->list, ®ulator_list);
3811 rdev_init_debugfs(rdev);
3813 mutex_unlock(®ulator_list_mutex);
3818 unset_regulator_supplies(rdev);
3821 regulator_ena_gpio_free(rdev);
3822 kfree(rdev->constraints);
3824 device_unregister(&rdev->dev);
3825 /* device core frees rdev */
3826 rdev = ERR_PTR(ret);
3831 rdev = ERR_PTR(ret);
3834 EXPORT_SYMBOL_GPL(regulator_register);
3837 * regulator_unregister - unregister regulator
3838 * @rdev: regulator to unregister
3840 * Called by regulator drivers to unregister a regulator.
3842 void regulator_unregister(struct regulator_dev *rdev)
3848 while (rdev->use_count--)
3849 regulator_disable(rdev->supply);
3850 regulator_put(rdev->supply);
3852 mutex_lock(®ulator_list_mutex);
3853 debugfs_remove_recursive(rdev->debugfs);
3854 flush_work(&rdev->disable_work.work);
3855 WARN_ON(rdev->open_count);
3856 unset_regulator_supplies(rdev);
3857 list_del(&rdev->list);
3858 mutex_unlock(®ulator_list_mutex);
3859 regulator_ena_gpio_free(rdev);
3860 device_unregister(&rdev->dev);
3862 EXPORT_SYMBOL_GPL(regulator_unregister);
3865 * regulator_suspend_prepare - prepare regulators for system wide suspend
3866 * @state: system suspend state
3868 * Configure each regulator with it's suspend operating parameters for state.
3869 * This will usually be called by machine suspend code prior to supending.
3871 int regulator_suspend_prepare(suspend_state_t state)
3873 struct regulator_dev *rdev;
3876 /* ON is handled by regulator active state */
3877 if (state == PM_SUSPEND_ON)
3880 mutex_lock(®ulator_list_mutex);
3881 list_for_each_entry(rdev, ®ulator_list, list) {
3883 mutex_lock(&rdev->mutex);
3884 ret = suspend_prepare(rdev, state);
3885 mutex_unlock(&rdev->mutex);
3888 rdev_err(rdev, "failed to prepare\n");
3893 mutex_unlock(®ulator_list_mutex);
3896 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3899 * regulator_suspend_finish - resume regulators from system wide suspend
3901 * Turn on regulators that might be turned off by regulator_suspend_prepare
3902 * and that should be turned on according to the regulators properties.
3904 int regulator_suspend_finish(void)
3906 struct regulator_dev *rdev;
3909 mutex_lock(®ulator_list_mutex);
3910 list_for_each_entry(rdev, ®ulator_list, list) {
3911 mutex_lock(&rdev->mutex);
3912 if (rdev->use_count > 0 || rdev->constraints->always_on) {
3913 if (!_regulator_is_enabled(rdev)) {
3914 error = _regulator_do_enable(rdev);
3919 if (!have_full_constraints())
3921 if (!_regulator_is_enabled(rdev))
3924 error = _regulator_do_disable(rdev);
3929 mutex_unlock(&rdev->mutex);
3931 mutex_unlock(®ulator_list_mutex);
3934 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3937 * regulator_has_full_constraints - the system has fully specified constraints
3939 * Calling this function will cause the regulator API to disable all
3940 * regulators which have a zero use count and don't have an always_on
3941 * constraint in a late_initcall.
3943 * The intention is that this will become the default behaviour in a
3944 * future kernel release so users are encouraged to use this facility
3947 void regulator_has_full_constraints(void)
3949 has_full_constraints = 1;
3951 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3954 * rdev_get_drvdata - get rdev regulator driver data
3957 * Get rdev regulator driver private data. This call can be used in the
3958 * regulator driver context.
3960 void *rdev_get_drvdata(struct regulator_dev *rdev)
3962 return rdev->reg_data;
3964 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3967 * regulator_get_drvdata - get regulator driver data
3968 * @regulator: regulator
3970 * Get regulator driver private data. This call can be used in the consumer
3971 * driver context when non API regulator specific functions need to be called.
3973 void *regulator_get_drvdata(struct regulator *regulator)
3975 return regulator->rdev->reg_data;
3977 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3980 * regulator_set_drvdata - set regulator driver data
3981 * @regulator: regulator
3984 void regulator_set_drvdata(struct regulator *regulator, void *data)
3986 regulator->rdev->reg_data = data;
3988 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3991 * regulator_get_id - get regulator ID
3994 int rdev_get_id(struct regulator_dev *rdev)
3996 return rdev->desc->id;
3998 EXPORT_SYMBOL_GPL(rdev_get_id);
4000 struct device *rdev_get_dev(struct regulator_dev *rdev)
4004 EXPORT_SYMBOL_GPL(rdev_get_dev);
4006 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4008 return reg_init_data->driver_data;
4010 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4012 #ifdef CONFIG_DEBUG_FS
4013 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
4014 size_t count, loff_t *ppos)
4016 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
4017 ssize_t len, ret = 0;
4018 struct regulator_map *map;
4023 list_for_each_entry(map, ®ulator_map_list, list) {
4024 len = snprintf(buf + ret, PAGE_SIZE - ret,
4026 rdev_get_name(map->regulator), map->dev_name,
4030 if (ret > PAGE_SIZE) {
4036 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4044 static const struct file_operations supply_map_fops = {
4045 #ifdef CONFIG_DEBUG_FS
4046 .read = supply_map_read_file,
4047 .llseek = default_llseek,
4051 #ifdef CONFIG_DEBUG_FS
4052 static void regulator_summary_show_subtree(struct seq_file *s,
4053 struct regulator_dev *rdev,
4056 struct list_head *list = s->private;
4057 struct regulator_dev *child;
4058 struct regulation_constraints *c;
4059 struct regulator *consumer;
4064 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4066 30 - level * 3, rdev_get_name(rdev),
4067 rdev->use_count, rdev->open_count, rdev->bypass_count);
4069 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4070 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4072 c = rdev->constraints;
4074 switch (rdev->desc->type) {
4075 case REGULATOR_VOLTAGE:
4076 seq_printf(s, "%5dmV %5dmV ",
4077 c->min_uV / 1000, c->max_uV / 1000);
4079 case REGULATOR_CURRENT:
4080 seq_printf(s, "%5dmA %5dmA ",
4081 c->min_uA / 1000, c->max_uA / 1000);
4088 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4089 if (consumer->dev->class == ®ulator_class)
4092 seq_printf(s, "%*s%-*s ",
4093 (level + 1) * 3 + 1, "",
4094 30 - (level + 1) * 3, dev_name(consumer->dev));
4096 switch (rdev->desc->type) {
4097 case REGULATOR_VOLTAGE:
4098 seq_printf(s, "%37dmV %5dmV",
4099 consumer->min_uV / 1000,
4100 consumer->max_uV / 1000);
4102 case REGULATOR_CURRENT:
4109 list_for_each_entry(child, list, list) {
4110 /* handle only non-root regulators supplied by current rdev */
4111 if (!child->supply || child->supply->rdev != rdev)
4114 regulator_summary_show_subtree(s, child, level + 1);
4118 static int regulator_summary_show(struct seq_file *s, void *data)
4120 struct list_head *list = s->private;
4121 struct regulator_dev *rdev;
4123 seq_puts(s, " regulator use open bypass voltage current min max\n");
4124 seq_puts(s, "-------------------------------------------------------------------------------\n");
4126 mutex_lock(®ulator_list_mutex);
4128 list_for_each_entry(rdev, list, list) {
4132 regulator_summary_show_subtree(s, rdev, 0);
4135 mutex_unlock(®ulator_list_mutex);
4140 static int regulator_summary_open(struct inode *inode, struct file *file)
4142 return single_open(file, regulator_summary_show, inode->i_private);
4146 static const struct file_operations regulator_summary_fops = {
4147 #ifdef CONFIG_DEBUG_FS
4148 .open = regulator_summary_open,
4150 .llseek = seq_lseek,
4151 .release = single_release,
4155 static int __init regulator_init(void)
4159 ret = class_register(®ulator_class);
4161 debugfs_root = debugfs_create_dir("regulator", NULL);
4163 pr_warn("regulator: Failed to create debugfs directory\n");
4165 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4168 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4169 ®ulator_list, ®ulator_summary_fops);
4171 regulator_dummy_init();
4176 /* init early to allow our consumers to complete system booting */
4177 core_initcall(regulator_init);
4179 static int __init regulator_late_cleanup(struct device *dev, void *data)
4181 struct regulator_dev *rdev = dev_to_rdev(dev);
4182 const struct regulator_ops *ops = rdev->desc->ops;
4183 struct regulation_constraints *c = rdev->constraints;
4186 if (c && c->always_on)
4189 if (c && !(c->valid_ops_mask & REGULATOR_CHANGE_STATUS))
4192 mutex_lock(&rdev->mutex);
4194 if (rdev->use_count)
4197 /* If we can't read the status assume it's on. */
4198 if (ops->is_enabled)
4199 enabled = ops->is_enabled(rdev);
4206 if (have_full_constraints()) {
4207 /* We log since this may kill the system if it goes
4209 rdev_info(rdev, "disabling\n");
4210 ret = _regulator_do_disable(rdev);
4212 rdev_err(rdev, "couldn't disable: %d\n", ret);
4214 /* The intention is that in future we will
4215 * assume that full constraints are provided
4216 * so warn even if we aren't going to do
4219 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4223 mutex_unlock(&rdev->mutex);
4228 static int __init regulator_init_complete(void)
4231 * Since DT doesn't provide an idiomatic mechanism for
4232 * enabling full constraints and since it's much more natural
4233 * with DT to provide them just assume that a DT enabled
4234 * system has full constraints.
4236 if (of_have_populated_dt())
4237 has_full_constraints = true;
4239 /* If we have a full configuration then disable any regulators
4240 * we have permission to change the status for and which are
4241 * not in use or always_on. This is effectively the default
4242 * for DT and ACPI as they have full constraints.
4244 class_for_each_device(®ulator_class, NULL, NULL,
4245 regulator_late_cleanup);
4249 late_initcall_sync(regulator_init_complete);