Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * core.c -- Voltage/Current Regulator framework. |
| 3 | * |
| 4 | * Copyright 2007, 2008 Wolfson Microelectronics PLC. |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 5 | * Copyright 2008 SlimLogic Ltd. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 6 | * |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 7 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 8 | * |
| 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. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/init.h> |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 18 | #include <linux/debugfs.h> |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 19 | #include <linux/device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 21 | #include <linux/async.h> |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 22 | #include <linux/err.h> |
| 23 | #include <linux/mutex.h> |
| 24 | #include <linux/suspend.h> |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 25 | #include <linux/delay.h> |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 26 | #include <linux/of.h> |
Mark Brown | 65b19ce | 2012-04-15 11:16:05 +0100 | [diff] [blame] | 27 | #include <linux/regmap.h> |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 28 | #include <linux/regulator/of_regulator.h> |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 29 | #include <linux/regulator/consumer.h> |
| 30 | #include <linux/regulator/driver.h> |
| 31 | #include <linux/regulator/machine.h> |
Paul Gortmaker | 65602c3 | 2011-07-17 16:28:23 -0400 | [diff] [blame] | 32 | #include <linux/module.h> |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 33 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 34 | #define CREATE_TRACE_POINTS |
| 35 | #include <trace/events/regulator.h> |
| 36 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 37 | #include "dummy.h" |
| 38 | |
Mark Brown | 7d51a0d | 2011-06-09 16:06:37 +0100 | [diff] [blame] | 39 | #define rdev_crit(rdev, fmt, ...) \ |
| 40 | pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 41 | #define rdev_err(rdev, fmt, ...) \ |
| 42 | pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
| 43 | #define rdev_warn(rdev, fmt, ...) \ |
| 44 | pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
| 45 | #define rdev_info(rdev, fmt, ...) \ |
| 46 | pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
| 47 | #define rdev_dbg(rdev, fmt, ...) \ |
| 48 | pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) |
| 49 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 50 | static DEFINE_MUTEX(regulator_list_mutex); |
| 51 | static LIST_HEAD(regulator_list); |
| 52 | static LIST_HEAD(regulator_map_list); |
Mark Brown | 21cf891 | 2010-12-21 23:30:07 +0000 | [diff] [blame] | 53 | static bool has_full_constraints; |
Mark Brown | 688fe99 | 2010-10-05 19:18:32 -0700 | [diff] [blame] | 54 | static bool board_wants_dummy_regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 55 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 56 | static struct dentry *debugfs_root; |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 57 | |
Mark Brown | 8dc5390 | 2008-12-31 12:52:40 +0000 | [diff] [blame] | 58 | /* |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 59 | * struct regulator_map |
| 60 | * |
| 61 | * Used to provide symbolic supply names to devices. |
| 62 | */ |
| 63 | struct regulator_map { |
| 64 | struct list_head list; |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 65 | const char *dev_name; /* The dev_name() for the consumer */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 66 | const char *supply; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 67 | struct regulator_dev *regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 68 | }; |
| 69 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 70 | /* |
| 71 | * struct regulator |
| 72 | * |
| 73 | * One for each consumer device. |
| 74 | */ |
| 75 | struct regulator { |
| 76 | struct device *dev; |
| 77 | struct list_head list; |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 78 | unsigned int always_on:1; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 79 | int uA_load; |
| 80 | int min_uV; |
| 81 | int max_uV; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 82 | char *supply_name; |
| 83 | struct device_attribute dev_attr; |
| 84 | struct regulator_dev *rdev; |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 85 | struct dentry *debugfs; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | static int _regulator_is_enabled(struct regulator_dev *rdev); |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 89 | static int _regulator_disable(struct regulator_dev *rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 90 | static int _regulator_get_voltage(struct regulator_dev *rdev); |
| 91 | static int _regulator_get_current_limit(struct regulator_dev *rdev); |
| 92 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev); |
| 93 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 94 | unsigned long event, void *data); |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 95 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 96 | int min_uV, int max_uV); |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 97 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 98 | struct device *dev, |
| 99 | const char *supply_name); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 100 | |
Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 101 | static const char *rdev_get_name(struct regulator_dev *rdev) |
| 102 | { |
| 103 | if (rdev->constraints && rdev->constraints->name) |
| 104 | return rdev->constraints->name; |
| 105 | else if (rdev->desc->name) |
| 106 | return rdev->desc->name; |
| 107 | else |
| 108 | return ""; |
| 109 | } |
| 110 | |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 111 | /** |
| 112 | * of_get_regulator - get a regulator device node based on supply name |
| 113 | * @dev: Device pointer for the consumer (of regulator) device |
| 114 | * @supply: regulator supply name |
| 115 | * |
| 116 | * Extract the regulator device node corresponding to the supply name. |
| 117 | * retruns the device node corresponding to the regulator if found, else |
| 118 | * returns NULL. |
| 119 | */ |
| 120 | static struct device_node *of_get_regulator(struct device *dev, const char *supply) |
| 121 | { |
| 122 | struct device_node *regnode = NULL; |
| 123 | char prop_name[32]; /* 32 is max size of property name */ |
| 124 | |
| 125 | dev_dbg(dev, "Looking up %s-supply from device tree\n", supply); |
| 126 | |
| 127 | snprintf(prop_name, 32, "%s-supply", supply); |
| 128 | regnode = of_parse_phandle(dev->of_node, prop_name, 0); |
| 129 | |
| 130 | if (!regnode) { |
Rajendra Nayak | 16fbcc3 | 2012-03-16 15:50:21 +0530 | [diff] [blame] | 131 | dev_dbg(dev, "Looking up %s property in node %s failed", |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 132 | prop_name, dev->of_node->full_name); |
| 133 | return NULL; |
| 134 | } |
| 135 | return regnode; |
| 136 | } |
| 137 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 138 | static int _regulator_can_change_status(struct regulator_dev *rdev) |
| 139 | { |
| 140 | if (!rdev->constraints) |
| 141 | return 0; |
| 142 | |
| 143 | if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS) |
| 144 | return 1; |
| 145 | else |
| 146 | return 0; |
| 147 | } |
| 148 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 149 | /* Platform voltage constraint check */ |
| 150 | static int regulator_check_voltage(struct regulator_dev *rdev, |
| 151 | int *min_uV, int *max_uV) |
| 152 | { |
| 153 | BUG_ON(*min_uV > *max_uV); |
| 154 | |
| 155 | if (!rdev->constraints) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 156 | rdev_err(rdev, "no constraints\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 157 | return -ENODEV; |
| 158 | } |
| 159 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 160 | rdev_err(rdev, "operation not allowed\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 161 | return -EPERM; |
| 162 | } |
| 163 | |
| 164 | if (*max_uV > rdev->constraints->max_uV) |
| 165 | *max_uV = rdev->constraints->max_uV; |
| 166 | if (*min_uV < rdev->constraints->min_uV) |
| 167 | *min_uV = rdev->constraints->min_uV; |
| 168 | |
Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 169 | if (*min_uV > *max_uV) { |
| 170 | rdev_err(rdev, "unsupportable voltage range: %d-%duV\n", |
Mark Brown | 54abd33 | 2011-07-21 15:07:37 +0100 | [diff] [blame] | 171 | *min_uV, *max_uV); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 172 | return -EINVAL; |
Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 173 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 178 | /* Make sure we select a voltage that suits the needs of all |
| 179 | * regulator consumers |
| 180 | */ |
| 181 | static int regulator_check_consumers(struct regulator_dev *rdev, |
| 182 | int *min_uV, int *max_uV) |
| 183 | { |
| 184 | struct regulator *regulator; |
| 185 | |
| 186 | list_for_each_entry(regulator, &rdev->consumer_list, list) { |
Mark Brown | 4aa922c | 2011-05-14 13:42:34 -0700 | [diff] [blame] | 187 | /* |
| 188 | * Assume consumers that didn't say anything are OK |
| 189 | * with anything in the constraint range. |
| 190 | */ |
| 191 | if (!regulator->min_uV && !regulator->max_uV) |
| 192 | continue; |
| 193 | |
Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 194 | if (*max_uV > regulator->max_uV) |
| 195 | *max_uV = regulator->max_uV; |
| 196 | if (*min_uV < regulator->min_uV) |
| 197 | *min_uV = regulator->min_uV; |
| 198 | } |
| 199 | |
| 200 | if (*min_uV > *max_uV) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 206 | /* current constraint check */ |
| 207 | static int regulator_check_current_limit(struct regulator_dev *rdev, |
| 208 | int *min_uA, int *max_uA) |
| 209 | { |
| 210 | BUG_ON(*min_uA > *max_uA); |
| 211 | |
| 212 | if (!rdev->constraints) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 213 | rdev_err(rdev, "no constraints\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 214 | return -ENODEV; |
| 215 | } |
| 216 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 217 | rdev_err(rdev, "operation not allowed\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 218 | return -EPERM; |
| 219 | } |
| 220 | |
| 221 | if (*max_uA > rdev->constraints->max_uA) |
| 222 | *max_uA = rdev->constraints->max_uA; |
| 223 | if (*min_uA < rdev->constraints->min_uA) |
| 224 | *min_uA = rdev->constraints->min_uA; |
| 225 | |
Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 226 | if (*min_uA > *max_uA) { |
| 227 | rdev_err(rdev, "unsupportable current range: %d-%duA\n", |
Mark Brown | 54abd33 | 2011-07-21 15:07:37 +0100 | [diff] [blame] | 228 | *min_uA, *max_uA); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 229 | return -EINVAL; |
Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 230 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* operating mode constraint check */ |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 236 | static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 237 | { |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 238 | switch (*mode) { |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 239 | case REGULATOR_MODE_FAST: |
| 240 | case REGULATOR_MODE_NORMAL: |
| 241 | case REGULATOR_MODE_IDLE: |
| 242 | case REGULATOR_MODE_STANDBY: |
| 243 | break; |
| 244 | default: |
Mark Brown | 89f425e | 2011-07-12 11:20:37 +0900 | [diff] [blame] | 245 | rdev_err(rdev, "invalid mode %x specified\n", *mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 246 | return -EINVAL; |
| 247 | } |
| 248 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 249 | if (!rdev->constraints) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 250 | rdev_err(rdev, "no constraints\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 251 | return -ENODEV; |
| 252 | } |
| 253 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 254 | rdev_err(rdev, "operation not allowed\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 255 | return -EPERM; |
| 256 | } |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 257 | |
| 258 | /* The modes are bitmasks, the most power hungry modes having |
| 259 | * the lowest values. If the requested mode isn't supported |
| 260 | * try higher modes. */ |
| 261 | while (*mode) { |
| 262 | if (rdev->constraints->valid_modes_mask & *mode) |
| 263 | return 0; |
| 264 | *mode /= 2; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 265 | } |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 266 | |
| 267 | return -EINVAL; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* dynamic regulator mode switching constraint check */ |
| 271 | static int regulator_check_drms(struct regulator_dev *rdev) |
| 272 | { |
| 273 | if (!rdev->constraints) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 274 | rdev_err(rdev, "no constraints\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 275 | return -ENODEV; |
| 276 | } |
| 277 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 278 | rdev_err(rdev, "operation not allowed\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 279 | return -EPERM; |
| 280 | } |
| 281 | return 0; |
| 282 | } |
| 283 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 284 | static ssize_t regulator_uV_show(struct device *dev, |
| 285 | struct device_attribute *attr, char *buf) |
| 286 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 287 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 288 | ssize_t ret; |
| 289 | |
| 290 | mutex_lock(&rdev->mutex); |
| 291 | ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev)); |
| 292 | mutex_unlock(&rdev->mutex); |
| 293 | |
| 294 | return ret; |
| 295 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 296 | static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 297 | |
| 298 | static ssize_t regulator_uA_show(struct device *dev, |
| 299 | struct device_attribute *attr, char *buf) |
| 300 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 301 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 302 | |
| 303 | return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev)); |
| 304 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 305 | static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 306 | |
Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 307 | static ssize_t regulator_name_show(struct device *dev, |
| 308 | struct device_attribute *attr, char *buf) |
| 309 | { |
| 310 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 311 | |
Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 312 | return sprintf(buf, "%s\n", rdev_get_name(rdev)); |
Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 313 | } |
| 314 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 315 | static ssize_t regulator_print_opmode(char *buf, int mode) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 316 | { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 317 | switch (mode) { |
| 318 | case REGULATOR_MODE_FAST: |
| 319 | return sprintf(buf, "fast\n"); |
| 320 | case REGULATOR_MODE_NORMAL: |
| 321 | return sprintf(buf, "normal\n"); |
| 322 | case REGULATOR_MODE_IDLE: |
| 323 | return sprintf(buf, "idle\n"); |
| 324 | case REGULATOR_MODE_STANDBY: |
| 325 | return sprintf(buf, "standby\n"); |
| 326 | } |
| 327 | return sprintf(buf, "unknown\n"); |
| 328 | } |
| 329 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 330 | static ssize_t regulator_opmode_show(struct device *dev, |
| 331 | struct device_attribute *attr, char *buf) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 332 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 333 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 334 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 335 | return regulator_print_opmode(buf, _regulator_get_mode(rdev)); |
| 336 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 337 | static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 338 | |
| 339 | static ssize_t regulator_print_state(char *buf, int state) |
| 340 | { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 341 | if (state > 0) |
| 342 | return sprintf(buf, "enabled\n"); |
| 343 | else if (state == 0) |
| 344 | return sprintf(buf, "disabled\n"); |
| 345 | else |
| 346 | return sprintf(buf, "unknown\n"); |
| 347 | } |
| 348 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 349 | static ssize_t regulator_state_show(struct device *dev, |
| 350 | struct device_attribute *attr, char *buf) |
| 351 | { |
| 352 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 353 | ssize_t ret; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 354 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 355 | mutex_lock(&rdev->mutex); |
| 356 | ret = regulator_print_state(buf, _regulator_is_enabled(rdev)); |
| 357 | mutex_unlock(&rdev->mutex); |
| 358 | |
| 359 | return ret; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 360 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 361 | static DEVICE_ATTR(state, 0444, regulator_state_show, NULL); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 362 | |
David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 363 | static ssize_t regulator_status_show(struct device *dev, |
| 364 | struct device_attribute *attr, char *buf) |
| 365 | { |
| 366 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
| 367 | int status; |
| 368 | char *label; |
| 369 | |
| 370 | status = rdev->desc->ops->get_status(rdev); |
| 371 | if (status < 0) |
| 372 | return status; |
| 373 | |
| 374 | switch (status) { |
| 375 | case REGULATOR_STATUS_OFF: |
| 376 | label = "off"; |
| 377 | break; |
| 378 | case REGULATOR_STATUS_ON: |
| 379 | label = "on"; |
| 380 | break; |
| 381 | case REGULATOR_STATUS_ERROR: |
| 382 | label = "error"; |
| 383 | break; |
| 384 | case REGULATOR_STATUS_FAST: |
| 385 | label = "fast"; |
| 386 | break; |
| 387 | case REGULATOR_STATUS_NORMAL: |
| 388 | label = "normal"; |
| 389 | break; |
| 390 | case REGULATOR_STATUS_IDLE: |
| 391 | label = "idle"; |
| 392 | break; |
| 393 | case REGULATOR_STATUS_STANDBY: |
| 394 | label = "standby"; |
| 395 | break; |
Krystian Garbaciak | 1beaf76 | 2012-07-12 13:53:35 +0100 | [diff] [blame^] | 396 | case REGULATOR_STATUS_UNDEFINED: |
| 397 | label = "undefined"; |
| 398 | break; |
David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 399 | default: |
| 400 | return -ERANGE; |
| 401 | } |
| 402 | |
| 403 | return sprintf(buf, "%s\n", label); |
| 404 | } |
| 405 | static DEVICE_ATTR(status, 0444, regulator_status_show, NULL); |
| 406 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 407 | static ssize_t regulator_min_uA_show(struct device *dev, |
| 408 | struct device_attribute *attr, char *buf) |
| 409 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 410 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 411 | |
| 412 | if (!rdev->constraints) |
| 413 | return sprintf(buf, "constraint not defined\n"); |
| 414 | |
| 415 | return sprintf(buf, "%d\n", rdev->constraints->min_uA); |
| 416 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 417 | static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 418 | |
| 419 | static ssize_t regulator_max_uA_show(struct device *dev, |
| 420 | struct device_attribute *attr, char *buf) |
| 421 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 422 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 423 | |
| 424 | if (!rdev->constraints) |
| 425 | return sprintf(buf, "constraint not defined\n"); |
| 426 | |
| 427 | return sprintf(buf, "%d\n", rdev->constraints->max_uA); |
| 428 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 429 | static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 430 | |
| 431 | static ssize_t regulator_min_uV_show(struct device *dev, |
| 432 | struct device_attribute *attr, char *buf) |
| 433 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 434 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 435 | |
| 436 | if (!rdev->constraints) |
| 437 | return sprintf(buf, "constraint not defined\n"); |
| 438 | |
| 439 | return sprintf(buf, "%d\n", rdev->constraints->min_uV); |
| 440 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 441 | static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 442 | |
| 443 | static ssize_t regulator_max_uV_show(struct device *dev, |
| 444 | struct device_attribute *attr, char *buf) |
| 445 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 446 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 447 | |
| 448 | if (!rdev->constraints) |
| 449 | return sprintf(buf, "constraint not defined\n"); |
| 450 | |
| 451 | return sprintf(buf, "%d\n", rdev->constraints->max_uV); |
| 452 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 453 | static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 454 | |
| 455 | static ssize_t regulator_total_uA_show(struct device *dev, |
| 456 | struct device_attribute *attr, char *buf) |
| 457 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 458 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 459 | struct regulator *regulator; |
| 460 | int uA = 0; |
| 461 | |
| 462 | mutex_lock(&rdev->mutex); |
| 463 | list_for_each_entry(regulator, &rdev->consumer_list, list) |
Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 464 | uA += regulator->uA_load; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 465 | mutex_unlock(&rdev->mutex); |
| 466 | return sprintf(buf, "%d\n", uA); |
| 467 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 468 | static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 469 | |
| 470 | static ssize_t regulator_num_users_show(struct device *dev, |
| 471 | struct device_attribute *attr, char *buf) |
| 472 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 473 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 474 | return sprintf(buf, "%d\n", rdev->use_count); |
| 475 | } |
| 476 | |
| 477 | static ssize_t regulator_type_show(struct device *dev, |
| 478 | struct device_attribute *attr, char *buf) |
| 479 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 480 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 481 | |
| 482 | switch (rdev->desc->type) { |
| 483 | case REGULATOR_VOLTAGE: |
| 484 | return sprintf(buf, "voltage\n"); |
| 485 | case REGULATOR_CURRENT: |
| 486 | return sprintf(buf, "current\n"); |
| 487 | } |
| 488 | return sprintf(buf, "unknown\n"); |
| 489 | } |
| 490 | |
| 491 | static ssize_t regulator_suspend_mem_uV_show(struct device *dev, |
| 492 | struct device_attribute *attr, char *buf) |
| 493 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 494 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 495 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 496 | return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV); |
| 497 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 498 | static DEVICE_ATTR(suspend_mem_microvolts, 0444, |
| 499 | regulator_suspend_mem_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 500 | |
| 501 | static ssize_t regulator_suspend_disk_uV_show(struct device *dev, |
| 502 | struct device_attribute *attr, char *buf) |
| 503 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 504 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 505 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 506 | return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV); |
| 507 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 508 | static DEVICE_ATTR(suspend_disk_microvolts, 0444, |
| 509 | regulator_suspend_disk_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 510 | |
| 511 | static ssize_t regulator_suspend_standby_uV_show(struct device *dev, |
| 512 | struct device_attribute *attr, char *buf) |
| 513 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 514 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 515 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 516 | return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV); |
| 517 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 518 | static DEVICE_ATTR(suspend_standby_microvolts, 0444, |
| 519 | regulator_suspend_standby_uV_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 520 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 521 | static ssize_t regulator_suspend_mem_mode_show(struct device *dev, |
| 522 | struct device_attribute *attr, char *buf) |
| 523 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 524 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 525 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 526 | return regulator_print_opmode(buf, |
| 527 | rdev->constraints->state_mem.mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 528 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 529 | static DEVICE_ATTR(suspend_mem_mode, 0444, |
| 530 | regulator_suspend_mem_mode_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 531 | |
| 532 | static ssize_t regulator_suspend_disk_mode_show(struct device *dev, |
| 533 | struct device_attribute *attr, char *buf) |
| 534 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 535 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 536 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 537 | return regulator_print_opmode(buf, |
| 538 | rdev->constraints->state_disk.mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 539 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 540 | static DEVICE_ATTR(suspend_disk_mode, 0444, |
| 541 | regulator_suspend_disk_mode_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 542 | |
| 543 | static ssize_t regulator_suspend_standby_mode_show(struct device *dev, |
| 544 | struct device_attribute *attr, char *buf) |
| 545 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 546 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 547 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 548 | return regulator_print_opmode(buf, |
| 549 | rdev->constraints->state_standby.mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 550 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 551 | static DEVICE_ATTR(suspend_standby_mode, 0444, |
| 552 | regulator_suspend_standby_mode_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 553 | |
| 554 | static ssize_t regulator_suspend_mem_state_show(struct device *dev, |
| 555 | struct device_attribute *attr, char *buf) |
| 556 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 557 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 558 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 559 | return regulator_print_state(buf, |
| 560 | rdev->constraints->state_mem.enabled); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 561 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 562 | static DEVICE_ATTR(suspend_mem_state, 0444, |
| 563 | regulator_suspend_mem_state_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 564 | |
| 565 | static ssize_t regulator_suspend_disk_state_show(struct device *dev, |
| 566 | struct device_attribute *attr, char *buf) |
| 567 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 568 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 569 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 570 | return regulator_print_state(buf, |
| 571 | rdev->constraints->state_disk.enabled); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 572 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 573 | static DEVICE_ATTR(suspend_disk_state, 0444, |
| 574 | regulator_suspend_disk_state_show, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 575 | |
| 576 | static ssize_t regulator_suspend_standby_state_show(struct device *dev, |
| 577 | struct device_attribute *attr, char *buf) |
| 578 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 579 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 580 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 581 | return regulator_print_state(buf, |
| 582 | rdev->constraints->state_standby.enabled); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 583 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 584 | static DEVICE_ATTR(suspend_standby_state, 0444, |
| 585 | regulator_suspend_standby_state_show, NULL); |
Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 586 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 587 | |
| 588 | /* |
| 589 | * These are the only attributes are present for all regulators. |
| 590 | * Other attributes are a function of regulator functionality. |
| 591 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 592 | static struct device_attribute regulator_dev_attrs[] = { |
Mark Brown | bc558a6 | 2008-10-10 15:33:20 +0100 | [diff] [blame] | 593 | __ATTR(name, 0444, regulator_name_show, NULL), |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 594 | __ATTR(num_users, 0444, regulator_num_users_show, NULL), |
| 595 | __ATTR(type, 0444, regulator_type_show, NULL), |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 596 | __ATTR_NULL, |
| 597 | }; |
| 598 | |
| 599 | static void regulator_dev_release(struct device *dev) |
| 600 | { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 601 | struct regulator_dev *rdev = dev_get_drvdata(dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 602 | kfree(rdev); |
| 603 | } |
| 604 | |
| 605 | static struct class regulator_class = { |
| 606 | .name = "regulator", |
| 607 | .dev_release = regulator_dev_release, |
| 608 | .dev_attrs = regulator_dev_attrs, |
| 609 | }; |
| 610 | |
| 611 | /* Calculate the new optimum regulator operating mode based on the new total |
| 612 | * consumer load. All locks held by caller */ |
| 613 | static void drms_uA_update(struct regulator_dev *rdev) |
| 614 | { |
| 615 | struct regulator *sibling; |
| 616 | int current_uA = 0, output_uV, input_uV, err; |
| 617 | unsigned int mode; |
| 618 | |
| 619 | err = regulator_check_drms(rdev); |
| 620 | if (err < 0 || !rdev->desc->ops->get_optimum_mode || |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 621 | (!rdev->desc->ops->get_voltage && |
| 622 | !rdev->desc->ops->get_voltage_sel) || |
| 623 | !rdev->desc->ops->set_mode) |
Dan Carpenter | 036de8e | 2009-04-08 13:52:39 +0300 | [diff] [blame] | 624 | return; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 625 | |
| 626 | /* get output voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 627 | output_uV = _regulator_get_voltage(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 628 | if (output_uV <= 0) |
| 629 | return; |
| 630 | |
| 631 | /* get input voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 632 | input_uV = 0; |
| 633 | if (rdev->supply) |
Axel Lin | 3f24f5a | 2012-04-13 21:35:05 +0800 | [diff] [blame] | 634 | input_uV = regulator_get_voltage(rdev->supply); |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 635 | if (input_uV <= 0) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 636 | input_uV = rdev->constraints->input_uV; |
| 637 | if (input_uV <= 0) |
| 638 | return; |
| 639 | |
| 640 | /* calc total requested load */ |
| 641 | list_for_each_entry(sibling, &rdev->consumer_list, list) |
Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 642 | current_uA += sibling->uA_load; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 643 | |
| 644 | /* now get the optimum mode for our new total regulator load */ |
| 645 | mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV, |
| 646 | output_uV, current_uA); |
| 647 | |
| 648 | /* check the new mode is allowed */ |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 649 | err = regulator_mode_constrain(rdev, &mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 650 | if (err == 0) |
| 651 | rdev->desc->ops->set_mode(rdev, mode); |
| 652 | } |
| 653 | |
| 654 | static int suspend_set_state(struct regulator_dev *rdev, |
| 655 | struct regulator_state *rstate) |
| 656 | { |
| 657 | int ret = 0; |
Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 658 | |
| 659 | /* If we have no suspend mode configration don't set anything; |
Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 660 | * only warn if the driver implements set_suspend_voltage or |
| 661 | * set_suspend_mode callback. |
Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 662 | */ |
| 663 | if (!rstate->enabled && !rstate->disabled) { |
Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 664 | if (rdev->desc->ops->set_suspend_voltage || |
| 665 | rdev->desc->ops->set_suspend_mode) |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 666 | rdev_warn(rdev, "No configuration\n"); |
Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | if (rstate->enabled && rstate->disabled) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 671 | rdev_err(rdev, "invalid configuration\n"); |
Mark Brown | 638f85c | 2009-10-22 16:31:33 +0100 | [diff] [blame] | 672 | return -EINVAL; |
| 673 | } |
| 674 | |
Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 675 | if (rstate->enabled && rdev->desc->ops->set_suspend_enable) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 676 | ret = rdev->desc->ops->set_suspend_enable(rdev); |
Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 677 | else if (rstate->disabled && rdev->desc->ops->set_suspend_disable) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 678 | ret = rdev->desc->ops->set_suspend_disable(rdev); |
Axel Lin | 8ac0e95 | 2012-04-14 09:14:34 +0800 | [diff] [blame] | 679 | else /* OK if set_suspend_enable or set_suspend_disable is NULL */ |
| 680 | ret = 0; |
| 681 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 682 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 683 | rdev_err(rdev, "failed to enabled/disable\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) { |
| 688 | ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV); |
| 689 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 690 | rdev_err(rdev, "failed to set voltage\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 691 | return ret; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) { |
| 696 | ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode); |
| 697 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 698 | rdev_err(rdev, "failed to set mode\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 699 | return ret; |
| 700 | } |
| 701 | } |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | /* locks held by caller */ |
| 706 | static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state) |
| 707 | { |
| 708 | if (!rdev->constraints) |
| 709 | return -EINVAL; |
| 710 | |
| 711 | switch (state) { |
| 712 | case PM_SUSPEND_STANDBY: |
| 713 | return suspend_set_state(rdev, |
| 714 | &rdev->constraints->state_standby); |
| 715 | case PM_SUSPEND_MEM: |
| 716 | return suspend_set_state(rdev, |
| 717 | &rdev->constraints->state_mem); |
| 718 | case PM_SUSPEND_MAX: |
| 719 | return suspend_set_state(rdev, |
| 720 | &rdev->constraints->state_disk); |
| 721 | default: |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | static void print_constraints(struct regulator_dev *rdev) |
| 727 | { |
| 728 | struct regulation_constraints *constraints = rdev->constraints; |
Mark Brown | 973e9a2 | 2010-02-11 19:20:48 +0000 | [diff] [blame] | 729 | char buf[80] = ""; |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 730 | int count = 0; |
| 731 | int ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 732 | |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 733 | if (constraints->min_uV && constraints->max_uV) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 734 | if (constraints->min_uV == constraints->max_uV) |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 735 | count += sprintf(buf + count, "%d mV ", |
| 736 | constraints->min_uV / 1000); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 737 | else |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 738 | count += sprintf(buf + count, "%d <--> %d mV ", |
| 739 | constraints->min_uV / 1000, |
| 740 | constraints->max_uV / 1000); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 741 | } |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 742 | |
| 743 | if (!constraints->min_uV || |
| 744 | constraints->min_uV != constraints->max_uV) { |
| 745 | ret = _regulator_get_voltage(rdev); |
| 746 | if (ret > 0) |
| 747 | count += sprintf(buf + count, "at %d mV ", ret / 1000); |
| 748 | } |
| 749 | |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 750 | if (constraints->uV_offset) |
| 751 | count += sprintf(buf, "%dmV offset ", |
| 752 | constraints->uV_offset / 1000); |
| 753 | |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 754 | if (constraints->min_uA && constraints->max_uA) { |
| 755 | if (constraints->min_uA == constraints->max_uA) |
| 756 | count += sprintf(buf + count, "%d mA ", |
| 757 | constraints->min_uA / 1000); |
| 758 | else |
| 759 | count += sprintf(buf + count, "%d <--> %d mA ", |
| 760 | constraints->min_uA / 1000, |
| 761 | constraints->max_uA / 1000); |
| 762 | } |
| 763 | |
| 764 | if (!constraints->min_uA || |
| 765 | constraints->min_uA != constraints->max_uA) { |
| 766 | ret = _regulator_get_current_limit(rdev); |
| 767 | if (ret > 0) |
Cyril Chemparathy | e4a6376 | 2010-09-22 12:30:15 -0400 | [diff] [blame] | 768 | count += sprintf(buf + count, "at %d mA ", ret / 1000); |
Mark Brown | 8f031b4 | 2009-10-22 16:31:31 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 771 | if (constraints->valid_modes_mask & REGULATOR_MODE_FAST) |
| 772 | count += sprintf(buf + count, "fast "); |
| 773 | if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL) |
| 774 | count += sprintf(buf + count, "normal "); |
| 775 | if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE) |
| 776 | count += sprintf(buf + count, "idle "); |
| 777 | if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY) |
| 778 | count += sprintf(buf + count, "standby"); |
| 779 | |
Mark Brown | 13ce29f | 2010-12-17 16:04:12 +0000 | [diff] [blame] | 780 | rdev_info(rdev, "%s\n", buf); |
Mark Brown | 4a68292 | 2012-02-09 13:26:13 +0000 | [diff] [blame] | 781 | |
| 782 | if ((constraints->min_uV != constraints->max_uV) && |
| 783 | !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) |
| 784 | rdev_warn(rdev, |
| 785 | "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 788 | static int machine_constraints_voltage(struct regulator_dev *rdev, |
Mark Brown | 1083c39 | 2009-10-22 16:31:32 +0100 | [diff] [blame] | 789 | struct regulation_constraints *constraints) |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 790 | { |
| 791 | struct regulator_ops *ops = rdev->desc->ops; |
Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 792 | int ret; |
| 793 | |
| 794 | /* do we need to apply the constraint voltage */ |
| 795 | if (rdev->constraints->apply_uV && |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 796 | rdev->constraints->min_uV == rdev->constraints->max_uV) { |
| 797 | ret = _regulator_do_set_voltage(rdev, |
| 798 | rdev->constraints->min_uV, |
| 799 | rdev->constraints->max_uV); |
| 800 | if (ret < 0) { |
| 801 | rdev_err(rdev, "failed to apply %duV constraint\n", |
| 802 | rdev->constraints->min_uV); |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 803 | return ret; |
| 804 | } |
Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 805 | } |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 806 | |
| 807 | /* constrain machine-level voltage specs to fit |
| 808 | * the actual range supported by this regulator. |
| 809 | */ |
| 810 | if (ops->list_voltage && rdev->desc->n_voltages) { |
| 811 | int count = rdev->desc->n_voltages; |
| 812 | int i; |
| 813 | int min_uV = INT_MAX; |
| 814 | int max_uV = INT_MIN; |
| 815 | int cmin = constraints->min_uV; |
| 816 | int cmax = constraints->max_uV; |
| 817 | |
| 818 | /* it's safe to autoconfigure fixed-voltage supplies |
| 819 | and the constraints are used by list_voltage. */ |
| 820 | if (count == 1 && !cmin) { |
| 821 | cmin = 1; |
| 822 | cmax = INT_MAX; |
| 823 | constraints->min_uV = cmin; |
| 824 | constraints->max_uV = cmax; |
| 825 | } |
| 826 | |
| 827 | /* voltage constraints are optional */ |
| 828 | if ((cmin == 0) && (cmax == 0)) |
| 829 | return 0; |
| 830 | |
| 831 | /* else require explicit machine-level constraints */ |
| 832 | if (cmin <= 0 || cmax <= 0 || cmax < cmin) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 833 | rdev_err(rdev, "invalid voltage constraints\n"); |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 834 | return -EINVAL; |
| 835 | } |
| 836 | |
| 837 | /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */ |
| 838 | for (i = 0; i < count; i++) { |
| 839 | int value; |
| 840 | |
| 841 | value = ops->list_voltage(rdev, i); |
| 842 | if (value <= 0) |
| 843 | continue; |
| 844 | |
| 845 | /* maybe adjust [min_uV..max_uV] */ |
| 846 | if (value >= cmin && value < min_uV) |
| 847 | min_uV = value; |
| 848 | if (value <= cmax && value > max_uV) |
| 849 | max_uV = value; |
| 850 | } |
| 851 | |
| 852 | /* final: [min_uV..max_uV] valid iff constraints valid */ |
| 853 | if (max_uV < min_uV) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 854 | rdev_err(rdev, "unsupportable voltage constraints\n"); |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 855 | return -EINVAL; |
| 856 | } |
| 857 | |
| 858 | /* use regulator's subset of machine constraints */ |
| 859 | if (constraints->min_uV < min_uV) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 860 | rdev_dbg(rdev, "override min_uV, %d -> %d\n", |
| 861 | constraints->min_uV, min_uV); |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 862 | constraints->min_uV = min_uV; |
| 863 | } |
| 864 | if (constraints->max_uV > max_uV) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 865 | rdev_dbg(rdev, "override max_uV, %d -> %d\n", |
| 866 | constraints->max_uV, max_uV); |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 867 | constraints->max_uV = max_uV; |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 874 | /** |
| 875 | * set_machine_constraints - sets regulator constraints |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 876 | * @rdev: regulator source |
Mark Brown | c8e7e46 | 2008-12-31 12:52:42 +0000 | [diff] [blame] | 877 | * @constraints: constraints to apply |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 878 | * |
| 879 | * Allows platform initialisation code to define and constrain |
| 880 | * regulator circuits e.g. valid voltage/current ranges, etc. NOTE: |
| 881 | * Constraints *must* be set by platform code in order for some |
| 882 | * regulator operations to proceed i.e. set_voltage, set_current_limit, |
| 883 | * set_mode. |
| 884 | */ |
| 885 | static int set_machine_constraints(struct regulator_dev *rdev, |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 886 | const struct regulation_constraints *constraints) |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 887 | { |
| 888 | int ret = 0; |
Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 889 | struct regulator_ops *ops = rdev->desc->ops; |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 890 | |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 891 | if (constraints) |
| 892 | rdev->constraints = kmemdup(constraints, sizeof(*constraints), |
| 893 | GFP_KERNEL); |
| 894 | else |
| 895 | rdev->constraints = kzalloc(sizeof(*constraints), |
| 896 | GFP_KERNEL); |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 897 | if (!rdev->constraints) |
| 898 | return -ENOMEM; |
Mark Brown | af5866c | 2009-10-22 16:31:30 +0100 | [diff] [blame] | 899 | |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 900 | ret = machine_constraints_voltage(rdev, rdev->constraints); |
Mark Brown | e79055d | 2009-10-19 15:53:50 +0100 | [diff] [blame] | 901 | if (ret != 0) |
| 902 | goto out; |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 903 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 904 | /* do we need to setup our suspend state */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 905 | if (rdev->constraints->initial_state) { |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 906 | ret = suspend_prepare(rdev, rdev->constraints->initial_state); |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 907 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 908 | rdev_err(rdev, "failed to set suspend state\n"); |
Mark Brown | e06f5b4 | 2008-09-09 16:21:19 +0100 | [diff] [blame] | 909 | goto out; |
| 910 | } |
| 911 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 912 | |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 913 | if (rdev->constraints->initial_mode) { |
Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 914 | if (!ops->set_mode) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 915 | rdev_err(rdev, "no set_mode operation\n"); |
Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 916 | ret = -EINVAL; |
| 917 | goto out; |
| 918 | } |
| 919 | |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 920 | ret = ops->set_mode(rdev, rdev->constraints->initial_mode); |
Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 921 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 922 | rdev_err(rdev, "failed to set initial mode: %d\n", ret); |
Mark Brown | a308466 | 2009-02-26 19:24:19 +0000 | [diff] [blame] | 923 | goto out; |
| 924 | } |
| 925 | } |
| 926 | |
Mark Brown | cacf90f | 2009-03-02 16:32:46 +0000 | [diff] [blame] | 927 | /* If the constraints say the regulator should be on at this point |
| 928 | * and we have control then make sure it is enabled. |
| 929 | */ |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 930 | if ((rdev->constraints->always_on || rdev->constraints->boot_on) && |
| 931 | ops->enable) { |
Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 932 | ret = ops->enable(rdev); |
| 933 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 934 | rdev_err(rdev, "failed to enable\n"); |
Mark Brown | e5fda26 | 2008-09-09 16:21:20 +0100 | [diff] [blame] | 935 | goto out; |
| 936 | } |
| 937 | } |
| 938 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 939 | print_constraints(rdev); |
Axel Lin | 1a6958e7 | 2011-07-15 10:50:43 +0800 | [diff] [blame] | 940 | return 0; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 941 | out: |
Axel Lin | 1a6958e7 | 2011-07-15 10:50:43 +0800 | [diff] [blame] | 942 | kfree(rdev->constraints); |
| 943 | rdev->constraints = NULL; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 944 | return ret; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * set_supply - set regulator supply regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 949 | * @rdev: regulator name |
| 950 | * @supply_rdev: supply regulator name |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 951 | * |
| 952 | * Called by platform initialisation code to set the supply regulator for this |
| 953 | * regulator. This ensures that a regulators supply will also be enabled by the |
| 954 | * core if it's child is enabled. |
| 955 | */ |
| 956 | static int set_supply(struct regulator_dev *rdev, |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 957 | struct regulator_dev *supply_rdev) |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 958 | { |
| 959 | int err; |
| 960 | |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 961 | rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev)); |
| 962 | |
| 963 | rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY"); |
Axel Lin | 32c78de | 2011-12-29 17:03:20 +0800 | [diff] [blame] | 964 | if (rdev->supply == NULL) { |
| 965 | err = -ENOMEM; |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 966 | return err; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 967 | } |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 968 | |
| 969 | return 0; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /** |
Randy Dunlap | 06c63f9 | 2010-11-18 15:02:26 -0800 | [diff] [blame] | 973 | * set_consumer_device_supply - Bind a regulator to a symbolic supply |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 974 | * @rdev: regulator source |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 975 | * @consumer_dev_name: dev_name() string for device supply applies to |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 976 | * @supply: symbolic name for supply |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 977 | * |
| 978 | * Allows platform initialisation code to map physical regulator |
| 979 | * sources to symbolic names for supplies for use by devices. Devices |
| 980 | * should use these symbolic names to request regulators, avoiding the |
| 981 | * need to provide board-specific regulator names as platform data. |
| 982 | */ |
| 983 | static int set_consumer_device_supply(struct regulator_dev *rdev, |
Mark Brown | 737f360 | 2012-02-02 00:10:51 +0000 | [diff] [blame] | 984 | const char *consumer_dev_name, |
| 985 | const char *supply) |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 986 | { |
| 987 | struct regulator_map *node; |
Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 988 | int has_dev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 989 | |
| 990 | if (supply == NULL) |
| 991 | return -EINVAL; |
| 992 | |
Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 993 | if (consumer_dev_name != NULL) |
| 994 | has_dev = 1; |
| 995 | else |
| 996 | has_dev = 0; |
| 997 | |
David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 998 | list_for_each_entry(node, ®ulator_map_list, list) { |
Jani Nikula | 23b5cc2 | 2010-04-29 10:55:09 +0300 | [diff] [blame] | 999 | if (node->dev_name && consumer_dev_name) { |
| 1000 | if (strcmp(node->dev_name, consumer_dev_name) != 0) |
| 1001 | continue; |
| 1002 | } else if (node->dev_name || consumer_dev_name) { |
David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1003 | continue; |
Jani Nikula | 23b5cc2 | 2010-04-29 10:55:09 +0300 | [diff] [blame] | 1004 | } |
| 1005 | |
David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1006 | if (strcmp(node->supply, supply) != 0) |
| 1007 | continue; |
| 1008 | |
Mark Brown | 737f360 | 2012-02-02 00:10:51 +0000 | [diff] [blame] | 1009 | pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n", |
| 1010 | consumer_dev_name, |
| 1011 | dev_name(&node->regulator->dev), |
| 1012 | node->regulator->desc->name, |
| 1013 | supply, |
| 1014 | dev_name(&rdev->dev), rdev_get_name(rdev)); |
David Brownell | 6001e13 | 2008-12-31 12:54:19 +0000 | [diff] [blame] | 1015 | return -EBUSY; |
| 1016 | } |
| 1017 | |
Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1018 | node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1019 | if (node == NULL) |
| 1020 | return -ENOMEM; |
| 1021 | |
| 1022 | node->regulator = rdev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1023 | node->supply = supply; |
| 1024 | |
Mark Brown | 9ed2099 | 2009-07-21 16:00:26 +0100 | [diff] [blame] | 1025 | if (has_dev) { |
| 1026 | node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL); |
| 1027 | if (node->dev_name == NULL) { |
| 1028 | kfree(node); |
| 1029 | return -ENOMEM; |
| 1030 | } |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1031 | } |
| 1032 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1033 | list_add(&node->list, ®ulator_map_list); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1037 | static void unset_regulator_supplies(struct regulator_dev *rdev) |
| 1038 | { |
| 1039 | struct regulator_map *node, *n; |
| 1040 | |
| 1041 | list_for_each_entry_safe(node, n, ®ulator_map_list, list) { |
| 1042 | if (rdev == node->regulator) { |
| 1043 | list_del(&node->list); |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1044 | kfree(node->dev_name); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1045 | kfree(node); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | |
Mark Brown | f5726ae | 2011-06-09 16:22:20 +0100 | [diff] [blame] | 1050 | #define REG_STR_SIZE 64 |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1051 | |
| 1052 | static struct regulator *create_regulator(struct regulator_dev *rdev, |
| 1053 | struct device *dev, |
| 1054 | const char *supply_name) |
| 1055 | { |
| 1056 | struct regulator *regulator; |
| 1057 | char buf[REG_STR_SIZE]; |
| 1058 | int err, size; |
| 1059 | |
| 1060 | regulator = kzalloc(sizeof(*regulator), GFP_KERNEL); |
| 1061 | if (regulator == NULL) |
| 1062 | return NULL; |
| 1063 | |
| 1064 | mutex_lock(&rdev->mutex); |
| 1065 | regulator->rdev = rdev; |
| 1066 | list_add(®ulator->list, &rdev->consumer_list); |
| 1067 | |
| 1068 | if (dev) { |
Shawn Guo | e2c98ea | 2012-07-05 14:19:42 +0800 | [diff] [blame] | 1069 | regulator->dev = dev; |
| 1070 | |
Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1071 | /* Add a link to the device sysfs entry */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1072 | size = scnprintf(buf, REG_STR_SIZE, "%s-%s", |
| 1073 | dev->kobj.name, supply_name); |
| 1074 | if (size >= REG_STR_SIZE) |
Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1075 | goto overflow_err; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1076 | |
| 1077 | regulator->supply_name = kstrdup(buf, GFP_KERNEL); |
| 1078 | if (regulator->supply_name == NULL) |
Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1079 | goto overflow_err; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1080 | |
| 1081 | err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj, |
| 1082 | buf); |
| 1083 | if (err) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1084 | rdev_warn(rdev, "could not add device link %s err %d\n", |
| 1085 | dev->kobj.name, err); |
Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1086 | /* non-fatal */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1087 | } |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1088 | } else { |
| 1089 | regulator->supply_name = kstrdup(supply_name, GFP_KERNEL); |
| 1090 | if (regulator->supply_name == NULL) |
Mark Brown | 222cc7b | 2012-06-22 11:39:16 +0100 | [diff] [blame] | 1091 | goto overflow_err; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1092 | } |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1093 | |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1094 | regulator->debugfs = debugfs_create_dir(regulator->supply_name, |
| 1095 | rdev->debugfs); |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 1096 | if (!regulator->debugfs) { |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1097 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1098 | } else { |
| 1099 | debugfs_create_u32("uA_load", 0444, regulator->debugfs, |
| 1100 | ®ulator->uA_load); |
| 1101 | debugfs_create_u32("min_uV", 0444, regulator->debugfs, |
| 1102 | ®ulator->min_uV); |
| 1103 | debugfs_create_u32("max_uV", 0444, regulator->debugfs, |
| 1104 | ®ulator->max_uV); |
| 1105 | } |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1106 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1107 | /* |
| 1108 | * Check now if the regulator is an always on regulator - if |
| 1109 | * it is then we don't need to do nearly so much work for |
| 1110 | * enable/disable calls. |
| 1111 | */ |
| 1112 | if (!_regulator_can_change_status(rdev) && |
| 1113 | _regulator_is_enabled(rdev)) |
| 1114 | regulator->always_on = true; |
| 1115 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1116 | mutex_unlock(&rdev->mutex); |
| 1117 | return regulator; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1118 | overflow_err: |
| 1119 | list_del(®ulator->list); |
| 1120 | kfree(regulator); |
| 1121 | mutex_unlock(&rdev->mutex); |
| 1122 | return NULL; |
| 1123 | } |
| 1124 | |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1125 | static int _regulator_get_enable_time(struct regulator_dev *rdev) |
| 1126 | { |
| 1127 | if (!rdev->desc->ops->enable_time) |
| 1128 | return 0; |
| 1129 | return rdev->desc->ops->enable_time(rdev); |
| 1130 | } |
| 1131 | |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1132 | static struct regulator_dev *regulator_dev_lookup(struct device *dev, |
Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1133 | const char *supply, |
| 1134 | int *ret) |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1135 | { |
| 1136 | struct regulator_dev *r; |
| 1137 | struct device_node *node; |
Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1138 | struct regulator_map *map; |
| 1139 | const char *devname = NULL; |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1140 | |
| 1141 | /* first do a dt based lookup */ |
| 1142 | if (dev && dev->of_node) { |
| 1143 | node = of_get_regulator(dev, supply); |
Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1144 | if (node) { |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1145 | list_for_each_entry(r, ®ulator_list, list) |
| 1146 | if (r->dev.parent && |
| 1147 | node == r->dev.of_node) |
| 1148 | return r; |
Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1149 | } else { |
| 1150 | /* |
| 1151 | * If we couldn't even get the node then it's |
| 1152 | * not just that the device didn't register |
| 1153 | * yet, there's no node and we'll never |
| 1154 | * succeed. |
| 1155 | */ |
| 1156 | *ret = -ENODEV; |
| 1157 | } |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | /* if not found, try doing it non-dt way */ |
Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1161 | if (dev) |
| 1162 | devname = dev_name(dev); |
| 1163 | |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1164 | list_for_each_entry(r, ®ulator_list, list) |
| 1165 | if (strcmp(rdev_get_name(r), supply) == 0) |
| 1166 | return r; |
| 1167 | |
Mark Brown | 576ca436 | 2012-03-30 12:24:37 +0100 | [diff] [blame] | 1168 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 1169 | /* If the mapping has a device set up it must match */ |
| 1170 | if (map->dev_name && |
| 1171 | (!devname || strcmp(map->dev_name, devname))) |
| 1172 | continue; |
| 1173 | |
| 1174 | if (strcmp(map->supply, supply) == 0) |
| 1175 | return map->regulator; |
| 1176 | } |
| 1177 | |
| 1178 | |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1179 | return NULL; |
| 1180 | } |
| 1181 | |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1182 | /* Internal regulator request function */ |
| 1183 | static struct regulator *_regulator_get(struct device *dev, const char *id, |
| 1184 | int exclusive) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1185 | { |
| 1186 | struct regulator_dev *rdev; |
Mark Brown | 04bf301 | 2012-03-11 13:07:56 +0000 | [diff] [blame] | 1187 | struct regulator *regulator = ERR_PTR(-EPROBE_DEFER); |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1188 | const char *devname = NULL; |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1189 | int ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1190 | |
| 1191 | if (id == NULL) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1192 | pr_err("get() with no identifier\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1193 | return regulator; |
| 1194 | } |
| 1195 | |
Mark Brown | 40f9244 | 2009-06-17 17:56:39 +0100 | [diff] [blame] | 1196 | if (dev) |
| 1197 | devname = dev_name(dev); |
| 1198 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1199 | mutex_lock(®ulator_list_mutex); |
| 1200 | |
Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 1201 | rdev = regulator_dev_lookup(dev, id, &ret); |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 1202 | if (rdev) |
| 1203 | goto found; |
| 1204 | |
Mark Brown | 688fe99 | 2010-10-05 19:18:32 -0700 | [diff] [blame] | 1205 | if (board_wants_dummy_regulator) { |
| 1206 | rdev = dummy_regulator_rdev; |
| 1207 | goto found; |
| 1208 | } |
| 1209 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 1210 | #ifdef CONFIG_REGULATOR_DUMMY |
| 1211 | if (!devname) |
| 1212 | devname = "deviceless"; |
| 1213 | |
| 1214 | /* If the board didn't flag that it was fully constrained then |
| 1215 | * substitute in a dummy regulator so consumers can continue. |
| 1216 | */ |
| 1217 | if (!has_full_constraints) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1218 | pr_warn("%s supply %s not found, using dummy regulator\n", |
| 1219 | devname, id); |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 1220 | rdev = dummy_regulator_rdev; |
| 1221 | goto found; |
| 1222 | } |
| 1223 | #endif |
| 1224 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1225 | mutex_unlock(®ulator_list_mutex); |
| 1226 | return regulator; |
| 1227 | |
| 1228 | found: |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1229 | if (rdev->exclusive) { |
| 1230 | regulator = ERR_PTR(-EPERM); |
| 1231 | goto out; |
| 1232 | } |
| 1233 | |
| 1234 | if (exclusive && rdev->open_count) { |
| 1235 | regulator = ERR_PTR(-EBUSY); |
| 1236 | goto out; |
| 1237 | } |
| 1238 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1239 | if (!try_module_get(rdev->owner)) |
| 1240 | goto out; |
| 1241 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1242 | regulator = create_regulator(rdev, dev, id); |
| 1243 | if (regulator == NULL) { |
| 1244 | regulator = ERR_PTR(-ENOMEM); |
| 1245 | module_put(rdev->owner); |
Axel Lin | bcda432 | 2011-12-29 17:02:08 +0800 | [diff] [blame] | 1246 | goto out; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1247 | } |
| 1248 | |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1249 | rdev->open_count++; |
| 1250 | if (exclusive) { |
| 1251 | rdev->exclusive = 1; |
| 1252 | |
| 1253 | ret = _regulator_is_enabled(rdev); |
| 1254 | if (ret > 0) |
| 1255 | rdev->use_count = 1; |
| 1256 | else |
| 1257 | rdev->use_count = 0; |
| 1258 | } |
| 1259 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 1260 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1261 | mutex_unlock(®ulator_list_mutex); |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1262 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1263 | return regulator; |
| 1264 | } |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1265 | |
| 1266 | /** |
| 1267 | * regulator_get - lookup and obtain a reference to a regulator. |
| 1268 | * @dev: device for regulator "consumer" |
| 1269 | * @id: Supply name or regulator ID. |
| 1270 | * |
| 1271 | * Returns a struct regulator corresponding to the regulator producer, |
| 1272 | * or IS_ERR() condition containing errno. |
| 1273 | * |
| 1274 | * Use of supply names configured via regulator_set_device_supply() is |
| 1275 | * strongly encouraged. It is recommended that the supply name used |
| 1276 | * should match the name used for the supply and/or the relevant |
| 1277 | * device pins in the datasheet. |
| 1278 | */ |
| 1279 | struct regulator *regulator_get(struct device *dev, const char *id) |
| 1280 | { |
| 1281 | return _regulator_get(dev, id, 0); |
| 1282 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1283 | EXPORT_SYMBOL_GPL(regulator_get); |
| 1284 | |
Stephen Boyd | 070b907 | 2012-01-16 19:39:58 -0800 | [diff] [blame] | 1285 | static void devm_regulator_release(struct device *dev, void *res) |
| 1286 | { |
| 1287 | regulator_put(*(struct regulator **)res); |
| 1288 | } |
| 1289 | |
| 1290 | /** |
| 1291 | * devm_regulator_get - Resource managed regulator_get() |
| 1292 | * @dev: device for regulator "consumer" |
| 1293 | * @id: Supply name or regulator ID. |
| 1294 | * |
| 1295 | * Managed regulator_get(). Regulators returned from this function are |
| 1296 | * automatically regulator_put() on driver detach. See regulator_get() for more |
| 1297 | * information. |
| 1298 | */ |
| 1299 | struct regulator *devm_regulator_get(struct device *dev, const char *id) |
| 1300 | { |
| 1301 | struct regulator **ptr, *regulator; |
| 1302 | |
| 1303 | ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL); |
| 1304 | if (!ptr) |
| 1305 | return ERR_PTR(-ENOMEM); |
| 1306 | |
| 1307 | regulator = regulator_get(dev, id); |
| 1308 | if (!IS_ERR(regulator)) { |
| 1309 | *ptr = regulator; |
| 1310 | devres_add(dev, ptr); |
| 1311 | } else { |
| 1312 | devres_free(ptr); |
| 1313 | } |
| 1314 | |
| 1315 | return regulator; |
| 1316 | } |
| 1317 | EXPORT_SYMBOL_GPL(devm_regulator_get); |
| 1318 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1319 | /** |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1320 | * regulator_get_exclusive - obtain exclusive access to a regulator. |
| 1321 | * @dev: device for regulator "consumer" |
| 1322 | * @id: Supply name or regulator ID. |
| 1323 | * |
| 1324 | * Returns a struct regulator corresponding to the regulator producer, |
| 1325 | * or IS_ERR() condition containing errno. Other consumers will be |
| 1326 | * unable to obtain this reference is held and the use count for the |
| 1327 | * regulator will be initialised to reflect the current state of the |
| 1328 | * regulator. |
| 1329 | * |
| 1330 | * This is intended for use by consumers which cannot tolerate shared |
| 1331 | * use of the regulator such as those which need to force the |
| 1332 | * regulator off for correct operation of the hardware they are |
| 1333 | * controlling. |
| 1334 | * |
| 1335 | * Use of supply names configured via regulator_set_device_supply() is |
| 1336 | * strongly encouraged. It is recommended that the supply name used |
| 1337 | * should match the name used for the supply and/or the relevant |
| 1338 | * device pins in the datasheet. |
| 1339 | */ |
| 1340 | struct regulator *regulator_get_exclusive(struct device *dev, const char *id) |
| 1341 | { |
| 1342 | return _regulator_get(dev, id, 1); |
| 1343 | } |
| 1344 | EXPORT_SYMBOL_GPL(regulator_get_exclusive); |
| 1345 | |
| 1346 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1347 | * regulator_put - "free" the regulator source |
| 1348 | * @regulator: regulator source |
| 1349 | * |
| 1350 | * Note: drivers must ensure that all regulator_enable calls made on this |
| 1351 | * regulator source are balanced by regulator_disable calls prior to calling |
| 1352 | * this function. |
| 1353 | */ |
| 1354 | void regulator_put(struct regulator *regulator) |
| 1355 | { |
| 1356 | struct regulator_dev *rdev; |
| 1357 | |
| 1358 | if (regulator == NULL || IS_ERR(regulator)) |
| 1359 | return; |
| 1360 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1361 | mutex_lock(®ulator_list_mutex); |
| 1362 | rdev = regulator->rdev; |
| 1363 | |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1364 | debugfs_remove_recursive(regulator->debugfs); |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1365 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1366 | /* remove any sysfs entries */ |
Shawn Guo | e2c98ea | 2012-07-05 14:19:42 +0800 | [diff] [blame] | 1367 | if (regulator->dev) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1368 | sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name); |
Mark Brown | 5de7051 | 2011-06-19 13:33:16 +0100 | [diff] [blame] | 1369 | kfree(regulator->supply_name); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1370 | list_del(®ulator->list); |
| 1371 | kfree(regulator); |
| 1372 | |
Mark Brown | 5ffbd13 | 2009-07-21 16:00:23 +0100 | [diff] [blame] | 1373 | rdev->open_count--; |
| 1374 | rdev->exclusive = 0; |
| 1375 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1376 | module_put(rdev->owner); |
| 1377 | mutex_unlock(®ulator_list_mutex); |
| 1378 | } |
| 1379 | EXPORT_SYMBOL_GPL(regulator_put); |
| 1380 | |
Mark Brown | d5ad34f | 2012-01-20 20:09:18 +0000 | [diff] [blame] | 1381 | static int devm_regulator_match(struct device *dev, void *res, void *data) |
| 1382 | { |
| 1383 | struct regulator **r = res; |
| 1384 | if (!r || !*r) { |
| 1385 | WARN_ON(!r || !*r); |
| 1386 | return 0; |
| 1387 | } |
| 1388 | return *r == data; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
| 1392 | * devm_regulator_put - Resource managed regulator_put() |
| 1393 | * @regulator: regulator to free |
| 1394 | * |
| 1395 | * Deallocate a regulator allocated with devm_regulator_get(). Normally |
| 1396 | * this function will not need to be called and the resource management |
| 1397 | * code will ensure that the resource is freed. |
| 1398 | */ |
| 1399 | void devm_regulator_put(struct regulator *regulator) |
| 1400 | { |
| 1401 | int rc; |
| 1402 | |
Mark Brown | 361ff50 | 2012-05-07 14:14:30 +0100 | [diff] [blame] | 1403 | rc = devres_release(regulator->dev, devm_regulator_release, |
Mark Brown | d5ad34f | 2012-01-20 20:09:18 +0000 | [diff] [blame] | 1404 | devm_regulator_match, regulator); |
Mark Brown | 230a5a1c | 2012-06-15 18:25:08 +0100 | [diff] [blame] | 1405 | if (rc != 0) |
Mark Brown | 968c2c1 | 2012-05-07 11:34:52 +0100 | [diff] [blame] | 1406 | WARN_ON(rc); |
Mark Brown | d5ad34f | 2012-01-20 20:09:18 +0000 | [diff] [blame] | 1407 | } |
| 1408 | EXPORT_SYMBOL_GPL(devm_regulator_put); |
| 1409 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1410 | /* locks held by regulator_enable() */ |
| 1411 | static int _regulator_enable(struct regulator_dev *rdev) |
| 1412 | { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1413 | int ret, delay; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1414 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1415 | /* check voltage and requested load before enabling */ |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1416 | if (rdev->constraints && |
| 1417 | (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) |
| 1418 | drms_uA_update(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1419 | |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1420 | if (rdev->use_count == 0) { |
| 1421 | /* The regulator may on if it's not switchable or left on */ |
| 1422 | ret = _regulator_is_enabled(rdev); |
| 1423 | if (ret == -EINVAL || ret == 0) { |
| 1424 | if (!_regulator_can_change_status(rdev)) |
| 1425 | return -EPERM; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1426 | |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1427 | if (!rdev->desc->ops->enable) |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1428 | return -EINVAL; |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1429 | |
| 1430 | /* Query before enabling in case configuration |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1431 | * dependent. */ |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1432 | ret = _regulator_get_enable_time(rdev); |
| 1433 | if (ret >= 0) { |
| 1434 | delay = ret; |
| 1435 | } else { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1436 | rdev_warn(rdev, "enable_time() failed: %d\n", |
Daniel Walker | 1d7372e | 2010-11-17 15:30:28 -0800 | [diff] [blame] | 1437 | ret); |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1438 | delay = 0; |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1439 | } |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1440 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1441 | trace_regulator_enable(rdev_get_name(rdev)); |
| 1442 | |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1443 | /* Allow the regulator to ramp; it would be useful |
| 1444 | * to extend this for bulk operations so that the |
| 1445 | * regulators can ramp together. */ |
| 1446 | ret = rdev->desc->ops->enable(rdev); |
| 1447 | if (ret < 0) |
| 1448 | return ret; |
| 1449 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1450 | trace_regulator_enable_delay(rdev_get_name(rdev)); |
| 1451 | |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1452 | if (delay >= 1000) { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1453 | mdelay(delay / 1000); |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1454 | udelay(delay % 1000); |
| 1455 | } else if (delay) { |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1456 | udelay(delay); |
Axel Lin | e36c1df | 2010-11-05 21:51:32 +0800 | [diff] [blame] | 1457 | } |
Mark Brown | 31aae2b | 2009-12-21 12:21:52 +0000 | [diff] [blame] | 1458 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1459 | trace_regulator_enable_complete(rdev_get_name(rdev)); |
| 1460 | |
Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 1461 | } else if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1462 | rdev_err(rdev, "is_enabled() failed: %d\n", ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1463 | return ret; |
| 1464 | } |
Linus Walleij | a7433cf | 2009-08-26 12:54:04 +0200 | [diff] [blame] | 1465 | /* Fallthrough on positive return values - already enabled */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1468 | rdev->use_count++; |
| 1469 | |
| 1470 | return 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * regulator_enable - enable regulator output |
| 1475 | * @regulator: regulator source |
| 1476 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1477 | * Request that the regulator be enabled with the regulator output at |
| 1478 | * the predefined voltage or current value. Calls to regulator_enable() |
| 1479 | * must be balanced with calls to regulator_disable(). |
| 1480 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1481 | * NOTE: the output value can be set by other drivers, boot loader or may be |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1482 | * hardwired in the regulator. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1483 | */ |
| 1484 | int regulator_enable(struct regulator *regulator) |
| 1485 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1486 | struct regulator_dev *rdev = regulator->rdev; |
| 1487 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1488 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1489 | if (regulator->always_on) |
| 1490 | return 0; |
| 1491 | |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1492 | if (rdev->supply) { |
| 1493 | ret = regulator_enable(rdev->supply); |
| 1494 | if (ret != 0) |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1498 | mutex_lock(&rdev->mutex); |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1499 | ret = _regulator_enable(rdev); |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1500 | mutex_unlock(&rdev->mutex); |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1501 | |
Heiko Stübner | d1685e4 | 2011-10-14 18:00:29 +0200 | [diff] [blame] | 1502 | if (ret != 0 && rdev->supply) |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1503 | regulator_disable(rdev->supply); |
| 1504 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1505 | return ret; |
| 1506 | } |
| 1507 | EXPORT_SYMBOL_GPL(regulator_enable); |
| 1508 | |
| 1509 | /* locks held by regulator_disable() */ |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1510 | static int _regulator_disable(struct regulator_dev *rdev) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1511 | { |
| 1512 | int ret = 0; |
| 1513 | |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1514 | if (WARN(rdev->use_count <= 0, |
Joe Perches | 43e7ee3 | 2010-12-06 14:05:19 -0800 | [diff] [blame] | 1515 | "unbalanced disables for %s\n", rdev_get_name(rdev))) |
David Brownell | cd94b50 | 2009-03-11 16:43:34 -0800 | [diff] [blame] | 1516 | return -EIO; |
| 1517 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1518 | /* are we the last user and permitted to disable ? */ |
Mark Brown | 60ef66f | 2009-10-13 13:06:50 +0100 | [diff] [blame] | 1519 | if (rdev->use_count == 1 && |
| 1520 | (rdev->constraints && !rdev->constraints->always_on)) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1521 | |
| 1522 | /* we are last user */ |
Mark Brown | 9a2372f | 2009-08-03 18:49:57 +0100 | [diff] [blame] | 1523 | if (_regulator_can_change_status(rdev) && |
| 1524 | rdev->desc->ops->disable) { |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1525 | trace_regulator_disable(rdev_get_name(rdev)); |
| 1526 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1527 | ret = rdev->desc->ops->disable(rdev); |
| 1528 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1529 | rdev_err(rdev, "failed to disable\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1530 | return ret; |
| 1531 | } |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1532 | |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 1533 | trace_regulator_disable_complete(rdev_get_name(rdev)); |
| 1534 | |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1535 | _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, |
| 1536 | NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1537 | } |
| 1538 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1539 | rdev->use_count = 0; |
| 1540 | } else if (rdev->use_count > 1) { |
| 1541 | |
| 1542 | if (rdev->constraints && |
| 1543 | (rdev->constraints->valid_ops_mask & |
| 1544 | REGULATOR_CHANGE_DRMS)) |
| 1545 | drms_uA_update(rdev); |
| 1546 | |
| 1547 | rdev->use_count--; |
| 1548 | } |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1549 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1550 | return ret; |
| 1551 | } |
| 1552 | |
| 1553 | /** |
| 1554 | * regulator_disable - disable regulator output |
| 1555 | * @regulator: regulator source |
| 1556 | * |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1557 | * Disable the regulator output voltage or current. Calls to |
| 1558 | * regulator_enable() must be balanced with calls to |
| 1559 | * regulator_disable(). |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 1560 | * |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1561 | * NOTE: this will only disable the regulator output if no other consumer |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 1562 | * devices have it enabled, the regulator device supports disabling and |
| 1563 | * machine constraints permit this operation. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1564 | */ |
| 1565 | int regulator_disable(struct regulator *regulator) |
| 1566 | { |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1567 | struct regulator_dev *rdev = regulator->rdev; |
| 1568 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1569 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1570 | if (regulator->always_on) |
| 1571 | return 0; |
| 1572 | |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1573 | mutex_lock(&rdev->mutex); |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1574 | ret = _regulator_disable(rdev); |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1575 | mutex_unlock(&rdev->mutex); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1576 | |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1577 | if (ret == 0 && rdev->supply) |
| 1578 | regulator_disable(rdev->supply); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1579 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1580 | return ret; |
| 1581 | } |
| 1582 | EXPORT_SYMBOL_GPL(regulator_disable); |
| 1583 | |
| 1584 | /* locks held by regulator_force_disable() */ |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1585 | static int _regulator_force_disable(struct regulator_dev *rdev) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1586 | { |
| 1587 | int ret = 0; |
| 1588 | |
| 1589 | /* force disable */ |
| 1590 | if (rdev->desc->ops->disable) { |
| 1591 | /* ah well, who wants to live forever... */ |
| 1592 | ret = rdev->desc->ops->disable(rdev); |
| 1593 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 1594 | rdev_err(rdev, "failed to force disable\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1595 | return ret; |
| 1596 | } |
| 1597 | /* notify other consumers that power has been forced off */ |
Mark Brown | 84b6826 | 2009-12-01 21:12:27 +0000 | [diff] [blame] | 1598 | _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE | |
| 1599 | REGULATOR_EVENT_DISABLE, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1600 | } |
| 1601 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1602 | return ret; |
| 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * regulator_force_disable - force disable regulator output |
| 1607 | * @regulator: regulator source |
| 1608 | * |
| 1609 | * Forcibly disable the regulator output voltage or current. |
| 1610 | * NOTE: this *will* disable the regulator output even if other consumer |
| 1611 | * devices have it enabled. This should be used for situations when device |
| 1612 | * damage will likely occur if the regulator is not disabled (e.g. over temp). |
| 1613 | */ |
| 1614 | int regulator_force_disable(struct regulator *regulator) |
| 1615 | { |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1616 | struct regulator_dev *rdev = regulator->rdev; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1617 | int ret; |
| 1618 | |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1619 | mutex_lock(&rdev->mutex); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1620 | regulator->uA_load = 0; |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1621 | ret = _regulator_force_disable(regulator->rdev); |
Mark Brown | 82d1583 | 2011-05-09 11:41:02 +0200 | [diff] [blame] | 1622 | mutex_unlock(&rdev->mutex); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1623 | |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 1624 | if (rdev->supply) |
| 1625 | while (rdev->open_count--) |
| 1626 | regulator_disable(rdev->supply); |
Jeffrey Carlyle | 8cbf811 | 2010-10-08 14:49:19 -0500 | [diff] [blame] | 1627 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1628 | return ret; |
| 1629 | } |
| 1630 | EXPORT_SYMBOL_GPL(regulator_force_disable); |
| 1631 | |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 1632 | static void regulator_disable_work(struct work_struct *work) |
| 1633 | { |
| 1634 | struct regulator_dev *rdev = container_of(work, struct regulator_dev, |
| 1635 | disable_work.work); |
| 1636 | int count, i, ret; |
| 1637 | |
| 1638 | mutex_lock(&rdev->mutex); |
| 1639 | |
| 1640 | BUG_ON(!rdev->deferred_disables); |
| 1641 | |
| 1642 | count = rdev->deferred_disables; |
| 1643 | rdev->deferred_disables = 0; |
| 1644 | |
| 1645 | for (i = 0; i < count; i++) { |
| 1646 | ret = _regulator_disable(rdev); |
| 1647 | if (ret != 0) |
| 1648 | rdev_err(rdev, "Deferred disable failed: %d\n", ret); |
| 1649 | } |
| 1650 | |
| 1651 | mutex_unlock(&rdev->mutex); |
| 1652 | |
| 1653 | if (rdev->supply) { |
| 1654 | for (i = 0; i < count; i++) { |
| 1655 | ret = regulator_disable(rdev->supply); |
| 1656 | if (ret != 0) { |
| 1657 | rdev_err(rdev, |
| 1658 | "Supply disable failed: %d\n", ret); |
| 1659 | } |
| 1660 | } |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /** |
| 1665 | * regulator_disable_deferred - disable regulator output with delay |
| 1666 | * @regulator: regulator source |
| 1667 | * @ms: miliseconds until the regulator is disabled |
| 1668 | * |
| 1669 | * Execute regulator_disable() on the regulator after a delay. This |
| 1670 | * is intended for use with devices that require some time to quiesce. |
| 1671 | * |
| 1672 | * NOTE: this will only disable the regulator output if no other consumer |
| 1673 | * devices have it enabled, the regulator device supports disabling and |
| 1674 | * machine constraints permit this operation. |
| 1675 | */ |
| 1676 | int regulator_disable_deferred(struct regulator *regulator, int ms) |
| 1677 | { |
| 1678 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | aa59802 | 2011-10-03 22:42:43 +0100 | [diff] [blame] | 1679 | int ret; |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 1680 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1681 | if (regulator->always_on) |
| 1682 | return 0; |
| 1683 | |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 1684 | mutex_lock(&rdev->mutex); |
| 1685 | rdev->deferred_disables++; |
| 1686 | mutex_unlock(&rdev->mutex); |
| 1687 | |
Mark Brown | aa59802 | 2011-10-03 22:42:43 +0100 | [diff] [blame] | 1688 | ret = schedule_delayed_work(&rdev->disable_work, |
| 1689 | msecs_to_jiffies(ms)); |
| 1690 | if (ret < 0) |
| 1691 | return ret; |
| 1692 | else |
| 1693 | return 0; |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 1694 | } |
| 1695 | EXPORT_SYMBOL_GPL(regulator_disable_deferred); |
| 1696 | |
Mark Brown | cd6dffb | 2012-04-15 12:37:47 +0100 | [diff] [blame] | 1697 | /** |
| 1698 | * regulator_is_enabled_regmap - standard is_enabled() for regmap users |
| 1699 | * |
| 1700 | * @rdev: regulator to operate on |
| 1701 | * |
| 1702 | * Regulators that use regmap for their register I/O can set the |
| 1703 | * enable_reg and enable_mask fields in their descriptor and then use |
| 1704 | * this as their is_enabled operation, saving some code. |
| 1705 | */ |
| 1706 | int regulator_is_enabled_regmap(struct regulator_dev *rdev) |
| 1707 | { |
| 1708 | unsigned int val; |
| 1709 | int ret; |
| 1710 | |
| 1711 | ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); |
| 1712 | if (ret != 0) |
| 1713 | return ret; |
| 1714 | |
| 1715 | return (val & rdev->desc->enable_mask) != 0; |
| 1716 | } |
| 1717 | EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap); |
| 1718 | |
| 1719 | /** |
| 1720 | * regulator_enable_regmap - standard enable() for regmap users |
| 1721 | * |
| 1722 | * @rdev: regulator to operate on |
| 1723 | * |
| 1724 | * Regulators that use regmap for their register I/O can set the |
| 1725 | * enable_reg and enable_mask fields in their descriptor and then use |
| 1726 | * this as their enable() operation, saving some code. |
| 1727 | */ |
| 1728 | int regulator_enable_regmap(struct regulator_dev *rdev) |
| 1729 | { |
| 1730 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 1731 | rdev->desc->enable_mask, |
| 1732 | rdev->desc->enable_mask); |
| 1733 | } |
| 1734 | EXPORT_SYMBOL_GPL(regulator_enable_regmap); |
| 1735 | |
| 1736 | /** |
| 1737 | * regulator_disable_regmap - standard disable() for regmap users |
| 1738 | * |
| 1739 | * @rdev: regulator to operate on |
| 1740 | * |
| 1741 | * Regulators that use regmap for their register I/O can set the |
| 1742 | * enable_reg and enable_mask fields in their descriptor and then use |
| 1743 | * this as their disable() operation, saving some code. |
| 1744 | */ |
| 1745 | int regulator_disable_regmap(struct regulator_dev *rdev) |
| 1746 | { |
| 1747 | return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg, |
| 1748 | rdev->desc->enable_mask, 0); |
| 1749 | } |
| 1750 | EXPORT_SYMBOL_GPL(regulator_disable_regmap); |
| 1751 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1752 | static int _regulator_is_enabled(struct regulator_dev *rdev) |
| 1753 | { |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 1754 | /* If we don't know then assume that the regulator is always on */ |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1755 | if (!rdev->desc->ops->is_enabled) |
Mark Brown | 9a7f6a4 | 2010-02-11 17:22:45 +0000 | [diff] [blame] | 1756 | return 1; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1757 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1758 | return rdev->desc->ops->is_enabled(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /** |
| 1762 | * regulator_is_enabled - is the regulator output enabled |
| 1763 | * @regulator: regulator source |
| 1764 | * |
David Brownell | 412aec6 | 2008-11-16 11:44:46 -0800 | [diff] [blame] | 1765 | * Returns positive if the regulator driver backing the source/client |
| 1766 | * has requested that the device be enabled, zero if it hasn't, else a |
| 1767 | * negative errno code. |
| 1768 | * |
| 1769 | * Note that the device backing this regulator handle can have multiple |
| 1770 | * users, so it might be enabled even if regulator_enable() was never |
| 1771 | * called for this particular source. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1772 | */ |
| 1773 | int regulator_is_enabled(struct regulator *regulator) |
| 1774 | { |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1775 | int ret; |
| 1776 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 1777 | if (regulator->always_on) |
| 1778 | return 1; |
| 1779 | |
Mark Brown | 9332546 | 2009-08-03 18:49:56 +0100 | [diff] [blame] | 1780 | mutex_lock(®ulator->rdev->mutex); |
| 1781 | ret = _regulator_is_enabled(regulator->rdev); |
| 1782 | mutex_unlock(®ulator->rdev->mutex); |
| 1783 | |
| 1784 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 1785 | } |
| 1786 | EXPORT_SYMBOL_GPL(regulator_is_enabled); |
| 1787 | |
| 1788 | /** |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 1789 | * regulator_count_voltages - count regulator_list_voltage() selectors |
| 1790 | * @regulator: regulator source |
| 1791 | * |
| 1792 | * Returns number of selectors, or negative errno. Selectors are |
| 1793 | * numbered starting at zero, and typically correspond to bitfields |
| 1794 | * in hardware registers. |
| 1795 | */ |
| 1796 | int regulator_count_voltages(struct regulator *regulator) |
| 1797 | { |
| 1798 | struct regulator_dev *rdev = regulator->rdev; |
| 1799 | |
| 1800 | return rdev->desc->n_voltages ? : -EINVAL; |
| 1801 | } |
| 1802 | EXPORT_SYMBOL_GPL(regulator_count_voltages); |
| 1803 | |
| 1804 | /** |
Mark Brown | bca7bbf | 2012-05-09 21:38:33 +0100 | [diff] [blame] | 1805 | * regulator_list_voltage_linear - List voltages with simple calculation |
| 1806 | * |
| 1807 | * @rdev: Regulator device |
| 1808 | * @selector: Selector to convert into a voltage |
| 1809 | * |
| 1810 | * Regulators with a simple linear mapping between voltages and |
| 1811 | * selectors can set min_uV and uV_step in the regulator descriptor |
| 1812 | * and then use this function as their list_voltage() operation, |
| 1813 | */ |
| 1814 | int regulator_list_voltage_linear(struct regulator_dev *rdev, |
| 1815 | unsigned int selector) |
| 1816 | { |
| 1817 | if (selector >= rdev->desc->n_voltages) |
| 1818 | return -EINVAL; |
| 1819 | |
| 1820 | return rdev->desc->min_uV + (rdev->desc->uV_step * selector); |
| 1821 | } |
| 1822 | EXPORT_SYMBOL_GPL(regulator_list_voltage_linear); |
| 1823 | |
| 1824 | /** |
Axel Lin | cffc959 | 2012-05-20 10:30:21 +0800 | [diff] [blame] | 1825 | * regulator_list_voltage_table - List voltages with table based mapping |
| 1826 | * |
| 1827 | * @rdev: Regulator device |
| 1828 | * @selector: Selector to convert into a voltage |
| 1829 | * |
| 1830 | * Regulators with table based mapping between voltages and |
| 1831 | * selectors can set volt_table in the regulator descriptor |
| 1832 | * and then use this function as their list_voltage() operation. |
| 1833 | */ |
| 1834 | int regulator_list_voltage_table(struct regulator_dev *rdev, |
| 1835 | unsigned int selector) |
| 1836 | { |
| 1837 | if (!rdev->desc->volt_table) { |
| 1838 | BUG_ON(!rdev->desc->volt_table); |
| 1839 | return -EINVAL; |
| 1840 | } |
| 1841 | |
| 1842 | if (selector >= rdev->desc->n_voltages) |
| 1843 | return -EINVAL; |
| 1844 | |
| 1845 | return rdev->desc->volt_table[selector]; |
| 1846 | } |
| 1847 | EXPORT_SYMBOL_GPL(regulator_list_voltage_table); |
| 1848 | |
| 1849 | /** |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 1850 | * regulator_list_voltage - enumerate supported voltages |
| 1851 | * @regulator: regulator source |
| 1852 | * @selector: identify voltage to list |
| 1853 | * Context: can sleep |
| 1854 | * |
| 1855 | * Returns a voltage that can be passed to @regulator_set_voltage(), |
Thomas Weber | 8839316 | 2010-03-16 11:47:56 +0100 | [diff] [blame] | 1856 | * zero if this selector code can't be used on this system, or a |
David Brownell | 4367cfd | 2009-02-26 11:48:36 -0800 | [diff] [blame] | 1857 | * negative errno. |
| 1858 | */ |
| 1859 | int regulator_list_voltage(struct regulator *regulator, unsigned selector) |
| 1860 | { |
| 1861 | struct regulator_dev *rdev = regulator->rdev; |
| 1862 | struct regulator_ops *ops = rdev->desc->ops; |
| 1863 | int ret; |
| 1864 | |
| 1865 | if (!ops->list_voltage || selector >= rdev->desc->n_voltages) |
| 1866 | return -EINVAL; |
| 1867 | |
| 1868 | mutex_lock(&rdev->mutex); |
| 1869 | ret = ops->list_voltage(rdev, selector); |
| 1870 | mutex_unlock(&rdev->mutex); |
| 1871 | |
| 1872 | if (ret > 0) { |
| 1873 | if (ret < rdev->constraints->min_uV) |
| 1874 | ret = 0; |
| 1875 | else if (ret > rdev->constraints->max_uV) |
| 1876 | ret = 0; |
| 1877 | } |
| 1878 | |
| 1879 | return ret; |
| 1880 | } |
| 1881 | EXPORT_SYMBOL_GPL(regulator_list_voltage); |
| 1882 | |
| 1883 | /** |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1884 | * regulator_is_supported_voltage - check if a voltage range can be supported |
| 1885 | * |
| 1886 | * @regulator: Regulator to check. |
| 1887 | * @min_uV: Minimum required voltage in uV. |
| 1888 | * @max_uV: Maximum required voltage in uV. |
| 1889 | * |
| 1890 | * Returns a boolean or a negative error code. |
| 1891 | */ |
| 1892 | int regulator_is_supported_voltage(struct regulator *regulator, |
| 1893 | int min_uV, int max_uV) |
| 1894 | { |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 1895 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1896 | int i, voltages, ret; |
| 1897 | |
Mark Brown | c5f3939 | 2012-07-02 15:00:18 +0100 | [diff] [blame] | 1898 | /* If we can't change voltage check the current voltage */ |
| 1899 | if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) { |
| 1900 | ret = regulator_get_voltage(regulator); |
| 1901 | if (ret >= 0) |
| 1902 | return (min_uV >= ret && ret <= max_uV); |
| 1903 | else |
| 1904 | return ret; |
| 1905 | } |
| 1906 | |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1907 | ret = regulator_count_voltages(regulator); |
| 1908 | if (ret < 0) |
| 1909 | return ret; |
| 1910 | voltages = ret; |
| 1911 | |
| 1912 | for (i = 0; i < voltages; i++) { |
| 1913 | ret = regulator_list_voltage(regulator, i); |
| 1914 | |
| 1915 | if (ret >= min_uV && ret <= max_uV) |
| 1916 | return 1; |
| 1917 | } |
| 1918 | |
| 1919 | return 0; |
| 1920 | } |
Mark Brown | a398eaa | 2011-12-28 12:48:45 +0000 | [diff] [blame] | 1921 | EXPORT_SYMBOL_GPL(regulator_is_supported_voltage); |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 1922 | |
Mark Brown | 4ab5b3d | 2012-04-15 11:23:30 +0100 | [diff] [blame] | 1923 | /** |
| 1924 | * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users |
| 1925 | * |
| 1926 | * @rdev: regulator to operate on |
| 1927 | * |
| 1928 | * Regulators that use regmap for their register I/O can set the |
| 1929 | * vsel_reg and vsel_mask fields in their descriptor and then use this |
| 1930 | * as their get_voltage_vsel operation, saving some code. |
| 1931 | */ |
| 1932 | int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev) |
| 1933 | { |
| 1934 | unsigned int val; |
| 1935 | int ret; |
| 1936 | |
| 1937 | ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val); |
| 1938 | if (ret != 0) |
| 1939 | return ret; |
| 1940 | |
| 1941 | val &= rdev->desc->vsel_mask; |
| 1942 | val >>= ffs(rdev->desc->vsel_mask) - 1; |
| 1943 | |
| 1944 | return val; |
| 1945 | } |
| 1946 | EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap); |
| 1947 | |
| 1948 | /** |
| 1949 | * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users |
| 1950 | * |
| 1951 | * @rdev: regulator to operate on |
| 1952 | * @sel: Selector to set |
| 1953 | * |
| 1954 | * Regulators that use regmap for their register I/O can set the |
| 1955 | * vsel_reg and vsel_mask fields in their descriptor and then use this |
| 1956 | * as their set_voltage_vsel operation, saving some code. |
| 1957 | */ |
| 1958 | int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel) |
| 1959 | { |
| 1960 | sel <<= ffs(rdev->desc->vsel_mask) - 1; |
| 1961 | |
| 1962 | return regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg, |
| 1963 | rdev->desc->vsel_mask, sel); |
| 1964 | } |
| 1965 | EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap); |
| 1966 | |
Mark Brown | e843fc4 | 2012-05-09 21:16:06 +0100 | [diff] [blame] | 1967 | /** |
| 1968 | * regulator_map_voltage_iterate - map_voltage() based on list_voltage() |
| 1969 | * |
| 1970 | * @rdev: Regulator to operate on |
| 1971 | * @min_uV: Lower bound for voltage |
| 1972 | * @max_uV: Upper bound for voltage |
| 1973 | * |
| 1974 | * Drivers implementing set_voltage_sel() and list_voltage() can use |
| 1975 | * this as their map_voltage() operation. It will find a suitable |
| 1976 | * voltage by calling list_voltage() until it gets something in bounds |
| 1977 | * for the requested voltages. |
| 1978 | */ |
| 1979 | int regulator_map_voltage_iterate(struct regulator_dev *rdev, |
| 1980 | int min_uV, int max_uV) |
| 1981 | { |
| 1982 | int best_val = INT_MAX; |
| 1983 | int selector = 0; |
| 1984 | int i, ret; |
| 1985 | |
| 1986 | /* Find the smallest voltage that falls within the specified |
| 1987 | * range. |
| 1988 | */ |
| 1989 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 1990 | ret = rdev->desc->ops->list_voltage(rdev, i); |
| 1991 | if (ret < 0) |
| 1992 | continue; |
| 1993 | |
| 1994 | if (ret < best_val && ret >= min_uV && ret <= max_uV) { |
| 1995 | best_val = ret; |
| 1996 | selector = i; |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | if (best_val != INT_MAX) |
| 2001 | return selector; |
| 2002 | else |
| 2003 | return -EINVAL; |
| 2004 | } |
| 2005 | EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate); |
| 2006 | |
Mark Brown | bca7bbf | 2012-05-09 21:38:33 +0100 | [diff] [blame] | 2007 | /** |
| 2008 | * regulator_map_voltage_linear - map_voltage() for simple linear mappings |
| 2009 | * |
| 2010 | * @rdev: Regulator to operate on |
| 2011 | * @min_uV: Lower bound for voltage |
| 2012 | * @max_uV: Upper bound for voltage |
| 2013 | * |
| 2014 | * Drivers providing min_uV and uV_step in their regulator_desc can |
| 2015 | * use this as their map_voltage() operation. |
| 2016 | */ |
| 2017 | int regulator_map_voltage_linear(struct regulator_dev *rdev, |
| 2018 | int min_uV, int max_uV) |
| 2019 | { |
| 2020 | int ret, voltage; |
| 2021 | |
| 2022 | if (!rdev->desc->uV_step) { |
| 2023 | BUG_ON(!rdev->desc->uV_step); |
| 2024 | return -EINVAL; |
| 2025 | } |
| 2026 | |
Axel Lin | ccfcb1c | 2012-05-14 10:33:37 +0800 | [diff] [blame] | 2027 | ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step); |
Mark Brown | bca7bbf | 2012-05-09 21:38:33 +0100 | [diff] [blame] | 2028 | if (ret < 0) |
| 2029 | return ret; |
| 2030 | |
| 2031 | /* Map back into a voltage to verify we're still in bounds */ |
| 2032 | voltage = rdev->desc->ops->list_voltage(rdev, ret); |
| 2033 | if (voltage < min_uV || voltage > max_uV) |
| 2034 | return -EINVAL; |
| 2035 | |
| 2036 | return ret; |
| 2037 | } |
| 2038 | EXPORT_SYMBOL_GPL(regulator_map_voltage_linear); |
| 2039 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2040 | static int _regulator_do_set_voltage(struct regulator_dev *rdev, |
| 2041 | int min_uV, int max_uV) |
| 2042 | { |
| 2043 | int ret; |
Linus Walleij | 77af1b264 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2044 | int delay = 0; |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2045 | int best_val = 0; |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2046 | unsigned int selector; |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2047 | int old_selector = -1; |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2048 | |
| 2049 | trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV); |
| 2050 | |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2051 | min_uV += rdev->constraints->uV_offset; |
| 2052 | max_uV += rdev->constraints->uV_offset; |
| 2053 | |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2054 | /* |
| 2055 | * If we can't obtain the old selector there is not enough |
| 2056 | * info to call set_voltage_time_sel(). |
| 2057 | */ |
Axel Lin | 8b7485e | 2012-05-21 09:37:52 +0800 | [diff] [blame] | 2058 | if (_regulator_is_enabled(rdev) && |
| 2059 | rdev->desc->ops->set_voltage_time_sel && |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2060 | rdev->desc->ops->get_voltage_sel) { |
| 2061 | old_selector = rdev->desc->ops->get_voltage_sel(rdev); |
| 2062 | if (old_selector < 0) |
| 2063 | return old_selector; |
| 2064 | } |
| 2065 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2066 | if (rdev->desc->ops->set_voltage) { |
| 2067 | ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, |
| 2068 | &selector); |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2069 | |
| 2070 | if (ret >= 0) { |
| 2071 | if (rdev->desc->ops->list_voltage) |
| 2072 | best_val = rdev->desc->ops->list_voltage(rdev, |
| 2073 | selector); |
| 2074 | else |
| 2075 | best_val = _regulator_get_voltage(rdev); |
| 2076 | } |
| 2077 | |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2078 | } else if (rdev->desc->ops->set_voltage_sel) { |
Axel Lin | 9152c36 | 2012-06-04 09:41:38 +0800 | [diff] [blame] | 2079 | if (rdev->desc->ops->map_voltage) { |
Mark Brown | e843fc4 | 2012-05-09 21:16:06 +0100 | [diff] [blame] | 2080 | ret = rdev->desc->ops->map_voltage(rdev, min_uV, |
| 2081 | max_uV); |
Axel Lin | 9152c36 | 2012-06-04 09:41:38 +0800 | [diff] [blame] | 2082 | } else { |
| 2083 | if (rdev->desc->ops->list_voltage == |
| 2084 | regulator_list_voltage_linear) |
| 2085 | ret = regulator_map_voltage_linear(rdev, |
| 2086 | min_uV, max_uV); |
| 2087 | else |
| 2088 | ret = regulator_map_voltage_iterate(rdev, |
| 2089 | min_uV, max_uV); |
| 2090 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2091 | |
Mark Brown | e843fc4 | 2012-05-09 21:16:06 +0100 | [diff] [blame] | 2092 | if (ret >= 0) { |
Mark Brown | e113d79 | 2012-06-26 10:57:51 +0100 | [diff] [blame] | 2093 | best_val = rdev->desc->ops->list_voltage(rdev, ret); |
| 2094 | if (min_uV <= best_val && max_uV >= best_val) { |
| 2095 | selector = ret; |
| 2096 | ret = rdev->desc->ops->set_voltage_sel(rdev, |
| 2097 | ret); |
| 2098 | } else { |
| 2099 | ret = -EINVAL; |
| 2100 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2101 | } |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2102 | } else { |
| 2103 | ret = -EINVAL; |
| 2104 | } |
| 2105 | |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2106 | /* Call set_voltage_time_sel if successfully obtained old_selector */ |
Mark Brown | 5aff3a8 | 2012-06-26 11:16:58 +0100 | [diff] [blame] | 2107 | if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 && |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2108 | rdev->desc->ops->set_voltage_time_sel) { |
| 2109 | |
| 2110 | delay = rdev->desc->ops->set_voltage_time_sel(rdev, |
| 2111 | old_selector, selector); |
| 2112 | if (delay < 0) { |
| 2113 | rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", |
| 2114 | delay); |
| 2115 | delay = 0; |
| 2116 | } |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2117 | |
Philip Rakity | 8b96de3 | 2012-06-14 15:07:56 -0700 | [diff] [blame] | 2118 | /* Insert any necessary delays */ |
| 2119 | if (delay >= 1000) { |
| 2120 | mdelay(delay / 1000); |
| 2121 | udelay(delay % 1000); |
| 2122 | } else if (delay) { |
| 2123 | udelay(delay); |
| 2124 | } |
Linus Walleij | 77af1b264 | 2011-03-17 13:24:36 +0100 | [diff] [blame] | 2125 | } |
| 2126 | |
Philip Rakity | 2f7baf8 | 2012-06-15 11:27:36 -0700 | [diff] [blame] | 2127 | if (ret == 0 && best_val >= 0) |
Mark Brown | ded06a5 | 2010-12-16 13:59:10 +0000 | [diff] [blame] | 2128 | _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, |
Philip Rakity | 2f7baf8 | 2012-06-15 11:27:36 -0700 | [diff] [blame] | 2129 | (void *)best_val); |
Mark Brown | ded06a5 | 2010-12-16 13:59:10 +0000 | [diff] [blame] | 2130 | |
Axel Lin | eba41a5 | 2012-04-04 10:32:10 +0800 | [diff] [blame] | 2131 | trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val); |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2132 | |
| 2133 | return ret; |
| 2134 | } |
| 2135 | |
Mark Brown | a7a1ad9 | 2009-07-21 16:00:24 +0100 | [diff] [blame] | 2136 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2137 | * regulator_set_voltage - set regulator output voltage |
| 2138 | * @regulator: regulator source |
| 2139 | * @min_uV: Minimum required voltage in uV |
| 2140 | * @max_uV: Maximum acceptable voltage in uV |
| 2141 | * |
| 2142 | * Sets a voltage regulator to the desired output voltage. This can be set |
| 2143 | * during any regulator state. IOW, regulator can be disabled or enabled. |
| 2144 | * |
| 2145 | * If the regulator is enabled then the voltage will change to the new value |
| 2146 | * immediately otherwise if the regulator is disabled the regulator will |
| 2147 | * output at the new voltage when enabled. |
| 2148 | * |
| 2149 | * NOTE: If the regulator is shared between several devices then the lowest |
| 2150 | * request voltage that meets the system constraints will be used. |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2151 | * Regulator system constraints must be set for this regulator before |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2152 | * calling this function otherwise this call will fail. |
| 2153 | */ |
| 2154 | int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV) |
| 2155 | { |
| 2156 | struct regulator_dev *rdev = regulator->rdev; |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2157 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2158 | |
| 2159 | mutex_lock(&rdev->mutex); |
| 2160 | |
Mark Brown | 95a3c23 | 2010-12-16 15:49:37 +0000 | [diff] [blame] | 2161 | /* If we're setting the same range as last time the change |
| 2162 | * should be a noop (some cpufreq implementations use the same |
| 2163 | * voltage for multiple frequencies, for example). |
| 2164 | */ |
| 2165 | if (regulator->min_uV == min_uV && regulator->max_uV == max_uV) |
| 2166 | goto out; |
| 2167 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2168 | /* sanity check */ |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2169 | if (!rdev->desc->ops->set_voltage && |
| 2170 | !rdev->desc->ops->set_voltage_sel) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2171 | ret = -EINVAL; |
| 2172 | goto out; |
| 2173 | } |
| 2174 | |
| 2175 | /* constraints check */ |
| 2176 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 2177 | if (ret < 0) |
| 2178 | goto out; |
| 2179 | regulator->min_uV = min_uV; |
| 2180 | regulator->max_uV = max_uV; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 2181 | |
Thomas Petazzoni | 05fda3b1a | 2010-12-03 11:31:07 +0100 | [diff] [blame] | 2182 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
| 2183 | if (ret < 0) |
| 2184 | goto out; |
| 2185 | |
Mark Brown | 7579025 | 2010-12-12 14:25:50 +0000 | [diff] [blame] | 2186 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
Mark Brown | 02fa3ec | 2010-11-10 14:38:30 +0000 | [diff] [blame] | 2187 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2188 | out: |
| 2189 | mutex_unlock(&rdev->mutex); |
| 2190 | return ret; |
| 2191 | } |
| 2192 | EXPORT_SYMBOL_GPL(regulator_set_voltage); |
| 2193 | |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 2194 | /** |
Linus Walleij | 88cd222b | 2011-03-17 13:24:52 +0100 | [diff] [blame] | 2195 | * regulator_set_voltage_time - get raise/fall time |
| 2196 | * @regulator: regulator source |
| 2197 | * @old_uV: starting voltage in microvolts |
| 2198 | * @new_uV: target voltage in microvolts |
| 2199 | * |
| 2200 | * Provided with the starting and ending voltage, this function attempts to |
| 2201 | * calculate the time in microseconds required to rise or fall to this new |
| 2202 | * voltage. |
| 2203 | */ |
| 2204 | int regulator_set_voltage_time(struct regulator *regulator, |
| 2205 | int old_uV, int new_uV) |
| 2206 | { |
| 2207 | struct regulator_dev *rdev = regulator->rdev; |
| 2208 | struct regulator_ops *ops = rdev->desc->ops; |
| 2209 | int old_sel = -1; |
| 2210 | int new_sel = -1; |
| 2211 | int voltage; |
| 2212 | int i; |
| 2213 | |
| 2214 | /* Currently requires operations to do this */ |
| 2215 | if (!ops->list_voltage || !ops->set_voltage_time_sel |
| 2216 | || !rdev->desc->n_voltages) |
| 2217 | return -EINVAL; |
| 2218 | |
| 2219 | for (i = 0; i < rdev->desc->n_voltages; i++) { |
| 2220 | /* We only look for exact voltage matches here */ |
| 2221 | voltage = regulator_list_voltage(regulator, i); |
| 2222 | if (voltage < 0) |
| 2223 | return -EINVAL; |
| 2224 | if (voltage == 0) |
| 2225 | continue; |
| 2226 | if (voltage == old_uV) |
| 2227 | old_sel = i; |
| 2228 | if (voltage == new_uV) |
| 2229 | new_sel = i; |
| 2230 | } |
| 2231 | |
| 2232 | if (old_sel < 0 || new_sel < 0) |
| 2233 | return -EINVAL; |
| 2234 | |
| 2235 | return ops->set_voltage_time_sel(rdev, old_sel, new_sel); |
| 2236 | } |
| 2237 | EXPORT_SYMBOL_GPL(regulator_set_voltage_time); |
| 2238 | |
| 2239 | /** |
Mark Brown | 606a256 | 2010-12-16 15:49:36 +0000 | [diff] [blame] | 2240 | * regulator_sync_voltage - re-apply last regulator output voltage |
| 2241 | * @regulator: regulator source |
| 2242 | * |
| 2243 | * Re-apply the last configured voltage. This is intended to be used |
| 2244 | * where some external control source the consumer is cooperating with |
| 2245 | * has caused the configured voltage to change. |
| 2246 | */ |
| 2247 | int regulator_sync_voltage(struct regulator *regulator) |
| 2248 | { |
| 2249 | struct regulator_dev *rdev = regulator->rdev; |
| 2250 | int ret, min_uV, max_uV; |
| 2251 | |
| 2252 | mutex_lock(&rdev->mutex); |
| 2253 | |
| 2254 | if (!rdev->desc->ops->set_voltage && |
| 2255 | !rdev->desc->ops->set_voltage_sel) { |
| 2256 | ret = -EINVAL; |
| 2257 | goto out; |
| 2258 | } |
| 2259 | |
| 2260 | /* This is only going to work if we've had a voltage configured. */ |
| 2261 | if (!regulator->min_uV && !regulator->max_uV) { |
| 2262 | ret = -EINVAL; |
| 2263 | goto out; |
| 2264 | } |
| 2265 | |
| 2266 | min_uV = regulator->min_uV; |
| 2267 | max_uV = regulator->max_uV; |
| 2268 | |
| 2269 | /* This should be a paranoia check... */ |
| 2270 | ret = regulator_check_voltage(rdev, &min_uV, &max_uV); |
| 2271 | if (ret < 0) |
| 2272 | goto out; |
| 2273 | |
| 2274 | ret = regulator_check_consumers(rdev, &min_uV, &max_uV); |
| 2275 | if (ret < 0) |
| 2276 | goto out; |
| 2277 | |
| 2278 | ret = _regulator_do_set_voltage(rdev, min_uV, max_uV); |
| 2279 | |
| 2280 | out: |
| 2281 | mutex_unlock(&rdev->mutex); |
| 2282 | return ret; |
| 2283 | } |
| 2284 | EXPORT_SYMBOL_GPL(regulator_sync_voltage); |
| 2285 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2286 | static int _regulator_get_voltage(struct regulator_dev *rdev) |
| 2287 | { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2288 | int sel, ret; |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 2289 | |
| 2290 | if (rdev->desc->ops->get_voltage_sel) { |
| 2291 | sel = rdev->desc->ops->get_voltage_sel(rdev); |
| 2292 | if (sel < 0) |
| 2293 | return sel; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2294 | ret = rdev->desc->ops->list_voltage(rdev, sel); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2295 | } else if (rdev->desc->ops->get_voltage) { |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2296 | ret = rdev->desc->ops->get_voltage(rdev); |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2297 | } else { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2298 | return -EINVAL; |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2299 | } |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2300 | |
Axel Lin | cb220d1 | 2011-05-23 20:08:10 +0800 | [diff] [blame] | 2301 | if (ret < 0) |
| 2302 | return ret; |
Mark Brown | bf5892a | 2011-05-08 22:13:37 +0100 | [diff] [blame] | 2303 | return ret - rdev->constraints->uV_offset; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /** |
| 2307 | * regulator_get_voltage - get regulator output voltage |
| 2308 | * @regulator: regulator source |
| 2309 | * |
| 2310 | * This returns the current regulator voltage in uV. |
| 2311 | * |
| 2312 | * NOTE: If the regulator is disabled it will return the voltage value. This |
| 2313 | * function should not be used to determine regulator state. |
| 2314 | */ |
| 2315 | int regulator_get_voltage(struct regulator *regulator) |
| 2316 | { |
| 2317 | int ret; |
| 2318 | |
| 2319 | mutex_lock(®ulator->rdev->mutex); |
| 2320 | |
| 2321 | ret = _regulator_get_voltage(regulator->rdev); |
| 2322 | |
| 2323 | mutex_unlock(®ulator->rdev->mutex); |
| 2324 | |
| 2325 | return ret; |
| 2326 | } |
| 2327 | EXPORT_SYMBOL_GPL(regulator_get_voltage); |
| 2328 | |
| 2329 | /** |
| 2330 | * regulator_set_current_limit - set regulator output current limit |
| 2331 | * @regulator: regulator source |
| 2332 | * @min_uA: Minimuum supported current in uA |
| 2333 | * @max_uA: Maximum supported current in uA |
| 2334 | * |
| 2335 | * Sets current sink to the desired output current. This can be set during |
| 2336 | * any regulator state. IOW, regulator can be disabled or enabled. |
| 2337 | * |
| 2338 | * If the regulator is enabled then the current will change to the new value |
| 2339 | * immediately otherwise if the regulator is disabled the regulator will |
| 2340 | * output at the new current when enabled. |
| 2341 | * |
| 2342 | * NOTE: Regulator system constraints must be set for this regulator before |
| 2343 | * calling this function otherwise this call will fail. |
| 2344 | */ |
| 2345 | int regulator_set_current_limit(struct regulator *regulator, |
| 2346 | int min_uA, int max_uA) |
| 2347 | { |
| 2348 | struct regulator_dev *rdev = regulator->rdev; |
| 2349 | int ret; |
| 2350 | |
| 2351 | mutex_lock(&rdev->mutex); |
| 2352 | |
| 2353 | /* sanity check */ |
| 2354 | if (!rdev->desc->ops->set_current_limit) { |
| 2355 | ret = -EINVAL; |
| 2356 | goto out; |
| 2357 | } |
| 2358 | |
| 2359 | /* constraints check */ |
| 2360 | ret = regulator_check_current_limit(rdev, &min_uA, &max_uA); |
| 2361 | if (ret < 0) |
| 2362 | goto out; |
| 2363 | |
| 2364 | ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA); |
| 2365 | out: |
| 2366 | mutex_unlock(&rdev->mutex); |
| 2367 | return ret; |
| 2368 | } |
| 2369 | EXPORT_SYMBOL_GPL(regulator_set_current_limit); |
| 2370 | |
| 2371 | static int _regulator_get_current_limit(struct regulator_dev *rdev) |
| 2372 | { |
| 2373 | int ret; |
| 2374 | |
| 2375 | mutex_lock(&rdev->mutex); |
| 2376 | |
| 2377 | /* sanity check */ |
| 2378 | if (!rdev->desc->ops->get_current_limit) { |
| 2379 | ret = -EINVAL; |
| 2380 | goto out; |
| 2381 | } |
| 2382 | |
| 2383 | ret = rdev->desc->ops->get_current_limit(rdev); |
| 2384 | out: |
| 2385 | mutex_unlock(&rdev->mutex); |
| 2386 | return ret; |
| 2387 | } |
| 2388 | |
| 2389 | /** |
| 2390 | * regulator_get_current_limit - get regulator output current |
| 2391 | * @regulator: regulator source |
| 2392 | * |
| 2393 | * This returns the current supplied by the specified current sink in uA. |
| 2394 | * |
| 2395 | * NOTE: If the regulator is disabled it will return the current value. This |
| 2396 | * function should not be used to determine regulator state. |
| 2397 | */ |
| 2398 | int regulator_get_current_limit(struct regulator *regulator) |
| 2399 | { |
| 2400 | return _regulator_get_current_limit(regulator->rdev); |
| 2401 | } |
| 2402 | EXPORT_SYMBOL_GPL(regulator_get_current_limit); |
| 2403 | |
| 2404 | /** |
| 2405 | * regulator_set_mode - set regulator operating mode |
| 2406 | * @regulator: regulator source |
| 2407 | * @mode: operating mode - one of the REGULATOR_MODE constants |
| 2408 | * |
| 2409 | * Set regulator operating mode to increase regulator efficiency or improve |
| 2410 | * regulation performance. |
| 2411 | * |
| 2412 | * NOTE: Regulator system constraints must be set for this regulator before |
| 2413 | * calling this function otherwise this call will fail. |
| 2414 | */ |
| 2415 | int regulator_set_mode(struct regulator *regulator, unsigned int mode) |
| 2416 | { |
| 2417 | struct regulator_dev *rdev = regulator->rdev; |
| 2418 | int ret; |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 2419 | int regulator_curr_mode; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2420 | |
| 2421 | mutex_lock(&rdev->mutex); |
| 2422 | |
| 2423 | /* sanity check */ |
| 2424 | if (!rdev->desc->ops->set_mode) { |
| 2425 | ret = -EINVAL; |
| 2426 | goto out; |
| 2427 | } |
| 2428 | |
Sundar R Iyer | 500b4ac | 2010-05-17 21:24:48 +0530 | [diff] [blame] | 2429 | /* return if the same mode is requested */ |
| 2430 | if (rdev->desc->ops->get_mode) { |
| 2431 | regulator_curr_mode = rdev->desc->ops->get_mode(rdev); |
| 2432 | if (regulator_curr_mode == mode) { |
| 2433 | ret = 0; |
| 2434 | goto out; |
| 2435 | } |
| 2436 | } |
| 2437 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2438 | /* constraints check */ |
Axel Lin | 22c51b4 | 2011-04-01 18:25:25 +0800 | [diff] [blame] | 2439 | ret = regulator_mode_constrain(rdev, &mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2440 | if (ret < 0) |
| 2441 | goto out; |
| 2442 | |
| 2443 | ret = rdev->desc->ops->set_mode(rdev, mode); |
| 2444 | out: |
| 2445 | mutex_unlock(&rdev->mutex); |
| 2446 | return ret; |
| 2447 | } |
| 2448 | EXPORT_SYMBOL_GPL(regulator_set_mode); |
| 2449 | |
| 2450 | static unsigned int _regulator_get_mode(struct regulator_dev *rdev) |
| 2451 | { |
| 2452 | int ret; |
| 2453 | |
| 2454 | mutex_lock(&rdev->mutex); |
| 2455 | |
| 2456 | /* sanity check */ |
| 2457 | if (!rdev->desc->ops->get_mode) { |
| 2458 | ret = -EINVAL; |
| 2459 | goto out; |
| 2460 | } |
| 2461 | |
| 2462 | ret = rdev->desc->ops->get_mode(rdev); |
| 2463 | out: |
| 2464 | mutex_unlock(&rdev->mutex); |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * regulator_get_mode - get regulator operating mode |
| 2470 | * @regulator: regulator source |
| 2471 | * |
| 2472 | * Get the current regulator operating mode. |
| 2473 | */ |
| 2474 | unsigned int regulator_get_mode(struct regulator *regulator) |
| 2475 | { |
| 2476 | return _regulator_get_mode(regulator->rdev); |
| 2477 | } |
| 2478 | EXPORT_SYMBOL_GPL(regulator_get_mode); |
| 2479 | |
| 2480 | /** |
| 2481 | * regulator_set_optimum_mode - set regulator optimum operating mode |
| 2482 | * @regulator: regulator source |
| 2483 | * @uA_load: load current |
| 2484 | * |
| 2485 | * Notifies the regulator core of a new device load. This is then used by |
| 2486 | * DRMS (if enabled by constraints) to set the most efficient regulator |
| 2487 | * operating mode for the new regulator loading. |
| 2488 | * |
| 2489 | * Consumer devices notify their supply regulator of the maximum power |
| 2490 | * they will require (can be taken from device datasheet in the power |
| 2491 | * consumption tables) when they change operational status and hence power |
| 2492 | * state. Examples of operational state changes that can affect power |
| 2493 | * consumption are :- |
| 2494 | * |
| 2495 | * o Device is opened / closed. |
| 2496 | * o Device I/O is about to begin or has just finished. |
| 2497 | * o Device is idling in between work. |
| 2498 | * |
| 2499 | * This information is also exported via sysfs to userspace. |
| 2500 | * |
| 2501 | * DRMS will sum the total requested load on the regulator and change |
| 2502 | * to the most efficient operating mode if platform constraints allow. |
| 2503 | * |
| 2504 | * Returns the new regulator mode or error. |
| 2505 | */ |
| 2506 | int regulator_set_optimum_mode(struct regulator *regulator, int uA_load) |
| 2507 | { |
| 2508 | struct regulator_dev *rdev = regulator->rdev; |
| 2509 | struct regulator *consumer; |
| 2510 | int ret, output_uV, input_uV, total_uA_load = 0; |
| 2511 | unsigned int mode; |
| 2512 | |
| 2513 | mutex_lock(&rdev->mutex); |
| 2514 | |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2515 | /* |
| 2516 | * first check to see if we can set modes at all, otherwise just |
| 2517 | * tell the consumer everything is OK. |
| 2518 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2519 | regulator->uA_load = uA_load; |
| 2520 | ret = regulator_check_drms(rdev); |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2521 | if (ret < 0) { |
| 2522 | ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2523 | goto out; |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2524 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2525 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2526 | if (!rdev->desc->ops->get_optimum_mode) |
| 2527 | goto out; |
| 2528 | |
Mark Brown | a4b4148 | 2011-05-14 11:19:45 -0700 | [diff] [blame] | 2529 | /* |
| 2530 | * we can actually do this so any errors are indicators of |
| 2531 | * potential real failure. |
| 2532 | */ |
| 2533 | ret = -EINVAL; |
| 2534 | |
Axel Lin | 854ccba | 2012-04-16 18:44:23 +0800 | [diff] [blame] | 2535 | if (!rdev->desc->ops->set_mode) |
| 2536 | goto out; |
| 2537 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2538 | /* get output voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 2539 | output_uV = _regulator_get_voltage(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2540 | if (output_uV <= 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2541 | rdev_err(rdev, "invalid output voltage found\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2542 | goto out; |
| 2543 | } |
| 2544 | |
| 2545 | /* get input voltage */ |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 2546 | input_uV = 0; |
| 2547 | if (rdev->supply) |
Mark Brown | 3801b86 | 2011-06-09 16:22:22 +0100 | [diff] [blame] | 2548 | input_uV = regulator_get_voltage(rdev->supply); |
Mark Brown | 1bf5a1f | 2010-12-10 17:28:06 +0000 | [diff] [blame] | 2549 | if (input_uV <= 0) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2550 | input_uV = rdev->constraints->input_uV; |
| 2551 | if (input_uV <= 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2552 | rdev_err(rdev, "invalid input voltage found\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2553 | goto out; |
| 2554 | } |
| 2555 | |
| 2556 | /* calc total requested load for this regulator */ |
| 2557 | list_for_each_entry(consumer, &rdev->consumer_list, list) |
Stefan Roese | fa2984d | 2009-11-27 15:56:34 +0100 | [diff] [blame] | 2558 | total_uA_load += consumer->uA_load; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2559 | |
| 2560 | mode = rdev->desc->ops->get_optimum_mode(rdev, |
| 2561 | input_uV, output_uV, |
| 2562 | total_uA_load); |
Mark Brown | 2c60823 | 2011-03-30 06:29:12 +0900 | [diff] [blame] | 2563 | ret = regulator_mode_constrain(rdev, &mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 2564 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2565 | rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n", |
| 2566 | total_uA_load, input_uV, output_uV); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2567 | goto out; |
| 2568 | } |
| 2569 | |
| 2570 | ret = rdev->desc->ops->set_mode(rdev, mode); |
David Brownell | e573520 | 2008-11-16 11:46:56 -0800 | [diff] [blame] | 2571 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2572 | rdev_err(rdev, "failed to set optimum mode %x\n", mode); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2573 | goto out; |
| 2574 | } |
| 2575 | ret = mode; |
| 2576 | out: |
| 2577 | mutex_unlock(&rdev->mutex); |
| 2578 | return ret; |
| 2579 | } |
| 2580 | EXPORT_SYMBOL_GPL(regulator_set_optimum_mode); |
| 2581 | |
| 2582 | /** |
| 2583 | * regulator_register_notifier - register regulator event notifier |
| 2584 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2585 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2586 | * |
| 2587 | * Register notifier block to receive regulator events. |
| 2588 | */ |
| 2589 | int regulator_register_notifier(struct regulator *regulator, |
| 2590 | struct notifier_block *nb) |
| 2591 | { |
| 2592 | return blocking_notifier_chain_register(®ulator->rdev->notifier, |
| 2593 | nb); |
| 2594 | } |
| 2595 | EXPORT_SYMBOL_GPL(regulator_register_notifier); |
| 2596 | |
| 2597 | /** |
| 2598 | * regulator_unregister_notifier - unregister regulator event notifier |
| 2599 | * @regulator: regulator source |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2600 | * @nb: notifier block |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2601 | * |
| 2602 | * Unregister regulator event notifier block. |
| 2603 | */ |
| 2604 | int regulator_unregister_notifier(struct regulator *regulator, |
| 2605 | struct notifier_block *nb) |
| 2606 | { |
| 2607 | return blocking_notifier_chain_unregister(®ulator->rdev->notifier, |
| 2608 | nb); |
| 2609 | } |
| 2610 | EXPORT_SYMBOL_GPL(regulator_unregister_notifier); |
| 2611 | |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2612 | /* notify regulator consumers and downstream regulator consumers. |
| 2613 | * Note mutex must be held by caller. |
| 2614 | */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2615 | static void _notifier_call_chain(struct regulator_dev *rdev, |
| 2616 | unsigned long event, void *data) |
| 2617 | { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2618 | /* call rdev chain first */ |
Mark Brown | d8493d2 | 2012-06-15 19:09:09 +0100 | [diff] [blame] | 2619 | blocking_notifier_call_chain(&rdev->notifier, event, data); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | /** |
| 2623 | * regulator_bulk_get - get multiple regulator consumers |
| 2624 | * |
| 2625 | * @dev: Device to supply |
| 2626 | * @num_consumers: Number of consumers to register |
| 2627 | * @consumers: Configuration of consumers; clients are stored here. |
| 2628 | * |
| 2629 | * @return 0 on success, an errno on failure. |
| 2630 | * |
| 2631 | * This helper function allows drivers to get several regulator |
| 2632 | * consumers in one operation. If any of the regulators cannot be |
| 2633 | * acquired then any regulators that were allocated will be freed |
| 2634 | * before returning to the caller. |
| 2635 | */ |
| 2636 | int regulator_bulk_get(struct device *dev, int num_consumers, |
| 2637 | struct regulator_bulk_data *consumers) |
| 2638 | { |
| 2639 | int i; |
| 2640 | int ret; |
| 2641 | |
| 2642 | for (i = 0; i < num_consumers; i++) |
| 2643 | consumers[i].consumer = NULL; |
| 2644 | |
| 2645 | for (i = 0; i < num_consumers; i++) { |
| 2646 | consumers[i].consumer = regulator_get(dev, |
| 2647 | consumers[i].supply); |
| 2648 | if (IS_ERR(consumers[i].consumer)) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2649 | ret = PTR_ERR(consumers[i].consumer); |
Mark Brown | 5b30762 | 2009-10-13 13:06:49 +0100 | [diff] [blame] | 2650 | dev_err(dev, "Failed to get supply '%s': %d\n", |
| 2651 | consumers[i].supply, ret); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2652 | consumers[i].consumer = NULL; |
| 2653 | goto err; |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | return 0; |
| 2658 | |
| 2659 | err: |
Axel Lin | b29c769 | 2012-02-20 10:32:16 +0800 | [diff] [blame] | 2660 | while (--i >= 0) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2661 | regulator_put(consumers[i].consumer); |
| 2662 | |
| 2663 | return ret; |
| 2664 | } |
| 2665 | EXPORT_SYMBOL_GPL(regulator_bulk_get); |
| 2666 | |
Mark Brown | e6e7403 | 2012-01-20 20:10:08 +0000 | [diff] [blame] | 2667 | /** |
| 2668 | * devm_regulator_bulk_get - managed get multiple regulator consumers |
| 2669 | * |
| 2670 | * @dev: Device to supply |
| 2671 | * @num_consumers: Number of consumers to register |
| 2672 | * @consumers: Configuration of consumers; clients are stored here. |
| 2673 | * |
| 2674 | * @return 0 on success, an errno on failure. |
| 2675 | * |
| 2676 | * This helper function allows drivers to get several regulator |
| 2677 | * consumers in one operation with management, the regulators will |
| 2678 | * automatically be freed when the device is unbound. If any of the |
| 2679 | * regulators cannot be acquired then any regulators that were |
| 2680 | * allocated will be freed before returning to the caller. |
| 2681 | */ |
| 2682 | int devm_regulator_bulk_get(struct device *dev, int num_consumers, |
| 2683 | struct regulator_bulk_data *consumers) |
| 2684 | { |
| 2685 | int i; |
| 2686 | int ret; |
| 2687 | |
| 2688 | for (i = 0; i < num_consumers; i++) |
| 2689 | consumers[i].consumer = NULL; |
| 2690 | |
| 2691 | for (i = 0; i < num_consumers; i++) { |
| 2692 | consumers[i].consumer = devm_regulator_get(dev, |
| 2693 | consumers[i].supply); |
| 2694 | if (IS_ERR(consumers[i].consumer)) { |
| 2695 | ret = PTR_ERR(consumers[i].consumer); |
| 2696 | dev_err(dev, "Failed to get supply '%s': %d\n", |
| 2697 | consumers[i].supply, ret); |
| 2698 | consumers[i].consumer = NULL; |
| 2699 | goto err; |
| 2700 | } |
| 2701 | } |
| 2702 | |
| 2703 | return 0; |
| 2704 | |
| 2705 | err: |
| 2706 | for (i = 0; i < num_consumers && consumers[i].consumer; i++) |
| 2707 | devm_regulator_put(consumers[i].consumer); |
| 2708 | |
| 2709 | return ret; |
| 2710 | } |
| 2711 | EXPORT_SYMBOL_GPL(devm_regulator_bulk_get); |
| 2712 | |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2713 | static void regulator_bulk_enable_async(void *data, async_cookie_t cookie) |
| 2714 | { |
| 2715 | struct regulator_bulk_data *bulk = data; |
| 2716 | |
| 2717 | bulk->ret = regulator_enable(bulk->consumer); |
| 2718 | } |
| 2719 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2720 | /** |
| 2721 | * regulator_bulk_enable - enable multiple regulator consumers |
| 2722 | * |
| 2723 | * @num_consumers: Number of consumers |
| 2724 | * @consumers: Consumer data; clients are stored here. |
| 2725 | * @return 0 on success, an errno on failure |
| 2726 | * |
| 2727 | * This convenience API allows consumers to enable multiple regulator |
| 2728 | * clients in a single API call. If any consumers cannot be enabled |
| 2729 | * then any others that were enabled will be disabled again prior to |
| 2730 | * return. |
| 2731 | */ |
| 2732 | int regulator_bulk_enable(int num_consumers, |
| 2733 | struct regulator_bulk_data *consumers) |
| 2734 | { |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2735 | LIST_HEAD(async_domain); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2736 | int i; |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2737 | int ret = 0; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2738 | |
Mark Brown | 6492bc1 | 2012-04-19 13:19:07 +0100 | [diff] [blame] | 2739 | for (i = 0; i < num_consumers; i++) { |
| 2740 | if (consumers[i].consumer->always_on) |
| 2741 | consumers[i].ret = 0; |
| 2742 | else |
| 2743 | async_schedule_domain(regulator_bulk_enable_async, |
| 2744 | &consumers[i], &async_domain); |
| 2745 | } |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2746 | |
| 2747 | async_synchronize_full_domain(&async_domain); |
| 2748 | |
| 2749 | /* If any consumer failed we need to unwind any that succeeded */ |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2750 | for (i = 0; i < num_consumers; i++) { |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2751 | if (consumers[i].ret != 0) { |
| 2752 | ret = consumers[i].ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2753 | goto err; |
Mark Brown | f21e0e8 | 2011-05-24 08:12:40 +0800 | [diff] [blame] | 2754 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | return 0; |
| 2758 | |
| 2759 | err: |
Axel Lin | b29c769 | 2012-02-20 10:32:16 +0800 | [diff] [blame] | 2760 | pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret); |
| 2761 | while (--i >= 0) |
| 2762 | regulator_disable(consumers[i].consumer); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2763 | |
| 2764 | return ret; |
| 2765 | } |
| 2766 | EXPORT_SYMBOL_GPL(regulator_bulk_enable); |
| 2767 | |
| 2768 | /** |
| 2769 | * regulator_bulk_disable - disable multiple regulator consumers |
| 2770 | * |
| 2771 | * @num_consumers: Number of consumers |
| 2772 | * @consumers: Consumer data; clients are stored here. |
| 2773 | * @return 0 on success, an errno on failure |
| 2774 | * |
| 2775 | * This convenience API allows consumers to disable multiple regulator |
Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 2776 | * clients in a single API call. If any consumers cannot be disabled |
| 2777 | * then any others that were disabled will be enabled again prior to |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2778 | * return. |
| 2779 | */ |
| 2780 | int regulator_bulk_disable(int num_consumers, |
| 2781 | struct regulator_bulk_data *consumers) |
| 2782 | { |
| 2783 | int i; |
Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 2784 | int ret, r; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2785 | |
Sylwester Nawrocki | 49e2263 | 2012-01-25 12:35:38 +0100 | [diff] [blame] | 2786 | for (i = num_consumers - 1; i >= 0; --i) { |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2787 | ret = regulator_disable(consumers[i].consumer); |
| 2788 | if (ret != 0) |
| 2789 | goto err; |
| 2790 | } |
| 2791 | |
| 2792 | return 0; |
| 2793 | |
| 2794 | err: |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 2795 | pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret); |
Mark Brown | 01e86f4 | 2012-03-28 21:36:38 +0100 | [diff] [blame] | 2796 | for (++i; i < num_consumers; ++i) { |
| 2797 | r = regulator_enable(consumers[i].consumer); |
| 2798 | if (r != 0) |
| 2799 | pr_err("Failed to reename %s: %d\n", |
| 2800 | consumers[i].supply, r); |
| 2801 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2802 | |
| 2803 | return ret; |
| 2804 | } |
| 2805 | EXPORT_SYMBOL_GPL(regulator_bulk_disable); |
| 2806 | |
| 2807 | /** |
Donggeun Kim | e1de2f4 | 2012-01-03 16:22:03 +0900 | [diff] [blame] | 2808 | * regulator_bulk_force_disable - force disable multiple regulator consumers |
| 2809 | * |
| 2810 | * @num_consumers: Number of consumers |
| 2811 | * @consumers: Consumer data; clients are stored here. |
| 2812 | * @return 0 on success, an errno on failure |
| 2813 | * |
| 2814 | * This convenience API allows consumers to forcibly disable multiple regulator |
| 2815 | * clients in a single API call. |
| 2816 | * NOTE: This should be used for situations when device damage will |
| 2817 | * likely occur if the regulators are not disabled (e.g. over temp). |
| 2818 | * Although regulator_force_disable function call for some consumers can |
| 2819 | * return error numbers, the function is called for all consumers. |
| 2820 | */ |
| 2821 | int regulator_bulk_force_disable(int num_consumers, |
| 2822 | struct regulator_bulk_data *consumers) |
| 2823 | { |
| 2824 | int i; |
| 2825 | int ret; |
| 2826 | |
| 2827 | for (i = 0; i < num_consumers; i++) |
| 2828 | consumers[i].ret = |
| 2829 | regulator_force_disable(consumers[i].consumer); |
| 2830 | |
| 2831 | for (i = 0; i < num_consumers; i++) { |
| 2832 | if (consumers[i].ret != 0) { |
| 2833 | ret = consumers[i].ret; |
| 2834 | goto out; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | return 0; |
| 2839 | out: |
| 2840 | return ret; |
| 2841 | } |
| 2842 | EXPORT_SYMBOL_GPL(regulator_bulk_force_disable); |
| 2843 | |
| 2844 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2845 | * regulator_bulk_free - free multiple regulator consumers |
| 2846 | * |
| 2847 | * @num_consumers: Number of consumers |
| 2848 | * @consumers: Consumer data; clients are stored here. |
| 2849 | * |
| 2850 | * This convenience API allows consumers to free multiple regulator |
| 2851 | * clients in a single API call. |
| 2852 | */ |
| 2853 | void regulator_bulk_free(int num_consumers, |
| 2854 | struct regulator_bulk_data *consumers) |
| 2855 | { |
| 2856 | int i; |
| 2857 | |
| 2858 | for (i = 0; i < num_consumers; i++) { |
| 2859 | regulator_put(consumers[i].consumer); |
| 2860 | consumers[i].consumer = NULL; |
| 2861 | } |
| 2862 | } |
| 2863 | EXPORT_SYMBOL_GPL(regulator_bulk_free); |
| 2864 | |
| 2865 | /** |
| 2866 | * regulator_notifier_call_chain - call regulator event notifier |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2867 | * @rdev: regulator source |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2868 | * @event: notifier block |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 2869 | * @data: callback-specific data. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2870 | * |
| 2871 | * Called by regulator drivers to notify clients a regulator event has |
| 2872 | * occurred. We also notify regulator clients downstream. |
Jonathan Cameron | b136fb4 | 2009-01-19 18:20:58 +0000 | [diff] [blame] | 2873 | * Note lock must be held by caller. |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 2874 | */ |
| 2875 | int regulator_notifier_call_chain(struct regulator_dev *rdev, |
| 2876 | unsigned long event, void *data) |
| 2877 | { |
| 2878 | _notifier_call_chain(rdev, event, data); |
| 2879 | return NOTIFY_DONE; |
| 2880 | |
| 2881 | } |
| 2882 | EXPORT_SYMBOL_GPL(regulator_notifier_call_chain); |
| 2883 | |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 2884 | /** |
| 2885 | * regulator_mode_to_status - convert a regulator mode into a status |
| 2886 | * |
| 2887 | * @mode: Mode to convert |
| 2888 | * |
| 2889 | * Convert a regulator mode into a status. |
| 2890 | */ |
| 2891 | int regulator_mode_to_status(unsigned int mode) |
| 2892 | { |
| 2893 | switch (mode) { |
| 2894 | case REGULATOR_MODE_FAST: |
| 2895 | return REGULATOR_STATUS_FAST; |
| 2896 | case REGULATOR_MODE_NORMAL: |
| 2897 | return REGULATOR_STATUS_NORMAL; |
| 2898 | case REGULATOR_MODE_IDLE: |
| 2899 | return REGULATOR_STATUS_IDLE; |
Krystian Garbaciak | 03ffcf3 | 2012-07-12 11:50:38 +0100 | [diff] [blame] | 2900 | case REGULATOR_MODE_STANDBY: |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 2901 | return REGULATOR_STATUS_STANDBY; |
| 2902 | default: |
Krystian Garbaciak | 1beaf76 | 2012-07-12 13:53:35 +0100 | [diff] [blame^] | 2903 | return REGULATOR_STATUS_UNDEFINED; |
Mark Brown | be72197 | 2009-08-04 20:09:52 +0200 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | EXPORT_SYMBOL_GPL(regulator_mode_to_status); |
| 2907 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2908 | /* |
| 2909 | * To avoid cluttering sysfs (and memory) with useless state, only |
| 2910 | * create attributes that can be meaningfully displayed. |
| 2911 | */ |
| 2912 | static int add_regulator_attributes(struct regulator_dev *rdev) |
| 2913 | { |
| 2914 | struct device *dev = &rdev->dev; |
| 2915 | struct regulator_ops *ops = rdev->desc->ops; |
| 2916 | int status = 0; |
| 2917 | |
| 2918 | /* some attributes need specific methods to be displayed */ |
Mark Brown | 4c78899 | 2011-11-02 11:39:09 +0000 | [diff] [blame] | 2919 | if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) || |
| 2920 | (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) { |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2921 | status = device_create_file(dev, &dev_attr_microvolts); |
| 2922 | if (status < 0) |
| 2923 | return status; |
| 2924 | } |
| 2925 | if (ops->get_current_limit) { |
| 2926 | status = device_create_file(dev, &dev_attr_microamps); |
| 2927 | if (status < 0) |
| 2928 | return status; |
| 2929 | } |
| 2930 | if (ops->get_mode) { |
| 2931 | status = device_create_file(dev, &dev_attr_opmode); |
| 2932 | if (status < 0) |
| 2933 | return status; |
| 2934 | } |
| 2935 | if (ops->is_enabled) { |
| 2936 | status = device_create_file(dev, &dev_attr_state); |
| 2937 | if (status < 0) |
| 2938 | return status; |
| 2939 | } |
David Brownell | 853116a | 2009-01-14 23:03:17 -0800 | [diff] [blame] | 2940 | if (ops->get_status) { |
| 2941 | status = device_create_file(dev, &dev_attr_status); |
| 2942 | if (status < 0) |
| 2943 | return status; |
| 2944 | } |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2945 | |
| 2946 | /* some attributes are type-specific */ |
| 2947 | if (rdev->desc->type == REGULATOR_CURRENT) { |
| 2948 | status = device_create_file(dev, &dev_attr_requested_microamps); |
| 2949 | if (status < 0) |
| 2950 | return status; |
| 2951 | } |
| 2952 | |
| 2953 | /* all the other attributes exist to support constraints; |
| 2954 | * don't show them if there are no constraints, or if the |
| 2955 | * relevant supporting methods are missing. |
| 2956 | */ |
| 2957 | if (!rdev->constraints) |
| 2958 | return status; |
| 2959 | |
| 2960 | /* constraints need specific supporting methods */ |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 2961 | if (ops->set_voltage || ops->set_voltage_sel) { |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2962 | status = device_create_file(dev, &dev_attr_min_microvolts); |
| 2963 | if (status < 0) |
| 2964 | return status; |
| 2965 | status = device_create_file(dev, &dev_attr_max_microvolts); |
| 2966 | if (status < 0) |
| 2967 | return status; |
| 2968 | } |
| 2969 | if (ops->set_current_limit) { |
| 2970 | status = device_create_file(dev, &dev_attr_min_microamps); |
| 2971 | if (status < 0) |
| 2972 | return status; |
| 2973 | status = device_create_file(dev, &dev_attr_max_microamps); |
| 2974 | if (status < 0) |
| 2975 | return status; |
| 2976 | } |
| 2977 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 2978 | status = device_create_file(dev, &dev_attr_suspend_standby_state); |
| 2979 | if (status < 0) |
| 2980 | return status; |
| 2981 | status = device_create_file(dev, &dev_attr_suspend_mem_state); |
| 2982 | if (status < 0) |
| 2983 | return status; |
| 2984 | status = device_create_file(dev, &dev_attr_suspend_disk_state); |
| 2985 | if (status < 0) |
| 2986 | return status; |
| 2987 | |
| 2988 | if (ops->set_suspend_voltage) { |
| 2989 | status = device_create_file(dev, |
| 2990 | &dev_attr_suspend_standby_microvolts); |
| 2991 | if (status < 0) |
| 2992 | return status; |
| 2993 | status = device_create_file(dev, |
| 2994 | &dev_attr_suspend_mem_microvolts); |
| 2995 | if (status < 0) |
| 2996 | return status; |
| 2997 | status = device_create_file(dev, |
| 2998 | &dev_attr_suspend_disk_microvolts); |
| 2999 | if (status < 0) |
| 3000 | return status; |
| 3001 | } |
| 3002 | |
| 3003 | if (ops->set_suspend_mode) { |
| 3004 | status = device_create_file(dev, |
| 3005 | &dev_attr_suspend_standby_mode); |
| 3006 | if (status < 0) |
| 3007 | return status; |
| 3008 | status = device_create_file(dev, |
| 3009 | &dev_attr_suspend_mem_mode); |
| 3010 | if (status < 0) |
| 3011 | return status; |
| 3012 | status = device_create_file(dev, |
| 3013 | &dev_attr_suspend_disk_mode); |
| 3014 | if (status < 0) |
| 3015 | return status; |
| 3016 | } |
| 3017 | |
| 3018 | return status; |
| 3019 | } |
| 3020 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3021 | static void rdev_init_debugfs(struct regulator_dev *rdev) |
| 3022 | { |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3023 | rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root); |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 3024 | if (!rdev->debugfs) { |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3025 | rdev_warn(rdev, "Failed to create debugfs directory\n"); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3026 | return; |
| 3027 | } |
| 3028 | |
| 3029 | debugfs_create_u32("use_count", 0444, rdev->debugfs, |
| 3030 | &rdev->use_count); |
| 3031 | debugfs_create_u32("open_count", 0444, rdev->debugfs, |
| 3032 | &rdev->open_count); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3035 | /** |
| 3036 | * regulator_register - register regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3037 | * @regulator_desc: regulator to register |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3038 | * @config: runtime configuration for regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3039 | * |
| 3040 | * Called by regulator drivers to register a regulator. |
| 3041 | * Returns 0 on success. |
| 3042 | */ |
Mark Brown | 65f2684 | 2012-04-03 20:46:53 +0100 | [diff] [blame] | 3043 | struct regulator_dev * |
| 3044 | regulator_register(const struct regulator_desc *regulator_desc, |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3045 | const struct regulator_config *config) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3046 | { |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3047 | const struct regulation_constraints *constraints = NULL; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3048 | const struct regulator_init_data *init_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3049 | static atomic_t regulator_no = ATOMIC_INIT(0); |
| 3050 | struct regulator_dev *rdev; |
Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 3051 | struct device *dev; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3052 | int ret, i; |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 3053 | const char *supply = NULL; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3054 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3055 | if (regulator_desc == NULL || config == NULL) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3056 | return ERR_PTR(-EINVAL); |
| 3057 | |
Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 3058 | dev = config->dev; |
Mark Brown | dcf7011 | 2012-05-08 18:09:12 +0100 | [diff] [blame] | 3059 | WARN_ON(!dev); |
Mark Brown | 32c8fad | 2012-04-11 10:19:12 +0100 | [diff] [blame] | 3060 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3061 | if (regulator_desc->name == NULL || regulator_desc->ops == NULL) |
| 3062 | return ERR_PTR(-EINVAL); |
| 3063 | |
Diego Liziero | cd78dfc | 2009-04-14 03:04:47 +0200 | [diff] [blame] | 3064 | if (regulator_desc->type != REGULATOR_VOLTAGE && |
| 3065 | regulator_desc->type != REGULATOR_CURRENT) |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3066 | return ERR_PTR(-EINVAL); |
| 3067 | |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3068 | /* Only one of each should be implemented */ |
| 3069 | WARN_ON(regulator_desc->ops->get_voltage && |
| 3070 | regulator_desc->ops->get_voltage_sel); |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 3071 | WARN_ON(regulator_desc->ops->set_voltage && |
| 3072 | regulator_desc->ops->set_voltage_sel); |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3073 | |
| 3074 | /* If we're using selectors we must implement list_voltage. */ |
| 3075 | if (regulator_desc->ops->get_voltage_sel && |
| 3076 | !regulator_desc->ops->list_voltage) { |
| 3077 | return ERR_PTR(-EINVAL); |
| 3078 | } |
Mark Brown | e8eef82 | 2010-12-12 14:36:17 +0000 | [diff] [blame] | 3079 | if (regulator_desc->ops->set_voltage_sel && |
| 3080 | !regulator_desc->ops->list_voltage) { |
| 3081 | return ERR_PTR(-EINVAL); |
| 3082 | } |
Mark Brown | 476c2d8 | 2010-12-10 17:28:07 +0000 | [diff] [blame] | 3083 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3084 | init_data = config->init_data; |
| 3085 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3086 | rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL); |
| 3087 | if (rdev == NULL) |
| 3088 | return ERR_PTR(-ENOMEM); |
| 3089 | |
| 3090 | mutex_lock(®ulator_list_mutex); |
| 3091 | |
| 3092 | mutex_init(&rdev->mutex); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3093 | rdev->reg_data = config->driver_data; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3094 | rdev->owner = regulator_desc->owner; |
| 3095 | rdev->desc = regulator_desc; |
Mark Brown | 3a4b0a0 | 2012-05-08 18:10:45 +0100 | [diff] [blame] | 3096 | if (config->regmap) |
| 3097 | rdev->regmap = config->regmap; |
| 3098 | else |
| 3099 | rdev->regmap = dev_get_regmap(dev, NULL); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3100 | INIT_LIST_HEAD(&rdev->consumer_list); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3101 | INIT_LIST_HEAD(&rdev->list); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3102 | BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier); |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 3103 | INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3104 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3105 | /* preform any regulator specific init */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3106 | if (init_data && init_data->regulator_init) { |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3107 | ret = init_data->regulator_init(rdev->reg_data); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3108 | if (ret < 0) |
| 3109 | goto clean; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3110 | } |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3111 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3112 | /* register with sysfs */ |
| 3113 | rdev->dev.class = ®ulator_class; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 3114 | rdev->dev.of_node = config->of_node; |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3115 | rdev->dev.parent = dev; |
Kay Sievers | 812460a | 2008-11-02 03:55:10 +0100 | [diff] [blame] | 3116 | dev_set_name(&rdev->dev, "regulator.%d", |
| 3117 | atomic_inc_return(®ulator_no) - 1); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3118 | ret = device_register(&rdev->dev); |
Vasiliy Kulikov | ad7725c | 2010-09-19 16:55:01 +0400 | [diff] [blame] | 3119 | if (ret != 0) { |
| 3120 | put_device(&rdev->dev); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3121 | goto clean; |
Vasiliy Kulikov | ad7725c | 2010-09-19 16:55:01 +0400 | [diff] [blame] | 3122 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3123 | |
| 3124 | dev_set_drvdata(&rdev->dev, rdev); |
| 3125 | |
Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 3126 | /* set regulator constraints */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3127 | if (init_data) |
| 3128 | constraints = &init_data->constraints; |
| 3129 | |
| 3130 | ret = set_machine_constraints(rdev, constraints); |
Mike Rapoport | 74f544c | 2008-11-25 14:53:53 +0200 | [diff] [blame] | 3131 | if (ret < 0) |
| 3132 | goto scrub; |
| 3133 | |
David Brownell | 7ad68e2 | 2008-11-11 17:39:02 -0800 | [diff] [blame] | 3134 | /* add attributes supported by this regulator */ |
| 3135 | ret = add_regulator_attributes(rdev); |
| 3136 | if (ret < 0) |
| 3137 | goto scrub; |
| 3138 | |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3139 | if (init_data && init_data->supply_regulator) |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 3140 | supply = init_data->supply_regulator; |
| 3141 | else if (regulator_desc->supply_name) |
| 3142 | supply = regulator_desc->supply_name; |
| 3143 | |
| 3144 | if (supply) { |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3145 | struct regulator_dev *r; |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3146 | |
Mark Brown | 6d191a5 | 2012-03-29 14:19:02 +0100 | [diff] [blame] | 3147 | r = regulator_dev_lookup(dev, supply, &ret); |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3148 | |
Rajendra Nayak | 69511a4 | 2011-11-18 16:47:20 +0530 | [diff] [blame] | 3149 | if (!r) { |
| 3150 | dev_err(dev, "Failed to find supply %s\n", supply); |
Mark Brown | 04bf301 | 2012-03-11 13:07:56 +0000 | [diff] [blame] | 3151 | ret = -EPROBE_DEFER; |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3152 | goto scrub; |
| 3153 | } |
| 3154 | |
| 3155 | ret = set_supply(rdev, r); |
| 3156 | if (ret < 0) |
| 3157 | goto scrub; |
Laxman Dewangan | b2296bd | 2012-01-02 13:08:45 +0530 | [diff] [blame] | 3158 | |
| 3159 | /* Enable supply if rail is enabled */ |
Mark Brown | b1a8683 | 2012-05-14 00:40:14 +0100 | [diff] [blame] | 3160 | if (_regulator_is_enabled(rdev)) { |
Laxman Dewangan | b2296bd | 2012-01-02 13:08:45 +0530 | [diff] [blame] | 3161 | ret = regulator_enable(rdev->supply); |
| 3162 | if (ret < 0) |
| 3163 | goto scrub; |
| 3164 | } |
Mark Brown | 0178f3e | 2010-04-26 15:18:14 +0100 | [diff] [blame] | 3165 | } |
| 3166 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3167 | /* add consumers devices */ |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3168 | if (init_data) { |
| 3169 | for (i = 0; i < init_data->num_consumer_supplies; i++) { |
| 3170 | ret = set_consumer_device_supply(rdev, |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3171 | init_data->consumer_supplies[i].dev_name, |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 3172 | init_data->consumer_supplies[i].supply); |
Mark Brown | 9a8f5e0 | 2011-11-29 18:11:19 +0000 | [diff] [blame] | 3173 | if (ret < 0) { |
| 3174 | dev_err(dev, "Failed to set supply %s\n", |
| 3175 | init_data->consumer_supplies[i].supply); |
| 3176 | goto unset_supplies; |
| 3177 | } |
Mark Brown | 23c2f04 | 2011-02-24 17:39:09 +0000 | [diff] [blame] | 3178 | } |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | list_add(&rdev->list, ®ulator_list); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3182 | |
| 3183 | rdev_init_debugfs(rdev); |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3184 | out: |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3185 | mutex_unlock(®ulator_list_mutex); |
| 3186 | return rdev; |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3187 | |
Jani Nikula | d4033b5 | 2010-04-29 10:55:11 +0300 | [diff] [blame] | 3188 | unset_supplies: |
| 3189 | unset_regulator_supplies(rdev); |
| 3190 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3191 | scrub: |
Mark Brown | e81dba85 | 2012-05-13 18:35:56 +0100 | [diff] [blame] | 3192 | if (rdev->supply) |
| 3193 | regulator_put(rdev->supply); |
Axel Lin | 1a6958e7 | 2011-07-15 10:50:43 +0800 | [diff] [blame] | 3194 | kfree(rdev->constraints); |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3195 | device_unregister(&rdev->dev); |
Paul Walmsley | 53032da | 2009-04-25 05:28:36 -0600 | [diff] [blame] | 3196 | /* device core frees rdev */ |
| 3197 | rdev = ERR_PTR(ret); |
| 3198 | goto out; |
| 3199 | |
David Brownell | 4fca954 | 2008-11-11 17:38:53 -0800 | [diff] [blame] | 3200 | clean: |
| 3201 | kfree(rdev); |
| 3202 | rdev = ERR_PTR(ret); |
| 3203 | goto out; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3204 | } |
| 3205 | EXPORT_SYMBOL_GPL(regulator_register); |
| 3206 | |
| 3207 | /** |
| 3208 | * regulator_unregister - unregister regulator |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3209 | * @rdev: regulator to unregister |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3210 | * |
| 3211 | * Called by regulator drivers to unregister a regulator. |
| 3212 | */ |
| 3213 | void regulator_unregister(struct regulator_dev *rdev) |
| 3214 | { |
| 3215 | if (rdev == NULL) |
| 3216 | return; |
| 3217 | |
Mark Brown | e032b37 | 2012-03-28 21:17:55 +0100 | [diff] [blame] | 3218 | if (rdev->supply) |
| 3219 | regulator_put(rdev->supply); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3220 | mutex_lock(®ulator_list_mutex); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3221 | debugfs_remove_recursive(rdev->debugfs); |
Mark Brown | da07ecd | 2011-09-11 09:53:50 +0100 | [diff] [blame] | 3222 | flush_work_sync(&rdev->disable_work.work); |
Mark Brown | 6bf87d1 | 2009-07-21 16:00:25 +0100 | [diff] [blame] | 3223 | WARN_ON(rdev->open_count); |
Mike Rapoport | 0f1d747 | 2009-01-22 16:00:29 +0200 | [diff] [blame] | 3224 | unset_regulator_supplies(rdev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3225 | list_del(&rdev->list); |
Mark Brown | f8c12fe | 2010-11-29 15:55:17 +0000 | [diff] [blame] | 3226 | kfree(rdev->constraints); |
Lothar Waßmann | 58fb5cf | 2011-11-28 15:38:37 +0100 | [diff] [blame] | 3227 | device_unregister(&rdev->dev); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3228 | mutex_unlock(®ulator_list_mutex); |
| 3229 | } |
| 3230 | EXPORT_SYMBOL_GPL(regulator_unregister); |
| 3231 | |
| 3232 | /** |
Mark Brown | cf7bbcd | 2008-12-31 12:52:43 +0000 | [diff] [blame] | 3233 | * regulator_suspend_prepare - prepare regulators for system wide suspend |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3234 | * @state: system suspend state |
| 3235 | * |
| 3236 | * Configure each regulator with it's suspend operating parameters for state. |
| 3237 | * This will usually be called by machine suspend code prior to supending. |
| 3238 | */ |
| 3239 | int regulator_suspend_prepare(suspend_state_t state) |
| 3240 | { |
| 3241 | struct regulator_dev *rdev; |
| 3242 | int ret = 0; |
| 3243 | |
| 3244 | /* ON is handled by regulator active state */ |
| 3245 | if (state == PM_SUSPEND_ON) |
| 3246 | return -EINVAL; |
| 3247 | |
| 3248 | mutex_lock(®ulator_list_mutex); |
| 3249 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3250 | |
| 3251 | mutex_lock(&rdev->mutex); |
| 3252 | ret = suspend_prepare(rdev, state); |
| 3253 | mutex_unlock(&rdev->mutex); |
| 3254 | |
| 3255 | if (ret < 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3256 | rdev_err(rdev, "failed to prepare\n"); |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3257 | goto out; |
| 3258 | } |
| 3259 | } |
| 3260 | out: |
| 3261 | mutex_unlock(®ulator_list_mutex); |
| 3262 | return ret; |
| 3263 | } |
| 3264 | EXPORT_SYMBOL_GPL(regulator_suspend_prepare); |
| 3265 | |
| 3266 | /** |
MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 3267 | * regulator_suspend_finish - resume regulators from system wide suspend |
| 3268 | * |
| 3269 | * Turn on regulators that might be turned off by regulator_suspend_prepare |
| 3270 | * and that should be turned on according to the regulators properties. |
| 3271 | */ |
| 3272 | int regulator_suspend_finish(void) |
| 3273 | { |
| 3274 | struct regulator_dev *rdev; |
| 3275 | int ret = 0, error; |
| 3276 | |
| 3277 | mutex_lock(®ulator_list_mutex); |
| 3278 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3279 | struct regulator_ops *ops = rdev->desc->ops; |
| 3280 | |
| 3281 | mutex_lock(&rdev->mutex); |
| 3282 | if ((rdev->use_count > 0 || rdev->constraints->always_on) && |
| 3283 | ops->enable) { |
| 3284 | error = ops->enable(rdev); |
| 3285 | if (error) |
| 3286 | ret = error; |
| 3287 | } else { |
| 3288 | if (!has_full_constraints) |
| 3289 | goto unlock; |
| 3290 | if (!ops->disable) |
| 3291 | goto unlock; |
Mark Brown | b1a8683 | 2012-05-14 00:40:14 +0100 | [diff] [blame] | 3292 | if (!_regulator_is_enabled(rdev)) |
MyungJoo Ham | 7a32b58 | 2011-03-11 10:13:59 +0900 | [diff] [blame] | 3293 | goto unlock; |
| 3294 | |
| 3295 | error = ops->disable(rdev); |
| 3296 | if (error) |
| 3297 | ret = error; |
| 3298 | } |
| 3299 | unlock: |
| 3300 | mutex_unlock(&rdev->mutex); |
| 3301 | } |
| 3302 | mutex_unlock(®ulator_list_mutex); |
| 3303 | return ret; |
| 3304 | } |
| 3305 | EXPORT_SYMBOL_GPL(regulator_suspend_finish); |
| 3306 | |
| 3307 | /** |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3308 | * regulator_has_full_constraints - the system has fully specified constraints |
| 3309 | * |
| 3310 | * Calling this function will cause the regulator API to disable all |
| 3311 | * regulators which have a zero use count and don't have an always_on |
| 3312 | * constraint in a late_initcall. |
| 3313 | * |
| 3314 | * The intention is that this will become the default behaviour in a |
| 3315 | * future kernel release so users are encouraged to use this facility |
| 3316 | * now. |
| 3317 | */ |
| 3318 | void regulator_has_full_constraints(void) |
| 3319 | { |
| 3320 | has_full_constraints = 1; |
| 3321 | } |
| 3322 | EXPORT_SYMBOL_GPL(regulator_has_full_constraints); |
| 3323 | |
| 3324 | /** |
Mark Brown | 688fe99 | 2010-10-05 19:18:32 -0700 | [diff] [blame] | 3325 | * regulator_use_dummy_regulator - Provide a dummy regulator when none is found |
| 3326 | * |
| 3327 | * Calling this function will cause the regulator API to provide a |
| 3328 | * dummy regulator to consumers if no physical regulator is found, |
| 3329 | * allowing most consumers to proceed as though a regulator were |
| 3330 | * configured. This allows systems such as those with software |
| 3331 | * controllable regulators for the CPU core only to be brought up more |
| 3332 | * readily. |
| 3333 | */ |
| 3334 | void regulator_use_dummy_regulator(void) |
| 3335 | { |
| 3336 | board_wants_dummy_regulator = true; |
| 3337 | } |
| 3338 | EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator); |
| 3339 | |
| 3340 | /** |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3341 | * rdev_get_drvdata - get rdev regulator driver data |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3342 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3343 | * |
| 3344 | * Get rdev regulator driver private data. This call can be used in the |
| 3345 | * regulator driver context. |
| 3346 | */ |
| 3347 | void *rdev_get_drvdata(struct regulator_dev *rdev) |
| 3348 | { |
| 3349 | return rdev->reg_data; |
| 3350 | } |
| 3351 | EXPORT_SYMBOL_GPL(rdev_get_drvdata); |
| 3352 | |
| 3353 | /** |
| 3354 | * regulator_get_drvdata - get regulator driver data |
| 3355 | * @regulator: regulator |
| 3356 | * |
| 3357 | * Get regulator driver private data. This call can be used in the consumer |
| 3358 | * driver context when non API regulator specific functions need to be called. |
| 3359 | */ |
| 3360 | void *regulator_get_drvdata(struct regulator *regulator) |
| 3361 | { |
| 3362 | return regulator->rdev->reg_data; |
| 3363 | } |
| 3364 | EXPORT_SYMBOL_GPL(regulator_get_drvdata); |
| 3365 | |
| 3366 | /** |
| 3367 | * regulator_set_drvdata - set regulator driver data |
| 3368 | * @regulator: regulator |
| 3369 | * @data: data |
| 3370 | */ |
| 3371 | void regulator_set_drvdata(struct regulator *regulator, void *data) |
| 3372 | { |
| 3373 | regulator->rdev->reg_data = data; |
| 3374 | } |
| 3375 | EXPORT_SYMBOL_GPL(regulator_set_drvdata); |
| 3376 | |
| 3377 | /** |
| 3378 | * regulator_get_id - get regulator ID |
Mark Brown | 69279fb | 2008-12-31 12:52:41 +0000 | [diff] [blame] | 3379 | * @rdev: regulator |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3380 | */ |
| 3381 | int rdev_get_id(struct regulator_dev *rdev) |
| 3382 | { |
| 3383 | return rdev->desc->id; |
| 3384 | } |
| 3385 | EXPORT_SYMBOL_GPL(rdev_get_id); |
| 3386 | |
Liam Girdwood | a5766f1 | 2008-10-10 13:22:20 +0100 | [diff] [blame] | 3387 | struct device *rdev_get_dev(struct regulator_dev *rdev) |
| 3388 | { |
| 3389 | return &rdev->dev; |
| 3390 | } |
| 3391 | EXPORT_SYMBOL_GPL(rdev_get_dev); |
| 3392 | |
| 3393 | void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data) |
| 3394 | { |
| 3395 | return reg_init_data->driver_data; |
| 3396 | } |
| 3397 | EXPORT_SYMBOL_GPL(regulator_get_init_drvdata); |
| 3398 | |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3399 | #ifdef CONFIG_DEBUG_FS |
| 3400 | static ssize_t supply_map_read_file(struct file *file, char __user *user_buf, |
| 3401 | size_t count, loff_t *ppos) |
| 3402 | { |
| 3403 | char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 3404 | ssize_t len, ret = 0; |
| 3405 | struct regulator_map *map; |
| 3406 | |
| 3407 | if (!buf) |
| 3408 | return -ENOMEM; |
| 3409 | |
| 3410 | list_for_each_entry(map, ®ulator_map_list, list) { |
| 3411 | len = snprintf(buf + ret, PAGE_SIZE - ret, |
| 3412 | "%s -> %s.%s\n", |
| 3413 | rdev_get_name(map->regulator), map->dev_name, |
| 3414 | map->supply); |
| 3415 | if (len >= 0) |
| 3416 | ret += len; |
| 3417 | if (ret > PAGE_SIZE) { |
| 3418 | ret = PAGE_SIZE; |
| 3419 | break; |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret); |
| 3424 | |
| 3425 | kfree(buf); |
| 3426 | |
| 3427 | return ret; |
| 3428 | } |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 3429 | #endif |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3430 | |
| 3431 | static const struct file_operations supply_map_fops = { |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 3432 | #ifdef CONFIG_DEBUG_FS |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3433 | .read = supply_map_read_file, |
| 3434 | .llseek = default_llseek, |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3435 | #endif |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 3436 | }; |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3437 | |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3438 | static int __init regulator_init(void) |
| 3439 | { |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3440 | int ret; |
| 3441 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3442 | ret = class_register(®ulator_class); |
| 3443 | |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3444 | debugfs_root = debugfs_create_dir("regulator", NULL); |
Stephen Boyd | 2475143 | 2012-02-20 22:50:42 -0800 | [diff] [blame] | 3445 | if (!debugfs_root) |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3446 | pr_warn("regulator: Failed to create debugfs directory\n"); |
Mark Brown | ba55a97 | 2011-08-23 17:39:10 +0100 | [diff] [blame] | 3447 | |
Mark Brown | f4d562c | 2012-02-20 21:01:04 +0000 | [diff] [blame] | 3448 | debugfs_create_file("supply_map", 0444, debugfs_root, NULL, |
| 3449 | &supply_map_fops); |
Mark Brown | 1130e5b | 2010-12-21 23:49:31 +0000 | [diff] [blame] | 3450 | |
Mark Brown | 34abbd6 | 2010-02-12 10:18:08 +0000 | [diff] [blame] | 3451 | regulator_dummy_init(); |
| 3452 | |
| 3453 | return ret; |
Liam Girdwood | 414c70c | 2008-04-30 15:59:04 +0100 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | /* init early to allow our consumers to complete system booting */ |
| 3457 | core_initcall(regulator_init); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3458 | |
| 3459 | static int __init regulator_init_complete(void) |
| 3460 | { |
| 3461 | struct regulator_dev *rdev; |
| 3462 | struct regulator_ops *ops; |
| 3463 | struct regulation_constraints *c; |
| 3464 | int enabled, ret; |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3465 | |
| 3466 | mutex_lock(®ulator_list_mutex); |
| 3467 | |
| 3468 | /* If we have a full configuration then disable any regulators |
| 3469 | * which are not in use or always_on. This will become the |
| 3470 | * default behaviour in the future. |
| 3471 | */ |
| 3472 | list_for_each_entry(rdev, ®ulator_list, list) { |
| 3473 | ops = rdev->desc->ops; |
| 3474 | c = rdev->constraints; |
| 3475 | |
Mark Brown | f25e0b4 | 2009-08-03 18:49:55 +0100 | [diff] [blame] | 3476 | if (!ops->disable || (c && c->always_on)) |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3477 | continue; |
| 3478 | |
| 3479 | mutex_lock(&rdev->mutex); |
| 3480 | |
| 3481 | if (rdev->use_count) |
| 3482 | goto unlock; |
| 3483 | |
| 3484 | /* If we can't read the status assume it's on. */ |
| 3485 | if (ops->is_enabled) |
| 3486 | enabled = ops->is_enabled(rdev); |
| 3487 | else |
| 3488 | enabled = 1; |
| 3489 | |
| 3490 | if (!enabled) |
| 3491 | goto unlock; |
| 3492 | |
| 3493 | if (has_full_constraints) { |
| 3494 | /* We log since this may kill the system if it |
| 3495 | * goes wrong. */ |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3496 | rdev_info(rdev, "disabling\n"); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3497 | ret = ops->disable(rdev); |
| 3498 | if (ret != 0) { |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3499 | rdev_err(rdev, "couldn't disable: %d\n", ret); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3500 | } |
| 3501 | } else { |
| 3502 | /* The intention is that in future we will |
| 3503 | * assume that full constraints are provided |
| 3504 | * so warn even if we aren't going to do |
| 3505 | * anything here. |
| 3506 | */ |
Joe Perches | 5da84fd | 2010-11-30 05:53:48 -0800 | [diff] [blame] | 3507 | rdev_warn(rdev, "incomplete constraints, leaving on\n"); |
Mark Brown | ca72556 | 2009-03-16 19:36:34 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | unlock: |
| 3511 | mutex_unlock(&rdev->mutex); |
| 3512 | } |
| 3513 | |
| 3514 | mutex_unlock(®ulator_list_mutex); |
| 3515 | |
| 3516 | return 0; |
| 3517 | } |
| 3518 | late_initcall(regulator_init_complete); |