blob: c946e5ff729820d892cff76cc1b54dea96f49deb [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000018#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010019#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080021#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053026#include <linux/of.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#include <linux/seq_file.h>
28#include <linux/uaccess.h>
Rajendra Nayak69511a42011-11-18 16:47:20 +053029#include <linux/regulator/of_regulator.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#include <linux/regulator/consumer.h>
31#include <linux/regulator/driver.h>
32#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040033#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010034
Mark Brown02fa3ec2010-11-10 14:38:30 +000035#define CREATE_TRACE_POINTS
36#include <trace/events/regulator.h>
37
Mark Brown34abbd62010-02-12 10:18:08 +000038#include "dummy.h"
39
Mark Brown7d51a0d2011-06-09 16:06:37 +010040#define rdev_crit(rdev, fmt, ...) \
41 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080042#define rdev_err(rdev, fmt, ...) \
43 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_warn(rdev, fmt, ...) \
45 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_info(rdev, fmt, ...) \
47 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48#define rdev_dbg(rdev, fmt, ...) \
49 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
50
Liam Girdwood414c70c2008-04-30 15:59:04 +010051static DEFINE_MUTEX(regulator_list_mutex);
52static LIST_HEAD(regulator_list);
53static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000054static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070055static bool board_wants_dummy_regulator;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056static int suppress_info_printing;
Liam Girdwood414c70c2008-04-30 15:59:04 +010057
Mark Brown1130e5b2010-12-21 23:49:31 +000058static struct dentry *debugfs_root;
Mark Brown1130e5b2010-12-21 23:49:31 +000059
Mark Brown8dc53902008-12-31 12:52:40 +000060/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010061 * struct regulator_map
62 *
63 * Used to provide symbolic supply names to devices.
64 */
65struct regulator_map {
66 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010067 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010068 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010069 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010070};
71
Liam Girdwood414c70c2008-04-30 15:59:04 +010072/*
73 * struct regulator
74 *
75 * One for each consumer device.
76 */
77struct regulator {
78 struct device *dev;
79 struct list_head list;
80 int uA_load;
81 int min_uV;
82 int max_uV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 int enabled;
Liam Girdwood414c70c2008-04-30 15:59:04 +010084 char *supply_name;
85 struct device_attribute dev_attr;
86 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010087 struct dentry *debugfs;
Liam Girdwood414c70c2008-04-30 15:59:04 +010088};
89
90static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010091static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010092static int _regulator_get_voltage(struct regulator_dev *rdev);
93static int _regulator_get_current_limit(struct regulator_dev *rdev);
94static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
95static void _notifier_call_chain(struct regulator_dev *rdev,
96 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000097static int _regulator_do_set_voltage(struct regulator_dev *rdev,
98 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010099static struct regulator *create_regulator(struct regulator_dev *rdev,
100 struct device *dev,
101 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100102
Mark Brown1083c392009-10-22 16:31:32 +0100103static const char *rdev_get_name(struct regulator_dev *rdev)
104{
105 if (rdev->constraints && rdev->constraints->name)
106 return rdev->constraints->name;
107 else if (rdev->desc->name)
108 return rdev->desc->name;
109 else
110 return "";
111}
112
Liam Girdwood414c70c2008-04-30 15:59:04 +0100113/* gets the regulator for a given consumer device */
114static struct regulator *get_device_regulator(struct device *dev)
115{
116 struct regulator *regulator = NULL;
117 struct regulator_dev *rdev;
118
119 mutex_lock(&regulator_list_mutex);
120 list_for_each_entry(rdev, &regulator_list, list) {
121 mutex_lock(&rdev->mutex);
122 list_for_each_entry(regulator, &rdev->consumer_list, list) {
123 if (regulator->dev == dev) {
124 mutex_unlock(&rdev->mutex);
125 mutex_unlock(&regulator_list_mutex);
126 return regulator;
127 }
128 }
129 mutex_unlock(&rdev->mutex);
130 }
131 mutex_unlock(&regulator_list_mutex);
132 return NULL;
133}
134
Rajendra Nayak69511a42011-11-18 16:47:20 +0530135/**
136 * of_get_regulator - get a regulator device node based on supply name
137 * @dev: Device pointer for the consumer (of regulator) device
138 * @supply: regulator supply name
139 *
140 * Extract the regulator device node corresponding to the supply name.
141 * retruns the device node corresponding to the regulator if found, else
142 * returns NULL.
143 */
144static struct device_node *of_get_regulator(struct device *dev, const char *supply)
145{
146 struct device_node *regnode = NULL;
147 char prop_name[32]; /* 32 is max size of property name */
148
149 dev_dbg(dev, "Looking up %s-supply from device tree\n", supply);
150
151 snprintf(prop_name, 32, "%s-supply", supply);
152 regnode = of_parse_phandle(dev->of_node, prop_name, 0);
153
154 if (!regnode) {
Rajendra Nayak16fbcc32012-03-16 15:50:21 +0530155 dev_dbg(dev, "Looking up %s property in node %s failed",
Rajendra Nayak69511a42011-11-18 16:47:20 +0530156 prop_name, dev->of_node->full_name);
157 return NULL;
158 }
159 return regnode;
160}
161
Liam Girdwood414c70c2008-04-30 15:59:04 +0100162/* Platform voltage constraint check */
163static int regulator_check_voltage(struct regulator_dev *rdev,
164 int *min_uV, int *max_uV)
165{
166 BUG_ON(*min_uV > *max_uV);
167
168 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800169 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100170 return -ENODEV;
171 }
172 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800173 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -EPERM;
175 }
176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 /* check if requested voltage range actually overlaps the constraints */
178 if (*max_uV < rdev->constraints->min_uV ||
179 *min_uV > rdev->constraints->max_uV) {
180 rdev_err(rdev, "requested voltage range [%d, %d] does not fit "
181 "within constraints: [%d, %d]\n", *min_uV, *max_uV,
182 rdev->constraints->min_uV, rdev->constraints->max_uV);
183 return -EINVAL;
184 }
185
Liam Girdwood414c70c2008-04-30 15:59:04 +0100186 if (*max_uV > rdev->constraints->max_uV)
187 *max_uV = rdev->constraints->max_uV;
188 if (*min_uV < rdev->constraints->min_uV)
189 *min_uV = rdev->constraints->min_uV;
190
Mark Brown89f425e2011-07-12 11:20:37 +0900191 if (*min_uV > *max_uV) {
192 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100193 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900195 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196
197 return 0;
198}
199
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100200/* Make sure we select a voltage that suits the needs of all
201 * regulator consumers
202 */
203static int regulator_check_consumers(struct regulator_dev *rdev,
204 int *min_uV, int *max_uV)
205{
206 struct regulator *regulator;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700207 int init_min_uV = *min_uV;
208 int init_max_uV = *max_uV;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100209
210 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700211 /*
212 * Assume consumers that didn't say anything are OK
213 * with anything in the constraint range.
214 */
215 if (!regulator->min_uV && !regulator->max_uV)
216 continue;
217
Steve Mucklef132c6c2012-06-06 18:30:57 -0700218 if (init_max_uV < regulator->min_uV
219 || init_min_uV > regulator->max_uV)
220 rdev_err(rdev, "requested voltage range [%d, %d] does "
221 "not fit within previously voted range: "
222 "[%d, %d]\n", init_min_uV, init_max_uV,
223 regulator->min_uV, regulator->max_uV);
224
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100225 if (*max_uV > regulator->max_uV)
226 *max_uV = regulator->max_uV;
227 if (*min_uV < regulator->min_uV)
228 *min_uV = regulator->min_uV;
229 }
230
231 if (*min_uV > *max_uV)
232 return -EINVAL;
233
234 return 0;
235}
236
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237/* current constraint check */
238static int regulator_check_current_limit(struct regulator_dev *rdev,
239 int *min_uA, int *max_uA)
240{
241 BUG_ON(*min_uA > *max_uA);
242
243 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800244 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245 return -ENODEV;
246 }
247 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800248 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 return -EPERM;
250 }
251
252 if (*max_uA > rdev->constraints->max_uA)
253 *max_uA = rdev->constraints->max_uA;
254 if (*min_uA < rdev->constraints->min_uA)
255 *min_uA = rdev->constraints->min_uA;
256
Mark Brown89f425e2011-07-12 11:20:37 +0900257 if (*min_uA > *max_uA) {
258 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100259 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900261 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100262
263 return 0;
264}
265
266/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900267static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100268{
Mark Brown2c608232011-03-30 06:29:12 +0900269 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800270 case REGULATOR_MODE_FAST:
271 case REGULATOR_MODE_NORMAL:
272 case REGULATOR_MODE_IDLE:
273 case REGULATOR_MODE_STANDBY:
274 break;
275 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900276 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800277 return -EINVAL;
278 }
279
Liam Girdwood414c70c2008-04-30 15:59:04 +0100280 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800281 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282 return -ENODEV;
283 }
284 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800285 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 return -EPERM;
287 }
Mark Brown2c608232011-03-30 06:29:12 +0900288
289 /* The modes are bitmasks, the most power hungry modes having
290 * the lowest values. If the requested mode isn't supported
291 * try higher modes. */
292 while (*mode) {
293 if (rdev->constraints->valid_modes_mask & *mode)
294 return 0;
295 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296 }
Mark Brown2c608232011-03-30 06:29:12 +0900297
298 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299}
300
301/* dynamic regulator mode switching constraint check */
302static int regulator_check_drms(struct regulator_dev *rdev)
303{
304 if (!rdev->constraints) {
Matt Wagantall812a8852012-06-27 14:36:20 -0700305 rdev_dbg(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100306 return -ENODEV;
307 }
308 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Matt Wagantall812a8852012-06-27 14:36:20 -0700309 rdev_dbg(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100310 return -EPERM;
311 }
312 return 0;
313}
314
315static ssize_t device_requested_uA_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
317{
318 struct regulator *regulator;
319
320 regulator = get_device_regulator(dev);
321 if (regulator == NULL)
322 return 0;
323
324 return sprintf(buf, "%d\n", regulator->uA_load);
325}
326
327static ssize_t regulator_uV_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100330 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331 ssize_t ret;
332
333 mutex_lock(&rdev->mutex);
334 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
335 mutex_unlock(&rdev->mutex);
336
337 return ret;
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340
341static ssize_t regulator_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100344 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
347}
David Brownell7ad68e22008-11-11 17:39:02 -0800348static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349
Mark Brownbc558a62008-10-10 15:33:20 +0100350static ssize_t regulator_name_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100354
Mark Brown1083c392009-10-22 16:31:32 +0100355 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100356}
357
David Brownell4fca9542008-11-11 17:38:53 -0800358static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100359{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100360 switch (mode) {
361 case REGULATOR_MODE_FAST:
362 return sprintf(buf, "fast\n");
363 case REGULATOR_MODE_NORMAL:
364 return sprintf(buf, "normal\n");
365 case REGULATOR_MODE_IDLE:
366 return sprintf(buf, "idle\n");
367 case REGULATOR_MODE_STANDBY:
368 return sprintf(buf, "standby\n");
369 }
370 return sprintf(buf, "unknown\n");
371}
372
David Brownell4fca9542008-11-11 17:38:53 -0800373static ssize_t regulator_opmode_show(struct device *dev,
374 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100375{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100376 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
David Brownell4fca9542008-11-11 17:38:53 -0800378 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
379}
David Brownell7ad68e22008-11-11 17:39:02 -0800380static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800381
382static ssize_t regulator_print_state(char *buf, int state)
383{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100384 if (state > 0)
385 return sprintf(buf, "enabled\n");
386 else if (state == 0)
387 return sprintf(buf, "disabled\n");
388 else
389 return sprintf(buf, "unknown\n");
390}
391
David Brownell4fca9542008-11-11 17:38:53 -0800392static ssize_t regulator_state_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100396 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800397
Mark Brown93325462009-08-03 18:49:56 +0100398 mutex_lock(&rdev->mutex);
399 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
400 mutex_unlock(&rdev->mutex);
401
402 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800403}
David Brownell7ad68e22008-11-11 17:39:02 -0800404static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800405
David Brownell853116a2009-01-14 23:03:17 -0800406static ssize_t regulator_status_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
409 struct regulator_dev *rdev = dev_get_drvdata(dev);
410 int status;
411 char *label;
412
413 status = rdev->desc->ops->get_status(rdev);
414 if (status < 0)
415 return status;
416
417 switch (status) {
418 case REGULATOR_STATUS_OFF:
419 label = "off";
420 break;
421 case REGULATOR_STATUS_ON:
422 label = "on";
423 break;
424 case REGULATOR_STATUS_ERROR:
425 label = "error";
426 break;
427 case REGULATOR_STATUS_FAST:
428 label = "fast";
429 break;
430 case REGULATOR_STATUS_NORMAL:
431 label = "normal";
432 break;
433 case REGULATOR_STATUS_IDLE:
434 label = "idle";
435 break;
436 case REGULATOR_STATUS_STANDBY:
437 label = "standby";
438 break;
439 default:
440 return -ERANGE;
441 }
442
443 return sprintf(buf, "%s\n", label);
444}
445static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
446
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447static ssize_t regulator_min_uA_show(struct device *dev,
448 struct device_attribute *attr, char *buf)
449{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100450 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100451
452 if (!rdev->constraints)
453 return sprintf(buf, "constraint not defined\n");
454
455 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
456}
David Brownell7ad68e22008-11-11 17:39:02 -0800457static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
459static ssize_t regulator_max_uA_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100462 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
464 if (!rdev->constraints)
465 return sprintf(buf, "constraint not defined\n");
466
467 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
468}
David Brownell7ad68e22008-11-11 17:39:02 -0800469static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100470
471static ssize_t regulator_min_uV_show(struct device *dev,
472 struct device_attribute *attr, char *buf)
473{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100474 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475
476 if (!rdev->constraints)
477 return sprintf(buf, "constraint not defined\n");
478
479 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
480}
David Brownell7ad68e22008-11-11 17:39:02 -0800481static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_max_uV_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488 if (!rdev->constraints)
489 return sprintf(buf, "constraint not defined\n");
490
491 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
492}
David Brownell7ad68e22008-11-11 17:39:02 -0800493static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494
495static ssize_t regulator_total_uA_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499 struct regulator *regulator;
500 int uA = 0;
501
502 mutex_lock(&rdev->mutex);
503 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100504 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505 mutex_unlock(&rdev->mutex);
506 return sprintf(buf, "%d\n", uA);
507}
David Brownell7ad68e22008-11-11 17:39:02 -0800508static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
510static ssize_t regulator_num_users_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
512{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100513 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514 return sprintf(buf, "%d\n", rdev->use_count);
515}
516
517static ssize_t regulator_type_show(struct device *dev,
518 struct device_attribute *attr, char *buf)
519{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100520 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522 switch (rdev->desc->type) {
523 case REGULATOR_VOLTAGE:
524 return sprintf(buf, "voltage\n");
525 case REGULATOR_CURRENT:
526 return sprintf(buf, "current\n");
527 }
528 return sprintf(buf, "unknown\n");
529}
530
531static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
532 struct device_attribute *attr, char *buf)
533{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100534 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(suspend_mem_microvolts, 0444,
539 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
541static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100544 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
547}
David Brownell7ad68e22008-11-11 17:39:02 -0800548static DEVICE_ATTR(suspend_disk_microvolts, 0444,
549 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
552 struct device_attribute *attr, char *buf)
553{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100554 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
557}
David Brownell7ad68e22008-11-11 17:39:02 -0800558static DEVICE_ATTR(suspend_standby_microvolts, 0444,
559 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100560
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
562 struct device_attribute *attr, char *buf)
563{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100564 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565
David Brownell4fca9542008-11-11 17:38:53 -0800566 return regulator_print_opmode(buf,
567 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568}
David Brownell7ad68e22008-11-11 17:39:02 -0800569static DEVICE_ATTR(suspend_mem_mode, 0444,
570 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571
572static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
573 struct device_attribute *attr, char *buf)
574{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100575 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576
David Brownell4fca9542008-11-11 17:38:53 -0800577 return regulator_print_opmode(buf,
578 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579}
David Brownell7ad68e22008-11-11 17:39:02 -0800580static DEVICE_ATTR(suspend_disk_mode, 0444,
581 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
584 struct device_attribute *attr, char *buf)
585{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100586 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587
David Brownell4fca9542008-11-11 17:38:53 -0800588 return regulator_print_opmode(buf,
589 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590}
David Brownell7ad68e22008-11-11 17:39:02 -0800591static DEVICE_ATTR(suspend_standby_mode, 0444,
592 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593
594static ssize_t regulator_suspend_mem_state_show(struct device *dev,
595 struct device_attribute *attr, char *buf)
596{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100597 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100598
David Brownell4fca9542008-11-11 17:38:53 -0800599 return regulator_print_state(buf,
600 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100601}
David Brownell7ad68e22008-11-11 17:39:02 -0800602static DEVICE_ATTR(suspend_mem_state, 0444,
603 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
605static ssize_t regulator_suspend_disk_state_show(struct device *dev,
606 struct device_attribute *attr, char *buf)
607{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100608 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609
David Brownell4fca9542008-11-11 17:38:53 -0800610 return regulator_print_state(buf,
611 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612}
David Brownell7ad68e22008-11-11 17:39:02 -0800613static DEVICE_ATTR(suspend_disk_state, 0444,
614 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615
616static ssize_t regulator_suspend_standby_state_show(struct device *dev,
617 struct device_attribute *attr, char *buf)
618{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100619 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
David Brownell4fca9542008-11-11 17:38:53 -0800621 return regulator_print_state(buf,
622 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623}
David Brownell7ad68e22008-11-11 17:39:02 -0800624static DEVICE_ATTR(suspend_standby_state, 0444,
625 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100626
David Brownell7ad68e22008-11-11 17:39:02 -0800627
628/*
629 * These are the only attributes are present for all regulators.
630 * Other attributes are a function of regulator functionality.
631 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100632static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100633 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100634 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
635 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636 __ATTR_NULL,
637};
638
639static void regulator_dev_release(struct device *dev)
640{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100641 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100642 kfree(rdev);
643}
644
645static struct class regulator_class = {
646 .name = "regulator",
647 .dev_release = regulator_dev_release,
648 .dev_attrs = regulator_dev_attrs,
649};
650
651/* Calculate the new optimum regulator operating mode based on the new total
652 * consumer load. All locks held by caller */
653static void drms_uA_update(struct regulator_dev *rdev)
654{
655 struct regulator *sibling;
656 int current_uA = 0, output_uV, input_uV, err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 unsigned int regulator_curr_mode, mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100658
659 err = regulator_check_drms(rdev);
660 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000661 (!rdev->desc->ops->get_voltage &&
662 !rdev->desc->ops->get_voltage_sel) ||
663 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300664 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100665
666 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000667 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 if (output_uV <= 0)
669 return;
670
671 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000672 input_uV = 0;
673 if (rdev->supply)
674 input_uV = _regulator_get_voltage(rdev);
675 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 input_uV = rdev->constraints->input_uV;
677 if (input_uV <= 0)
678 return;
679
680 /* calc total requested load */
681 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100682 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683
684 /* now get the optimum mode for our new total regulator load */
685 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
686 output_uV, current_uA);
687
688 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900689 err = regulator_mode_constrain(rdev, &mode);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 /* return if the same mode is requested */
691 if (rdev->desc->ops->get_mode) {
692 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
693 if (regulator_curr_mode == mode)
694 return;
695 } else
696 return;
697
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698 if (err == 0)
699 rdev->desc->ops->set_mode(rdev, mode);
700}
701
702static int suspend_set_state(struct regulator_dev *rdev,
703 struct regulator_state *rstate)
704{
705 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100706 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100707
Mark Brown638f85c2009-10-22 16:31:33 +0100708 can_set_state = rdev->desc->ops->set_suspend_enable &&
709 rdev->desc->ops->set_suspend_disable;
710
711 /* If we have no suspend mode configration don't set anything;
712 * only warn if the driver actually makes the suspend mode
713 * configurable.
714 */
715 if (!rstate->enabled && !rstate->disabled) {
716 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800717 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100718 return 0;
719 }
720
721 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800722 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100723 return -EINVAL;
724 }
725
726 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800727 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100729 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100730
731 if (rstate->enabled)
732 ret = rdev->desc->ops->set_suspend_enable(rdev);
733 else
734 ret = rdev->desc->ops->set_suspend_disable(rdev);
735 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800736 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100737 return ret;
738 }
739
740 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
741 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
742 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800743 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100744 return ret;
745 }
746 }
747
748 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
749 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
750 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800751 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100752 return ret;
753 }
754 }
755 return ret;
756}
757
758/* locks held by caller */
759static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
760{
761 if (!rdev->constraints)
762 return -EINVAL;
763
764 switch (state) {
765 case PM_SUSPEND_STANDBY:
766 return suspend_set_state(rdev,
767 &rdev->constraints->state_standby);
768 case PM_SUSPEND_MEM:
769 return suspend_set_state(rdev,
770 &rdev->constraints->state_mem);
771 case PM_SUSPEND_MAX:
772 return suspend_set_state(rdev,
773 &rdev->constraints->state_disk);
774 default:
775 return -EINVAL;
776 }
777}
778
779static void print_constraints(struct regulator_dev *rdev)
780{
781 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000782 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100783 int count = 0;
784 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100785
Mark Brown8f031b42009-10-22 16:31:31 +0100786 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100787 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100788 count += sprintf(buf + count, "%d mV ",
789 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100790 else
Mark Brown8f031b42009-10-22 16:31:31 +0100791 count += sprintf(buf + count, "%d <--> %d mV ",
792 constraints->min_uV / 1000,
793 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100794 }
Mark Brown8f031b42009-10-22 16:31:31 +0100795
796 if (!constraints->min_uV ||
797 constraints->min_uV != constraints->max_uV) {
798 ret = _regulator_get_voltage(rdev);
799 if (ret > 0)
800 count += sprintf(buf + count, "at %d mV ", ret / 1000);
801 }
802
Mark Brownbf5892a2011-05-08 22:13:37 +0100803 if (constraints->uV_offset)
804 count += sprintf(buf, "%dmV offset ",
805 constraints->uV_offset / 1000);
806
Mark Brown8f031b42009-10-22 16:31:31 +0100807 if (constraints->min_uA && constraints->max_uA) {
808 if (constraints->min_uA == constraints->max_uA)
809 count += sprintf(buf + count, "%d mA ",
810 constraints->min_uA / 1000);
811 else
812 count += sprintf(buf + count, "%d <--> %d mA ",
813 constraints->min_uA / 1000,
814 constraints->max_uA / 1000);
815 }
816
817 if (!constraints->min_uA ||
818 constraints->min_uA != constraints->max_uA) {
819 ret = _regulator_get_current_limit(rdev);
820 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400821 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100822 }
823
Liam Girdwood414c70c2008-04-30 15:59:04 +0100824 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
825 count += sprintf(buf + count, "fast ");
826 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
827 count += sprintf(buf + count, "normal ");
828 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
829 count += sprintf(buf + count, "idle ");
830 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
831 count += sprintf(buf + count, "standby");
832
Mark Brown13ce29f2010-12-17 16:04:12 +0000833 rdev_info(rdev, "%s\n", buf);
Mark Brown4a682922012-02-09 13:26:13 +0000834
835 if ((constraints->min_uV != constraints->max_uV) &&
836 !(constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE))
837 rdev_warn(rdev,
838 "Voltage range but no REGULATOR_CHANGE_VOLTAGE\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100839}
840
Mark Browne79055d2009-10-19 15:53:50 +0100841static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100842 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100843{
844 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100845 int ret;
846
847 /* do we need to apply the constraint voltage */
848 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000849 rdev->constraints->min_uV == rdev->constraints->max_uV) {
850 ret = _regulator_do_set_voltage(rdev,
851 rdev->constraints->min_uV,
852 rdev->constraints->max_uV);
853 if (ret < 0) {
854 rdev_err(rdev, "failed to apply %duV constraint\n",
855 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000856 return ret;
857 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100858 }
Mark Browne79055d2009-10-19 15:53:50 +0100859
860 /* constrain machine-level voltage specs to fit
861 * the actual range supported by this regulator.
862 */
863 if (ops->list_voltage && rdev->desc->n_voltages) {
864 int count = rdev->desc->n_voltages;
865 int i;
866 int min_uV = INT_MAX;
867 int max_uV = INT_MIN;
868 int cmin = constraints->min_uV;
869 int cmax = constraints->max_uV;
870
871 /* it's safe to autoconfigure fixed-voltage supplies
872 and the constraints are used by list_voltage. */
873 if (count == 1 && !cmin) {
874 cmin = 1;
875 cmax = INT_MAX;
876 constraints->min_uV = cmin;
877 constraints->max_uV = cmax;
878 }
879
880 /* voltage constraints are optional */
881 if ((cmin == 0) && (cmax == 0))
882 return 0;
883
884 /* else require explicit machine-level constraints */
885 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800886 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100887 return -EINVAL;
888 }
889
890 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
891 for (i = 0; i < count; i++) {
892 int value;
893
894 value = ops->list_voltage(rdev, i);
895 if (value <= 0)
896 continue;
897
898 /* maybe adjust [min_uV..max_uV] */
899 if (value >= cmin && value < min_uV)
900 min_uV = value;
901 if (value <= cmax && value > max_uV)
902 max_uV = value;
903 }
904
905 /* final: [min_uV..max_uV] valid iff constraints valid */
906 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800907 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100908 return -EINVAL;
909 }
910
911 /* use regulator's subset of machine constraints */
912 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800913 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
914 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100915 constraints->min_uV = min_uV;
916 }
917 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800918 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
919 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100920 constraints->max_uV = max_uV;
921 }
922 }
923
924 return 0;
925}
926
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927/**
928 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000929 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000930 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931 *
932 * Allows platform initialisation code to define and constrain
933 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
934 * Constraints *must* be set by platform code in order for some
935 * regulator operations to proceed i.e. set_voltage, set_current_limit,
936 * set_mode.
937 */
938static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000939 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100940{
941 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100942 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100943
Mark Brown9a8f5e02011-11-29 18:11:19 +0000944 if (constraints)
945 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
946 GFP_KERNEL);
947 else
948 rdev->constraints = kzalloc(sizeof(*constraints),
949 GFP_KERNEL);
Mark Brownf8c12fe2010-11-29 15:55:17 +0000950 if (!rdev->constraints)
951 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100952
Mark Brownf8c12fe2010-11-29 15:55:17 +0000953 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100954 if (ret != 0)
955 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800956
Liam Girdwooda5766f12008-10-10 13:22:20 +0100957 /* do we need to setup our suspend state */
Mark Brown9a8f5e02011-11-29 18:11:19 +0000958 if (rdev->constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000959 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100960 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800961 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100962 goto out;
963 }
964 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100965
Mark Brown9a8f5e02011-11-29 18:11:19 +0000966 if (rdev->constraints->initial_mode) {
Mark Browna3084662009-02-26 19:24:19 +0000967 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800968 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000969 ret = -EINVAL;
970 goto out;
971 }
972
Mark Brownf8c12fe2010-11-29 15:55:17 +0000973 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000974 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800975 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000976 goto out;
977 }
978 }
979
Mark Browncacf90f2009-03-02 16:32:46 +0000980 /* If the constraints say the regulator should be on at this point
981 * and we have control then make sure it is enabled.
982 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000983 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
984 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100985 ret = ops->enable(rdev);
986 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800987 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100988 goto out;
989 }
990 }
991
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 if (!suppress_info_printing)
993 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800994 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800996 kfree(rdev->constraints);
997 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100998 return ret;
999}
1000
1001/**
1002 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +00001003 * @rdev: regulator name
1004 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +01001005 *
1006 * Called by platform initialisation code to set the supply regulator for this
1007 * regulator. This ensures that a regulators supply will also be enabled by the
1008 * core if it's child is enabled.
1009 */
1010static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +01001011 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001012{
1013 int err;
1014
Steve Mucklef132c6c2012-06-06 18:30:57 -07001015 if (!suppress_info_printing)
1016 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
Mark Brown3801b862011-06-09 16:22:22 +01001017
1018 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
Axel Lin32c78de2011-12-29 17:03:20 +08001019 if (rdev->supply == NULL) {
1020 err = -ENOMEM;
Mark Brown3801b862011-06-09 16:22:22 +01001021 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001022 }
Mark Brown3801b862011-06-09 16:22:22 +01001023
1024 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001025}
1026
1027/**
Randy Dunlap06c63f92010-11-18 15:02:26 -08001028 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +00001029 * @rdev: regulator source
Mark Brown40f92442009-06-17 17:56:39 +01001030 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +00001031 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +01001032 *
1033 * Allows platform initialisation code to map physical regulator
1034 * sources to symbolic names for supplies for use by devices. Devices
1035 * should use these symbolic names to request regulators, avoiding the
1036 * need to provide board-specific regulator names as platform data.
1037 */
1038static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown737f3602012-02-02 00:10:51 +00001039 const char *consumer_dev_name,
1040 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +01001041{
1042 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +01001043 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001044
1045 if (supply == NULL)
1046 return -EINVAL;
1047
Mark Brown9ed20992009-07-21 16:00:26 +01001048 if (consumer_dev_name != NULL)
1049 has_dev = 1;
1050 else
1051 has_dev = 0;
1052
David Brownell6001e132008-12-31 12:54:19 +00001053 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001054 if (node->dev_name && consumer_dev_name) {
1055 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1056 continue;
1057 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001058 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001059 }
1060
David Brownell6001e132008-12-31 12:54:19 +00001061 if (strcmp(node->supply, supply) != 0)
1062 continue;
1063
Mark Brown737f3602012-02-02 00:10:51 +00001064 pr_debug("%s: %s/%s is '%s' supply; fail %s/%s\n",
1065 consumer_dev_name,
1066 dev_name(&node->regulator->dev),
1067 node->regulator->desc->name,
1068 supply,
1069 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001070 return -EBUSY;
1071 }
1072
Mark Brown9ed20992009-07-21 16:00:26 +01001073 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001074 if (node == NULL)
1075 return -ENOMEM;
1076
1077 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001078 node->supply = supply;
1079
Mark Brown9ed20992009-07-21 16:00:26 +01001080 if (has_dev) {
1081 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1082 if (node->dev_name == NULL) {
1083 kfree(node);
1084 return -ENOMEM;
1085 }
Mark Brown40f92442009-06-17 17:56:39 +01001086 }
1087
Liam Girdwooda5766f12008-10-10 13:22:20 +01001088 list_add(&node->list, &regulator_map_list);
1089 return 0;
1090}
1091
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001092static void unset_regulator_supplies(struct regulator_dev *rdev)
1093{
1094 struct regulator_map *node, *n;
1095
1096 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1097 if (rdev == node->regulator) {
1098 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001099 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001100 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001101 }
1102 }
1103}
1104
Mark Brownf5726ae2011-06-09 16:22:20 +01001105#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106
1107static struct regulator *create_regulator(struct regulator_dev *rdev,
1108 struct device *dev,
1109 const char *supply_name)
1110{
1111 struct regulator *regulator;
1112 char buf[REG_STR_SIZE];
1113 int err, size;
1114
1115 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1116 if (regulator == NULL)
1117 return NULL;
1118
1119 mutex_lock(&rdev->mutex);
1120 regulator->rdev = rdev;
1121 list_add(&regulator->list, &rdev->consumer_list);
1122
1123 if (dev) {
1124 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001125 size = scnprintf(buf, REG_STR_SIZE,
1126 "microamps_requested_%s-%s",
1127 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128 if (size >= REG_STR_SIZE)
1129 goto overflow_err;
1130
1131 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001132 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001133 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1134 if (regulator->dev_attr.attr.name == NULL)
1135 goto attr_name_err;
1136
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 regulator->dev_attr.attr.mode = 0444;
1138 regulator->dev_attr.show = device_requested_uA_show;
1139 err = device_create_file(dev, &regulator->dev_attr);
1140 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001141 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001142 goto attr_name_err;
1143 }
1144
1145 /* also add a link to the device sysfs entry */
1146 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1147 dev->kobj.name, supply_name);
1148 if (size >= REG_STR_SIZE)
1149 goto attr_err;
1150
1151 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1152 if (regulator->supply_name == NULL)
1153 goto attr_err;
1154
1155 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1156 buf);
1157 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001158 rdev_warn(rdev, "could not add device link %s err %d\n",
1159 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160 goto link_name_err;
1161 }
Mark Brown5de70512011-06-19 13:33:16 +01001162 } else {
1163 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1164 if (regulator->supply_name == NULL)
1165 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001166 }
Mark Brown5de70512011-06-19 13:33:16 +01001167
Mark Brown5de70512011-06-19 13:33:16 +01001168 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1169 rdev->debugfs);
Stephen Boyd24751432012-02-20 22:50:42 -08001170 if (!regulator->debugfs) {
Mark Brown5de70512011-06-19 13:33:16 +01001171 rdev_warn(rdev, "Failed to create debugfs directory\n");
Mark Brown5de70512011-06-19 13:33:16 +01001172 } else {
1173 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1174 &regulator->uA_load);
1175 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1176 &regulator->min_uV);
1177 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1178 &regulator->max_uV);
1179 }
Mark Brown5de70512011-06-19 13:33:16 +01001180
Liam Girdwood414c70c2008-04-30 15:59:04 +01001181 mutex_unlock(&rdev->mutex);
1182 return regulator;
1183link_name_err:
1184 kfree(regulator->supply_name);
1185attr_err:
1186 device_remove_file(regulator->dev, &regulator->dev_attr);
1187attr_name_err:
1188 kfree(regulator->dev_attr.attr.name);
1189overflow_err:
1190 list_del(&regulator->list);
1191 kfree(regulator);
1192 mutex_unlock(&rdev->mutex);
1193 return NULL;
1194}
1195
Mark Brown31aae2b2009-12-21 12:21:52 +00001196static int _regulator_get_enable_time(struct regulator_dev *rdev)
1197{
1198 if (!rdev->desc->ops->enable_time)
1199 return 0;
1200 return rdev->desc->ops->enable_time(rdev);
1201}
1202
Rajendra Nayak69511a42011-11-18 16:47:20 +05301203static struct regulator_dev *regulator_dev_lookup(struct device *dev,
1204 const char *supply)
1205{
1206 struct regulator_dev *r;
1207 struct device_node *node;
1208
1209 /* first do a dt based lookup */
1210 if (dev && dev->of_node) {
1211 node = of_get_regulator(dev, supply);
1212 if (node)
1213 list_for_each_entry(r, &regulator_list, list)
1214 if (r->dev.parent &&
1215 node == r->dev.of_node)
1216 return r;
1217 }
1218
1219 /* if not found, try doing it non-dt way */
1220 list_for_each_entry(r, &regulator_list, list)
1221 if (strcmp(rdev_get_name(r), supply) == 0)
1222 return r;
1223
1224 return NULL;
1225}
1226
Mark Brown5ffbd132009-07-21 16:00:23 +01001227/* Internal regulator request function */
1228static struct regulator *_regulator_get(struct device *dev, const char *id,
1229 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230{
1231 struct regulator_dev *rdev;
1232 struct regulator_map *map;
Mark Brown04bf3012012-03-11 13:07:56 +00001233 struct regulator *regulator = ERR_PTR(-EPROBE_DEFER);
Mark Brown40f92442009-06-17 17:56:39 +01001234 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001235 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001236
1237 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001238 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 return regulator;
1240 }
1241
Mark Brown40f92442009-06-17 17:56:39 +01001242 if (dev)
1243 devname = dev_name(dev);
1244
Liam Girdwood414c70c2008-04-30 15:59:04 +01001245 mutex_lock(&regulator_list_mutex);
1246
Rajendra Nayak69511a42011-11-18 16:47:20 +05301247 rdev = regulator_dev_lookup(dev, id);
1248 if (rdev)
1249 goto found;
1250
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001252 /* If the mapping has a device set up it must match */
1253 if (map->dev_name &&
1254 (!devname || strcmp(map->dev_name, devname)))
1255 continue;
1256
1257 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001258 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001260 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001261 }
Mark Brown34abbd62010-02-12 10:18:08 +00001262
Mark Brown688fe992010-10-05 19:18:32 -07001263 if (board_wants_dummy_regulator) {
1264 rdev = dummy_regulator_rdev;
1265 goto found;
1266 }
1267
Mark Brown34abbd62010-02-12 10:18:08 +00001268#ifdef CONFIG_REGULATOR_DUMMY
1269 if (!devname)
1270 devname = "deviceless";
1271
1272 /* If the board didn't flag that it was fully constrained then
1273 * substitute in a dummy regulator so consumers can continue.
1274 */
1275 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001276 pr_warn("%s supply %s not found, using dummy regulator\n",
1277 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001278 rdev = dummy_regulator_rdev;
1279 goto found;
1280 }
1281#endif
1282
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283 mutex_unlock(&regulator_list_mutex);
1284 return regulator;
1285
1286found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001287 if (rdev->exclusive) {
1288 regulator = ERR_PTR(-EPERM);
1289 goto out;
1290 }
1291
1292 if (exclusive && rdev->open_count) {
1293 regulator = ERR_PTR(-EBUSY);
1294 goto out;
1295 }
1296
Liam Girdwooda5766f12008-10-10 13:22:20 +01001297 if (!try_module_get(rdev->owner))
1298 goto out;
1299
Liam Girdwood414c70c2008-04-30 15:59:04 +01001300 regulator = create_regulator(rdev, dev, id);
1301 if (regulator == NULL) {
1302 regulator = ERR_PTR(-ENOMEM);
1303 module_put(rdev->owner);
Axel Linbcda4322011-12-29 17:02:08 +08001304 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001305 }
1306
Mark Brown5ffbd132009-07-21 16:00:23 +01001307 rdev->open_count++;
1308 if (exclusive) {
1309 rdev->exclusive = 1;
1310
1311 ret = _regulator_is_enabled(rdev);
1312 if (ret > 0)
1313 rdev->use_count = 1;
1314 else
1315 rdev->use_count = 0;
1316 }
1317
Liam Girdwooda5766f12008-10-10 13:22:20 +01001318out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001319 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001320
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321 return regulator;
1322}
Mark Brown5ffbd132009-07-21 16:00:23 +01001323
1324/**
1325 * regulator_get - lookup and obtain a reference to a regulator.
1326 * @dev: device for regulator "consumer"
1327 * @id: Supply name or regulator ID.
1328 *
1329 * Returns a struct regulator corresponding to the regulator producer,
1330 * or IS_ERR() condition containing errno.
1331 *
1332 * Use of supply names configured via regulator_set_device_supply() is
1333 * strongly encouraged. It is recommended that the supply name used
1334 * should match the name used for the supply and/or the relevant
1335 * device pins in the datasheet.
1336 */
1337struct regulator *regulator_get(struct device *dev, const char *id)
1338{
1339 return _regulator_get(dev, id, 0);
1340}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341EXPORT_SYMBOL_GPL(regulator_get);
1342
Stephen Boyd070b9072012-01-16 19:39:58 -08001343static void devm_regulator_release(struct device *dev, void *res)
1344{
1345 regulator_put(*(struct regulator **)res);
1346}
1347
1348/**
1349 * devm_regulator_get - Resource managed regulator_get()
1350 * @dev: device for regulator "consumer"
1351 * @id: Supply name or regulator ID.
1352 *
1353 * Managed regulator_get(). Regulators returned from this function are
1354 * automatically regulator_put() on driver detach. See regulator_get() for more
1355 * information.
1356 */
1357struct regulator *devm_regulator_get(struct device *dev, const char *id)
1358{
1359 struct regulator **ptr, *regulator;
1360
1361 ptr = devres_alloc(devm_regulator_release, sizeof(*ptr), GFP_KERNEL);
1362 if (!ptr)
1363 return ERR_PTR(-ENOMEM);
1364
1365 regulator = regulator_get(dev, id);
1366 if (!IS_ERR(regulator)) {
1367 *ptr = regulator;
1368 devres_add(dev, ptr);
1369 } else {
1370 devres_free(ptr);
1371 }
1372
1373 return regulator;
1374}
1375EXPORT_SYMBOL_GPL(devm_regulator_get);
1376
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001378 * regulator_get_exclusive - obtain exclusive access to a regulator.
1379 * @dev: device for regulator "consumer"
1380 * @id: Supply name or regulator ID.
1381 *
1382 * Returns a struct regulator corresponding to the regulator producer,
1383 * or IS_ERR() condition containing errno. Other consumers will be
1384 * unable to obtain this reference is held and the use count for the
1385 * regulator will be initialised to reflect the current state of the
1386 * regulator.
1387 *
1388 * This is intended for use by consumers which cannot tolerate shared
1389 * use of the regulator such as those which need to force the
1390 * regulator off for correct operation of the hardware they are
1391 * controlling.
1392 *
1393 * Use of supply names configured via regulator_set_device_supply() is
1394 * strongly encouraged. It is recommended that the supply name used
1395 * should match the name used for the supply and/or the relevant
1396 * device pins in the datasheet.
1397 */
1398struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1399{
1400 return _regulator_get(dev, id, 1);
1401}
1402EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1403
1404/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 * regulator_put - "free" the regulator source
1406 * @regulator: regulator source
1407 *
1408 * Note: drivers must ensure that all regulator_enable calls made on this
1409 * regulator source are balanced by regulator_disable calls prior to calling
1410 * this function.
1411 */
1412void regulator_put(struct regulator *regulator)
1413{
1414 struct regulator_dev *rdev;
1415
1416 if (regulator == NULL || IS_ERR(regulator))
1417 return;
1418
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 mutex_lock(&regulator_list_mutex);
1420 rdev = regulator->rdev;
1421
Mark Brown5de70512011-06-19 13:33:16 +01001422 debugfs_remove_recursive(regulator->debugfs);
Mark Brown5de70512011-06-19 13:33:16 +01001423
Liam Girdwood414c70c2008-04-30 15:59:04 +01001424 /* remove any sysfs entries */
1425 if (regulator->dev) {
1426 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001427 device_remove_file(regulator->dev, &regulator->dev_attr);
1428 kfree(regulator->dev_attr.attr.name);
1429 }
Mark Brown5de70512011-06-19 13:33:16 +01001430 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 list_del(&regulator->list);
1432 kfree(regulator);
1433
Mark Brown5ffbd132009-07-21 16:00:23 +01001434 rdev->open_count--;
1435 rdev->exclusive = 0;
1436
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 module_put(rdev->owner);
1438 mutex_unlock(&regulator_list_mutex);
1439}
1440EXPORT_SYMBOL_GPL(regulator_put);
1441
Mark Brownd5ad34f2012-01-20 20:09:18 +00001442static int devm_regulator_match(struct device *dev, void *res, void *data)
1443{
1444 struct regulator **r = res;
1445 if (!r || !*r) {
1446 WARN_ON(!r || !*r);
1447 return 0;
1448 }
1449 return *r == data;
1450}
1451
1452/**
1453 * devm_regulator_put - Resource managed regulator_put()
1454 * @regulator: regulator to free
1455 *
1456 * Deallocate a regulator allocated with devm_regulator_get(). Normally
1457 * this function will not need to be called and the resource management
1458 * code will ensure that the resource is freed.
1459 */
1460void devm_regulator_put(struct regulator *regulator)
1461{
1462 int rc;
1463
1464 rc = devres_destroy(regulator->dev, devm_regulator_release,
1465 devm_regulator_match, regulator);
Mark Brown968c2c12012-05-07 11:34:52 +01001466 if (rc == 0)
1467 regulator_put(regulator);
1468 else
1469 WARN_ON(rc);
Mark Brownd5ad34f2012-01-20 20:09:18 +00001470}
1471EXPORT_SYMBOL_GPL(devm_regulator_put);
1472
Mark Brown9a2372f2009-08-03 18:49:57 +01001473static int _regulator_can_change_status(struct regulator_dev *rdev)
1474{
1475 if (!rdev->constraints)
1476 return 0;
1477
1478 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1479 return 1;
1480 else
1481 return 0;
1482}
1483
Liam Girdwood414c70c2008-04-30 15:59:04 +01001484/* locks held by regulator_enable() */
1485static int _regulator_enable(struct regulator_dev *rdev)
1486{
Mark Brown31aae2b2009-12-21 12:21:52 +00001487 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488
Liam Girdwood414c70c2008-04-30 15:59:04 +01001489 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001490 if (rdev->constraints &&
1491 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1492 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493
Mark Brown9a2372f2009-08-03 18:49:57 +01001494 if (rdev->use_count == 0) {
1495 /* The regulator may on if it's not switchable or left on */
1496 ret = _regulator_is_enabled(rdev);
1497 if (ret == -EINVAL || ret == 0) {
1498 if (!_regulator_can_change_status(rdev))
1499 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500
Mark Brown31aae2b2009-12-21 12:21:52 +00001501 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001502 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001503
1504 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001505 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001506 ret = _regulator_get_enable_time(rdev);
1507 if (ret >= 0) {
1508 delay = ret;
1509 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001510 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001511 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001512 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001513 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001514
Mark Brown02fa3ec2010-11-10 14:38:30 +00001515 trace_regulator_enable(rdev_get_name(rdev));
1516
Mark Brown31aae2b2009-12-21 12:21:52 +00001517 /* Allow the regulator to ramp; it would be useful
1518 * to extend this for bulk operations so that the
1519 * regulators can ramp together. */
1520 ret = rdev->desc->ops->enable(rdev);
1521 if (ret < 0)
1522 return ret;
1523
Mark Brown02fa3ec2010-11-10 14:38:30 +00001524 trace_regulator_enable_delay(rdev_get_name(rdev));
1525
Axel Line36c1df2010-11-05 21:51:32 +08001526 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001527 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001528 udelay(delay % 1000);
1529 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001530 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001531 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001532
Mark Brown02fa3ec2010-11-10 14:38:30 +00001533 trace_regulator_enable_complete(rdev_get_name(rdev));
1534
Linus Walleija7433cf2009-08-26 12:54:04 +02001535 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001536 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001537 return ret;
1538 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001539 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540 }
1541
Mark Brown9a2372f2009-08-03 18:49:57 +01001542 rdev->use_count++;
1543
1544 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001545}
1546
1547/**
1548 * regulator_enable - enable regulator output
1549 * @regulator: regulator source
1550 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001551 * Request that the regulator be enabled with the regulator output at
1552 * the predefined voltage or current value. Calls to regulator_enable()
1553 * must be balanced with calls to regulator_disable().
1554 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001556 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001557 */
1558int regulator_enable(struct regulator *regulator)
1559{
David Brownell412aec62008-11-16 11:44:46 -08001560 struct regulator_dev *rdev = regulator->rdev;
1561 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001562
Mark Brown3801b862011-06-09 16:22:22 +01001563 if (rdev->supply) {
1564 ret = regulator_enable(rdev->supply);
1565 if (ret != 0)
1566 return ret;
1567 }
1568
David Brownell412aec62008-11-16 11:44:46 -08001569 mutex_lock(&rdev->mutex);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001570
David Brownellcd94b502009-03-11 16:43:34 -08001571 ret = _regulator_enable(rdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001572 if (ret == 0)
1573 regulator->enabled++;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574
David Brownell412aec62008-11-16 11:44:46 -08001575 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001576
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001577 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001578 regulator_disable(rdev->supply);
1579
Liam Girdwood414c70c2008-04-30 15:59:04 +01001580 return ret;
1581}
1582EXPORT_SYMBOL_GPL(regulator_enable);
1583
1584/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001585static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001586{
1587 int ret = 0;
1588
David Brownellcd94b502009-03-11 16:43:34 -08001589 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001590 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001591 return -EIO;
1592
Liam Girdwood414c70c2008-04-30 15:59:04 +01001593 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001594 if (rdev->use_count == 1 &&
1595 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001596
1597 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001598 if (_regulator_can_change_status(rdev) &&
1599 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001600 trace_regulator_disable(rdev_get_name(rdev));
1601
Liam Girdwood414c70c2008-04-30 15:59:04 +01001602 ret = rdev->desc->ops->disable(rdev);
1603 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001604 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001605 return ret;
1606 }
Mark Brown84b68262009-12-01 21:12:27 +00001607
Mark Brown02fa3ec2010-11-10 14:38:30 +00001608 trace_regulator_disable_complete(rdev_get_name(rdev));
1609
Mark Brown84b68262009-12-01 21:12:27 +00001610 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1611 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001612 }
1613
Liam Girdwood414c70c2008-04-30 15:59:04 +01001614 rdev->use_count = 0;
1615 } else if (rdev->use_count > 1) {
1616
1617 if (rdev->constraints &&
1618 (rdev->constraints->valid_ops_mask &
1619 REGULATOR_CHANGE_DRMS))
1620 drms_uA_update(rdev);
1621
1622 rdev->use_count--;
1623 }
Mark Brown3801b862011-06-09 16:22:22 +01001624
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625 return ret;
1626}
1627
1628/**
1629 * regulator_disable - disable regulator output
1630 * @regulator: regulator source
1631 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001632 * Disable the regulator output voltage or current. Calls to
1633 * regulator_enable() must be balanced with calls to
1634 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001635 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001636 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001637 * devices have it enabled, the regulator device supports disabling and
1638 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001639 */
1640int regulator_disable(struct regulator *regulator)
1641{
David Brownell412aec62008-11-16 11:44:46 -08001642 struct regulator_dev *rdev = regulator->rdev;
1643 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001644
David Brownell412aec62008-11-16 11:44:46 -08001645 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001646 ret = _regulator_disable(rdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001647 if (ret == 0)
1648 regulator->enabled--;
David Brownell412aec62008-11-16 11:44:46 -08001649 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001650
Mark Brown3801b862011-06-09 16:22:22 +01001651 if (ret == 0 && rdev->supply)
1652 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001653
Liam Girdwood414c70c2008-04-30 15:59:04 +01001654 return ret;
1655}
1656EXPORT_SYMBOL_GPL(regulator_disable);
1657
1658/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001659static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001660{
1661 int ret = 0;
1662
1663 /* force disable */
1664 if (rdev->desc->ops->disable) {
1665 /* ah well, who wants to live forever... */
1666 ret = rdev->desc->ops->disable(rdev);
1667 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001668 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669 return ret;
1670 }
1671 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001672 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1673 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 }
1675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676 return ret;
1677}
1678
1679/**
1680 * regulator_force_disable - force disable regulator output
1681 * @regulator: regulator source
1682 *
1683 * Forcibly disable the regulator output voltage or current.
1684 * NOTE: this *will* disable the regulator output even if other consumer
1685 * devices have it enabled. This should be used for situations when device
1686 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1687 */
1688int regulator_force_disable(struct regulator *regulator)
1689{
Mark Brown82d15832011-05-09 11:41:02 +02001690 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001691 int ret;
1692
Mark Brown82d15832011-05-09 11:41:02 +02001693 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001694 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001695 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001696 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001697
Mark Brown3801b862011-06-09 16:22:22 +01001698 if (rdev->supply)
1699 while (rdev->open_count--)
1700 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001701
Liam Girdwood414c70c2008-04-30 15:59:04 +01001702 return ret;
1703}
1704EXPORT_SYMBOL_GPL(regulator_force_disable);
1705
Mark Brownda07ecd2011-09-11 09:53:50 +01001706static void regulator_disable_work(struct work_struct *work)
1707{
1708 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1709 disable_work.work);
1710 int count, i, ret;
1711
1712 mutex_lock(&rdev->mutex);
1713
1714 BUG_ON(!rdev->deferred_disables);
1715
1716 count = rdev->deferred_disables;
1717 rdev->deferred_disables = 0;
1718
1719 for (i = 0; i < count; i++) {
1720 ret = _regulator_disable(rdev);
1721 if (ret != 0)
1722 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1723 }
1724
1725 mutex_unlock(&rdev->mutex);
1726
1727 if (rdev->supply) {
1728 for (i = 0; i < count; i++) {
1729 ret = regulator_disable(rdev->supply);
1730 if (ret != 0) {
1731 rdev_err(rdev,
1732 "Supply disable failed: %d\n", ret);
1733 }
1734 }
1735 }
1736}
1737
1738/**
1739 * regulator_disable_deferred - disable regulator output with delay
1740 * @regulator: regulator source
1741 * @ms: miliseconds until the regulator is disabled
1742 *
1743 * Execute regulator_disable() on the regulator after a delay. This
1744 * is intended for use with devices that require some time to quiesce.
1745 *
1746 * NOTE: this will only disable the regulator output if no other consumer
1747 * devices have it enabled, the regulator device supports disabling and
1748 * machine constraints permit this operation.
1749 */
1750int regulator_disable_deferred(struct regulator *regulator, int ms)
1751{
1752 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001753 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001754
1755 mutex_lock(&rdev->mutex);
1756 rdev->deferred_disables++;
1757 mutex_unlock(&rdev->mutex);
1758
Mark Brownaa598022011-10-03 22:42:43 +01001759 ret = schedule_delayed_work(&rdev->disable_work,
1760 msecs_to_jiffies(ms));
1761 if (ret < 0)
1762 return ret;
1763 else
1764 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001765}
1766EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1767
Liam Girdwood414c70c2008-04-30 15:59:04 +01001768static int _regulator_is_enabled(struct regulator_dev *rdev)
1769{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001770 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001771 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001772 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001773
Mark Brown93325462009-08-03 18:49:56 +01001774 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775}
1776
1777/**
1778 * regulator_is_enabled - is the regulator output enabled
1779 * @regulator: regulator source
1780 *
David Brownell412aec62008-11-16 11:44:46 -08001781 * Returns positive if the regulator driver backing the source/client
1782 * has requested that the device be enabled, zero if it hasn't, else a
1783 * negative errno code.
1784 *
1785 * Note that the device backing this regulator handle can have multiple
1786 * users, so it might be enabled even if regulator_enable() was never
1787 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001788 */
1789int regulator_is_enabled(struct regulator *regulator)
1790{
Mark Brown93325462009-08-03 18:49:56 +01001791 int ret;
1792
1793 mutex_lock(&regulator->rdev->mutex);
1794 ret = _regulator_is_enabled(regulator->rdev);
1795 mutex_unlock(&regulator->rdev->mutex);
1796
1797 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001798}
1799EXPORT_SYMBOL_GPL(regulator_is_enabled);
1800
1801/**
David Brownell4367cfd2009-02-26 11:48:36 -08001802 * regulator_count_voltages - count regulator_list_voltage() selectors
1803 * @regulator: regulator source
1804 *
1805 * Returns number of selectors, or negative errno. Selectors are
1806 * numbered starting at zero, and typically correspond to bitfields
1807 * in hardware registers.
1808 */
1809int regulator_count_voltages(struct regulator *regulator)
1810{
1811 struct regulator_dev *rdev = regulator->rdev;
1812
1813 return rdev->desc->n_voltages ? : -EINVAL;
1814}
1815EXPORT_SYMBOL_GPL(regulator_count_voltages);
1816
1817/**
1818 * regulator_list_voltage - enumerate supported voltages
1819 * @regulator: regulator source
1820 * @selector: identify voltage to list
1821 * Context: can sleep
1822 *
1823 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001824 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001825 * negative errno.
1826 */
1827int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1828{
1829 struct regulator_dev *rdev = regulator->rdev;
1830 struct regulator_ops *ops = rdev->desc->ops;
1831 int ret;
1832
1833 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1834 return -EINVAL;
1835
1836 mutex_lock(&rdev->mutex);
1837 ret = ops->list_voltage(rdev, selector);
1838 mutex_unlock(&rdev->mutex);
1839
1840 if (ret > 0) {
1841 if (ret < rdev->constraints->min_uV)
1842 ret = 0;
1843 else if (ret > rdev->constraints->max_uV)
1844 ret = 0;
1845 }
1846
1847 return ret;
1848}
1849EXPORT_SYMBOL_GPL(regulator_list_voltage);
1850
1851/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001852 * regulator_is_supported_voltage - check if a voltage range can be supported
1853 *
1854 * @regulator: Regulator to check.
1855 * @min_uV: Minimum required voltage in uV.
1856 * @max_uV: Maximum required voltage in uV.
1857 *
1858 * Returns a boolean or a negative error code.
1859 */
1860int regulator_is_supported_voltage(struct regulator *regulator,
1861 int min_uV, int max_uV)
1862{
1863 int i, voltages, ret;
1864
1865 ret = regulator_count_voltages(regulator);
1866 if (ret < 0)
1867 return ret;
1868 voltages = ret;
1869
1870 for (i = 0; i < voltages; i++) {
1871 ret = regulator_list_voltage(regulator, i);
1872
1873 if (ret >= min_uV && ret <= max_uV)
1874 return 1;
1875 }
1876
1877 return 0;
1878}
Mark Browna398eaa2011-12-28 12:48:45 +00001879EXPORT_SYMBOL_GPL(regulator_is_supported_voltage);
Mark Browna7a1ad92009-07-21 16:00:24 +01001880
Mark Brown75790252010-12-12 14:25:50 +00001881static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1882 int min_uV, int max_uV)
1883{
1884 int ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001885 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001886 unsigned int selector;
1887
1888 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1889
Mark Brownbf5892a2011-05-08 22:13:37 +01001890 min_uV += rdev->constraints->uV_offset;
1891 max_uV += rdev->constraints->uV_offset;
1892
Mark Brown75790252010-12-12 14:25:50 +00001893 if (rdev->desc->ops->set_voltage) {
1894 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1895 &selector);
1896
1897 if (rdev->desc->ops->list_voltage)
1898 selector = rdev->desc->ops->list_voltage(rdev,
1899 selector);
David Collinsf6acc6c2012-11-13 16:14:53 -08001900 else if (rdev->desc->ops->get_voltage)
1901 selector = rdev->desc->ops->get_voltage(rdev);
Mark Brown75790252010-12-12 14:25:50 +00001902 else
1903 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001904 } else if (rdev->desc->ops->set_voltage_sel) {
1905 int best_val = INT_MAX;
1906 int i;
1907
1908 selector = 0;
1909
1910 /* Find the smallest voltage that falls within the specified
1911 * range.
1912 */
1913 for (i = 0; i < rdev->desc->n_voltages; i++) {
1914 ret = rdev->desc->ops->list_voltage(rdev, i);
1915 if (ret < 0)
1916 continue;
1917
1918 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1919 best_val = ret;
1920 selector = i;
1921 }
1922 }
1923
Linus Walleij77af1b22011-03-17 13:24:36 +01001924 /*
1925 * If we can't obtain the old selector there is not enough
1926 * info to call set_voltage_time_sel().
1927 */
1928 if (rdev->desc->ops->set_voltage_time_sel &&
1929 rdev->desc->ops->get_voltage_sel) {
1930 unsigned int old_selector = 0;
1931
1932 ret = rdev->desc->ops->get_voltage_sel(rdev);
1933 if (ret < 0)
1934 return ret;
1935 old_selector = ret;
Axel Lin07351232012-02-24 23:13:19 +08001936 ret = rdev->desc->ops->set_voltage_time_sel(rdev,
Linus Walleij77af1b22011-03-17 13:24:36 +01001937 old_selector, selector);
Axel Lin07351232012-02-24 23:13:19 +08001938 if (ret < 0)
1939 rdev_warn(rdev, "set_voltage_time_sel() failed: %d\n", ret);
1940 else
1941 delay = ret;
Linus Walleij77af1b22011-03-17 13:24:36 +01001942 }
1943
Mark Browne8eef822010-12-12 14:36:17 +00001944 if (best_val != INT_MAX) {
1945 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1946 selector = best_val;
1947 } else {
1948 ret = -EINVAL;
1949 }
Mark Brown75790252010-12-12 14:25:50 +00001950 } else {
1951 ret = -EINVAL;
1952 }
1953
Linus Walleij77af1b22011-03-17 13:24:36 +01001954 /* Insert any necessary delays */
1955 if (delay >= 1000) {
1956 mdelay(delay / 1000);
1957 udelay(delay % 1000);
1958 } else if (delay) {
1959 udelay(delay);
1960 }
1961
Mark Brownded06a52010-12-16 13:59:10 +00001962 if (ret == 0)
1963 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1964 NULL);
1965
Mark Brown75790252010-12-12 14:25:50 +00001966 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1967
1968 return ret;
1969}
1970
Mark Browna7a1ad92009-07-21 16:00:24 +01001971/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001972 * regulator_set_voltage - set regulator output voltage
1973 * @regulator: regulator source
1974 * @min_uV: Minimum required voltage in uV
1975 * @max_uV: Maximum acceptable voltage in uV
1976 *
1977 * Sets a voltage regulator to the desired output voltage. This can be set
1978 * during any regulator state. IOW, regulator can be disabled or enabled.
1979 *
1980 * If the regulator is enabled then the voltage will change to the new value
1981 * immediately otherwise if the regulator is disabled the regulator will
1982 * output at the new voltage when enabled.
1983 *
1984 * NOTE: If the regulator is shared between several devices then the lowest
1985 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001986 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001987 * calling this function otherwise this call will fail.
1988 */
1989int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1990{
1991 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001992 int ret = 0;
Paolo Pisati6ab7daa2012-12-13 10:13:00 +01001993 int old_min_uV, old_max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001994
1995 mutex_lock(&rdev->mutex);
1996
Mark Brown95a3c232010-12-16 15:49:37 +00001997 /* If we're setting the same range as last time the change
1998 * should be a noop (some cpufreq implementations use the same
1999 * voltage for multiple frequencies, for example).
2000 */
2001 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
2002 goto out;
2003
Liam Girdwood414c70c2008-04-30 15:59:04 +01002004 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00002005 if (!rdev->desc->ops->set_voltage &&
2006 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002007 ret = -EINVAL;
2008 goto out;
2009 }
2010
2011 /* constraints check */
2012 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2013 if (ret < 0)
2014 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015
Paolo Pisati6ab7daa2012-12-13 10:13:00 +01002016 /* restore original values in case of error */
2017 old_min_uV = regulator->min_uV;
2018 old_max_uV = regulator->max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002019 regulator->min_uV = min_uV;
2020 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00002021
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002022 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
Paolo Pisati6ab7daa2012-12-13 10:13:00 +01002023 if (ret < 0)
2024 goto out2;
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01002025
Mark Brown75790252010-12-12 14:25:50 +00002026 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Paolo Pisati6ab7daa2012-12-13 10:13:00 +01002027 if (ret < 0)
2028 goto out2;
Mark Brown02fa3ec2010-11-10 14:38:30 +00002029
Liam Girdwood414c70c2008-04-30 15:59:04 +01002030out:
2031 mutex_unlock(&rdev->mutex);
2032 return ret;
Paolo Pisati6ab7daa2012-12-13 10:13:00 +01002033out2:
2034 regulator->min_uV = old_min_uV;
2035 regulator->max_uV = old_max_uV;
2036 mutex_unlock(&rdev->mutex);
2037 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002038}
2039EXPORT_SYMBOL_GPL(regulator_set_voltage);
2040
Mark Brown606a2562010-12-16 15:49:36 +00002041/**
Linus Walleij88cd2222011-03-17 13:24:52 +01002042 * regulator_set_voltage_time - get raise/fall time
2043 * @regulator: regulator source
2044 * @old_uV: starting voltage in microvolts
2045 * @new_uV: target voltage in microvolts
2046 *
2047 * Provided with the starting and ending voltage, this function attempts to
2048 * calculate the time in microseconds required to rise or fall to this new
2049 * voltage.
2050 */
2051int regulator_set_voltage_time(struct regulator *regulator,
2052 int old_uV, int new_uV)
2053{
2054 struct regulator_dev *rdev = regulator->rdev;
2055 struct regulator_ops *ops = rdev->desc->ops;
2056 int old_sel = -1;
2057 int new_sel = -1;
2058 int voltage;
2059 int i;
2060
2061 /* Currently requires operations to do this */
2062 if (!ops->list_voltage || !ops->set_voltage_time_sel
2063 || !rdev->desc->n_voltages)
2064 return -EINVAL;
2065
2066 for (i = 0; i < rdev->desc->n_voltages; i++) {
2067 /* We only look for exact voltage matches here */
2068 voltage = regulator_list_voltage(regulator, i);
2069 if (voltage < 0)
2070 return -EINVAL;
2071 if (voltage == 0)
2072 continue;
2073 if (voltage == old_uV)
2074 old_sel = i;
2075 if (voltage == new_uV)
2076 new_sel = i;
2077 }
2078
2079 if (old_sel < 0 || new_sel < 0)
2080 return -EINVAL;
2081
2082 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
2083}
2084EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
2085
2086/**
Mark Brown606a2562010-12-16 15:49:36 +00002087 * regulator_sync_voltage - re-apply last regulator output voltage
2088 * @regulator: regulator source
2089 *
2090 * Re-apply the last configured voltage. This is intended to be used
2091 * where some external control source the consumer is cooperating with
2092 * has caused the configured voltage to change.
2093 */
2094int regulator_sync_voltage(struct regulator *regulator)
2095{
2096 struct regulator_dev *rdev = regulator->rdev;
2097 int ret, min_uV, max_uV;
2098
2099 mutex_lock(&rdev->mutex);
2100
2101 if (!rdev->desc->ops->set_voltage &&
2102 !rdev->desc->ops->set_voltage_sel) {
2103 ret = -EINVAL;
2104 goto out;
2105 }
2106
2107 /* This is only going to work if we've had a voltage configured. */
2108 if (!regulator->min_uV && !regulator->max_uV) {
2109 ret = -EINVAL;
2110 goto out;
2111 }
2112
2113 min_uV = regulator->min_uV;
2114 max_uV = regulator->max_uV;
2115
2116 /* This should be a paranoia check... */
2117 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
2118 if (ret < 0)
2119 goto out;
2120
2121 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
2122 if (ret < 0)
2123 goto out;
2124
2125 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
2126
2127out:
2128 mutex_unlock(&rdev->mutex);
2129 return ret;
2130}
2131EXPORT_SYMBOL_GPL(regulator_sync_voltage);
2132
Liam Girdwood414c70c2008-04-30 15:59:04 +01002133static int _regulator_get_voltage(struct regulator_dev *rdev)
2134{
Mark Brownbf5892a2011-05-08 22:13:37 +01002135 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00002136
2137 if (rdev->desc->ops->get_voltage_sel) {
2138 sel = rdev->desc->ops->get_voltage_sel(rdev);
2139 if (sel < 0)
2140 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01002141 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08002142 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01002143 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08002144 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08002146 }
Mark Brownbf5892a2011-05-08 22:13:37 +01002147
Axel Lincb220d12011-05-23 20:08:10 +08002148 if (ret < 0)
2149 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01002150 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002151}
2152
2153/**
2154 * regulator_get_voltage - get regulator output voltage
2155 * @regulator: regulator source
2156 *
2157 * This returns the current regulator voltage in uV.
2158 *
2159 * NOTE: If the regulator is disabled it will return the voltage value. This
2160 * function should not be used to determine regulator state.
2161 */
2162int regulator_get_voltage(struct regulator *regulator)
2163{
2164 int ret;
2165
2166 mutex_lock(&regulator->rdev->mutex);
2167
2168 ret = _regulator_get_voltage(regulator->rdev);
2169
2170 mutex_unlock(&regulator->rdev->mutex);
2171
2172 return ret;
2173}
2174EXPORT_SYMBOL_GPL(regulator_get_voltage);
2175
2176/**
2177 * regulator_set_current_limit - set regulator output current limit
2178 * @regulator: regulator source
2179 * @min_uA: Minimuum supported current in uA
2180 * @max_uA: Maximum supported current in uA
2181 *
2182 * Sets current sink to the desired output current. This can be set during
2183 * any regulator state. IOW, regulator can be disabled or enabled.
2184 *
2185 * If the regulator is enabled then the current will change to the new value
2186 * immediately otherwise if the regulator is disabled the regulator will
2187 * output at the new current when enabled.
2188 *
2189 * NOTE: Regulator system constraints must be set for this regulator before
2190 * calling this function otherwise this call will fail.
2191 */
2192int regulator_set_current_limit(struct regulator *regulator,
2193 int min_uA, int max_uA)
2194{
2195 struct regulator_dev *rdev = regulator->rdev;
2196 int ret;
2197
2198 mutex_lock(&rdev->mutex);
2199
2200 /* sanity check */
2201 if (!rdev->desc->ops->set_current_limit) {
2202 ret = -EINVAL;
2203 goto out;
2204 }
2205
2206 /* constraints check */
2207 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2208 if (ret < 0)
2209 goto out;
2210
2211 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2212out:
2213 mutex_unlock(&rdev->mutex);
2214 return ret;
2215}
2216EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2217
2218static int _regulator_get_current_limit(struct regulator_dev *rdev)
2219{
2220 int ret;
2221
2222 mutex_lock(&rdev->mutex);
2223
2224 /* sanity check */
2225 if (!rdev->desc->ops->get_current_limit) {
2226 ret = -EINVAL;
2227 goto out;
2228 }
2229
2230 ret = rdev->desc->ops->get_current_limit(rdev);
2231out:
2232 mutex_unlock(&rdev->mutex);
2233 return ret;
2234}
2235
2236/**
2237 * regulator_get_current_limit - get regulator output current
2238 * @regulator: regulator source
2239 *
2240 * This returns the current supplied by the specified current sink in uA.
2241 *
2242 * NOTE: If the regulator is disabled it will return the current value. This
2243 * function should not be used to determine regulator state.
2244 */
2245int regulator_get_current_limit(struct regulator *regulator)
2246{
2247 return _regulator_get_current_limit(regulator->rdev);
2248}
2249EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2250
2251/**
2252 * regulator_set_mode - set regulator operating mode
2253 * @regulator: regulator source
2254 * @mode: operating mode - one of the REGULATOR_MODE constants
2255 *
2256 * Set regulator operating mode to increase regulator efficiency or improve
2257 * regulation performance.
2258 *
2259 * NOTE: Regulator system constraints must be set for this regulator before
2260 * calling this function otherwise this call will fail.
2261 */
2262int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2263{
2264 struct regulator_dev *rdev = regulator->rdev;
2265 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302266 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267
2268 mutex_lock(&rdev->mutex);
2269
2270 /* sanity check */
2271 if (!rdev->desc->ops->set_mode) {
2272 ret = -EINVAL;
2273 goto out;
2274 }
2275
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302276 /* return if the same mode is requested */
2277 if (rdev->desc->ops->get_mode) {
2278 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2279 if (regulator_curr_mode == mode) {
2280 ret = 0;
2281 goto out;
2282 }
2283 }
2284
Liam Girdwood414c70c2008-04-30 15:59:04 +01002285 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002286 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287 if (ret < 0)
2288 goto out;
2289
2290 ret = rdev->desc->ops->set_mode(rdev, mode);
2291out:
2292 mutex_unlock(&rdev->mutex);
2293 return ret;
2294}
2295EXPORT_SYMBOL_GPL(regulator_set_mode);
2296
2297static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2298{
2299 int ret;
2300
2301 mutex_lock(&rdev->mutex);
2302
2303 /* sanity check */
2304 if (!rdev->desc->ops->get_mode) {
2305 ret = -EINVAL;
2306 goto out;
2307 }
2308
2309 ret = rdev->desc->ops->get_mode(rdev);
2310out:
2311 mutex_unlock(&rdev->mutex);
2312 return ret;
2313}
2314
2315/**
2316 * regulator_get_mode - get regulator operating mode
2317 * @regulator: regulator source
2318 *
2319 * Get the current regulator operating mode.
2320 */
2321unsigned int regulator_get_mode(struct regulator *regulator)
2322{
2323 return _regulator_get_mode(regulator->rdev);
2324}
2325EXPORT_SYMBOL_GPL(regulator_get_mode);
2326
2327/**
2328 * regulator_set_optimum_mode - set regulator optimum operating mode
2329 * @regulator: regulator source
2330 * @uA_load: load current
2331 *
2332 * Notifies the regulator core of a new device load. This is then used by
2333 * DRMS (if enabled by constraints) to set the most efficient regulator
2334 * operating mode for the new regulator loading.
2335 *
2336 * Consumer devices notify their supply regulator of the maximum power
2337 * they will require (can be taken from device datasheet in the power
2338 * consumption tables) when they change operational status and hence power
2339 * state. Examples of operational state changes that can affect power
2340 * consumption are :-
2341 *
2342 * o Device is opened / closed.
2343 * o Device I/O is about to begin or has just finished.
2344 * o Device is idling in between work.
2345 *
2346 * This information is also exported via sysfs to userspace.
2347 *
2348 * DRMS will sum the total requested load on the regulator and change
2349 * to the most efficient operating mode if platform constraints allow.
2350 *
2351 * Returns the new regulator mode or error.
2352 */
2353int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2354{
2355 struct regulator_dev *rdev = regulator->rdev;
2356 struct regulator *consumer;
Stephen Boyd81797d92012-07-02 16:41:25 -07002357 int ret, output_uV, input_uV = 0, total_uA_load = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358 unsigned int mode;
2359
Stephen Boyd81797d92012-07-02 16:41:25 -07002360 if (rdev->supply)
2361 input_uV = regulator_get_voltage(rdev->supply);
2362
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 mutex_lock(&rdev->mutex);
2364
Mark Browna4b41482011-05-14 11:19:45 -07002365 /*
2366 * first check to see if we can set modes at all, otherwise just
2367 * tell the consumer everything is OK.
2368 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002369 regulator->uA_load = uA_load;
2370 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002371 if (ret < 0) {
2372 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002373 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002374 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002375
Liam Girdwood414c70c2008-04-30 15:59:04 +01002376 if (!rdev->desc->ops->get_optimum_mode)
2377 goto out;
2378
Mark Browna4b41482011-05-14 11:19:45 -07002379 /*
2380 * we can actually do this so any errors are indicators of
2381 * potential real failure.
2382 */
2383 ret = -EINVAL;
2384
Liam Girdwood414c70c2008-04-30 15:59:04 +01002385 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002386 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002388 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002389 goto out;
2390 }
2391
Stephen Boyd81797d92012-07-02 16:41:25 -07002392 /* No supply? Use constraint voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002393 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002394 input_uV = rdev->constraints->input_uV;
2395 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002396 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002397 goto out;
2398 }
2399
2400 /* calc total requested load for this regulator */
2401 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002402 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002403
2404 mode = rdev->desc->ops->get_optimum_mode(rdev,
2405 input_uV, output_uV,
2406 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002407 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002408 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002409 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2410 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002411 goto out;
2412 }
2413
2414 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002415 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002416 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002417 goto out;
2418 }
2419 ret = mode;
2420out:
2421 mutex_unlock(&rdev->mutex);
2422 return ret;
2423}
2424EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2425
2426/**
2427 * regulator_register_notifier - register regulator event notifier
2428 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002429 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002430 *
2431 * Register notifier block to receive regulator events.
2432 */
2433int regulator_register_notifier(struct regulator *regulator,
2434 struct notifier_block *nb)
2435{
2436 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2437 nb);
2438}
2439EXPORT_SYMBOL_GPL(regulator_register_notifier);
2440
2441/**
2442 * regulator_unregister_notifier - unregister regulator event notifier
2443 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002444 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002445 *
2446 * Unregister regulator event notifier block.
2447 */
2448int regulator_unregister_notifier(struct regulator *regulator,
2449 struct notifier_block *nb)
2450{
2451 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2452 nb);
2453}
2454EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2455
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002456/* notify regulator consumers and downstream regulator consumers.
2457 * Note mutex must be held by caller.
2458 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002459static void _notifier_call_chain(struct regulator_dev *rdev,
2460 unsigned long event, void *data)
2461{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002462 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002463 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002464}
2465
2466/**
2467 * regulator_bulk_get - get multiple regulator consumers
2468 *
2469 * @dev: Device to supply
2470 * @num_consumers: Number of consumers to register
2471 * @consumers: Configuration of consumers; clients are stored here.
2472 *
2473 * @return 0 on success, an errno on failure.
2474 *
2475 * This helper function allows drivers to get several regulator
2476 * consumers in one operation. If any of the regulators cannot be
2477 * acquired then any regulators that were allocated will be freed
2478 * before returning to the caller.
2479 */
2480int regulator_bulk_get(struct device *dev, int num_consumers,
2481 struct regulator_bulk_data *consumers)
2482{
2483 int i;
2484 int ret;
2485
2486 for (i = 0; i < num_consumers; i++)
2487 consumers[i].consumer = NULL;
2488
2489 for (i = 0; i < num_consumers; i++) {
2490 consumers[i].consumer = regulator_get(dev,
2491 consumers[i].supply);
2492 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002493 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002494 dev_err(dev, "Failed to get supply '%s': %d\n",
2495 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002496 consumers[i].consumer = NULL;
2497 goto err;
2498 }
2499 }
2500
2501 return 0;
2502
2503err:
Axel Linb29c7692012-02-20 10:32:16 +08002504 while (--i >= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002505 regulator_put(consumers[i].consumer);
2506
2507 return ret;
2508}
2509EXPORT_SYMBOL_GPL(regulator_bulk_get);
2510
Mark Browne6e74032012-01-20 20:10:08 +00002511/**
2512 * devm_regulator_bulk_get - managed get multiple regulator consumers
2513 *
2514 * @dev: Device to supply
2515 * @num_consumers: Number of consumers to register
2516 * @consumers: Configuration of consumers; clients are stored here.
2517 *
2518 * @return 0 on success, an errno on failure.
2519 *
2520 * This helper function allows drivers to get several regulator
2521 * consumers in one operation with management, the regulators will
2522 * automatically be freed when the device is unbound. If any of the
2523 * regulators cannot be acquired then any regulators that were
2524 * allocated will be freed before returning to the caller.
2525 */
2526int devm_regulator_bulk_get(struct device *dev, int num_consumers,
2527 struct regulator_bulk_data *consumers)
2528{
2529 int i;
2530 int ret;
2531
2532 for (i = 0; i < num_consumers; i++)
2533 consumers[i].consumer = NULL;
2534
2535 for (i = 0; i < num_consumers; i++) {
2536 consumers[i].consumer = devm_regulator_get(dev,
2537 consumers[i].supply);
2538 if (IS_ERR(consumers[i].consumer)) {
2539 ret = PTR_ERR(consumers[i].consumer);
2540 dev_err(dev, "Failed to get supply '%s': %d\n",
2541 consumers[i].supply, ret);
2542 consumers[i].consumer = NULL;
2543 goto err;
2544 }
2545 }
2546
2547 return 0;
2548
2549err:
2550 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2551 devm_regulator_put(consumers[i].consumer);
2552
2553 return ret;
2554}
2555EXPORT_SYMBOL_GPL(devm_regulator_bulk_get);
2556
Mark Brownf21e0e82011-05-24 08:12:40 +08002557static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2558{
2559 struct regulator_bulk_data *bulk = data;
2560
2561 bulk->ret = regulator_enable(bulk->consumer);
2562}
2563
Liam Girdwood414c70c2008-04-30 15:59:04 +01002564/**
2565 * regulator_bulk_enable - enable multiple regulator consumers
2566 *
2567 * @num_consumers: Number of consumers
2568 * @consumers: Consumer data; clients are stored here.
2569 * @return 0 on success, an errno on failure
2570 *
2571 * This convenience API allows consumers to enable multiple regulator
2572 * clients in a single API call. If any consumers cannot be enabled
2573 * then any others that were enabled will be disabled again prior to
2574 * return.
2575 */
2576int regulator_bulk_enable(int num_consumers,
2577 struct regulator_bulk_data *consumers)
2578{
Mark Brownf21e0e82011-05-24 08:12:40 +08002579 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002581 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002582
Mark Brownf21e0e82011-05-24 08:12:40 +08002583 for (i = 0; i < num_consumers; i++)
2584 async_schedule_domain(regulator_bulk_enable_async,
2585 &consumers[i], &async_domain);
2586
2587 async_synchronize_full_domain(&async_domain);
2588
2589 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002590 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002591 if (consumers[i].ret != 0) {
2592 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002593 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002594 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002595 }
2596
2597 return 0;
2598
2599err:
Axel Linb29c7692012-02-20 10:32:16 +08002600 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
2601 while (--i >= 0)
2602 regulator_disable(consumers[i].consumer);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002603
2604 return ret;
2605}
2606EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2607
2608/**
Justin Paupore1d17cf52011-08-16 15:39:01 -07002609 * regulator_bulk_set_voltage - set voltage for multiple regulator consumers
2610 *
2611 * @num_consumers: Number of consumers
2612 * @consumers: Consumer data; clients are stored here.
2613 * @return 0 on success, an errno on failure
2614 *
2615 * This convenience API allows the voted voltage ranges of multiple regulator
2616 * clients to be set in a single API call. If any consumers cannot have their
2617 * voltages set, this function returns WITHOUT withdrawing votes for any
2618 * consumers that have already been set.
2619 */
2620int regulator_bulk_set_voltage(int num_consumers,
2621 struct regulator_bulk_data *consumers)
2622{
2623 int i;
2624 int rc;
2625
2626 for (i = 0; i < num_consumers; i++) {
2627 if (!consumers[i].min_uV && !consumers[i].max_uV)
2628 continue;
2629 rc = regulator_set_voltage(consumers[i].consumer,
2630 consumers[i].min_uV,
2631 consumers[i].max_uV);
2632 if (rc)
2633 goto err;
2634 }
2635
2636 return 0;
2637
2638err:
2639 pr_err("Failed to set voltage for %s: %d\n", consumers[i].supply, rc);
2640 return rc;
2641}
2642EXPORT_SYMBOL_GPL(regulator_bulk_set_voltage);
2643
2644/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002645 * regulator_bulk_disable - disable multiple regulator consumers
2646 *
2647 * @num_consumers: Number of consumers
2648 * @consumers: Consumer data; clients are stored here.
2649 * @return 0 on success, an errno on failure
2650 *
2651 * This convenience API allows consumers to disable multiple regulator
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002652 * clients in a single API call. If any consumers cannot be disabled
2653 * then any others that were disabled will be enabled again prior to
Liam Girdwood414c70c2008-04-30 15:59:04 +01002654 * return.
2655 */
2656int regulator_bulk_disable(int num_consumers,
2657 struct regulator_bulk_data *consumers)
2658{
2659 int i;
2660 int ret;
2661
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002662 for (i = num_consumers - 1; i >= 0; --i) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002663 ret = regulator_disable(consumers[i].consumer);
2664 if (ret != 0)
2665 goto err;
2666 }
2667
2668 return 0;
2669
2670err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002671 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Sylwester Nawrocki49e22632012-01-25 12:35:38 +01002672 for (++i; i < num_consumers; ++i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002673 regulator_enable(consumers[i].consumer);
2674
2675 return ret;
2676}
2677EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2678
2679/**
Donggeun Kime1de2f42012-01-03 16:22:03 +09002680 * regulator_bulk_force_disable - force disable multiple regulator consumers
2681 *
2682 * @num_consumers: Number of consumers
2683 * @consumers: Consumer data; clients are stored here.
2684 * @return 0 on success, an errno on failure
2685 *
2686 * This convenience API allows consumers to forcibly disable multiple regulator
2687 * clients in a single API call.
2688 * NOTE: This should be used for situations when device damage will
2689 * likely occur if the regulators are not disabled (e.g. over temp).
2690 * Although regulator_force_disable function call for some consumers can
2691 * return error numbers, the function is called for all consumers.
2692 */
2693int regulator_bulk_force_disable(int num_consumers,
2694 struct regulator_bulk_data *consumers)
2695{
2696 int i;
2697 int ret;
2698
2699 for (i = 0; i < num_consumers; i++)
2700 consumers[i].ret =
2701 regulator_force_disable(consumers[i].consumer);
2702
2703 for (i = 0; i < num_consumers; i++) {
2704 if (consumers[i].ret != 0) {
2705 ret = consumers[i].ret;
2706 goto out;
2707 }
2708 }
2709
2710 return 0;
2711out:
2712 return ret;
2713}
2714EXPORT_SYMBOL_GPL(regulator_bulk_force_disable);
2715
2716/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002717 * regulator_bulk_free - free multiple regulator consumers
2718 *
2719 * @num_consumers: Number of consumers
2720 * @consumers: Consumer data; clients are stored here.
2721 *
2722 * This convenience API allows consumers to free multiple regulator
2723 * clients in a single API call.
2724 */
2725void regulator_bulk_free(int num_consumers,
2726 struct regulator_bulk_data *consumers)
2727{
2728 int i;
2729
2730 for (i = 0; i < num_consumers; i++) {
2731 regulator_put(consumers[i].consumer);
2732 consumers[i].consumer = NULL;
2733 }
2734}
2735EXPORT_SYMBOL_GPL(regulator_bulk_free);
2736
2737/**
2738 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002739 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002740 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002741 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002742 *
2743 * Called by regulator drivers to notify clients a regulator event has
2744 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002745 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002746 */
2747int regulator_notifier_call_chain(struct regulator_dev *rdev,
2748 unsigned long event, void *data)
2749{
2750 _notifier_call_chain(rdev, event, data);
2751 return NOTIFY_DONE;
2752
2753}
2754EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2755
Mark Brownbe721972009-08-04 20:09:52 +02002756/**
2757 * regulator_mode_to_status - convert a regulator mode into a status
2758 *
2759 * @mode: Mode to convert
2760 *
2761 * Convert a regulator mode into a status.
2762 */
2763int regulator_mode_to_status(unsigned int mode)
2764{
2765 switch (mode) {
2766 case REGULATOR_MODE_FAST:
2767 return REGULATOR_STATUS_FAST;
2768 case REGULATOR_MODE_NORMAL:
2769 return REGULATOR_STATUS_NORMAL;
2770 case REGULATOR_MODE_IDLE:
2771 return REGULATOR_STATUS_IDLE;
2772 case REGULATOR_STATUS_STANDBY:
2773 return REGULATOR_STATUS_STANDBY;
2774 default:
2775 return 0;
2776 }
2777}
2778EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2779
David Brownell7ad68e22008-11-11 17:39:02 -08002780/*
2781 * To avoid cluttering sysfs (and memory) with useless state, only
2782 * create attributes that can be meaningfully displayed.
2783 */
2784static int add_regulator_attributes(struct regulator_dev *rdev)
2785{
2786 struct device *dev = &rdev->dev;
2787 struct regulator_ops *ops = rdev->desc->ops;
2788 int status = 0;
2789
2790 /* some attributes need specific methods to be displayed */
Mark Brown4c788992011-11-02 11:39:09 +00002791 if ((ops->get_voltage && ops->get_voltage(rdev) >= 0) ||
2792 (ops->get_voltage_sel && ops->get_voltage_sel(rdev) >= 0)) {
David Brownell7ad68e22008-11-11 17:39:02 -08002793 status = device_create_file(dev, &dev_attr_microvolts);
2794 if (status < 0)
2795 return status;
2796 }
2797 if (ops->get_current_limit) {
2798 status = device_create_file(dev, &dev_attr_microamps);
2799 if (status < 0)
2800 return status;
2801 }
2802 if (ops->get_mode) {
2803 status = device_create_file(dev, &dev_attr_opmode);
2804 if (status < 0)
2805 return status;
2806 }
2807 if (ops->is_enabled) {
2808 status = device_create_file(dev, &dev_attr_state);
2809 if (status < 0)
2810 return status;
2811 }
David Brownell853116a2009-01-14 23:03:17 -08002812 if (ops->get_status) {
2813 status = device_create_file(dev, &dev_attr_status);
2814 if (status < 0)
2815 return status;
2816 }
David Brownell7ad68e22008-11-11 17:39:02 -08002817
2818 /* some attributes are type-specific */
2819 if (rdev->desc->type == REGULATOR_CURRENT) {
2820 status = device_create_file(dev, &dev_attr_requested_microamps);
2821 if (status < 0)
2822 return status;
2823 }
2824
2825 /* all the other attributes exist to support constraints;
2826 * don't show them if there are no constraints, or if the
2827 * relevant supporting methods are missing.
2828 */
2829 if (!rdev->constraints)
2830 return status;
2831
2832 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002833 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002834 status = device_create_file(dev, &dev_attr_min_microvolts);
2835 if (status < 0)
2836 return status;
2837 status = device_create_file(dev, &dev_attr_max_microvolts);
2838 if (status < 0)
2839 return status;
2840 }
2841 if (ops->set_current_limit) {
2842 status = device_create_file(dev, &dev_attr_min_microamps);
2843 if (status < 0)
2844 return status;
2845 status = device_create_file(dev, &dev_attr_max_microamps);
2846 if (status < 0)
2847 return status;
2848 }
2849
2850 /* suspend mode constraints need multiple supporting methods */
2851 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2852 return status;
2853
2854 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2855 if (status < 0)
2856 return status;
2857 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2858 if (status < 0)
2859 return status;
2860 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2861 if (status < 0)
2862 return status;
2863
2864 if (ops->set_suspend_voltage) {
2865 status = device_create_file(dev,
2866 &dev_attr_suspend_standby_microvolts);
2867 if (status < 0)
2868 return status;
2869 status = device_create_file(dev,
2870 &dev_attr_suspend_mem_microvolts);
2871 if (status < 0)
2872 return status;
2873 status = device_create_file(dev,
2874 &dev_attr_suspend_disk_microvolts);
2875 if (status < 0)
2876 return status;
2877 }
2878
2879 if (ops->set_suspend_mode) {
2880 status = device_create_file(dev,
2881 &dev_attr_suspend_standby_mode);
2882 if (status < 0)
2883 return status;
2884 status = device_create_file(dev,
2885 &dev_attr_suspend_mem_mode);
2886 if (status < 0)
2887 return status;
2888 status = device_create_file(dev,
2889 &dev_attr_suspend_disk_mode);
2890 if (status < 0)
2891 return status;
2892 }
2893
2894 return status;
2895}
2896
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002897#ifdef CONFIG_DEBUG_FS
2898
2899#define MAX_DEBUG_BUF_LEN 50
2900
2901static DEFINE_MUTEX(debug_buf_mutex);
2902static char debug_buf[MAX_DEBUG_BUF_LEN];
2903
2904static int reg_debug_enable_set(void *data, u64 val)
2905{
2906 int err_info;
2907 if (IS_ERR(data) || data == NULL) {
2908 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2909 return -ENOMEM;
2910 }
2911
2912 if (val)
2913 err_info = regulator_enable(data);
2914 else
2915 err_info = regulator_disable(data);
2916
2917 return err_info;
2918}
2919
2920static int reg_debug_enable_get(void *data, u64 *val)
2921{
2922 if (IS_ERR(data) || data == NULL) {
2923 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2924 return -ENOMEM;
2925 }
2926
2927 *val = regulator_is_enabled(data);
2928 return 0;
2929}
2930
2931DEFINE_SIMPLE_ATTRIBUTE(reg_enable_fops, reg_debug_enable_get,
2932 reg_debug_enable_set, "%llu\n");
2933
2934static int reg_debug_fdisable_set(void *data, u64 val)
2935{
2936 int err_info;
2937 if (IS_ERR(data) || data == NULL) {
2938 pr_err("Function Input Error %ld\n", PTR_ERR(data));
2939 return -ENOMEM;
2940 }
2941
2942 if (val > 0)
2943 err_info = regulator_force_disable(data);
2944 else
2945 err_info = 0;
2946
2947 return err_info;
2948}
2949
2950DEFINE_SIMPLE_ATTRIBUTE(reg_fdisable_fops, reg_debug_enable_get,
2951 reg_debug_fdisable_set, "%llu\n");
2952
2953static ssize_t reg_debug_volt_set(struct file *file, const char __user *buf,
2954 size_t count, loff_t *ppos)
2955{
2956 int err_info, filled;
2957 int min, max = -1;
2958 if (IS_ERR(file) || file == NULL) {
2959 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2960 return -ENOMEM;
2961 }
2962
2963 if (count < MAX_DEBUG_BUF_LEN) {
2964 mutex_lock(&debug_buf_mutex);
2965
2966 if (copy_from_user(debug_buf, (void __user *) buf, count))
2967 return -EFAULT;
2968
2969 debug_buf[count] = '\0';
2970 filled = sscanf(debug_buf, "%d %d", &min, &max);
2971
2972 mutex_unlock(&debug_buf_mutex);
2973 /* check that user entered two numbers */
2974 if (filled < 2 || min < 0 || max < min) {
2975 pr_info("Error, correct format: 'echo \"min max\""
2976 " > voltage");
2977 return -ENOMEM;
2978 } else {
2979 err_info = regulator_set_voltage(file->private_data,
2980 min, max);
2981 }
2982 } else {
2983 pr_err("Error-Input voltage pair"
2984 " string exceeds maximum buffer length");
2985
2986 return -ENOMEM;
2987 }
2988
2989 return count;
2990}
2991
2992static ssize_t reg_debug_volt_get(struct file *file, char __user *buf,
2993 size_t count, loff_t *ppos)
2994{
2995 int voltage, output, rc;
2996 if (IS_ERR(file) || file == NULL) {
2997 pr_err("Function Input Error %ld\n", PTR_ERR(file));
2998 return -ENOMEM;
2999 }
3000
3001 voltage = regulator_get_voltage(file->private_data);
3002 mutex_lock(&debug_buf_mutex);
3003
3004 output = snprintf(debug_buf, MAX_DEBUG_BUF_LEN-1, "%d\n", voltage);
3005 rc = simple_read_from_buffer((void __user *) buf, output, ppos,
3006 (void *) debug_buf, output);
3007
3008 mutex_unlock(&debug_buf_mutex);
3009
3010 return rc;
3011}
3012
3013static int reg_debug_volt_open(struct inode *inode, struct file *file)
3014{
3015 if (IS_ERR(file) || file == NULL) {
3016 pr_err("Function Input Error %ld\n", PTR_ERR(file));
3017 return -ENOMEM;
3018 }
3019
3020 file->private_data = inode->i_private;
3021 return 0;
3022}
3023
3024static const struct file_operations reg_volt_fops = {
3025 .write = reg_debug_volt_set,
3026 .open = reg_debug_volt_open,
3027 .read = reg_debug_volt_get,
3028};
3029
3030static int reg_debug_mode_set(void *data, u64 val)
3031{
3032 int err_info;
3033 if (IS_ERR(data) || data == NULL) {
3034 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3035 return -ENOMEM;
3036 }
3037
3038 err_info = regulator_set_mode(data, (unsigned int)val);
3039
3040 return err_info;
3041}
3042
3043static int reg_debug_mode_get(void *data, u64 *val)
3044{
3045 int err_info;
3046 if (IS_ERR(data) || data == NULL) {
3047 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3048 return -ENOMEM;
3049 }
3050
3051 err_info = regulator_get_mode(data);
3052
3053 if (err_info < 0) {
3054 pr_err("Regulator_get_mode returned an error!\n");
3055 return -ENOMEM;
3056 } else {
3057 *val = err_info;
3058 return 0;
3059 }
3060}
3061
3062DEFINE_SIMPLE_ATTRIBUTE(reg_mode_fops, reg_debug_mode_get,
3063 reg_debug_mode_set, "%llu\n");
3064
3065static int reg_debug_optimum_mode_set(void *data, u64 val)
3066{
3067 int err_info;
3068 if (IS_ERR(data) || data == NULL) {
3069 pr_err("Function Input Error %ld\n", PTR_ERR(data));
3070 return -ENOMEM;
3071 }
3072
3073 err_info = regulator_set_optimum_mode(data, (unsigned int)val);
3074
3075 if (err_info < 0) {
3076 pr_err("Regulator_set_optimum_mode returned an error!\n");
3077 return err_info;
3078 }
3079
3080 return 0;
3081}
3082
3083DEFINE_SIMPLE_ATTRIBUTE(reg_optimum_mode_fops, reg_debug_mode_get,
3084 reg_debug_optimum_mode_set, "%llu\n");
3085
3086static int reg_debug_consumers_show(struct seq_file *m, void *v)
3087{
3088 struct regulator_dev *rdev = m->private;
3089 struct regulator *reg;
3090 char *supply_name;
3091
3092 if (!rdev) {
3093 pr_err("regulator device missing");
3094 return -EINVAL;
3095 }
3096
3097 mutex_lock(&rdev->mutex);
3098
3099 /* Print a header if there are consumers. */
3100 if (rdev->open_count)
3101 seq_printf(m, "Device-Supply "
3102 "EN Min_uV Max_uV load_uA\n");
3103
3104 list_for_each_entry(reg, &rdev->consumer_list, list) {
3105 if (reg->supply_name)
3106 supply_name = reg->supply_name;
3107 else
3108 supply_name = "(null)-(null)";
3109
3110 seq_printf(m, "%-32s %c %8d %8d %8d\n", supply_name,
3111 (reg->enabled ? 'Y' : 'N'), reg->min_uV, reg->max_uV,
3112 reg->uA_load);
3113 }
3114
3115 mutex_unlock(&rdev->mutex);
3116
3117 return 0;
3118}
3119
3120static int reg_debug_consumers_open(struct inode *inode, struct file *file)
3121{
3122 return single_open(file, reg_debug_consumers_show, inode->i_private);
3123}
3124
3125static const struct file_operations reg_consumers_fops = {
3126 .owner = THIS_MODULE,
3127 .open = reg_debug_consumers_open,
3128 .read = seq_read,
3129 .llseek = seq_lseek,
3130 .release = single_release,
3131};
3132
Mark Brown1130e5b2010-12-21 23:49:31 +00003133static void rdev_init_debugfs(struct regulator_dev *rdev)
3134{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003135 struct dentry *err_ptr = NULL;
3136 struct regulator *reg;
3137 struct regulator_ops *reg_ops;
3138 mode_t mode;
3139
3140 if (IS_ERR(rdev) || rdev == NULL ||
3141 IS_ERR(debugfs_root) || debugfs_root == NULL) {
3142 pr_err("Error-Bad Function Input\n");
3143 goto error;
3144 }
3145
Mark Brown1130e5b2010-12-21 23:49:31 +00003146 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
3147 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
3148 rdev_warn(rdev, "Failed to create debugfs directory\n");
3149 rdev->debugfs = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003150 goto error;
Mark Brown1130e5b2010-12-21 23:49:31 +00003151 }
3152
3153 debugfs_create_u32("use_count", 0444, rdev->debugfs,
3154 &rdev->use_count);
3155 debugfs_create_u32("open_count", 0444, rdev->debugfs,
3156 &rdev->open_count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003157 debugfs_create_file("consumers", 0444, rdev->debugfs, rdev,
3158 &reg_consumers_fops);
3159
3160 reg = regulator_get(NULL, rdev->desc->name);
3161 if (IS_ERR(reg) || reg == NULL) {
3162 pr_err("Error-Bad Function Input\n");
3163 goto error;
3164 }
3165
3166 reg_ops = rdev->desc->ops;
3167 mode = S_IRUGO | S_IWUSR;
3168 /* Enabled File */
3169 if (mode)
3170 err_ptr = debugfs_create_file("enable", mode, rdev->debugfs,
3171 reg, &reg_enable_fops);
3172 if (IS_ERR(err_ptr)) {
3173 pr_err("Error-Could not create enable file\n");
3174 debugfs_remove_recursive(rdev->debugfs);
3175 goto error;
3176 }
3177
3178 mode = 0;
3179 /* Force-Disable File */
3180 if (reg_ops->is_enabled)
3181 mode |= S_IRUGO;
3182 if (reg_ops->enable || reg_ops->disable)
3183 mode |= S_IWUSR;
3184 if (mode)
3185 err_ptr = debugfs_create_file("force_disable", mode,
3186 rdev->debugfs, reg, &reg_fdisable_fops);
3187 if (IS_ERR(err_ptr)) {
3188 pr_err("Error-Could not create force_disable file\n");
3189 debugfs_remove_recursive(rdev->debugfs);
3190 goto error;
3191 }
3192
3193 mode = 0;
3194 /* Voltage File */
3195 if (reg_ops->get_voltage)
3196 mode |= S_IRUGO;
3197 if (reg_ops->set_voltage)
3198 mode |= S_IWUSR;
3199 if (mode)
3200 err_ptr = debugfs_create_file("voltage", mode, rdev->debugfs,
3201 reg, &reg_volt_fops);
3202 if (IS_ERR(err_ptr)) {
3203 pr_err("Error-Could not create voltage file\n");
3204 debugfs_remove_recursive(rdev->debugfs);
3205 goto error;
3206 }
3207
3208 mode = 0;
3209 /* Mode File */
3210 if (reg_ops->get_mode)
3211 mode |= S_IRUGO;
3212 if (reg_ops->set_mode)
3213 mode |= S_IWUSR;
3214 if (mode)
3215 err_ptr = debugfs_create_file("mode", mode, rdev->debugfs,
3216 reg, &reg_mode_fops);
3217 if (IS_ERR(err_ptr)) {
3218 pr_err("Error-Could not create mode file\n");
3219 debugfs_remove_recursive(rdev->debugfs);
3220 goto error;
3221 }
3222
3223 mode = 0;
3224 /* Optimum Mode File */
3225 if (reg_ops->get_mode)
3226 mode |= S_IRUGO;
3227 if (reg_ops->set_mode)
3228 mode |= S_IWUSR;
3229 if (mode)
3230 err_ptr = debugfs_create_file("optimum_mode", mode,
3231 rdev->debugfs, reg, &reg_optimum_mode_fops);
3232 if (IS_ERR(err_ptr)) {
3233 pr_err("Error-Could not create optimum_mode file\n");
3234 debugfs_remove_recursive(rdev->debugfs);
3235 goto error;
3236 }
3237
3238error:
3239 return;
Mark Brown1130e5b2010-12-21 23:49:31 +00003240}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003241#else
3242static inline void rdev_init_debugfs(struct regulator_dev *rdev)
3243{
3244 return;
3245}
3246#endif
Mark Brown1130e5b2010-12-21 23:49:31 +00003247
Liam Girdwood414c70c2008-04-30 15:59:04 +01003248/**
3249 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00003250 * @regulator_desc: regulator to register
3251 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00003252 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00003253 * @driver_data: private regulator data
Mark Brown4a7cbb52012-01-24 11:17:26 +00003254 * @of_node: OpenFirmware node to parse for device tree bindings (may be
3255 * NULL).
Liam Girdwood414c70c2008-04-30 15:59:04 +01003256 *
3257 * Called by regulator drivers to register a regulator.
3258 * Returns 0 on success.
3259 */
3260struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00003261 struct device *dev, const struct regulator_init_data *init_data,
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303262 void *driver_data, struct device_node *of_node)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003263{
Mark Brown9a8f5e02011-11-29 18:11:19 +00003264 const struct regulation_constraints *constraints = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003265 static atomic_t regulator_no = ATOMIC_INIT(0);
3266 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003267 int ret, i;
Rajendra Nayak69511a42011-11-18 16:47:20 +05303268 const char *supply = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003269
3270 if (regulator_desc == NULL)
3271 return ERR_PTR(-EINVAL);
3272
3273 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
3274 return ERR_PTR(-EINVAL);
3275
Diego Lizierocd78dfc2009-04-14 03:04:47 +02003276 if (regulator_desc->type != REGULATOR_VOLTAGE &&
3277 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01003278 return ERR_PTR(-EINVAL);
3279
Mark Brown476c2d82010-12-10 17:28:07 +00003280 /* Only one of each should be implemented */
3281 WARN_ON(regulator_desc->ops->get_voltage &&
3282 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00003283 WARN_ON(regulator_desc->ops->set_voltage &&
3284 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00003285
3286 /* If we're using selectors we must implement list_voltage. */
3287 if (regulator_desc->ops->get_voltage_sel &&
3288 !regulator_desc->ops->list_voltage) {
3289 return ERR_PTR(-EINVAL);
3290 }
Mark Browne8eef822010-12-12 14:36:17 +00003291 if (regulator_desc->ops->set_voltage_sel &&
3292 !regulator_desc->ops->list_voltage) {
3293 return ERR_PTR(-EINVAL);
3294 }
Mark Brown476c2d82010-12-10 17:28:07 +00003295
Liam Girdwood414c70c2008-04-30 15:59:04 +01003296 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
3297 if (rdev == NULL)
3298 return ERR_PTR(-ENOMEM);
3299
3300 mutex_lock(&regulator_list_mutex);
3301
3302 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003303 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003304 rdev->owner = regulator_desc->owner;
3305 rdev->desc = regulator_desc;
3306 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003307 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003308 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01003309 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003310
Liam Girdwooda5766f12008-10-10 13:22:20 +01003311 /* preform any regulator specific init */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003312 if (init_data && init_data->regulator_init) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01003313 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08003314 if (ret < 0)
3315 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003316 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01003317
Liam Girdwooda5766f12008-10-10 13:22:20 +01003318 /* register with sysfs */
3319 rdev->dev.class = &regulator_class;
Rajendra Nayak2c043bc2011-11-18 16:47:19 +05303320 rdev->dev.of_node = of_node;
Liam Girdwooda5766f12008-10-10 13:22:20 +01003321 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01003322 dev_set_name(&rdev->dev, "regulator.%d",
3323 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01003324 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003325 if (ret != 0) {
3326 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08003327 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04003328 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003329
3330 dev_set_drvdata(&rdev->dev, rdev);
3331
Mike Rapoport74f544c2008-11-25 14:53:53 +02003332 /* set regulator constraints */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003333 if (init_data)
3334 constraints = &init_data->constraints;
3335
3336 ret = set_machine_constraints(rdev, constraints);
Mike Rapoport74f544c2008-11-25 14:53:53 +02003337 if (ret < 0)
3338 goto scrub;
3339
David Brownell7ad68e22008-11-11 17:39:02 -08003340 /* add attributes supported by this regulator */
3341 ret = add_regulator_attributes(rdev);
3342 if (ret < 0)
3343 goto scrub;
3344
Mark Brown9a8f5e02011-11-29 18:11:19 +00003345 if (init_data && init_data->supply_regulator)
Rajendra Nayak69511a42011-11-18 16:47:20 +05303346 supply = init_data->supply_regulator;
3347 else if (regulator_desc->supply_name)
3348 supply = regulator_desc->supply_name;
3349
3350 if (supply) {
Mark Brown0178f3e2010-04-26 15:18:14 +01003351 struct regulator_dev *r;
Mark Brown0178f3e2010-04-26 15:18:14 +01003352
Rajendra Nayak69511a42011-11-18 16:47:20 +05303353 r = regulator_dev_lookup(dev, supply);
Mark Brown0178f3e2010-04-26 15:18:14 +01003354
Rajendra Nayak69511a42011-11-18 16:47:20 +05303355 if (!r) {
3356 dev_err(dev, "Failed to find supply %s\n", supply);
Mark Brown04bf3012012-03-11 13:07:56 +00003357 ret = -EPROBE_DEFER;
Mark Brown0178f3e2010-04-26 15:18:14 +01003358 goto scrub;
3359 }
3360
3361 ret = set_supply(rdev, r);
3362 if (ret < 0)
3363 goto scrub;
3364 }
3365
Liam Girdwooda5766f12008-10-10 13:22:20 +01003366 /* add consumers devices */
Mark Brown9a8f5e02011-11-29 18:11:19 +00003367 if (init_data) {
3368 for (i = 0; i < init_data->num_consumer_supplies; i++) {
3369 ret = set_consumer_device_supply(rdev,
Mark Brown9a8f5e02011-11-29 18:11:19 +00003370 init_data->consumer_supplies[i].dev_name,
Mark Brown23c2f042011-02-24 17:39:09 +00003371 init_data->consumer_supplies[i].supply);
Mark Brown9a8f5e02011-11-29 18:11:19 +00003372 if (ret < 0) {
3373 dev_err(dev, "Failed to set supply %s\n",
3374 init_data->consumer_supplies[i].supply);
3375 goto unset_supplies;
3376 }
Mark Brown23c2f042011-02-24 17:39:09 +00003377 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01003378 }
3379
3380 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00003381
Liam Girdwood414c70c2008-04-30 15:59:04 +01003382 mutex_unlock(&regulator_list_mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003383 rdev_init_debugfs(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01003384 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003385
Liam Girdwood414c70c2008-04-30 15:59:04 +01003386out:
3387 mutex_unlock(&regulator_list_mutex);
3388 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08003389
Jani Nikulad4033b52010-04-29 10:55:11 +03003390unset_supplies:
3391 unset_regulator_supplies(rdev);
3392
David Brownell4fca9542008-11-11 17:38:53 -08003393scrub:
Mark Brown20da2dc2012-05-13 18:35:56 +01003394 if (rdev->supply)
3395 regulator_put(rdev->supply);
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);