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