blob: 57cde4595d6982c613488f66f3c3851390104c6e [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);
1900 else
1901 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001902 } else if (rdev->desc->ops->set_voltage_sel) {
1903 int best_val = INT_MAX;
1904 int i;
1905
1906 selector = 0;
1907
1908 /* Find the smallest voltage that falls within the specified
1909 * range.
1910 */
1911 for (i = 0; i < rdev->desc->n_voltages; i++) {
1912 ret = rdev->desc->ops->list_voltage(rdev, i);
1913 if (ret < 0)
1914 continue;
1915
1916 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1917 best_val = ret;
1918 selector = i;
1919 }
1920 }
1921
Linus Walleij77af1b22011-03-17 13:24:36 +01001922 /*
1923 * If we can't obtain the old selector there is not enough
1924 * info to call set_voltage_time_sel().
1925 */
1926 if (rdev->desc->ops->set_voltage_time_sel &&
1927 rdev->desc->ops->get_voltage_sel) {
1928 unsigned int old_selector = 0;
1929
1930 ret = rdev->desc->ops->get_voltage_sel(rdev);
1931 if (ret < 0)
1932 return ret;
1933 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001934 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b22011-03-17 13:24:36 +01001935 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001936 if (ret < 0)
1937 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1938 else
1939 delay = ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001940 }
1941
Mark Browne8eef822010-12-12 14:36:17 +00001942 if (best_val != INT_MAX) {
1943 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1944 selector = best_val;
1945 } else {
1946 ret = -EINVAL;
1947 }
Mark Brown75790252010-12-12 14:25:50 +00001948 } else {
1949 ret = -EINVAL;
1950 }
1951
Linus Walleij77af1b22011-03-17 13:24:36 +01001952 /* Insert any necessary delays */
1953 if (delay >= 1000) {
1954 mdelay(delay / 1000);
1955 udelay(delay % 1000);
1956 } else if (delay) {
1957 udelay(delay);
1958 }
1959
Mark Brownded06a52010-12-16 13:59:10 +00001960 if (ret == 0)
1961 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1962 NULL);
1963
Mark Brown75790252010-12-12 14:25:50 +00001964 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1965
1966 return ret;
1967}
1968
Mark Browna7a1ad92009-07-21 16:00:24 +01001969/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001970 * regulator_set_voltage - set regulator output voltage
1971 * @regulator: regulator source
1972 * @min_uV: Minimum required voltage in uV
1973 * @max_uV: Maximum acceptable voltage in uV
1974 *
1975 * Sets a voltage regulator to the desired output voltage. This can be set
1976 * during any regulator state. IOW, regulator can be disabled or enabled.
1977 *
1978 * If the regulator is enabled then the voltage will change to the new value
1979 * immediately otherwise if the regulator is disabled the regulator will
1980 * output at the new voltage when enabled.
1981 *
1982 * NOTE: If the regulator is shared between several devices then the lowest
1983 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001984 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001985 * calling this function otherwise this call will fail.
1986 */
1987int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1988{
1989 struct regulator_dev *rdev = regulator->rdev;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001990 int prev_min_uV, prev_max_uV;
Mark Brown95a3c232010-12-16 15:49:37 +00001991 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001992
1993 mutex_lock(&rdev->mutex);
1994
Mark Brown95a3c232010-12-16 15:49:37 +00001995 /* If we're setting the same range as last time the change
1996 * should be a noop (some cpufreq implementations use the same
1997 * voltage for multiple frequencies, for example).
1998 */
1999 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2000 goto out;
2001
Liam Girdwood414c70c2008-04-30 15:59:04 +01002002 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002003 if (!rdev->desc->ops->set_voltage &&
2004 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005 ret = -EINVAL;
2006 goto out;
2007 }
2008
2009 /* constraints check */
2010 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2011 if (ret < 0)
2012 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002013
Steve Mucklef132c6c2012-06-06 18:30:57 -07002014 prev_min_uV = regulator->min_uV;
2015 prev_max_uV = regulator->max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016
Liam Girdwood414c70c2008-04-30 15:59:04 +01002017 regulator->min_uV = min_uV;
2018 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002019
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002020 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002021 if (ret < 0) {
2022 regulator->min_uV = prev_min_uV;
2023 regulator->max_uV = prev_max_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002024 goto out;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002025 }
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002026
Mark Brown75790252010-12-12 14:25:50 +00002027 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00002028
Liam Girdwood414c70c2008-04-30 15:59:04 +01002029out:
2030 mutex_unlock(&rdev->mutex);
2031 return ret;
2032}
2033EXPORT_SYMBOL_GPL(regulator_set_voltage);
2034
Mark Brown606a2562010-12-16 15:49:36 +00002035/**
Linus Walleij88cd2222011-03-17 13:24:52 +01002036 * regulator_set_voltage_time - get raise/fall time
2037 * @regulator: regulator source
2038 * @old_uV: starting voltage in microvolts
2039 * @new_uV: target voltage in microvolts
2040 *
2041 * Provided with the starting and ending voltage, this function attempts to
2042 * calculate the time in microseconds required to rise or fall to this new
2043 * voltage.
2044 */
2045int regulator_set_voltage_time(struct regulator *regulator,
2046 int old_uV, int new_uV)
2047{
2048 struct regulator_dev *rdev = regulator->rdev;
2049 struct regulator_ops *ops = rdev->desc->ops;
2050 int old_sel = -1;
2051 int new_sel = -1;
2052 int voltage;
2053 int i;
2054
2055 /* Currently requires operations to do this */
2056 if (!ops->list_voltage || !ops->set_voltage_time_sel
2057 || !rdev->desc->n_voltages)
2058 return -EINVAL;
2059
2060 for (i = 0; i < rdev->desc->n_voltages; i++) {
2061 /* We only look for exact voltage matches here */
2062 voltage = regulator_list_voltage(regulator, i);
2063 if (voltage < 0)
2064 return -EINVAL;
2065 if (voltage == 0)
2066 continue;
2067 if (voltage == old_uV)
2068 old_sel = i;
2069 if (voltage == new_uV)
2070 new_sel = i;
2071 }
2072
2073 if (old_sel < 0 || new_sel < 0)
2074 return -EINVAL;
2075
2076 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2077}
2078EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2079
2080/**
Mark Brown606a2562010-12-16 15:49:36 +00002081 * regulator_sync_voltage - re-apply last regulator output voltage
2082 * @regulator: regulator source
2083 *
2084 * Re-apply the last configured voltage. This is intended to be used
2085 * where some external control source the consumer is cooperating with
2086 * has caused the configured voltage to change.
2087 */
2088int regulator_sync_voltage(struct regulator *regulator)
2089{
2090 struct regulator_dev *rdev = regulator->rdev;
2091 int ret, min_uV, max_uV;
2092
2093 mutex_lock(&rdev->mutex);
2094
2095 if (!rdev->desc->ops->set_voltage &&
2096 !rdev->desc->ops->set_voltage_sel) {
2097 ret = -EINVAL;
2098 goto out;
2099 }
2100
2101 /* This is only going to work if we've had a voltage configured. */
2102 if (!regulator->min_uV && !regulator->max_uV) {
2103 ret = -EINVAL;
2104 goto out;
2105 }
2106
2107 min_uV = regulator->min_uV;
2108 max_uV = regulator->max_uV;
2109
2110 /* This should be a paranoia check... */
2111 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2112 if (ret < 0)
2113 goto out;
2114
2115 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2116 if (ret < 0)
2117 goto out;
2118
2119 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2120
2121out:
2122 mutex_unlock(&rdev->mutex);
2123 return ret;
2124}
2125EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2126
Liam Girdwood414c70c2008-04-30 15:59:04 +01002127static int _regulator_get_voltage(struct regulator_dev *rdev)
2128{
Mark Brownbf5892a2011-05-08 22:13:37 +01002129 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002130
2131 if (rdev->desc->ops->get_voltage_sel) {
2132 sel = rdev->desc->ops->get_voltage_sel(rdev);
2133 if (sel < 0)
2134 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002135 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002136 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002137 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002138 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002139 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002140 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002141
Axel Lincb220d12011-05-23 20:08:10 +08002142 if (ret < 0)
2143 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002144 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145}
2146
2147/**
2148 * regulator_get_voltage - get regulator output voltage
2149 * @regulator: regulator source
2150 *
2151 * This returns the current regulator voltage in uV.
2152 *
2153 * NOTE: If the regulator is disabled it will return the voltage value. This
2154 * function should not be used to determine regulator state.
2155 */
2156int regulator_get_voltage(struct regulator *regulator)
2157{
2158 int ret;
2159
2160 mutex_lock(&regulator->rdev->mutex);
2161
2162 ret = _regulator_get_voltage(regulator->rdev);
2163
2164 mutex_unlock(&regulator->rdev->mutex);
2165
2166 return ret;
2167}
2168EXPORT_SYMBOL_GPL(regulator_get_voltage);
2169
2170/**
2171 * regulator_set_current_limit - set regulator output current limit
2172 * @regulator: regulator source
2173 * @min_uA: Minimuum supported current in uA
2174 * @max_uA: Maximum supported current in uA
2175 *
2176 * Sets current sink to the desired output current. This can be set during
2177 * any regulator state. IOW, regulator can be disabled or enabled.
2178 *
2179 * If the regulator is enabled then the current will change to the new value
2180 * immediately otherwise if the regulator is disabled the regulator will
2181 * output at the new current when enabled.
2182 *
2183 * NOTE: Regulator system constraints must be set for this regulator before
2184 * calling this function otherwise this call will fail.
2185 */
2186int regulator_set_current_limit(struct regulator *regulator,
2187 int min_uA, int max_uA)
2188{
2189 struct regulator_dev *rdev = regulator->rdev;
2190 int ret;
2191
2192 mutex_lock(&rdev->mutex);
2193
2194 /* sanity check */
2195 if (!rdev->desc->ops->set_current_limit) {
2196 ret = -EINVAL;
2197 goto out;
2198 }
2199
2200 /* constraints check */
2201 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2202 if (ret < 0)
2203 goto out;
2204
2205 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2206out:
2207 mutex_unlock(&rdev->mutex);
2208 return ret;
2209}
2210EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2211
2212static int _regulator_get_current_limit(struct regulator_dev *rdev)
2213{
2214 int ret;
2215
2216 mutex_lock(&rdev->mutex);
2217
2218 /* sanity check */
2219 if (!rdev->desc->ops->get_current_limit) {
2220 ret = -EINVAL;
2221 goto out;
2222 }
2223
2224 ret = rdev->desc->ops->get_current_limit(rdev);
2225out:
2226 mutex_unlock(&rdev->mutex);
2227 return ret;
2228}
2229
2230/**
2231 * regulator_get_current_limit - get regulator output current
2232 * @regulator: regulator source
2233 *
2234 * This returns the current supplied by the specified current sink in uA.
2235 *
2236 * NOTE: If the regulator is disabled it will return the current value. This
2237 * function should not be used to determine regulator state.
2238 */
2239int regulator_get_current_limit(struct regulator *regulator)
2240{
2241 return _regulator_get_current_limit(regulator->rdev);
2242}
2243EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2244
2245/**
2246 * regulator_set_mode - set regulator operating mode
2247 * @regulator: regulator source
2248 * @mode: operating mode - one of the REGULATOR_MODE constants
2249 *
2250 * Set regulator operating mode to increase regulator efficiency or improve
2251 * regulation performance.
2252 *
2253 * NOTE: Regulator system constraints must be set for this regulator before
2254 * calling this function otherwise this call will fail.
2255 */
2256int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2257{
2258 struct regulator_dev *rdev = regulator->rdev;
2259 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302260 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002261
2262 mutex_lock(&rdev->mutex);
2263
2264 /* sanity check */
2265 if (!rdev->desc->ops->set_mode) {
2266 ret = -EINVAL;
2267 goto out;
2268 }
2269
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302270 /* return if the same mode is requested */
2271 if (rdev->desc->ops->get_mode) {
2272 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2273 if (regulator_curr_mode == mode) {
2274 ret = 0;
2275 goto out;
2276 }
2277 }
2278
Liam Girdwood414c70c2008-04-30 15:59:04 +01002279 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002280 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002281 if (ret < 0)
2282 goto out;
2283
2284 ret = rdev->desc->ops->set_mode(rdev, mode);
2285out:
2286 mutex_unlock(&rdev->mutex);
2287 return ret;
2288}
2289EXPORT_SYMBOL_GPL(regulator_set_mode);
2290
2291static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2292{
2293 int ret;
2294
2295 mutex_lock(&rdev->mutex);
2296
2297 /* sanity check */
2298 if (!rdev->desc->ops->get_mode) {
2299 ret = -EINVAL;
2300 goto out;
2301 }
2302
2303 ret = rdev->desc->ops->get_mode(rdev);
2304out:
2305 mutex_unlock(&rdev->mutex);
2306 return ret;
2307}
2308
2309/**
2310 * regulator_get_mode - get regulator operating mode
2311 * @regulator: regulator source
2312 *
2313 * Get the current regulator operating mode.
2314 */
2315unsigned int regulator_get_mode(struct regulator *regulator)
2316{
2317 return _regulator_get_mode(regulator->rdev);
2318}
2319EXPORT_SYMBOL_GPL(regulator_get_mode);
2320
2321/**
2322 * regulator_set_optimum_mode - set regulator optimum operating mode
2323 * @regulator: regulator source
2324 * @uA_load: load current
2325 *
2326 * Notifies the regulator core of a new device load. This is then used by
2327 * DRMS (if enabled by constraints) to set the most efficient regulator
2328 * operating mode for the new regulator loading.
2329 *
2330 * Consumer devices notify their supply regulator of the maximum power
2331 * they will require (can be taken from device datasheet in the power
2332 * consumption tables) when they change operational status and hence power
2333 * state. Examples of operational state changes that can affect power
2334 * consumption are :-
2335 *
2336 * o Device is opened / closed.
2337 * o Device I/O is about to begin or has just finished.
2338 * o Device is idling in between work.
2339 *
2340 * This information is also exported via sysfs to userspace.
2341 *
2342 * DRMS will sum the total requested load on the regulator and change
2343 * to the most efficient operating mode if platform constraints allow.
2344 *
2345 * Returns the new regulator mode or error.
2346 */
2347int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2348{
2349 struct regulator_dev *rdev = regulator->rdev;
2350 struct regulator *consumer;
Stephen Boyd81797d92012-07-02 16:41:25 -07002351 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352 unsigned int mode;
2353
Stephen Boyd81797d92012-07-02 16:41:25 -07002354 if (rdev->supply)
2355 input_uV = regulator_get_voltage(rdev->supply);
2356
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 mutex_lock(&rdev->mutex);
2358
Mark Browna4b41482011-05-14 11:19:45 -07002359 /*
2360 * first check to see if we can set modes at all, otherwise just
2361 * tell the consumer everything is OK.
2362 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 regulator->uA_load = uA_load;
2364 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002365 if (ret < 0) {
2366 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002368 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002369
Liam Girdwood414c70c2008-04-30 15:59:04 +01002370 if (!rdev->desc->ops->get_optimum_mode)
2371 goto out;
2372
Mark Browna4b41482011-05-14 11:19:45 -07002373 /*
2374 * we can actually do this so any errors are indicators of
2375 * potential real failure.
2376 */
2377 ret = -EINVAL;
2378
Liam Girdwood414c70c2008-04-30 15:59:04 +01002379 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002380 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002381 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002382 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002383 goto out;
2384 }
2385
Stephen Boyd81797d92012-07-02 16:41:25 -07002386 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002387 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002388 input_uV = rdev->constraints->input_uV;
2389 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002390 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002391 goto out;
2392 }
2393
2394 /* calc total requested load for this regulator */
2395 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002396 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002397
2398 mode = rdev->desc->ops->get_optimum_mode(rdev,
2399 input_uV, output_uV,
2400 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002401 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002402 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002403 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2404 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002405 goto out;
2406 }
2407
2408 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002409 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002410 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002411 goto out;
2412 }
2413 ret = mode;
2414out:
2415 mutex_unlock(&rdev->mutex);
2416 return ret;
2417}
2418EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2419
2420/**
2421 * regulator_register_notifier - register regulator event notifier
2422 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002423 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002424 *
2425 * Register notifier block to receive regulator events.
2426 */
2427int regulator_register_notifier(struct regulator *regulator,
2428 struct notifier_block *nb)
2429{
2430 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2431 nb);
2432}
2433EXPORT_SYMBOL_GPL(regulator_register_notifier);
2434
2435/**
2436 * regulator_unregister_notifier - unregister regulator event notifier
2437 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002438 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002439 *
2440 * Unregister regulator event notifier block.
2441 */
2442int regulator_unregister_notifier(struct regulator *regulator,
2443 struct notifier_block *nb)
2444{
2445 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2446 nb);
2447}
2448EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2449
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002450/* notify regulator consumers and downstream regulator consumers.
2451 * Note mutex must be held by caller.
2452 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002453static void _notifier_call_chain(struct regulator_dev *rdev,
2454 unsigned long event, void *data)
2455{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002456 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002457 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002458}
2459
2460/**
2461 * regulator_bulk_get - get multiple regulator consumers
2462 *
2463 * @dev: Device to supply
2464 * @num_consumers: Number of consumers to register
2465 * @consumers: Configuration of consumers; clients are stored here.
2466 *
2467 * @return 0 on success, an errno on failure.
2468 *
2469 * This helper function allows drivers to get several regulator
2470 * consumers in one operation. If any of the regulators cannot be
2471 * acquired then any regulators that were allocated will be freed
2472 * before returning to the caller.
2473 */
2474int regulator_bulk_get(struct device *dev, int num_consumers,
2475 struct regulator_bulk_data *consumers)
2476{
2477 int i;
2478 int ret;
2479
2480 for (i = 0; i < num_consumers; i++)
2481 consumers[i].consumer = NULL;
2482
2483 for (i = 0; i < num_consumers; i++) {
2484 consumers[i].consumer = regulator_get(dev,
2485 consumers[i].supply);
2486 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002487 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002488 dev_err(dev, "Failed to get supply '%s': %d\n",
2489 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002490 consumers[i].consumer = NULL;
2491 goto err;
2492 }
2493 }
2494
2495 return 0;
2496
2497err:
Axel Linb29c7692012-02-20 10:32:16 +08002498 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002499 regulator_put(consumers[i].consumer);
2500
2501 return ret;
2502}
2503EXPORT_SYMBOL_GPL(regulator_bulk_get);
2504
Mark Browne6e74032012-01-20 20:10:08 +00002505/**
2506 * devm_regulator_bulk_get - managed get multiple regulator consumers
2507 *
2508 * @dev: Device to supply
2509 * @num_consumers: Number of consumers to register
2510 * @consumers: Configuration of consumers; clients are stored here.
2511 *
2512 * @return 0 on success, an errno on failure.
2513 *
2514 * This helper function allows drivers to get several regulator
2515 * consumers in one operation with management, the regulators will
2516 * automatically be freed when the device is unbound. If any of the
2517 * regulators cannot be acquired then any regulators that were
2518 * allocated will be freed before returning to the caller.
2519 */
2520int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2521 struct regulator_bulk_data *consumers)
2522{
2523 int i;
2524 int ret;
2525
2526 for (i = 0; i < num_consumers; i++)
2527 consumers[i].consumer = NULL;
2528
2529 for (i = 0; i < num_consumers; i++) {
2530 consumers[i].consumer = devm_regulator_get(dev,
2531 consumers[i].supply);
2532 if (IS_ERR(consumers[i].consumer)) {
2533 ret = PTR_ERR(consumers[i].consumer);
2534 dev_err(dev, "Failed to get supply '%s': %d\n",
2535 consumers[i].supply, ret);
2536 consumers[i].consumer = NULL;
2537 goto err;
2538 }
2539 }
2540
2541 return 0;
2542
2543err:
2544 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2545 devm_regulator_put(consumers[i].consumer);
2546
2547 return ret;
2548}
2549EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2550
Mark Brownf21e0e82011-05-24 08:12:40 +08002551static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2552{
2553 struct regulator_bulk_data *bulk = data;
2554
2555 bulk->ret = regulator_enable(bulk->consumer);
2556}
2557
Liam Girdwood414c70c2008-04-30 15:59:04 +01002558/**
2559 * regulator_bulk_enable - enable multiple regulator consumers
2560 *
2561 * @num_consumers: Number of consumers
2562 * @consumers: Consumer data; clients are stored here.
2563 * @return 0 on success, an errno on failure
2564 *
2565 * This convenience API allows consumers to enable multiple regulator
2566 * clients in a single API call. If any consumers cannot be enabled
2567 * then any others that were enabled will be disabled again prior to
2568 * return.
2569 */
2570int regulator_bulk_enable(int num_consumers,
2571 struct regulator_bulk_data *consumers)
2572{
Mark Brownf21e0e82011-05-24 08:12:40 +08002573 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002574 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002575 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002576
Mark Brownf21e0e82011-05-24 08:12:40 +08002577 for (i = 0; i < num_consumers; i++)
2578 async_schedule_domain(regulator_bulk_enable_async,
2579 &consumers[i], &async_domain);
2580
2581 async_synchronize_full_domain(&async_domain);
2582
2583 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002584 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002585 if (consumers[i].ret != 0) {
2586 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002587 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002588 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002589 }
2590
2591 return 0;
2592
2593err:
Axel Linb29c7692012-02-20 10:32:16 +08002594 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2595 while (--i >= 0)
2596 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002597
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2601
2602/**
Justin Paupore1d17cf52011-08-16 15:39:01 -07002603 * regulator_bulk_set_voltage - set voltage for multiple regulator consumers
2604 *
2605 * @num_consumers: Number of consumers
2606 * @consumers: Consumer data; clients are stored here.
2607 * @return 0 on success, an errno on failure
2608 *
2609 * This convenience API allows the voted voltage ranges of multiple regulator
2610 * clients to be set in a single API call. If any consumers cannot have their
2611 * voltages set, this function returns WITHOUT withdrawing votes for any
2612 * consumers that have already been set.
2613 */
2614int regulator_bulk_set_voltage(int num_consumers,
2615 struct regulator_bulk_data *consumers)
2616{
2617 int i;
2618 int rc;
2619
2620 for (i = 0; i < num_consumers; i++) {
2621 if (!consumers[i].min_uV && !consumers[i].max_uV)
2622 continue;
2623 rc = regulator_set_voltage(consumers[i].consumer,
2624 consumers[i].min_uV,
2625 consumers[i].max_uV);
2626 if (rc)
2627 goto err;
2628 }
2629
2630 return 0;
2631
2632err:
2633 pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc);
2634 return rc;
2635}
2636EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage);
2637
2638/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002639 * regulator_bulk_disable - disable multiple regulator consumers
2640 *
2641 * @num_consumers: Number of consumers
2642 * @consumers: Consumer data; clients are stored here.
2643 * @return 0 on success, an errno on failure
2644 *
2645 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002646 * clients in a single API call. If any consumers cannot be disabled
2647 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002648 * return.
2649 */
2650int regulator_bulk_disable(int num_consumers,
2651 struct regulator_bulk_data *consumers)
2652{
2653 int i;
2654 int ret;
2655
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002656 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002657 ret = regulator_disable(consumers[i].consumer);
2658 if (ret != 0)
2659 goto err;
2660 }
2661
2662 return 0;
2663
2664err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002665 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002666 for (++i; i < num_consumers; ++i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002667 regulator_enable(consumers[i].consumer);
2668
2669 return ret;
2670}
2671EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2672
2673/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002674 * regulator_bulk_force_disable - force disable multiple regulator consumers
2675 *
2676 * @num_consumers: Number of consumers
2677 * @consumers: Consumer data; clients are stored here.
2678 * @return 0 on success, an errno on failure
2679 *
2680 * This convenience API allows consumers to forcibly disable multiple regulator
2681 * clients in a single API call.
2682 * NOTE: This should be used for situations when device damage will
2683 * likely occur if the regulators are not disabled (e.g. over temp).
2684 * Although regulator_force_disable function call for some consumers can
2685 * return error numbers, the function is called for all consumers.
2686 */
2687int regulator_bulk_force_disable(int num_consumers,
2688 struct regulator_bulk_data *consumers)
2689{
2690 int i;
2691 int ret;
2692
2693 for (i = 0; i < num_consumers; i++)
2694 consumers[i].ret =
2695 regulator_force_disable(consumers[i].consumer);
2696
2697 for (i = 0; i < num_consumers; i++) {
2698 if (consumers[i].ret != 0) {
2699 ret = consumers[i].ret;
2700 goto out;
2701 }
2702 }
2703
2704 return 0;
2705out:
2706 return ret;
2707}
2708EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2709
2710/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002711 * regulator_bulk_free - free multiple regulator consumers
2712 *
2713 * @num_consumers: Number of consumers
2714 * @consumers: Consumer data; clients are stored here.
2715 *
2716 * This convenience API allows consumers to free multiple regulator
2717 * clients in a single API call.
2718 */
2719void regulator_bulk_free(int num_consumers,
2720 struct regulator_bulk_data *consumers)
2721{
2722 int i;
2723
2724 for (i = 0; i < num_consumers; i++) {
2725 regulator_put(consumers[i].consumer);
2726 consumers[i].consumer = NULL;
2727 }
2728}
2729EXPORT_SYMBOL_GPL(regulator_bulk_free);
2730
2731/**
2732 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002733 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002734 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002735 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002736 *
2737 * Called by regulator drivers to notify clients a regulator event has
2738 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002739 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002740 */
2741int regulator_notifier_call_chain(struct regulator_dev *rdev,
2742 unsigned long event, void *data)
2743{
2744 _notifier_call_chain(rdev, event, data);
2745 return NOTIFY_DONE;
2746
2747}
2748EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2749
Mark Brownbe721972009-08-04 20:09:52 +02002750/**
2751 * regulator_mode_to_status - convert a regulator mode into a status
2752 *
2753 * @mode: Mode to convert
2754 *
2755 * Convert a regulator mode into a status.
2756 */
2757int regulator_mode_to_status(unsigned int mode)
2758{
2759 switch (mode) {
2760 case REGULATOR_MODE_FAST:
2761 return REGULATOR_STATUS_FAST;
2762 case REGULATOR_MODE_NORMAL:
2763 return REGULATOR_STATUS_NORMAL;
2764 case REGULATOR_MODE_IDLE:
2765 return REGULATOR_STATUS_IDLE;
2766 case REGULATOR_STATUS_STANDBY:
2767 return REGULATOR_STATUS_STANDBY;
2768 default:
2769 return 0;
2770 }
2771}
2772EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2773
David Brownell7ad68e22008-11-11 17:39:02 -08002774/*
2775 * To avoid cluttering sysfs (and memory) with useless state, only
2776 * create attributes that can be meaningfully displayed.
2777 */
2778static int add_regulator_attributes(struct regulator_dev *rdev)
2779{
2780 struct device *dev = &rdev->dev;
2781 struct regulator_ops *ops = rdev->desc->ops;
2782 int status = 0;
2783
2784 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002785 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2786 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002787 status = device_create_file(dev, &dev_attr_microvolts);
2788 if (status < 0)
2789 return status;
2790 }
2791 if (ops->get_current_limit) {
2792 status = device_create_file(dev, &dev_attr_microamps);
2793 if (status < 0)
2794 return status;
2795 }
2796 if (ops->get_mode) {
2797 status = device_create_file(dev, &dev_attr_opmode);
2798 if (status < 0)
2799 return status;
2800 }
2801 if (ops->is_enabled) {
2802 status = device_create_file(dev, &dev_attr_state);
2803 if (status < 0)
2804 return status;
2805 }
David Brownell853116a2009-01-14 23:03:17 -08002806 if (ops->get_status) {
2807 status = device_create_file(dev, &dev_attr_status);
2808 if (status < 0)
2809 return status;
2810 }
David Brownell7ad68e22008-11-11 17:39:02 -08002811
2812 /* some attributes are type-specific */
2813 if (rdev->desc->type == REGULATOR_CURRENT) {
2814 status = device_create_file(dev, &dev_attr_requested_microamps);
2815 if (status < 0)
2816 return status;
2817 }
2818
2819 /* all the other attributes exist to support constraints;
2820 * don't show them if there are no constraints, or if the
2821 * relevant supporting methods are missing.
2822 */
2823 if (!rdev->constraints)
2824 return status;
2825
2826 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002827 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002828 status = device_create_file(dev, &dev_attr_min_microvolts);
2829 if (status < 0)
2830 return status;
2831 status = device_create_file(dev, &dev_attr_max_microvolts);
2832 if (status < 0)
2833 return status;
2834 }
2835 if (ops->set_current_limit) {
2836 status = device_create_file(dev, &dev_attr_min_microamps);
2837 if (status < 0)
2838 return status;
2839 status = device_create_file(dev, &dev_attr_max_microamps);
2840 if (status < 0)
2841 return status;
2842 }
2843
2844 /* suspend mode constraints need multiple supporting methods */
2845 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2846 return status;
2847
2848 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2849 if (status < 0)
2850 return status;
2851 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2852 if (status < 0)
2853 return status;
2854 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2855 if (status < 0)
2856 return status;
2857
2858 if (ops->set_suspend_voltage) {
2859 status = device_create_file(dev,
2860 &dev_attr_suspend_standby_microvolts);
2861 if (status < 0)
2862 return status;
2863 status = device_create_file(dev,
2864 &dev_attr_suspend_mem_microvolts);
2865 if (status < 0)
2866 return status;
2867 status = device_create_file(dev,
2868 &dev_attr_suspend_disk_microvolts);
2869 if (status < 0)
2870 return status;
2871 }
2872
2873 if (ops->set_suspend_mode) {
2874 status = device_create_file(dev,
2875 &dev_attr_suspend_standby_mode);
2876 if (status < 0)
2877 return status;
2878 status = device_create_file(dev,
2879 &dev_attr_suspend_mem_mode);
2880 if (status < 0)
2881 return status;
2882 status = device_create_file(dev,
2883 &dev_attr_suspend_disk_mode);
2884 if (status < 0)
2885 return status;
2886 }
2887
2888 return status;
2889}
2890
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002891#ifdef CONFIG_DEBUG_FS
2892
2893#define MAX_DEBUG_BUF_LEN 50
2894
2895static DEFINE_MUTEX(debug_buf_mutex);
2896static char debug_buf[MAX_DEBUG_BUF_LEN];
2897
2898static int reg_debug_enable_set(void *data, u64 val)
2899{
2900 int err_info;
2901 if (IS_ERR(data) || data == NULL) {
2902 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2903 return -ENOMEM;
2904 }
2905
2906 if (val)
2907 err_info = regulator_enable(data);
2908 else
2909 err_info = regulator_disable(data);
2910
2911 return err_info;
2912}
2913
2914static int reg_debug_enable_get(void *data, u64 *val)
2915{
2916 if (IS_ERR(data) || data == NULL) {
2917 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2918 return -ENOMEM;
2919 }
2920
2921 *val = regulator_is_enabled(data);
2922 return 0;
2923}
2924
2925DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
2926 reg_debug_enable_set, "%llu\n");
2927
2928static int reg_debug_fdisable_set(void *data, u64 val)
2929{
2930 int err_info;
2931 if (IS_ERR(data) || data == NULL) {
2932 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2933 return -ENOMEM;
2934 }
2935
2936 if (val > 0)
2937 err_info = regulator_force_disable(data);
2938 else
2939 err_info = 0;
2940
2941 return err_info;
2942}
2943
2944DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get,
2945 reg_debug_fdisable_set, "%llu\n");
2946
2947static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf,
2948 size_t count, loff_t *ppos)
2949{
2950 int err_info, filled;
2951 int min, max = -1;
2952 if (IS_ERR(file) || file == NULL) {
2953 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2954 return -ENOMEM;
2955 }
2956
2957 if (count < MAX_DEBUG_BUF_LEN) {
2958 mutex_lock(&debug_buf_mutex);
2959
2960 if (copy_from_user(debug_buf, (void __user *) buf, count))
2961 return -EFAULT;
2962
2963 debug_buf[count] = '\0';
2964 filled = sscanf(debug_buf, "%d %d", &min, &max);
2965
2966 mutex_unlock(&debug_buf_mutex);
2967 /* check that user entered two numbers */
2968 if (filled < 2 || min < 0 || max < min) {
2969 pr_info("Error, correct format: 'echo \"min max\""
2970 " > voltage");
2971 return -ENOMEM;
2972 } else {
2973 err_info = regulator_set_voltage(file->private_data,
2974 min, max);
2975 }
2976 } else {
2977 pr_err("Error-Input voltage pair"
2978 " string exceeds maximum buffer length");
2979
2980 return -ENOMEM;
2981 }
2982
2983 return count;
2984}
2985
2986static ssize_t reg_debug_volt_get(struct file *file, char __user *buf,
2987 size_t count, loff_t *ppos)
2988{
2989 int voltage, output, rc;
2990 if (IS_ERR(file) || file == NULL) {
2991 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2992 return -ENOMEM;
2993 }
2994
2995 voltage = regulator_get_voltage(file->private_data);
2996 mutex_lock(&debug_buf_mutex);
2997
2998 output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage);
2999 rc = simple_read_from_buffer((void __user *) buf, output, ppos,
3000 (void *) debug_buf, output);
3001
3002 mutex_unlock(&debug_buf_mutex);
3003
3004 return rc;
3005}
3006
3007static int reg_debug_volt_open(struct inode *inode, struct file *file)
3008{
3009 if (IS_ERR(file) || file == NULL) {
3010 pr_err("Function Input Error %ld\n", PTR_ERR(file));
3011 return -ENOMEM;
3012 }
3013
3014 file->private_data = inode->i_private;
3015 return 0;
3016}
3017
3018static const struct file_operations reg_volt_fops = {
3019 .write = reg_debug_volt_set,
3020 .open = reg_debug_volt_open,
3021 .read = reg_debug_volt_get,
3022};
3023
3024static int reg_debug_mode_set(void *data, u64 val)
3025{
3026 int err_info;
3027 if (IS_ERR(data) || data == NULL) {
3028 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3029 return -ENOMEM;
3030 }
3031
3032 err_info = regulator_set_mode(data, (unsigned int)val);
3033
3034 return err_info;
3035}
3036
3037static int reg_debug_mode_get(void *data, u64 *val)
3038{
3039 int err_info;
3040 if (IS_ERR(data) || data == NULL) {
3041 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3042 return -ENOMEM;
3043 }
3044
3045 err_info = regulator_get_mode(data);
3046
3047 if (err_info < 0) {
3048 pr_err("Regulator_get_mode returned an error!\n");
3049 return -ENOMEM;
3050 } else {
3051 *val = err_info;
3052 return 0;
3053 }
3054}
3055
3056DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get,
3057 reg_debug_mode_set, "%llu\n");
3058
3059static int reg_debug_optimum_mode_set(void *data, u64 val)
3060{
3061 int err_info;
3062 if (IS_ERR(data) || data == NULL) {
3063 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3064 return -ENOMEM;
3065 }
3066
3067 err_info = regulator_set_optimum_mode(data, (unsigned int)val);
3068
3069 if (err_info < 0) {
3070 pr_err("Regulator_set_optimum_mode returned an error!\n");
3071 return err_info;
3072 }
3073
3074 return 0;
3075}
3076
3077DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get,
3078 reg_debug_optimum_mode_set, "%llu\n");
3079
3080static int reg_debug_consumers_show(struct seq_file *m, void *v)
3081{
3082 struct regulator_dev *rdev = m->private;
3083 struct regulator *reg;
3084 char *supply_name;
3085
3086 if (!rdev) {
3087 pr_err("regulator device missing");
3088 return -EINVAL;
3089 }
3090
3091 mutex_lock(&rdev->mutex);
3092
3093 /* Print a header if there are consumers. */
3094 if (rdev->open_count)
3095 seq_printf(m, "Device-Supply "
3096 "EN Min_uV Max_uV load_uA\n");
3097
3098 list_for_each_entry(reg, &rdev->consumer_list, list) {
3099 if (reg->supply_name)
3100 supply_name = reg->supply_name;
3101 else
3102 supply_name = "(null)-(null)";
3103
3104 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
3105 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
3106 reg->uA_load);
3107 }
3108
3109 mutex_unlock(&rdev->mutex);
3110
3111 return 0;
3112}
3113
3114static int reg_debug_consumers_open(struct inode *inode, struct file *file)
3115{
3116 return single_open(file, reg_debug_consumers_show, inode->i_private);
3117}
3118
3119static const struct file_operations reg_consumers_fops = {
3120 .owner = THIS_MODULE,
3121 .open = reg_debug_consumers_open,
3122 .read = seq_read,
3123 .llseek = seq_lseek,
3124 .release = single_release,
3125};
3126
Mark Brown1130e5b2010-12-21 23:49:31 +00003127static void rdev_init_debugfs(struct regulator_dev *rdev)
3128{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003129 struct dentry *err_ptr = NULL;
3130 struct regulator *reg;
3131 struct regulator_ops *reg_ops;
3132 mode_t mode;
3133
3134 if (IS_ERR(rdev) || rdev == NULL ||
3135 IS_ERR(debugfs_root) || debugfs_root == NULL) {
3136 pr_err("Error-Bad Function Input\n");
3137 goto error;
3138 }
3139
Mark Brown1130e5b2010-12-21 23:49:31 +00003140 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3141 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
3142 rdev_warn(rdev, "Failed to create debugfs directory\n");
3143 rdev->debugfs = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003144 goto error;
Mark Brown1130e5b2010-12-21 23:49:31 +00003145 }
3146
3147 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3148 &rdev->use_count);
3149 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3150 &rdev->open_count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003151 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
3152 &reg_consumers_fops);
3153
3154 reg = regulator_get(NULL, rdev->desc->name);
3155 if (IS_ERR(reg) || reg == NULL) {
3156 pr_err("Error-Bad Function Input\n");
3157 goto error;
3158 }
3159
3160 reg_ops = rdev->desc->ops;
3161 mode = S_IRUGO | S_IWUSR;
3162 /* Enabled File */
3163 if (mode)
3164 err_ptr = debugfs_create_file("enable", mode, rdev->debugfs,
3165 reg, &reg_enable_fops);
3166 if (IS_ERR(err_ptr)) {
3167 pr_err("Error-Could not create enable file\n");
3168 debugfs_remove_recursive(rdev->debugfs);
3169 goto error;
3170 }
3171
3172 mode = 0;
3173 /* Force-Disable File */
3174 if (reg_ops->is_enabled)
3175 mode |= S_IRUGO;
3176 if (reg_ops->enable || reg_ops->disable)
3177 mode |= S_IWUSR;
3178 if (mode)
3179 err_ptr = debugfs_create_file("force_disable", mode,
3180 rdev->debugfs, reg, &reg_fdisable_fops);
3181 if (IS_ERR(err_ptr)) {
3182 pr_err("Error-Could not create force_disable file\n");
3183 debugfs_remove_recursive(rdev->debugfs);
3184 goto error;
3185 }
3186
3187 mode = 0;
3188 /* Voltage File */
3189 if (reg_ops->get_voltage)
3190 mode |= S_IRUGO;
3191 if (reg_ops->set_voltage)
3192 mode |= S_IWUSR;
3193 if (mode)
3194 err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs,
3195 reg, &reg_volt_fops);
3196 if (IS_ERR(err_ptr)) {
3197 pr_err("Error-Could not create voltage file\n");
3198 debugfs_remove_recursive(rdev->debugfs);
3199 goto error;
3200 }
3201
3202 mode = 0;
3203 /* Mode File */
3204 if (reg_ops->get_mode)
3205 mode |= S_IRUGO;
3206 if (reg_ops->set_mode)
3207 mode |= S_IWUSR;
3208 if (mode)
3209 err_ptr = debugfs_create_file("mode", mode, rdev->debugfs,
3210 reg, &reg_mode_fops);
3211 if (IS_ERR(err_ptr)) {
3212 pr_err("Error-Could not create mode file\n");
3213 debugfs_remove_recursive(rdev->debugfs);
3214 goto error;
3215 }
3216
3217 mode = 0;
3218 /* Optimum Mode File */
3219 if (reg_ops->get_mode)
3220 mode |= S_IRUGO;
3221 if (reg_ops->set_mode)
3222 mode |= S_IWUSR;
3223 if (mode)
3224 err_ptr = debugfs_create_file("optimum_mode", mode,
3225 rdev->debugfs, reg, &reg_optimum_mode_fops);
3226 if (IS_ERR(err_ptr)) {
3227 pr_err("Error-Could not create optimum_mode file\n");
3228 debugfs_remove_recursive(rdev->debugfs);
3229 goto error;
3230 }
3231
3232error:
3233 return;
Mark Brown1130e5b2010-12-21 23:49:31 +00003234}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003235#else
3236static inline void rdev_init_debugfs(struct regulator_dev *rdev)
3237{
3238 return;
3239}
3240#endif
Mark Brown1130e5b2010-12-21 23:49:31 +00003241
Liam Girdwood414c70c2008-04-30 15:59:04 +01003242/**
3243 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003244 * @regulator_desc: regulator to register
3245 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00003246 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00003247 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00003248 * @of_node: OpenFirmware node to parse for device tree bindings (may be
3249 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01003250 *
3251 * Called by regulator drivers to register a regulator.
3252 * Returns 0 on success.
3253 */
3254struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00003255 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303256 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003257{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003258 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003259 static atomic_t regulator_no = ATOMIC_INIT(0);
3260 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003261 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303262 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003263
3264 if (regulator_desc == NULL)
3265 return ERR_PTR(-EINVAL);
3266
3267 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3268 return ERR_PTR(-EINVAL);
3269
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003270 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3271 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003272 return ERR_PTR(-EINVAL);
3273
Mark Brown476c2d82010-12-10 17:28:07 +00003274 /* Only one of each should be implemented */
3275 WARN_ON(regulator_desc->ops->get_voltage &&
3276 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003277 WARN_ON(regulator_desc->ops->set_voltage &&
3278 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003279
3280 /* If we're using selectors we must implement list_voltage. */
3281 if (regulator_desc->ops->get_voltage_sel &&
3282 !regulator_desc->ops->list_voltage) {
3283 return ERR_PTR(-EINVAL);
3284 }
Mark Browne8eef822010-12-12 14:36:17 +00003285 if (regulator_desc->ops->set_voltage_sel &&
3286 !regulator_desc->ops->list_voltage) {
3287 return ERR_PTR(-EINVAL);
3288 }
Mark Brown476c2d82010-12-10 17:28:07 +00003289
Liam Girdwood414c70c2008-04-30 15:59:04 +01003290 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3291 if (rdev == NULL)
3292 return ERR_PTR(-ENOMEM);
3293
3294 mutex_lock(&regulator_list_mutex);
3295
3296 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003297 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003298 rdev->owner = regulator_desc->owner;
3299 rdev->desc = regulator_desc;
3300 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003301 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003302 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003303 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003304
Liam Girdwooda5766f12008-10-10 13:22:20 +01003305 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003306 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003307 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003308 if (ret < 0)
3309 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003310 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003311
Liam Girdwooda5766f12008-10-10 13:22:20 +01003312 /* register with sysfs */
3313 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303314 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003315 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003316 dev_set_name(&rdev->dev, "regulator.%d",
3317 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003318 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003319 if (ret != 0) {
3320 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003321 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003322 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003323
3324 dev_set_drvdata(&rdev->dev, rdev);
3325
Mike Rapoport74f544c2008-11-25 14:53:53 +02003326 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003327 if (init_data)
3328 constraints = &init_data->constraints;
3329
3330 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003331 if (ret < 0)
3332 goto scrub;
3333
David Brownell7ad68e22008-11-11 17:39:02 -08003334 /* add attributes supported by this regulator */
3335 ret = add_regulator_attributes(rdev);
3336 if (ret < 0)
3337 goto scrub;
3338
Mark Brown9a8f5e02011-11-29 18:11:19 +00003339 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303340 supply = init_data->supply_regulator;
3341 else if (regulator_desc->supply_name)
3342 supply = regulator_desc->supply_name;
3343
3344 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003345 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003346
Rajendra Nayak69511a42011-11-18 16:47:20 +05303347 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01003348
Rajendra Nayak69511a42011-11-18 16:47:20 +05303349 if (!r) {
3350 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003351 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003352 goto scrub;
3353 }
3354
3355 ret = set_supply(rdev, r);
3356 if (ret < 0)
3357 goto scrub;
Laxman Dewanganb2296bd2012-01-02 13:08:45 +05303358
3359 /* Enable supply if rail is enabled */
3360 if (rdev->desc->ops->is_enabled &&
3361 rdev->desc->ops->is_enabled(rdev)) {
3362 ret = regulator_enable(rdev->supply);
3363 if (ret < 0)
3364 goto scrub;
3365 }
Mark Brown0178f3e2010-04-26 15:18:14 +01003366 }
3367
Liam Girdwooda5766f12008-10-10 13:22:20 +01003368 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003369 if (init_data) {
3370 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3371 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003372 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003373 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003374 if (ret < 0) {
3375 dev_err(dev, "Failed to set supply %s\n",
3376 init_data->consumer_supplies[i].supply);
3377 goto unset_supplies;
3378 }
Mark Brown23c2f042011-02-24 17:39:09 +00003379 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003380 }
3381
3382 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003383
Liam Girdwood414c70c2008-04-30 15:59:04 +01003384 mutex_unlock(&regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003385 rdev_init_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003386 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003387
Liam Girdwood414c70c2008-04-30 15:59:04 +01003388out:
3389 mutex_unlock(&regulator_list_mutex);
3390 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003391
Jani Nikulad4033b52010-04-29 10:55:11 +03003392unset_supplies:
3393 unset_regulator_supplies(rdev);
3394
David Brownell4fca9542008-11-11 17:38:53 -08003395scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08003396 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08003397 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06003398 /* device core frees rdev */
3399 rdev = ERR_PTR(ret);
3400 goto out;
3401
David Brownell4fca9542008-11-11 17:38:53 -08003402clean:
3403 kfree(rdev);
3404 rdev = ERR_PTR(ret);
3405 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003406}
3407EXPORT_SYMBOL_GPL(regulator_register);
3408
3409/**
3410 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003411 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01003412 *
3413 * Called by regulator drivers to unregister a regulator.
3414 */
3415void regulator_unregister(struct regulator_dev *rdev)
3416{
3417 if (rdev == NULL)
3418 return;
3419
Mark Browne032b372012-03-28 21:17:55 +01003420 if (rdev->supply)
3421 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003422 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00003423 debugfs_remove_recursive(rdev->debugfs);
Mark Brownda07ecd2011-09-11 09:53:50 +01003424 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01003425 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02003426 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003427 list_del(&rdev->list);
Mark Brownf8c12fe2010-11-29 15:55:17 +00003428 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01003429 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003430 mutex_unlock(&regulator_list_mutex);
3431}
3432EXPORT_SYMBOL_GPL(regulator_unregister);
3433
3434/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00003435 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01003436 * @state: system suspend state
3437 *
3438 * Configure each regulator with it's suspend operating parameters for state.
3439 * This will usually be called by machine suspend code prior to supending.
3440 */
3441int regulator_suspend_prepare(suspend_state_t state)
3442{
3443 struct regulator_dev *rdev;
3444 int ret = 0;
3445
3446 /* ON is handled by regulator active state */
3447 if (state == PM_SUSPEND_ON)
3448 return -EINVAL;
3449
3450 mutex_lock(&regulator_list_mutex);
3451 list_for_each_entry(rdev, &regulator_list, list) {
3452
3453 mutex_lock(&rdev->mutex);
3454 ret = suspend_prepare(rdev, state);
3455 mutex_unlock(&rdev->mutex);
3456
3457 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003458 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01003459 goto out;
3460 }
3461 }
3462out:
3463 mutex_unlock(&regulator_list_mutex);
3464 return ret;
3465}
3466EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
3467
3468/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09003469 * regulator_suspend_finish - resume regulators from system wide suspend
3470 *
3471 * Turn on regulators that might be turned off by regulator_suspend_prepare
3472 * and that should be turned on according to the regulators properties.
3473 */
3474int regulator_suspend_finish(void)
3475{
3476 struct regulator_dev *rdev;
3477 int ret = 0, error;
3478
3479 mutex_lock(&regulator_list_mutex);
3480 list_for_each_entry(rdev, &regulator_list, list) {
3481 struct regulator_ops *ops = rdev->desc->ops;
3482
3483 mutex_lock(&rdev->mutex);
3484 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
3485 ops->enable) {
3486 error = ops->enable(rdev);
3487 if (error)
3488 ret = error;
3489 } else {
3490 if (!has_full_constraints)
3491 goto unlock;
3492 if (!ops->disable)
3493 goto unlock;
3494 if (ops->is_enabled && !ops->is_enabled(rdev))
3495 goto unlock;
3496
3497 error = ops->disable(rdev);
3498 if (error)
3499 ret = error;
3500 }
3501unlock:
3502 mutex_unlock(&rdev->mutex);
3503 }
3504 mutex_unlock(&regulator_list_mutex);
3505 return ret;
3506}
3507EXPORT_SYMBOL_GPL(regulator_suspend_finish);
3508
3509/**
Mark Brownca725562009-03-16 19:36:34 +00003510 * regulator_has_full_constraints - the system has fully specified constraints
3511 *
3512 * Calling this function will cause the regulator API to disable all
3513 * regulators which have a zero use count and don't have an always_on
3514 * constraint in a late_initcall.
3515 *
3516 * The intention is that this will become the default behaviour in a
3517 * future kernel release so users are encouraged to use this facility
3518 * now.
3519 */
3520void regulator_has_full_constraints(void)
3521{
3522 has_full_constraints = 1;
3523}
3524EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
3525
3526/**
Mark Brown688fe992010-10-05 19:18:32 -07003527 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
3528 *
3529 * Calling this function will cause the regulator API to provide a
3530 * dummy regulator to consumers if no physical regulator is found,
3531 * allowing most consumers to proceed as though a regulator were
3532 * configured. This allows systems such as those with software
3533 * controllable regulators for the CPU core only to be brought up more
3534 * readily.
3535 */
3536void regulator_use_dummy_regulator(void)
3537{
3538 board_wants_dummy_regulator = true;
3539}
3540EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
3541
3542/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003543 * regulator_suppress_info_printing - disable printing of info messages
3544 *
3545 * The regulator framework calls print_constraints() when a regulator is
3546 * registered. It also prints a disable message for each unused regulator in
3547 * regulator_init_complete().
3548 *
3549 * Calling this function ensures that such messages do not end up in the
3550 * log.
3551 */
3552void regulator_suppress_info_printing(void)
3553{
3554 suppress_info_printing = 1;
3555}
3556EXPORT_SYMBOL_GPL(regulator_suppress_info_printing);
3557
3558/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01003559 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00003560 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003561 *
3562 * Get rdev regulator driver private data. This call can be used in the
3563 * regulator driver context.
3564 */
3565void *rdev_get_drvdata(struct regulator_dev *rdev)
3566{
3567 return rdev->reg_data;
3568}
3569EXPORT_SYMBOL_GPL(rdev_get_drvdata);
3570
3571/**
3572 * regulator_get_drvdata - get regulator driver data
3573 * @regulator: regulator
3574 *
3575 * Get regulator driver private data. This call can be used in the consumer
3576 * driver context when non API regulator specific functions need to be called.
3577 */
3578void *regulator_get_drvdata(struct regulator *regulator)
3579{
3580 return regulator->rdev->reg_data;
3581}
3582EXPORT_SYMBOL_GPL(regulator_get_drvdata);
3583
3584/**
3585 * regulator_set_drvdata - set regulator driver data
3586 * @regulator: regulator
3587 * @data: data
3588 */
3589void regulator_set_drvdata(struct regulator *regulator, void *data)
3590{
3591 regulator->rdev->reg_data = data;
3592}
3593EXPORT_SYMBOL_GPL(regulator_set_drvdata);
3594
3595/**
3596 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00003597 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01003598 */
3599int rdev_get_id(struct regulator_dev *rdev)
3600{
3601 return rdev->desc->id;
3602}
3603EXPORT_SYMBOL_GPL(rdev_get_id);
3604
Liam Girdwooda5766f12008-10-10 13:22:20 +01003605struct device *rdev_get_dev(struct regulator_dev *rdev)
3606{
3607 return &rdev->dev;
3608}
3609EXPORT_SYMBOL_GPL(rdev_get_dev);
3610
3611void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
3612{
3613 return reg_init_data->driver_data;
3614}
3615EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
3616
Mark Brownba55a972011-08-23 17:39:10 +01003617#ifdef CONFIG_DEBUG_FS
3618static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
3619 size_t count, loff_t *ppos)
3620{
3621 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
3622 ssize_t len, ret = 0;
3623 struct regulator_map *map;
3624
3625 if (!buf)
3626 return -ENOMEM;
3627
3628 list_for_each_entry(map, &regulator_map_list, list) {
3629 len = snprintf(buf + ret, PAGE_SIZE - ret,
3630 "%s -> %s.%s\n",
3631 rdev_get_name(map->regulator), map->dev_name,
3632 map->supply);
3633 if (len >= 0)
3634 ret += len;
3635 if (ret > PAGE_SIZE) {
3636 ret = PAGE_SIZE;
3637 break;
3638 }
3639 }
3640
3641 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3642
3643 kfree(buf);
3644
3645 return ret;
3646}
Stephen Boyd24751432012-02-20 22:50:42 -08003647#endif
Mark Brownba55a972011-08-23 17:39:10 +01003648
3649static const struct file_operations supply_map_fops = {
Stephen Boyd24751432012-02-20 22:50:42 -08003650#ifdef CONFIG_DEBUG_FS
Mark Brownba55a972011-08-23 17:39:10 +01003651 .read = supply_map_read_file,
3652 .llseek = default_llseek,
Mark Brownba55a972011-08-23 17:39:10 +01003653#endif
Stephen Boyd24751432012-02-20 22:50:42 -08003654};
Mark Brownba55a972011-08-23 17:39:10 +01003655
Liam Girdwood414c70c2008-04-30 15:59:04 +01003656static int __init regulator_init(void)
3657{
Mark Brown34abbd62010-02-12 10:18:08 +00003658 int ret;
3659
Mark Brown34abbd62010-02-12 10:18:08 +00003660 ret = class_register(&regulator_class);
3661
Mark Brown1130e5b2010-12-21 23:49:31 +00003662 debugfs_root = debugfs_create_dir("regulator", NULL);
Stephen Boyd24751432012-02-20 22:50:42 -08003663 if (!debugfs_root)
Mark Brown1130e5b2010-12-21 23:49:31 +00003664 pr_warn("regulator: Failed to create debugfs directory\n");
Mark Brownba55a972011-08-23 17:39:10 +01003665
Mark Brownf4d562c2012-02-20 21:01:04 +00003666 debugfs_create_file("supply_map", 0444, debugfs_root, NULL,
3667 &supply_map_fops);
Mark Brown1130e5b2010-12-21 23:49:31 +00003668
Mark Brown34abbd62010-02-12 10:18:08 +00003669 regulator_dummy_init();
3670
3671 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003672}
3673
3674/* init early to allow our consumers to complete system booting */
3675core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003676
3677static int __init regulator_init_complete(void)
3678{
3679 struct regulator_dev *rdev;
3680 struct regulator_ops *ops;
3681 struct regulation_constraints *c;
3682 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003683
3684 mutex_lock(&regulator_list_mutex);
3685
3686 /* If we have a full configuration then disable any regulators
3687 * which are not in use or always_on. This will become the
3688 * default behaviour in the future.
3689 */
3690 list_for_each_entry(rdev, &regulator_list, list) {
3691 ops = rdev->desc->ops;
3692 c = rdev->constraints;
3693
Mark Brownf25e0b42009-08-03 18:49:55 +01003694 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003695 continue;
3696
3697 mutex_lock(&rdev->mutex);
3698
3699 if (rdev->use_count)
3700 goto unlock;
3701
3702 /* If we can't read the status assume it's on. */
3703 if (ops->is_enabled)
3704 enabled = ops->is_enabled(rdev);
3705 else
3706 enabled = 1;
3707
3708 if (!enabled)
3709 goto unlock;
3710
3711 if (has_full_constraints) {
3712 /* We log since this may kill the system if it
3713 * goes wrong. */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003714 if (!suppress_info_printing)
3715 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003716 ret = ops->disable(rdev);
3717 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003718 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003719 }
3720 } else {
3721 /* The intention is that in future we will
3722 * assume that full constraints are provided
3723 * so warn even if we aren't going to do
3724 * anything here.
3725 */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003726 if (!suppress_info_printing)
3727 rdev_warn(rdev, "incomplete constraints, "
3728 "leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003729 }
3730
3731unlock:
3732 mutex_unlock(&rdev->mutex);
3733 }
3734
3735 mutex_unlock(&regulator_list_mutex);
3736
3737 return 0;
3738}
3739late_initcall(regulator_init_complete);