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