Linux 5.1.15
[linux/fpc-iii.git] / drivers / regulator / core.c
blob35a7d020afecdcb093e9db4d653dc8a9a7b8b87c
1 /*
2 * core.c -- Voltage/Current Regulator framework.
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 * Copyright 2008 SlimLogic Ltd.
7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/debugfs.h>
19 #include <linux/device.h>
20 #include <linux/slab.h>
21 #include <linux/async.h>
22 #include <linux/err.h>
23 #include <linux/mutex.h>
24 #include <linux/suspend.h>
25 #include <linux/delay.h>
26 #include <linux/gpio/consumer.h>
27 #include <linux/of.h>
28 #include <linux/regmap.h>
29 #include <linux/regulator/of_regulator.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/regulator/driver.h>
32 #include <linux/regulator/machine.h>
33 #include <linux/module.h>
35 #define CREATE_TRACE_POINTS
36 #include <trace/events/regulator.h>
38 #include "dummy.h"
39 #include "internal.h"
41 #define rdev_crit(rdev, fmt, ...) \
42 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43 #define rdev_err(rdev, fmt, ...) \
44 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45 #define rdev_warn(rdev, fmt, ...) \
46 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47 #define rdev_info(rdev, fmt, ...) \
48 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
49 #define rdev_dbg(rdev, fmt, ...) \
50 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
52 static DEFINE_WW_CLASS(regulator_ww_class);
53 static DEFINE_MUTEX(regulator_nesting_mutex);
54 static DEFINE_MUTEX(regulator_list_mutex);
55 static LIST_HEAD(regulator_map_list);
56 static LIST_HEAD(regulator_ena_gpio_list);
57 static LIST_HEAD(regulator_supply_alias_list);
58 static bool has_full_constraints;
60 static struct dentry *debugfs_root;
63 * struct regulator_map
65 * Used to provide symbolic supply names to devices.
67 struct regulator_map {
68 struct list_head list;
69 const char *dev_name; /* The dev_name() for the consumer */
70 const char *supply;
71 struct regulator_dev *regulator;
75 * struct regulator_enable_gpio
77 * Management for shared enable GPIO pin
79 struct regulator_enable_gpio {
80 struct list_head list;
81 struct gpio_desc *gpiod;
82 u32 enable_count; /* a number of enabled shared GPIO */
83 u32 request_count; /* a number of requested shared GPIO */
87 * struct regulator_supply_alias
89 * Used to map lookups for a supply onto an alternative device.
91 struct regulator_supply_alias {
92 struct list_head list;
93 struct device *src_dev;
94 const char *src_supply;
95 struct device *alias_dev;
96 const char *alias_supply;
99 static int _regulator_is_enabled(struct regulator_dev *rdev);
100 static int _regulator_disable(struct regulator *regulator);
101 static int _regulator_get_voltage(struct regulator_dev *rdev);
102 static int _regulator_get_current_limit(struct regulator_dev *rdev);
103 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
104 static int _notifier_call_chain(struct regulator_dev *rdev,
105 unsigned long event, void *data);
106 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
107 int min_uV, int max_uV);
108 static int regulator_balance_voltage(struct regulator_dev *rdev,
109 suspend_state_t state);
110 static int regulator_set_voltage_rdev(struct regulator_dev *rdev,
111 int min_uV, int max_uV,
112 suspend_state_t state);
113 static struct regulator *create_regulator(struct regulator_dev *rdev,
114 struct device *dev,
115 const char *supply_name);
116 static void _regulator_put(struct regulator *regulator);
118 static const char *rdev_get_name(struct regulator_dev *rdev)
120 if (rdev->constraints && rdev->constraints->name)
121 return rdev->constraints->name;
122 else if (rdev->desc->name)
123 return rdev->desc->name;
124 else
125 return "";
128 static bool have_full_constraints(void)
130 return has_full_constraints || of_have_populated_dt();
133 static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops)
135 if (!rdev->constraints) {
136 rdev_err(rdev, "no constraints\n");
137 return false;
140 if (rdev->constraints->valid_ops_mask & ops)
141 return true;
143 return false;
147 * regulator_lock_nested - lock a single regulator
148 * @rdev: regulator source
149 * @ww_ctx: w/w mutex acquire context
151 * This function can be called many times by one task on
152 * a single regulator and its mutex will be locked only
153 * once. If a task, which is calling this function is other
154 * than the one, which initially locked the mutex, it will
155 * wait on mutex.
157 static inline int regulator_lock_nested(struct regulator_dev *rdev,
158 struct ww_acquire_ctx *ww_ctx)
160 bool lock = false;
161 int ret = 0;
163 mutex_lock(&regulator_nesting_mutex);
165 if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) {
166 if (rdev->mutex_owner == current)
167 rdev->ref_cnt++;
168 else
169 lock = true;
171 if (lock) {
172 mutex_unlock(&regulator_nesting_mutex);
173 ret = ww_mutex_lock(&rdev->mutex, ww_ctx);
174 mutex_lock(&regulator_nesting_mutex);
176 } else {
177 lock = true;
180 if (lock && ret != -EDEADLK) {
181 rdev->ref_cnt++;
182 rdev->mutex_owner = current;
185 mutex_unlock(&regulator_nesting_mutex);
187 return ret;
191 * regulator_lock - lock a single regulator
192 * @rdev: regulator source
194 * This function can be called many times by one task on
195 * a single regulator and its mutex will be locked only
196 * once. If a task, which is calling this function is other
197 * than the one, which initially locked the mutex, it will
198 * wait on mutex.
200 void regulator_lock(struct regulator_dev *rdev)
202 regulator_lock_nested(rdev, NULL);
204 EXPORT_SYMBOL_GPL(regulator_lock);
207 * regulator_unlock - unlock a single regulator
208 * @rdev: regulator_source
210 * This function unlocks the mutex when the
211 * reference counter reaches 0.
213 void regulator_unlock(struct regulator_dev *rdev)
215 mutex_lock(&regulator_nesting_mutex);
217 if (--rdev->ref_cnt == 0) {
218 rdev->mutex_owner = NULL;
219 ww_mutex_unlock(&rdev->mutex);
222 WARN_ON_ONCE(rdev->ref_cnt < 0);
224 mutex_unlock(&regulator_nesting_mutex);
226 EXPORT_SYMBOL_GPL(regulator_unlock);
228 static bool regulator_supply_is_couple(struct regulator_dev *rdev)
230 struct regulator_dev *c_rdev;
231 int i;
233 for (i = 1; i < rdev->coupling_desc.n_coupled; i++) {
234 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
236 if (rdev->supply->rdev == c_rdev)
237 return true;
240 return false;
243 static void regulator_unlock_recursive(struct regulator_dev *rdev,
244 unsigned int n_coupled)
246 struct regulator_dev *c_rdev;
247 int i;
249 for (i = n_coupled; i > 0; i--) {
250 c_rdev = rdev->coupling_desc.coupled_rdevs[i - 1];
252 if (!c_rdev)
253 continue;
255 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev))
256 regulator_unlock_recursive(
257 c_rdev->supply->rdev,
258 c_rdev->coupling_desc.n_coupled);
260 regulator_unlock(c_rdev);
264 static int regulator_lock_recursive(struct regulator_dev *rdev,
265 struct regulator_dev **new_contended_rdev,
266 struct regulator_dev **old_contended_rdev,
267 struct ww_acquire_ctx *ww_ctx)
269 struct regulator_dev *c_rdev;
270 int i, err;
272 for (i = 0; i < rdev->coupling_desc.n_coupled; i++) {
273 c_rdev = rdev->coupling_desc.coupled_rdevs[i];
275 if (!c_rdev)
276 continue;
278 if (c_rdev != *old_contended_rdev) {
279 err = regulator_lock_nested(c_rdev, ww_ctx);
280 if (err) {
281 if (err == -EDEADLK) {
282 *new_contended_rdev = c_rdev;
283 goto err_unlock;
286 /* shouldn't happen */
287 WARN_ON_ONCE(err != -EALREADY);
289 } else {
290 *old_contended_rdev = NULL;
293 if (c_rdev->supply && !regulator_supply_is_couple(c_rdev)) {
294 err = regulator_lock_recursive(c_rdev->supply->rdev,
295 new_contended_rdev,
296 old_contended_rdev,
297 ww_ctx);
298 if (err) {
299 regulator_unlock(c_rdev);
300 goto err_unlock;
305 return 0;
307 err_unlock:
308 regulator_unlock_recursive(rdev, i);
310 return err;
314 * regulator_unlock_dependent - unlock regulator's suppliers and coupled
315 * regulators
316 * @rdev: regulator source
317 * @ww_ctx: w/w mutex acquire context
319 * Unlock all regulators related with rdev by coupling or supplying.
321 static void regulator_unlock_dependent(struct regulator_dev *rdev,
322 struct ww_acquire_ctx *ww_ctx)
324 regulator_unlock_recursive(rdev, rdev->coupling_desc.n_coupled);
325 ww_acquire_fini(ww_ctx);
329 * regulator_lock_dependent - lock regulator's suppliers and coupled regulators
330 * @rdev: regulator source
331 * @ww_ctx: w/w mutex acquire context
333 * This function as a wrapper on regulator_lock_recursive(), which locks
334 * all regulators related with rdev by coupling or supplying.
336 static void regulator_lock_dependent(struct regulator_dev *rdev,
337 struct ww_acquire_ctx *ww_ctx)
339 struct regulator_dev *new_contended_rdev = NULL;
340 struct regulator_dev *old_contended_rdev = NULL;
341 int err;
343 mutex_lock(&regulator_list_mutex);
345 ww_acquire_init(ww_ctx, &regulator_ww_class);
347 do {
348 if (new_contended_rdev) {
349 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
350 old_contended_rdev = new_contended_rdev;
351 old_contended_rdev->ref_cnt++;
354 err = regulator_lock_recursive(rdev,
355 &new_contended_rdev,
356 &old_contended_rdev,
357 ww_ctx);
359 if (old_contended_rdev)
360 regulator_unlock(old_contended_rdev);
362 } while (err == -EDEADLK);
364 ww_acquire_done(ww_ctx);
366 mutex_unlock(&regulator_list_mutex);
370 * of_get_child_regulator - get a child regulator device node
371 * based on supply name
372 * @parent: Parent device node
373 * @prop_name: Combination regulator supply name and "-supply"
375 * Traverse all child nodes.
376 * Extract the child regulator device node corresponding to the supply name.
377 * returns the device node corresponding to the regulator if found, else
378 * returns NULL.
380 static struct device_node *of_get_child_regulator(struct device_node *parent,
381 const char *prop_name)
383 struct device_node *regnode = NULL;
384 struct device_node *child = NULL;
386 for_each_child_of_node(parent, child) {
387 regnode = of_parse_phandle(child, prop_name, 0);
389 if (!regnode) {
390 regnode = of_get_child_regulator(child, prop_name);
391 if (regnode)
392 return regnode;
393 } else {
394 return regnode;
397 return NULL;
401 * of_get_regulator - get a regulator device node based on supply name
402 * @dev: Device pointer for the consumer (of regulator) device
403 * @supply: regulator supply name
405 * Extract the regulator device node corresponding to the supply name.
406 * returns the device node corresponding to the regulator if found, else
407 * returns NULL.
409 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
411 struct device_node *regnode = NULL;
412 char prop_name[32]; /* 32 is max size of property name */
414 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
416 snprintf(prop_name, 32, "%s-supply", supply);
417 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
419 if (!regnode) {
420 regnode = of_get_child_regulator(dev->of_node, prop_name);
421 if (regnode)
422 return regnode;
424 dev_dbg(dev, "Looking up %s property in node %pOF failed\n",
425 prop_name, dev->of_node);
426 return NULL;
428 return regnode;
431 /* Platform voltage constraint check */
432 static int regulator_check_voltage(struct regulator_dev *rdev,
433 int *min_uV, int *max_uV)
435 BUG_ON(*min_uV > *max_uV);
437 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
438 rdev_err(rdev, "voltage operation not allowed\n");
439 return -EPERM;
442 if (*max_uV > rdev->constraints->max_uV)
443 *max_uV = rdev->constraints->max_uV;
444 if (*min_uV < rdev->constraints->min_uV)
445 *min_uV = rdev->constraints->min_uV;
447 if (*min_uV > *max_uV) {
448 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
449 *min_uV, *max_uV);
450 return -EINVAL;
453 return 0;
456 /* return 0 if the state is valid */
457 static int regulator_check_states(suspend_state_t state)
459 return (state > PM_SUSPEND_MAX || state == PM_SUSPEND_TO_IDLE);
462 /* Make sure we select a voltage that suits the needs of all
463 * regulator consumers
465 static int regulator_check_consumers(struct regulator_dev *rdev,
466 int *min_uV, int *max_uV,
467 suspend_state_t state)
469 struct regulator *regulator;
470 struct regulator_voltage *voltage;
472 list_for_each_entry(regulator, &rdev->consumer_list, list) {
473 voltage = &regulator->voltage[state];
475 * Assume consumers that didn't say anything are OK
476 * with anything in the constraint range.
478 if (!voltage->min_uV && !voltage->max_uV)
479 continue;
481 if (*max_uV > voltage->max_uV)
482 *max_uV = voltage->max_uV;
483 if (*min_uV < voltage->min_uV)
484 *min_uV = voltage->min_uV;
487 if (*min_uV > *max_uV) {
488 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
489 *min_uV, *max_uV);
490 return -EINVAL;
493 return 0;
496 /* current constraint check */
497 static int regulator_check_current_limit(struct regulator_dev *rdev,
498 int *min_uA, int *max_uA)
500 BUG_ON(*min_uA > *max_uA);
502 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_CURRENT)) {
503 rdev_err(rdev, "current operation not allowed\n");
504 return -EPERM;
507 if (*max_uA > rdev->constraints->max_uA)
508 *max_uA = rdev->constraints->max_uA;
509 if (*min_uA < rdev->constraints->min_uA)
510 *min_uA = rdev->constraints->min_uA;
512 if (*min_uA > *max_uA) {
513 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
514 *min_uA, *max_uA);
515 return -EINVAL;
518 return 0;
521 /* operating mode constraint check */
522 static int regulator_mode_constrain(struct regulator_dev *rdev,
523 unsigned int *mode)
525 switch (*mode) {
526 case REGULATOR_MODE_FAST:
527 case REGULATOR_MODE_NORMAL:
528 case REGULATOR_MODE_IDLE:
529 case REGULATOR_MODE_STANDBY:
530 break;
531 default:
532 rdev_err(rdev, "invalid mode %x specified\n", *mode);
533 return -EINVAL;
536 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_MODE)) {
537 rdev_err(rdev, "mode operation not allowed\n");
538 return -EPERM;
541 /* The modes are bitmasks, the most power hungry modes having
542 * the lowest values. If the requested mode isn't supported
543 * try higher modes. */
544 while (*mode) {
545 if (rdev->constraints->valid_modes_mask & *mode)
546 return 0;
547 *mode /= 2;
550 return -EINVAL;
553 static inline struct regulator_state *
554 regulator_get_suspend_state(struct regulator_dev *rdev, suspend_state_t state)
556 if (rdev->constraints == NULL)
557 return NULL;
559 switch (state) {
560 case PM_SUSPEND_STANDBY:
561 return &rdev->constraints->state_standby;
562 case PM_SUSPEND_MEM:
563 return &rdev->constraints->state_mem;
564 case PM_SUSPEND_MAX:
565 return &rdev->constraints->state_disk;
566 default:
567 return NULL;
571 static ssize_t regulator_uV_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
574 struct regulator_dev *rdev = dev_get_drvdata(dev);
575 ssize_t ret;
577 regulator_lock(rdev);
578 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
579 regulator_unlock(rdev);
581 return ret;
583 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
585 static ssize_t regulator_uA_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
588 struct regulator_dev *rdev = dev_get_drvdata(dev);
590 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
592 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
594 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
595 char *buf)
597 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 return sprintf(buf, "%s\n", rdev_get_name(rdev));
601 static DEVICE_ATTR_RO(name);
603 static const char *regulator_opmode_to_str(int mode)
605 switch (mode) {
606 case REGULATOR_MODE_FAST:
607 return "fast";
608 case REGULATOR_MODE_NORMAL:
609 return "normal";
610 case REGULATOR_MODE_IDLE:
611 return "idle";
612 case REGULATOR_MODE_STANDBY:
613 return "standby";
615 return "unknown";
618 static ssize_t regulator_print_opmode(char *buf, int mode)
620 return sprintf(buf, "%s\n", regulator_opmode_to_str(mode));
623 static ssize_t regulator_opmode_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
626 struct regulator_dev *rdev = dev_get_drvdata(dev);
628 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
630 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
632 static ssize_t regulator_print_state(char *buf, int state)
634 if (state > 0)
635 return sprintf(buf, "enabled\n");
636 else if (state == 0)
637 return sprintf(buf, "disabled\n");
638 else
639 return sprintf(buf, "unknown\n");
642 static ssize_t regulator_state_show(struct device *dev,
643 struct device_attribute *attr, char *buf)
645 struct regulator_dev *rdev = dev_get_drvdata(dev);
646 ssize_t ret;
648 regulator_lock(rdev);
649 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
650 regulator_unlock(rdev);
652 return ret;
654 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
656 static ssize_t regulator_status_show(struct device *dev,
657 struct device_attribute *attr, char *buf)
659 struct regulator_dev *rdev = dev_get_drvdata(dev);
660 int status;
661 char *label;
663 status = rdev->desc->ops->get_status(rdev);
664 if (status < 0)
665 return status;
667 switch (status) {
668 case REGULATOR_STATUS_OFF:
669 label = "off";
670 break;
671 case REGULATOR_STATUS_ON:
672 label = "on";
673 break;
674 case REGULATOR_STATUS_ERROR:
675 label = "error";
676 break;
677 case REGULATOR_STATUS_FAST:
678 label = "fast";
679 break;
680 case REGULATOR_STATUS_NORMAL:
681 label = "normal";
682 break;
683 case REGULATOR_STATUS_IDLE:
684 label = "idle";
685 break;
686 case REGULATOR_STATUS_STANDBY:
687 label = "standby";
688 break;
689 case REGULATOR_STATUS_BYPASS:
690 label = "bypass";
691 break;
692 case REGULATOR_STATUS_UNDEFINED:
693 label = "undefined";
694 break;
695 default:
696 return -ERANGE;
699 return sprintf(buf, "%s\n", label);
701 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
703 static ssize_t regulator_min_uA_show(struct device *dev,
704 struct device_attribute *attr, char *buf)
706 struct regulator_dev *rdev = dev_get_drvdata(dev);
708 if (!rdev->constraints)
709 return sprintf(buf, "constraint not defined\n");
711 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
713 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
715 static ssize_t regulator_max_uA_show(struct device *dev,
716 struct device_attribute *attr, char *buf)
718 struct regulator_dev *rdev = dev_get_drvdata(dev);
720 if (!rdev->constraints)
721 return sprintf(buf, "constraint not defined\n");
723 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
725 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
727 static ssize_t regulator_min_uV_show(struct device *dev,
728 struct device_attribute *attr, char *buf)
730 struct regulator_dev *rdev = dev_get_drvdata(dev);
732 if (!rdev->constraints)
733 return sprintf(buf, "constraint not defined\n");
735 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
737 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
739 static ssize_t regulator_max_uV_show(struct device *dev,
740 struct device_attribute *attr, char *buf)
742 struct regulator_dev *rdev = dev_get_drvdata(dev);
744 if (!rdev->constraints)
745 return sprintf(buf, "constraint not defined\n");
747 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
749 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
751 static ssize_t regulator_total_uA_show(struct device *dev,
752 struct device_attribute *attr, char *buf)
754 struct regulator_dev *rdev = dev_get_drvdata(dev);
755 struct regulator *regulator;
756 int uA = 0;
758 regulator_lock(rdev);
759 list_for_each_entry(regulator, &rdev->consumer_list, list) {
760 if (regulator->enable_count)
761 uA += regulator->uA_load;
763 regulator_unlock(rdev);
764 return sprintf(buf, "%d\n", uA);
766 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
768 static ssize_t num_users_show(struct device *dev, struct device_attribute *attr,
769 char *buf)
771 struct regulator_dev *rdev = dev_get_drvdata(dev);
772 return sprintf(buf, "%d\n", rdev->use_count);
774 static DEVICE_ATTR_RO(num_users);
776 static ssize_t type_show(struct device *dev, struct device_attribute *attr,
777 char *buf)
779 struct regulator_dev *rdev = dev_get_drvdata(dev);
781 switch (rdev->desc->type) {
782 case REGULATOR_VOLTAGE:
783 return sprintf(buf, "voltage\n");
784 case REGULATOR_CURRENT:
785 return sprintf(buf, "current\n");
787 return sprintf(buf, "unknown\n");
789 static DEVICE_ATTR_RO(type);
791 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
792 struct device_attribute *attr, char *buf)
794 struct regulator_dev *rdev = dev_get_drvdata(dev);
796 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
798 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
799 regulator_suspend_mem_uV_show, NULL);
801 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
802 struct device_attribute *attr, char *buf)
804 struct regulator_dev *rdev = dev_get_drvdata(dev);
806 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
808 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
809 regulator_suspend_disk_uV_show, NULL);
811 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
812 struct device_attribute *attr, char *buf)
814 struct regulator_dev *rdev = dev_get_drvdata(dev);
816 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
818 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
819 regulator_suspend_standby_uV_show, NULL);
821 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
824 struct regulator_dev *rdev = dev_get_drvdata(dev);
826 return regulator_print_opmode(buf,
827 rdev->constraints->state_mem.mode);
829 static DEVICE_ATTR(suspend_mem_mode, 0444,
830 regulator_suspend_mem_mode_show, NULL);
832 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
833 struct device_attribute *attr, char *buf)
835 struct regulator_dev *rdev = dev_get_drvdata(dev);
837 return regulator_print_opmode(buf,
838 rdev->constraints->state_disk.mode);
840 static DEVICE_ATTR(suspend_disk_mode, 0444,
841 regulator_suspend_disk_mode_show, NULL);
843 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
844 struct device_attribute *attr, char *buf)
846 struct regulator_dev *rdev = dev_get_drvdata(dev);
848 return regulator_print_opmode(buf,
849 rdev->constraints->state_standby.mode);
851 static DEVICE_ATTR(suspend_standby_mode, 0444,
852 regulator_suspend_standby_mode_show, NULL);
854 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
855 struct device_attribute *attr, char *buf)
857 struct regulator_dev *rdev = dev_get_drvdata(dev);
859 return regulator_print_state(buf,
860 rdev->constraints->state_mem.enabled);
862 static DEVICE_ATTR(suspend_mem_state, 0444,
863 regulator_suspend_mem_state_show, NULL);
865 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
866 struct device_attribute *attr, char *buf)
868 struct regulator_dev *rdev = dev_get_drvdata(dev);
870 return regulator_print_state(buf,
871 rdev->constraints->state_disk.enabled);
873 static DEVICE_ATTR(suspend_disk_state, 0444,
874 regulator_suspend_disk_state_show, NULL);
876 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
877 struct device_attribute *attr, char *buf)
879 struct regulator_dev *rdev = dev_get_drvdata(dev);
881 return regulator_print_state(buf,
882 rdev->constraints->state_standby.enabled);
884 static DEVICE_ATTR(suspend_standby_state, 0444,
885 regulator_suspend_standby_state_show, NULL);
887 static ssize_t regulator_bypass_show(struct device *dev,
888 struct device_attribute *attr, char *buf)
890 struct regulator_dev *rdev = dev_get_drvdata(dev);
891 const char *report;
892 bool bypass;
893 int ret;
895 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
897 if (ret != 0)
898 report = "unknown";
899 else if (bypass)
900 report = "enabled";
901 else
902 report = "disabled";
904 return sprintf(buf, "%s\n", report);
906 static DEVICE_ATTR(bypass, 0444,
907 regulator_bypass_show, NULL);
909 /* Calculate the new optimum regulator operating mode based on the new total
910 * consumer load. All locks held by caller */
911 static int drms_uA_update(struct regulator_dev *rdev)
913 struct regulator *sibling;
914 int current_uA = 0, output_uV, input_uV, err;
915 unsigned int mode;
918 * first check to see if we can set modes at all, otherwise just
919 * tell the consumer everything is OK.
921 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_DRMS)) {
922 rdev_dbg(rdev, "DRMS operation not allowed\n");
923 return 0;
926 if (!rdev->desc->ops->get_optimum_mode &&
927 !rdev->desc->ops->set_load)
928 return 0;
930 if (!rdev->desc->ops->set_mode &&
931 !rdev->desc->ops->set_load)
932 return -EINVAL;
934 /* calc total requested load */
935 list_for_each_entry(sibling, &rdev->consumer_list, list) {
936 if (sibling->enable_count)
937 current_uA += sibling->uA_load;
940 current_uA += rdev->constraints->system_load;
942 if (rdev->desc->ops->set_load) {
943 /* set the optimum mode for our new total regulator load */
944 err = rdev->desc->ops->set_load(rdev, current_uA);
945 if (err < 0)
946 rdev_err(rdev, "failed to set load %d\n", current_uA);
947 } else {
948 /* get output voltage */
949 output_uV = _regulator_get_voltage(rdev);
950 if (output_uV <= 0) {
951 rdev_err(rdev, "invalid output voltage found\n");
952 return -EINVAL;
955 /* get input voltage */
956 input_uV = 0;
957 if (rdev->supply)
958 input_uV = regulator_get_voltage(rdev->supply);
959 if (input_uV <= 0)
960 input_uV = rdev->constraints->input_uV;
961 if (input_uV <= 0) {
962 rdev_err(rdev, "invalid input voltage found\n");
963 return -EINVAL;
966 /* now get the optimum mode for our new total regulator load */
967 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
968 output_uV, current_uA);
970 /* check the new mode is allowed */
971 err = regulator_mode_constrain(rdev, &mode);
972 if (err < 0) {
973 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
974 current_uA, input_uV, output_uV);
975 return err;
978 err = rdev->desc->ops->set_mode(rdev, mode);
979 if (err < 0)
980 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
983 return err;
986 static int suspend_set_state(struct regulator_dev *rdev,
987 suspend_state_t state)
989 int ret = 0;
990 struct regulator_state *rstate;
992 rstate = regulator_get_suspend_state(rdev, state);
993 if (rstate == NULL)
994 return 0;
996 /* If we have no suspend mode configuration don't set anything;
997 * only warn if the driver implements set_suspend_voltage or
998 * set_suspend_mode callback.
1000 if (rstate->enabled != ENABLE_IN_SUSPEND &&
1001 rstate->enabled != DISABLE_IN_SUSPEND) {
1002 if (rdev->desc->ops->set_suspend_voltage ||
1003 rdev->desc->ops->set_suspend_mode)
1004 rdev_warn(rdev, "No configuration\n");
1005 return 0;
1008 if (rstate->enabled == ENABLE_IN_SUSPEND &&
1009 rdev->desc->ops->set_suspend_enable)
1010 ret = rdev->desc->ops->set_suspend_enable(rdev);
1011 else if (rstate->enabled == DISABLE_IN_SUSPEND &&
1012 rdev->desc->ops->set_suspend_disable)
1013 ret = rdev->desc->ops->set_suspend_disable(rdev);
1014 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
1015 ret = 0;
1017 if (ret < 0) {
1018 rdev_err(rdev, "failed to enabled/disable\n");
1019 return ret;
1022 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
1023 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
1024 if (ret < 0) {
1025 rdev_err(rdev, "failed to set voltage\n");
1026 return ret;
1030 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
1031 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
1032 if (ret < 0) {
1033 rdev_err(rdev, "failed to set mode\n");
1034 return ret;
1038 return ret;
1041 static void print_constraints(struct regulator_dev *rdev)
1043 struct regulation_constraints *constraints = rdev->constraints;
1044 char buf[160] = "";
1045 size_t len = sizeof(buf) - 1;
1046 int count = 0;
1047 int ret;
1049 if (constraints->min_uV && constraints->max_uV) {
1050 if (constraints->min_uV == constraints->max_uV)
1051 count += scnprintf(buf + count, len - count, "%d mV ",
1052 constraints->min_uV / 1000);
1053 else
1054 count += scnprintf(buf + count, len - count,
1055 "%d <--> %d mV ",
1056 constraints->min_uV / 1000,
1057 constraints->max_uV / 1000);
1060 if (!constraints->min_uV ||
1061 constraints->min_uV != constraints->max_uV) {
1062 ret = _regulator_get_voltage(rdev);
1063 if (ret > 0)
1064 count += scnprintf(buf + count, len - count,
1065 "at %d mV ", ret / 1000);
1068 if (constraints->uV_offset)
1069 count += scnprintf(buf + count, len - count, "%dmV offset ",
1070 constraints->uV_offset / 1000);
1072 if (constraints->min_uA && constraints->max_uA) {
1073 if (constraints->min_uA == constraints->max_uA)
1074 count += scnprintf(buf + count, len - count, "%d mA ",
1075 constraints->min_uA / 1000);
1076 else
1077 count += scnprintf(buf + count, len - count,
1078 "%d <--> %d mA ",
1079 constraints->min_uA / 1000,
1080 constraints->max_uA / 1000);
1083 if (!constraints->min_uA ||
1084 constraints->min_uA != constraints->max_uA) {
1085 ret = _regulator_get_current_limit(rdev);
1086 if (ret > 0)
1087 count += scnprintf(buf + count, len - count,
1088 "at %d mA ", ret / 1000);
1091 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
1092 count += scnprintf(buf + count, len - count, "fast ");
1093 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
1094 count += scnprintf(buf + count, len - count, "normal ");
1095 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
1096 count += scnprintf(buf + count, len - count, "idle ");
1097 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
1098 count += scnprintf(buf + count, len - count, "standby");
1100 if (!count)
1101 scnprintf(buf, len, "no parameters");
1103 rdev_dbg(rdev, "%s\n", buf);
1105 if ((constraints->min_uV != constraints->max_uV) &&
1106 !regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE))
1107 rdev_warn(rdev,
1108 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
1111 static int machine_constraints_voltage(struct regulator_dev *rdev,
1112 struct regulation_constraints *constraints)
1114 const struct regulator_ops *ops = rdev->desc->ops;
1115 int ret;
1117 /* do we need to apply the constraint voltage */
1118 if (rdev->constraints->apply_uV &&
1119 rdev->constraints->min_uV && rdev->constraints->max_uV) {
1120 int target_min, target_max;
1121 int current_uV = _regulator_get_voltage(rdev);
1123 if (current_uV == -ENOTRECOVERABLE) {
1124 /* This regulator can't be read and must be initialized */
1125 rdev_info(rdev, "Setting %d-%duV\n",
1126 rdev->constraints->min_uV,
1127 rdev->constraints->max_uV);
1128 _regulator_do_set_voltage(rdev,
1129 rdev->constraints->min_uV,
1130 rdev->constraints->max_uV);
1131 current_uV = _regulator_get_voltage(rdev);
1134 if (current_uV < 0) {
1135 rdev_err(rdev,
1136 "failed to get the current voltage(%d)\n",
1137 current_uV);
1138 return current_uV;
1142 * If we're below the minimum voltage move up to the
1143 * minimum voltage, if we're above the maximum voltage
1144 * then move down to the maximum.
1146 target_min = current_uV;
1147 target_max = current_uV;
1149 if (current_uV < rdev->constraints->min_uV) {
1150 target_min = rdev->constraints->min_uV;
1151 target_max = rdev->constraints->min_uV;
1154 if (current_uV > rdev->constraints->max_uV) {
1155 target_min = rdev->constraints->max_uV;
1156 target_max = rdev->constraints->max_uV;
1159 if (target_min != current_uV || target_max != current_uV) {
1160 rdev_info(rdev, "Bringing %duV into %d-%duV\n",
1161 current_uV, target_min, target_max);
1162 ret = _regulator_do_set_voltage(
1163 rdev, target_min, target_max);
1164 if (ret < 0) {
1165 rdev_err(rdev,
1166 "failed to apply %d-%duV constraint(%d)\n",
1167 target_min, target_max, ret);
1168 return ret;
1173 /* constrain machine-level voltage specs to fit
1174 * the actual range supported by this regulator.
1176 if (ops->list_voltage && rdev->desc->n_voltages) {
1177 int count = rdev->desc->n_voltages;
1178 int i;
1179 int min_uV = INT_MAX;
1180 int max_uV = INT_MIN;
1181 int cmin = constraints->min_uV;
1182 int cmax = constraints->max_uV;
1184 /* it's safe to autoconfigure fixed-voltage supplies
1185 and the constraints are used by list_voltage. */
1186 if (count == 1 && !cmin) {
1187 cmin = 1;
1188 cmax = INT_MAX;
1189 constraints->min_uV = cmin;
1190 constraints->max_uV = cmax;
1193 /* voltage constraints are optional */
1194 if ((cmin == 0) && (cmax == 0))
1195 return 0;
1197 /* else require explicit machine-level constraints */
1198 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
1199 rdev_err(rdev, "invalid voltage constraints\n");
1200 return -EINVAL;
1203 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
1204 for (i = 0; i < count; i++) {
1205 int value;
1207 value = ops->list_voltage(rdev, i);
1208 if (value <= 0)
1209 continue;
1211 /* maybe adjust [min_uV..max_uV] */
1212 if (value >= cmin && value < min_uV)
1213 min_uV = value;
1214 if (value <= cmax && value > max_uV)
1215 max_uV = value;
1218 /* final: [min_uV..max_uV] valid iff constraints valid */
1219 if (max_uV < min_uV) {
1220 rdev_err(rdev,
1221 "unsupportable voltage constraints %u-%uuV\n",
1222 min_uV, max_uV);
1223 return -EINVAL;
1226 /* use regulator's subset of machine constraints */
1227 if (constraints->min_uV < min_uV) {
1228 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
1229 constraints->min_uV, min_uV);
1230 constraints->min_uV = min_uV;
1232 if (constraints->max_uV > max_uV) {
1233 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
1234 constraints->max_uV, max_uV);
1235 constraints->max_uV = max_uV;
1239 return 0;
1242 static int machine_constraints_current(struct regulator_dev *rdev,
1243 struct regulation_constraints *constraints)
1245 const struct regulator_ops *ops = rdev->desc->ops;
1246 int ret;
1248 if (!constraints->min_uA && !constraints->max_uA)
1249 return 0;
1251 if (constraints->min_uA > constraints->max_uA) {
1252 rdev_err(rdev, "Invalid current constraints\n");
1253 return -EINVAL;
1256 if (!ops->set_current_limit || !ops->get_current_limit) {
1257 rdev_warn(rdev, "Operation of current configuration missing\n");
1258 return 0;
1261 /* Set regulator current in constraints range */
1262 ret = ops->set_current_limit(rdev, constraints->min_uA,
1263 constraints->max_uA);
1264 if (ret < 0) {
1265 rdev_err(rdev, "Failed to set current constraint, %d\n", ret);
1266 return ret;
1269 return 0;
1272 static int _regulator_do_enable(struct regulator_dev *rdev);
1275 * set_machine_constraints - sets regulator constraints
1276 * @rdev: regulator source
1277 * @constraints: constraints to apply
1279 * Allows platform initialisation code to define and constrain
1280 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
1281 * Constraints *must* be set by platform code in order for some
1282 * regulator operations to proceed i.e. set_voltage, set_current_limit,
1283 * set_mode.
1285 static int set_machine_constraints(struct regulator_dev *rdev,
1286 const struct regulation_constraints *constraints)
1288 int ret = 0;
1289 const struct regulator_ops *ops = rdev->desc->ops;
1291 if (constraints)
1292 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
1293 GFP_KERNEL);
1294 else
1295 rdev->constraints = kzalloc(sizeof(*constraints),
1296 GFP_KERNEL);
1297 if (!rdev->constraints)
1298 return -ENOMEM;
1300 ret = machine_constraints_voltage(rdev, rdev->constraints);
1301 if (ret != 0)
1302 return ret;
1304 ret = machine_constraints_current(rdev, rdev->constraints);
1305 if (ret != 0)
1306 return ret;
1308 if (rdev->constraints->ilim_uA && ops->set_input_current_limit) {
1309 ret = ops->set_input_current_limit(rdev,
1310 rdev->constraints->ilim_uA);
1311 if (ret < 0) {
1312 rdev_err(rdev, "failed to set input limit\n");
1313 return ret;
1317 /* do we need to setup our suspend state */
1318 if (rdev->constraints->initial_state) {
1319 ret = suspend_set_state(rdev, rdev->constraints->initial_state);
1320 if (ret < 0) {
1321 rdev_err(rdev, "failed to set suspend state\n");
1322 return ret;
1326 if (rdev->constraints->initial_mode) {
1327 if (!ops->set_mode) {
1328 rdev_err(rdev, "no set_mode operation\n");
1329 return -EINVAL;
1332 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
1333 if (ret < 0) {
1334 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
1335 return ret;
1337 } else if (rdev->constraints->system_load) {
1339 * We'll only apply the initial system load if an
1340 * initial mode wasn't specified.
1342 regulator_lock(rdev);
1343 drms_uA_update(rdev);
1344 regulator_unlock(rdev);
1347 if ((rdev->constraints->ramp_delay || rdev->constraints->ramp_disable)
1348 && ops->set_ramp_delay) {
1349 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
1350 if (ret < 0) {
1351 rdev_err(rdev, "failed to set ramp_delay\n");
1352 return ret;
1356 if (rdev->constraints->pull_down && ops->set_pull_down) {
1357 ret = ops->set_pull_down(rdev);
1358 if (ret < 0) {
1359 rdev_err(rdev, "failed to set pull down\n");
1360 return ret;
1364 if (rdev->constraints->soft_start && ops->set_soft_start) {
1365 ret = ops->set_soft_start(rdev);
1366 if (ret < 0) {
1367 rdev_err(rdev, "failed to set soft start\n");
1368 return ret;
1372 if (rdev->constraints->over_current_protection
1373 && ops->set_over_current_protection) {
1374 ret = ops->set_over_current_protection(rdev);
1375 if (ret < 0) {
1376 rdev_err(rdev, "failed to set over current protection\n");
1377 return ret;
1381 if (rdev->constraints->active_discharge && ops->set_active_discharge) {
1382 bool ad_state = (rdev->constraints->active_discharge ==
1383 REGULATOR_ACTIVE_DISCHARGE_ENABLE) ? true : false;
1385 ret = ops->set_active_discharge(rdev, ad_state);
1386 if (ret < 0) {
1387 rdev_err(rdev, "failed to set active discharge\n");
1388 return ret;
1392 /* If the constraints say the regulator should be on at this point
1393 * and we have control then make sure it is enabled.
1395 if (rdev->constraints->always_on || rdev->constraints->boot_on) {
1396 if (rdev->supply) {
1397 ret = regulator_enable(rdev->supply);
1398 if (ret < 0) {
1399 _regulator_put(rdev->supply);
1400 rdev->supply = NULL;
1401 return ret;
1405 ret = _regulator_do_enable(rdev);
1406 if (ret < 0 && ret != -EINVAL) {
1407 rdev_err(rdev, "failed to enable\n");
1408 return ret;
1410 rdev->use_count++;
1413 print_constraints(rdev);
1414 return 0;
1418 * set_supply - set regulator supply regulator
1419 * @rdev: regulator name
1420 * @supply_rdev: supply regulator name
1422 * Called by platform initialisation code to set the supply regulator for this
1423 * regulator. This ensures that a regulators supply will also be enabled by the
1424 * core if it's child is enabled.
1426 static int set_supply(struct regulator_dev *rdev,
1427 struct regulator_dev *supply_rdev)
1429 int err;
1431 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1433 if (!try_module_get(supply_rdev->owner))
1434 return -ENODEV;
1436 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1437 if (rdev->supply == NULL) {
1438 err = -ENOMEM;
1439 return err;
1441 supply_rdev->open_count++;
1443 return 0;
1447 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1448 * @rdev: regulator source
1449 * @consumer_dev_name: dev_name() string for device supply applies to
1450 * @supply: symbolic name for supply
1452 * Allows platform initialisation code to map physical regulator
1453 * sources to symbolic names for supplies for use by devices. Devices
1454 * should use these symbolic names to request regulators, avoiding the
1455 * need to provide board-specific regulator names as platform data.
1457 static int set_consumer_device_supply(struct regulator_dev *rdev,
1458 const char *consumer_dev_name,
1459 const char *supply)
1461 struct regulator_map *node;
1462 int has_dev;
1464 if (supply == NULL)
1465 return -EINVAL;
1467 if (consumer_dev_name != NULL)
1468 has_dev = 1;
1469 else
1470 has_dev = 0;
1472 list_for_each_entry(node, &regulator_map_list, list) {
1473 if (node->dev_name && consumer_dev_name) {
1474 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1475 continue;
1476 } else if (node->dev_name || consumer_dev_name) {
1477 continue;
1480 if (strcmp(node->supply, supply) != 0)
1481 continue;
1483 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1484 consumer_dev_name,
1485 dev_name(&node->regulator->dev),
1486 node->regulator->desc->name,
1487 supply,
1488 dev_name(&rdev->dev), rdev_get_name(rdev));
1489 return -EBUSY;
1492 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1493 if (node == NULL)
1494 return -ENOMEM;
1496 node->regulator = rdev;
1497 node->supply = supply;
1499 if (has_dev) {
1500 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1501 if (node->dev_name == NULL) {
1502 kfree(node);
1503 return -ENOMEM;
1507 list_add(&node->list, &regulator_map_list);
1508 return 0;
1511 static void unset_regulator_supplies(struct regulator_dev *rdev)
1513 struct regulator_map *node, *n;
1515 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1516 if (rdev == node->regulator) {
1517 list_del(&node->list);
1518 kfree(node->dev_name);
1519 kfree(node);
1524 #ifdef CONFIG_DEBUG_FS
1525 static ssize_t constraint_flags_read_file(struct file *file,
1526 char __user *user_buf,
1527 size_t count, loff_t *ppos)
1529 const struct regulator *regulator = file->private_data;
1530 const struct regulation_constraints *c = regulator->rdev->constraints;
1531 char *buf;
1532 ssize_t ret;
1534 if (!c)
1535 return 0;
1537 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1538 if (!buf)
1539 return -ENOMEM;
1541 ret = snprintf(buf, PAGE_SIZE,
1542 "always_on: %u\n"
1543 "boot_on: %u\n"
1544 "apply_uV: %u\n"
1545 "ramp_disable: %u\n"
1546 "soft_start: %u\n"
1547 "pull_down: %u\n"
1548 "over_current_protection: %u\n",
1549 c->always_on,
1550 c->boot_on,
1551 c->apply_uV,
1552 c->ramp_disable,
1553 c->soft_start,
1554 c->pull_down,
1555 c->over_current_protection);
1557 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
1558 kfree(buf);
1560 return ret;
1563 #endif
1565 static const struct file_operations constraint_flags_fops = {
1566 #ifdef CONFIG_DEBUG_FS
1567 .open = simple_open,
1568 .read = constraint_flags_read_file,
1569 .llseek = default_llseek,
1570 #endif
1573 #define REG_STR_SIZE 64
1575 static struct regulator *create_regulator(struct regulator_dev *rdev,
1576 struct device *dev,
1577 const char *supply_name)
1579 struct regulator *regulator;
1580 char buf[REG_STR_SIZE];
1581 int err, size;
1583 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1584 if (regulator == NULL)
1585 return NULL;
1587 regulator_lock(rdev);
1588 regulator->rdev = rdev;
1589 list_add(&regulator->list, &rdev->consumer_list);
1591 if (dev) {
1592 regulator->dev = dev;
1594 /* Add a link to the device sysfs entry */
1595 size = snprintf(buf, REG_STR_SIZE, "%s-%s",
1596 dev->kobj.name, supply_name);
1597 if (size >= REG_STR_SIZE)
1598 goto overflow_err;
1600 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1601 if (regulator->supply_name == NULL)
1602 goto overflow_err;
1604 err = sysfs_create_link_nowarn(&rdev->dev.kobj, &dev->kobj,
1605 buf);
1606 if (err) {
1607 rdev_dbg(rdev, "could not add device link %s err %d\n",
1608 dev->kobj.name, err);
1609 /* non-fatal */
1611 } else {
1612 regulator->supply_name = kstrdup_const(supply_name, GFP_KERNEL);
1613 if (regulator->supply_name == NULL)
1614 goto overflow_err;
1617 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1618 rdev->debugfs);
1619 if (!regulator->debugfs) {
1620 rdev_dbg(rdev, "Failed to create debugfs directory\n");
1621 } else {
1622 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1623 &regulator->uA_load);
1624 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1625 &regulator->voltage[PM_SUSPEND_ON].min_uV);
1626 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1627 &regulator->voltage[PM_SUSPEND_ON].max_uV);
1628 debugfs_create_file("constraint_flags", 0444,
1629 regulator->debugfs, regulator,
1630 &constraint_flags_fops);
1634 * Check now if the regulator is an always on regulator - if
1635 * it is then we don't need to do nearly so much work for
1636 * enable/disable calls.
1638 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS) &&
1639 _regulator_is_enabled(rdev))
1640 regulator->always_on = true;
1642 regulator_unlock(rdev);
1643 return regulator;
1644 overflow_err:
1645 list_del(&regulator->list);
1646 kfree(regulator);
1647 regulator_unlock(rdev);
1648 return NULL;
1651 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1653 if (rdev->constraints && rdev->constraints->enable_time)
1654 return rdev->constraints->enable_time;
1655 if (!rdev->desc->ops->enable_time)
1656 return rdev->desc->enable_time;
1657 return rdev->desc->ops->enable_time(rdev);
1660 static struct regulator_supply_alias *regulator_find_supply_alias(
1661 struct device *dev, const char *supply)
1663 struct regulator_supply_alias *map;
1665 list_for_each_entry(map, &regulator_supply_alias_list, list)
1666 if (map->src_dev == dev && strcmp(map->src_supply, supply) == 0)
1667 return map;
1669 return NULL;
1672 static void regulator_supply_alias(struct device **dev, const char **supply)
1674 struct regulator_supply_alias *map;
1676 map = regulator_find_supply_alias(*dev, *supply);
1677 if (map) {
1678 dev_dbg(*dev, "Mapping supply %s to %s,%s\n",
1679 *supply, map->alias_supply,
1680 dev_name(map->alias_dev));
1681 *dev = map->alias_dev;
1682 *supply = map->alias_supply;
1686 static int regulator_match(struct device *dev, const void *data)
1688 struct regulator_dev *r = dev_to_rdev(dev);
1690 return strcmp(rdev_get_name(r), data) == 0;
1693 static struct regulator_dev *regulator_lookup_by_name(const char *name)
1695 struct device *dev;
1697 dev = class_find_device(&regulator_class, NULL, name, regulator_match);
1699 return dev ? dev_to_rdev(dev) : NULL;
1703 * regulator_dev_lookup - lookup a regulator device.
1704 * @dev: device for regulator "consumer".
1705 * @supply: Supply name or regulator ID.
1707 * If successful, returns a struct regulator_dev that corresponds to the name
1708 * @supply and with the embedded struct device refcount incremented by one.
1709 * The refcount must be dropped by calling put_device().
1710 * On failure one of the following ERR-PTR-encoded values is returned:
1711 * -ENODEV if lookup fails permanently, -EPROBE_DEFER if lookup could succeed
1712 * in the future.
1714 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1715 const char *supply)
1717 struct regulator_dev *r = NULL;
1718 struct device_node *node;
1719 struct regulator_map *map;
1720 const char *devname = NULL;
1722 regulator_supply_alias(&dev, &supply);
1724 /* first do a dt based lookup */
1725 if (dev && dev->of_node) {
1726 node = of_get_regulator(dev, supply);
1727 if (node) {
1728 r = of_find_regulator_by_node(node);
1729 if (r)
1730 return r;
1733 * We have a node, but there is no device.
1734 * assume it has not registered yet.
1736 return ERR_PTR(-EPROBE_DEFER);
1740 /* if not found, try doing it non-dt way */
1741 if (dev)
1742 devname = dev_name(dev);
1744 mutex_lock(&regulator_list_mutex);
1745 list_for_each_entry(map, &regulator_map_list, list) {
1746 /* If the mapping has a device set up it must match */
1747 if (map->dev_name &&
1748 (!devname || strcmp(map->dev_name, devname)))
1749 continue;
1751 if (strcmp(map->supply, supply) == 0 &&
1752 get_device(&map->regulator->dev)) {
1753 r = map->regulator;
1754 break;
1757 mutex_unlock(&regulator_list_mutex);
1759 if (r)
1760 return r;
1762 r = regulator_lookup_by_name(supply);
1763 if (r)
1764 return r;
1766 return ERR_PTR(-ENODEV);
1769 static int regulator_resolve_supply(struct regulator_dev *rdev)
1771 struct regulator_dev *r;
1772 struct device *dev = rdev->dev.parent;
1773 int ret;
1775 /* No supply to resolve? */
1776 if (!rdev->supply_name)
1777 return 0;
1779 /* Supply already resolved? */
1780 if (rdev->supply)
1781 return 0;
1783 r = regulator_dev_lookup(dev, rdev->supply_name);
1784 if (IS_ERR(r)) {
1785 ret = PTR_ERR(r);
1787 /* Did the lookup explicitly defer for us? */
1788 if (ret == -EPROBE_DEFER)
1789 return ret;
1791 if (have_full_constraints()) {
1792 r = dummy_regulator_rdev;
1793 get_device(&r->dev);
1794 } else {
1795 dev_err(dev, "Failed to resolve %s-supply for %s\n",
1796 rdev->supply_name, rdev->desc->name);
1797 return -EPROBE_DEFER;
1802 * If the supply's parent device is not the same as the
1803 * regulator's parent device, then ensure the parent device
1804 * is bound before we resolve the supply, in case the parent
1805 * device get probe deferred and unregisters the supply.
1807 if (r->dev.parent && r->dev.parent != rdev->dev.parent) {
1808 if (!device_is_bound(r->dev.parent)) {
1809 put_device(&r->dev);
1810 return -EPROBE_DEFER;
1814 /* Recursively resolve the supply of the supply */
1815 ret = regulator_resolve_supply(r);
1816 if (ret < 0) {
1817 put_device(&r->dev);
1818 return ret;
1821 ret = set_supply(rdev, r);
1822 if (ret < 0) {
1823 put_device(&r->dev);
1824 return ret;
1828 * In set_machine_constraints() we may have turned this regulator on
1829 * but we couldn't propagate to the supply if it hadn't been resolved
1830 * yet. Do it now.
1832 if (rdev->use_count) {
1833 ret = regulator_enable(rdev->supply);
1834 if (ret < 0) {
1835 _regulator_put(rdev->supply);
1836 rdev->supply = NULL;
1837 return ret;
1841 return 0;
1844 /* Internal regulator request function */
1845 struct regulator *_regulator_get(struct device *dev, const char *id,
1846 enum regulator_get_type get_type)
1848 struct regulator_dev *rdev;
1849 struct regulator *regulator;
1850 const char *devname = dev ? dev_name(dev) : "deviceless";
1851 int ret;
1853 if (get_type >= MAX_GET_TYPE) {
1854 dev_err(dev, "invalid type %d in %s\n", get_type, __func__);
1855 return ERR_PTR(-EINVAL);
1858 if (id == NULL) {
1859 pr_err("get() with no identifier\n");
1860 return ERR_PTR(-EINVAL);
1863 rdev = regulator_dev_lookup(dev, id);
1864 if (IS_ERR(rdev)) {
1865 ret = PTR_ERR(rdev);
1868 * If regulator_dev_lookup() fails with error other
1869 * than -ENODEV our job here is done, we simply return it.
1871 if (ret != -ENODEV)
1872 return ERR_PTR(ret);
1874 if (!have_full_constraints()) {
1875 dev_warn(dev,
1876 "incomplete constraints, dummy supplies not allowed\n");
1877 return ERR_PTR(-ENODEV);
1880 switch (get_type) {
1881 case NORMAL_GET:
1883 * Assume that a regulator is physically present and
1884 * enabled, even if it isn't hooked up, and just
1885 * provide a dummy.
1887 dev_warn(dev,
1888 "%s supply %s not found, using dummy regulator\n",
1889 devname, id);
1890 rdev = dummy_regulator_rdev;
1891 get_device(&rdev->dev);
1892 break;
1894 case EXCLUSIVE_GET:
1895 dev_warn(dev,
1896 "dummy supplies not allowed for exclusive requests\n");
1897 /* fall through */
1899 default:
1900 return ERR_PTR(-ENODEV);
1904 if (rdev->exclusive) {
1905 regulator = ERR_PTR(-EPERM);
1906 put_device(&rdev->dev);
1907 return regulator;
1910 if (get_type == EXCLUSIVE_GET && rdev->open_count) {
1911 regulator = ERR_PTR(-EBUSY);
1912 put_device(&rdev->dev);
1913 return regulator;
1916 mutex_lock(&regulator_list_mutex);
1917 ret = (rdev->coupling_desc.n_resolved != rdev->coupling_desc.n_coupled);
1918 mutex_unlock(&regulator_list_mutex);
1920 if (ret != 0) {
1921 regulator = ERR_PTR(-EPROBE_DEFER);
1922 put_device(&rdev->dev);
1923 return regulator;
1926 ret = regulator_resolve_supply(rdev);
1927 if (ret < 0) {
1928 regulator = ERR_PTR(ret);
1929 put_device(&rdev->dev);
1930 return regulator;
1933 if (!try_module_get(rdev->owner)) {
1934 regulator = ERR_PTR(-EPROBE_DEFER);
1935 put_device(&rdev->dev);
1936 return regulator;
1939 regulator = create_regulator(rdev, dev, id);
1940 if (regulator == NULL) {
1941 regulator = ERR_PTR(-ENOMEM);
1942 put_device(&rdev->dev);
1943 module_put(rdev->owner);
1944 return regulator;
1947 rdev->open_count++;
1948 if (get_type == EXCLUSIVE_GET) {
1949 rdev->exclusive = 1;
1951 ret = _regulator_is_enabled(rdev);
1952 if (ret > 0)
1953 rdev->use_count = 1;
1954 else
1955 rdev->use_count = 0;
1958 device_link_add(dev, &rdev->dev, DL_FLAG_STATELESS);
1960 return regulator;
1964 * regulator_get - lookup and obtain a reference to a regulator.
1965 * @dev: device for regulator "consumer"
1966 * @id: Supply name or regulator ID.
1968 * Returns a struct regulator corresponding to the regulator producer,
1969 * or IS_ERR() condition containing errno.
1971 * Use of supply names configured via regulator_set_device_supply() is
1972 * strongly encouraged. It is recommended that the supply name used
1973 * should match the name used for the supply and/or the relevant
1974 * device pins in the datasheet.
1976 struct regulator *regulator_get(struct device *dev, const char *id)
1978 return _regulator_get(dev, id, NORMAL_GET);
1980 EXPORT_SYMBOL_GPL(regulator_get);
1983 * regulator_get_exclusive - obtain exclusive access to a regulator.
1984 * @dev: device for regulator "consumer"
1985 * @id: Supply name or regulator ID.
1987 * Returns a struct regulator corresponding to the regulator producer,
1988 * or IS_ERR() condition containing errno. Other consumers will be
1989 * unable to obtain this regulator while this reference is held and the
1990 * use count for the regulator will be initialised to reflect the current
1991 * state of the regulator.
1993 * This is intended for use by consumers which cannot tolerate shared
1994 * use of the regulator such as those which need to force the
1995 * regulator off for correct operation of the hardware they are
1996 * controlling.
1998 * Use of supply names configured via regulator_set_device_supply() is
1999 * strongly encouraged. It is recommended that the supply name used
2000 * should match the name used for the supply and/or the relevant
2001 * device pins in the datasheet.
2003 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
2005 return _regulator_get(dev, id, EXCLUSIVE_GET);
2007 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
2010 * regulator_get_optional - obtain optional access to a regulator.
2011 * @dev: device for regulator "consumer"
2012 * @id: Supply name or regulator ID.
2014 * Returns a struct regulator corresponding to the regulator producer,
2015 * or IS_ERR() condition containing errno.
2017 * This is intended for use by consumers for devices which can have
2018 * some supplies unconnected in normal use, such as some MMC devices.
2019 * It can allow the regulator core to provide stub supplies for other
2020 * supplies requested using normal regulator_get() calls without
2021 * disrupting the operation of drivers that can handle absent
2022 * supplies.
2024 * Use of supply names configured via regulator_set_device_supply() is
2025 * strongly encouraged. It is recommended that the supply name used
2026 * should match the name used for the supply and/or the relevant
2027 * device pins in the datasheet.
2029 struct regulator *regulator_get_optional(struct device *dev, const char *id)
2031 return _regulator_get(dev, id, OPTIONAL_GET);
2033 EXPORT_SYMBOL_GPL(regulator_get_optional);
2035 /* regulator_list_mutex lock held by regulator_put() */
2036 static void _regulator_put(struct regulator *regulator)
2038 struct regulator_dev *rdev;
2040 if (IS_ERR_OR_NULL(regulator))
2041 return;
2043 lockdep_assert_held_once(&regulator_list_mutex);
2045 /* Docs say you must disable before calling regulator_put() */
2046 WARN_ON(regulator->enable_count);
2048 rdev = regulator->rdev;
2050 debugfs_remove_recursive(regulator->debugfs);
2052 if (regulator->dev) {
2053 device_link_remove(regulator->dev, &rdev->dev);
2055 /* remove any sysfs entries */
2056 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
2059 regulator_lock(rdev);
2060 list_del(&regulator->list);
2062 rdev->open_count--;
2063 rdev->exclusive = 0;
2064 put_device(&rdev->dev);
2065 regulator_unlock(rdev);
2067 kfree_const(regulator->supply_name);
2068 kfree(regulator);
2070 module_put(rdev->owner);
2074 * regulator_put - "free" the regulator source
2075 * @regulator: regulator source
2077 * Note: drivers must ensure that all regulator_enable calls made on this
2078 * regulator source are balanced by regulator_disable calls prior to calling
2079 * this function.
2081 void regulator_put(struct regulator *regulator)
2083 mutex_lock(&regulator_list_mutex);
2084 _regulator_put(regulator);
2085 mutex_unlock(&regulator_list_mutex);
2087 EXPORT_SYMBOL_GPL(regulator_put);
2090 * regulator_register_supply_alias - Provide device alias for supply lookup
2092 * @dev: device that will be given as the regulator "consumer"
2093 * @id: Supply name or regulator ID
2094 * @alias_dev: device that should be used to lookup the supply
2095 * @alias_id: Supply name or regulator ID that should be used to lookup the
2096 * supply
2098 * All lookups for id on dev will instead be conducted for alias_id on
2099 * alias_dev.
2101 int regulator_register_supply_alias(struct device *dev, const char *id,
2102 struct device *alias_dev,
2103 const char *alias_id)
2105 struct regulator_supply_alias *map;
2107 map = regulator_find_supply_alias(dev, id);
2108 if (map)
2109 return -EEXIST;
2111 map = kzalloc(sizeof(struct regulator_supply_alias), GFP_KERNEL);
2112 if (!map)
2113 return -ENOMEM;
2115 map->src_dev = dev;
2116 map->src_supply = id;
2117 map->alias_dev = alias_dev;
2118 map->alias_supply = alias_id;
2120 list_add(&map->list, &regulator_supply_alias_list);
2122 pr_info("Adding alias for supply %s,%s -> %s,%s\n",
2123 id, dev_name(dev), alias_id, dev_name(alias_dev));
2125 return 0;
2127 EXPORT_SYMBOL_GPL(regulator_register_supply_alias);
2130 * regulator_unregister_supply_alias - Remove device alias
2132 * @dev: device that will be given as the regulator "consumer"
2133 * @id: Supply name or regulator ID
2135 * Remove a lookup alias if one exists for id on dev.
2137 void regulator_unregister_supply_alias(struct device *dev, const char *id)
2139 struct regulator_supply_alias *map;
2141 map = regulator_find_supply_alias(dev, id);
2142 if (map) {
2143 list_del(&map->list);
2144 kfree(map);
2147 EXPORT_SYMBOL_GPL(regulator_unregister_supply_alias);
2150 * regulator_bulk_register_supply_alias - register multiple aliases
2152 * @dev: device that will be given as the regulator "consumer"
2153 * @id: List of supply names or regulator IDs
2154 * @alias_dev: device that should be used to lookup the supply
2155 * @alias_id: List of supply names or regulator IDs that should be used to
2156 * lookup the supply
2157 * @num_id: Number of aliases to register
2159 * @return 0 on success, an errno on failure.
2161 * This helper function allows drivers to register several supply
2162 * aliases in one operation. If any of the aliases cannot be
2163 * registered any aliases that were registered will be removed
2164 * before returning to the caller.
2166 int regulator_bulk_register_supply_alias(struct device *dev,
2167 const char *const *id,
2168 struct device *alias_dev,
2169 const char *const *alias_id,
2170 int num_id)
2172 int i;
2173 int ret;
2175 for (i = 0; i < num_id; ++i) {
2176 ret = regulator_register_supply_alias(dev, id[i], alias_dev,
2177 alias_id[i]);
2178 if (ret < 0)
2179 goto err;
2182 return 0;
2184 err:
2185 dev_err(dev,
2186 "Failed to create supply alias %s,%s -> %s,%s\n",
2187 id[i], dev_name(dev), alias_id[i], dev_name(alias_dev));
2189 while (--i >= 0)
2190 regulator_unregister_supply_alias(dev, id[i]);
2192 return ret;
2194 EXPORT_SYMBOL_GPL(regulator_bulk_register_supply_alias);
2197 * regulator_bulk_unregister_supply_alias - unregister multiple aliases
2199 * @dev: device that will be given as the regulator "consumer"
2200 * @id: List of supply names or regulator IDs
2201 * @num_id: Number of aliases to unregister
2203 * This helper function allows drivers to unregister several supply
2204 * aliases in one operation.
2206 void regulator_bulk_unregister_supply_alias(struct device *dev,
2207 const char *const *id,
2208 int num_id)
2210 int i;
2212 for (i = 0; i < num_id; ++i)
2213 regulator_unregister_supply_alias(dev, id[i]);
2215 EXPORT_SYMBOL_GPL(regulator_bulk_unregister_supply_alias);
2218 /* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
2219 static int regulator_ena_gpio_request(struct regulator_dev *rdev,
2220 const struct regulator_config *config)
2222 struct regulator_enable_gpio *pin;
2223 struct gpio_desc *gpiod;
2225 gpiod = config->ena_gpiod;
2227 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
2228 if (pin->gpiod == gpiod) {
2229 rdev_dbg(rdev, "GPIO is already used\n");
2230 goto update_ena_gpio_to_rdev;
2234 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
2235 if (pin == NULL)
2236 return -ENOMEM;
2238 pin->gpiod = gpiod;
2239 list_add(&pin->list, &regulator_ena_gpio_list);
2241 update_ena_gpio_to_rdev:
2242 pin->request_count++;
2243 rdev->ena_pin = pin;
2244 return 0;
2247 static void regulator_ena_gpio_free(struct regulator_dev *rdev)
2249 struct regulator_enable_gpio *pin, *n;
2251 if (!rdev->ena_pin)
2252 return;
2254 /* Free the GPIO only in case of no use */
2255 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
2256 if (pin->gpiod == rdev->ena_pin->gpiod) {
2257 if (pin->request_count <= 1) {
2258 pin->request_count = 0;
2259 gpiod_put(pin->gpiod);
2260 list_del(&pin->list);
2261 kfree(pin);
2262 rdev->ena_pin = NULL;
2263 return;
2264 } else {
2265 pin->request_count--;
2272 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
2273 * @rdev: regulator_dev structure
2274 * @enable: enable GPIO at initial use?
2276 * GPIO is enabled in case of initial use. (enable_count is 0)
2277 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
2279 static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
2281 struct regulator_enable_gpio *pin = rdev->ena_pin;
2283 if (!pin)
2284 return -EINVAL;
2286 if (enable) {
2287 /* Enable GPIO at initial use */
2288 if (pin->enable_count == 0)
2289 gpiod_set_value_cansleep(pin->gpiod, 1);
2291 pin->enable_count++;
2292 } else {
2293 if (pin->enable_count > 1) {
2294 pin->enable_count--;
2295 return 0;
2298 /* Disable GPIO if not used */
2299 if (pin->enable_count <= 1) {
2300 gpiod_set_value_cansleep(pin->gpiod, 0);
2301 pin->enable_count = 0;
2305 return 0;
2309 * _regulator_enable_delay - a delay helper function
2310 * @delay: time to delay in microseconds
2312 * Delay for the requested amount of time as per the guidelines in:
2314 * Documentation/timers/timers-howto.txt
2316 * The assumption here is that regulators will never be enabled in
2317 * atomic context and therefore sleeping functions can be used.
2319 static void _regulator_enable_delay(unsigned int delay)
2321 unsigned int ms = delay / 1000;
2322 unsigned int us = delay % 1000;
2324 if (ms > 0) {
2326 * For small enough values, handle super-millisecond
2327 * delays in the usleep_range() call below.
2329 if (ms < 20)
2330 us += ms * 1000;
2331 else
2332 msleep(ms);
2336 * Give the scheduler some room to coalesce with any other
2337 * wakeup sources. For delays shorter than 10 us, don't even
2338 * bother setting up high-resolution timers and just busy-
2339 * loop.
2341 if (us >= 10)
2342 usleep_range(us, us + 100);
2343 else
2344 udelay(us);
2347 static int _regulator_do_enable(struct regulator_dev *rdev)
2349 int ret, delay;
2351 /* Query before enabling in case configuration dependent. */
2352 ret = _regulator_get_enable_time(rdev);
2353 if (ret >= 0) {
2354 delay = ret;
2355 } else {
2356 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
2357 delay = 0;
2360 trace_regulator_enable(rdev_get_name(rdev));
2362 if (rdev->desc->off_on_delay) {
2363 /* if needed, keep a distance of off_on_delay from last time
2364 * this regulator was disabled.
2366 unsigned long start_jiffy = jiffies;
2367 unsigned long intended, max_delay, remaining;
2369 max_delay = usecs_to_jiffies(rdev->desc->off_on_delay);
2370 intended = rdev->last_off_jiffy + max_delay;
2372 if (time_before(start_jiffy, intended)) {
2373 /* calc remaining jiffies to deal with one-time
2374 * timer wrapping.
2375 * in case of multiple timer wrapping, either it can be
2376 * detected by out-of-range remaining, or it cannot be
2377 * detected and we get a penalty of
2378 * _regulator_enable_delay().
2380 remaining = intended - start_jiffy;
2381 if (remaining <= max_delay)
2382 _regulator_enable_delay(
2383 jiffies_to_usecs(remaining));
2387 if (rdev->ena_pin) {
2388 if (!rdev->ena_gpio_state) {
2389 ret = regulator_ena_gpio_ctrl(rdev, true);
2390 if (ret < 0)
2391 return ret;
2392 rdev->ena_gpio_state = 1;
2394 } else if (rdev->desc->ops->enable) {
2395 ret = rdev->desc->ops->enable(rdev);
2396 if (ret < 0)
2397 return ret;
2398 } else {
2399 return -EINVAL;
2402 /* Allow the regulator to ramp; it would be useful to extend
2403 * this for bulk operations so that the regulators can ramp
2404 * together. */
2405 trace_regulator_enable_delay(rdev_get_name(rdev));
2407 _regulator_enable_delay(delay);
2409 trace_regulator_enable_complete(rdev_get_name(rdev));
2411 return 0;
2415 * _regulator_handle_consumer_enable - handle that a consumer enabled
2416 * @regulator: regulator source
2418 * Some things on a regulator consumer (like the contribution towards total
2419 * load on the regulator) only have an effect when the consumer wants the
2420 * regulator enabled. Explained in example with two consumers of the same
2421 * regulator:
2422 * consumer A: set_load(100); => total load = 0
2423 * consumer A: regulator_enable(); => total load = 100
2424 * consumer B: set_load(1000); => total load = 100
2425 * consumer B: regulator_enable(); => total load = 1100
2426 * consumer A: regulator_disable(); => total_load = 1000
2428 * This function (together with _regulator_handle_consumer_disable) is
2429 * responsible for keeping track of the refcount for a given regulator consumer
2430 * and applying / unapplying these things.
2432 * Returns 0 upon no error; -error upon error.
2434 static int _regulator_handle_consumer_enable(struct regulator *regulator)
2436 struct regulator_dev *rdev = regulator->rdev;
2438 lockdep_assert_held_once(&rdev->mutex.base);
2440 regulator->enable_count++;
2441 if (regulator->uA_load && regulator->enable_count == 1)
2442 return drms_uA_update(rdev);
2444 return 0;
2448 * _regulator_handle_consumer_disable - handle that a consumer disabled
2449 * @regulator: regulator source
2451 * The opposite of _regulator_handle_consumer_enable().
2453 * Returns 0 upon no error; -error upon error.
2455 static int _regulator_handle_consumer_disable(struct regulator *regulator)
2457 struct regulator_dev *rdev = regulator->rdev;
2459 lockdep_assert_held_once(&rdev->mutex.base);
2461 if (!regulator->enable_count) {
2462 rdev_err(rdev, "Underflow of regulator enable count\n");
2463 return -EINVAL;
2466 regulator->enable_count--;
2467 if (regulator->uA_load && regulator->enable_count == 0)
2468 return drms_uA_update(rdev);
2470 return 0;
2473 /* locks held by regulator_enable() */
2474 static int _regulator_enable(struct regulator *regulator)
2476 struct regulator_dev *rdev = regulator->rdev;
2477 int ret;
2479 lockdep_assert_held_once(&rdev->mutex.base);
2481 if (rdev->use_count == 0 && rdev->supply) {
2482 ret = _regulator_enable(rdev->supply);
2483 if (ret < 0)
2484 return ret;
2487 /* balance only if there are regulators coupled */
2488 if (rdev->coupling_desc.n_coupled > 1) {
2489 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2490 if (ret < 0)
2491 goto err_disable_supply;
2494 ret = _regulator_handle_consumer_enable(regulator);
2495 if (ret < 0)
2496 goto err_disable_supply;
2498 if (rdev->use_count == 0) {
2499 /* The regulator may on if it's not switchable or left on */
2500 ret = _regulator_is_enabled(rdev);
2501 if (ret == -EINVAL || ret == 0) {
2502 if (!regulator_ops_is_valid(rdev,
2503 REGULATOR_CHANGE_STATUS)) {
2504 ret = -EPERM;
2505 goto err_consumer_disable;
2508 ret = _regulator_do_enable(rdev);
2509 if (ret < 0)
2510 goto err_consumer_disable;
2512 _notifier_call_chain(rdev, REGULATOR_EVENT_ENABLE,
2513 NULL);
2514 } else if (ret < 0) {
2515 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
2516 goto err_consumer_disable;
2518 /* Fallthrough on positive return values - already enabled */
2521 rdev->use_count++;
2523 return 0;
2525 err_consumer_disable:
2526 _regulator_handle_consumer_disable(regulator);
2528 err_disable_supply:
2529 if (rdev->use_count == 0 && rdev->supply)
2530 _regulator_disable(rdev->supply);
2532 return ret;
2536 * regulator_enable - enable regulator output
2537 * @regulator: regulator source
2539 * Request that the regulator be enabled with the regulator output at
2540 * the predefined voltage or current value. Calls to regulator_enable()
2541 * must be balanced with calls to regulator_disable().
2543 * NOTE: the output value can be set by other drivers, boot loader or may be
2544 * hardwired in the regulator.
2546 int regulator_enable(struct regulator *regulator)
2548 struct regulator_dev *rdev = regulator->rdev;
2549 struct ww_acquire_ctx ww_ctx;
2550 int ret;
2552 regulator_lock_dependent(rdev, &ww_ctx);
2553 ret = _regulator_enable(regulator);
2554 regulator_unlock_dependent(rdev, &ww_ctx);
2556 return ret;
2558 EXPORT_SYMBOL_GPL(regulator_enable);
2560 static int _regulator_do_disable(struct regulator_dev *rdev)
2562 int ret;
2564 trace_regulator_disable(rdev_get_name(rdev));
2566 if (rdev->ena_pin) {
2567 if (rdev->ena_gpio_state) {
2568 ret = regulator_ena_gpio_ctrl(rdev, false);
2569 if (ret < 0)
2570 return ret;
2571 rdev->ena_gpio_state = 0;
2574 } else if (rdev->desc->ops->disable) {
2575 ret = rdev->desc->ops->disable(rdev);
2576 if (ret != 0)
2577 return ret;
2580 /* cares about last_off_jiffy only if off_on_delay is required by
2581 * device.
2583 if (rdev->desc->off_on_delay)
2584 rdev->last_off_jiffy = jiffies;
2586 trace_regulator_disable_complete(rdev_get_name(rdev));
2588 return 0;
2591 /* locks held by regulator_disable() */
2592 static int _regulator_disable(struct regulator *regulator)
2594 struct regulator_dev *rdev = regulator->rdev;
2595 int ret = 0;
2597 lockdep_assert_held_once(&rdev->mutex.base);
2599 if (WARN(rdev->use_count <= 0,
2600 "unbalanced disables for %s\n", rdev_get_name(rdev)))
2601 return -EIO;
2603 /* are we the last user and permitted to disable ? */
2604 if (rdev->use_count == 1 &&
2605 (rdev->constraints && !rdev->constraints->always_on)) {
2607 /* we are last user */
2608 if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) {
2609 ret = _notifier_call_chain(rdev,
2610 REGULATOR_EVENT_PRE_DISABLE,
2611 NULL);
2612 if (ret & NOTIFY_STOP_MASK)
2613 return -EINVAL;
2615 ret = _regulator_do_disable(rdev);
2616 if (ret < 0) {
2617 rdev_err(rdev, "failed to disable\n");
2618 _notifier_call_chain(rdev,
2619 REGULATOR_EVENT_ABORT_DISABLE,
2620 NULL);
2621 return ret;
2623 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
2624 NULL);
2627 rdev->use_count = 0;
2628 } else if (rdev->use_count > 1) {
2629 rdev->use_count--;
2632 if (ret == 0)
2633 ret = _regulator_handle_consumer_disable(regulator);
2635 if (ret == 0 && rdev->coupling_desc.n_coupled > 1)
2636 ret = regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2638 if (ret == 0 && rdev->use_count == 0 && rdev->supply)
2639 ret = _regulator_disable(rdev->supply);
2641 return ret;
2645 * regulator_disable - disable regulator output
2646 * @regulator: regulator source
2648 * Disable the regulator output voltage or current. Calls to
2649 * regulator_enable() must be balanced with calls to
2650 * regulator_disable().
2652 * NOTE: this will only disable the regulator output if no other consumer
2653 * devices have it enabled, the regulator device supports disabling and
2654 * machine constraints permit this operation.
2656 int regulator_disable(struct regulator *regulator)
2658 struct regulator_dev *rdev = regulator->rdev;
2659 struct ww_acquire_ctx ww_ctx;
2660 int ret;
2662 regulator_lock_dependent(rdev, &ww_ctx);
2663 ret = _regulator_disable(regulator);
2664 regulator_unlock_dependent(rdev, &ww_ctx);
2666 return ret;
2668 EXPORT_SYMBOL_GPL(regulator_disable);
2670 /* locks held by regulator_force_disable() */
2671 static int _regulator_force_disable(struct regulator_dev *rdev)
2673 int ret = 0;
2675 lockdep_assert_held_once(&rdev->mutex.base);
2677 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2678 REGULATOR_EVENT_PRE_DISABLE, NULL);
2679 if (ret & NOTIFY_STOP_MASK)
2680 return -EINVAL;
2682 ret = _regulator_do_disable(rdev);
2683 if (ret < 0) {
2684 rdev_err(rdev, "failed to force disable\n");
2685 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2686 REGULATOR_EVENT_ABORT_DISABLE, NULL);
2687 return ret;
2690 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
2691 REGULATOR_EVENT_DISABLE, NULL);
2693 return 0;
2697 * regulator_force_disable - force disable regulator output
2698 * @regulator: regulator source
2700 * Forcibly disable the regulator output voltage or current.
2701 * NOTE: this *will* disable the regulator output even if other consumer
2702 * devices have it enabled. This should be used for situations when device
2703 * damage will likely occur if the regulator is not disabled (e.g. over temp).
2705 int regulator_force_disable(struct regulator *regulator)
2707 struct regulator_dev *rdev = regulator->rdev;
2708 struct ww_acquire_ctx ww_ctx;
2709 int ret;
2711 regulator_lock_dependent(rdev, &ww_ctx);
2713 ret = _regulator_force_disable(regulator->rdev);
2715 if (rdev->coupling_desc.n_coupled > 1)
2716 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2718 if (regulator->uA_load) {
2719 regulator->uA_load = 0;
2720 ret = drms_uA_update(rdev);
2723 if (rdev->use_count != 0 && rdev->supply)
2724 _regulator_disable(rdev->supply);
2726 regulator_unlock_dependent(rdev, &ww_ctx);
2728 return ret;
2730 EXPORT_SYMBOL_GPL(regulator_force_disable);
2732 static void regulator_disable_work(struct work_struct *work)
2734 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
2735 disable_work.work);
2736 struct ww_acquire_ctx ww_ctx;
2737 int count, i, ret;
2738 struct regulator *regulator;
2739 int total_count = 0;
2741 regulator_lock_dependent(rdev, &ww_ctx);
2744 * Workqueue functions queue the new work instance while the previous
2745 * work instance is being processed. Cancel the queued work instance
2746 * as the work instance under processing does the job of the queued
2747 * work instance.
2749 cancel_delayed_work(&rdev->disable_work);
2751 list_for_each_entry(regulator, &rdev->consumer_list, list) {
2752 count = regulator->deferred_disables;
2754 if (!count)
2755 continue;
2757 total_count += count;
2758 regulator->deferred_disables = 0;
2760 for (i = 0; i < count; i++) {
2761 ret = _regulator_disable(regulator);
2762 if (ret != 0)
2763 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
2766 WARN_ON(!total_count);
2768 if (rdev->coupling_desc.n_coupled > 1)
2769 regulator_balance_voltage(rdev, PM_SUSPEND_ON);
2771 regulator_unlock_dependent(rdev, &ww_ctx);
2775 * regulator_disable_deferred - disable regulator output with delay
2776 * @regulator: regulator source
2777 * @ms: milliseconds until the regulator is disabled
2779 * Execute regulator_disable() on the regulator after a delay. This
2780 * is intended for use with devices that require some time to quiesce.
2782 * NOTE: this will only disable the regulator output if no other consumer
2783 * devices have it enabled, the regulator device supports disabling and
2784 * machine constraints permit this operation.
2786 int regulator_disable_deferred(struct regulator *regulator, int ms)
2788 struct regulator_dev *rdev = regulator->rdev;
2790 if (!ms)
2791 return regulator_disable(regulator);
2793 regulator_lock(rdev);
2794 regulator->deferred_disables++;
2795 mod_delayed_work(system_power_efficient_wq, &rdev->disable_work,
2796 msecs_to_jiffies(ms));
2797 regulator_unlock(rdev);
2799 return 0;
2801 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
2803 static int _regulator_is_enabled(struct regulator_dev *rdev)
2805 /* A GPIO control always takes precedence */
2806 if (rdev->ena_pin)
2807 return rdev->ena_gpio_state;
2809 /* If we don't know then assume that the regulator is always on */
2810 if (!rdev->desc->ops->is_enabled)
2811 return 1;
2813 return rdev->desc->ops->is_enabled(rdev);
2816 static int _regulator_list_voltage(struct regulator_dev *rdev,
2817 unsigned selector, int lock)
2819 const struct regulator_ops *ops = rdev->desc->ops;
2820 int ret;
2822 if (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1 && !selector)
2823 return rdev->desc->fixed_uV;
2825 if (ops->list_voltage) {
2826 if (selector >= rdev->desc->n_voltages)
2827 return -EINVAL;
2828 if (lock)
2829 regulator_lock(rdev);
2830 ret = ops->list_voltage(rdev, selector);
2831 if (lock)
2832 regulator_unlock(rdev);
2833 } else if (rdev->is_switch && rdev->supply) {
2834 ret = _regulator_list_voltage(rdev->supply->rdev,
2835 selector, lock);
2836 } else {
2837 return -EINVAL;
2840 if (ret > 0) {
2841 if (ret < rdev->constraints->min_uV)
2842 ret = 0;
2843 else if (ret > rdev->constraints->max_uV)
2844 ret = 0;
2847 return ret;
2851 * regulator_is_enabled - is the regulator output enabled
2852 * @regulator: regulator source
2854 * Returns positive if the regulator driver backing the source/client
2855 * has requested that the device be enabled, zero if it hasn't, else a
2856 * negative errno code.
2858 * Note that the device backing this regulator handle can have multiple
2859 * users, so it might be enabled even if regulator_enable() was never
2860 * called for this particular source.
2862 int regulator_is_enabled(struct regulator *regulator)
2864 int ret;
2866 if (regulator->always_on)
2867 return 1;
2869 regulator_lock(regulator->rdev);
2870 ret = _regulator_is_enabled(regulator->rdev);
2871 regulator_unlock(regulator->rdev);
2873 return ret;
2875 EXPORT_SYMBOL_GPL(regulator_is_enabled);
2878 * regulator_count_voltages - count regulator_list_voltage() selectors
2879 * @regulator: regulator source
2881 * Returns number of selectors, or negative errno. Selectors are
2882 * numbered starting at zero, and typically correspond to bitfields
2883 * in hardware registers.
2885 int regulator_count_voltages(struct regulator *regulator)
2887 struct regulator_dev *rdev = regulator->rdev;
2889 if (rdev->desc->n_voltages)
2890 return rdev->desc->n_voltages;
2892 if (!rdev->is_switch || !rdev->supply)
2893 return -EINVAL;
2895 return regulator_count_voltages(rdev->supply);
2897 EXPORT_SYMBOL_GPL(regulator_count_voltages);
2900 * regulator_list_voltage - enumerate supported voltages
2901 * @regulator: regulator source
2902 * @selector: identify voltage to list
2903 * Context: can sleep
2905 * Returns a voltage that can be passed to @regulator_set_voltage(),
2906 * zero if this selector code can't be used on this system, or a
2907 * negative errno.
2909 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2911 return _regulator_list_voltage(regulator->rdev, selector, 1);
2913 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2916 * regulator_get_regmap - get the regulator's register map
2917 * @regulator: regulator source
2919 * Returns the register map for the given regulator, or an ERR_PTR value
2920 * if the regulator doesn't use regmap.
2922 struct regmap *regulator_get_regmap(struct regulator *regulator)
2924 struct regmap *map = regulator->rdev->regmap;
2926 return map ? map : ERR_PTR(-EOPNOTSUPP);
2930 * regulator_get_hardware_vsel_register - get the HW voltage selector register
2931 * @regulator: regulator source
2932 * @vsel_reg: voltage selector register, output parameter
2933 * @vsel_mask: mask for voltage selector bitfield, output parameter
2935 * Returns the hardware register offset and bitmask used for setting the
2936 * regulator voltage. This might be useful when configuring voltage-scaling
2937 * hardware or firmware that can make I2C requests behind the kernel's back,
2938 * for example.
2940 * On success, the output parameters @vsel_reg and @vsel_mask are filled in
2941 * and 0 is returned, otherwise a negative errno is returned.
2943 int regulator_get_hardware_vsel_register(struct regulator *regulator,
2944 unsigned *vsel_reg,
2945 unsigned *vsel_mask)
2947 struct regulator_dev *rdev = regulator->rdev;
2948 const struct regulator_ops *ops = rdev->desc->ops;
2950 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2951 return -EOPNOTSUPP;
2953 *vsel_reg = rdev->desc->vsel_reg;
2954 *vsel_mask = rdev->desc->vsel_mask;
2956 return 0;
2958 EXPORT_SYMBOL_GPL(regulator_get_hardware_vsel_register);
2961 * regulator_list_hardware_vsel - get the HW-specific register value for a selector
2962 * @regulator: regulator source
2963 * @selector: identify voltage to list
2965 * Converts the selector to a hardware-specific voltage selector that can be
2966 * directly written to the regulator registers. The address of the voltage
2967 * register can be determined by calling @regulator_get_hardware_vsel_register.
2969 * On error a negative errno is returned.
2971 int regulator_list_hardware_vsel(struct regulator *regulator,
2972 unsigned selector)
2974 struct regulator_dev *rdev = regulator->rdev;
2975 const struct regulator_ops *ops = rdev->desc->ops;
2977 if (selector >= rdev->desc->n_voltages)
2978 return -EINVAL;
2979 if (ops->set_voltage_sel != regulator_set_voltage_sel_regmap)
2980 return -EOPNOTSUPP;
2982 return selector;
2984 EXPORT_SYMBOL_GPL(regulator_list_hardware_vsel);
2987 * regulator_get_linear_step - return the voltage step size between VSEL values
2988 * @regulator: regulator source
2990 * Returns the voltage step size between VSEL values for linear
2991 * regulators, or return 0 if the regulator isn't a linear regulator.
2993 unsigned int regulator_get_linear_step(struct regulator *regulator)
2995 struct regulator_dev *rdev = regulator->rdev;
2997 return rdev->desc->uV_step;
2999 EXPORT_SYMBOL_GPL(regulator_get_linear_step);
3002 * regulator_is_supported_voltage - check if a voltage range can be supported
3004 * @regulator: Regulator to check.
3005 * @min_uV: Minimum required voltage in uV.
3006 * @max_uV: Maximum required voltage in uV.
3008 * Returns a boolean or a negative error code.
3010 int regulator_is_supported_voltage(struct regulator *regulator,
3011 int min_uV, int max_uV)
3013 struct regulator_dev *rdev = regulator->rdev;
3014 int i, voltages, ret;
3016 /* If we can't change voltage check the current voltage */
3017 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3018 ret = regulator_get_voltage(regulator);
3019 if (ret >= 0)
3020 return min_uV <= ret && ret <= max_uV;
3021 else
3022 return ret;
3025 /* Any voltage within constrains range is fine? */
3026 if (rdev->desc->continuous_voltage_range)
3027 return min_uV >= rdev->constraints->min_uV &&
3028 max_uV <= rdev->constraints->max_uV;
3030 ret = regulator_count_voltages(regulator);
3031 if (ret < 0)
3032 return ret;
3033 voltages = ret;
3035 for (i = 0; i < voltages; i++) {
3036 ret = regulator_list_voltage(regulator, i);
3038 if (ret >= min_uV && ret <= max_uV)
3039 return 1;
3042 return 0;
3044 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
3046 static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
3047 int max_uV)
3049 const struct regulator_desc *desc = rdev->desc;
3051 if (desc->ops->map_voltage)
3052 return desc->ops->map_voltage(rdev, min_uV, max_uV);
3054 if (desc->ops->list_voltage == regulator_list_voltage_linear)
3055 return regulator_map_voltage_linear(rdev, min_uV, max_uV);
3057 if (desc->ops->list_voltage == regulator_list_voltage_linear_range)
3058 return regulator_map_voltage_linear_range(rdev, min_uV, max_uV);
3060 if (desc->ops->list_voltage ==
3061 regulator_list_voltage_pickable_linear_range)
3062 return regulator_map_voltage_pickable_linear_range(rdev,
3063 min_uV, max_uV);
3065 return regulator_map_voltage_iterate(rdev, min_uV, max_uV);
3068 static int _regulator_call_set_voltage(struct regulator_dev *rdev,
3069 int min_uV, int max_uV,
3070 unsigned *selector)
3072 struct pre_voltage_change_data data;
3073 int ret;
3075 data.old_uV = _regulator_get_voltage(rdev);
3076 data.min_uV = min_uV;
3077 data.max_uV = max_uV;
3078 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3079 &data);
3080 if (ret & NOTIFY_STOP_MASK)
3081 return -EINVAL;
3083 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, selector);
3084 if (ret >= 0)
3085 return ret;
3087 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3088 (void *)data.old_uV);
3090 return ret;
3093 static int _regulator_call_set_voltage_sel(struct regulator_dev *rdev,
3094 int uV, unsigned selector)
3096 struct pre_voltage_change_data data;
3097 int ret;
3099 data.old_uV = _regulator_get_voltage(rdev);
3100 data.min_uV = uV;
3101 data.max_uV = uV;
3102 ret = _notifier_call_chain(rdev, REGULATOR_EVENT_PRE_VOLTAGE_CHANGE,
3103 &data);
3104 if (ret & NOTIFY_STOP_MASK)
3105 return -EINVAL;
3107 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
3108 if (ret >= 0)
3109 return ret;
3111 _notifier_call_chain(rdev, REGULATOR_EVENT_ABORT_VOLTAGE_CHANGE,
3112 (void *)data.old_uV);
3114 return ret;
3117 static int _regulator_set_voltage_time(struct regulator_dev *rdev,
3118 int old_uV, int new_uV)
3120 unsigned int ramp_delay = 0;
3122 if (rdev->constraints->ramp_delay)
3123 ramp_delay = rdev->constraints->ramp_delay;
3124 else if (rdev->desc->ramp_delay)
3125 ramp_delay = rdev->desc->ramp_delay;
3126 else if (rdev->constraints->settling_time)
3127 return rdev->constraints->settling_time;
3128 else if (rdev->constraints->settling_time_up &&
3129 (new_uV > old_uV))
3130 return rdev->constraints->settling_time_up;
3131 else if (rdev->constraints->settling_time_down &&
3132 (new_uV < old_uV))
3133 return rdev->constraints->settling_time_down;
3135 if (ramp_delay == 0) {
3136 rdev_dbg(rdev, "ramp_delay not set\n");
3137 return 0;
3140 return DIV_ROUND_UP(abs(new_uV - old_uV), ramp_delay);
3143 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
3144 int min_uV, int max_uV)
3146 int ret;
3147 int delay = 0;
3148 int best_val = 0;
3149 unsigned int selector;
3150 int old_selector = -1;
3151 const struct regulator_ops *ops = rdev->desc->ops;
3152 int old_uV = _regulator_get_voltage(rdev);
3154 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
3156 min_uV += rdev->constraints->uV_offset;
3157 max_uV += rdev->constraints->uV_offset;
3160 * If we can't obtain the old selector there is not enough
3161 * info to call set_voltage_time_sel().
3163 if (_regulator_is_enabled(rdev) &&
3164 ops->set_voltage_time_sel && ops->get_voltage_sel) {
3165 old_selector = ops->get_voltage_sel(rdev);
3166 if (old_selector < 0)
3167 return old_selector;
3170 if (ops->set_voltage) {
3171 ret = _regulator_call_set_voltage(rdev, min_uV, max_uV,
3172 &selector);
3174 if (ret >= 0) {
3175 if (ops->list_voltage)
3176 best_val = ops->list_voltage(rdev,
3177 selector);
3178 else
3179 best_val = _regulator_get_voltage(rdev);
3182 } else if (ops->set_voltage_sel) {
3183 ret = regulator_map_voltage(rdev, min_uV, max_uV);
3184 if (ret >= 0) {
3185 best_val = ops->list_voltage(rdev, ret);
3186 if (min_uV <= best_val && max_uV >= best_val) {
3187 selector = ret;
3188 if (old_selector == selector)
3189 ret = 0;
3190 else
3191 ret = _regulator_call_set_voltage_sel(
3192 rdev, best_val, selector);
3193 } else {
3194 ret = -EINVAL;
3197 } else {
3198 ret = -EINVAL;
3201 if (ret)
3202 goto out;
3204 if (ops->set_voltage_time_sel) {
3206 * Call set_voltage_time_sel if successfully obtained
3207 * old_selector
3209 if (old_selector >= 0 && old_selector != selector)
3210 delay = ops->set_voltage_time_sel(rdev, old_selector,
3211 selector);
3212 } else {
3213 if (old_uV != best_val) {
3214 if (ops->set_voltage_time)
3215 delay = ops->set_voltage_time(rdev, old_uV,
3216 best_val);
3217 else
3218 delay = _regulator_set_voltage_time(rdev,
3219 old_uV,
3220 best_val);
3224 if (delay < 0) {
3225 rdev_warn(rdev, "failed to get delay: %d\n", delay);
3226 delay = 0;
3229 /* Insert any necessary delays */
3230 if (delay >= 1000) {
3231 mdelay(delay / 1000);
3232 udelay(delay % 1000);
3233 } else if (delay) {
3234 udelay(delay);
3237 if (best_val >= 0) {
3238 unsigned long data = best_val;
3240 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
3241 (void *)data);
3244 out:
3245 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
3247 return ret;
3250 static int _regulator_do_set_suspend_voltage(struct regulator_dev *rdev,
3251 int min_uV, int max_uV, suspend_state_t state)
3253 struct regulator_state *rstate;
3254 int uV, sel;
3256 rstate = regulator_get_suspend_state(rdev, state);
3257 if (rstate == NULL)
3258 return -EINVAL;
3260 if (min_uV < rstate->min_uV)
3261 min_uV = rstate->min_uV;
3262 if (max_uV > rstate->max_uV)
3263 max_uV = rstate->max_uV;
3265 sel = regulator_map_voltage(rdev, min_uV, max_uV);
3266 if (sel < 0)
3267 return sel;
3269 uV = rdev->desc->ops->list_voltage(rdev, sel);
3270 if (uV >= min_uV && uV <= max_uV)
3271 rstate->uV = uV;
3273 return 0;
3276 static int regulator_set_voltage_unlocked(struct regulator *regulator,
3277 int min_uV, int max_uV,
3278 suspend_state_t state)
3280 struct regulator_dev *rdev = regulator->rdev;
3281 struct regulator_voltage *voltage = &regulator->voltage[state];
3282 int ret = 0;
3283 int old_min_uV, old_max_uV;
3284 int current_uV;
3286 /* If we're setting the same range as last time the change
3287 * should be a noop (some cpufreq implementations use the same
3288 * voltage for multiple frequencies, for example).
3290 if (voltage->min_uV == min_uV && voltage->max_uV == max_uV)
3291 goto out;
3293 /* If we're trying to set a range that overlaps the current voltage,
3294 * return successfully even though the regulator does not support
3295 * changing the voltage.
3297 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
3298 current_uV = _regulator_get_voltage(rdev);
3299 if (min_uV <= current_uV && current_uV <= max_uV) {
3300 voltage->min_uV = min_uV;
3301 voltage->max_uV = max_uV;
3302 goto out;
3306 /* sanity check */
3307 if (!rdev->desc->ops->set_voltage &&
3308 !rdev->desc->ops->set_voltage_sel) {
3309 ret = -EINVAL;
3310 goto out;
3313 /* constraints check */
3314 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3315 if (ret < 0)
3316 goto out;
3318 /* restore original values in case of error */
3319 old_min_uV = voltage->min_uV;
3320 old_max_uV = voltage->max_uV;
3321 voltage->min_uV = min_uV;
3322 voltage->max_uV = max_uV;
3324 /* for not coupled regulators this will just set the voltage */
3325 ret = regulator_balance_voltage(rdev, state);
3326 if (ret < 0) {
3327 voltage->min_uV = old_min_uV;
3328 voltage->max_uV = old_max_uV;
3331 out:
3332 return ret;
3335 static int regulator_set_voltage_rdev(struct regulator_dev *rdev, int min_uV,
3336 int max_uV, suspend_state_t state)
3338 int best_supply_uV = 0;
3339 int supply_change_uV = 0;
3340 int ret;
3342 if (rdev->supply &&
3343 regulator_ops_is_valid(rdev->supply->rdev,
3344 REGULATOR_CHANGE_VOLTAGE) &&
3345 (rdev->desc->min_dropout_uV || !(rdev->desc->ops->get_voltage ||
3346 rdev->desc->ops->get_voltage_sel))) {
3347 int current_supply_uV;
3348 int selector;
3350 selector = regulator_map_voltage(rdev, min_uV, max_uV);
3351 if (selector < 0) {
3352 ret = selector;
3353 goto out;
3356 best_supply_uV = _regulator_list_voltage(rdev, selector, 0);
3357 if (best_supply_uV < 0) {
3358 ret = best_supply_uV;
3359 goto out;
3362 best_supply_uV += rdev->desc->min_dropout_uV;
3364 current_supply_uV = _regulator_get_voltage(rdev->supply->rdev);
3365 if (current_supply_uV < 0) {
3366 ret = current_supply_uV;
3367 goto out;
3370 supply_change_uV = best_supply_uV - current_supply_uV;
3373 if (supply_change_uV > 0) {
3374 ret = regulator_set_voltage_unlocked(rdev->supply,
3375 best_supply_uV, INT_MAX, state);
3376 if (ret) {
3377 dev_err(&rdev->dev, "Failed to increase supply voltage: %d\n",
3378 ret);
3379 goto out;
3383 if (state == PM_SUSPEND_ON)
3384 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3385 else
3386 ret = _regulator_do_set_suspend_voltage(rdev, min_uV,
3387 max_uV, state);
3388 if (ret < 0)
3389 goto out;
3391 if (supply_change_uV < 0) {
3392 ret = regulator_set_voltage_unlocked(rdev->supply,
3393 best_supply_uV, INT_MAX, state);
3394 if (ret)
3395 dev_warn(&rdev->dev, "Failed to decrease supply voltage: %d\n",
3396 ret);
3397 /* No need to fail here */
3398 ret = 0;
3401 out:
3402 return ret;
3405 static int regulator_limit_voltage_step(struct regulator_dev *rdev,
3406 int *current_uV, int *min_uV)
3408 struct regulation_constraints *constraints = rdev->constraints;
3410 /* Limit voltage change only if necessary */
3411 if (!constraints->max_uV_step || !_regulator_is_enabled(rdev))
3412 return 1;
3414 if (*current_uV < 0) {
3415 *current_uV = _regulator_get_voltage(rdev);
3417 if (*current_uV < 0)
3418 return *current_uV;
3421 if (abs(*current_uV - *min_uV) <= constraints->max_uV_step)
3422 return 1;
3424 /* Clamp target voltage within the given step */
3425 if (*current_uV < *min_uV)
3426 *min_uV = min(*current_uV + constraints->max_uV_step,
3427 *min_uV);
3428 else
3429 *min_uV = max(*current_uV - constraints->max_uV_step,
3430 *min_uV);
3432 return 0;
3435 static int regulator_get_optimal_voltage(struct regulator_dev *rdev,
3436 int *current_uV,
3437 int *min_uV, int *max_uV,
3438 suspend_state_t state,
3439 int n_coupled)
3441 struct coupling_desc *c_desc = &rdev->coupling_desc;
3442 struct regulator_dev **c_rdevs = c_desc->coupled_rdevs;
3443 struct regulation_constraints *constraints = rdev->constraints;
3444 int max_spread = constraints->max_spread;
3445 int desired_min_uV = 0, desired_max_uV = INT_MAX;
3446 int max_current_uV = 0, min_current_uV = INT_MAX;
3447 int highest_min_uV = 0, target_uV, possible_uV;
3448 int i, ret;
3449 bool done;
3451 *current_uV = -1;
3454 * If there are no coupled regulators, simply set the voltage
3455 * demanded by consumers.
3457 if (n_coupled == 1) {
3459 * If consumers don't provide any demands, set voltage
3460 * to min_uV
3462 desired_min_uV = constraints->min_uV;
3463 desired_max_uV = constraints->max_uV;
3465 ret = regulator_check_consumers(rdev,
3466 &desired_min_uV,
3467 &desired_max_uV, state);
3468 if (ret < 0)
3469 return ret;
3471 possible_uV = desired_min_uV;
3472 done = true;
3474 goto finish;
3477 /* Find highest min desired voltage */
3478 for (i = 0; i < n_coupled; i++) {
3479 int tmp_min = 0;
3480 int tmp_max = INT_MAX;
3482 lockdep_assert_held_once(&c_rdevs[i]->mutex.base);
3484 ret = regulator_check_consumers(c_rdevs[i],
3485 &tmp_min,
3486 &tmp_max, state);
3487 if (ret < 0)
3488 return ret;
3490 ret = regulator_check_voltage(c_rdevs[i], &tmp_min, &tmp_max);
3491 if (ret < 0)
3492 return ret;
3494 highest_min_uV = max(highest_min_uV, tmp_min);
3496 if (i == 0) {
3497 desired_min_uV = tmp_min;
3498 desired_max_uV = tmp_max;
3503 * Let target_uV be equal to the desired one if possible.
3504 * If not, set it to minimum voltage, allowed by other coupled
3505 * regulators.
3507 target_uV = max(desired_min_uV, highest_min_uV - max_spread);
3510 * Find min and max voltages, which currently aren't violating
3511 * max_spread.
3513 for (i = 1; i < n_coupled; i++) {
3514 int tmp_act;
3516 if (!_regulator_is_enabled(c_rdevs[i]))
3517 continue;
3519 tmp_act = _regulator_get_voltage(c_rdevs[i]);
3520 if (tmp_act < 0)
3521 return tmp_act;
3523 min_current_uV = min(tmp_act, min_current_uV);
3524 max_current_uV = max(tmp_act, max_current_uV);
3527 /* There aren't any other regulators enabled */
3528 if (max_current_uV == 0) {
3529 possible_uV = target_uV;
3530 } else {
3532 * Correct target voltage, so as it currently isn't
3533 * violating max_spread
3535 possible_uV = max(target_uV, max_current_uV - max_spread);
3536 possible_uV = min(possible_uV, min_current_uV + max_spread);
3539 if (possible_uV > desired_max_uV)
3540 return -EINVAL;
3542 done = (possible_uV == target_uV);
3543 desired_min_uV = possible_uV;
3545 finish:
3546 /* Apply max_uV_step constraint if necessary */
3547 if (state == PM_SUSPEND_ON) {
3548 ret = regulator_limit_voltage_step(rdev, current_uV,
3549 &desired_min_uV);
3550 if (ret < 0)
3551 return ret;
3553 if (ret == 0)
3554 done = false;
3557 /* Set current_uV if wasn't done earlier in the code and if necessary */
3558 if (n_coupled > 1 && *current_uV == -1) {
3560 if (_regulator_is_enabled(rdev)) {
3561 ret = _regulator_get_voltage(rdev);
3562 if (ret < 0)
3563 return ret;
3565 *current_uV = ret;
3566 } else {
3567 *current_uV = desired_min_uV;
3571 *min_uV = desired_min_uV;
3572 *max_uV = desired_max_uV;
3574 return done;
3577 static int regulator_balance_voltage(struct regulator_dev *rdev,
3578 suspend_state_t state)
3580 struct regulator_dev **c_rdevs;
3581 struct regulator_dev *best_rdev;
3582 struct coupling_desc *c_desc = &rdev->coupling_desc;
3583 int i, ret, n_coupled, best_min_uV, best_max_uV, best_c_rdev;
3584 bool best_c_rdev_done, c_rdev_done[MAX_COUPLED];
3585 unsigned int delta, best_delta;
3587 c_rdevs = c_desc->coupled_rdevs;
3588 n_coupled = c_desc->n_coupled;
3591 * If system is in a state other than PM_SUSPEND_ON, don't check
3592 * other coupled regulators.
3594 if (state != PM_SUSPEND_ON)
3595 n_coupled = 1;
3597 if (c_desc->n_resolved < n_coupled) {
3598 rdev_err(rdev, "Not all coupled regulators registered\n");
3599 return -EPERM;
3602 for (i = 0; i < n_coupled; i++)
3603 c_rdev_done[i] = false;
3606 * Find the best possible voltage change on each loop. Leave the loop
3607 * if there isn't any possible change.
3609 do {
3610 best_c_rdev_done = false;
3611 best_delta = 0;
3612 best_min_uV = 0;
3613 best_max_uV = 0;
3614 best_c_rdev = 0;
3615 best_rdev = NULL;
3618 * Find highest difference between optimal voltage
3619 * and current voltage.
3621 for (i = 0; i < n_coupled; i++) {
3623 * optimal_uV is the best voltage that can be set for
3624 * i-th regulator at the moment without violating
3625 * max_spread constraint in order to balance
3626 * the coupled voltages.
3628 int optimal_uV = 0, optimal_max_uV = 0, current_uV = 0;
3630 if (c_rdev_done[i])
3631 continue;
3633 ret = regulator_get_optimal_voltage(c_rdevs[i],
3634 &current_uV,
3635 &optimal_uV,
3636 &optimal_max_uV,
3637 state, n_coupled);
3638 if (ret < 0)
3639 goto out;
3641 delta = abs(optimal_uV - current_uV);
3643 if (delta && best_delta <= delta) {
3644 best_c_rdev_done = ret;
3645 best_delta = delta;
3646 best_rdev = c_rdevs[i];
3647 best_min_uV = optimal_uV;
3648 best_max_uV = optimal_max_uV;
3649 best_c_rdev = i;
3653 /* Nothing to change, return successfully */
3654 if (!best_rdev) {
3655 ret = 0;
3656 goto out;
3659 ret = regulator_set_voltage_rdev(best_rdev, best_min_uV,
3660 best_max_uV, state);
3662 if (ret < 0)
3663 goto out;
3665 c_rdev_done[best_c_rdev] = best_c_rdev_done;
3667 } while (n_coupled > 1);
3669 out:
3670 return ret;
3674 * regulator_set_voltage - set regulator output voltage
3675 * @regulator: regulator source
3676 * @min_uV: Minimum required voltage in uV
3677 * @max_uV: Maximum acceptable voltage in uV
3679 * Sets a voltage regulator to the desired output voltage. This can be set
3680 * during any regulator state. IOW, regulator can be disabled or enabled.
3682 * If the regulator is enabled then the voltage will change to the new value
3683 * immediately otherwise if the regulator is disabled the regulator will
3684 * output at the new voltage when enabled.
3686 * NOTE: If the regulator is shared between several devices then the lowest
3687 * request voltage that meets the system constraints will be used.
3688 * Regulator system constraints must be set for this regulator before
3689 * calling this function otherwise this call will fail.
3691 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
3693 struct ww_acquire_ctx ww_ctx;
3694 int ret;
3696 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3698 ret = regulator_set_voltage_unlocked(regulator, min_uV, max_uV,
3699 PM_SUSPEND_ON);
3701 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3703 return ret;
3705 EXPORT_SYMBOL_GPL(regulator_set_voltage);
3707 static inline int regulator_suspend_toggle(struct regulator_dev *rdev,
3708 suspend_state_t state, bool en)
3710 struct regulator_state *rstate;
3712 rstate = regulator_get_suspend_state(rdev, state);
3713 if (rstate == NULL)
3714 return -EINVAL;
3716 if (!rstate->changeable)
3717 return -EPERM;
3719 rstate->enabled = (en) ? ENABLE_IN_SUSPEND : DISABLE_IN_SUSPEND;
3721 return 0;
3724 int regulator_suspend_enable(struct regulator_dev *rdev,
3725 suspend_state_t state)
3727 return regulator_suspend_toggle(rdev, state, true);
3729 EXPORT_SYMBOL_GPL(regulator_suspend_enable);
3731 int regulator_suspend_disable(struct regulator_dev *rdev,
3732 suspend_state_t state)
3734 struct regulator *regulator;
3735 struct regulator_voltage *voltage;
3738 * if any consumer wants this regulator device keeping on in
3739 * suspend states, don't set it as disabled.
3741 list_for_each_entry(regulator, &rdev->consumer_list, list) {
3742 voltage = &regulator->voltage[state];
3743 if (voltage->min_uV || voltage->max_uV)
3744 return 0;
3747 return regulator_suspend_toggle(rdev, state, false);
3749 EXPORT_SYMBOL_GPL(regulator_suspend_disable);
3751 static int _regulator_set_suspend_voltage(struct regulator *regulator,
3752 int min_uV, int max_uV,
3753 suspend_state_t state)
3755 struct regulator_dev *rdev = regulator->rdev;
3756 struct regulator_state *rstate;
3758 rstate = regulator_get_suspend_state(rdev, state);
3759 if (rstate == NULL)
3760 return -EINVAL;
3762 if (rstate->min_uV == rstate->max_uV) {
3763 rdev_err(rdev, "The suspend voltage can't be changed!\n");
3764 return -EPERM;
3767 return regulator_set_voltage_unlocked(regulator, min_uV, max_uV, state);
3770 int regulator_set_suspend_voltage(struct regulator *regulator, int min_uV,
3771 int max_uV, suspend_state_t state)
3773 struct ww_acquire_ctx ww_ctx;
3774 int ret;
3776 /* PM_SUSPEND_ON is handled by regulator_set_voltage() */
3777 if (regulator_check_states(state) || state == PM_SUSPEND_ON)
3778 return -EINVAL;
3780 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3782 ret = _regulator_set_suspend_voltage(regulator, min_uV,
3783 max_uV, state);
3785 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3787 return ret;
3789 EXPORT_SYMBOL_GPL(regulator_set_suspend_voltage);
3792 * regulator_set_voltage_time - get raise/fall time
3793 * @regulator: regulator source
3794 * @old_uV: starting voltage in microvolts
3795 * @new_uV: target voltage in microvolts
3797 * Provided with the starting and ending voltage, this function attempts to
3798 * calculate the time in microseconds required to rise or fall to this new
3799 * voltage.
3801 int regulator_set_voltage_time(struct regulator *regulator,
3802 int old_uV, int new_uV)
3804 struct regulator_dev *rdev = regulator->rdev;
3805 const struct regulator_ops *ops = rdev->desc->ops;
3806 int old_sel = -1;
3807 int new_sel = -1;
3808 int voltage;
3809 int i;
3811 if (ops->set_voltage_time)
3812 return ops->set_voltage_time(rdev, old_uV, new_uV);
3813 else if (!ops->set_voltage_time_sel)
3814 return _regulator_set_voltage_time(rdev, old_uV, new_uV);
3816 /* Currently requires operations to do this */
3817 if (!ops->list_voltage || !rdev->desc->n_voltages)
3818 return -EINVAL;
3820 for (i = 0; i < rdev->desc->n_voltages; i++) {
3821 /* We only look for exact voltage matches here */
3822 voltage = regulator_list_voltage(regulator, i);
3823 if (voltage < 0)
3824 return -EINVAL;
3825 if (voltage == 0)
3826 continue;
3827 if (voltage == old_uV)
3828 old_sel = i;
3829 if (voltage == new_uV)
3830 new_sel = i;
3833 if (old_sel < 0 || new_sel < 0)
3834 return -EINVAL;
3836 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
3838 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
3841 * regulator_set_voltage_time_sel - get raise/fall time
3842 * @rdev: regulator source device
3843 * @old_selector: selector for starting voltage
3844 * @new_selector: selector for target voltage
3846 * Provided with the starting and target voltage selectors, this function
3847 * returns time in microseconds required to rise or fall to this new voltage
3849 * Drivers providing ramp_delay in regulation_constraints can use this as their
3850 * set_voltage_time_sel() operation.
3852 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
3853 unsigned int old_selector,
3854 unsigned int new_selector)
3856 int old_volt, new_volt;
3858 /* sanity check */
3859 if (!rdev->desc->ops->list_voltage)
3860 return -EINVAL;
3862 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
3863 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
3865 if (rdev->desc->ops->set_voltage_time)
3866 return rdev->desc->ops->set_voltage_time(rdev, old_volt,
3867 new_volt);
3868 else
3869 return _regulator_set_voltage_time(rdev, old_volt, new_volt);
3871 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
3874 * regulator_sync_voltage - re-apply last regulator output voltage
3875 * @regulator: regulator source
3877 * Re-apply the last configured voltage. This is intended to be used
3878 * where some external control source the consumer is cooperating with
3879 * has caused the configured voltage to change.
3881 int regulator_sync_voltage(struct regulator *regulator)
3883 struct regulator_dev *rdev = regulator->rdev;
3884 struct regulator_voltage *voltage = &regulator->voltage[PM_SUSPEND_ON];
3885 int ret, min_uV, max_uV;
3887 regulator_lock(rdev);
3889 if (!rdev->desc->ops->set_voltage &&
3890 !rdev->desc->ops->set_voltage_sel) {
3891 ret = -EINVAL;
3892 goto out;
3895 /* This is only going to work if we've had a voltage configured. */
3896 if (!voltage->min_uV && !voltage->max_uV) {
3897 ret = -EINVAL;
3898 goto out;
3901 min_uV = voltage->min_uV;
3902 max_uV = voltage->max_uV;
3904 /* This should be a paranoia check... */
3905 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
3906 if (ret < 0)
3907 goto out;
3909 ret = regulator_check_consumers(rdev, &min_uV, &max_uV, 0);
3910 if (ret < 0)
3911 goto out;
3913 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
3915 out:
3916 regulator_unlock(rdev);
3917 return ret;
3919 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
3921 static int _regulator_get_voltage(struct regulator_dev *rdev)
3923 int sel, ret;
3924 bool bypassed;
3926 if (rdev->desc->ops->get_bypass) {
3927 ret = rdev->desc->ops->get_bypass(rdev, &bypassed);
3928 if (ret < 0)
3929 return ret;
3930 if (bypassed) {
3931 /* if bypassed the regulator must have a supply */
3932 if (!rdev->supply) {
3933 rdev_err(rdev,
3934 "bypassed regulator has no supply!\n");
3935 return -EPROBE_DEFER;
3938 return _regulator_get_voltage(rdev->supply->rdev);
3942 if (rdev->desc->ops->get_voltage_sel) {
3943 sel = rdev->desc->ops->get_voltage_sel(rdev);
3944 if (sel < 0)
3945 return sel;
3946 ret = rdev->desc->ops->list_voltage(rdev, sel);
3947 } else if (rdev->desc->ops->get_voltage) {
3948 ret = rdev->desc->ops->get_voltage(rdev);
3949 } else if (rdev->desc->ops->list_voltage) {
3950 ret = rdev->desc->ops->list_voltage(rdev, 0);
3951 } else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
3952 ret = rdev->desc->fixed_uV;
3953 } else if (rdev->supply) {
3954 ret = _regulator_get_voltage(rdev->supply->rdev);
3955 } else {
3956 return -EINVAL;
3959 if (ret < 0)
3960 return ret;
3961 return ret - rdev->constraints->uV_offset;
3965 * regulator_get_voltage - get regulator output voltage
3966 * @regulator: regulator source
3968 * This returns the current regulator voltage in uV.
3970 * NOTE: If the regulator is disabled it will return the voltage value. This
3971 * function should not be used to determine regulator state.
3973 int regulator_get_voltage(struct regulator *regulator)
3975 struct ww_acquire_ctx ww_ctx;
3976 int ret;
3978 regulator_lock_dependent(regulator->rdev, &ww_ctx);
3979 ret = _regulator_get_voltage(regulator->rdev);
3980 regulator_unlock_dependent(regulator->rdev, &ww_ctx);
3982 return ret;
3984 EXPORT_SYMBOL_GPL(regulator_get_voltage);
3987 * regulator_set_current_limit - set regulator output current limit
3988 * @regulator: regulator source
3989 * @min_uA: Minimum supported current in uA
3990 * @max_uA: Maximum supported current in uA
3992 * Sets current sink to the desired output current. This can be set during
3993 * any regulator state. IOW, regulator can be disabled or enabled.
3995 * If the regulator is enabled then the current will change to the new value
3996 * immediately otherwise if the regulator is disabled the regulator will
3997 * output at the new current when enabled.
3999 * NOTE: Regulator system constraints must be set for this regulator before
4000 * calling this function otherwise this call will fail.
4002 int regulator_set_current_limit(struct regulator *regulator,
4003 int min_uA, int max_uA)
4005 struct regulator_dev *rdev = regulator->rdev;
4006 int ret;
4008 regulator_lock(rdev);
4010 /* sanity check */
4011 if (!rdev->desc->ops->set_current_limit) {
4012 ret = -EINVAL;
4013 goto out;
4016 /* constraints check */
4017 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
4018 if (ret < 0)
4019 goto out;
4021 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
4022 out:
4023 regulator_unlock(rdev);
4024 return ret;
4026 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
4028 static int _regulator_get_current_limit_unlocked(struct regulator_dev *rdev)
4030 /* sanity check */
4031 if (!rdev->desc->ops->get_current_limit)
4032 return -EINVAL;
4034 return rdev->desc->ops->get_current_limit(rdev);
4037 static int _regulator_get_current_limit(struct regulator_dev *rdev)
4039 int ret;
4041 regulator_lock(rdev);
4042 ret = _regulator_get_current_limit_unlocked(rdev);
4043 regulator_unlock(rdev);
4045 return ret;
4049 * regulator_get_current_limit - get regulator output current
4050 * @regulator: regulator source
4052 * This returns the current supplied by the specified current sink in uA.
4054 * NOTE: If the regulator is disabled it will return the current value. This
4055 * function should not be used to determine regulator state.
4057 int regulator_get_current_limit(struct regulator *regulator)
4059 return _regulator_get_current_limit(regulator->rdev);
4061 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
4064 * regulator_set_mode - set regulator operating mode
4065 * @regulator: regulator source
4066 * @mode: operating mode - one of the REGULATOR_MODE constants
4068 * Set regulator operating mode to increase regulator efficiency or improve
4069 * regulation performance.
4071 * NOTE: Regulator system constraints must be set for this regulator before
4072 * calling this function otherwise this call will fail.
4074 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
4076 struct regulator_dev *rdev = regulator->rdev;
4077 int ret;
4078 int regulator_curr_mode;
4080 regulator_lock(rdev);
4082 /* sanity check */
4083 if (!rdev->desc->ops->set_mode) {
4084 ret = -EINVAL;
4085 goto out;
4088 /* return if the same mode is requested */
4089 if (rdev->desc->ops->get_mode) {
4090 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
4091 if (regulator_curr_mode == mode) {
4092 ret = 0;
4093 goto out;
4097 /* constraints check */
4098 ret = regulator_mode_constrain(rdev, &mode);
4099 if (ret < 0)
4100 goto out;
4102 ret = rdev->desc->ops->set_mode(rdev, mode);
4103 out:
4104 regulator_unlock(rdev);
4105 return ret;
4107 EXPORT_SYMBOL_GPL(regulator_set_mode);
4109 static unsigned int _regulator_get_mode_unlocked(struct regulator_dev *rdev)
4111 /* sanity check */
4112 if (!rdev->desc->ops->get_mode)
4113 return -EINVAL;
4115 return rdev->desc->ops->get_mode(rdev);
4118 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
4120 int ret;
4122 regulator_lock(rdev);
4123 ret = _regulator_get_mode_unlocked(rdev);
4124 regulator_unlock(rdev);
4126 return ret;
4130 * regulator_get_mode - get regulator operating mode
4131 * @regulator: regulator source
4133 * Get the current regulator operating mode.
4135 unsigned int regulator_get_mode(struct regulator *regulator)
4137 return _regulator_get_mode(regulator->rdev);
4139 EXPORT_SYMBOL_GPL(regulator_get_mode);
4141 static int _regulator_get_error_flags(struct regulator_dev *rdev,
4142 unsigned int *flags)
4144 int ret;
4146 regulator_lock(rdev);
4148 /* sanity check */
4149 if (!rdev->desc->ops->get_error_flags) {
4150 ret = -EINVAL;
4151 goto out;
4154 ret = rdev->desc->ops->get_error_flags(rdev, flags);
4155 out:
4156 regulator_unlock(rdev);
4157 return ret;
4161 * regulator_get_error_flags - get regulator error information
4162 * @regulator: regulator source
4163 * @flags: pointer to store error flags
4165 * Get the current regulator error information.
4167 int regulator_get_error_flags(struct regulator *regulator,
4168 unsigned int *flags)
4170 return _regulator_get_error_flags(regulator->rdev, flags);
4172 EXPORT_SYMBOL_GPL(regulator_get_error_flags);
4175 * regulator_set_load - set regulator load
4176 * @regulator: regulator source
4177 * @uA_load: load current
4179 * Notifies the regulator core of a new device load. This is then used by
4180 * DRMS (if enabled by constraints) to set the most efficient regulator
4181 * operating mode for the new regulator loading.
4183 * Consumer devices notify their supply regulator of the maximum power
4184 * they will require (can be taken from device datasheet in the power
4185 * consumption tables) when they change operational status and hence power
4186 * state. Examples of operational state changes that can affect power
4187 * consumption are :-
4189 * o Device is opened / closed.
4190 * o Device I/O is about to begin or has just finished.
4191 * o Device is idling in between work.
4193 * This information is also exported via sysfs to userspace.
4195 * DRMS will sum the total requested load on the regulator and change
4196 * to the most efficient operating mode if platform constraints allow.
4198 * NOTE: when a regulator consumer requests to have a regulator
4199 * disabled then any load that consumer requested no longer counts
4200 * toward the total requested load. If the regulator is re-enabled
4201 * then the previously requested load will start counting again.
4203 * If a regulator is an always-on regulator then an individual consumer's
4204 * load will still be removed if that consumer is fully disabled.
4206 * On error a negative errno is returned.
4208 int regulator_set_load(struct regulator *regulator, int uA_load)
4210 struct regulator_dev *rdev = regulator->rdev;
4211 int old_uA_load;
4212 int ret = 0;
4214 regulator_lock(rdev);
4215 old_uA_load = regulator->uA_load;
4216 regulator->uA_load = uA_load;
4217 if (regulator->enable_count && old_uA_load != uA_load) {
4218 ret = drms_uA_update(rdev);
4219 if (ret < 0)
4220 regulator->uA_load = old_uA_load;
4222 regulator_unlock(rdev);
4224 return ret;
4226 EXPORT_SYMBOL_GPL(regulator_set_load);
4229 * regulator_allow_bypass - allow the regulator to go into bypass mode
4231 * @regulator: Regulator to configure
4232 * @enable: enable or disable bypass mode
4234 * Allow the regulator to go into bypass mode if all other consumers
4235 * for the regulator also enable bypass mode and the machine
4236 * constraints allow this. Bypass mode means that the regulator is
4237 * simply passing the input directly to the output with no regulation.
4239 int regulator_allow_bypass(struct regulator *regulator, bool enable)
4241 struct regulator_dev *rdev = regulator->rdev;
4242 int ret = 0;
4244 if (!rdev->desc->ops->set_bypass)
4245 return 0;
4247 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_BYPASS))
4248 return 0;
4250 regulator_lock(rdev);
4252 if (enable && !regulator->bypass) {
4253 rdev->bypass_count++;
4255 if (rdev->bypass_count == rdev->open_count) {
4256 ret = rdev->desc->ops->set_bypass(rdev, enable);
4257 if (ret != 0)
4258 rdev->bypass_count--;
4261 } else if (!enable && regulator->bypass) {
4262 rdev->bypass_count--;
4264 if (rdev->bypass_count != rdev->open_count) {
4265 ret = rdev->desc->ops->set_bypass(rdev, enable);
4266 if (ret != 0)
4267 rdev->bypass_count++;
4271 if (ret == 0)
4272 regulator->bypass = enable;
4274 regulator_unlock(rdev);
4276 return ret;
4278 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
4281 * regulator_register_notifier - register regulator event notifier
4282 * @regulator: regulator source
4283 * @nb: notifier block
4285 * Register notifier block to receive regulator events.
4287 int regulator_register_notifier(struct regulator *regulator,
4288 struct notifier_block *nb)
4290 return blocking_notifier_chain_register(&regulator->rdev->notifier,
4291 nb);
4293 EXPORT_SYMBOL_GPL(regulator_register_notifier);
4296 * regulator_unregister_notifier - unregister regulator event notifier
4297 * @regulator: regulator source
4298 * @nb: notifier block
4300 * Unregister regulator event notifier block.
4302 int regulator_unregister_notifier(struct regulator *regulator,
4303 struct notifier_block *nb)
4305 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
4306 nb);
4308 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
4310 /* notify regulator consumers and downstream regulator consumers.
4311 * Note mutex must be held by caller.
4313 static int _notifier_call_chain(struct regulator_dev *rdev,
4314 unsigned long event, void *data)
4316 /* call rdev chain first */
4317 return blocking_notifier_call_chain(&rdev->notifier, event, data);
4321 * regulator_bulk_get - get multiple regulator consumers
4323 * @dev: Device to supply
4324 * @num_consumers: Number of consumers to register
4325 * @consumers: Configuration of consumers; clients are stored here.
4327 * @return 0 on success, an errno on failure.
4329 * This helper function allows drivers to get several regulator
4330 * consumers in one operation. If any of the regulators cannot be
4331 * acquired then any regulators that were allocated will be freed
4332 * before returning to the caller.
4334 int regulator_bulk_get(struct device *dev, int num_consumers,
4335 struct regulator_bulk_data *consumers)
4337 int i;
4338 int ret;
4340 for (i = 0; i < num_consumers; i++)
4341 consumers[i].consumer = NULL;
4343 for (i = 0; i < num_consumers; i++) {
4344 consumers[i].consumer = regulator_get(dev,
4345 consumers[i].supply);
4346 if (IS_ERR(consumers[i].consumer)) {
4347 ret = PTR_ERR(consumers[i].consumer);
4348 dev_err(dev, "Failed to get supply '%s': %d\n",
4349 consumers[i].supply, ret);
4350 consumers[i].consumer = NULL;
4351 goto err;
4355 return 0;
4357 err:
4358 while (--i >= 0)
4359 regulator_put(consumers[i].consumer);
4361 return ret;
4363 EXPORT_SYMBOL_GPL(regulator_bulk_get);
4365 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
4367 struct regulator_bulk_data *bulk = data;
4369 bulk->ret = regulator_enable(bulk->consumer);
4373 * regulator_bulk_enable - enable multiple regulator consumers
4375 * @num_consumers: Number of consumers
4376 * @consumers: Consumer data; clients are stored here.
4377 * @return 0 on success, an errno on failure
4379 * This convenience API allows consumers to enable multiple regulator
4380 * clients in a single API call. If any consumers cannot be enabled
4381 * then any others that were enabled will be disabled again prior to
4382 * return.
4384 int regulator_bulk_enable(int num_consumers,
4385 struct regulator_bulk_data *consumers)
4387 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
4388 int i;
4389 int ret = 0;
4391 for (i = 0; i < num_consumers; i++) {
4392 async_schedule_domain(regulator_bulk_enable_async,
4393 &consumers[i], &async_domain);
4396 async_synchronize_full_domain(&async_domain);
4398 /* If any consumer failed we need to unwind any that succeeded */
4399 for (i = 0; i < num_consumers; i++) {
4400 if (consumers[i].ret != 0) {
4401 ret = consumers[i].ret;
4402 goto err;
4406 return 0;
4408 err:
4409 for (i = 0; i < num_consumers; i++) {
4410 if (consumers[i].ret < 0)
4411 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
4412 consumers[i].ret);
4413 else
4414 regulator_disable(consumers[i].consumer);
4417 return ret;
4419 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
4422 * regulator_bulk_disable - disable multiple regulator consumers
4424 * @num_consumers: Number of consumers
4425 * @consumers: Consumer data; clients are stored here.
4426 * @return 0 on success, an errno on failure
4428 * This convenience API allows consumers to disable multiple regulator
4429 * clients in a single API call. If any consumers cannot be disabled
4430 * then any others that were disabled will be enabled again prior to
4431 * return.
4433 int regulator_bulk_disable(int num_consumers,
4434 struct regulator_bulk_data *consumers)
4436 int i;
4437 int ret, r;
4439 for (i = num_consumers - 1; i >= 0; --i) {
4440 ret = regulator_disable(consumers[i].consumer);
4441 if (ret != 0)
4442 goto err;
4445 return 0;
4447 err:
4448 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
4449 for (++i; i < num_consumers; ++i) {
4450 r = regulator_enable(consumers[i].consumer);
4451 if (r != 0)
4452 pr_err("Failed to re-enable %s: %d\n",
4453 consumers[i].supply, r);
4456 return ret;
4458 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
4461 * regulator_bulk_force_disable - force disable multiple regulator consumers
4463 * @num_consumers: Number of consumers
4464 * @consumers: Consumer data; clients are stored here.
4465 * @return 0 on success, an errno on failure
4467 * This convenience API allows consumers to forcibly disable multiple regulator
4468 * clients in a single API call.
4469 * NOTE: This should be used for situations when device damage will
4470 * likely occur if the regulators are not disabled (e.g. over temp).
4471 * Although regulator_force_disable function call for some consumers can
4472 * return error numbers, the function is called for all consumers.
4474 int regulator_bulk_force_disable(int num_consumers,
4475 struct regulator_bulk_data *consumers)
4477 int i;
4478 int ret = 0;
4480 for (i = 0; i < num_consumers; i++) {
4481 consumers[i].ret =
4482 regulator_force_disable(consumers[i].consumer);
4484 /* Store first error for reporting */
4485 if (consumers[i].ret && !ret)
4486 ret = consumers[i].ret;
4489 return ret;
4491 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
4494 * regulator_bulk_free - free multiple regulator consumers
4496 * @num_consumers: Number of consumers
4497 * @consumers: Consumer data; clients are stored here.
4499 * This convenience API allows consumers to free multiple regulator
4500 * clients in a single API call.
4502 void regulator_bulk_free(int num_consumers,
4503 struct regulator_bulk_data *consumers)
4505 int i;
4507 for (i = 0; i < num_consumers; i++) {
4508 regulator_put(consumers[i].consumer);
4509 consumers[i].consumer = NULL;
4512 EXPORT_SYMBOL_GPL(regulator_bulk_free);
4515 * regulator_notifier_call_chain - call regulator event notifier
4516 * @rdev: regulator source
4517 * @event: notifier block
4518 * @data: callback-specific data.
4520 * Called by regulator drivers to notify clients a regulator event has
4521 * occurred. We also notify regulator clients downstream.
4522 * Note lock must be held by caller.
4524 int regulator_notifier_call_chain(struct regulator_dev *rdev,
4525 unsigned long event, void *data)
4527 lockdep_assert_held_once(&rdev->mutex.base);
4529 _notifier_call_chain(rdev, event, data);
4530 return NOTIFY_DONE;
4533 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
4536 * regulator_mode_to_status - convert a regulator mode into a status
4538 * @mode: Mode to convert
4540 * Convert a regulator mode into a status.
4542 int regulator_mode_to_status(unsigned int mode)
4544 switch (mode) {
4545 case REGULATOR_MODE_FAST:
4546 return REGULATOR_STATUS_FAST;
4547 case REGULATOR_MODE_NORMAL:
4548 return REGULATOR_STATUS_NORMAL;
4549 case REGULATOR_MODE_IDLE:
4550 return REGULATOR_STATUS_IDLE;
4551 case REGULATOR_MODE_STANDBY:
4552 return REGULATOR_STATUS_STANDBY;
4553 default:
4554 return REGULATOR_STATUS_UNDEFINED;
4557 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
4559 static struct attribute *regulator_dev_attrs[] = {
4560 &dev_attr_name.attr,
4561 &dev_attr_num_users.attr,
4562 &dev_attr_type.attr,
4563 &dev_attr_microvolts.attr,
4564 &dev_attr_microamps.attr,
4565 &dev_attr_opmode.attr,
4566 &dev_attr_state.attr,
4567 &dev_attr_status.attr,
4568 &dev_attr_bypass.attr,
4569 &dev_attr_requested_microamps.attr,
4570 &dev_attr_min_microvolts.attr,
4571 &dev_attr_max_microvolts.attr,
4572 &dev_attr_min_microamps.attr,
4573 &dev_attr_max_microamps.attr,
4574 &dev_attr_suspend_standby_state.attr,
4575 &dev_attr_suspend_mem_state.attr,
4576 &dev_attr_suspend_disk_state.attr,
4577 &dev_attr_suspend_standby_microvolts.attr,
4578 &dev_attr_suspend_mem_microvolts.attr,
4579 &dev_attr_suspend_disk_microvolts.attr,
4580 &dev_attr_suspend_standby_mode.attr,
4581 &dev_attr_suspend_mem_mode.attr,
4582 &dev_attr_suspend_disk_mode.attr,
4583 NULL
4587 * To avoid cluttering sysfs (and memory) with useless state, only
4588 * create attributes that can be meaningfully displayed.
4590 static umode_t regulator_attr_is_visible(struct kobject *kobj,
4591 struct attribute *attr, int idx)
4593 struct device *dev = kobj_to_dev(kobj);
4594 struct regulator_dev *rdev = dev_to_rdev(dev);
4595 const struct regulator_ops *ops = rdev->desc->ops;
4596 umode_t mode = attr->mode;
4598 /* these three are always present */
4599 if (attr == &dev_attr_name.attr ||
4600 attr == &dev_attr_num_users.attr ||
4601 attr == &dev_attr_type.attr)
4602 return mode;
4604 /* some attributes need specific methods to be displayed */
4605 if (attr == &dev_attr_microvolts.attr) {
4606 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
4607 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
4608 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0) ||
4609 (rdev->desc->fixed_uV && rdev->desc->n_voltages == 1))
4610 return mode;
4611 return 0;
4614 if (attr == &dev_attr_microamps.attr)
4615 return ops->get_current_limit ? mode : 0;
4617 if (attr == &dev_attr_opmode.attr)
4618 return ops->get_mode ? mode : 0;
4620 if (attr == &dev_attr_state.attr)
4621 return (rdev->ena_pin || ops->is_enabled) ? mode : 0;
4623 if (attr == &dev_attr_status.attr)
4624 return ops->get_status ? mode : 0;
4626 if (attr == &dev_attr_bypass.attr)
4627 return ops->get_bypass ? mode : 0;
4629 /* constraints need specific supporting methods */
4630 if (attr == &dev_attr_min_microvolts.attr ||
4631 attr == &dev_attr_max_microvolts.attr)
4632 return (ops->set_voltage || ops->set_voltage_sel) ? mode : 0;
4634 if (attr == &dev_attr_min_microamps.attr ||
4635 attr == &dev_attr_max_microamps.attr)
4636 return ops->set_current_limit ? mode : 0;
4638 if (attr == &dev_attr_suspend_standby_state.attr ||
4639 attr == &dev_attr_suspend_mem_state.attr ||
4640 attr == &dev_attr_suspend_disk_state.attr)
4641 return mode;
4643 if (attr == &dev_attr_suspend_standby_microvolts.attr ||
4644 attr == &dev_attr_suspend_mem_microvolts.attr ||
4645 attr == &dev_attr_suspend_disk_microvolts.attr)
4646 return ops->set_suspend_voltage ? mode : 0;
4648 if (attr == &dev_attr_suspend_standby_mode.attr ||
4649 attr == &dev_attr_suspend_mem_mode.attr ||
4650 attr == &dev_attr_suspend_disk_mode.attr)
4651 return ops->set_suspend_mode ? mode : 0;
4653 return mode;
4656 static const struct attribute_group regulator_dev_group = {
4657 .attrs = regulator_dev_attrs,
4658 .is_visible = regulator_attr_is_visible,
4661 static const struct attribute_group *regulator_dev_groups[] = {
4662 &regulator_dev_group,
4663 NULL
4666 static void regulator_dev_release(struct device *dev)
4668 struct regulator_dev *rdev = dev_get_drvdata(dev);
4670 kfree(rdev->constraints);
4671 of_node_put(rdev->dev.of_node);
4672 kfree(rdev);
4675 static void rdev_init_debugfs(struct regulator_dev *rdev)
4677 struct device *parent = rdev->dev.parent;
4678 const char *rname = rdev_get_name(rdev);
4679 char name[NAME_MAX];
4681 /* Avoid duplicate debugfs directory names */
4682 if (parent && rname == rdev->desc->name) {
4683 snprintf(name, sizeof(name), "%s-%s", dev_name(parent),
4684 rname);
4685 rname = name;
4688 rdev->debugfs = debugfs_create_dir(rname, debugfs_root);
4689 if (!rdev->debugfs) {
4690 rdev_warn(rdev, "Failed to create debugfs directory\n");
4691 return;
4694 debugfs_create_u32("use_count", 0444, rdev->debugfs,
4695 &rdev->use_count);
4696 debugfs_create_u32("open_count", 0444, rdev->debugfs,
4697 &rdev->open_count);
4698 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
4699 &rdev->bypass_count);
4702 static int regulator_register_resolve_supply(struct device *dev, void *data)
4704 struct regulator_dev *rdev = dev_to_rdev(dev);
4706 if (regulator_resolve_supply(rdev))
4707 rdev_dbg(rdev, "unable to resolve supply\n");
4709 return 0;
4712 static void regulator_resolve_coupling(struct regulator_dev *rdev)
4714 struct coupling_desc *c_desc = &rdev->coupling_desc;
4715 int n_coupled = c_desc->n_coupled;
4716 struct regulator_dev *c_rdev;
4717 int i;
4719 for (i = 1; i < n_coupled; i++) {
4720 /* already resolved */
4721 if (c_desc->coupled_rdevs[i])
4722 continue;
4724 c_rdev = of_parse_coupled_regulator(rdev, i - 1);
4726 if (!c_rdev)
4727 continue;
4729 regulator_lock(c_rdev);
4731 c_desc->coupled_rdevs[i] = c_rdev;
4732 c_desc->n_resolved++;
4734 regulator_unlock(c_rdev);
4736 regulator_resolve_coupling(c_rdev);
4740 static void regulator_remove_coupling(struct regulator_dev *rdev)
4742 struct coupling_desc *__c_desc, *c_desc = &rdev->coupling_desc;
4743 struct regulator_dev *__c_rdev, *c_rdev;
4744 unsigned int __n_coupled, n_coupled;
4745 int i, k;
4747 n_coupled = c_desc->n_coupled;
4749 for (i = 1; i < n_coupled; i++) {
4750 c_rdev = c_desc->coupled_rdevs[i];
4752 if (!c_rdev)
4753 continue;
4755 regulator_lock(c_rdev);
4757 __c_desc = &c_rdev->coupling_desc;
4758 __n_coupled = __c_desc->n_coupled;
4760 for (k = 1; k < __n_coupled; k++) {
4761 __c_rdev = __c_desc->coupled_rdevs[k];
4763 if (__c_rdev == rdev) {
4764 __c_desc->coupled_rdevs[k] = NULL;
4765 __c_desc->n_resolved--;
4766 break;
4770 regulator_unlock(c_rdev);
4772 c_desc->coupled_rdevs[i] = NULL;
4773 c_desc->n_resolved--;
4777 static int regulator_init_coupling(struct regulator_dev *rdev)
4779 int n_phandles;
4781 if (!IS_ENABLED(CONFIG_OF))
4782 n_phandles = 0;
4783 else
4784 n_phandles = of_get_n_coupled(rdev);
4786 if (n_phandles + 1 > MAX_COUPLED) {
4787 rdev_err(rdev, "too many regulators coupled\n");
4788 return -EPERM;
4792 * Every regulator should always have coupling descriptor filled with
4793 * at least pointer to itself.
4795 rdev->coupling_desc.coupled_rdevs[0] = rdev;
4796 rdev->coupling_desc.n_coupled = n_phandles + 1;
4797 rdev->coupling_desc.n_resolved++;
4799 /* regulator isn't coupled */
4800 if (n_phandles == 0)
4801 return 0;
4803 /* regulator, which can't change its voltage, can't be coupled */
4804 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_VOLTAGE)) {
4805 rdev_err(rdev, "voltage operation not allowed\n");
4806 return -EPERM;
4809 if (rdev->constraints->max_spread <= 0) {
4810 rdev_err(rdev, "wrong max_spread value\n");
4811 return -EPERM;
4814 if (!of_check_coupling_data(rdev))
4815 return -EPERM;
4817 return 0;
4821 * regulator_register - register regulator
4822 * @regulator_desc: regulator to register
4823 * @cfg: runtime configuration for regulator
4825 * Called by regulator drivers to register a regulator.
4826 * Returns a valid pointer to struct regulator_dev on success
4827 * or an ERR_PTR() on error.
4829 struct regulator_dev *
4830 regulator_register(const struct regulator_desc *regulator_desc,
4831 const struct regulator_config *cfg)
4833 const struct regulation_constraints *constraints = NULL;
4834 const struct regulator_init_data *init_data;
4835 struct regulator_config *config = NULL;
4836 static atomic_t regulator_no = ATOMIC_INIT(-1);
4837 struct regulator_dev *rdev;
4838 bool dangling_cfg_gpiod = false;
4839 bool dangling_of_gpiod = false;
4840 struct device *dev;
4841 int ret, i;
4843 if (cfg == NULL)
4844 return ERR_PTR(-EINVAL);
4845 if (cfg->ena_gpiod)
4846 dangling_cfg_gpiod = true;
4847 if (regulator_desc == NULL) {
4848 ret = -EINVAL;
4849 goto rinse;
4852 dev = cfg->dev;
4853 WARN_ON(!dev);
4855 if (regulator_desc->name == NULL || regulator_desc->ops == NULL) {
4856 ret = -EINVAL;
4857 goto rinse;
4860 if (regulator_desc->type != REGULATOR_VOLTAGE &&
4861 regulator_desc->type != REGULATOR_CURRENT) {
4862 ret = -EINVAL;
4863 goto rinse;
4866 /* Only one of each should be implemented */
4867 WARN_ON(regulator_desc->ops->get_voltage &&
4868 regulator_desc->ops->get_voltage_sel);
4869 WARN_ON(regulator_desc->ops->set_voltage &&
4870 regulator_desc->ops->set_voltage_sel);
4872 /* If we're using selectors we must implement list_voltage. */
4873 if (regulator_desc->ops->get_voltage_sel &&
4874 !regulator_desc->ops->list_voltage) {
4875 ret = -EINVAL;
4876 goto rinse;
4878 if (regulator_desc->ops->set_voltage_sel &&
4879 !regulator_desc->ops->list_voltage) {
4880 ret = -EINVAL;
4881 goto rinse;
4884 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
4885 if (rdev == NULL) {
4886 ret = -ENOMEM;
4887 goto rinse;
4891 * Duplicate the config so the driver could override it after
4892 * parsing init data.
4894 config = kmemdup(cfg, sizeof(*cfg), GFP_KERNEL);
4895 if (config == NULL) {
4896 kfree(rdev);
4897 ret = -ENOMEM;
4898 goto rinse;
4901 init_data = regulator_of_get_init_data(dev, regulator_desc, config,
4902 &rdev->dev.of_node);
4904 * We need to keep track of any GPIO descriptor coming from the
4905 * device tree until we have handled it over to the core. If the
4906 * config that was passed in to this function DOES NOT contain
4907 * a descriptor, and the config after this call DOES contain
4908 * a descriptor, we definitely got one from parsing the device
4909 * tree.
4911 if (!cfg->ena_gpiod && config->ena_gpiod)
4912 dangling_of_gpiod = true;
4913 if (!init_data) {
4914 init_data = config->init_data;
4915 rdev->dev.of_node = of_node_get(config->of_node);
4918 ww_mutex_init(&rdev->mutex, &regulator_ww_class);
4919 rdev->reg_data = config->driver_data;
4920 rdev->owner = regulator_desc->owner;
4921 rdev->desc = regulator_desc;
4922 if (config->regmap)
4923 rdev->regmap = config->regmap;
4924 else if (dev_get_regmap(dev, NULL))
4925 rdev->regmap = dev_get_regmap(dev, NULL);
4926 else if (dev->parent)
4927 rdev->regmap = dev_get_regmap(dev->parent, NULL);
4928 INIT_LIST_HEAD(&rdev->consumer_list);
4929 INIT_LIST_HEAD(&rdev->list);
4930 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
4931 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
4933 /* preform any regulator specific init */
4934 if (init_data && init_data->regulator_init) {
4935 ret = init_data->regulator_init(rdev->reg_data);
4936 if (ret < 0)
4937 goto clean;
4940 if (config->ena_gpiod) {
4941 mutex_lock(&regulator_list_mutex);
4942 ret = regulator_ena_gpio_request(rdev, config);
4943 mutex_unlock(&regulator_list_mutex);
4944 if (ret != 0) {
4945 rdev_err(rdev, "Failed to request enable GPIO: %d\n",
4946 ret);
4947 goto clean;
4949 /* The regulator core took over the GPIO descriptor */
4950 dangling_cfg_gpiod = false;
4951 dangling_of_gpiod = false;
4954 /* register with sysfs */
4955 rdev->dev.class = &regulator_class;
4956 rdev->dev.parent = dev;
4957 dev_set_name(&rdev->dev, "regulator.%lu",
4958 (unsigned long) atomic_inc_return(&regulator_no));
4960 /* set regulator constraints */
4961 if (init_data)
4962 constraints = &init_data->constraints;
4964 if (init_data && init_data->supply_regulator)
4965 rdev->supply_name = init_data->supply_regulator;
4966 else if (regulator_desc->supply_name)
4967 rdev->supply_name = regulator_desc->supply_name;
4970 * Attempt to resolve the regulator supply, if specified,
4971 * but don't return an error if we fail because we will try
4972 * to resolve it again later as more regulators are added.
4974 if (regulator_resolve_supply(rdev))
4975 rdev_dbg(rdev, "unable to resolve supply\n");
4977 ret = set_machine_constraints(rdev, constraints);
4978 if (ret < 0)
4979 goto wash;
4981 ret = regulator_init_coupling(rdev);
4982 if (ret < 0)
4983 goto wash;
4985 /* add consumers devices */
4986 if (init_data) {
4987 mutex_lock(&regulator_list_mutex);
4988 for (i = 0; i < init_data->num_consumer_supplies; i++) {
4989 ret = set_consumer_device_supply(rdev,
4990 init_data->consumer_supplies[i].dev_name,
4991 init_data->consumer_supplies[i].supply);
4992 if (ret < 0) {
4993 mutex_unlock(&regulator_list_mutex);
4994 dev_err(dev, "Failed to set supply %s\n",
4995 init_data->consumer_supplies[i].supply);
4996 goto unset_supplies;
4999 mutex_unlock(&regulator_list_mutex);
5002 if (!rdev->desc->ops->get_voltage &&
5003 !rdev->desc->ops->list_voltage &&
5004 !rdev->desc->fixed_uV)
5005 rdev->is_switch = true;
5007 dev_set_drvdata(&rdev->dev, rdev);
5008 ret = device_register(&rdev->dev);
5009 if (ret != 0) {
5010 put_device(&rdev->dev);
5011 goto unset_supplies;
5014 rdev_init_debugfs(rdev);
5016 /* try to resolve regulators coupling since a new one was registered */
5017 mutex_lock(&regulator_list_mutex);
5018 regulator_resolve_coupling(rdev);
5019 mutex_unlock(&regulator_list_mutex);
5021 /* try to resolve regulators supply since a new one was registered */
5022 class_for_each_device(&regulator_class, NULL, NULL,
5023 regulator_register_resolve_supply);
5024 kfree(config);
5025 return rdev;
5027 unset_supplies:
5028 mutex_lock(&regulator_list_mutex);
5029 unset_regulator_supplies(rdev);
5030 mutex_unlock(&regulator_list_mutex);
5031 wash:
5032 kfree(rdev->constraints);
5033 mutex_lock(&regulator_list_mutex);
5034 regulator_ena_gpio_free(rdev);
5035 mutex_unlock(&regulator_list_mutex);
5036 clean:
5037 if (dangling_of_gpiod)
5038 gpiod_put(config->ena_gpiod);
5039 kfree(rdev);
5040 kfree(config);
5041 rinse:
5042 if (dangling_cfg_gpiod)
5043 gpiod_put(cfg->ena_gpiod);
5044 return ERR_PTR(ret);
5046 EXPORT_SYMBOL_GPL(regulator_register);
5049 * regulator_unregister - unregister regulator
5050 * @rdev: regulator to unregister
5052 * Called by regulator drivers to unregister a regulator.
5054 void regulator_unregister(struct regulator_dev *rdev)
5056 if (rdev == NULL)
5057 return;
5059 if (rdev->supply) {
5060 while (rdev->use_count--)
5061 regulator_disable(rdev->supply);
5062 regulator_put(rdev->supply);
5065 flush_work(&rdev->disable_work.work);
5067 mutex_lock(&regulator_list_mutex);
5069 debugfs_remove_recursive(rdev->debugfs);
5070 WARN_ON(rdev->open_count);
5071 regulator_remove_coupling(rdev);
5072 unset_regulator_supplies(rdev);
5073 list_del(&rdev->list);
5074 regulator_ena_gpio_free(rdev);
5075 device_unregister(&rdev->dev);
5077 mutex_unlock(&regulator_list_mutex);
5079 EXPORT_SYMBOL_GPL(regulator_unregister);
5081 #ifdef CONFIG_SUSPEND
5083 * regulator_suspend - prepare regulators for system wide suspend
5084 * @dev: ``&struct device`` pointer that is passed to _regulator_suspend()
5086 * Configure each regulator with it's suspend operating parameters for state.
5088 static int regulator_suspend(struct device *dev)
5090 struct regulator_dev *rdev = dev_to_rdev(dev);
5091 suspend_state_t state = pm_suspend_target_state;
5092 int ret;
5094 regulator_lock(rdev);
5095 ret = suspend_set_state(rdev, state);
5096 regulator_unlock(rdev);
5098 return ret;
5101 static int regulator_resume(struct device *dev)
5103 suspend_state_t state = pm_suspend_target_state;
5104 struct regulator_dev *rdev = dev_to_rdev(dev);
5105 struct regulator_state *rstate;
5106 int ret = 0;
5108 rstate = regulator_get_suspend_state(rdev, state);
5109 if (rstate == NULL)
5110 return 0;
5112 regulator_lock(rdev);
5114 if (rdev->desc->ops->resume &&
5115 (rstate->enabled == ENABLE_IN_SUSPEND ||
5116 rstate->enabled == DISABLE_IN_SUSPEND))
5117 ret = rdev->desc->ops->resume(rdev);
5119 regulator_unlock(rdev);
5121 return ret;
5123 #else /* !CONFIG_SUSPEND */
5125 #define regulator_suspend NULL
5126 #define regulator_resume NULL
5128 #endif /* !CONFIG_SUSPEND */
5130 #ifdef CONFIG_PM
5131 static const struct dev_pm_ops __maybe_unused regulator_pm_ops = {
5132 .suspend = regulator_suspend,
5133 .resume = regulator_resume,
5135 #endif
5137 struct class regulator_class = {
5138 .name = "regulator",
5139 .dev_release = regulator_dev_release,
5140 .dev_groups = regulator_dev_groups,
5141 #ifdef CONFIG_PM
5142 .pm = &regulator_pm_ops,
5143 #endif
5146 * regulator_has_full_constraints - the system has fully specified constraints
5148 * Calling this function will cause the regulator API to disable all
5149 * regulators which have a zero use count and don't have an always_on
5150 * constraint in a late_initcall.
5152 * The intention is that this will become the default behaviour in a
5153 * future kernel release so users are encouraged to use this facility
5154 * now.
5156 void regulator_has_full_constraints(void)
5158 has_full_constraints = 1;
5160 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
5163 * rdev_get_drvdata - get rdev regulator driver data
5164 * @rdev: regulator
5166 * Get rdev regulator driver private data. This call can be used in the
5167 * regulator driver context.
5169 void *rdev_get_drvdata(struct regulator_dev *rdev)
5171 return rdev->reg_data;
5173 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
5176 * regulator_get_drvdata - get regulator driver data
5177 * @regulator: regulator
5179 * Get regulator driver private data. This call can be used in the consumer
5180 * driver context when non API regulator specific functions need to be called.
5182 void *regulator_get_drvdata(struct regulator *regulator)
5184 return regulator->rdev->reg_data;
5186 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
5189 * regulator_set_drvdata - set regulator driver data
5190 * @regulator: regulator
5191 * @data: data
5193 void regulator_set_drvdata(struct regulator *regulator, void *data)
5195 regulator->rdev->reg_data = data;
5197 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
5200 * regulator_get_id - get regulator ID
5201 * @rdev: regulator
5203 int rdev_get_id(struct regulator_dev *rdev)
5205 return rdev->desc->id;
5207 EXPORT_SYMBOL_GPL(rdev_get_id);
5209 struct device *rdev_get_dev(struct regulator_dev *rdev)
5211 return &rdev->dev;
5213 EXPORT_SYMBOL_GPL(rdev_get_dev);
5215 struct regmap *rdev_get_regmap(struct regulator_dev *rdev)
5217 return rdev->regmap;
5219 EXPORT_SYMBOL_GPL(rdev_get_regmap);
5221 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
5223 return reg_init_data->driver_data;
5225 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
5227 #ifdef CONFIG_DEBUG_FS
5228 static int supply_map_show(struct seq_file *sf, void *data)
5230 struct regulator_map *map;
5232 list_for_each_entry(map, &regulator_map_list, list) {
5233 seq_printf(sf, "%s -> %s.%s\n",
5234 rdev_get_name(map->regulator), map->dev_name,
5235 map->supply);
5238 return 0;
5240 DEFINE_SHOW_ATTRIBUTE(supply_map);
5242 struct summary_data {
5243 struct seq_file *s;
5244 struct regulator_dev *parent;
5245 int level;
5248 static void regulator_summary_show_subtree(struct seq_file *s,
5249 struct regulator_dev *rdev,
5250 int level);
5252 static int regulator_summary_show_children(struct device *dev, void *data)
5254 struct regulator_dev *rdev = dev_to_rdev(dev);
5255 struct summary_data *summary_data = data;
5257 if (rdev->supply && rdev->supply->rdev == summary_data->parent)
5258 regulator_summary_show_subtree(summary_data->s, rdev,
5259 summary_data->level + 1);
5261 return 0;
5264 static void regulator_summary_show_subtree(struct seq_file *s,
5265 struct regulator_dev *rdev,
5266 int level)
5268 struct regulation_constraints *c;
5269 struct regulator *consumer;
5270 struct summary_data summary_data;
5271 unsigned int opmode;
5273 if (!rdev)
5274 return;
5276 opmode = _regulator_get_mode_unlocked(rdev);
5277 seq_printf(s, "%*s%-*s %3d %4d %6d %7s ",
5278 level * 3 + 1, "",
5279 30 - level * 3, rdev_get_name(rdev),
5280 rdev->use_count, rdev->open_count, rdev->bypass_count,
5281 regulator_opmode_to_str(opmode));
5283 seq_printf(s, "%5dmV ", _regulator_get_voltage(rdev) / 1000);
5284 seq_printf(s, "%5dmA ",
5285 _regulator_get_current_limit_unlocked(rdev) / 1000);
5287 c = rdev->constraints;
5288 if (c) {
5289 switch (rdev->desc->type) {
5290 case REGULATOR_VOLTAGE:
5291 seq_printf(s, "%5dmV %5dmV ",
5292 c->min_uV / 1000, c->max_uV / 1000);
5293 break;
5294 case REGULATOR_CURRENT:
5295 seq_printf(s, "%5dmA %5dmA ",
5296 c->min_uA / 1000, c->max_uA / 1000);
5297 break;
5301 seq_puts(s, "\n");
5303 list_for_each_entry(consumer, &rdev->consumer_list, list) {
5304 if (consumer->dev && consumer->dev->class == &regulator_class)
5305 continue;
5307 seq_printf(s, "%*s%-*s ",
5308 (level + 1) * 3 + 1, "",
5309 30 - (level + 1) * 3,
5310 consumer->dev ? dev_name(consumer->dev) : "deviceless");
5312 switch (rdev->desc->type) {
5313 case REGULATOR_VOLTAGE:
5314 seq_printf(s, "%3d %33dmA%c%5dmV %5dmV",
5315 consumer->enable_count,
5316 consumer->uA_load / 1000,
5317 consumer->uA_load && !consumer->enable_count ?
5318 '*' : ' ',
5319 consumer->voltage[PM_SUSPEND_ON].min_uV / 1000,
5320 consumer->voltage[PM_SUSPEND_ON].max_uV / 1000);
5321 break;
5322 case REGULATOR_CURRENT:
5323 break;
5326 seq_puts(s, "\n");
5329 summary_data.s = s;
5330 summary_data.level = level;
5331 summary_data.parent = rdev;
5333 class_for_each_device(&regulator_class, NULL, &summary_data,
5334 regulator_summary_show_children);
5337 struct summary_lock_data {
5338 struct ww_acquire_ctx *ww_ctx;
5339 struct regulator_dev **new_contended_rdev;
5340 struct regulator_dev **old_contended_rdev;
5343 static int regulator_summary_lock_one(struct device *dev, void *data)
5345 struct regulator_dev *rdev = dev_to_rdev(dev);
5346 struct summary_lock_data *lock_data = data;
5347 int ret = 0;
5349 if (rdev != *lock_data->old_contended_rdev) {
5350 ret = regulator_lock_nested(rdev, lock_data->ww_ctx);
5352 if (ret == -EDEADLK)
5353 *lock_data->new_contended_rdev = rdev;
5354 else
5355 WARN_ON_ONCE(ret);
5356 } else {
5357 *lock_data->old_contended_rdev = NULL;
5360 return ret;
5363 static int regulator_summary_unlock_one(struct device *dev, void *data)
5365 struct regulator_dev *rdev = dev_to_rdev(dev);
5366 struct summary_lock_data *lock_data = data;
5368 if (lock_data) {
5369 if (rdev == *lock_data->new_contended_rdev)
5370 return -EDEADLK;
5373 regulator_unlock(rdev);
5375 return 0;
5378 static int regulator_summary_lock_all(struct ww_acquire_ctx *ww_ctx,
5379 struct regulator_dev **new_contended_rdev,
5380 struct regulator_dev **old_contended_rdev)
5382 struct summary_lock_data lock_data;
5383 int ret;
5385 lock_data.ww_ctx = ww_ctx;
5386 lock_data.new_contended_rdev = new_contended_rdev;
5387 lock_data.old_contended_rdev = old_contended_rdev;
5389 ret = class_for_each_device(&regulator_class, NULL, &lock_data,
5390 regulator_summary_lock_one);
5391 if (ret)
5392 class_for_each_device(&regulator_class, NULL, &lock_data,
5393 regulator_summary_unlock_one);
5395 return ret;
5398 static void regulator_summary_lock(struct ww_acquire_ctx *ww_ctx)
5400 struct regulator_dev *new_contended_rdev = NULL;
5401 struct regulator_dev *old_contended_rdev = NULL;
5402 int err;
5404 mutex_lock(&regulator_list_mutex);
5406 ww_acquire_init(ww_ctx, &regulator_ww_class);
5408 do {
5409 if (new_contended_rdev) {
5410 ww_mutex_lock_slow(&new_contended_rdev->mutex, ww_ctx);
5411 old_contended_rdev = new_contended_rdev;
5412 old_contended_rdev->ref_cnt++;
5415 err = regulator_summary_lock_all(ww_ctx,
5416 &new_contended_rdev,
5417 &old_contended_rdev);
5419 if (old_contended_rdev)
5420 regulator_unlock(old_contended_rdev);
5422 } while (err == -EDEADLK);
5424 ww_acquire_done(ww_ctx);
5427 static void regulator_summary_unlock(struct ww_acquire_ctx *ww_ctx)
5429 class_for_each_device(&regulator_class, NULL, NULL,
5430 regulator_summary_unlock_one);
5431 ww_acquire_fini(ww_ctx);
5433 mutex_unlock(&regulator_list_mutex);
5436 static int regulator_summary_show_roots(struct device *dev, void *data)
5438 struct regulator_dev *rdev = dev_to_rdev(dev);
5439 struct seq_file *s = data;
5441 if (!rdev->supply)
5442 regulator_summary_show_subtree(s, rdev, 0);
5444 return 0;
5447 static int regulator_summary_show(struct seq_file *s, void *data)
5449 struct ww_acquire_ctx ww_ctx;
5451 seq_puts(s, " regulator use open bypass opmode voltage current min max\n");
5452 seq_puts(s, "---------------------------------------------------------------------------------------\n");
5454 regulator_summary_lock(&ww_ctx);
5456 class_for_each_device(&regulator_class, NULL, s,
5457 regulator_summary_show_roots);
5459 regulator_summary_unlock(&ww_ctx);
5461 return 0;
5463 DEFINE_SHOW_ATTRIBUTE(regulator_summary);
5464 #endif /* CONFIG_DEBUG_FS */
5466 static int __init regulator_init(void)
5468 int ret;
5470 ret = class_register(&regulator_class);
5472 debugfs_root = debugfs_create_dir("regulator", NULL);
5473 if (!debugfs_root)
5474 pr_warn("regulator: Failed to create debugfs directory\n");
5476 #ifdef CONFIG_DEBUG_FS
5477 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
5478 &supply_map_fops);
5480 debugfs_create_file("regulator_summary", 0444, debugfs_root,
5481 NULL, &regulator_summary_fops);
5482 #endif
5483 regulator_dummy_init();
5485 return ret;
5488 /* init early to allow our consumers to complete system booting */
5489 core_initcall(regulator_init);
5491 static int __init regulator_late_cleanup(struct device *dev, void *data)
5493 struct regulator_dev *rdev = dev_to_rdev(dev);
5494 const struct regulator_ops *ops = rdev->desc->ops;
5495 struct regulation_constraints *c = rdev->constraints;
5496 int enabled, ret;
5498 if (c && c->always_on)
5499 return 0;
5501 if (!regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS))
5502 return 0;
5504 regulator_lock(rdev);
5506 if (rdev->use_count)
5507 goto unlock;
5509 /* If we can't read the status assume it's on. */
5510 if (ops->is_enabled)
5511 enabled = ops->is_enabled(rdev);
5512 else
5513 enabled = 1;
5515 if (!enabled)
5516 goto unlock;
5518 if (have_full_constraints()) {
5519 /* We log since this may kill the system if it goes
5520 * wrong. */
5521 rdev_info(rdev, "disabling\n");
5522 ret = _regulator_do_disable(rdev);
5523 if (ret != 0)
5524 rdev_err(rdev, "couldn't disable: %d\n", ret);
5525 } else {
5526 /* The intention is that in future we will
5527 * assume that full constraints are provided
5528 * so warn even if we aren't going to do
5529 * anything here.
5531 rdev_warn(rdev, "incomplete constraints, leaving on\n");
5534 unlock:
5535 regulator_unlock(rdev);
5537 return 0;
5540 static int __init regulator_init_complete(void)
5543 * Since DT doesn't provide an idiomatic mechanism for
5544 * enabling full constraints and since it's much more natural
5545 * with DT to provide them just assume that a DT enabled
5546 * system has full constraints.
5548 if (of_have_populated_dt())
5549 has_full_constraints = true;
5552 * Regulators may had failed to resolve their input supplies
5553 * when were registered, either because the input supply was
5554 * not registered yet or because its parent device was not
5555 * bound yet. So attempt to resolve the input supplies for
5556 * pending regulators before trying to disable unused ones.
5558 class_for_each_device(&regulator_class, NULL, NULL,
5559 regulator_register_resolve_supply);
5561 /* If we have a full configuration then disable any regulators
5562 * we have permission to change the status for and which are
5563 * not in use or always_on. This is effectively the default
5564 * for DT and ACPI as they have full constraints.
5566 class_for_each_device(&regulator_class, NULL, NULL,
5567 regulator_late_cleanup);
5569 return 0;
5571 late_initcall_sync(regulator_init_complete);