2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
27 #include <linux/gpio/consumer.h>
29 #include <linux/regmap.h>
30 #include <linux/regulator/of_regulator.h>
31 #include <linux/regulator/consumer.h>
32 #include <linux/regulator/driver.h>
33 #include <linux/regulator/machine.h>
34 #include <linux/module.h>
36 #define CREATE_TRACE_POINTS
37 #include <trace/events/regulator.h>
42 #define rdev_crit(rdev, fmt, ...) \
43 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_err(rdev, fmt, ...) \
45 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_warn(rdev, fmt, ...) \
47 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_info(rdev, fmt, ...) \
49 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50 #define rdev_dbg(rdev, fmt, ...) \
51 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
53 static DEFINE_MUTEX(regulator_list_mutex);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
59 static struct dentry *debugfs_root;
62 * struct regulator_map
64 * Used to provide symbolic supply names to devices.
66 struct regulator_map {
67 struct list_head list;
68 const char *dev_name; /* The dev_name() for the consumer */
70 struct regulator_dev *regulator;
74 * struct regulator_enable_gpio
76 * Management for shared enable GPIO pin
78 struct regulator_enable_gpio {
79 struct list_head list;
80 struct gpio_desc *gpiod;
81 u32 enable_count; /* a number of enabled shared GPIO */
82 u32 request_count; /* a number of requested shared GPIO */
83 unsigned int ena_gpio_invert:1;
87 * struct regulator_supply_alias
89 * Used to map lookups for a supply onto an alternative device.
91 struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
99 static int _regulator_is_enabled(struct regulator_dev *rdev);
100 static int _regulator_disable(struct regulator_dev *rdev);
101 static int _regulator_get_voltage(struct regulator_dev *rdev);
102 static int _regulator_get_current_limit(struct regulator_dev *rdev);
103 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104 static int _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108 static struct regulator *create_regulator(struct regulator_dev *rdev,
110 const char *supply_name);
111 static void _regulator_put(struct regulator *regulator);
113 static const char *rdev_get_name(struct regulator_dev *rdev)
115 if (rdev->constraints && rdev->constraints->name)
116 return rdev->constraints->name;
117 else if (rdev->desc->name)
118 return rdev->desc->name;
123 static bool have_full_constraints(void)
125 return has_full_constraints || of_have_populated_dt();
128 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
130 if (!rdev->constraints) {
131 rdev_err(rdev, "no constraints\n");
135 if (rdev->constraints->valid_ops_mask & ops)
141 static inline struct regulator_dev *rdev_get_supply(struct regulator_dev *rdev)
143 if (rdev && rdev->supply)
144 return rdev->supply->rdev;
150 * regulator_lock_supply - lock a regulator and its supplies
151 * @rdev: regulator source
153 static void regulator_lock_supply(struct regulator_dev *rdev)
157 for (i = 0; rdev; rdev = rdev_get_supply(rdev), i++)
158 mutex_lock_nested(&rdev->mutex, i);
162 * regulator_unlock_supply - unlock a regulator and its supplies
163 * @rdev: regulator source
165 static void regulator_unlock_supply(struct regulator_dev *rdev)
167 struct regulator *supply;
170 mutex_unlock(&rdev->mutex);
171 supply = rdev->supply;
181 * of_get_regulator - get a regulator device node based on supply name
182 * @dev: Device pointer for the consumer (of regulator) device
183 * @supply: regulator supply name
185 * Extract the regulator device node corresponding to the supply name.
186 * returns the device node corresponding to the regulator if found, else
189 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
191 struct device_node *regnode = NULL;
192 char prop_name[32]; /* 32 is max size of property name */
194 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
196 snprintf(prop_name, 32, "%s-supply", supply);
197 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
200 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
201 prop_name, dev->of_node);
207 /* Platform voltage constraint check */
208 static int regulator_check_voltage(struct regulator_dev *rdev,
209 int *min_uV, int *max_uV)
211 BUG_ON(*min_uV > *max_uV);
213 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
214 rdev_err(rdev, "voltage operation not allowed\n");
218 if (*max_uV > rdev->constraints->max_uV)
219 *max_uV = rdev->constraints->max_uV;
220 if (*min_uV < rdev->constraints->min_uV)
221 *min_uV = rdev->constraints->min_uV;
223 if (*min_uV > *max_uV) {
224 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
232 /* return 0 if the state is valid */
233 static int regulator_check_states(suspend_state_t state)
235 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
238 /* Make sure we select a voltage that suits the needs of all
239 * regulator consumers
241 static int regulator_check_consumers(struct regulator_dev *rdev,
242 int *min_uV, int *max_uV,
243 suspend_state_t state)
245 struct regulator *regulator;
246 struct regulator_voltage *voltage;
248 list_for_each_entry(regulator, &rdev->consumer_list, list) {
249 voltage = ®ulator->voltage[state];
251 * Assume consumers that didn't say anything are OK
252 * with anything in the constraint range.
254 if (!voltage->min_uV && !voltage->max_uV)
257 if (*max_uV > voltage->max_uV)
258 *max_uV = voltage->max_uV;
259 if (*min_uV < voltage->min_uV)
260 *min_uV = voltage->min_uV;
263 if (*min_uV > *max_uV) {
264 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
272 /* current constraint check */
273 static int regulator_check_current_limit(struct regulator_dev *rdev,
274 int *min_uA, int *max_uA)
276 BUG_ON(*min_uA > *max_uA);
278 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
279 rdev_err(rdev, "current operation not allowed\n");
283 if (*max_uA > rdev->constraints->max_uA)
284 *max_uA = rdev->constraints->max_uA;
285 if (*min_uA < rdev->constraints->min_uA)
286 *min_uA = rdev->constraints->min_uA;
288 if (*min_uA > *max_uA) {
289 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
297 /* operating mode constraint check */
298 static int regulator_mode_constrain(struct regulator_dev *rdev,
302 case REGULATOR_MODE_FAST:
303 case REGULATOR_MODE_NORMAL:
304 case REGULATOR_MODE_IDLE:
305 case REGULATOR_MODE_STANDBY:
308 rdev_err(rdev, "invalid mode %x specified\n", *mode);
312 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
313 rdev_err(rdev, "mode operation not allowed\n");
317 /* The modes are bitmasks, the most power hungry modes having
318 * the lowest values. If the requested mode isn't supported
319 * try higher modes. */
321 if (rdev->constraints->valid_modes_mask & *mode)
329 static inline struct regulator_state *
330 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
332 if (rdev->constraints == NULL)
336 case PM_SUSPEND_STANDBY:
337 return &rdev->constraints->state_standby;
339 return &rdev->constraints->state_mem;
341 return &rdev->constraints->state_disk;
347 static ssize_t regulator_uV_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
350 struct regulator_dev *rdev = dev_get_drvdata(dev);
353 mutex_lock(&rdev->mutex);
354 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
355 mutex_unlock(&rdev->mutex);
359 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
361 static ssize_t regulator_uA_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
364 struct regulator_dev *rdev = dev_get_drvdata(dev);
366 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
368 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
370 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
373 struct regulator_dev *rdev = dev_get_drvdata(dev);
375 return sprintf(buf, "%s\n", rdev_get_name(rdev));
377 static DEVICE_ATTR_RO(name);
379 static ssize_t regulator_print_opmode(char *buf, int mode)
382 case REGULATOR_MODE_FAST:
383 return sprintf(buf, "fast\n");
384 case REGULATOR_MODE_NORMAL:
385 return sprintf(buf, "normal\n");
386 case REGULATOR_MODE_IDLE:
387 return sprintf(buf, "idle\n");
388 case REGULATOR_MODE_STANDBY:
389 return sprintf(buf, "standby\n");
391 return sprintf(buf, "unknown\n");
394 static ssize_t regulator_opmode_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
397 struct regulator_dev *rdev = dev_get_drvdata(dev);
399 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
401 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
403 static ssize_t regulator_print_state(char *buf, int state)
406 return sprintf(buf, "enabled\n");
408 return sprintf(buf, "disabled\n");
410 return sprintf(buf, "unknown\n");
413 static ssize_t regulator_state_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
416 struct regulator_dev *rdev = dev_get_drvdata(dev);
419 mutex_lock(&rdev->mutex);
420 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
421 mutex_unlock(&rdev->mutex);
425 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
427 static ssize_t regulator_status_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
430 struct regulator_dev *rdev = dev_get_drvdata(dev);
434 status = rdev->desc->ops->get_status(rdev);
439 case REGULATOR_STATUS_OFF:
442 case REGULATOR_STATUS_ON:
445 case REGULATOR_STATUS_ERROR:
448 case REGULATOR_STATUS_FAST:
451 case REGULATOR_STATUS_NORMAL:
454 case REGULATOR_STATUS_IDLE:
457 case REGULATOR_STATUS_STANDBY:
460 case REGULATOR_STATUS_BYPASS:
463 case REGULATOR_STATUS_UNDEFINED:
470 return sprintf(buf, "%s\n", label);
472 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
474 static ssize_t regulator_min_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
479 if (!rdev->constraints)
480 return sprintf(buf, "constraint not defined\n");
482 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
484 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
486 static ssize_t regulator_max_uA_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
489 struct regulator_dev *rdev = dev_get_drvdata(dev);
491 if (!rdev->constraints)
492 return sprintf(buf, "constraint not defined\n");
494 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
496 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
498 static ssize_t regulator_min_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
501 struct regulator_dev *rdev = dev_get_drvdata(dev);
503 if (!rdev->constraints)
504 return sprintf(buf, "constraint not defined\n");
506 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
508 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
510 static ssize_t regulator_max_uV_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct regulator_dev *rdev = dev_get_drvdata(dev);
515 if (!rdev->constraints)
516 return sprintf(buf, "constraint not defined\n");
518 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
520 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
522 static ssize_t regulator_total_uA_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
525 struct regulator_dev *rdev = dev_get_drvdata(dev);
526 struct regulator *regulator;
529 mutex_lock(&rdev->mutex);
530 list_for_each_entry(regulator, &rdev->consumer_list, list)
531 uA += regulator->uA_load;
532 mutex_unlock(&rdev->mutex);
533 return sprintf(buf, "%d\n", uA);
535 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
537 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
541 return sprintf(buf, "%d\n", rdev->use_count);
543 static DEVICE_ATTR_RO(num_users);
545 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
548 struct regulator_dev *rdev = dev_get_drvdata(dev);
550 switch (rdev->desc->type) {
551 case REGULATOR_VOLTAGE:
552 return sprintf(buf, "voltage\n");
553 case REGULATOR_CURRENT:
554 return sprintf(buf, "current\n");
556 return sprintf(buf, "unknown\n");
558 static DEVICE_ATTR_RO(type);
560 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
563 struct regulator_dev *rdev = dev_get_drvdata(dev);
565 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
567 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
568 regulator_suspend_mem_uV_show, NULL);
570 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
575 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
577 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
578 regulator_suspend_disk_uV_show, NULL);
580 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
581 struct device_attribute *attr, char *buf)
583 struct regulator_dev *rdev = dev_get_drvdata(dev);
585 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
587 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
588 regulator_suspend_standby_uV_show, NULL);
590 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
591 struct device_attribute *attr, char *buf)
593 struct regulator_dev *rdev = dev_get_drvdata(dev);
595 return regulator_print_opmode(buf,
596 rdev->constraints->state_mem.mode);
598 static DEVICE_ATTR(suspend_mem_mode, 0444,
599 regulator_suspend_mem_mode_show, NULL);
601 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct regulator_dev *rdev = dev_get_drvdata(dev);
606 return regulator_print_opmode(buf,
607 rdev->constraints->state_disk.mode);
609 static DEVICE_ATTR(suspend_disk_mode, 0444,
610 regulator_suspend_disk_mode_show, NULL);
612 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
613 struct device_attribute *attr, char *buf)
615 struct regulator_dev *rdev = dev_get_drvdata(dev);
617 return regulator_print_opmode(buf,
618 rdev->constraints->state_standby.mode);
620 static DEVICE_ATTR(suspend_standby_mode, 0444,
621 regulator_suspend_standby_mode_show, NULL);
623 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
626 struct regulator_dev *rdev = dev_get_drvdata(dev);
628 return regulator_print_state(buf,
629 rdev->constraints->state_mem.enabled);
631 static DEVICE_ATTR(suspend_mem_state, 0444,
632 regulator_suspend_mem_state_show, NULL);
634 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
637 struct regulator_dev *rdev = dev_get_drvdata(dev);
639 return regulator_print_state(buf,
640 rdev->constraints->state_disk.enabled);
642 static DEVICE_ATTR(suspend_disk_state, 0444,
643 regulator_suspend_disk_state_show, NULL);
645 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
646 struct device_attribute *attr, char *buf)
648 struct regulator_dev *rdev = dev_get_drvdata(dev);
650 return regulator_print_state(buf,
651 rdev->constraints->state_standby.enabled);
653 static DEVICE_ATTR(suspend_standby_state, 0444,
654 regulator_suspend_standby_state_show, NULL);
656 static ssize_t regulator_bypass_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
659 struct regulator_dev *rdev = dev_get_drvdata(dev);
664 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
673 return sprintf(buf, "%s\n", report);
675 static DEVICE_ATTR(bypass, 0444,
676 regulator_bypass_show, NULL);
678 /* Calculate the new optimum regulator operating mode based on the new total
679 * consumer load. All locks held by caller */
680 static int drms_uA_update(struct regulator_dev *rdev)
682 struct regulator *sibling;
683 int current_uA = 0, output_uV, input_uV, err;
686 lockdep_assert_held_once(&rdev->mutex);
689 * first check to see if we can set modes at all, otherwise just
690 * tell the consumer everything is OK.
692 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
695 if (!rdev->desc->ops->get_optimum_mode &&
696 !rdev->desc->ops->set_load)
699 if (!rdev->desc->ops->set_mode &&
700 !rdev->desc->ops->set_load)
703 /* calc total requested load */
704 list_for_each_entry(sibling, &rdev->consumer_list, list)
705 current_uA += sibling->uA_load;
707 current_uA += rdev->constraints->system_load;
709 if (rdev->desc->ops->set_load) {
710 /* set the optimum mode for our new total regulator load */
711 err = rdev->desc->ops->set_load(rdev, current_uA);
713 rdev_err(rdev, "failed to set load %d\n", current_uA);
715 /* get output voltage */
716 output_uV = _regulator_get_voltage(rdev);
717 if (output_uV <= 0) {
718 rdev_err(rdev, "invalid output voltage found\n");
722 /* get input voltage */
725 input_uV = regulator_get_voltage(rdev->supply);
727 input_uV = rdev->constraints->input_uV;
729 rdev_err(rdev, "invalid input voltage found\n");
733 /* now get the optimum mode for our new total regulator load */
734 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
735 output_uV, current_uA);
737 /* check the new mode is allowed */
738 err = regulator_mode_constrain(rdev, &mode);
740 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
741 current_uA, input_uV, output_uV);
745 err = rdev->desc->ops->set_mode(rdev, mode);
747 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
753 static int suspend_set_state(struct regulator_dev *rdev,
754 suspend_state_t state)
757 struct regulator_state *rstate;
759 rstate = regulator_get_suspend_state(rdev, state);
763 /* If we have no suspend mode configration don't set anything;
764 * only warn if the driver implements set_suspend_voltage or
765 * set_suspend_mode callback.
767 if (rstate->enabled != ENABLE_IN_SUSPEND &&
768 rstate->enabled != DISABLE_IN_SUSPEND) {
769 if (rdev->desc->ops->set_suspend_voltage ||
770 rdev->desc->ops->set_suspend_mode)
771 rdev_warn(rdev, "No configuration\n");
775 if (rstate->enabled == ENABLE_IN_SUSPEND &&
776 rdev->desc->ops->set_suspend_enable)
777 ret = rdev->desc->ops->set_suspend_enable(rdev);
778 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
779 rdev->desc->ops->set_suspend_disable)
780 ret = rdev->desc->ops->set_suspend_disable(rdev);
781 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
785 rdev_err(rdev, "failed to enabled/disable\n");
789 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
790 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
792 rdev_err(rdev, "failed to set voltage\n");
797 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
798 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
800 rdev_err(rdev, "failed to set mode\n");
808 static void print_constraints(struct regulator_dev *rdev)
810 struct regulation_constraints *constraints = rdev->constraints;
812 size_t len = sizeof(buf) - 1;
816 if (constraints->min_uV && constraints->max_uV) {
817 if (constraints->min_uV == constraints->max_uV)
818 count += scnprintf(buf + count, len - count, "%d mV ",
819 constraints->min_uV / 1000);
821 count += scnprintf(buf + count, len - count,
823 constraints->min_uV / 1000,
824 constraints->max_uV / 1000);
827 if (!constraints->min_uV ||
828 constraints->min_uV != constraints->max_uV) {
829 ret = _regulator_get_voltage(rdev);
831 count += scnprintf(buf + count, len - count,
832 "at %d mV ", ret / 1000);
835 if (constraints->uV_offset)
836 count += scnprintf(buf + count, len - count, "%dmV offset ",
837 constraints->uV_offset / 1000);
839 if (constraints->min_uA && constraints->max_uA) {
840 if (constraints->min_uA == constraints->max_uA)
841 count += scnprintf(buf + count, len - count, "%d mA ",
842 constraints->min_uA / 1000);
844 count += scnprintf(buf + count, len - count,
846 constraints->min_uA / 1000,
847 constraints->max_uA / 1000);
850 if (!constraints->min_uA ||
851 constraints->min_uA != constraints->max_uA) {
852 ret = _regulator_get_current_limit(rdev);
854 count += scnprintf(buf + count, len - count,
855 "at %d mA ", ret / 1000);
858 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
859 count += scnprintf(buf + count, len - count, "fast ");
860 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
861 count += scnprintf(buf + count, len - count, "normal ");
862 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
863 count += scnprintf(buf + count, len - count, "idle ");
864 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
865 count += scnprintf(buf + count, len - count, "standby");
868 scnprintf(buf, len, "no parameters");
870 rdev_dbg(rdev, "%s\n", buf);
872 if ((constraints->min_uV != constraints->max_uV) &&
873 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
875 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
878 static int machine_constraints_voltage(struct regulator_dev *rdev,
879 struct regulation_constraints *constraints)
881 const struct regulator_ops *ops = rdev->desc->ops;
884 /* do we need to apply the constraint voltage */
885 if (rdev->constraints->apply_uV &&
886 rdev->constraints->min_uV && rdev->constraints->max_uV) {
887 int target_min, target_max;
888 int current_uV = _regulator_get_voltage(rdev);
889 if (current_uV < 0) {
891 "failed to get the current voltage(%d)\n",
897 * If we're below the minimum voltage move up to the
898 * minimum voltage, if we're above the maximum voltage
899 * then move down to the maximum.
901 target_min = current_uV;
902 target_max = current_uV;
904 if (current_uV < rdev->constraints->min_uV) {
905 target_min = rdev->constraints->min_uV;
906 target_max = rdev->constraints->min_uV;
909 if (current_uV > rdev->constraints->max_uV) {
910 target_min = rdev->constraints->max_uV;
911 target_max = rdev->constraints->max_uV;
914 if (target_min != current_uV || target_max != current_uV) {
915 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
916 current_uV, target_min, target_max);
917 ret = _regulator_do_set_voltage(
918 rdev, target_min, target_max);
921 "failed to apply %d-%duV constraint(%d)\n",
922 target_min, target_max, ret);
928 /* constrain machine-level voltage specs to fit
929 * the actual range supported by this regulator.
931 if (ops->list_voltage && rdev->desc->n_voltages) {
932 int count = rdev->desc->n_voltages;
934 int min_uV = INT_MAX;
935 int max_uV = INT_MIN;
936 int cmin = constraints->min_uV;
937 int cmax = constraints->max_uV;
939 /* it's safe to autoconfigure fixed-voltage supplies
940 and the constraints are used by list_voltage. */
941 if (count == 1 && !cmin) {
944 constraints->min_uV = cmin;
945 constraints->max_uV = cmax;
948 /* voltage constraints are optional */
949 if ((cmin == 0) && (cmax == 0))
952 /* else require explicit machine-level constraints */
953 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
954 rdev_err(rdev, "invalid voltage constraints\n");
958 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
959 for (i = 0; i < count; i++) {
962 value = ops->list_voltage(rdev, i);
966 /* maybe adjust [min_uV..max_uV] */
967 if (value >= cmin && value < min_uV)
969 if (value <= cmax && value > max_uV)
973 /* final: [min_uV..max_uV] valid iff constraints valid */
974 if (max_uV < min_uV) {
976 "unsupportable voltage constraints %u-%uuV\n",
981 /* use regulator's subset of machine constraints */
982 if (constraints->min_uV < min_uV) {
983 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
984 constraints->min_uV, min_uV);
985 constraints->min_uV = min_uV;
987 if (constraints->max_uV > max_uV) {
988 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
989 constraints->max_uV, max_uV);
990 constraints->max_uV = max_uV;
997 static int machine_constraints_current(struct regulator_dev *rdev,
998 struct regulation_constraints *constraints)
1000 const struct regulator_ops *ops = rdev->desc->ops;
1003 if (!constraints->min_uA && !constraints->max_uA)
1006 if (constraints->min_uA > constraints->max_uA) {
1007 rdev_err(rdev, "Invalid current constraints\n");
1011 if (!ops->set_current_limit || !ops->get_current_limit) {
1012 rdev_warn(rdev, "Operation of current configuration missing\n");
1016 /* Set regulator current in constraints range */
1017 ret = ops->set_current_limit(rdev, constraints->min_uA,
1018 constraints->max_uA);
1020 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1027 static int _regulator_do_enable(struct regulator_dev *rdev);
1030 * set_machine_constraints - sets regulator constraints
1031 * @rdev: regulator source
1032 * @constraints: constraints to apply
1034 * Allows platform initialisation code to define and constrain
1035 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1036 * Constraints *must* be set by platform code in order for some
1037 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1040 static int set_machine_constraints(struct regulator_dev *rdev,
1041 const struct regulation_constraints *constraints)
1044 const struct regulator_ops *ops = rdev->desc->ops;
1047 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1050 rdev->constraints = kzalloc(sizeof(*constraints),
1052 if (!rdev->constraints)
1055 ret = machine_constraints_voltage(rdev, rdev->constraints);
1059 ret = machine_constraints_current(rdev, rdev->constraints);
1063 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1064 ret = ops->set_input_current_limit(rdev,
1065 rdev->constraints->ilim_uA);
1067 rdev_err(rdev, "failed to set input limit\n");
1072 /* do we need to setup our suspend state */
1073 if (rdev->constraints->initial_state) {
1074 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1076 rdev_err(rdev, "failed to set suspend state\n");
1081 if (rdev->constraints->initial_mode) {
1082 if (!ops->set_mode) {
1083 rdev_err(rdev, "no set_mode operation\n");
1087 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1089 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1094 /* If the constraints say the regulator should be on at this point
1095 * and we have control then make sure it is enabled.
1097 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1098 ret = _regulator_do_enable(rdev);
1099 if (ret < 0 && ret != -EINVAL) {
1100 rdev_err(rdev, "failed to enable\n");
1105 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1106 && ops->set_ramp_delay) {
1107 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1109 rdev_err(rdev, "failed to set ramp_delay\n");
1114 if (rdev->constraints->pull_down && ops->set_pull_down) {
1115 ret = ops->set_pull_down(rdev);
1117 rdev_err(rdev, "failed to set pull down\n");
1122 if (rdev->constraints->soft_start && ops->set_soft_start) {
1123 ret = ops->set_soft_start(rdev);
1125 rdev_err(rdev, "failed to set soft start\n");
1130 if (rdev->constraints->over_current_protection
1131 && ops->set_over_current_protection) {
1132 ret = ops->set_over_current_protection(rdev);
1134 rdev_err(rdev, "failed to set over current protection\n");
1139 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1140 bool ad_state = (rdev->constraints->active_discharge ==
1141 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1143 ret = ops->set_active_discharge(rdev, ad_state);
1145 rdev_err(rdev, "failed to set active discharge\n");
1150 print_constraints(rdev);
1155 * set_supply - set regulator supply regulator
1156 * @rdev: regulator name
1157 * @supply_rdev: supply regulator name
1159 * Called by platform initialisation code to set the supply regulator for this
1160 * regulator. This ensures that a regulators supply will also be enabled by the
1161 * core if it's child is enabled.
1163 static int set_supply(struct regulator_dev *rdev,
1164 struct regulator_dev *supply_rdev)
1168 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1170 if (!try_module_get(supply_rdev->owner))
1173 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1174 if (rdev->supply == NULL) {
1178 supply_rdev->open_count++;
1184 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1185 * @rdev: regulator source
1186 * @consumer_dev_name: dev_name() string for device supply applies to
1187 * @supply: symbolic name for supply
1189 * Allows platform initialisation code to map physical regulator
1190 * sources to symbolic names for supplies for use by devices. Devices
1191 * should use these symbolic names to request regulators, avoiding the
1192 * need to provide board-specific regulator names as platform data.
1194 static int set_consumer_device_supply(struct regulator_dev *rdev,
1195 const char *consumer_dev_name,
1198 struct regulator_map *node;
1204 if (consumer_dev_name != NULL)
1209 list_for_each_entry(node, ®ulator_map_list, list) {
1210 if (node->dev_name && consumer_dev_name) {
1211 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1213 } else if (node->dev_name || consumer_dev_name) {
1217 if (strcmp(node->supply, supply) != 0)
1220 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1222 dev_name(&node->regulator->dev),
1223 node->regulator->desc->name,
1225 dev_name(&rdev->dev), rdev_get_name(rdev));
1229 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1233 node->regulator = rdev;
1234 node->supply = supply;
1237 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1238 if (node->dev_name == NULL) {
1244 list_add(&node->list, ®ulator_map_list);
1248 static void unset_regulator_supplies(struct regulator_dev *rdev)
1250 struct regulator_map *node, *n;
1252 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1253 if (rdev == node->regulator) {
1254 list_del(&node->list);
1255 kfree(node->dev_name);
1261 #ifdef CONFIG_DEBUG_FS
1262 static ssize_t constraint_flags_read_file(struct file *file,
1263 char __user *user_buf,
1264 size_t count, loff_t *ppos)
1266 const struct regulator *regulator = file->private_data;
1267 const struct regulation_constraints *c = regulator->rdev->constraints;
1274 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1278 ret = snprintf(buf, PAGE_SIZE,
1282 "ramp_disable: %u\n"
1285 "over_current_protection: %u\n",
1292 c->over_current_protection);
1294 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1302 static const struct file_operations constraint_flags_fops = {
1303 #ifdef CONFIG_DEBUG_FS
1304 .open = simple_open,
1305 .read = constraint_flags_read_file,
1306 .llseek = default_llseek,
1310 #define REG_STR_SIZE 64
1312 static struct regulator *create_regulator(struct regulator_dev *rdev,
1314 const char *supply_name)
1316 struct regulator *regulator;
1317 char buf[REG_STR_SIZE];
1320 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1321 if (regulator == NULL)
1324 mutex_lock(&rdev->mutex);
1325 regulator->rdev = rdev;
1326 list_add(®ulator->list, &rdev->consumer_list);
1329 regulator->dev = dev;
1331 /* Add a link to the device sysfs entry */
1332 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1333 dev->kobj.name, supply_name);
1334 if (size >= REG_STR_SIZE)
1337 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1338 if (regulator->supply_name == NULL)
1341 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1344 rdev_dbg(rdev, "could not add device link %s err %d\n",
1345 dev->kobj.name, err);
1349 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1350 if (regulator->supply_name == NULL)
1354 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1356 if (!regulator->debugfs) {
1357 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1359 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1360 ®ulator->uA_load);
1361 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1362 ®ulator->voltage[PM_SUSPEND_ON].min_uV);
1363 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1364 ®ulator->voltage[PM_SUSPEND_ON].max_uV);
1365 debugfs_create_file("constraint_flags", 0444,
1366 regulator->debugfs, regulator,
1367 &constraint_flags_fops);
1371 * Check now if the regulator is an always on regulator - if
1372 * it is then we don't need to do nearly so much work for
1373 * enable/disable calls.
1375 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1376 _regulator_is_enabled(rdev))
1377 regulator->always_on = true;
1379 mutex_unlock(&rdev->mutex);
1382 list_del(®ulator->list);
1384 mutex_unlock(&rdev->mutex);
1388 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1390 if (rdev->constraints && rdev->constraints->enable_time)
1391 return rdev->constraints->enable_time;
1392 if (!rdev->desc->ops->enable_time)
1393 return rdev->desc->enable_time;
1394 return rdev->desc->ops->enable_time(rdev);
1397 static struct regulator_supply_alias *regulator_find_supply_alias(
1398 struct device *dev, const char *supply)
1400 struct regulator_supply_alias *map;
1402 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1403 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1409 static void regulator_supply_alias(struct device **dev, const char **supply)
1411 struct regulator_supply_alias *map;
1413 map = regulator_find_supply_alias(*dev, *supply);
1415 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1416 *supply, map->alias_supply,
1417 dev_name(map->alias_dev));
1418 *dev = map->alias_dev;
1419 *supply = map->alias_supply;
1423 static int regulator_match(struct device *dev, const void *data)
1425 struct regulator_dev *r = dev_to_rdev(dev);
1427 return strcmp(rdev_get_name(r), data) == 0;
1430 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1434 dev = class_find_device(®ulator_class, NULL, name, regulator_match);
1436 return dev ? dev_to_rdev(dev) : NULL;
1440 * regulator_dev_lookup - lookup a regulator device.
1441 * @dev: device for regulator "consumer".
1442 * @supply: Supply name or regulator ID.
1444 * If successful, returns a struct regulator_dev that corresponds to the name
1445 * @supply and with the embedded struct device refcount incremented by one.
1446 * The refcount must be dropped by calling put_device().
1447 * On failure one of the following ERR-PTR-encoded values is returned:
1448 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1451 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1454 struct regulator_dev *r = NULL;
1455 struct device_node *node;
1456 struct regulator_map *map;
1457 const char *devname = NULL;
1459 regulator_supply_alias(&dev, &supply);
1461 /* first do a dt based lookup */
1462 if (dev && dev->of_node) {
1463 node = of_get_regulator(dev, supply);
1465 r = of_find_regulator_by_node(node);
1470 * We have a node, but there is no device.
1471 * assume it has not registered yet.
1473 return ERR_PTR(-EPROBE_DEFER);
1477 /* if not found, try doing it non-dt way */
1479 devname = dev_name(dev);
1481 mutex_lock(®ulator_list_mutex);
1482 list_for_each_entry(map, ®ulator_map_list, list) {
1483 /* If the mapping has a device set up it must match */
1484 if (map->dev_name &&
1485 (!devname || strcmp(map->dev_name, devname)))
1488 if (strcmp(map->supply, supply) == 0 &&
1489 get_device(&map->regulator->dev)) {
1494 mutex_unlock(®ulator_list_mutex);
1499 r = regulator_lookup_by_name(supply);
1503 return ERR_PTR(-ENODEV);
1506 static int regulator_resolve_supply(struct regulator_dev *rdev)
1508 struct regulator_dev *r;
1509 struct device *dev = rdev->dev.parent;
1512 /* No supply to resovle? */
1513 if (!rdev->supply_name)
1516 /* Supply already resolved? */
1520 r = regulator_dev_lookup(dev, rdev->supply_name);
1524 /* Did the lookup explicitly defer for us? */
1525 if (ret == -EPROBE_DEFER)
1528 if (have_full_constraints()) {
1529 r = dummy_regulator_rdev;
1530 get_device(&r->dev);
1532 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1533 rdev->supply_name, rdev->desc->name);
1534 return -EPROBE_DEFER;
1539 * If the supply's parent device is not the same as the
1540 * regulator's parent device, then ensure the parent device
1541 * is bound before we resolve the supply, in case the parent
1542 * device get probe deferred and unregisters the supply.
1544 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1545 if (!device_is_bound(r->dev.parent)) {
1546 put_device(&r->dev);
1547 return -EPROBE_DEFER;
1551 /* Recursively resolve the supply of the supply */
1552 ret = regulator_resolve_supply(r);
1554 put_device(&r->dev);
1558 ret = set_supply(rdev, r);
1560 put_device(&r->dev);
1564 /* Cascade always-on state to supply */
1565 if (_regulator_is_enabled(rdev)) {
1566 ret = regulator_enable(rdev->supply);
1568 _regulator_put(rdev->supply);
1569 rdev->supply = NULL;
1577 /* Internal regulator request function */
1578 struct regulator *_regulator_get(struct device *dev, const char *id,
1579 enum regulator_get_type get_type)
1581 struct regulator_dev *rdev;
1582 struct regulator *regulator;
1583 const char *devname = dev ? dev_name(dev) : "deviceless";
1586 if (get_type >= MAX_GET_TYPE) {
1587 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1588 return ERR_PTR(-EINVAL);
1592 pr_err("get() with no identifier\n");
1593 return ERR_PTR(-EINVAL);
1596 rdev = regulator_dev_lookup(dev, id);
1598 ret = PTR_ERR(rdev);
1601 * If regulator_dev_lookup() fails with error other
1602 * than -ENODEV our job here is done, we simply return it.
1605 return ERR_PTR(ret);
1607 if (!have_full_constraints()) {
1609 "incomplete constraints, dummy supplies not allowed\n");
1610 return ERR_PTR(-ENODEV);
1616 * Assume that a regulator is physically present and
1617 * enabled, even if it isn't hooked up, and just
1621 "%s supply %s not found, using dummy regulator\n",
1623 rdev = dummy_regulator_rdev;
1624 get_device(&rdev->dev);
1629 "dummy supplies not allowed for exclusive requests\n");
1633 return ERR_PTR(-ENODEV);
1637 if (rdev->exclusive) {
1638 regulator = ERR_PTR(-EPERM);
1639 put_device(&rdev->dev);
1643 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1644 regulator = ERR_PTR(-EBUSY);
1645 put_device(&rdev->dev);
1649 ret = regulator_resolve_supply(rdev);
1651 regulator = ERR_PTR(ret);
1652 put_device(&rdev->dev);
1656 if (!try_module_get(rdev->owner)) {
1657 regulator = ERR_PTR(-EPROBE_DEFER);
1658 put_device(&rdev->dev);
1662 regulator = create_regulator(rdev, dev, id);
1663 if (regulator == NULL) {
1664 regulator = ERR_PTR(-ENOMEM);
1665 put_device(&rdev->dev);
1666 module_put(rdev->owner);
1671 if (get_type == EXCLUSIVE_GET) {
1672 rdev->exclusive = 1;
1674 ret = _regulator_is_enabled(rdev);
1676 rdev->use_count = 1;
1678 rdev->use_count = 0;
1685 * regulator_get - lookup and obtain a reference to a regulator.
1686 * @dev: device for regulator "consumer"
1687 * @id: Supply name or regulator ID.
1689 * Returns a struct regulator corresponding to the regulator producer,
1690 * or IS_ERR() condition containing errno.
1692 * Use of supply names configured via regulator_set_device_supply() is
1693 * strongly encouraged. It is recommended that the supply name used
1694 * should match the name used for the supply and/or the relevant
1695 * device pins in the datasheet.
1697 struct regulator *regulator_get(struct device *dev, const char *id)
1699 return _regulator_get(dev, id, NORMAL_GET);
1701 EXPORT_SYMBOL_GPL(regulator_get);
1704 * regulator_get_exclusive - obtain exclusive access to a regulator.
1705 * @dev: device for regulator "consumer"
1706 * @id: Supply name or regulator ID.
1708 * Returns a struct regulator corresponding to the regulator producer,
1709 * or IS_ERR() condition containing errno. Other consumers will be
1710 * unable to obtain this regulator while this reference is held and the
1711 * use count for the regulator will be initialised to reflect the current
1712 * state of the regulator.
1714 * This is intended for use by consumers which cannot tolerate shared
1715 * use of the regulator such as those which need to force the
1716 * regulator off for correct operation of the hardware they are
1719 * Use of supply names configured via regulator_set_device_supply() is
1720 * strongly encouraged. It is recommended that the supply name used
1721 * should match the name used for the supply and/or the relevant
1722 * device pins in the datasheet.
1724 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1726 return _regulator_get(dev, id, EXCLUSIVE_GET);
1728 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1731 * regulator_get_optional - obtain optional access to a regulator.
1732 * @dev: device for regulator "consumer"
1733 * @id: Supply name or regulator ID.
1735 * Returns a struct regulator corresponding to the regulator producer,
1736 * or IS_ERR() condition containing errno.
1738 * This is intended for use by consumers for devices which can have
1739 * some supplies unconnected in normal use, such as some MMC devices.
1740 * It can allow the regulator core to provide stub supplies for other
1741 * supplies requested using normal regulator_get() calls without
1742 * disrupting the operation of drivers that can handle absent
1745 * Use of supply names configured via regulator_set_device_supply() is
1746 * strongly encouraged. It is recommended that the supply name used
1747 * should match the name used for the supply and/or the relevant
1748 * device pins in the datasheet.
1750 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1752 return _regulator_get(dev, id, OPTIONAL_GET);
1754 EXPORT_SYMBOL_GPL(regulator_get_optional);
1756 /* regulator_list_mutex lock held by regulator_put() */
1757 static void _regulator_put(struct regulator *regulator)
1759 struct regulator_dev *rdev;
1761 if (IS_ERR_OR_NULL(regulator))
1764 lockdep_assert_held_once(®ulator_list_mutex);
1766 rdev = regulator->rdev;
1768 debugfs_remove_recursive(regulator->debugfs);
1770 /* remove any sysfs entries */
1772 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1773 mutex_lock(&rdev->mutex);
1774 list_del(®ulator->list);
1777 rdev->exclusive = 0;
1778 put_device(&rdev->dev);
1779 mutex_unlock(&rdev->mutex);
1781 kfree_const(regulator->supply_name);
1784 module_put(rdev->owner);
1788 * regulator_put - "free" the regulator source
1789 * @regulator: regulator source
1791 * Note: drivers must ensure that all regulator_enable calls made on this
1792 * regulator source are balanced by regulator_disable calls prior to calling
1795 void regulator_put(struct regulator *regulator)
1797 mutex_lock(®ulator_list_mutex);
1798 _regulator_put(regulator);
1799 mutex_unlock(®ulator_list_mutex);
1801 EXPORT_SYMBOL_GPL(regulator_put);
1804 * regulator_register_supply_alias - Provide device alias for supply lookup
1806 * @dev: device that will be given as the regulator "consumer"
1807 * @id: Supply name or regulator ID
1808 * @alias_dev: device that should be used to lookup the supply
1809 * @alias_id: Supply name or regulator ID that should be used to lookup the
1812 * All lookups for id on dev will instead be conducted for alias_id on
1815 int regulator_register_supply_alias(struct device *dev, const char *id,
1816 struct device *alias_dev,
1817 const char *alias_id)
1819 struct regulator_supply_alias *map;
1821 map = regulator_find_supply_alias(dev, id);
1825 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1830 map->src_supply = id;
1831 map->alias_dev = alias_dev;
1832 map->alias_supply = alias_id;
1834 list_add(&map->list, ®ulator_supply_alias_list);
1836 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1837 id, dev_name(dev), alias_id, dev_name(alias_dev));
1841 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1844 * regulator_unregister_supply_alias - Remove device alias
1846 * @dev: device that will be given as the regulator "consumer"
1847 * @id: Supply name or regulator ID
1849 * Remove a lookup alias if one exists for id on dev.
1851 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1853 struct regulator_supply_alias *map;
1855 map = regulator_find_supply_alias(dev, id);
1857 list_del(&map->list);
1861 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1864 * regulator_bulk_register_supply_alias - register multiple aliases
1866 * @dev: device that will be given as the regulator "consumer"
1867 * @id: List of supply names or regulator IDs
1868 * @alias_dev: device that should be used to lookup the supply
1869 * @alias_id: List of supply names or regulator IDs that should be used to
1871 * @num_id: Number of aliases to register
1873 * @return 0 on success, an errno on failure.
1875 * This helper function allows drivers to register several supply
1876 * aliases in one operation. If any of the aliases cannot be
1877 * registered any aliases that were registered will be removed
1878 * before returning to the caller.
1880 int regulator_bulk_register_supply_alias(struct device *dev,
1881 const char *const *id,
1882 struct device *alias_dev,
1883 const char *const *alias_id,
1889 for (i = 0; i < num_id; ++i) {
1890 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1900 "Failed to create supply alias %s,%s -> %s,%s\n",
1901 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1904 regulator_unregister_supply_alias(dev, id[i]);
1908 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1911 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1913 * @dev: device that will be given as the regulator "consumer"
1914 * @id: List of supply names or regulator IDs
1915 * @num_id: Number of aliases to unregister
1917 * This helper function allows drivers to unregister several supply
1918 * aliases in one operation.
1920 void regulator_bulk_unregister_supply_alias(struct device *dev,
1921 const char *const *id,
1926 for (i = 0; i < num_id; ++i)
1927 regulator_unregister_supply_alias(dev, id[i]);
1929 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1932 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1933 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1934 const struct regulator_config *config)
1936 struct regulator_enable_gpio *pin;
1937 struct gpio_desc *gpiod;
1940 if (config->ena_gpiod)
1941 gpiod = config->ena_gpiod;
1943 gpiod = gpio_to_desc(config->ena_gpio);
1945 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1946 if (pin->gpiod == gpiod) {
1947 rdev_dbg(rdev, "GPIO %d is already used\n",
1949 goto update_ena_gpio_to_rdev;
1953 if (!config->ena_gpiod) {
1954 ret = gpio_request_one(config->ena_gpio,
1955 GPIOF_DIR_OUT | config->ena_gpio_flags,
1956 rdev_get_name(rdev));
1961 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1963 if (!config->ena_gpiod)
1964 gpio_free(config->ena_gpio);
1969 pin->ena_gpio_invert = config->ena_gpio_invert;
1970 list_add(&pin->list, ®ulator_ena_gpio_list);
1972 update_ena_gpio_to_rdev:
1973 pin->request_count++;
1974 rdev->ena_pin = pin;
1978 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1980 struct regulator_enable_gpio *pin, *n;
1985 /* Free the GPIO only in case of no use */
1986 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1987 if (pin->gpiod == rdev->ena_pin->gpiod) {
1988 if (pin->request_count <= 1) {
1989 pin->request_count = 0;
1990 gpiod_put(pin->gpiod);
1991 list_del(&pin->list);
1993 rdev->ena_pin = NULL;
1996 pin->request_count--;
2003 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2004 * @rdev: regulator_dev structure
2005 * @enable: enable GPIO at initial use?
2007 * GPIO is enabled in case of initial use. (enable_count is 0)
2008 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2010 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2012 struct regulator_enable_gpio *pin = rdev->ena_pin;
2018 /* Enable GPIO at initial use */
2019 if (pin->enable_count == 0)
2020 gpiod_set_value_cansleep(pin->gpiod,
2021 !pin->ena_gpio_invert);
2023 pin->enable_count++;
2025 if (pin->enable_count > 1) {
2026 pin->enable_count--;
2030 /* Disable GPIO if not used */
2031 if (pin->enable_count <= 1) {
2032 gpiod_set_value_cansleep(pin->gpiod,
2033 pin->ena_gpio_invert);
2034 pin->enable_count = 0;
2042 * _regulator_enable_delay - a delay helper function
2043 * @delay: time to delay in microseconds
2045 * Delay for the requested amount of time as per the guidelines in:
2047 * Documentation/timers/timers-howto.txt
2049 * The assumption here is that regulators will never be enabled in
2050 * atomic context and therefore sleeping functions can be used.
2052 static void _regulator_enable_delay(unsigned int delay)
2054 unsigned int ms = delay / 1000;
2055 unsigned int us = delay % 1000;
2059 * For small enough values, handle super-millisecond
2060 * delays in the usleep_range() call below.
2069 * Give the scheduler some room to coalesce with any other
2070 * wakeup sources. For delays shorter than 10 us, don't even
2071 * bother setting up high-resolution timers and just busy-
2075 usleep_range(us, us + 100);
2080 static int _regulator_do_enable(struct regulator_dev *rdev)
2084 /* Query before enabling in case configuration dependent. */
2085 ret = _regulator_get_enable_time(rdev);
2089 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2093 trace_regulator_enable(rdev_get_name(rdev));
2095 if (rdev->desc->off_on_delay) {
2096 /* if needed, keep a distance of off_on_delay from last time
2097 * this regulator was disabled.
2099 unsigned long start_jiffy = jiffies;
2100 unsigned long intended, max_delay, remaining;
2102 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2103 intended = rdev->last_off_jiffy + max_delay;
2105 if (time_before(start_jiffy, intended)) {
2106 /* calc remaining jiffies to deal with one-time
2108 * in case of multiple timer wrapping, either it can be
2109 * detected by out-of-range remaining, or it cannot be
2110 * detected and we gets a panelty of
2111 * _regulator_enable_delay().
2113 remaining = intended - start_jiffy;
2114 if (remaining <= max_delay)
2115 _regulator_enable_delay(
2116 jiffies_to_usecs(remaining));
2120 if (rdev->ena_pin) {
2121 if (!rdev->ena_gpio_state) {
2122 ret = regulator_ena_gpio_ctrl(rdev, true);
2125 rdev->ena_gpio_state = 1;
2127 } else if (rdev->desc->ops->enable) {
2128 ret = rdev->desc->ops->enable(rdev);
2135 /* Allow the regulator to ramp; it would be useful to extend
2136 * this for bulk operations so that the regulators can ramp
2138 trace_regulator_enable_delay(rdev_get_name(rdev));
2140 _regulator_enable_delay(delay);
2142 trace_regulator_enable_complete(rdev_get_name(rdev));
2147 /* locks held by regulator_enable() */
2148 static int _regulator_enable(struct regulator_dev *rdev)
2152 lockdep_assert_held_once(&rdev->mutex);
2154 /* check voltage and requested load before enabling */
2155 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2156 drms_uA_update(rdev);
2158 if (rdev->use_count == 0) {
2159 /* The regulator may on if it's not switchable or left on */
2160 ret = _regulator_is_enabled(rdev);
2161 if (ret == -EINVAL || ret == 0) {
2162 if (!regulator_ops_is_valid(rdev,
2163 REGULATOR_CHANGE_STATUS))
2166 ret = _regulator_do_enable(rdev);
2170 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2172 } else if (ret < 0) {
2173 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2176 /* Fallthrough on positive return values - already enabled */
2185 * regulator_enable - enable regulator output
2186 * @regulator: regulator source
2188 * Request that the regulator be enabled with the regulator output at
2189 * the predefined voltage or current value. Calls to regulator_enable()
2190 * must be balanced with calls to regulator_disable().
2192 * NOTE: the output value can be set by other drivers, boot loader or may be
2193 * hardwired in the regulator.
2195 int regulator_enable(struct regulator *regulator)
2197 struct regulator_dev *rdev = regulator->rdev;
2200 if (regulator->always_on)
2204 ret = regulator_enable(rdev->supply);
2209 mutex_lock(&rdev->mutex);
2210 ret = _regulator_enable(rdev);
2211 mutex_unlock(&rdev->mutex);
2213 if (ret != 0 && rdev->supply)
2214 regulator_disable(rdev->supply);
2218 EXPORT_SYMBOL_GPL(regulator_enable);
2220 static int _regulator_do_disable(struct regulator_dev *rdev)
2224 trace_regulator_disable(rdev_get_name(rdev));
2226 if (rdev->ena_pin) {
2227 if (rdev->ena_gpio_state) {
2228 ret = regulator_ena_gpio_ctrl(rdev, false);
2231 rdev->ena_gpio_state = 0;
2234 } else if (rdev->desc->ops->disable) {
2235 ret = rdev->desc->ops->disable(rdev);
2240 /* cares about last_off_jiffy only if off_on_delay is required by
2243 if (rdev->desc->off_on_delay)
2244 rdev->last_off_jiffy = jiffies;
2246 trace_regulator_disable_complete(rdev_get_name(rdev));
2251 /* locks held by regulator_disable() */
2252 static int _regulator_disable(struct regulator_dev *rdev)
2256 lockdep_assert_held_once(&rdev->mutex);
2258 if (WARN(rdev->use_count <= 0,
2259 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2262 /* are we the last user and permitted to disable ? */
2263 if (rdev->use_count == 1 &&
2264 (rdev->constraints && !rdev->constraints->always_on)) {
2266 /* we are last user */
2267 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2268 ret = _notifier_call_chain(rdev,
2269 REGULATOR_EVENT_PRE_DISABLE,
2271 if (ret & NOTIFY_STOP_MASK)
2274 ret = _regulator_do_disable(rdev);
2276 rdev_err(rdev, "failed to disable\n");
2277 _notifier_call_chain(rdev,
2278 REGULATOR_EVENT_ABORT_DISABLE,
2282 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2286 rdev->use_count = 0;
2287 } else if (rdev->use_count > 1) {
2288 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS))
2289 drms_uA_update(rdev);
2298 * regulator_disable - disable regulator output
2299 * @regulator: regulator source
2301 * Disable the regulator output voltage or current. Calls to
2302 * regulator_enable() must be balanced with calls to
2303 * regulator_disable().
2305 * NOTE: this will only disable the regulator output if no other consumer
2306 * devices have it enabled, the regulator device supports disabling and
2307 * machine constraints permit this operation.
2309 int regulator_disable(struct regulator *regulator)
2311 struct regulator_dev *rdev = regulator->rdev;
2314 if (regulator->always_on)
2317 mutex_lock(&rdev->mutex);
2318 ret = _regulator_disable(rdev);
2319 mutex_unlock(&rdev->mutex);
2321 if (ret == 0 && rdev->supply)
2322 regulator_disable(rdev->supply);
2326 EXPORT_SYMBOL_GPL(regulator_disable);
2328 /* locks held by regulator_force_disable() */
2329 static int _regulator_force_disable(struct regulator_dev *rdev)
2333 lockdep_assert_held_once(&rdev->mutex);
2335 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2336 REGULATOR_EVENT_PRE_DISABLE, NULL);
2337 if (ret & NOTIFY_STOP_MASK)
2340 ret = _regulator_do_disable(rdev);
2342 rdev_err(rdev, "failed to force disable\n");
2343 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2344 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2348 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2349 REGULATOR_EVENT_DISABLE, NULL);
2355 * regulator_force_disable - force disable regulator output
2356 * @regulator: regulator source
2358 * Forcibly disable the regulator output voltage or current.
2359 * NOTE: this *will* disable the regulator output even if other consumer
2360 * devices have it enabled. This should be used for situations when device
2361 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2363 int regulator_force_disable(struct regulator *regulator)
2365 struct regulator_dev *rdev = regulator->rdev;
2368 mutex_lock(&rdev->mutex);
2369 regulator->uA_load = 0;
2370 ret = _regulator_force_disable(regulator->rdev);
2371 mutex_unlock(&rdev->mutex);
2374 while (rdev->open_count--)
2375 regulator_disable(rdev->supply);
2379 EXPORT_SYMBOL_GPL(regulator_force_disable);
2381 static void regulator_disable_work(struct work_struct *work)
2383 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2387 mutex_lock(&rdev->mutex);
2389 BUG_ON(!rdev->deferred_disables);
2391 count = rdev->deferred_disables;
2392 rdev->deferred_disables = 0;
2395 * Workqueue functions queue the new work instance while the previous
2396 * work instance is being processed. Cancel the queued work instance
2397 * as the work instance under processing does the job of the queued
2400 cancel_delayed_work(&rdev->disable_work);
2402 for (i = 0; i < count; i++) {
2403 ret = _regulator_disable(rdev);
2405 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2408 mutex_unlock(&rdev->mutex);
2411 for (i = 0; i < count; i++) {
2412 ret = regulator_disable(rdev->supply);
2415 "Supply disable failed: %d\n", ret);
2422 * regulator_disable_deferred - disable regulator output with delay
2423 * @regulator: regulator source
2424 * @ms: miliseconds until the regulator is disabled
2426 * Execute regulator_disable() on the regulator after a delay. This
2427 * is intended for use with devices that require some time to quiesce.
2429 * NOTE: this will only disable the regulator output if no other consumer
2430 * devices have it enabled, the regulator device supports disabling and
2431 * machine constraints permit this operation.
2433 int regulator_disable_deferred(struct regulator *regulator, int ms)
2435 struct regulator_dev *rdev = regulator->rdev;
2437 if (regulator->always_on)
2441 return regulator_disable(regulator);
2443 mutex_lock(&rdev->mutex);
2444 rdev->deferred_disables++;
2445 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2446 msecs_to_jiffies(ms));
2447 mutex_unlock(&rdev->mutex);
2451 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2453 static int _regulator_is_enabled(struct regulator_dev *rdev)
2455 /* A GPIO control always takes precedence */
2457 return rdev->ena_gpio_state;
2459 /* If we don't know then assume that the regulator is always on */
2460 if (!rdev->desc->ops->is_enabled)
2463 return rdev->desc->ops->is_enabled(rdev);
2466 static int _regulator_list_voltage(struct regulator_dev *rdev,
2467 unsigned selector, int lock)
2469 const struct regulator_ops *ops = rdev->desc->ops;
2472 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2473 return rdev->desc->fixed_uV;
2475 if (ops->list_voltage) {
2476 if (selector >= rdev->desc->n_voltages)
2479 mutex_lock(&rdev->mutex);
2480 ret = ops->list_voltage(rdev, selector);
2482 mutex_unlock(&rdev->mutex);
2483 } else if (rdev->is_switch && rdev->supply) {
2484 ret = _regulator_list_voltage(rdev->supply->rdev,
2491 if (ret < rdev->constraints->min_uV)
2493 else if (ret > rdev->constraints->max_uV)
2501 * regulator_is_enabled - is the regulator output enabled
2502 * @regulator: regulator source
2504 * Returns positive if the regulator driver backing the source/client
2505 * has requested that the device be enabled, zero if it hasn't, else a
2506 * negative errno code.
2508 * Note that the device backing this regulator handle can have multiple
2509 * users, so it might be enabled even if regulator_enable() was never
2510 * called for this particular source.
2512 int regulator_is_enabled(struct regulator *regulator)
2516 if (regulator->always_on)
2519 mutex_lock(®ulator->rdev->mutex);
2520 ret = _regulator_is_enabled(regulator->rdev);
2521 mutex_unlock(®ulator->rdev->mutex);
2525 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2528 * regulator_count_voltages - count regulator_list_voltage() selectors
2529 * @regulator: regulator source
2531 * Returns number of selectors, or negative errno. Selectors are
2532 * numbered starting at zero, and typically correspond to bitfields
2533 * in hardware registers.
2535 int regulator_count_voltages(struct regulator *regulator)
2537 struct regulator_dev *rdev = regulator->rdev;
2539 if (rdev->desc->n_voltages)
2540 return rdev->desc->n_voltages;
2542 if (!rdev->is_switch || !rdev->supply)
2545 return regulator_count_voltages(rdev->supply);
2547 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2550 * regulator_list_voltage - enumerate supported voltages
2551 * @regulator: regulator source
2552 * @selector: identify voltage to list
2553 * Context: can sleep
2555 * Returns a voltage that can be passed to @regulator_set_voltage(),
2556 * zero if this selector code can't be used on this system, or a
2559 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2561 return _regulator_list_voltage(regulator->rdev, selector, 1);
2563 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2566 * regulator_get_regmap - get the regulator's register map
2567 * @regulator: regulator source
2569 * Returns the register map for the given regulator, or an ERR_PTR value
2570 * if the regulator doesn't use regmap.
2572 struct regmap *regulator_get_regmap(struct regulator *regulator)
2574 struct regmap *map = regulator->rdev->regmap;
2576 return map ? map : ERR_PTR(-EOPNOTSUPP);
2580 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2581 * @regulator: regulator source
2582 * @vsel_reg: voltage selector register, output parameter
2583 * @vsel_mask: mask for voltage selector bitfield, output parameter
2585 * Returns the hardware register offset and bitmask used for setting the
2586 * regulator voltage. This might be useful when configuring voltage-scaling
2587 * hardware or firmware that can make I2C requests behind the kernel's back,
2590 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2591 * and 0 is returned, otherwise a negative errno is returned.
2593 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2595 unsigned *vsel_mask)
2597 struct regulator_dev *rdev = regulator->rdev;
2598 const struct regulator_ops *ops = rdev->desc->ops;
2600 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2603 *vsel_reg = rdev->desc->vsel_reg;
2604 *vsel_mask = rdev->desc->vsel_mask;
2608 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2611 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2612 * @regulator: regulator source
2613 * @selector: identify voltage to list
2615 * Converts the selector to a hardware-specific voltage selector that can be
2616 * directly written to the regulator registers. The address of the voltage
2617 * register can be determined by calling @regulator_get_hardware_vsel_register.
2619 * On error a negative errno is returned.
2621 int regulator_list_hardware_vsel(struct regulator *regulator,
2624 struct regulator_dev *rdev = regulator->rdev;
2625 const struct regulator_ops *ops = rdev->desc->ops;
2627 if (selector >= rdev->desc->n_voltages)
2629 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2634 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2637 * regulator_get_linear_step - return the voltage step size between VSEL values
2638 * @regulator: regulator source
2640 * Returns the voltage step size between VSEL values for linear
2641 * regulators, or return 0 if the regulator isn't a linear regulator.
2643 unsigned int regulator_get_linear_step(struct regulator *regulator)
2645 struct regulator_dev *rdev = regulator->rdev;
2647 return rdev->desc->uV_step;
2649 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2652 * regulator_is_supported_voltage - check if a voltage range can be supported
2654 * @regulator: Regulator to check.
2655 * @min_uV: Minimum required voltage in uV.
2656 * @max_uV: Maximum required voltage in uV.
2658 * Returns a boolean or a negative error code.
2660 int regulator_is_supported_voltage(struct regulator *regulator,
2661 int min_uV, int max_uV)
2663 struct regulator_dev *rdev = regulator->rdev;
2664 int i, voltages, ret;
2666 /* If we can't change voltage check the current voltage */
2667 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2668 ret = regulator_get_voltage(regulator);
2670 return min_uV <= ret && ret <= max_uV;
2675 /* Any voltage within constrains range is fine? */
2676 if (rdev->desc->continuous_voltage_range)
2677 return min_uV >= rdev->constraints->min_uV &&
2678 max_uV <= rdev->constraints->max_uV;
2680 ret = regulator_count_voltages(regulator);
2685 for (i = 0; i < voltages; i++) {
2686 ret = regulator_list_voltage(regulator, i);
2688 if (ret >= min_uV && ret <= max_uV)
2694 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2696 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
2699 const struct regulator_desc *desc = rdev->desc;
2701 if (desc->ops->map_voltage)
2702 return desc->ops->map_voltage(rdev, min_uV, max_uV);
2704 if (desc->ops->list_voltage == regulator_list_voltage_linear)
2705 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
2707 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
2708 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
2710 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
2713 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
2714 int min_uV, int max_uV,
2717 struct pre_voltage_change_data data;
2720 data.old_uV = _regulator_get_voltage(rdev);
2721 data.min_uV = min_uV;
2722 data.max_uV = max_uV;
2723 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2725 if (ret & NOTIFY_STOP_MASK)
2728 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
2732 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2733 (void *)data.old_uV);
2738 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
2739 int uV, unsigned selector)
2741 struct pre_voltage_change_data data;
2744 data.old_uV = _regulator_get_voltage(rdev);
2747 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
2749 if (ret & NOTIFY_STOP_MASK)
2752 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
2756 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
2757 (void *)data.old_uV);
2762 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
2763 int old_uV, int new_uV)
2765 unsigned int ramp_delay = 0;
2767 if (rdev->constraints->ramp_delay)
2768 ramp_delay = rdev->constraints->ramp_delay;
2769 else if (rdev->desc->ramp_delay)
2770 ramp_delay = rdev->desc->ramp_delay;
2771 else if (rdev->constraints->settling_time)
2772 return rdev->constraints->settling_time;
2773 else if (rdev->constraints->settling_time_up &&
2775 return rdev->constraints->settling_time_up;
2776 else if (rdev->constraints->settling_time_down &&
2778 return rdev->constraints->settling_time_down;
2780 if (ramp_delay == 0) {
2781 rdev_dbg(rdev, "ramp_delay not set\n");
2785 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
2788 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2789 int min_uV, int max_uV)
2794 unsigned int selector;
2795 int old_selector = -1;
2796 const struct regulator_ops *ops = rdev->desc->ops;
2797 int old_uV = _regulator_get_voltage(rdev);
2799 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2801 min_uV += rdev->constraints->uV_offset;
2802 max_uV += rdev->constraints->uV_offset;
2805 * If we can't obtain the old selector there is not enough
2806 * info to call set_voltage_time_sel().
2808 if (_regulator_is_enabled(rdev) &&
2809 ops->set_voltage_time_sel && ops->get_voltage_sel) {
2810 old_selector = ops->get_voltage_sel(rdev);
2811 if (old_selector < 0)
2812 return old_selector;
2815 if (ops->set_voltage) {
2816 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
2820 if (ops->list_voltage)
2821 best_val = ops->list_voltage(rdev,
2824 best_val = _regulator_get_voltage(rdev);
2827 } else if (ops->set_voltage_sel) {
2828 ret = regulator_map_voltage(rdev, min_uV, max_uV);
2830 best_val = ops->list_voltage(rdev, ret);
2831 if (min_uV <= best_val && max_uV >= best_val) {
2833 if (old_selector == selector)
2836 ret = _regulator_call_set_voltage_sel(
2837 rdev, best_val, selector);
2849 if (ops->set_voltage_time_sel) {
2851 * Call set_voltage_time_sel if successfully obtained
2854 if (old_selector >= 0 && old_selector != selector)
2855 delay = ops->set_voltage_time_sel(rdev, old_selector,
2858 if (old_uV != best_val) {
2859 if (ops->set_voltage_time)
2860 delay = ops->set_voltage_time(rdev, old_uV,
2863 delay = _regulator_set_voltage_time(rdev,
2870 rdev_warn(rdev, "failed to get delay: %d\n", delay);
2874 /* Insert any necessary delays */
2875 if (delay >= 1000) {
2876 mdelay(delay / 1000);
2877 udelay(delay % 1000);
2882 if (best_val >= 0) {
2883 unsigned long data = best_val;
2885 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2890 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2895 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
2896 int min_uV, int max_uV, suspend_state_t state)
2898 struct regulator_state *rstate;
2901 rstate = regulator_get_suspend_state(rdev, state);
2905 if (min_uV < rstate->min_uV)
2906 min_uV = rstate->min_uV;
2907 if (max_uV > rstate->max_uV)
2908 max_uV = rstate->max_uV;
2910 sel = regulator_map_voltage(rdev, min_uV, max_uV);
2914 uV = rdev->desc->ops->list_voltage(rdev, sel);
2915 if (uV >= min_uV && uV <= max_uV)
2921 static int regulator_set_voltage_unlocked(struct regulator *regulator,
2922 int min_uV, int max_uV,
2923 suspend_state_t state)
2925 struct regulator_dev *rdev = regulator->rdev;
2926 struct regulator_voltage *voltage = ®ulator->voltage[state];
2928 int old_min_uV, old_max_uV;
2930 int best_supply_uV = 0;
2931 int supply_change_uV = 0;
2933 /* If we're setting the same range as last time the change
2934 * should be a noop (some cpufreq implementations use the same
2935 * voltage for multiple frequencies, for example).
2937 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
2940 /* If we're trying to set a range that overlaps the current voltage,
2941 * return successfully even though the regulator does not support
2942 * changing the voltage.
2944 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
2945 current_uV = _regulator_get_voltage(rdev);
2946 if (min_uV <= current_uV && current_uV <= max_uV) {
2947 voltage->min_uV = min_uV;
2948 voltage->max_uV = max_uV;
2954 if (!rdev->desc->ops->set_voltage &&
2955 !rdev->desc->ops->set_voltage_sel) {
2960 /* constraints check */
2961 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2965 /* restore original values in case of error */
2966 old_min_uV = voltage->min_uV;
2967 old_max_uV = voltage->max_uV;
2968 voltage->min_uV = min_uV;
2969 voltage->max_uV = max_uV;
2971 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, state);
2976 regulator_ops_is_valid(rdev->supply->rdev,
2977 REGULATOR_CHANGE_VOLTAGE) &&
2978 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
2979 rdev->desc->ops->get_voltage_sel))) {
2980 int current_supply_uV;
2983 selector = regulator_map_voltage(rdev, min_uV, max_uV);
2989 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
2990 if (best_supply_uV < 0) {
2991 ret = best_supply_uV;
2995 best_supply_uV += rdev->desc->min_dropout_uV;
2997 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
2998 if (current_supply_uV < 0) {
2999 ret = current_supply_uV;
3003 supply_change_uV = best_supply_uV - current_supply_uV;
3006 if (supply_change_uV > 0) {
3007 ret = regulator_set_voltage_unlocked(rdev->supply,
3008 best_supply_uV, INT_MAX, state);
3010 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3016 if (state == PM_SUSPEND_ON)
3017 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3019 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3024 if (supply_change_uV < 0) {
3025 ret = regulator_set_voltage_unlocked(rdev->supply,
3026 best_supply_uV, INT_MAX, state);
3028 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3030 /* No need to fail here */
3037 voltage->min_uV = old_min_uV;
3038 voltage->max_uV = old_max_uV;
3044 * regulator_set_voltage - set regulator output voltage
3045 * @regulator: regulator source
3046 * @min_uV: Minimum required voltage in uV
3047 * @max_uV: Maximum acceptable voltage in uV
3049 * Sets a voltage regulator to the desired output voltage. This can be set
3050 * during any regulator state. IOW, regulator can be disabled or enabled.
3052 * If the regulator is enabled then the voltage will change to the new value
3053 * immediately otherwise if the regulator is disabled the regulator will
3054 * output at the new voltage when enabled.
3056 * NOTE: If the regulator is shared between several devices then the lowest
3057 * request voltage that meets the system constraints will be used.
3058 * Regulator system constraints must be set for this regulator before
3059 * calling this function otherwise this call will fail.
3061 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3065 regulator_lock_supply(regulator->rdev);
3067 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3070 regulator_unlock_supply(regulator->rdev);
3074 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3076 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3077 suspend_state_t state, bool en)
3079 struct regulator_state *rstate;
3081 rstate = regulator_get_suspend_state(rdev, state);
3085 if (!rstate->changeable)
3088 rstate->enabled = en;
3093 int regulator_suspend_enable(struct regulator_dev *rdev,
3094 suspend_state_t state)
3096 return regulator_suspend_toggle(rdev, state, true);
3098 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3100 int regulator_suspend_disable(struct regulator_dev *rdev,
3101 suspend_state_t state)
3103 struct regulator *regulator;
3104 struct regulator_voltage *voltage;
3107 * if any consumer wants this regulator device keeping on in
3108 * suspend states, don't set it as disabled.
3110 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3111 voltage = ®ulator->voltage[state];
3112 if (voltage->min_uV || voltage->max_uV)
3116 return regulator_suspend_toggle(rdev, state, false);
3118 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3120 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3121 int min_uV, int max_uV,
3122 suspend_state_t state)
3124 struct regulator_dev *rdev = regulator->rdev;
3125 struct regulator_state *rstate;
3127 rstate = regulator_get_suspend_state(rdev, state);
3131 if (rstate->min_uV == rstate->max_uV) {
3132 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3136 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3139 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3140 int max_uV, suspend_state_t state)
3144 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3145 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3148 regulator_lock_supply(regulator->rdev);
3150 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3153 regulator_unlock_supply(regulator->rdev);
3157 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3160 * regulator_set_voltage_time - get raise/fall time
3161 * @regulator: regulator source
3162 * @old_uV: starting voltage in microvolts
3163 * @new_uV: target voltage in microvolts
3165 * Provided with the starting and ending voltage, this function attempts to
3166 * calculate the time in microseconds required to rise or fall to this new
3169 int regulator_set_voltage_time(struct regulator *regulator,
3170 int old_uV, int new_uV)
3172 struct regulator_dev *rdev = regulator->rdev;
3173 const struct regulator_ops *ops = rdev->desc->ops;
3179 if (ops->set_voltage_time)
3180 return ops->set_voltage_time(rdev, old_uV, new_uV);
3181 else if (!ops->set_voltage_time_sel)
3182 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3184 /* Currently requires operations to do this */
3185 if (!ops->list_voltage || !rdev->desc->n_voltages)
3188 for (i = 0; i < rdev->desc->n_voltages; i++) {
3189 /* We only look for exact voltage matches here */
3190 voltage = regulator_list_voltage(regulator, i);
3195 if (voltage == old_uV)
3197 if (voltage == new_uV)
3201 if (old_sel < 0 || new_sel < 0)
3204 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3206 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3209 * regulator_set_voltage_time_sel - get raise/fall time
3210 * @rdev: regulator source device
3211 * @old_selector: selector for starting voltage
3212 * @new_selector: selector for target voltage
3214 * Provided with the starting and target voltage selectors, this function
3215 * returns time in microseconds required to rise or fall to this new voltage
3217 * Drivers providing ramp_delay in regulation_constraints can use this as their
3218 * set_voltage_time_sel() operation.
3220 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3221 unsigned int old_selector,
3222 unsigned int new_selector)
3224 int old_volt, new_volt;
3227 if (!rdev->desc->ops->list_voltage)
3230 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3231 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3233 if (rdev->desc->ops->set_voltage_time)
3234 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3237 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3239 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3242 * regulator_sync_voltage - re-apply last regulator output voltage
3243 * @regulator: regulator source
3245 * Re-apply the last configured voltage. This is intended to be used
3246 * where some external control source the consumer is cooperating with
3247 * has caused the configured voltage to change.
3249 int regulator_sync_voltage(struct regulator *regulator)
3251 struct regulator_dev *rdev = regulator->rdev;
3252 struct regulator_voltage *voltage = ®ulator->voltage[PM_SUSPEND_ON];
3253 int ret, min_uV, max_uV;
3255 mutex_lock(&rdev->mutex);
3257 if (!rdev->desc->ops->set_voltage &&
3258 !rdev->desc->ops->set_voltage_sel) {
3263 /* This is only going to work if we've had a voltage configured. */
3264 if (!voltage->min_uV && !voltage->max_uV) {
3269 min_uV = voltage->min_uV;
3270 max_uV = voltage->max_uV;
3272 /* This should be a paranoia check... */
3273 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3277 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3281 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3284 mutex_unlock(&rdev->mutex);
3287 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3289 static int _regulator_get_voltage(struct regulator_dev *rdev)
3294 if (rdev->desc->ops->get_bypass) {
3295 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3299 /* if bypassed the regulator must have a supply */
3300 if (!rdev->supply) {
3302 "bypassed regulator has no supply!\n");
3303 return -EPROBE_DEFER;
3306 return _regulator_get_voltage(rdev->supply->rdev);
3310 if (rdev->desc->ops->get_voltage_sel) {
3311 sel = rdev->desc->ops->get_voltage_sel(rdev);
3314 ret = rdev->desc->ops->list_voltage(rdev, sel);
3315 } else if (rdev->desc->ops->get_voltage) {
3316 ret = rdev->desc->ops->get_voltage(rdev);
3317 } else if (rdev->desc->ops->list_voltage) {
3318 ret = rdev->desc->ops->list_voltage(rdev, 0);
3319 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3320 ret = rdev->desc->fixed_uV;
3321 } else if (rdev->supply) {
3322 ret = _regulator_get_voltage(rdev->supply->rdev);
3329 return ret - rdev->constraints->uV_offset;
3333 * regulator_get_voltage - get regulator output voltage
3334 * @regulator: regulator source
3336 * This returns the current regulator voltage in uV.
3338 * NOTE: If the regulator is disabled it will return the voltage value. This
3339 * function should not be used to determine regulator state.
3341 int regulator_get_voltage(struct regulator *regulator)
3345 regulator_lock_supply(regulator->rdev);
3347 ret = _regulator_get_voltage(regulator->rdev);
3349 regulator_unlock_supply(regulator->rdev);
3353 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3356 * regulator_set_current_limit - set regulator output current limit
3357 * @regulator: regulator source
3358 * @min_uA: Minimum supported current in uA
3359 * @max_uA: Maximum supported current in uA
3361 * Sets current sink to the desired output current. This can be set during
3362 * any regulator state. IOW, regulator can be disabled or enabled.
3364 * If the regulator is enabled then the current will change to the new value
3365 * immediately otherwise if the regulator is disabled the regulator will
3366 * output at the new current when enabled.
3368 * NOTE: Regulator system constraints must be set for this regulator before
3369 * calling this function otherwise this call will fail.
3371 int regulator_set_current_limit(struct regulator *regulator,
3372 int min_uA, int max_uA)
3374 struct regulator_dev *rdev = regulator->rdev;
3377 mutex_lock(&rdev->mutex);
3380 if (!rdev->desc->ops->set_current_limit) {
3385 /* constraints check */
3386 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
3390 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
3392 mutex_unlock(&rdev->mutex);
3395 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
3397 static int _regulator_get_current_limit(struct regulator_dev *rdev)
3401 mutex_lock(&rdev->mutex);
3404 if (!rdev->desc->ops->get_current_limit) {
3409 ret = rdev->desc->ops->get_current_limit(rdev);
3411 mutex_unlock(&rdev->mutex);
3416 * regulator_get_current_limit - get regulator output current
3417 * @regulator: regulator source
3419 * This returns the current supplied by the specified current sink in uA.
3421 * NOTE: If the regulator is disabled it will return the current value. This
3422 * function should not be used to determine regulator state.
3424 int regulator_get_current_limit(struct regulator *regulator)
3426 return _regulator_get_current_limit(regulator->rdev);
3428 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
3431 * regulator_set_mode - set regulator operating mode
3432 * @regulator: regulator source
3433 * @mode: operating mode - one of the REGULATOR_MODE constants
3435 * Set regulator operating mode to increase regulator efficiency or improve
3436 * regulation performance.
3438 * NOTE: Regulator system constraints must be set for this regulator before
3439 * calling this function otherwise this call will fail.
3441 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
3443 struct regulator_dev *rdev = regulator->rdev;
3445 int regulator_curr_mode;
3447 mutex_lock(&rdev->mutex);
3450 if (!rdev->desc->ops->set_mode) {
3455 /* return if the same mode is requested */
3456 if (rdev->desc->ops->get_mode) {
3457 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
3458 if (regulator_curr_mode == mode) {
3464 /* constraints check */
3465 ret = regulator_mode_constrain(rdev, &mode);
3469 ret = rdev->desc->ops->set_mode(rdev, mode);
3471 mutex_unlock(&rdev->mutex);
3474 EXPORT_SYMBOL_GPL(regulator_set_mode);
3476 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
3480 mutex_lock(&rdev->mutex);
3483 if (!rdev->desc->ops->get_mode) {
3488 ret = rdev->desc->ops->get_mode(rdev);
3490 mutex_unlock(&rdev->mutex);
3495 * regulator_get_mode - get regulator operating mode
3496 * @regulator: regulator source
3498 * Get the current regulator operating mode.
3500 unsigned int regulator_get_mode(struct regulator *regulator)
3502 return _regulator_get_mode(regulator->rdev);
3504 EXPORT_SYMBOL_GPL(regulator_get_mode);
3506 static int _regulator_get_error_flags(struct regulator_dev *rdev,
3507 unsigned int *flags)
3511 mutex_lock(&rdev->mutex);
3514 if (!rdev->desc->ops->get_error_flags) {
3519 ret = rdev->desc->ops->get_error_flags(rdev, flags);
3521 mutex_unlock(&rdev->mutex);
3526 * regulator_get_error_flags - get regulator error information
3527 * @regulator: regulator source
3528 * @flags: pointer to store error flags
3530 * Get the current regulator error information.
3532 int regulator_get_error_flags(struct regulator *regulator,
3533 unsigned int *flags)
3535 return _regulator_get_error_flags(regulator->rdev, flags);
3537 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
3540 * regulator_set_load - set regulator load
3541 * @regulator: regulator source
3542 * @uA_load: load current
3544 * Notifies the regulator core of a new device load. This is then used by
3545 * DRMS (if enabled by constraints) to set the most efficient regulator
3546 * operating mode for the new regulator loading.
3548 * Consumer devices notify their supply regulator of the maximum power
3549 * they will require (can be taken from device datasheet in the power
3550 * consumption tables) when they change operational status and hence power
3551 * state. Examples of operational state changes that can affect power
3552 * consumption are :-
3554 * o Device is opened / closed.
3555 * o Device I/O is about to begin or has just finished.
3556 * o Device is idling in between work.
3558 * This information is also exported via sysfs to userspace.
3560 * DRMS will sum the total requested load on the regulator and change
3561 * to the most efficient operating mode if platform constraints allow.
3563 * On error a negative errno is returned.
3565 int regulator_set_load(struct regulator *regulator, int uA_load)
3567 struct regulator_dev *rdev = regulator->rdev;
3570 mutex_lock(&rdev->mutex);
3571 regulator->uA_load = uA_load;
3572 ret = drms_uA_update(rdev);
3573 mutex_unlock(&rdev->mutex);
3577 EXPORT_SYMBOL_GPL(regulator_set_load);
3580 * regulator_allow_bypass - allow the regulator to go into bypass mode
3582 * @regulator: Regulator to configure
3583 * @enable: enable or disable bypass mode
3585 * Allow the regulator to go into bypass mode if all other consumers
3586 * for the regulator also enable bypass mode and the machine
3587 * constraints allow this. Bypass mode means that the regulator is
3588 * simply passing the input directly to the output with no regulation.
3590 int regulator_allow_bypass(struct regulator *regulator, bool enable)
3592 struct regulator_dev *rdev = regulator->rdev;
3595 if (!rdev->desc->ops->set_bypass)
3598 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
3601 mutex_lock(&rdev->mutex);
3603 if (enable && !regulator->bypass) {
3604 rdev->bypass_count++;
3606 if (rdev->bypass_count == rdev->open_count) {
3607 ret = rdev->desc->ops->set_bypass(rdev, enable);
3609 rdev->bypass_count--;
3612 } else if (!enable && regulator->bypass) {
3613 rdev->bypass_count--;
3615 if (rdev->bypass_count != rdev->open_count) {
3616 ret = rdev->desc->ops->set_bypass(rdev, enable);
3618 rdev->bypass_count++;
3623 regulator->bypass = enable;
3625 mutex_unlock(&rdev->mutex);
3629 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3632 * regulator_register_notifier - register regulator event notifier
3633 * @regulator: regulator source
3634 * @nb: notifier block
3636 * Register notifier block to receive regulator events.
3638 int regulator_register_notifier(struct regulator *regulator,
3639 struct notifier_block *nb)
3641 return blocking_notifier_chain_register(®ulator->rdev->notifier,
3644 EXPORT_SYMBOL_GPL(regulator_register_notifier);
3647 * regulator_unregister_notifier - unregister regulator event notifier
3648 * @regulator: regulator source
3649 * @nb: notifier block
3651 * Unregister regulator event notifier block.
3653 int regulator_unregister_notifier(struct regulator *regulator,
3654 struct notifier_block *nb)
3656 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
3659 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3661 /* notify regulator consumers and downstream regulator consumers.
3662 * Note mutex must be held by caller.
3664 static int _notifier_call_chain(struct regulator_dev *rdev,
3665 unsigned long event, void *data)
3667 /* call rdev chain first */
3668 return blocking_notifier_call_chain(&rdev->notifier, event, data);
3672 * regulator_bulk_get - get multiple regulator consumers
3674 * @dev: Device to supply
3675 * @num_consumers: Number of consumers to register
3676 * @consumers: Configuration of consumers; clients are stored here.
3678 * @return 0 on success, an errno on failure.
3680 * This helper function allows drivers to get several regulator
3681 * consumers in one operation. If any of the regulators cannot be
3682 * acquired then any regulators that were allocated will be freed
3683 * before returning to the caller.
3685 int regulator_bulk_get(struct device *dev, int num_consumers,
3686 struct regulator_bulk_data *consumers)
3691 for (i = 0; i < num_consumers; i++)
3692 consumers[i].consumer = NULL;
3694 for (i = 0; i < num_consumers; i++) {
3695 consumers[i].consumer = regulator_get(dev,
3696 consumers[i].supply);
3697 if (IS_ERR(consumers[i].consumer)) {
3698 ret = PTR_ERR(consumers[i].consumer);
3699 dev_err(dev, "Failed to get supply '%s': %d\n",
3700 consumers[i].supply, ret);
3701 consumers[i].consumer = NULL;
3710 regulator_put(consumers[i].consumer);
3714 EXPORT_SYMBOL_GPL(regulator_bulk_get);
3716 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3718 struct regulator_bulk_data *bulk = data;
3720 bulk->ret = regulator_enable(bulk->consumer);
3724 * regulator_bulk_enable - enable multiple regulator consumers
3726 * @num_consumers: Number of consumers
3727 * @consumers: Consumer data; clients are stored here.
3728 * @return 0 on success, an errno on failure
3730 * This convenience API allows consumers to enable multiple regulator
3731 * clients in a single API call. If any consumers cannot be enabled
3732 * then any others that were enabled will be disabled again prior to
3735 int regulator_bulk_enable(int num_consumers,
3736 struct regulator_bulk_data *consumers)
3738 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3742 for (i = 0; i < num_consumers; i++) {
3743 if (consumers[i].consumer->always_on)
3744 consumers[i].ret = 0;
3746 async_schedule_domain(regulator_bulk_enable_async,
3747 &consumers[i], &async_domain);
3750 async_synchronize_full_domain(&async_domain);
3752 /* If any consumer failed we need to unwind any that succeeded */
3753 for (i = 0; i < num_consumers; i++) {
3754 if (consumers[i].ret != 0) {
3755 ret = consumers[i].ret;
3763 for (i = 0; i < num_consumers; i++) {
3764 if (consumers[i].ret < 0)
3765 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3768 regulator_disable(consumers[i].consumer);
3773 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3776 * regulator_bulk_disable - disable multiple regulator consumers
3778 * @num_consumers: Number of consumers
3779 * @consumers: Consumer data; clients are stored here.
3780 * @return 0 on success, an errno on failure
3782 * This convenience API allows consumers to disable multiple regulator
3783 * clients in a single API call. If any consumers cannot be disabled
3784 * then any others that were disabled will be enabled again prior to
3787 int regulator_bulk_disable(int num_consumers,
3788 struct regulator_bulk_data *consumers)
3793 for (i = num_consumers - 1; i >= 0; --i) {
3794 ret = regulator_disable(consumers[i].consumer);
3802 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3803 for (++i; i < num_consumers; ++i) {
3804 r = regulator_enable(consumers[i].consumer);
3806 pr_err("Failed to re-enable %s: %d\n",
3807 consumers[i].supply, r);
3812 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3815 * regulator_bulk_force_disable - force disable multiple regulator consumers
3817 * @num_consumers: Number of consumers
3818 * @consumers: Consumer data; clients are stored here.
3819 * @return 0 on success, an errno on failure
3821 * This convenience API allows consumers to forcibly disable multiple regulator
3822 * clients in a single API call.
3823 * NOTE: This should be used for situations when device damage will
3824 * likely occur if the regulators are not disabled (e.g. over temp).
3825 * Although regulator_force_disable function call for some consumers can
3826 * return error numbers, the function is called for all consumers.
3828 int regulator_bulk_force_disable(int num_consumers,
3829 struct regulator_bulk_data *consumers)
3834 for (i = 0; i < num_consumers; i++) {
3836 regulator_force_disable(consumers[i].consumer);
3838 /* Store first error for reporting */
3839 if (consumers[i].ret && !ret)
3840 ret = consumers[i].ret;
3845 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3848 * regulator_bulk_free - free multiple regulator consumers
3850 * @num_consumers: Number of consumers
3851 * @consumers: Consumer data; clients are stored here.
3853 * This convenience API allows consumers to free multiple regulator
3854 * clients in a single API call.
3856 void regulator_bulk_free(int num_consumers,
3857 struct regulator_bulk_data *consumers)
3861 for (i = 0; i < num_consumers; i++) {
3862 regulator_put(consumers[i].consumer);
3863 consumers[i].consumer = NULL;
3866 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3869 * regulator_notifier_call_chain - call regulator event notifier
3870 * @rdev: regulator source
3871 * @event: notifier block
3872 * @data: callback-specific data.
3874 * Called by regulator drivers to notify clients a regulator event has
3875 * occurred. We also notify regulator clients downstream.
3876 * Note lock must be held by caller.
3878 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3879 unsigned long event, void *data)
3881 lockdep_assert_held_once(&rdev->mutex);
3883 _notifier_call_chain(rdev, event, data);
3887 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3890 * regulator_mode_to_status - convert a regulator mode into a status
3892 * @mode: Mode to convert
3894 * Convert a regulator mode into a status.
3896 int regulator_mode_to_status(unsigned int mode)
3899 case REGULATOR_MODE_FAST:
3900 return REGULATOR_STATUS_FAST;
3901 case REGULATOR_MODE_NORMAL:
3902 return REGULATOR_STATUS_NORMAL;
3903 case REGULATOR_MODE_IDLE:
3904 return REGULATOR_STATUS_IDLE;
3905 case REGULATOR_MODE_STANDBY:
3906 return REGULATOR_STATUS_STANDBY;
3908 return REGULATOR_STATUS_UNDEFINED;
3911 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3913 static struct attribute *regulator_dev_attrs[] = {
3914 &dev_attr_name.attr,
3915 &dev_attr_num_users.attr,
3916 &dev_attr_type.attr,
3917 &dev_attr_microvolts.attr,
3918 &dev_attr_microamps.attr,
3919 &dev_attr_opmode.attr,
3920 &dev_attr_state.attr,
3921 &dev_attr_status.attr,
3922 &dev_attr_bypass.attr,
3923 &dev_attr_requested_microamps.attr,
3924 &dev_attr_min_microvolts.attr,
3925 &dev_attr_max_microvolts.attr,
3926 &dev_attr_min_microamps.attr,
3927 &dev_attr_max_microamps.attr,
3928 &dev_attr_suspend_standby_state.attr,
3929 &dev_attr_suspend_mem_state.attr,
3930 &dev_attr_suspend_disk_state.attr,
3931 &dev_attr_suspend_standby_microvolts.attr,
3932 &dev_attr_suspend_mem_microvolts.attr,
3933 &dev_attr_suspend_disk_microvolts.attr,
3934 &dev_attr_suspend_standby_mode.attr,
3935 &dev_attr_suspend_mem_mode.attr,
3936 &dev_attr_suspend_disk_mode.attr,
3941 * To avoid cluttering sysfs (and memory) with useless state, only
3942 * create attributes that can be meaningfully displayed.
3944 static umode_t regulator_attr_is_visible(struct kobject *kobj,
3945 struct attribute *attr, int idx)
3947 struct device *dev = kobj_to_dev(kobj);
3948 struct regulator_dev *rdev = dev_to_rdev(dev);
3949 const struct regulator_ops *ops = rdev->desc->ops;
3950 umode_t mode = attr->mode;
3952 /* these three are always present */
3953 if (attr == &dev_attr_name.attr ||
3954 attr == &dev_attr_num_users.attr ||
3955 attr == &dev_attr_type.attr)
3958 /* some attributes need specific methods to be displayed */
3959 if (attr == &dev_attr_microvolts.attr) {
3960 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3961 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3962 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
3963 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
3968 if (attr == &dev_attr_microamps.attr)
3969 return ops->get_current_limit ? mode : 0;
3971 if (attr == &dev_attr_opmode.attr)
3972 return ops->get_mode ? mode : 0;
3974 if (attr == &dev_attr_state.attr)
3975 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
3977 if (attr == &dev_attr_status.attr)
3978 return ops->get_status ? mode : 0;
3980 if (attr == &dev_attr_bypass.attr)
3981 return ops->get_bypass ? mode : 0;
3983 /* some attributes are type-specific */
3984 if (attr == &dev_attr_requested_microamps.attr)
3985 return rdev->desc->type == REGULATOR_CURRENT ? mode : 0;
3987 /* constraints need specific supporting methods */
3988 if (attr == &dev_attr_min_microvolts.attr ||
3989 attr == &dev_attr_max_microvolts.attr)
3990 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
3992 if (attr == &dev_attr_min_microamps.attr ||
3993 attr == &dev_attr_max_microamps.attr)
3994 return ops->set_current_limit ? mode : 0;
3996 if (attr == &dev_attr_suspend_standby_state.attr ||
3997 attr == &dev_attr_suspend_mem_state.attr ||
3998 attr == &dev_attr_suspend_disk_state.attr)
4001 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4002 attr == &dev_attr_suspend_mem_microvolts.attr ||
4003 attr == &dev_attr_suspend_disk_microvolts.attr)
4004 return ops->set_suspend_voltage ? mode : 0;
4006 if (attr == &dev_attr_suspend_standby_mode.attr ||
4007 attr == &dev_attr_suspend_mem_mode.attr ||
4008 attr == &dev_attr_suspend_disk_mode.attr)
4009 return ops->set_suspend_mode ? mode : 0;
4014 static const struct attribute_group regulator_dev_group = {
4015 .attrs = regulator_dev_attrs,
4016 .is_visible = regulator_attr_is_visible,
4019 static const struct attribute_group *regulator_dev_groups[] = {
4020 ®ulator_dev_group,
4024 static void regulator_dev_release(struct device *dev)
4026 struct regulator_dev *rdev = dev_get_drvdata(dev);
4028 kfree(rdev->constraints);
4029 of_node_put(rdev->dev.of_node);
4033 static void rdev_init_debugfs(struct regulator_dev *rdev)
4035 struct device *parent = rdev->dev.parent;
4036 const char *rname = rdev_get_name(rdev);
4037 char name[NAME_MAX];
4039 /* Avoid duplicate debugfs directory names */
4040 if (parent && rname == rdev->desc->name) {
4041 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4046 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4047 if (!rdev->debugfs) {
4048 rdev_warn(rdev, "Failed to create debugfs directory\n");
4052 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4054 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4056 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4057 &rdev->bypass_count);
4060 static int regulator_register_resolve_supply(struct device *dev, void *data)
4062 struct regulator_dev *rdev = dev_to_rdev(dev);
4064 if (regulator_resolve_supply(rdev))
4065 rdev_dbg(rdev, "unable to resolve supply\n");
4071 * regulator_register - register regulator
4072 * @regulator_desc: regulator to register
4073 * @cfg: runtime configuration for regulator
4075 * Called by regulator drivers to register a regulator.
4076 * Returns a valid pointer to struct regulator_dev on success
4077 * or an ERR_PTR() on error.
4079 struct regulator_dev *
4080 regulator_register(const struct regulator_desc *regulator_desc,
4081 const struct regulator_config *cfg)
4083 const struct regulation_constraints *constraints = NULL;
4084 const struct regulator_init_data *init_data;
4085 struct regulator_config *config = NULL;
4086 static atomic_t regulator_no = ATOMIC_INIT(-1);
4087 struct regulator_dev *rdev;
4091 if (regulator_desc == NULL || cfg == NULL)
4092 return ERR_PTR(-EINVAL);
4097 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
4098 return ERR_PTR(-EINVAL);
4100 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4101 regulator_desc->type != REGULATOR_CURRENT)
4102 return ERR_PTR(-EINVAL);
4104 /* Only one of each should be implemented */
4105 WARN_ON(regulator_desc->ops->get_voltage &&
4106 regulator_desc->ops->get_voltage_sel);
4107 WARN_ON(regulator_desc->ops->set_voltage &&
4108 regulator_desc->ops->set_voltage_sel);
4110 /* If we're using selectors we must implement list_voltage. */
4111 if (regulator_desc->ops->get_voltage_sel &&
4112 !regulator_desc->ops->list_voltage) {
4113 return ERR_PTR(-EINVAL);
4115 if (regulator_desc->ops->set_voltage_sel &&
4116 !regulator_desc->ops->list_voltage) {
4117 return ERR_PTR(-EINVAL);
4120 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4122 return ERR_PTR(-ENOMEM);
4125 * Duplicate the config so the driver could override it after
4126 * parsing init data.
4128 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4129 if (config == NULL) {
4131 return ERR_PTR(-ENOMEM);
4134 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4135 &rdev->dev.of_node);
4137 init_data = config->init_data;
4138 rdev->dev.of_node = of_node_get(config->of_node);
4141 mutex_init(&rdev->mutex);
4142 rdev->reg_data = config->driver_data;
4143 rdev->owner = regulator_desc->owner;
4144 rdev->desc = regulator_desc;
4146 rdev->regmap = config->regmap;
4147 else if (dev_get_regmap(dev, NULL))
4148 rdev->regmap = dev_get_regmap(dev, NULL);
4149 else if (dev->parent)
4150 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4151 INIT_LIST_HEAD(&rdev->consumer_list);
4152 INIT_LIST_HEAD(&rdev->list);
4153 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4154 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4156 /* preform any regulator specific init */
4157 if (init_data && init_data->regulator_init) {
4158 ret = init_data->regulator_init(rdev->reg_data);
4163 if (config->ena_gpiod ||
4164 ((config->ena_gpio || config->ena_gpio_initialized) &&
4165 gpio_is_valid(config->ena_gpio))) {
4166 mutex_lock(®ulator_list_mutex);
4167 ret = regulator_ena_gpio_request(rdev, config);
4168 mutex_unlock(®ulator_list_mutex);
4170 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
4171 config->ena_gpio, ret);
4176 /* register with sysfs */
4177 rdev->dev.class = ®ulator_class;
4178 rdev->dev.parent = dev;
4179 dev_set_name(&rdev->dev, "regulator.%lu",
4180 (unsigned long) atomic_inc_return(®ulator_no));
4182 /* set regulator constraints */
4184 constraints = &init_data->constraints;
4186 if (init_data && init_data->supply_regulator)
4187 rdev->supply_name = init_data->supply_regulator;
4188 else if (regulator_desc->supply_name)
4189 rdev->supply_name = regulator_desc->supply_name;
4192 * Attempt to resolve the regulator supply, if specified,
4193 * but don't return an error if we fail because we will try
4194 * to resolve it again later as more regulators are added.
4196 if (regulator_resolve_supply(rdev))
4197 rdev_dbg(rdev, "unable to resolve supply\n");
4199 ret = set_machine_constraints(rdev, constraints);
4203 /* add consumers devices */
4205 mutex_lock(®ulator_list_mutex);
4206 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4207 ret = set_consumer_device_supply(rdev,
4208 init_data->consumer_supplies[i].dev_name,
4209 init_data->consumer_supplies[i].supply);
4211 mutex_unlock(®ulator_list_mutex);
4212 dev_err(dev, "Failed to set supply %s\n",
4213 init_data->consumer_supplies[i].supply);
4214 goto unset_supplies;
4217 mutex_unlock(®ulator_list_mutex);
4220 if (!rdev->desc->ops->get_voltage &&
4221 !rdev->desc->ops->list_voltage &&
4222 !rdev->desc->fixed_uV)
4223 rdev->is_switch = true;
4225 ret = device_register(&rdev->dev);
4227 put_device(&rdev->dev);
4228 goto unset_supplies;
4231 dev_set_drvdata(&rdev->dev, rdev);
4232 rdev_init_debugfs(rdev);
4234 /* try to resolve regulators supply since a new one was registered */
4235 class_for_each_device(®ulator_class, NULL, NULL,
4236 regulator_register_resolve_supply);
4241 mutex_lock(®ulator_list_mutex);
4242 unset_regulator_supplies(rdev);
4243 mutex_unlock(®ulator_list_mutex);
4245 kfree(rdev->constraints);
4246 mutex_lock(®ulator_list_mutex);
4247 regulator_ena_gpio_free(rdev);
4248 mutex_unlock(®ulator_list_mutex);
4252 return ERR_PTR(ret);
4254 EXPORT_SYMBOL_GPL(regulator_register);
4257 * regulator_unregister - unregister regulator
4258 * @rdev: regulator to unregister
4260 * Called by regulator drivers to unregister a regulator.
4262 void regulator_unregister(struct regulator_dev *rdev)
4268 while (rdev->use_count--)
4269 regulator_disable(rdev->supply);
4270 regulator_put(rdev->supply);
4272 mutex_lock(®ulator_list_mutex);
4273 debugfs_remove_recursive(rdev->debugfs);
4274 flush_work(&rdev->disable_work.work);
4275 WARN_ON(rdev->open_count);
4276 unset_regulator_supplies(rdev);
4277 list_del(&rdev->list);
4278 regulator_ena_gpio_free(rdev);
4279 mutex_unlock(®ulator_list_mutex);
4280 device_unregister(&rdev->dev);
4282 EXPORT_SYMBOL_GPL(regulator_unregister);
4284 #ifdef CONFIG_SUSPEND
4285 static int _regulator_suspend_late(struct device *dev, void *data)
4287 struct regulator_dev *rdev = dev_to_rdev(dev);
4288 suspend_state_t *state = data;
4291 mutex_lock(&rdev->mutex);
4292 ret = suspend_set_state(rdev, *state);
4293 mutex_unlock(&rdev->mutex);
4299 * regulator_suspend_late - prepare regulators for system wide suspend
4300 * @state: system suspend state
4302 * Configure each regulator with it's suspend operating parameters for state.
4304 static int regulator_suspend_late(struct device *dev)
4306 suspend_state_t state = pm_suspend_target_state;
4308 return class_for_each_device(®ulator_class, NULL, &state,
4309 _regulator_suspend_late);
4312 static int _regulator_resume_early(struct device *dev, void *data)
4315 struct regulator_dev *rdev = dev_to_rdev(dev);
4316 suspend_state_t *state = data;
4317 struct regulator_state *rstate;
4319 rstate = regulator_get_suspend_state(rdev, *state);
4323 mutex_lock(&rdev->mutex);
4325 if (rdev->desc->ops->resume_early &&
4326 (rstate->enabled == ENABLE_IN_SUSPEND ||
4327 rstate->enabled == DISABLE_IN_SUSPEND))
4328 ret = rdev->desc->ops->resume_early(rdev);
4330 mutex_unlock(&rdev->mutex);
4335 static int regulator_resume_early(struct device *dev)
4337 suspend_state_t state = pm_suspend_target_state;
4339 return class_for_each_device(®ulator_class, NULL, &state,
4340 _regulator_resume_early);
4343 #else /* !CONFIG_SUSPEND */
4345 #define regulator_suspend_late NULL
4346 #define regulator_resume_early NULL
4348 #endif /* !CONFIG_SUSPEND */
4351 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
4352 .suspend_late = regulator_suspend_late,
4353 .resume_early = regulator_resume_early,
4357 struct class regulator_class = {
4358 .name = "regulator",
4359 .dev_release = regulator_dev_release,
4360 .dev_groups = regulator_dev_groups,
4362 .pm = ®ulator_pm_ops,
4366 * regulator_has_full_constraints - the system has fully specified constraints
4368 * Calling this function will cause the regulator API to disable all
4369 * regulators which have a zero use count and don't have an always_on
4370 * constraint in a late_initcall.
4372 * The intention is that this will become the default behaviour in a
4373 * future kernel release so users are encouraged to use this facility
4376 void regulator_has_full_constraints(void)
4378 has_full_constraints = 1;
4380 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
4383 * rdev_get_drvdata - get rdev regulator driver data
4386 * Get rdev regulator driver private data. This call can be used in the
4387 * regulator driver context.
4389 void *rdev_get_drvdata(struct regulator_dev *rdev)
4391 return rdev->reg_data;
4393 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
4396 * regulator_get_drvdata - get regulator driver data
4397 * @regulator: regulator
4399 * Get regulator driver private data. This call can be used in the consumer
4400 * driver context when non API regulator specific functions need to be called.
4402 void *regulator_get_drvdata(struct regulator *regulator)
4404 return regulator->rdev->reg_data;
4406 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
4409 * regulator_set_drvdata - set regulator driver data
4410 * @regulator: regulator
4413 void regulator_set_drvdata(struct regulator *regulator, void *data)
4415 regulator->rdev->reg_data = data;
4417 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
4420 * regulator_get_id - get regulator ID
4423 int rdev_get_id(struct regulator_dev *rdev)
4425 return rdev->desc->id;
4427 EXPORT_SYMBOL_GPL(rdev_get_id);
4429 struct device *rdev_get_dev(struct regulator_dev *rdev)
4433 EXPORT_SYMBOL_GPL(rdev_get_dev);
4435 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
4437 return reg_init_data->driver_data;
4439 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
4441 #ifdef CONFIG_DEBUG_FS
4442 static int supply_map_show(struct seq_file *sf, void *data)
4444 struct regulator_map *map;
4446 list_for_each_entry(map, ®ulator_map_list, list) {
4447 seq_printf(sf, "%s -> %s.%s\n",
4448 rdev_get_name(map->regulator), map->dev_name,
4455 static int supply_map_open(struct inode *inode, struct file *file)
4457 return single_open(file, supply_map_show, inode->i_private);
4461 static const struct file_operations supply_map_fops = {
4462 #ifdef CONFIG_DEBUG_FS
4463 .open = supply_map_open,
4465 .llseek = seq_lseek,
4466 .release = single_release,
4470 #ifdef CONFIG_DEBUG_FS
4471 struct summary_data {
4473 struct regulator_dev *parent;
4477 static void regulator_summary_show_subtree(struct seq_file *s,
4478 struct regulator_dev *rdev,
4481 static int regulator_summary_show_children(struct device *dev, void *data)
4483 struct regulator_dev *rdev = dev_to_rdev(dev);
4484 struct summary_data *summary_data = data;
4486 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
4487 regulator_summary_show_subtree(summary_data->s, rdev,
4488 summary_data->level + 1);
4493 static void regulator_summary_show_subtree(struct seq_file *s,
4494 struct regulator_dev *rdev,
4497 struct regulation_constraints *c;
4498 struct regulator *consumer;
4499 struct summary_data summary_data;
4504 seq_printf(s, "%*s%-*s %3d %4d %6d ",
4506 30 - level * 3, rdev_get_name(rdev),
4507 rdev->use_count, rdev->open_count, rdev->bypass_count);
4509 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
4510 seq_printf(s, "%5dmA ", _regulator_get_current_limit(rdev) / 1000);
4512 c = rdev->constraints;
4514 switch (rdev->desc->type) {
4515 case REGULATOR_VOLTAGE:
4516 seq_printf(s, "%5dmV %5dmV ",
4517 c->min_uV / 1000, c->max_uV / 1000);
4519 case REGULATOR_CURRENT:
4520 seq_printf(s, "%5dmA %5dmA ",
4521 c->min_uA / 1000, c->max_uA / 1000);
4528 list_for_each_entry(consumer, &rdev->consumer_list, list) {
4529 if (consumer->dev && consumer->dev->class == ®ulator_class)
4532 seq_printf(s, "%*s%-*s ",
4533 (level + 1) * 3 + 1, "",
4534 30 - (level + 1) * 3,
4535 consumer->dev ? dev_name(consumer->dev) : "deviceless");
4537 switch (rdev->desc->type) {
4538 case REGULATOR_VOLTAGE:
4539 seq_printf(s, "%37dmV %5dmV",
4540 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
4541 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
4543 case REGULATOR_CURRENT:
4551 summary_data.level = level;
4552 summary_data.parent = rdev;
4554 class_for_each_device(®ulator_class, NULL, &summary_data,
4555 regulator_summary_show_children);
4558 static int regulator_summary_show_roots(struct device *dev, void *data)
4560 struct regulator_dev *rdev = dev_to_rdev(dev);
4561 struct seq_file *s = data;
4564 regulator_summary_show_subtree(s, rdev, 0);
4569 static int regulator_summary_show(struct seq_file *s, void *data)
4571 seq_puts(s, " regulator use open bypass voltage current min max\n");
4572 seq_puts(s, "-------------------------------------------------------------------------------\n");
4574 class_for_each_device(®ulator_class, NULL, s,
4575 regulator_summary_show_roots);
4580 static int regulator_summary_open(struct inode *inode, struct file *file)
4582 return single_open(file, regulator_summary_show, inode->i_private);
4586 static const struct file_operations regulator_summary_fops = {
4587 #ifdef CONFIG_DEBUG_FS
4588 .open = regulator_summary_open,
4590 .llseek = seq_lseek,
4591 .release = single_release,
4595 static int __init regulator_init(void)
4599 ret = class_register(®ulator_class);
4601 debugfs_root = debugfs_create_dir("regulator", NULL);
4603 pr_warn("regulator: Failed to create debugfs directory\n");
4605 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4608 debugfs_create_file("regulator_summary", 0444, debugfs_root,
4609 NULL, ®ulator_summary_fops);
4611 regulator_dummy_init();
4616 /* init early to allow our consumers to complete system booting */
4617 core_initcall(regulator_init);
4619 static int __init regulator_late_cleanup(struct device *dev, void *data)
4621 struct regulator_dev *rdev = dev_to_rdev(dev);
4622 const struct regulator_ops *ops = rdev->desc->ops;
4623 struct regulation_constraints *c = rdev->constraints;
4626 if (c && c->always_on)
4629 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
4632 mutex_lock(&rdev->mutex);
4634 if (rdev->use_count)
4637 /* If we can't read the status assume it's on. */
4638 if (ops->is_enabled)
4639 enabled = ops->is_enabled(rdev);
4646 if (have_full_constraints()) {
4647 /* We log since this may kill the system if it goes
4649 rdev_info(rdev, "disabling\n");
4650 ret = _regulator_do_disable(rdev);
4652 rdev_err(rdev, "couldn't disable: %d\n", ret);
4654 /* The intention is that in future we will
4655 * assume that full constraints are provided
4656 * so warn even if we aren't going to do
4659 rdev_warn(rdev, "incomplete constraints, leaving on\n");
4663 mutex_unlock(&rdev->mutex);
4668 static int __init regulator_init_complete(void)
4671 * Since DT doesn't provide an idiomatic mechanism for
4672 * enabling full constraints and since it's much more natural
4673 * with DT to provide them just assume that a DT enabled
4674 * system has full constraints.
4676 if (of_have_populated_dt())
4677 has_full_constraints = true;
4680 * Regulators may had failed to resolve their input supplies
4681 * when were registered, either because the input supply was
4682 * not registered yet or because its parent device was not
4683 * bound yet. So attempt to resolve the input supplies for
4684 * pending regulators before trying to disable unused ones.
4686 class_for_each_device(®ulator_class, NULL, NULL,
4687 regulator_register_resolve_supply);
4689 /* If we have a full configuration then disable any regulators
4690 * we have permission to change the status for and which are
4691 * not in use or always_on. This is effectively the default
4692 * for DT and ACPI as they have full constraints.
4694 class_for_each_device(®ulator_class, NULL, NULL,
4695 regulator_late_cleanup);
4699 late_initcall_sync(regulator_init_complete);