thp: avoid dumping huge zero page
[linux-2.6/libata-dev.git] / drivers / regulator / core.c
blob278584302f2d162c56599ad61a62ade3cdcceadd
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 dev_err(regulator->dev, "Restricting voltage, %u-%uuV\n",
204 regulator->min_uV, regulator->max_uV);
205 return -EINVAL;
208 return 0;
211 /* current constraint check */
212 static int regulator_check_current_limit(struct regulator_dev *rdev,
213 int *min_uA, int *max_uA)
215 BUG_ON(*min_uA > *max_uA);
217 if (!rdev->constraints) {
218 rdev_err(rdev, "no constraints\n");
219 return -ENODEV;
221 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
222 rdev_err(rdev, "operation not allowed\n");
223 return -EPERM;
226 if (*max_uA > rdev->constraints->max_uA)
227 *max_uA = rdev->constraints->max_uA;
228 if (*min_uA < rdev->constraints->min_uA)
229 *min_uA = rdev->constraints->min_uA;
231 if (*min_uA > *max_uA) {
232 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
233 *min_uA, *max_uA);
234 return -EINVAL;
237 return 0;
240 /* operating mode constraint check */
241 static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
243 switch (*mode) {
244 case REGULATOR_MODE_FAST:
245 case REGULATOR_MODE_NORMAL:
246 case REGULATOR_MODE_IDLE:
247 case REGULATOR_MODE_STANDBY:
248 break;
249 default:
250 rdev_err(rdev, "invalid mode %x specified\n", *mode);
251 return -EINVAL;
254 if (!rdev->constraints) {
255 rdev_err(rdev, "no constraints\n");
256 return -ENODEV;
258 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
259 rdev_err(rdev, "operation not allowed\n");
260 return -EPERM;
263 /* The modes are bitmasks, the most power hungry modes having
264 * the lowest values. If the requested mode isn't supported
265 * try higher modes. */
266 while (*mode) {
267 if (rdev->constraints->valid_modes_mask & *mode)
268 return 0;
269 *mode /= 2;
272 return -EINVAL;
275 /* dynamic regulator mode switching constraint check */
276 static int regulator_check_drms(struct regulator_dev *rdev)
278 if (!rdev->constraints) {
279 rdev_err(rdev, "no constraints\n");
280 return -ENODEV;
282 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
283 rdev_err(rdev, "operation not allowed\n");
284 return -EPERM;
286 return 0;
289 static ssize_t regulator_uV_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
292 struct regulator_dev *rdev = dev_get_drvdata(dev);
293 ssize_t ret;
295 mutex_lock(&rdev->mutex);
296 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
297 mutex_unlock(&rdev->mutex);
299 return ret;
301 static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
303 static ssize_t regulator_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
306 struct regulator_dev *rdev = dev_get_drvdata(dev);
308 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
310 static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
312 static ssize_t regulator_name_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
315 struct regulator_dev *rdev = dev_get_drvdata(dev);
317 return sprintf(buf, "%s\n", rdev_get_name(rdev));
320 static ssize_t regulator_print_opmode(char *buf, int mode)
322 switch (mode) {
323 case REGULATOR_MODE_FAST:
324 return sprintf(buf, "fast\n");
325 case REGULATOR_MODE_NORMAL:
326 return sprintf(buf, "normal\n");
327 case REGULATOR_MODE_IDLE:
328 return sprintf(buf, "idle\n");
329 case REGULATOR_MODE_STANDBY:
330 return sprintf(buf, "standby\n");
332 return sprintf(buf, "unknown\n");
335 static ssize_t regulator_opmode_show(struct device *dev,
336 struct device_attribute *attr, char *buf)
338 struct regulator_dev *rdev = dev_get_drvdata(dev);
340 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
342 static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
344 static ssize_t regulator_print_state(char *buf, int state)
346 if (state > 0)
347 return sprintf(buf, "enabled\n");
348 else if (state == 0)
349 return sprintf(buf, "disabled\n");
350 else
351 return sprintf(buf, "unknown\n");
354 static ssize_t regulator_state_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
357 struct regulator_dev *rdev = dev_get_drvdata(dev);
358 ssize_t ret;
360 mutex_lock(&rdev->mutex);
361 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
362 mutex_unlock(&rdev->mutex);
364 return ret;
366 static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
368 static ssize_t regulator_status_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
372 int status;
373 char *label;
375 status = rdev->desc->ops->get_status(rdev);
376 if (status < 0)
377 return status;
379 switch (status) {
380 case REGULATOR_STATUS_OFF:
381 label = "off";
382 break;
383 case REGULATOR_STATUS_ON:
384 label = "on";
385 break;
386 case REGULATOR_STATUS_ERROR:
387 label = "error";
388 break;
389 case REGULATOR_STATUS_FAST:
390 label = "fast";
391 break;
392 case REGULATOR_STATUS_NORMAL:
393 label = "normal";
394 break;
395 case REGULATOR_STATUS_IDLE:
396 label = "idle";
397 break;
398 case REGULATOR_STATUS_STANDBY:
399 label = "standby";
400 break;
401 case REGULATOR_STATUS_BYPASS:
402 label = "bypass";
403 break;
404 case REGULATOR_STATUS_UNDEFINED:
405 label = "undefined";
406 break;
407 default:
408 return -ERANGE;
411 return sprintf(buf, "%s\n", label);
413 static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
415 static ssize_t regulator_min_uA_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
418 struct regulator_dev *rdev = dev_get_drvdata(dev);
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
423 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
425 static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
427 static ssize_t regulator_max_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
430 struct regulator_dev *rdev = dev_get_drvdata(dev);
432 if (!rdev->constraints)
433 return sprintf(buf, "constraint not defined\n");
435 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
437 static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
439 static ssize_t regulator_min_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
442 struct regulator_dev *rdev = dev_get_drvdata(dev);
444 if (!rdev->constraints)
445 return sprintf(buf, "constraint not defined\n");
447 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
449 static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
451 static ssize_t regulator_max_uV_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
454 struct regulator_dev *rdev = dev_get_drvdata(dev);
456 if (!rdev->constraints)
457 return sprintf(buf, "constraint not defined\n");
459 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
461 static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
463 static ssize_t regulator_total_uA_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
466 struct regulator_dev *rdev = dev_get_drvdata(dev);
467 struct regulator *regulator;
468 int uA = 0;
470 mutex_lock(&rdev->mutex);
471 list_for_each_entry(regulator, &rdev->consumer_list, list)
472 uA += regulator->uA_load;
473 mutex_unlock(&rdev->mutex);
474 return sprintf(buf, "%d\n", uA);
476 static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
478 static ssize_t regulator_num_users_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
481 struct regulator_dev *rdev = dev_get_drvdata(dev);
482 return sprintf(buf, "%d\n", rdev->use_count);
485 static ssize_t regulator_type_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
488 struct regulator_dev *rdev = dev_get_drvdata(dev);
490 switch (rdev->desc->type) {
491 case REGULATOR_VOLTAGE:
492 return sprintf(buf, "voltage\n");
493 case REGULATOR_CURRENT:
494 return sprintf(buf, "current\n");
496 return sprintf(buf, "unknown\n");
499 static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
502 struct regulator_dev *rdev = dev_get_drvdata(dev);
504 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
506 static DEVICE_ATTR(suspend_mem_microvolts, 0444,
507 regulator_suspend_mem_uV_show, NULL);
509 static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
510 struct device_attribute *attr, char *buf)
512 struct regulator_dev *rdev = dev_get_drvdata(dev);
514 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
516 static DEVICE_ATTR(suspend_disk_microvolts, 0444,
517 regulator_suspend_disk_uV_show, NULL);
519 static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
522 struct regulator_dev *rdev = dev_get_drvdata(dev);
524 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
526 static DEVICE_ATTR(suspend_standby_microvolts, 0444,
527 regulator_suspend_standby_uV_show, NULL);
529 static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
530 struct device_attribute *attr, char *buf)
532 struct regulator_dev *rdev = dev_get_drvdata(dev);
534 return regulator_print_opmode(buf,
535 rdev->constraints->state_mem.mode);
537 static DEVICE_ATTR(suspend_mem_mode, 0444,
538 regulator_suspend_mem_mode_show, NULL);
540 static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
541 struct device_attribute *attr, char *buf)
543 struct regulator_dev *rdev = dev_get_drvdata(dev);
545 return regulator_print_opmode(buf,
546 rdev->constraints->state_disk.mode);
548 static DEVICE_ATTR(suspend_disk_mode, 0444,
549 regulator_suspend_disk_mode_show, NULL);
551 static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
554 struct regulator_dev *rdev = dev_get_drvdata(dev);
556 return regulator_print_opmode(buf,
557 rdev->constraints->state_standby.mode);
559 static DEVICE_ATTR(suspend_standby_mode, 0444,
560 regulator_suspend_standby_mode_show, NULL);
562 static ssize_t regulator_suspend_mem_state_show(struct device *dev,
563 struct device_attribute *attr, char *buf)
565 struct regulator_dev *rdev = dev_get_drvdata(dev);
567 return regulator_print_state(buf,
568 rdev->constraints->state_mem.enabled);
570 static DEVICE_ATTR(suspend_mem_state, 0444,
571 regulator_suspend_mem_state_show, NULL);
573 static ssize_t regulator_suspend_disk_state_show(struct device *dev,
574 struct device_attribute *attr, char *buf)
576 struct regulator_dev *rdev = dev_get_drvdata(dev);
578 return regulator_print_state(buf,
579 rdev->constraints->state_disk.enabled);
581 static DEVICE_ATTR(suspend_disk_state, 0444,
582 regulator_suspend_disk_state_show, NULL);
584 static ssize_t regulator_suspend_standby_state_show(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 struct regulator_dev *rdev = dev_get_drvdata(dev);
589 return regulator_print_state(buf,
590 rdev->constraints->state_standby.enabled);
592 static DEVICE_ATTR(suspend_standby_state, 0444,
593 regulator_suspend_standby_state_show, NULL);
595 static ssize_t regulator_bypass_show(struct device *dev,
596 struct device_attribute *attr, char *buf)
598 struct regulator_dev *rdev = dev_get_drvdata(dev);
599 const char *report;
600 bool bypass;
601 int ret;
603 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
605 if (ret != 0)
606 report = "unknown";
607 else if (bypass)
608 report = "enabled";
609 else
610 report = "disabled";
612 return sprintf(buf, "%s\n", report);
614 static DEVICE_ATTR(bypass, 0444,
615 regulator_bypass_show, NULL);
618 * These are the only attributes are present for all regulators.
619 * Other attributes are a function of regulator functionality.
621 static struct device_attribute regulator_dev_attrs[] = {
622 __ATTR(name, 0444, regulator_name_show, NULL),
623 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
624 __ATTR(type, 0444, regulator_type_show, NULL),
625 __ATTR_NULL,
628 static void regulator_dev_release(struct device *dev)
630 struct regulator_dev *rdev = dev_get_drvdata(dev);
631 kfree(rdev);
634 static struct class regulator_class = {
635 .name = "regulator",
636 .dev_release = regulator_dev_release,
637 .dev_attrs = regulator_dev_attrs,
640 /* Calculate the new optimum regulator operating mode based on the new total
641 * consumer load. All locks held by caller */
642 static void drms_uA_update(struct regulator_dev *rdev)
644 struct regulator *sibling;
645 int current_uA = 0, output_uV, input_uV, err;
646 unsigned int mode;
648 err = regulator_check_drms(rdev);
649 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
650 (!rdev->desc->ops->get_voltage &&
651 !rdev->desc->ops->get_voltage_sel) ||
652 !rdev->desc->ops->set_mode)
653 return;
655 /* get output voltage */
656 output_uV = _regulator_get_voltage(rdev);
657 if (output_uV <= 0)
658 return;
660 /* get input voltage */
661 input_uV = 0;
662 if (rdev->supply)
663 input_uV = regulator_get_voltage(rdev->supply);
664 if (input_uV <= 0)
665 input_uV = rdev->constraints->input_uV;
666 if (input_uV <= 0)
667 return;
669 /* calc total requested load */
670 list_for_each_entry(sibling, &rdev->consumer_list, list)
671 current_uA += sibling->uA_load;
673 /* now get the optimum mode for our new total regulator load */
674 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
675 output_uV, current_uA);
677 /* check the new mode is allowed */
678 err = regulator_mode_constrain(rdev, &mode);
679 if (err == 0)
680 rdev->desc->ops->set_mode(rdev, mode);
683 static int suspend_set_state(struct regulator_dev *rdev,
684 struct regulator_state *rstate)
686 int ret = 0;
688 /* If we have no suspend mode configration don't set anything;
689 * only warn if the driver implements set_suspend_voltage or
690 * set_suspend_mode callback.
692 if (!rstate->enabled && !rstate->disabled) {
693 if (rdev->desc->ops->set_suspend_voltage ||
694 rdev->desc->ops->set_suspend_mode)
695 rdev_warn(rdev, "No configuration\n");
696 return 0;
699 if (rstate->enabled && rstate->disabled) {
700 rdev_err(rdev, "invalid configuration\n");
701 return -EINVAL;
704 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
705 ret = rdev->desc->ops->set_suspend_enable(rdev);
706 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
707 ret = rdev->desc->ops->set_suspend_disable(rdev);
708 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
709 ret = 0;
711 if (ret < 0) {
712 rdev_err(rdev, "failed to enabled/disable\n");
713 return ret;
716 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
717 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
718 if (ret < 0) {
719 rdev_err(rdev, "failed to set voltage\n");
720 return ret;
724 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
725 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
726 if (ret < 0) {
727 rdev_err(rdev, "failed to set mode\n");
728 return ret;
731 return ret;
734 /* locks held by caller */
735 static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
737 if (!rdev->constraints)
738 return -EINVAL;
740 switch (state) {
741 case PM_SUSPEND_STANDBY:
742 return suspend_set_state(rdev,
743 &rdev->constraints->state_standby);
744 case PM_SUSPEND_MEM:
745 return suspend_set_state(rdev,
746 &rdev->constraints->state_mem);
747 case PM_SUSPEND_MAX:
748 return suspend_set_state(rdev,
749 &rdev->constraints->state_disk);
750 default:
751 return -EINVAL;
755 static void print_constraints(struct regulator_dev *rdev)
757 struct regulation_constraints *constraints = rdev->constraints;
758 char buf[80] = "";
759 int count = 0;
760 int ret;
762 if (constraints->min_uV && constraints->max_uV) {
763 if (constraints->min_uV == constraints->max_uV)
764 count += sprintf(buf + count, "%d mV ",
765 constraints->min_uV / 1000);
766 else
767 count += sprintf(buf + count, "%d <--> %d mV ",
768 constraints->min_uV / 1000,
769 constraints->max_uV / 1000);
772 if (!constraints->min_uV ||
773 constraints->min_uV != constraints->max_uV) {
774 ret = _regulator_get_voltage(rdev);
775 if (ret > 0)
776 count += sprintf(buf + count, "at %d mV ", ret / 1000);
779 if (constraints->uV_offset)
780 count += sprintf(buf, "%dmV offset ",
781 constraints->uV_offset / 1000);
783 if (constraints->min_uA && constraints->max_uA) {
784 if (constraints->min_uA == constraints->max_uA)
785 count += sprintf(buf + count, "%d mA ",
786 constraints->min_uA / 1000);
787 else
788 count += sprintf(buf + count, "%d <--> %d mA ",
789 constraints->min_uA / 1000,
790 constraints->max_uA / 1000);
793 if (!constraints->min_uA ||
794 constraints->min_uA != constraints->max_uA) {
795 ret = _regulator_get_current_limit(rdev);
796 if (ret > 0)
797 count += sprintf(buf + count, "at %d mA ", ret / 1000);
800 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
801 count += sprintf(buf + count, "fast ");
802 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
803 count += sprintf(buf + count, "normal ");
804 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
805 count += sprintf(buf + count, "idle ");
806 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
807 count += sprintf(buf + count, "standby");
809 if (!count)
810 sprintf(buf, "no parameters");
812 rdev_info(rdev, "%s\n", buf);
814 if ((constraints->min_uV != constraints->max_uV) &&
815 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
816 rdev_warn(rdev,
817 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
820 static int machine_constraints_voltage(struct regulator_dev *rdev,
821 struct regulation_constraints *constraints)
823 struct regulator_ops *ops = rdev->desc->ops;
824 int ret;
826 /* do we need to apply the constraint voltage */
827 if (rdev->constraints->apply_uV &&
828 rdev->constraints->min_uV == rdev->constraints->max_uV) {
829 ret = _regulator_do_set_voltage(rdev,
830 rdev->constraints->min_uV,
831 rdev->constraints->max_uV);
832 if (ret < 0) {
833 rdev_err(rdev, "failed to apply %duV constraint\n",
834 rdev->constraints->min_uV);
835 return ret;
839 /* constrain machine-level voltage specs to fit
840 * the actual range supported by this regulator.
842 if (ops->list_voltage && rdev->desc->n_voltages) {
843 int count = rdev->desc->n_voltages;
844 int i;
845 int min_uV = INT_MAX;
846 int max_uV = INT_MIN;
847 int cmin = constraints->min_uV;
848 int cmax = constraints->max_uV;
850 /* it's safe to autoconfigure fixed-voltage supplies
851 and the constraints are used by list_voltage. */
852 if (count == 1 && !cmin) {
853 cmin = 1;
854 cmax = INT_MAX;
855 constraints->min_uV = cmin;
856 constraints->max_uV = cmax;
859 /* voltage constraints are optional */
860 if ((cmin == 0) && (cmax == 0))
861 return 0;
863 /* else require explicit machine-level constraints */
864 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
865 rdev_err(rdev, "invalid voltage constraints\n");
866 return -EINVAL;
869 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
870 for (i = 0; i < count; i++) {
871 int value;
873 value = ops->list_voltage(rdev, i);
874 if (value <= 0)
875 continue;
877 /* maybe adjust [min_uV..max_uV] */
878 if (value >= cmin && value < min_uV)
879 min_uV = value;
880 if (value <= cmax && value > max_uV)
881 max_uV = value;
884 /* final: [min_uV..max_uV] valid iff constraints valid */
885 if (max_uV < min_uV) {
886 rdev_err(rdev,
887 "unsupportable voltage constraints %u-%uuV\n",
888 min_uV, max_uV);
889 return -EINVAL;
892 /* use regulator's subset of machine constraints */
893 if (constraints->min_uV < min_uV) {
894 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
895 constraints->min_uV, min_uV);
896 constraints->min_uV = min_uV;
898 if (constraints->max_uV > max_uV) {
899 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
900 constraints->max_uV, max_uV);
901 constraints->max_uV = max_uV;
905 return 0;
909 * set_machine_constraints - sets regulator constraints
910 * @rdev: regulator source
911 * @constraints: constraints to apply
913 * Allows platform initialisation code to define and constrain
914 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
915 * Constraints *must* be set by platform code in order for some
916 * regulator operations to proceed i.e. set_voltage, set_current_limit,
917 * set_mode.
919 static int set_machine_constraints(struct regulator_dev *rdev,
920 const struct regulation_constraints *constraints)
922 int ret = 0;
923 struct regulator_ops *ops = rdev->desc->ops;
925 if (constraints)
926 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
927 GFP_KERNEL);
928 else
929 rdev->constraints = kzalloc(sizeof(*constraints),
930 GFP_KERNEL);
931 if (!rdev->constraints)
932 return -ENOMEM;
934 ret = machine_constraints_voltage(rdev, rdev->constraints);
935 if (ret != 0)
936 goto out;
938 /* do we need to setup our suspend state */
939 if (rdev->constraints->initial_state) {
940 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
941 if (ret < 0) {
942 rdev_err(rdev, "failed to set suspend state\n");
943 goto out;
947 if (rdev->constraints->initial_mode) {
948 if (!ops->set_mode) {
949 rdev_err(rdev, "no set_mode operation\n");
950 ret = -EINVAL;
951 goto out;
954 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
955 if (ret < 0) {
956 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
957 goto out;
961 /* If the constraints say the regulator should be on at this point
962 * and we have control then make sure it is enabled.
964 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
965 ops->enable) {
966 ret = ops->enable(rdev);
967 if (ret < 0) {
968 rdev_err(rdev, "failed to enable\n");
969 goto out;
973 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
974 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
975 if (ret < 0) {
976 rdev_err(rdev, "failed to set ramp_delay\n");
977 goto out;
981 print_constraints(rdev);
982 return 0;
983 out:
984 kfree(rdev->constraints);
985 rdev->constraints = NULL;
986 return ret;
990 * set_supply - set regulator supply regulator
991 * @rdev: regulator name
992 * @supply_rdev: supply regulator name
994 * Called by platform initialisation code to set the supply regulator for this
995 * regulator. This ensures that a regulators supply will also be enabled by the
996 * core if it's child is enabled.
998 static int set_supply(struct regulator_dev *rdev,
999 struct regulator_dev *supply_rdev)
1001 int err;
1003 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1005 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
1006 if (rdev->supply == NULL) {
1007 err = -ENOMEM;
1008 return err;
1010 supply_rdev->open_count++;
1012 return 0;
1016 * set_consumer_device_supply - Bind a regulator to a symbolic supply
1017 * @rdev: regulator source
1018 * @consumer_dev_name: dev_name() string for device supply applies to
1019 * @supply: symbolic name for supply
1021 * Allows platform initialisation code to map physical regulator
1022 * sources to symbolic names for supplies for use by devices. Devices
1023 * should use these symbolic names to request regulators, avoiding the
1024 * need to provide board-specific regulator names as platform data.
1026 static int set_consumer_device_supply(struct regulator_dev *rdev,
1027 const char *consumer_dev_name,
1028 const char *supply)
1030 struct regulator_map *node;
1031 int has_dev;
1033 if (supply == NULL)
1034 return -EINVAL;
1036 if (consumer_dev_name != NULL)
1037 has_dev = 1;
1038 else
1039 has_dev = 0;
1041 list_for_each_entry(node, &regulator_map_list, list) {
1042 if (node->dev_name && consumer_dev_name) {
1043 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1044 continue;
1045 } else if (node->dev_name || consumer_dev_name) {
1046 continue;
1049 if (strcmp(node->supply, supply) != 0)
1050 continue;
1052 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1053 consumer_dev_name,
1054 dev_name(&node->regulator->dev),
1055 node->regulator->desc->name,
1056 supply,
1057 dev_name(&rdev->dev), rdev_get_name(rdev));
1058 return -EBUSY;
1061 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
1062 if (node == NULL)
1063 return -ENOMEM;
1065 node->regulator = rdev;
1066 node->supply = supply;
1068 if (has_dev) {
1069 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1070 if (node->dev_name == NULL) {
1071 kfree(node);
1072 return -ENOMEM;
1076 list_add(&node->list, &regulator_map_list);
1077 return 0;
1080 static void unset_regulator_supplies(struct regulator_dev *rdev)
1082 struct regulator_map *node, *n;
1084 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1085 if (rdev == node->regulator) {
1086 list_del(&node->list);
1087 kfree(node->dev_name);
1088 kfree(node);
1093 #define REG_STR_SIZE 64
1095 static struct regulator *create_regulator(struct regulator_dev *rdev,
1096 struct device *dev,
1097 const char *supply_name)
1099 struct regulator *regulator;
1100 char buf[REG_STR_SIZE];
1101 int err, size;
1103 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1104 if (regulator == NULL)
1105 return NULL;
1107 mutex_lock(&rdev->mutex);
1108 regulator->rdev = rdev;
1109 list_add(&regulator->list, &rdev->consumer_list);
1111 if (dev) {
1112 regulator->dev = dev;
1114 /* Add a link to the device sysfs entry */
1115 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1116 dev->kobj.name, supply_name);
1117 if (size >= REG_STR_SIZE)
1118 goto overflow_err;
1120 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1121 if (regulator->supply_name == NULL)
1122 goto overflow_err;
1124 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1125 buf);
1126 if (err) {
1127 rdev_warn(rdev, "could not add device link %s err %d\n",
1128 dev->kobj.name, err);
1129 /* non-fatal */
1131 } else {
1132 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1133 if (regulator->supply_name == NULL)
1134 goto overflow_err;
1137 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1138 rdev->debugfs);
1139 if (!regulator->debugfs) {
1140 rdev_warn(rdev, "Failed to create debugfs directory\n");
1141 } else {
1142 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1143 &regulator->uA_load);
1144 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1145 &regulator->min_uV);
1146 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1147 &regulator->max_uV);
1151 * Check now if the regulator is an always on regulator - if
1152 * it is then we don't need to do nearly so much work for
1153 * enable/disable calls.
1155 if (!_regulator_can_change_status(rdev) &&
1156 _regulator_is_enabled(rdev))
1157 regulator->always_on = true;
1159 mutex_unlock(&rdev->mutex);
1160 return regulator;
1161 overflow_err:
1162 list_del(&regulator->list);
1163 kfree(regulator);
1164 mutex_unlock(&rdev->mutex);
1165 return NULL;
1168 static int _regulator_get_enable_time(struct regulator_dev *rdev)
1170 if (!rdev->desc->ops->enable_time)
1171 return rdev->desc->enable_time;
1172 return rdev->desc->ops->enable_time(rdev);
1175 static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1176 const char *supply,
1177 int *ret)
1179 struct regulator_dev *r;
1180 struct device_node *node;
1181 struct regulator_map *map;
1182 const char *devname = NULL;
1184 /* first do a dt based lookup */
1185 if (dev && dev->of_node) {
1186 node = of_get_regulator(dev, supply);
1187 if (node) {
1188 list_for_each_entry(r, &regulator_list, list)
1189 if (r->dev.parent &&
1190 node == r->dev.of_node)
1191 return r;
1192 } else {
1194 * If we couldn't even get the node then it's
1195 * not just that the device didn't register
1196 * yet, there's no node and we'll never
1197 * succeed.
1199 *ret = -ENODEV;
1203 /* if not found, try doing it non-dt way */
1204 if (dev)
1205 devname = dev_name(dev);
1207 list_for_each_entry(r, &regulator_list, list)
1208 if (strcmp(rdev_get_name(r), supply) == 0)
1209 return r;
1211 list_for_each_entry(map, &regulator_map_list, list) {
1212 /* If the mapping has a device set up it must match */
1213 if (map->dev_name &&
1214 (!devname || strcmp(map->dev_name, devname)))
1215 continue;
1217 if (strcmp(map->supply, supply) == 0)
1218 return map->regulator;
1222 return NULL;
1225 /* Internal regulator request function */
1226 static struct regulator *_regulator_get(struct device *dev, const char *id,
1227 int exclusive)
1229 struct regulator_dev *rdev;
1230 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
1231 const char *devname = NULL;
1232 int ret;
1234 if (id == NULL) {
1235 pr_err("get() with no identifier\n");
1236 return regulator;
1239 if (dev)
1240 devname = dev_name(dev);
1242 mutex_lock(&regulator_list_mutex);
1244 rdev = regulator_dev_lookup(dev, id, &ret);
1245 if (rdev)
1246 goto found;
1248 if (board_wants_dummy_regulator) {
1249 rdev = dummy_regulator_rdev;
1250 goto found;
1253 #ifdef CONFIG_REGULATOR_DUMMY
1254 if (!devname)
1255 devname = "deviceless";
1257 /* If the board didn't flag that it was fully constrained then
1258 * substitute in a dummy regulator so consumers can continue.
1260 if (!has_full_constraints) {
1261 pr_warn("%s supply %s not found, using dummy regulator\n",
1262 devname, id);
1263 rdev = dummy_regulator_rdev;
1264 goto found;
1266 #endif
1268 mutex_unlock(&regulator_list_mutex);
1269 return regulator;
1271 found:
1272 if (rdev->exclusive) {
1273 regulator = ERR_PTR(-EPERM);
1274 goto out;
1277 if (exclusive && rdev->open_count) {
1278 regulator = ERR_PTR(-EBUSY);
1279 goto out;
1282 if (!try_module_get(rdev->owner))
1283 goto out;
1285 regulator = create_regulator(rdev, dev, id);
1286 if (regulator == NULL) {
1287 regulator = ERR_PTR(-ENOMEM);
1288 module_put(rdev->owner);
1289 goto out;
1292 rdev->open_count++;
1293 if (exclusive) {
1294 rdev->exclusive = 1;
1296 ret = _regulator_is_enabled(rdev);
1297 if (ret > 0)
1298 rdev->use_count = 1;
1299 else
1300 rdev->use_count = 0;
1303 out:
1304 mutex_unlock(&regulator_list_mutex);
1306 return regulator;
1310 * regulator_get - lookup and obtain a reference to a regulator.
1311 * @dev: device for regulator "consumer"
1312 * @id: Supply name or regulator ID.
1314 * Returns a struct regulator corresponding to the regulator producer,
1315 * or IS_ERR() condition containing errno.
1317 * Use of supply names configured via regulator_set_device_supply() is
1318 * strongly encouraged. It is recommended that the supply name used
1319 * should match the name used for the supply and/or the relevant
1320 * device pins in the datasheet.
1322 struct regulator *regulator_get(struct device *dev, const char *id)
1324 return _regulator_get(dev, id, 0);
1326 EXPORT_SYMBOL_GPL(regulator_get);
1328 static void devm_regulator_release(struct device *dev, void *res)
1330 regulator_put(*(struct regulator **)res);
1334 * devm_regulator_get - Resource managed regulator_get()
1335 * @dev: device for regulator "consumer"
1336 * @id: Supply name or regulator ID.
1338 * Managed regulator_get(). Regulators returned from this function are
1339 * automatically regulator_put() on driver detach. See regulator_get() for more
1340 * information.
1342 struct regulator *devm_regulator_get(struct device *dev, const char *id)
1344 struct regulator **ptr, *regulator;
1346 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1347 if (!ptr)
1348 return ERR_PTR(-ENOMEM);
1350 regulator = regulator_get(dev, id);
1351 if (!IS_ERR(regulator)) {
1352 *ptr = regulator;
1353 devres_add(dev, ptr);
1354 } else {
1355 devres_free(ptr);
1358 return regulator;
1360 EXPORT_SYMBOL_GPL(devm_regulator_get);
1363 * regulator_get_exclusive - obtain exclusive access to a regulator.
1364 * @dev: device for regulator "consumer"
1365 * @id: Supply name or regulator ID.
1367 * Returns a struct regulator corresponding to the regulator producer,
1368 * or IS_ERR() condition containing errno. Other consumers will be
1369 * unable to obtain this reference is held and the use count for the
1370 * regulator will be initialised to reflect the current state of the
1371 * regulator.
1373 * This is intended for use by consumers which cannot tolerate shared
1374 * use of the regulator such as those which need to force the
1375 * regulator off for correct operation of the hardware they are
1376 * controlling.
1378 * Use of supply names configured via regulator_set_device_supply() is
1379 * strongly encouraged. It is recommended that the supply name used
1380 * should match the name used for the supply and/or the relevant
1381 * device pins in the datasheet.
1383 struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1385 return _regulator_get(dev, id, 1);
1387 EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1389 /* Locks held by regulator_put() */
1390 static void _regulator_put(struct regulator *regulator)
1392 struct regulator_dev *rdev;
1394 if (regulator == NULL || IS_ERR(regulator))
1395 return;
1397 rdev = regulator->rdev;
1399 debugfs_remove_recursive(regulator->debugfs);
1401 /* remove any sysfs entries */
1402 if (regulator->dev)
1403 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1404 kfree(regulator->supply_name);
1405 list_del(&regulator->list);
1406 kfree(regulator);
1408 rdev->open_count--;
1409 rdev->exclusive = 0;
1411 module_put(rdev->owner);
1415 * regulator_put - "free" the regulator source
1416 * @regulator: regulator source
1418 * Note: drivers must ensure that all regulator_enable calls made on this
1419 * regulator source are balanced by regulator_disable calls prior to calling
1420 * this function.
1422 void regulator_put(struct regulator *regulator)
1424 mutex_lock(&regulator_list_mutex);
1425 _regulator_put(regulator);
1426 mutex_unlock(&regulator_list_mutex);
1428 EXPORT_SYMBOL_GPL(regulator_put);
1430 static int devm_regulator_match(struct device *dev, void *res, void *data)
1432 struct regulator **r = res;
1433 if (!r || !*r) {
1434 WARN_ON(!r || !*r);
1435 return 0;
1437 return *r == data;
1441 * devm_regulator_put - Resource managed regulator_put()
1442 * @regulator: regulator to free
1444 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1445 * this function will not need to be called and the resource management
1446 * code will ensure that the resource is freed.
1448 void devm_regulator_put(struct regulator *regulator)
1450 int rc;
1452 rc = devres_release(regulator->dev, devm_regulator_release,
1453 devm_regulator_match, regulator);
1454 if (rc != 0)
1455 WARN_ON(rc);
1457 EXPORT_SYMBOL_GPL(devm_regulator_put);
1459 static int _regulator_do_enable(struct regulator_dev *rdev)
1461 int ret, delay;
1463 /* Query before enabling in case configuration dependent. */
1464 ret = _regulator_get_enable_time(rdev);
1465 if (ret >= 0) {
1466 delay = ret;
1467 } else {
1468 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1469 delay = 0;
1472 trace_regulator_enable(rdev_get_name(rdev));
1474 if (rdev->ena_gpio) {
1475 gpio_set_value_cansleep(rdev->ena_gpio,
1476 !rdev->ena_gpio_invert);
1477 rdev->ena_gpio_state = 1;
1478 } else if (rdev->desc->ops->enable) {
1479 ret = rdev->desc->ops->enable(rdev);
1480 if (ret < 0)
1481 return ret;
1482 } else {
1483 return -EINVAL;
1486 /* Allow the regulator to ramp; it would be useful to extend
1487 * this for bulk operations so that the regulators can ramp
1488 * together. */
1489 trace_regulator_enable_delay(rdev_get_name(rdev));
1491 if (delay >= 1000) {
1492 mdelay(delay / 1000);
1493 udelay(delay % 1000);
1494 } else if (delay) {
1495 udelay(delay);
1498 trace_regulator_enable_complete(rdev_get_name(rdev));
1500 return 0;
1503 /* locks held by regulator_enable() */
1504 static int _regulator_enable(struct regulator_dev *rdev)
1506 int ret;
1508 /* check voltage and requested load before enabling */
1509 if (rdev->constraints &&
1510 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1511 drms_uA_update(rdev);
1513 if (rdev->use_count == 0) {
1514 /* The regulator may on if it's not switchable or left on */
1515 ret = _regulator_is_enabled(rdev);
1516 if (ret == -EINVAL || ret == 0) {
1517 if (!_regulator_can_change_status(rdev))
1518 return -EPERM;
1520 ret = _regulator_do_enable(rdev);
1521 if (ret < 0)
1522 return ret;
1524 } else if (ret < 0) {
1525 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
1526 return ret;
1528 /* Fallthrough on positive return values - already enabled */
1531 rdev->use_count++;
1533 return 0;
1537 * regulator_enable - enable regulator output
1538 * @regulator: regulator source
1540 * Request that the regulator be enabled with the regulator output at
1541 * the predefined voltage or current value. Calls to regulator_enable()
1542 * must be balanced with calls to regulator_disable().
1544 * NOTE: the output value can be set by other drivers, boot loader or may be
1545 * hardwired in the regulator.
1547 int regulator_enable(struct regulator *regulator)
1549 struct regulator_dev *rdev = regulator->rdev;
1550 int ret = 0;
1552 if (regulator->always_on)
1553 return 0;
1555 if (rdev->supply) {
1556 ret = regulator_enable(rdev->supply);
1557 if (ret != 0)
1558 return ret;
1561 mutex_lock(&rdev->mutex);
1562 ret = _regulator_enable(rdev);
1563 mutex_unlock(&rdev->mutex);
1565 if (ret != 0 && rdev->supply)
1566 regulator_disable(rdev->supply);
1568 return ret;
1570 EXPORT_SYMBOL_GPL(regulator_enable);
1572 static int _regulator_do_disable(struct regulator_dev *rdev)
1574 int ret;
1576 trace_regulator_disable(rdev_get_name(rdev));
1578 if (rdev->ena_gpio) {
1579 gpio_set_value_cansleep(rdev->ena_gpio,
1580 rdev->ena_gpio_invert);
1581 rdev->ena_gpio_state = 0;
1583 } else if (rdev->desc->ops->disable) {
1584 ret = rdev->desc->ops->disable(rdev);
1585 if (ret != 0)
1586 return ret;
1589 trace_regulator_disable_complete(rdev_get_name(rdev));
1591 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1592 NULL);
1593 return 0;
1596 /* locks held by regulator_disable() */
1597 static int _regulator_disable(struct regulator_dev *rdev)
1599 int ret = 0;
1601 if (WARN(rdev->use_count <= 0,
1602 "unbalanced disables for %s\n", rdev_get_name(rdev)))
1603 return -EIO;
1605 /* are we the last user and permitted to disable ? */
1606 if (rdev->use_count == 1 &&
1607 (rdev->constraints && !rdev->constraints->always_on)) {
1609 /* we are last user */
1610 if (_regulator_can_change_status(rdev)) {
1611 ret = _regulator_do_disable(rdev);
1612 if (ret < 0) {
1613 rdev_err(rdev, "failed to disable\n");
1614 return ret;
1618 rdev->use_count = 0;
1619 } else if (rdev->use_count > 1) {
1621 if (rdev->constraints &&
1622 (rdev->constraints->valid_ops_mask &
1623 REGULATOR_CHANGE_DRMS))
1624 drms_uA_update(rdev);
1626 rdev->use_count--;
1629 return ret;
1633 * regulator_disable - disable regulator output
1634 * @regulator: regulator source
1636 * Disable the regulator output voltage or current. Calls to
1637 * regulator_enable() must be balanced with calls to
1638 * regulator_disable().
1640 * NOTE: this will only disable the regulator output if no other consumer
1641 * devices have it enabled, the regulator device supports disabling and
1642 * machine constraints permit this operation.
1644 int regulator_disable(struct regulator *regulator)
1646 struct regulator_dev *rdev = regulator->rdev;
1647 int ret = 0;
1649 if (regulator->always_on)
1650 return 0;
1652 mutex_lock(&rdev->mutex);
1653 ret = _regulator_disable(rdev);
1654 mutex_unlock(&rdev->mutex);
1656 if (ret == 0 && rdev->supply)
1657 regulator_disable(rdev->supply);
1659 return ret;
1661 EXPORT_SYMBOL_GPL(regulator_disable);
1663 /* locks held by regulator_force_disable() */
1664 static int _regulator_force_disable(struct regulator_dev *rdev)
1666 int ret = 0;
1668 /* force disable */
1669 if (rdev->desc->ops->disable) {
1670 /* ah well, who wants to live forever... */
1671 ret = rdev->desc->ops->disable(rdev);
1672 if (ret < 0) {
1673 rdev_err(rdev, "failed to force disable\n");
1674 return ret;
1676 /* notify other consumers that power has been forced off */
1677 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1678 REGULATOR_EVENT_DISABLE, NULL);
1681 return ret;
1685 * regulator_force_disable - force disable regulator output
1686 * @regulator: regulator source
1688 * Forcibly disable the regulator output voltage or current.
1689 * NOTE: this *will* disable the regulator output even if other consumer
1690 * devices have it enabled. This should be used for situations when device
1691 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1693 int regulator_force_disable(struct regulator *regulator)
1695 struct regulator_dev *rdev = regulator->rdev;
1696 int ret;
1698 mutex_lock(&rdev->mutex);
1699 regulator->uA_load = 0;
1700 ret = _regulator_force_disable(regulator->rdev);
1701 mutex_unlock(&rdev->mutex);
1703 if (rdev->supply)
1704 while (rdev->open_count--)
1705 regulator_disable(rdev->supply);
1707 return ret;
1709 EXPORT_SYMBOL_GPL(regulator_force_disable);
1711 static void regulator_disable_work(struct work_struct *work)
1713 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1714 disable_work.work);
1715 int count, i, ret;
1717 mutex_lock(&rdev->mutex);
1719 BUG_ON(!rdev->deferred_disables);
1721 count = rdev->deferred_disables;
1722 rdev->deferred_disables = 0;
1724 for (i = 0; i < count; i++) {
1725 ret = _regulator_disable(rdev);
1726 if (ret != 0)
1727 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1730 mutex_unlock(&rdev->mutex);
1732 if (rdev->supply) {
1733 for (i = 0; i < count; i++) {
1734 ret = regulator_disable(rdev->supply);
1735 if (ret != 0) {
1736 rdev_err(rdev,
1737 "Supply disable failed: %d\n", ret);
1744 * regulator_disable_deferred - disable regulator output with delay
1745 * @regulator: regulator source
1746 * @ms: miliseconds until the regulator is disabled
1748 * Execute regulator_disable() on the regulator after a delay. This
1749 * is intended for use with devices that require some time to quiesce.
1751 * NOTE: this will only disable the regulator output if no other consumer
1752 * devices have it enabled, the regulator device supports disabling and
1753 * machine constraints permit this operation.
1755 int regulator_disable_deferred(struct regulator *regulator, int ms)
1757 struct regulator_dev *rdev = regulator->rdev;
1758 int ret;
1760 if (regulator->always_on)
1761 return 0;
1763 if (!ms)
1764 return regulator_disable(regulator);
1766 mutex_lock(&rdev->mutex);
1767 rdev->deferred_disables++;
1768 mutex_unlock(&rdev->mutex);
1770 ret = schedule_delayed_work(&rdev->disable_work,
1771 msecs_to_jiffies(ms));
1772 if (ret < 0)
1773 return ret;
1774 else
1775 return 0;
1777 EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1780 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1782 * @rdev: regulator to operate on
1784 * Regulators that use regmap for their register I/O can set the
1785 * enable_reg and enable_mask fields in their descriptor and then use
1786 * this as their is_enabled operation, saving some code.
1788 int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1790 unsigned int val;
1791 int ret;
1793 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1794 if (ret != 0)
1795 return ret;
1797 return (val & rdev->desc->enable_mask) != 0;
1799 EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1802 * regulator_enable_regmap - standard enable() for regmap users
1804 * @rdev: regulator to operate on
1806 * Regulators that use regmap for their register I/O can set the
1807 * enable_reg and enable_mask fields in their descriptor and then use
1808 * this as their enable() operation, saving some code.
1810 int regulator_enable_regmap(struct regulator_dev *rdev)
1812 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1813 rdev->desc->enable_mask,
1814 rdev->desc->enable_mask);
1816 EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1819 * regulator_disable_regmap - standard disable() for regmap users
1821 * @rdev: regulator to operate on
1823 * Regulators that use regmap for their register I/O can set the
1824 * enable_reg and enable_mask fields in their descriptor and then use
1825 * this as their disable() operation, saving some code.
1827 int regulator_disable_regmap(struct regulator_dev *rdev)
1829 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1830 rdev->desc->enable_mask, 0);
1832 EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1834 static int _regulator_is_enabled(struct regulator_dev *rdev)
1836 /* A GPIO control always takes precedence */
1837 if (rdev->ena_gpio)
1838 return rdev->ena_gpio_state;
1840 /* If we don't know then assume that the regulator is always on */
1841 if (!rdev->desc->ops->is_enabled)
1842 return 1;
1844 return rdev->desc->ops->is_enabled(rdev);
1848 * regulator_is_enabled - is the regulator output enabled
1849 * @regulator: regulator source
1851 * Returns positive if the regulator driver backing the source/client
1852 * has requested that the device be enabled, zero if it hasn't, else a
1853 * negative errno code.
1855 * Note that the device backing this regulator handle can have multiple
1856 * users, so it might be enabled even if regulator_enable() was never
1857 * called for this particular source.
1859 int regulator_is_enabled(struct regulator *regulator)
1861 int ret;
1863 if (regulator->always_on)
1864 return 1;
1866 mutex_lock(&regulator->rdev->mutex);
1867 ret = _regulator_is_enabled(regulator->rdev);
1868 mutex_unlock(&regulator->rdev->mutex);
1870 return ret;
1872 EXPORT_SYMBOL_GPL(regulator_is_enabled);
1875 * regulator_can_change_voltage - check if regulator can change voltage
1876 * @regulator: regulator source
1878 * Returns positive if the regulator driver backing the source/client
1879 * can change its voltage, false otherwise. Usefull for detecting fixed
1880 * or dummy regulators and disabling voltage change logic in the client
1881 * driver.
1883 int regulator_can_change_voltage(struct regulator *regulator)
1885 struct regulator_dev *rdev = regulator->rdev;
1887 if (rdev->constraints &&
1888 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1889 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1890 return 1;
1892 if (rdev->desc->continuous_voltage_range &&
1893 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1894 rdev->constraints->min_uV != rdev->constraints->max_uV)
1895 return 1;
1898 return 0;
1900 EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1903 * regulator_count_voltages - count regulator_list_voltage() selectors
1904 * @regulator: regulator source
1906 * Returns number of selectors, or negative errno. Selectors are
1907 * numbered starting at zero, and typically correspond to bitfields
1908 * in hardware registers.
1910 int regulator_count_voltages(struct regulator *regulator)
1912 struct regulator_dev *rdev = regulator->rdev;
1914 return rdev->desc->n_voltages ? : -EINVAL;
1916 EXPORT_SYMBOL_GPL(regulator_count_voltages);
1919 * regulator_list_voltage_linear - List voltages with simple calculation
1921 * @rdev: Regulator device
1922 * @selector: Selector to convert into a voltage
1924 * Regulators with a simple linear mapping between voltages and
1925 * selectors can set min_uV and uV_step in the regulator descriptor
1926 * and then use this function as their list_voltage() operation,
1928 int regulator_list_voltage_linear(struct regulator_dev *rdev,
1929 unsigned int selector)
1931 if (selector >= rdev->desc->n_voltages)
1932 return -EINVAL;
1933 if (selector < rdev->desc->linear_min_sel)
1934 return 0;
1936 selector -= rdev->desc->linear_min_sel;
1938 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
1940 EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
1943 * regulator_list_voltage_table - List voltages with table based mapping
1945 * @rdev: Regulator device
1946 * @selector: Selector to convert into a voltage
1948 * Regulators with table based mapping between voltages and
1949 * selectors can set volt_table in the regulator descriptor
1950 * and then use this function as their list_voltage() operation.
1952 int regulator_list_voltage_table(struct regulator_dev *rdev,
1953 unsigned int selector)
1955 if (!rdev->desc->volt_table) {
1956 BUG_ON(!rdev->desc->volt_table);
1957 return -EINVAL;
1960 if (selector >= rdev->desc->n_voltages)
1961 return -EINVAL;
1963 return rdev->desc->volt_table[selector];
1965 EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
1968 * regulator_list_voltage - enumerate supported voltages
1969 * @regulator: regulator source
1970 * @selector: identify voltage to list
1971 * Context: can sleep
1973 * Returns a voltage that can be passed to @regulator_set_voltage(),
1974 * zero if this selector code can't be used on this system, or a
1975 * negative errno.
1977 int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1979 struct regulator_dev *rdev = regulator->rdev;
1980 struct regulator_ops *ops = rdev->desc->ops;
1981 int ret;
1983 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1984 return -EINVAL;
1986 mutex_lock(&rdev->mutex);
1987 ret = ops->list_voltage(rdev, selector);
1988 mutex_unlock(&rdev->mutex);
1990 if (ret > 0) {
1991 if (ret < rdev->constraints->min_uV)
1992 ret = 0;
1993 else if (ret > rdev->constraints->max_uV)
1994 ret = 0;
1997 return ret;
1999 EXPORT_SYMBOL_GPL(regulator_list_voltage);
2002 * regulator_is_supported_voltage - check if a voltage range can be supported
2004 * @regulator: Regulator to check.
2005 * @min_uV: Minimum required voltage in uV.
2006 * @max_uV: Maximum required voltage in uV.
2008 * Returns a boolean or a negative error code.
2010 int regulator_is_supported_voltage(struct regulator *regulator,
2011 int min_uV, int max_uV)
2013 struct regulator_dev *rdev = regulator->rdev;
2014 int i, voltages, ret;
2016 /* If we can't change voltage check the current voltage */
2017 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2018 ret = regulator_get_voltage(regulator);
2019 if (ret >= 0)
2020 return (min_uV <= ret && ret <= max_uV);
2021 else
2022 return ret;
2025 /* Any voltage within constrains range is fine? */
2026 if (rdev->desc->continuous_voltage_range)
2027 return min_uV >= rdev->constraints->min_uV &&
2028 max_uV <= rdev->constraints->max_uV;
2030 ret = regulator_count_voltages(regulator);
2031 if (ret < 0)
2032 return ret;
2033 voltages = ret;
2035 for (i = 0; i < voltages; i++) {
2036 ret = regulator_list_voltage(regulator, i);
2038 if (ret >= min_uV && ret <= max_uV)
2039 return 1;
2042 return 0;
2044 EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
2047 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2049 * @rdev: regulator to operate on
2051 * Regulators that use regmap for their register I/O can set the
2052 * vsel_reg and vsel_mask fields in their descriptor and then use this
2053 * as their get_voltage_vsel operation, saving some code.
2055 int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2057 unsigned int val;
2058 int ret;
2060 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2061 if (ret != 0)
2062 return ret;
2064 val &= rdev->desc->vsel_mask;
2065 val >>= ffs(rdev->desc->vsel_mask) - 1;
2067 return val;
2069 EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2072 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2074 * @rdev: regulator to operate on
2075 * @sel: Selector to set
2077 * Regulators that use regmap for their register I/O can set the
2078 * vsel_reg and vsel_mask fields in their descriptor and then use this
2079 * as their set_voltage_vsel operation, saving some code.
2081 int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2083 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2085 return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
2086 rdev->desc->vsel_mask, sel);
2088 EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2091 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2093 * @rdev: Regulator to operate on
2094 * @min_uV: Lower bound for voltage
2095 * @max_uV: Upper bound for voltage
2097 * Drivers implementing set_voltage_sel() and list_voltage() can use
2098 * this as their map_voltage() operation. It will find a suitable
2099 * voltage by calling list_voltage() until it gets something in bounds
2100 * for the requested voltages.
2102 int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2103 int min_uV, int max_uV)
2105 int best_val = INT_MAX;
2106 int selector = 0;
2107 int i, ret;
2109 /* Find the smallest voltage that falls within the specified
2110 * range.
2112 for (i = 0; i < rdev->desc->n_voltages; i++) {
2113 ret = rdev->desc->ops->list_voltage(rdev, i);
2114 if (ret < 0)
2115 continue;
2117 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2118 best_val = ret;
2119 selector = i;
2123 if (best_val != INT_MAX)
2124 return selector;
2125 else
2126 return -EINVAL;
2128 EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2131 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2133 * @rdev: Regulator to operate on
2134 * @min_uV: Lower bound for voltage
2135 * @max_uV: Upper bound for voltage
2137 * Drivers providing min_uV and uV_step in their regulator_desc can
2138 * use this as their map_voltage() operation.
2140 int regulator_map_voltage_linear(struct regulator_dev *rdev,
2141 int min_uV, int max_uV)
2143 int ret, voltage;
2145 /* Allow uV_step to be 0 for fixed voltage */
2146 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2147 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2148 return 0;
2149 else
2150 return -EINVAL;
2153 if (!rdev->desc->uV_step) {
2154 BUG_ON(!rdev->desc->uV_step);
2155 return -EINVAL;
2158 if (min_uV < rdev->desc->min_uV)
2159 min_uV = rdev->desc->min_uV;
2161 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
2162 if (ret < 0)
2163 return ret;
2165 ret += rdev->desc->linear_min_sel;
2167 /* Map back into a voltage to verify we're still in bounds */
2168 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2169 if (voltage < min_uV || voltage > max_uV)
2170 return -EINVAL;
2172 return ret;
2174 EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2176 static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2177 int min_uV, int max_uV)
2179 int ret;
2180 int delay = 0;
2181 int best_val = 0;
2182 unsigned int selector;
2183 int old_selector = -1;
2185 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2187 min_uV += rdev->constraints->uV_offset;
2188 max_uV += rdev->constraints->uV_offset;
2191 * If we can't obtain the old selector there is not enough
2192 * info to call set_voltage_time_sel().
2194 if (_regulator_is_enabled(rdev) &&
2195 rdev->desc->ops->set_voltage_time_sel &&
2196 rdev->desc->ops->get_voltage_sel) {
2197 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2198 if (old_selector < 0)
2199 return old_selector;
2202 if (rdev->desc->ops->set_voltage) {
2203 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2204 &selector);
2206 if (ret >= 0) {
2207 if (rdev->desc->ops->list_voltage)
2208 best_val = rdev->desc->ops->list_voltage(rdev,
2209 selector);
2210 else
2211 best_val = _regulator_get_voltage(rdev);
2214 } else if (rdev->desc->ops->set_voltage_sel) {
2215 if (rdev->desc->ops->map_voltage) {
2216 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2217 max_uV);
2218 } else {
2219 if (rdev->desc->ops->list_voltage ==
2220 regulator_list_voltage_linear)
2221 ret = regulator_map_voltage_linear(rdev,
2222 min_uV, max_uV);
2223 else
2224 ret = regulator_map_voltage_iterate(rdev,
2225 min_uV, max_uV);
2228 if (ret >= 0) {
2229 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2230 if (min_uV <= best_val && max_uV >= best_val) {
2231 selector = ret;
2232 ret = rdev->desc->ops->set_voltage_sel(rdev,
2233 ret);
2234 } else {
2235 ret = -EINVAL;
2238 } else {
2239 ret = -EINVAL;
2242 /* Call set_voltage_time_sel if successfully obtained old_selector */
2243 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
2244 rdev->desc->ops->set_voltage_time_sel) {
2246 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2247 old_selector, selector);
2248 if (delay < 0) {
2249 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2250 delay);
2251 delay = 0;
2254 /* Insert any necessary delays */
2255 if (delay >= 1000) {
2256 mdelay(delay / 1000);
2257 udelay(delay % 1000);
2258 } else if (delay) {
2259 udelay(delay);
2263 if (ret == 0 && best_val >= 0) {
2264 unsigned long data = best_val;
2266 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
2267 (void *)data);
2270 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
2272 return ret;
2276 * regulator_set_voltage - set regulator output voltage
2277 * @regulator: regulator source
2278 * @min_uV: Minimum required voltage in uV
2279 * @max_uV: Maximum acceptable voltage in uV
2281 * Sets a voltage regulator to the desired output voltage. This can be set
2282 * during any regulator state. IOW, regulator can be disabled or enabled.
2284 * If the regulator is enabled then the voltage will change to the new value
2285 * immediately otherwise if the regulator is disabled the regulator will
2286 * output at the new voltage when enabled.
2288 * NOTE: If the regulator is shared between several devices then the lowest
2289 * request voltage that meets the system constraints will be used.
2290 * Regulator system constraints must be set for this regulator before
2291 * calling this function otherwise this call will fail.
2293 int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2295 struct regulator_dev *rdev = regulator->rdev;
2296 int ret = 0;
2298 mutex_lock(&rdev->mutex);
2300 /* If we're setting the same range as last time the change
2301 * should be a noop (some cpufreq implementations use the same
2302 * voltage for multiple frequencies, for example).
2304 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2305 goto out;
2307 /* sanity check */
2308 if (!rdev->desc->ops->set_voltage &&
2309 !rdev->desc->ops->set_voltage_sel) {
2310 ret = -EINVAL;
2311 goto out;
2314 /* constraints check */
2315 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2316 if (ret < 0)
2317 goto out;
2318 regulator->min_uV = min_uV;
2319 regulator->max_uV = max_uV;
2321 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2322 if (ret < 0)
2323 goto out;
2325 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2327 out:
2328 mutex_unlock(&rdev->mutex);
2329 return ret;
2331 EXPORT_SYMBOL_GPL(regulator_set_voltage);
2334 * regulator_set_voltage_time - get raise/fall time
2335 * @regulator: regulator source
2336 * @old_uV: starting voltage in microvolts
2337 * @new_uV: target voltage in microvolts
2339 * Provided with the starting and ending voltage, this function attempts to
2340 * calculate the time in microseconds required to rise or fall to this new
2341 * voltage.
2343 int regulator_set_voltage_time(struct regulator *regulator,
2344 int old_uV, int new_uV)
2346 struct regulator_dev *rdev = regulator->rdev;
2347 struct regulator_ops *ops = rdev->desc->ops;
2348 int old_sel = -1;
2349 int new_sel = -1;
2350 int voltage;
2351 int i;
2353 /* Currently requires operations to do this */
2354 if (!ops->list_voltage || !ops->set_voltage_time_sel
2355 || !rdev->desc->n_voltages)
2356 return -EINVAL;
2358 for (i = 0; i < rdev->desc->n_voltages; i++) {
2359 /* We only look for exact voltage matches here */
2360 voltage = regulator_list_voltage(regulator, i);
2361 if (voltage < 0)
2362 return -EINVAL;
2363 if (voltage == 0)
2364 continue;
2365 if (voltage == old_uV)
2366 old_sel = i;
2367 if (voltage == new_uV)
2368 new_sel = i;
2371 if (old_sel < 0 || new_sel < 0)
2372 return -EINVAL;
2374 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2376 EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2379 * regulator_set_voltage_time_sel - get raise/fall time
2380 * @rdev: regulator source device
2381 * @old_selector: selector for starting voltage
2382 * @new_selector: selector for target voltage
2384 * Provided with the starting and target voltage selectors, this function
2385 * returns time in microseconds required to rise or fall to this new voltage
2387 * Drivers providing ramp_delay in regulation_constraints can use this as their
2388 * set_voltage_time_sel() operation.
2390 int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2391 unsigned int old_selector,
2392 unsigned int new_selector)
2394 unsigned int ramp_delay = 0;
2395 int old_volt, new_volt;
2397 if (rdev->constraints->ramp_delay)
2398 ramp_delay = rdev->constraints->ramp_delay;
2399 else if (rdev->desc->ramp_delay)
2400 ramp_delay = rdev->desc->ramp_delay;
2402 if (ramp_delay == 0) {
2403 rdev_warn(rdev, "ramp_delay not set\n");
2404 return 0;
2407 /* sanity check */
2408 if (!rdev->desc->ops->list_voltage)
2409 return -EINVAL;
2411 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2412 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2414 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
2416 EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
2419 * regulator_sync_voltage - re-apply last regulator output voltage
2420 * @regulator: regulator source
2422 * Re-apply the last configured voltage. This is intended to be used
2423 * where some external control source the consumer is cooperating with
2424 * has caused the configured voltage to change.
2426 int regulator_sync_voltage(struct regulator *regulator)
2428 struct regulator_dev *rdev = regulator->rdev;
2429 int ret, min_uV, max_uV;
2431 mutex_lock(&rdev->mutex);
2433 if (!rdev->desc->ops->set_voltage &&
2434 !rdev->desc->ops->set_voltage_sel) {
2435 ret = -EINVAL;
2436 goto out;
2439 /* This is only going to work if we've had a voltage configured. */
2440 if (!regulator->min_uV && !regulator->max_uV) {
2441 ret = -EINVAL;
2442 goto out;
2445 min_uV = regulator->min_uV;
2446 max_uV = regulator->max_uV;
2448 /* This should be a paranoia check... */
2449 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2450 if (ret < 0)
2451 goto out;
2453 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2454 if (ret < 0)
2455 goto out;
2457 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2459 out:
2460 mutex_unlock(&rdev->mutex);
2461 return ret;
2463 EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2465 static int _regulator_get_voltage(struct regulator_dev *rdev)
2467 int sel, ret;
2469 if (rdev->desc->ops->get_voltage_sel) {
2470 sel = rdev->desc->ops->get_voltage_sel(rdev);
2471 if (sel < 0)
2472 return sel;
2473 ret = rdev->desc->ops->list_voltage(rdev, sel);
2474 } else if (rdev->desc->ops->get_voltage) {
2475 ret = rdev->desc->ops->get_voltage(rdev);
2476 } else if (rdev->desc->ops->list_voltage) {
2477 ret = rdev->desc->ops->list_voltage(rdev, 0);
2478 } else {
2479 return -EINVAL;
2482 if (ret < 0)
2483 return ret;
2484 return ret - rdev->constraints->uV_offset;
2488 * regulator_get_voltage - get regulator output voltage
2489 * @regulator: regulator source
2491 * This returns the current regulator voltage in uV.
2493 * NOTE: If the regulator is disabled it will return the voltage value. This
2494 * function should not be used to determine regulator state.
2496 int regulator_get_voltage(struct regulator *regulator)
2498 int ret;
2500 mutex_lock(&regulator->rdev->mutex);
2502 ret = _regulator_get_voltage(regulator->rdev);
2504 mutex_unlock(&regulator->rdev->mutex);
2506 return ret;
2508 EXPORT_SYMBOL_GPL(regulator_get_voltage);
2511 * regulator_set_current_limit - set regulator output current limit
2512 * @regulator: regulator source
2513 * @min_uA: Minimuum supported current in uA
2514 * @max_uA: Maximum supported current in uA
2516 * Sets current sink to the desired output current. This can be set during
2517 * any regulator state. IOW, regulator can be disabled or enabled.
2519 * If the regulator is enabled then the current will change to the new value
2520 * immediately otherwise if the regulator is disabled the regulator will
2521 * output at the new current when enabled.
2523 * NOTE: Regulator system constraints must be set for this regulator before
2524 * calling this function otherwise this call will fail.
2526 int regulator_set_current_limit(struct regulator *regulator,
2527 int min_uA, int max_uA)
2529 struct regulator_dev *rdev = regulator->rdev;
2530 int ret;
2532 mutex_lock(&rdev->mutex);
2534 /* sanity check */
2535 if (!rdev->desc->ops->set_current_limit) {
2536 ret = -EINVAL;
2537 goto out;
2540 /* constraints check */
2541 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2542 if (ret < 0)
2543 goto out;
2545 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2546 out:
2547 mutex_unlock(&rdev->mutex);
2548 return ret;
2550 EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2552 static int _regulator_get_current_limit(struct regulator_dev *rdev)
2554 int ret;
2556 mutex_lock(&rdev->mutex);
2558 /* sanity check */
2559 if (!rdev->desc->ops->get_current_limit) {
2560 ret = -EINVAL;
2561 goto out;
2564 ret = rdev->desc->ops->get_current_limit(rdev);
2565 out:
2566 mutex_unlock(&rdev->mutex);
2567 return ret;
2571 * regulator_get_current_limit - get regulator output current
2572 * @regulator: regulator source
2574 * This returns the current supplied by the specified current sink in uA.
2576 * NOTE: If the regulator is disabled it will return the current value. This
2577 * function should not be used to determine regulator state.
2579 int regulator_get_current_limit(struct regulator *regulator)
2581 return _regulator_get_current_limit(regulator->rdev);
2583 EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2586 * regulator_set_mode - set regulator operating mode
2587 * @regulator: regulator source
2588 * @mode: operating mode - one of the REGULATOR_MODE constants
2590 * Set regulator operating mode to increase regulator efficiency or improve
2591 * regulation performance.
2593 * NOTE: Regulator system constraints must be set for this regulator before
2594 * calling this function otherwise this call will fail.
2596 int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2598 struct regulator_dev *rdev = regulator->rdev;
2599 int ret;
2600 int regulator_curr_mode;
2602 mutex_lock(&rdev->mutex);
2604 /* sanity check */
2605 if (!rdev->desc->ops->set_mode) {
2606 ret = -EINVAL;
2607 goto out;
2610 /* return if the same mode is requested */
2611 if (rdev->desc->ops->get_mode) {
2612 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2613 if (regulator_curr_mode == mode) {
2614 ret = 0;
2615 goto out;
2619 /* constraints check */
2620 ret = regulator_mode_constrain(rdev, &mode);
2621 if (ret < 0)
2622 goto out;
2624 ret = rdev->desc->ops->set_mode(rdev, mode);
2625 out:
2626 mutex_unlock(&rdev->mutex);
2627 return ret;
2629 EXPORT_SYMBOL_GPL(regulator_set_mode);
2631 static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2633 int ret;
2635 mutex_lock(&rdev->mutex);
2637 /* sanity check */
2638 if (!rdev->desc->ops->get_mode) {
2639 ret = -EINVAL;
2640 goto out;
2643 ret = rdev->desc->ops->get_mode(rdev);
2644 out:
2645 mutex_unlock(&rdev->mutex);
2646 return ret;
2650 * regulator_get_mode - get regulator operating mode
2651 * @regulator: regulator source
2653 * Get the current regulator operating mode.
2655 unsigned int regulator_get_mode(struct regulator *regulator)
2657 return _regulator_get_mode(regulator->rdev);
2659 EXPORT_SYMBOL_GPL(regulator_get_mode);
2662 * regulator_set_optimum_mode - set regulator optimum operating mode
2663 * @regulator: regulator source
2664 * @uA_load: load current
2666 * Notifies the regulator core of a new device load. This is then used by
2667 * DRMS (if enabled by constraints) to set the most efficient regulator
2668 * operating mode for the new regulator loading.
2670 * Consumer devices notify their supply regulator of the maximum power
2671 * they will require (can be taken from device datasheet in the power
2672 * consumption tables) when they change operational status and hence power
2673 * state. Examples of operational state changes that can affect power
2674 * consumption are :-
2676 * o Device is opened / closed.
2677 * o Device I/O is about to begin or has just finished.
2678 * o Device is idling in between work.
2680 * This information is also exported via sysfs to userspace.
2682 * DRMS will sum the total requested load on the regulator and change
2683 * to the most efficient operating mode if platform constraints allow.
2685 * Returns the new regulator mode or error.
2687 int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2689 struct regulator_dev *rdev = regulator->rdev;
2690 struct regulator *consumer;
2691 int ret, output_uV, input_uV = 0, total_uA_load = 0;
2692 unsigned int mode;
2694 if (rdev->supply)
2695 input_uV = regulator_get_voltage(rdev->supply);
2697 mutex_lock(&rdev->mutex);
2700 * first check to see if we can set modes at all, otherwise just
2701 * tell the consumer everything is OK.
2703 regulator->uA_load = uA_load;
2704 ret = regulator_check_drms(rdev);
2705 if (ret < 0) {
2706 ret = 0;
2707 goto out;
2710 if (!rdev->desc->ops->get_optimum_mode)
2711 goto out;
2714 * we can actually do this so any errors are indicators of
2715 * potential real failure.
2717 ret = -EINVAL;
2719 if (!rdev->desc->ops->set_mode)
2720 goto out;
2722 /* get output voltage */
2723 output_uV = _regulator_get_voltage(rdev);
2724 if (output_uV <= 0) {
2725 rdev_err(rdev, "invalid output voltage found\n");
2726 goto out;
2729 /* No supply? Use constraint voltage */
2730 if (input_uV <= 0)
2731 input_uV = rdev->constraints->input_uV;
2732 if (input_uV <= 0) {
2733 rdev_err(rdev, "invalid input voltage found\n");
2734 goto out;
2737 /* calc total requested load for this regulator */
2738 list_for_each_entry(consumer, &rdev->consumer_list, list)
2739 total_uA_load += consumer->uA_load;
2741 mode = rdev->desc->ops->get_optimum_mode(rdev,
2742 input_uV, output_uV,
2743 total_uA_load);
2744 ret = regulator_mode_constrain(rdev, &mode);
2745 if (ret < 0) {
2746 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2747 total_uA_load, input_uV, output_uV);
2748 goto out;
2751 ret = rdev->desc->ops->set_mode(rdev, mode);
2752 if (ret < 0) {
2753 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
2754 goto out;
2756 ret = mode;
2757 out:
2758 mutex_unlock(&rdev->mutex);
2759 return ret;
2761 EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2764 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2766 * @rdev: device to operate on.
2767 * @enable: state to set.
2769 int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2771 unsigned int val;
2773 if (enable)
2774 val = rdev->desc->bypass_mask;
2775 else
2776 val = 0;
2778 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2779 rdev->desc->bypass_mask, val);
2781 EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2784 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2786 * @rdev: device to operate on.
2787 * @enable: current state.
2789 int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2791 unsigned int val;
2792 int ret;
2794 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2795 if (ret != 0)
2796 return ret;
2798 *enable = val & rdev->desc->bypass_mask;
2800 return 0;
2802 EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2805 * regulator_allow_bypass - allow the regulator to go into bypass mode
2807 * @regulator: Regulator to configure
2808 * @allow: enable or disable bypass mode
2810 * Allow the regulator to go into bypass mode if all other consumers
2811 * for the regulator also enable bypass mode and the machine
2812 * constraints allow this. Bypass mode means that the regulator is
2813 * simply passing the input directly to the output with no regulation.
2815 int regulator_allow_bypass(struct regulator *regulator, bool enable)
2817 struct regulator_dev *rdev = regulator->rdev;
2818 int ret = 0;
2820 if (!rdev->desc->ops->set_bypass)
2821 return 0;
2823 if (rdev->constraints &&
2824 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2825 return 0;
2827 mutex_lock(&rdev->mutex);
2829 if (enable && !regulator->bypass) {
2830 rdev->bypass_count++;
2832 if (rdev->bypass_count == rdev->open_count) {
2833 ret = rdev->desc->ops->set_bypass(rdev, enable);
2834 if (ret != 0)
2835 rdev->bypass_count--;
2838 } else if (!enable && regulator->bypass) {
2839 rdev->bypass_count--;
2841 if (rdev->bypass_count != rdev->open_count) {
2842 ret = rdev->desc->ops->set_bypass(rdev, enable);
2843 if (ret != 0)
2844 rdev->bypass_count++;
2848 if (ret == 0)
2849 regulator->bypass = enable;
2851 mutex_unlock(&rdev->mutex);
2853 return ret;
2855 EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2858 * regulator_register_notifier - register regulator event notifier
2859 * @regulator: regulator source
2860 * @nb: notifier block
2862 * Register notifier block to receive regulator events.
2864 int regulator_register_notifier(struct regulator *regulator,
2865 struct notifier_block *nb)
2867 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2868 nb);
2870 EXPORT_SYMBOL_GPL(regulator_register_notifier);
2873 * regulator_unregister_notifier - unregister regulator event notifier
2874 * @regulator: regulator source
2875 * @nb: notifier block
2877 * Unregister regulator event notifier block.
2879 int regulator_unregister_notifier(struct regulator *regulator,
2880 struct notifier_block *nb)
2882 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2883 nb);
2885 EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2887 /* notify regulator consumers and downstream regulator consumers.
2888 * Note mutex must be held by caller.
2890 static void _notifier_call_chain(struct regulator_dev *rdev,
2891 unsigned long event, void *data)
2893 /* call rdev chain first */
2894 blocking_notifier_call_chain(&rdev->notifier, event, data);
2898 * regulator_bulk_get - get multiple regulator consumers
2900 * @dev: Device to supply
2901 * @num_consumers: Number of consumers to register
2902 * @consumers: Configuration of consumers; clients are stored here.
2904 * @return 0 on success, an errno on failure.
2906 * This helper function allows drivers to get several regulator
2907 * consumers in one operation. If any of the regulators cannot be
2908 * acquired then any regulators that were allocated will be freed
2909 * before returning to the caller.
2911 int regulator_bulk_get(struct device *dev, int num_consumers,
2912 struct regulator_bulk_data *consumers)
2914 int i;
2915 int ret;
2917 for (i = 0; i < num_consumers; i++)
2918 consumers[i].consumer = NULL;
2920 for (i = 0; i < num_consumers; i++) {
2921 consumers[i].consumer = regulator_get(dev,
2922 consumers[i].supply);
2923 if (IS_ERR(consumers[i].consumer)) {
2924 ret = PTR_ERR(consumers[i].consumer);
2925 dev_err(dev, "Failed to get supply '%s': %d\n",
2926 consumers[i].supply, ret);
2927 consumers[i].consumer = NULL;
2928 goto err;
2932 return 0;
2934 err:
2935 while (--i >= 0)
2936 regulator_put(consumers[i].consumer);
2938 return ret;
2940 EXPORT_SYMBOL_GPL(regulator_bulk_get);
2943 * devm_regulator_bulk_get - managed get multiple regulator consumers
2945 * @dev: Device to supply
2946 * @num_consumers: Number of consumers to register
2947 * @consumers: Configuration of consumers; clients are stored here.
2949 * @return 0 on success, an errno on failure.
2951 * This helper function allows drivers to get several regulator
2952 * consumers in one operation with management, the regulators will
2953 * automatically be freed when the device is unbound. If any of the
2954 * regulators cannot be acquired then any regulators that were
2955 * allocated will be freed before returning to the caller.
2957 int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2958 struct regulator_bulk_data *consumers)
2960 int i;
2961 int ret;
2963 for (i = 0; i < num_consumers; i++)
2964 consumers[i].consumer = NULL;
2966 for (i = 0; i < num_consumers; i++) {
2967 consumers[i].consumer = devm_regulator_get(dev,
2968 consumers[i].supply);
2969 if (IS_ERR(consumers[i].consumer)) {
2970 ret = PTR_ERR(consumers[i].consumer);
2971 dev_err(dev, "Failed to get supply '%s': %d\n",
2972 consumers[i].supply, ret);
2973 consumers[i].consumer = NULL;
2974 goto err;
2978 return 0;
2980 err:
2981 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2982 devm_regulator_put(consumers[i].consumer);
2984 return ret;
2986 EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2988 static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2990 struct regulator_bulk_data *bulk = data;
2992 bulk->ret = regulator_enable(bulk->consumer);
2996 * regulator_bulk_enable - enable multiple regulator consumers
2998 * @num_consumers: Number of consumers
2999 * @consumers: Consumer data; clients are stored here.
3000 * @return 0 on success, an errno on failure
3002 * This convenience API allows consumers to enable multiple regulator
3003 * clients in a single API call. If any consumers cannot be enabled
3004 * then any others that were enabled will be disabled again prior to
3005 * return.
3007 int regulator_bulk_enable(int num_consumers,
3008 struct regulator_bulk_data *consumers)
3010 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
3011 int i;
3012 int ret = 0;
3014 for (i = 0; i < num_consumers; i++) {
3015 if (consumers[i].consumer->always_on)
3016 consumers[i].ret = 0;
3017 else
3018 async_schedule_domain(regulator_bulk_enable_async,
3019 &consumers[i], &async_domain);
3022 async_synchronize_full_domain(&async_domain);
3024 /* If any consumer failed we need to unwind any that succeeded */
3025 for (i = 0; i < num_consumers; i++) {
3026 if (consumers[i].ret != 0) {
3027 ret = consumers[i].ret;
3028 goto err;
3032 return 0;
3034 err:
3035 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3036 while (--i >= 0)
3037 regulator_disable(consumers[i].consumer);
3039 return ret;
3041 EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3044 * regulator_bulk_disable - disable multiple regulator consumers
3046 * @num_consumers: Number of consumers
3047 * @consumers: Consumer data; clients are stored here.
3048 * @return 0 on success, an errno on failure
3050 * This convenience API allows consumers to disable multiple regulator
3051 * clients in a single API call. If any consumers cannot be disabled
3052 * then any others that were disabled will be enabled again prior to
3053 * return.
3055 int regulator_bulk_disable(int num_consumers,
3056 struct regulator_bulk_data *consumers)
3058 int i;
3059 int ret, r;
3061 for (i = num_consumers - 1; i >= 0; --i) {
3062 ret = regulator_disable(consumers[i].consumer);
3063 if (ret != 0)
3064 goto err;
3067 return 0;
3069 err:
3070 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
3071 for (++i; i < num_consumers; ++i) {
3072 r = regulator_enable(consumers[i].consumer);
3073 if (r != 0)
3074 pr_err("Failed to reename %s: %d\n",
3075 consumers[i].supply, r);
3078 return ret;
3080 EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3083 * regulator_bulk_force_disable - force disable multiple regulator consumers
3085 * @num_consumers: Number of consumers
3086 * @consumers: Consumer data; clients are stored here.
3087 * @return 0 on success, an errno on failure
3089 * This convenience API allows consumers to forcibly disable multiple regulator
3090 * clients in a single API call.
3091 * NOTE: This should be used for situations when device damage will
3092 * likely occur if the regulators are not disabled (e.g. over temp).
3093 * Although regulator_force_disable function call for some consumers can
3094 * return error numbers, the function is called for all consumers.
3096 int regulator_bulk_force_disable(int num_consumers,
3097 struct regulator_bulk_data *consumers)
3099 int i;
3100 int ret;
3102 for (i = 0; i < num_consumers; i++)
3103 consumers[i].ret =
3104 regulator_force_disable(consumers[i].consumer);
3106 for (i = 0; i < num_consumers; i++) {
3107 if (consumers[i].ret != 0) {
3108 ret = consumers[i].ret;
3109 goto out;
3113 return 0;
3114 out:
3115 return ret;
3117 EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3120 * regulator_bulk_free - free multiple regulator consumers
3122 * @num_consumers: Number of consumers
3123 * @consumers: Consumer data; clients are stored here.
3125 * This convenience API allows consumers to free multiple regulator
3126 * clients in a single API call.
3128 void regulator_bulk_free(int num_consumers,
3129 struct regulator_bulk_data *consumers)
3131 int i;
3133 for (i = 0; i < num_consumers; i++) {
3134 regulator_put(consumers[i].consumer);
3135 consumers[i].consumer = NULL;
3138 EXPORT_SYMBOL_GPL(regulator_bulk_free);
3141 * regulator_notifier_call_chain - call regulator event notifier
3142 * @rdev: regulator source
3143 * @event: notifier block
3144 * @data: callback-specific data.
3146 * Called by regulator drivers to notify clients a regulator event has
3147 * occurred. We also notify regulator clients downstream.
3148 * Note lock must be held by caller.
3150 int regulator_notifier_call_chain(struct regulator_dev *rdev,
3151 unsigned long event, void *data)
3153 _notifier_call_chain(rdev, event, data);
3154 return NOTIFY_DONE;
3157 EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3160 * regulator_mode_to_status - convert a regulator mode into a status
3162 * @mode: Mode to convert
3164 * Convert a regulator mode into a status.
3166 int regulator_mode_to_status(unsigned int mode)
3168 switch (mode) {
3169 case REGULATOR_MODE_FAST:
3170 return REGULATOR_STATUS_FAST;
3171 case REGULATOR_MODE_NORMAL:
3172 return REGULATOR_STATUS_NORMAL;
3173 case REGULATOR_MODE_IDLE:
3174 return REGULATOR_STATUS_IDLE;
3175 case REGULATOR_MODE_STANDBY:
3176 return REGULATOR_STATUS_STANDBY;
3177 default:
3178 return REGULATOR_STATUS_UNDEFINED;
3181 EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3184 * To avoid cluttering sysfs (and memory) with useless state, only
3185 * create attributes that can be meaningfully displayed.
3187 static int add_regulator_attributes(struct regulator_dev *rdev)
3189 struct device *dev = &rdev->dev;
3190 struct regulator_ops *ops = rdev->desc->ops;
3191 int status = 0;
3193 /* some attributes need specific methods to be displayed */
3194 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
3195 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3196 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
3197 status = device_create_file(dev, &dev_attr_microvolts);
3198 if (status < 0)
3199 return status;
3201 if (ops->get_current_limit) {
3202 status = device_create_file(dev, &dev_attr_microamps);
3203 if (status < 0)
3204 return status;
3206 if (ops->get_mode) {
3207 status = device_create_file(dev, &dev_attr_opmode);
3208 if (status < 0)
3209 return status;
3211 if (ops->is_enabled) {
3212 status = device_create_file(dev, &dev_attr_state);
3213 if (status < 0)
3214 return status;
3216 if (ops->get_status) {
3217 status = device_create_file(dev, &dev_attr_status);
3218 if (status < 0)
3219 return status;
3221 if (ops->get_bypass) {
3222 status = device_create_file(dev, &dev_attr_bypass);
3223 if (status < 0)
3224 return status;
3227 /* some attributes are type-specific */
3228 if (rdev->desc->type == REGULATOR_CURRENT) {
3229 status = device_create_file(dev, &dev_attr_requested_microamps);
3230 if (status < 0)
3231 return status;
3234 /* all the other attributes exist to support constraints;
3235 * don't show them if there are no constraints, or if the
3236 * relevant supporting methods are missing.
3238 if (!rdev->constraints)
3239 return status;
3241 /* constraints need specific supporting methods */
3242 if (ops->set_voltage || ops->set_voltage_sel) {
3243 status = device_create_file(dev, &dev_attr_min_microvolts);
3244 if (status < 0)
3245 return status;
3246 status = device_create_file(dev, &dev_attr_max_microvolts);
3247 if (status < 0)
3248 return status;
3250 if (ops->set_current_limit) {
3251 status = device_create_file(dev, &dev_attr_min_microamps);
3252 if (status < 0)
3253 return status;
3254 status = device_create_file(dev, &dev_attr_max_microamps);
3255 if (status < 0)
3256 return status;
3259 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3260 if (status < 0)
3261 return status;
3262 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3263 if (status < 0)
3264 return status;
3265 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3266 if (status < 0)
3267 return status;
3269 if (ops->set_suspend_voltage) {
3270 status = device_create_file(dev,
3271 &dev_attr_suspend_standby_microvolts);
3272 if (status < 0)
3273 return status;
3274 status = device_create_file(dev,
3275 &dev_attr_suspend_mem_microvolts);
3276 if (status < 0)
3277 return status;
3278 status = device_create_file(dev,
3279 &dev_attr_suspend_disk_microvolts);
3280 if (status < 0)
3281 return status;
3284 if (ops->set_suspend_mode) {
3285 status = device_create_file(dev,
3286 &dev_attr_suspend_standby_mode);
3287 if (status < 0)
3288 return status;
3289 status = device_create_file(dev,
3290 &dev_attr_suspend_mem_mode);
3291 if (status < 0)
3292 return status;
3293 status = device_create_file(dev,
3294 &dev_attr_suspend_disk_mode);
3295 if (status < 0)
3296 return status;
3299 return status;
3302 static void rdev_init_debugfs(struct regulator_dev *rdev)
3304 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3305 if (!rdev->debugfs) {
3306 rdev_warn(rdev, "Failed to create debugfs directory\n");
3307 return;
3310 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3311 &rdev->use_count);
3312 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3313 &rdev->open_count);
3314 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3315 &rdev->bypass_count);
3319 * regulator_register - register regulator
3320 * @regulator_desc: regulator to register
3321 * @config: runtime configuration for regulator
3323 * Called by regulator drivers to register a regulator.
3324 * Returns a valid pointer to struct regulator_dev on success
3325 * or an ERR_PTR() on error.
3327 struct regulator_dev *
3328 regulator_register(const struct regulator_desc *regulator_desc,
3329 const struct regulator_config *config)
3331 const struct regulation_constraints *constraints = NULL;
3332 const struct regulator_init_data *init_data;
3333 static atomic_t regulator_no = ATOMIC_INIT(0);
3334 struct regulator_dev *rdev;
3335 struct device *dev;
3336 int ret, i;
3337 const char *supply = NULL;
3339 if (regulator_desc == NULL || config == NULL)
3340 return ERR_PTR(-EINVAL);
3342 dev = config->dev;
3343 WARN_ON(!dev);
3345 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3346 return ERR_PTR(-EINVAL);
3348 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3349 regulator_desc->type != REGULATOR_CURRENT)
3350 return ERR_PTR(-EINVAL);
3352 /* Only one of each should be implemented */
3353 WARN_ON(regulator_desc->ops->get_voltage &&
3354 regulator_desc->ops->get_voltage_sel);
3355 WARN_ON(regulator_desc->ops->set_voltage &&
3356 regulator_desc->ops->set_voltage_sel);
3358 /* If we're using selectors we must implement list_voltage. */
3359 if (regulator_desc->ops->get_voltage_sel &&
3360 !regulator_desc->ops->list_voltage) {
3361 return ERR_PTR(-EINVAL);
3363 if (regulator_desc->ops->set_voltage_sel &&
3364 !regulator_desc->ops->list_voltage) {
3365 return ERR_PTR(-EINVAL);
3368 init_data = config->init_data;
3370 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3371 if (rdev == NULL)
3372 return ERR_PTR(-ENOMEM);
3374 mutex_lock(&regulator_list_mutex);
3376 mutex_init(&rdev->mutex);
3377 rdev->reg_data = config->driver_data;
3378 rdev->owner = regulator_desc->owner;
3379 rdev->desc = regulator_desc;
3380 if (config->regmap)
3381 rdev->regmap = config->regmap;
3382 else if (dev_get_regmap(dev, NULL))
3383 rdev->regmap = dev_get_regmap(dev, NULL);
3384 else if (dev->parent)
3385 rdev->regmap = dev_get_regmap(dev->parent, NULL);
3386 INIT_LIST_HEAD(&rdev->consumer_list);
3387 INIT_LIST_HEAD(&rdev->list);
3388 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
3389 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
3391 /* preform any regulator specific init */
3392 if (init_data && init_data->regulator_init) {
3393 ret = init_data->regulator_init(rdev->reg_data);
3394 if (ret < 0)
3395 goto clean;
3398 /* register with sysfs */
3399 rdev->dev.class = &regulator_class;
3400 rdev->dev.of_node = config->of_node;
3401 rdev->dev.parent = dev;
3402 dev_set_name(&rdev->dev, "regulator.%d",
3403 atomic_inc_return(&regulator_no) - 1);
3404 ret = device_register(&rdev->dev);
3405 if (ret != 0) {
3406 put_device(&rdev->dev);
3407 goto clean;
3410 dev_set_drvdata(&rdev->dev, rdev);
3412 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
3413 ret = gpio_request_one(config->ena_gpio,
3414 GPIOF_DIR_OUT | config->ena_gpio_flags,
3415 rdev_get_name(rdev));
3416 if (ret != 0) {
3417 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3418 config->ena_gpio, ret);
3419 goto wash;
3422 rdev->ena_gpio = config->ena_gpio;
3423 rdev->ena_gpio_invert = config->ena_gpio_invert;
3425 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3426 rdev->ena_gpio_state = 1;
3428 if (rdev->ena_gpio_invert)
3429 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3432 /* set regulator constraints */
3433 if (init_data)
3434 constraints = &init_data->constraints;
3436 ret = set_machine_constraints(rdev, constraints);
3437 if (ret < 0)
3438 goto scrub;
3440 /* add attributes supported by this regulator */
3441 ret = add_regulator_attributes(rdev);
3442 if (ret < 0)
3443 goto scrub;
3445 if (init_data && init_data->supply_regulator)
3446 supply = init_data->supply_regulator;
3447 else if (regulator_desc->supply_name)
3448 supply = regulator_desc->supply_name;
3450 if (supply) {
3451 struct regulator_dev *r;
3453 r = regulator_dev_lookup(dev, supply, &ret);
3455 if (!r) {
3456 dev_err(dev, "Failed to find supply %s\n", supply);
3457 ret = -EPROBE_DEFER;
3458 goto scrub;
3461 ret = set_supply(rdev, r);
3462 if (ret < 0)
3463 goto scrub;
3465 /* Enable supply if rail is enabled */
3466 if (_regulator_is_enabled(rdev)) {
3467 ret = regulator_enable(rdev->supply);
3468 if (ret < 0)
3469 goto scrub;
3473 /* add consumers devices */
3474 if (init_data) {
3475 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3476 ret = set_consumer_device_supply(rdev,
3477 init_data->consumer_supplies[i].dev_name,
3478 init_data->consumer_supplies[i].supply);
3479 if (ret < 0) {
3480 dev_err(dev, "Failed to set supply %s\n",
3481 init_data->consumer_supplies[i].supply);
3482 goto unset_supplies;
3487 list_add(&rdev->list, &regulator_list);
3489 rdev_init_debugfs(rdev);
3490 out:
3491 mutex_unlock(&regulator_list_mutex);
3492 return rdev;
3494 unset_supplies:
3495 unset_regulator_supplies(rdev);
3497 scrub:
3498 if (rdev->supply)
3499 _regulator_put(rdev->supply);
3500 if (rdev->ena_gpio)
3501 gpio_free(rdev->ena_gpio);
3502 kfree(rdev->constraints);
3503 wash:
3504 device_unregister(&rdev->dev);
3505 /* device core frees rdev */
3506 rdev = ERR_PTR(ret);
3507 goto out;
3509 clean:
3510 kfree(rdev);
3511 rdev = ERR_PTR(ret);
3512 goto out;
3514 EXPORT_SYMBOL_GPL(regulator_register);
3517 * regulator_unregister - unregister regulator
3518 * @rdev: regulator to unregister
3520 * Called by regulator drivers to unregister a regulator.
3522 void regulator_unregister(struct regulator_dev *rdev)
3524 if (rdev == NULL)
3525 return;
3527 if (rdev->supply)
3528 regulator_put(rdev->supply);
3529 mutex_lock(&regulator_list_mutex);
3530 debugfs_remove_recursive(rdev->debugfs);
3531 flush_work(&rdev->disable_work.work);
3532 WARN_ON(rdev->open_count);
3533 unset_regulator_supplies(rdev);
3534 list_del(&rdev->list);
3535 kfree(rdev->constraints);
3536 if (rdev->ena_gpio)
3537 gpio_free(rdev->ena_gpio);
3538 device_unregister(&rdev->dev);
3539 mutex_unlock(&regulator_list_mutex);
3541 EXPORT_SYMBOL_GPL(regulator_unregister);
3544 * regulator_suspend_prepare - prepare regulators for system wide suspend
3545 * @state: system suspend state
3547 * Configure each regulator with it's suspend operating parameters for state.
3548 * This will usually be called by machine suspend code prior to supending.
3550 int regulator_suspend_prepare(suspend_state_t state)
3552 struct regulator_dev *rdev;
3553 int ret = 0;
3555 /* ON is handled by regulator active state */
3556 if (state == PM_SUSPEND_ON)
3557 return -EINVAL;
3559 mutex_lock(&regulator_list_mutex);
3560 list_for_each_entry(rdev, &regulator_list, list) {
3562 mutex_lock(&rdev->mutex);
3563 ret = suspend_prepare(rdev, state);
3564 mutex_unlock(&rdev->mutex);
3566 if (ret < 0) {
3567 rdev_err(rdev, "failed to prepare\n");
3568 goto out;
3571 out:
3572 mutex_unlock(&regulator_list_mutex);
3573 return ret;
3575 EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3578 * regulator_suspend_finish - resume regulators from system wide suspend
3580 * Turn on regulators that might be turned off by regulator_suspend_prepare
3581 * and that should be turned on according to the regulators properties.
3583 int regulator_suspend_finish(void)
3585 struct regulator_dev *rdev;
3586 int ret = 0, error;
3588 mutex_lock(&regulator_list_mutex);
3589 list_for_each_entry(rdev, &regulator_list, list) {
3590 struct regulator_ops *ops = rdev->desc->ops;
3592 mutex_lock(&rdev->mutex);
3593 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3594 ops->enable) {
3595 error = ops->enable(rdev);
3596 if (error)
3597 ret = error;
3598 } else {
3599 if (!has_full_constraints)
3600 goto unlock;
3601 if (!ops->disable)
3602 goto unlock;
3603 if (!_regulator_is_enabled(rdev))
3604 goto unlock;
3606 error = ops->disable(rdev);
3607 if (error)
3608 ret = error;
3610 unlock:
3611 mutex_unlock(&rdev->mutex);
3613 mutex_unlock(&regulator_list_mutex);
3614 return ret;
3616 EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3619 * regulator_has_full_constraints - the system has fully specified constraints
3621 * Calling this function will cause the regulator API to disable all
3622 * regulators which have a zero use count and don't have an always_on
3623 * constraint in a late_initcall.
3625 * The intention is that this will become the default behaviour in a
3626 * future kernel release so users are encouraged to use this facility
3627 * now.
3629 void regulator_has_full_constraints(void)
3631 has_full_constraints = 1;
3633 EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3636 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3638 * Calling this function will cause the regulator API to provide a
3639 * dummy regulator to consumers if no physical regulator is found,
3640 * allowing most consumers to proceed as though a regulator were
3641 * configured. This allows systems such as those with software
3642 * controllable regulators for the CPU core only to be brought up more
3643 * readily.
3645 void regulator_use_dummy_regulator(void)
3647 board_wants_dummy_regulator = true;
3649 EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3652 * rdev_get_drvdata - get rdev regulator driver data
3653 * @rdev: regulator
3655 * Get rdev regulator driver private data. This call can be used in the
3656 * regulator driver context.
3658 void *rdev_get_drvdata(struct regulator_dev *rdev)
3660 return rdev->reg_data;
3662 EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3665 * regulator_get_drvdata - get regulator driver data
3666 * @regulator: regulator
3668 * Get regulator driver private data. This call can be used in the consumer
3669 * driver context when non API regulator specific functions need to be called.
3671 void *regulator_get_drvdata(struct regulator *regulator)
3673 return regulator->rdev->reg_data;
3675 EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3678 * regulator_set_drvdata - set regulator driver data
3679 * @regulator: regulator
3680 * @data: data
3682 void regulator_set_drvdata(struct regulator *regulator, void *data)
3684 regulator->rdev->reg_data = data;
3686 EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3689 * regulator_get_id - get regulator ID
3690 * @rdev: regulator
3692 int rdev_get_id(struct regulator_dev *rdev)
3694 return rdev->desc->id;
3696 EXPORT_SYMBOL_GPL(rdev_get_id);
3698 struct device *rdev_get_dev(struct regulator_dev *rdev)
3700 return &rdev->dev;
3702 EXPORT_SYMBOL_GPL(rdev_get_dev);
3704 void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3706 return reg_init_data->driver_data;
3708 EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3710 #ifdef CONFIG_DEBUG_FS
3711 static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3712 size_t count, loff_t *ppos)
3714 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3715 ssize_t len, ret = 0;
3716 struct regulator_map *map;
3718 if (!buf)
3719 return -ENOMEM;
3721 list_for_each_entry(map, &regulator_map_list, list) {
3722 len = snprintf(buf + ret, PAGE_SIZE - ret,
3723 "%s -> %s.%s\n",
3724 rdev_get_name(map->regulator), map->dev_name,
3725 map->supply);
3726 if (len >= 0)
3727 ret += len;
3728 if (ret > PAGE_SIZE) {
3729 ret = PAGE_SIZE;
3730 break;
3734 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3736 kfree(buf);
3738 return ret;
3740 #endif
3742 static const struct file_operations supply_map_fops = {
3743 #ifdef CONFIG_DEBUG_FS
3744 .read = supply_map_read_file,
3745 .llseek = default_llseek,
3746 #endif
3749 static int __init regulator_init(void)
3751 int ret;
3753 ret = class_register(&regulator_class);
3755 debugfs_root = debugfs_create_dir("regulator", NULL);
3756 if (!debugfs_root)
3757 pr_warn("regulator: Failed to create debugfs directory\n");
3759 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3760 &supply_map_fops);
3762 regulator_dummy_init();
3764 return ret;
3767 /* init early to allow our consumers to complete system booting */
3768 core_initcall(regulator_init);
3770 static int __init regulator_init_complete(void)
3772 struct regulator_dev *rdev;
3773 struct regulator_ops *ops;
3774 struct regulation_constraints *c;
3775 int enabled, ret;
3778 * Since DT doesn't provide an idiomatic mechanism for
3779 * enabling full constraints and since it's much more natural
3780 * with DT to provide them just assume that a DT enabled
3781 * system has full constraints.
3783 if (of_have_populated_dt())
3784 has_full_constraints = true;
3786 mutex_lock(&regulator_list_mutex);
3788 /* If we have a full configuration then disable any regulators
3789 * which are not in use or always_on. This will become the
3790 * default behaviour in the future.
3792 list_for_each_entry(rdev, &regulator_list, list) {
3793 ops = rdev->desc->ops;
3794 c = rdev->constraints;
3796 if (!ops->disable || (c && c->always_on))
3797 continue;
3799 mutex_lock(&rdev->mutex);
3801 if (rdev->use_count)
3802 goto unlock;
3804 /* If we can't read the status assume it's on. */
3805 if (ops->is_enabled)
3806 enabled = ops->is_enabled(rdev);
3807 else
3808 enabled = 1;
3810 if (!enabled)
3811 goto unlock;
3813 if (has_full_constraints) {
3814 /* We log since this may kill the system if it
3815 * goes wrong. */
3816 rdev_info(rdev, "disabling\n");
3817 ret = ops->disable(rdev);
3818 if (ret != 0) {
3819 rdev_err(rdev, "couldn't disable: %d\n", ret);
3821 } else {
3822 /* The intention is that in future we will
3823 * assume that full constraints are provided
3824 * so warn even if we aren't going to do
3825 * anything here.
3827 rdev_warn(rdev, "incomplete constraints, leaving on\n");
3830 unlock:
3831 mutex_unlock(&rdev->mutex);
3834 mutex_unlock(&regulator_list_mutex);
3836 return 0;
3838 late_initcall(regulator_init_complete);