blob: 0ebb944dd74cac5ea01c1e85598c8a2d3d50615e [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>
Rajendra Nayak69511a42011-11-18 16:47:20 +053026#include <linux/of.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/seq_file.h>
28#include <linux/uaccess.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);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056static int suppress_info_printing;
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/*
73 * struct regulator
74 *
75 * One for each consumer device.
76 */
77struct regulator {
78 struct device *dev;
79 struct list_head list;
80 int uA_load;
81 int min_uV;
82 int max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 int enabled;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010088};
89
90static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010091static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010092static int _regulator_get_voltage(struct regulator_dev *rdev);
93static int _regulator_get_current_limit(struct regulator_dev *rdev);
94static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000097static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010099static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102
Mark Brown1083c392009-10-22 16:31:32 +0100103static const char *rdev_get_name(struct regulator_dev *rdev)
104{
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111}
112
Liam Girdwood414c70c2008-04-30 15:59:04 +0100113/* gets the regulator for a given consumer device */
114static struct regulator *get_device_regulator(struct device *dev)
115{
116 struct regulator *regulator = NULL;
117 struct regulator_dev *rdev;
118
119 mutex_lock(&regulator_list_mutex);
120 list_for_each_entry(rdev, &regulator_list, list) {
121 mutex_lock(&rdev->mutex);
122 list_for_each_entry(regulator, &rdev->consumer_list, list) {
123 if (regulator->dev == dev) {
124 mutex_unlock(&rdev->mutex);
125 mutex_unlock(&regulator_list_mutex);
126 return regulator;
127 }
128 }
129 mutex_unlock(&rdev->mutex);
130 }
131 mutex_unlock(&regulator_list_mutex);
132 return NULL;
133}
134
Rajendra Nayak69511a42011-11-18 16:47:20 +0530135/**
136 * of_get_regulator - get a regulator device node based on supply name
137 * @dev: Device pointer for the consumer (of regulator) device
138 * @supply: regulator supply name
139 *
140 * Extract the regulator device node corresponding to the supply name.
141 * retruns the device node corresponding to the regulator if found, else
142 * returns NULL.
143 */
144static struct device_node *of_get_regulator(struct device *dev, const char *supply)
145{
146 struct device_node *regnode = NULL;
147 char prop_name[32]; /* 32 is max size of property name */
148
149 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
150
151 snprintf(prop_name, 32, "%s-supply", supply);
152 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
153
154 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530155 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530156 prop_name, dev->of_node->full_name);
157 return NULL;
158 }
159 return regnode;
160}
161
Liam Girdwood414c70c2008-04-30 15:59:04 +0100162/* Platform voltage constraint check */
163static int regulator_check_voltage(struct regulator_dev *rdev,
164 int *min_uV, int *max_uV)
165{
166 BUG_ON(*min_uV > *max_uV);
167
168 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800169 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 return -ENODEV;
171 }
172 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800173 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -EPERM;
175 }
176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 /* check if requested voltage range actually overlaps the constraints */
178 if (*max_uV < rdev->constraints->min_uV ||
179 *min_uV > rdev->constraints->max_uV) {
180 rdev_err(rdev, "requested voltage range [%d, %d] does not fit "
181 "within constraints: [%d, %d]\n", *min_uV, *max_uV,
182 rdev->constraints->min_uV, rdev->constraints->max_uV);
183 return -EINVAL;
184 }
185
Liam Girdwood414c70c2008-04-30 15:59:04 +0100186 if (*max_uV > rdev->constraints->max_uV)
187 *max_uV = rdev->constraints->max_uV;
188 if (*min_uV < rdev->constraints->min_uV)
189 *min_uV = rdev->constraints->min_uV;
190
Mark Brown89f425e2011-07-12 11:20:37 +0900191 if (*min_uV > *max_uV) {
192 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100193 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900195 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196
197 return 0;
198}
199
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100200/* Make sure we select a voltage that suits the needs of all
201 * regulator consumers
202 */
203static int regulator_check_consumers(struct regulator_dev *rdev,
204 int *min_uV, int *max_uV)
205{
206 struct regulator *regulator;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700207 int init_min_uV = *min_uV;
208 int init_max_uV = *max_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100209
210 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700211 /*
212 * Assume consumers that didn't say anything are OK
213 * with anything in the constraint range.
214 */
215 if (!regulator->min_uV && !regulator->max_uV)
216 continue;
217
Steve Mucklef132c6c2012-06-06 18:30:57 -0700218 if (init_max_uV < regulator->min_uV
219 || init_min_uV > regulator->max_uV)
220 rdev_err(rdev, "requested voltage range [%d, %d] does "
221 "not fit within previously voted range: "
222 "[%d, %d]\n", init_min_uV, init_max_uV,
223 regulator->min_uV, regulator->max_uV);
224
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100225 if (*max_uV > regulator->max_uV)
226 *max_uV = regulator->max_uV;
227 if (*min_uV < regulator->min_uV)
228 *min_uV = regulator->min_uV;
229 }
230
231 if (*min_uV > *max_uV)
232 return -EINVAL;
233
234 return 0;
235}
236
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237/* current constraint check */
238static int regulator_check_current_limit(struct regulator_dev *rdev,
239 int *min_uA, int *max_uA)
240{
241 BUG_ON(*min_uA > *max_uA);
242
243 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800244 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245 return -ENODEV;
246 }
247 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800248 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 return -EPERM;
250 }
251
252 if (*max_uA > rdev->constraints->max_uA)
253 *max_uA = rdev->constraints->max_uA;
254 if (*min_uA < rdev->constraints->min_uA)
255 *min_uA = rdev->constraints->min_uA;
256
Mark Brown89f425e2011-07-12 11:20:37 +0900257 if (*min_uA > *max_uA) {
258 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100259 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900261 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100262
263 return 0;
264}
265
266/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900267static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100268{
Mark Brown2c608232011-03-30 06:29:12 +0900269 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800270 case REGULATOR_MODE_FAST:
271 case REGULATOR_MODE_NORMAL:
272 case REGULATOR_MODE_IDLE:
273 case REGULATOR_MODE_STANDBY:
274 break;
275 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900276 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800277 return -EINVAL;
278 }
279
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800281 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282 return -ENODEV;
283 }
284 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800285 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 return -EPERM;
287 }
Mark Brown2c608232011-03-30 06:29:12 +0900288
289 /* The modes are bitmasks, the most power hungry modes having
290 * the lowest values. If the requested mode isn't supported
291 * try higher modes. */
292 while (*mode) {
293 if (rdev->constraints->valid_modes_mask & *mode)
294 return 0;
295 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296 }
Mark Brown2c608232011-03-30 06:29:12 +0900297
298 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299}
300
301/* dynamic regulator mode switching constraint check */
302static int regulator_check_drms(struct regulator_dev *rdev)
303{
304 if (!rdev->constraints) {
Matt Wagantall812a8852012-06-27 14:36:20 -0700305 rdev_dbg(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100306 return -ENODEV;
307 }
308 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Matt Wagantall812a8852012-06-27 14:36:20 -0700309 rdev_dbg(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310 return -EPERM;
311 }
312 return 0;
313}
314
315static ssize_t device_requested_uA_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct regulator *regulator;
319
320 regulator = get_device_regulator(dev);
321 if (regulator == NULL)
322 return 0;
323
324 return sprintf(buf, "%d\n", regulator->uA_load);
325}
326
327static ssize_t regulator_uV_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100330 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331 ssize_t ret;
332
333 mutex_lock(&rdev->mutex);
334 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
335 mutex_unlock(&rdev->mutex);
336
337 return ret;
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340
341static ssize_t regulator_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100344 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
347}
David Brownell7ad68e22008-11-11 17:39:02 -0800348static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349
Mark Brownbc558a62008-10-10 15:33:20 +0100350static ssize_t regulator_name_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100354
Mark Brown1083c392009-10-22 16:31:32 +0100355 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100356}
357
David Brownell4fca9542008-11-11 17:38:53 -0800358static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360 switch (mode) {
361 case REGULATOR_MODE_FAST:
362 return sprintf(buf, "fast\n");
363 case REGULATOR_MODE_NORMAL:
364 return sprintf(buf, "normal\n");
365 case REGULATOR_MODE_IDLE:
366 return sprintf(buf, "idle\n");
367 case REGULATOR_MODE_STANDBY:
368 return sprintf(buf, "standby\n");
369 }
370 return sprintf(buf, "unknown\n");
371}
372
David Brownell4fca9542008-11-11 17:38:53 -0800373static ssize_t regulator_opmode_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100375{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100376 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
David Brownell4fca9542008-11-11 17:38:53 -0800378 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
379}
David Brownell7ad68e22008-11-11 17:39:02 -0800380static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800381
382static ssize_t regulator_print_state(char *buf, int state)
383{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384 if (state > 0)
385 return sprintf(buf, "enabled\n");
386 else if (state == 0)
387 return sprintf(buf, "disabled\n");
388 else
389 return sprintf(buf, "unknown\n");
390}
391
David Brownell4fca9542008-11-11 17:38:53 -0800392static ssize_t regulator_state_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100396 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800397
Mark Brown93325462009-08-03 18:49:56 +0100398 mutex_lock(&rdev->mutex);
399 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
400 mutex_unlock(&rdev->mutex);
401
402 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800403}
David Brownell7ad68e22008-11-11 17:39:02 -0800404static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800405
David Brownell853116a2009-01-14 23:03:17 -0800406static ssize_t regulator_status_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 struct regulator_dev *rdev = dev_get_drvdata(dev);
410 int status;
411 char *label;
412
413 status = rdev->desc->ops->get_status(rdev);
414 if (status < 0)
415 return status;
416
417 switch (status) {
418 case REGULATOR_STATUS_OFF:
419 label = "off";
420 break;
421 case REGULATOR_STATUS_ON:
422 label = "on";
423 break;
424 case REGULATOR_STATUS_ERROR:
425 label = "error";
426 break;
427 case REGULATOR_STATUS_FAST:
428 label = "fast";
429 break;
430 case REGULATOR_STATUS_NORMAL:
431 label = "normal";
432 break;
433 case REGULATOR_STATUS_IDLE:
434 label = "idle";
435 break;
436 case REGULATOR_STATUS_STANDBY:
437 label = "standby";
438 break;
439 default:
440 return -ERANGE;
441 }
442
443 return sprintf(buf, "%s\n", label);
444}
445static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
446
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447static ssize_t regulator_min_uA_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
449{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100450 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100451
452 if (!rdev->constraints)
453 return sprintf(buf, "constraint not defined\n");
454
455 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
456}
David Brownell7ad68e22008-11-11 17:39:02 -0800457static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
459static ssize_t regulator_max_uA_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100462 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
464 if (!rdev->constraints)
465 return sprintf(buf, "constraint not defined\n");
466
467 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
468}
David Brownell7ad68e22008-11-11 17:39:02 -0800469static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470
471static ssize_t regulator_min_uV_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100474 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475
476 if (!rdev->constraints)
477 return sprintf(buf, "constraint not defined\n");
478
479 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
480}
David Brownell7ad68e22008-11-11 17:39:02 -0800481static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_max_uV_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488 if (!rdev->constraints)
489 return sprintf(buf, "constraint not defined\n");
490
491 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
492}
David Brownell7ad68e22008-11-11 17:39:02 -0800493static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494
495static ssize_t regulator_total_uA_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499 struct regulator *regulator;
500 int uA = 0;
501
502 mutex_lock(&rdev->mutex);
503 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100504 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505 mutex_unlock(&rdev->mutex);
506 return sprintf(buf, "%d\n", uA);
507}
David Brownell7ad68e22008-11-11 17:39:02 -0800508static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
510static ssize_t regulator_num_users_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100513 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->use_count);
515}
516
517static ssize_t regulator_type_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
519{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100520 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522 switch (rdev->desc->type) {
523 case REGULATOR_VOLTAGE:
524 return sprintf(buf, "voltage\n");
525 case REGULATOR_CURRENT:
526 return sprintf(buf, "current\n");
527 }
528 return sprintf(buf, "unknown\n");
529}
530
531static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(suspend_mem_microvolts, 0444,
539 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
541static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100544 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_disk_microvolts, 0444,
549 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
557}
David Brownell7ad68e22008-11-11 17:39:02 -0800558static DEVICE_ATTR(suspend_standby_microvolts, 0444,
559 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100564 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565
David Brownell4fca9542008-11-11 17:38:53 -0800566 return regulator_print_opmode(buf,
567 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568}
David Brownell7ad68e22008-11-11 17:39:02 -0800569static DEVICE_ATTR(suspend_mem_mode, 0444,
570 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100575 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
David Brownell4fca9542008-11-11 17:38:53 -0800577 return regulator_print_opmode(buf,
578 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_disk_mode, 0444,
581 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
David Brownell4fca9542008-11-11 17:38:53 -0800588 return regulator_print_opmode(buf,
589 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590}
David Brownell7ad68e22008-11-11 17:39:02 -0800591static DEVICE_ATTR(suspend_standby_mode, 0444,
592 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
594static ssize_t regulator_suspend_mem_state_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100597 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598
David Brownell4fca9542008-11-11 17:38:53 -0800599 return regulator_print_state(buf,
600 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601}
David Brownell7ad68e22008-11-11 17:39:02 -0800602static DEVICE_ATTR(suspend_mem_state, 0444,
603 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
605static ssize_t regulator_suspend_disk_state_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
607{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100608 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609
David Brownell4fca9542008-11-11 17:38:53 -0800610 return regulator_print_state(buf,
611 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612}
David Brownell7ad68e22008-11-11 17:39:02 -0800613static DEVICE_ATTR(suspend_disk_state, 0444,
614 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615
616static ssize_t regulator_suspend_standby_state_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100619 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
David Brownell4fca9542008-11-11 17:38:53 -0800621 return regulator_print_state(buf,
622 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623}
David Brownell7ad68e22008-11-11 17:39:02 -0800624static DEVICE_ATTR(suspend_standby_state, 0444,
625 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100626
David Brownell7ad68e22008-11-11 17:39:02 -0800627
628/*
629 * These are the only attributes are present for all regulators.
630 * Other attributes are a function of regulator functionality.
631 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100632static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100633 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100634 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
635 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636 __ATTR_NULL,
637};
638
639static void regulator_dev_release(struct device *dev)
640{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100641 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100642 kfree(rdev);
643}
644
645static struct class regulator_class = {
646 .name = "regulator",
647 .dev_release = regulator_dev_release,
648 .dev_attrs = regulator_dev_attrs,
649};
650
651/* Calculate the new optimum regulator operating mode based on the new total
652 * consumer load. All locks held by caller */
653static void drms_uA_update(struct regulator_dev *rdev)
654{
655 struct regulator *sibling;
656 int current_uA = 0, output_uV, input_uV, err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 unsigned int regulator_curr_mode, mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100658
659 err = regulator_check_drms(rdev);
660 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000661 (!rdev->desc->ops->get_voltage &&
662 !rdev->desc->ops->get_voltage_sel) ||
663 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300664 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665
666 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000667 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 if (output_uV <= 0)
669 return;
670
671 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000672 input_uV = 0;
673 if (rdev->supply)
674 input_uV = _regulator_get_voltage(rdev);
675 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 input_uV = rdev->constraints->input_uV;
677 if (input_uV <= 0)
678 return;
679
680 /* calc total requested load */
681 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100682 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
684 /* now get the optimum mode for our new total regulator load */
685 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
686 output_uV, current_uA);
687
688 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900689 err = regulator_mode_constrain(rdev, &mode);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 /* return if the same mode is requested */
691 if (rdev->desc->ops->get_mode) {
692 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
693 if (regulator_curr_mode == mode)
694 return;
695 } else
696 return;
697
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698 if (err == 0)
699 rdev->desc->ops->set_mode(rdev, mode);
700}
701
702static int suspend_set_state(struct regulator_dev *rdev,
703 struct regulator_state *rstate)
704{
705 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100706 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707
Mark Brown638f85c2009-10-22 16:31:33 +0100708 can_set_state = rdev->desc->ops->set_suspend_enable &&
709 rdev->desc->ops->set_suspend_disable;
710
711 /* If we have no suspend mode configration don't set anything;
712 * only warn if the driver actually makes the suspend mode
713 * configurable.
714 */
715 if (!rstate->enabled && !rstate->disabled) {
716 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800717 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100718 return 0;
719 }
720
721 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800722 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100723 return -EINVAL;
724 }
725
726 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100729 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100730
731 if (rstate->enabled)
732 ret = rdev->desc->ops->set_suspend_enable(rdev);
733 else
734 ret = rdev->desc->ops->set_suspend_disable(rdev);
735 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800736 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100737 return ret;
738 }
739
740 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
741 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
742 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800743 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100744 return ret;
745 }
746 }
747
748 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
749 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
750 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800751 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100752 return ret;
753 }
754 }
755 return ret;
756}
757
758/* locks held by caller */
759static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
760{
761 if (!rdev->constraints)
762 return -EINVAL;
763
764 switch (state) {
765 case PM_SUSPEND_STANDBY:
766 return suspend_set_state(rdev,
767 &rdev->constraints->state_standby);
768 case PM_SUSPEND_MEM:
769 return suspend_set_state(rdev,
770 &rdev->constraints->state_mem);
771 case PM_SUSPEND_MAX:
772 return suspend_set_state(rdev,
773 &rdev->constraints->state_disk);
774 default:
775 return -EINVAL;
776 }
777}
778
779static void print_constraints(struct regulator_dev *rdev)
780{
781 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000782 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100783 int count = 0;
784 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100785
Mark Brown8f031b42009-10-22 16:31:31 +0100786 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100787 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100788 count += sprintf(buf + count, "%d mV ",
789 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100790 else
Mark Brown8f031b42009-10-22 16:31:31 +0100791 count += sprintf(buf + count, "%d <--> %d mV ",
792 constraints->min_uV / 1000,
793 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100794 }
Mark Brown8f031b42009-10-22 16:31:31 +0100795
796 if (!constraints->min_uV ||
797 constraints->min_uV != constraints->max_uV) {
798 ret = _regulator_get_voltage(rdev);
799 if (ret > 0)
800 count += sprintf(buf + count, "at %d mV ", ret / 1000);
801 }
802
Mark Brownbf5892a2011-05-08 22:13:37 +0100803 if (constraints->uV_offset)
804 count += sprintf(buf, "%dmV offset ",
805 constraints->uV_offset / 1000);
806
Mark Brown8f031b42009-10-22 16:31:31 +0100807 if (constraints->min_uA && constraints->max_uA) {
808 if (constraints->min_uA == constraints->max_uA)
809 count += sprintf(buf + count, "%d mA ",
810 constraints->min_uA / 1000);
811 else
812 count += sprintf(buf + count, "%d <--> %d mA ",
813 constraints->min_uA / 1000,
814 constraints->max_uA / 1000);
815 }
816
817 if (!constraints->min_uA ||
818 constraints->min_uA != constraints->max_uA) {
819 ret = _regulator_get_current_limit(rdev);
820 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400821 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100822 }
823
Liam Girdwood414c70c2008-04-30 15:59:04 +0100824 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
825 count += sprintf(buf + count, "fast ");
826 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
827 count += sprintf(buf + count, "normal ");
828 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
829 count += sprintf(buf + count, "idle ");
830 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
831 count += sprintf(buf + count, "standby");
832
Mark Brown13ce29f2010-12-17 16:04:12 +0000833 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000834
835 if ((constraints->min_uV != constraints->max_uV) &&
836 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
837 rdev_warn(rdev,
838 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100839}
840
Mark Browne79055d2009-10-19 15:53:50 +0100841static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100842 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100843{
844 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100845 int ret;
846
847 /* do we need to apply the constraint voltage */
848 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000849 rdev->constraints->min_uV == rdev->constraints->max_uV) {
850 ret = _regulator_do_set_voltage(rdev,
851 rdev->constraints->min_uV,
852 rdev->constraints->max_uV);
853 if (ret < 0) {
854 rdev_err(rdev, "failed to apply %duV constraint\n",
855 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000856 return ret;
857 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100858 }
Mark Browne79055d2009-10-19 15:53:50 +0100859
860 /* constrain machine-level voltage specs to fit
861 * the actual range supported by this regulator.
862 */
863 if (ops->list_voltage && rdev->desc->n_voltages) {
864 int count = rdev->desc->n_voltages;
865 int i;
866 int min_uV = INT_MAX;
867 int max_uV = INT_MIN;
868 int cmin = constraints->min_uV;
869 int cmax = constraints->max_uV;
870
871 /* it's safe to autoconfigure fixed-voltage supplies
872 and the constraints are used by list_voltage. */
873 if (count == 1 && !cmin) {
874 cmin = 1;
875 cmax = INT_MAX;
876 constraints->min_uV = cmin;
877 constraints->max_uV = cmax;
878 }
879
880 /* voltage constraints are optional */
881 if ((cmin == 0) && (cmax == 0))
882 return 0;
883
884 /* else require explicit machine-level constraints */
885 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800886 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100887 return -EINVAL;
888 }
889
890 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
891 for (i = 0; i < count; i++) {
892 int value;
893
894 value = ops->list_voltage(rdev, i);
895 if (value <= 0)
896 continue;
897
898 /* maybe adjust [min_uV..max_uV] */
899 if (value >= cmin && value < min_uV)
900 min_uV = value;
901 if (value <= cmax && value > max_uV)
902 max_uV = value;
903 }
904
905 /* final: [min_uV..max_uV] valid iff constraints valid */
906 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800907 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100908 return -EINVAL;
909 }
910
911 /* use regulator's subset of machine constraints */
912 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800913 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
914 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100915 constraints->min_uV = min_uV;
916 }
917 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800918 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
919 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100920 constraints->max_uV = max_uV;
921 }
922 }
923
924 return 0;
925}
926
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927/**
928 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000929 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000930 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931 *
932 * Allows platform initialisation code to define and constrain
933 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
934 * Constraints *must* be set by platform code in order for some
935 * regulator operations to proceed i.e. set_voltage, set_current_limit,
936 * set_mode.
937 */
938static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000939 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100940{
941 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100942 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100943
Mark Brown9a8f5e02011-11-29 18:11:19 +0000944 if (constraints)
945 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
946 GFP_KERNEL);
947 else
948 rdev->constraints = kzalloc(sizeof(*constraints),
949 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000950 if (!rdev->constraints)
951 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100952
Mark Brownf8c12fe2010-11-29 15:55:17 +0000953 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100954 if (ret != 0)
955 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800956
Liam Girdwooda5766f12008-10-10 13:22:20 +0100957 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000958 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000959 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100960 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800961 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100962 goto out;
963 }
964 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100965
Mark Brown9a8f5e02011-11-29 18:11:19 +0000966 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000967 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800968 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000969 ret = -EINVAL;
970 goto out;
971 }
972
Mark Brownf8c12fe2010-11-29 15:55:17 +0000973 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000974 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800975 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000976 goto out;
977 }
978 }
979
Mark Browncacf90f2009-03-02 16:32:46 +0000980 /* If the constraints say the regulator should be on at this point
981 * and we have control then make sure it is enabled.
982 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000983 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
984 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100985 ret = ops->enable(rdev);
986 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800987 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100988 goto out;
989 }
990 }
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 if (!suppress_info_printing)
993 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800994 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800996 kfree(rdev->constraints);
997 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998 return ret;
999}
1000
1001/**
1002 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001003 * @rdev: regulator name
1004 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001005 *
1006 * Called by platform initialisation code to set the supply regulator for this
1007 * regulator. This ensures that a regulators supply will also be enabled by the
1008 * core if it's child is enabled.
1009 */
1010static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001011 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001012{
1013 int err;
1014
Steve Mucklef132c6c2012-06-06 18:30:57 -07001015 if (!suppress_info_printing)
1016 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
Mark Brown3801b862011-06-09 16:22:22 +01001017
1018 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001019 if (rdev->supply == NULL) {
1020 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001021 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001022 }
Mark Brown3801b862011-06-09 16:22:22 +01001023
1024 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001025}
1026
1027/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001028 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001029 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001030 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001031 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032 *
1033 * Allows platform initialisation code to map physical regulator
1034 * sources to symbolic names for supplies for use by devices. Devices
1035 * should use these symbolic names to request regulators, avoiding the
1036 * need to provide board-specific regulator names as platform data.
1037 */
1038static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001039 const char *consumer_dev_name,
1040 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041{
1042 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001043 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001044
1045 if (supply == NULL)
1046 return -EINVAL;
1047
Mark Brown9ed20992009-07-21 16:00:26 +01001048 if (consumer_dev_name != NULL)
1049 has_dev = 1;
1050 else
1051 has_dev = 0;
1052
David Brownell6001e132008-12-31 12:54:19 +00001053 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001054 if (node->dev_name && consumer_dev_name) {
1055 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1056 continue;
1057 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001058 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001059 }
1060
David Brownell6001e132008-12-31 12:54:19 +00001061 if (strcmp(node->supply, supply) != 0)
1062 continue;
1063
Mark Brown737f3602012-02-02 00:10:51 +00001064 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1065 consumer_dev_name,
1066 dev_name(&node->regulator->dev),
1067 node->regulator->desc->name,
1068 supply,
1069 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001070 return -EBUSY;
1071 }
1072
Mark Brown9ed20992009-07-21 16:00:26 +01001073 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001074 if (node == NULL)
1075 return -ENOMEM;
1076
1077 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001078 node->supply = supply;
1079
Mark Brown9ed20992009-07-21 16:00:26 +01001080 if (has_dev) {
1081 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1082 if (node->dev_name == NULL) {
1083 kfree(node);
1084 return -ENOMEM;
1085 }
Mark Brown40f92442009-06-17 17:56:39 +01001086 }
1087
Liam Girdwooda5766f12008-10-10 13:22:20 +01001088 list_add(&node->list, &regulator_map_list);
1089 return 0;
1090}
1091
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001092static void unset_regulator_supplies(struct regulator_dev *rdev)
1093{
1094 struct regulator_map *node, *n;
1095
1096 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1097 if (rdev == node->regulator) {
1098 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001099 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001100 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001101 }
1102 }
1103}
1104
Mark Brownf5726ae2011-06-09 16:22:20 +01001105#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106
1107static struct regulator *create_regulator(struct regulator_dev *rdev,
1108 struct device *dev,
1109 const char *supply_name)
1110{
1111 struct regulator *regulator;
1112 char buf[REG_STR_SIZE];
1113 int err, size;
1114
1115 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1116 if (regulator == NULL)
1117 return NULL;
1118
1119 mutex_lock(&rdev->mutex);
1120 regulator->rdev = rdev;
1121 list_add(&regulator->list, &rdev->consumer_list);
1122
1123 if (dev) {
1124 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001125 size = scnprintf(buf, REG_STR_SIZE,
1126 "microamps_requested_%s-%s",
1127 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128 if (size >= REG_STR_SIZE)
1129 goto overflow_err;
1130
1131 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001132 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1134 if (regulator->dev_attr.attr.name == NULL)
1135 goto attr_name_err;
1136
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 regulator->dev_attr.attr.mode = 0444;
1138 regulator->dev_attr.show = device_requested_uA_show;
1139 err = device_create_file(dev, &regulator->dev_attr);
1140 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001141 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001142 goto attr_name_err;
1143 }
1144
1145 /* also add a link to the device sysfs entry */
1146 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1147 dev->kobj.name, supply_name);
1148 if (size >= REG_STR_SIZE)
1149 goto attr_err;
1150
1151 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1152 if (regulator->supply_name == NULL)
1153 goto attr_err;
1154
1155 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1156 buf);
1157 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001158 rdev_warn(rdev, "could not add device link %s err %d\n",
1159 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160 goto link_name_err;
1161 }
Mark Brown5de70512011-06-19 13:33:16 +01001162 } else {
1163 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1164 if (regulator->supply_name == NULL)
1165 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001166 }
Mark Brown5de70512011-06-19 13:33:16 +01001167
Mark Brown5de70512011-06-19 13:33:16 +01001168 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1169 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001170 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001171 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001172 } else {
1173 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1174 &regulator->uA_load);
1175 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1176 &regulator->min_uV);
1177 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1178 &regulator->max_uV);
1179 }
Mark Brown5de70512011-06-19 13:33:16 +01001180
Liam Girdwood414c70c2008-04-30 15:59:04 +01001181 mutex_unlock(&rdev->mutex);
1182 return regulator;
1183link_name_err:
1184 kfree(regulator->supply_name);
1185attr_err:
1186 device_remove_file(regulator->dev, &regulator->dev_attr);
1187attr_name_err:
1188 kfree(regulator->dev_attr.attr.name);
1189overflow_err:
1190 list_del(&regulator->list);
1191 kfree(regulator);
1192 mutex_unlock(&rdev->mutex);
1193 return NULL;
1194}
1195
Mark Brown31aae2b2009-12-21 12:21:52 +00001196static int _regulator_get_enable_time(struct regulator_dev *rdev)
1197{
1198 if (!rdev->desc->ops->enable_time)
1199 return 0;
1200 return rdev->desc->ops->enable_time(rdev);
1201}
1202
Rajendra Nayak69511a42011-11-18 16:47:20 +05301203static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1204 const char *supply)
1205{
1206 struct regulator_dev *r;
1207 struct device_node *node;
1208
1209 /* first do a dt based lookup */
1210 if (dev && dev->of_node) {
1211 node = of_get_regulator(dev, supply);
1212 if (node)
1213 list_for_each_entry(r, &regulator_list, list)
1214 if (r->dev.parent &&
1215 node == r->dev.of_node)
1216 return r;
1217 }
1218
1219 /* if not found, try doing it non-dt way */
1220 list_for_each_entry(r, &regulator_list, list)
1221 if (strcmp(rdev_get_name(r), supply) == 0)
1222 return r;
1223
1224 return NULL;
1225}
1226
Mark Brown5ffbd132009-07-21 16:00:23 +01001227/* Internal regulator request function */
1228static struct regulator *_regulator_get(struct device *dev, const char *id,
1229 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230{
1231 struct regulator_dev *rdev;
1232 struct regulator_map *map;
Mark Brown04bf3012012-03-11 13:07:56 +00001233 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001234 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001235 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236
1237 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001238 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 return regulator;
1240 }
1241
Mark Brown40f92442009-06-17 17:56:39 +01001242 if (dev)
1243 devname = dev_name(dev);
1244
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245 mutex_lock(&regulator_list_mutex);
1246
Rajendra Nayak69511a42011-11-18 16:47:20 +05301247 rdev = regulator_dev_lookup(dev, id);
1248 if (rdev)
1249 goto found;
1250
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001252 /* If the mapping has a device set up it must match */
1253 if (map->dev_name &&
1254 (!devname || strcmp(map->dev_name, devname)))
1255 continue;
1256
1257 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001258 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001260 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001261 }
Mark Brown34abbd62010-02-12 10:18:08 +00001262
Mark Brown688fe992010-10-05 19:18:32 -07001263 if (board_wants_dummy_regulator) {
1264 rdev = dummy_regulator_rdev;
1265 goto found;
1266 }
1267
Mark Brown34abbd62010-02-12 10:18:08 +00001268#ifdef CONFIG_REGULATOR_DUMMY
1269 if (!devname)
1270 devname = "deviceless";
1271
1272 /* If the board didn't flag that it was fully constrained then
1273 * substitute in a dummy regulator so consumers can continue.
1274 */
1275 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001276 pr_warn("%s supply %s not found, using dummy regulator\n",
1277 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001278 rdev = dummy_regulator_rdev;
1279 goto found;
1280 }
1281#endif
1282
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283 mutex_unlock(&regulator_list_mutex);
1284 return regulator;
1285
1286found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001287 if (rdev->exclusive) {
1288 regulator = ERR_PTR(-EPERM);
1289 goto out;
1290 }
1291
1292 if (exclusive && rdev->open_count) {
1293 regulator = ERR_PTR(-EBUSY);
1294 goto out;
1295 }
1296
Liam Girdwooda5766f12008-10-10 13:22:20 +01001297 if (!try_module_get(rdev->owner))
1298 goto out;
1299
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300 regulator = create_regulator(rdev, dev, id);
1301 if (regulator == NULL) {
1302 regulator = ERR_PTR(-ENOMEM);
1303 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001304 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001305 }
1306
Mark Brown5ffbd132009-07-21 16:00:23 +01001307 rdev->open_count++;
1308 if (exclusive) {
1309 rdev->exclusive = 1;
1310
1311 ret = _regulator_is_enabled(rdev);
1312 if (ret > 0)
1313 rdev->use_count = 1;
1314 else
1315 rdev->use_count = 0;
1316 }
1317
Liam Girdwooda5766f12008-10-10 13:22:20 +01001318out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001319 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001320
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321 return regulator;
1322}
Mark Brown5ffbd132009-07-21 16:00:23 +01001323
1324/**
1325 * regulator_get - lookup and obtain a reference to a regulator.
1326 * @dev: device for regulator "consumer"
1327 * @id: Supply name or regulator ID.
1328 *
1329 * Returns a struct regulator corresponding to the regulator producer,
1330 * or IS_ERR() condition containing errno.
1331 *
1332 * Use of supply names configured via regulator_set_device_supply() is
1333 * strongly encouraged. It is recommended that the supply name used
1334 * should match the name used for the supply and/or the relevant
1335 * device pins in the datasheet.
1336 */
1337struct regulator *regulator_get(struct device *dev, const char *id)
1338{
1339 return _regulator_get(dev, id, 0);
1340}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341EXPORT_SYMBOL_GPL(regulator_get);
1342
Stephen Boyd070b9072012-01-16 19:39:58 -08001343static void devm_regulator_release(struct device *dev, void *res)
1344{
1345 regulator_put(*(struct regulator **)res);
1346}
1347
1348/**
1349 * devm_regulator_get - Resource managed regulator_get()
1350 * @dev: device for regulator "consumer"
1351 * @id: Supply name or regulator ID.
1352 *
1353 * Managed regulator_get(). Regulators returned from this function are
1354 * automatically regulator_put() on driver detach. See regulator_get() for more
1355 * information.
1356 */
1357struct regulator *devm_regulator_get(struct device *dev, const char *id)
1358{
1359 struct regulator **ptr, *regulator;
1360
1361 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1362 if (!ptr)
1363 return ERR_PTR(-ENOMEM);
1364
1365 regulator = regulator_get(dev, id);
1366 if (!IS_ERR(regulator)) {
1367 *ptr = regulator;
1368 devres_add(dev, ptr);
1369 } else {
1370 devres_free(ptr);
1371 }
1372
1373 return regulator;
1374}
1375EXPORT_SYMBOL_GPL(devm_regulator_get);
1376
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001378 * regulator_get_exclusive - obtain exclusive access to a regulator.
1379 * @dev: device for regulator "consumer"
1380 * @id: Supply name or regulator ID.
1381 *
1382 * Returns a struct regulator corresponding to the regulator producer,
1383 * or IS_ERR() condition containing errno. Other consumers will be
1384 * unable to obtain this reference is held and the use count for the
1385 * regulator will be initialised to reflect the current state of the
1386 * regulator.
1387 *
1388 * This is intended for use by consumers which cannot tolerate shared
1389 * use of the regulator such as those which need to force the
1390 * regulator off for correct operation of the hardware they are
1391 * controlling.
1392 *
1393 * Use of supply names configured via regulator_set_device_supply() is
1394 * strongly encouraged. It is recommended that the supply name used
1395 * should match the name used for the supply and/or the relevant
1396 * device pins in the datasheet.
1397 */
1398struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1399{
1400 return _regulator_get(dev, id, 1);
1401}
1402EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1403
1404/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 * regulator_put - "free" the regulator source
1406 * @regulator: regulator source
1407 *
1408 * Note: drivers must ensure that all regulator_enable calls made on this
1409 * regulator source are balanced by regulator_disable calls prior to calling
1410 * this function.
1411 */
1412void regulator_put(struct regulator *regulator)
1413{
1414 struct regulator_dev *rdev;
1415
1416 if (regulator == NULL || IS_ERR(regulator))
1417 return;
1418
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 mutex_lock(&regulator_list_mutex);
1420 rdev = regulator->rdev;
1421
Mark Brown5de70512011-06-19 13:33:16 +01001422 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001423
Liam Girdwood414c70c2008-04-30 15:59:04 +01001424 /* remove any sysfs entries */
1425 if (regulator->dev) {
1426 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001427 device_remove_file(regulator->dev, &regulator->dev_attr);
1428 kfree(regulator->dev_attr.attr.name);
1429 }
Mark Brown5de70512011-06-19 13:33:16 +01001430 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 list_del(&regulator->list);
1432 kfree(regulator);
1433
Mark Brown5ffbd132009-07-21 16:00:23 +01001434 rdev->open_count--;
1435 rdev->exclusive = 0;
1436
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 module_put(rdev->owner);
1438 mutex_unlock(&regulator_list_mutex);
1439}
1440EXPORT_SYMBOL_GPL(regulator_put);
1441
Mark Brownd5ad34f2012-01-20 20:09:18 +00001442static int devm_regulator_match(struct device *dev, void *res, void *data)
1443{
1444 struct regulator **r = res;
1445 if (!r || !*r) {
1446 WARN_ON(!r || !*r);
1447 return 0;
1448 }
1449 return *r == data;
1450}
1451
1452/**
1453 * devm_regulator_put - Resource managed regulator_put()
1454 * @regulator: regulator to free
1455 *
1456 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1457 * this function will not need to be called and the resource management
1458 * code will ensure that the resource is freed.
1459 */
1460void devm_regulator_put(struct regulator *regulator)
1461{
1462 int rc;
1463
1464 rc = devres_destroy(regulator->dev, devm_regulator_release,
1465 devm_regulator_match, regulator);
Mark Brown968c2c12012-05-07 11:34:52 +01001466 if (rc == 0)
1467 regulator_put(regulator);
1468 else
1469 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001470}
1471EXPORT_SYMBOL_GPL(devm_regulator_put);
1472
Mark Brown9a2372f2009-08-03 18:49:57 +01001473static int _regulator_can_change_status(struct regulator_dev *rdev)
1474{
1475 if (!rdev->constraints)
1476 return 0;
1477
1478 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1479 return 1;
1480 else
1481 return 0;
1482}
1483
Liam Girdwood414c70c2008-04-30 15:59:04 +01001484/* locks held by regulator_enable() */
1485static int _regulator_enable(struct regulator_dev *rdev)
1486{
Mark Brown31aae2b2009-12-21 12:21:52 +00001487 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488
Liam Girdwood414c70c2008-04-30 15:59:04 +01001489 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001490 if (rdev->constraints &&
1491 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1492 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493
Mark Brown9a2372f2009-08-03 18:49:57 +01001494 if (rdev->use_count == 0) {
1495 /* The regulator may on if it's not switchable or left on */
1496 ret = _regulator_is_enabled(rdev);
1497 if (ret == -EINVAL || ret == 0) {
1498 if (!_regulator_can_change_status(rdev))
1499 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500
Mark Brown31aae2b2009-12-21 12:21:52 +00001501 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001502 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001503
1504 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001505 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001506 ret = _regulator_get_enable_time(rdev);
1507 if (ret >= 0) {
1508 delay = ret;
1509 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001510 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001511 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001512 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001513 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001514
Mark Brown02fa3ec2010-11-10 14:38:30 +00001515 trace_regulator_enable(rdev_get_name(rdev));
1516
Mark Brown31aae2b2009-12-21 12:21:52 +00001517 /* Allow the regulator to ramp; it would be useful
1518 * to extend this for bulk operations so that the
1519 * regulators can ramp together. */
1520 ret = rdev->desc->ops->enable(rdev);
1521 if (ret < 0)
1522 return ret;
1523
Mark Brown02fa3ec2010-11-10 14:38:30 +00001524 trace_regulator_enable_delay(rdev_get_name(rdev));
1525
Axel Line36c1df2010-11-05 21:51:32 +08001526 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001527 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001528 udelay(delay % 1000);
1529 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001530 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001531 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001532
Mark Brown02fa3ec2010-11-10 14:38:30 +00001533 trace_regulator_enable_complete(rdev_get_name(rdev));
1534
Linus Walleija7433cf2009-08-26 12:54:04 +02001535 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001536 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001537 return ret;
1538 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001539 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540 }
1541
Mark Brown9a2372f2009-08-03 18:49:57 +01001542 rdev->use_count++;
1543
1544 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001545}
1546
1547/**
1548 * regulator_enable - enable regulator output
1549 * @regulator: regulator source
1550 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001551 * Request that the regulator be enabled with the regulator output at
1552 * the predefined voltage or current value. Calls to regulator_enable()
1553 * must be balanced with calls to regulator_disable().
1554 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001556 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001557 */
1558int regulator_enable(struct regulator *regulator)
1559{
David Brownell412aec62008-11-16 11:44:46 -08001560 struct regulator_dev *rdev = regulator->rdev;
1561 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001562
Mark Brown3801b862011-06-09 16:22:22 +01001563 if (rdev->supply) {
1564 ret = regulator_enable(rdev->supply);
1565 if (ret != 0)
1566 return ret;
1567 }
1568
David Brownell412aec62008-11-16 11:44:46 -08001569 mutex_lock(&rdev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001570
David Brownellcd94b502009-03-11 16:43:34 -08001571 ret = _regulator_enable(rdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001572 if (ret == 0)
1573 regulator->enabled++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574
David Brownell412aec62008-11-16 11:44:46 -08001575 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001576
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001577 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001578 regulator_disable(rdev->supply);
1579
Liam Girdwood414c70c2008-04-30 15:59:04 +01001580 return ret;
1581}
1582EXPORT_SYMBOL_GPL(regulator_enable);
1583
1584/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001585static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586{
1587 int ret = 0;
1588
David Brownellcd94b502009-03-11 16:43:34 -08001589 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001590 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001591 return -EIO;
1592
Liam Girdwood414c70c2008-04-30 15:59:04 +01001593 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001594 if (rdev->use_count == 1 &&
1595 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596
1597 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001598 if (_regulator_can_change_status(rdev) &&
1599 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001600 trace_regulator_disable(rdev_get_name(rdev));
1601
Liam Girdwood414c70c2008-04-30 15:59:04 +01001602 ret = rdev->desc->ops->disable(rdev);
1603 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001604 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 return ret;
1606 }
Mark Brown84b68262009-12-01 21:12:27 +00001607
Mark Brown02fa3ec2010-11-10 14:38:30 +00001608 trace_regulator_disable_complete(rdev_get_name(rdev));
1609
Mark Brown84b68262009-12-01 21:12:27 +00001610 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1611 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001612 }
1613
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 rdev->use_count = 0;
1615 } else if (rdev->use_count > 1) {
1616
1617 if (rdev->constraints &&
1618 (rdev->constraints->valid_ops_mask &
1619 REGULATOR_CHANGE_DRMS))
1620 drms_uA_update(rdev);
1621
1622 rdev->use_count--;
1623 }
Mark Brown3801b862011-06-09 16:22:22 +01001624
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625 return ret;
1626}
1627
1628/**
1629 * regulator_disable - disable regulator output
1630 * @regulator: regulator source
1631 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001632 * Disable the regulator output voltage or current. Calls to
1633 * regulator_enable() must be balanced with calls to
1634 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001635 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001636 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001637 * devices have it enabled, the regulator device supports disabling and
1638 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001639 */
1640int regulator_disable(struct regulator *regulator)
1641{
David Brownell412aec62008-11-16 11:44:46 -08001642 struct regulator_dev *rdev = regulator->rdev;
1643 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001644
David Brownell412aec62008-11-16 11:44:46 -08001645 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001646 ret = _regulator_disable(rdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001647 if (ret == 0)
1648 regulator->enabled--;
David Brownell412aec62008-11-16 11:44:46 -08001649 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001650
Mark Brown3801b862011-06-09 16:22:22 +01001651 if (ret == 0 && rdev->supply)
1652 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001653
Liam Girdwood414c70c2008-04-30 15:59:04 +01001654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_disable);
1657
1658/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001659static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001660{
1661 int ret = 0;
1662
1663 /* force disable */
1664 if (rdev->desc->ops->disable) {
1665 /* ah well, who wants to live forever... */
1666 ret = rdev->desc->ops->disable(rdev);
1667 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001668 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669 return ret;
1670 }
1671 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001672 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1673 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 }
1675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676 return ret;
1677}
1678
1679/**
1680 * regulator_force_disable - force disable regulator output
1681 * @regulator: regulator source
1682 *
1683 * Forcibly disable the regulator output voltage or current.
1684 * NOTE: this *will* disable the regulator output even if other consumer
1685 * devices have it enabled. This should be used for situations when device
1686 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1687 */
1688int regulator_force_disable(struct regulator *regulator)
1689{
Mark Brown82d15832011-05-09 11:41:02 +02001690 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001691 int ret;
1692
Mark Brown82d15832011-05-09 11:41:02 +02001693 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001694 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001695 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001696 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001697
Mark Brown3801b862011-06-09 16:22:22 +01001698 if (rdev->supply)
1699 while (rdev->open_count--)
1700 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return ret;
1703}
1704EXPORT_SYMBOL_GPL(regulator_force_disable);
1705
Mark Brownda07ecd2011-09-11 09:53:50 +01001706static void regulator_disable_work(struct work_struct *work)
1707{
1708 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1709 disable_work.work);
1710 int count, i, ret;
1711
1712 mutex_lock(&rdev->mutex);
1713
1714 BUG_ON(!rdev->deferred_disables);
1715
1716 count = rdev->deferred_disables;
1717 rdev->deferred_disables = 0;
1718
1719 for (i = 0; i < count; i++) {
1720 ret = _regulator_disable(rdev);
1721 if (ret != 0)
1722 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1723 }
1724
1725 mutex_unlock(&rdev->mutex);
1726
1727 if (rdev->supply) {
1728 for (i = 0; i < count; i++) {
1729 ret = regulator_disable(rdev->supply);
1730 if (ret != 0) {
1731 rdev_err(rdev,
1732 "Supply disable failed: %d\n", ret);
1733 }
1734 }
1735 }
1736}
1737
1738/**
1739 * regulator_disable_deferred - disable regulator output with delay
1740 * @regulator: regulator source
1741 * @ms: miliseconds until the regulator is disabled
1742 *
1743 * Execute regulator_disable() on the regulator after a delay. This
1744 * is intended for use with devices that require some time to quiesce.
1745 *
1746 * NOTE: this will only disable the regulator output if no other consumer
1747 * devices have it enabled, the regulator device supports disabling and
1748 * machine constraints permit this operation.
1749 */
1750int regulator_disable_deferred(struct regulator *regulator, int ms)
1751{
1752 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001753 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001754
1755 mutex_lock(&rdev->mutex);
1756 rdev->deferred_disables++;
1757 mutex_unlock(&rdev->mutex);
1758
Mark Brownaa598022011-10-03 22:42:43 +01001759 ret = schedule_delayed_work(&rdev->disable_work,
1760 msecs_to_jiffies(ms));
1761 if (ret < 0)
1762 return ret;
1763 else
1764 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001765}
1766EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1767
Liam Girdwood414c70c2008-04-30 15:59:04 +01001768static int _regulator_is_enabled(struct regulator_dev *rdev)
1769{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001770 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001771 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001772 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001773
Mark Brown93325462009-08-03 18:49:56 +01001774 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775}
1776
1777/**
1778 * regulator_is_enabled - is the regulator output enabled
1779 * @regulator: regulator source
1780 *
David Brownell412aec62008-11-16 11:44:46 -08001781 * Returns positive if the regulator driver backing the source/client
1782 * has requested that the device be enabled, zero if it hasn't, else a
1783 * negative errno code.
1784 *
1785 * Note that the device backing this regulator handle can have multiple
1786 * users, so it might be enabled even if regulator_enable() was never
1787 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001788 */
1789int regulator_is_enabled(struct regulator *regulator)
1790{
Mark Brown93325462009-08-03 18:49:56 +01001791 int ret;
1792
1793 mutex_lock(&regulator->rdev->mutex);
1794 ret = _regulator_is_enabled(regulator->rdev);
1795 mutex_unlock(&regulator->rdev->mutex);
1796
1797 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001798}
1799EXPORT_SYMBOL_GPL(regulator_is_enabled);
1800
1801/**
David Brownell4367cfd2009-02-26 11:48:36 -08001802 * regulator_count_voltages - count regulator_list_voltage() selectors
1803 * @regulator: regulator source
1804 *
1805 * Returns number of selectors, or negative errno. Selectors are
1806 * numbered starting at zero, and typically correspond to bitfields
1807 * in hardware registers.
1808 */
1809int regulator_count_voltages(struct regulator *regulator)
1810{
1811 struct regulator_dev *rdev = regulator->rdev;
1812
1813 return rdev->desc->n_voltages ? : -EINVAL;
1814}
1815EXPORT_SYMBOL_GPL(regulator_count_voltages);
1816
1817/**
1818 * regulator_list_voltage - enumerate supported voltages
1819 * @regulator: regulator source
1820 * @selector: identify voltage to list
1821 * Context: can sleep
1822 *
1823 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001824 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001825 * negative errno.
1826 */
1827int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1828{
1829 struct regulator_dev *rdev = regulator->rdev;
1830 struct regulator_ops *ops = rdev->desc->ops;
1831 int ret;
1832
1833 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1834 return -EINVAL;
1835
1836 mutex_lock(&rdev->mutex);
1837 ret = ops->list_voltage(rdev, selector);
1838 mutex_unlock(&rdev->mutex);
1839
1840 if (ret > 0) {
1841 if (ret < rdev->constraints->min_uV)
1842 ret = 0;
1843 else if (ret > rdev->constraints->max_uV)
1844 ret = 0;
1845 }
1846
1847 return ret;
1848}
1849EXPORT_SYMBOL_GPL(regulator_list_voltage);
1850
1851/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001852 * regulator_is_supported_voltage - check if a voltage range can be supported
1853 *
1854 * @regulator: Regulator to check.
1855 * @min_uV: Minimum required voltage in uV.
1856 * @max_uV: Maximum required voltage in uV.
1857 *
1858 * Returns a boolean or a negative error code.
1859 */
1860int regulator_is_supported_voltage(struct regulator *regulator,
1861 int min_uV, int max_uV)
1862{
1863 int i, voltages, ret;
1864
1865 ret = regulator_count_voltages(regulator);
1866 if (ret < 0)
1867 return ret;
1868 voltages = ret;
1869
1870 for (i = 0; i < voltages; i++) {
1871 ret = regulator_list_voltage(regulator, i);
1872
1873 if (ret >= min_uV && ret <= max_uV)
1874 return 1;
1875 }
1876
1877 return 0;
1878}
Mark Browna398eaa2011-12-28 12:48:45 +00001879EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001880
Mark Brown75790252010-12-12 14:25:50 +00001881static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1882 int min_uV, int max_uV)
1883{
1884 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001885 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001886 unsigned int selector;
1887
1888 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1889
Mark Brownbf5892a2011-05-08 22:13:37 +01001890 min_uV += rdev->constraints->uV_offset;
1891 max_uV += rdev->constraints->uV_offset;
1892
Mark Brown75790252010-12-12 14:25:50 +00001893 if (rdev->desc->ops->set_voltage) {
1894 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1895 &selector);
1896
1897 if (rdev->desc->ops->list_voltage)
1898 selector = rdev->desc->ops->list_voltage(rdev,
1899 selector);
David Collinsf6acc6c2012-11-13 16:14:53 -08001900 else if (rdev->desc->ops->get_voltage)
1901 selector = rdev->desc->ops->get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00001902 else
1903 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001904 } else if (rdev->desc->ops->set_voltage_sel) {
1905 int best_val = INT_MAX;
1906 int i;
1907
1908 selector = 0;
1909
1910 /* Find the smallest voltage that falls within the specified
1911 * range.
1912 */
1913 for (i = 0; i < rdev->desc->n_voltages; i++) {
1914 ret = rdev->desc->ops->list_voltage(rdev, i);
1915 if (ret < 0)
1916 continue;
1917
1918 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1919 best_val = ret;
1920 selector = i;
1921 }
1922 }
1923
Linus Walleij77af1b22011-03-17 13:24:36 +01001924 /*
1925 * If we can't obtain the old selector there is not enough
1926 * info to call set_voltage_time_sel().
1927 */
1928 if (rdev->desc->ops->set_voltage_time_sel &&
1929 rdev->desc->ops->get_voltage_sel) {
1930 unsigned int old_selector = 0;
1931
1932 ret = rdev->desc->ops->get_voltage_sel(rdev);
1933 if (ret < 0)
1934 return ret;
1935 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001936 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b22011-03-17 13:24:36 +01001937 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001938 if (ret < 0)
1939 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1940 else
1941 delay = ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001942 }
1943
Mark Browne8eef822010-12-12 14:36:17 +00001944 if (best_val != INT_MAX) {
1945 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1946 selector = best_val;
1947 } else {
1948 ret = -EINVAL;
1949 }
Mark Brown75790252010-12-12 14:25:50 +00001950 } else {
1951 ret = -EINVAL;
1952 }
1953
Linus Walleij77af1b22011-03-17 13:24:36 +01001954 /* Insert any necessary delays */
1955 if (delay >= 1000) {
1956 mdelay(delay / 1000);
1957 udelay(delay % 1000);
1958 } else if (delay) {
1959 udelay(delay);
1960 }
1961
Mark Brownded06a52010-12-16 13:59:10 +00001962 if (ret == 0)
1963 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1964 NULL);
1965
Mark Brown75790252010-12-12 14:25:50 +00001966 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1967
1968 return ret;
1969}
1970
Mark Browna7a1ad92009-07-21 16:00:24 +01001971/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001972 * regulator_set_voltage - set regulator output voltage
1973 * @regulator: regulator source
1974 * @min_uV: Minimum required voltage in uV
1975 * @max_uV: Maximum acceptable voltage in uV
1976 *
1977 * Sets a voltage regulator to the desired output voltage. This can be set
1978 * during any regulator state. IOW, regulator can be disabled or enabled.
1979 *
1980 * If the regulator is enabled then the voltage will change to the new value
1981 * immediately otherwise if the regulator is disabled the regulator will
1982 * output at the new voltage when enabled.
1983 *
1984 * NOTE: If the regulator is shared between several devices then the lowest
1985 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001986 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001987 * calling this function otherwise this call will fail.
1988 */
1989int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1990{
1991 struct regulator_dev *rdev = regulator->rdev;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001992 int prev_min_uV, prev_max_uV;
Mark Brown95a3c232010-12-16 15:49:37 +00001993 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001994
1995 mutex_lock(&rdev->mutex);
1996
Mark Brown95a3c232010-12-16 15:49:37 +00001997 /* If we're setting the same range as last time the change
1998 * should be a noop (some cpufreq implementations use the same
1999 * voltage for multiple frequencies, for example).
2000 */
2001 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2002 goto out;
2003
Liam Girdwood414c70c2008-04-30 15:59:04 +01002004 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002005 if (!rdev->desc->ops->set_voltage &&
2006 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002007 ret = -EINVAL;
2008 goto out;
2009 }
2010
2011 /* constraints check */
2012 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2013 if (ret < 0)
2014 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015
Steve Mucklef132c6c2012-06-06 18:30:57 -07002016 prev_min_uV = regulator->min_uV;
2017 prev_max_uV = regulator->max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002018
Liam Girdwood414c70c2008-04-30 15:59:04 +01002019 regulator->min_uV = min_uV;
2020 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002021
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002022 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002023 if (ret < 0) {
2024 regulator->min_uV = prev_min_uV;
2025 regulator->max_uV = prev_max_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002026 goto out;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002027 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002028
Mark Brown75790252010-12-12 14:25:50 +00002029 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002030
Liam Girdwood414c70c2008-04-30 15:59:04 +01002031out:
2032 mutex_unlock(&rdev->mutex);
2033 return ret;
2034}
2035EXPORT_SYMBOL_GPL(regulator_set_voltage);
2036
Mark Brown606a2562010-12-16 15:49:36 +00002037/**
Linus Walleij88cd2222011-03-17 13:24:52 +01002038 * regulator_set_voltage_time - get raise/fall time
2039 * @regulator: regulator source
2040 * @old_uV: starting voltage in microvolts
2041 * @new_uV: target voltage in microvolts
2042 *
2043 * Provided with the starting and ending voltage, this function attempts to
2044 * calculate the time in microseconds required to rise or fall to this new
2045 * voltage.
2046 */
2047int regulator_set_voltage_time(struct regulator *regulator,
2048 int old_uV, int new_uV)
2049{
2050 struct regulator_dev *rdev = regulator->rdev;
2051 struct regulator_ops *ops = rdev->desc->ops;
2052 int old_sel = -1;
2053 int new_sel = -1;
2054 int voltage;
2055 int i;
2056
2057 /* Currently requires operations to do this */
2058 if (!ops->list_voltage || !ops->set_voltage_time_sel
2059 || !rdev->desc->n_voltages)
2060 return -EINVAL;
2061
2062 for (i = 0; i < rdev->desc->n_voltages; i++) {
2063 /* We only look for exact voltage matches here */
2064 voltage = regulator_list_voltage(regulator, i);
2065 if (voltage < 0)
2066 return -EINVAL;
2067 if (voltage == 0)
2068 continue;
2069 if (voltage == old_uV)
2070 old_sel = i;
2071 if (voltage == new_uV)
2072 new_sel = i;
2073 }
2074
2075 if (old_sel < 0 || new_sel < 0)
2076 return -EINVAL;
2077
2078 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2079}
2080EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2081
2082/**
Mark Brown606a2562010-12-16 15:49:36 +00002083 * regulator_sync_voltage - re-apply last regulator output voltage
2084 * @regulator: regulator source
2085 *
2086 * Re-apply the last configured voltage. This is intended to be used
2087 * where some external control source the consumer is cooperating with
2088 * has caused the configured voltage to change.
2089 */
2090int regulator_sync_voltage(struct regulator *regulator)
2091{
2092 struct regulator_dev *rdev = regulator->rdev;
2093 int ret, min_uV, max_uV;
2094
2095 mutex_lock(&rdev->mutex);
2096
2097 if (!rdev->desc->ops->set_voltage &&
2098 !rdev->desc->ops->set_voltage_sel) {
2099 ret = -EINVAL;
2100 goto out;
2101 }
2102
2103 /* This is only going to work if we've had a voltage configured. */
2104 if (!regulator->min_uV && !regulator->max_uV) {
2105 ret = -EINVAL;
2106 goto out;
2107 }
2108
2109 min_uV = regulator->min_uV;
2110 max_uV = regulator->max_uV;
2111
2112 /* This should be a paranoia check... */
2113 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2114 if (ret < 0)
2115 goto out;
2116
2117 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2118 if (ret < 0)
2119 goto out;
2120
2121 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2122
2123out:
2124 mutex_unlock(&rdev->mutex);
2125 return ret;
2126}
2127EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2128
Liam Girdwood414c70c2008-04-30 15:59:04 +01002129static int _regulator_get_voltage(struct regulator_dev *rdev)
2130{
Mark Brownbf5892a2011-05-08 22:13:37 +01002131 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002132
2133 if (rdev->desc->ops->get_voltage_sel) {
2134 sel = rdev->desc->ops->get_voltage_sel(rdev);
2135 if (sel < 0)
2136 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002137 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002138 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002139 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002140 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002141 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002142 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002143
Axel Lincb220d12011-05-23 20:08:10 +08002144 if (ret < 0)
2145 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002146 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147}
2148
2149/**
2150 * regulator_get_voltage - get regulator output voltage
2151 * @regulator: regulator source
2152 *
2153 * This returns the current regulator voltage in uV.
2154 *
2155 * NOTE: If the regulator is disabled it will return the voltage value. This
2156 * function should not be used to determine regulator state.
2157 */
2158int regulator_get_voltage(struct regulator *regulator)
2159{
2160 int ret;
2161
2162 mutex_lock(&regulator->rdev->mutex);
2163
2164 ret = _regulator_get_voltage(regulator->rdev);
2165
2166 mutex_unlock(&regulator->rdev->mutex);
2167
2168 return ret;
2169}
2170EXPORT_SYMBOL_GPL(regulator_get_voltage);
2171
2172/**
2173 * regulator_set_current_limit - set regulator output current limit
2174 * @regulator: regulator source
2175 * @min_uA: Minimuum supported current in uA
2176 * @max_uA: Maximum supported current in uA
2177 *
2178 * Sets current sink to the desired output current. This can be set during
2179 * any regulator state. IOW, regulator can be disabled or enabled.
2180 *
2181 * If the regulator is enabled then the current will change to the new value
2182 * immediately otherwise if the regulator is disabled the regulator will
2183 * output at the new current when enabled.
2184 *
2185 * NOTE: Regulator system constraints must be set for this regulator before
2186 * calling this function otherwise this call will fail.
2187 */
2188int regulator_set_current_limit(struct regulator *regulator,
2189 int min_uA, int max_uA)
2190{
2191 struct regulator_dev *rdev = regulator->rdev;
2192 int ret;
2193
2194 mutex_lock(&rdev->mutex);
2195
2196 /* sanity check */
2197 if (!rdev->desc->ops->set_current_limit) {
2198 ret = -EINVAL;
2199 goto out;
2200 }
2201
2202 /* constraints check */
2203 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2204 if (ret < 0)
2205 goto out;
2206
2207 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2208out:
2209 mutex_unlock(&rdev->mutex);
2210 return ret;
2211}
2212EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2213
2214static int _regulator_get_current_limit(struct regulator_dev *rdev)
2215{
2216 int ret;
2217
2218 mutex_lock(&rdev->mutex);
2219
2220 /* sanity check */
2221 if (!rdev->desc->ops->get_current_limit) {
2222 ret = -EINVAL;
2223 goto out;
2224 }
2225
2226 ret = rdev->desc->ops->get_current_limit(rdev);
2227out:
2228 mutex_unlock(&rdev->mutex);
2229 return ret;
2230}
2231
2232/**
2233 * regulator_get_current_limit - get regulator output current
2234 * @regulator: regulator source
2235 *
2236 * This returns the current supplied by the specified current sink in uA.
2237 *
2238 * NOTE: If the regulator is disabled it will return the current value. This
2239 * function should not be used to determine regulator state.
2240 */
2241int regulator_get_current_limit(struct regulator *regulator)
2242{
2243 return _regulator_get_current_limit(regulator->rdev);
2244}
2245EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2246
2247/**
2248 * regulator_set_mode - set regulator operating mode
2249 * @regulator: regulator source
2250 * @mode: operating mode - one of the REGULATOR_MODE constants
2251 *
2252 * Set regulator operating mode to increase regulator efficiency or improve
2253 * regulation performance.
2254 *
2255 * NOTE: Regulator system constraints must be set for this regulator before
2256 * calling this function otherwise this call will fail.
2257 */
2258int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2259{
2260 struct regulator_dev *rdev = regulator->rdev;
2261 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302262 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002263
2264 mutex_lock(&rdev->mutex);
2265
2266 /* sanity check */
2267 if (!rdev->desc->ops->set_mode) {
2268 ret = -EINVAL;
2269 goto out;
2270 }
2271
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302272 /* return if the same mode is requested */
2273 if (rdev->desc->ops->get_mode) {
2274 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2275 if (regulator_curr_mode == mode) {
2276 ret = 0;
2277 goto out;
2278 }
2279 }
2280
Liam Girdwood414c70c2008-04-30 15:59:04 +01002281 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002282 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002283 if (ret < 0)
2284 goto out;
2285
2286 ret = rdev->desc->ops->set_mode(rdev, mode);
2287out:
2288 mutex_unlock(&rdev->mutex);
2289 return ret;
2290}
2291EXPORT_SYMBOL_GPL(regulator_set_mode);
2292
2293static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2294{
2295 int ret;
2296
2297 mutex_lock(&rdev->mutex);
2298
2299 /* sanity check */
2300 if (!rdev->desc->ops->get_mode) {
2301 ret = -EINVAL;
2302 goto out;
2303 }
2304
2305 ret = rdev->desc->ops->get_mode(rdev);
2306out:
2307 mutex_unlock(&rdev->mutex);
2308 return ret;
2309}
2310
2311/**
2312 * regulator_get_mode - get regulator operating mode
2313 * @regulator: regulator source
2314 *
2315 * Get the current regulator operating mode.
2316 */
2317unsigned int regulator_get_mode(struct regulator *regulator)
2318{
2319 return _regulator_get_mode(regulator->rdev);
2320}
2321EXPORT_SYMBOL_GPL(regulator_get_mode);
2322
2323/**
2324 * regulator_set_optimum_mode - set regulator optimum operating mode
2325 * @regulator: regulator source
2326 * @uA_load: load current
2327 *
2328 * Notifies the regulator core of a new device load. This is then used by
2329 * DRMS (if enabled by constraints) to set the most efficient regulator
2330 * operating mode for the new regulator loading.
2331 *
2332 * Consumer devices notify their supply regulator of the maximum power
2333 * they will require (can be taken from device datasheet in the power
2334 * consumption tables) when they change operational status and hence power
2335 * state. Examples of operational state changes that can affect power
2336 * consumption are :-
2337 *
2338 * o Device is opened / closed.
2339 * o Device I/O is about to begin or has just finished.
2340 * o Device is idling in between work.
2341 *
2342 * This information is also exported via sysfs to userspace.
2343 *
2344 * DRMS will sum the total requested load on the regulator and change
2345 * to the most efficient operating mode if platform constraints allow.
2346 *
2347 * Returns the new regulator mode or error.
2348 */
2349int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2350{
2351 struct regulator_dev *rdev = regulator->rdev;
2352 struct regulator *consumer;
Stephen Boyd81797d92012-07-02 16:41:25 -07002353 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002354 unsigned int mode;
2355
Stephen Boyd81797d92012-07-02 16:41:25 -07002356 if (rdev->supply)
2357 input_uV = regulator_get_voltage(rdev->supply);
2358
Liam Girdwood414c70c2008-04-30 15:59:04 +01002359 mutex_lock(&rdev->mutex);
2360
Mark Browna4b41482011-05-14 11:19:45 -07002361 /*
2362 * first check to see if we can set modes at all, otherwise just
2363 * tell the consumer everything is OK.
2364 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002365 regulator->uA_load = uA_load;
2366 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002367 if (ret < 0) {
2368 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002369 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002370 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002371
Liam Girdwood414c70c2008-04-30 15:59:04 +01002372 if (!rdev->desc->ops->get_optimum_mode)
2373 goto out;
2374
Mark Browna4b41482011-05-14 11:19:45 -07002375 /*
2376 * we can actually do this so any errors are indicators of
2377 * potential real failure.
2378 */
2379 ret = -EINVAL;
2380
Liam Girdwood414c70c2008-04-30 15:59:04 +01002381 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002382 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002383 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002384 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002385 goto out;
2386 }
2387
Stephen Boyd81797d92012-07-02 16:41:25 -07002388 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002389 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002390 input_uV = rdev->constraints->input_uV;
2391 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002392 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002393 goto out;
2394 }
2395
2396 /* calc total requested load for this regulator */
2397 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002398 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002399
2400 mode = rdev->desc->ops->get_optimum_mode(rdev,
2401 input_uV, output_uV,
2402 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002403 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002404 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002405 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2406 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002407 goto out;
2408 }
2409
2410 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002411 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002412 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002413 goto out;
2414 }
2415 ret = mode;
2416out:
2417 mutex_unlock(&rdev->mutex);
2418 return ret;
2419}
2420EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2421
2422/**
2423 * regulator_register_notifier - register regulator event notifier
2424 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002425 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002426 *
2427 * Register notifier block to receive regulator events.
2428 */
2429int regulator_register_notifier(struct regulator *regulator,
2430 struct notifier_block *nb)
2431{
2432 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2433 nb);
2434}
2435EXPORT_SYMBOL_GPL(regulator_register_notifier);
2436
2437/**
2438 * regulator_unregister_notifier - unregister regulator event notifier
2439 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002440 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002441 *
2442 * Unregister regulator event notifier block.
2443 */
2444int regulator_unregister_notifier(struct regulator *regulator,
2445 struct notifier_block *nb)
2446{
2447 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2448 nb);
2449}
2450EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2451
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002452/* notify regulator consumers and downstream regulator consumers.
2453 * Note mutex must be held by caller.
2454 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002455static void _notifier_call_chain(struct regulator_dev *rdev,
2456 unsigned long event, void *data)
2457{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002458 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002459 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002460}
2461
2462/**
2463 * regulator_bulk_get - get multiple regulator consumers
2464 *
2465 * @dev: Device to supply
2466 * @num_consumers: Number of consumers to register
2467 * @consumers: Configuration of consumers; clients are stored here.
2468 *
2469 * @return 0 on success, an errno on failure.
2470 *
2471 * This helper function allows drivers to get several regulator
2472 * consumers in one operation. If any of the regulators cannot be
2473 * acquired then any regulators that were allocated will be freed
2474 * before returning to the caller.
2475 */
2476int regulator_bulk_get(struct device *dev, int num_consumers,
2477 struct regulator_bulk_data *consumers)
2478{
2479 int i;
2480 int ret;
2481
2482 for (i = 0; i < num_consumers; i++)
2483 consumers[i].consumer = NULL;
2484
2485 for (i = 0; i < num_consumers; i++) {
2486 consumers[i].consumer = regulator_get(dev,
2487 consumers[i].supply);
2488 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002489 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002490 dev_err(dev, "Failed to get supply '%s': %d\n",
2491 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002492 consumers[i].consumer = NULL;
2493 goto err;
2494 }
2495 }
2496
2497 return 0;
2498
2499err:
Axel Linb29c7692012-02-20 10:32:16 +08002500 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002501 regulator_put(consumers[i].consumer);
2502
2503 return ret;
2504}
2505EXPORT_SYMBOL_GPL(regulator_bulk_get);
2506
Mark Browne6e74032012-01-20 20:10:08 +00002507/**
2508 * devm_regulator_bulk_get - managed get multiple regulator consumers
2509 *
2510 * @dev: Device to supply
2511 * @num_consumers: Number of consumers to register
2512 * @consumers: Configuration of consumers; clients are stored here.
2513 *
2514 * @return 0 on success, an errno on failure.
2515 *
2516 * This helper function allows drivers to get several regulator
2517 * consumers in one operation with management, the regulators will
2518 * automatically be freed when the device is unbound. If any of the
2519 * regulators cannot be acquired then any regulators that were
2520 * allocated will be freed before returning to the caller.
2521 */
2522int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2523 struct regulator_bulk_data *consumers)
2524{
2525 int i;
2526 int ret;
2527
2528 for (i = 0; i < num_consumers; i++)
2529 consumers[i].consumer = NULL;
2530
2531 for (i = 0; i < num_consumers; i++) {
2532 consumers[i].consumer = devm_regulator_get(dev,
2533 consumers[i].supply);
2534 if (IS_ERR(consumers[i].consumer)) {
2535 ret = PTR_ERR(consumers[i].consumer);
2536 dev_err(dev, "Failed to get supply '%s': %d\n",
2537 consumers[i].supply, ret);
2538 consumers[i].consumer = NULL;
2539 goto err;
2540 }
2541 }
2542
2543 return 0;
2544
2545err:
2546 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2547 devm_regulator_put(consumers[i].consumer);
2548
2549 return ret;
2550}
2551EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2552
Mark Brownf21e0e82011-05-24 08:12:40 +08002553static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2554{
2555 struct regulator_bulk_data *bulk = data;
2556
2557 bulk->ret = regulator_enable(bulk->consumer);
2558}
2559
Liam Girdwood414c70c2008-04-30 15:59:04 +01002560/**
2561 * regulator_bulk_enable - enable multiple regulator consumers
2562 *
2563 * @num_consumers: Number of consumers
2564 * @consumers: Consumer data; clients are stored here.
2565 * @return 0 on success, an errno on failure
2566 *
2567 * This convenience API allows consumers to enable multiple regulator
2568 * clients in a single API call. If any consumers cannot be enabled
2569 * then any others that were enabled will be disabled again prior to
2570 * return.
2571 */
2572int regulator_bulk_enable(int num_consumers,
2573 struct regulator_bulk_data *consumers)
2574{
Mark Brownf21e0e82011-05-24 08:12:40 +08002575 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002576 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002577 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002578
Mark Brownf21e0e82011-05-24 08:12:40 +08002579 for (i = 0; i < num_consumers; i++)
2580 async_schedule_domain(regulator_bulk_enable_async,
2581 &consumers[i], &async_domain);
2582
2583 async_synchronize_full_domain(&async_domain);
2584
2585 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002586 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002587 if (consumers[i].ret != 0) {
2588 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002589 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002590 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002591 }
2592
2593 return 0;
2594
2595err:
Axel Linb29c7692012-02-20 10:32:16 +08002596 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2597 while (--i >= 0)
2598 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002599
2600 return ret;
2601}
2602EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2603
2604/**
Justin Paupore1d17cf52011-08-16 15:39:01 -07002605 * regulator_bulk_set_voltage - set voltage for multiple regulator consumers
2606 *
2607 * @num_consumers: Number of consumers
2608 * @consumers: Consumer data; clients are stored here.
2609 * @return 0 on success, an errno on failure
2610 *
2611 * This convenience API allows the voted voltage ranges of multiple regulator
2612 * clients to be set in a single API call. If any consumers cannot have their
2613 * voltages set, this function returns WITHOUT withdrawing votes for any
2614 * consumers that have already been set.
2615 */
2616int regulator_bulk_set_voltage(int num_consumers,
2617 struct regulator_bulk_data *consumers)
2618{
2619 int i;
2620 int rc;
2621
2622 for (i = 0; i < num_consumers; i++) {
2623 if (!consumers[i].min_uV && !consumers[i].max_uV)
2624 continue;
2625 rc = regulator_set_voltage(consumers[i].consumer,
2626 consumers[i].min_uV,
2627 consumers[i].max_uV);
2628 if (rc)
2629 goto err;
2630 }
2631
2632 return 0;
2633
2634err:
2635 pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc);
2636 return rc;
2637}
2638EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage);
2639
2640/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002641 * regulator_bulk_disable - disable multiple regulator consumers
2642 *
2643 * @num_consumers: Number of consumers
2644 * @consumers: Consumer data; clients are stored here.
2645 * @return 0 on success, an errno on failure
2646 *
2647 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002648 * clients in a single API call. If any consumers cannot be disabled
2649 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002650 * return.
2651 */
2652int regulator_bulk_disable(int num_consumers,
2653 struct regulator_bulk_data *consumers)
2654{
2655 int i;
2656 int ret;
2657
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002658 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002659 ret = regulator_disable(consumers[i].consumer);
2660 if (ret != 0)
2661 goto err;
2662 }
2663
2664 return 0;
2665
2666err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002667 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002668 for (++i; i < num_consumers; ++i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002669 regulator_enable(consumers[i].consumer);
2670
2671 return ret;
2672}
2673EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2674
2675/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002676 * regulator_bulk_force_disable - force disable multiple regulator consumers
2677 *
2678 * @num_consumers: Number of consumers
2679 * @consumers: Consumer data; clients are stored here.
2680 * @return 0 on success, an errno on failure
2681 *
2682 * This convenience API allows consumers to forcibly disable multiple regulator
2683 * clients in a single API call.
2684 * NOTE: This should be used for situations when device damage will
2685 * likely occur if the regulators are not disabled (e.g. over temp).
2686 * Although regulator_force_disable function call for some consumers can
2687 * return error numbers, the function is called for all consumers.
2688 */
2689int regulator_bulk_force_disable(int num_consumers,
2690 struct regulator_bulk_data *consumers)
2691{
2692 int i;
2693 int ret;
2694
2695 for (i = 0; i < num_consumers; i++)
2696 consumers[i].ret =
2697 regulator_force_disable(consumers[i].consumer);
2698
2699 for (i = 0; i < num_consumers; i++) {
2700 if (consumers[i].ret != 0) {
2701 ret = consumers[i].ret;
2702 goto out;
2703 }
2704 }
2705
2706 return 0;
2707out:
2708 return ret;
2709}
2710EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2711
2712/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002713 * regulator_bulk_free - free multiple regulator consumers
2714 *
2715 * @num_consumers: Number of consumers
2716 * @consumers: Consumer data; clients are stored here.
2717 *
2718 * This convenience API allows consumers to free multiple regulator
2719 * clients in a single API call.
2720 */
2721void regulator_bulk_free(int num_consumers,
2722 struct regulator_bulk_data *consumers)
2723{
2724 int i;
2725
2726 for (i = 0; i < num_consumers; i++) {
2727 regulator_put(consumers[i].consumer);
2728 consumers[i].consumer = NULL;
2729 }
2730}
2731EXPORT_SYMBOL_GPL(regulator_bulk_free);
2732
2733/**
2734 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002735 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002736 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002737 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002738 *
2739 * Called by regulator drivers to notify clients a regulator event has
2740 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002741 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002742 */
2743int regulator_notifier_call_chain(struct regulator_dev *rdev,
2744 unsigned long event, void *data)
2745{
2746 _notifier_call_chain(rdev, event, data);
2747 return NOTIFY_DONE;
2748
2749}
2750EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2751
Mark Brownbe721972009-08-04 20:09:52 +02002752/**
2753 * regulator_mode_to_status - convert a regulator mode into a status
2754 *
2755 * @mode: Mode to convert
2756 *
2757 * Convert a regulator mode into a status.
2758 */
2759int regulator_mode_to_status(unsigned int mode)
2760{
2761 switch (mode) {
2762 case REGULATOR_MODE_FAST:
2763 return REGULATOR_STATUS_FAST;
2764 case REGULATOR_MODE_NORMAL:
2765 return REGULATOR_STATUS_NORMAL;
2766 case REGULATOR_MODE_IDLE:
2767 return REGULATOR_STATUS_IDLE;
2768 case REGULATOR_STATUS_STANDBY:
2769 return REGULATOR_STATUS_STANDBY;
2770 default:
2771 return 0;
2772 }
2773}
2774EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2775
David Brownell7ad68e22008-11-11 17:39:02 -08002776/*
2777 * To avoid cluttering sysfs (and memory) with useless state, only
2778 * create attributes that can be meaningfully displayed.
2779 */
2780static int add_regulator_attributes(struct regulator_dev *rdev)
2781{
2782 struct device *dev = &rdev->dev;
2783 struct regulator_ops *ops = rdev->desc->ops;
2784 int status = 0;
2785
2786 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002787 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2788 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002789 status = device_create_file(dev, &dev_attr_microvolts);
2790 if (status < 0)
2791 return status;
2792 }
2793 if (ops->get_current_limit) {
2794 status = device_create_file(dev, &dev_attr_microamps);
2795 if (status < 0)
2796 return status;
2797 }
2798 if (ops->get_mode) {
2799 status = device_create_file(dev, &dev_attr_opmode);
2800 if (status < 0)
2801 return status;
2802 }
2803 if (ops->is_enabled) {
2804 status = device_create_file(dev, &dev_attr_state);
2805 if (status < 0)
2806 return status;
2807 }
David Brownell853116a2009-01-14 23:03:17 -08002808 if (ops->get_status) {
2809 status = device_create_file(dev, &dev_attr_status);
2810 if (status < 0)
2811 return status;
2812 }
David Brownell7ad68e22008-11-11 17:39:02 -08002813
2814 /* some attributes are type-specific */
2815 if (rdev->desc->type == REGULATOR_CURRENT) {
2816 status = device_create_file(dev, &dev_attr_requested_microamps);
2817 if (status < 0)
2818 return status;
2819 }
2820
2821 /* all the other attributes exist to support constraints;
2822 * don't show them if there are no constraints, or if the
2823 * relevant supporting methods are missing.
2824 */
2825 if (!rdev->constraints)
2826 return status;
2827
2828 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002829 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002830 status = device_create_file(dev, &dev_attr_min_microvolts);
2831 if (status < 0)
2832 return status;
2833 status = device_create_file(dev, &dev_attr_max_microvolts);
2834 if (status < 0)
2835 return status;
2836 }
2837 if (ops->set_current_limit) {
2838 status = device_create_file(dev, &dev_attr_min_microamps);
2839 if (status < 0)
2840 return status;
2841 status = device_create_file(dev, &dev_attr_max_microamps);
2842 if (status < 0)
2843 return status;
2844 }
2845
2846 /* suspend mode constraints need multiple supporting methods */
2847 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2848 return status;
2849
2850 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2851 if (status < 0)
2852 return status;
2853 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2854 if (status < 0)
2855 return status;
2856 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2857 if (status < 0)
2858 return status;
2859
2860 if (ops->set_suspend_voltage) {
2861 status = device_create_file(dev,
2862 &dev_attr_suspend_standby_microvolts);
2863 if (status < 0)
2864 return status;
2865 status = device_create_file(dev,
2866 &dev_attr_suspend_mem_microvolts);
2867 if (status < 0)
2868 return status;
2869 status = device_create_file(dev,
2870 &dev_attr_suspend_disk_microvolts);
2871 if (status < 0)
2872 return status;
2873 }
2874
2875 if (ops->set_suspend_mode) {
2876 status = device_create_file(dev,
2877 &dev_attr_suspend_standby_mode);
2878 if (status < 0)
2879 return status;
2880 status = device_create_file(dev,
2881 &dev_attr_suspend_mem_mode);
2882 if (status < 0)
2883 return status;
2884 status = device_create_file(dev,
2885 &dev_attr_suspend_disk_mode);
2886 if (status < 0)
2887 return status;
2888 }
2889
2890 return status;
2891}
2892
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002893#ifdef CONFIG_DEBUG_FS
2894
2895#define MAX_DEBUG_BUF_LEN 50
2896
2897static DEFINE_MUTEX(debug_buf_mutex);
2898static char debug_buf[MAX_DEBUG_BUF_LEN];
2899
2900static int reg_debug_enable_set(void *data, u64 val)
2901{
2902 int err_info;
2903 if (IS_ERR(data) || data == NULL) {
2904 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2905 return -ENOMEM;
2906 }
2907
2908 if (val)
2909 err_info = regulator_enable(data);
2910 else
2911 err_info = regulator_disable(data);
2912
2913 return err_info;
2914}
2915
2916static int reg_debug_enable_get(void *data, u64 *val)
2917{
2918 if (IS_ERR(data) || data == NULL) {
2919 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2920 return -ENOMEM;
2921 }
2922
2923 *val = regulator_is_enabled(data);
2924 return 0;
2925}
2926
2927DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
2928 reg_debug_enable_set, "%llu\n");
2929
2930static int reg_debug_fdisable_set(void *data, u64 val)
2931{
2932 int err_info;
2933 if (IS_ERR(data) || data == NULL) {
2934 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2935 return -ENOMEM;
2936 }
2937
2938 if (val > 0)
2939 err_info = regulator_force_disable(data);
2940 else
2941 err_info = 0;
2942
2943 return err_info;
2944}
2945
2946DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get,
2947 reg_debug_fdisable_set, "%llu\n");
2948
2949static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf,
2950 size_t count, loff_t *ppos)
2951{
2952 int err_info, filled;
2953 int min, max = -1;
2954 if (IS_ERR(file) || file == NULL) {
2955 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2956 return -ENOMEM;
2957 }
2958
2959 if (count < MAX_DEBUG_BUF_LEN) {
2960 mutex_lock(&debug_buf_mutex);
2961
2962 if (copy_from_user(debug_buf, (void __user *) buf, count))
2963 return -EFAULT;
2964
2965 debug_buf[count] = '\0';
2966 filled = sscanf(debug_buf, "%d %d", &min, &max);
2967
2968 mutex_unlock(&debug_buf_mutex);
2969 /* check that user entered two numbers */
2970 if (filled < 2 || min < 0 || max < min) {
2971 pr_info("Error, correct format: 'echo \"min max\""
2972 " > voltage");
2973 return -ENOMEM;
2974 } else {
2975 err_info = regulator_set_voltage(file->private_data,
2976 min, max);
2977 }
2978 } else {
2979 pr_err("Error-Input voltage pair"
2980 " string exceeds maximum buffer length");
2981
2982 return -ENOMEM;
2983 }
2984
2985 return count;
2986}
2987
2988static ssize_t reg_debug_volt_get(struct file *file, char __user *buf,
2989 size_t count, loff_t *ppos)
2990{
2991 int voltage, output, rc;
2992 if (IS_ERR(file) || file == NULL) {
2993 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2994 return -ENOMEM;
2995 }
2996
2997 voltage = regulator_get_voltage(file->private_data);
2998 mutex_lock(&debug_buf_mutex);
2999
3000 output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage);
3001 rc = simple_read_from_buffer((void __user *) buf, output, ppos,
3002 (void *) debug_buf, output);
3003
3004 mutex_unlock(&debug_buf_mutex);
3005
3006 return rc;
3007}
3008
3009static int reg_debug_volt_open(struct inode *inode, struct file *file)
3010{
3011 if (IS_ERR(file) || file == NULL) {
3012 pr_err("Function Input Error %ld\n", PTR_ERR(file));
3013 return -ENOMEM;
3014 }
3015
3016 file->private_data = inode->i_private;
3017 return 0;
3018}
3019
3020static const struct file_operations reg_volt_fops = {
3021 .write = reg_debug_volt_set,
3022 .open = reg_debug_volt_open,
3023 .read = reg_debug_volt_get,
3024};
3025
3026static int reg_debug_mode_set(void *data, u64 val)
3027{
3028 int err_info;
3029 if (IS_ERR(data) || data == NULL) {
3030 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3031 return -ENOMEM;
3032 }
3033
3034 err_info = regulator_set_mode(data, (unsigned int)val);
3035
3036 return err_info;
3037}
3038
3039static int reg_debug_mode_get(void *data, u64 *val)
3040{
3041 int err_info;
3042 if (IS_ERR(data) || data == NULL) {
3043 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3044 return -ENOMEM;
3045 }
3046
3047 err_info = regulator_get_mode(data);
3048
3049 if (err_info < 0) {
3050 pr_err("Regulator_get_mode returned an error!\n");
3051 return -ENOMEM;
3052 } else {
3053 *val = err_info;
3054 return 0;
3055 }
3056}
3057
3058DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get,
3059 reg_debug_mode_set, "%llu\n");
3060
3061static int reg_debug_optimum_mode_set(void *data, u64 val)
3062{
3063 int err_info;
3064 if (IS_ERR(data) || data == NULL) {
3065 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3066 return -ENOMEM;
3067 }
3068
3069 err_info = regulator_set_optimum_mode(data, (unsigned int)val);
3070
3071 if (err_info < 0) {
3072 pr_err("Regulator_set_optimum_mode returned an error!\n");
3073 return err_info;
3074 }
3075
3076 return 0;
3077}
3078
3079DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get,
3080 reg_debug_optimum_mode_set, "%llu\n");
3081
3082static int reg_debug_consumers_show(struct seq_file *m, void *v)
3083{
3084 struct regulator_dev *rdev = m->private;
3085 struct regulator *reg;
3086 char *supply_name;
3087
3088 if (!rdev) {
3089 pr_err("regulator device missing");
3090 return -EINVAL;
3091 }
3092
3093 mutex_lock(&rdev->mutex);
3094
3095 /* Print a header if there are consumers. */
3096 if (rdev->open_count)
3097 seq_printf(m, "Device-Supply "
3098 "EN Min_uV Max_uV load_uA\n");
3099
3100 list_for_each_entry(reg, &rdev->consumer_list, list) {
3101 if (reg->supply_name)
3102 supply_name = reg->supply_name;
3103 else
3104 supply_name = "(null)-(null)";
3105
3106 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
3107 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
3108 reg->uA_load);
3109 }
3110
3111 mutex_unlock(&rdev->mutex);
3112
3113 return 0;
3114}
3115
3116static int reg_debug_consumers_open(struct inode *inode, struct file *file)
3117{
3118 return single_open(file, reg_debug_consumers_show, inode->i_private);
3119}
3120
3121static const struct file_operations reg_consumers_fops = {
3122 .owner = THIS_MODULE,
3123 .open = reg_debug_consumers_open,
3124 .read = seq_read,
3125 .llseek = seq_lseek,
3126 .release = single_release,
3127};
3128
Mark Brown1130e5b2010-12-21 23:49:31 +00003129static void rdev_init_debugfs(struct regulator_dev *rdev)
3130{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003131 struct dentry *err_ptr = NULL;
3132 struct regulator *reg;
3133 struct regulator_ops *reg_ops;
3134 mode_t mode;
3135
3136 if (IS_ERR(rdev) || rdev == NULL ||
3137 IS_ERR(debugfs_root) || debugfs_root == NULL) {
3138 pr_err("Error-Bad Function Input\n");
3139 goto error;
3140 }
3141
Mark Brown1130e5b2010-12-21 23:49:31 +00003142 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3143 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
3144 rdev_warn(rdev, "Failed to create debugfs directory\n");
3145 rdev->debugfs = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003146 goto error;
Mark Brown1130e5b2010-12-21 23:49:31 +00003147 }
3148
3149 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3150 &rdev->use_count);
3151 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3152 &rdev->open_count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003153 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
3154 &reg_consumers_fops);
3155
3156 reg = regulator_get(NULL, rdev->desc->name);
3157 if (IS_ERR(reg) || reg == NULL) {
3158 pr_err("Error-Bad Function Input\n");
3159 goto error;
3160 }
3161
3162 reg_ops = rdev->desc->ops;
3163 mode = S_IRUGO | S_IWUSR;
3164 /* Enabled File */
3165 if (mode)
3166 err_ptr = debugfs_create_file("enable", mode, rdev->debugfs,
3167 reg, &reg_enable_fops);
3168 if (IS_ERR(err_ptr)) {
3169 pr_err("Error-Could not create enable file\n");
3170 debugfs_remove_recursive(rdev->debugfs);
3171 goto error;
3172 }
3173
3174 mode = 0;
3175 /* Force-Disable File */
3176 if (reg_ops->is_enabled)
3177 mode |= S_IRUGO;
3178 if (reg_ops->enable || reg_ops->disable)
3179 mode |= S_IWUSR;
3180 if (mode)
3181 err_ptr = debugfs_create_file("force_disable", mode,
3182 rdev->debugfs, reg, &reg_fdisable_fops);
3183 if (IS_ERR(err_ptr)) {
3184 pr_err("Error-Could not create force_disable file\n");
3185 debugfs_remove_recursive(rdev->debugfs);
3186 goto error;
3187 }
3188
3189 mode = 0;
3190 /* Voltage File */
3191 if (reg_ops->get_voltage)
3192 mode |= S_IRUGO;
3193 if (reg_ops->set_voltage)
3194 mode |= S_IWUSR;
3195 if (mode)
3196 err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs,
3197 reg, &reg_volt_fops);
3198 if (IS_ERR(err_ptr)) {
3199 pr_err("Error-Could not create voltage file\n");
3200 debugfs_remove_recursive(rdev->debugfs);
3201 goto error;
3202 }
3203
3204 mode = 0;
3205 /* Mode File */
3206 if (reg_ops->get_mode)
3207 mode |= S_IRUGO;
3208 if (reg_ops->set_mode)
3209 mode |= S_IWUSR;
3210 if (mode)
3211 err_ptr = debugfs_create_file("mode", mode, rdev->debugfs,
3212 reg, &reg_mode_fops);
3213 if (IS_ERR(err_ptr)) {
3214 pr_err("Error-Could not create mode file\n");
3215 debugfs_remove_recursive(rdev->debugfs);
3216 goto error;
3217 }
3218
3219 mode = 0;
3220 /* Optimum Mode File */
3221 if (reg_ops->get_mode)
3222 mode |= S_IRUGO;
3223 if (reg_ops->set_mode)
3224 mode |= S_IWUSR;
3225 if (mode)
3226 err_ptr = debugfs_create_file("optimum_mode", mode,
3227 rdev->debugfs, reg, &reg_optimum_mode_fops);
3228 if (IS_ERR(err_ptr)) {
3229 pr_err("Error-Could not create optimum_mode file\n");
3230 debugfs_remove_recursive(rdev->debugfs);
3231 goto error;
3232 }
3233
3234error:
3235 return;
Mark Brown1130e5b2010-12-21 23:49:31 +00003236}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003237#else
3238static inline void rdev_init_debugfs(struct regulator_dev *rdev)
3239{
3240 return;
3241}
3242#endif
Mark Brown1130e5b2010-12-21 23:49:31 +00003243
Liam Girdwood414c70c2008-04-30 15:59:04 +01003244/**
3245 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003246 * @regulator_desc: regulator to register
3247 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00003248 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00003249 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00003250 * @of_node: OpenFirmware node to parse for device tree bindings (may be
3251 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01003252 *
3253 * Called by regulator drivers to register a regulator.
3254 * Returns 0 on success.
3255 */
3256struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00003257 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303258 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003259{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003260 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003261 static atomic_t regulator_no = ATOMIC_INIT(0);
3262 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003263 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303264 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265
3266 if (regulator_desc == NULL)
3267 return ERR_PTR(-EINVAL);
3268
3269 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3270 return ERR_PTR(-EINVAL);
3271
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003272 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3273 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003274 return ERR_PTR(-EINVAL);
3275
Mark Brown476c2d82010-12-10 17:28:07 +00003276 /* Only one of each should be implemented */
3277 WARN_ON(regulator_desc->ops->get_voltage &&
3278 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003279 WARN_ON(regulator_desc->ops->set_voltage &&
3280 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003281
3282 /* If we're using selectors we must implement list_voltage. */
3283 if (regulator_desc->ops->get_voltage_sel &&
3284 !regulator_desc->ops->list_voltage) {
3285 return ERR_PTR(-EINVAL);
3286 }
Mark Browne8eef822010-12-12 14:36:17 +00003287 if (regulator_desc->ops->set_voltage_sel &&
3288 !regulator_desc->ops->list_voltage) {
3289 return ERR_PTR(-EINVAL);
3290 }
Mark Brown476c2d82010-12-10 17:28:07 +00003291
Liam Girdwood414c70c2008-04-30 15:59:04 +01003292 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3293 if (rdev == NULL)
3294 return ERR_PTR(-ENOMEM);
3295
3296 mutex_lock(&regulator_list_mutex);
3297
3298 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003299 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003300 rdev->owner = regulator_desc->owner;
3301 rdev->desc = regulator_desc;
3302 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003303 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003304 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003305 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003306
Liam Girdwooda5766f12008-10-10 13:22:20 +01003307 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003308 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003309 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003310 if (ret < 0)
3311 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003312 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003313
Liam Girdwooda5766f12008-10-10 13:22:20 +01003314 /* register with sysfs */
3315 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303316 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003317 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003318 dev_set_name(&rdev->dev, "regulator.%d",
3319 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003320 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003321 if (ret != 0) {
3322 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003323 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003324 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003325
3326 dev_set_drvdata(&rdev->dev, rdev);
3327
Mike Rapoport74f544c2008-11-25 14:53:53 +02003328 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003329 if (init_data)
3330 constraints = &init_data->constraints;
3331
3332 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003333 if (ret < 0)
3334 goto scrub;
3335
David Brownell7ad68e22008-11-11 17:39:02 -08003336 /* add attributes supported by this regulator */
3337 ret = add_regulator_attributes(rdev);
3338 if (ret < 0)
3339 goto scrub;
3340
Mark Brown9a8f5e02011-11-29 18:11:19 +00003341 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303342 supply = init_data->supply_regulator;
3343 else if (regulator_desc->supply_name)
3344 supply = regulator_desc->supply_name;
3345
3346 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003347 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003348
Rajendra Nayak69511a42011-11-18 16:47:20 +05303349 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01003350
Rajendra Nayak69511a42011-11-18 16:47:20 +05303351 if (!r) {
3352 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003353 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003354 goto scrub;
3355 }
3356
3357 ret = set_supply(rdev, r);
3358 if (ret < 0)
3359 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303360
3361 /* Enable supply if rail is enabled */
3362 if (rdev->desc->ops->is_enabled &&
3363 rdev->desc->ops->is_enabled(rdev)) {
3364 ret = regulator_enable(rdev->supply);
3365 if (ret < 0)
3366 goto scrub;
3367 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003368 }
3369
Liam Girdwooda5766f12008-10-10 13:22:20 +01003370 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003371 if (init_data) {
3372 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3373 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003374 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003375 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003376 if (ret < 0) {
3377 dev_err(dev, "Failed to set supply %s\n",
3378 init_data->consumer_supplies[i].supply);
3379 goto unset_supplies;
3380 }
Mark Brown23c2f042011-02-24 17:39:09 +00003381 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003382 }
3383
3384 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003385
Liam Girdwood414c70c2008-04-30 15:59:04 +01003386 mutex_unlock(&regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003387 rdev_init_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003388 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003389
Liam Girdwood414c70c2008-04-30 15:59:04 +01003390out:
3391 mutex_unlock(&regulator_list_mutex);
3392 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003393
Jani Nikulad4033b52010-04-29 10:55:11 +03003394unset_supplies:
3395 unset_regulator_supplies(rdev);
3396
David Brownell4fca9542008-11-11 17:38:53 -08003397scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08003398 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003399 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003400 /* device core frees rdev */
3401 rdev = ERR_PTR(ret);
3402 goto out;
3403
David Brownell4fca9542008-11-11 17:38:53 -08003404clean:
3405 kfree(rdev);
3406 rdev = ERR_PTR(ret);
3407 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003408}
3409EXPORT_SYMBOL_GPL(regulator_register);
3410
3411/**
3412 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003413 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003414 *
3415 * Called by regulator drivers to unregister a regulator.
3416 */
3417void regulator_unregister(struct regulator_dev *rdev)
3418{
3419 if (rdev == NULL)
3420 return;
3421
Mark Browne032b372012-03-28 21:17:55 +01003422 if (rdev->supply)
3423 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003424 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003425 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003426 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003427 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003428 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003429 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003430 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003431 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003432 mutex_unlock(&regulator_list_mutex);
3433}
3434EXPORT_SYMBOL_GPL(regulator_unregister);
3435
3436/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003437 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003438 * @state: system suspend state
3439 *
3440 * Configure each regulator with it's suspend operating parameters for state.
3441 * This will usually be called by machine suspend code prior to supending.
3442 */
3443int regulator_suspend_prepare(suspend_state_t state)
3444{
3445 struct regulator_dev *rdev;
3446 int ret = 0;
3447
3448 /* ON is handled by regulator active state */
3449 if (state == PM_SUSPEND_ON)
3450 return -EINVAL;
3451
3452 mutex_lock(&regulator_list_mutex);
3453 list_for_each_entry(rdev, &regulator_list, list) {
3454
3455 mutex_lock(&rdev->mutex);
3456 ret = suspend_prepare(rdev, state);
3457 mutex_unlock(&rdev->mutex);
3458
3459 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003460 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003461 goto out;
3462 }
3463 }
3464out:
3465 mutex_unlock(&regulator_list_mutex);
3466 return ret;
3467}
3468EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3469
3470/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003471 * regulator_suspend_finish - resume regulators from system wide suspend
3472 *
3473 * Turn on regulators that might be turned off by regulator_suspend_prepare
3474 * and that should be turned on according to the regulators properties.
3475 */
3476int regulator_suspend_finish(void)
3477{
3478 struct regulator_dev *rdev;
3479 int ret = 0, error;
3480
3481 mutex_lock(&regulator_list_mutex);
3482 list_for_each_entry(rdev, &regulator_list, list) {
3483 struct regulator_ops *ops = rdev->desc->ops;
3484
3485 mutex_lock(&rdev->mutex);
3486 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3487 ops->enable) {
3488 error = ops->enable(rdev);
3489 if (error)
3490 ret = error;
3491 } else {
3492 if (!has_full_constraints)
3493 goto unlock;
3494 if (!ops->disable)
3495 goto unlock;
3496 if (ops->is_enabled && !ops->is_enabled(rdev))
3497 goto unlock;
3498
3499 error = ops->disable(rdev);
3500 if (error)
3501 ret = error;
3502 }
3503unlock:
3504 mutex_unlock(&rdev->mutex);
3505 }
3506 mutex_unlock(&regulator_list_mutex);
3507 return ret;
3508}
3509EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3510
3511/**
Mark Brownca725562009-03-16 19:36:34 +00003512 * regulator_has_full_constraints - the system has fully specified constraints
3513 *
3514 * Calling this function will cause the regulator API to disable all
3515 * regulators which have a zero use count and don't have an always_on
3516 * constraint in a late_initcall.
3517 *
3518 * The intention is that this will become the default behaviour in a
3519 * future kernel release so users are encouraged to use this facility
3520 * now.
3521 */
3522void regulator_has_full_constraints(void)
3523{
3524 has_full_constraints = 1;
3525}
3526EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3527
3528/**
Mark Brown688fe992010-10-05 19:18:32 -07003529 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3530 *
3531 * Calling this function will cause the regulator API to provide a
3532 * dummy regulator to consumers if no physical regulator is found,
3533 * allowing most consumers to proceed as though a regulator were
3534 * configured. This allows systems such as those with software
3535 * controllable regulators for the CPU core only to be brought up more
3536 * readily.
3537 */
3538void regulator_use_dummy_regulator(void)
3539{
3540 board_wants_dummy_regulator = true;
3541}
3542EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3543
3544/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003545 * regulator_suppress_info_printing - disable printing of info messages
3546 *
3547 * The regulator framework calls print_constraints() when a regulator is
3548 * registered. It also prints a disable message for each unused regulator in
3549 * regulator_init_complete().
3550 *
3551 * Calling this function ensures that such messages do not end up in the
3552 * log.
3553 */
3554void regulator_suppress_info_printing(void)
3555{
3556 suppress_info_printing = 1;
3557}
3558EXPORT_SYMBOL_GPL(regulator_suppress_info_printing);
3559
3560/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003561 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003562 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003563 *
3564 * Get rdev regulator driver private data. This call can be used in the
3565 * regulator driver context.
3566 */
3567void *rdev_get_drvdata(struct regulator_dev *rdev)
3568{
3569 return rdev->reg_data;
3570}
3571EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3572
3573/**
3574 * regulator_get_drvdata - get regulator driver data
3575 * @regulator: regulator
3576 *
3577 * Get regulator driver private data. This call can be used in the consumer
3578 * driver context when non API regulator specific functions need to be called.
3579 */
3580void *regulator_get_drvdata(struct regulator *regulator)
3581{
3582 return regulator->rdev->reg_data;
3583}
3584EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3585
3586/**
3587 * regulator_set_drvdata - set regulator driver data
3588 * @regulator: regulator
3589 * @data: data
3590 */
3591void regulator_set_drvdata(struct regulator *regulator, void *data)
3592{
3593 regulator->rdev->reg_data = data;
3594}
3595EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3596
3597/**
3598 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003599 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003600 */
3601int rdev_get_id(struct regulator_dev *rdev)
3602{
3603 return rdev->desc->id;
3604}
3605EXPORT_SYMBOL_GPL(rdev_get_id);
3606
Liam Girdwooda5766f12008-10-10 13:22:20 +01003607struct device *rdev_get_dev(struct regulator_dev *rdev)
3608{
3609 return &rdev->dev;
3610}
3611EXPORT_SYMBOL_GPL(rdev_get_dev);
3612
3613void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3614{
3615 return reg_init_data->driver_data;
3616}
3617EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3618
Mark Brownba55a972011-08-23 17:39:10 +01003619#ifdef CONFIG_DEBUG_FS
3620static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3621 size_t count, loff_t *ppos)
3622{
3623 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3624 ssize_t len, ret = 0;
3625 struct regulator_map *map;
3626
3627 if (!buf)
3628 return -ENOMEM;
3629
3630 list_for_each_entry(map, &regulator_map_list, list) {
3631 len = snprintf(buf + ret, PAGE_SIZE - ret,
3632 "%s -> %s.%s\n",
3633 rdev_get_name(map->regulator), map->dev_name,
3634 map->supply);
3635 if (len >= 0)
3636 ret += len;
3637 if (ret > PAGE_SIZE) {
3638 ret = PAGE_SIZE;
3639 break;
3640 }
3641 }
3642
3643 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3644
3645 kfree(buf);
3646
3647 return ret;
3648}
Stephen Boyd24751432012-02-20 22:50:42 -08003649#endif
Mark Brownba55a972011-08-23 17:39:10 +01003650
3651static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003652#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003653 .read = supply_map_read_file,
3654 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003655#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003656};
Mark Brownba55a972011-08-23 17:39:10 +01003657
Liam Girdwood414c70c2008-04-30 15:59:04 +01003658static int __init regulator_init(void)
3659{
Mark Brown34abbd62010-02-12 10:18:08 +00003660 int ret;
3661
Mark Brown34abbd62010-02-12 10:18:08 +00003662 ret = class_register(&regulator_class);
3663
Mark Brown1130e5b2010-12-21 23:49:31 +00003664 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003665 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003666 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003667
Mark Brownf4d562c2012-02-20 21:01:04 +00003668 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3669 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003670
Mark Brown34abbd62010-02-12 10:18:08 +00003671 regulator_dummy_init();
3672
3673 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003674}
3675
3676/* init early to allow our consumers to complete system booting */
3677core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003678
3679static int __init regulator_init_complete(void)
3680{
3681 struct regulator_dev *rdev;
3682 struct regulator_ops *ops;
3683 struct regulation_constraints *c;
3684 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003685
3686 mutex_lock(&regulator_list_mutex);
3687
3688 /* If we have a full configuration then disable any regulators
3689 * which are not in use or always_on. This will become the
3690 * default behaviour in the future.
3691 */
3692 list_for_each_entry(rdev, &regulator_list, list) {
3693 ops = rdev->desc->ops;
3694 c = rdev->constraints;
3695
Mark Brownf25e0b42009-08-03 18:49:55 +01003696 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003697 continue;
3698
3699 mutex_lock(&rdev->mutex);
3700
3701 if (rdev->use_count)
3702 goto unlock;
3703
3704 /* If we can't read the status assume it's on. */
3705 if (ops->is_enabled)
3706 enabled = ops->is_enabled(rdev);
3707 else
3708 enabled = 1;
3709
3710 if (!enabled)
3711 goto unlock;
3712
3713 if (has_full_constraints) {
3714 /* We log since this may kill the system if it
3715 * goes wrong. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003716 if (!suppress_info_printing)
3717 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003718 ret = ops->disable(rdev);
3719 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003720 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003721 }
3722 } else {
3723 /* The intention is that in future we will
3724 * assume that full constraints are provided
3725 * so warn even if we aren't going to do
3726 * anything here.
3727 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003728 if (!suppress_info_printing)
3729 rdev_warn(rdev, "incomplete constraints, "
3730 "leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003731 }
3732
3733unlock:
3734 mutex_unlock(&rdev->mutex);
3735 }
3736
3737 mutex_unlock(&regulator_list_mutex);
3738
3739 return 0;
3740}
3741late_initcall(regulator_init_complete);