blob: a27a5b6267dde117c8273877c0b12c5b1a498847 [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.
Maxime Ripard167d41d2013-03-23 11:00:41 +0100133 * returns the device node corresponding to the regulator if found, else
Rajendra Nayak69511a42011-11-18 16:47:20 +0530134 * 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;
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001246 int ret = 0;
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
Nishanth Menon1e4b5452013-04-16 16:45:16 -05001262 /*
1263 * If we have return value from dev_lookup fail, we do not expect to
1264 * succeed, so, quit with appropriate error value
1265 */
1266 if (ret) {
1267 regulator = ERR_PTR(ret);
1268 goto out;
1269 }
1270
Mark Brown688fe992010-10-05 19:18:32 -07001271 if (board_wants_dummy_regulator) {
1272 rdev = dummy_regulator_rdev;
1273 goto found;
1274 }
1275
Mark Brown34abbd62010-02-12 10:18:08 +00001276#ifdef CONFIG_REGULATOR_DUMMY
1277 if (!devname)
1278 devname = "deviceless";
1279
1280 /* If the board didn't flag that it was fully constrained then
1281 * substitute in a dummy regulator so consumers can continue.
1282 */
1283 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001284 pr_warn("%s supply %s not found, using dummy regulator\n",
1285 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001286 rdev = dummy_regulator_rdev;
1287 goto found;
1288 }
1289#endif
1290
Liam Girdwood414c70c2008-04-30 15:59:04 +01001291 mutex_unlock(&regulator_list_mutex);
1292 return regulator;
1293
1294found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001295 if (rdev->exclusive) {
1296 regulator = ERR_PTR(-EPERM);
1297 goto out;
1298 }
1299
1300 if (exclusive && rdev->open_count) {
1301 regulator = ERR_PTR(-EBUSY);
1302 goto out;
1303 }
1304
Liam Girdwooda5766f12008-10-10 13:22:20 +01001305 if (!try_module_get(rdev->owner))
1306 goto out;
1307
Liam Girdwood414c70c2008-04-30 15:59:04 +01001308 regulator = create_regulator(rdev, dev, id);
1309 if (regulator == NULL) {
1310 regulator = ERR_PTR(-ENOMEM);
1311 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001312 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001313 }
1314
Mark Brown5ffbd132009-07-21 16:00:23 +01001315 rdev->open_count++;
1316 if (exclusive) {
1317 rdev->exclusive = 1;
1318
1319 ret = _regulator_is_enabled(rdev);
1320 if (ret > 0)
1321 rdev->use_count = 1;
1322 else
1323 rdev->use_count = 0;
1324 }
1325
Liam Girdwooda5766f12008-10-10 13:22:20 +01001326out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001327 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001328
Liam Girdwood414c70c2008-04-30 15:59:04 +01001329 return regulator;
1330}
Mark Brown5ffbd132009-07-21 16:00:23 +01001331
1332/**
1333 * regulator_get - lookup and obtain a reference to a regulator.
1334 * @dev: device for regulator "consumer"
1335 * @id: Supply name or regulator ID.
1336 *
1337 * Returns a struct regulator corresponding to the regulator producer,
1338 * or IS_ERR() condition containing errno.
1339 *
1340 * Use of supply names configured via regulator_set_device_supply() is
1341 * strongly encouraged. It is recommended that the supply name used
1342 * should match the name used for the supply and/or the relevant
1343 * device pins in the datasheet.
1344 */
1345struct regulator *regulator_get(struct device *dev, const char *id)
1346{
1347 return _regulator_get(dev, id, 0);
1348}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349EXPORT_SYMBOL_GPL(regulator_get);
1350
Stephen Boyd070b9072012-01-16 19:39:58 -08001351static void devm_regulator_release(struct device *dev, void *res)
1352{
1353 regulator_put(*(struct regulator **)res);
1354}
1355
1356/**
1357 * devm_regulator_get - Resource managed regulator_get()
1358 * @dev: device for regulator "consumer"
1359 * @id: Supply name or regulator ID.
1360 *
1361 * Managed regulator_get(). Regulators returned from this function are
1362 * automatically regulator_put() on driver detach. See regulator_get() for more
1363 * information.
1364 */
1365struct regulator *devm_regulator_get(struct device *dev, const char *id)
1366{
1367 struct regulator **ptr, *regulator;
1368
1369 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1370 if (!ptr)
1371 return ERR_PTR(-ENOMEM);
1372
1373 regulator = regulator_get(dev, id);
1374 if (!IS_ERR(regulator)) {
1375 *ptr = regulator;
1376 devres_add(dev, ptr);
1377 } else {
1378 devres_free(ptr);
1379 }
1380
1381 return regulator;
1382}
1383EXPORT_SYMBOL_GPL(devm_regulator_get);
1384
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001386 * regulator_get_exclusive - obtain exclusive access to a regulator.
1387 * @dev: device for regulator "consumer"
1388 * @id: Supply name or regulator ID.
1389 *
1390 * Returns a struct regulator corresponding to the regulator producer,
1391 * or IS_ERR() condition containing errno. Other consumers will be
1392 * unable to obtain this reference is held and the use count for the
1393 * regulator will be initialised to reflect the current state of the
1394 * regulator.
1395 *
1396 * This is intended for use by consumers which cannot tolerate shared
1397 * use of the regulator such as those which need to force the
1398 * regulator off for correct operation of the hardware they are
1399 * controlling.
1400 *
1401 * Use of supply names configured via regulator_set_device_supply() is
1402 * strongly encouraged. It is recommended that the supply name used
1403 * should match the name used for the supply and/or the relevant
1404 * device pins in the datasheet.
1405 */
1406struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1407{
1408 return _regulator_get(dev, id, 1);
1409}
1410EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1411
Mark Brownde1dd9f2013-07-29 21:42:42 +01001412/**
1413 * regulator_get_optional - obtain optional access to a regulator.
1414 * @dev: device for regulator "consumer"
1415 * @id: Supply name or regulator ID.
1416 *
1417 * Returns a struct regulator corresponding to the regulator producer,
1418 * or IS_ERR() condition containing errno. Other consumers will be
1419 * unable to obtain this reference is held and the use count for the
1420 * regulator will be initialised to reflect the current state of the
1421 * regulator.
1422 *
1423 * This is intended for use by consumers for devices which can have
1424 * some supplies unconnected in normal use, such as some MMC devices.
1425 * It can allow the regulator core to provide stub supplies for other
1426 * supplies requested using normal regulator_get() calls without
1427 * disrupting the operation of drivers that can handle absent
1428 * supplies.
1429 *
1430 * Use of supply names configured via regulator_set_device_supply() is
1431 * strongly encouraged. It is recommended that the supply name used
1432 * should match the name used for the supply and/or the relevant
1433 * device pins in the datasheet.
1434 */
1435struct regulator *regulator_get_optional(struct device *dev, const char *id)
1436{
1437 return _regulator_get(dev, id, 0);
1438}
1439EXPORT_SYMBOL_GPL(regulator_get_optional);
1440
1441/**
1442 * devm_regulator_get_optional - Resource managed regulator_get_optional()
1443 * @dev: device for regulator "consumer"
1444 * @id: Supply name or regulator ID.
1445 *
1446 * Managed regulator_get_optional(). Regulators returned from this
1447 * function are automatically regulator_put() on driver detach. See
1448 * regulator_get_optional() for more information.
1449 */
1450struct regulator *devm_regulator_get_optional(struct device *dev,
1451 const char *id)
1452{
1453 struct regulator **ptr, *regulator;
1454
1455 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1456 if (!ptr)
1457 return ERR_PTR(-ENOMEM);
1458
1459 regulator = regulator_get_optional(dev, id);
1460 if (!IS_ERR(regulator)) {
1461 *ptr = regulator;
1462 devres_add(dev, ptr);
1463 } else {
1464 devres_free(ptr);
1465 }
1466
1467 return regulator;
1468}
1469EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
1470
Charles Keepax23ff2f02012-11-14 09:39:31 +00001471/* Locks held by regulator_put() */
1472static void _regulator_put(struct regulator *regulator)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001473{
1474 struct regulator_dev *rdev;
1475
1476 if (regulator == NULL || IS_ERR(regulator))
1477 return;
1478
Liam Girdwood414c70c2008-04-30 15:59:04 +01001479 rdev = regulator->rdev;
1480
Mark Brown5de70512011-06-19 13:33:16 +01001481 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001482
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483 /* remove any sysfs entries */
Shawn Guoe2c98ea2012-07-05 14:19:42 +08001484 if (regulator->dev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Mark Brown5de70512011-06-19 13:33:16 +01001486 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001487 list_del(&regulator->list);
1488 kfree(regulator);
1489
Mark Brown5ffbd132009-07-21 16:00:23 +01001490 rdev->open_count--;
1491 rdev->exclusive = 0;
1492
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493 module_put(rdev->owner);
Charles Keepax23ff2f02012-11-14 09:39:31 +00001494}
1495
1496/**
1497 * regulator_put - "free" the regulator source
1498 * @regulator: regulator source
1499 *
1500 * Note: drivers must ensure that all regulator_enable calls made on this
1501 * regulator source are balanced by regulator_disable calls prior to calling
1502 * this function.
1503 */
1504void regulator_put(struct regulator *regulator)
1505{
1506 mutex_lock(&regulator_list_mutex);
1507 _regulator_put(regulator);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508 mutex_unlock(&regulator_list_mutex);
1509}
1510EXPORT_SYMBOL_GPL(regulator_put);
1511
Mark Brownd5ad34f2012-01-20 20:09:18 +00001512static int devm_regulator_match(struct device *dev, void *res, void *data)
1513{
1514 struct regulator **r = res;
1515 if (!r || !*r) {
1516 WARN_ON(!r || !*r);
1517 return 0;
1518 }
1519 return *r == data;
1520}
1521
1522/**
1523 * devm_regulator_put - Resource managed regulator_put()
1524 * @regulator: regulator to free
1525 *
1526 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1527 * this function will not need to be called and the resource management
1528 * code will ensure that the resource is freed.
1529 */
1530void devm_regulator_put(struct regulator *regulator)
1531{
1532 int rc;
1533
Mark Brown361ff502012-05-07 14:14:30 +01001534 rc = devres_release(regulator->dev, devm_regulator_release,
Mark Brownd5ad34f2012-01-20 20:09:18 +00001535 devm_regulator_match, regulator);
Mark Brown230a5a1c2012-06-15 18:25:08 +01001536 if (rc != 0)
Mark Brown968c2c12012-05-07 11:34:52 +01001537 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001538}
1539EXPORT_SYMBOL_GPL(devm_regulator_put);
1540
Kim, Milof19b00d2013-02-18 06:50:39 +00001541/* Manage enable GPIO list. Same GPIO pin can be shared among regulators */
1542static int regulator_ena_gpio_request(struct regulator_dev *rdev,
1543 const struct regulator_config *config)
1544{
1545 struct regulator_enable_gpio *pin;
1546 int ret;
1547
1548 list_for_each_entry(pin, &regulator_ena_gpio_list, list) {
1549 if (pin->gpio == config->ena_gpio) {
1550 rdev_dbg(rdev, "GPIO %d is already used\n",
1551 config->ena_gpio);
1552 goto update_ena_gpio_to_rdev;
1553 }
1554 }
1555
1556 ret = gpio_request_one(config->ena_gpio,
1557 GPIOF_DIR_OUT | config->ena_gpio_flags,
1558 rdev_get_name(rdev));
1559 if (ret)
1560 return ret;
1561
1562 pin = kzalloc(sizeof(struct regulator_enable_gpio), GFP_KERNEL);
1563 if (pin == NULL) {
1564 gpio_free(config->ena_gpio);
1565 return -ENOMEM;
1566 }
1567
1568 pin->gpio = config->ena_gpio;
1569 pin->ena_gpio_invert = config->ena_gpio_invert;
1570 list_add(&pin->list, &regulator_ena_gpio_list);
1571
1572update_ena_gpio_to_rdev:
1573 pin->request_count++;
1574 rdev->ena_pin = pin;
1575 return 0;
1576}
1577
1578static void regulator_ena_gpio_free(struct regulator_dev *rdev)
1579{
1580 struct regulator_enable_gpio *pin, *n;
1581
1582 if (!rdev->ena_pin)
1583 return;
1584
1585 /* Free the GPIO only in case of no use */
1586 list_for_each_entry_safe(pin, n, &regulator_ena_gpio_list, list) {
1587 if (pin->gpio == rdev->ena_pin->gpio) {
1588 if (pin->request_count <= 1) {
1589 pin->request_count = 0;
1590 gpio_free(pin->gpio);
1591 list_del(&pin->list);
1592 kfree(pin);
1593 } else {
1594 pin->request_count--;
1595 }
1596 }
1597 }
1598}
1599
Kim, Milo967cfb12013-02-18 06:50:48 +00001600/**
Robert P. J. Day31d6eeb2013-05-02 10:19:11 -04001601 * regulator_ena_gpio_ctrl - balance enable_count of each GPIO and actual GPIO pin control
1602 * @rdev: regulator_dev structure
1603 * @enable: enable GPIO at initial use?
1604 *
Kim, Milo967cfb12013-02-18 06:50:48 +00001605 * GPIO is enabled in case of initial use. (enable_count is 0)
1606 * GPIO is disabled when it is not shared any more. (enable_count <= 1)
1607 */
1608static int regulator_ena_gpio_ctrl(struct regulator_dev *rdev, bool enable)
1609{
1610 struct regulator_enable_gpio *pin = rdev->ena_pin;
1611
1612 if (!pin)
1613 return -EINVAL;
1614
1615 if (enable) {
1616 /* Enable GPIO at initial use */
1617 if (pin->enable_count == 0)
1618 gpio_set_value_cansleep(pin->gpio,
1619 !pin->ena_gpio_invert);
1620
1621 pin->enable_count++;
1622 } else {
1623 if (pin->enable_count > 1) {
1624 pin->enable_count--;
1625 return 0;
1626 }
1627
1628 /* Disable GPIO if not used */
1629 if (pin->enable_count <= 1) {
1630 gpio_set_value_cansleep(pin->gpio,
1631 pin->ena_gpio_invert);
1632 pin->enable_count = 0;
1633 }
1634 }
1635
1636 return 0;
1637}
1638
Mark Brown5c5659d2012-06-27 13:21:26 +01001639static int _regulator_do_enable(struct regulator_dev *rdev)
1640{
1641 int ret, delay;
1642
1643 /* Query before enabling in case configuration dependent. */
1644 ret = _regulator_get_enable_time(rdev);
1645 if (ret >= 0) {
1646 delay = ret;
1647 } else {
1648 rdev_warn(rdev, "enable_time() failed: %d\n", ret);
1649 delay = 0;
1650 }
1651
1652 trace_regulator_enable(rdev_get_name(rdev));
1653
Kim, Milo967cfb12013-02-18 06:50:48 +00001654 if (rdev->ena_pin) {
1655 ret = regulator_ena_gpio_ctrl(rdev, true);
1656 if (ret < 0)
1657 return ret;
Mark Brown65f73502012-06-27 14:14:38 +01001658 rdev->ena_gpio_state = 1;
1659 } else if (rdev->desc->ops->enable) {
Mark Brown5c5659d2012-06-27 13:21:26 +01001660 ret = rdev->desc->ops->enable(rdev);
1661 if (ret < 0)
1662 return ret;
1663 } else {
1664 return -EINVAL;
1665 }
1666
1667 /* Allow the regulator to ramp; it would be useful to extend
1668 * this for bulk operations so that the regulators can ramp
1669 * together. */
1670 trace_regulator_enable_delay(rdev_get_name(rdev));
1671
1672 if (delay >= 1000) {
1673 mdelay(delay / 1000);
1674 udelay(delay % 1000);
1675 } else if (delay) {
1676 udelay(delay);
1677 }
1678
1679 trace_regulator_enable_complete(rdev_get_name(rdev));
1680
1681 return 0;
1682}
1683
Liam Girdwood414c70c2008-04-30 15:59:04 +01001684/* locks held by regulator_enable() */
1685static int _regulator_enable(struct regulator_dev *rdev)
1686{
Mark Brown5c5659d2012-06-27 13:21:26 +01001687 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001688
Liam Girdwood414c70c2008-04-30 15:59:04 +01001689 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001690 if (rdev->constraints &&
1691 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1692 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001693
Mark Brown9a2372f2009-08-03 18:49:57 +01001694 if (rdev->use_count == 0) {
1695 /* The regulator may on if it's not switchable or left on */
1696 ret = _regulator_is_enabled(rdev);
1697 if (ret == -EINVAL || ret == 0) {
1698 if (!_regulator_can_change_status(rdev))
1699 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001700
Mark Brown5c5659d2012-06-27 13:21:26 +01001701 ret = _regulator_do_enable(rdev);
Mark Brown31aae2b2009-12-21 12:21:52 +00001702 if (ret < 0)
1703 return ret;
1704
Linus Walleija7433cf2009-08-26 12:54:04 +02001705 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001706 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001707 return ret;
1708 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001709 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001710 }
1711
Mark Brown9a2372f2009-08-03 18:49:57 +01001712 rdev->use_count++;
1713
1714 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001715}
1716
1717/**
1718 * regulator_enable - enable regulator output
1719 * @regulator: regulator source
1720 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001721 * Request that the regulator be enabled with the regulator output at
1722 * the predefined voltage or current value. Calls to regulator_enable()
1723 * must be balanced with calls to regulator_disable().
1724 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001725 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001726 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001727 */
1728int regulator_enable(struct regulator *regulator)
1729{
David Brownell412aec62008-11-16 11:44:46 -08001730 struct regulator_dev *rdev = regulator->rdev;
1731 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001732
Mark Brown6492bc12012-04-19 13:19:07 +01001733 if (regulator->always_on)
1734 return 0;
1735
Mark Brown3801b862011-06-09 16:22:22 +01001736 if (rdev->supply) {
1737 ret = regulator_enable(rdev->supply);
1738 if (ret != 0)
1739 return ret;
1740 }
1741
David Brownell412aec62008-11-16 11:44:46 -08001742 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001743 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001744 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001745
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001746 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001747 regulator_disable(rdev->supply);
1748
Liam Girdwood414c70c2008-04-30 15:59:04 +01001749 return ret;
1750}
1751EXPORT_SYMBOL_GPL(regulator_enable);
1752
Mark Brown5c5659d2012-06-27 13:21:26 +01001753static int _regulator_do_disable(struct regulator_dev *rdev)
1754{
1755 int ret;
1756
1757 trace_regulator_disable(rdev_get_name(rdev));
1758
Kim, Milo967cfb12013-02-18 06:50:48 +00001759 if (rdev->ena_pin) {
1760 ret = regulator_ena_gpio_ctrl(rdev, false);
1761 if (ret < 0)
1762 return ret;
Mark Brown5c5659d2012-06-27 13:21:26 +01001763 rdev->ena_gpio_state = 0;
1764
1765 } else if (rdev->desc->ops->disable) {
1766 ret = rdev->desc->ops->disable(rdev);
1767 if (ret != 0)
1768 return ret;
1769 }
1770
1771 trace_regulator_disable_complete(rdev_get_name(rdev));
1772
1773 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1774 NULL);
1775 return 0;
1776}
1777
Liam Girdwood414c70c2008-04-30 15:59:04 +01001778/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001779static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001780{
1781 int ret = 0;
1782
David Brownellcd94b502009-03-11 16:43:34 -08001783 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001784 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001785 return -EIO;
1786
Liam Girdwood414c70c2008-04-30 15:59:04 +01001787 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001788 if (rdev->use_count == 1 &&
1789 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001790
1791 /* we are last user */
Mark Brown5c5659d2012-06-27 13:21:26 +01001792 if (_regulator_can_change_status(rdev)) {
1793 ret = _regulator_do_disable(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001794 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001795 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001796 return ret;
1797 }
1798 }
1799
Liam Girdwood414c70c2008-04-30 15:59:04 +01001800 rdev->use_count = 0;
1801 } else if (rdev->use_count > 1) {
1802
1803 if (rdev->constraints &&
1804 (rdev->constraints->valid_ops_mask &
1805 REGULATOR_CHANGE_DRMS))
1806 drms_uA_update(rdev);
1807
1808 rdev->use_count--;
1809 }
Mark Brown3801b862011-06-09 16:22:22 +01001810
Liam Girdwood414c70c2008-04-30 15:59:04 +01001811 return ret;
1812}
1813
1814/**
1815 * regulator_disable - disable regulator output
1816 * @regulator: regulator source
1817 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001818 * Disable the regulator output voltage or current. Calls to
1819 * regulator_enable() must be balanced with calls to
1820 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001821 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001822 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001823 * devices have it enabled, the regulator device supports disabling and
1824 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001825 */
1826int regulator_disable(struct regulator *regulator)
1827{
David Brownell412aec62008-11-16 11:44:46 -08001828 struct regulator_dev *rdev = regulator->rdev;
1829 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001830
Mark Brown6492bc12012-04-19 13:19:07 +01001831 if (regulator->always_on)
1832 return 0;
1833
David Brownell412aec62008-11-16 11:44:46 -08001834 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001835 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001836 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001837
Mark Brown3801b862011-06-09 16:22:22 +01001838 if (ret == 0 && rdev->supply)
1839 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001840
Liam Girdwood414c70c2008-04-30 15:59:04 +01001841 return ret;
1842}
1843EXPORT_SYMBOL_GPL(regulator_disable);
1844
1845/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001846static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001847{
1848 int ret = 0;
1849
1850 /* force disable */
1851 if (rdev->desc->ops->disable) {
1852 /* ah well, who wants to live forever... */
1853 ret = rdev->desc->ops->disable(rdev);
1854 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001855 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001856 return ret;
1857 }
1858 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001859 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1860 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001861 }
1862
Liam Girdwood414c70c2008-04-30 15:59:04 +01001863 return ret;
1864}
1865
1866/**
1867 * regulator_force_disable - force disable regulator output
1868 * @regulator: regulator source
1869 *
1870 * Forcibly disable the regulator output voltage or current.
1871 * NOTE: this *will* disable the regulator output even if other consumer
1872 * devices have it enabled. This should be used for situations when device
1873 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1874 */
1875int regulator_force_disable(struct regulator *regulator)
1876{
Mark Brown82d15832011-05-09 11:41:02 +02001877 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878 int ret;
1879
Mark Brown82d15832011-05-09 11:41:02 +02001880 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001881 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001882 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001883 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001884
Mark Brown3801b862011-06-09 16:22:22 +01001885 if (rdev->supply)
1886 while (rdev->open_count--)
1887 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001888
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889 return ret;
1890}
1891EXPORT_SYMBOL_GPL(regulator_force_disable);
1892
Mark Brownda07ecd2011-09-11 09:53:50 +01001893static void regulator_disable_work(struct work_struct *work)
1894{
1895 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1896 disable_work.work);
1897 int count, i, ret;
1898
1899 mutex_lock(&rdev->mutex);
1900
1901 BUG_ON(!rdev->deferred_disables);
1902
1903 count = rdev->deferred_disables;
1904 rdev->deferred_disables = 0;
1905
1906 for (i = 0; i < count; i++) {
1907 ret = _regulator_disable(rdev);
1908 if (ret != 0)
1909 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1910 }
1911
1912 mutex_unlock(&rdev->mutex);
1913
1914 if (rdev->supply) {
1915 for (i = 0; i < count; i++) {
1916 ret = regulator_disable(rdev->supply);
1917 if (ret != 0) {
1918 rdev_err(rdev,
1919 "Supply disable failed: %d\n", ret);
1920 }
1921 }
1922 }
1923}
1924
1925/**
1926 * regulator_disable_deferred - disable regulator output with delay
1927 * @regulator: regulator source
1928 * @ms: miliseconds until the regulator is disabled
1929 *
1930 * Execute regulator_disable() on the regulator after a delay. This
1931 * is intended for use with devices that require some time to quiesce.
1932 *
1933 * NOTE: this will only disable the regulator output if no other consumer
1934 * devices have it enabled, the regulator device supports disabling and
1935 * machine constraints permit this operation.
1936 */
1937int regulator_disable_deferred(struct regulator *regulator, int ms)
1938{
1939 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001940 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001941
Mark Brown6492bc12012-04-19 13:19:07 +01001942 if (regulator->always_on)
1943 return 0;
1944
Mark Brown2b5a24a2012-09-07 11:00:53 +08001945 if (!ms)
1946 return regulator_disable(regulator);
1947
Mark Brownda07ecd2011-09-11 09:53:50 +01001948 mutex_lock(&rdev->mutex);
1949 rdev->deferred_disables++;
1950 mutex_unlock(&rdev->mutex);
1951
Mark Brownaa598022011-10-03 22:42:43 +01001952 ret = schedule_delayed_work(&rdev->disable_work,
1953 msecs_to_jiffies(ms));
1954 if (ret < 0)
1955 return ret;
1956 else
1957 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001958}
1959EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1960
Mark Browncd6dffb2012-04-15 12:37:47 +01001961/**
1962 * regulator_is_enabled_regmap - standard is_enabled() for regmap users
1963 *
1964 * @rdev: regulator to operate on
1965 *
1966 * Regulators that use regmap for their register I/O can set the
1967 * enable_reg and enable_mask fields in their descriptor and then use
1968 * this as their is_enabled operation, saving some code.
1969 */
1970int regulator_is_enabled_regmap(struct regulator_dev *rdev)
1971{
1972 unsigned int val;
1973 int ret;
1974
1975 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val);
1976 if (ret != 0)
1977 return ret;
1978
Axel Lin51dcdaf2013-03-05 14:16:00 +08001979 if (rdev->desc->enable_is_inverted)
1980 return (val & rdev->desc->enable_mask) == 0;
1981 else
1982 return (val & rdev->desc->enable_mask) != 0;
Mark Browncd6dffb2012-04-15 12:37:47 +01001983}
1984EXPORT_SYMBOL_GPL(regulator_is_enabled_regmap);
1985
1986/**
1987 * regulator_enable_regmap - standard enable() for regmap users
1988 *
1989 * @rdev: regulator to operate on
1990 *
1991 * Regulators that use regmap for their register I/O can set the
1992 * enable_reg and enable_mask fields in their descriptor and then use
1993 * this as their enable() operation, saving some code.
1994 */
1995int regulator_enable_regmap(struct regulator_dev *rdev)
1996{
Axel Lin51dcdaf2013-03-05 14:16:00 +08001997 unsigned int val;
1998
1999 if (rdev->desc->enable_is_inverted)
2000 val = 0;
2001 else
2002 val = rdev->desc->enable_mask;
2003
Mark Browncd6dffb2012-04-15 12:37:47 +01002004 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Axel Lin51dcdaf2013-03-05 14:16:00 +08002005 rdev->desc->enable_mask, val);
Mark Browncd6dffb2012-04-15 12:37:47 +01002006}
2007EXPORT_SYMBOL_GPL(regulator_enable_regmap);
2008
2009/**
2010 * regulator_disable_regmap - standard disable() for regmap users
2011 *
2012 * @rdev: regulator to operate on
2013 *
2014 * Regulators that use regmap for their register I/O can set the
2015 * enable_reg and enable_mask fields in their descriptor and then use
2016 * this as their disable() operation, saving some code.
2017 */
2018int regulator_disable_regmap(struct regulator_dev *rdev)
2019{
Axel Lin51dcdaf2013-03-05 14:16:00 +08002020 unsigned int val;
2021
2022 if (rdev->desc->enable_is_inverted)
2023 val = rdev->desc->enable_mask;
2024 else
2025 val = 0;
2026
Mark Browncd6dffb2012-04-15 12:37:47 +01002027 return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
Axel Lin51dcdaf2013-03-05 14:16:00 +08002028 rdev->desc->enable_mask, val);
Mark Browncd6dffb2012-04-15 12:37:47 +01002029}
2030EXPORT_SYMBOL_GPL(regulator_disable_regmap);
2031
Liam Girdwood414c70c2008-04-30 15:59:04 +01002032static int _regulator_is_enabled(struct regulator_dev *rdev)
2033{
Mark Brown65f73502012-06-27 14:14:38 +01002034 /* A GPIO control always takes precedence */
Kim, Milo7b74d142013-02-18 06:50:55 +00002035 if (rdev->ena_pin)
Mark Brown65f73502012-06-27 14:14:38 +01002036 return rdev->ena_gpio_state;
2037
Mark Brown9a7f6a42010-02-11 17:22:45 +00002038 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01002039 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00002040 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002041
Mark Brown93325462009-08-03 18:49:56 +01002042 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002043}
2044
2045/**
2046 * regulator_is_enabled - is the regulator output enabled
2047 * @regulator: regulator source
2048 *
David Brownell412aec62008-11-16 11:44:46 -08002049 * Returns positive if the regulator driver backing the source/client
2050 * has requested that the device be enabled, zero if it hasn't, else a
2051 * negative errno code.
2052 *
2053 * Note that the device backing this regulator handle can have multiple
2054 * users, so it might be enabled even if regulator_enable() was never
2055 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002056 */
2057int regulator_is_enabled(struct regulator *regulator)
2058{
Mark Brown93325462009-08-03 18:49:56 +01002059 int ret;
2060
Mark Brown6492bc12012-04-19 13:19:07 +01002061 if (regulator->always_on)
2062 return 1;
2063
Mark Brown93325462009-08-03 18:49:56 +01002064 mutex_lock(&regulator->rdev->mutex);
2065 ret = _regulator_is_enabled(regulator->rdev);
2066 mutex_unlock(&regulator->rdev->mutex);
2067
2068 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002069}
2070EXPORT_SYMBOL_GPL(regulator_is_enabled);
2071
2072/**
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002073 * regulator_can_change_voltage - check if regulator can change voltage
2074 * @regulator: regulator source
2075 *
2076 * Returns positive if the regulator driver backing the source/client
2077 * can change its voltage, false otherwise. Usefull for detecting fixed
2078 * or dummy regulators and disabling voltage change logic in the client
2079 * driver.
2080 */
2081int regulator_can_change_voltage(struct regulator *regulator)
2082{
2083 struct regulator_dev *rdev = regulator->rdev;
2084
2085 if (rdev->constraints &&
Axel Lin19280e42012-12-12 09:22:46 +08002086 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2087 if (rdev->desc->n_voltages - rdev->desc->linear_min_sel > 1)
2088 return 1;
2089
2090 if (rdev->desc->continuous_voltage_range &&
2091 rdev->constraints->min_uV && rdev->constraints->max_uV &&
2092 rdev->constraints->min_uV != rdev->constraints->max_uV)
2093 return 1;
2094 }
Marek Szyprowskid1e7de32012-12-04 15:01:01 +01002095
2096 return 0;
2097}
2098EXPORT_SYMBOL_GPL(regulator_can_change_voltage);
2099
2100/**
David Brownell4367cfd2009-02-26 11:48:36 -08002101 * regulator_count_voltages - count regulator_list_voltage() selectors
2102 * @regulator: regulator source
2103 *
2104 * Returns number of selectors, or negative errno. Selectors are
2105 * numbered starting at zero, and typically correspond to bitfields
2106 * in hardware registers.
2107 */
2108int regulator_count_voltages(struct regulator *regulator)
2109{
2110 struct regulator_dev *rdev = regulator->rdev;
2111
2112 return rdev->desc->n_voltages ? : -EINVAL;
2113}
2114EXPORT_SYMBOL_GPL(regulator_count_voltages);
2115
2116/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01002117 * regulator_list_voltage_linear - List voltages with simple calculation
2118 *
2119 * @rdev: Regulator device
2120 * @selector: Selector to convert into a voltage
2121 *
2122 * Regulators with a simple linear mapping between voltages and
2123 * selectors can set min_uV and uV_step in the regulator descriptor
2124 * and then use this function as their list_voltage() operation,
2125 */
2126int regulator_list_voltage_linear(struct regulator_dev *rdev,
2127 unsigned int selector)
2128{
2129 if (selector >= rdev->desc->n_voltages)
2130 return -EINVAL;
Axel Lin33234e72012-11-27 10:24:33 +08002131 if (selector < rdev->desc->linear_min_sel)
2132 return 0;
2133
2134 selector -= rdev->desc->linear_min_sel;
Mark Brownbca7bbf2012-05-09 21:38:33 +01002135
2136 return rdev->desc->min_uV + (rdev->desc->uV_step * selector);
2137}
2138EXPORT_SYMBOL_GPL(regulator_list_voltage_linear);
2139
2140/**
Axel Lincffc9592012-05-20 10:30:21 +08002141 * regulator_list_voltage_table - List voltages with table based mapping
2142 *
2143 * @rdev: Regulator device
2144 * @selector: Selector to convert into a voltage
2145 *
2146 * Regulators with table based mapping between voltages and
2147 * selectors can set volt_table in the regulator descriptor
2148 * and then use this function as their list_voltage() operation.
2149 */
2150int regulator_list_voltage_table(struct regulator_dev *rdev,
2151 unsigned int selector)
2152{
2153 if (!rdev->desc->volt_table) {
2154 BUG_ON(!rdev->desc->volt_table);
2155 return -EINVAL;
2156 }
2157
2158 if (selector >= rdev->desc->n_voltages)
2159 return -EINVAL;
2160
2161 return rdev->desc->volt_table[selector];
2162}
2163EXPORT_SYMBOL_GPL(regulator_list_voltage_table);
2164
2165/**
David Brownell4367cfd2009-02-26 11:48:36 -08002166 * regulator_list_voltage - enumerate supported voltages
2167 * @regulator: regulator source
2168 * @selector: identify voltage to list
2169 * Context: can sleep
2170 *
2171 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01002172 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08002173 * negative errno.
2174 */
2175int regulator_list_voltage(struct regulator *regulator, unsigned selector)
2176{
2177 struct regulator_dev *rdev = regulator->rdev;
2178 struct regulator_ops *ops = rdev->desc->ops;
2179 int ret;
2180
2181 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
2182 return -EINVAL;
2183
2184 mutex_lock(&rdev->mutex);
2185 ret = ops->list_voltage(rdev, selector);
2186 mutex_unlock(&rdev->mutex);
2187
2188 if (ret > 0) {
2189 if (ret < rdev->constraints->min_uV)
2190 ret = 0;
2191 else if (ret > rdev->constraints->max_uV)
2192 ret = 0;
2193 }
2194
2195 return ret;
2196}
2197EXPORT_SYMBOL_GPL(regulator_list_voltage);
2198
2199/**
Paul Walmsley2a668a82013-06-07 08:06:56 +00002200 * regulator_get_linear_step - return the voltage step size between VSEL values
2201 * @regulator: regulator source
2202 *
2203 * Returns the voltage step size between VSEL values for linear
2204 * regulators, or return 0 if the regulator isn't a linear regulator.
2205 */
2206unsigned int regulator_get_linear_step(struct regulator *regulator)
2207{
2208 struct regulator_dev *rdev = regulator->rdev;
2209
2210 return rdev->desc->uV_step;
2211}
2212EXPORT_SYMBOL_GPL(regulator_get_linear_step);
2213
2214/**
Mark Browna7a1ad92009-07-21 16:00:24 +01002215 * regulator_is_supported_voltage - check if a voltage range can be supported
2216 *
2217 * @regulator: Regulator to check.
2218 * @min_uV: Minimum required voltage in uV.
2219 * @max_uV: Maximum required voltage in uV.
2220 *
2221 * Returns a boolean or a negative error code.
2222 */
2223int regulator_is_supported_voltage(struct regulator *regulator,
2224 int min_uV, int max_uV)
2225{
Mark Brownc5f39392012-07-02 15:00:18 +01002226 struct regulator_dev *rdev = regulator->rdev;
Mark Browna7a1ad92009-07-21 16:00:24 +01002227 int i, voltages, ret;
2228
Mark Brownc5f39392012-07-02 15:00:18 +01002229 /* If we can't change voltage check the current voltage */
2230 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
2231 ret = regulator_get_voltage(regulator);
2232 if (ret >= 0)
Marek Szyprowskif0f98b12012-11-13 09:48:51 +01002233 return (min_uV <= ret && ret <= max_uV);
Mark Brownc5f39392012-07-02 15:00:18 +01002234 else
2235 return ret;
2236 }
2237
Pawel Mollbd7a2b62012-09-24 18:56:53 +01002238 /* Any voltage within constrains range is fine? */
2239 if (rdev->desc->continuous_voltage_range)
2240 return min_uV >= rdev->constraints->min_uV &&
2241 max_uV <= rdev->constraints->max_uV;
2242
Mark Browna7a1ad92009-07-21 16:00:24 +01002243 ret = regulator_count_voltages(regulator);
2244 if (ret < 0)
2245 return ret;
2246 voltages = ret;
2247
2248 for (i = 0; i < voltages; i++) {
2249 ret = regulator_list_voltage(regulator, i);
2250
2251 if (ret >= min_uV && ret <= max_uV)
2252 return 1;
2253 }
2254
2255 return 0;
2256}
Mark Browna398eaa2011-12-28 12:48:45 +00002257EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01002258
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002259/**
2260 * regulator_get_voltage_sel_regmap - standard get_voltage_sel for regmap users
2261 *
2262 * @rdev: regulator to operate on
2263 *
2264 * Regulators that use regmap for their register I/O can set the
2265 * vsel_reg and vsel_mask fields in their descriptor and then use this
2266 * as their get_voltage_vsel operation, saving some code.
2267 */
2268int regulator_get_voltage_sel_regmap(struct regulator_dev *rdev)
2269{
2270 unsigned int val;
2271 int ret;
2272
2273 ret = regmap_read(rdev->regmap, rdev->desc->vsel_reg, &val);
2274 if (ret != 0)
2275 return ret;
2276
2277 val &= rdev->desc->vsel_mask;
2278 val >>= ffs(rdev->desc->vsel_mask) - 1;
2279
2280 return val;
2281}
2282EXPORT_SYMBOL_GPL(regulator_get_voltage_sel_regmap);
2283
2284/**
2285 * regulator_set_voltage_sel_regmap - standard set_voltage_sel for regmap users
2286 *
2287 * @rdev: regulator to operate on
2288 * @sel: Selector to set
2289 *
2290 * Regulators that use regmap for their register I/O can set the
2291 * vsel_reg and vsel_mask fields in their descriptor and then use this
2292 * as their set_voltage_vsel operation, saving some code.
2293 */
2294int regulator_set_voltage_sel_regmap(struct regulator_dev *rdev, unsigned sel)
2295{
Axel Linc8520b42012-12-18 09:30:10 +08002296 int ret;
2297
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002298 sel <<= ffs(rdev->desc->vsel_mask) - 1;
2299
Axel Linc8520b42012-12-18 09:30:10 +08002300 ret = regmap_update_bits(rdev->regmap, rdev->desc->vsel_reg,
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002301 rdev->desc->vsel_mask, sel);
Axel Linc8520b42012-12-18 09:30:10 +08002302 if (ret)
2303 return ret;
2304
2305 if (rdev->desc->apply_bit)
2306 ret = regmap_update_bits(rdev->regmap, rdev->desc->apply_reg,
2307 rdev->desc->apply_bit,
2308 rdev->desc->apply_bit);
2309 return ret;
Mark Brown4ab5b3d2012-04-15 11:23:30 +01002310}
2311EXPORT_SYMBOL_GPL(regulator_set_voltage_sel_regmap);
2312
Mark Browne843fc42012-05-09 21:16:06 +01002313/**
2314 * regulator_map_voltage_iterate - map_voltage() based on list_voltage()
2315 *
2316 * @rdev: Regulator to operate on
2317 * @min_uV: Lower bound for voltage
2318 * @max_uV: Upper bound for voltage
2319 *
2320 * Drivers implementing set_voltage_sel() and list_voltage() can use
2321 * this as their map_voltage() operation. It will find a suitable
2322 * voltage by calling list_voltage() until it gets something in bounds
2323 * for the requested voltages.
2324 */
2325int regulator_map_voltage_iterate(struct regulator_dev *rdev,
2326 int min_uV, int max_uV)
2327{
2328 int best_val = INT_MAX;
2329 int selector = 0;
2330 int i, ret;
2331
2332 /* Find the smallest voltage that falls within the specified
2333 * range.
2334 */
2335 for (i = 0; i < rdev->desc->n_voltages; i++) {
2336 ret = rdev->desc->ops->list_voltage(rdev, i);
2337 if (ret < 0)
2338 continue;
2339
2340 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
2341 best_val = ret;
2342 selector = i;
2343 }
2344 }
2345
2346 if (best_val != INT_MAX)
2347 return selector;
2348 else
2349 return -EINVAL;
2350}
2351EXPORT_SYMBOL_GPL(regulator_map_voltage_iterate);
2352
Mark Brownbca7bbf2012-05-09 21:38:33 +01002353/**
Axel Linfcf371e2013-04-18 10:34:49 +08002354 * regulator_map_voltage_ascend - map_voltage() for ascendant voltage list
2355 *
2356 * @rdev: Regulator to operate on
2357 * @min_uV: Lower bound for voltage
2358 * @max_uV: Upper bound for voltage
2359 *
2360 * Drivers that have ascendant voltage list can use this as their
2361 * map_voltage() operation.
2362 */
2363int regulator_map_voltage_ascend(struct regulator_dev *rdev,
2364 int min_uV, int max_uV)
2365{
2366 int i, ret;
2367
2368 for (i = 0; i < rdev->desc->n_voltages; i++) {
2369 ret = rdev->desc->ops->list_voltage(rdev, i);
2370 if (ret < 0)
2371 continue;
2372
2373 if (ret > max_uV)
2374 break;
2375
2376 if (ret >= min_uV && ret <= max_uV)
2377 return i;
2378 }
2379
2380 return -EINVAL;
2381}
2382EXPORT_SYMBOL_GPL(regulator_map_voltage_ascend);
2383
2384/**
Mark Brownbca7bbf2012-05-09 21:38:33 +01002385 * regulator_map_voltage_linear - map_voltage() for simple linear mappings
2386 *
2387 * @rdev: Regulator to operate on
2388 * @min_uV: Lower bound for voltage
2389 * @max_uV: Upper bound for voltage
2390 *
2391 * Drivers providing min_uV and uV_step in their regulator_desc can
2392 * use this as their map_voltage() operation.
2393 */
2394int regulator_map_voltage_linear(struct regulator_dev *rdev,
2395 int min_uV, int max_uV)
2396{
2397 int ret, voltage;
2398
Axel Lin5a6881e2012-06-07 10:05:14 +08002399 /* Allow uV_step to be 0 for fixed voltage */
2400 if (rdev->desc->n_voltages == 1 && rdev->desc->uV_step == 0) {
2401 if (min_uV <= rdev->desc->min_uV && rdev->desc->min_uV <= max_uV)
2402 return 0;
2403 else
2404 return -EINVAL;
2405 }
2406
Mark Brownbca7bbf2012-05-09 21:38:33 +01002407 if (!rdev->desc->uV_step) {
2408 BUG_ON(!rdev->desc->uV_step);
2409 return -EINVAL;
2410 }
2411
Axel Lin0bdc81e2012-06-07 09:52:12 +08002412 if (min_uV < rdev->desc->min_uV)
2413 min_uV = rdev->desc->min_uV;
2414
Axel Linccfcb1c2012-05-14 10:33:37 +08002415 ret = DIV_ROUND_UP(min_uV - rdev->desc->min_uV, rdev->desc->uV_step);
Mark Brownbca7bbf2012-05-09 21:38:33 +01002416 if (ret < 0)
2417 return ret;
2418
Axel Lin33234e72012-11-27 10:24:33 +08002419 ret += rdev->desc->linear_min_sel;
2420
Mark Brownbca7bbf2012-05-09 21:38:33 +01002421 /* Map back into a voltage to verify we're still in bounds */
2422 voltage = rdev->desc->ops->list_voltage(rdev, ret);
2423 if (voltage < min_uV || voltage > max_uV)
2424 return -EINVAL;
2425
2426 return ret;
2427}
2428EXPORT_SYMBOL_GPL(regulator_map_voltage_linear);
2429
Mark Brown75790252010-12-12 14:25:50 +00002430static int _regulator_do_set_voltage(struct regulator_dev *rdev,
2431 int min_uV, int max_uV)
2432{
2433 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01002434 int delay = 0;
Mark Browne113d792012-06-26 10:57:51 +01002435 int best_val = 0;
Mark Brown75790252010-12-12 14:25:50 +00002436 unsigned int selector;
Axel Lineba41a52012-04-04 10:32:10 +08002437 int old_selector = -1;
Mark Brown75790252010-12-12 14:25:50 +00002438
2439 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
2440
Mark Brownbf5892a2011-05-08 22:13:37 +01002441 min_uV += rdev->constraints->uV_offset;
2442 max_uV += rdev->constraints->uV_offset;
2443
Axel Lineba41a52012-04-04 10:32:10 +08002444 /*
2445 * If we can't obtain the old selector there is not enough
2446 * info to call set_voltage_time_sel().
2447 */
Axel Lin8b7485e2012-05-21 09:37:52 +08002448 if (_regulator_is_enabled(rdev) &&
2449 rdev->desc->ops->set_voltage_time_sel &&
Axel Lineba41a52012-04-04 10:32:10 +08002450 rdev->desc->ops->get_voltage_sel) {
2451 old_selector = rdev->desc->ops->get_voltage_sel(rdev);
2452 if (old_selector < 0)
2453 return old_selector;
2454 }
2455
Mark Brown75790252010-12-12 14:25:50 +00002456 if (rdev->desc->ops->set_voltage) {
2457 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
2458 &selector);
Mark Browne113d792012-06-26 10:57:51 +01002459
2460 if (ret >= 0) {
2461 if (rdev->desc->ops->list_voltage)
2462 best_val = rdev->desc->ops->list_voltage(rdev,
2463 selector);
2464 else
2465 best_val = _regulator_get_voltage(rdev);
2466 }
2467
Mark Browne8eef822010-12-12 14:36:17 +00002468 } else if (rdev->desc->ops->set_voltage_sel) {
Axel Lin9152c362012-06-04 09:41:38 +08002469 if (rdev->desc->ops->map_voltage) {
Mark Browne843fc42012-05-09 21:16:06 +01002470 ret = rdev->desc->ops->map_voltage(rdev, min_uV,
2471 max_uV);
Axel Lin9152c362012-06-04 09:41:38 +08002472 } else {
2473 if (rdev->desc->ops->list_voltage ==
2474 regulator_list_voltage_linear)
2475 ret = regulator_map_voltage_linear(rdev,
2476 min_uV, max_uV);
2477 else
2478 ret = regulator_map_voltage_iterate(rdev,
2479 min_uV, max_uV);
2480 }
Mark Browne8eef822010-12-12 14:36:17 +00002481
Mark Browne843fc42012-05-09 21:16:06 +01002482 if (ret >= 0) {
Mark Browne113d792012-06-26 10:57:51 +01002483 best_val = rdev->desc->ops->list_voltage(rdev, ret);
2484 if (min_uV <= best_val && max_uV >= best_val) {
2485 selector = ret;
Axel Linc66a5662013-02-06 11:09:48 +08002486 if (old_selector == selector)
2487 ret = 0;
2488 else
2489 ret = rdev->desc->ops->set_voltage_sel(
2490 rdev, ret);
Mark Browne113d792012-06-26 10:57:51 +01002491 } else {
2492 ret = -EINVAL;
2493 }
Mark Browne8eef822010-12-12 14:36:17 +00002494 }
Mark Brown75790252010-12-12 14:25:50 +00002495 } else {
2496 ret = -EINVAL;
2497 }
2498
Axel Lineba41a52012-04-04 10:32:10 +08002499 /* Call set_voltage_time_sel if successfully obtained old_selector */
Mark Brown5aff3a82012-06-26 11:16:58 +01002500 if (ret == 0 && _regulator_is_enabled(rdev) && old_selector >= 0 &&
Axel Linc66a5662013-02-06 11:09:48 +08002501 old_selector != selector && rdev->desc->ops->set_voltage_time_sel) {
Axel Lineba41a52012-04-04 10:32:10 +08002502
2503 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
2504 old_selector, selector);
2505 if (delay < 0) {
2506 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n",
2507 delay);
2508 delay = 0;
2509 }
Axel Lineba41a52012-04-04 10:32:10 +08002510
Philip Rakity8b96de32012-06-14 15:07:56 -07002511 /* Insert any necessary delays */
2512 if (delay >= 1000) {
2513 mdelay(delay / 1000);
2514 udelay(delay % 1000);
2515 } else if (delay) {
2516 udelay(delay);
2517 }
Linus Walleij77af1b2642011-03-17 13:24:36 +01002518 }
2519
Axel Lin2f6c7972012-08-06 23:44:19 +08002520 if (ret == 0 && best_val >= 0) {
2521 unsigned long data = best_val;
2522
Mark Brownded06a52010-12-16 13:59:10 +00002523 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
Axel Lin2f6c7972012-08-06 23:44:19 +08002524 (void *)data);
2525 }
Mark Brownded06a52010-12-16 13:59:10 +00002526
Axel Lineba41a52012-04-04 10:32:10 +08002527 trace_regulator_set_voltage_complete(rdev_get_name(rdev), best_val);
Mark Brown75790252010-12-12 14:25:50 +00002528
2529 return ret;
2530}
2531
Mark Browna7a1ad92009-07-21 16:00:24 +01002532/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002533 * regulator_set_voltage - set regulator output voltage
2534 * @regulator: regulator source
2535 * @min_uV: Minimum required voltage in uV
2536 * @max_uV: Maximum acceptable voltage in uV
2537 *
2538 * Sets a voltage regulator to the desired output voltage. This can be set
2539 * during any regulator state. IOW, regulator can be disabled or enabled.
2540 *
2541 * If the regulator is enabled then the voltage will change to the new value
2542 * immediately otherwise if the regulator is disabled the regulator will
2543 * output at the new voltage when enabled.
2544 *
2545 * NOTE: If the regulator is shared between several devices then the lowest
2546 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00002547 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01002548 * calling this function otherwise this call will fail.
2549 */
2550int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
2551{
2552 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00002553 int ret = 0;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002554 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002555
2556 mutex_lock(&rdev->mutex);
2557
Mark Brown95a3c232010-12-16 15:49:37 +00002558 /* If we're setting the same range as last time the change
2559 * should be a noop (some cpufreq implementations use the same
2560 * voltage for multiple frequencies, for example).
2561 */
2562 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2563 goto out;
2564
Liam Girdwood414c70c2008-04-30 15:59:04 +01002565 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002566 if (!rdev->desc->ops->set_voltage &&
2567 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002568 ret = -EINVAL;
2569 goto out;
2570 }
2571
2572 /* constraints check */
2573 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2574 if (ret < 0)
2575 goto out;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002576
2577 /* restore original values in case of error */
2578 old_min_uV = regulator->min_uV;
2579 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 regulator->min_uV = min_uV;
2581 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002582
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002583 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2584 if (ret < 0)
Paolo Pisati92d7a552012-12-13 10:13:00 +01002585 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002586
Mark Brown75790252010-12-12 14:25:50 +00002587 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati92d7a552012-12-13 10:13:00 +01002588 if (ret < 0)
2589 goto out2;
2590
Liam Girdwood414c70c2008-04-30 15:59:04 +01002591out:
2592 mutex_unlock(&rdev->mutex);
2593 return ret;
Paolo Pisati92d7a552012-12-13 10:13:00 +01002594out2:
2595 regulator->min_uV = old_min_uV;
2596 regulator->max_uV = old_max_uV;
2597 mutex_unlock(&rdev->mutex);
2598 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002599}
2600EXPORT_SYMBOL_GPL(regulator_set_voltage);
2601
Mark Brown606a2562010-12-16 15:49:36 +00002602/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01002603 * regulator_set_voltage_time - get raise/fall time
2604 * @regulator: regulator source
2605 * @old_uV: starting voltage in microvolts
2606 * @new_uV: target voltage in microvolts
2607 *
2608 * Provided with the starting and ending voltage, this function attempts to
2609 * calculate the time in microseconds required to rise or fall to this new
2610 * voltage.
2611 */
2612int regulator_set_voltage_time(struct regulator *regulator,
2613 int old_uV, int new_uV)
2614{
2615 struct regulator_dev *rdev = regulator->rdev;
2616 struct regulator_ops *ops = rdev->desc->ops;
2617 int old_sel = -1;
2618 int new_sel = -1;
2619 int voltage;
2620 int i;
2621
2622 /* Currently requires operations to do this */
2623 if (!ops->list_voltage || !ops->set_voltage_time_sel
2624 || !rdev->desc->n_voltages)
2625 return -EINVAL;
2626
2627 for (i = 0; i < rdev->desc->n_voltages; i++) {
2628 /* We only look for exact voltage matches here */
2629 voltage = regulator_list_voltage(regulator, i);
2630 if (voltage < 0)
2631 return -EINVAL;
2632 if (voltage == 0)
2633 continue;
2634 if (voltage == old_uV)
2635 old_sel = i;
2636 if (voltage == new_uV)
2637 new_sel = i;
2638 }
2639
2640 if (old_sel < 0 || new_sel < 0)
2641 return -EINVAL;
2642
2643 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2644}
2645EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2646
2647/**
Randy Dunlap296c6562012-08-18 17:44:14 -07002648 * regulator_set_voltage_time_sel - get raise/fall time
2649 * @rdev: regulator source device
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302650 * @old_selector: selector for starting voltage
2651 * @new_selector: selector for target voltage
2652 *
2653 * Provided with the starting and target voltage selectors, this function
2654 * returns time in microseconds required to rise or fall to this new voltage
2655 *
Axel Linf11d08c2012-06-19 09:38:45 +08002656 * Drivers providing ramp_delay in regulation_constraints can use this as their
Axel Lin398715a2012-06-18 10:11:28 +08002657 * set_voltage_time_sel() operation.
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302658 */
2659int regulator_set_voltage_time_sel(struct regulator_dev *rdev,
2660 unsigned int old_selector,
2661 unsigned int new_selector)
2662{
Axel Lin398715a2012-06-18 10:11:28 +08002663 unsigned int ramp_delay = 0;
Axel Linf11d08c2012-06-19 09:38:45 +08002664 int old_volt, new_volt;
Axel Lin398715a2012-06-18 10:11:28 +08002665
2666 if (rdev->constraints->ramp_delay)
2667 ramp_delay = rdev->constraints->ramp_delay;
2668 else if (rdev->desc->ramp_delay)
2669 ramp_delay = rdev->desc->ramp_delay;
2670
2671 if (ramp_delay == 0) {
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302672 rdev_warn(rdev, "ramp_delay not set\n");
Axel Lin398715a2012-06-18 10:11:28 +08002673 return 0;
Yadwinder Singh Brar6f0b2c62012-06-11 17:41:08 +05302674 }
Axel Lin398715a2012-06-18 10:11:28 +08002675
Axel Linf11d08c2012-06-19 09:38:45 +08002676 /* sanity check */
2677 if (!rdev->desc->ops->list_voltage)
2678 return -EINVAL;
Axel Lin398715a2012-06-18 10:11:28 +08002679
Axel Linf11d08c2012-06-19 09:38:45 +08002680 old_volt = rdev->desc->ops->list_voltage(rdev, old_selector);
2681 new_volt = rdev->desc->ops->list_voltage(rdev, new_selector);
2682
2683 return DIV_ROUND_UP(abs(new_volt - old_volt), ramp_delay);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302684}
Mark Brownb19dbf72012-06-23 11:34:20 +01002685EXPORT_SYMBOL_GPL(regulator_set_voltage_time_sel);
Yadwinder Singh Brar98a175b2012-06-09 16:40:38 +05302686
2687/**
Mark Brown606a2562010-12-16 15:49:36 +00002688 * regulator_sync_voltage - re-apply last regulator output voltage
2689 * @regulator: regulator source
2690 *
2691 * Re-apply the last configured voltage. This is intended to be used
2692 * where some external control source the consumer is cooperating with
2693 * has caused the configured voltage to change.
2694 */
2695int regulator_sync_voltage(struct regulator *regulator)
2696{
2697 struct regulator_dev *rdev = regulator->rdev;
2698 int ret, min_uV, max_uV;
2699
2700 mutex_lock(&rdev->mutex);
2701
2702 if (!rdev->desc->ops->set_voltage &&
2703 !rdev->desc->ops->set_voltage_sel) {
2704 ret = -EINVAL;
2705 goto out;
2706 }
2707
2708 /* This is only going to work if we've had a voltage configured. */
2709 if (!regulator->min_uV && !regulator->max_uV) {
2710 ret = -EINVAL;
2711 goto out;
2712 }
2713
2714 min_uV = regulator->min_uV;
2715 max_uV = regulator->max_uV;
2716
2717 /* This should be a paranoia check... */
2718 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2719 if (ret < 0)
2720 goto out;
2721
2722 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2723 if (ret < 0)
2724 goto out;
2725
2726 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2727
2728out:
2729 mutex_unlock(&rdev->mutex);
2730 return ret;
2731}
2732EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2733
Liam Girdwood414c70c2008-04-30 15:59:04 +01002734static int _regulator_get_voltage(struct regulator_dev *rdev)
2735{
Mark Brownbf5892a2011-05-08 22:13:37 +01002736 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002737
2738 if (rdev->desc->ops->get_voltage_sel) {
2739 sel = rdev->desc->ops->get_voltage_sel(rdev);
2740 if (sel < 0)
2741 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002742 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002743 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002744 ret = rdev->desc->ops->get_voltage(rdev);
Mark Brownf7df20e2012-08-09 16:42:19 +01002745 } else if (rdev->desc->ops->list_voltage) {
2746 ret = rdev->desc->ops->list_voltage(rdev, 0);
Axel Lincb220d12011-05-23 20:08:10 +08002747 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002748 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002749 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002750
Axel Lincb220d12011-05-23 20:08:10 +08002751 if (ret < 0)
2752 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002753 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002754}
2755
2756/**
2757 * regulator_get_voltage - get regulator output voltage
2758 * @regulator: regulator source
2759 *
2760 * This returns the current regulator voltage in uV.
2761 *
2762 * NOTE: If the regulator is disabled it will return the voltage value. This
2763 * function should not be used to determine regulator state.
2764 */
2765int regulator_get_voltage(struct regulator *regulator)
2766{
2767 int ret;
2768
2769 mutex_lock(&regulator->rdev->mutex);
2770
2771 ret = _regulator_get_voltage(regulator->rdev);
2772
2773 mutex_unlock(&regulator->rdev->mutex);
2774
2775 return ret;
2776}
2777EXPORT_SYMBOL_GPL(regulator_get_voltage);
2778
2779/**
2780 * regulator_set_current_limit - set regulator output current limit
2781 * @regulator: regulator source
Charles Keepaxce0d10f2013-05-21 15:04:07 +01002782 * @min_uA: Minimum supported current in uA
Liam Girdwood414c70c2008-04-30 15:59:04 +01002783 * @max_uA: Maximum supported current in uA
2784 *
2785 * Sets current sink to the desired output current. This can be set during
2786 * any regulator state. IOW, regulator can be disabled or enabled.
2787 *
2788 * If the regulator is enabled then the current will change to the new value
2789 * immediately otherwise if the regulator is disabled the regulator will
2790 * output at the new current when enabled.
2791 *
2792 * NOTE: Regulator system constraints must be set for this regulator before
2793 * calling this function otherwise this call will fail.
2794 */
2795int regulator_set_current_limit(struct regulator *regulator,
2796 int min_uA, int max_uA)
2797{
2798 struct regulator_dev *rdev = regulator->rdev;
2799 int ret;
2800
2801 mutex_lock(&rdev->mutex);
2802
2803 /* sanity check */
2804 if (!rdev->desc->ops->set_current_limit) {
2805 ret = -EINVAL;
2806 goto out;
2807 }
2808
2809 /* constraints check */
2810 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2811 if (ret < 0)
2812 goto out;
2813
2814 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2815out:
2816 mutex_unlock(&rdev->mutex);
2817 return ret;
2818}
2819EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2820
2821static int _regulator_get_current_limit(struct regulator_dev *rdev)
2822{
2823 int ret;
2824
2825 mutex_lock(&rdev->mutex);
2826
2827 /* sanity check */
2828 if (!rdev->desc->ops->get_current_limit) {
2829 ret = -EINVAL;
2830 goto out;
2831 }
2832
2833 ret = rdev->desc->ops->get_current_limit(rdev);
2834out:
2835 mutex_unlock(&rdev->mutex);
2836 return ret;
2837}
2838
2839/**
2840 * regulator_get_current_limit - get regulator output current
2841 * @regulator: regulator source
2842 *
2843 * This returns the current supplied by the specified current sink in uA.
2844 *
2845 * NOTE: If the regulator is disabled it will return the current value. This
2846 * function should not be used to determine regulator state.
2847 */
2848int regulator_get_current_limit(struct regulator *regulator)
2849{
2850 return _regulator_get_current_limit(regulator->rdev);
2851}
2852EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2853
2854/**
2855 * regulator_set_mode - set regulator operating mode
2856 * @regulator: regulator source
2857 * @mode: operating mode - one of the REGULATOR_MODE constants
2858 *
2859 * Set regulator operating mode to increase regulator efficiency or improve
2860 * regulation performance.
2861 *
2862 * NOTE: Regulator system constraints must be set for this regulator before
2863 * calling this function otherwise this call will fail.
2864 */
2865int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2866{
2867 struct regulator_dev *rdev = regulator->rdev;
2868 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302869 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002870
2871 mutex_lock(&rdev->mutex);
2872
2873 /* sanity check */
2874 if (!rdev->desc->ops->set_mode) {
2875 ret = -EINVAL;
2876 goto out;
2877 }
2878
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302879 /* return if the same mode is requested */
2880 if (rdev->desc->ops->get_mode) {
2881 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2882 if (regulator_curr_mode == mode) {
2883 ret = 0;
2884 goto out;
2885 }
2886 }
2887
Liam Girdwood414c70c2008-04-30 15:59:04 +01002888 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002889 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002890 if (ret < 0)
2891 goto out;
2892
2893 ret = rdev->desc->ops->set_mode(rdev, mode);
2894out:
2895 mutex_unlock(&rdev->mutex);
2896 return ret;
2897}
2898EXPORT_SYMBOL_GPL(regulator_set_mode);
2899
2900static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2901{
2902 int ret;
2903
2904 mutex_lock(&rdev->mutex);
2905
2906 /* sanity check */
2907 if (!rdev->desc->ops->get_mode) {
2908 ret = -EINVAL;
2909 goto out;
2910 }
2911
2912 ret = rdev->desc->ops->get_mode(rdev);
2913out:
2914 mutex_unlock(&rdev->mutex);
2915 return ret;
2916}
2917
2918/**
2919 * regulator_get_mode - get regulator operating mode
2920 * @regulator: regulator source
2921 *
2922 * Get the current regulator operating mode.
2923 */
2924unsigned int regulator_get_mode(struct regulator *regulator)
2925{
2926 return _regulator_get_mode(regulator->rdev);
2927}
2928EXPORT_SYMBOL_GPL(regulator_get_mode);
2929
2930/**
2931 * regulator_set_optimum_mode - set regulator optimum operating mode
2932 * @regulator: regulator source
2933 * @uA_load: load current
2934 *
2935 * Notifies the regulator core of a new device load. This is then used by
2936 * DRMS (if enabled by constraints) to set the most efficient regulator
2937 * operating mode for the new regulator loading.
2938 *
2939 * Consumer devices notify their supply regulator of the maximum power
2940 * they will require (can be taken from device datasheet in the power
2941 * consumption tables) when they change operational status and hence power
2942 * state. Examples of operational state changes that can affect power
2943 * consumption are :-
2944 *
2945 * o Device is opened / closed.
2946 * o Device I/O is about to begin or has just finished.
2947 * o Device is idling in between work.
2948 *
2949 * This information is also exported via sysfs to userspace.
2950 *
2951 * DRMS will sum the total requested load on the regulator and change
2952 * to the most efficient operating mode if platform constraints allow.
2953 *
2954 * Returns the new regulator mode or error.
2955 */
2956int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2957{
2958 struct regulator_dev *rdev = regulator->rdev;
2959 struct regulator *consumer;
Stephen Boydd92d95b62012-07-02 19:21:06 -07002960 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002961 unsigned int mode;
2962
Stephen Boydd92d95b62012-07-02 19:21:06 -07002963 if (rdev->supply)
2964 input_uV = regulator_get_voltage(rdev->supply);
2965
Liam Girdwood414c70c2008-04-30 15:59:04 +01002966 mutex_lock(&rdev->mutex);
2967
Mark Browna4b41482011-05-14 11:19:45 -07002968 /*
2969 * first check to see if we can set modes at all, otherwise just
2970 * tell the consumer everything is OK.
2971 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002972 regulator->uA_load = uA_load;
2973 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002974 if (ret < 0) {
2975 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002976 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002977 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002978
Liam Girdwood414c70c2008-04-30 15:59:04 +01002979 if (!rdev->desc->ops->get_optimum_mode)
2980 goto out;
2981
Mark Browna4b41482011-05-14 11:19:45 -07002982 /*
2983 * we can actually do this so any errors are indicators of
2984 * potential real failure.
2985 */
2986 ret = -EINVAL;
2987
Axel Lin854ccba2012-04-16 18:44:23 +08002988 if (!rdev->desc->ops->set_mode)
2989 goto out;
2990
Liam Girdwood414c70c2008-04-30 15:59:04 +01002991 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002992 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002993 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002994 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002995 goto out;
2996 }
2997
Stephen Boydd92d95b62012-07-02 19:21:06 -07002998 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002999 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003000 input_uV = rdev->constraints->input_uV;
3001 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003002 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003003 goto out;
3004 }
3005
3006 /* calc total requested load for this regulator */
3007 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01003008 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003009
3010 mode = rdev->desc->ops->get_optimum_mode(rdev,
3011 input_uV, output_uV,
3012 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09003013 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08003014 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003015 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
3016 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003017 goto out;
3018 }
3019
3020 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08003021 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003022 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003023 goto out;
3024 }
3025 ret = mode;
3026out:
3027 mutex_unlock(&rdev->mutex);
3028 return ret;
3029}
3030EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
3031
3032/**
Mark Browndf367932012-08-27 16:04:23 -07003033 * regulator_set_bypass_regmap - Default set_bypass() using regmap
3034 *
3035 * @rdev: device to operate on.
3036 * @enable: state to set.
3037 */
3038int regulator_set_bypass_regmap(struct regulator_dev *rdev, bool enable)
3039{
3040 unsigned int val;
3041
3042 if (enable)
3043 val = rdev->desc->bypass_mask;
3044 else
3045 val = 0;
3046
3047 return regmap_update_bits(rdev->regmap, rdev->desc->bypass_reg,
3048 rdev->desc->bypass_mask, val);
3049}
3050EXPORT_SYMBOL_GPL(regulator_set_bypass_regmap);
3051
3052/**
3053 * regulator_get_bypass_regmap - Default get_bypass() using regmap
3054 *
3055 * @rdev: device to operate on.
3056 * @enable: current state.
3057 */
3058int regulator_get_bypass_regmap(struct regulator_dev *rdev, bool *enable)
3059{
3060 unsigned int val;
3061 int ret;
3062
3063 ret = regmap_read(rdev->regmap, rdev->desc->bypass_reg, &val);
3064 if (ret != 0)
3065 return ret;
3066
3067 *enable = val & rdev->desc->bypass_mask;
3068
3069 return 0;
3070}
3071EXPORT_SYMBOL_GPL(regulator_get_bypass_regmap);
3072
3073/**
Mark Brownf59c8f92012-08-31 10:36:37 -07003074 * regulator_allow_bypass - allow the regulator to go into bypass mode
3075 *
3076 * @regulator: Regulator to configure
Nishanth Menon9345dfb2013-02-28 18:44:54 -06003077 * @enable: enable or disable bypass mode
Mark Brownf59c8f92012-08-31 10:36:37 -07003078 *
3079 * Allow the regulator to go into bypass mode if all other consumers
3080 * for the regulator also enable bypass mode and the machine
3081 * constraints allow this. Bypass mode means that the regulator is
3082 * simply passing the input directly to the output with no regulation.
3083 */
3084int regulator_allow_bypass(struct regulator *regulator, bool enable)
3085{
3086 struct regulator_dev *rdev = regulator->rdev;
3087 int ret = 0;
3088
3089 if (!rdev->desc->ops->set_bypass)
3090 return 0;
3091
3092 if (rdev->constraints &&
3093 !(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_BYPASS))
3094 return 0;
3095
3096 mutex_lock(&rdev->mutex);
3097
3098 if (enable && !regulator->bypass) {
3099 rdev->bypass_count++;
3100
3101 if (rdev->bypass_count == rdev->open_count) {
3102 ret = rdev->desc->ops->set_bypass(rdev, enable);
3103 if (ret != 0)
3104 rdev->bypass_count--;
3105 }
3106
3107 } else if (!enable && regulator->bypass) {
3108 rdev->bypass_count--;
3109
3110 if (rdev->bypass_count != rdev->open_count) {
3111 ret = rdev->desc->ops->set_bypass(rdev, enable);
3112 if (ret != 0)
3113 rdev->bypass_count++;
3114 }
3115 }
3116
3117 if (ret == 0)
3118 regulator->bypass = enable;
3119
3120 mutex_unlock(&rdev->mutex);
3121
3122 return ret;
3123}
3124EXPORT_SYMBOL_GPL(regulator_allow_bypass);
3125
3126/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003127 * regulator_register_notifier - register regulator event notifier
3128 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003129 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003130 *
3131 * Register notifier block to receive regulator events.
3132 */
3133int regulator_register_notifier(struct regulator *regulator,
3134 struct notifier_block *nb)
3135{
3136 return blocking_notifier_chain_register(&regulator->rdev->notifier,
3137 nb);
3138}
3139EXPORT_SYMBOL_GPL(regulator_register_notifier);
3140
3141/**
3142 * regulator_unregister_notifier - unregister regulator event notifier
3143 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00003144 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01003145 *
3146 * Unregister regulator event notifier block.
3147 */
3148int regulator_unregister_notifier(struct regulator *regulator,
3149 struct notifier_block *nb)
3150{
3151 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
3152 nb);
3153}
3154EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
3155
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003156/* notify regulator consumers and downstream regulator consumers.
3157 * Note mutex must be held by caller.
3158 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003159static void _notifier_call_chain(struct regulator_dev *rdev,
3160 unsigned long event, void *data)
3161{
Liam Girdwood414c70c2008-04-30 15:59:04 +01003162 /* call rdev chain first */
Mark Brownd8493d22012-06-15 19:09:09 +01003163 blocking_notifier_call_chain(&rdev->notifier, event, data);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003164}
3165
3166/**
3167 * regulator_bulk_get - get multiple regulator consumers
3168 *
3169 * @dev: Device to supply
3170 * @num_consumers: Number of consumers to register
3171 * @consumers: Configuration of consumers; clients are stored here.
3172 *
3173 * @return 0 on success, an errno on failure.
3174 *
3175 * This helper function allows drivers to get several regulator
3176 * consumers in one operation. If any of the regulators cannot be
3177 * acquired then any regulators that were allocated will be freed
3178 * before returning to the caller.
3179 */
3180int regulator_bulk_get(struct device *dev, int num_consumers,
3181 struct regulator_bulk_data *consumers)
3182{
3183 int i;
3184 int ret;
3185
3186 for (i = 0; i < num_consumers; i++)
3187 consumers[i].consumer = NULL;
3188
3189 for (i = 0; i < num_consumers; i++) {
3190 consumers[i].consumer = regulator_get(dev,
3191 consumers[i].supply);
3192 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003193 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01003194 dev_err(dev, "Failed to get supply '%s': %d\n",
3195 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003196 consumers[i].consumer = NULL;
3197 goto err;
3198 }
3199 }
3200
3201 return 0;
3202
3203err:
Axel Linb29c7692012-02-20 10:32:16 +08003204 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003205 regulator_put(consumers[i].consumer);
3206
3207 return ret;
3208}
3209EXPORT_SYMBOL_GPL(regulator_bulk_get);
3210
Mark Browne6e74032012-01-20 20:10:08 +00003211/**
3212 * devm_regulator_bulk_get - managed get multiple regulator consumers
3213 *
3214 * @dev: Device to supply
3215 * @num_consumers: Number of consumers to register
3216 * @consumers: Configuration of consumers; clients are stored here.
3217 *
3218 * @return 0 on success, an errno on failure.
3219 *
3220 * This helper function allows drivers to get several regulator
3221 * consumers in one operation with management, the regulators will
3222 * automatically be freed when the device is unbound. If any of the
3223 * regulators cannot be acquired then any regulators that were
3224 * allocated will be freed before returning to the caller.
3225 */
3226int devm_regulator_bulk_get(struct device *dev, int num_consumers,
3227 struct regulator_bulk_data *consumers)
3228{
3229 int i;
3230 int ret;
3231
3232 for (i = 0; i < num_consumers; i++)
3233 consumers[i].consumer = NULL;
3234
3235 for (i = 0; i < num_consumers; i++) {
3236 consumers[i].consumer = devm_regulator_get(dev,
3237 consumers[i].supply);
3238 if (IS_ERR(consumers[i].consumer)) {
3239 ret = PTR_ERR(consumers[i].consumer);
3240 dev_err(dev, "Failed to get supply '%s': %d\n",
3241 consumers[i].supply, ret);
3242 consumers[i].consumer = NULL;
3243 goto err;
3244 }
3245 }
3246
3247 return 0;
3248
3249err:
3250 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
3251 devm_regulator_put(consumers[i].consumer);
3252
3253 return ret;
3254}
3255EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
3256
Mark Brownf21e0e82011-05-24 08:12:40 +08003257static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
3258{
3259 struct regulator_bulk_data *bulk = data;
3260
3261 bulk->ret = regulator_enable(bulk->consumer);
3262}
3263
Liam Girdwood414c70c2008-04-30 15:59:04 +01003264/**
3265 * regulator_bulk_enable - enable multiple regulator consumers
3266 *
3267 * @num_consumers: Number of consumers
3268 * @consumers: Consumer data; clients are stored here.
3269 * @return 0 on success, an errno on failure
3270 *
3271 * This convenience API allows consumers to enable multiple regulator
3272 * clients in a single API call. If any consumers cannot be enabled
3273 * then any others that were enabled will be disabled again prior to
3274 * return.
3275 */
3276int regulator_bulk_enable(int num_consumers,
3277 struct regulator_bulk_data *consumers)
3278{
Dan Williams2955b472012-07-09 19:33:25 -07003279 ASYNC_DOMAIN_EXCLUSIVE(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003280 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08003281 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003282
Mark Brown6492bc12012-04-19 13:19:07 +01003283 for (i = 0; i < num_consumers; i++) {
3284 if (consumers[i].consumer->always_on)
3285 consumers[i].ret = 0;
3286 else
3287 async_schedule_domain(regulator_bulk_enable_async,
3288 &consumers[i], &async_domain);
3289 }
Mark Brownf21e0e82011-05-24 08:12:40 +08003290
3291 async_synchronize_full_domain(&async_domain);
3292
3293 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01003294 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08003295 if (consumers[i].ret != 0) {
3296 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003297 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08003298 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003299 }
3300
3301 return 0;
3302
3303err:
Andrzej Hajdafbe31052013-03-01 12:24:05 +01003304 for (i = 0; i < num_consumers; i++) {
3305 if (consumers[i].ret < 0)
3306 pr_err("Failed to enable %s: %d\n", consumers[i].supply,
3307 consumers[i].ret);
3308 else
3309 regulator_disable(consumers[i].consumer);
3310 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003311
3312 return ret;
3313}
3314EXPORT_SYMBOL_GPL(regulator_bulk_enable);
3315
3316/**
3317 * regulator_bulk_disable - disable multiple regulator consumers
3318 *
3319 * @num_consumers: Number of consumers
3320 * @consumers: Consumer data; clients are stored here.
3321 * @return 0 on success, an errno on failure
3322 *
3323 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003324 * clients in a single API call. If any consumers cannot be disabled
3325 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01003326 * return.
3327 */
3328int regulator_bulk_disable(int num_consumers,
3329 struct regulator_bulk_data *consumers)
3330{
3331 int i;
Mark Brown01e86f42012-03-28 21:36:38 +01003332 int ret, r;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003333
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01003334 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01003335 ret = regulator_disable(consumers[i].consumer);
3336 if (ret != 0)
3337 goto err;
3338 }
3339
3340 return 0;
3341
3342err:
Joe Perches5da84fd2010-11-30 05:53:48 -08003343 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Mark Brown01e86f42012-03-28 21:36:38 +01003344 for (++i; i < num_consumers; ++i) {
3345 r = regulator_enable(consumers[i].consumer);
3346 if (r != 0)
3347 pr_err("Failed to reename %s: %d\n",
3348 consumers[i].supply, r);
3349 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003350
3351 return ret;
3352}
3353EXPORT_SYMBOL_GPL(regulator_bulk_disable);
3354
3355/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09003356 * regulator_bulk_force_disable - force disable multiple regulator consumers
3357 *
3358 * @num_consumers: Number of consumers
3359 * @consumers: Consumer data; clients are stored here.
3360 * @return 0 on success, an errno on failure
3361 *
3362 * This convenience API allows consumers to forcibly disable multiple regulator
3363 * clients in a single API call.
3364 * NOTE: This should be used for situations when device damage will
3365 * likely occur if the regulators are not disabled (e.g. over temp).
3366 * Although regulator_force_disable function call for some consumers can
3367 * return error numbers, the function is called for all consumers.
3368 */
3369int regulator_bulk_force_disable(int num_consumers,
3370 struct regulator_bulk_data *consumers)
3371{
3372 int i;
3373 int ret;
3374
3375 for (i = 0; i < num_consumers; i++)
3376 consumers[i].ret =
3377 regulator_force_disable(consumers[i].consumer);
3378
3379 for (i = 0; i < num_consumers; i++) {
3380 if (consumers[i].ret != 0) {
3381 ret = consumers[i].ret;
3382 goto out;
3383 }
3384 }
3385
3386 return 0;
3387out:
3388 return ret;
3389}
3390EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
3391
3392/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003393 * regulator_bulk_free - free multiple regulator consumers
3394 *
3395 * @num_consumers: Number of consumers
3396 * @consumers: Consumer data; clients are stored here.
3397 *
3398 * This convenience API allows consumers to free multiple regulator
3399 * clients in a single API call.
3400 */
3401void regulator_bulk_free(int num_consumers,
3402 struct regulator_bulk_data *consumers)
3403{
3404 int i;
3405
3406 for (i = 0; i < num_consumers; i++) {
3407 regulator_put(consumers[i].consumer);
3408 consumers[i].consumer = NULL;
3409 }
3410}
3411EXPORT_SYMBOL_GPL(regulator_bulk_free);
3412
3413/**
3414 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00003415 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01003416 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00003417 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003418 *
3419 * Called by regulator drivers to notify clients a regulator event has
3420 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00003421 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003422 */
3423int regulator_notifier_call_chain(struct regulator_dev *rdev,
3424 unsigned long event, void *data)
3425{
3426 _notifier_call_chain(rdev, event, data);
3427 return NOTIFY_DONE;
3428
3429}
3430EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
3431
Mark Brownbe721972009-08-04 20:09:52 +02003432/**
3433 * regulator_mode_to_status - convert a regulator mode into a status
3434 *
3435 * @mode: Mode to convert
3436 *
3437 * Convert a regulator mode into a status.
3438 */
3439int regulator_mode_to_status(unsigned int mode)
3440{
3441 switch (mode) {
3442 case REGULATOR_MODE_FAST:
3443 return REGULATOR_STATUS_FAST;
3444 case REGULATOR_MODE_NORMAL:
3445 return REGULATOR_STATUS_NORMAL;
3446 case REGULATOR_MODE_IDLE:
3447 return REGULATOR_STATUS_IDLE;
Krystian Garbaciak03ffcf32012-07-12 11:50:38 +01003448 case REGULATOR_MODE_STANDBY:
Mark Brownbe721972009-08-04 20:09:52 +02003449 return REGULATOR_STATUS_STANDBY;
3450 default:
Krystian Garbaciak1beaf762012-07-12 13:53:35 +01003451 return REGULATOR_STATUS_UNDEFINED;
Mark Brownbe721972009-08-04 20:09:52 +02003452 }
3453}
3454EXPORT_SYMBOL_GPL(regulator_mode_to_status);
3455
David Brownell7ad68e22008-11-11 17:39:02 -08003456/*
3457 * To avoid cluttering sysfs (and memory) with useless state, only
3458 * create attributes that can be meaningfully displayed.
3459 */
3460static int add_regulator_attributes(struct regulator_dev *rdev)
3461{
3462 struct device *dev = &rdev->dev;
3463 struct regulator_ops *ops = rdev->desc->ops;
3464 int status = 0;
3465
3466 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00003467 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
Mark Brownf2889e62012-08-27 11:37:04 -07003468 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0) ||
3469 (ops->list_voltage && ops->list_voltage(rdev, 0) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08003470 status = device_create_file(dev, &dev_attr_microvolts);
3471 if (status < 0)
3472 return status;
3473 }
3474 if (ops->get_current_limit) {
3475 status = device_create_file(dev, &dev_attr_microamps);
3476 if (status < 0)
3477 return status;
3478 }
3479 if (ops->get_mode) {
3480 status = device_create_file(dev, &dev_attr_opmode);
3481 if (status < 0)
3482 return status;
3483 }
Kim, Milo7b74d142013-02-18 06:50:55 +00003484 if (rdev->ena_pin || ops->is_enabled) {
David Brownell7ad68e22008-11-11 17:39:02 -08003485 status = device_create_file(dev, &dev_attr_state);
3486 if (status < 0)
3487 return status;
3488 }
David Brownell853116a2009-01-14 23:03:17 -08003489 if (ops->get_status) {
3490 status = device_create_file(dev, &dev_attr_status);
3491 if (status < 0)
3492 return status;
3493 }
Mark Brownf59c8f92012-08-31 10:36:37 -07003494 if (ops->get_bypass) {
3495 status = device_create_file(dev, &dev_attr_bypass);
3496 if (status < 0)
3497 return status;
3498 }
David Brownell7ad68e22008-11-11 17:39:02 -08003499
3500 /* some attributes are type-specific */
3501 if (rdev->desc->type == REGULATOR_CURRENT) {
3502 status = device_create_file(dev, &dev_attr_requested_microamps);
3503 if (status < 0)
3504 return status;
3505 }
3506
3507 /* all the other attributes exist to support constraints;
3508 * don't show them if there are no constraints, or if the
3509 * relevant supporting methods are missing.
3510 */
3511 if (!rdev->constraints)
3512 return status;
3513
3514 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00003515 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08003516 status = device_create_file(dev, &dev_attr_min_microvolts);
3517 if (status < 0)
3518 return status;
3519 status = device_create_file(dev, &dev_attr_max_microvolts);
3520 if (status < 0)
3521 return status;
3522 }
3523 if (ops->set_current_limit) {
3524 status = device_create_file(dev, &dev_attr_min_microamps);
3525 if (status < 0)
3526 return status;
3527 status = device_create_file(dev, &dev_attr_max_microamps);
3528 if (status < 0)
3529 return status;
3530 }
3531
David Brownell7ad68e22008-11-11 17:39:02 -08003532 status = device_create_file(dev, &dev_attr_suspend_standby_state);
3533 if (status < 0)
3534 return status;
3535 status = device_create_file(dev, &dev_attr_suspend_mem_state);
3536 if (status < 0)
3537 return status;
3538 status = device_create_file(dev, &dev_attr_suspend_disk_state);
3539 if (status < 0)
3540 return status;
3541
3542 if (ops->set_suspend_voltage) {
3543 status = device_create_file(dev,
3544 &dev_attr_suspend_standby_microvolts);
3545 if (status < 0)
3546 return status;
3547 status = device_create_file(dev,
3548 &dev_attr_suspend_mem_microvolts);
3549 if (status < 0)
3550 return status;
3551 status = device_create_file(dev,
3552 &dev_attr_suspend_disk_microvolts);
3553 if (status < 0)
3554 return status;
3555 }
3556
3557 if (ops->set_suspend_mode) {
3558 status = device_create_file(dev,
3559 &dev_attr_suspend_standby_mode);
3560 if (status < 0)
3561 return status;
3562 status = device_create_file(dev,
3563 &dev_attr_suspend_mem_mode);
3564 if (status < 0)
3565 return status;
3566 status = device_create_file(dev,
3567 &dev_attr_suspend_disk_mode);
3568 if (status < 0)
3569 return status;
3570 }
3571
3572 return status;
3573}
3574
Mark Brown1130e5b2010-12-21 23:49:31 +00003575static void rdev_init_debugfs(struct regulator_dev *rdev)
3576{
Mark Brown1130e5b2010-12-21 23:49:31 +00003577 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
Stephen Boyd24751432012-02-20 22:50:42 -08003578 if (!rdev->debugfs) {
Mark Brown1130e5b2010-12-21 23:49:31 +00003579 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003580 return;
3581 }
3582
3583 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3584 &rdev->use_count);
3585 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3586 &rdev->open_count);
Mark Brownf59c8f92012-08-31 10:36:37 -07003587 debugfs_create_u32("bypass_count", 0444, rdev->debugfs,
3588 &rdev->bypass_count);
Mark Brown1130e5b2010-12-21 23:49:31 +00003589}
3590
Liam Girdwood414c70c2008-04-30 15:59:04 +01003591/**
3592 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003593 * @regulator_desc: regulator to register
Mark Brownc1727082012-04-04 00:50:22 +01003594 * @config: runtime configuration for regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003595 *
3596 * Called by regulator drivers to register a regulator.
Axel Lin03846182013-01-03 21:01:47 +08003597 * Returns a valid pointer to struct regulator_dev on success
3598 * or an ERR_PTR() on error.
Liam Girdwood414c70c2008-04-30 15:59:04 +01003599 */
Mark Brown65f26842012-04-03 20:46:53 +01003600struct regulator_dev *
3601regulator_register(const struct regulator_desc *regulator_desc,
Mark Brownc1727082012-04-04 00:50:22 +01003602 const struct regulator_config *config)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003603{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003604 const struct regulation_constraints *constraints = NULL;
Mark Brownc1727082012-04-04 00:50:22 +01003605 const struct regulator_init_data *init_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003606 static atomic_t regulator_no = ATOMIC_INIT(0);
3607 struct regulator_dev *rdev;
Mark Brown32c8fad2012-04-11 10:19:12 +01003608 struct device *dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003609 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303610 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003611
Mark Brownc1727082012-04-04 00:50:22 +01003612 if (regulator_desc == NULL || config == NULL)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003613 return ERR_PTR(-EINVAL);
3614
Mark Brown32c8fad2012-04-11 10:19:12 +01003615 dev = config->dev;
Mark Browndcf70112012-05-08 18:09:12 +01003616 WARN_ON(!dev);
Mark Brown32c8fad2012-04-11 10:19:12 +01003617
Liam Girdwood414c70c2008-04-30 15:59:04 +01003618 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3619 return ERR_PTR(-EINVAL);
3620
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003621 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3622 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003623 return ERR_PTR(-EINVAL);
3624
Mark Brown476c2d82010-12-10 17:28:07 +00003625 /* Only one of each should be implemented */
3626 WARN_ON(regulator_desc->ops->get_voltage &&
3627 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003628 WARN_ON(regulator_desc->ops->set_voltage &&
3629 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003630
3631 /* If we're using selectors we must implement list_voltage. */
3632 if (regulator_desc->ops->get_voltage_sel &&
3633 !regulator_desc->ops->list_voltage) {
3634 return ERR_PTR(-EINVAL);
3635 }
Mark Browne8eef822010-12-12 14:36:17 +00003636 if (regulator_desc->ops->set_voltage_sel &&
3637 !regulator_desc->ops->list_voltage) {
3638 return ERR_PTR(-EINVAL);
3639 }
Mark Brown476c2d82010-12-10 17:28:07 +00003640
Mark Brownc1727082012-04-04 00:50:22 +01003641 init_data = config->init_data;
3642
Liam Girdwood414c70c2008-04-30 15:59:04 +01003643 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3644 if (rdev == NULL)
3645 return ERR_PTR(-ENOMEM);
3646
3647 mutex_lock(&regulator_list_mutex);
3648
3649 mutex_init(&rdev->mutex);
Mark Brownc1727082012-04-04 00:50:22 +01003650 rdev->reg_data = config->driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003651 rdev->owner = regulator_desc->owner;
3652 rdev->desc = regulator_desc;
Mark Brown3a4b0a02012-05-08 18:10:45 +01003653 if (config->regmap)
3654 rdev->regmap = config->regmap;
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303655 else if (dev_get_regmap(dev, NULL))
Mark Brown3a4b0a02012-05-08 18:10:45 +01003656 rdev->regmap = dev_get_regmap(dev, NULL);
AnilKumar Ch52b84da2012-09-07 20:45:05 +05303657 else if (dev->parent)
3658 rdev->regmap = dev_get_regmap(dev->parent, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003659 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003660 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003661 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003662 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003663
Liam Girdwooda5766f12008-10-10 13:22:20 +01003664 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003665 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003666 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003667 if (ret < 0)
3668 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003669 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003670
Liam Girdwooda5766f12008-10-10 13:22:20 +01003671 /* register with sysfs */
3672 rdev->dev.class = &regulator_class;
Mark Brownc1727082012-04-04 00:50:22 +01003673 rdev->dev.of_node = config->of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003674 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003675 dev_set_name(&rdev->dev, "regulator.%d",
3676 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003677 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003678 if (ret != 0) {
3679 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003680 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003681 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003682
3683 dev_set_drvdata(&rdev->dev, rdev);
3684
Marek Szyprowskib2a1ef42012-08-09 16:33:00 +02003685 if (config->ena_gpio && gpio_is_valid(config->ena_gpio)) {
Kim, Milof19b00d2013-02-18 06:50:39 +00003686 ret = regulator_ena_gpio_request(rdev, config);
Mark Brown65f73502012-06-27 14:14:38 +01003687 if (ret != 0) {
3688 rdev_err(rdev, "Failed to request enable GPIO%d: %d\n",
3689 config->ena_gpio, ret);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003690 goto wash;
Mark Brown65f73502012-06-27 14:14:38 +01003691 }
3692
Mark Brown65f73502012-06-27 14:14:38 +01003693 if (config->ena_gpio_flags & GPIOF_OUT_INIT_HIGH)
3694 rdev->ena_gpio_state = 1;
3695
Kim, Milo7b74d142013-02-18 06:50:55 +00003696 if (config->ena_gpio_invert)
Mark Brown65f73502012-06-27 14:14:38 +01003697 rdev->ena_gpio_state = !rdev->ena_gpio_state;
3698 }
3699
Mike Rapoport74f544c2008-11-25 14:53:53 +02003700 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003701 if (init_data)
3702 constraints = &init_data->constraints;
3703
3704 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003705 if (ret < 0)
3706 goto scrub;
3707
David Brownell7ad68e22008-11-11 17:39:02 -08003708 /* add attributes supported by this regulator */
3709 ret = add_regulator_attributes(rdev);
3710 if (ret < 0)
3711 goto scrub;
3712
Mark Brown9a8f5e02011-11-29 18:11:19 +00003713 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303714 supply = init_data->supply_regulator;
3715 else if (regulator_desc->supply_name)
3716 supply = regulator_desc->supply_name;
3717
3718 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003719 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003720
Mark Brown6d191a52012-03-29 14:19:02 +01003721 r = regulator_dev_lookup(dev, supply, &ret);
Mark Brown0178f3e2010-04-26 15:18:14 +01003722
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003723 if (ret == -ENODEV) {
3724 /*
3725 * No supply was specified for this regulator and
3726 * there will never be one.
3727 */
3728 ret = 0;
3729 goto add_dev;
3730 } else if (!r) {
Rajendra Nayak69511a42011-11-18 16:47:20 +05303731 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003732 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003733 goto scrub;
3734 }
3735
3736 ret = set_supply(rdev, r);
3737 if (ret < 0)
3738 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303739
3740 /* Enable supply if rail is enabled */
Mark Brownb1a86832012-05-14 00:40:14 +01003741 if (_regulator_is_enabled(rdev)) {
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303742 ret = regulator_enable(rdev->supply);
3743 if (ret < 0)
3744 goto scrub;
3745 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003746 }
3747
Andrew Bresticker0f7b87f2013-04-04 15:27:47 -07003748add_dev:
Liam Girdwooda5766f12008-10-10 13:22:20 +01003749 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003750 if (init_data) {
3751 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3752 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003753 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003754 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003755 if (ret < 0) {
3756 dev_err(dev, "Failed to set supply %s\n",
3757 init_data->consumer_supplies[i].supply);
3758 goto unset_supplies;
3759 }
Mark Brown23c2f042011-02-24 17:39:09 +00003760 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003761 }
3762
3763 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003764
3765 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003766out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01003767 mutex_unlock(&regulator_list_mutex);
3768 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003769
Jani Nikulad4033b52010-04-29 10:55:11 +03003770unset_supplies:
3771 unset_regulator_supplies(rdev);
3772
David Brownell4fca9542008-11-11 17:38:53 -08003773scrub:
Mark Browne81dba852012-05-13 18:35:56 +01003774 if (rdev->supply)
Charles Keepax23ff2f02012-11-14 09:39:31 +00003775 _regulator_put(rdev->supply);
Kim, Milof19b00d2013-02-18 06:50:39 +00003776 regulator_ena_gpio_free(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +08003777 kfree(rdev->constraints);
Andrew Lunnb2da55d2012-10-28 16:01:11 +01003778wash:
David Brownell4fca9542008-11-11 17:38:53 -08003779 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003780 /* device core frees rdev */
3781 rdev = ERR_PTR(ret);
3782 goto out;
3783
David Brownell4fca9542008-11-11 17:38:53 -08003784clean:
3785 kfree(rdev);
3786 rdev = ERR_PTR(ret);
3787 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003788}
3789EXPORT_SYMBOL_GPL(regulator_register);
3790
3791/**
3792 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003793 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003794 *
3795 * Called by regulator drivers to unregister a regulator.
3796 */
3797void regulator_unregister(struct regulator_dev *rdev)
3798{
3799 if (rdev == NULL)
3800 return;
3801
Mark Browne032b372012-03-28 21:17:55 +01003802 if (rdev->supply)
3803 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003804 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003805 debugfs_remove_recursive(rdev->debugfs);
Tejun Heo43829732012-08-20 14:51:24 -07003806 flush_work(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003807 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003808 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003809 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003810 kfree(rdev->constraints);
Kim, Milof19b00d2013-02-18 06:50:39 +00003811 regulator_ena_gpio_free(rdev);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003812 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003813 mutex_unlock(&regulator_list_mutex);
3814}
3815EXPORT_SYMBOL_GPL(regulator_unregister);
3816
3817/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003818 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003819 * @state: system suspend state
3820 *
3821 * Configure each regulator with it's suspend operating parameters for state.
3822 * This will usually be called by machine suspend code prior to supending.
3823 */
3824int regulator_suspend_prepare(suspend_state_t state)
3825{
3826 struct regulator_dev *rdev;
3827 int ret = 0;
3828
3829 /* ON is handled by regulator active state */
3830 if (state == PM_SUSPEND_ON)
3831 return -EINVAL;
3832
3833 mutex_lock(&regulator_list_mutex);
3834 list_for_each_entry(rdev, &regulator_list, list) {
3835
3836 mutex_lock(&rdev->mutex);
3837 ret = suspend_prepare(rdev, state);
3838 mutex_unlock(&rdev->mutex);
3839
3840 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003841 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003842 goto out;
3843 }
3844 }
3845out:
3846 mutex_unlock(&regulator_list_mutex);
3847 return ret;
3848}
3849EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3850
3851/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003852 * regulator_suspend_finish - resume regulators from system wide suspend
3853 *
3854 * Turn on regulators that might be turned off by regulator_suspend_prepare
3855 * and that should be turned on according to the regulators properties.
3856 */
3857int regulator_suspend_finish(void)
3858{
3859 struct regulator_dev *rdev;
3860 int ret = 0, error;
3861
3862 mutex_lock(&regulator_list_mutex);
3863 list_for_each_entry(rdev, &regulator_list, list) {
3864 struct regulator_ops *ops = rdev->desc->ops;
3865
3866 mutex_lock(&rdev->mutex);
3867 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3868 ops->enable) {
3869 error = ops->enable(rdev);
3870 if (error)
3871 ret = error;
3872 } else {
3873 if (!has_full_constraints)
3874 goto unlock;
3875 if (!ops->disable)
3876 goto unlock;
Mark Brownb1a86832012-05-14 00:40:14 +01003877 if (!_regulator_is_enabled(rdev))
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003878 goto unlock;
3879
3880 error = ops->disable(rdev);
3881 if (error)
3882 ret = error;
3883 }
3884unlock:
3885 mutex_unlock(&rdev->mutex);
3886 }
3887 mutex_unlock(&regulator_list_mutex);
3888 return ret;
3889}
3890EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3891
3892/**
Mark Brownca725562009-03-16 19:36:34 +00003893 * regulator_has_full_constraints - the system has fully specified constraints
3894 *
3895 * Calling this function will cause the regulator API to disable all
3896 * regulators which have a zero use count and don't have an always_on
3897 * constraint in a late_initcall.
3898 *
3899 * The intention is that this will become the default behaviour in a
3900 * future kernel release so users are encouraged to use this facility
3901 * now.
3902 */
3903void regulator_has_full_constraints(void)
3904{
3905 has_full_constraints = 1;
3906}
3907EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3908
3909/**
Mark Brown688fe992010-10-05 19:18:32 -07003910 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3911 *
3912 * Calling this function will cause the regulator API to provide a
3913 * dummy regulator to consumers if no physical regulator is found,
3914 * allowing most consumers to proceed as though a regulator were
3915 * configured. This allows systems such as those with software
3916 * controllable regulators for the CPU core only to be brought up more
3917 * readily.
3918 */
3919void regulator_use_dummy_regulator(void)
3920{
3921 board_wants_dummy_regulator = true;
3922}
3923EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3924
3925/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003926 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003927 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003928 *
3929 * Get rdev regulator driver private data. This call can be used in the
3930 * regulator driver context.
3931 */
3932void *rdev_get_drvdata(struct regulator_dev *rdev)
3933{
3934 return rdev->reg_data;
3935}
3936EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3937
3938/**
3939 * regulator_get_drvdata - get regulator driver data
3940 * @regulator: regulator
3941 *
3942 * Get regulator driver private data. This call can be used in the consumer
3943 * driver context when non API regulator specific functions need to be called.
3944 */
3945void *regulator_get_drvdata(struct regulator *regulator)
3946{
3947 return regulator->rdev->reg_data;
3948}
3949EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3950
3951/**
3952 * regulator_set_drvdata - set regulator driver data
3953 * @regulator: regulator
3954 * @data: data
3955 */
3956void regulator_set_drvdata(struct regulator *regulator, void *data)
3957{
3958 regulator->rdev->reg_data = data;
3959}
3960EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3961
3962/**
3963 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003964 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003965 */
3966int rdev_get_id(struct regulator_dev *rdev)
3967{
3968 return rdev->desc->id;
3969}
3970EXPORT_SYMBOL_GPL(rdev_get_id);
3971
Liam Girdwooda5766f12008-10-10 13:22:20 +01003972struct device *rdev_get_dev(struct regulator_dev *rdev)
3973{
3974 return &rdev->dev;
3975}
3976EXPORT_SYMBOL_GPL(rdev_get_dev);
3977
3978void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3979{
3980 return reg_init_data->driver_data;
3981}
3982EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3983
Mark Brownba55a972011-08-23 17:39:10 +01003984#ifdef CONFIG_DEBUG_FS
3985static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3986 size_t count, loff_t *ppos)
3987{
3988 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3989 ssize_t len, ret = 0;
3990 struct regulator_map *map;
3991
3992 if (!buf)
3993 return -ENOMEM;
3994
3995 list_for_each_entry(map, &regulator_map_list, list) {
3996 len = snprintf(buf + ret, PAGE_SIZE - ret,
3997 "%s -> %s.%s\n",
3998 rdev_get_name(map->regulator), map->dev_name,
3999 map->supply);
4000 if (len >= 0)
4001 ret += len;
4002 if (ret > PAGE_SIZE) {
4003 ret = PAGE_SIZE;
4004 break;
4005 }
4006 }
4007
4008 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
4009
4010 kfree(buf);
4011
4012 return ret;
4013}
Stephen Boyd24751432012-02-20 22:50:42 -08004014#endif
Mark Brownba55a972011-08-23 17:39:10 +01004015
4016static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08004017#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01004018 .read = supply_map_read_file,
4019 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01004020#endif
Stephen Boyd24751432012-02-20 22:50:42 -08004021};
Mark Brownba55a972011-08-23 17:39:10 +01004022
Liam Girdwood414c70c2008-04-30 15:59:04 +01004023static int __init regulator_init(void)
4024{
Mark Brown34abbd62010-02-12 10:18:08 +00004025 int ret;
4026
Mark Brown34abbd62010-02-12 10:18:08 +00004027 ret = class_register(&regulator_class);
4028
Mark Brown1130e5b2010-12-21 23:49:31 +00004029 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08004030 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00004031 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01004032
Mark Brownf4d562c2012-02-20 21:01:04 +00004033 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
4034 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00004035
Mark Brown34abbd62010-02-12 10:18:08 +00004036 regulator_dummy_init();
4037
4038 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01004039}
4040
4041/* init early to allow our consumers to complete system booting */
4042core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00004043
4044static int __init regulator_init_complete(void)
4045{
4046 struct regulator_dev *rdev;
4047 struct regulator_ops *ops;
4048 struct regulation_constraints *c;
4049 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00004050
Mark Brown86f5fcf2012-07-06 18:19:13 +01004051 /*
4052 * Since DT doesn't provide an idiomatic mechanism for
4053 * enabling full constraints and since it's much more natural
4054 * with DT to provide them just assume that a DT enabled
4055 * system has full constraints.
4056 */
4057 if (of_have_populated_dt())
4058 has_full_constraints = true;
4059
Mark Brownca725562009-03-16 19:36:34 +00004060 mutex_lock(&regulator_list_mutex);
4061
4062 /* If we have a full configuration then disable any regulators
4063 * which are not in use or always_on. This will become the
4064 * default behaviour in the future.
4065 */
4066 list_for_each_entry(rdev, &regulator_list, list) {
4067 ops = rdev->desc->ops;
4068 c = rdev->constraints;
4069
Mark Brownf25e0b42009-08-03 18:49:55 +01004070 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00004071 continue;
4072
4073 mutex_lock(&rdev->mutex);
4074
4075 if (rdev->use_count)
4076 goto unlock;
4077
4078 /* If we can't read the status assume it's on. */
4079 if (ops->is_enabled)
4080 enabled = ops->is_enabled(rdev);
4081 else
4082 enabled = 1;
4083
4084 if (!enabled)
4085 goto unlock;
4086
4087 if (has_full_constraints) {
4088 /* We log since this may kill the system if it
4089 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08004090 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00004091 ret = ops->disable(rdev);
4092 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08004093 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00004094 }
4095 } else {
4096 /* The intention is that in future we will
4097 * assume that full constraints are provided
4098 * so warn even if we aren't going to do
4099 * anything here.
4100 */
Joe Perches5da84fd2010-11-30 05:53:48 -08004101 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00004102 }
4103
4104unlock:
4105 mutex_unlock(&rdev->mutex);
4106 }
4107
4108 mutex_unlock(&regulator_list_mutex);
4109
4110 return 0;
4111}
4112late_initcall(regulator_init_complete);