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