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