2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
41 #define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52 static DEFINE_MUTEX(regulator_list_mutex);
53 static LIST_HEAD(regulator_list);
54 static LIST_HEAD(regulator_map_list);
55 static LIST_HEAD(regulator_ena_gpio_list);
56 static LIST_HEAD(regulator_supply_alias_list);
57 static bool has_full_constraints;
58 static bool board_wants_dummy_regulator;
60 static struct dentry *debugfs_root;
63 * struct regulator_map
65 * Used to provide symbolic supply names to devices.
67 struct regulator_map {
68 struct list_head list;
69 const char *dev_name; /* The dev_name() for the consumer */
71 struct regulator_dev *regulator;
75 * struct regulator_enable_gpio
77 * Management for shared enable GPIO pin
79 struct regulator_enable_gpio {
80 struct list_head list;
82 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
84 unsigned int ena_gpio_invert:1;
88 * struct regulator_supply_alias
90 * Used to map lookups for a supply onto an alternative device.
92 struct regulator_supply_alias {
93 struct list_head list;
94 struct device *src_dev;
95 const char *src_supply;
96 struct device *alias_dev;
97 const char *alias_supply;
100 static int _regulator_is_enabled(struct regulator_dev *rdev);
101 static int _regulator_disable(struct regulator_dev *rdev);
102 static int _regulator_get_voltage(struct regulator_dev *rdev);
103 static int _regulator_get_current_limit(struct regulator_dev *rdev);
104 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
105 static void _notifier_call_chain(struct regulator_dev *rdev,
106 unsigned long event, void *data);
107 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
108 int min_uV, int max_uV);
109 static struct regulator *create_regulator(struct regulator_dev *rdev,
111 const char *supply_name);
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;
124 * of_get_regulator - get a regulator device node based on supply name
125 * @dev: Device pointer for the consumer (of regulator) device
126 * @supply: regulator supply name
128 * Extract the regulator device node corresponding to the supply name.
129 * returns the device node corresponding to the regulator if found, else
132 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
134 struct device_node *regnode = NULL;
135 char prop_name[32]; /* 32 is max size of property name */
137 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
139 snprintf(prop_name, 32, "%s-supply", supply);
140 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
143 dev_dbg(dev, "Looking up %s property in node %s failed",
144 prop_name, dev->of_node->full_name);
150 static int _regulator_can_change_status(struct regulator_dev *rdev)
152 if (!rdev->constraints)
155 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
161 /* Platform voltage constraint check */
162 static int regulator_check_voltage(struct regulator_dev *rdev,
163 int *min_uV, int *max_uV)
165 BUG_ON(*min_uV > *max_uV);
167 if (!rdev->constraints) {
168 rdev_err(rdev, "no constraints\n");
171 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
172 rdev_err(rdev, "operation not allowed\n");
176 if (*max_uV > rdev->constraints->max_uV)
177 *max_uV = rdev->constraints->max_uV;
178 if (*min_uV < rdev->constraints->min_uV)
179 *min_uV = rdev->constraints->min_uV;
181 if (*min_uV > *max_uV) {
182 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
190 /* Make sure we select a voltage that suits the needs of all
191 * regulator consumers
193 static int regulator_check_consumers(struct regulator_dev *rdev,
194 int *min_uV, int *max_uV)
196 struct regulator *regulator;
198 list_for_each_entry(regulator, &rdev->consumer_list, list) {
200 * Assume consumers that didn't say anything are OK
201 * with anything in the constraint range.
203 if (!regulator->min_uV && !regulator->max_uV)
206 if (*max_uV > regulator->max_uV)
207 *max_uV = regulator->max_uV;
208 if (*min_uV < regulator->min_uV)
209 *min_uV = regulator->min_uV;
212 if (*min_uV > *max_uV) {
213 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
221 /* current constraint check */
222 static int regulator_check_current_limit(struct regulator_dev *rdev,
223 int *min_uA, int *max_uA)
225 BUG_ON(*min_uA > *max_uA);
227 if (!rdev->constraints) {
228 rdev_err(rdev, "no constraints\n");
231 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
232 rdev_err(rdev, "operation not allowed\n");
236 if (*max_uA > rdev->constraints->max_uA)
237 *max_uA = rdev->constraints->max_uA;
238 if (*min_uA < rdev->constraints->min_uA)
239 *min_uA = rdev->constraints->min_uA;
241 if (*min_uA > *max_uA) {
242 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
250 /* operating mode constraint check */
251 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
254 case REGULATOR_MODE_FAST:
255 case REGULATOR_MODE_NORMAL:
256 case REGULATOR_MODE_IDLE:
257 case REGULATOR_MODE_STANDBY:
260 rdev_err(rdev, "invalid mode %x specified\n", *mode);
264 if (!rdev->constraints) {
265 rdev_err(rdev, "no constraints\n");
268 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
269 rdev_err(rdev, "operation not allowed\n");
273 /* The modes are bitmasks, the most power hungry modes having
274 * the lowest values. If the requested mode isn't supported
275 * try higher modes. */
277 if (rdev->constraints->valid_modes_mask & *mode)
285 /* dynamic regulator mode switching constraint check */
286 static int regulator_check_drms(struct regulator_dev *rdev)
288 if (!rdev->constraints) {
289 rdev_err(rdev, "no constraints\n");
292 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
293 rdev_err(rdev, "operation not allowed\n");
299 static ssize_t regulator_uV_show(struct device *dev,
300 struct device_attribute *attr, char *buf)
302 struct regulator_dev *rdev = dev_get_drvdata(dev);
305 mutex_lock(&rdev->mutex);
306 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
307 mutex_unlock(&rdev->mutex);
311 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
313 static ssize_t regulator_uA_show(struct device *dev,
314 struct device_attribute *attr, char *buf)
316 struct regulator_dev *rdev = dev_get_drvdata(dev);
318 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
320 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
322 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
325 struct regulator_dev *rdev = dev_get_drvdata(dev);
327 return sprintf(buf, "%s\n", rdev_get_name(rdev));
329 static DEVICE_ATTR_RO(name);
331 static ssize_t regulator_print_opmode(char *buf, int mode)
334 case REGULATOR_MODE_FAST:
335 return sprintf(buf, "fast\n");
336 case REGULATOR_MODE_NORMAL:
337 return sprintf(buf, "normal\n");
338 case REGULATOR_MODE_IDLE:
339 return sprintf(buf, "idle\n");
340 case REGULATOR_MODE_STANDBY:
341 return sprintf(buf, "standby\n");
343 return sprintf(buf, "unknown\n");
346 static ssize_t regulator_opmode_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct regulator_dev *rdev = dev_get_drvdata(dev);
351 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
353 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
355 static ssize_t regulator_print_state(char *buf, int state)
358 return sprintf(buf, "enabled\n");
360 return sprintf(buf, "disabled\n");
362 return sprintf(buf, "unknown\n");
365 static ssize_t regulator_state_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
371 mutex_lock(&rdev->mutex);
372 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
373 mutex_unlock(&rdev->mutex);
377 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
379 static ssize_t regulator_status_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
382 struct regulator_dev *rdev = dev_get_drvdata(dev);
386 status = rdev->desc->ops->get_status(rdev);
391 case REGULATOR_STATUS_OFF:
394 case REGULATOR_STATUS_ON:
397 case REGULATOR_STATUS_ERROR:
400 case REGULATOR_STATUS_FAST:
403 case REGULATOR_STATUS_NORMAL:
406 case REGULATOR_STATUS_IDLE:
409 case REGULATOR_STATUS_STANDBY:
412 case REGULATOR_STATUS_BYPASS:
415 case REGULATOR_STATUS_UNDEFINED:
422 return sprintf(buf, "%s\n", label);
424 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
426 static ssize_t regulator_min_uA_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
429 struct regulator_dev *rdev = dev_get_drvdata(dev);
431 if (!rdev->constraints)
432 return sprintf(buf, "constraint not defined\n");
434 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
436 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
438 static ssize_t regulator_max_uA_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
441 struct regulator_dev *rdev = dev_get_drvdata(dev);
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
446 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
448 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
450 static ssize_t regulator_min_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
453 struct regulator_dev *rdev = dev_get_drvdata(dev);
455 if (!rdev->constraints)
456 return sprintf(buf, "constraint not defined\n");
458 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
460 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
462 static ssize_t regulator_max_uV_show(struct device *dev,
463 struct device_attribute *attr, char *buf)
465 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 if (!rdev->constraints)
468 return sprintf(buf, "constraint not defined\n");
470 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
472 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
474 static ssize_t regulator_total_uA_show(struct device *dev,
475 struct device_attribute *attr, char *buf)
477 struct regulator_dev *rdev = dev_get_drvdata(dev);
478 struct regulator *regulator;
481 mutex_lock(&rdev->mutex);
482 list_for_each_entry(regulator, &rdev->consumer_list, list)
483 uA += regulator->uA_load;
484 mutex_unlock(&rdev->mutex);
485 return sprintf(buf, "%d\n", uA);
487 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
489 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
492 struct regulator_dev *rdev = dev_get_drvdata(dev);
493 return sprintf(buf, "%d\n", rdev->use_count);
495 static DEVICE_ATTR_RO(num_users);
497 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
500 struct regulator_dev *rdev = dev_get_drvdata(dev);
502 switch (rdev->desc->type) {
503 case REGULATOR_VOLTAGE:
504 return sprintf(buf, "voltage\n");
505 case REGULATOR_CURRENT:
506 return sprintf(buf, "current\n");
508 return sprintf(buf, "unknown\n");
510 static DEVICE_ATTR_RO(type);
512 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
513 struct device_attribute *attr, char *buf)
515 struct regulator_dev *rdev = dev_get_drvdata(dev);
517 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
519 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
520 regulator_suspend_mem_uV_show, NULL);
522 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
525 struct regulator_dev *rdev = dev_get_drvdata(dev);
527 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
529 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
530 regulator_suspend_disk_uV_show, NULL);
532 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
533 struct device_attribute *attr, char *buf)
535 struct regulator_dev *rdev = dev_get_drvdata(dev);
537 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
539 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
540 regulator_suspend_standby_uV_show, NULL);
542 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
543 struct device_attribute *attr, char *buf)
545 struct regulator_dev *rdev = dev_get_drvdata(dev);
547 return regulator_print_opmode(buf,
548 rdev->constraints->state_mem.mode);
550 static DEVICE_ATTR(suspend_mem_mode, 0444,
551 regulator_suspend_mem_mode_show, NULL);
553 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
554 struct device_attribute *attr, char *buf)
556 struct regulator_dev *rdev = dev_get_drvdata(dev);
558 return regulator_print_opmode(buf,
559 rdev->constraints->state_disk.mode);
561 static DEVICE_ATTR(suspend_disk_mode, 0444,
562 regulator_suspend_disk_mode_show, NULL);
564 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
565 struct device_attribute *attr, char *buf)
567 struct regulator_dev *rdev = dev_get_drvdata(dev);
569 return regulator_print_opmode(buf,
570 rdev->constraints->state_standby.mode);
572 static DEVICE_ATTR(suspend_standby_mode, 0444,
573 regulator_suspend_standby_mode_show, NULL);
575 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
576 struct device_attribute *attr, char *buf)
578 struct regulator_dev *rdev = dev_get_drvdata(dev);
580 return regulator_print_state(buf,
581 rdev->constraints->state_mem.enabled);
583 static DEVICE_ATTR(suspend_mem_state, 0444,
584 regulator_suspend_mem_state_show, NULL);
586 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
587 struct device_attribute *attr, char *buf)
589 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 return regulator_print_state(buf,
592 rdev->constraints->state_disk.enabled);
594 static DEVICE_ATTR(suspend_disk_state, 0444,
595 regulator_suspend_disk_state_show, NULL);
597 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
600 struct regulator_dev *rdev = dev_get_drvdata(dev);
602 return regulator_print_state(buf,
603 rdev->constraints->state_standby.enabled);
605 static DEVICE_ATTR(suspend_standby_state, 0444,
606 regulator_suspend_standby_state_show, NULL);
608 static ssize_t regulator_bypass_show(struct device *dev,
609 struct device_attribute *attr, char *buf)
611 struct regulator_dev *rdev = dev_get_drvdata(dev);
616 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
625 return sprintf(buf, "%s\n", report);
627 static DEVICE_ATTR(bypass, 0444,
628 regulator_bypass_show, NULL);
631 * These are the only attributes are present for all regulators.
632 * Other attributes are a function of regulator functionality.
634 static struct attribute *regulator_dev_attrs[] = {
636 &dev_attr_num_users.attr,
640 ATTRIBUTE_GROUPS(regulator_dev);
642 static void regulator_dev_release(struct device *dev)
644 struct regulator_dev *rdev = dev_get_drvdata(dev);
648 static struct class regulator_class = {
650 .dev_release = regulator_dev_release,
651 .dev_groups = regulator_dev_groups,
654 /* Calculate the new optimum regulator operating mode based on the new total
655 * consumer load. All locks held by caller */
656 static void drms_uA_update(struct regulator_dev *rdev)
658 struct regulator *sibling;
659 int current_uA = 0, output_uV, input_uV, err;
662 err = regulator_check_drms(rdev);
663 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
664 (!rdev->desc->ops->get_voltage &&
665 !rdev->desc->ops->get_voltage_sel) ||
666 !rdev->desc->ops->set_mode)
669 /* get output voltage */
670 output_uV = _regulator_get_voltage(rdev);
674 /* get input voltage */
677 input_uV = regulator_get_voltage(rdev->supply);
679 input_uV = rdev->constraints->input_uV;
683 /* calc total requested load */
684 list_for_each_entry(sibling, &rdev->consumer_list, list)
685 current_uA += sibling->uA_load;
687 /* now get the optimum mode for our new total regulator load */
688 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 output_uV, current_uA);
691 /* check the new mode is allowed */
692 err = regulator_mode_constrain(rdev, &mode);
694 rdev->desc->ops->set_mode(rdev, mode);
697 static int suspend_set_state(struct regulator_dev *rdev,
698 struct regulator_state *rstate)
702 /* If we have no suspend mode configration don't set anything;
703 * only warn if the driver implements set_suspend_voltage or
704 * set_suspend_mode callback.
706 if (!rstate->enabled && !rstate->disabled) {
707 if (rdev->desc->ops->set_suspend_voltage ||
708 rdev->desc->ops->set_suspend_mode)
709 rdev_warn(rdev, "No configuration\n");
713 if (rstate->enabled && rstate->disabled) {
714 rdev_err(rdev, "invalid configuration\n");
718 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
719 ret = rdev->desc->ops->set_suspend_enable(rdev);
720 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
721 ret = rdev->desc->ops->set_suspend_disable(rdev);
722 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
726 rdev_err(rdev, "failed to enabled/disable\n");
730 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
733 rdev_err(rdev, "failed to set voltage\n");
738 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
741 rdev_err(rdev, "failed to set mode\n");
748 /* locks held by caller */
749 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
751 if (!rdev->constraints)
755 case PM_SUSPEND_STANDBY:
756 return suspend_set_state(rdev,
757 &rdev->constraints->state_standby);
759 return suspend_set_state(rdev,
760 &rdev->constraints->state_mem);
762 return suspend_set_state(rdev,
763 &rdev->constraints->state_disk);
769 static void print_constraints(struct regulator_dev *rdev)
771 struct regulation_constraints *constraints = rdev->constraints;
776 if (constraints->min_uV && constraints->max_uV) {
777 if (constraints->min_uV == constraints->max_uV)
778 count += sprintf(buf + count, "%d mV ",
779 constraints->min_uV / 1000);
781 count += sprintf(buf + count, "%d <--> %d mV ",
782 constraints->min_uV / 1000,
783 constraints->max_uV / 1000);
786 if (!constraints->min_uV ||
787 constraints->min_uV != constraints->max_uV) {
788 ret = _regulator_get_voltage(rdev);
790 count += sprintf(buf + count, "at %d mV ", ret / 1000);
793 if (constraints->uV_offset)
794 count += sprintf(buf, "%dmV offset ",
795 constraints->uV_offset / 1000);
797 if (constraints->min_uA && constraints->max_uA) {
798 if (constraints->min_uA == constraints->max_uA)
799 count += sprintf(buf + count, "%d mA ",
800 constraints->min_uA / 1000);
802 count += sprintf(buf + count, "%d <--> %d mA ",
803 constraints->min_uA / 1000,
804 constraints->max_uA / 1000);
807 if (!constraints->min_uA ||
808 constraints->min_uA != constraints->max_uA) {
809 ret = _regulator_get_current_limit(rdev);
811 count += sprintf(buf + count, "at %d mA ", ret / 1000);
814 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 count += sprintf(buf + count, "fast ");
816 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 count += sprintf(buf + count, "normal ");
818 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 count += sprintf(buf + count, "idle ");
820 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 count += sprintf(buf + count, "standby");
824 sprintf(buf, "no parameters");
826 rdev_info(rdev, "%s\n", buf);
828 if ((constraints->min_uV != constraints->max_uV) &&
829 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
831 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
834 static int machine_constraints_voltage(struct regulator_dev *rdev,
835 struct regulation_constraints *constraints)
837 struct regulator_ops *ops = rdev->desc->ops;
840 /* do we need to apply the constraint voltage */
841 if (rdev->constraints->apply_uV &&
842 rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 ret = _regulator_do_set_voltage(rdev,
844 rdev->constraints->min_uV,
845 rdev->constraints->max_uV);
847 rdev_err(rdev, "failed to apply %duV constraint\n",
848 rdev->constraints->min_uV);
853 /* constrain machine-level voltage specs to fit
854 * the actual range supported by this regulator.
856 if (ops->list_voltage && rdev->desc->n_voltages) {
857 int count = rdev->desc->n_voltages;
859 int min_uV = INT_MAX;
860 int max_uV = INT_MIN;
861 int cmin = constraints->min_uV;
862 int cmax = constraints->max_uV;
864 /* it's safe to autoconfigure fixed-voltage supplies
865 and the constraints are used by list_voltage. */
866 if (count == 1 && !cmin) {
869 constraints->min_uV = cmin;
870 constraints->max_uV = cmax;
873 /* voltage constraints are optional */
874 if ((cmin == 0) && (cmax == 0))
877 /* else require explicit machine-level constraints */
878 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
879 rdev_err(rdev, "invalid voltage constraints\n");
883 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 for (i = 0; i < count; i++) {
887 value = ops->list_voltage(rdev, i);
891 /* maybe adjust [min_uV..max_uV] */
892 if (value >= cmin && value < min_uV)
894 if (value <= cmax && value > max_uV)
898 /* final: [min_uV..max_uV] valid iff constraints valid */
899 if (max_uV < min_uV) {
901 "unsupportable voltage constraints %u-%uuV\n",
906 /* use regulator's subset of machine constraints */
907 if (constraints->min_uV < min_uV) {
908 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 constraints->min_uV, min_uV);
910 constraints->min_uV = min_uV;
912 if (constraints->max_uV > max_uV) {
913 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 constraints->max_uV, max_uV);
915 constraints->max_uV = max_uV;
923 * set_machine_constraints - sets regulator constraints
924 * @rdev: regulator source
925 * @constraints: constraints to apply
927 * Allows platform initialisation code to define and constrain
928 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
929 * Constraints *must* be set by platform code in order for some
930 * regulator operations to proceed i.e. set_voltage, set_current_limit,
933 static int set_machine_constraints(struct regulator_dev *rdev,
934 const struct regulation_constraints *constraints)
937 struct regulator_ops *ops = rdev->desc->ops;
940 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
943 rdev->constraints = kzalloc(sizeof(*constraints),
945 if (!rdev->constraints)
948 ret = machine_constraints_voltage(rdev, rdev->constraints);
952 /* do we need to setup our suspend state */
953 if (rdev->constraints->initial_state) {
954 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
956 rdev_err(rdev, "failed to set suspend state\n");
961 if (rdev->constraints->initial_mode) {
962 if (!ops->set_mode) {
963 rdev_err(rdev, "no set_mode operation\n");
968 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
970 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
975 /* If the constraints say the regulator should be on at this point
976 * and we have control then make sure it is enabled.
978 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
980 ret = ops->enable(rdev);
982 rdev_err(rdev, "failed to enable\n");
987 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
988 && ops->set_ramp_delay) {
989 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
991 rdev_err(rdev, "failed to set ramp_delay\n");
996 print_constraints(rdev);
999 kfree(rdev->constraints);
1000 rdev->constraints = NULL;
1005 * set_supply - set regulator supply regulator
1006 * @rdev: regulator name
1007 * @supply_rdev: supply regulator name
1009 * Called by platform initialisation code to set the supply regulator for this
1010 * regulator. This ensures that a regulators supply will also be enabled by the
1011 * core if it's child is enabled.
1013 static int set_supply(struct regulator_dev *rdev,
1014 struct regulator_dev *supply_rdev)
1018 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1020 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1021 if (rdev->supply == NULL) {
1025 supply_rdev->open_count++;
1031 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1032 * @rdev: regulator source
1033 * @consumer_dev_name: dev_name() string for device supply applies to
1034 * @supply: symbolic name for supply
1036 * Allows platform initialisation code to map physical regulator
1037 * sources to symbolic names for supplies for use by devices. Devices
1038 * should use these symbolic names to request regulators, avoiding the
1039 * need to provide board-specific regulator names as platform data.
1041 static int set_consumer_device_supply(struct regulator_dev *rdev,
1042 const char *consumer_dev_name,
1045 struct regulator_map *node;
1051 if (consumer_dev_name != NULL)
1056 list_for_each_entry(node, ®ulator_map_list, list) {
1057 if (node->dev_name && consumer_dev_name) {
1058 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1060 } else if (node->dev_name || consumer_dev_name) {
1064 if (strcmp(node->supply, supply) != 0)
1067 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1069 dev_name(&node->regulator->dev),
1070 node->regulator->desc->name,
1072 dev_name(&rdev->dev), rdev_get_name(rdev));
1076 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1080 node->regulator = rdev;
1081 node->supply = supply;
1084 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1085 if (node->dev_name == NULL) {
1091 list_add(&node->list, ®ulator_map_list);
1095 static void unset_regulator_supplies(struct regulator_dev *rdev)
1097 struct regulator_map *node, *n;
1099 list_for_each_entry_safe(node, n, ®ulator_map_list, list) {
1100 if (rdev == node->regulator) {
1101 list_del(&node->list);
1102 kfree(node->dev_name);
1108 #define REG_STR_SIZE 64
1110 static struct regulator *create_regulator(struct regulator_dev *rdev,
1112 const char *supply_name)
1114 struct regulator *regulator;
1115 char buf[REG_STR_SIZE];
1118 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1119 if (regulator == NULL)
1122 mutex_lock(&rdev->mutex);
1123 regulator->rdev = rdev;
1124 list_add(®ulator->list, &rdev->consumer_list);
1127 regulator->dev = dev;
1129 /* Add a link to the device sysfs entry */
1130 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1131 dev->kobj.name, supply_name);
1132 if (size >= REG_STR_SIZE)
1135 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1136 if (regulator->supply_name == NULL)
1139 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1142 rdev_warn(rdev, "could not add device link %s err %d\n",
1143 dev->kobj.name, err);
1147 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1148 if (regulator->supply_name == NULL)
1152 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1154 if (!regulator->debugfs) {
1155 rdev_warn(rdev, "Failed to create debugfs directory\n");
1157 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1158 ®ulator->uA_load);
1159 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1160 ®ulator->min_uV);
1161 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1162 ®ulator->max_uV);
1166 * Check now if the regulator is an always on regulator - if
1167 * it is then we don't need to do nearly so much work for
1168 * enable/disable calls.
1170 if (!_regulator_can_change_status(rdev) &&
1171 _regulator_is_enabled(rdev))
1172 regulator->always_on = true;
1174 mutex_unlock(&rdev->mutex);
1177 list_del(®ulator->list);
1179 mutex_unlock(&rdev->mutex);
1183 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1185 if (!rdev->desc->ops->enable_time)
1186 return rdev->desc->enable_time;
1187 return rdev->desc->ops->enable_time(rdev);
1190 static struct regulator_supply_alias *regulator_find_supply_alias(
1191 struct device *dev, const char *supply)
1193 struct regulator_supply_alias *map;
1195 list_for_each_entry(map, ®ulator_supply_alias_list, list)
1196 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1202 static void regulator_supply_alias(struct device **dev, const char **supply)
1204 struct regulator_supply_alias *map;
1206 map = regulator_find_supply_alias(*dev, *supply);
1208 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1209 *supply, map->alias_supply,
1210 dev_name(map->alias_dev));
1211 *dev = map->alias_dev;
1212 *supply = map->alias_supply;
1216 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1220 struct regulator_dev *r;
1221 struct device_node *node;
1222 struct regulator_map *map;
1223 const char *devname = NULL;
1225 regulator_supply_alias(&dev, &supply);
1227 /* first do a dt based lookup */
1228 if (dev && dev->of_node) {
1229 node = of_get_regulator(dev, supply);
1231 list_for_each_entry(r, ®ulator_list, list)
1232 if (r->dev.parent &&
1233 node == r->dev.of_node)
1237 * If we couldn't even get the node then it's
1238 * not just that the device didn't register
1239 * yet, there's no node and we'll never
1246 /* if not found, try doing it non-dt way */
1248 devname = dev_name(dev);
1250 list_for_each_entry(r, ®ulator_list, list)
1251 if (strcmp(rdev_get_name(r), supply) == 0)
1254 list_for_each_entry(map, ®ulator_map_list, list) {
1255 /* If the mapping has a device set up it must match */
1256 if (map->dev_name &&
1257 (!devname || strcmp(map->dev_name, devname)))
1260 if (strcmp(map->supply, supply) == 0)
1261 return map->regulator;
1268 /* Internal regulator request function */
1269 static struct regulator *_regulator_get(struct device *dev, const char *id,
1272 struct regulator_dev *rdev;
1273 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1274 const char *devname = NULL;
1278 pr_err("get() with no identifier\n");
1283 devname = dev_name(dev);
1285 mutex_lock(®ulator_list_mutex);
1287 rdev = regulator_dev_lookup(dev, id, &ret);
1292 * If we have return value from dev_lookup fail, we do not expect to
1293 * succeed, so, quit with appropriate error value
1296 regulator = ERR_PTR(ret);
1300 if (board_wants_dummy_regulator) {
1301 rdev = dummy_regulator_rdev;
1305 #ifdef CONFIG_REGULATOR_DUMMY
1307 devname = "deviceless";
1309 /* If the board didn't flag that it was fully constrained then
1310 * substitute in a dummy regulator so consumers can continue.
1312 if (!has_full_constraints) {
1313 pr_warn("%s supply %s not found, using dummy regulator\n",
1315 rdev = dummy_regulator_rdev;
1320 mutex_unlock(®ulator_list_mutex);
1324 if (rdev->exclusive) {
1325 regulator = ERR_PTR(-EPERM);
1329 if (exclusive && rdev->open_count) {
1330 regulator = ERR_PTR(-EBUSY);
1334 if (!try_module_get(rdev->owner))
1337 regulator = create_regulator(rdev, dev, id);
1338 if (regulator == NULL) {
1339 regulator = ERR_PTR(-ENOMEM);
1340 module_put(rdev->owner);
1346 rdev->exclusive = 1;
1348 ret = _regulator_is_enabled(rdev);
1350 rdev->use_count = 1;
1352 rdev->use_count = 0;
1356 mutex_unlock(®ulator_list_mutex);
1362 * regulator_get - lookup and obtain a reference to a regulator.
1363 * @dev: device for regulator "consumer"
1364 * @id: Supply name or regulator ID.
1366 * Returns a struct regulator corresponding to the regulator producer,
1367 * or IS_ERR() condition containing errno.
1369 * Use of supply names configured via regulator_set_device_supply() is
1370 * strongly encouraged. It is recommended that the supply name used
1371 * should match the name used for the supply and/or the relevant
1372 * device pins in the datasheet.
1374 struct regulator *regulator_get(struct device *dev, const char *id)
1376 return _regulator_get(dev, id, false);
1378 EXPORT_SYMBOL_GPL(regulator_get);
1381 * regulator_get_exclusive - obtain exclusive access to a regulator.
1382 * @dev: device for regulator "consumer"
1383 * @id: Supply name or regulator ID.
1385 * Returns a struct regulator corresponding to the regulator producer,
1386 * or IS_ERR() condition containing errno. Other consumers will be
1387 * unable to obtain this reference is held and the use count for the
1388 * regulator will be initialised to reflect the current state of the
1391 * This is intended for use by consumers which cannot tolerate shared
1392 * use of the regulator such as those which need to force the
1393 * regulator off for correct operation of the hardware they are
1396 * Use of supply names configured via regulator_set_device_supply() is
1397 * strongly encouraged. It is recommended that the supply name used
1398 * should match the name used for the supply and/or the relevant
1399 * device pins in the datasheet.
1401 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1403 return _regulator_get(dev, id, true);
1405 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1408 * regulator_get_optional - obtain optional access to a regulator.
1409 * @dev: device for regulator "consumer"
1410 * @id: Supply name or regulator ID.
1412 * Returns a struct regulator corresponding to the regulator producer,
1413 * or IS_ERR() condition containing errno. Other consumers will be
1414 * unable to obtain this reference is held and the use count for the
1415 * regulator will be initialised to reflect the current state of the
1418 * This is intended for use by consumers for devices which can have
1419 * some supplies unconnected in normal use, such as some MMC devices.
1420 * It can allow the regulator core to provide stub supplies for other
1421 * supplies requested using normal regulator_get() calls without
1422 * disrupting the operation of drivers that can handle absent
1425 * Use of supply names configured via regulator_set_device_supply() is
1426 * strongly encouraged. It is recommended that the supply name used
1427 * should match the name used for the supply and/or the relevant
1428 * device pins in the datasheet.
1430 struct regulator *regulator_get_optional(struct device *dev, const char *id)
1432 return _regulator_get(dev, id, 0);
1434 EXPORT_SYMBOL_GPL(regulator_get_optional);
1436 /* Locks held by regulator_put() */
1437 static void _regulator_put(struct regulator *regulator)
1439 struct regulator_dev *rdev;
1441 if (regulator == NULL || IS_ERR(regulator))
1444 rdev = regulator->rdev;
1446 debugfs_remove_recursive(regulator->debugfs);
1448 /* remove any sysfs entries */
1450 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1451 kfree(regulator->supply_name);
1452 list_del(®ulator->list);
1456 rdev->exclusive = 0;
1458 module_put(rdev->owner);
1462 * regulator_put - "free" the regulator source
1463 * @regulator: regulator source
1465 * Note: drivers must ensure that all regulator_enable calls made on this
1466 * regulator source are balanced by regulator_disable calls prior to calling
1469 void regulator_put(struct regulator *regulator)
1471 mutex_lock(®ulator_list_mutex);
1472 _regulator_put(regulator);
1473 mutex_unlock(®ulator_list_mutex);
1475 EXPORT_SYMBOL_GPL(regulator_put);
1478 * regulator_register_supply_alias - Provide device alias for supply lookup
1480 * @dev: device that will be given as the regulator "consumer"
1481 * @id: Supply name or regulator ID
1482 * @alias_dev: device that should be used to lookup the supply
1483 * @alias_id: Supply name or regulator ID that should be used to lookup the
1486 * All lookups for id on dev will instead be conducted for alias_id on
1489 int regulator_register_supply_alias(struct device *dev, const char *id,
1490 struct device *alias_dev,
1491 const char *alias_id)
1493 struct regulator_supply_alias *map;
1495 map = regulator_find_supply_alias(dev, id);
1499 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
1504 map->src_supply = id;
1505 map->alias_dev = alias_dev;
1506 map->alias_supply = alias_id;
1508 list_add(&map->list, ®ulator_supply_alias_list);
1510 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
1511 id, dev_name(dev), alias_id, dev_name(alias_dev));
1515 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
1518 * regulator_unregister_supply_alias - Remove device alias
1520 * @dev: device that will be given as the regulator "consumer"
1521 * @id: Supply name or regulator ID
1523 * Remove a lookup alias if one exists for id on dev.
1525 void regulator_unregister_supply_alias(struct device *dev, const char *id)
1527 struct regulator_supply_alias *map;
1529 map = regulator_find_supply_alias(dev, id);
1531 list_del(&map->list);
1535 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
1538 * regulator_bulk_register_supply_alias - register multiple aliases
1540 * @dev: device that will be given as the regulator "consumer"
1541 * @id: List of supply names or regulator IDs
1542 * @alias_dev: device that should be used to lookup the supply
1543 * @alias_id: List of supply names or regulator IDs that should be used to
1545 * @num_id: Number of aliases to register
1547 * @return 0 on success, an errno on failure.
1549 * This helper function allows drivers to register several supply
1550 * aliases in one operation. If any of the aliases cannot be
1551 * registered any aliases that were registered will be removed
1552 * before returning to the caller.
1554 int regulator_bulk_register_supply_alias(struct device *dev, const char **id,
1555 struct device *alias_dev,
1556 const char **alias_id,
1562 for (i = 0; i < num_id; ++i) {
1563 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
1573 "Failed to create supply alias %s,%s -> %s,%s\n",
1574 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
1577 regulator_unregister_supply_alias(dev, id[i]);
1581 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
1584 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
1586 * @dev: device that will be given as the regulator "consumer"
1587 * @id: List of supply names or regulator IDs
1588 * @num_id: Number of aliases to unregister
1590 * This helper function allows drivers to unregister several supply
1591 * aliases in one operation.
1593 void regulator_bulk_unregister_supply_alias(struct device *dev,
1599 for (i = 0; i < num_id; ++i)
1600 regulator_unregister_supply_alias(dev, id[i]);
1602 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
1605 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1606 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1607 const struct regulator_config *config)
1609 struct regulator_enable_gpio *pin;
1612 list_for_each_entry(pin, ®ulator_ena_gpio_list, list) {
1613 if (pin->gpio == config->ena_gpio) {
1614 rdev_dbg(rdev, "GPIO %d is already used\n",
1616 goto update_ena_gpio_to_rdev;
1620 ret = gpio_request_one(config->ena_gpio,
1621 GPIOF_DIR_OUT | config->ena_gpio_flags,
1622 rdev_get_name(rdev));
1626 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1628 gpio_free(config->ena_gpio);
1632 pin->gpio = config->ena_gpio;
1633 pin->ena_gpio_invert = config->ena_gpio_invert;
1634 list_add(&pin->list, ®ulator_ena_gpio_list);
1636 update_ena_gpio_to_rdev:
1637 pin->request_count++;
1638 rdev->ena_pin = pin;
1642 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1644 struct regulator_enable_gpio *pin, *n;
1649 /* Free the GPIO only in case of no use */
1650 list_for_each_entry_safe(pin, n, ®ulator_ena_gpio_list, list) {
1651 if (pin->gpio == rdev->ena_pin->gpio) {
1652 if (pin->request_count <= 1) {
1653 pin->request_count = 0;
1654 gpio_free(pin->gpio);
1655 list_del(&pin->list);
1658 pin->request_count--;
1665 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1666 * @rdev: regulator_dev structure
1667 * @enable: enable GPIO at initial use?
1669 * GPIO is enabled in case of initial use. (enable_count is 0)
1670 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1672 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1674 struct regulator_enable_gpio *pin = rdev->ena_pin;
1680 /* Enable GPIO at initial use */
1681 if (pin->enable_count == 0)
1682 gpio_set_value_cansleep(pin->gpio,
1683 !pin->ena_gpio_invert);
1685 pin->enable_count++;
1687 if (pin->enable_count > 1) {
1688 pin->enable_count--;
1692 /* Disable GPIO if not used */
1693 if (pin->enable_count <= 1) {
1694 gpio_set_value_cansleep(pin->gpio,
1695 pin->ena_gpio_invert);
1696 pin->enable_count = 0;
1703 static int _regulator_do_enable(struct regulator_dev *rdev)
1707 /* Query before enabling in case configuration dependent. */
1708 ret = _regulator_get_enable_time(rdev);
1712 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1716 trace_regulator_enable(rdev_get_name(rdev));
1718 if (rdev->ena_pin) {
1719 ret = regulator_ena_gpio_ctrl(rdev, true);
1722 rdev->ena_gpio_state = 1;
1723 } else if (rdev->desc->ops->enable) {
1724 ret = rdev->desc->ops->enable(rdev);
1731 /* Allow the regulator to ramp; it would be useful to extend
1732 * this for bulk operations so that the regulators can ramp
1734 trace_regulator_enable_delay(rdev_get_name(rdev));
1736 if (delay >= 1000) {
1737 mdelay(delay / 1000);
1738 udelay(delay % 1000);
1743 trace_regulator_enable_complete(rdev_get_name(rdev));
1748 /* locks held by regulator_enable() */
1749 static int _regulator_enable(struct regulator_dev *rdev)
1753 /* check voltage and requested load before enabling */
1754 if (rdev->constraints &&
1755 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1756 drms_uA_update(rdev);
1758 if (rdev->use_count == 0) {
1759 /* The regulator may on if it's not switchable or left on */
1760 ret = _regulator_is_enabled(rdev);
1761 if (ret == -EINVAL || ret == 0) {
1762 if (!_regulator_can_change_status(rdev))
1765 ret = _regulator_do_enable(rdev);
1769 } else if (ret < 0) {
1770 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1773 /* Fallthrough on positive return values - already enabled */
1782 * regulator_enable - enable regulator output
1783 * @regulator: regulator source
1785 * Request that the regulator be enabled with the regulator output at
1786 * the predefined voltage or current value. Calls to regulator_enable()
1787 * must be balanced with calls to regulator_disable().
1789 * NOTE: the output value can be set by other drivers, boot loader or may be
1790 * hardwired in the regulator.
1792 int regulator_enable(struct regulator *regulator)
1794 struct regulator_dev *rdev = regulator->rdev;
1797 if (regulator->always_on)
1801 ret = regulator_enable(rdev->supply);
1806 mutex_lock(&rdev->mutex);
1807 ret = _regulator_enable(rdev);
1808 mutex_unlock(&rdev->mutex);
1810 if (ret != 0 && rdev->supply)
1811 regulator_disable(rdev->supply);
1815 EXPORT_SYMBOL_GPL(regulator_enable);
1817 static int _regulator_do_disable(struct regulator_dev *rdev)
1821 trace_regulator_disable(rdev_get_name(rdev));
1823 if (rdev->ena_pin) {
1824 ret = regulator_ena_gpio_ctrl(rdev, false);
1827 rdev->ena_gpio_state = 0;
1829 } else if (rdev->desc->ops->disable) {
1830 ret = rdev->desc->ops->disable(rdev);
1835 trace_regulator_disable_complete(rdev_get_name(rdev));
1837 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1842 /* locks held by regulator_disable() */
1843 static int _regulator_disable(struct regulator_dev *rdev)
1847 if (WARN(rdev->use_count <= 0,
1848 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1851 /* are we the last user and permitted to disable ? */
1852 if (rdev->use_count == 1 &&
1853 (rdev->constraints && !rdev->constraints->always_on)) {
1855 /* we are last user */
1856 if (_regulator_can_change_status(rdev)) {
1857 ret = _regulator_do_disable(rdev);
1859 rdev_err(rdev, "failed to disable\n");
1864 rdev->use_count = 0;
1865 } else if (rdev->use_count > 1) {
1867 if (rdev->constraints &&
1868 (rdev->constraints->valid_ops_mask &
1869 REGULATOR_CHANGE_DRMS))
1870 drms_uA_update(rdev);
1879 * regulator_disable - disable regulator output
1880 * @regulator: regulator source
1882 * Disable the regulator output voltage or current. Calls to
1883 * regulator_enable() must be balanced with calls to
1884 * regulator_disable().
1886 * NOTE: this will only disable the regulator output if no other consumer
1887 * devices have it enabled, the regulator device supports disabling and
1888 * machine constraints permit this operation.
1890 int regulator_disable(struct regulator *regulator)
1892 struct regulator_dev *rdev = regulator->rdev;
1895 if (regulator->always_on)
1898 mutex_lock(&rdev->mutex);
1899 ret = _regulator_disable(rdev);
1900 mutex_unlock(&rdev->mutex);
1902 if (ret == 0 && rdev->supply)
1903 regulator_disable(rdev->supply);
1907 EXPORT_SYMBOL_GPL(regulator_disable);
1909 /* locks held by regulator_force_disable() */
1910 static int _regulator_force_disable(struct regulator_dev *rdev)
1915 if (rdev->desc->ops->disable) {
1916 /* ah well, who wants to live forever... */
1917 ret = rdev->desc->ops->disable(rdev);
1919 rdev_err(rdev, "failed to force disable\n");
1922 /* notify other consumers that power has been forced off */
1923 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1924 REGULATOR_EVENT_DISABLE, NULL);
1931 * regulator_force_disable - force disable regulator output
1932 * @regulator: regulator source
1934 * Forcibly disable the regulator output voltage or current.
1935 * NOTE: this *will* disable the regulator output even if other consumer
1936 * devices have it enabled. This should be used for situations when device
1937 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1939 int regulator_force_disable(struct regulator *regulator)
1941 struct regulator_dev *rdev = regulator->rdev;
1944 mutex_lock(&rdev->mutex);
1945 regulator->uA_load = 0;
1946 ret = _regulator_force_disable(regulator->rdev);
1947 mutex_unlock(&rdev->mutex);
1950 while (rdev->open_count--)
1951 regulator_disable(rdev->supply);
1955 EXPORT_SYMBOL_GPL(regulator_force_disable);
1957 static void regulator_disable_work(struct work_struct *work)
1959 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1963 mutex_lock(&rdev->mutex);
1965 BUG_ON(!rdev->deferred_disables);
1967 count = rdev->deferred_disables;
1968 rdev->deferred_disables = 0;
1970 for (i = 0; i < count; i++) {
1971 ret = _regulator_disable(rdev);
1973 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1976 mutex_unlock(&rdev->mutex);
1979 for (i = 0; i < count; i++) {
1980 ret = regulator_disable(rdev->supply);
1983 "Supply disable failed: %d\n", ret);
1990 * regulator_disable_deferred - disable regulator output with delay
1991 * @regulator: regulator source
1992 * @ms: miliseconds until the regulator is disabled
1994 * Execute regulator_disable() on the regulator after a delay. This
1995 * is intended for use with devices that require some time to quiesce.
1997 * NOTE: this will only disable the regulator output if no other consumer
1998 * devices have it enabled, the regulator device supports disabling and
1999 * machine constraints permit this operation.
2001 int regulator_disable_deferred(struct regulator *regulator, int ms)
2003 struct regulator_dev *rdev = regulator->rdev;
2006 if (regulator->always_on)
2010 return regulator_disable(regulator);
2012 mutex_lock(&rdev->mutex);
2013 rdev->deferred_disables++;
2014 mutex_unlock(&rdev->mutex);
2016 ret = queue_delayed_work(system_power_efficient_wq,
2017 &rdev->disable_work,
2018 msecs_to_jiffies(ms));
2024 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2026 static int _regulator_is_enabled(struct regulator_dev *rdev)
2028 /* A GPIO control always takes precedence */
2030 return rdev->ena_gpio_state;
2032 /* If we don't know then assume that the regulator is always on */
2033 if (!rdev->desc->ops->is_enabled)
2036 return rdev->desc->ops->is_enabled(rdev);
2040 * regulator_is_enabled - is the regulator output enabled
2041 * @regulator: regulator source
2043 * Returns positive if the regulator driver backing the source/client
2044 * has requested that the device be enabled, zero if it hasn't, else a
2045 * negative errno code.
2047 * Note that the device backing this regulator handle can have multiple
2048 * users, so it might be enabled even if regulator_enable() was never
2049 * called for this particular source.
2051 int regulator_is_enabled(struct regulator *regulator)
2055 if (regulator->always_on)
2058 mutex_lock(®ulator->rdev->mutex);
2059 ret = _regulator_is_enabled(regulator->rdev);
2060 mutex_unlock(®ulator->rdev->mutex);
2064 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2067 * regulator_can_change_voltage - check if regulator can change voltage
2068 * @regulator: regulator source
2070 * Returns positive if the regulator driver backing the source/client
2071 * can change its voltage, false otherwise. Usefull for detecting fixed
2072 * or dummy regulators and disabling voltage change logic in the client
2075 int regulator_can_change_voltage(struct regulator *regulator)
2077 struct regulator_dev *rdev = regulator->rdev;
2079 if (rdev->constraints &&
2080 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2081 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2084 if (rdev->desc->continuous_voltage_range &&
2085 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2086 rdev->constraints->min_uV != rdev->constraints->max_uV)
2092 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2095 * regulator_count_voltages - count regulator_list_voltage() selectors
2096 * @regulator: regulator source
2098 * Returns number of selectors, or negative errno. Selectors are
2099 * numbered starting at zero, and typically correspond to bitfields
2100 * in hardware registers.
2102 int regulator_count_voltages(struct regulator *regulator)
2104 struct regulator_dev *rdev = regulator->rdev;
2106 return rdev->desc->n_voltages ? : -EINVAL;
2108 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2111 * regulator_list_voltage - enumerate supported voltages
2112 * @regulator: regulator source
2113 * @selector: identify voltage to list
2114 * Context: can sleep
2116 * Returns a voltage that can be passed to @regulator_set_voltage(),
2117 * zero if this selector code can't be used on this system, or a
2120 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2122 struct regulator_dev *rdev = regulator->rdev;
2123 struct regulator_ops *ops = rdev->desc->ops;
2126 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2129 mutex_lock(&rdev->mutex);
2130 ret = ops->list_voltage(rdev, selector);
2131 mutex_unlock(&rdev->mutex);
2134 if (ret < rdev->constraints->min_uV)
2136 else if (ret > rdev->constraints->max_uV)
2142 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2145 * regulator_get_linear_step - return the voltage step size between VSEL values
2146 * @regulator: regulator source
2148 * Returns the voltage step size between VSEL values for linear
2149 * regulators, or return 0 if the regulator isn't a linear regulator.
2151 unsigned int regulator_get_linear_step(struct regulator *regulator)
2153 struct regulator_dev *rdev = regulator->rdev;
2155 return rdev->desc->uV_step;
2157 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2160 * regulator_is_supported_voltage - check if a voltage range can be supported
2162 * @regulator: Regulator to check.
2163 * @min_uV: Minimum required voltage in uV.
2164 * @max_uV: Maximum required voltage in uV.
2166 * Returns a boolean or a negative error code.
2168 int regulator_is_supported_voltage(struct regulator *regulator,
2169 int min_uV, int max_uV)
2171 struct regulator_dev *rdev = regulator->rdev;
2172 int i, voltages, ret;
2174 /* If we can't change voltage check the current voltage */
2175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2176 ret = regulator_get_voltage(regulator);
2178 return (min_uV <= ret && ret <= max_uV);
2183 /* Any voltage within constrains range is fine? */
2184 if (rdev->desc->continuous_voltage_range)
2185 return min_uV >= rdev->constraints->min_uV &&
2186 max_uV <= rdev->constraints->max_uV;
2188 ret = regulator_count_voltages(regulator);
2193 for (i = 0; i < voltages; i++) {
2194 ret = regulator_list_voltage(regulator, i);
2196 if (ret >= min_uV && ret <= max_uV)
2202 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2204 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2205 int min_uV, int max_uV)
2210 unsigned int selector;
2211 int old_selector = -1;
2213 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2215 min_uV += rdev->constraints->uV_offset;
2216 max_uV += rdev->constraints->uV_offset;
2219 * If we can't obtain the old selector there is not enough
2220 * info to call set_voltage_time_sel().
2222 if (_regulator_is_enabled(rdev) &&
2223 rdev->desc->ops->set_voltage_time_sel &&
2224 rdev->desc->ops->get_voltage_sel) {
2225 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2226 if (old_selector < 0)
2227 return old_selector;
2230 if (rdev->desc->ops->set_voltage) {
2231 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2235 if (rdev->desc->ops->list_voltage)
2236 best_val = rdev->desc->ops->list_voltage(rdev,
2239 best_val = _regulator_get_voltage(rdev);
2242 } else if (rdev->desc->ops->set_voltage_sel) {
2243 if (rdev->desc->ops->map_voltage) {
2244 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2247 if (rdev->desc->ops->list_voltage ==
2248 regulator_list_voltage_linear)
2249 ret = regulator_map_voltage_linear(rdev,
2252 ret = regulator_map_voltage_iterate(rdev,
2257 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2258 if (min_uV <= best_val && max_uV >= best_val) {
2260 if (old_selector == selector)
2263 ret = rdev->desc->ops->set_voltage_sel(
2273 /* Call set_voltage_time_sel if successfully obtained old_selector */
2274 if (ret == 0 && !rdev->constraints->ramp_disable && old_selector >= 0
2275 && old_selector != selector) {
2277 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2278 old_selector, selector);
2280 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2285 /* Insert any necessary delays */
2286 if (delay >= 1000) {
2287 mdelay(delay / 1000);
2288 udelay(delay % 1000);
2294 if (ret == 0 && best_val >= 0) {
2295 unsigned long data = best_val;
2297 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2301 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2307 * regulator_set_voltage - set regulator output voltage
2308 * @regulator: regulator source
2309 * @min_uV: Minimum required voltage in uV
2310 * @max_uV: Maximum acceptable voltage in uV
2312 * Sets a voltage regulator to the desired output voltage. This can be set
2313 * during any regulator state. IOW, regulator can be disabled or enabled.
2315 * If the regulator is enabled then the voltage will change to the new value
2316 * immediately otherwise if the regulator is disabled the regulator will
2317 * output at the new voltage when enabled.
2319 * NOTE: If the regulator is shared between several devices then the lowest
2320 * request voltage that meets the system constraints will be used.
2321 * Regulator system constraints must be set for this regulator before
2322 * calling this function otherwise this call will fail.
2324 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2326 struct regulator_dev *rdev = regulator->rdev;
2328 int old_min_uV, old_max_uV;
2330 mutex_lock(&rdev->mutex);
2332 /* If we're setting the same range as last time the change
2333 * should be a noop (some cpufreq implementations use the same
2334 * voltage for multiple frequencies, for example).
2336 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2340 if (!rdev->desc->ops->set_voltage &&
2341 !rdev->desc->ops->set_voltage_sel) {
2346 /* constraints check */
2347 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2351 /* restore original values in case of error */
2352 old_min_uV = regulator->min_uV;
2353 old_max_uV = regulator->max_uV;
2354 regulator->min_uV = min_uV;
2355 regulator->max_uV = max_uV;
2357 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2361 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2366 mutex_unlock(&rdev->mutex);
2369 regulator->min_uV = old_min_uV;
2370 regulator->max_uV = old_max_uV;
2371 mutex_unlock(&rdev->mutex);
2374 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2377 * regulator_set_voltage_time - get raise/fall time
2378 * @regulator: regulator source
2379 * @old_uV: starting voltage in microvolts
2380 * @new_uV: target voltage in microvolts
2382 * Provided with the starting and ending voltage, this function attempts to
2383 * calculate the time in microseconds required to rise or fall to this new
2386 int regulator_set_voltage_time(struct regulator *regulator,
2387 int old_uV, int new_uV)
2389 struct regulator_dev *rdev = regulator->rdev;
2390 struct regulator_ops *ops = rdev->desc->ops;
2396 /* Currently requires operations to do this */
2397 if (!ops->list_voltage || !ops->set_voltage_time_sel
2398 || !rdev->desc->n_voltages)
2401 for (i = 0; i < rdev->desc->n_voltages; i++) {
2402 /* We only look for exact voltage matches here */
2403 voltage = regulator_list_voltage(regulator, i);
2408 if (voltage == old_uV)
2410 if (voltage == new_uV)
2414 if (old_sel < 0 || new_sel < 0)
2417 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2419 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2422 * regulator_set_voltage_time_sel - get raise/fall time
2423 * @rdev: regulator source device
2424 * @old_selector: selector for starting voltage
2425 * @new_selector: selector for target voltage
2427 * Provided with the starting and target voltage selectors, this function
2428 * returns time in microseconds required to rise or fall to this new voltage
2430 * Drivers providing ramp_delay in regulation_constraints can use this as their
2431 * set_voltage_time_sel() operation.
2433 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2434 unsigned int old_selector,
2435 unsigned int new_selector)
2437 unsigned int ramp_delay = 0;
2438 int old_volt, new_volt;
2440 if (rdev->constraints->ramp_delay)
2441 ramp_delay = rdev->constraints->ramp_delay;
2442 else if (rdev->desc->ramp_delay)
2443 ramp_delay = rdev->desc->ramp_delay;
2445 if (ramp_delay == 0) {
2446 rdev_warn(rdev, "ramp_delay not set\n");
2451 if (!rdev->desc->ops->list_voltage)
2454 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2455 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2457 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2459 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2462 * regulator_sync_voltage - re-apply last regulator output voltage
2463 * @regulator: regulator source
2465 * Re-apply the last configured voltage. This is intended to be used
2466 * where some external control source the consumer is cooperating with
2467 * has caused the configured voltage to change.
2469 int regulator_sync_voltage(struct regulator *regulator)
2471 struct regulator_dev *rdev = regulator->rdev;
2472 int ret, min_uV, max_uV;
2474 mutex_lock(&rdev->mutex);
2476 if (!rdev->desc->ops->set_voltage &&
2477 !rdev->desc->ops->set_voltage_sel) {
2482 /* This is only going to work if we've had a voltage configured. */
2483 if (!regulator->min_uV && !regulator->max_uV) {
2488 min_uV = regulator->min_uV;
2489 max_uV = regulator->max_uV;
2491 /* This should be a paranoia check... */
2492 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2496 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2500 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2503 mutex_unlock(&rdev->mutex);
2506 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2508 static int _regulator_get_voltage(struct regulator_dev *rdev)
2512 if (rdev->desc->ops->get_voltage_sel) {
2513 sel = rdev->desc->ops->get_voltage_sel(rdev);
2516 ret = rdev->desc->ops->list_voltage(rdev, sel);
2517 } else if (rdev->desc->ops->get_voltage) {
2518 ret = rdev->desc->ops->get_voltage(rdev);
2519 } else if (rdev->desc->ops->list_voltage) {
2520 ret = rdev->desc->ops->list_voltage(rdev, 0);
2527 return ret - rdev->constraints->uV_offset;
2531 * regulator_get_voltage - get regulator output voltage
2532 * @regulator: regulator source
2534 * This returns the current regulator voltage in uV.
2536 * NOTE: If the regulator is disabled it will return the voltage value. This
2537 * function should not be used to determine regulator state.
2539 int regulator_get_voltage(struct regulator *regulator)
2543 mutex_lock(®ulator->rdev->mutex);
2545 ret = _regulator_get_voltage(regulator->rdev);
2547 mutex_unlock(®ulator->rdev->mutex);
2551 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2554 * regulator_set_current_limit - set regulator output current limit
2555 * @regulator: regulator source
2556 * @min_uA: Minimum supported current in uA
2557 * @max_uA: Maximum supported current in uA
2559 * Sets current sink to the desired output current. This can be set during
2560 * any regulator state. IOW, regulator can be disabled or enabled.
2562 * If the regulator is enabled then the current will change to the new value
2563 * immediately otherwise if the regulator is disabled the regulator will
2564 * output at the new current when enabled.
2566 * NOTE: Regulator system constraints must be set for this regulator before
2567 * calling this function otherwise this call will fail.
2569 int regulator_set_current_limit(struct regulator *regulator,
2570 int min_uA, int max_uA)
2572 struct regulator_dev *rdev = regulator->rdev;
2575 mutex_lock(&rdev->mutex);
2578 if (!rdev->desc->ops->set_current_limit) {
2583 /* constraints check */
2584 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2588 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2590 mutex_unlock(&rdev->mutex);
2593 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2595 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2599 mutex_lock(&rdev->mutex);
2602 if (!rdev->desc->ops->get_current_limit) {
2607 ret = rdev->desc->ops->get_current_limit(rdev);
2609 mutex_unlock(&rdev->mutex);
2614 * regulator_get_current_limit - get regulator output current
2615 * @regulator: regulator source
2617 * This returns the current supplied by the specified current sink in uA.
2619 * NOTE: If the regulator is disabled it will return the current value. This
2620 * function should not be used to determine regulator state.
2622 int regulator_get_current_limit(struct regulator *regulator)
2624 return _regulator_get_current_limit(regulator->rdev);
2626 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2629 * regulator_set_mode - set regulator operating mode
2630 * @regulator: regulator source
2631 * @mode: operating mode - one of the REGULATOR_MODE constants
2633 * Set regulator operating mode to increase regulator efficiency or improve
2634 * regulation performance.
2636 * NOTE: Regulator system constraints must be set for this regulator before
2637 * calling this function otherwise this call will fail.
2639 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2641 struct regulator_dev *rdev = regulator->rdev;
2643 int regulator_curr_mode;
2645 mutex_lock(&rdev->mutex);
2648 if (!rdev->desc->ops->set_mode) {
2653 /* return if the same mode is requested */
2654 if (rdev->desc->ops->get_mode) {
2655 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2656 if (regulator_curr_mode == mode) {
2662 /* constraints check */
2663 ret = regulator_mode_constrain(rdev, &mode);
2667 ret = rdev->desc->ops->set_mode(rdev, mode);
2669 mutex_unlock(&rdev->mutex);
2672 EXPORT_SYMBOL_GPL(regulator_set_mode);
2674 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2678 mutex_lock(&rdev->mutex);
2681 if (!rdev->desc->ops->get_mode) {
2686 ret = rdev->desc->ops->get_mode(rdev);
2688 mutex_unlock(&rdev->mutex);
2693 * regulator_get_mode - get regulator operating mode
2694 * @regulator: regulator source
2696 * Get the current regulator operating mode.
2698 unsigned int regulator_get_mode(struct regulator *regulator)
2700 return _regulator_get_mode(regulator->rdev);
2702 EXPORT_SYMBOL_GPL(regulator_get_mode);
2705 * regulator_set_optimum_mode - set regulator optimum operating mode
2706 * @regulator: regulator source
2707 * @uA_load: load current
2709 * Notifies the regulator core of a new device load. This is then used by
2710 * DRMS (if enabled by constraints) to set the most efficient regulator
2711 * operating mode for the new regulator loading.
2713 * Consumer devices notify their supply regulator of the maximum power
2714 * they will require (can be taken from device datasheet in the power
2715 * consumption tables) when they change operational status and hence power
2716 * state. Examples of operational state changes that can affect power
2717 * consumption are :-
2719 * o Device is opened / closed.
2720 * o Device I/O is about to begin or has just finished.
2721 * o Device is idling in between work.
2723 * This information is also exported via sysfs to userspace.
2725 * DRMS will sum the total requested load on the regulator and change
2726 * to the most efficient operating mode if platform constraints allow.
2728 * Returns the new regulator mode or error.
2730 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2732 struct regulator_dev *rdev = regulator->rdev;
2733 struct regulator *consumer;
2734 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2738 input_uV = regulator_get_voltage(rdev->supply);
2740 mutex_lock(&rdev->mutex);
2743 * first check to see if we can set modes at all, otherwise just
2744 * tell the consumer everything is OK.
2746 regulator->uA_load = uA_load;
2747 ret = regulator_check_drms(rdev);
2753 if (!rdev->desc->ops->get_optimum_mode)
2757 * we can actually do this so any errors are indicators of
2758 * potential real failure.
2762 if (!rdev->desc->ops->set_mode)
2765 /* get output voltage */
2766 output_uV = _regulator_get_voltage(rdev);
2767 if (output_uV <= 0) {
2768 rdev_err(rdev, "invalid output voltage found\n");
2772 /* No supply? Use constraint voltage */
2774 input_uV = rdev->constraints->input_uV;
2775 if (input_uV <= 0) {
2776 rdev_err(rdev, "invalid input voltage found\n");
2780 /* calc total requested load for this regulator */
2781 list_for_each_entry(consumer, &rdev->consumer_list, list)
2782 total_uA_load += consumer->uA_load;
2784 mode = rdev->desc->ops->get_optimum_mode(rdev,
2785 input_uV, output_uV,
2787 ret = regulator_mode_constrain(rdev, &mode);
2789 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2790 total_uA_load, input_uV, output_uV);
2794 ret = rdev->desc->ops->set_mode(rdev, mode);
2796 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2801 mutex_unlock(&rdev->mutex);
2804 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2807 * regulator_allow_bypass - allow the regulator to go into bypass mode
2809 * @regulator: Regulator to configure
2810 * @enable: enable or disable bypass mode
2812 * Allow the regulator to go into bypass mode if all other consumers
2813 * for the regulator also enable bypass mode and the machine
2814 * constraints allow this. Bypass mode means that the regulator is
2815 * simply passing the input directly to the output with no regulation.
2817 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2819 struct regulator_dev *rdev = regulator->rdev;
2822 if (!rdev->desc->ops->set_bypass)
2825 if (rdev->constraints &&
2826 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2829 mutex_lock(&rdev->mutex);
2831 if (enable && !regulator->bypass) {
2832 rdev->bypass_count++;
2834 if (rdev->bypass_count == rdev->open_count) {
2835 ret = rdev->desc->ops->set_bypass(rdev, enable);
2837 rdev->bypass_count--;
2840 } else if (!enable && regulator->bypass) {
2841 rdev->bypass_count--;
2843 if (rdev->bypass_count != rdev->open_count) {
2844 ret = rdev->desc->ops->set_bypass(rdev, enable);
2846 rdev->bypass_count++;
2851 regulator->bypass = enable;
2853 mutex_unlock(&rdev->mutex);
2857 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2860 * regulator_register_notifier - register regulator event notifier
2861 * @regulator: regulator source
2862 * @nb: notifier block
2864 * Register notifier block to receive regulator events.
2866 int regulator_register_notifier(struct regulator *regulator,
2867 struct notifier_block *nb)
2869 return blocking_notifier_chain_register(®ulator->rdev->notifier,
2872 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2875 * regulator_unregister_notifier - unregister regulator event notifier
2876 * @regulator: regulator source
2877 * @nb: notifier block
2879 * Unregister regulator event notifier block.
2881 int regulator_unregister_notifier(struct regulator *regulator,
2882 struct notifier_block *nb)
2884 return blocking_notifier_chain_unregister(®ulator->rdev->notifier,
2887 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2889 /* notify regulator consumers and downstream regulator consumers.
2890 * Note mutex must be held by caller.
2892 static void _notifier_call_chain(struct regulator_dev *rdev,
2893 unsigned long event, void *data)
2895 /* call rdev chain first */
2896 blocking_notifier_call_chain(&rdev->notifier, event, data);
2900 * regulator_bulk_get - get multiple regulator consumers
2902 * @dev: Device to supply
2903 * @num_consumers: Number of consumers to register
2904 * @consumers: Configuration of consumers; clients are stored here.
2906 * @return 0 on success, an errno on failure.
2908 * This helper function allows drivers to get several regulator
2909 * consumers in one operation. If any of the regulators cannot be
2910 * acquired then any regulators that were allocated will be freed
2911 * before returning to the caller.
2913 int regulator_bulk_get(struct device *dev, int num_consumers,
2914 struct regulator_bulk_data *consumers)
2919 for (i = 0; i < num_consumers; i++)
2920 consumers[i].consumer = NULL;
2922 for (i = 0; i < num_consumers; i++) {
2923 consumers[i].consumer = regulator_get(dev,
2924 consumers[i].supply);
2925 if (IS_ERR(consumers[i].consumer)) {
2926 ret = PTR_ERR(consumers[i].consumer);
2927 dev_err(dev, "Failed to get supply '%s': %d\n",
2928 consumers[i].supply, ret);
2929 consumers[i].consumer = NULL;
2938 regulator_put(consumers[i].consumer);
2942 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2944 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2946 struct regulator_bulk_data *bulk = data;
2948 bulk->ret = regulator_enable(bulk->consumer);
2952 * regulator_bulk_enable - enable multiple regulator consumers
2954 * @num_consumers: Number of consumers
2955 * @consumers: Consumer data; clients are stored here.
2956 * @return 0 on success, an errno on failure
2958 * This convenience API allows consumers to enable multiple regulator
2959 * clients in a single API call. If any consumers cannot be enabled
2960 * then any others that were enabled will be disabled again prior to
2963 int regulator_bulk_enable(int num_consumers,
2964 struct regulator_bulk_data *consumers)
2966 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2970 for (i = 0; i < num_consumers; i++) {
2971 if (consumers[i].consumer->always_on)
2972 consumers[i].ret = 0;
2974 async_schedule_domain(regulator_bulk_enable_async,
2975 &consumers[i], &async_domain);
2978 async_synchronize_full_domain(&async_domain);
2980 /* If any consumer failed we need to unwind any that succeeded */
2981 for (i = 0; i < num_consumers; i++) {
2982 if (consumers[i].ret != 0) {
2983 ret = consumers[i].ret;
2991 for (i = 0; i < num_consumers; i++) {
2992 if (consumers[i].ret < 0)
2993 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
2996 regulator_disable(consumers[i].consumer);
3001 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3004 * regulator_bulk_disable - disable multiple regulator consumers
3006 * @num_consumers: Number of consumers
3007 * @consumers: Consumer data; clients are stored here.
3008 * @return 0 on success, an errno on failure
3010 * This convenience API allows consumers to disable multiple regulator
3011 * clients in a single API call. If any consumers cannot be disabled
3012 * then any others that were disabled will be enabled again prior to
3015 int regulator_bulk_disable(int num_consumers,
3016 struct regulator_bulk_data *consumers)
3021 for (i = num_consumers - 1; i >= 0; --i) {
3022 ret = regulator_disable(consumers[i].consumer);
3030 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3031 for (++i; i < num_consumers; ++i) {
3032 r = regulator_enable(consumers[i].consumer);
3034 pr_err("Failed to reename %s: %d\n",
3035 consumers[i].supply, r);
3040 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3043 * regulator_bulk_force_disable - force disable multiple regulator consumers
3045 * @num_consumers: Number of consumers
3046 * @consumers: Consumer data; clients are stored here.
3047 * @return 0 on success, an errno on failure
3049 * This convenience API allows consumers to forcibly disable multiple regulator
3050 * clients in a single API call.
3051 * NOTE: This should be used for situations when device damage will
3052 * likely occur if the regulators are not disabled (e.g. over temp).
3053 * Although regulator_force_disable function call for some consumers can
3054 * return error numbers, the function is called for all consumers.
3056 int regulator_bulk_force_disable(int num_consumers,
3057 struct regulator_bulk_data *consumers)
3062 for (i = 0; i < num_consumers; i++)
3064 regulator_force_disable(consumers[i].consumer);
3066 for (i = 0; i < num_consumers; i++) {
3067 if (consumers[i].ret != 0) {
3068 ret = consumers[i].ret;
3077 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3080 * regulator_bulk_free - free multiple regulator consumers
3082 * @num_consumers: Number of consumers
3083 * @consumers: Consumer data; clients are stored here.
3085 * This convenience API allows consumers to free multiple regulator
3086 * clients in a single API call.
3088 void regulator_bulk_free(int num_consumers,
3089 struct regulator_bulk_data *consumers)
3093 for (i = 0; i < num_consumers; i++) {
3094 regulator_put(consumers[i].consumer);
3095 consumers[i].consumer = NULL;
3098 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3101 * regulator_notifier_call_chain - call regulator event notifier
3102 * @rdev: regulator source
3103 * @event: notifier block
3104 * @data: callback-specific data.
3106 * Called by regulator drivers to notify clients a regulator event has
3107 * occurred. We also notify regulator clients downstream.
3108 * Note lock must be held by caller.
3110 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3111 unsigned long event, void *data)
3113 _notifier_call_chain(rdev, event, data);
3117 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3120 * regulator_mode_to_status - convert a regulator mode into a status
3122 * @mode: Mode to convert
3124 * Convert a regulator mode into a status.
3126 int regulator_mode_to_status(unsigned int mode)
3129 case REGULATOR_MODE_FAST:
3130 return REGULATOR_STATUS_FAST;
3131 case REGULATOR_MODE_NORMAL:
3132 return REGULATOR_STATUS_NORMAL;
3133 case REGULATOR_MODE_IDLE:
3134 return REGULATOR_STATUS_IDLE;
3135 case REGULATOR_MODE_STANDBY:
3136 return REGULATOR_STATUS_STANDBY;
3138 return REGULATOR_STATUS_UNDEFINED;
3141 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3144 * To avoid cluttering sysfs (and memory) with useless state, only
3145 * create attributes that can be meaningfully displayed.
3147 static int add_regulator_attributes(struct regulator_dev *rdev)
3149 struct device *dev = &rdev->dev;
3150 struct regulator_ops *ops = rdev->desc->ops;
3153 /* some attributes need specific methods to be displayed */
3154 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3155 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3156 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3157 status = device_create_file(dev, &dev_attr_microvolts);
3161 if (ops->get_current_limit) {
3162 status = device_create_file(dev, &dev_attr_microamps);
3166 if (ops->get_mode) {
3167 status = device_create_file(dev, &dev_attr_opmode);
3171 if (rdev->ena_pin || ops->is_enabled) {
3172 status = device_create_file(dev, &dev_attr_state);
3176 if (ops->get_status) {
3177 status = device_create_file(dev, &dev_attr_status);
3181 if (ops->get_bypass) {
3182 status = device_create_file(dev, &dev_attr_bypass);
3187 /* some attributes are type-specific */
3188 if (rdev->desc->type == REGULATOR_CURRENT) {
3189 status = device_create_file(dev, &dev_attr_requested_microamps);
3194 /* all the other attributes exist to support constraints;
3195 * don't show them if there are no constraints, or if the
3196 * relevant supporting methods are missing.
3198 if (!rdev->constraints)
3201 /* constraints need specific supporting methods */
3202 if (ops->set_voltage || ops->set_voltage_sel) {
3203 status = device_create_file(dev, &dev_attr_min_microvolts);
3206 status = device_create_file(dev, &dev_attr_max_microvolts);
3210 if (ops->set_current_limit) {
3211 status = device_create_file(dev, &dev_attr_min_microamps);
3214 status = device_create_file(dev, &dev_attr_max_microamps);
3219 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3222 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3225 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3229 if (ops->set_suspend_voltage) {
3230 status = device_create_file(dev,
3231 &dev_attr_suspend_standby_microvolts);
3234 status = device_create_file(dev,
3235 &dev_attr_suspend_mem_microvolts);
3238 status = device_create_file(dev,
3239 &dev_attr_suspend_disk_microvolts);
3244 if (ops->set_suspend_mode) {
3245 status = device_create_file(dev,
3246 &dev_attr_suspend_standby_mode);
3249 status = device_create_file(dev,
3250 &dev_attr_suspend_mem_mode);
3253 status = device_create_file(dev,
3254 &dev_attr_suspend_disk_mode);
3262 static void rdev_init_debugfs(struct regulator_dev *rdev)
3264 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3265 if (!rdev->debugfs) {
3266 rdev_warn(rdev, "Failed to create debugfs directory\n");
3270 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3272 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3274 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3275 &rdev->bypass_count);
3279 * regulator_register - register regulator
3280 * @regulator_desc: regulator to register
3281 * @config: runtime configuration for regulator
3283 * Called by regulator drivers to register a regulator.
3284 * Returns a valid pointer to struct regulator_dev on success
3285 * or an ERR_PTR() on error.
3287 struct regulator_dev *
3288 regulator_register(const struct regulator_desc *regulator_desc,
3289 const struct regulator_config *config)
3291 const struct regulation_constraints *constraints = NULL;
3292 const struct regulator_init_data *init_data;
3293 static atomic_t regulator_no = ATOMIC_INIT(0);
3294 struct regulator_dev *rdev;
3297 const char *supply = NULL;
3299 if (regulator_desc == NULL || config == NULL)
3300 return ERR_PTR(-EINVAL);
3305 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3306 return ERR_PTR(-EINVAL);
3308 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3309 regulator_desc->type != REGULATOR_CURRENT)
3310 return ERR_PTR(-EINVAL);
3312 /* Only one of each should be implemented */
3313 WARN_ON(regulator_desc->ops->get_voltage &&
3314 regulator_desc->ops->get_voltage_sel);
3315 WARN_ON(regulator_desc->ops->set_voltage &&
3316 regulator_desc->ops->set_voltage_sel);
3318 /* If we're using selectors we must implement list_voltage. */
3319 if (regulator_desc->ops->get_voltage_sel &&
3320 !regulator_desc->ops->list_voltage) {
3321 return ERR_PTR(-EINVAL);
3323 if (regulator_desc->ops->set_voltage_sel &&
3324 !regulator_desc->ops->list_voltage) {
3325 return ERR_PTR(-EINVAL);
3328 init_data = config->init_data;
3330 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3332 return ERR_PTR(-ENOMEM);
3334 mutex_lock(®ulator_list_mutex);
3336 mutex_init(&rdev->mutex);
3337 rdev->reg_data = config->driver_data;
3338 rdev->owner = regulator_desc->owner;
3339 rdev->desc = regulator_desc;
3341 rdev->regmap = config->regmap;
3342 else if (dev_get_regmap(dev, NULL))
3343 rdev->regmap = dev_get_regmap(dev, NULL);
3344 else if (dev->parent)
3345 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3346 INIT_LIST_HEAD(&rdev->consumer_list);
3347 INIT_LIST_HEAD(&rdev->list);
3348 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3349 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3351 /* preform any regulator specific init */
3352 if (init_data && init_data->regulator_init) {
3353 ret = init_data->regulator_init(rdev->reg_data);
3358 /* register with sysfs */
3359 rdev->dev.class = ®ulator_class;
3360 rdev->dev.of_node = config->of_node;
3361 rdev->dev.parent = dev;
3362 dev_set_name(&rdev->dev, "regulator.%d",
3363 atomic_inc_return(®ulator_no) - 1);
3364 ret = device_register(&rdev->dev);
3366 put_device(&rdev->dev);
3370 dev_set_drvdata(&rdev->dev, rdev);
3372 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3373 ret = regulator_ena_gpio_request(rdev, config);
3375 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3376 config->ena_gpio, ret);
3380 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3381 rdev->ena_gpio_state = 1;
3383 if (config->ena_gpio_invert)
3384 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3387 /* set regulator constraints */
3389 constraints = &init_data->constraints;
3391 ret = set_machine_constraints(rdev, constraints);
3395 /* add attributes supported by this regulator */
3396 ret = add_regulator_attributes(rdev);
3400 if (init_data && init_data->supply_regulator)
3401 supply = init_data->supply_regulator;
3402 else if (regulator_desc->supply_name)
3403 supply = regulator_desc->supply_name;
3406 struct regulator_dev *r;
3408 r = regulator_dev_lookup(dev, supply, &ret);
3410 if (ret == -ENODEV) {
3412 * No supply was specified for this regulator and
3413 * there will never be one.
3418 dev_err(dev, "Failed to find supply %s\n", supply);
3419 ret = -EPROBE_DEFER;
3423 ret = set_supply(rdev, r);
3427 /* Enable supply if rail is enabled */
3428 if (_regulator_is_enabled(rdev)) {
3429 ret = regulator_enable(rdev->supply);
3436 /* add consumers devices */
3438 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3439 ret = set_consumer_device_supply(rdev,
3440 init_data->consumer_supplies[i].dev_name,
3441 init_data->consumer_supplies[i].supply);
3443 dev_err(dev, "Failed to set supply %s\n",
3444 init_data->consumer_supplies[i].supply);
3445 goto unset_supplies;
3450 list_add(&rdev->list, ®ulator_list);
3452 rdev_init_debugfs(rdev);
3454 mutex_unlock(®ulator_list_mutex);
3458 unset_regulator_supplies(rdev);
3462 _regulator_put(rdev->supply);
3463 regulator_ena_gpio_free(rdev);
3464 kfree(rdev->constraints);
3466 device_unregister(&rdev->dev);
3467 /* device core frees rdev */
3468 rdev = ERR_PTR(ret);
3473 rdev = ERR_PTR(ret);
3476 EXPORT_SYMBOL_GPL(regulator_register);
3479 * regulator_unregister - unregister regulator
3480 * @rdev: regulator to unregister
3482 * Called by regulator drivers to unregister a regulator.
3484 void regulator_unregister(struct regulator_dev *rdev)
3490 while (rdev->use_count--)
3491 regulator_disable(rdev->supply);
3492 regulator_put(rdev->supply);
3494 mutex_lock(®ulator_list_mutex);
3495 debugfs_remove_recursive(rdev->debugfs);
3496 flush_work(&rdev->disable_work.work);
3497 WARN_ON(rdev->open_count);
3498 unset_regulator_supplies(rdev);
3499 list_del(&rdev->list);
3500 kfree(rdev->constraints);
3501 regulator_ena_gpio_free(rdev);
3502 device_unregister(&rdev->dev);
3503 mutex_unlock(®ulator_list_mutex);
3505 EXPORT_SYMBOL_GPL(regulator_unregister);
3508 * regulator_suspend_prepare - prepare regulators for system wide suspend
3509 * @state: system suspend state
3511 * Configure each regulator with it's suspend operating parameters for state.
3512 * This will usually be called by machine suspend code prior to supending.
3514 int regulator_suspend_prepare(suspend_state_t state)
3516 struct regulator_dev *rdev;
3519 /* ON is handled by regulator active state */
3520 if (state == PM_SUSPEND_ON)
3523 mutex_lock(®ulator_list_mutex);
3524 list_for_each_entry(rdev, ®ulator_list, list) {
3526 mutex_lock(&rdev->mutex);
3527 ret = suspend_prepare(rdev, state);
3528 mutex_unlock(&rdev->mutex);
3531 rdev_err(rdev, "failed to prepare\n");
3536 mutex_unlock(®ulator_list_mutex);
3539 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3542 * regulator_suspend_finish - resume regulators from system wide suspend
3544 * Turn on regulators that might be turned off by regulator_suspend_prepare
3545 * and that should be turned on according to the regulators properties.
3547 int regulator_suspend_finish(void)
3549 struct regulator_dev *rdev;
3552 mutex_lock(®ulator_list_mutex);
3553 list_for_each_entry(rdev, ®ulator_list, list) {
3554 struct regulator_ops *ops = rdev->desc->ops;
3556 mutex_lock(&rdev->mutex);
3557 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3559 error = ops->enable(rdev);
3563 if (!has_full_constraints)
3567 if (!_regulator_is_enabled(rdev))
3570 error = ops->disable(rdev);
3575 mutex_unlock(&rdev->mutex);
3577 mutex_unlock(®ulator_list_mutex);
3580 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3583 * regulator_has_full_constraints - the system has fully specified constraints
3585 * Calling this function will cause the regulator API to disable all
3586 * regulators which have a zero use count and don't have an always_on
3587 * constraint in a late_initcall.
3589 * The intention is that this will become the default behaviour in a
3590 * future kernel release so users are encouraged to use this facility
3593 void regulator_has_full_constraints(void)
3595 has_full_constraints = 1;
3597 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3600 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3602 * Calling this function will cause the regulator API to provide a
3603 * dummy regulator to consumers if no physical regulator is found,
3604 * allowing most consumers to proceed as though a regulator were
3605 * configured. This allows systems such as those with software
3606 * controllable regulators for the CPU core only to be brought up more
3609 void regulator_use_dummy_regulator(void)
3611 board_wants_dummy_regulator = true;
3613 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3616 * rdev_get_drvdata - get rdev regulator driver data
3619 * Get rdev regulator driver private data. This call can be used in the
3620 * regulator driver context.
3622 void *rdev_get_drvdata(struct regulator_dev *rdev)
3624 return rdev->reg_data;
3626 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3629 * regulator_get_drvdata - get regulator driver data
3630 * @regulator: regulator
3632 * Get regulator driver private data. This call can be used in the consumer
3633 * driver context when non API regulator specific functions need to be called.
3635 void *regulator_get_drvdata(struct regulator *regulator)
3637 return regulator->rdev->reg_data;
3639 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3642 * regulator_set_drvdata - set regulator driver data
3643 * @regulator: regulator
3646 void regulator_set_drvdata(struct regulator *regulator, void *data)
3648 regulator->rdev->reg_data = data;
3650 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3653 * regulator_get_id - get regulator ID
3656 int rdev_get_id(struct regulator_dev *rdev)
3658 return rdev->desc->id;
3660 EXPORT_SYMBOL_GPL(rdev_get_id);
3662 struct device *rdev_get_dev(struct regulator_dev *rdev)
3666 EXPORT_SYMBOL_GPL(rdev_get_dev);
3668 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3670 return reg_init_data->driver_data;
3672 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3674 #ifdef CONFIG_DEBUG_FS
3675 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3676 size_t count, loff_t *ppos)
3678 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3679 ssize_t len, ret = 0;
3680 struct regulator_map *map;
3685 list_for_each_entry(map, ®ulator_map_list, list) {
3686 len = snprintf(buf + ret, PAGE_SIZE - ret,
3688 rdev_get_name(map->regulator), map->dev_name,
3692 if (ret > PAGE_SIZE) {
3698 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3706 static const struct file_operations supply_map_fops = {
3707 #ifdef CONFIG_DEBUG_FS
3708 .read = supply_map_read_file,
3709 .llseek = default_llseek,
3713 static int __init regulator_init(void)
3717 ret = class_register(®ulator_class);
3719 debugfs_root = debugfs_create_dir("regulator", NULL);
3721 pr_warn("regulator: Failed to create debugfs directory\n");
3723 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3726 regulator_dummy_init();
3731 /* init early to allow our consumers to complete system booting */
3732 core_initcall(regulator_init);
3734 static int __init regulator_init_complete(void)
3736 struct regulator_dev *rdev;
3737 struct regulator_ops *ops;
3738 struct regulation_constraints *c;
3742 * Since DT doesn't provide an idiomatic mechanism for
3743 * enabling full constraints and since it's much more natural
3744 * with DT to provide them just assume that a DT enabled
3745 * system has full constraints.
3747 if (of_have_populated_dt())
3748 has_full_constraints = true;
3750 mutex_lock(®ulator_list_mutex);
3752 /* If we have a full configuration then disable any regulators
3753 * which are not in use or always_on. This will become the
3754 * default behaviour in the future.
3756 list_for_each_entry(rdev, ®ulator_list, list) {
3757 ops = rdev->desc->ops;
3758 c = rdev->constraints;
3760 if (!ops->disable || (c && c->always_on))
3763 mutex_lock(&rdev->mutex);
3765 if (rdev->use_count)
3768 /* If we can't read the status assume it's on. */
3769 if (ops->is_enabled)
3770 enabled = ops->is_enabled(rdev);
3777 if (has_full_constraints) {
3778 /* We log since this may kill the system if it
3780 rdev_info(rdev, "disabling\n");
3781 ret = ops->disable(rdev);
3783 rdev_err(rdev, "couldn't disable: %d\n", ret);
3786 /* The intention is that in future we will
3787 * assume that full constraints are provided
3788 * so warn even if we aren't going to do
3791 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3795 mutex_unlock(&rdev->mutex);
3798 mutex_unlock(®ulator_list_mutex);
3802 late_initcall(regulator_init_complete);