EHCI: implement new semantics for URB_ISO_ASAP
[linux-2.6.git] / drivers / regulator / core.c
blob5c4829cba6a62de6168c6bb3c4913812e5e8b4e1
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.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"
40 #define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42 #define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44 #define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46 #define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48 #define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
51 static DEFINE_MUTEX(regulator_list_mutex);
52 static LIST_HEAD(regulator_list);
53 static LIST_HEAD(regulator_map_list);
54 static bool has_full_constraints;
55 static bool board_wants_dummy_regulator;
57 static struct dentry *debugfs_root;
60 * struct regulator_map
62 * Used to provide symbolic supply names to devices.
64 struct regulator_map {
65 struct list_head list;
66 const char *dev_name; /* The dev_name() for the consumer */
67 const char *supply;
68 struct regulator_dev *regulator;
72 * struct regulator
74 * One for each consumer device.
76 struct regulator {
77 struct device *dev;
78 struct list_head list;
79 unsigned int always_on:1;
80 unsigned int bypass:1;
81 int uA_load;
82 int min_uV;
83 int max_uV;
84 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
87 struct dentry *debugfs;
90 static int _regulator_is_enabled(struct regulator_dev *rdev);
91 static int _regulator_disable(struct regulator_dev *rdev);
92 static int _regulator_get_voltage(struct regulator_dev *rdev);
93 static int _regulator_get_current_limit(struct regulator_dev *rdev);
94 static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95 static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
97 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
99 static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
103 static const char *rdev_get_name(struct regulator_dev *rdev)
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
114 * of_get_regulator - get a regulator device node based on supply name
115 * @dev: Device pointer for the consumer (of regulator) device
116 * @supply: regulator supply name
118 * Extract the regulator device node corresponding to the supply name.
119 * retruns the device node corresponding to the regulator if found, else
120 * returns NULL.
122 static struct device_node *of_get_regulator(struct device *dev, const char *supply)
124 struct device_node *regnode = NULL;
125 char prop_name[32]; /* 32 is max size of property name */
127 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
129 snprintf(prop_name, 32, "%s-supply", supply);
130 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
132 if (!regnode) {
133 dev_dbg(dev, "Looking up %s property in node %s failed",
134 prop_name, dev->of_node->full_name);
135 return NULL;
137 return regnode;
140 static int _regulator_can_change_status(struct regulator_dev *rdev)
142 if (!rdev->constraints)
143 return 0;
145 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
146 return 1;
147 else
148 return 0;
151 /* Platform voltage constraint check */
152 static int regulator_check_voltage(struct regulator_dev *rdev,
153 int *min_uV, int *max_uV)
155 BUG_ON(*min_uV > *max_uV);
157 if (!rdev->constraints) {
158 rdev_err(rdev, "no constraints\n");
159 return -ENODEV;
161 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
162 rdev_err(rdev, "operation not allowed\n");
163 return -EPERM;
166 if (*max_uV > rdev->constraints->max_uV)
167 *max_uV = rdev->constraints->max_uV;
168 if (*min_uV < rdev->constraints->min_uV)
169 *min_uV = rdev->constraints->min_uV;
171 if (*min_uV > *max_uV) {
172 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
173 *min_uV, *max_uV);
174 return -EINVAL;
177 return 0;
180 /* Make sure we select a voltage that suits the needs of all
181 * regulator consumers
183 static int regulator_check_consumers(struct regulator_dev *rdev,
184 int *min_uV, int *max_uV)
186 struct regulator *regulator;
188 list_for_each_entry(regulator, &rdev->consumer_list, list) {
190 * Assume consumers that didn't say anything are OK
191 * with anything in the constraint range.
193 if (!regulator->min_uV && !regulator->max_uV)
194 continue;
196 if (*max_uV > regulator->max_uV)
197 *max_uV = regulator->max_uV;
198 if (*min_uV < regulator->min_uV)
199 *min_uV = regulator->min_uV;
202 if (*min_uV > *max_uV)
203 return -EINVAL;
205 return 0;
208 /* current constraint check */
209 static int regulator_check_current_limit(struct regulator_dev *rdev,
210 int *min_uA, int *max_uA)
212 BUG_ON(*min_uA > *max_uA);
214 if (!rdev->constraints) {
215 rdev_err(rdev, "no constraints\n");
216 return -ENODEV;
218 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
219 rdev_err(rdev, "operation not allowed\n");
220 return -EPERM;
223 if (*max_uA > rdev->constraints->max_uA)
224 *max_uA = rdev->constraints->max_uA;
225 if (*min_uA < rdev->constraints->min_uA)
226 *min_uA = rdev->constraints->min_uA;
228 if (*min_uA > *max_uA) {
229 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
230 *min_uA, *max_uA);
231 return -EINVAL;
234 return 0;
237 /* operating mode constraint check */
238 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
240 switch (*mode) {
241 case REGULATOR_MODE_FAST:
242 case REGULATOR_MODE_NORMAL:
243 case REGULATOR_MODE_IDLE:
244 case REGULATOR_MODE_STANDBY:
245 break;
246 default:
247 rdev_err(rdev, "invalid mode %x specified\n", *mode);
248 return -EINVAL;
251 if (!rdev->constraints) {
252 rdev_err(rdev, "no constraints\n");
253 return -ENODEV;
255 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
256 rdev_err(rdev, "operation not allowed\n");
257 return -EPERM;
260 /* The modes are bitmasks, the most power hungry modes having
261 * the lowest values. If the requested mode isn't supported
262 * try higher modes. */
263 while (*mode) {
264 if (rdev->constraints->valid_modes_mask & *mode)
265 return 0;
266 *mode /= 2;
269 return -EINVAL;
272 /* dynamic regulator mode switching constraint check */
273 static int regulator_check_drms(struct regulator_dev *rdev)
275 if (!rdev->constraints) {
276 rdev_err(rdev, "no constraints\n");
277 return -ENODEV;
279 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
280 rdev_err(rdev, "operation not allowed\n");
281 return -EPERM;
283 return 0;
286 static ssize_t regulator_uV_show(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct regulator_dev *rdev = dev_get_drvdata(dev);
290 ssize_t ret;
292 mutex_lock(&rdev->mutex);
293 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
294 mutex_unlock(&rdev->mutex);
296 return ret;
298 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
300 static ssize_t regulator_uA_show(struct device *dev,
301 struct device_attribute *attr, char *buf)
303 struct regulator_dev *rdev = dev_get_drvdata(dev);
305 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
307 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
309 static ssize_t regulator_name_show(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct regulator_dev *rdev = dev_get_drvdata(dev);
314 return sprintf(buf, "%s\n", rdev_get_name(rdev));
317 static ssize_t regulator_print_opmode(char *buf, int mode)
319 switch (mode) {
320 case REGULATOR_MODE_FAST:
321 return sprintf(buf, "fast\n");
322 case REGULATOR_MODE_NORMAL:
323 return sprintf(buf, "normal\n");
324 case REGULATOR_MODE_IDLE:
325 return sprintf(buf, "idle\n");
326 case REGULATOR_MODE_STANDBY:
327 return sprintf(buf, "standby\n");
329 return sprintf(buf, "unknown\n");
332 static ssize_t regulator_opmode_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
335 struct regulator_dev *rdev = dev_get_drvdata(dev);
337 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
339 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
341 static ssize_t regulator_print_state(char *buf, int state)
343 if (state > 0)
344 return sprintf(buf, "enabled\n");
345 else if (state == 0)
346 return sprintf(buf, "disabled\n");
347 else
348 return sprintf(buf, "unknown\n");
351 static ssize_t regulator_state_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
354 struct regulator_dev *rdev = dev_get_drvdata(dev);
355 ssize_t ret;
357 mutex_lock(&rdev->mutex);
358 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
359 mutex_unlock(&rdev->mutex);
361 return ret;
363 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
365 static ssize_t regulator_status_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
368 struct regulator_dev *rdev = dev_get_drvdata(dev);
369 int status;
370 char *label;
372 status = rdev->desc->ops->get_status(rdev);
373 if (status < 0)
374 return status;
376 switch (status) {
377 case REGULATOR_STATUS_OFF:
378 label = "off";
379 break;
380 case REGULATOR_STATUS_ON:
381 label = "on";
382 break;
383 case REGULATOR_STATUS_ERROR:
384 label = "error";
385 break;
386 case REGULATOR_STATUS_FAST:
387 label = "fast";
388 break;
389 case REGULATOR_STATUS_NORMAL:
390 label = "normal";
391 break;
392 case REGULATOR_STATUS_IDLE:
393 label = "idle";
394 break;
395 case REGULATOR_STATUS_STANDBY:
396 label = "standby";
397 break;
398 case REGULATOR_STATUS_BYPASS:
399 label = "bypass";
400 break;
401 case REGULATOR_STATUS_UNDEFINED:
402 label = "undefined";
403 break;
404 default:
405 return -ERANGE;
408 return sprintf(buf, "%s\n", label);
410 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
412 static ssize_t regulator_min_uA_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
415 struct regulator_dev *rdev = dev_get_drvdata(dev);
417 if (!rdev->constraints)
418 return sprintf(buf, "constraint not defined\n");
420 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
422 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
424 static ssize_t regulator_max_uA_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
427 struct regulator_dev *rdev = dev_get_drvdata(dev);
429 if (!rdev->constraints)
430 return sprintf(buf, "constraint not defined\n");
432 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
434 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
436 static ssize_t regulator_min_uV_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
439 struct regulator_dev *rdev = dev_get_drvdata(dev);
441 if (!rdev->constraints)
442 return sprintf(buf, "constraint not defined\n");
444 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
446 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
448 static ssize_t regulator_max_uV_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
451 struct regulator_dev *rdev = dev_get_drvdata(dev);
453 if (!rdev->constraints)
454 return sprintf(buf, "constraint not defined\n");
456 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
458 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
460 static ssize_t regulator_total_uA_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
463 struct regulator_dev *rdev = dev_get_drvdata(dev);
464 struct regulator *regulator;
465 int uA = 0;
467 mutex_lock(&rdev->mutex);
468 list_for_each_entry(regulator, &rdev->consumer_list, list)
469 uA += regulator->uA_load;
470 mutex_unlock(&rdev->mutex);
471 return sprintf(buf, "%d\n", uA);
473 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
475 static ssize_t regulator_num_users_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
478 struct regulator_dev *rdev = dev_get_drvdata(dev);
479 return sprintf(buf, "%d\n", rdev->use_count);
482 static ssize_t regulator_type_show(struct device *dev,
483 struct device_attribute *attr, char *buf)
485 struct regulator_dev *rdev = dev_get_drvdata(dev);
487 switch (rdev->desc->type) {
488 case REGULATOR_VOLTAGE:
489 return sprintf(buf, "voltage\n");
490 case REGULATOR_CURRENT:
491 return sprintf(buf, "current\n");
493 return sprintf(buf, "unknown\n");
496 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
499 struct regulator_dev *rdev = dev_get_drvdata(dev);
501 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
503 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
504 regulator_suspend_mem_uV_show, NULL);
506 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
509 struct regulator_dev *rdev = dev_get_drvdata(dev);
511 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
513 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
514 regulator_suspend_disk_uV_show, NULL);
516 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
519 struct regulator_dev *rdev = dev_get_drvdata(dev);
521 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
523 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
524 regulator_suspend_standby_uV_show, NULL);
526 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
529 struct regulator_dev *rdev = dev_get_drvdata(dev);
531 return regulator_print_opmode(buf,
532 rdev->constraints->state_mem.mode);
534 static DEVICE_ATTR(suspend_mem_mode, 0444,
535 regulator_suspend_mem_mode_show, NULL);
537 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
540 struct regulator_dev *rdev = dev_get_drvdata(dev);
542 return regulator_print_opmode(buf,
543 rdev->constraints->state_disk.mode);
545 static DEVICE_ATTR(suspend_disk_mode, 0444,
546 regulator_suspend_disk_mode_show, NULL);
548 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
551 struct regulator_dev *rdev = dev_get_drvdata(dev);
553 return regulator_print_opmode(buf,
554 rdev->constraints->state_standby.mode);
556 static DEVICE_ATTR(suspend_standby_mode, 0444,
557 regulator_suspend_standby_mode_show, NULL);
559 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
560 struct device_attribute *attr, char *buf)
562 struct regulator_dev *rdev = dev_get_drvdata(dev);
564 return regulator_print_state(buf,
565 rdev->constraints->state_mem.enabled);
567 static DEVICE_ATTR(suspend_mem_state, 0444,
568 regulator_suspend_mem_state_show, NULL);
570 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
573 struct regulator_dev *rdev = dev_get_drvdata(dev);
575 return regulator_print_state(buf,
576 rdev->constraints->state_disk.enabled);
578 static DEVICE_ATTR(suspend_disk_state, 0444,
579 regulator_suspend_disk_state_show, NULL);
581 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
582 struct device_attribute *attr, char *buf)
584 struct regulator_dev *rdev = dev_get_drvdata(dev);
586 return regulator_print_state(buf,
587 rdev->constraints->state_standby.enabled);
589 static DEVICE_ATTR(suspend_standby_state, 0444,
590 regulator_suspend_standby_state_show, NULL);
592 static ssize_t regulator_bypass_show(struct device *dev,
593 struct device_attribute *attr, char *buf)
595 struct regulator_dev *rdev = dev_get_drvdata(dev);
596 const char *report;
597 bool bypass;
598 int ret;
600 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
602 if (ret != 0)
603 report = "unknown";
604 else if (bypass)
605 report = "enabled";
606 else
607 report = "disabled";
609 return sprintf(buf, "%s\n", report);
611 static DEVICE_ATTR(bypass, 0444,
612 regulator_bypass_show, NULL);
615 * These are the only attributes are present for all regulators.
616 * Other attributes are a function of regulator functionality.
618 static struct device_attribute regulator_dev_attrs[] = {
619 __ATTR(name, 0444, regulator_name_show, NULL),
620 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
621 __ATTR(type, 0444, regulator_type_show, NULL),
622 __ATTR_NULL,
625 static void regulator_dev_release(struct device *dev)
627 struct regulator_dev *rdev = dev_get_drvdata(dev);
628 kfree(rdev);
631 static struct class regulator_class = {
632 .name = "regulator",
633 .dev_release = regulator_dev_release,
634 .dev_attrs = regulator_dev_attrs,
637 /* Calculate the new optimum regulator operating mode based on the new total
638 * consumer load. All locks held by caller */
639 static void drms_uA_update(struct regulator_dev *rdev)
641 struct regulator *sibling;
642 int current_uA = 0, output_uV, input_uV, err;
643 unsigned int mode;
645 err = regulator_check_drms(rdev);
646 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
647 (!rdev->desc->ops->get_voltage &&
648 !rdev->desc->ops->get_voltage_sel) ||
649 !rdev->desc->ops->set_mode)
650 return;
652 /* get output voltage */
653 output_uV = _regulator_get_voltage(rdev);
654 if (output_uV <= 0)
655 return;
657 /* get input voltage */
658 input_uV = 0;
659 if (rdev->supply)
660 input_uV = regulator_get_voltage(rdev->supply);
661 if (input_uV <= 0)
662 input_uV = rdev->constraints->input_uV;
663 if (input_uV <= 0)
664 return;
666 /* calc total requested load */
667 list_for_each_entry(sibling, &rdev->consumer_list, list)
668 current_uA += sibling->uA_load;
670 /* now get the optimum mode for our new total regulator load */
671 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
672 output_uV, current_uA);
674 /* check the new mode is allowed */
675 err = regulator_mode_constrain(rdev, &mode);
676 if (err == 0)
677 rdev->desc->ops->set_mode(rdev, mode);
680 static int suspend_set_state(struct regulator_dev *rdev,
681 struct regulator_state *rstate)
683 int ret = 0;
685 /* If we have no suspend mode configration don't set anything;
686 * only warn if the driver implements set_suspend_voltage or
687 * set_suspend_mode callback.
689 if (!rstate->enabled && !rstate->disabled) {
690 if (rdev->desc->ops->set_suspend_voltage ||
691 rdev->desc->ops->set_suspend_mode)
692 rdev_warn(rdev, "No configuration\n");
693 return 0;
696 if (rstate->enabled && rstate->disabled) {
697 rdev_err(rdev, "invalid configuration\n");
698 return -EINVAL;
701 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
702 ret = rdev->desc->ops->set_suspend_enable(rdev);
703 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
704 ret = rdev->desc->ops->set_suspend_disable(rdev);
705 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
706 ret = 0;
708 if (ret < 0) {
709 rdev_err(rdev, "failed to enabled/disable\n");
710 return ret;
713 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
714 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
715 if (ret < 0) {
716 rdev_err(rdev, "failed to set voltage\n");
717 return ret;
721 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
722 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
723 if (ret < 0) {
724 rdev_err(rdev, "failed to set mode\n");
725 return ret;
728 return ret;
731 /* locks held by caller */
732 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
734 if (!rdev->constraints)
735 return -EINVAL;
737 switch (state) {
738 case PM_SUSPEND_STANDBY:
739 return suspend_set_state(rdev,
740 &rdev->constraints->state_standby);
741 case PM_SUSPEND_MEM:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_mem);
744 case PM_SUSPEND_MAX:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_disk);
747 default:
748 return -EINVAL;
752 static void print_constraints(struct regulator_dev *rdev)
754 struct regulation_constraints *constraints = rdev->constraints;
755 char buf[80] = "";
756 int count = 0;
757 int ret;
759 if (constraints->min_uV && constraints->max_uV) {
760 if (constraints->min_uV == constraints->max_uV)
761 count += sprintf(buf + count, "%d mV ",
762 constraints->min_uV / 1000);
763 else
764 count += sprintf(buf + count, "%d <--> %d mV ",
765 constraints->min_uV / 1000,
766 constraints->max_uV / 1000);
769 if (!constraints->min_uV ||
770 constraints->min_uV != constraints->max_uV) {
771 ret = _regulator_get_voltage(rdev);
772 if (ret > 0)
773 count += sprintf(buf + count, "at %d mV ", ret / 1000);
776 if (constraints->uV_offset)
777 count += sprintf(buf, "%dmV offset ",
778 constraints->uV_offset / 1000);
780 if (constraints->min_uA && constraints->max_uA) {
781 if (constraints->min_uA == constraints->max_uA)
782 count += sprintf(buf + count, "%d mA ",
783 constraints->min_uA / 1000);
784 else
785 count += sprintf(buf + count, "%d <--> %d mA ",
786 constraints->min_uA / 1000,
787 constraints->max_uA / 1000);
790 if (!constraints->min_uA ||
791 constraints->min_uA != constraints->max_uA) {
792 ret = _regulator_get_current_limit(rdev);
793 if (ret > 0)
794 count += sprintf(buf + count, "at %d mA ", ret / 1000);
797 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
798 count += sprintf(buf + count, "fast ");
799 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
800 count += sprintf(buf + count, "normal ");
801 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
802 count += sprintf(buf + count, "idle ");
803 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
804 count += sprintf(buf + count, "standby");
806 if (!count)
807 sprintf(buf, "no parameters");
809 rdev_info(rdev, "%s\n", buf);
811 if ((constraints->min_uV != constraints->max_uV) &&
812 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
813 rdev_warn(rdev,
814 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
817 static int machine_constraints_voltage(struct regulator_dev *rdev,
818 struct regulation_constraints *constraints)
820 struct regulator_ops *ops = rdev->desc->ops;
821 int ret;
823 /* do we need to apply the constraint voltage */
824 if (rdev->constraints->apply_uV &&
825 rdev->constraints->min_uV == rdev->constraints->max_uV) {
826 ret = _regulator_do_set_voltage(rdev,
827 rdev->constraints->min_uV,
828 rdev->constraints->max_uV);
829 if (ret < 0) {
830 rdev_err(rdev, "failed to apply %duV constraint\n",
831 rdev->constraints->min_uV);
832 return ret;
836 /* constrain machine-level voltage specs to fit
837 * the actual range supported by this regulator.
839 if (ops->list_voltage && rdev->desc->n_voltages) {
840 int count = rdev->desc->n_voltages;
841 int i;
842 int min_uV = INT_MAX;
843 int max_uV = INT_MIN;
844 int cmin = constraints->min_uV;
845 int cmax = constraints->max_uV;
847 /* it's safe to autoconfigure fixed-voltage supplies
848 and the constraints are used by list_voltage. */
849 if (count == 1 && !cmin) {
850 cmin = 1;
851 cmax = INT_MAX;
852 constraints->min_uV = cmin;
853 constraints->max_uV = cmax;
856 /* voltage constraints are optional */
857 if ((cmin == 0) && (cmax == 0))
858 return 0;
860 /* else require explicit machine-level constraints */
861 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
862 rdev_err(rdev, "invalid voltage constraints\n");
863 return -EINVAL;
866 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
867 for (i = 0; i < count; i++) {
868 int value;
870 value = ops->list_voltage(rdev, i);
871 if (value <= 0)
872 continue;
874 /* maybe adjust [min_uV..max_uV] */
875 if (value >= cmin && value < min_uV)
876 min_uV = value;
877 if (value <= cmax && value > max_uV)
878 max_uV = value;
881 /* final: [min_uV..max_uV] valid iff constraints valid */
882 if (max_uV < min_uV) {
883 rdev_err(rdev, "unsupportable voltage constraints\n");
884 return -EINVAL;
887 /* use regulator's subset of machine constraints */
888 if (constraints->min_uV < min_uV) {
889 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
890 constraints->min_uV, min_uV);
891 constraints->min_uV = min_uV;
893 if (constraints->max_uV > max_uV) {
894 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
895 constraints->max_uV, max_uV);
896 constraints->max_uV = max_uV;
900 return 0;
904 * set_machine_constraints - sets regulator constraints
905 * @rdev: regulator source
906 * @constraints: constraints to apply
908 * Allows platform initialisation code to define and constrain
909 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
910 * Constraints *must* be set by platform code in order for some
911 * regulator operations to proceed i.e. set_voltage, set_current_limit,
912 * set_mode.
914 static int set_machine_constraints(struct regulator_dev *rdev,
915 const struct regulation_constraints *constraints)
917 int ret = 0;
918 struct regulator_ops *ops = rdev->desc->ops;
920 if (constraints)
921 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
922 GFP_KERNEL);
923 else
924 rdev->constraints = kzalloc(sizeof(*constraints),
925 GFP_KERNEL);
926 if (!rdev->constraints)
927 return -ENOMEM;
929 ret = machine_constraints_voltage(rdev, rdev->constraints);
930 if (ret != 0)
931 goto out;
933 /* do we need to setup our suspend state */
934 if (rdev->constraints->initial_state) {
935 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
936 if (ret < 0) {
937 rdev_err(rdev, "failed to set suspend state\n");
938 goto out;
942 if (rdev->constraints->initial_mode) {
943 if (!ops->set_mode) {
944 rdev_err(rdev, "no set_mode operation\n");
945 ret = -EINVAL;
946 goto out;
949 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
950 if (ret < 0) {
951 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
952 goto out;
956 /* If the constraints say the regulator should be on at this point
957 * and we have control then make sure it is enabled.
959 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
960 ops->enable) {
961 ret = ops->enable(rdev);
962 if (ret < 0) {
963 rdev_err(rdev, "failed to enable\n");
964 goto out;
968 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
969 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
970 if (ret < 0) {
971 rdev_err(rdev, "failed to set ramp_delay\n");
972 goto out;
976 print_constraints(rdev);
977 return 0;
978 out:
979 kfree(rdev->constraints);
980 rdev->constraints = NULL;
981 return ret;
985 * set_supply - set regulator supply regulator
986 * @rdev: regulator name
987 * @supply_rdev: supply regulator name
989 * Called by platform initialisation code to set the supply regulator for this
990 * regulator. This ensures that a regulators supply will also be enabled by the
991 * core if it's child is enabled.
993 static int set_supply(struct regulator_dev *rdev,
994 struct regulator_dev *supply_rdev)
996 int err;
998 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1000 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1001 if (rdev->supply == NULL) {
1002 err = -ENOMEM;
1003 return err;
1005 supply_rdev->open_count++;
1007 return 0;
1011 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1012 * @rdev: regulator source
1013 * @consumer_dev_name: dev_name() string for device supply applies to
1014 * @supply: symbolic name for supply
1016 * Allows platform initialisation code to map physical regulator
1017 * sources to symbolic names for supplies for use by devices. Devices
1018 * should use these symbolic names to request regulators, avoiding the
1019 * need to provide board-specific regulator names as platform data.
1021 static int set_consumer_device_supply(struct regulator_dev *rdev,
1022 const char *consumer_dev_name,
1023 const char *supply)
1025 struct regulator_map *node;
1026 int has_dev;
1028 if (supply == NULL)
1029 return -EINVAL;
1031 if (consumer_dev_name != NULL)
1032 has_dev = 1;
1033 else
1034 has_dev = 0;
1036 list_for_each_entry(node, &regulator_map_list, list) {
1037 if (node->dev_name && consumer_dev_name) {
1038 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1039 continue;
1040 } else if (node->dev_name || consumer_dev_name) {
1041 continue;
1044 if (strcmp(node->supply, supply) != 0)
1045 continue;
1047 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1048 consumer_dev_name,
1049 dev_name(&node->regulator->dev),
1050 node->regulator->desc->name,
1051 supply,
1052 dev_name(&rdev->dev), rdev_get_name(rdev));
1053 return -EBUSY;
1056 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1057 if (node == NULL)
1058 return -ENOMEM;
1060 node->regulator = rdev;
1061 node->supply = supply;
1063 if (has_dev) {
1064 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1065 if (node->dev_name == NULL) {
1066 kfree(node);
1067 return -ENOMEM;
1071 list_add(&node->list, &regulator_map_list);
1072 return 0;
1075 static void unset_regulator_supplies(struct regulator_dev *rdev)
1077 struct regulator_map *node, *n;
1079 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1080 if (rdev == node->regulator) {
1081 list_del(&node->list);
1082 kfree(node->dev_name);
1083 kfree(node);
1088 #define REG_STR_SIZE 64
1090 static struct regulator *create_regulator(struct regulator_dev *rdev,
1091 struct device *dev,
1092 const char *supply_name)
1094 struct regulator *regulator;
1095 char buf[REG_STR_SIZE];
1096 int err, size;
1098 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1099 if (regulator == NULL)
1100 return NULL;
1102 mutex_lock(&rdev->mutex);
1103 regulator->rdev = rdev;
1104 list_add(&regulator->list, &rdev->consumer_list);
1106 if (dev) {
1107 regulator->dev = dev;
1109 /* Add a link to the device sysfs entry */
1110 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1111 dev->kobj.name, supply_name);
1112 if (size >= REG_STR_SIZE)
1113 goto overflow_err;
1115 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1116 if (regulator->supply_name == NULL)
1117 goto overflow_err;
1119 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1120 buf);
1121 if (err) {
1122 rdev_warn(rdev, "could not add device link %s err %d\n",
1123 dev->kobj.name, err);
1124 /* non-fatal */
1126 } else {
1127 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1128 if (regulator->supply_name == NULL)
1129 goto overflow_err;
1132 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1133 rdev->debugfs);
1134 if (!regulator->debugfs) {
1135 rdev_warn(rdev, "Failed to create debugfs directory\n");
1136 } else {
1137 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1138 &regulator->uA_load);
1139 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1140 &regulator->min_uV);
1141 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1142 &regulator->max_uV);
1146 * Check now if the regulator is an always on regulator - if
1147 * it is then we don't need to do nearly so much work for
1148 * enable/disable calls.
1150 if (!_regulator_can_change_status(rdev) &&
1151 _regulator_is_enabled(rdev))
1152 regulator->always_on = true;
1154 mutex_unlock(&rdev->mutex);
1155 return regulator;
1156 overflow_err:
1157 list_del(&regulator->list);
1158 kfree(regulator);
1159 mutex_unlock(&rdev->mutex);
1160 return NULL;
1163 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1165 if (!rdev->desc->ops->enable_time)
1166 return rdev->desc->enable_time;
1167 return rdev->desc->ops->enable_time(rdev);
1170 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1171 const char *supply,
1172 int *ret)
1174 struct regulator_dev *r;
1175 struct device_node *node;
1176 struct regulator_map *map;
1177 const char *devname = NULL;
1179 /* first do a dt based lookup */
1180 if (dev && dev->of_node) {
1181 node = of_get_regulator(dev, supply);
1182 if (node) {
1183 list_for_each_entry(r, &regulator_list, list)
1184 if (r->dev.parent &&
1185 node == r->dev.of_node)
1186 return r;
1187 } else {
1189 * If we couldn't even get the node then it's
1190 * not just that the device didn't register
1191 * yet, there's no node and we'll never
1192 * succeed.
1194 *ret = -ENODEV;
1198 /* if not found, try doing it non-dt way */
1199 if (dev)
1200 devname = dev_name(dev);
1202 list_for_each_entry(r, &regulator_list, list)
1203 if (strcmp(rdev_get_name(r), supply) == 0)
1204 return r;
1206 list_for_each_entry(map, &regulator_map_list, list) {
1207 /* If the mapping has a device set up it must match */
1208 if (map->dev_name &&
1209 (!devname || strcmp(map->dev_name, devname)))
1210 continue;
1212 if (strcmp(map->supply, supply) == 0)
1213 return map->regulator;
1217 return NULL;
1220 /* Internal regulator request function */
1221 static struct regulator *_regulator_get(struct device *dev, const char *id,
1222 int exclusive)
1224 struct regulator_dev *rdev;
1225 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1226 const char *devname = NULL;
1227 int ret;
1229 if (id == NULL) {
1230 pr_err("get() with no identifier\n");
1231 return regulator;
1234 if (dev)
1235 devname = dev_name(dev);
1237 mutex_lock(&regulator_list_mutex);
1239 rdev = regulator_dev_lookup(dev, id, &ret);
1240 if (rdev)
1241 goto found;
1243 if (board_wants_dummy_regulator) {
1244 rdev = dummy_regulator_rdev;
1245 goto found;
1248 #ifdef CONFIG_REGULATOR_DUMMY
1249 if (!devname)
1250 devname = "deviceless";
1252 /* If the board didn't flag that it was fully constrained then
1253 * substitute in a dummy regulator so consumers can continue.
1255 if (!has_full_constraints) {
1256 pr_warn("%s supply %s not found, using dummy regulator\n",
1257 devname, id);
1258 rdev = dummy_regulator_rdev;
1259 goto found;
1261 #endif
1263 mutex_unlock(&regulator_list_mutex);
1264 return regulator;
1266 found:
1267 if (rdev->exclusive) {
1268 regulator = ERR_PTR(-EPERM);
1269 goto out;
1272 if (exclusive && rdev->open_count) {
1273 regulator = ERR_PTR(-EBUSY);
1274 goto out;
1277 if (!try_module_get(rdev->owner))
1278 goto out;
1280 regulator = create_regulator(rdev, dev, id);
1281 if (regulator == NULL) {
1282 regulator = ERR_PTR(-ENOMEM);
1283 module_put(rdev->owner);
1284 goto out;
1287 rdev->open_count++;
1288 if (exclusive) {
1289 rdev->exclusive = 1;
1291 ret = _regulator_is_enabled(rdev);
1292 if (ret > 0)
1293 rdev->use_count = 1;
1294 else
1295 rdev->use_count = 0;
1298 out:
1299 mutex_unlock(&regulator_list_mutex);
1301 return regulator;
1305 * regulator_get - lookup and obtain a reference to a regulator.
1306 * @dev: device for regulator "consumer"
1307 * @id: Supply name or regulator ID.
1309 * Returns a struct regulator corresponding to the regulator producer,
1310 * or IS_ERR() condition containing errno.
1312 * Use of supply names configured via regulator_set_device_supply() is
1313 * strongly encouraged. It is recommended that the supply name used
1314 * should match the name used for the supply and/or the relevant
1315 * device pins in the datasheet.
1317 struct regulator *regulator_get(struct device *dev, const char *id)
1319 return _regulator_get(dev, id, 0);
1321 EXPORT_SYMBOL_GPL(regulator_get);
1323 static void devm_regulator_release(struct device *dev, void *res)
1325 regulator_put(*(struct regulator **)res);
1329 * devm_regulator_get - Resource managed regulator_get()
1330 * @dev: device for regulator "consumer"
1331 * @id: Supply name or regulator ID.
1333 * Managed regulator_get(). Regulators returned from this function are
1334 * automatically regulator_put() on driver detach. See regulator_get() for more
1335 * information.
1337 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1339 struct regulator **ptr, *regulator;
1341 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1342 if (!ptr)
1343 return ERR_PTR(-ENOMEM);
1345 regulator = regulator_get(dev, id);
1346 if (!IS_ERR(regulator)) {
1347 *ptr = regulator;
1348 devres_add(dev, ptr);
1349 } else {
1350 devres_free(ptr);
1353 return regulator;
1355 EXPORT_SYMBOL_GPL(devm_regulator_get);
1358 * regulator_get_exclusive - obtain exclusive access to a regulator.
1359 * @dev: device for regulator "consumer"
1360 * @id: Supply name or regulator ID.
1362 * Returns a struct regulator corresponding to the regulator producer,
1363 * or IS_ERR() condition containing errno. Other consumers will be
1364 * unable to obtain this reference is held and the use count for the
1365 * regulator will be initialised to reflect the current state of the
1366 * regulator.
1368 * This is intended for use by consumers which cannot tolerate shared
1369 * use of the regulator such as those which need to force the
1370 * regulator off for correct operation of the hardware they are
1371 * controlling.
1373 * Use of supply names configured via regulator_set_device_supply() is
1374 * strongly encouraged. It is recommended that the supply name used
1375 * should match the name used for the supply and/or the relevant
1376 * device pins in the datasheet.
1378 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1380 return _regulator_get(dev, id, 1);
1382 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1385 * regulator_put - "free" the regulator source
1386 * @regulator: regulator source
1388 * Note: drivers must ensure that all regulator_enable calls made on this
1389 * regulator source are balanced by regulator_disable calls prior to calling
1390 * this function.
1392 void regulator_put(struct regulator *regulator)
1394 struct regulator_dev *rdev;
1396 if (regulator == NULL || IS_ERR(regulator))
1397 return;
1399 mutex_lock(&regulator_list_mutex);
1400 rdev = regulator->rdev;
1402 debugfs_remove_recursive(regulator->debugfs);
1404 /* remove any sysfs entries */
1405 if (regulator->dev)
1406 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1407 kfree(regulator->supply_name);
1408 list_del(&regulator->list);
1409 kfree(regulator);
1411 rdev->open_count--;
1412 rdev->exclusive = 0;
1414 module_put(rdev->owner);
1415 mutex_unlock(&regulator_list_mutex);
1417 EXPORT_SYMBOL_GPL(regulator_put);
1419 static int devm_regulator_match(struct device *dev, void *res, void *data)
1421 struct regulator **r = res;
1422 if (!r || !*r) {
1423 WARN_ON(!r || !*r);
1424 return 0;
1426 return *r == data;
1430 * devm_regulator_put - Resource managed regulator_put()
1431 * @regulator: regulator to free
1433 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1434 * this function will not need to be called and the resource management
1435 * code will ensure that the resource is freed.
1437 void devm_regulator_put(struct regulator *regulator)
1439 int rc;
1441 rc = devres_release(regulator->dev, devm_regulator_release,
1442 devm_regulator_match, regulator);
1443 if (rc != 0)
1444 WARN_ON(rc);
1446 EXPORT_SYMBOL_GPL(devm_regulator_put);
1448 static int _regulator_do_enable(struct regulator_dev *rdev)
1450 int ret, delay;
1452 /* Query before enabling in case configuration dependent. */
1453 ret = _regulator_get_enable_time(rdev);
1454 if (ret >= 0) {
1455 delay = ret;
1456 } else {
1457 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1458 delay = 0;
1461 trace_regulator_enable(rdev_get_name(rdev));
1463 if (rdev->ena_gpio) {
1464 gpio_set_value_cansleep(rdev->ena_gpio,
1465 !rdev->ena_gpio_invert);
1466 rdev->ena_gpio_state = 1;
1467 } else if (rdev->desc->ops->enable) {
1468 ret = rdev->desc->ops->enable(rdev);
1469 if (ret < 0)
1470 return ret;
1471 } else {
1472 return -EINVAL;
1475 /* Allow the regulator to ramp; it would be useful to extend
1476 * this for bulk operations so that the regulators can ramp
1477 * together. */
1478 trace_regulator_enable_delay(rdev_get_name(rdev));
1480 if (delay >= 1000) {
1481 mdelay(delay / 1000);
1482 udelay(delay % 1000);
1483 } else if (delay) {
1484 udelay(delay);
1487 trace_regulator_enable_complete(rdev_get_name(rdev));
1489 return 0;
1492 /* locks held by regulator_enable() */
1493 static int _regulator_enable(struct regulator_dev *rdev)
1495 int ret;
1497 /* check voltage and requested load before enabling */
1498 if (rdev->constraints &&
1499 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1500 drms_uA_update(rdev);
1502 if (rdev->use_count == 0) {
1503 /* The regulator may on if it's not switchable or left on */
1504 ret = _regulator_is_enabled(rdev);
1505 if (ret == -EINVAL || ret == 0) {
1506 if (!_regulator_can_change_status(rdev))
1507 return -EPERM;
1509 ret = _regulator_do_enable(rdev);
1510 if (ret < 0)
1511 return ret;
1513 } else if (ret < 0) {
1514 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1515 return ret;
1517 /* Fallthrough on positive return values - already enabled */
1520 rdev->use_count++;
1522 return 0;
1526 * regulator_enable - enable regulator output
1527 * @regulator: regulator source
1529 * Request that the regulator be enabled with the regulator output at
1530 * the predefined voltage or current value. Calls to regulator_enable()
1531 * must be balanced with calls to regulator_disable().
1533 * NOTE: the output value can be set by other drivers, boot loader or may be
1534 * hardwired in the regulator.
1536 int regulator_enable(struct regulator *regulator)
1538 struct regulator_dev *rdev = regulator->rdev;
1539 int ret = 0;
1541 if (regulator->always_on)
1542 return 0;
1544 if (rdev->supply) {
1545 ret = regulator_enable(rdev->supply);
1546 if (ret != 0)
1547 return ret;
1550 mutex_lock(&rdev->mutex);
1551 ret = _regulator_enable(rdev);
1552 mutex_unlock(&rdev->mutex);
1554 if (ret != 0 && rdev->supply)
1555 regulator_disable(rdev->supply);
1557 return ret;
1559 EXPORT_SYMBOL_GPL(regulator_enable);
1561 static int _regulator_do_disable(struct regulator_dev *rdev)
1563 int ret;
1565 trace_regulator_disable(rdev_get_name(rdev));
1567 if (rdev->ena_gpio) {
1568 gpio_set_value_cansleep(rdev->ena_gpio,
1569 rdev->ena_gpio_invert);
1570 rdev->ena_gpio_state = 0;
1572 } else if (rdev->desc->ops->disable) {
1573 ret = rdev->desc->ops->disable(rdev);
1574 if (ret != 0)
1575 return ret;
1578 trace_regulator_disable_complete(rdev_get_name(rdev));
1580 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1581 NULL);
1582 return 0;
1585 /* locks held by regulator_disable() */
1586 static int _regulator_disable(struct regulator_dev *rdev)
1588 int ret = 0;
1590 if (WARN(rdev->use_count <= 0,
1591 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1592 return -EIO;
1594 /* are we the last user and permitted to disable ? */
1595 if (rdev->use_count == 1 &&
1596 (rdev->constraints && !rdev->constraints->always_on)) {
1598 /* we are last user */
1599 if (_regulator_can_change_status(rdev)) {
1600 ret = _regulator_do_disable(rdev);
1601 if (ret < 0) {
1602 rdev_err(rdev, "failed to disable\n");
1603 return ret;
1607 rdev->use_count = 0;
1608 } else if (rdev->use_count > 1) {
1610 if (rdev->constraints &&
1611 (rdev->constraints->valid_ops_mask &
1612 REGULATOR_CHANGE_DRMS))
1613 drms_uA_update(rdev);
1615 rdev->use_count--;
1618 return ret;
1622 * regulator_disable - disable regulator output
1623 * @regulator: regulator source
1625 * Disable the regulator output voltage or current. Calls to
1626 * regulator_enable() must be balanced with calls to
1627 * regulator_disable().
1629 * NOTE: this will only disable the regulator output if no other consumer
1630 * devices have it enabled, the regulator device supports disabling and
1631 * machine constraints permit this operation.
1633 int regulator_disable(struct regulator *regulator)
1635 struct regulator_dev *rdev = regulator->rdev;
1636 int ret = 0;
1638 if (regulator->always_on)
1639 return 0;
1641 mutex_lock(&rdev->mutex);
1642 ret = _regulator_disable(rdev);
1643 mutex_unlock(&rdev->mutex);
1645 if (ret == 0 && rdev->supply)
1646 regulator_disable(rdev->supply);
1648 return ret;
1650 EXPORT_SYMBOL_GPL(regulator_disable);
1652 /* locks held by regulator_force_disable() */
1653 static int _regulator_force_disable(struct regulator_dev *rdev)
1655 int ret = 0;
1657 /* force disable */
1658 if (rdev->desc->ops->disable) {
1659 /* ah well, who wants to live forever... */
1660 ret = rdev->desc->ops->disable(rdev);
1661 if (ret < 0) {
1662 rdev_err(rdev, "failed to force disable\n");
1663 return ret;
1665 /* notify other consumers that power has been forced off */
1666 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1667 REGULATOR_EVENT_DISABLE, NULL);
1670 return ret;
1674 * regulator_force_disable - force disable regulator output
1675 * @regulator: regulator source
1677 * Forcibly disable the regulator output voltage or current.
1678 * NOTE: this *will* disable the regulator output even if other consumer
1679 * devices have it enabled. This should be used for situations when device
1680 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1682 int regulator_force_disable(struct regulator *regulator)
1684 struct regulator_dev *rdev = regulator->rdev;
1685 int ret;
1687 mutex_lock(&rdev->mutex);
1688 regulator->uA_load = 0;
1689 ret = _regulator_force_disable(regulator->rdev);
1690 mutex_unlock(&rdev->mutex);
1692 if (rdev->supply)
1693 while (rdev->open_count--)
1694 regulator_disable(rdev->supply);
1696 return ret;
1698 EXPORT_SYMBOL_GPL(regulator_force_disable);
1700 static void regulator_disable_work(struct work_struct *work)
1702 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1703 disable_work.work);
1704 int count, i, ret;
1706 mutex_lock(&rdev->mutex);
1708 BUG_ON(!rdev->deferred_disables);
1710 count = rdev->deferred_disables;
1711 rdev->deferred_disables = 0;
1713 for (i = 0; i < count; i++) {
1714 ret = _regulator_disable(rdev);
1715 if (ret != 0)
1716 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1719 mutex_unlock(&rdev->mutex);
1721 if (rdev->supply) {
1722 for (i = 0; i < count; i++) {
1723 ret = regulator_disable(rdev->supply);
1724 if (ret != 0) {
1725 rdev_err(rdev,
1726 "Supply disable failed: %d\n", ret);
1733 * regulator_disable_deferred - disable regulator output with delay
1734 * @regulator: regulator source
1735 * @ms: miliseconds until the regulator is disabled
1737 * Execute regulator_disable() on the regulator after a delay. This
1738 * is intended for use with devices that require some time to quiesce.
1740 * NOTE: this will only disable the regulator output if no other consumer
1741 * devices have it enabled, the regulator device supports disabling and
1742 * machine constraints permit this operation.
1744 int regulator_disable_deferred(struct regulator *regulator, int ms)
1746 struct regulator_dev *rdev = regulator->rdev;
1747 int ret;
1749 if (regulator->always_on)
1750 return 0;
1752 if (!ms)
1753 return regulator_disable(regulator);
1755 mutex_lock(&rdev->mutex);
1756 rdev->deferred_disables++;
1757 mutex_unlock(&rdev->mutex);
1759 ret = schedule_delayed_work(&rdev->disable_work,
1760 msecs_to_jiffies(ms));
1761 if (ret < 0)
1762 return ret;
1763 else
1764 return 0;
1766 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1769 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1771 * @rdev: regulator to operate on
1773 * Regulators that use regmap for their register I/O can set the
1774 * enable_reg and enable_mask fields in their descriptor and then use
1775 * this as their is_enabled operation, saving some code.
1777 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1779 unsigned int val;
1780 int ret;
1782 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1783 if (ret != 0)
1784 return ret;
1786 return (val & rdev->desc->enable_mask) != 0;
1788 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1791 * regulator_enable_regmap - standard enable() for regmap users
1793 * @rdev: regulator to operate on
1795 * Regulators that use regmap for their register I/O can set the
1796 * enable_reg and enable_mask fields in their descriptor and then use
1797 * this as their enable() operation, saving some code.
1799 int regulator_enable_regmap(struct regulator_dev *rdev)
1801 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1802 rdev->desc->enable_mask,
1803 rdev->desc->enable_mask);
1805 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1808 * regulator_disable_regmap - standard disable() for regmap users
1810 * @rdev: regulator to operate on
1812 * Regulators that use regmap for their register I/O can set the
1813 * enable_reg and enable_mask fields in their descriptor and then use
1814 * this as their disable() operation, saving some code.
1816 int regulator_disable_regmap(struct regulator_dev *rdev)
1818 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1819 rdev->desc->enable_mask, 0);
1821 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1823 static int _regulator_is_enabled(struct regulator_dev *rdev)
1825 /* A GPIO control always takes precedence */
1826 if (rdev->ena_gpio)
1827 return rdev->ena_gpio_state;
1829 /* If we don't know then assume that the regulator is always on */
1830 if (!rdev->desc->ops->is_enabled)
1831 return 1;
1833 return rdev->desc->ops->is_enabled(rdev);
1837 * regulator_is_enabled - is the regulator output enabled
1838 * @regulator: regulator source
1840 * Returns positive if the regulator driver backing the source/client
1841 * has requested that the device be enabled, zero if it hasn't, else a
1842 * negative errno code.
1844 * Note that the device backing this regulator handle can have multiple
1845 * users, so it might be enabled even if regulator_enable() was never
1846 * called for this particular source.
1848 int regulator_is_enabled(struct regulator *regulator)
1850 int ret;
1852 if (regulator->always_on)
1853 return 1;
1855 mutex_lock(&regulator->rdev->mutex);
1856 ret = _regulator_is_enabled(regulator->rdev);
1857 mutex_unlock(&regulator->rdev->mutex);
1859 return ret;
1861 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1864 * regulator_count_voltages - count regulator_list_voltage() selectors
1865 * @regulator: regulator source
1867 * Returns number of selectors, or negative errno. Selectors are
1868 * numbered starting at zero, and typically correspond to bitfields
1869 * in hardware registers.
1871 int regulator_count_voltages(struct regulator *regulator)
1873 struct regulator_dev *rdev = regulator->rdev;
1875 return rdev->desc->n_voltages ? : -EINVAL;
1877 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1880 * regulator_list_voltage_linear - List voltages with simple calculation
1882 * @rdev: Regulator device
1883 * @selector: Selector to convert into a voltage
1885 * Regulators with a simple linear mapping between voltages and
1886 * selectors can set min_uV and uV_step in the regulator descriptor
1887 * and then use this function as their list_voltage() operation,
1889 int regulator_list_voltage_linear(struct regulator_dev *rdev,
1890 unsigned int selector)
1892 if (selector >= rdev->desc->n_voltages)
1893 return -EINVAL;
1895 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1897 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1900 * regulator_list_voltage_table - List voltages with table based mapping
1902 * @rdev: Regulator device
1903 * @selector: Selector to convert into a voltage
1905 * Regulators with table based mapping between voltages and
1906 * selectors can set volt_table in the regulator descriptor
1907 * and then use this function as their list_voltage() operation.
1909 int regulator_list_voltage_table(struct regulator_dev *rdev,
1910 unsigned int selector)
1912 if (!rdev->desc->volt_table) {
1913 BUG_ON(!rdev->desc->volt_table);
1914 return -EINVAL;
1917 if (selector >= rdev->desc->n_voltages)
1918 return -EINVAL;
1920 return rdev->desc->volt_table[selector];
1922 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1925 * regulator_list_voltage - enumerate supported voltages
1926 * @regulator: regulator source
1927 * @selector: identify voltage to list
1928 * Context: can sleep
1930 * Returns a voltage that can be passed to @regulator_set_voltage(),
1931 * zero if this selector code can't be used on this system, or a
1932 * negative errno.
1934 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1936 struct regulator_dev *rdev = regulator->rdev;
1937 struct regulator_ops *ops = rdev->desc->ops;
1938 int ret;
1940 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1941 return -EINVAL;
1943 mutex_lock(&rdev->mutex);
1944 ret = ops->list_voltage(rdev, selector);
1945 mutex_unlock(&rdev->mutex);
1947 if (ret > 0) {
1948 if (ret < rdev->constraints->min_uV)
1949 ret = 0;
1950 else if (ret > rdev->constraints->max_uV)
1951 ret = 0;
1954 return ret;
1956 EXPORT_SYMBOL_GPL(regulator_list_voltage);
1959 * regulator_is_supported_voltage - check if a voltage range can be supported
1961 * @regulator: Regulator to check.
1962 * @min_uV: Minimum required voltage in uV.
1963 * @max_uV: Maximum required voltage in uV.
1965 * Returns a boolean or a negative error code.
1967 int regulator_is_supported_voltage(struct regulator *regulator,
1968 int min_uV, int max_uV)
1970 struct regulator_dev *rdev = regulator->rdev;
1971 int i, voltages, ret;
1973 /* If we can't change voltage check the current voltage */
1974 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1975 ret = regulator_get_voltage(regulator);
1976 if (ret >= 0)
1977 return (min_uV >= ret && ret <= max_uV);
1978 else
1979 return ret;
1982 ret = regulator_count_voltages(regulator);
1983 if (ret < 0)
1984 return ret;
1985 voltages = ret;
1987 for (i = 0; i < voltages; i++) {
1988 ret = regulator_list_voltage(regulator, i);
1990 if (ret >= min_uV && ret <= max_uV)
1991 return 1;
1994 return 0;
1996 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
1999 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2001 * @rdev: regulator to operate on
2003 * Regulators that use regmap for their register I/O can set the
2004 * vsel_reg and vsel_mask fields in their descriptor and then use this
2005 * as their get_voltage_vsel operation, saving some code.
2007 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2009 unsigned int val;
2010 int ret;
2012 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2013 if (ret != 0)
2014 return ret;
2016 val &= rdev->desc->vsel_mask;
2017 val >>= ffs(rdev->desc->vsel_mask) - 1;
2019 return val;
2021 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2024 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2026 * @rdev: regulator to operate on
2027 * @sel: Selector to set
2029 * Regulators that use regmap for their register I/O can set the
2030 * vsel_reg and vsel_mask fields in their descriptor and then use this
2031 * as their set_voltage_vsel operation, saving some code.
2033 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2035 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2037 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2038 rdev->desc->vsel_mask, sel);
2040 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2043 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2045 * @rdev: Regulator to operate on
2046 * @min_uV: Lower bound for voltage
2047 * @max_uV: Upper bound for voltage
2049 * Drivers implementing set_voltage_sel() and list_voltage() can use
2050 * this as their map_voltage() operation. It will find a suitable
2051 * voltage by calling list_voltage() until it gets something in bounds
2052 * for the requested voltages.
2054 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2055 int min_uV, int max_uV)
2057 int best_val = INT_MAX;
2058 int selector = 0;
2059 int i, ret;
2061 /* Find the smallest voltage that falls within the specified
2062 * range.
2064 for (i = 0; i < rdev->desc->n_voltages; i++) {
2065 ret = rdev->desc->ops->list_voltage(rdev, i);
2066 if (ret < 0)
2067 continue;
2069 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2070 best_val = ret;
2071 selector = i;
2075 if (best_val != INT_MAX)
2076 return selector;
2077 else
2078 return -EINVAL;
2080 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2083 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2085 * @rdev: Regulator to operate on
2086 * @min_uV: Lower bound for voltage
2087 * @max_uV: Upper bound for voltage
2089 * Drivers providing min_uV and uV_step in their regulator_desc can
2090 * use this as their map_voltage() operation.
2092 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2093 int min_uV, int max_uV)
2095 int ret, voltage;
2097 /* Allow uV_step to be 0 for fixed voltage */
2098 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2099 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2100 return 0;
2101 else
2102 return -EINVAL;
2105 if (!rdev->desc->uV_step) {
2106 BUG_ON(!rdev->desc->uV_step);
2107 return -EINVAL;
2110 if (min_uV < rdev->desc->min_uV)
2111 min_uV = rdev->desc->min_uV;
2113 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2114 if (ret < 0)
2115 return ret;
2117 /* Map back into a voltage to verify we're still in bounds */
2118 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2119 if (voltage < min_uV || voltage > max_uV)
2120 return -EINVAL;
2122 return ret;
2124 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2126 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2127 int min_uV, int max_uV)
2129 int ret;
2130 int delay = 0;
2131 int best_val = 0;
2132 unsigned int selector;
2133 int old_selector = -1;
2135 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2137 min_uV += rdev->constraints->uV_offset;
2138 max_uV += rdev->constraints->uV_offset;
2141 * If we can't obtain the old selector there is not enough
2142 * info to call set_voltage_time_sel().
2144 if (_regulator_is_enabled(rdev) &&
2145 rdev->desc->ops->set_voltage_time_sel &&
2146 rdev->desc->ops->get_voltage_sel) {
2147 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2148 if (old_selector < 0)
2149 return old_selector;
2152 if (rdev->desc->ops->set_voltage) {
2153 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2154 &selector);
2156 if (ret >= 0) {
2157 if (rdev->desc->ops->list_voltage)
2158 best_val = rdev->desc->ops->list_voltage(rdev,
2159 selector);
2160 else
2161 best_val = _regulator_get_voltage(rdev);
2164 } else if (rdev->desc->ops->set_voltage_sel) {
2165 if (rdev->desc->ops->map_voltage) {
2166 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2167 max_uV);
2168 } else {
2169 if (rdev->desc->ops->list_voltage ==
2170 regulator_list_voltage_linear)
2171 ret = regulator_map_voltage_linear(rdev,
2172 min_uV, max_uV);
2173 else
2174 ret = regulator_map_voltage_iterate(rdev,
2175 min_uV, max_uV);
2178 if (ret >= 0) {
2179 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2180 if (min_uV <= best_val && max_uV >= best_val) {
2181 selector = ret;
2182 ret = rdev->desc->ops->set_voltage_sel(rdev,
2183 ret);
2184 } else {
2185 ret = -EINVAL;
2188 } else {
2189 ret = -EINVAL;
2192 /* Call set_voltage_time_sel if successfully obtained old_selector */
2193 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2194 rdev->desc->ops->set_voltage_time_sel) {
2196 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2197 old_selector, selector);
2198 if (delay < 0) {
2199 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2200 delay);
2201 delay = 0;
2204 /* Insert any necessary delays */
2205 if (delay >= 1000) {
2206 mdelay(delay / 1000);
2207 udelay(delay % 1000);
2208 } else if (delay) {
2209 udelay(delay);
2213 if (ret == 0 && best_val >= 0) {
2214 unsigned long data = best_val;
2216 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2217 (void *)data);
2220 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2222 return ret;
2226 * regulator_set_voltage - set regulator output voltage
2227 * @regulator: regulator source
2228 * @min_uV: Minimum required voltage in uV
2229 * @max_uV: Maximum acceptable voltage in uV
2231 * Sets a voltage regulator to the desired output voltage. This can be set
2232 * during any regulator state. IOW, regulator can be disabled or enabled.
2234 * If the regulator is enabled then the voltage will change to the new value
2235 * immediately otherwise if the regulator is disabled the regulator will
2236 * output at the new voltage when enabled.
2238 * NOTE: If the regulator is shared between several devices then the lowest
2239 * request voltage that meets the system constraints will be used.
2240 * Regulator system constraints must be set for this regulator before
2241 * calling this function otherwise this call will fail.
2243 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2245 struct regulator_dev *rdev = regulator->rdev;
2246 int ret = 0;
2248 mutex_lock(&rdev->mutex);
2250 /* If we're setting the same range as last time the change
2251 * should be a noop (some cpufreq implementations use the same
2252 * voltage for multiple frequencies, for example).
2254 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2255 goto out;
2257 /* sanity check */
2258 if (!rdev->desc->ops->set_voltage &&
2259 !rdev->desc->ops->set_voltage_sel) {
2260 ret = -EINVAL;
2261 goto out;
2264 /* constraints check */
2265 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2266 if (ret < 0)
2267 goto out;
2268 regulator->min_uV = min_uV;
2269 regulator->max_uV = max_uV;
2271 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2272 if (ret < 0)
2273 goto out;
2275 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2277 out:
2278 mutex_unlock(&rdev->mutex);
2279 return ret;
2281 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2284 * regulator_set_voltage_time - get raise/fall time
2285 * @regulator: regulator source
2286 * @old_uV: starting voltage in microvolts
2287 * @new_uV: target voltage in microvolts
2289 * Provided with the starting and ending voltage, this function attempts to
2290 * calculate the time in microseconds required to rise or fall to this new
2291 * voltage.
2293 int regulator_set_voltage_time(struct regulator *regulator,
2294 int old_uV, int new_uV)
2296 struct regulator_dev *rdev = regulator->rdev;
2297 struct regulator_ops *ops = rdev->desc->ops;
2298 int old_sel = -1;
2299 int new_sel = -1;
2300 int voltage;
2301 int i;
2303 /* Currently requires operations to do this */
2304 if (!ops->list_voltage || !ops->set_voltage_time_sel
2305 || !rdev->desc->n_voltages)
2306 return -EINVAL;
2308 for (i = 0; i < rdev->desc->n_voltages; i++) {
2309 /* We only look for exact voltage matches here */
2310 voltage = regulator_list_voltage(regulator, i);
2311 if (voltage < 0)
2312 return -EINVAL;
2313 if (voltage == 0)
2314 continue;
2315 if (voltage == old_uV)
2316 old_sel = i;
2317 if (voltage == new_uV)
2318 new_sel = i;
2321 if (old_sel < 0 || new_sel < 0)
2322 return -EINVAL;
2324 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2326 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2329 * regulator_set_voltage_time_sel - get raise/fall time
2330 * @rdev: regulator source device
2331 * @old_selector: selector for starting voltage
2332 * @new_selector: selector for target voltage
2334 * Provided with the starting and target voltage selectors, this function
2335 * returns time in microseconds required to rise or fall to this new voltage
2337 * Drivers providing ramp_delay in regulation_constraints can use this as their
2338 * set_voltage_time_sel() operation.
2340 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2341 unsigned int old_selector,
2342 unsigned int new_selector)
2344 unsigned int ramp_delay = 0;
2345 int old_volt, new_volt;
2347 if (rdev->constraints->ramp_delay)
2348 ramp_delay = rdev->constraints->ramp_delay;
2349 else if (rdev->desc->ramp_delay)
2350 ramp_delay = rdev->desc->ramp_delay;
2352 if (ramp_delay == 0) {
2353 rdev_warn(rdev, "ramp_delay not set\n");
2354 return 0;
2357 /* sanity check */
2358 if (!rdev->desc->ops->list_voltage)
2359 return -EINVAL;
2361 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2362 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2364 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2366 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2369 * regulator_sync_voltage - re-apply last regulator output voltage
2370 * @regulator: regulator source
2372 * Re-apply the last configured voltage. This is intended to be used
2373 * where some external control source the consumer is cooperating with
2374 * has caused the configured voltage to change.
2376 int regulator_sync_voltage(struct regulator *regulator)
2378 struct regulator_dev *rdev = regulator->rdev;
2379 int ret, min_uV, max_uV;
2381 mutex_lock(&rdev->mutex);
2383 if (!rdev->desc->ops->set_voltage &&
2384 !rdev->desc->ops->set_voltage_sel) {
2385 ret = -EINVAL;
2386 goto out;
2389 /* This is only going to work if we've had a voltage configured. */
2390 if (!regulator->min_uV && !regulator->max_uV) {
2391 ret = -EINVAL;
2392 goto out;
2395 min_uV = regulator->min_uV;
2396 max_uV = regulator->max_uV;
2398 /* This should be a paranoia check... */
2399 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2400 if (ret < 0)
2401 goto out;
2403 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2404 if (ret < 0)
2405 goto out;
2407 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2409 out:
2410 mutex_unlock(&rdev->mutex);
2411 return ret;
2413 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2415 static int _regulator_get_voltage(struct regulator_dev *rdev)
2417 int sel, ret;
2419 if (rdev->desc->ops->get_voltage_sel) {
2420 sel = rdev->desc->ops->get_voltage_sel(rdev);
2421 if (sel < 0)
2422 return sel;
2423 ret = rdev->desc->ops->list_voltage(rdev, sel);
2424 } else if (rdev->desc->ops->get_voltage) {
2425 ret = rdev->desc->ops->get_voltage(rdev);
2426 } else if (rdev->desc->ops->list_voltage) {
2427 ret = rdev->desc->ops->list_voltage(rdev, 0);
2428 } else {
2429 return -EINVAL;
2432 if (ret < 0)
2433 return ret;
2434 return ret - rdev->constraints->uV_offset;
2438 * regulator_get_voltage - get regulator output voltage
2439 * @regulator: regulator source
2441 * This returns the current regulator voltage in uV.
2443 * NOTE: If the regulator is disabled it will return the voltage value. This
2444 * function should not be used to determine regulator state.
2446 int regulator_get_voltage(struct regulator *regulator)
2448 int ret;
2450 mutex_lock(&regulator->rdev->mutex);
2452 ret = _regulator_get_voltage(regulator->rdev);
2454 mutex_unlock(&regulator->rdev->mutex);
2456 return ret;
2458 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2461 * regulator_set_current_limit - set regulator output current limit
2462 * @regulator: regulator source
2463 * @min_uA: Minimuum supported current in uA
2464 * @max_uA: Maximum supported current in uA
2466 * Sets current sink to the desired output current. This can be set during
2467 * any regulator state. IOW, regulator can be disabled or enabled.
2469 * If the regulator is enabled then the current will change to the new value
2470 * immediately otherwise if the regulator is disabled the regulator will
2471 * output at the new current when enabled.
2473 * NOTE: Regulator system constraints must be set for this regulator before
2474 * calling this function otherwise this call will fail.
2476 int regulator_set_current_limit(struct regulator *regulator,
2477 int min_uA, int max_uA)
2479 struct regulator_dev *rdev = regulator->rdev;
2480 int ret;
2482 mutex_lock(&rdev->mutex);
2484 /* sanity check */
2485 if (!rdev->desc->ops->set_current_limit) {
2486 ret = -EINVAL;
2487 goto out;
2490 /* constraints check */
2491 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2492 if (ret < 0)
2493 goto out;
2495 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2496 out:
2497 mutex_unlock(&rdev->mutex);
2498 return ret;
2500 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2502 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2504 int ret;
2506 mutex_lock(&rdev->mutex);
2508 /* sanity check */
2509 if (!rdev->desc->ops->get_current_limit) {
2510 ret = -EINVAL;
2511 goto out;
2514 ret = rdev->desc->ops->get_current_limit(rdev);
2515 out:
2516 mutex_unlock(&rdev->mutex);
2517 return ret;
2521 * regulator_get_current_limit - get regulator output current
2522 * @regulator: regulator source
2524 * This returns the current supplied by the specified current sink in uA.
2526 * NOTE: If the regulator is disabled it will return the current value. This
2527 * function should not be used to determine regulator state.
2529 int regulator_get_current_limit(struct regulator *regulator)
2531 return _regulator_get_current_limit(regulator->rdev);
2533 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2536 * regulator_set_mode - set regulator operating mode
2537 * @regulator: regulator source
2538 * @mode: operating mode - one of the REGULATOR_MODE constants
2540 * Set regulator operating mode to increase regulator efficiency or improve
2541 * regulation performance.
2543 * NOTE: Regulator system constraints must be set for this regulator before
2544 * calling this function otherwise this call will fail.
2546 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2548 struct regulator_dev *rdev = regulator->rdev;
2549 int ret;
2550 int regulator_curr_mode;
2552 mutex_lock(&rdev->mutex);
2554 /* sanity check */
2555 if (!rdev->desc->ops->set_mode) {
2556 ret = -EINVAL;
2557 goto out;
2560 /* return if the same mode is requested */
2561 if (rdev->desc->ops->get_mode) {
2562 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2563 if (regulator_curr_mode == mode) {
2564 ret = 0;
2565 goto out;
2569 /* constraints check */
2570 ret = regulator_mode_constrain(rdev, &mode);
2571 if (ret < 0)
2572 goto out;
2574 ret = rdev->desc->ops->set_mode(rdev, mode);
2575 out:
2576 mutex_unlock(&rdev->mutex);
2577 return ret;
2579 EXPORT_SYMBOL_GPL(regulator_set_mode);
2581 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2583 int ret;
2585 mutex_lock(&rdev->mutex);
2587 /* sanity check */
2588 if (!rdev->desc->ops->get_mode) {
2589 ret = -EINVAL;
2590 goto out;
2593 ret = rdev->desc->ops->get_mode(rdev);
2594 out:
2595 mutex_unlock(&rdev->mutex);
2596 return ret;
2600 * regulator_get_mode - get regulator operating mode
2601 * @regulator: regulator source
2603 * Get the current regulator operating mode.
2605 unsigned int regulator_get_mode(struct regulator *regulator)
2607 return _regulator_get_mode(regulator->rdev);
2609 EXPORT_SYMBOL_GPL(regulator_get_mode);
2612 * regulator_set_optimum_mode - set regulator optimum operating mode
2613 * @regulator: regulator source
2614 * @uA_load: load current
2616 * Notifies the regulator core of a new device load. This is then used by
2617 * DRMS (if enabled by constraints) to set the most efficient regulator
2618 * operating mode for the new regulator loading.
2620 * Consumer devices notify their supply regulator of the maximum power
2621 * they will require (can be taken from device datasheet in the power
2622 * consumption tables) when they change operational status and hence power
2623 * state. Examples of operational state changes that can affect power
2624 * consumption are :-
2626 * o Device is opened / closed.
2627 * o Device I/O is about to begin or has just finished.
2628 * o Device is idling in between work.
2630 * This information is also exported via sysfs to userspace.
2632 * DRMS will sum the total requested load on the regulator and change
2633 * to the most efficient operating mode if platform constraints allow.
2635 * Returns the new regulator mode or error.
2637 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2639 struct regulator_dev *rdev = regulator->rdev;
2640 struct regulator *consumer;
2641 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2642 unsigned int mode;
2644 if (rdev->supply)
2645 input_uV = regulator_get_voltage(rdev->supply);
2647 mutex_lock(&rdev->mutex);
2650 * first check to see if we can set modes at all, otherwise just
2651 * tell the consumer everything is OK.
2653 regulator->uA_load = uA_load;
2654 ret = regulator_check_drms(rdev);
2655 if (ret < 0) {
2656 ret = 0;
2657 goto out;
2660 if (!rdev->desc->ops->get_optimum_mode)
2661 goto out;
2664 * we can actually do this so any errors are indicators of
2665 * potential real failure.
2667 ret = -EINVAL;
2669 if (!rdev->desc->ops->set_mode)
2670 goto out;
2672 /* get output voltage */
2673 output_uV = _regulator_get_voltage(rdev);
2674 if (output_uV <= 0) {
2675 rdev_err(rdev, "invalid output voltage found\n");
2676 goto out;
2679 /* No supply? Use constraint voltage */
2680 if (input_uV <= 0)
2681 input_uV = rdev->constraints->input_uV;
2682 if (input_uV <= 0) {
2683 rdev_err(rdev, "invalid input voltage found\n");
2684 goto out;
2687 /* calc total requested load for this regulator */
2688 list_for_each_entry(consumer, &rdev->consumer_list, list)
2689 total_uA_load += consumer->uA_load;
2691 mode = rdev->desc->ops->get_optimum_mode(rdev,
2692 input_uV, output_uV,
2693 total_uA_load);
2694 ret = regulator_mode_constrain(rdev, &mode);
2695 if (ret < 0) {
2696 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2697 total_uA_load, input_uV, output_uV);
2698 goto out;
2701 ret = rdev->desc->ops->set_mode(rdev, mode);
2702 if (ret < 0) {
2703 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2704 goto out;
2706 ret = mode;
2707 out:
2708 mutex_unlock(&rdev->mutex);
2709 return ret;
2711 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2714 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2716 * @rdev: device to operate on.
2717 * @enable: state to set.
2719 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2721 unsigned int val;
2723 if (enable)
2724 val = rdev->desc->bypass_mask;
2725 else
2726 val = 0;
2728 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2729 rdev->desc->bypass_mask, val);
2731 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2734 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2736 * @rdev: device to operate on.
2737 * @enable: current state.
2739 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2741 unsigned int val;
2742 int ret;
2744 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2745 if (ret != 0)
2746 return ret;
2748 *enable = val & rdev->desc->bypass_mask;
2750 return 0;
2752 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2755 * regulator_allow_bypass - allow the regulator to go into bypass mode
2757 * @regulator: Regulator to configure
2758 * @allow: enable or disable bypass mode
2760 * Allow the regulator to go into bypass mode if all other consumers
2761 * for the regulator also enable bypass mode and the machine
2762 * constraints allow this. Bypass mode means that the regulator is
2763 * simply passing the input directly to the output with no regulation.
2765 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2767 struct regulator_dev *rdev = regulator->rdev;
2768 int ret = 0;
2770 if (!rdev->desc->ops->set_bypass)
2771 return 0;
2773 if (rdev->constraints &&
2774 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2775 return 0;
2777 mutex_lock(&rdev->mutex);
2779 if (enable && !regulator->bypass) {
2780 rdev->bypass_count++;
2782 if (rdev->bypass_count == rdev->open_count) {
2783 ret = rdev->desc->ops->set_bypass(rdev, enable);
2784 if (ret != 0)
2785 rdev->bypass_count--;
2788 } else if (!enable && regulator->bypass) {
2789 rdev->bypass_count--;
2791 if (rdev->bypass_count != rdev->open_count) {
2792 ret = rdev->desc->ops->set_bypass(rdev, enable);
2793 if (ret != 0)
2794 rdev->bypass_count++;
2798 if (ret == 0)
2799 regulator->bypass = enable;
2801 mutex_unlock(&rdev->mutex);
2803 return ret;
2805 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2808 * regulator_register_notifier - register regulator event notifier
2809 * @regulator: regulator source
2810 * @nb: notifier block
2812 * Register notifier block to receive regulator events.
2814 int regulator_register_notifier(struct regulator *regulator,
2815 struct notifier_block *nb)
2817 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2818 nb);
2820 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2823 * regulator_unregister_notifier - unregister regulator event notifier
2824 * @regulator: regulator source
2825 * @nb: notifier block
2827 * Unregister regulator event notifier block.
2829 int regulator_unregister_notifier(struct regulator *regulator,
2830 struct notifier_block *nb)
2832 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2833 nb);
2835 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2837 /* notify regulator consumers and downstream regulator consumers.
2838 * Note mutex must be held by caller.
2840 static void _notifier_call_chain(struct regulator_dev *rdev,
2841 unsigned long event, void *data)
2843 /* call rdev chain first */
2844 blocking_notifier_call_chain(&rdev->notifier, event, data);
2848 * regulator_bulk_get - get multiple regulator consumers
2850 * @dev: Device to supply
2851 * @num_consumers: Number of consumers to register
2852 * @consumers: Configuration of consumers; clients are stored here.
2854 * @return 0 on success, an errno on failure.
2856 * This helper function allows drivers to get several regulator
2857 * consumers in one operation. If any of the regulators cannot be
2858 * acquired then any regulators that were allocated will be freed
2859 * before returning to the caller.
2861 int regulator_bulk_get(struct device *dev, int num_consumers,
2862 struct regulator_bulk_data *consumers)
2864 int i;
2865 int ret;
2867 for (i = 0; i < num_consumers; i++)
2868 consumers[i].consumer = NULL;
2870 for (i = 0; i < num_consumers; i++) {
2871 consumers[i].consumer = regulator_get(dev,
2872 consumers[i].supply);
2873 if (IS_ERR(consumers[i].consumer)) {
2874 ret = PTR_ERR(consumers[i].consumer);
2875 dev_err(dev, "Failed to get supply '%s': %d\n",
2876 consumers[i].supply, ret);
2877 consumers[i].consumer = NULL;
2878 goto err;
2882 return 0;
2884 err:
2885 while (--i >= 0)
2886 regulator_put(consumers[i].consumer);
2888 return ret;
2890 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2893 * devm_regulator_bulk_get - managed get multiple regulator consumers
2895 * @dev: Device to supply
2896 * @num_consumers: Number of consumers to register
2897 * @consumers: Configuration of consumers; clients are stored here.
2899 * @return 0 on success, an errno on failure.
2901 * This helper function allows drivers to get several regulator
2902 * consumers in one operation with management, the regulators will
2903 * automatically be freed when the device is unbound. If any of the
2904 * regulators cannot be acquired then any regulators that were
2905 * allocated will be freed before returning to the caller.
2907 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2908 struct regulator_bulk_data *consumers)
2910 int i;
2911 int ret;
2913 for (i = 0; i < num_consumers; i++)
2914 consumers[i].consumer = NULL;
2916 for (i = 0; i < num_consumers; i++) {
2917 consumers[i].consumer = devm_regulator_get(dev,
2918 consumers[i].supply);
2919 if (IS_ERR(consumers[i].consumer)) {
2920 ret = PTR_ERR(consumers[i].consumer);
2921 dev_err(dev, "Failed to get supply '%s': %d\n",
2922 consumers[i].supply, ret);
2923 consumers[i].consumer = NULL;
2924 goto err;
2928 return 0;
2930 err:
2931 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2932 devm_regulator_put(consumers[i].consumer);
2934 return ret;
2936 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2938 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2940 struct regulator_bulk_data *bulk = data;
2942 bulk->ret = regulator_enable(bulk->consumer);
2946 * regulator_bulk_enable - enable multiple regulator consumers
2948 * @num_consumers: Number of consumers
2949 * @consumers: Consumer data; clients are stored here.
2950 * @return 0 on success, an errno on failure
2952 * This convenience API allows consumers to enable multiple regulator
2953 * clients in a single API call. If any consumers cannot be enabled
2954 * then any others that were enabled will be disabled again prior to
2955 * return.
2957 int regulator_bulk_enable(int num_consumers,
2958 struct regulator_bulk_data *consumers)
2960 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
2961 int i;
2962 int ret = 0;
2964 for (i = 0; i < num_consumers; i++) {
2965 if (consumers[i].consumer->always_on)
2966 consumers[i].ret = 0;
2967 else
2968 async_schedule_domain(regulator_bulk_enable_async,
2969 &consumers[i], &async_domain);
2972 async_synchronize_full_domain(&async_domain);
2974 /* If any consumer failed we need to unwind any that succeeded */
2975 for (i = 0; i < num_consumers; i++) {
2976 if (consumers[i].ret != 0) {
2977 ret = consumers[i].ret;
2978 goto err;
2982 return 0;
2984 err:
2985 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2986 while (--i >= 0)
2987 regulator_disable(consumers[i].consumer);
2989 return ret;
2991 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2994 * regulator_bulk_disable - disable multiple regulator consumers
2996 * @num_consumers: Number of consumers
2997 * @consumers: Consumer data; clients are stored here.
2998 * @return 0 on success, an errno on failure
3000 * This convenience API allows consumers to disable multiple regulator
3001 * clients in a single API call. If any consumers cannot be disabled
3002 * then any others that were disabled will be enabled again prior to
3003 * return.
3005 int regulator_bulk_disable(int num_consumers,
3006 struct regulator_bulk_data *consumers)
3008 int i;
3009 int ret, r;
3011 for (i = num_consumers - 1; i >= 0; --i) {
3012 ret = regulator_disable(consumers[i].consumer);
3013 if (ret != 0)
3014 goto err;
3017 return 0;
3019 err:
3020 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3021 for (++i; i < num_consumers; ++i) {
3022 r = regulator_enable(consumers[i].consumer);
3023 if (r != 0)
3024 pr_err("Failed to reename %s: %d\n",
3025 consumers[i].supply, r);
3028 return ret;
3030 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3033 * regulator_bulk_force_disable - force disable multiple regulator consumers
3035 * @num_consumers: Number of consumers
3036 * @consumers: Consumer data; clients are stored here.
3037 * @return 0 on success, an errno on failure
3039 * This convenience API allows consumers to forcibly disable multiple regulator
3040 * clients in a single API call.
3041 * NOTE: This should be used for situations when device damage will
3042 * likely occur if the regulators are not disabled (e.g. over temp).
3043 * Although regulator_force_disable function call for some consumers can
3044 * return error numbers, the function is called for all consumers.
3046 int regulator_bulk_force_disable(int num_consumers,
3047 struct regulator_bulk_data *consumers)
3049 int i;
3050 int ret;
3052 for (i = 0; i < num_consumers; i++)
3053 consumers[i].ret =
3054 regulator_force_disable(consumers[i].consumer);
3056 for (i = 0; i < num_consumers; i++) {
3057 if (consumers[i].ret != 0) {
3058 ret = consumers[i].ret;
3059 goto out;
3063 return 0;
3064 out:
3065 return ret;
3067 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3070 * regulator_bulk_free - free multiple regulator consumers
3072 * @num_consumers: Number of consumers
3073 * @consumers: Consumer data; clients are stored here.
3075 * This convenience API allows consumers to free multiple regulator
3076 * clients in a single API call.
3078 void regulator_bulk_free(int num_consumers,
3079 struct regulator_bulk_data *consumers)
3081 int i;
3083 for (i = 0; i < num_consumers; i++) {
3084 regulator_put(consumers[i].consumer);
3085 consumers[i].consumer = NULL;
3088 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3091 * regulator_notifier_call_chain - call regulator event notifier
3092 * @rdev: regulator source
3093 * @event: notifier block
3094 * @data: callback-specific data.
3096 * Called by regulator drivers to notify clients a regulator event has
3097 * occurred. We also notify regulator clients downstream.
3098 * Note lock must be held by caller.
3100 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3101 unsigned long event, void *data)
3103 _notifier_call_chain(rdev, event, data);
3104 return NOTIFY_DONE;
3107 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3110 * regulator_mode_to_status - convert a regulator mode into a status
3112 * @mode: Mode to convert
3114 * Convert a regulator mode into a status.
3116 int regulator_mode_to_status(unsigned int mode)
3118 switch (mode) {
3119 case REGULATOR_MODE_FAST:
3120 return REGULATOR_STATUS_FAST;
3121 case REGULATOR_MODE_NORMAL:
3122 return REGULATOR_STATUS_NORMAL;
3123 case REGULATOR_MODE_IDLE:
3124 return REGULATOR_STATUS_IDLE;
3125 case REGULATOR_MODE_STANDBY:
3126 return REGULATOR_STATUS_STANDBY;
3127 default:
3128 return REGULATOR_STATUS_UNDEFINED;
3131 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3134 * To avoid cluttering sysfs (and memory) with useless state, only
3135 * create attributes that can be meaningfully displayed.
3137 static int add_regulator_attributes(struct regulator_dev *rdev)
3139 struct device *dev = &rdev->dev;
3140 struct regulator_ops *ops = rdev->desc->ops;
3141 int status = 0;
3143 /* some attributes need specific methods to be displayed */
3144 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3145 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3146 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3147 status = device_create_file(dev, &dev_attr_microvolts);
3148 if (status < 0)
3149 return status;
3151 if (ops->get_current_limit) {
3152 status = device_create_file(dev, &dev_attr_microamps);
3153 if (status < 0)
3154 return status;
3156 if (ops->get_mode) {
3157 status = device_create_file(dev, &dev_attr_opmode);
3158 if (status < 0)
3159 return status;
3161 if (ops->is_enabled) {
3162 status = device_create_file(dev, &dev_attr_state);
3163 if (status < 0)
3164 return status;
3166 if (ops->get_status) {
3167 status = device_create_file(dev, &dev_attr_status);
3168 if (status < 0)
3169 return status;
3171 if (ops->get_bypass) {
3172 status = device_create_file(dev, &dev_attr_bypass);
3173 if (status < 0)
3174 return status;
3177 /* some attributes are type-specific */
3178 if (rdev->desc->type == REGULATOR_CURRENT) {
3179 status = device_create_file(dev, &dev_attr_requested_microamps);
3180 if (status < 0)
3181 return status;
3184 /* all the other attributes exist to support constraints;
3185 * don't show them if there are no constraints, or if the
3186 * relevant supporting methods are missing.
3188 if (!rdev->constraints)
3189 return status;
3191 /* constraints need specific supporting methods */
3192 if (ops->set_voltage || ops->set_voltage_sel) {
3193 status = device_create_file(dev, &dev_attr_min_microvolts);
3194 if (status < 0)
3195 return status;
3196 status = device_create_file(dev, &dev_attr_max_microvolts);
3197 if (status < 0)
3198 return status;
3200 if (ops->set_current_limit) {
3201 status = device_create_file(dev, &dev_attr_min_microamps);
3202 if (status < 0)
3203 return status;
3204 status = device_create_file(dev, &dev_attr_max_microamps);
3205 if (status < 0)
3206 return status;
3209 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3210 if (status < 0)
3211 return status;
3212 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3213 if (status < 0)
3214 return status;
3215 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3216 if (status < 0)
3217 return status;
3219 if (ops->set_suspend_voltage) {
3220 status = device_create_file(dev,
3221 &dev_attr_suspend_standby_microvolts);
3222 if (status < 0)
3223 return status;
3224 status = device_create_file(dev,
3225 &dev_attr_suspend_mem_microvolts);
3226 if (status < 0)
3227 return status;
3228 status = device_create_file(dev,
3229 &dev_attr_suspend_disk_microvolts);
3230 if (status < 0)
3231 return status;
3234 if (ops->set_suspend_mode) {
3235 status = device_create_file(dev,
3236 &dev_attr_suspend_standby_mode);
3237 if (status < 0)
3238 return status;
3239 status = device_create_file(dev,
3240 &dev_attr_suspend_mem_mode);
3241 if (status < 0)
3242 return status;
3243 status = device_create_file(dev,
3244 &dev_attr_suspend_disk_mode);
3245 if (status < 0)
3246 return status;
3249 return status;
3252 static void rdev_init_debugfs(struct regulator_dev *rdev)
3254 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3255 if (!rdev->debugfs) {
3256 rdev_warn(rdev, "Failed to create debugfs directory\n");
3257 return;
3260 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3261 &rdev->use_count);
3262 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3263 &rdev->open_count);
3264 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3265 &rdev->bypass_count);
3269 * regulator_register - register regulator
3270 * @regulator_desc: regulator to register
3271 * @config: runtime configuration for regulator
3273 * Called by regulator drivers to register a regulator.
3274 * Returns 0 on success.
3276 struct regulator_dev *
3277 regulator_register(const struct regulator_desc *regulator_desc,
3278 const struct regulator_config *config)
3280 const struct regulation_constraints *constraints = NULL;
3281 const struct regulator_init_data *init_data;
3282 static atomic_t regulator_no = ATOMIC_INIT(0);
3283 struct regulator_dev *rdev;
3284 struct device *dev;
3285 int ret, i;
3286 const char *supply = NULL;
3288 if (regulator_desc == NULL || config == NULL)
3289 return ERR_PTR(-EINVAL);
3291 dev = config->dev;
3292 WARN_ON(!dev);
3294 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3295 return ERR_PTR(-EINVAL);
3297 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3298 regulator_desc->type != REGULATOR_CURRENT)
3299 return ERR_PTR(-EINVAL);
3301 /* Only one of each should be implemented */
3302 WARN_ON(regulator_desc->ops->get_voltage &&
3303 regulator_desc->ops->get_voltage_sel);
3304 WARN_ON(regulator_desc->ops->set_voltage &&
3305 regulator_desc->ops->set_voltage_sel);
3307 /* If we're using selectors we must implement list_voltage. */
3308 if (regulator_desc->ops->get_voltage_sel &&
3309 !regulator_desc->ops->list_voltage) {
3310 return ERR_PTR(-EINVAL);
3312 if (regulator_desc->ops->set_voltage_sel &&
3313 !regulator_desc->ops->list_voltage) {
3314 return ERR_PTR(-EINVAL);
3317 init_data = config->init_data;
3319 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3320 if (rdev == NULL)
3321 return ERR_PTR(-ENOMEM);
3323 mutex_lock(&regulator_list_mutex);
3325 mutex_init(&rdev->mutex);
3326 rdev->reg_data = config->driver_data;
3327 rdev->owner = regulator_desc->owner;
3328 rdev->desc = regulator_desc;
3329 if (config->regmap)
3330 rdev->regmap = config->regmap;
3331 else if (dev_get_regmap(dev, NULL))
3332 rdev->regmap = dev_get_regmap(dev, NULL);
3333 else if (dev->parent)
3334 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3335 INIT_LIST_HEAD(&rdev->consumer_list);
3336 INIT_LIST_HEAD(&rdev->list);
3337 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3338 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3340 /* preform any regulator specific init */
3341 if (init_data && init_data->regulator_init) {
3342 ret = init_data->regulator_init(rdev->reg_data);
3343 if (ret < 0)
3344 goto clean;
3347 /* register with sysfs */
3348 rdev->dev.class = &regulator_class;
3349 rdev->dev.of_node = config->of_node;
3350 rdev->dev.parent = dev;
3351 dev_set_name(&rdev->dev, "regulator.%d",
3352 atomic_inc_return(&regulator_no) - 1);
3353 ret = device_register(&rdev->dev);
3354 if (ret != 0) {
3355 put_device(&rdev->dev);
3356 goto clean;
3359 dev_set_drvdata(&rdev->dev, rdev);
3361 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3362 ret = gpio_request_one(config->ena_gpio,
3363 GPIOF_DIR_OUT | config->ena_gpio_flags,
3364 rdev_get_name(rdev));
3365 if (ret != 0) {
3366 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3367 config->ena_gpio, ret);
3368 goto clean;
3371 rdev->ena_gpio = config->ena_gpio;
3372 rdev->ena_gpio_invert = config->ena_gpio_invert;
3374 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3375 rdev->ena_gpio_state = 1;
3377 if (rdev->ena_gpio_invert)
3378 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3381 /* set regulator constraints */
3382 if (init_data)
3383 constraints = &init_data->constraints;
3385 ret = set_machine_constraints(rdev, constraints);
3386 if (ret < 0)
3387 goto scrub;
3389 /* add attributes supported by this regulator */
3390 ret = add_regulator_attributes(rdev);
3391 if (ret < 0)
3392 goto scrub;
3394 if (init_data && init_data->supply_regulator)
3395 supply = init_data->supply_regulator;
3396 else if (regulator_desc->supply_name)
3397 supply = regulator_desc->supply_name;
3399 if (supply) {
3400 struct regulator_dev *r;
3402 r = regulator_dev_lookup(dev, supply, &ret);
3404 if (!r) {
3405 dev_err(dev, "Failed to find supply %s\n", supply);
3406 ret = -EPROBE_DEFER;
3407 goto scrub;
3410 ret = set_supply(rdev, r);
3411 if (ret < 0)
3412 goto scrub;
3414 /* Enable supply if rail is enabled */
3415 if (_regulator_is_enabled(rdev)) {
3416 ret = regulator_enable(rdev->supply);
3417 if (ret < 0)
3418 goto scrub;
3422 /* add consumers devices */
3423 if (init_data) {
3424 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3425 ret = set_consumer_device_supply(rdev,
3426 init_data->consumer_supplies[i].dev_name,
3427 init_data->consumer_supplies[i].supply);
3428 if (ret < 0) {
3429 dev_err(dev, "Failed to set supply %s\n",
3430 init_data->consumer_supplies[i].supply);
3431 goto unset_supplies;
3436 list_add(&rdev->list, &regulator_list);
3438 rdev_init_debugfs(rdev);
3439 out:
3440 mutex_unlock(&regulator_list_mutex);
3441 return rdev;
3443 unset_supplies:
3444 unset_regulator_supplies(rdev);
3446 scrub:
3447 if (rdev->supply)
3448 regulator_put(rdev->supply);
3449 if (rdev->ena_gpio)
3450 gpio_free(rdev->ena_gpio);
3451 kfree(rdev->constraints);
3452 device_unregister(&rdev->dev);
3453 /* device core frees rdev */
3454 rdev = ERR_PTR(ret);
3455 goto out;
3457 clean:
3458 kfree(rdev);
3459 rdev = ERR_PTR(ret);
3460 goto out;
3462 EXPORT_SYMBOL_GPL(regulator_register);
3465 * regulator_unregister - unregister regulator
3466 * @rdev: regulator to unregister
3468 * Called by regulator drivers to unregister a regulator.
3470 void regulator_unregister(struct regulator_dev *rdev)
3472 if (rdev == NULL)
3473 return;
3475 if (rdev->supply)
3476 regulator_put(rdev->supply);
3477 mutex_lock(&regulator_list_mutex);
3478 debugfs_remove_recursive(rdev->debugfs);
3479 flush_work(&rdev->disable_work.work);
3480 WARN_ON(rdev->open_count);
3481 unset_regulator_supplies(rdev);
3482 list_del(&rdev->list);
3483 kfree(rdev->constraints);
3484 if (rdev->ena_gpio)
3485 gpio_free(rdev->ena_gpio);
3486 device_unregister(&rdev->dev);
3487 mutex_unlock(&regulator_list_mutex);
3489 EXPORT_SYMBOL_GPL(regulator_unregister);
3492 * regulator_suspend_prepare - prepare regulators for system wide suspend
3493 * @state: system suspend state
3495 * Configure each regulator with it's suspend operating parameters for state.
3496 * This will usually be called by machine suspend code prior to supending.
3498 int regulator_suspend_prepare(suspend_state_t state)
3500 struct regulator_dev *rdev;
3501 int ret = 0;
3503 /* ON is handled by regulator active state */
3504 if (state == PM_SUSPEND_ON)
3505 return -EINVAL;
3507 mutex_lock(&regulator_list_mutex);
3508 list_for_each_entry(rdev, &regulator_list, list) {
3510 mutex_lock(&rdev->mutex);
3511 ret = suspend_prepare(rdev, state);
3512 mutex_unlock(&rdev->mutex);
3514 if (ret < 0) {
3515 rdev_err(rdev, "failed to prepare\n");
3516 goto out;
3519 out:
3520 mutex_unlock(&regulator_list_mutex);
3521 return ret;
3523 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3526 * regulator_suspend_finish - resume regulators from system wide suspend
3528 * Turn on regulators that might be turned off by regulator_suspend_prepare
3529 * and that should be turned on according to the regulators properties.
3531 int regulator_suspend_finish(void)
3533 struct regulator_dev *rdev;
3534 int ret = 0, error;
3536 mutex_lock(&regulator_list_mutex);
3537 list_for_each_entry(rdev, &regulator_list, list) {
3538 struct regulator_ops *ops = rdev->desc->ops;
3540 mutex_lock(&rdev->mutex);
3541 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3542 ops->enable) {
3543 error = ops->enable(rdev);
3544 if (error)
3545 ret = error;
3546 } else {
3547 if (!has_full_constraints)
3548 goto unlock;
3549 if (!ops->disable)
3550 goto unlock;
3551 if (!_regulator_is_enabled(rdev))
3552 goto unlock;
3554 error = ops->disable(rdev);
3555 if (error)
3556 ret = error;
3558 unlock:
3559 mutex_unlock(&rdev->mutex);
3561 mutex_unlock(&regulator_list_mutex);
3562 return ret;
3564 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3567 * regulator_has_full_constraints - the system has fully specified constraints
3569 * Calling this function will cause the regulator API to disable all
3570 * regulators which have a zero use count and don't have an always_on
3571 * constraint in a late_initcall.
3573 * The intention is that this will become the default behaviour in a
3574 * future kernel release so users are encouraged to use this facility
3575 * now.
3577 void regulator_has_full_constraints(void)
3579 has_full_constraints = 1;
3581 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3584 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3586 * Calling this function will cause the regulator API to provide a
3587 * dummy regulator to consumers if no physical regulator is found,
3588 * allowing most consumers to proceed as though a regulator were
3589 * configured. This allows systems such as those with software
3590 * controllable regulators for the CPU core only to be brought up more
3591 * readily.
3593 void regulator_use_dummy_regulator(void)
3595 board_wants_dummy_regulator = true;
3597 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3600 * rdev_get_drvdata - get rdev regulator driver data
3601 * @rdev: regulator
3603 * Get rdev regulator driver private data. This call can be used in the
3604 * regulator driver context.
3606 void *rdev_get_drvdata(struct regulator_dev *rdev)
3608 return rdev->reg_data;
3610 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3613 * regulator_get_drvdata - get regulator driver data
3614 * @regulator: regulator
3616 * Get regulator driver private data. This call can be used in the consumer
3617 * driver context when non API regulator specific functions need to be called.
3619 void *regulator_get_drvdata(struct regulator *regulator)
3621 return regulator->rdev->reg_data;
3623 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3626 * regulator_set_drvdata - set regulator driver data
3627 * @regulator: regulator
3628 * @data: data
3630 void regulator_set_drvdata(struct regulator *regulator, void *data)
3632 regulator->rdev->reg_data = data;
3634 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3637 * regulator_get_id - get regulator ID
3638 * @rdev: regulator
3640 int rdev_get_id(struct regulator_dev *rdev)
3642 return rdev->desc->id;
3644 EXPORT_SYMBOL_GPL(rdev_get_id);
3646 struct device *rdev_get_dev(struct regulator_dev *rdev)
3648 return &rdev->dev;
3650 EXPORT_SYMBOL_GPL(rdev_get_dev);
3652 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3654 return reg_init_data->driver_data;
3656 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3658 #ifdef CONFIG_DEBUG_FS
3659 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3660 size_t count, loff_t *ppos)
3662 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3663 ssize_t len, ret = 0;
3664 struct regulator_map *map;
3666 if (!buf)
3667 return -ENOMEM;
3669 list_for_each_entry(map, &regulator_map_list, list) {
3670 len = snprintf(buf + ret, PAGE_SIZE - ret,
3671 "%s -> %s.%s\n",
3672 rdev_get_name(map->regulator), map->dev_name,
3673 map->supply);
3674 if (len >= 0)
3675 ret += len;
3676 if (ret > PAGE_SIZE) {
3677 ret = PAGE_SIZE;
3678 break;
3682 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3684 kfree(buf);
3686 return ret;
3688 #endif
3690 static const struct file_operations supply_map_fops = {
3691 #ifdef CONFIG_DEBUG_FS
3692 .read = supply_map_read_file,
3693 .llseek = default_llseek,
3694 #endif
3697 static int __init regulator_init(void)
3699 int ret;
3701 ret = class_register(&regulator_class);
3703 debugfs_root = debugfs_create_dir("regulator", NULL);
3704 if (!debugfs_root)
3705 pr_warn("regulator: Failed to create debugfs directory\n");
3707 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3708 &supply_map_fops);
3710 regulator_dummy_init();
3712 return ret;
3715 /* init early to allow our consumers to complete system booting */
3716 core_initcall(regulator_init);
3718 static int __init regulator_init_complete(void)
3720 struct regulator_dev *rdev;
3721 struct regulator_ops *ops;
3722 struct regulation_constraints *c;
3723 int enabled, ret;
3726 * Since DT doesn't provide an idiomatic mechanism for
3727 * enabling full constraints and since it's much more natural
3728 * with DT to provide them just assume that a DT enabled
3729 * system has full constraints.
3731 if (of_have_populated_dt())
3732 has_full_constraints = true;
3734 mutex_lock(&regulator_list_mutex);
3736 /* If we have a full configuration then disable any regulators
3737 * which are not in use or always_on. This will become the
3738 * default behaviour in the future.
3740 list_for_each_entry(rdev, &regulator_list, list) {
3741 ops = rdev->desc->ops;
3742 c = rdev->constraints;
3744 if (!ops->disable || (c && c->always_on))
3745 continue;
3747 mutex_lock(&rdev->mutex);
3749 if (rdev->use_count)
3750 goto unlock;
3752 /* If we can't read the status assume it's on. */
3753 if (ops->is_enabled)
3754 enabled = ops->is_enabled(rdev);
3755 else
3756 enabled = 1;
3758 if (!enabled)
3759 goto unlock;
3761 if (has_full_constraints) {
3762 /* We log since this may kill the system if it
3763 * goes wrong. */
3764 rdev_info(rdev, "disabling\n");
3765 ret = ops->disable(rdev);
3766 if (ret != 0) {
3767 rdev_err(rdev, "couldn't disable: %d\n", ret);
3769 } else {
3770 /* The intention is that in future we will
3771 * assume that full constraints are provided
3772 * so warn even if we aren't going to do
3773 * anything here.
3775 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3778 unlock:
3779 mutex_unlock(&rdev->mutex);
3782 mutex_unlock(&regulator_list_mutex);
3784 return 0;
3786 late_initcall(regulator_init_complete);