spi: sprd: adi: Add a reset reason for watchdog mode
[linux/fpc-iii.git] / drivers / regulator / core.c
blobe0c0cf462004958635420a95d11ca00eb2b9d472
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // core.c -- Voltage/Current Regulator framework.
4 //
5 // Copyright 2007, 2008 Wolfson Microelectronics PLC.
6 // Copyright 2008 SlimLogic Ltd.
7 //
8 // Author: Liam Girdwood <lrg@slimlogic.co.uk>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/debugfs.h>
13 #include <linux/device.h>
14 #include <linux/slab.h>
15 #include <linux/async.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/gpio/consumer.h>
21 #include <linux/of.h>
22 #include <linux/regmap.h>
23 #include <linux/regulator/of_regulator.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/regulator/coupler.h>
26 #include <linux/regulator/driver.h>
27 #include <linux/regulator/machine.h>
28 #include <linux/module.h>
30 #define CREATE_TRACE_POINTS
31 #include <trace/events/regulator.h>
33 #include "dummy.h"
34 #include "internal.h"
36 #define rdev_crit(rdev, fmt, ...) \
37 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38 #define rdev_err(rdev, fmt, ...) \
39 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40 #define rdev_warn(rdev, fmt, ...) \
41 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_info(rdev, fmt, ...) \
43 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_dbg(rdev, fmt, ...) \
45 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 static DEFINE_WW_CLASS(regulator_ww_class);
48 static DEFINE_MUTEX(regulator_nesting_mutex);
49 static DEFINE_MUTEX(regulator_list_mutex);
50 static LIST_HEAD(regulator_map_list);
51 static LIST_HEAD(regulator_ena_gpio_list);
52 static LIST_HEAD(regulator_supply_alias_list);
53 static LIST_HEAD(regulator_coupler_list);
54 static bool has_full_constraints;
56 static struct dentry *debugfs_root;
59 * struct regulator_map
61 * Used to provide symbolic supply names to devices.
63 struct regulator_map {
64 struct list_head list;
65 const char *dev_name; /* The dev_name() for the consumer */
66 const char *supply;
67 struct regulator_dev *regulator;
71 * struct regulator_enable_gpio
73 * Management for shared enable GPIO pin
75 struct regulator_enable_gpio {
76 struct list_head list;
77 struct gpio_desc *gpiod;
78 u32 enable_count; /* a number of enabled shared GPIO */
79 u32 request_count; /* a number of requested shared GPIO */
83 * struct regulator_supply_alias
85 * Used to map lookups for a supply onto an alternative device.
87 struct regulator_supply_alias {
88 struct list_head list;
89 struct device *src_dev;
90 const char *src_supply;
91 struct device *alias_dev;
92 const char *alias_supply;
95 static int _regulator_is_enabled(struct regulator_dev *rdev);
96 static int _regulator_disable(struct regulator *regulator);
97 static int _regulator_get_current_limit(struct regulator_dev *rdev);
98 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
99 static int _notifier_call_chain(struct regulator_dev *rdev,
100 unsigned long event, void *data);
101 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
102 int min_uV, int max_uV);
103 static int regulator_balance_voltage(struct regulator_dev *rdev,
104 suspend_state_t state);
105 static struct regulator *create_regulator(struct regulator_dev *rdev,
106 struct device *dev,
107 const char *supply_name);
108 static void _regulator_put(struct regulator *regulator);
110 const char *rdev_get_name(struct regulator_dev *rdev)
112 if (rdev->constraints && rdev->constraints->name)
113 return rdev->constraints->name;
114 else if (rdev->desc->name)
115 return rdev->desc->name;
116 else
117 return "";
120 static bool have_full_constraints(void)
122 return has_full_constraints || of_have_populated_dt();
125 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
127 if (!rdev->constraints) {
128 rdev_err(rdev, "no constraints\n");
129 return false;
132 if (rdev->constraints->valid_ops_mask & ops)
133 return true;
135 return false;
139 * regulator_lock_nested - lock a single regulator
140 * @rdev: regulator source
141 * @ww_ctx: w/w mutex acquire context
143 * This function can be called many times by one task on
144 * a single regulator and its mutex will be locked only
145 * once. If a task, which is calling this function is other
146 * than the one, which initially locked the mutex, it will
147 * wait on mutex.
149 static inline int regulator_lock_nested(struct regulator_dev *rdev,
150 struct ww_acquire_ctx *ww_ctx)
152 bool lock = false;
153 int ret = 0;
155 mutex_lock(&regulator_nesting_mutex);
157 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
158 if (rdev->mutex_owner == current)
159 rdev->ref_cnt++;
160 else
161 lock = true;
163 if (lock) {
164 mutex_unlock(&regulator_nesting_mutex);
165 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
166 mutex_lock(&regulator_nesting_mutex);
168 } else {
169 lock = true;
172 if (lock && ret != -EDEADLK) {
173 rdev->ref_cnt++;
174 rdev->mutex_owner = current;
177 mutex_unlock(&regulator_nesting_mutex);
179 return ret;
183 * regulator_lock - lock a single regulator
184 * @rdev: regulator source
186 * This function can be called many times by one task on
187 * a single regulator and its mutex will be locked only
188 * once. If a task, which is calling this function is other
189 * than the one, which initially locked the mutex, it will
190 * wait on mutex.
192 void regulator_lock(struct regulator_dev *rdev)
194 regulator_lock_nested(rdev, NULL);
196 EXPORT_SYMBOL_GPL(regulator_lock);
199 * regulator_unlock - unlock a single regulator
200 * @rdev: regulator_source
202 * This function unlocks the mutex when the
203 * reference counter reaches 0.
205 void regulator_unlock(struct regulator_dev *rdev)
207 mutex_lock(&regulator_nesting_mutex);
209 if (--rdev->ref_cnt == 0) {
210 rdev->mutex_owner = NULL;
211 ww_mutex_unlock(&rdev->mutex);
214 WARN_ON_ONCE(rdev->ref_cnt < 0);
216 mutex_unlock(&regulator_nesting_mutex);
218 EXPORT_SYMBOL_GPL(regulator_unlock);
220 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
222 struct regulator_dev *c_rdev;
223 int i;
225 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
226 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
228 if (rdev->supply->rdev == c_rdev)
229 return true;
232 return false;
235 static void regulator_unlock_recursive(struct regulator_dev *rdev,
236 unsigned int n_coupled)
238 struct regulator_dev *c_rdev;
239 int i;
241 for (i = n_coupled; i > 0; i--) {
242 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
244 if (!c_rdev)
245 continue;
247 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
248 regulator_unlock_recursive(
249 c_rdev->supply->rdev,
250 c_rdev->coupling_desc.n_coupled);
252 regulator_unlock(c_rdev);
256 static int regulator_lock_recursive(struct regulator_dev *rdev,
257 struct regulator_dev **new_contended_rdev,
258 struct regulator_dev **old_contended_rdev,
259 struct ww_acquire_ctx *ww_ctx)
261 struct regulator_dev *c_rdev;
262 int i, err;
264 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
265 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
267 if (!c_rdev)
268 continue;
270 if (c_rdev != *old_contended_rdev) {
271 err = regulator_lock_nested(c_rdev, ww_ctx);
272 if (err) {
273 if (err == -EDEADLK) {
274 *new_contended_rdev = c_rdev;
275 goto err_unlock;
278 /* shouldn't happen */
279 WARN_ON_ONCE(err != -EALREADY);
281 } else {
282 *old_contended_rdev = NULL;
285 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
286 err = regulator_lock_recursive(c_rdev->supply->rdev,
287 new_contended_rdev,
288 old_contended_rdev,
289 ww_ctx);
290 if (err) {
291 regulator_unlock(c_rdev);
292 goto err_unlock;
297 return 0;
299 err_unlock:
300 regulator_unlock_recursive(rdev, i);
302 return err;
306 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
307 * regulators
308 * @rdev: regulator source
309 * @ww_ctx: w/w mutex acquire context
311 * Unlock all regulators related with rdev by coupling or supplying.
313 static void regulator_unlock_dependent(struct regulator_dev *rdev,
314 struct ww_acquire_ctx *ww_ctx)
316 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
317 ww_acquire_fini(ww_ctx);
321 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
322 * @rdev: regulator source
323 * @ww_ctx: w/w mutex acquire context
325 * This function as a wrapper on regulator_lock_recursive(), which locks
326 * all regulators related with rdev by coupling or supplying.
328 static void regulator_lock_dependent(struct regulator_dev *rdev,
329 struct ww_acquire_ctx *ww_ctx)
331 struct regulator_dev *new_contended_rdev = NULL;
332 struct regulator_dev *old_contended_rdev = NULL;
333 int err;
335 mutex_lock(&regulator_list_mutex);
337 ww_acquire_init(ww_ctx, &regulator_ww_class);
339 do {
340 if (new_contended_rdev) {
341 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
342 old_contended_rdev = new_contended_rdev;
343 old_contended_rdev->ref_cnt++;
346 err = regulator_lock_recursive(rdev,
347 &new_contended_rdev,
348 &old_contended_rdev,
349 ww_ctx);
351 if (old_contended_rdev)
352 regulator_unlock(old_contended_rdev);
354 } while (err == -EDEADLK);
356 ww_acquire_done(ww_ctx);
358 mutex_unlock(&regulator_list_mutex);
362 * of_get_child_regulator - get a child regulator device node
363 * based on supply name
364 * @parent: Parent device node
365 * @prop_name: Combination regulator supply name and "-supply"
367 * Traverse all child nodes.
368 * Extract the child regulator device node corresponding to the supply name.
369 * returns the device node corresponding to the regulator if found, else
370 * returns NULL.
372 static struct device_node *of_get_child_regulator(struct device_node *parent,
373 const char *prop_name)
375 struct device_node *regnode = NULL;
376 struct device_node *child = NULL;
378 for_each_child_of_node(parent, child) {
379 regnode = of_parse_phandle(child, prop_name, 0);
381 if (!regnode) {
382 regnode = of_get_child_regulator(child, prop_name);
383 if (regnode)
384 return regnode;
385 } else {
386 return regnode;
389 return NULL;
393 * of_get_regulator - get a regulator device node based on supply name
394 * @dev: Device pointer for the consumer (of regulator) device
395 * @supply: regulator supply name
397 * Extract the regulator device node corresponding to the supply name.
398 * returns the device node corresponding to the regulator if found, else
399 * returns NULL.
401 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
403 struct device_node *regnode = NULL;
404 char prop_name[32]; /* 32 is max size of property name */
406 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
408 snprintf(prop_name, 32, "%s-supply", supply);
409 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
411 if (!regnode) {
412 regnode = of_get_child_regulator(dev->of_node, prop_name);
413 if (regnode)
414 return regnode;
416 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
417 prop_name, dev->of_node);
418 return NULL;
420 return regnode;
423 /* Platform voltage constraint check */
424 int regulator_check_voltage(struct regulator_dev *rdev,
425 int *min_uV, int *max_uV)
427 BUG_ON(*min_uV > *max_uV);
429 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
430 rdev_err(rdev, "voltage operation not allowed\n");
431 return -EPERM;
434 if (*max_uV > rdev->constraints->max_uV)
435 *max_uV = rdev->constraints->max_uV;
436 if (*min_uV < rdev->constraints->min_uV)
437 *min_uV = rdev->constraints->min_uV;
439 if (*min_uV > *max_uV) {
440 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
441 *min_uV, *max_uV);
442 return -EINVAL;
445 return 0;
448 /* return 0 if the state is valid */
449 static int regulator_check_states(suspend_state_t state)
451 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
454 /* Make sure we select a voltage that suits the needs of all
455 * regulator consumers
457 int regulator_check_consumers(struct regulator_dev *rdev,
458 int *min_uV, int *max_uV,
459 suspend_state_t state)
461 struct regulator *regulator;
462 struct regulator_voltage *voltage;
464 list_for_each_entry(regulator, &rdev->consumer_list, list) {
465 voltage = &regulator->voltage[state];
467 * Assume consumers that didn't say anything are OK
468 * with anything in the constraint range.
470 if (!voltage->min_uV && !voltage->max_uV)
471 continue;
473 if (*max_uV > voltage->max_uV)
474 *max_uV = voltage->max_uV;
475 if (*min_uV < voltage->min_uV)
476 *min_uV = voltage->min_uV;
479 if (*min_uV > *max_uV) {
480 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
481 *min_uV, *max_uV);
482 return -EINVAL;
485 return 0;
488 /* current constraint check */
489 static int regulator_check_current_limit(struct regulator_dev *rdev,
490 int *min_uA, int *max_uA)
492 BUG_ON(*min_uA > *max_uA);
494 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
495 rdev_err(rdev, "current operation not allowed\n");
496 return -EPERM;
499 if (*max_uA > rdev->constraints->max_uA)
500 *max_uA = rdev->constraints->max_uA;
501 if (*min_uA < rdev->constraints->min_uA)
502 *min_uA = rdev->constraints->min_uA;
504 if (*min_uA > *max_uA) {
505 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
506 *min_uA, *max_uA);
507 return -EINVAL;
510 return 0;
513 /* operating mode constraint check */
514 static int regulator_mode_constrain(struct regulator_dev *rdev,
515 unsigned int *mode)
517 switch (*mode) {
518 case REGULATOR_MODE_FAST:
519 case REGULATOR_MODE_NORMAL:
520 case REGULATOR_MODE_IDLE:
521 case REGULATOR_MODE_STANDBY:
522 break;
523 default:
524 rdev_err(rdev, "invalid mode %x specified\n", *mode);
525 return -EINVAL;
528 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
529 rdev_err(rdev, "mode operation not allowed\n");
530 return -EPERM;
533 /* The modes are bitmasks, the most power hungry modes having
534 * the lowest values. If the requested mode isn't supported
535 * try higher modes. */
536 while (*mode) {
537 if (rdev->constraints->valid_modes_mask & *mode)
538 return 0;
539 *mode /= 2;
542 return -EINVAL;
545 static inline struct regulator_state *
546 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
548 if (rdev->constraints == NULL)
549 return NULL;
551 switch (state) {
552 case PM_SUSPEND_STANDBY:
553 return &rdev->constraints->state_standby;
554 case PM_SUSPEND_MEM:
555 return &rdev->constraints->state_mem;
556 case PM_SUSPEND_MAX:
557 return &rdev->constraints->state_disk;
558 default:
559 return NULL;
563 static ssize_t regulator_uV_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
566 struct regulator_dev *rdev = dev_get_drvdata(dev);
567 ssize_t ret;
569 regulator_lock(rdev);
570 ret = sprintf(buf, "%d\n", regulator_get_voltage_rdev(rdev));
571 regulator_unlock(rdev);
573 return ret;
575 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
577 static ssize_t regulator_uA_show(struct device *dev,
578 struct device_attribute *attr, char *buf)
580 struct regulator_dev *rdev = dev_get_drvdata(dev);
582 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
584 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
586 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
587 char *buf)
589 struct regulator_dev *rdev = dev_get_drvdata(dev);
591 return sprintf(buf, "%s\n", rdev_get_name(rdev));
593 static DEVICE_ATTR_RO(name);
595 static const char *regulator_opmode_to_str(int mode)
597 switch (mode) {
598 case REGULATOR_MODE_FAST:
599 return "fast";
600 case REGULATOR_MODE_NORMAL:
601 return "normal";
602 case REGULATOR_MODE_IDLE:
603 return "idle";
604 case REGULATOR_MODE_STANDBY:
605 return "standby";
607 return "unknown";
610 static ssize_t regulator_print_opmode(char *buf, int mode)
612 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
615 static ssize_t regulator_opmode_show(struct device *dev,
616 struct device_attribute *attr, char *buf)
618 struct regulator_dev *rdev = dev_get_drvdata(dev);
620 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
622 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
624 static ssize_t regulator_print_state(char *buf, int state)
626 if (state > 0)
627 return sprintf(buf, "enabled\n");
628 else if (state == 0)
629 return sprintf(buf, "disabled\n");
630 else
631 return sprintf(buf, "unknown\n");
634 static ssize_t regulator_state_show(struct device *dev,
635 struct device_attribute *attr, char *buf)
637 struct regulator_dev *rdev = dev_get_drvdata(dev);
638 ssize_t ret;
640 regulator_lock(rdev);
641 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
642 regulator_unlock(rdev);
644 return ret;
646 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
648 static ssize_t regulator_status_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
651 struct regulator_dev *rdev = dev_get_drvdata(dev);
652 int status;
653 char *label;
655 status = rdev->desc->ops->get_status(rdev);
656 if (status < 0)
657 return status;
659 switch (status) {
660 case REGULATOR_STATUS_OFF:
661 label = "off";
662 break;
663 case REGULATOR_STATUS_ON:
664 label = "on";
665 break;
666 case REGULATOR_STATUS_ERROR:
667 label = "error";
668 break;
669 case REGULATOR_STATUS_FAST:
670 label = "fast";
671 break;
672 case REGULATOR_STATUS_NORMAL:
673 label = "normal";
674 break;
675 case REGULATOR_STATUS_IDLE:
676 label = "idle";
677 break;
678 case REGULATOR_STATUS_STANDBY:
679 label = "standby";
680 break;
681 case REGULATOR_STATUS_BYPASS:
682 label = "bypass";
683 break;
684 case REGULATOR_STATUS_UNDEFINED:
685 label = "undefined";
686 break;
687 default:
688 return -ERANGE;
691 return sprintf(buf, "%s\n", label);
693 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
695 static ssize_t regulator_min_uA_show(struct device *dev,
696 struct device_attribute *attr, char *buf)
698 struct regulator_dev *rdev = dev_get_drvdata(dev);
700 if (!rdev->constraints)
701 return sprintf(buf, "constraint not defined\n");
703 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
705 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
707 static ssize_t regulator_max_uA_show(struct device *dev,
708 struct device_attribute *attr, char *buf)
710 struct regulator_dev *rdev = dev_get_drvdata(dev);
712 if (!rdev->constraints)
713 return sprintf(buf, "constraint not defined\n");
715 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
717 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
719 static ssize_t regulator_min_uV_show(struct device *dev,
720 struct device_attribute *attr, char *buf)
722 struct regulator_dev *rdev = dev_get_drvdata(dev);
724 if (!rdev->constraints)
725 return sprintf(buf, "constraint not defined\n");
727 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
729 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
731 static ssize_t regulator_max_uV_show(struct device *dev,
732 struct device_attribute *attr, char *buf)
734 struct regulator_dev *rdev = dev_get_drvdata(dev);
736 if (!rdev->constraints)
737 return sprintf(buf, "constraint not defined\n");
739 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
741 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
743 static ssize_t regulator_total_uA_show(struct device *dev,
744 struct device_attribute *attr, char *buf)
746 struct regulator_dev *rdev = dev_get_drvdata(dev);
747 struct regulator *regulator;
748 int uA = 0;
750 regulator_lock(rdev);
751 list_for_each_entry(regulator, &rdev->consumer_list, list) {
752 if (regulator->enable_count)
753 uA += regulator->uA_load;
755 regulator_unlock(rdev);
756 return sprintf(buf, "%d\n", uA);
758 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
760 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
761 char *buf)
763 struct regulator_dev *rdev = dev_get_drvdata(dev);
764 return sprintf(buf, "%d\n", rdev->use_count);
766 static DEVICE_ATTR_RO(num_users);
768 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
769 char *buf)
771 struct regulator_dev *rdev = dev_get_drvdata(dev);
773 switch (rdev->desc->type) {
774 case REGULATOR_VOLTAGE:
775 return sprintf(buf, "voltage\n");
776 case REGULATOR_CURRENT:
777 return sprintf(buf, "current\n");
779 return sprintf(buf, "unknown\n");
781 static DEVICE_ATTR_RO(type);
783 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
784 struct device_attribute *attr, char *buf)
786 struct regulator_dev *rdev = dev_get_drvdata(dev);
788 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
790 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
791 regulator_suspend_mem_uV_show, NULL);
793 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
794 struct device_attribute *attr, char *buf)
796 struct regulator_dev *rdev = dev_get_drvdata(dev);
798 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
800 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
801 regulator_suspend_disk_uV_show, NULL);
803 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
804 struct device_attribute *attr, char *buf)
806 struct regulator_dev *rdev = dev_get_drvdata(dev);
808 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
810 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
811 regulator_suspend_standby_uV_show, NULL);
813 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
814 struct device_attribute *attr, char *buf)
816 struct regulator_dev *rdev = dev_get_drvdata(dev);
818 return regulator_print_opmode(buf,
819 rdev->constraints->state_mem.mode);
821 static DEVICE_ATTR(suspend_mem_mode, 0444,
822 regulator_suspend_mem_mode_show, NULL);
824 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
825 struct device_attribute *attr, char *buf)
827 struct regulator_dev *rdev = dev_get_drvdata(dev);
829 return regulator_print_opmode(buf,
830 rdev->constraints->state_disk.mode);
832 static DEVICE_ATTR(suspend_disk_mode, 0444,
833 regulator_suspend_disk_mode_show, NULL);
835 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
836 struct device_attribute *attr, char *buf)
838 struct regulator_dev *rdev = dev_get_drvdata(dev);
840 return regulator_print_opmode(buf,
841 rdev->constraints->state_standby.mode);
843 static DEVICE_ATTR(suspend_standby_mode, 0444,
844 regulator_suspend_standby_mode_show, NULL);
846 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
847 struct device_attribute *attr, char *buf)
849 struct regulator_dev *rdev = dev_get_drvdata(dev);
851 return regulator_print_state(buf,
852 rdev->constraints->state_mem.enabled);
854 static DEVICE_ATTR(suspend_mem_state, 0444,
855 regulator_suspend_mem_state_show, NULL);
857 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
858 struct device_attribute *attr, char *buf)
860 struct regulator_dev *rdev = dev_get_drvdata(dev);
862 return regulator_print_state(buf,
863 rdev->constraints->state_disk.enabled);
865 static DEVICE_ATTR(suspend_disk_state, 0444,
866 regulator_suspend_disk_state_show, NULL);
868 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
869 struct device_attribute *attr, char *buf)
871 struct regulator_dev *rdev = dev_get_drvdata(dev);
873 return regulator_print_state(buf,
874 rdev->constraints->state_standby.enabled);
876 static DEVICE_ATTR(suspend_standby_state, 0444,
877 regulator_suspend_standby_state_show, NULL);
879 static ssize_t regulator_bypass_show(struct device *dev,
880 struct device_attribute *attr, char *buf)
882 struct regulator_dev *rdev = dev_get_drvdata(dev);
883 const char *report;
884 bool bypass;
885 int ret;
887 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
889 if (ret != 0)
890 report = "unknown";
891 else if (bypass)
892 report = "enabled";
893 else
894 report = "disabled";
896 return sprintf(buf, "%s\n", report);
898 static DEVICE_ATTR(bypass, 0444,
899 regulator_bypass_show, NULL);
901 /* Calculate the new optimum regulator operating mode based on the new total
902 * consumer load. All locks held by caller */
903 static int drms_uA_update(struct regulator_dev *rdev)
905 struct regulator *sibling;
906 int current_uA = 0, output_uV, input_uV, err;
907 unsigned int mode;
910 * first check to see if we can set modes at all, otherwise just
911 * tell the consumer everything is OK.
913 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
914 rdev_dbg(rdev, "DRMS operation not allowed\n");
915 return 0;
918 if (!rdev->desc->ops->get_optimum_mode &&
919 !rdev->desc->ops->set_load)
920 return 0;
922 if (!rdev->desc->ops->set_mode &&
923 !rdev->desc->ops->set_load)
924 return -EINVAL;
926 /* calc total requested load */
927 list_for_each_entry(sibling, &rdev->consumer_list, list) {
928 if (sibling->enable_count)
929 current_uA += sibling->uA_load;
932 current_uA += rdev->constraints->system_load;
934 if (rdev->desc->ops->set_load) {
935 /* set the optimum mode for our new total regulator load */
936 err = rdev->desc->ops->set_load(rdev, current_uA);
937 if (err < 0)
938 rdev_err(rdev, "failed to set load %d\n", current_uA);
939 } else {
940 /* get output voltage */
941 output_uV = regulator_get_voltage_rdev(rdev);
942 if (output_uV <= 0) {
943 rdev_err(rdev, "invalid output voltage found\n");
944 return -EINVAL;
947 /* get input voltage */
948 input_uV = 0;
949 if (rdev->supply)
950 input_uV = regulator_get_voltage(rdev->supply);
951 if (input_uV <= 0)
952 input_uV = rdev->constraints->input_uV;
953 if (input_uV <= 0) {
954 rdev_err(rdev, "invalid input voltage found\n");
955 return -EINVAL;
958 /* now get the optimum mode for our new total regulator load */
959 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
960 output_uV, current_uA);
962 /* check the new mode is allowed */
963 err = regulator_mode_constrain(rdev, &mode);
964 if (err < 0) {
965 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
966 current_uA, input_uV, output_uV);
967 return err;
970 err = rdev->desc->ops->set_mode(rdev, mode);
971 if (err < 0)
972 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
975 return err;
978 static int suspend_set_state(struct regulator_dev *rdev,
979 suspend_state_t state)
981 int ret = 0;
982 struct regulator_state *rstate;
984 rstate = regulator_get_suspend_state(rdev, state);
985 if (rstate == NULL)
986 return 0;
988 /* If we have no suspend mode configuration don't set anything;
989 * only warn if the driver implements set_suspend_voltage or
990 * set_suspend_mode callback.
992 if (rstate->enabled != ENABLE_IN_SUSPEND &&
993 rstate->enabled != DISABLE_IN_SUSPEND) {
994 if (rdev->desc->ops->set_suspend_voltage ||
995 rdev->desc->ops->set_suspend_mode)
996 rdev_warn(rdev, "No configuration\n");
997 return 0;
1000 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1001 rdev->desc->ops->set_suspend_enable)
1002 ret = rdev->desc->ops->set_suspend_enable(rdev);
1003 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1004 rdev->desc->ops->set_suspend_disable)
1005 ret = rdev->desc->ops->set_suspend_disable(rdev);
1006 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1007 ret = 0;
1009 if (ret < 0) {
1010 rdev_err(rdev, "failed to enabled/disable\n");
1011 return ret;
1014 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1015 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1016 if (ret < 0) {
1017 rdev_err(rdev, "failed to set voltage\n");
1018 return ret;
1022 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1023 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1024 if (ret < 0) {
1025 rdev_err(rdev, "failed to set mode\n");
1026 return ret;
1030 return ret;
1033 static void print_constraints(struct regulator_dev *rdev)
1035 struct regulation_constraints *constraints = rdev->constraints;
1036 char buf[160] = "";
1037 size_t len = sizeof(buf) - 1;
1038 int count = 0;
1039 int ret;
1041 if (constraints->min_uV && constraints->max_uV) {
1042 if (constraints->min_uV == constraints->max_uV)
1043 count += scnprintf(buf + count, len - count, "%d mV ",
1044 constraints->min_uV / 1000);
1045 else
1046 count += scnprintf(buf + count, len - count,
1047 "%d <--> %d mV ",
1048 constraints->min_uV / 1000,
1049 constraints->max_uV / 1000);
1052 if (!constraints->min_uV ||
1053 constraints->min_uV != constraints->max_uV) {
1054 ret = regulator_get_voltage_rdev(rdev);
1055 if (ret > 0)
1056 count += scnprintf(buf + count, len - count,
1057 "at %d mV ", ret / 1000);
1060 if (constraints->uV_offset)
1061 count += scnprintf(buf + count, len - count, "%dmV offset ",
1062 constraints->uV_offset / 1000);
1064 if (constraints->min_uA && constraints->max_uA) {
1065 if (constraints->min_uA == constraints->max_uA)
1066 count += scnprintf(buf + count, len - count, "%d mA ",
1067 constraints->min_uA / 1000);
1068 else
1069 count += scnprintf(buf + count, len - count,
1070 "%d <--> %d mA ",
1071 constraints->min_uA / 1000,
1072 constraints->max_uA / 1000);
1075 if (!constraints->min_uA ||
1076 constraints->min_uA != constraints->max_uA) {
1077 ret = _regulator_get_current_limit(rdev);
1078 if (ret > 0)
1079 count += scnprintf(buf + count, len - count,
1080 "at %d mA ", ret / 1000);
1083 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1084 count += scnprintf(buf + count, len - count, "fast ");
1085 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1086 count += scnprintf(buf + count, len - count, "normal ");
1087 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1088 count += scnprintf(buf + count, len - count, "idle ");
1089 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1090 count += scnprintf(buf + count, len - count, "standby");
1092 if (!count)
1093 scnprintf(buf, len, "no parameters");
1095 rdev_dbg(rdev, "%s\n", buf);
1097 if ((constraints->min_uV != constraints->max_uV) &&
1098 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1099 rdev_warn(rdev,
1100 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1103 static int machine_constraints_voltage(struct regulator_dev *rdev,
1104 struct regulation_constraints *constraints)
1106 const struct regulator_ops *ops = rdev->desc->ops;
1107 int ret;
1109 /* do we need to apply the constraint voltage */
1110 if (rdev->constraints->apply_uV &&
1111 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1112 int target_min, target_max;
1113 int current_uV = regulator_get_voltage_rdev(rdev);
1115 if (current_uV == -ENOTRECOVERABLE) {
1116 /* This regulator can't be read and must be initialized */
1117 rdev_info(rdev, "Setting %d-%duV\n",
1118 rdev->constraints->min_uV,
1119 rdev->constraints->max_uV);
1120 _regulator_do_set_voltage(rdev,
1121 rdev->constraints->min_uV,
1122 rdev->constraints->max_uV);
1123 current_uV = regulator_get_voltage_rdev(rdev);
1126 if (current_uV < 0) {
1127 rdev_err(rdev,
1128 "failed to get the current voltage(%d)\n",
1129 current_uV);
1130 return current_uV;
1134 * If we're below the minimum voltage move up to the
1135 * minimum voltage, if we're above the maximum voltage
1136 * then move down to the maximum.
1138 target_min = current_uV;
1139 target_max = current_uV;
1141 if (current_uV < rdev->constraints->min_uV) {
1142 target_min = rdev->constraints->min_uV;
1143 target_max = rdev->constraints->min_uV;
1146 if (current_uV > rdev->constraints->max_uV) {
1147 target_min = rdev->constraints->max_uV;
1148 target_max = rdev->constraints->max_uV;
1151 if (target_min != current_uV || target_max != current_uV) {
1152 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1153 current_uV, target_min, target_max);
1154 ret = _regulator_do_set_voltage(
1155 rdev, target_min, target_max);
1156 if (ret < 0) {
1157 rdev_err(rdev,
1158 "failed to apply %d-%duV constraint(%d)\n",
1159 target_min, target_max, ret);
1160 return ret;
1165 /* constrain machine-level voltage specs to fit
1166 * the actual range supported by this regulator.
1168 if (ops->list_voltage && rdev->desc->n_voltages) {
1169 int count = rdev->desc->n_voltages;
1170 int i;
1171 int min_uV = INT_MAX;
1172 int max_uV = INT_MIN;
1173 int cmin = constraints->min_uV;
1174 int cmax = constraints->max_uV;
1176 /* it's safe to autoconfigure fixed-voltage supplies
1177 and the constraints are used by list_voltage. */
1178 if (count == 1 && !cmin) {
1179 cmin = 1;
1180 cmax = INT_MAX;
1181 constraints->min_uV = cmin;
1182 constraints->max_uV = cmax;
1185 /* voltage constraints are optional */
1186 if ((cmin == 0) && (cmax == 0))
1187 return 0;
1189 /* else require explicit machine-level constraints */
1190 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1191 rdev_err(rdev, "invalid voltage constraints\n");
1192 return -EINVAL;
1195 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1196 for (i = 0; i < count; i++) {
1197 int value;
1199 value = ops->list_voltage(rdev, i);
1200 if (value <= 0)
1201 continue;
1203 /* maybe adjust [min_uV..max_uV] */
1204 if (value >= cmin && value < min_uV)
1205 min_uV = value;
1206 if (value <= cmax && value > max_uV)
1207 max_uV = value;
1210 /* final: [min_uV..max_uV] valid iff constraints valid */
1211 if (max_uV < min_uV) {
1212 rdev_err(rdev,
1213 "unsupportable voltage constraints %u-%uuV\n",
1214 min_uV, max_uV);
1215 return -EINVAL;
1218 /* use regulator's subset of machine constraints */
1219 if (constraints->min_uV < min_uV) {
1220 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1221 constraints->min_uV, min_uV);
1222 constraints->min_uV = min_uV;
1224 if (constraints->max_uV > max_uV) {
1225 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1226 constraints->max_uV, max_uV);
1227 constraints->max_uV = max_uV;
1231 return 0;
1234 static int machine_constraints_current(struct regulator_dev *rdev,
1235 struct regulation_constraints *constraints)
1237 const struct regulator_ops *ops = rdev->desc->ops;
1238 int ret;
1240 if (!constraints->min_uA && !constraints->max_uA)
1241 return 0;
1243 if (constraints->min_uA > constraints->max_uA) {
1244 rdev_err(rdev, "Invalid current constraints\n");
1245 return -EINVAL;
1248 if (!ops->set_current_limit || !ops->get_current_limit) {
1249 rdev_warn(rdev, "Operation of current configuration missing\n");
1250 return 0;
1253 /* Set regulator current in constraints range */
1254 ret = ops->set_current_limit(rdev, constraints->min_uA,
1255 constraints->max_uA);
1256 if (ret < 0) {
1257 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1258 return ret;
1261 return 0;
1264 static int _regulator_do_enable(struct regulator_dev *rdev);
1267 * set_machine_constraints - sets regulator constraints
1268 * @rdev: regulator source
1269 * @constraints: constraints to apply
1271 * Allows platform initialisation code to define and constrain
1272 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1273 * Constraints *must* be set by platform code in order for some
1274 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1275 * set_mode.
1277 static int set_machine_constraints(struct regulator_dev *rdev,
1278 const struct regulation_constraints *constraints)
1280 int ret = 0;
1281 const struct regulator_ops *ops = rdev->desc->ops;
1283 if (constraints)
1284 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1285 GFP_KERNEL);
1286 else
1287 rdev->constraints = kzalloc(sizeof(*constraints),
1288 GFP_KERNEL);
1289 if (!rdev->constraints)
1290 return -ENOMEM;
1292 ret = machine_constraints_voltage(rdev, rdev->constraints);
1293 if (ret != 0)
1294 return ret;
1296 ret = machine_constraints_current(rdev, rdev->constraints);
1297 if (ret != 0)
1298 return ret;
1300 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1301 ret = ops->set_input_current_limit(rdev,
1302 rdev->constraints->ilim_uA);
1303 if (ret < 0) {
1304 rdev_err(rdev, "failed to set input limit\n");
1305 return ret;
1309 /* do we need to setup our suspend state */
1310 if (rdev->constraints->initial_state) {
1311 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1312 if (ret < 0) {
1313 rdev_err(rdev, "failed to set suspend state\n");
1314 return ret;
1318 if (rdev->constraints->initial_mode) {
1319 if (!ops->set_mode) {
1320 rdev_err(rdev, "no set_mode operation\n");
1321 return -EINVAL;
1324 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1325 if (ret < 0) {
1326 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1327 return ret;
1329 } else if (rdev->constraints->system_load) {
1331 * We'll only apply the initial system load if an
1332 * initial mode wasn't specified.
1334 drms_uA_update(rdev);
1337 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1338 && ops->set_ramp_delay) {
1339 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1340 if (ret < 0) {
1341 rdev_err(rdev, "failed to set ramp_delay\n");
1342 return ret;
1346 if (rdev->constraints->pull_down && ops->set_pull_down) {
1347 ret = ops->set_pull_down(rdev);
1348 if (ret < 0) {
1349 rdev_err(rdev, "failed to set pull down\n");
1350 return ret;
1354 if (rdev->constraints->soft_start && ops->set_soft_start) {
1355 ret = ops->set_soft_start(rdev);
1356 if (ret < 0) {
1357 rdev_err(rdev, "failed to set soft start\n");
1358 return ret;
1362 if (rdev->constraints->over_current_protection
1363 && ops->set_over_current_protection) {
1364 ret = ops->set_over_current_protection(rdev);
1365 if (ret < 0) {
1366 rdev_err(rdev, "failed to set over current protection\n");
1367 return ret;
1371 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1372 bool ad_state = (rdev->constraints->active_discharge ==
1373 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1375 ret = ops->set_active_discharge(rdev, ad_state);
1376 if (ret < 0) {
1377 rdev_err(rdev, "failed to set active discharge\n");
1378 return ret;
1382 /* If the constraints say the regulator should be on at this point
1383 * and we have control then make sure it is enabled.
1385 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1386 if (rdev->supply) {
1387 ret = regulator_enable(rdev->supply);
1388 if (ret < 0) {
1389 _regulator_put(rdev->supply);
1390 rdev->supply = NULL;
1391 return ret;
1395 ret = _regulator_do_enable(rdev);
1396 if (ret < 0 && ret != -EINVAL) {
1397 rdev_err(rdev, "failed to enable\n");
1398 return ret;
1400 rdev->use_count++;
1403 print_constraints(rdev);
1404 return 0;
1408 * set_supply - set regulator supply regulator
1409 * @rdev: regulator name
1410 * @supply_rdev: supply regulator name
1412 * Called by platform initialisation code to set the supply regulator for this
1413 * regulator. This ensures that a regulators supply will also be enabled by the
1414 * core if it's child is enabled.
1416 static int set_supply(struct regulator_dev *rdev,
1417 struct regulator_dev *supply_rdev)
1419 int err;
1421 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1423 if (!try_module_get(supply_rdev->owner))
1424 return -ENODEV;
1426 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1427 if (rdev->supply == NULL) {
1428 err = -ENOMEM;
1429 return err;
1431 supply_rdev->open_count++;
1433 return 0;
1437 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1438 * @rdev: regulator source
1439 * @consumer_dev_name: dev_name() string for device supply applies to
1440 * @supply: symbolic name for supply
1442 * Allows platform initialisation code to map physical regulator
1443 * sources to symbolic names for supplies for use by devices. Devices
1444 * should use these symbolic names to request regulators, avoiding the
1445 * need to provide board-specific regulator names as platform data.
1447 static int set_consumer_device_supply(struct regulator_dev *rdev,
1448 const char *consumer_dev_name,
1449 const char *supply)
1451 struct regulator_map *node;
1452 int has_dev;
1454 if (supply == NULL)
1455 return -EINVAL;
1457 if (consumer_dev_name != NULL)
1458 has_dev = 1;
1459 else
1460 has_dev = 0;
1462 list_for_each_entry(node, &regulator_map_list, list) {
1463 if (node->dev_name && consumer_dev_name) {
1464 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1465 continue;
1466 } else if (node->dev_name || consumer_dev_name) {
1467 continue;
1470 if (strcmp(node->supply, supply) != 0)
1471 continue;
1473 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1474 consumer_dev_name,
1475 dev_name(&node->regulator->dev),
1476 node->regulator->desc->name,
1477 supply,
1478 dev_name(&rdev->dev), rdev_get_name(rdev));
1479 return -EBUSY;
1482 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1483 if (node == NULL)
1484 return -ENOMEM;
1486 node->regulator = rdev;
1487 node->supply = supply;
1489 if (has_dev) {
1490 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1491 if (node->dev_name == NULL) {
1492 kfree(node);
1493 return -ENOMEM;
1497 list_add(&node->list, &regulator_map_list);
1498 return 0;
1501 static void unset_regulator_supplies(struct regulator_dev *rdev)
1503 struct regulator_map *node, *n;
1505 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1506 if (rdev == node->regulator) {
1507 list_del(&node->list);
1508 kfree(node->dev_name);
1509 kfree(node);
1514 #ifdef CONFIG_DEBUG_FS
1515 static ssize_t constraint_flags_read_file(struct file *file,
1516 char __user *user_buf,
1517 size_t count, loff_t *ppos)
1519 const struct regulator *regulator = file->private_data;
1520 const struct regulation_constraints *c = regulator->rdev->constraints;
1521 char *buf;
1522 ssize_t ret;
1524 if (!c)
1525 return 0;
1527 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1528 if (!buf)
1529 return -ENOMEM;
1531 ret = snprintf(buf, PAGE_SIZE,
1532 "always_on: %u\n"
1533 "boot_on: %u\n"
1534 "apply_uV: %u\n"
1535 "ramp_disable: %u\n"
1536 "soft_start: %u\n"
1537 "pull_down: %u\n"
1538 "over_current_protection: %u\n",
1539 c->always_on,
1540 c->boot_on,
1541 c->apply_uV,
1542 c->ramp_disable,
1543 c->soft_start,
1544 c->pull_down,
1545 c->over_current_protection);
1547 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1548 kfree(buf);
1550 return ret;
1553 #endif
1555 static const struct file_operations constraint_flags_fops = {
1556 #ifdef CONFIG_DEBUG_FS
1557 .open = simple_open,
1558 .read = constraint_flags_read_file,
1559 .llseek = default_llseek,
1560 #endif
1563 #define REG_STR_SIZE 64
1565 static struct regulator *create_regulator(struct regulator_dev *rdev,
1566 struct device *dev,
1567 const char *supply_name)
1569 struct regulator *regulator;
1570 char buf[REG_STR_SIZE];
1571 int err, size;
1573 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1574 if (regulator == NULL)
1575 return NULL;
1577 regulator_lock(rdev);
1578 regulator->rdev = rdev;
1579 list_add(&regulator->list, &rdev->consumer_list);
1581 if (dev) {
1582 regulator->dev = dev;
1584 /* Add a link to the device sysfs entry */
1585 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1586 dev->kobj.name, supply_name);
1587 if (size >= REG_STR_SIZE)
1588 goto overflow_err;
1590 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1591 if (regulator->supply_name == NULL)
1592 goto overflow_err;
1594 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1595 buf);
1596 if (err) {
1597 rdev_dbg(rdev, "could not add device link %s err %d\n",
1598 dev->kobj.name, err);
1599 /* non-fatal */
1601 } else {
1602 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1603 if (regulator->supply_name == NULL)
1604 goto overflow_err;
1607 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1608 rdev->debugfs);
1609 if (!regulator->debugfs) {
1610 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1611 } else {
1612 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1613 &regulator->uA_load);
1614 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1615 &regulator->voltage[PM_SUSPEND_ON].min_uV);
1616 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1617 &regulator->voltage[PM_SUSPEND_ON].max_uV);
1618 debugfs_create_file("constraint_flags", 0444,
1619 regulator->debugfs, regulator,
1620 &constraint_flags_fops);
1624 * Check now if the regulator is an always on regulator - if
1625 * it is then we don't need to do nearly so much work for
1626 * enable/disable calls.
1628 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1629 _regulator_is_enabled(rdev))
1630 regulator->always_on = true;
1632 regulator_unlock(rdev);
1633 return regulator;
1634 overflow_err:
1635 list_del(&regulator->list);
1636 kfree(regulator);
1637 regulator_unlock(rdev);
1638 return NULL;
1641 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1643 if (rdev->constraints && rdev->constraints->enable_time)
1644 return rdev->constraints->enable_time;
1645 if (rdev->desc->ops->enable_time)
1646 return rdev->desc->ops->enable_time(rdev);
1647 return rdev->desc->enable_time;
1650 static struct regulator_supply_alias *regulator_find_supply_alias(
1651 struct device *dev, const char *supply)
1653 struct regulator_supply_alias *map;
1655 list_for_each_entry(map, &regulator_supply_alias_list, list)
1656 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1657 return map;
1659 return NULL;
1662 static void regulator_supply_alias(struct device **dev, const char **supply)
1664 struct regulator_supply_alias *map;
1666 map = regulator_find_supply_alias(*dev, *supply);
1667 if (map) {
1668 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1669 *supply, map->alias_supply,
1670 dev_name(map->alias_dev));
1671 *dev = map->alias_dev;
1672 *supply = map->alias_supply;
1676 static int regulator_match(struct device *dev, const void *data)
1678 struct regulator_dev *r = dev_to_rdev(dev);
1680 return strcmp(rdev_get_name(r), data) == 0;
1683 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1685 struct device *dev;
1687 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1689 return dev ? dev_to_rdev(dev) : NULL;
1693 * regulator_dev_lookup - lookup a regulator device.
1694 * @dev: device for regulator "consumer".
1695 * @supply: Supply name or regulator ID.
1697 * If successful, returns a struct regulator_dev that corresponds to the name
1698 * @supply and with the embedded struct device refcount incremented by one.
1699 * The refcount must be dropped by calling put_device().
1700 * On failure one of the following ERR-PTR-encoded values is returned:
1701 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1702 * in the future.
1704 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1705 const char *supply)
1707 struct regulator_dev *r = NULL;
1708 struct device_node *node;
1709 struct regulator_map *map;
1710 const char *devname = NULL;
1712 regulator_supply_alias(&dev, &supply);
1714 /* first do a dt based lookup */
1715 if (dev && dev->of_node) {
1716 node = of_get_regulator(dev, supply);
1717 if (node) {
1718 r = of_find_regulator_by_node(node);
1719 if (r)
1720 return r;
1723 * We have a node, but there is no device.
1724 * assume it has not registered yet.
1726 return ERR_PTR(-EPROBE_DEFER);
1730 /* if not found, try doing it non-dt way */
1731 if (dev)
1732 devname = dev_name(dev);
1734 mutex_lock(&regulator_list_mutex);
1735 list_for_each_entry(map, &regulator_map_list, list) {
1736 /* If the mapping has a device set up it must match */
1737 if (map->dev_name &&
1738 (!devname || strcmp(map->dev_name, devname)))
1739 continue;
1741 if (strcmp(map->supply, supply) == 0 &&
1742 get_device(&map->regulator->dev)) {
1743 r = map->regulator;
1744 break;
1747 mutex_unlock(&regulator_list_mutex);
1749 if (r)
1750 return r;
1752 r = regulator_lookup_by_name(supply);
1753 if (r)
1754 return r;
1756 return ERR_PTR(-ENODEV);
1759 static int regulator_resolve_supply(struct regulator_dev *rdev)
1761 struct regulator_dev *r;
1762 struct device *dev = rdev->dev.parent;
1763 int ret;
1765 /* No supply to resolve? */
1766 if (!rdev->supply_name)
1767 return 0;
1769 /* Supply already resolved? */
1770 if (rdev->supply)
1771 return 0;
1773 r = regulator_dev_lookup(dev, rdev->supply_name);
1774 if (IS_ERR(r)) {
1775 ret = PTR_ERR(r);
1777 /* Did the lookup explicitly defer for us? */
1778 if (ret == -EPROBE_DEFER)
1779 return ret;
1781 if (have_full_constraints()) {
1782 r = dummy_regulator_rdev;
1783 get_device(&r->dev);
1784 } else {
1785 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1786 rdev->supply_name, rdev->desc->name);
1787 return -EPROBE_DEFER;
1792 * If the supply's parent device is not the same as the
1793 * regulator's parent device, then ensure the parent device
1794 * is bound before we resolve the supply, in case the parent
1795 * device get probe deferred and unregisters the supply.
1797 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1798 if (!device_is_bound(r->dev.parent)) {
1799 put_device(&r->dev);
1800 return -EPROBE_DEFER;
1804 /* Recursively resolve the supply of the supply */
1805 ret = regulator_resolve_supply(r);
1806 if (ret < 0) {
1807 put_device(&r->dev);
1808 return ret;
1811 ret = set_supply(rdev, r);
1812 if (ret < 0) {
1813 put_device(&r->dev);
1814 return ret;
1818 * In set_machine_constraints() we may have turned this regulator on
1819 * but we couldn't propagate to the supply if it hadn't been resolved
1820 * yet. Do it now.
1822 if (rdev->use_count) {
1823 ret = regulator_enable(rdev->supply);
1824 if (ret < 0) {
1825 _regulator_put(rdev->supply);
1826 rdev->supply = NULL;
1827 return ret;
1831 return 0;
1834 /* Internal regulator request function */
1835 struct regulator *_regulator_get(struct device *dev, const char *id,
1836 enum regulator_get_type get_type)
1838 struct regulator_dev *rdev;
1839 struct regulator *regulator;
1840 const char *devname = dev ? dev_name(dev) : "deviceless";
1841 int ret;
1843 if (get_type >= MAX_GET_TYPE) {
1844 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1845 return ERR_PTR(-EINVAL);
1848 if (id == NULL) {
1849 pr_err("get() with no identifier\n");
1850 return ERR_PTR(-EINVAL);
1853 rdev = regulator_dev_lookup(dev, id);
1854 if (IS_ERR(rdev)) {
1855 ret = PTR_ERR(rdev);
1858 * If regulator_dev_lookup() fails with error other
1859 * than -ENODEV our job here is done, we simply return it.
1861 if (ret != -ENODEV)
1862 return ERR_PTR(ret);
1864 if (!have_full_constraints()) {
1865 dev_warn(dev,
1866 "incomplete constraints, dummy supplies not allowed\n");
1867 return ERR_PTR(-ENODEV);
1870 switch (get_type) {
1871 case NORMAL_GET:
1873 * Assume that a regulator is physically present and
1874 * enabled, even if it isn't hooked up, and just
1875 * provide a dummy.
1877 dev_warn(dev,
1878 "%s supply %s not found, using dummy regulator\n",
1879 devname, id);
1880 rdev = dummy_regulator_rdev;
1881 get_device(&rdev->dev);
1882 break;
1884 case EXCLUSIVE_GET:
1885 dev_warn(dev,
1886 "dummy supplies not allowed for exclusive requests\n");
1887 /* fall through */
1889 default:
1890 return ERR_PTR(-ENODEV);
1894 if (rdev->exclusive) {
1895 regulator = ERR_PTR(-EPERM);
1896 put_device(&rdev->dev);
1897 return regulator;
1900 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1901 regulator = ERR_PTR(-EBUSY);
1902 put_device(&rdev->dev);
1903 return regulator;
1906 mutex_lock(&regulator_list_mutex);
1907 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1908 mutex_unlock(&regulator_list_mutex);
1910 if (ret != 0) {
1911 regulator = ERR_PTR(-EPROBE_DEFER);
1912 put_device(&rdev->dev);
1913 return regulator;
1916 ret = regulator_resolve_supply(rdev);
1917 if (ret < 0) {
1918 regulator = ERR_PTR(ret);
1919 put_device(&rdev->dev);
1920 return regulator;
1923 if (!try_module_get(rdev->owner)) {
1924 regulator = ERR_PTR(-EPROBE_DEFER);
1925 put_device(&rdev->dev);
1926 return regulator;
1929 regulator = create_regulator(rdev, dev, id);
1930 if (regulator == NULL) {
1931 regulator = ERR_PTR(-ENOMEM);
1932 put_device(&rdev->dev);
1933 module_put(rdev->owner);
1934 return regulator;
1937 rdev->open_count++;
1938 if (get_type == EXCLUSIVE_GET) {
1939 rdev->exclusive = 1;
1941 ret = _regulator_is_enabled(rdev);
1942 if (ret > 0)
1943 rdev->use_count = 1;
1944 else
1945 rdev->use_count = 0;
1948 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1950 return regulator;
1954 * regulator_get - lookup and obtain a reference to a regulator.
1955 * @dev: device for regulator "consumer"
1956 * @id: Supply name or regulator ID.
1958 * Returns a struct regulator corresponding to the regulator producer,
1959 * or IS_ERR() condition containing errno.
1961 * Use of supply names configured via regulator_set_device_supply() is
1962 * strongly encouraged. It is recommended that the supply name used
1963 * should match the name used for the supply and/or the relevant
1964 * device pins in the datasheet.
1966 struct regulator *regulator_get(struct device *dev, const char *id)
1968 return _regulator_get(dev, id, NORMAL_GET);
1970 EXPORT_SYMBOL_GPL(regulator_get);
1973 * regulator_get_exclusive - obtain exclusive access to a regulator.
1974 * @dev: device for regulator "consumer"
1975 * @id: Supply name or regulator ID.
1977 * Returns a struct regulator corresponding to the regulator producer,
1978 * or IS_ERR() condition containing errno. Other consumers will be
1979 * unable to obtain this regulator while this reference is held and the
1980 * use count for the regulator will be initialised to reflect the current
1981 * state of the regulator.
1983 * This is intended for use by consumers which cannot tolerate shared
1984 * use of the regulator such as those which need to force the
1985 * regulator off for correct operation of the hardware they are
1986 * controlling.
1988 * Use of supply names configured via regulator_set_device_supply() is
1989 * strongly encouraged. It is recommended that the supply name used
1990 * should match the name used for the supply and/or the relevant
1991 * device pins in the datasheet.
1993 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1995 return _regulator_get(dev, id, EXCLUSIVE_GET);
1997 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2000 * regulator_get_optional - obtain optional access to a regulator.
2001 * @dev: device for regulator "consumer"
2002 * @id: Supply name or regulator ID.
2004 * Returns a struct regulator corresponding to the regulator producer,
2005 * or IS_ERR() condition containing errno.
2007 * This is intended for use by consumers for devices which can have
2008 * some supplies unconnected in normal use, such as some MMC devices.
2009 * It can allow the regulator core to provide stub supplies for other
2010 * supplies requested using normal regulator_get() calls without
2011 * disrupting the operation of drivers that can handle absent
2012 * supplies.
2014 * Use of supply names configured via regulator_set_device_supply() is
2015 * strongly encouraged. It is recommended that the supply name used
2016 * should match the name used for the supply and/or the relevant
2017 * device pins in the datasheet.
2019 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2021 return _regulator_get(dev, id, OPTIONAL_GET);
2023 EXPORT_SYMBOL_GPL(regulator_get_optional);
2025 /* regulator_list_mutex lock held by regulator_put() */
2026 static void _regulator_put(struct regulator *regulator)
2028 struct regulator_dev *rdev;
2030 if (IS_ERR_OR_NULL(regulator))
2031 return;
2033 lockdep_assert_held_once(&regulator_list_mutex);
2035 /* Docs say you must disable before calling regulator_put() */
2036 WARN_ON(regulator->enable_count);
2038 rdev = regulator->rdev;
2040 debugfs_remove_recursive(regulator->debugfs);
2042 if (regulator->dev) {
2043 device_link_remove(regulator->dev, &rdev->dev);
2045 /* remove any sysfs entries */
2046 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2049 regulator_lock(rdev);
2050 list_del(&regulator->list);
2052 rdev->open_count--;
2053 rdev->exclusive = 0;
2054 put_device(&rdev->dev);
2055 regulator_unlock(rdev);
2057 kfree_const(regulator->supply_name);
2058 kfree(regulator);
2060 module_put(rdev->owner);
2064 * regulator_put - "free" the regulator source
2065 * @regulator: regulator source
2067 * Note: drivers must ensure that all regulator_enable calls made on this
2068 * regulator source are balanced by regulator_disable calls prior to calling
2069 * this function.
2071 void regulator_put(struct regulator *regulator)
2073 mutex_lock(&regulator_list_mutex);
2074 _regulator_put(regulator);
2075 mutex_unlock(&regulator_list_mutex);
2077 EXPORT_SYMBOL_GPL(regulator_put);
2080 * regulator_register_supply_alias - Provide device alias for supply lookup
2082 * @dev: device that will be given as the regulator "consumer"
2083 * @id: Supply name or regulator ID
2084 * @alias_dev: device that should be used to lookup the supply
2085 * @alias_id: Supply name or regulator ID that should be used to lookup the
2086 * supply
2088 * All lookups for id on dev will instead be conducted for alias_id on
2089 * alias_dev.
2091 int regulator_register_supply_alias(struct device *dev, const char *id,
2092 struct device *alias_dev,
2093 const char *alias_id)
2095 struct regulator_supply_alias *map;
2097 map = regulator_find_supply_alias(dev, id);
2098 if (map)
2099 return -EEXIST;
2101 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2102 if (!map)
2103 return -ENOMEM;
2105 map->src_dev = dev;
2106 map->src_supply = id;
2107 map->alias_dev = alias_dev;
2108 map->alias_supply = alias_id;
2110 list_add(&map->list, &regulator_supply_alias_list);
2112 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2113 id, dev_name(dev), alias_id, dev_name(alias_dev));
2115 return 0;
2117 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2120 * regulator_unregister_supply_alias - Remove device alias
2122 * @dev: device that will be given as the regulator "consumer"
2123 * @id: Supply name or regulator ID
2125 * Remove a lookup alias if one exists for id on dev.
2127 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2129 struct regulator_supply_alias *map;
2131 map = regulator_find_supply_alias(dev, id);
2132 if (map) {
2133 list_del(&map->list);
2134 kfree(map);
2137 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2140 * regulator_bulk_register_supply_alias - register multiple aliases
2142 * @dev: device that will be given as the regulator "consumer"
2143 * @id: List of supply names or regulator IDs
2144 * @alias_dev: device that should be used to lookup the supply
2145 * @alias_id: List of supply names or regulator IDs that should be used to
2146 * lookup the supply
2147 * @num_id: Number of aliases to register
2149 * @return 0 on success, an errno on failure.
2151 * This helper function allows drivers to register several supply
2152 * aliases in one operation. If any of the aliases cannot be
2153 * registered any aliases that were registered will be removed
2154 * before returning to the caller.
2156 int regulator_bulk_register_supply_alias(struct device *dev,
2157 const char *const *id,
2158 struct device *alias_dev,
2159 const char *const *alias_id,
2160 int num_id)
2162 int i;
2163 int ret;
2165 for (i = 0; i < num_id; ++i) {
2166 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2167 alias_id[i]);
2168 if (ret < 0)
2169 goto err;
2172 return 0;
2174 err:
2175 dev_err(dev,
2176 "Failed to create supply alias %s,%s -> %s,%s\n",
2177 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2179 while (--i >= 0)
2180 regulator_unregister_supply_alias(dev, id[i]);
2182 return ret;
2184 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2187 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2189 * @dev: device that will be given as the regulator "consumer"
2190 * @id: List of supply names or regulator IDs
2191 * @num_id: Number of aliases to unregister
2193 * This helper function allows drivers to unregister several supply
2194 * aliases in one operation.
2196 void regulator_bulk_unregister_supply_alias(struct device *dev,
2197 const char *const *id,
2198 int num_id)
2200 int i;
2202 for (i = 0; i < num_id; ++i)
2203 regulator_unregister_supply_alias(dev, id[i]);
2205 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2208 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2209 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2210 const struct regulator_config *config)
2212 struct regulator_enable_gpio *pin;
2213 struct gpio_desc *gpiod;
2215 gpiod = config->ena_gpiod;
2217 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2218 if (pin->gpiod == gpiod) {
2219 rdev_dbg(rdev, "GPIO is already used\n");
2220 goto update_ena_gpio_to_rdev;
2224 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2225 if (pin == NULL)
2226 return -ENOMEM;
2228 pin->gpiod = gpiod;
2229 list_add(&pin->list, &regulator_ena_gpio_list);
2231 update_ena_gpio_to_rdev:
2232 pin->request_count++;
2233 rdev->ena_pin = pin;
2234 return 0;
2237 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2239 struct regulator_enable_gpio *pin, *n;
2241 if (!rdev->ena_pin)
2242 return;
2244 /* Free the GPIO only in case of no use */
2245 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2246 if (pin->gpiod == rdev->ena_pin->gpiod) {
2247 if (pin->request_count <= 1) {
2248 pin->request_count = 0;
2249 gpiod_put(pin->gpiod);
2250 list_del(&pin->list);
2251 kfree(pin);
2252 rdev->ena_pin = NULL;
2253 return;
2254 } else {
2255 pin->request_count--;
2262 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2263 * @rdev: regulator_dev structure
2264 * @enable: enable GPIO at initial use?
2266 * GPIO is enabled in case of initial use. (enable_count is 0)
2267 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2269 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2271 struct regulator_enable_gpio *pin = rdev->ena_pin;
2273 if (!pin)
2274 return -EINVAL;
2276 if (enable) {
2277 /* Enable GPIO at initial use */
2278 if (pin->enable_count == 0)
2279 gpiod_set_value_cansleep(pin->gpiod, 1);
2281 pin->enable_count++;
2282 } else {
2283 if (pin->enable_count > 1) {
2284 pin->enable_count--;
2285 return 0;
2288 /* Disable GPIO if not used */
2289 if (pin->enable_count <= 1) {
2290 gpiod_set_value_cansleep(pin->gpiod, 0);
2291 pin->enable_count = 0;
2295 return 0;
2299 * _regulator_enable_delay - a delay helper function
2300 * @delay: time to delay in microseconds
2302 * Delay for the requested amount of time as per the guidelines in:
2304 * Documentation/timers/timers-howto.rst
2306 * The assumption here is that regulators will never be enabled in
2307 * atomic context and therefore sleeping functions can be used.
2309 static void _regulator_enable_delay(unsigned int delay)
2311 unsigned int ms = delay / 1000;
2312 unsigned int us = delay % 1000;
2314 if (ms > 0) {
2316 * For small enough values, handle super-millisecond
2317 * delays in the usleep_range() call below.
2319 if (ms < 20)
2320 us += ms * 1000;
2321 else
2322 msleep(ms);
2326 * Give the scheduler some room to coalesce with any other
2327 * wakeup sources. For delays shorter than 10 us, don't even
2328 * bother setting up high-resolution timers and just busy-
2329 * loop.
2331 if (us >= 10)
2332 usleep_range(us, us + 100);
2333 else
2334 udelay(us);
2337 static int _regulator_do_enable(struct regulator_dev *rdev)
2339 int ret, delay;
2341 /* Query before enabling in case configuration dependent. */
2342 ret = _regulator_get_enable_time(rdev);
2343 if (ret >= 0) {
2344 delay = ret;
2345 } else {
2346 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2347 delay = 0;
2350 trace_regulator_enable(rdev_get_name(rdev));
2352 if (rdev->desc->off_on_delay) {
2353 /* if needed, keep a distance of off_on_delay from last time
2354 * this regulator was disabled.
2356 unsigned long start_jiffy = jiffies;
2357 unsigned long intended, max_delay, remaining;
2359 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2360 intended = rdev->last_off_jiffy + max_delay;
2362 if (time_before(start_jiffy, intended)) {
2363 /* calc remaining jiffies to deal with one-time
2364 * timer wrapping.
2365 * in case of multiple timer wrapping, either it can be
2366 * detected by out-of-range remaining, or it cannot be
2367 * detected and we get a penalty of
2368 * _regulator_enable_delay().
2370 remaining = intended - start_jiffy;
2371 if (remaining <= max_delay)
2372 _regulator_enable_delay(
2373 jiffies_to_usecs(remaining));
2377 if (rdev->ena_pin) {
2378 if (!rdev->ena_gpio_state) {
2379 ret = regulator_ena_gpio_ctrl(rdev, true);
2380 if (ret < 0)
2381 return ret;
2382 rdev->ena_gpio_state = 1;
2384 } else if (rdev->desc->ops->enable) {
2385 ret = rdev->desc->ops->enable(rdev);
2386 if (ret < 0)
2387 return ret;
2388 } else {
2389 return -EINVAL;
2392 /* Allow the regulator to ramp; it would be useful to extend
2393 * this for bulk operations so that the regulators can ramp
2394 * together. */
2395 trace_regulator_enable_delay(rdev_get_name(rdev));
2397 _regulator_enable_delay(delay);
2399 trace_regulator_enable_complete(rdev_get_name(rdev));
2401 return 0;
2405 * _regulator_handle_consumer_enable - handle that a consumer enabled
2406 * @regulator: regulator source
2408 * Some things on a regulator consumer (like the contribution towards total
2409 * load on the regulator) only have an effect when the consumer wants the
2410 * regulator enabled. Explained in example with two consumers of the same
2411 * regulator:
2412 * consumer A: set_load(100); => total load = 0
2413 * consumer A: regulator_enable(); => total load = 100
2414 * consumer B: set_load(1000); => total load = 100
2415 * consumer B: regulator_enable(); => total load = 1100
2416 * consumer A: regulator_disable(); => total_load = 1000
2418 * This function (together with _regulator_handle_consumer_disable) is
2419 * responsible for keeping track of the refcount for a given regulator consumer
2420 * and applying / unapplying these things.
2422 * Returns 0 upon no error; -error upon error.
2424 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2426 struct regulator_dev *rdev = regulator->rdev;
2428 lockdep_assert_held_once(&rdev->mutex.base);
2430 regulator->enable_count++;
2431 if (regulator->uA_load && regulator->enable_count == 1)
2432 return drms_uA_update(rdev);
2434 return 0;
2438 * _regulator_handle_consumer_disable - handle that a consumer disabled
2439 * @regulator: regulator source
2441 * The opposite of _regulator_handle_consumer_enable().
2443 * Returns 0 upon no error; -error upon error.
2445 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2447 struct regulator_dev *rdev = regulator->rdev;
2449 lockdep_assert_held_once(&rdev->mutex.base);
2451 if (!regulator->enable_count) {
2452 rdev_err(rdev, "Underflow of regulator enable count\n");
2453 return -EINVAL;
2456 regulator->enable_count--;
2457 if (regulator->uA_load && regulator->enable_count == 0)
2458 return drms_uA_update(rdev);
2460 return 0;
2463 /* locks held by regulator_enable() */
2464 static int _regulator_enable(struct regulator *regulator)
2466 struct regulator_dev *rdev = regulator->rdev;
2467 int ret;
2469 lockdep_assert_held_once(&rdev->mutex.base);
2471 if (rdev->use_count == 0 && rdev->supply) {
2472 ret = _regulator_enable(rdev->supply);
2473 if (ret < 0)
2474 return ret;
2477 /* balance only if there are regulators coupled */
2478 if (rdev->coupling_desc.n_coupled > 1) {
2479 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2480 if (ret < 0)
2481 goto err_disable_supply;
2484 ret = _regulator_handle_consumer_enable(regulator);
2485 if (ret < 0)
2486 goto err_disable_supply;
2488 if (rdev->use_count == 0) {
2489 /* The regulator may on if it's not switchable or left on */
2490 ret = _regulator_is_enabled(rdev);
2491 if (ret == -EINVAL || ret == 0) {
2492 if (!regulator_ops_is_valid(rdev,
2493 REGULATOR_CHANGE_STATUS)) {
2494 ret = -EPERM;
2495 goto err_consumer_disable;
2498 ret = _regulator_do_enable(rdev);
2499 if (ret < 0)
2500 goto err_consumer_disable;
2502 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2503 NULL);
2504 } else if (ret < 0) {
2505 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2506 goto err_consumer_disable;
2508 /* Fallthrough on positive return values - already enabled */
2511 rdev->use_count++;
2513 return 0;
2515 err_consumer_disable:
2516 _regulator_handle_consumer_disable(regulator);
2518 err_disable_supply:
2519 if (rdev->use_count == 0 && rdev->supply)
2520 _regulator_disable(rdev->supply);
2522 return ret;
2526 * regulator_enable - enable regulator output
2527 * @regulator: regulator source
2529 * Request that the regulator be enabled with the regulator output at
2530 * the predefined voltage or current value. Calls to regulator_enable()
2531 * must be balanced with calls to regulator_disable().
2533 * NOTE: the output value can be set by other drivers, boot loader or may be
2534 * hardwired in the regulator.
2536 int regulator_enable(struct regulator *regulator)
2538 struct regulator_dev *rdev = regulator->rdev;
2539 struct ww_acquire_ctx ww_ctx;
2540 int ret;
2542 regulator_lock_dependent(rdev, &ww_ctx);
2543 ret = _regulator_enable(regulator);
2544 regulator_unlock_dependent(rdev, &ww_ctx);
2546 return ret;
2548 EXPORT_SYMBOL_GPL(regulator_enable);
2550 static int _regulator_do_disable(struct regulator_dev *rdev)
2552 int ret;
2554 trace_regulator_disable(rdev_get_name(rdev));
2556 if (rdev->ena_pin) {
2557 if (rdev->ena_gpio_state) {
2558 ret = regulator_ena_gpio_ctrl(rdev, false);
2559 if (ret < 0)
2560 return ret;
2561 rdev->ena_gpio_state = 0;
2564 } else if (rdev->desc->ops->disable) {
2565 ret = rdev->desc->ops->disable(rdev);
2566 if (ret != 0)
2567 return ret;
2570 /* cares about last_off_jiffy only if off_on_delay is required by
2571 * device.
2573 if (rdev->desc->off_on_delay)
2574 rdev->last_off_jiffy = jiffies;
2576 trace_regulator_disable_complete(rdev_get_name(rdev));
2578 return 0;
2581 /* locks held by regulator_disable() */
2582 static int _regulator_disable(struct regulator *regulator)
2584 struct regulator_dev *rdev = regulator->rdev;
2585 int ret = 0;
2587 lockdep_assert_held_once(&rdev->mutex.base);
2589 if (WARN(rdev->use_count <= 0,
2590 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2591 return -EIO;
2593 /* are we the last user and permitted to disable ? */
2594 if (rdev->use_count == 1 &&
2595 (rdev->constraints && !rdev->constraints->always_on)) {
2597 /* we are last user */
2598 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2599 ret = _notifier_call_chain(rdev,
2600 REGULATOR_EVENT_PRE_DISABLE,
2601 NULL);
2602 if (ret & NOTIFY_STOP_MASK)
2603 return -EINVAL;
2605 ret = _regulator_do_disable(rdev);
2606 if (ret < 0) {
2607 rdev_err(rdev, "failed to disable\n");
2608 _notifier_call_chain(rdev,
2609 REGULATOR_EVENT_ABORT_DISABLE,
2610 NULL);
2611 return ret;
2613 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2614 NULL);
2617 rdev->use_count = 0;
2618 } else if (rdev->use_count > 1) {
2619 rdev->use_count--;
2622 if (ret == 0)
2623 ret = _regulator_handle_consumer_disable(regulator);
2625 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2626 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2628 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2629 ret = _regulator_disable(rdev->supply);
2631 return ret;
2635 * regulator_disable - disable regulator output
2636 * @regulator: regulator source
2638 * Disable the regulator output voltage or current. Calls to
2639 * regulator_enable() must be balanced with calls to
2640 * regulator_disable().
2642 * NOTE: this will only disable the regulator output if no other consumer
2643 * devices have it enabled, the regulator device supports disabling and
2644 * machine constraints permit this operation.
2646 int regulator_disable(struct regulator *regulator)
2648 struct regulator_dev *rdev = regulator->rdev;
2649 struct ww_acquire_ctx ww_ctx;
2650 int ret;
2652 regulator_lock_dependent(rdev, &ww_ctx);
2653 ret = _regulator_disable(regulator);
2654 regulator_unlock_dependent(rdev, &ww_ctx);
2656 return ret;
2658 EXPORT_SYMBOL_GPL(regulator_disable);
2660 /* locks held by regulator_force_disable() */
2661 static int _regulator_force_disable(struct regulator_dev *rdev)
2663 int ret = 0;
2665 lockdep_assert_held_once(&rdev->mutex.base);
2667 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2668 REGULATOR_EVENT_PRE_DISABLE, NULL);
2669 if (ret & NOTIFY_STOP_MASK)
2670 return -EINVAL;
2672 ret = _regulator_do_disable(rdev);
2673 if (ret < 0) {
2674 rdev_err(rdev, "failed to force disable\n");
2675 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2676 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2677 return ret;
2680 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2681 REGULATOR_EVENT_DISABLE, NULL);
2683 return 0;
2687 * regulator_force_disable - force disable regulator output
2688 * @regulator: regulator source
2690 * Forcibly disable the regulator output voltage or current.
2691 * NOTE: this *will* disable the regulator output even if other consumer
2692 * devices have it enabled. This should be used for situations when device
2693 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2695 int regulator_force_disable(struct regulator *regulator)
2697 struct regulator_dev *rdev = regulator->rdev;
2698 struct ww_acquire_ctx ww_ctx;
2699 int ret;
2701 regulator_lock_dependent(rdev, &ww_ctx);
2703 ret = _regulator_force_disable(regulator->rdev);
2705 if (rdev->coupling_desc.n_coupled > 1)
2706 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2708 if (regulator->uA_load) {
2709 regulator->uA_load = 0;
2710 ret = drms_uA_update(rdev);
2713 if (rdev->use_count != 0 && rdev->supply)
2714 _regulator_disable(rdev->supply);
2716 regulator_unlock_dependent(rdev, &ww_ctx);
2718 return ret;
2720 EXPORT_SYMBOL_GPL(regulator_force_disable);
2722 static void regulator_disable_work(struct work_struct *work)
2724 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2725 disable_work.work);
2726 struct ww_acquire_ctx ww_ctx;
2727 int count, i, ret;
2728 struct regulator *regulator;
2729 int total_count = 0;
2731 regulator_lock_dependent(rdev, &ww_ctx);
2734 * Workqueue functions queue the new work instance while the previous
2735 * work instance is being processed. Cancel the queued work instance
2736 * as the work instance under processing does the job of the queued
2737 * work instance.
2739 cancel_delayed_work(&rdev->disable_work);
2741 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2742 count = regulator->deferred_disables;
2744 if (!count)
2745 continue;
2747 total_count += count;
2748 regulator->deferred_disables = 0;
2750 for (i = 0; i < count; i++) {
2751 ret = _regulator_disable(regulator);
2752 if (ret != 0)
2753 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2756 WARN_ON(!total_count);
2758 if (rdev->coupling_desc.n_coupled > 1)
2759 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2761 regulator_unlock_dependent(rdev, &ww_ctx);
2765 * regulator_disable_deferred - disable regulator output with delay
2766 * @regulator: regulator source
2767 * @ms: milliseconds until the regulator is disabled
2769 * Execute regulator_disable() on the regulator after a delay. This
2770 * is intended for use with devices that require some time to quiesce.
2772 * NOTE: this will only disable the regulator output if no other consumer
2773 * devices have it enabled, the regulator device supports disabling and
2774 * machine constraints permit this operation.
2776 int regulator_disable_deferred(struct regulator *regulator, int ms)
2778 struct regulator_dev *rdev = regulator->rdev;
2780 if (!ms)
2781 return regulator_disable(regulator);
2783 regulator_lock(rdev);
2784 regulator->deferred_disables++;
2785 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2786 msecs_to_jiffies(ms));
2787 regulator_unlock(rdev);
2789 return 0;
2791 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2793 static int _regulator_is_enabled(struct regulator_dev *rdev)
2795 /* A GPIO control always takes precedence */
2796 if (rdev->ena_pin)
2797 return rdev->ena_gpio_state;
2799 /* If we don't know then assume that the regulator is always on */
2800 if (!rdev->desc->ops->is_enabled)
2801 return 1;
2803 return rdev->desc->ops->is_enabled(rdev);
2806 static int _regulator_list_voltage(struct regulator_dev *rdev,
2807 unsigned selector, int lock)
2809 const struct regulator_ops *ops = rdev->desc->ops;
2810 int ret;
2812 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2813 return rdev->desc->fixed_uV;
2815 if (ops->list_voltage) {
2816 if (selector >= rdev->desc->n_voltages)
2817 return -EINVAL;
2818 if (lock)
2819 regulator_lock(rdev);
2820 ret = ops->list_voltage(rdev, selector);
2821 if (lock)
2822 regulator_unlock(rdev);
2823 } else if (rdev->is_switch && rdev->supply) {
2824 ret = _regulator_list_voltage(rdev->supply->rdev,
2825 selector, lock);
2826 } else {
2827 return -EINVAL;
2830 if (ret > 0) {
2831 if (ret < rdev->constraints->min_uV)
2832 ret = 0;
2833 else if (ret > rdev->constraints->max_uV)
2834 ret = 0;
2837 return ret;
2841 * regulator_is_enabled - is the regulator output enabled
2842 * @regulator: regulator source
2844 * Returns positive if the regulator driver backing the source/client
2845 * has requested that the device be enabled, zero if it hasn't, else a
2846 * negative errno code.
2848 * Note that the device backing this regulator handle can have multiple
2849 * users, so it might be enabled even if regulator_enable() was never
2850 * called for this particular source.
2852 int regulator_is_enabled(struct regulator *regulator)
2854 int ret;
2856 if (regulator->always_on)
2857 return 1;
2859 regulator_lock(regulator->rdev);
2860 ret = _regulator_is_enabled(regulator->rdev);
2861 regulator_unlock(regulator->rdev);
2863 return ret;
2865 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2868 * regulator_count_voltages - count regulator_list_voltage() selectors
2869 * @regulator: regulator source
2871 * Returns number of selectors, or negative errno. Selectors are
2872 * numbered starting at zero, and typically correspond to bitfields
2873 * in hardware registers.
2875 int regulator_count_voltages(struct regulator *regulator)
2877 struct regulator_dev *rdev = regulator->rdev;
2879 if (rdev->desc->n_voltages)
2880 return rdev->desc->n_voltages;
2882 if (!rdev->is_switch || !rdev->supply)
2883 return -EINVAL;
2885 return regulator_count_voltages(rdev->supply);
2887 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2890 * regulator_list_voltage - enumerate supported voltages
2891 * @regulator: regulator source
2892 * @selector: identify voltage to list
2893 * Context: can sleep
2895 * Returns a voltage that can be passed to @regulator_set_voltage(),
2896 * zero if this selector code can't be used on this system, or a
2897 * negative errno.
2899 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2901 return _regulator_list_voltage(regulator->rdev, selector, 1);
2903 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2906 * regulator_get_regmap - get the regulator's register map
2907 * @regulator: regulator source
2909 * Returns the register map for the given regulator, or an ERR_PTR value
2910 * if the regulator doesn't use regmap.
2912 struct regmap *regulator_get_regmap(struct regulator *regulator)
2914 struct regmap *map = regulator->rdev->regmap;
2916 return map ? map : ERR_PTR(-EOPNOTSUPP);
2920 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2921 * @regulator: regulator source
2922 * @vsel_reg: voltage selector register, output parameter
2923 * @vsel_mask: mask for voltage selector bitfield, output parameter
2925 * Returns the hardware register offset and bitmask used for setting the
2926 * regulator voltage. This might be useful when configuring voltage-scaling
2927 * hardware or firmware that can make I2C requests behind the kernel's back,
2928 * for example.
2930 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2931 * and 0 is returned, otherwise a negative errno is returned.
2933 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2934 unsigned *vsel_reg,
2935 unsigned *vsel_mask)
2937 struct regulator_dev *rdev = regulator->rdev;
2938 const struct regulator_ops *ops = rdev->desc->ops;
2940 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2941 return -EOPNOTSUPP;
2943 *vsel_reg = rdev->desc->vsel_reg;
2944 *vsel_mask = rdev->desc->vsel_mask;
2946 return 0;
2948 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2951 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2952 * @regulator: regulator source
2953 * @selector: identify voltage to list
2955 * Converts the selector to a hardware-specific voltage selector that can be
2956 * directly written to the regulator registers. The address of the voltage
2957 * register can be determined by calling @regulator_get_hardware_vsel_register.
2959 * On error a negative errno is returned.
2961 int regulator_list_hardware_vsel(struct regulator *regulator,
2962 unsigned selector)
2964 struct regulator_dev *rdev = regulator->rdev;
2965 const struct regulator_ops *ops = rdev->desc->ops;
2967 if (selector >= rdev->desc->n_voltages)
2968 return -EINVAL;
2969 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2970 return -EOPNOTSUPP;
2972 return selector;
2974 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2977 * regulator_get_linear_step - return the voltage step size between VSEL values
2978 * @regulator: regulator source
2980 * Returns the voltage step size between VSEL values for linear
2981 * regulators, or return 0 if the regulator isn't a linear regulator.
2983 unsigned int regulator_get_linear_step(struct regulator *regulator)
2985 struct regulator_dev *rdev = regulator->rdev;
2987 return rdev->desc->uV_step;
2989 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2992 * regulator_is_supported_voltage - check if a voltage range can be supported
2994 * @regulator: Regulator to check.
2995 * @min_uV: Minimum required voltage in uV.
2996 * @max_uV: Maximum required voltage in uV.
2998 * Returns a boolean.
3000 int regulator_is_supported_voltage(struct regulator *regulator,
3001 int min_uV, int max_uV)
3003 struct regulator_dev *rdev = regulator->rdev;
3004 int i, voltages, ret;
3006 /* If we can't change voltage check the current voltage */
3007 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3008 ret = regulator_get_voltage(regulator);
3009 if (ret >= 0)
3010 return min_uV <= ret && ret <= max_uV;
3011 else
3012 return ret;
3015 /* Any voltage within constrains range is fine? */
3016 if (rdev->desc->continuous_voltage_range)
3017 return min_uV >= rdev->constraints->min_uV &&
3018 max_uV <= rdev->constraints->max_uV;
3020 ret = regulator_count_voltages(regulator);
3021 if (ret < 0)
3022 return 0;
3023 voltages = ret;
3025 for (i = 0; i < voltages; i++) {
3026 ret = regulator_list_voltage(regulator, i);
3028 if (ret >= min_uV && ret <= max_uV)
3029 return 1;
3032 return 0;
3034 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3036 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3037 int max_uV)
3039 const struct regulator_desc *desc = rdev->desc;
3041 if (desc->ops->map_voltage)
3042 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3044 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3045 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3047 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3048 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3050 if (desc->ops->list_voltage ==
3051 regulator_list_voltage_pickable_linear_range)
3052 return regulator_map_voltage_pickable_linear_range(rdev,
3053 min_uV, max_uV);
3055 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3058 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3059 int min_uV, int max_uV,
3060 unsigned *selector)
3062 struct pre_voltage_change_data data;
3063 int ret;
3065 data.old_uV = regulator_get_voltage_rdev(rdev);
3066 data.min_uV = min_uV;
3067 data.max_uV = max_uV;
3068 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3069 &data);
3070 if (ret & NOTIFY_STOP_MASK)
3071 return -EINVAL;
3073 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3074 if (ret >= 0)
3075 return ret;
3077 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3078 (void *)data.old_uV);
3080 return ret;
3083 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3084 int uV, unsigned selector)
3086 struct pre_voltage_change_data data;
3087 int ret;
3089 data.old_uV = regulator_get_voltage_rdev(rdev);
3090 data.min_uV = uV;
3091 data.max_uV = uV;
3092 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3093 &data);
3094 if (ret & NOTIFY_STOP_MASK)
3095 return -EINVAL;
3097 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3098 if (ret >= 0)
3099 return ret;
3101 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3102 (void *)data.old_uV);
3104 return ret;
3107 static int _regulator_set_voltage_sel_step(struct regulator_dev *rdev,
3108 int uV, int new_selector)
3110 const struct regulator_ops *ops = rdev->desc->ops;
3111 int diff, old_sel, curr_sel, ret;
3113 /* Stepping is only needed if the regulator is enabled. */
3114 if (!_regulator_is_enabled(rdev))
3115 goto final_set;
3117 if (!ops->get_voltage_sel)
3118 return -EINVAL;
3120 old_sel = ops->get_voltage_sel(rdev);
3121 if (old_sel < 0)
3122 return old_sel;
3124 diff = new_selector - old_sel;
3125 if (diff == 0)
3126 return 0; /* No change needed. */
3128 if (diff > 0) {
3129 /* Stepping up. */
3130 for (curr_sel = old_sel + rdev->desc->vsel_step;
3131 curr_sel < new_selector;
3132 curr_sel += rdev->desc->vsel_step) {
3134 * Call the callback directly instead of using
3135 * _regulator_call_set_voltage_sel() as we don't
3136 * want to notify anyone yet. Same in the branch
3137 * below.
3139 ret = ops->set_voltage_sel(rdev, curr_sel);
3140 if (ret)
3141 goto try_revert;
3143 } else {
3144 /* Stepping down. */
3145 for (curr_sel = old_sel - rdev->desc->vsel_step;
3146 curr_sel > new_selector;
3147 curr_sel -= rdev->desc->vsel_step) {
3148 ret = ops->set_voltage_sel(rdev, curr_sel);
3149 if (ret)
3150 goto try_revert;
3154 final_set:
3155 /* The final selector will trigger the notifiers. */
3156 return _regulator_call_set_voltage_sel(rdev, uV, new_selector);
3158 try_revert:
3160 * At least try to return to the previous voltage if setting a new
3161 * one failed.
3163 (void)ops->set_voltage_sel(rdev, old_sel);
3164 return ret;
3167 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3168 int old_uV, int new_uV)
3170 unsigned int ramp_delay = 0;
3172 if (rdev->constraints->ramp_delay)
3173 ramp_delay = rdev->constraints->ramp_delay;
3174 else if (rdev->desc->ramp_delay)
3175 ramp_delay = rdev->desc->ramp_delay;
3176 else if (rdev->constraints->settling_time)
3177 return rdev->constraints->settling_time;
3178 else if (rdev->constraints->settling_time_up &&
3179 (new_uV > old_uV))
3180 return rdev->constraints->settling_time_up;
3181 else if (rdev->constraints->settling_time_down &&
3182 (new_uV < old_uV))
3183 return rdev->constraints->settling_time_down;
3185 if (ramp_delay == 0) {
3186 rdev_dbg(rdev, "ramp_delay not set\n");
3187 return 0;
3190 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3193 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3194 int min_uV, int max_uV)
3196 int ret;
3197 int delay = 0;
3198 int best_val = 0;
3199 unsigned int selector;
3200 int old_selector = -1;
3201 const struct regulator_ops *ops = rdev->desc->ops;
3202 int old_uV = regulator_get_voltage_rdev(rdev);
3204 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3206 min_uV += rdev->constraints->uV_offset;
3207 max_uV += rdev->constraints->uV_offset;
3210 * If we can't obtain the old selector there is not enough
3211 * info to call set_voltage_time_sel().
3213 if (_regulator_is_enabled(rdev) &&
3214 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3215 old_selector = ops->get_voltage_sel(rdev);
3216 if (old_selector < 0)
3217 return old_selector;
3220 if (ops->set_voltage) {
3221 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3222 &selector);
3224 if (ret >= 0) {
3225 if (ops->list_voltage)
3226 best_val = ops->list_voltage(rdev,
3227 selector);
3228 else
3229 best_val = regulator_get_voltage_rdev(rdev);
3232 } else if (ops->set_voltage_sel) {
3233 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3234 if (ret >= 0) {
3235 best_val = ops->list_voltage(rdev, ret);
3236 if (min_uV <= best_val && max_uV >= best_val) {
3237 selector = ret;
3238 if (old_selector == selector)
3239 ret = 0;
3240 else if (rdev->desc->vsel_step)
3241 ret = _regulator_set_voltage_sel_step(
3242 rdev, best_val, selector);
3243 else
3244 ret = _regulator_call_set_voltage_sel(
3245 rdev, best_val, selector);
3246 } else {
3247 ret = -EINVAL;
3250 } else {
3251 ret = -EINVAL;
3254 if (ret)
3255 goto out;
3257 if (ops->set_voltage_time_sel) {
3259 * Call set_voltage_time_sel if successfully obtained
3260 * old_selector
3262 if (old_selector >= 0 && old_selector != selector)
3263 delay = ops->set_voltage_time_sel(rdev, old_selector,
3264 selector);
3265 } else {
3266 if (old_uV != best_val) {
3267 if (ops->set_voltage_time)
3268 delay = ops->set_voltage_time(rdev, old_uV,
3269 best_val);
3270 else
3271 delay = _regulator_set_voltage_time(rdev,
3272 old_uV,
3273 best_val);
3277 if (delay < 0) {
3278 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3279 delay = 0;
3282 /* Insert any necessary delays */
3283 if (delay >= 1000) {
3284 mdelay(delay / 1000);
3285 udelay(delay % 1000);
3286 } else if (delay) {
3287 udelay(delay);
3290 if (best_val >= 0) {
3291 unsigned long data = best_val;
3293 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3294 (void *)data);
3297 out:
3298 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3300 return ret;
3303 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3304 int min_uV, int max_uV, suspend_state_t state)
3306 struct regulator_state *rstate;
3307 int uV, sel;
3309 rstate = regulator_get_suspend_state(rdev, state);
3310 if (rstate == NULL)
3311 return -EINVAL;
3313 if (min_uV < rstate->min_uV)
3314 min_uV = rstate->min_uV;
3315 if (max_uV > rstate->max_uV)
3316 max_uV = rstate->max_uV;
3318 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3319 if (sel < 0)
3320 return sel;
3322 uV = rdev->desc->ops->list_voltage(rdev, sel);
3323 if (uV >= min_uV && uV <= max_uV)
3324 rstate->uV = uV;
3326 return 0;
3329 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3330 int min_uV, int max_uV,
3331 suspend_state_t state)
3333 struct regulator_dev *rdev = regulator->rdev;
3334 struct regulator_voltage *voltage = &regulator->voltage[state];
3335 int ret = 0;
3336 int old_min_uV, old_max_uV;
3337 int current_uV;
3339 /* If we're setting the same range as last time the change
3340 * should be a noop (some cpufreq implementations use the same
3341 * voltage for multiple frequencies, for example).
3343 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3344 goto out;
3346 /* If we're trying to set a range that overlaps the current voltage,
3347 * return successfully even though the regulator does not support
3348 * changing the voltage.
3350 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3351 current_uV = regulator_get_voltage_rdev(rdev);
3352 if (min_uV <= current_uV && current_uV <= max_uV) {
3353 voltage->min_uV = min_uV;
3354 voltage->max_uV = max_uV;
3355 goto out;
3359 /* sanity check */
3360 if (!rdev->desc->ops->set_voltage &&
3361 !rdev->desc->ops->set_voltage_sel) {
3362 ret = -EINVAL;
3363 goto out;
3366 /* constraints check */
3367 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3368 if (ret < 0)
3369 goto out;
3371 /* restore original values in case of error */
3372 old_min_uV = voltage->min_uV;
3373 old_max_uV = voltage->max_uV;
3374 voltage->min_uV = min_uV;
3375 voltage->max_uV = max_uV;
3377 /* for not coupled regulators this will just set the voltage */
3378 ret = regulator_balance_voltage(rdev, state);
3379 if (ret < 0) {
3380 voltage->min_uV = old_min_uV;
3381 voltage->max_uV = old_max_uV;
3384 out:
3385 return ret;
3388 int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3389 int max_uV, suspend_state_t state)
3391 int best_supply_uV = 0;
3392 int supply_change_uV = 0;
3393 int ret;
3395 if (rdev->supply &&
3396 regulator_ops_is_valid(rdev->supply->rdev,
3397 REGULATOR_CHANGE_VOLTAGE) &&
3398 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3399 rdev->desc->ops->get_voltage_sel))) {
3400 int current_supply_uV;
3401 int selector;
3403 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3404 if (selector < 0) {
3405 ret = selector;
3406 goto out;
3409 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3410 if (best_supply_uV < 0) {
3411 ret = best_supply_uV;
3412 goto out;
3415 best_supply_uV += rdev->desc->min_dropout_uV;
3417 current_supply_uV = regulator_get_voltage_rdev(rdev->supply->rdev);
3418 if (current_supply_uV < 0) {
3419 ret = current_supply_uV;
3420 goto out;
3423 supply_change_uV = best_supply_uV - current_supply_uV;
3426 if (supply_change_uV > 0) {
3427 ret = regulator_set_voltage_unlocked(rdev->supply,
3428 best_supply_uV, INT_MAX, state);
3429 if (ret) {
3430 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3431 ret);
3432 goto out;
3436 if (state == PM_SUSPEND_ON)
3437 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3438 else
3439 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3440 max_uV, state);
3441 if (ret < 0)
3442 goto out;
3444 if (supply_change_uV < 0) {
3445 ret = regulator_set_voltage_unlocked(rdev->supply,
3446 best_supply_uV, INT_MAX, state);
3447 if (ret)
3448 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3449 ret);
3450 /* No need to fail here */
3451 ret = 0;
3454 out:
3455 return ret;
3458 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3459 int *current_uV, int *min_uV)
3461 struct regulation_constraints *constraints = rdev->constraints;
3463 /* Limit voltage change only if necessary */
3464 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3465 return 1;
3467 if (*current_uV < 0) {
3468 *current_uV = regulator_get_voltage_rdev(rdev);
3470 if (*current_uV < 0)
3471 return *current_uV;
3474 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3475 return 1;
3477 /* Clamp target voltage within the given step */
3478 if (*current_uV < *min_uV)
3479 *min_uV = min(*current_uV + constraints->max_uV_step,
3480 *min_uV);
3481 else
3482 *min_uV = max(*current_uV - constraints->max_uV_step,
3483 *min_uV);
3485 return 0;
3488 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3489 int *current_uV,
3490 int *min_uV, int *max_uV,
3491 suspend_state_t state,
3492 int n_coupled)
3494 struct coupling_desc *c_desc = &rdev->coupling_desc;
3495 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3496 struct regulation_constraints *constraints = rdev->constraints;
3497 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3498 int max_current_uV = 0, min_current_uV = INT_MAX;
3499 int highest_min_uV = 0, target_uV, possible_uV;
3500 int i, ret, max_spread;
3501 bool done;
3503 *current_uV = -1;
3506 * If there are no coupled regulators, simply set the voltage
3507 * demanded by consumers.
3509 if (n_coupled == 1) {
3511 * If consumers don't provide any demands, set voltage
3512 * to min_uV
3514 desired_min_uV = constraints->min_uV;
3515 desired_max_uV = constraints->max_uV;
3517 ret = regulator_check_consumers(rdev,
3518 &desired_min_uV,
3519 &desired_max_uV, state);
3520 if (ret < 0)
3521 return ret;
3523 possible_uV = desired_min_uV;
3524 done = true;
3526 goto finish;
3529 /* Find highest min desired voltage */
3530 for (i = 0; i < n_coupled; i++) {
3531 int tmp_min = 0;
3532 int tmp_max = INT_MAX;
3534 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3536 ret = regulator_check_consumers(c_rdevs[i],
3537 &tmp_min,
3538 &tmp_max, state);
3539 if (ret < 0)
3540 return ret;
3542 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3543 if (ret < 0)
3544 return ret;
3546 highest_min_uV = max(highest_min_uV, tmp_min);
3548 if (i == 0) {
3549 desired_min_uV = tmp_min;
3550 desired_max_uV = tmp_max;
3554 max_spread = constraints->max_spread[0];
3557 * Let target_uV be equal to the desired one if possible.
3558 * If not, set it to minimum voltage, allowed by other coupled
3559 * regulators.
3561 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3564 * Find min and max voltages, which currently aren't violating
3565 * max_spread.
3567 for (i = 1; i < n_coupled; i++) {
3568 int tmp_act;
3570 if (!_regulator_is_enabled(c_rdevs[i]))
3571 continue;
3573 tmp_act = regulator_get_voltage_rdev(c_rdevs[i]);
3574 if (tmp_act < 0)
3575 return tmp_act;
3577 min_current_uV = min(tmp_act, min_current_uV);
3578 max_current_uV = max(tmp_act, max_current_uV);
3581 /* There aren't any other regulators enabled */
3582 if (max_current_uV == 0) {
3583 possible_uV = target_uV;
3584 } else {
3586 * Correct target voltage, so as it currently isn't
3587 * violating max_spread
3589 possible_uV = max(target_uV, max_current_uV - max_spread);
3590 possible_uV = min(possible_uV, min_current_uV + max_spread);
3593 if (possible_uV > desired_max_uV)
3594 return -EINVAL;
3596 done = (possible_uV == target_uV);
3597 desired_min_uV = possible_uV;
3599 finish:
3600 /* Apply max_uV_step constraint if necessary */
3601 if (state == PM_SUSPEND_ON) {
3602 ret = regulator_limit_voltage_step(rdev, current_uV,
3603 &desired_min_uV);
3604 if (ret < 0)
3605 return ret;
3607 if (ret == 0)
3608 done = false;
3611 /* Set current_uV if wasn't done earlier in the code and if necessary */
3612 if (n_coupled > 1 && *current_uV == -1) {
3614 if (_regulator_is_enabled(rdev)) {
3615 ret = regulator_get_voltage_rdev(rdev);
3616 if (ret < 0)
3617 return ret;
3619 *current_uV = ret;
3620 } else {
3621 *current_uV = desired_min_uV;
3625 *min_uV = desired_min_uV;
3626 *max_uV = desired_max_uV;
3628 return done;
3631 static int regulator_balance_voltage(struct regulator_dev *rdev,
3632 suspend_state_t state)
3634 struct regulator_dev **c_rdevs;
3635 struct regulator_dev *best_rdev;
3636 struct coupling_desc *c_desc = &rdev->coupling_desc;
3637 struct regulator_coupler *coupler = c_desc->coupler;
3638 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3639 unsigned int delta, best_delta;
3640 unsigned long c_rdev_done = 0;
3641 bool best_c_rdev_done;
3643 c_rdevs = c_desc->coupled_rdevs;
3644 n_coupled = c_desc->n_coupled;
3647 * If system is in a state other than PM_SUSPEND_ON, don't check
3648 * other coupled regulators.
3650 if (state != PM_SUSPEND_ON)
3651 n_coupled = 1;
3653 if (c_desc->n_resolved < n_coupled) {
3654 rdev_err(rdev, "Not all coupled regulators registered\n");
3655 return -EPERM;
3658 /* Invoke custom balancer for customized couplers */
3659 if (coupler && coupler->balance_voltage)
3660 return coupler->balance_voltage(coupler, rdev, state);
3663 * Find the best possible voltage change on each loop. Leave the loop
3664 * if there isn't any possible change.
3666 do {
3667 best_c_rdev_done = false;
3668 best_delta = 0;
3669 best_min_uV = 0;
3670 best_max_uV = 0;
3671 best_c_rdev = 0;
3672 best_rdev = NULL;
3675 * Find highest difference between optimal voltage
3676 * and current voltage.
3678 for (i = 0; i < n_coupled; i++) {
3680 * optimal_uV is the best voltage that can be set for
3681 * i-th regulator at the moment without violating
3682 * max_spread constraint in order to balance
3683 * the coupled voltages.
3685 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3687 if (test_bit(i, &c_rdev_done))
3688 continue;
3690 ret = regulator_get_optimal_voltage(c_rdevs[i],
3691 &current_uV,
3692 &optimal_uV,
3693 &optimal_max_uV,
3694 state, n_coupled);
3695 if (ret < 0)
3696 goto out;
3698 delta = abs(optimal_uV - current_uV);
3700 if (delta && best_delta <= delta) {
3701 best_c_rdev_done = ret;
3702 best_delta = delta;
3703 best_rdev = c_rdevs[i];
3704 best_min_uV = optimal_uV;
3705 best_max_uV = optimal_max_uV;
3706 best_c_rdev = i;
3710 /* Nothing to change, return successfully */
3711 if (!best_rdev) {
3712 ret = 0;
3713 goto out;
3716 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3717 best_max_uV, state);
3719 if (ret < 0)
3720 goto out;
3722 if (best_c_rdev_done)
3723 set_bit(best_c_rdev, &c_rdev_done);
3725 } while (n_coupled > 1);
3727 out:
3728 return ret;
3732 * regulator_set_voltage - set regulator output voltage
3733 * @regulator: regulator source
3734 * @min_uV: Minimum required voltage in uV
3735 * @max_uV: Maximum acceptable voltage in uV
3737 * Sets a voltage regulator to the desired output voltage. This can be set
3738 * during any regulator state. IOW, regulator can be disabled or enabled.
3740 * If the regulator is enabled then the voltage will change to the new value
3741 * immediately otherwise if the regulator is disabled the regulator will
3742 * output at the new voltage when enabled.
3744 * NOTE: If the regulator is shared between several devices then the lowest
3745 * request voltage that meets the system constraints will be used.
3746 * Regulator system constraints must be set for this regulator before
3747 * calling this function otherwise this call will fail.
3749 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3751 struct ww_acquire_ctx ww_ctx;
3752 int ret;
3754 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3756 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3757 PM_SUSPEND_ON);
3759 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3761 return ret;
3763 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3765 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3766 suspend_state_t state, bool en)
3768 struct regulator_state *rstate;
3770 rstate = regulator_get_suspend_state(rdev, state);
3771 if (rstate == NULL)
3772 return -EINVAL;
3774 if (!rstate->changeable)
3775 return -EPERM;
3777 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3779 return 0;
3782 int regulator_suspend_enable(struct regulator_dev *rdev,
3783 suspend_state_t state)
3785 return regulator_suspend_toggle(rdev, state, true);
3787 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3789 int regulator_suspend_disable(struct regulator_dev *rdev,
3790 suspend_state_t state)
3792 struct regulator *regulator;
3793 struct regulator_voltage *voltage;
3796 * if any consumer wants this regulator device keeping on in
3797 * suspend states, don't set it as disabled.
3799 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3800 voltage = &regulator->voltage[state];
3801 if (voltage->min_uV || voltage->max_uV)
3802 return 0;
3805 return regulator_suspend_toggle(rdev, state, false);
3807 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3809 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3810 int min_uV, int max_uV,
3811 suspend_state_t state)
3813 struct regulator_dev *rdev = regulator->rdev;
3814 struct regulator_state *rstate;
3816 rstate = regulator_get_suspend_state(rdev, state);
3817 if (rstate == NULL)
3818 return -EINVAL;
3820 if (rstate->min_uV == rstate->max_uV) {
3821 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3822 return -EPERM;
3825 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3828 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3829 int max_uV, suspend_state_t state)
3831 struct ww_acquire_ctx ww_ctx;
3832 int ret;
3834 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3835 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3836 return -EINVAL;
3838 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3840 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3841 max_uV, state);
3843 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3845 return ret;
3847 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3850 * regulator_set_voltage_time - get raise/fall time
3851 * @regulator: regulator source
3852 * @old_uV: starting voltage in microvolts
3853 * @new_uV: target voltage in microvolts
3855 * Provided with the starting and ending voltage, this function attempts to
3856 * calculate the time in microseconds required to rise or fall to this new
3857 * voltage.
3859 int regulator_set_voltage_time(struct regulator *regulator,
3860 int old_uV, int new_uV)
3862 struct regulator_dev *rdev = regulator->rdev;
3863 const struct regulator_ops *ops = rdev->desc->ops;
3864 int old_sel = -1;
3865 int new_sel = -1;
3866 int voltage;
3867 int i;
3869 if (ops->set_voltage_time)
3870 return ops->set_voltage_time(rdev, old_uV, new_uV);
3871 else if (!ops->set_voltage_time_sel)
3872 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3874 /* Currently requires operations to do this */
3875 if (!ops->list_voltage || !rdev->desc->n_voltages)
3876 return -EINVAL;
3878 for (i = 0; i < rdev->desc->n_voltages; i++) {
3879 /* We only look for exact voltage matches here */
3880 voltage = regulator_list_voltage(regulator, i);
3881 if (voltage < 0)
3882 return -EINVAL;
3883 if (voltage == 0)
3884 continue;
3885 if (voltage == old_uV)
3886 old_sel = i;
3887 if (voltage == new_uV)
3888 new_sel = i;
3891 if (old_sel < 0 || new_sel < 0)
3892 return -EINVAL;
3894 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3896 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3899 * regulator_set_voltage_time_sel - get raise/fall time
3900 * @rdev: regulator source device
3901 * @old_selector: selector for starting voltage
3902 * @new_selector: selector for target voltage
3904 * Provided with the starting and target voltage selectors, this function
3905 * returns time in microseconds required to rise or fall to this new voltage
3907 * Drivers providing ramp_delay in regulation_constraints can use this as their
3908 * set_voltage_time_sel() operation.
3910 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3911 unsigned int old_selector,
3912 unsigned int new_selector)
3914 int old_volt, new_volt;
3916 /* sanity check */
3917 if (!rdev->desc->ops->list_voltage)
3918 return -EINVAL;
3920 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3921 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3923 if (rdev->desc->ops->set_voltage_time)
3924 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3925 new_volt);
3926 else
3927 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3929 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3932 * regulator_sync_voltage - re-apply last regulator output voltage
3933 * @regulator: regulator source
3935 * Re-apply the last configured voltage. This is intended to be used
3936 * where some external control source the consumer is cooperating with
3937 * has caused the configured voltage to change.
3939 int regulator_sync_voltage(struct regulator *regulator)
3941 struct regulator_dev *rdev = regulator->rdev;
3942 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3943 int ret, min_uV, max_uV;
3945 regulator_lock(rdev);
3947 if (!rdev->desc->ops->set_voltage &&
3948 !rdev->desc->ops->set_voltage_sel) {
3949 ret = -EINVAL;
3950 goto out;
3953 /* This is only going to work if we've had a voltage configured. */
3954 if (!voltage->min_uV && !voltage->max_uV) {
3955 ret = -EINVAL;
3956 goto out;
3959 min_uV = voltage->min_uV;
3960 max_uV = voltage->max_uV;
3962 /* This should be a paranoia check... */
3963 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3964 if (ret < 0)
3965 goto out;
3967 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3968 if (ret < 0)
3969 goto out;
3971 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3973 out:
3974 regulator_unlock(rdev);
3975 return ret;
3977 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3979 int regulator_get_voltage_rdev(struct regulator_dev *rdev)
3981 int sel, ret;
3982 bool bypassed;
3984 if (rdev->desc->ops->get_bypass) {
3985 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3986 if (ret < 0)
3987 return ret;
3988 if (bypassed) {
3989 /* if bypassed the regulator must have a supply */
3990 if (!rdev->supply) {
3991 rdev_err(rdev,
3992 "bypassed regulator has no supply!\n");
3993 return -EPROBE_DEFER;
3996 return regulator_get_voltage_rdev(rdev->supply->rdev);
4000 if (rdev->desc->ops->get_voltage_sel) {
4001 sel = rdev->desc->ops->get_voltage_sel(rdev);
4002 if (sel < 0)
4003 return sel;
4004 ret = rdev->desc->ops->list_voltage(rdev, sel);
4005 } else if (rdev->desc->ops->get_voltage) {
4006 ret = rdev->desc->ops->get_voltage(rdev);
4007 } else if (rdev->desc->ops->list_voltage) {
4008 ret = rdev->desc->ops->list_voltage(rdev, 0);
4009 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
4010 ret = rdev->desc->fixed_uV;
4011 } else if (rdev->supply) {
4012 ret = regulator_get_voltage_rdev(rdev->supply->rdev);
4013 } else {
4014 return -EINVAL;
4017 if (ret < 0)
4018 return ret;
4019 return ret - rdev->constraints->uV_offset;
4023 * regulator_get_voltage - get regulator output voltage
4024 * @regulator: regulator source
4026 * This returns the current regulator voltage in uV.
4028 * NOTE: If the regulator is disabled it will return the voltage value. This
4029 * function should not be used to determine regulator state.
4031 int regulator_get_voltage(struct regulator *regulator)
4033 struct ww_acquire_ctx ww_ctx;
4034 int ret;
4036 regulator_lock_dependent(regulator->rdev, &ww_ctx);
4037 ret = regulator_get_voltage_rdev(regulator->rdev);
4038 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
4040 return ret;
4042 EXPORT_SYMBOL_GPL(regulator_get_voltage);
4045 * regulator_set_current_limit - set regulator output current limit
4046 * @regulator: regulator source
4047 * @min_uA: Minimum supported current in uA
4048 * @max_uA: Maximum supported current in uA
4050 * Sets current sink to the desired output current. This can be set during
4051 * any regulator state. IOW, regulator can be disabled or enabled.
4053 * If the regulator is enabled then the current will change to the new value
4054 * immediately otherwise if the regulator is disabled the regulator will
4055 * output at the new current when enabled.
4057 * NOTE: Regulator system constraints must be set for this regulator before
4058 * calling this function otherwise this call will fail.
4060 int regulator_set_current_limit(struct regulator *regulator,
4061 int min_uA, int max_uA)
4063 struct regulator_dev *rdev = regulator->rdev;
4064 int ret;
4066 regulator_lock(rdev);
4068 /* sanity check */
4069 if (!rdev->desc->ops->set_current_limit) {
4070 ret = -EINVAL;
4071 goto out;
4074 /* constraints check */
4075 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4076 if (ret < 0)
4077 goto out;
4079 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4080 out:
4081 regulator_unlock(rdev);
4082 return ret;
4084 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4086 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4088 /* sanity check */
4089 if (!rdev->desc->ops->get_current_limit)
4090 return -EINVAL;
4092 return rdev->desc->ops->get_current_limit(rdev);
4095 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4097 int ret;
4099 regulator_lock(rdev);
4100 ret = _regulator_get_current_limit_unlocked(rdev);
4101 regulator_unlock(rdev);
4103 return ret;
4107 * regulator_get_current_limit - get regulator output current
4108 * @regulator: regulator source
4110 * This returns the current supplied by the specified current sink in uA.
4112 * NOTE: If the regulator is disabled it will return the current value. This
4113 * function should not be used to determine regulator state.
4115 int regulator_get_current_limit(struct regulator *regulator)
4117 return _regulator_get_current_limit(regulator->rdev);
4119 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4122 * regulator_set_mode - set regulator operating mode
4123 * @regulator: regulator source
4124 * @mode: operating mode - one of the REGULATOR_MODE constants
4126 * Set regulator operating mode to increase regulator efficiency or improve
4127 * regulation performance.
4129 * NOTE: Regulator system constraints must be set for this regulator before
4130 * calling this function otherwise this call will fail.
4132 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4134 struct regulator_dev *rdev = regulator->rdev;
4135 int ret;
4136 int regulator_curr_mode;
4138 regulator_lock(rdev);
4140 /* sanity check */
4141 if (!rdev->desc->ops->set_mode) {
4142 ret = -EINVAL;
4143 goto out;
4146 /* return if the same mode is requested */
4147 if (rdev->desc->ops->get_mode) {
4148 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4149 if (regulator_curr_mode == mode) {
4150 ret = 0;
4151 goto out;
4155 /* constraints check */
4156 ret = regulator_mode_constrain(rdev, &mode);
4157 if (ret < 0)
4158 goto out;
4160 ret = rdev->desc->ops->set_mode(rdev, mode);
4161 out:
4162 regulator_unlock(rdev);
4163 return ret;
4165 EXPORT_SYMBOL_GPL(regulator_set_mode);
4167 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4169 /* sanity check */
4170 if (!rdev->desc->ops->get_mode)
4171 return -EINVAL;
4173 return rdev->desc->ops->get_mode(rdev);
4176 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4178 int ret;
4180 regulator_lock(rdev);
4181 ret = _regulator_get_mode_unlocked(rdev);
4182 regulator_unlock(rdev);
4184 return ret;
4188 * regulator_get_mode - get regulator operating mode
4189 * @regulator: regulator source
4191 * Get the current regulator operating mode.
4193 unsigned int regulator_get_mode(struct regulator *regulator)
4195 return _regulator_get_mode(regulator->rdev);
4197 EXPORT_SYMBOL_GPL(regulator_get_mode);
4199 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4200 unsigned int *flags)
4202 int ret;
4204 regulator_lock(rdev);
4206 /* sanity check */
4207 if (!rdev->desc->ops->get_error_flags) {
4208 ret = -EINVAL;
4209 goto out;
4212 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4213 out:
4214 regulator_unlock(rdev);
4215 return ret;
4219 * regulator_get_error_flags - get regulator error information
4220 * @regulator: regulator source
4221 * @flags: pointer to store error flags
4223 * Get the current regulator error information.
4225 int regulator_get_error_flags(struct regulator *regulator,
4226 unsigned int *flags)
4228 return _regulator_get_error_flags(regulator->rdev, flags);
4230 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4233 * regulator_set_load - set regulator load
4234 * @regulator: regulator source
4235 * @uA_load: load current
4237 * Notifies the regulator core of a new device load. This is then used by
4238 * DRMS (if enabled by constraints) to set the most efficient regulator
4239 * operating mode for the new regulator loading.
4241 * Consumer devices notify their supply regulator of the maximum power
4242 * they will require (can be taken from device datasheet in the power
4243 * consumption tables) when they change operational status and hence power
4244 * state. Examples of operational state changes that can affect power
4245 * consumption are :-
4247 * o Device is opened / closed.
4248 * o Device I/O is about to begin or has just finished.
4249 * o Device is idling in between work.
4251 * This information is also exported via sysfs to userspace.
4253 * DRMS will sum the total requested load on the regulator and change
4254 * to the most efficient operating mode if platform constraints allow.
4256 * NOTE: when a regulator consumer requests to have a regulator
4257 * disabled then any load that consumer requested no longer counts
4258 * toward the total requested load. If the regulator is re-enabled
4259 * then the previously requested load will start counting again.
4261 * If a regulator is an always-on regulator then an individual consumer's
4262 * load will still be removed if that consumer is fully disabled.
4264 * On error a negative errno is returned.
4266 int regulator_set_load(struct regulator *regulator, int uA_load)
4268 struct regulator_dev *rdev = regulator->rdev;
4269 int old_uA_load;
4270 int ret = 0;
4272 regulator_lock(rdev);
4273 old_uA_load = regulator->uA_load;
4274 regulator->uA_load = uA_load;
4275 if (regulator->enable_count && old_uA_load != uA_load) {
4276 ret = drms_uA_update(rdev);
4277 if (ret < 0)
4278 regulator->uA_load = old_uA_load;
4280 regulator_unlock(rdev);
4282 return ret;
4284 EXPORT_SYMBOL_GPL(regulator_set_load);
4287 * regulator_allow_bypass - allow the regulator to go into bypass mode
4289 * @regulator: Regulator to configure
4290 * @enable: enable or disable bypass mode
4292 * Allow the regulator to go into bypass mode if all other consumers
4293 * for the regulator also enable bypass mode and the machine
4294 * constraints allow this. Bypass mode means that the regulator is
4295 * simply passing the input directly to the output with no regulation.
4297 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4299 struct regulator_dev *rdev = regulator->rdev;
4300 int ret = 0;
4302 if (!rdev->desc->ops->set_bypass)
4303 return 0;
4305 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4306 return 0;
4308 regulator_lock(rdev);
4310 if (enable && !regulator->bypass) {
4311 rdev->bypass_count++;
4313 if (rdev->bypass_count == rdev->open_count) {
4314 ret = rdev->desc->ops->set_bypass(rdev, enable);
4315 if (ret != 0)
4316 rdev->bypass_count--;
4319 } else if (!enable && regulator->bypass) {
4320 rdev->bypass_count--;
4322 if (rdev->bypass_count != rdev->open_count) {
4323 ret = rdev->desc->ops->set_bypass(rdev, enable);
4324 if (ret != 0)
4325 rdev->bypass_count++;
4329 if (ret == 0)
4330 regulator->bypass = enable;
4332 regulator_unlock(rdev);
4334 return ret;
4336 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4339 * regulator_register_notifier - register regulator event notifier
4340 * @regulator: regulator source
4341 * @nb: notifier block
4343 * Register notifier block to receive regulator events.
4345 int regulator_register_notifier(struct regulator *regulator,
4346 struct notifier_block *nb)
4348 return blocking_notifier_chain_register(&regulator->rdev->notifier,
4349 nb);
4351 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4354 * regulator_unregister_notifier - unregister regulator event notifier
4355 * @regulator: regulator source
4356 * @nb: notifier block
4358 * Unregister regulator event notifier block.
4360 int regulator_unregister_notifier(struct regulator *regulator,
4361 struct notifier_block *nb)
4363 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4364 nb);
4366 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4368 /* notify regulator consumers and downstream regulator consumers.
4369 * Note mutex must be held by caller.
4371 static int _notifier_call_chain(struct regulator_dev *rdev,
4372 unsigned long event, void *data)
4374 /* call rdev chain first */
4375 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4379 * regulator_bulk_get - get multiple regulator consumers
4381 * @dev: Device to supply
4382 * @num_consumers: Number of consumers to register
4383 * @consumers: Configuration of consumers; clients are stored here.
4385 * @return 0 on success, an errno on failure.
4387 * This helper function allows drivers to get several regulator
4388 * consumers in one operation. If any of the regulators cannot be
4389 * acquired then any regulators that were allocated will be freed
4390 * before returning to the caller.
4392 int regulator_bulk_get(struct device *dev, int num_consumers,
4393 struct regulator_bulk_data *consumers)
4395 int i;
4396 int ret;
4398 for (i = 0; i < num_consumers; i++)
4399 consumers[i].consumer = NULL;
4401 for (i = 0; i < num_consumers; i++) {
4402 consumers[i].consumer = regulator_get(dev,
4403 consumers[i].supply);
4404 if (IS_ERR(consumers[i].consumer)) {
4405 ret = PTR_ERR(consumers[i].consumer);
4406 consumers[i].consumer = NULL;
4407 goto err;
4411 return 0;
4413 err:
4414 if (ret != -EPROBE_DEFER)
4415 dev_err(dev, "Failed to get supply '%s': %d\n",
4416 consumers[i].supply, ret);
4417 else
4418 dev_dbg(dev, "Failed to get supply '%s', deferring\n",
4419 consumers[i].supply);
4421 while (--i >= 0)
4422 regulator_put(consumers[i].consumer);
4424 return ret;
4426 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4428 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4430 struct regulator_bulk_data *bulk = data;
4432 bulk->ret = regulator_enable(bulk->consumer);
4436 * regulator_bulk_enable - enable multiple regulator consumers
4438 * @num_consumers: Number of consumers
4439 * @consumers: Consumer data; clients are stored here.
4440 * @return 0 on success, an errno on failure
4442 * This convenience API allows consumers to enable multiple regulator
4443 * clients in a single API call. If any consumers cannot be enabled
4444 * then any others that were enabled will be disabled again prior to
4445 * return.
4447 int regulator_bulk_enable(int num_consumers,
4448 struct regulator_bulk_data *consumers)
4450 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4451 int i;
4452 int ret = 0;
4454 for (i = 0; i < num_consumers; i++) {
4455 async_schedule_domain(regulator_bulk_enable_async,
4456 &consumers[i], &async_domain);
4459 async_synchronize_full_domain(&async_domain);
4461 /* If any consumer failed we need to unwind any that succeeded */
4462 for (i = 0; i < num_consumers; i++) {
4463 if (consumers[i].ret != 0) {
4464 ret = consumers[i].ret;
4465 goto err;
4469 return 0;
4471 err:
4472 for (i = 0; i < num_consumers; i++) {
4473 if (consumers[i].ret < 0)
4474 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4475 consumers[i].ret);
4476 else
4477 regulator_disable(consumers[i].consumer);
4480 return ret;
4482 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4485 * regulator_bulk_disable - disable multiple regulator consumers
4487 * @num_consumers: Number of consumers
4488 * @consumers: Consumer data; clients are stored here.
4489 * @return 0 on success, an errno on failure
4491 * This convenience API allows consumers to disable multiple regulator
4492 * clients in a single API call. If any consumers cannot be disabled
4493 * then any others that were disabled will be enabled again prior to
4494 * return.
4496 int regulator_bulk_disable(int num_consumers,
4497 struct regulator_bulk_data *consumers)
4499 int i;
4500 int ret, r;
4502 for (i = num_consumers - 1; i >= 0; --i) {
4503 ret = regulator_disable(consumers[i].consumer);
4504 if (ret != 0)
4505 goto err;
4508 return 0;
4510 err:
4511 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4512 for (++i; i < num_consumers; ++i) {
4513 r = regulator_enable(consumers[i].consumer);
4514 if (r != 0)
4515 pr_err("Failed to re-enable %s: %d\n",
4516 consumers[i].supply, r);
4519 return ret;
4521 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4524 * regulator_bulk_force_disable - force disable multiple regulator consumers
4526 * @num_consumers: Number of consumers
4527 * @consumers: Consumer data; clients are stored here.
4528 * @return 0 on success, an errno on failure
4530 * This convenience API allows consumers to forcibly disable multiple regulator
4531 * clients in a single API call.
4532 * NOTE: This should be used for situations when device damage will
4533 * likely occur if the regulators are not disabled (e.g. over temp).
4534 * Although regulator_force_disable function call for some consumers can
4535 * return error numbers, the function is called for all consumers.
4537 int regulator_bulk_force_disable(int num_consumers,
4538 struct regulator_bulk_data *consumers)
4540 int i;
4541 int ret = 0;
4543 for (i = 0; i < num_consumers; i++) {
4544 consumers[i].ret =
4545 regulator_force_disable(consumers[i].consumer);
4547 /* Store first error for reporting */
4548 if (consumers[i].ret && !ret)
4549 ret = consumers[i].ret;
4552 return ret;
4554 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4557 * regulator_bulk_free - free multiple regulator consumers
4559 * @num_consumers: Number of consumers
4560 * @consumers: Consumer data; clients are stored here.
4562 * This convenience API allows consumers to free multiple regulator
4563 * clients in a single API call.
4565 void regulator_bulk_free(int num_consumers,
4566 struct regulator_bulk_data *consumers)
4568 int i;
4570 for (i = 0; i < num_consumers; i++) {
4571 regulator_put(consumers[i].consumer);
4572 consumers[i].consumer = NULL;
4575 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4578 * regulator_notifier_call_chain - call regulator event notifier
4579 * @rdev: regulator source
4580 * @event: notifier block
4581 * @data: callback-specific data.
4583 * Called by regulator drivers to notify clients a regulator event has
4584 * occurred. We also notify regulator clients downstream.
4585 * Note lock must be held by caller.
4587 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4588 unsigned long event, void *data)
4590 lockdep_assert_held_once(&rdev->mutex.base);
4592 _notifier_call_chain(rdev, event, data);
4593 return NOTIFY_DONE;
4596 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4599 * regulator_mode_to_status - convert a regulator mode into a status
4601 * @mode: Mode to convert
4603 * Convert a regulator mode into a status.
4605 int regulator_mode_to_status(unsigned int mode)
4607 switch (mode) {
4608 case REGULATOR_MODE_FAST:
4609 return REGULATOR_STATUS_FAST;
4610 case REGULATOR_MODE_NORMAL:
4611 return REGULATOR_STATUS_NORMAL;
4612 case REGULATOR_MODE_IDLE:
4613 return REGULATOR_STATUS_IDLE;
4614 case REGULATOR_MODE_STANDBY:
4615 return REGULATOR_STATUS_STANDBY;
4616 default:
4617 return REGULATOR_STATUS_UNDEFINED;
4620 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4622 static struct attribute *regulator_dev_attrs[] = {
4623 &dev_attr_name.attr,
4624 &dev_attr_num_users.attr,
4625 &dev_attr_type.attr,
4626 &dev_attr_microvolts.attr,
4627 &dev_attr_microamps.attr,
4628 &dev_attr_opmode.attr,
4629 &dev_attr_state.attr,
4630 &dev_attr_status.attr,
4631 &dev_attr_bypass.attr,
4632 &dev_attr_requested_microamps.attr,
4633 &dev_attr_min_microvolts.attr,
4634 &dev_attr_max_microvolts.attr,
4635 &dev_attr_min_microamps.attr,
4636 &dev_attr_max_microamps.attr,
4637 &dev_attr_suspend_standby_state.attr,
4638 &dev_attr_suspend_mem_state.attr,
4639 &dev_attr_suspend_disk_state.attr,
4640 &dev_attr_suspend_standby_microvolts.attr,
4641 &dev_attr_suspend_mem_microvolts.attr,
4642 &dev_attr_suspend_disk_microvolts.attr,
4643 &dev_attr_suspend_standby_mode.attr,
4644 &dev_attr_suspend_mem_mode.attr,
4645 &dev_attr_suspend_disk_mode.attr,
4646 NULL
4650 * To avoid cluttering sysfs (and memory) with useless state, only
4651 * create attributes that can be meaningfully displayed.
4653 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4654 struct attribute *attr, int idx)
4656 struct device *dev = kobj_to_dev(kobj);
4657 struct regulator_dev *rdev = dev_to_rdev(dev);
4658 const struct regulator_ops *ops = rdev->desc->ops;
4659 umode_t mode = attr->mode;
4661 /* these three are always present */
4662 if (attr == &dev_attr_name.attr ||
4663 attr == &dev_attr_num_users.attr ||
4664 attr == &dev_attr_type.attr)
4665 return mode;
4667 /* some attributes need specific methods to be displayed */
4668 if (attr == &dev_attr_microvolts.attr) {
4669 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4670 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4671 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4672 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4673 return mode;
4674 return 0;
4677 if (attr == &dev_attr_microamps.attr)
4678 return ops->get_current_limit ? mode : 0;
4680 if (attr == &dev_attr_opmode.attr)
4681 return ops->get_mode ? mode : 0;
4683 if (attr == &dev_attr_state.attr)
4684 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4686 if (attr == &dev_attr_status.attr)
4687 return ops->get_status ? mode : 0;
4689 if (attr == &dev_attr_bypass.attr)
4690 return ops->get_bypass ? mode : 0;
4692 /* constraints need specific supporting methods */
4693 if (attr == &dev_attr_min_microvolts.attr ||
4694 attr == &dev_attr_max_microvolts.attr)
4695 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4697 if (attr == &dev_attr_min_microamps.attr ||
4698 attr == &dev_attr_max_microamps.attr)
4699 return ops->set_current_limit ? mode : 0;
4701 if (attr == &dev_attr_suspend_standby_state.attr ||
4702 attr == &dev_attr_suspend_mem_state.attr ||
4703 attr == &dev_attr_suspend_disk_state.attr)
4704 return mode;
4706 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4707 attr == &dev_attr_suspend_mem_microvolts.attr ||
4708 attr == &dev_attr_suspend_disk_microvolts.attr)
4709 return ops->set_suspend_voltage ? mode : 0;
4711 if (attr == &dev_attr_suspend_standby_mode.attr ||
4712 attr == &dev_attr_suspend_mem_mode.attr ||
4713 attr == &dev_attr_suspend_disk_mode.attr)
4714 return ops->set_suspend_mode ? mode : 0;
4716 return mode;
4719 static const struct attribute_group regulator_dev_group = {
4720 .attrs = regulator_dev_attrs,
4721 .is_visible = regulator_attr_is_visible,
4724 static const struct attribute_group *regulator_dev_groups[] = {
4725 &regulator_dev_group,
4726 NULL
4729 static void regulator_dev_release(struct device *dev)
4731 struct regulator_dev *rdev = dev_get_drvdata(dev);
4733 kfree(rdev->constraints);
4734 of_node_put(rdev->dev.of_node);
4735 kfree(rdev);
4738 static void rdev_init_debugfs(struct regulator_dev *rdev)
4740 struct device *parent = rdev->dev.parent;
4741 const char *rname = rdev_get_name(rdev);
4742 char name[NAME_MAX];
4744 /* Avoid duplicate debugfs directory names */
4745 if (parent && rname == rdev->desc->name) {
4746 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4747 rname);
4748 rname = name;
4751 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4752 if (!rdev->debugfs) {
4753 rdev_warn(rdev, "Failed to create debugfs directory\n");
4754 return;
4757 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4758 &rdev->use_count);
4759 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4760 &rdev->open_count);
4761 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4762 &rdev->bypass_count);
4765 static int regulator_register_resolve_supply(struct device *dev, void *data)
4767 struct regulator_dev *rdev = dev_to_rdev(dev);
4769 if (regulator_resolve_supply(rdev))
4770 rdev_dbg(rdev, "unable to resolve supply\n");
4772 return 0;
4775 int regulator_coupler_register(struct regulator_coupler *coupler)
4777 mutex_lock(&regulator_list_mutex);
4778 list_add_tail(&coupler->list, &regulator_coupler_list);
4779 mutex_unlock(&regulator_list_mutex);
4781 return 0;
4784 static struct regulator_coupler *
4785 regulator_find_coupler(struct regulator_dev *rdev)
4787 struct regulator_coupler *coupler;
4788 int err;
4791 * Note that regulators are appended to the list and the generic
4792 * coupler is registered first, hence it will be attached at last
4793 * if nobody cared.
4795 list_for_each_entry_reverse(coupler, &regulator_coupler_list, list) {
4796 err = coupler->attach_regulator(coupler, rdev);
4797 if (!err) {
4798 if (!coupler->balance_voltage &&
4799 rdev->coupling_desc.n_coupled > 2)
4800 goto err_unsupported;
4802 return coupler;
4805 if (err < 0)
4806 return ERR_PTR(err);
4808 if (err == 1)
4809 continue;
4811 break;
4814 return ERR_PTR(-EINVAL);
4816 err_unsupported:
4817 if (coupler->detach_regulator)
4818 coupler->detach_regulator(coupler, rdev);
4820 rdev_err(rdev,
4821 "Voltage balancing for multiple regulator couples is unimplemented\n");
4823 return ERR_PTR(-EPERM);
4826 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4828 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4829 struct coupling_desc *c_desc = &rdev->coupling_desc;
4830 int n_coupled = c_desc->n_coupled;
4831 struct regulator_dev *c_rdev;
4832 int i;
4834 for (i = 1; i < n_coupled; i++) {
4835 /* already resolved */
4836 if (c_desc->coupled_rdevs[i])
4837 continue;
4839 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4841 if (!c_rdev)
4842 continue;
4844 if (c_rdev->coupling_desc.coupler != coupler) {
4845 rdev_err(rdev, "coupler mismatch with %s\n",
4846 rdev_get_name(c_rdev));
4847 return;
4850 regulator_lock(c_rdev);
4852 c_desc->coupled_rdevs[i] = c_rdev;
4853 c_desc->n_resolved++;
4855 regulator_unlock(c_rdev);
4857 regulator_resolve_coupling(c_rdev);
4861 static void regulator_remove_coupling(struct regulator_dev *rdev)
4863 struct regulator_coupler *coupler = rdev->coupling_desc.coupler;
4864 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4865 struct regulator_dev *__c_rdev, *c_rdev;
4866 unsigned int __n_coupled, n_coupled;
4867 int i, k;
4868 int err;
4870 n_coupled = c_desc->n_coupled;
4872 for (i = 1; i < n_coupled; i++) {
4873 c_rdev = c_desc->coupled_rdevs[i];
4875 if (!c_rdev)
4876 continue;
4878 regulator_lock(c_rdev);
4880 __c_desc = &c_rdev->coupling_desc;
4881 __n_coupled = __c_desc->n_coupled;
4883 for (k = 1; k < __n_coupled; k++) {
4884 __c_rdev = __c_desc->coupled_rdevs[k];
4886 if (__c_rdev == rdev) {
4887 __c_desc->coupled_rdevs[k] = NULL;
4888 __c_desc->n_resolved--;
4889 break;
4893 regulator_unlock(c_rdev);
4895 c_desc->coupled_rdevs[i] = NULL;
4896 c_desc->n_resolved--;
4899 if (coupler && coupler->detach_regulator) {
4900 err = coupler->detach_regulator(coupler, rdev);
4901 if (err)
4902 rdev_err(rdev, "failed to detach from coupler: %d\n",
4903 err);
4906 kfree(rdev->coupling_desc.coupled_rdevs);
4907 rdev->coupling_desc.coupled_rdevs = NULL;
4910 static int regulator_init_coupling(struct regulator_dev *rdev)
4912 int err, n_phandles;
4913 size_t alloc_size;
4915 if (!IS_ENABLED(CONFIG_OF))
4916 n_phandles = 0;
4917 else
4918 n_phandles = of_get_n_coupled(rdev);
4920 alloc_size = sizeof(*rdev) * (n_phandles + 1);
4922 rdev->coupling_desc.coupled_rdevs = kzalloc(alloc_size, GFP_KERNEL);
4923 if (!rdev->coupling_desc.coupled_rdevs)
4924 return -ENOMEM;
4927 * Every regulator should always have coupling descriptor filled with
4928 * at least pointer to itself.
4930 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4931 rdev->coupling_desc.n_coupled = n_phandles + 1;
4932 rdev->coupling_desc.n_resolved++;
4934 /* regulator isn't coupled */
4935 if (n_phandles == 0)
4936 return 0;
4938 if (!of_check_coupling_data(rdev))
4939 return -EPERM;
4941 rdev->coupling_desc.coupler = regulator_find_coupler(rdev);
4942 if (IS_ERR(rdev->coupling_desc.coupler)) {
4943 err = PTR_ERR(rdev->coupling_desc.coupler);
4944 rdev_err(rdev, "failed to get coupler: %d\n", err);
4945 return err;
4948 return 0;
4951 static int generic_coupler_attach(struct regulator_coupler *coupler,
4952 struct regulator_dev *rdev)
4954 if (rdev->coupling_desc.n_coupled > 2) {
4955 rdev_err(rdev,
4956 "Voltage balancing for multiple regulator couples is unimplemented\n");
4957 return -EPERM;
4960 return 0;
4963 static struct regulator_coupler generic_regulator_coupler = {
4964 .attach_regulator = generic_coupler_attach,
4968 * regulator_register - register regulator
4969 * @regulator_desc: regulator to register
4970 * @cfg: runtime configuration for regulator
4972 * Called by regulator drivers to register a regulator.
4973 * Returns a valid pointer to struct regulator_dev on success
4974 * or an ERR_PTR() on error.
4976 struct regulator_dev *
4977 regulator_register(const struct regulator_desc *regulator_desc,
4978 const struct regulator_config *cfg)
4980 const struct regulation_constraints *constraints = NULL;
4981 const struct regulator_init_data *init_data;
4982 struct regulator_config *config = NULL;
4983 static atomic_t regulator_no = ATOMIC_INIT(-1);
4984 struct regulator_dev *rdev;
4985 bool dangling_cfg_gpiod = false;
4986 bool dangling_of_gpiod = false;
4987 struct device *dev;
4988 int ret, i;
4990 if (cfg == NULL)
4991 return ERR_PTR(-EINVAL);
4992 if (cfg->ena_gpiod)
4993 dangling_cfg_gpiod = true;
4994 if (regulator_desc == NULL) {
4995 ret = -EINVAL;
4996 goto rinse;
4999 dev = cfg->dev;
5000 WARN_ON(!dev);
5002 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
5003 ret = -EINVAL;
5004 goto rinse;
5007 if (regulator_desc->type != REGULATOR_VOLTAGE &&
5008 regulator_desc->type != REGULATOR_CURRENT) {
5009 ret = -EINVAL;
5010 goto rinse;
5013 /* Only one of each should be implemented */
5014 WARN_ON(regulator_desc->ops->get_voltage &&
5015 regulator_desc->ops->get_voltage_sel);
5016 WARN_ON(regulator_desc->ops->set_voltage &&
5017 regulator_desc->ops->set_voltage_sel);
5019 /* If we're using selectors we must implement list_voltage. */
5020 if (regulator_desc->ops->get_voltage_sel &&
5021 !regulator_desc->ops->list_voltage) {
5022 ret = -EINVAL;
5023 goto rinse;
5025 if (regulator_desc->ops->set_voltage_sel &&
5026 !regulator_desc->ops->list_voltage) {
5027 ret = -EINVAL;
5028 goto rinse;
5031 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
5032 if (rdev == NULL) {
5033 ret = -ENOMEM;
5034 goto rinse;
5038 * Duplicate the config so the driver could override it after
5039 * parsing init data.
5041 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
5042 if (config == NULL) {
5043 kfree(rdev);
5044 ret = -ENOMEM;
5045 goto rinse;
5048 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
5049 &rdev->dev.of_node);
5051 * We need to keep track of any GPIO descriptor coming from the
5052 * device tree until we have handled it over to the core. If the
5053 * config that was passed in to this function DOES NOT contain
5054 * a descriptor, and the config after this call DOES contain
5055 * a descriptor, we definitely got one from parsing the device
5056 * tree.
5058 if (!cfg->ena_gpiod && config->ena_gpiod)
5059 dangling_of_gpiod = true;
5060 if (!init_data) {
5061 init_data = config->init_data;
5062 rdev->dev.of_node = of_node_get(config->of_node);
5065 ww_mutex_init(&rdev->mutex, &regulator_ww_class);
5066 rdev->reg_data = config->driver_data;
5067 rdev->owner = regulator_desc->owner;
5068 rdev->desc = regulator_desc;
5069 if (config->regmap)
5070 rdev->regmap = config->regmap;
5071 else if (dev_get_regmap(dev, NULL))
5072 rdev->regmap = dev_get_regmap(dev, NULL);
5073 else if (dev->parent)
5074 rdev->regmap = dev_get_regmap(dev->parent, NULL);
5075 INIT_LIST_HEAD(&rdev->consumer_list);
5076 INIT_LIST_HEAD(&rdev->list);
5077 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
5078 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
5080 /* preform any regulator specific init */
5081 if (init_data && init_data->regulator_init) {
5082 ret = init_data->regulator_init(rdev->reg_data);
5083 if (ret < 0)
5084 goto clean;
5087 if (config->ena_gpiod) {
5088 mutex_lock(&regulator_list_mutex);
5089 ret = regulator_ena_gpio_request(rdev, config);
5090 mutex_unlock(&regulator_list_mutex);
5091 if (ret != 0) {
5092 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
5093 ret);
5094 goto clean;
5096 /* The regulator core took over the GPIO descriptor */
5097 dangling_cfg_gpiod = false;
5098 dangling_of_gpiod = false;
5101 /* register with sysfs */
5102 rdev->dev.class = &regulator_class;
5103 rdev->dev.parent = dev;
5104 dev_set_name(&rdev->dev, "regulator.%lu",
5105 (unsigned long) atomic_inc_return(&regulator_no));
5107 /* set regulator constraints */
5108 if (init_data)
5109 constraints = &init_data->constraints;
5111 if (init_data && init_data->supply_regulator)
5112 rdev->supply_name = init_data->supply_regulator;
5113 else if (regulator_desc->supply_name)
5114 rdev->supply_name = regulator_desc->supply_name;
5117 * Attempt to resolve the regulator supply, if specified,
5118 * but don't return an error if we fail because we will try
5119 * to resolve it again later as more regulators are added.
5121 if (regulator_resolve_supply(rdev))
5122 rdev_dbg(rdev, "unable to resolve supply\n");
5124 ret = set_machine_constraints(rdev, constraints);
5125 if (ret < 0)
5126 goto wash;
5128 mutex_lock(&regulator_list_mutex);
5129 ret = regulator_init_coupling(rdev);
5130 mutex_unlock(&regulator_list_mutex);
5131 if (ret < 0)
5132 goto wash;
5134 /* add consumers devices */
5135 if (init_data) {
5136 mutex_lock(&regulator_list_mutex);
5137 for (i = 0; i < init_data->num_consumer_supplies; i++) {
5138 ret = set_consumer_device_supply(rdev,
5139 init_data->consumer_supplies[i].dev_name,
5140 init_data->consumer_supplies[i].supply);
5141 if (ret < 0) {
5142 mutex_unlock(&regulator_list_mutex);
5143 dev_err(dev, "Failed to set supply %s\n",
5144 init_data->consumer_supplies[i].supply);
5145 goto unset_supplies;
5148 mutex_unlock(&regulator_list_mutex);
5151 if (!rdev->desc->ops->get_voltage &&
5152 !rdev->desc->ops->list_voltage &&
5153 !rdev->desc->fixed_uV)
5154 rdev->is_switch = true;
5156 dev_set_drvdata(&rdev->dev, rdev);
5157 ret = device_register(&rdev->dev);
5158 if (ret != 0) {
5159 put_device(&rdev->dev);
5160 goto unset_supplies;
5163 rdev_init_debugfs(rdev);
5165 /* try to resolve regulators coupling since a new one was registered */
5166 mutex_lock(&regulator_list_mutex);
5167 regulator_resolve_coupling(rdev);
5168 mutex_unlock(&regulator_list_mutex);
5170 /* try to resolve regulators supply since a new one was registered */
5171 class_for_each_device(&regulator_class, NULL, NULL,
5172 regulator_register_resolve_supply);
5173 kfree(config);
5174 return rdev;
5176 unset_supplies:
5177 mutex_lock(&regulator_list_mutex);
5178 unset_regulator_supplies(rdev);
5179 regulator_remove_coupling(rdev);
5180 mutex_unlock(&regulator_list_mutex);
5181 wash:
5182 kfree(rdev->constraints);
5183 mutex_lock(&regulator_list_mutex);
5184 regulator_ena_gpio_free(rdev);
5185 mutex_unlock(&regulator_list_mutex);
5186 clean:
5187 if (dangling_of_gpiod)
5188 gpiod_put(config->ena_gpiod);
5189 kfree(rdev);
5190 kfree(config);
5191 rinse:
5192 if (dangling_cfg_gpiod)
5193 gpiod_put(cfg->ena_gpiod);
5194 return ERR_PTR(ret);
5196 EXPORT_SYMBOL_GPL(regulator_register);
5199 * regulator_unregister - unregister regulator
5200 * @rdev: regulator to unregister
5202 * Called by regulator drivers to unregister a regulator.
5204 void regulator_unregister(struct regulator_dev *rdev)
5206 if (rdev == NULL)
5207 return;
5209 if (rdev->supply) {
5210 while (rdev->use_count--)
5211 regulator_disable(rdev->supply);
5212 regulator_put(rdev->supply);
5215 flush_work(&rdev->disable_work.work);
5217 mutex_lock(&regulator_list_mutex);
5219 debugfs_remove_recursive(rdev->debugfs);
5220 WARN_ON(rdev->open_count);
5221 regulator_remove_coupling(rdev);
5222 unset_regulator_supplies(rdev);
5223 list_del(&rdev->list);
5224 regulator_ena_gpio_free(rdev);
5225 device_unregister(&rdev->dev);
5227 mutex_unlock(&regulator_list_mutex);
5229 EXPORT_SYMBOL_GPL(regulator_unregister);
5231 #ifdef CONFIG_SUSPEND
5233 * regulator_suspend - prepare regulators for system wide suspend
5234 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5236 * Configure each regulator with it's suspend operating parameters for state.
5238 static int regulator_suspend(struct device *dev)
5240 struct regulator_dev *rdev = dev_to_rdev(dev);
5241 suspend_state_t state = pm_suspend_target_state;
5242 int ret;
5244 regulator_lock(rdev);
5245 ret = suspend_set_state(rdev, state);
5246 regulator_unlock(rdev);
5248 return ret;
5251 static int regulator_resume(struct device *dev)
5253 suspend_state_t state = pm_suspend_target_state;
5254 struct regulator_dev *rdev = dev_to_rdev(dev);
5255 struct regulator_state *rstate;
5256 int ret = 0;
5258 rstate = regulator_get_suspend_state(rdev, state);
5259 if (rstate == NULL)
5260 return 0;
5262 regulator_lock(rdev);
5264 if (rdev->desc->ops->resume &&
5265 (rstate->enabled == ENABLE_IN_SUSPEND ||
5266 rstate->enabled == DISABLE_IN_SUSPEND))
5267 ret = rdev->desc->ops->resume(rdev);
5269 regulator_unlock(rdev);
5271 return ret;
5273 #else /* !CONFIG_SUSPEND */
5275 #define regulator_suspend NULL
5276 #define regulator_resume NULL
5278 #endif /* !CONFIG_SUSPEND */
5280 #ifdef CONFIG_PM
5281 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5282 .suspend = regulator_suspend,
5283 .resume = regulator_resume,
5285 #endif
5287 struct class regulator_class = {
5288 .name = "regulator",
5289 .dev_release = regulator_dev_release,
5290 .dev_groups = regulator_dev_groups,
5291 #ifdef CONFIG_PM
5292 .pm = &regulator_pm_ops,
5293 #endif
5296 * regulator_has_full_constraints - the system has fully specified constraints
5298 * Calling this function will cause the regulator API to disable all
5299 * regulators which have a zero use count and don't have an always_on
5300 * constraint in a late_initcall.
5302 * The intention is that this will become the default behaviour in a
5303 * future kernel release so users are encouraged to use this facility
5304 * now.
5306 void regulator_has_full_constraints(void)
5308 has_full_constraints = 1;
5310 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5313 * rdev_get_drvdata - get rdev regulator driver data
5314 * @rdev: regulator
5316 * Get rdev regulator driver private data. This call can be used in the
5317 * regulator driver context.
5319 void *rdev_get_drvdata(struct regulator_dev *rdev)
5321 return rdev->reg_data;
5323 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5326 * regulator_get_drvdata - get regulator driver data
5327 * @regulator: regulator
5329 * Get regulator driver private data. This call can be used in the consumer
5330 * driver context when non API regulator specific functions need to be called.
5332 void *regulator_get_drvdata(struct regulator *regulator)
5334 return regulator->rdev->reg_data;
5336 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5339 * regulator_set_drvdata - set regulator driver data
5340 * @regulator: regulator
5341 * @data: data
5343 void regulator_set_drvdata(struct regulator *regulator, void *data)
5345 regulator->rdev->reg_data = data;
5347 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5350 * regulator_get_id - get regulator ID
5351 * @rdev: regulator
5353 int rdev_get_id(struct regulator_dev *rdev)
5355 return rdev->desc->id;
5357 EXPORT_SYMBOL_GPL(rdev_get_id);
5359 struct device *rdev_get_dev(struct regulator_dev *rdev)
5361 return &rdev->dev;
5363 EXPORT_SYMBOL_GPL(rdev_get_dev);
5365 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5367 return rdev->regmap;
5369 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5371 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5373 return reg_init_data->driver_data;
5375 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5377 #ifdef CONFIG_DEBUG_FS
5378 static int supply_map_show(struct seq_file *sf, void *data)
5380 struct regulator_map *map;
5382 list_for_each_entry(map, &regulator_map_list, list) {
5383 seq_printf(sf, "%s -> %s.%s\n",
5384 rdev_get_name(map->regulator), map->dev_name,
5385 map->supply);
5388 return 0;
5390 DEFINE_SHOW_ATTRIBUTE(supply_map);
5392 struct summary_data {
5393 struct seq_file *s;
5394 struct regulator_dev *parent;
5395 int level;
5398 static void regulator_summary_show_subtree(struct seq_file *s,
5399 struct regulator_dev *rdev,
5400 int level);
5402 static int regulator_summary_show_children(struct device *dev, void *data)
5404 struct regulator_dev *rdev = dev_to_rdev(dev);
5405 struct summary_data *summary_data = data;
5407 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5408 regulator_summary_show_subtree(summary_data->s, rdev,
5409 summary_data->level + 1);
5411 return 0;
5414 static void regulator_summary_show_subtree(struct seq_file *s,
5415 struct regulator_dev *rdev,
5416 int level)
5418 struct regulation_constraints *c;
5419 struct regulator *consumer;
5420 struct summary_data summary_data;
5421 unsigned int opmode;
5423 if (!rdev)
5424 return;
5426 opmode = _regulator_get_mode_unlocked(rdev);
5427 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5428 level * 3 + 1, "",
5429 30 - level * 3, rdev_get_name(rdev),
5430 rdev->use_count, rdev->open_count, rdev->bypass_count,
5431 regulator_opmode_to_str(opmode));
5433 seq_printf(s, "%5dmV ", regulator_get_voltage_rdev(rdev) / 1000);
5434 seq_printf(s, "%5dmA ",
5435 _regulator_get_current_limit_unlocked(rdev) / 1000);
5437 c = rdev->constraints;
5438 if (c) {
5439 switch (rdev->desc->type) {
5440 case REGULATOR_VOLTAGE:
5441 seq_printf(s, "%5dmV %5dmV ",
5442 c->min_uV / 1000, c->max_uV / 1000);
5443 break;
5444 case REGULATOR_CURRENT:
5445 seq_printf(s, "%5dmA %5dmA ",
5446 c->min_uA / 1000, c->max_uA / 1000);
5447 break;
5451 seq_puts(s, "\n");
5453 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5454 if (consumer->dev && consumer->dev->class == &regulator_class)
5455 continue;
5457 seq_printf(s, "%*s%-*s ",
5458 (level + 1) * 3 + 1, "",
5459 30 - (level + 1) * 3,
5460 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5462 switch (rdev->desc->type) {
5463 case REGULATOR_VOLTAGE:
5464 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5465 consumer->enable_count,
5466 consumer->uA_load / 1000,
5467 consumer->uA_load && !consumer->enable_count ?
5468 '*' : ' ',
5469 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5470 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5471 break;
5472 case REGULATOR_CURRENT:
5473 break;
5476 seq_puts(s, "\n");
5479 summary_data.s = s;
5480 summary_data.level = level;
5481 summary_data.parent = rdev;
5483 class_for_each_device(&regulator_class, NULL, &summary_data,
5484 regulator_summary_show_children);
5487 struct summary_lock_data {
5488 struct ww_acquire_ctx *ww_ctx;
5489 struct regulator_dev **new_contended_rdev;
5490 struct regulator_dev **old_contended_rdev;
5493 static int regulator_summary_lock_one(struct device *dev, void *data)
5495 struct regulator_dev *rdev = dev_to_rdev(dev);
5496 struct summary_lock_data *lock_data = data;
5497 int ret = 0;
5499 if (rdev != *lock_data->old_contended_rdev) {
5500 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5502 if (ret == -EDEADLK)
5503 *lock_data->new_contended_rdev = rdev;
5504 else
5505 WARN_ON_ONCE(ret);
5506 } else {
5507 *lock_data->old_contended_rdev = NULL;
5510 return ret;
5513 static int regulator_summary_unlock_one(struct device *dev, void *data)
5515 struct regulator_dev *rdev = dev_to_rdev(dev);
5516 struct summary_lock_data *lock_data = data;
5518 if (lock_data) {
5519 if (rdev == *lock_data->new_contended_rdev)
5520 return -EDEADLK;
5523 regulator_unlock(rdev);
5525 return 0;
5528 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5529 struct regulator_dev **new_contended_rdev,
5530 struct regulator_dev **old_contended_rdev)
5532 struct summary_lock_data lock_data;
5533 int ret;
5535 lock_data.ww_ctx = ww_ctx;
5536 lock_data.new_contended_rdev = new_contended_rdev;
5537 lock_data.old_contended_rdev = old_contended_rdev;
5539 ret = class_for_each_device(&regulator_class, NULL, &lock_data,
5540 regulator_summary_lock_one);
5541 if (ret)
5542 class_for_each_device(&regulator_class, NULL, &lock_data,
5543 regulator_summary_unlock_one);
5545 return ret;
5548 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5550 struct regulator_dev *new_contended_rdev = NULL;
5551 struct regulator_dev *old_contended_rdev = NULL;
5552 int err;
5554 mutex_lock(&regulator_list_mutex);
5556 ww_acquire_init(ww_ctx, &regulator_ww_class);
5558 do {
5559 if (new_contended_rdev) {
5560 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5561 old_contended_rdev = new_contended_rdev;
5562 old_contended_rdev->ref_cnt++;
5565 err = regulator_summary_lock_all(ww_ctx,
5566 &new_contended_rdev,
5567 &old_contended_rdev);
5569 if (old_contended_rdev)
5570 regulator_unlock(old_contended_rdev);
5572 } while (err == -EDEADLK);
5574 ww_acquire_done(ww_ctx);
5577 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5579 class_for_each_device(&regulator_class, NULL, NULL,
5580 regulator_summary_unlock_one);
5581 ww_acquire_fini(ww_ctx);
5583 mutex_unlock(&regulator_list_mutex);
5586 static int regulator_summary_show_roots(struct device *dev, void *data)
5588 struct regulator_dev *rdev = dev_to_rdev(dev);
5589 struct seq_file *s = data;
5591 if (!rdev->supply)
5592 regulator_summary_show_subtree(s, rdev, 0);
5594 return 0;
5597 static int regulator_summary_show(struct seq_file *s, void *data)
5599 struct ww_acquire_ctx ww_ctx;
5601 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5602 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5604 regulator_summary_lock(&ww_ctx);
5606 class_for_each_device(&regulator_class, NULL, s,
5607 regulator_summary_show_roots);
5609 regulator_summary_unlock(&ww_ctx);
5611 return 0;
5613 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5614 #endif /* CONFIG_DEBUG_FS */
5616 static int __init regulator_init(void)
5618 int ret;
5620 ret = class_register(&regulator_class);
5622 debugfs_root = debugfs_create_dir("regulator", NULL);
5623 if (!debugfs_root)
5624 pr_warn("regulator: Failed to create debugfs directory\n");
5626 #ifdef CONFIG_DEBUG_FS
5627 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5628 &supply_map_fops);
5630 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5631 NULL, &regulator_summary_fops);
5632 #endif
5633 regulator_dummy_init();
5635 regulator_coupler_register(&generic_regulator_coupler);
5637 return ret;
5640 /* init early to allow our consumers to complete system booting */
5641 core_initcall(regulator_init);
5643 static int __init regulator_late_cleanup(struct device *dev, void *data)
5645 struct regulator_dev *rdev = dev_to_rdev(dev);
5646 const struct regulator_ops *ops = rdev->desc->ops;
5647 struct regulation_constraints *c = rdev->constraints;
5648 int enabled, ret;
5650 if (c && c->always_on)
5651 return 0;
5653 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5654 return 0;
5656 regulator_lock(rdev);
5658 if (rdev->use_count)
5659 goto unlock;
5661 /* If we can't read the status assume it's on. */
5662 if (ops->is_enabled)
5663 enabled = ops->is_enabled(rdev);
5664 else
5665 enabled = 1;
5667 if (!enabled)
5668 goto unlock;
5670 if (have_full_constraints()) {
5671 /* We log since this may kill the system if it goes
5672 * wrong. */
5673 rdev_info(rdev, "disabling\n");
5674 ret = _regulator_do_disable(rdev);
5675 if (ret != 0)
5676 rdev_err(rdev, "couldn't disable: %d\n", ret);
5677 } else {
5678 /* The intention is that in future we will
5679 * assume that full constraints are provided
5680 * so warn even if we aren't going to do
5681 * anything here.
5683 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5686 unlock:
5687 regulator_unlock(rdev);
5689 return 0;
5692 static int __init regulator_init_complete(void)
5695 * Since DT doesn't provide an idiomatic mechanism for
5696 * enabling full constraints and since it's much more natural
5697 * with DT to provide them just assume that a DT enabled
5698 * system has full constraints.
5700 if (of_have_populated_dt())
5701 has_full_constraints = true;
5704 * Regulators may had failed to resolve their input supplies
5705 * when were registered, either because the input supply was
5706 * not registered yet or because its parent device was not
5707 * bound yet. So attempt to resolve the input supplies for
5708 * pending regulators before trying to disable unused ones.
5710 class_for_each_device(&regulator_class, NULL, NULL,
5711 regulator_register_resolve_supply);
5713 /* If we have a full configuration then disable any regulators
5714 * we have permission to change the status for and which are
5715 * not in use or always_on. This is effectively the default
5716 * for DT and ACPI as they have full constraints.
5718 class_for_each_device(&regulator_class, NULL, NULL,
5719 regulator_late_cleanup);
5721 return 0;
5723 late_initcall_sync(regulator_init_complete);