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