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