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