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