blob: 71d6adc4eeab377910ac8118de2495cb87040978 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
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 Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Mark Brown65f73502012-06-27 14:14:38 +010026#include <linux/gpio.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053027#include <linux/of.h>
Mark Brown65b19ce2012-04-15 11:16:05 +010028#include <linux/regmap.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#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 Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Kim, Milof19b00d2013-02-18 06:50:39 +000054static LIST_HEAD(regulator_ena_gpio_list);
Mark Brown21cf8912010-12-21 23:30:07 +000055static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070056static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010057
Mark Brown1130e5b2010-12-21 23:49:31 +000058static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000059
Mark Brown8dc53902008-12-31 12:52:40 +000060/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010061 * struct regulator_map
62 *
63 * Used to provide symbolic supply names to devices.
64 */
65struct regulator_map {
66 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010067 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010068 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010069 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010070};
71
Liam Girdwood414c70c2008-04-30 15:59:04 +010072/*
Kim, Milof19b00d2013-02-18 06:50:39 +000073 * struct regulator_enable_gpio
74 *
75 * Management for shared enable GPIO pin
76 */
77struct regulator_enable_gpio {
78 struct list_head list;
79 int gpio;
80 u32 enable_count; /* a number of enabled shared GPIO */
81 u32 request_count; /* a number of requested shared GPIO */
82 unsigned int ena_gpio_invert:1;
83};
84
85/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010086 * struct regulator
87 *
88 * One for each consumer device.
89 */
90struct regulator {
91 struct device *dev;
92 struct list_head list;
Mark Brown6492bc12012-04-19 13:19:07 +010093 unsigned int always_on:1;
Mark Brownf59c8f92012-08-31 10:36:37 -070094 unsigned int bypass:1;
Liam Girdwood414c70c2008-04-30 15:59:04 +010095 int uA_load;
96 int min_uV;
97 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010098 char *supply_name;
99 struct device_attribute dev_attr;
100 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +0100101 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102};
103
104static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +0100105static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100106static int _regulator_get_voltage(struct regulator_dev *rdev);
107static int _regulator_get_current_limit(struct regulator_dev *rdev);
108static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
109static void _notifier_call_chain(struct regulator_dev *rdev,
110 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +0000111static int _regulator_do_set_voltage(struct regulator_dev *rdev,
112 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +0100113static struct regulator *create_regulator(struct regulator_dev *rdev,
114 struct device *dev,
115 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100116
Mark Brown1083c392009-10-22 16:31:32 +0100117static const char *rdev_get_name(struct regulator_dev *rdev)
118{
119 if (rdev->constraints && rdev->constraints->name)
120 return rdev->constraints->name;
121 else if (rdev->desc->name)
122 return rdev->desc->name;
123 else
124 return "";
125}
126
Rajendra Nayak69511a42011-11-18 16:47:20 +0530127/**
128 * of_get_regulator - get a regulator device node based on supply name
129 * @dev: Device pointer for the consumer (of regulator) device
130 * @supply: regulator supply name
131 *
132 * Extract the regulator device node corresponding to the supply name.
133 * retruns the device node corresponding to the regulator if found, else
134 * returns NULL.
135 */
136static struct device_node *of_get_regulator(struct device *dev, const char *supply)
137{
138 struct device_node *regnode = NULL;
139 char prop_name[32]; /* 32 is max size of property name */
140
141 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
142
143 snprintf(prop_name, 32, "%s-supply", supply);
144 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
145
146 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530147 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530148 prop_name, dev->of_node->full_name);
149 return NULL;
150 }
151 return regnode;
152}
153
Mark Brown6492bc12012-04-19 13:19:07 +0100154static int _regulator_can_change_status(struct regulator_dev *rdev)
155{
156 if (!rdev->constraints)
157 return 0;
158
159 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
160 return 1;
161 else
162 return 0;
163}
164
Liam Girdwood414c70c2008-04-30 15:59:04 +0100165/* Platform voltage constraint check */
166static int regulator_check_voltage(struct regulator_dev *rdev,
167 int *min_uV, int *max_uV)
168{
169 BUG_ON(*min_uV > *max_uV);
170
171 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800172 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173 return -ENODEV;
174 }
175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800176 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100177 return -EPERM;
178 }
179
180 if (*max_uV > rdev->constraints->max_uV)
181 *max_uV = rdev->constraints->max_uV;
182 if (*min_uV < rdev->constraints->min_uV)
183 *min_uV = rdev->constraints->min_uV;
184
Mark Brown89f425e2011-07-12 11:20:37 +0900185 if (*min_uV > *max_uV) {
186 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100187 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100188 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900189 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100190
191 return 0;
192}
193
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100194/* Make sure we select a voltage that suits the needs of all
195 * regulator consumers
196 */
197static int regulator_check_consumers(struct regulator_dev *rdev,
198 int *min_uV, int *max_uV)
199{
200 struct regulator *regulator;
201
202 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700203 /*
204 * Assume consumers that didn't say anything are OK
205 * with anything in the constraint range.
206 */
207 if (!regulator->min_uV && !regulator->max_uV)
208 continue;
209
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100210 if (*max_uV > regulator->max_uV)
211 *max_uV = regulator->max_uV;
212 if (*min_uV < regulator->min_uV)
213 *min_uV = regulator->min_uV;
214 }
215
Mark Browndd8004a2012-11-28 17:09:27 +0000216 if (*min_uV > *max_uV) {
Russ Dill9c7b4e82013-02-14 04:46:33 -0800217 rdev_err(rdev, "Restricting voltage, %u-%uuV\n",
218 *min_uV, *max_uV);
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100219 return -EINVAL;
Mark Browndd8004a2012-11-28 17:09:27 +0000220 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100221
222 return 0;
223}
224
Liam Girdwood414c70c2008-04-30 15:59:04 +0100225/* current constraint check */
226static int regulator_check_current_limit(struct regulator_dev *rdev,
227 int *min_uA, int *max_uA)
228{
229 BUG_ON(*min_uA > *max_uA);
230
231 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800232 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -ENODEV;
234 }
235 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800236 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237 return -EPERM;
238 }
239
240 if (*max_uA > rdev->constraints->max_uA)
241 *max_uA = rdev->constraints->max_uA;
242 if (*min_uA < rdev->constraints->min_uA)
243 *min_uA = rdev->constraints->min_uA;
244
Mark Brown89f425e2011-07-12 11:20:37 +0900245 if (*min_uA > *max_uA) {
246 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100247 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100248 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900249 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250
251 return 0;
252}
253
254/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900255static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100256{
Mark Brown2c608232011-03-30 06:29:12 +0900257 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800258 case REGULATOR_MODE_FAST:
259 case REGULATOR_MODE_NORMAL:
260 case REGULATOR_MODE_IDLE:
261 case REGULATOR_MODE_STANDBY:
262 break;
263 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900264 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800265 return -EINVAL;
266 }
267
Liam Girdwood414c70c2008-04-30 15:59:04 +0100268 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800269 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 return -ENODEV;
271 }
272 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800273 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100274 return -EPERM;
275 }
Mark Brown2c608232011-03-30 06:29:12 +0900276
277 /* The modes are bitmasks, the most power hungry modes having
278 * the lowest values. If the requested mode isn't supported
279 * try higher modes. */
280 while (*mode) {
281 if (rdev->constraints->valid_modes_mask & *mode)
282 return 0;
283 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 }
Mark Brown2c608232011-03-30 06:29:12 +0900285
286 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100287}
288
289/* dynamic regulator mode switching constraint check */
290static int regulator_check_drms(struct regulator_dev *rdev)
291{
292 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800293 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294 return -ENODEV;
295 }
296 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800297 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298 return -EPERM;
299 }
300 return 0;
301}
302
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303static ssize_t regulator_uV_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100306 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307 ssize_t ret;
308
309 mutex_lock(&rdev->mutex);
310 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
311 mutex_unlock(&rdev->mutex);
312
313 return ret;
314}
David Brownell7ad68e22008-11-11 17:39:02 -0800315static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100316
317static ssize_t regulator_uA_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
319{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100320 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321
322 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
323}
David Brownell7ad68e22008-11-11 17:39:02 -0800324static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100325
Mark Brownbc558a62008-10-10 15:33:20 +0100326static ssize_t regulator_name_show(struct device *dev,
327 struct device_attribute *attr, char *buf)
328{
329 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100330
Mark Brown1083c392009-10-22 16:31:32 +0100331 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100332}
333
David Brownell4fca9542008-11-11 17:38:53 -0800334static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100335{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100336 switch (mode) {
337 case REGULATOR_MODE_FAST:
338 return sprintf(buf, "fast\n");
339 case REGULATOR_MODE_NORMAL:
340 return sprintf(buf, "normal\n");
341 case REGULATOR_MODE_IDLE:
342 return sprintf(buf, "idle\n");
343 case REGULATOR_MODE_STANDBY:
344 return sprintf(buf, "standby\n");
345 }
346 return sprintf(buf, "unknown\n");
347}
348
David Brownell4fca9542008-11-11 17:38:53 -0800349static ssize_t regulator_opmode_show(struct device *dev,
350 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100351{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100352 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100353
David Brownell4fca9542008-11-11 17:38:53 -0800354 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
355}
David Brownell7ad68e22008-11-11 17:39:02 -0800356static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800357
358static ssize_t regulator_print_state(char *buf, int state)
359{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360 if (state > 0)
361 return sprintf(buf, "enabled\n");
362 else if (state == 0)
363 return sprintf(buf, "disabled\n");
364 else
365 return sprintf(buf, "unknown\n");
366}
367
David Brownell4fca9542008-11-11 17:38:53 -0800368static ssize_t regulator_state_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
371 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100372 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800373
Mark Brown93325462009-08-03 18:49:56 +0100374 mutex_lock(&rdev->mutex);
375 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
376 mutex_unlock(&rdev->mutex);
377
378 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800379}
David Brownell7ad68e22008-11-11 17:39:02 -0800380static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800381
David Brownell853116a2009-01-14 23:03:17 -0800382static ssize_t regulator_status_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
385 struct regulator_dev *rdev = dev_get_drvdata(dev);
386 int status;
387 char *label;
388
389 status = rdev->desc->ops->get_status(rdev);
390 if (status < 0)
391 return status;
392
393 switch (status) {
394 case REGULATOR_STATUS_OFF:
395 label = "off";
396 break;
397 case REGULATOR_STATUS_ON:
398 label = "on";
399 break;
400 case REGULATOR_STATUS_ERROR:
401 label = "error";
402 break;
403 case REGULATOR_STATUS_FAST:
404 label = "fast";
405 break;
406 case REGULATOR_STATUS_NORMAL:
407 label = "normal";
408 break;
409 case REGULATOR_STATUS_IDLE:
410 label = "idle";
411 break;
412 case REGULATOR_STATUS_STANDBY:
413 label = "standby";
414 break;
Mark Brownf59c8f92012-08-31 10:36:37 -0700415 case REGULATOR_STATUS_BYPASS:
416 label = "bypass";
417 break;
Krystian Garbaciak1beaf762012-07-12 13:53:35 +0100418 case REGULATOR_STATUS_UNDEFINED:
419 label = "undefined";
420 break;
David Brownell853116a2009-01-14 23:03:17 -0800421 default:
422 return -ERANGE;
423 }
424
425 return sprintf(buf, "%s\n", label);
426}
427static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
428
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429static ssize_t regulator_min_uA_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100432 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
434 if (!rdev->constraints)
435 return sprintf(buf, "constraint not defined\n");
436
437 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
438}
David Brownell7ad68e22008-11-11 17:39:02 -0800439static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441static ssize_t regulator_max_uA_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
443{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100444 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100445
446 if (!rdev->constraints)
447 return sprintf(buf, "constraint not defined\n");
448
449 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
450}
David Brownell7ad68e22008-11-11 17:39:02 -0800451static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
453static ssize_t regulator_min_uV_show(struct device *dev,
454 struct device_attribute *attr, char *buf)
455{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100456 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100457
458 if (!rdev->constraints)
459 return sprintf(buf, "constraint not defined\n");
460
461 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
462}
David Brownell7ad68e22008-11-11 17:39:02 -0800463static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465static ssize_t regulator_max_uV_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100468 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
470 if (!rdev->constraints)
471 return sprintf(buf, "constraint not defined\n");
472
473 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
474}
David Brownell7ad68e22008-11-11 17:39:02 -0800475static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477static ssize_t regulator_total_uA_show(struct device *dev,
478 struct device_attribute *attr, char *buf)
479{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100480 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100481 struct regulator *regulator;
482 int uA = 0;
483
484 mutex_lock(&rdev->mutex);
485 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100486 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487 mutex_unlock(&rdev->mutex);
488 return sprintf(buf, "%d\n", uA);
489}
David Brownell7ad68e22008-11-11 17:39:02 -0800490static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
492static ssize_t regulator_num_users_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100495 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496 return sprintf(buf, "%d\n", rdev->use_count);
497}
498
499static ssize_t regulator_type_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100502 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
504 switch (rdev->desc->type) {
505 case REGULATOR_VOLTAGE:
506 return sprintf(buf, "voltage\n");
507 case REGULATOR_CURRENT:
508 return sprintf(buf, "current\n");
509 }
510 return sprintf(buf, "unknown\n");
511}
512
513static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
515{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100516 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
519}
David Brownell7ad68e22008-11-11 17:39:02 -0800520static DEVICE_ATTR(suspend_mem_microvolts, 0444,
521 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522
523static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100526 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
529}
David Brownell7ad68e22008-11-11 17:39:02 -0800530static DEVICE_ATTR(suspend_disk_microvolts, 0444,
531 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
533static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100536 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
Liam Girdwood414c70c2008-04-30 15:59:04 +0100538 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
539}
David Brownell7ad68e22008-11-11 17:39:02 -0800540static DEVICE_ATTR(suspend_standby_microvolts, 0444,
541 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
544 struct device_attribute *attr, char *buf)
545{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100546 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
David Brownell4fca9542008-11-11 17:38:53 -0800548 return regulator_print_opmode(buf,
549 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550}
David Brownell7ad68e22008-11-11 17:39:02 -0800551static DEVICE_ATTR(suspend_mem_mode, 0444,
552 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553
554static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
555 struct device_attribute *attr, char *buf)
556{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100557 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558
David Brownell4fca9542008-11-11 17:38:53 -0800559 return regulator_print_opmode(buf,
560 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561}
David Brownell7ad68e22008-11-11 17:39:02 -0800562static DEVICE_ATTR(suspend_disk_mode, 0444,
563 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100564
565static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
566 struct device_attribute *attr, char *buf)
567{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100568 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100569
David Brownell4fca9542008-11-11 17:38:53 -0800570 return regulator_print_opmode(buf,
571 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100572}
David Brownell7ad68e22008-11-11 17:39:02 -0800573static DEVICE_ATTR(suspend_standby_mode, 0444,
574 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575
576static ssize_t regulator_suspend_mem_state_show(struct device *dev,
577 struct device_attribute *attr, char *buf)
578{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100579 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580
David Brownell4fca9542008-11-11 17:38:53 -0800581 return regulator_print_state(buf,
582 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583}
David Brownell7ad68e22008-11-11 17:39:02 -0800584static DEVICE_ATTR(suspend_mem_state, 0444,
585 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100586
587static ssize_t regulator_suspend_disk_state_show(struct device *dev,
588 struct device_attribute *attr, char *buf)
589{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100590 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
David Brownell4fca9542008-11-11 17:38:53 -0800592 return regulator_print_state(buf,
593 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594}
David Brownell7ad68e22008-11-11 17:39:02 -0800595static DEVICE_ATTR(suspend_disk_state, 0444,
596 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597
598static ssize_t regulator_suspend_standby_state_show(struct device *dev,
599 struct device_attribute *attr, char *buf)
600{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100601 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100602
David Brownell4fca9542008-11-11 17:38:53 -0800603 return regulator_print_state(buf,
604 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100605}
David Brownell7ad68e22008-11-11 17:39:02 -0800606static DEVICE_ATTR(suspend_standby_state, 0444,
607 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100608
Mark Brownf59c8f92012-08-31 10:36:37 -0700609static ssize_t regulator_bypass_show(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
612 struct regulator_dev *rdev = dev_get_drvdata(dev);
613 const char *report;
614 bool bypass;
615 int ret;
616
617 ret = rdev->desc->ops->get_bypass(rdev, &bypass);
618
619 if (ret != 0)
620 report = "unknown";
621 else if (bypass)
622 report = "enabled";
623 else
624 report = "disabled";
625
626 return sprintf(buf, "%s\n", report);
627}
628static DEVICE_ATTR(bypass, 0444,
629 regulator_bypass_show, NULL);
David Brownell7ad68e22008-11-11 17:39:02 -0800630
631/*
632 * These are the only attributes are present for all regulators.
633 * Other attributes are a function of regulator functionality.
634 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100635static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100636 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100637 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
638 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639 __ATTR_NULL,
640};
641
642static void regulator_dev_release(struct device *dev)
643{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100644 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645 kfree(rdev);
646}
647
648static struct class regulator_class = {
649 .name = "regulator",
650 .dev_release = regulator_dev_release,
651 .dev_attrs = regulator_dev_attrs,
652};
653
654/* Calculate the new optimum regulator operating mode based on the new total
655 * consumer load. All locks held by caller */
656static void drms_uA_update(struct regulator_dev *rdev)
657{
658 struct regulator *sibling;
659 int current_uA = 0, output_uV, input_uV, err;
660 unsigned int mode;
661
662 err = regulator_check_drms(rdev);
663 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000664 (!rdev->desc->ops->get_voltage &&
665 !rdev->desc->ops->get_voltage_sel) ||
666 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300667 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668
669 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000670 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100671 if (output_uV <= 0)
672 return;
673
674 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000675 input_uV = 0;
676 if (rdev->supply)
Axel Lin3f24f5a2012-04-13 21:35:05 +0800677 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000678 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100679 input_uV = rdev->constraints->input_uV;
680 if (input_uV <= 0)
681 return;
682
683 /* calc total requested load */
684 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100685 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100686
687 /* now get the optimum mode for our new total regulator load */
688 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
689 output_uV, current_uA);
690
691 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900692 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100693 if (err == 0)
694 rdev->desc->ops->set_mode(rdev, mode);
695}
696
697static int suspend_set_state(struct regulator_dev *rdev,
698 struct regulator_state *rstate)
699{
700 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100701
702 /* If we have no suspend mode configration don't set anything;
Axel Lin8ac0e952012-04-14 09:14:34 +0800703 * only warn if the driver implements set_suspend_voltage or
704 * set_suspend_mode callback.
Mark Brown638f85c2009-10-22 16:31:33 +0100705 */
706 if (!rstate->enabled && !rstate->disabled) {
Axel Lin8ac0e952012-04-14 09:14:34 +0800707 if (rdev->desc->ops->set_suspend_voltage ||
708 rdev->desc->ops->set_suspend_mode)
Joe Perches5da84fd2010-11-30 05:53:48 -0800709 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100710 return 0;
711 }
712
713 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800714 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100715 return -EINVAL;
716 }
717
Axel Lin8ac0e952012-04-14 09:14:34 +0800718 if (rstate->enabled && rdev->desc->ops->set_suspend_enable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100719 ret = rdev->desc->ops->set_suspend_enable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800720 else if (rstate->disabled && rdev->desc->ops->set_suspend_disable)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721 ret = rdev->desc->ops->set_suspend_disable(rdev);
Axel Lin8ac0e952012-04-14 09:14:34 +0800722 else /* OK if set_suspend_enable or set_suspend_disable is NULL */
723 ret = 0;
724
Liam Girdwood414c70c2008-04-30 15:59:04 +0100725 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800726 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100727 return ret;
728 }
729
730 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
731 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
732 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800733 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734 return ret;
735 }
736 }
737
738 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
739 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
740 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800741 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100742 return ret;
743 }
744 }
745 return ret;
746}
747
748/* locks held by caller */
749static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
750{
751 if (!rdev->constraints)
752 return -EINVAL;
753
754 switch (state) {
755 case PM_SUSPEND_STANDBY:
756 return suspend_set_state(rdev,
757 &rdev->constraints->state_standby);
758 case PM_SUSPEND_MEM:
759 return suspend_set_state(rdev,
760 &rdev->constraints->state_mem);
761 case PM_SUSPEND_MAX:
762 return suspend_set_state(rdev,
763 &rdev->constraints->state_disk);
764 default:
765 return -EINVAL;
766 }
767}
768
769static void print_constraints(struct regulator_dev *rdev)
770{
771 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000772 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100773 int count = 0;
774 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100775
Mark Brown8f031b42009-10-22 16:31:31 +0100776 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100777 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100778 count += sprintf(buf + count, "%d mV ",
779 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100780 else
Mark Brown8f031b42009-10-22 16:31:31 +0100781 count += sprintf(buf + count, "%d <--> %d mV ",
782 constraints->min_uV / 1000,
783 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100784 }
Mark Brown8f031b42009-10-22 16:31:31 +0100785
786 if (!constraints->min_uV ||
787 constraints->min_uV != constraints->max_uV) {
788 ret = _regulator_get_voltage(rdev);
789 if (ret > 0)
790 count += sprintf(buf + count, "at %d mV ", ret / 1000);
791 }
792
Mark Brownbf5892a2011-05-08 22:13:37 +0100793 if (constraints->uV_offset)
794 count += sprintf(buf, "%dmV offset ",
795 constraints->uV_offset / 1000);
796
Mark Brown8f031b42009-10-22 16:31:31 +0100797 if (constraints->min_uA && constraints->max_uA) {
798 if (constraints->min_uA == constraints->max_uA)
799 count += sprintf(buf + count, "%d mA ",
800 constraints->min_uA / 1000);
801 else
802 count += sprintf(buf + count, "%d <--> %d mA ",
803 constraints->min_uA / 1000,
804 constraints->max_uA / 1000);
805 }
806
807 if (!constraints->min_uA ||
808 constraints->min_uA != constraints->max_uA) {
809 ret = _regulator_get_current_limit(rdev);
810 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400811 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100812 }
813
Liam Girdwood414c70c2008-04-30 15:59:04 +0100814 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
815 count += sprintf(buf + count, "fast ");
816 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
817 count += sprintf(buf + count, "normal ");
818 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
819 count += sprintf(buf + count, "idle ");
820 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
821 count += sprintf(buf + count, "standby");
822
Uwe Kleine-König215b8b02012-08-07 21:01:37 +0200823 if (!count)
824 sprintf(buf, "no parameters");
825
Mark Brown13ce29f2010-12-17 16:04:12 +0000826 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000827
828 if ((constraints->min_uV != constraints->max_uV) &&
829 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
830 rdev_warn(rdev,
831 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100832}
833
Mark Browne79055d2009-10-19 15:53:50 +0100834static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100835 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100836{
837 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100838 int ret;
839
840 /* do we need to apply the constraint voltage */
841 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000842 rdev->constraints->min_uV == rdev->constraints->max_uV) {
843 ret = _regulator_do_set_voltage(rdev,
844 rdev->constraints->min_uV,
845 rdev->constraints->max_uV);
846 if (ret < 0) {
847 rdev_err(rdev, "failed to apply %duV constraint\n",
848 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000849 return ret;
850 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100851 }
Mark Browne79055d2009-10-19 15:53:50 +0100852
853 /* constrain machine-level voltage specs to fit
854 * the actual range supported by this regulator.
855 */
856 if (ops->list_voltage && rdev->desc->n_voltages) {
857 int count = rdev->desc->n_voltages;
858 int i;
859 int min_uV = INT_MAX;
860 int max_uV = INT_MIN;
861 int cmin = constraints->min_uV;
862 int cmax = constraints->max_uV;
863
864 /* it's safe to autoconfigure fixed-voltage supplies
865 and the constraints are used by list_voltage. */
866 if (count == 1 && !cmin) {
867 cmin = 1;
868 cmax = INT_MAX;
869 constraints->min_uV = cmin;
870 constraints->max_uV = cmax;
871 }
872
873 /* voltage constraints are optional */
874 if ((cmin == 0) && (cmax == 0))
875 return 0;
876
877 /* else require explicit machine-level constraints */
878 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800879 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100880 return -EINVAL;
881 }
882
883 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
884 for (i = 0; i < count; i++) {
885 int value;
886
887 value = ops->list_voltage(rdev, i);
888 if (value <= 0)
889 continue;
890
891 /* maybe adjust [min_uV..max_uV] */
892 if (value >= cmin && value < min_uV)
893 min_uV = value;
894 if (value <= cmax && value > max_uV)
895 max_uV = value;
896 }
897
898 /* final: [min_uV..max_uV] valid iff constraints valid */
899 if (max_uV < min_uV) {
Mark Brownfff15be2012-11-27 18:48:56 +0000900 rdev_err(rdev,
901 "unsupportable voltage constraints %u-%uuV\n",
902 min_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100903 return -EINVAL;
904 }
905
906 /* use regulator's subset of machine constraints */
907 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800908 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
909 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100910 constraints->min_uV = min_uV;
911 }
912 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800913 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
914 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100915 constraints->max_uV = max_uV;
916 }
917 }
918
919 return 0;
920}
921
Liam Girdwooda5766f12008-10-10 13:22:20 +0100922/**
923 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000924 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000925 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100926 *
927 * Allows platform initialisation code to define and constrain
928 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
929 * Constraints *must* be set by platform code in order for some
930 * regulator operations to proceed i.e. set_voltage, set_current_limit,
931 * set_mode.
932 */
933static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000934 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100935{
936 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100937 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100938
Mark Brown9a8f5e02011-11-29 18:11:19 +0000939 if (constraints)
940 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
941 GFP_KERNEL);
942 else
943 rdev->constraints = kzalloc(sizeof(*constraints),
944 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000945 if (!rdev->constraints)
946 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100947
Mark Brownf8c12fe2010-11-29 15:55:17 +0000948 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100949 if (ret != 0)
950 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800951
Liam Girdwooda5766f12008-10-10 13:22:20 +0100952 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000953 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000954 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100955 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800956 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100957 goto out;
958 }
959 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100960
Mark Brown9a8f5e02011-11-29 18:11:19 +0000961 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000962 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800963 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000964 ret = -EINVAL;
965 goto out;
966 }
967
Mark Brownf8c12fe2010-11-29 15:55:17 +0000968 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000969 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800970 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000971 goto out;
972 }
973 }
974
Mark Browncacf90f2009-03-02 16:32:46 +0000975 /* If the constraints say the regulator should be on at this point
976 * and we have control then make sure it is enabled.
977 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000978 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
979 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100980 ret = ops->enable(rdev);
981 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800982 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100983 goto out;
984 }
985 }
986
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +0530987 if (rdev->constraints->ramp_delay && ops->set_ramp_delay) {
988 ret = ops->set_ramp_delay(rdev, rdev->constraints->ramp_delay);
989 if (ret < 0) {
990 rdev_err(rdev, "failed to set ramp_delay\n");
991 goto out;
992 }
993 }
994
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800996 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100997out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800998 kfree(rdev->constraints);
999 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001000 return ret;
1001}
1002
1003/**
1004 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001005 * @rdev: regulator name
1006 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001007 *
1008 * Called by platform initialisation code to set the supply regulator for this
1009 * regulator. This ensures that a regulators supply will also be enabled by the
1010 * core if it's child is enabled.
1011 */
1012static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001013 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001014{
1015 int err;
1016
Mark Brown3801b862011-06-09 16:22:22 +01001017 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
1018
1019 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001020 if (rdev->supply == NULL) {
1021 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001022 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001023 }
Laxman Dewangan57ad526a2012-07-23 20:35:46 +05301024 supply_rdev->open_count++;
Mark Brown3801b862011-06-09 16:22:22 +01001025
1026 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001027}
1028
1029/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001030 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001031 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001032 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001033 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001034 *
1035 * Allows platform initialisation code to map physical regulator
1036 * sources to symbolic names for supplies for use by devices. Devices
1037 * should use these symbolic names to request regulators, avoiding the
1038 * need to provide board-specific regulator names as platform data.
1039 */
1040static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001041 const char *consumer_dev_name,
1042 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001043{
1044 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001045 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001046
1047 if (supply == NULL)
1048 return -EINVAL;
1049
Mark Brown9ed20992009-07-21 16:00:26 +01001050 if (consumer_dev_name != NULL)
1051 has_dev = 1;
1052 else
1053 has_dev = 0;
1054
David Brownell6001e132008-12-31 12:54:19 +00001055 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001056 if (node->dev_name && consumer_dev_name) {
1057 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1058 continue;
1059 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001060 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001061 }
1062
David Brownell6001e132008-12-31 12:54:19 +00001063 if (strcmp(node->supply, supply) != 0)
1064 continue;
1065
Mark Brown737f3602012-02-02 00:10:51 +00001066 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1067 consumer_dev_name,
1068 dev_name(&node->regulator->dev),
1069 node->regulator->desc->name,
1070 supply,
1071 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001072 return -EBUSY;
1073 }
1074
Mark Brown9ed20992009-07-21 16:00:26 +01001075 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001076 if (node == NULL)
1077 return -ENOMEM;
1078
1079 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001080 node->supply = supply;
1081
Mark Brown9ed20992009-07-21 16:00:26 +01001082 if (has_dev) {
1083 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1084 if (node->dev_name == NULL) {
1085 kfree(node);
1086 return -ENOMEM;
1087 }
Mark Brown40f92442009-06-17 17:56:39 +01001088 }
1089
Liam Girdwooda5766f12008-10-10 13:22:20 +01001090 list_add(&node->list, &regulator_map_list);
1091 return 0;
1092}
1093
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001094static void unset_regulator_supplies(struct regulator_dev *rdev)
1095{
1096 struct regulator_map *node, *n;
1097
1098 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1099 if (rdev == node->regulator) {
1100 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001101 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001102 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001103 }
1104 }
1105}
1106
Mark Brownf5726ae2011-06-09 16:22:20 +01001107#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001108
1109static struct regulator *create_regulator(struct regulator_dev *rdev,
1110 struct device *dev,
1111 const char *supply_name)
1112{
1113 struct regulator *regulator;
1114 char buf[REG_STR_SIZE];
1115 int err, size;
1116
1117 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1118 if (regulator == NULL)
1119 return NULL;
1120
1121 mutex_lock(&rdev->mutex);
1122 regulator->rdev = rdev;
1123 list_add(&regulator->list, &rdev->consumer_list);
1124
1125 if (dev) {
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001126 regulator->dev = dev;
1127
Mark Brown222cc7b2012-06-22 11:39:16 +01001128 /* Add a link to the device sysfs entry */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1130 dev->kobj.name, supply_name);
1131 if (size >= REG_STR_SIZE)
Mark Brown222cc7b2012-06-22 11:39:16 +01001132 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133
1134 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1135 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001136 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137
1138 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1139 buf);
1140 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001141 rdev_warn(rdev, "could not add device link %s err %d\n",
1142 dev->kobj.name, err);
Mark Brown222cc7b2012-06-22 11:39:16 +01001143 /* non-fatal */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 }
Mark Brown5de70512011-06-19 13:33:16 +01001145 } else {
1146 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1147 if (regulator->supply_name == NULL)
Mark Brown222cc7b2012-06-22 11:39:16 +01001148 goto overflow_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 }
Mark Brown5de70512011-06-19 13:33:16 +01001150
Mark Brown5de70512011-06-19 13:33:16 +01001151 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1152 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001153 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001154 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001155 } else {
1156 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1157 &regulator->uA_load);
1158 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1159 &regulator->min_uV);
1160 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1161 &regulator->max_uV);
1162 }
Mark Brown5de70512011-06-19 13:33:16 +01001163
Mark Brown6492bc12012-04-19 13:19:07 +01001164 /*
1165 * Check now if the regulator is an always on regulator - if
1166 * it is then we don't need to do nearly so much work for
1167 * enable/disable calls.
1168 */
1169 if (!_regulator_can_change_status(rdev) &&
1170 _regulator_is_enabled(rdev))
1171 regulator->always_on = true;
1172
Liam Girdwood414c70c2008-04-30 15:59:04 +01001173 mutex_unlock(&rdev->mutex);
1174 return regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001175overflow_err:
1176 list_del(&regulator->list);
1177 kfree(regulator);
1178 mutex_unlock(&rdev->mutex);
1179 return NULL;
1180}
1181
Mark Brown31aae2b2009-12-21 12:21:52 +00001182static int _regulator_get_enable_time(struct regulator_dev *rdev)
1183{
1184 if (!rdev->desc->ops->enable_time)
Mark Brown79511ed2012-06-27 14:23:10 +01001185 return rdev->desc->enable_time;
Mark Brown31aae2b2009-12-21 12:21:52 +00001186 return rdev->desc->ops->enable_time(rdev);
1187}
1188
Rajendra Nayak69511a42011-11-18 16:47:20 +05301189static struct regulator_dev *regulator_dev_lookup(struct device *dev,
Mark Brown6d191a52012-03-29 14:19:02 +01001190 const char *supply,
1191 int *ret)
Rajendra Nayak69511a42011-11-18 16:47:20 +05301192{
1193 struct regulator_dev *r;
1194 struct device_node *node;
Mark Brown576ca4362012-03-30 12:24:37 +01001195 struct regulator_map *map;
1196 const char *devname = NULL;
Rajendra Nayak69511a42011-11-18 16:47:20 +05301197
1198 /* first do a dt based lookup */
1199 if (dev && dev->of_node) {
1200 node = of_get_regulator(dev, supply);
Mark Brown6d191a52012-03-29 14:19:02 +01001201 if (node) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05301202 list_for_each_entry(r, &regulator_list, list)
1203 if (r->dev.parent &&
1204 node == r->dev.of_node)
1205 return r;
Mark Brown6d191a52012-03-29 14:19:02 +01001206 } else {
1207 /*
1208 * If we couldn't even get the node then it's
1209 * not just that the device didn't register
1210 * yet, there's no node and we'll never
1211 * succeed.
1212 */
1213 *ret = -ENODEV;
1214 }
Rajendra Nayak69511a42011-11-18 16:47:20 +05301215 }
1216
1217 /* if not found, try doing it non-dt way */
Mark Brown576ca4362012-03-30 12:24:37 +01001218 if (dev)
1219 devname = dev_name(dev);
1220
Rajendra Nayak69511a42011-11-18 16:47:20 +05301221 list_for_each_entry(r, &regulator_list, list)
1222 if (strcmp(rdev_get_name(r), supply) == 0)
1223 return r;
1224
Mark Brown576ca4362012-03-30 12:24:37 +01001225 list_for_each_entry(map, &regulator_map_list, list) {
1226 /* If the mapping has a device set up it must match */
1227 if (map->dev_name &&
1228 (!devname || strcmp(map->dev_name, devname)))
1229 continue;
1230
1231 if (strcmp(map->supply, supply) == 0)
1232 return map->regulator;
1233 }
1234
1235
Rajendra Nayak69511a42011-11-18 16:47:20 +05301236 return NULL;
1237}
1238
Mark Brown5ffbd132009-07-21 16:00:23 +01001239/* Internal regulator request function */
1240static struct regulator *_regulator_get(struct device *dev, const char *id,
1241 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242{
1243 struct regulator_dev *rdev;
Mark Brown04bf3012012-03-11 13:07:56 +00001244 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001245 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001246 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001247
1248 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001249 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001250 return regulator;
1251 }
1252
Mark Brown40f92442009-06-17 17:56:39 +01001253 if (dev)
1254 devname = dev_name(dev);
1255
Liam Girdwood414c70c2008-04-30 15:59:04 +01001256 mutex_lock(&regulator_list_mutex);
1257
Mark Brown6d191a52012-03-29 14:19:02 +01001258 rdev = regulator_dev_lookup(dev, id, &ret);
Rajendra Nayak69511a42011-11-18 16:47:20 +05301259 if (rdev)
1260 goto found;
1261
Mark Brown688fe992010-10-05 19:18:32 -07001262 if (board_wants_dummy_regulator) {
1263 rdev = dummy_regulator_rdev;
1264 goto found;
1265 }
1266
Mark Brown34abbd62010-02-12 10:18:08 +00001267#ifdef CONFIG_REGULATOR_DUMMY
1268 if (!devname)
1269 devname = "deviceless";
1270
1271 /* If the board didn't flag that it was fully constrained then
1272 * substitute in a dummy regulator so consumers can continue.
1273 */
1274 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001275 pr_warn("%s supply %s not found, using dummy regulator\n",
1276 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001277 rdev = dummy_regulator_rdev;
1278 goto found;
1279 }
1280#endif
1281
Liam Girdwood414c70c2008-04-30 15:59:04 +01001282 mutex_unlock(&regulator_list_mutex);
1283 return regulator;
1284
1285found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001286 if (rdev->exclusive) {
1287 regulator = ERR_PTR(-EPERM);
1288 goto out;
1289 }
1290
1291 if (exclusive && rdev->open_count) {
1292 regulator = ERR_PTR(-EBUSY);
1293 goto out;
1294 }
1295
Liam Girdwooda5766f12008-10-10 13:22:20 +01001296 if (!try_module_get(rdev->owner))
1297 goto out;
1298
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299 regulator = create_regulator(rdev, dev, id);
1300 if (regulator == NULL) {
1301 regulator = ERR_PTR(-ENOMEM);
1302 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001303 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304 }
1305
Mark Brown5ffbd132009-07-21 16:00:23 +01001306 rdev->open_count++;
1307 if (exclusive) {
1308 rdev->exclusive = 1;
1309
1310 ret = _regulator_is_enabled(rdev);
1311 if (ret > 0)
1312 rdev->use_count = 1;
1313 else
1314 rdev->use_count = 0;
1315 }
1316
Liam Girdwooda5766f12008-10-10 13:22:20 +01001317out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001318 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001319
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320 return regulator;
1321}
Mark Brown5ffbd132009-07-21 16:00:23 +01001322
1323/**
1324 * regulator_get - lookup and obtain a reference to a regulator.
1325 * @dev: device for regulator "consumer"
1326 * @id: Supply name or regulator ID.
1327 *
1328 * Returns a struct regulator corresponding to the regulator producer,
1329 * or IS_ERR() condition containing errno.
1330 *
1331 * Use of supply names configured via regulator_set_device_supply() is
1332 * strongly encouraged. It is recommended that the supply name used
1333 * should match the name used for the supply and/or the relevant
1334 * device pins in the datasheet.
1335 */
1336struct regulator *regulator_get(struct device *dev, const char *id)
1337{
1338 return _regulator_get(dev, id, 0);
1339}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001340EXPORT_SYMBOL_GPL(regulator_get);
1341
Stephen Boyd070b9072012-01-16 19:39:58 -08001342static void devm_regulator_release(struct device *dev, void *res)
1343{
1344 regulator_put(*(struct regulator **)res);
1345}
1346
1347/**
1348 * devm_regulator_get - Resource managed regulator_get()
1349 * @dev: device for regulator "consumer"
1350 * @id: Supply name or regulator ID.
1351 *
1352 * Managed regulator_get(). Regulators returned from this function are
1353 * automatically regulator_put() on driver detach. See regulator_get() for more
1354 * information.
1355 */
1356struct regulator *devm_regulator_get(struct device *dev, const char *id)
1357{
1358 struct regulator **ptr, *regulator;
1359
1360 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1361 if (!ptr)
1362 return ERR_PTR(-ENOMEM);
1363
1364 regulator = regulator_get(dev, id);
1365 if (!IS_ERR(regulator)) {
1366 *ptr = regulator;
1367 devres_add(dev, ptr);
1368 } else {
1369 devres_free(ptr);
1370 }
1371
1372 return regulator;
1373}
1374EXPORT_SYMBOL_GPL(devm_regulator_get);
1375
Liam Girdwood414c70c2008-04-30 15:59:04 +01001376/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001377 * regulator_get_exclusive - obtain exclusive access to a regulator.
1378 * @dev: device for regulator "consumer"
1379 * @id: Supply name or regulator ID.
1380 *
1381 * Returns a struct regulator corresponding to the regulator producer,
1382 * or IS_ERR() condition containing errno. Other consumers will be
1383 * unable to obtain this reference is held and the use count for the
1384 * regulator will be initialised to reflect the current state of the
1385 * regulator.
1386 *
1387 * This is intended for use by consumers which cannot tolerate shared
1388 * use of the regulator such as those which need to force the
1389 * regulator off for correct operation of the hardware they are
1390 * controlling.
1391 *
1392 * Use of supply names configured via regulator_set_device_supply() is
1393 * strongly encouraged. It is recommended that the supply name used
1394 * should match the name used for the supply and/or the relevant
1395 * device pins in the datasheet.
1396 */
1397struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1398{
1399 return _regulator_get(dev, id, 1);
1400}
1401EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1402
Charles Keepax23ff2f02012-11-14 09:39:31 +00001403/* Locks held by regulator_put() */
1404static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405{
1406 struct regulator_dev *rdev;
1407
1408 if (regulator == NULL || IS_ERR(regulator))
1409 return;
1410
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 rdev = regulator->rdev;
1412
Mark Brown5de70512011-06-19 13:33:16 +01001413 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001414
Liam Girdwood414c70c2008-04-30 15:59:04 +01001415 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001416 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001417 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001418 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 list_del(&regulator->list);
1420 kfree(regulator);
1421
Mark Brown5ffbd132009-07-21 16:00:23 +01001422 rdev->open_count--;
1423 rdev->exclusive = 0;
1424
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001426}
1427
1428/**
1429 * regulator_put - "free" the regulator source
1430 * @regulator: regulator source
1431 *
1432 * Note: drivers must ensure that all regulator_enable calls made on this
1433 * regulator source are balanced by regulator_disable calls prior to calling
1434 * this function.
1435 */
1436void regulator_put(struct regulator *regulator)
1437{
1438 mutex_lock(&regulator_list_mutex);
1439 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440 mutex_unlock(&regulator_list_mutex);
1441}
1442EXPORT_SYMBOL_GPL(regulator_put);
1443
Mark Brownd5ad34f2012-01-20 20:09:18 +00001444static int devm_regulator_match(struct device *dev, void *res, void *data)
1445{
1446 struct regulator **r = res;
1447 if (!r || !*r) {
1448 WARN_ON(!r || !*r);
1449 return 0;
1450 }
1451 return *r == data;
1452}
1453
1454/**
1455 * devm_regulator_put - Resource managed regulator_put()
1456 * @regulator: regulator to free
1457 *
1458 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1459 * this function will not need to be called and the resource management
1460 * code will ensure that the resource is freed.
1461 */
1462void devm_regulator_put(struct regulator *regulator)
1463{
1464 int rc;
1465
Mark Brown361ff502012-05-07 14:14:30 +01001466 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001467 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001468 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001469 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001470}
1471EXPORT_SYMBOL_GPL(devm_regulator_put);
1472
Kim, Milof19b00d2013-02-18 06:50:39 +00001473/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1474static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1475 const struct regulator_config *config)
1476{
1477 struct regulator_enable_gpio *pin;
1478 int ret;
1479
1480 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1481 if (pin->gpio == config->ena_gpio) {
1482 rdev_dbg(rdev, "GPIO %d is already used\n",
1483 config->ena_gpio);
1484 goto update_ena_gpio_to_rdev;
1485 }
1486 }
1487
1488 ret = gpio_request_one(config->ena_gpio,
1489 GPIOF_DIR_OUT | config->ena_gpio_flags,
1490 rdev_get_name(rdev));
1491 if (ret)
1492 return ret;
1493
1494 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1495 if (pin == NULL) {
1496 gpio_free(config->ena_gpio);
1497 return -ENOMEM;
1498 }
1499
1500 pin->gpio = config->ena_gpio;
1501 pin->ena_gpio_invert = config->ena_gpio_invert;
1502 list_add(&pin->list, &regulator_ena_gpio_list);
1503
1504update_ena_gpio_to_rdev:
1505 pin->request_count++;
1506 rdev->ena_pin = pin;
1507 return 0;
1508}
1509
1510static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1511{
1512 struct regulator_enable_gpio *pin, *n;
1513
1514 if (!rdev->ena_pin)
1515 return;
1516
1517 /* Free the GPIO only in case of no use */
1518 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1519 if (pin->gpio == rdev->ena_pin->gpio) {
1520 if (pin->request_count <= 1) {
1521 pin->request_count = 0;
1522 gpio_free(pin->gpio);
1523 list_del(&pin->list);
1524 kfree(pin);
1525 } else {
1526 pin->request_count--;
1527 }
1528 }
1529 }
1530}
1531
Mark Brown5c5659d2012-06-27 13:21:26 +01001532static int _regulator_do_enable(struct regulator_dev *rdev)
1533{
1534 int ret, delay;
1535
1536 /* Query before enabling in case configuration dependent. */
1537 ret = _regulator_get_enable_time(rdev);
1538 if (ret >= 0) {
1539 delay = ret;
1540 } else {
1541 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1542 delay = 0;
1543 }
1544
1545 trace_regulator_enable(rdev_get_name(rdev));
1546
Mark Brown65f73502012-06-27 14:14:38 +01001547 if (rdev->ena_gpio) {
1548 gpio_set_value_cansleep(rdev->ena_gpio,
1549 !rdev->ena_gpio_invert);
1550 rdev->ena_gpio_state = 1;
1551 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001552 ret = rdev->desc->ops->enable(rdev);
1553 if (ret < 0)
1554 return ret;
1555 } else {
1556 return -EINVAL;
1557 }
1558
1559 /* Allow the regulator to ramp; it would be useful to extend
1560 * this for bulk operations so that the regulators can ramp
1561 * together. */
1562 trace_regulator_enable_delay(rdev_get_name(rdev));
1563
1564 if (delay >= 1000) {
1565 mdelay(delay / 1000);
1566 udelay(delay % 1000);
1567 } else if (delay) {
1568 udelay(delay);
1569 }
1570
1571 trace_regulator_enable_complete(rdev_get_name(rdev));
1572
1573 return 0;
1574}
1575
Liam Girdwood414c70c2008-04-30 15:59:04 +01001576/* locks held by regulator_enable() */
1577static int _regulator_enable(struct regulator_dev *rdev)
1578{
Mark Brown5c5659d2012-06-27 13:21:26 +01001579 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001580
Liam Girdwood414c70c2008-04-30 15:59:04 +01001581 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001582 if (rdev->constraints &&
1583 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1584 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001585
Mark Brown9a2372f2009-08-03 18:49:57 +01001586 if (rdev->use_count == 0) {
1587 /* The regulator may on if it's not switchable or left on */
1588 ret = _regulator_is_enabled(rdev);
1589 if (ret == -EINVAL || ret == 0) {
1590 if (!_regulator_can_change_status(rdev))
1591 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001592
Mark Brown5c5659d2012-06-27 13:21:26 +01001593 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001594 if (ret < 0)
1595 return ret;
1596
Linus Walleija7433cf2009-08-26 12:54:04 +02001597 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001598 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001599 return ret;
1600 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001601 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001602 }
1603
Mark Brown9a2372f2009-08-03 18:49:57 +01001604 rdev->use_count++;
1605
1606 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001607}
1608
1609/**
1610 * regulator_enable - enable regulator output
1611 * @regulator: regulator source
1612 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001613 * Request that the regulator be enabled with the regulator output at
1614 * the predefined voltage or current value. Calls to regulator_enable()
1615 * must be balanced with calls to regulator_disable().
1616 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001617 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001618 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001619 */
1620int regulator_enable(struct regulator *regulator)
1621{
David Brownell412aec62008-11-16 11:44:46 -08001622 struct regulator_dev *rdev = regulator->rdev;
1623 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001624
Mark Brown6492bc12012-04-19 13:19:07 +01001625 if (regulator->always_on)
1626 return 0;
1627
Mark Brown3801b862011-06-09 16:22:22 +01001628 if (rdev->supply) {
1629 ret = regulator_enable(rdev->supply);
1630 if (ret != 0)
1631 return ret;
1632 }
1633
David Brownell412aec62008-11-16 11:44:46 -08001634 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001635 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001636 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001637
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001638 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001639 regulator_disable(rdev->supply);
1640
Liam Girdwood414c70c2008-04-30 15:59:04 +01001641 return ret;
1642}
1643EXPORT_SYMBOL_GPL(regulator_enable);
1644
Mark Brown5c5659d2012-06-27 13:21:26 +01001645static int _regulator_do_disable(struct regulator_dev *rdev)
1646{
1647 int ret;
1648
1649 trace_regulator_disable(rdev_get_name(rdev));
1650
1651 if (rdev->ena_gpio) {
1652 gpio_set_value_cansleep(rdev->ena_gpio,
1653 rdev->ena_gpio_invert);
1654 rdev->ena_gpio_state = 0;
1655
1656 } else if (rdev->desc->ops->disable) {
1657 ret = rdev->desc->ops->disable(rdev);
1658 if (ret != 0)
1659 return ret;
1660 }
1661
1662 trace_regulator_disable_complete(rdev_get_name(rdev));
1663
1664 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1665 NULL);
1666 return 0;
1667}
1668
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001670static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001671{
1672 int ret = 0;
1673
David Brownellcd94b502009-03-11 16:43:34 -08001674 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001675 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001676 return -EIO;
1677
Liam Girdwood414c70c2008-04-30 15:59:04 +01001678 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001679 if (rdev->use_count == 1 &&
1680 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001681
1682 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001683 if (_regulator_can_change_status(rdev)) {
1684 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001685 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001686 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001687 return ret;
1688 }
1689 }
1690
Liam Girdwood414c70c2008-04-30 15:59:04 +01001691 rdev->use_count = 0;
1692 } else if (rdev->use_count > 1) {
1693
1694 if (rdev->constraints &&
1695 (rdev->constraints->valid_ops_mask &
1696 REGULATOR_CHANGE_DRMS))
1697 drms_uA_update(rdev);
1698
1699 rdev->use_count--;
1700 }
Mark Brown3801b862011-06-09 16:22:22 +01001701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return ret;
1703}
1704
1705/**
1706 * regulator_disable - disable regulator output
1707 * @regulator: regulator source
1708 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001709 * Disable the regulator output voltage or current. Calls to
1710 * regulator_enable() must be balanced with calls to
1711 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001712 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001713 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001714 * devices have it enabled, the regulator device supports disabling and
1715 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001716 */
1717int regulator_disable(struct regulator *regulator)
1718{
David Brownell412aec62008-11-16 11:44:46 -08001719 struct regulator_dev *rdev = regulator->rdev;
1720 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001721
Mark Brown6492bc12012-04-19 13:19:07 +01001722 if (regulator->always_on)
1723 return 0;
1724
David Brownell412aec62008-11-16 11:44:46 -08001725 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001726 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001727 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001728
Mark Brown3801b862011-06-09 16:22:22 +01001729 if (ret == 0 && rdev->supply)
1730 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001731
Liam Girdwood414c70c2008-04-30 15:59:04 +01001732 return ret;
1733}
1734EXPORT_SYMBOL_GPL(regulator_disable);
1735
1736/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001737static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001738{
1739 int ret = 0;
1740
1741 /* force disable */
1742 if (rdev->desc->ops->disable) {
1743 /* ah well, who wants to live forever... */
1744 ret = rdev->desc->ops->disable(rdev);
1745 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001746 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001747 return ret;
1748 }
1749 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001750 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1751 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001752 }
1753
Liam Girdwood414c70c2008-04-30 15:59:04 +01001754 return ret;
1755}
1756
1757/**
1758 * regulator_force_disable - force disable regulator output
1759 * @regulator: regulator source
1760 *
1761 * Forcibly disable the regulator output voltage or current.
1762 * NOTE: this *will* disable the regulator output even if other consumer
1763 * devices have it enabled. This should be used for situations when device
1764 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1765 */
1766int regulator_force_disable(struct regulator *regulator)
1767{
Mark Brown82d15832011-05-09 11:41:02 +02001768 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001769 int ret;
1770
Mark Brown82d15832011-05-09 11:41:02 +02001771 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001772 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001773 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001774 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001775
Mark Brown3801b862011-06-09 16:22:22 +01001776 if (rdev->supply)
1777 while (rdev->open_count--)
1778 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001779
Liam Girdwood414c70c2008-04-30 15:59:04 +01001780 return ret;
1781}
1782EXPORT_SYMBOL_GPL(regulator_force_disable);
1783
Mark Brownda07ecd2011-09-11 09:53:50 +01001784static void regulator_disable_work(struct work_struct *work)
1785{
1786 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1787 disable_work.work);
1788 int count, i, ret;
1789
1790 mutex_lock(&rdev->mutex);
1791
1792 BUG_ON(!rdev->deferred_disables);
1793
1794 count = rdev->deferred_disables;
1795 rdev->deferred_disables = 0;
1796
1797 for (i = 0; i < count; i++) {
1798 ret = _regulator_disable(rdev);
1799 if (ret != 0)
1800 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1801 }
1802
1803 mutex_unlock(&rdev->mutex);
1804
1805 if (rdev->supply) {
1806 for (i = 0; i < count; i++) {
1807 ret = regulator_disable(rdev->supply);
1808 if (ret != 0) {
1809 rdev_err(rdev,
1810 "Supply disable failed: %d\n", ret);
1811 }
1812 }
1813 }
1814}
1815
1816/**
1817 * regulator_disable_deferred - disable regulator output with delay
1818 * @regulator: regulator source
1819 * @ms: miliseconds until the regulator is disabled
1820 *
1821 * Execute regulator_disable() on the regulator after a delay. This
1822 * is intended for use with devices that require some time to quiesce.
1823 *
1824 * NOTE: this will only disable the regulator output if no other consumer
1825 * devices have it enabled, the regulator device supports disabling and
1826 * machine constraints permit this operation.
1827 */
1828int regulator_disable_deferred(struct regulator *regulator, int ms)
1829{
1830 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001831 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001832
Mark Brown6492bc12012-04-19 13:19:07 +01001833 if (regulator->always_on)
1834 return 0;
1835
Mark Brown2b5a24a2012-09-07 11:00:53 +08001836 if (!ms)
1837 return regulator_disable(regulator);
1838
Mark Brownda07ecd2011-09-11 09:53:50 +01001839 mutex_lock(&rdev->mutex);
1840 rdev->deferred_disables++;
1841 mutex_unlock(&rdev->mutex);
1842
Mark Brownaa598022011-10-03 22:42:43 +01001843 ret = schedule_delayed_work(&rdev->disable_work,
1844 msecs_to_jiffies(ms));
1845 if (ret < 0)
1846 return ret;
1847 else
1848 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001849}
1850EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1851
Mark Browncd6dffb2012-04-15 12:37:47 +01001852/**
1853 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1854 *
1855 * @rdev: regulator to operate on
1856 *
1857 * Regulators that use regmap for their register I/O can set the
1858 * enable_reg and enable_mask fields in their descriptor and then use
1859 * this as their is_enabled operation, saving some code.
1860 */
1861int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1862{
1863 unsigned int val;
1864 int ret;
1865
1866 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1867 if (ret != 0)
1868 return ret;
1869
1870 return (val & rdev->desc->enable_mask) != 0;
1871}
1872EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1873
1874/**
1875 * regulator_enable_regmap - standard enable() for regmap users
1876 *
1877 * @rdev: regulator to operate on
1878 *
1879 * Regulators that use regmap for their register I/O can set the
1880 * enable_reg and enable_mask fields in their descriptor and then use
1881 * this as their enable() operation, saving some code.
1882 */
1883int regulator_enable_regmap(struct regulator_dev *rdev)
1884{
1885 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1886 rdev->desc->enable_mask,
1887 rdev->desc->enable_mask);
1888}
1889EXPORT_SYMBOL_GPL(regulator_enable_regmap);
1890
1891/**
1892 * regulator_disable_regmap - standard disable() for regmap users
1893 *
1894 * @rdev: regulator to operate on
1895 *
1896 * Regulators that use regmap for their register I/O can set the
1897 * enable_reg and enable_mask fields in their descriptor and then use
1898 * this as their disable() operation, saving some code.
1899 */
1900int regulator_disable_regmap(struct regulator_dev *rdev)
1901{
1902 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
1903 rdev->desc->enable_mask, 0);
1904}
1905EXPORT_SYMBOL_GPL(regulator_disable_regmap);
1906
Liam Girdwood414c70c2008-04-30 15:59:04 +01001907static int _regulator_is_enabled(struct regulator_dev *rdev)
1908{
Mark Brown65f73502012-06-27 14:14:38 +01001909 /* A GPIO control always takes precedence */
1910 if (rdev->ena_gpio)
1911 return rdev->ena_gpio_state;
1912
Mark Brown9a7f6a42010-02-11 17:22:45 +00001913 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001914 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001915 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001916
Mark Brown93325462009-08-03 18:49:56 +01001917 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001918}
1919
1920/**
1921 * regulator_is_enabled - is the regulator output enabled
1922 * @regulator: regulator source
1923 *
David Brownell412aec62008-11-16 11:44:46 -08001924 * Returns positive if the regulator driver backing the source/client
1925 * has requested that the device be enabled, zero if it hasn't, else a
1926 * negative errno code.
1927 *
1928 * Note that the device backing this regulator handle can have multiple
1929 * users, so it might be enabled even if regulator_enable() was never
1930 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001931 */
1932int regulator_is_enabled(struct regulator *regulator)
1933{
Mark Brown93325462009-08-03 18:49:56 +01001934 int ret;
1935
Mark Brown6492bc12012-04-19 13:19:07 +01001936 if (regulator->always_on)
1937 return 1;
1938
Mark Brown93325462009-08-03 18:49:56 +01001939 mutex_lock(&regulator->rdev->mutex);
1940 ret = _regulator_is_enabled(regulator->rdev);
1941 mutex_unlock(&regulator->rdev->mutex);
1942
1943 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001944}
1945EXPORT_SYMBOL_GPL(regulator_is_enabled);
1946
1947/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001948 * regulator_can_change_voltage - check if regulator can change voltage
1949 * @regulator: regulator source
1950 *
1951 * Returns positive if the regulator driver backing the source/client
1952 * can change its voltage, false otherwise. Usefull for detecting fixed
1953 * or dummy regulators and disabling voltage change logic in the client
1954 * driver.
1955 */
1956int regulator_can_change_voltage(struct regulator *regulator)
1957{
1958 struct regulator_dev *rdev = regulator->rdev;
1959
1960 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08001961 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
1962 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
1963 return 1;
1964
1965 if (rdev->desc->continuous_voltage_range &&
1966 rdev->constraints->min_uV && rdev->constraints->max_uV &&
1967 rdev->constraints->min_uV != rdev->constraints->max_uV)
1968 return 1;
1969 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01001970
1971 return 0;
1972}
1973EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
1974
1975/**
David Brownell4367cfd2009-02-26 11:48:36 -08001976 * regulator_count_voltages - count regulator_list_voltage() selectors
1977 * @regulator: regulator source
1978 *
1979 * Returns number of selectors, or negative errno. Selectors are
1980 * numbered starting at zero, and typically correspond to bitfields
1981 * in hardware registers.
1982 */
1983int regulator_count_voltages(struct regulator *regulator)
1984{
1985 struct regulator_dev *rdev = regulator->rdev;
1986
1987 return rdev->desc->n_voltages ? : -EINVAL;
1988}
1989EXPORT_SYMBOL_GPL(regulator_count_voltages);
1990
1991/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01001992 * regulator_list_voltage_linear - List voltages with simple calculation
1993 *
1994 * @rdev: Regulator device
1995 * @selector: Selector to convert into a voltage
1996 *
1997 * Regulators with a simple linear mapping between voltages and
1998 * selectors can set min_uV and uV_step in the regulator descriptor
1999 * and then use this function as their list_voltage() operation,
2000 */
2001int regulator_list_voltage_linear(struct regulator_dev *rdev,
2002 unsigned int selector)
2003{
2004 if (selector >= rdev->desc->n_voltages)
2005 return -EINVAL;
Axel Lin33234e72012-11-27 10:24:33 +08002006 if (selector < rdev->desc->linear_min_sel)
2007 return 0;
2008
2009 selector -= rdev->desc->linear_min_sel;
Mark Brownbca7bbf2012-05-09 21:38:33 +01002010
2011 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2012}
2013EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2014
2015/**
Axel Lincffc9592012-05-20 10:30:21 +08002016 * regulator_list_voltage_table - List voltages with table based mapping
2017 *
2018 * @rdev: Regulator device
2019 * @selector: Selector to convert into a voltage
2020 *
2021 * Regulators with table based mapping between voltages and
2022 * selectors can set volt_table in the regulator descriptor
2023 * and then use this function as their list_voltage() operation.
2024 */
2025int regulator_list_voltage_table(struct regulator_dev *rdev,
2026 unsigned int selector)
2027{
2028 if (!rdev->desc->volt_table) {
2029 BUG_ON(!rdev->desc->volt_table);
2030 return -EINVAL;
2031 }
2032
2033 if (selector >= rdev->desc->n_voltages)
2034 return -EINVAL;
2035
2036 return rdev->desc->volt_table[selector];
2037}
2038EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2039
2040/**
David Brownell4367cfd2009-02-26 11:48:36 -08002041 * regulator_list_voltage - enumerate supported voltages
2042 * @regulator: regulator source
2043 * @selector: identify voltage to list
2044 * Context: can sleep
2045 *
2046 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002047 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002048 * negative errno.
2049 */
2050int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2051{
2052 struct regulator_dev *rdev = regulator->rdev;
2053 struct regulator_ops *ops = rdev->desc->ops;
2054 int ret;
2055
2056 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2057 return -EINVAL;
2058
2059 mutex_lock(&rdev->mutex);
2060 ret = ops->list_voltage(rdev, selector);
2061 mutex_unlock(&rdev->mutex);
2062
2063 if (ret > 0) {
2064 if (ret < rdev->constraints->min_uV)
2065 ret = 0;
2066 else if (ret > rdev->constraints->max_uV)
2067 ret = 0;
2068 }
2069
2070 return ret;
2071}
2072EXPORT_SYMBOL_GPL(regulator_list_voltage);
2073
2074/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002075 * regulator_is_supported_voltage - check if a voltage range can be supported
2076 *
2077 * @regulator: Regulator to check.
2078 * @min_uV: Minimum required voltage in uV.
2079 * @max_uV: Maximum required voltage in uV.
2080 *
2081 * Returns a boolean or a negative error code.
2082 */
2083int regulator_is_supported_voltage(struct regulator *regulator,
2084 int min_uV, int max_uV)
2085{
Mark Brownc5f39392012-07-02 15:00:18 +01002086 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002087 int i, voltages, ret;
2088
Mark Brownc5f39392012-07-02 15:00:18 +01002089 /* If we can't change voltage check the current voltage */
2090 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2091 ret = regulator_get_voltage(regulator);
2092 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002093 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002094 else
2095 return ret;
2096 }
2097
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002098 /* Any voltage within constrains range is fine? */
2099 if (rdev->desc->continuous_voltage_range)
2100 return min_uV >= rdev->constraints->min_uV &&
2101 max_uV <= rdev->constraints->max_uV;
2102
Mark Browna7a1ad92009-07-21 16:00:24 +01002103 ret = regulator_count_voltages(regulator);
2104 if (ret < 0)
2105 return ret;
2106 voltages = ret;
2107
2108 for (i = 0; i < voltages; i++) {
2109 ret = regulator_list_voltage(regulator, i);
2110
2111 if (ret >= min_uV && ret <= max_uV)
2112 return 1;
2113 }
2114
2115 return 0;
2116}
Mark Browna398eaa2011-12-28 12:48:45 +00002117EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002118
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002119/**
2120 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2121 *
2122 * @rdev: regulator to operate on
2123 *
2124 * Regulators that use regmap for their register I/O can set the
2125 * vsel_reg and vsel_mask fields in their descriptor and then use this
2126 * as their get_voltage_vsel operation, saving some code.
2127 */
2128int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2129{
2130 unsigned int val;
2131 int ret;
2132
2133 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2134 if (ret != 0)
2135 return ret;
2136
2137 val &= rdev->desc->vsel_mask;
2138 val >>= ffs(rdev->desc->vsel_mask) - 1;
2139
2140 return val;
2141}
2142EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2143
2144/**
2145 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2146 *
2147 * @rdev: regulator to operate on
2148 * @sel: Selector to set
2149 *
2150 * Regulators that use regmap for their register I/O can set the
2151 * vsel_reg and vsel_mask fields in their descriptor and then use this
2152 * as their set_voltage_vsel operation, saving some code.
2153 */
2154int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2155{
Axel Linc8520b42012-12-18 09:30:10 +08002156 int ret;
2157
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002158 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2159
Axel Linc8520b42012-12-18 09:30:10 +08002160 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002161 rdev->desc->vsel_mask, sel);
Axel Linc8520b42012-12-18 09:30:10 +08002162 if (ret)
2163 return ret;
2164
2165 if (rdev->desc->apply_bit)
2166 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2167 rdev->desc->apply_bit,
2168 rdev->desc->apply_bit);
2169 return ret;
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002170}
2171EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2172
Mark Browne843fc42012-05-09 21:16:06 +01002173/**
2174 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2175 *
2176 * @rdev: Regulator to operate on
2177 * @min_uV: Lower bound for voltage
2178 * @max_uV: Upper bound for voltage
2179 *
2180 * Drivers implementing set_voltage_sel() and list_voltage() can use
2181 * this as their map_voltage() operation. It will find a suitable
2182 * voltage by calling list_voltage() until it gets something in bounds
2183 * for the requested voltages.
2184 */
2185int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2186 int min_uV, int max_uV)
2187{
2188 int best_val = INT_MAX;
2189 int selector = 0;
2190 int i, ret;
2191
2192 /* Find the smallest voltage that falls within the specified
2193 * range.
2194 */
2195 for (i = 0; i < rdev->desc->n_voltages; i++) {
2196 ret = rdev->desc->ops->list_voltage(rdev, i);
2197 if (ret < 0)
2198 continue;
2199
2200 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2201 best_val = ret;
2202 selector = i;
2203 }
2204 }
2205
2206 if (best_val != INT_MAX)
2207 return selector;
2208 else
2209 return -EINVAL;
2210}
2211EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2212
Mark Brownbca7bbf2012-05-09 21:38:33 +01002213/**
2214 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2215 *
2216 * @rdev: Regulator to operate on
2217 * @min_uV: Lower bound for voltage
2218 * @max_uV: Upper bound for voltage
2219 *
2220 * Drivers providing min_uV and uV_step in their regulator_desc can
2221 * use this as their map_voltage() operation.
2222 */
2223int regulator_map_voltage_linear(struct regulator_dev *rdev,
2224 int min_uV, int max_uV)
2225{
2226 int ret, voltage;
2227
Axel Lin5a6881e2012-06-07 10:05:14 +08002228 /* Allow uV_step to be 0 for fixed voltage */
2229 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2230 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2231 return 0;
2232 else
2233 return -EINVAL;
2234 }
2235
Mark Brownbca7bbf2012-05-09 21:38:33 +01002236 if (!rdev->desc->uV_step) {
2237 BUG_ON(!rdev->desc->uV_step);
2238 return -EINVAL;
2239 }
2240
Axel Lin0bdc81e2012-06-07 09:52:12 +08002241 if (min_uV < rdev->desc->min_uV)
2242 min_uV = rdev->desc->min_uV;
2243
Axel Linccfcb1c2012-05-14 10:33:37 +08002244 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002245 if (ret < 0)
2246 return ret;
2247
Axel Lin33234e72012-11-27 10:24:33 +08002248 ret += rdev->desc->linear_min_sel;
2249
Mark Brownbca7bbf2012-05-09 21:38:33 +01002250 /* Map back into a voltage to verify we're still in bounds */
2251 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2252 if (voltage < min_uV || voltage > max_uV)
2253 return -EINVAL;
2254
2255 return ret;
2256}
2257EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2258
Mark Brown75790252010-12-12 14:25:50 +00002259static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2260 int min_uV, int max_uV)
2261{
2262 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002263 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002264 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002265 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002266 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002267
2268 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2269
Mark Brownbf5892a2011-05-08 22:13:37 +01002270 min_uV += rdev->constraints->uV_offset;
2271 max_uV += rdev->constraints->uV_offset;
2272
Axel Lineba41a52012-04-04 10:32:10 +08002273 /*
2274 * If we can't obtain the old selector there is not enough
2275 * info to call set_voltage_time_sel().
2276 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002277 if (_regulator_is_enabled(rdev) &&
2278 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002279 rdev->desc->ops->get_voltage_sel) {
2280 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2281 if (old_selector < 0)
2282 return old_selector;
2283 }
2284
Mark Brown75790252010-12-12 14:25:50 +00002285 if (rdev->desc->ops->set_voltage) {
2286 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2287 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002288
2289 if (ret >= 0) {
2290 if (rdev->desc->ops->list_voltage)
2291 best_val = rdev->desc->ops->list_voltage(rdev,
2292 selector);
2293 else
2294 best_val = _regulator_get_voltage(rdev);
2295 }
2296
Mark Browne8eef822010-12-12 14:36:17 +00002297 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002298 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002299 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2300 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002301 } else {
2302 if (rdev->desc->ops->list_voltage ==
2303 regulator_list_voltage_linear)
2304 ret = regulator_map_voltage_linear(rdev,
2305 min_uV, max_uV);
2306 else
2307 ret = regulator_map_voltage_iterate(rdev,
2308 min_uV, max_uV);
2309 }
Mark Browne8eef822010-12-12 14:36:17 +00002310
Mark Browne843fc42012-05-09 21:16:06 +01002311 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002312 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2313 if (min_uV <= best_val && max_uV >= best_val) {
2314 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002315 if (old_selector == selector)
2316 ret = 0;
2317 else
2318 ret = rdev->desc->ops->set_voltage_sel(
2319 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002320 } else {
2321 ret = -EINVAL;
2322 }
Mark Browne8eef822010-12-12 14:36:17 +00002323 }
Mark Brown75790252010-12-12 14:25:50 +00002324 } else {
2325 ret = -EINVAL;
2326 }
2327
Axel Lineba41a52012-04-04 10:32:10 +08002328 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002329 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Linc66a5662013-02-06 11:09:48 +08002330 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
Axel Lineba41a52012-04-04 10:32:10 +08002331
2332 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2333 old_selector, selector);
2334 if (delay < 0) {
2335 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2336 delay);
2337 delay = 0;
2338 }
Axel Lineba41a52012-04-04 10:32:10 +08002339
Philip Rakity8b96de32012-06-14 15:07:56 -07002340 /* Insert any necessary delays */
2341 if (delay >= 1000) {
2342 mdelay(delay / 1000);
2343 udelay(delay % 1000);
2344 } else if (delay) {
2345 udelay(delay);
2346 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002347 }
2348
Axel Lin2f6c7972012-08-06 23:44:19 +08002349 if (ret == 0 && best_val >= 0) {
2350 unsigned long data = best_val;
2351
Mark Brownded06a52010-12-16 13:59:10 +00002352 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002353 (void *)data);
2354 }
Mark Brownded06a52010-12-16 13:59:10 +00002355
Axel Lineba41a52012-04-04 10:32:10 +08002356 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002357
2358 return ret;
2359}
2360
Mark Browna7a1ad92009-07-21 16:00:24 +01002361/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002362 * regulator_set_voltage - set regulator output voltage
2363 * @regulator: regulator source
2364 * @min_uV: Minimum required voltage in uV
2365 * @max_uV: Maximum acceptable voltage in uV
2366 *
2367 * Sets a voltage regulator to the desired output voltage. This can be set
2368 * during any regulator state. IOW, regulator can be disabled or enabled.
2369 *
2370 * If the regulator is enabled then the voltage will change to the new value
2371 * immediately otherwise if the regulator is disabled the regulator will
2372 * output at the new voltage when enabled.
2373 *
2374 * NOTE: If the regulator is shared between several devices then the lowest
2375 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002376 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002377 * calling this function otherwise this call will fail.
2378 */
2379int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2380{
2381 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002382 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002383 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002384
2385 mutex_lock(&rdev->mutex);
2386
Mark Brown95a3c232010-12-16 15:49:37 +00002387 /* If we're setting the same range as last time the change
2388 * should be a noop (some cpufreq implementations use the same
2389 * voltage for multiple frequencies, for example).
2390 */
2391 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2392 goto out;
2393
Liam Girdwood414c70c2008-04-30 15:59:04 +01002394 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002395 if (!rdev->desc->ops->set_voltage &&
2396 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002397 ret = -EINVAL;
2398 goto out;
2399 }
2400
2401 /* constraints check */
2402 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2403 if (ret < 0)
2404 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002405
2406 /* restore original values in case of error */
2407 old_min_uV = regulator->min_uV;
2408 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002409 regulator->min_uV = min_uV;
2410 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002411
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002412 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2413 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002414 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002415
Mark Brown75790252010-12-12 14:25:50 +00002416 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002417 if (ret < 0)
2418 goto out2;
2419
Liam Girdwood414c70c2008-04-30 15:59:04 +01002420out:
2421 mutex_unlock(&rdev->mutex);
2422 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002423out2:
2424 regulator->min_uV = old_min_uV;
2425 regulator->max_uV = old_max_uV;
2426 mutex_unlock(&rdev->mutex);
2427 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002428}
2429EXPORT_SYMBOL_GPL(regulator_set_voltage);
2430
Mark Brown606a2562010-12-16 15:49:36 +00002431/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002432 * regulator_set_voltage_time - get raise/fall time
2433 * @regulator: regulator source
2434 * @old_uV: starting voltage in microvolts
2435 * @new_uV: target voltage in microvolts
2436 *
2437 * Provided with the starting and ending voltage, this function attempts to
2438 * calculate the time in microseconds required to rise or fall to this new
2439 * voltage.
2440 */
2441int regulator_set_voltage_time(struct regulator *regulator,
2442 int old_uV, int new_uV)
2443{
2444 struct regulator_dev *rdev = regulator->rdev;
2445 struct regulator_ops *ops = rdev->desc->ops;
2446 int old_sel = -1;
2447 int new_sel = -1;
2448 int voltage;
2449 int i;
2450
2451 /* Currently requires operations to do this */
2452 if (!ops->list_voltage || !ops->set_voltage_time_sel
2453 || !rdev->desc->n_voltages)
2454 return -EINVAL;
2455
2456 for (i = 0; i < rdev->desc->n_voltages; i++) {
2457 /* We only look for exact voltage matches here */
2458 voltage = regulator_list_voltage(regulator, i);
2459 if (voltage < 0)
2460 return -EINVAL;
2461 if (voltage == 0)
2462 continue;
2463 if (voltage == old_uV)
2464 old_sel = i;
2465 if (voltage == new_uV)
2466 new_sel = i;
2467 }
2468
2469 if (old_sel < 0 || new_sel < 0)
2470 return -EINVAL;
2471
2472 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2473}
2474EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2475
2476/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002477 * regulator_set_voltage_time_sel - get raise/fall time
2478 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302479 * @old_selector: selector for starting voltage
2480 * @new_selector: selector for target voltage
2481 *
2482 * Provided with the starting and target voltage selectors, this function
2483 * returns time in microseconds required to rise or fall to this new voltage
2484 *
Axel Linf11d08c2012-06-19 09:38:45 +08002485 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002486 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302487 */
2488int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2489 unsigned int old_selector,
2490 unsigned int new_selector)
2491{
Axel Lin398715a2012-06-18 10:11:28 +08002492 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002493 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002494
2495 if (rdev->constraints->ramp_delay)
2496 ramp_delay = rdev->constraints->ramp_delay;
2497 else if (rdev->desc->ramp_delay)
2498 ramp_delay = rdev->desc->ramp_delay;
2499
2500 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302501 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002502 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302503 }
Axel Lin398715a2012-06-18 10:11:28 +08002504
Axel Linf11d08c2012-06-19 09:38:45 +08002505 /* sanity check */
2506 if (!rdev->desc->ops->list_voltage)
2507 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002508
Axel Linf11d08c2012-06-19 09:38:45 +08002509 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2510 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2511
2512 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302513}
Mark Brownb19dbf72012-06-23 11:34:20 +01002514EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302515
2516/**
Mark Brown606a2562010-12-16 15:49:36 +00002517 * regulator_sync_voltage - re-apply last regulator output voltage
2518 * @regulator: regulator source
2519 *
2520 * Re-apply the last configured voltage. This is intended to be used
2521 * where some external control source the consumer is cooperating with
2522 * has caused the configured voltage to change.
2523 */
2524int regulator_sync_voltage(struct regulator *regulator)
2525{
2526 struct regulator_dev *rdev = regulator->rdev;
2527 int ret, min_uV, max_uV;
2528
2529 mutex_lock(&rdev->mutex);
2530
2531 if (!rdev->desc->ops->set_voltage &&
2532 !rdev->desc->ops->set_voltage_sel) {
2533 ret = -EINVAL;
2534 goto out;
2535 }
2536
2537 /* This is only going to work if we've had a voltage configured. */
2538 if (!regulator->min_uV && !regulator->max_uV) {
2539 ret = -EINVAL;
2540 goto out;
2541 }
2542
2543 min_uV = regulator->min_uV;
2544 max_uV = regulator->max_uV;
2545
2546 /* This should be a paranoia check... */
2547 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2548 if (ret < 0)
2549 goto out;
2550
2551 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2552 if (ret < 0)
2553 goto out;
2554
2555 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2556
2557out:
2558 mutex_unlock(&rdev->mutex);
2559 return ret;
2560}
2561EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2562
Liam Girdwood414c70c2008-04-30 15:59:04 +01002563static int _regulator_get_voltage(struct regulator_dev *rdev)
2564{
Mark Brownbf5892a2011-05-08 22:13:37 +01002565 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002566
2567 if (rdev->desc->ops->get_voltage_sel) {
2568 sel = rdev->desc->ops->get_voltage_sel(rdev);
2569 if (sel < 0)
2570 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002571 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002572 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002573 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002574 } else if (rdev->desc->ops->list_voltage) {
2575 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002576 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002577 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002578 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002579
Axel Lincb220d12011-05-23 20:08:10 +08002580 if (ret < 0)
2581 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002582 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583}
2584
2585/**
2586 * regulator_get_voltage - get regulator output voltage
2587 * @regulator: regulator source
2588 *
2589 * This returns the current regulator voltage in uV.
2590 *
2591 * NOTE: If the regulator is disabled it will return the voltage value. This
2592 * function should not be used to determine regulator state.
2593 */
2594int regulator_get_voltage(struct regulator *regulator)
2595{
2596 int ret;
2597
2598 mutex_lock(&regulator->rdev->mutex);
2599
2600 ret = _regulator_get_voltage(regulator->rdev);
2601
2602 mutex_unlock(&regulator->rdev->mutex);
2603
2604 return ret;
2605}
2606EXPORT_SYMBOL_GPL(regulator_get_voltage);
2607
2608/**
2609 * regulator_set_current_limit - set regulator output current limit
2610 * @regulator: regulator source
2611 * @min_uA: Minimuum supported current in uA
2612 * @max_uA: Maximum supported current in uA
2613 *
2614 * Sets current sink to the desired output current. This can be set during
2615 * any regulator state. IOW, regulator can be disabled or enabled.
2616 *
2617 * If the regulator is enabled then the current will change to the new value
2618 * immediately otherwise if the regulator is disabled the regulator will
2619 * output at the new current when enabled.
2620 *
2621 * NOTE: Regulator system constraints must be set for this regulator before
2622 * calling this function otherwise this call will fail.
2623 */
2624int regulator_set_current_limit(struct regulator *regulator,
2625 int min_uA, int max_uA)
2626{
2627 struct regulator_dev *rdev = regulator->rdev;
2628 int ret;
2629
2630 mutex_lock(&rdev->mutex);
2631
2632 /* sanity check */
2633 if (!rdev->desc->ops->set_current_limit) {
2634 ret = -EINVAL;
2635 goto out;
2636 }
2637
2638 /* constraints check */
2639 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2640 if (ret < 0)
2641 goto out;
2642
2643 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2644out:
2645 mutex_unlock(&rdev->mutex);
2646 return ret;
2647}
2648EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2649
2650static int _regulator_get_current_limit(struct regulator_dev *rdev)
2651{
2652 int ret;
2653
2654 mutex_lock(&rdev->mutex);
2655
2656 /* sanity check */
2657 if (!rdev->desc->ops->get_current_limit) {
2658 ret = -EINVAL;
2659 goto out;
2660 }
2661
2662 ret = rdev->desc->ops->get_current_limit(rdev);
2663out:
2664 mutex_unlock(&rdev->mutex);
2665 return ret;
2666}
2667
2668/**
2669 * regulator_get_current_limit - get regulator output current
2670 * @regulator: regulator source
2671 *
2672 * This returns the current supplied by the specified current sink in uA.
2673 *
2674 * NOTE: If the regulator is disabled it will return the current value. This
2675 * function should not be used to determine regulator state.
2676 */
2677int regulator_get_current_limit(struct regulator *regulator)
2678{
2679 return _regulator_get_current_limit(regulator->rdev);
2680}
2681EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2682
2683/**
2684 * regulator_set_mode - set regulator operating mode
2685 * @regulator: regulator source
2686 * @mode: operating mode - one of the REGULATOR_MODE constants
2687 *
2688 * Set regulator operating mode to increase regulator efficiency or improve
2689 * regulation performance.
2690 *
2691 * NOTE: Regulator system constraints must be set for this regulator before
2692 * calling this function otherwise this call will fail.
2693 */
2694int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2695{
2696 struct regulator_dev *rdev = regulator->rdev;
2697 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302698 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002699
2700 mutex_lock(&rdev->mutex);
2701
2702 /* sanity check */
2703 if (!rdev->desc->ops->set_mode) {
2704 ret = -EINVAL;
2705 goto out;
2706 }
2707
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302708 /* return if the same mode is requested */
2709 if (rdev->desc->ops->get_mode) {
2710 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2711 if (regulator_curr_mode == mode) {
2712 ret = 0;
2713 goto out;
2714 }
2715 }
2716
Liam Girdwood414c70c2008-04-30 15:59:04 +01002717 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002718 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002719 if (ret < 0)
2720 goto out;
2721
2722 ret = rdev->desc->ops->set_mode(rdev, mode);
2723out:
2724 mutex_unlock(&rdev->mutex);
2725 return ret;
2726}
2727EXPORT_SYMBOL_GPL(regulator_set_mode);
2728
2729static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2730{
2731 int ret;
2732
2733 mutex_lock(&rdev->mutex);
2734
2735 /* sanity check */
2736 if (!rdev->desc->ops->get_mode) {
2737 ret = -EINVAL;
2738 goto out;
2739 }
2740
2741 ret = rdev->desc->ops->get_mode(rdev);
2742out:
2743 mutex_unlock(&rdev->mutex);
2744 return ret;
2745}
2746
2747/**
2748 * regulator_get_mode - get regulator operating mode
2749 * @regulator: regulator source
2750 *
2751 * Get the current regulator operating mode.
2752 */
2753unsigned int regulator_get_mode(struct regulator *regulator)
2754{
2755 return _regulator_get_mode(regulator->rdev);
2756}
2757EXPORT_SYMBOL_GPL(regulator_get_mode);
2758
2759/**
2760 * regulator_set_optimum_mode - set regulator optimum operating mode
2761 * @regulator: regulator source
2762 * @uA_load: load current
2763 *
2764 * Notifies the regulator core of a new device load. This is then used by
2765 * DRMS (if enabled by constraints) to set the most efficient regulator
2766 * operating mode for the new regulator loading.
2767 *
2768 * Consumer devices notify their supply regulator of the maximum power
2769 * they will require (can be taken from device datasheet in the power
2770 * consumption tables) when they change operational status and hence power
2771 * state. Examples of operational state changes that can affect power
2772 * consumption are :-
2773 *
2774 * o Device is opened / closed.
2775 * o Device I/O is about to begin or has just finished.
2776 * o Device is idling in between work.
2777 *
2778 * This information is also exported via sysfs to userspace.
2779 *
2780 * DRMS will sum the total requested load on the regulator and change
2781 * to the most efficient operating mode if platform constraints allow.
2782 *
2783 * Returns the new regulator mode or error.
2784 */
2785int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2786{
2787 struct regulator_dev *rdev = regulator->rdev;
2788 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002789 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002790 unsigned int mode;
2791
Stephen Boydd92d95b62012-07-02 19:21:06 -07002792 if (rdev->supply)
2793 input_uV = regulator_get_voltage(rdev->supply);
2794
Liam Girdwood414c70c2008-04-30 15:59:04 +01002795 mutex_lock(&rdev->mutex);
2796
Mark Browna4b41482011-05-14 11:19:45 -07002797 /*
2798 * first check to see if we can set modes at all, otherwise just
2799 * tell the consumer everything is OK.
2800 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002801 regulator->uA_load = uA_load;
2802 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002803 if (ret < 0) {
2804 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002805 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002806 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002807
Liam Girdwood414c70c2008-04-30 15:59:04 +01002808 if (!rdev->desc->ops->get_optimum_mode)
2809 goto out;
2810
Mark Browna4b41482011-05-14 11:19:45 -07002811 /*
2812 * we can actually do this so any errors are indicators of
2813 * potential real failure.
2814 */
2815 ret = -EINVAL;
2816
Axel Lin854ccba2012-04-16 18:44:23 +08002817 if (!rdev->desc->ops->set_mode)
2818 goto out;
2819
Liam Girdwood414c70c2008-04-30 15:59:04 +01002820 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002821 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002822 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002823 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002824 goto out;
2825 }
2826
Stephen Boydd92d95b62012-07-02 19:21:06 -07002827 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002828 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002829 input_uV = rdev->constraints->input_uV;
2830 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002831 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002832 goto out;
2833 }
2834
2835 /* calc total requested load for this regulator */
2836 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002837 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002838
2839 mode = rdev->desc->ops->get_optimum_mode(rdev,
2840 input_uV, output_uV,
2841 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002842 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002843 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002844 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2845 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002846 goto out;
2847 }
2848
2849 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002850 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002851 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852 goto out;
2853 }
2854 ret = mode;
2855out:
2856 mutex_unlock(&rdev->mutex);
2857 return ret;
2858}
2859EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2860
2861/**
Mark Browndf367932012-08-27 16:04:23 -07002862 * regulator_set_bypass_regmap - Default set_bypass() using regmap
2863 *
2864 * @rdev: device to operate on.
2865 * @enable: state to set.
2866 */
2867int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
2868{
2869 unsigned int val;
2870
2871 if (enable)
2872 val = rdev->desc->bypass_mask;
2873 else
2874 val = 0;
2875
2876 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
2877 rdev->desc->bypass_mask, val);
2878}
2879EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
2880
2881/**
2882 * regulator_get_bypass_regmap - Default get_bypass() using regmap
2883 *
2884 * @rdev: device to operate on.
2885 * @enable: current state.
2886 */
2887int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
2888{
2889 unsigned int val;
2890 int ret;
2891
2892 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
2893 if (ret != 0)
2894 return ret;
2895
2896 *enable = val & rdev->desc->bypass_mask;
2897
2898 return 0;
2899}
2900EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
2901
2902/**
Mark Brownf59c8f92012-08-31 10:36:37 -07002903 * regulator_allow_bypass - allow the regulator to go into bypass mode
2904 *
2905 * @regulator: Regulator to configure
2906 * @allow: enable or disable bypass mode
2907 *
2908 * Allow the regulator to go into bypass mode if all other consumers
2909 * for the regulator also enable bypass mode and the machine
2910 * constraints allow this. Bypass mode means that the regulator is
2911 * simply passing the input directly to the output with no regulation.
2912 */
2913int regulator_allow_bypass(struct regulator *regulator, bool enable)
2914{
2915 struct regulator_dev *rdev = regulator->rdev;
2916 int ret = 0;
2917
2918 if (!rdev->desc->ops->set_bypass)
2919 return 0;
2920
2921 if (rdev->constraints &&
2922 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
2923 return 0;
2924
2925 mutex_lock(&rdev->mutex);
2926
2927 if (enable && !regulator->bypass) {
2928 rdev->bypass_count++;
2929
2930 if (rdev->bypass_count == rdev->open_count) {
2931 ret = rdev->desc->ops->set_bypass(rdev, enable);
2932 if (ret != 0)
2933 rdev->bypass_count--;
2934 }
2935
2936 } else if (!enable && regulator->bypass) {
2937 rdev->bypass_count--;
2938
2939 if (rdev->bypass_count != rdev->open_count) {
2940 ret = rdev->desc->ops->set_bypass(rdev, enable);
2941 if (ret != 0)
2942 rdev->bypass_count++;
2943 }
2944 }
2945
2946 if (ret == 0)
2947 regulator->bypass = enable;
2948
2949 mutex_unlock(&rdev->mutex);
2950
2951 return ret;
2952}
2953EXPORT_SYMBOL_GPL(regulator_allow_bypass);
2954
2955/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002956 * regulator_register_notifier - register regulator event notifier
2957 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002958 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002959 *
2960 * Register notifier block to receive regulator events.
2961 */
2962int regulator_register_notifier(struct regulator *regulator,
2963 struct notifier_block *nb)
2964{
2965 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2966 nb);
2967}
2968EXPORT_SYMBOL_GPL(regulator_register_notifier);
2969
2970/**
2971 * regulator_unregister_notifier - unregister regulator event notifier
2972 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002973 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002974 *
2975 * Unregister regulator event notifier block.
2976 */
2977int regulator_unregister_notifier(struct regulator *regulator,
2978 struct notifier_block *nb)
2979{
2980 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2981 nb);
2982}
2983EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2984
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002985/* notify regulator consumers and downstream regulator consumers.
2986 * Note mutex must be held by caller.
2987 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002988static void _notifier_call_chain(struct regulator_dev *rdev,
2989 unsigned long event, void *data)
2990{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002991 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01002992 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002993}
2994
2995/**
2996 * regulator_bulk_get - get multiple regulator consumers
2997 *
2998 * @dev: Device to supply
2999 * @num_consumers: Number of consumers to register
3000 * @consumers: Configuration of consumers; clients are stored here.
3001 *
3002 * @return 0 on success, an errno on failure.
3003 *
3004 * This helper function allows drivers to get several regulator
3005 * consumers in one operation. If any of the regulators cannot be
3006 * acquired then any regulators that were allocated will be freed
3007 * before returning to the caller.
3008 */
3009int regulator_bulk_get(struct device *dev, int num_consumers,
3010 struct regulator_bulk_data *consumers)
3011{
3012 int i;
3013 int ret;
3014
3015 for (i = 0; i < num_consumers; i++)
3016 consumers[i].consumer = NULL;
3017
3018 for (i = 0; i < num_consumers; i++) {
3019 consumers[i].consumer = regulator_get(dev,
3020 consumers[i].supply);
3021 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003022 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003023 dev_err(dev, "Failed to get supply '%s': %d\n",
3024 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003025 consumers[i].consumer = NULL;
3026 goto err;
3027 }
3028 }
3029
3030 return 0;
3031
3032err:
Axel Linb29c7692012-02-20 10:32:16 +08003033 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003034 regulator_put(consumers[i].consumer);
3035
3036 return ret;
3037}
3038EXPORT_SYMBOL_GPL(regulator_bulk_get);
3039
Mark Browne6e74032012-01-20 20:10:08 +00003040/**
3041 * devm_regulator_bulk_get - managed get multiple regulator consumers
3042 *
3043 * @dev: Device to supply
3044 * @num_consumers: Number of consumers to register
3045 * @consumers: Configuration of consumers; clients are stored here.
3046 *
3047 * @return 0 on success, an errno on failure.
3048 *
3049 * This helper function allows drivers to get several regulator
3050 * consumers in one operation with management, the regulators will
3051 * automatically be freed when the device is unbound. If any of the
3052 * regulators cannot be acquired then any regulators that were
3053 * allocated will be freed before returning to the caller.
3054 */
3055int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3056 struct regulator_bulk_data *consumers)
3057{
3058 int i;
3059 int ret;
3060
3061 for (i = 0; i < num_consumers; i++)
3062 consumers[i].consumer = NULL;
3063
3064 for (i = 0; i < num_consumers; i++) {
3065 consumers[i].consumer = devm_regulator_get(dev,
3066 consumers[i].supply);
3067 if (IS_ERR(consumers[i].consumer)) {
3068 ret = PTR_ERR(consumers[i].consumer);
3069 dev_err(dev, "Failed to get supply '%s': %d\n",
3070 consumers[i].supply, ret);
3071 consumers[i].consumer = NULL;
3072 goto err;
3073 }
3074 }
3075
3076 return 0;
3077
3078err:
3079 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3080 devm_regulator_put(consumers[i].consumer);
3081
3082 return ret;
3083}
3084EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3085
Mark Brownf21e0e82011-05-24 08:12:40 +08003086static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3087{
3088 struct regulator_bulk_data *bulk = data;
3089
3090 bulk->ret = regulator_enable(bulk->consumer);
3091}
3092
Liam Girdwood414c70c2008-04-30 15:59:04 +01003093/**
3094 * regulator_bulk_enable - enable multiple regulator consumers
3095 *
3096 * @num_consumers: Number of consumers
3097 * @consumers: Consumer data; clients are stored here.
3098 * @return 0 on success, an errno on failure
3099 *
3100 * This convenience API allows consumers to enable multiple regulator
3101 * clients in a single API call. If any consumers cannot be enabled
3102 * then any others that were enabled will be disabled again prior to
3103 * return.
3104 */
3105int regulator_bulk_enable(int num_consumers,
3106 struct regulator_bulk_data *consumers)
3107{
Dan Williams2955b472012-07-09 19:33:25 -07003108 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003109 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003110 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003111
Mark Brown6492bc12012-04-19 13:19:07 +01003112 for (i = 0; i < num_consumers; i++) {
3113 if (consumers[i].consumer->always_on)
3114 consumers[i].ret = 0;
3115 else
3116 async_schedule_domain(regulator_bulk_enable_async,
3117 &consumers[i], &async_domain);
3118 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003119
3120 async_synchronize_full_domain(&async_domain);
3121
3122 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003123 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003124 if (consumers[i].ret != 0) {
3125 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003126 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003127 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003128 }
3129
3130 return 0;
3131
3132err:
Axel Linb29c7692012-02-20 10:32:16 +08003133 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
3134 while (--i >= 0)
3135 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003136
3137 return ret;
3138}
3139EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3140
3141/**
3142 * regulator_bulk_disable - disable multiple regulator consumers
3143 *
3144 * @num_consumers: Number of consumers
3145 * @consumers: Consumer data; clients are stored here.
3146 * @return 0 on success, an errno on failure
3147 *
3148 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003149 * clients in a single API call. If any consumers cannot be disabled
3150 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003151 * return.
3152 */
3153int regulator_bulk_disable(int num_consumers,
3154 struct regulator_bulk_data *consumers)
3155{
3156 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003157 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003158
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003159 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003160 ret = regulator_disable(consumers[i].consumer);
3161 if (ret != 0)
3162 goto err;
3163 }
3164
3165 return 0;
3166
3167err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003168 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003169 for (++i; i < num_consumers; ++i) {
3170 r = regulator_enable(consumers[i].consumer);
3171 if (r != 0)
3172 pr_err("Failed to reename %s: %d\n",
3173 consumers[i].supply, r);
3174 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003175
3176 return ret;
3177}
3178EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3179
3180/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003181 * regulator_bulk_force_disable - force disable multiple regulator consumers
3182 *
3183 * @num_consumers: Number of consumers
3184 * @consumers: Consumer data; clients are stored here.
3185 * @return 0 on success, an errno on failure
3186 *
3187 * This convenience API allows consumers to forcibly disable multiple regulator
3188 * clients in a single API call.
3189 * NOTE: This should be used for situations when device damage will
3190 * likely occur if the regulators are not disabled (e.g. over temp).
3191 * Although regulator_force_disable function call for some consumers can
3192 * return error numbers, the function is called for all consumers.
3193 */
3194int regulator_bulk_force_disable(int num_consumers,
3195 struct regulator_bulk_data *consumers)
3196{
3197 int i;
3198 int ret;
3199
3200 for (i = 0; i < num_consumers; i++)
3201 consumers[i].ret =
3202 regulator_force_disable(consumers[i].consumer);
3203
3204 for (i = 0; i < num_consumers; i++) {
3205 if (consumers[i].ret != 0) {
3206 ret = consumers[i].ret;
3207 goto out;
3208 }
3209 }
3210
3211 return 0;
3212out:
3213 return ret;
3214}
3215EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3216
3217/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003218 * regulator_bulk_free - free multiple regulator consumers
3219 *
3220 * @num_consumers: Number of consumers
3221 * @consumers: Consumer data; clients are stored here.
3222 *
3223 * This convenience API allows consumers to free multiple regulator
3224 * clients in a single API call.
3225 */
3226void regulator_bulk_free(int num_consumers,
3227 struct regulator_bulk_data *consumers)
3228{
3229 int i;
3230
3231 for (i = 0; i < num_consumers; i++) {
3232 regulator_put(consumers[i].consumer);
3233 consumers[i].consumer = NULL;
3234 }
3235}
3236EXPORT_SYMBOL_GPL(regulator_bulk_free);
3237
3238/**
3239 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003240 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003241 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003242 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003243 *
3244 * Called by regulator drivers to notify clients a regulator event has
3245 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003246 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003247 */
3248int regulator_notifier_call_chain(struct regulator_dev *rdev,
3249 unsigned long event, void *data)
3250{
3251 _notifier_call_chain(rdev, event, data);
3252 return NOTIFY_DONE;
3253
3254}
3255EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3256
Mark Brownbe721972009-08-04 20:09:52 +02003257/**
3258 * regulator_mode_to_status - convert a regulator mode into a status
3259 *
3260 * @mode: Mode to convert
3261 *
3262 * Convert a regulator mode into a status.
3263 */
3264int regulator_mode_to_status(unsigned int mode)
3265{
3266 switch (mode) {
3267 case REGULATOR_MODE_FAST:
3268 return REGULATOR_STATUS_FAST;
3269 case REGULATOR_MODE_NORMAL:
3270 return REGULATOR_STATUS_NORMAL;
3271 case REGULATOR_MODE_IDLE:
3272 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003273 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003274 return REGULATOR_STATUS_STANDBY;
3275 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003276 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003277 }
3278}
3279EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3280
David Brownell7ad68e22008-11-11 17:39:02 -08003281/*
3282 * To avoid cluttering sysfs (and memory) with useless state, only
3283 * create attributes that can be meaningfully displayed.
3284 */
3285static int add_regulator_attributes(struct regulator_dev *rdev)
3286{
3287 struct device *dev = &rdev->dev;
3288 struct regulator_ops *ops = rdev->desc->ops;
3289 int status = 0;
3290
3291 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003292 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003293 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3294 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003295 status = device_create_file(dev, &dev_attr_microvolts);
3296 if (status < 0)
3297 return status;
3298 }
3299 if (ops->get_current_limit) {
3300 status = device_create_file(dev, &dev_attr_microamps);
3301 if (status < 0)
3302 return status;
3303 }
3304 if (ops->get_mode) {
3305 status = device_create_file(dev, &dev_attr_opmode);
3306 if (status < 0)
3307 return status;
3308 }
Michał Mirosław896b65f2013-02-01 20:40:17 +01003309 if (rdev->ena_gpio || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003310 status = device_create_file(dev, &dev_attr_state);
3311 if (status < 0)
3312 return status;
3313 }
David Brownell853116a2009-01-14 23:03:17 -08003314 if (ops->get_status) {
3315 status = device_create_file(dev, &dev_attr_status);
3316 if (status < 0)
3317 return status;
3318 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003319 if (ops->get_bypass) {
3320 status = device_create_file(dev, &dev_attr_bypass);
3321 if (status < 0)
3322 return status;
3323 }
David Brownell7ad68e22008-11-11 17:39:02 -08003324
3325 /* some attributes are type-specific */
3326 if (rdev->desc->type == REGULATOR_CURRENT) {
3327 status = device_create_file(dev, &dev_attr_requested_microamps);
3328 if (status < 0)
3329 return status;
3330 }
3331
3332 /* all the other attributes exist to support constraints;
3333 * don't show them if there are no constraints, or if the
3334 * relevant supporting methods are missing.
3335 */
3336 if (!rdev->constraints)
3337 return status;
3338
3339 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003340 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003341 status = device_create_file(dev, &dev_attr_min_microvolts);
3342 if (status < 0)
3343 return status;
3344 status = device_create_file(dev, &dev_attr_max_microvolts);
3345 if (status < 0)
3346 return status;
3347 }
3348 if (ops->set_current_limit) {
3349 status = device_create_file(dev, &dev_attr_min_microamps);
3350 if (status < 0)
3351 return status;
3352 status = device_create_file(dev, &dev_attr_max_microamps);
3353 if (status < 0)
3354 return status;
3355 }
3356
David Brownell7ad68e22008-11-11 17:39:02 -08003357 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3358 if (status < 0)
3359 return status;
3360 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3361 if (status < 0)
3362 return status;
3363 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3364 if (status < 0)
3365 return status;
3366
3367 if (ops->set_suspend_voltage) {
3368 status = device_create_file(dev,
3369 &dev_attr_suspend_standby_microvolts);
3370 if (status < 0)
3371 return status;
3372 status = device_create_file(dev,
3373 &dev_attr_suspend_mem_microvolts);
3374 if (status < 0)
3375 return status;
3376 status = device_create_file(dev,
3377 &dev_attr_suspend_disk_microvolts);
3378 if (status < 0)
3379 return status;
3380 }
3381
3382 if (ops->set_suspend_mode) {
3383 status = device_create_file(dev,
3384 &dev_attr_suspend_standby_mode);
3385 if (status < 0)
3386 return status;
3387 status = device_create_file(dev,
3388 &dev_attr_suspend_mem_mode);
3389 if (status < 0)
3390 return status;
3391 status = device_create_file(dev,
3392 &dev_attr_suspend_disk_mode);
3393 if (status < 0)
3394 return status;
3395 }
3396
3397 return status;
3398}
3399
Mark Brown1130e5b2010-12-21 23:49:31 +00003400static void rdev_init_debugfs(struct regulator_dev *rdev)
3401{
Mark Brown1130e5b2010-12-21 23:49:31 +00003402 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003403 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003404 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003405 return;
3406 }
3407
3408 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3409 &rdev->use_count);
3410 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3411 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003412 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3413 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003414}
3415
Liam Girdwood414c70c2008-04-30 15:59:04 +01003416/**
3417 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003418 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003419 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003420 *
3421 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003422 * Returns a valid pointer to struct regulator_dev on success
3423 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003424 */
Mark Brown65f26842012-04-03 20:46:53 +01003425struct regulator_dev *
3426regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003427 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003428{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003429 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003430 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003431 static atomic_t regulator_no = ATOMIC_INIT(0);
3432 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003433 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003434 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303435 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003436
Mark Brownc1727082012-04-04 00:50:22 +01003437 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003438 return ERR_PTR(-EINVAL);
3439
Mark Brown32c8fad2012-04-11 10:19:12 +01003440 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003441 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003442
Liam Girdwood414c70c2008-04-30 15:59:04 +01003443 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3444 return ERR_PTR(-EINVAL);
3445
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003446 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3447 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003448 return ERR_PTR(-EINVAL);
3449
Mark Brown476c2d82010-12-10 17:28:07 +00003450 /* Only one of each should be implemented */
3451 WARN_ON(regulator_desc->ops->get_voltage &&
3452 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003453 WARN_ON(regulator_desc->ops->set_voltage &&
3454 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003455
3456 /* If we're using selectors we must implement list_voltage. */
3457 if (regulator_desc->ops->get_voltage_sel &&
3458 !regulator_desc->ops->list_voltage) {
3459 return ERR_PTR(-EINVAL);
3460 }
Mark Browne8eef822010-12-12 14:36:17 +00003461 if (regulator_desc->ops->set_voltage_sel &&
3462 !regulator_desc->ops->list_voltage) {
3463 return ERR_PTR(-EINVAL);
3464 }
Mark Brown476c2d82010-12-10 17:28:07 +00003465
Mark Brownc1727082012-04-04 00:50:22 +01003466 init_data = config->init_data;
3467
Liam Girdwood414c70c2008-04-30 15:59:04 +01003468 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3469 if (rdev == NULL)
3470 return ERR_PTR(-ENOMEM);
3471
3472 mutex_lock(&regulator_list_mutex);
3473
3474 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003475 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003476 rdev->owner = regulator_desc->owner;
3477 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003478 if (config->regmap)
3479 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303480 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003481 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303482 else if (dev->parent)
3483 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003484 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003485 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003486 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003487 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003488
Liam Girdwooda5766f12008-10-10 13:22:20 +01003489 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003490 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003491 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003492 if (ret < 0)
3493 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003494 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003495
Liam Girdwooda5766f12008-10-10 13:22:20 +01003496 /* register with sysfs */
3497 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003498 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003499 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003500 dev_set_name(&rdev->dev, "regulator.%d",
3501 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003502 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003503 if (ret != 0) {
3504 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003505 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003506 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003507
3508 dev_set_drvdata(&rdev->dev, rdev);
3509
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003510 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003511 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003512 if (ret != 0) {
3513 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3514 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003515 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003516 }
3517
Mark Brown65f73502012-06-27 14:14:38 +01003518 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3519 rdev->ena_gpio_state = 1;
3520
3521 if (rdev->ena_gpio_invert)
3522 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3523 }
3524
Mike Rapoport74f544c2008-11-25 14:53:53 +02003525 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003526 if (init_data)
3527 constraints = &init_data->constraints;
3528
3529 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003530 if (ret < 0)
3531 goto scrub;
3532
David Brownell7ad68e22008-11-11 17:39:02 -08003533 /* add attributes supported by this regulator */
3534 ret = add_regulator_attributes(rdev);
3535 if (ret < 0)
3536 goto scrub;
3537
Mark Brown9a8f5e02011-11-29 18:11:19 +00003538 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303539 supply = init_data->supply_regulator;
3540 else if (regulator_desc->supply_name)
3541 supply = regulator_desc->supply_name;
3542
3543 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003544 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003545
Mark Brown6d191a52012-03-29 14:19:02 +01003546 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003547
Rajendra Nayak69511a42011-11-18 16:47:20 +05303548 if (!r) {
3549 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003550 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003551 goto scrub;
3552 }
3553
3554 ret = set_supply(rdev, r);
3555 if (ret < 0)
3556 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303557
3558 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003559 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303560 ret = regulator_enable(rdev->supply);
3561 if (ret < 0)
3562 goto scrub;
3563 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003564 }
3565
Liam Girdwooda5766f12008-10-10 13:22:20 +01003566 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003567 if (init_data) {
3568 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3569 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003570 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003571 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003572 if (ret < 0) {
3573 dev_err(dev, "Failed to set supply %s\n",
3574 init_data->consumer_supplies[i].supply);
3575 goto unset_supplies;
3576 }
Mark Brown23c2f042011-02-24 17:39:09 +00003577 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003578 }
3579
3580 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003581
3582 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003583out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003584 mutex_unlock(&regulator_list_mutex);
3585 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003586
Jani Nikulad4033b52010-04-29 10:55:11 +03003587unset_supplies:
3588 unset_regulator_supplies(rdev);
3589
David Brownell4fca9542008-11-11 17:38:53 -08003590scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003591 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003592 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003593 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003594 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003595wash:
David Brownell4fca9542008-11-11 17:38:53 -08003596 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003597 /* device core frees rdev */
3598 rdev = ERR_PTR(ret);
3599 goto out;
3600
David Brownell4fca9542008-11-11 17:38:53 -08003601clean:
3602 kfree(rdev);
3603 rdev = ERR_PTR(ret);
3604 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003605}
3606EXPORT_SYMBOL_GPL(regulator_register);
3607
3608/**
3609 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003610 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003611 *
3612 * Called by regulator drivers to unregister a regulator.
3613 */
3614void regulator_unregister(struct regulator_dev *rdev)
3615{
3616 if (rdev == NULL)
3617 return;
3618
Mark Browne032b372012-03-28 21:17:55 +01003619 if (rdev->supply)
3620 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003621 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003622 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003623 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003624 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003625 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003626 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003627 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003628 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003629 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003630 mutex_unlock(&regulator_list_mutex);
3631}
3632EXPORT_SYMBOL_GPL(regulator_unregister);
3633
3634/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003635 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003636 * @state: system suspend state
3637 *
3638 * Configure each regulator with it's suspend operating parameters for state.
3639 * This will usually be called by machine suspend code prior to supending.
3640 */
3641int regulator_suspend_prepare(suspend_state_t state)
3642{
3643 struct regulator_dev *rdev;
3644 int ret = 0;
3645
3646 /* ON is handled by regulator active state */
3647 if (state == PM_SUSPEND_ON)
3648 return -EINVAL;
3649
3650 mutex_lock(&regulator_list_mutex);
3651 list_for_each_entry(rdev, &regulator_list, list) {
3652
3653 mutex_lock(&rdev->mutex);
3654 ret = suspend_prepare(rdev, state);
3655 mutex_unlock(&rdev->mutex);
3656
3657 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003658 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003659 goto out;
3660 }
3661 }
3662out:
3663 mutex_unlock(&regulator_list_mutex);
3664 return ret;
3665}
3666EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3667
3668/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003669 * regulator_suspend_finish - resume regulators from system wide suspend
3670 *
3671 * Turn on regulators that might be turned off by regulator_suspend_prepare
3672 * and that should be turned on according to the regulators properties.
3673 */
3674int regulator_suspend_finish(void)
3675{
3676 struct regulator_dev *rdev;
3677 int ret = 0, error;
3678
3679 mutex_lock(&regulator_list_mutex);
3680 list_for_each_entry(rdev, &regulator_list, list) {
3681 struct regulator_ops *ops = rdev->desc->ops;
3682
3683 mutex_lock(&rdev->mutex);
3684 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3685 ops->enable) {
3686 error = ops->enable(rdev);
3687 if (error)
3688 ret = error;
3689 } else {
3690 if (!has_full_constraints)
3691 goto unlock;
3692 if (!ops->disable)
3693 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003694 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003695 goto unlock;
3696
3697 error = ops->disable(rdev);
3698 if (error)
3699 ret = error;
3700 }
3701unlock:
3702 mutex_unlock(&rdev->mutex);
3703 }
3704 mutex_unlock(&regulator_list_mutex);
3705 return ret;
3706}
3707EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3708
3709/**
Mark Brownca725562009-03-16 19:36:34 +00003710 * regulator_has_full_constraints - the system has fully specified constraints
3711 *
3712 * Calling this function will cause the regulator API to disable all
3713 * regulators which have a zero use count and don't have an always_on
3714 * constraint in a late_initcall.
3715 *
3716 * The intention is that this will become the default behaviour in a
3717 * future kernel release so users are encouraged to use this facility
3718 * now.
3719 */
3720void regulator_has_full_constraints(void)
3721{
3722 has_full_constraints = 1;
3723}
3724EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3725
3726/**
Mark Brown688fe992010-10-05 19:18:32 -07003727 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3728 *
3729 * Calling this function will cause the regulator API to provide a
3730 * dummy regulator to consumers if no physical regulator is found,
3731 * allowing most consumers to proceed as though a regulator were
3732 * configured. This allows systems such as those with software
3733 * controllable regulators for the CPU core only to be brought up more
3734 * readily.
3735 */
3736void regulator_use_dummy_regulator(void)
3737{
3738 board_wants_dummy_regulator = true;
3739}
3740EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3741
3742/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003743 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003744 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003745 *
3746 * Get rdev regulator driver private data. This call can be used in the
3747 * regulator driver context.
3748 */
3749void *rdev_get_drvdata(struct regulator_dev *rdev)
3750{
3751 return rdev->reg_data;
3752}
3753EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3754
3755/**
3756 * regulator_get_drvdata - get regulator driver data
3757 * @regulator: regulator
3758 *
3759 * Get regulator driver private data. This call can be used in the consumer
3760 * driver context when non API regulator specific functions need to be called.
3761 */
3762void *regulator_get_drvdata(struct regulator *regulator)
3763{
3764 return regulator->rdev->reg_data;
3765}
3766EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3767
3768/**
3769 * regulator_set_drvdata - set regulator driver data
3770 * @regulator: regulator
3771 * @data: data
3772 */
3773void regulator_set_drvdata(struct regulator *regulator, void *data)
3774{
3775 regulator->rdev->reg_data = data;
3776}
3777EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3778
3779/**
3780 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003781 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003782 */
3783int rdev_get_id(struct regulator_dev *rdev)
3784{
3785 return rdev->desc->id;
3786}
3787EXPORT_SYMBOL_GPL(rdev_get_id);
3788
Liam Girdwooda5766f12008-10-10 13:22:20 +01003789struct device *rdev_get_dev(struct regulator_dev *rdev)
3790{
3791 return &rdev->dev;
3792}
3793EXPORT_SYMBOL_GPL(rdev_get_dev);
3794
3795void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3796{
3797 return reg_init_data->driver_data;
3798}
3799EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3800
Mark Brownba55a972011-08-23 17:39:10 +01003801#ifdef CONFIG_DEBUG_FS
3802static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3803 size_t count, loff_t *ppos)
3804{
3805 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3806 ssize_t len, ret = 0;
3807 struct regulator_map *map;
3808
3809 if (!buf)
3810 return -ENOMEM;
3811
3812 list_for_each_entry(map, &regulator_map_list, list) {
3813 len = snprintf(buf + ret, PAGE_SIZE - ret,
3814 "%s -> %s.%s\n",
3815 rdev_get_name(map->regulator), map->dev_name,
3816 map->supply);
3817 if (len >= 0)
3818 ret += len;
3819 if (ret > PAGE_SIZE) {
3820 ret = PAGE_SIZE;
3821 break;
3822 }
3823 }
3824
3825 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3826
3827 kfree(buf);
3828
3829 return ret;
3830}
Stephen Boyd24751432012-02-20 22:50:42 -08003831#endif
Mark Brownba55a972011-08-23 17:39:10 +01003832
3833static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003834#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003835 .read = supply_map_read_file,
3836 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003837#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003838};
Mark Brownba55a972011-08-23 17:39:10 +01003839
Liam Girdwood414c70c2008-04-30 15:59:04 +01003840static int __init regulator_init(void)
3841{
Mark Brown34abbd62010-02-12 10:18:08 +00003842 int ret;
3843
Mark Brown34abbd62010-02-12 10:18:08 +00003844 ret = class_register(&regulator_class);
3845
Mark Brown1130e5b2010-12-21 23:49:31 +00003846 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003847 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003848 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003849
Mark Brownf4d562c2012-02-20 21:01:04 +00003850 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3851 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003852
Mark Brown34abbd62010-02-12 10:18:08 +00003853 regulator_dummy_init();
3854
3855 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003856}
3857
3858/* init early to allow our consumers to complete system booting */
3859core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003860
3861static int __init regulator_init_complete(void)
3862{
3863 struct regulator_dev *rdev;
3864 struct regulator_ops *ops;
3865 struct regulation_constraints *c;
3866 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003867
Mark Brown86f5fcf2012-07-06 18:19:13 +01003868 /*
3869 * Since DT doesn't provide an idiomatic mechanism for
3870 * enabling full constraints and since it's much more natural
3871 * with DT to provide them just assume that a DT enabled
3872 * system has full constraints.
3873 */
3874 if (of_have_populated_dt())
3875 has_full_constraints = true;
3876
Mark Brownca725562009-03-16 19:36:34 +00003877 mutex_lock(&regulator_list_mutex);
3878
3879 /* If we have a full configuration then disable any regulators
3880 * which are not in use or always_on. This will become the
3881 * default behaviour in the future.
3882 */
3883 list_for_each_entry(rdev, &regulator_list, list) {
3884 ops = rdev->desc->ops;
3885 c = rdev->constraints;
3886
Mark Brownf25e0b42009-08-03 18:49:55 +01003887 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003888 continue;
3889
3890 mutex_lock(&rdev->mutex);
3891
3892 if (rdev->use_count)
3893 goto unlock;
3894
3895 /* If we can't read the status assume it's on. */
3896 if (ops->is_enabled)
3897 enabled = ops->is_enabled(rdev);
3898 else
3899 enabled = 1;
3900
3901 if (!enabled)
3902 goto unlock;
3903
3904 if (has_full_constraints) {
3905 /* We log since this may kill the system if it
3906 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003907 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003908 ret = ops->disable(rdev);
3909 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003910 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003911 }
3912 } else {
3913 /* The intention is that in future we will
3914 * assume that full constraints are provided
3915 * so warn even if we aren't going to do
3916 * anything here.
3917 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003918 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003919 }
3920
3921unlock:
3922 mutex_unlock(&rdev->mutex);
3923 }
3924
3925 mutex_unlock(&regulator_list_mutex);
3926
3927 return 0;
3928}
3929late_initcall(regulator_init_complete);