blob: 938398f3e869c57814fc130b9133ac435118214e [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
Daniel Walker1d7372e2010-11-17 15:30:28 -080016#define pr_fmt(fmt) "%s: " fmt, __func__
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080017
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000020#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010021#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080023#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/err.h>
25#include <linux/mutex.h>
26#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000027#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010028#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
Paul Gortmaker65602c32011-07-17 16:28:23 -040031#include <linux/module.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown02fa3ec2010-11-10 14:38:30 +000033#define CREATE_TRACE_POINTS
34#include <trace/events/regulator.h>
35
Mark Brown34abbd62010-02-12 10:18:08 +000036#include "dummy.h"
37
Mark Brown7d51a0d2011-06-09 16:06:37 +010038#define rdev_crit(rdev, fmt, ...) \
39 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080040#define rdev_err(rdev, fmt, ...) \
41 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_warn(rdev, fmt, ...) \
43 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44#define rdev_info(rdev, fmt, ...) \
45 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
46#define rdev_dbg(rdev, fmt, ...) \
47 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
48
Liam Girdwood414c70c2008-04-30 15:59:04 +010049static DEFINE_MUTEX(regulator_list_mutex);
50static LIST_HEAD(regulator_list);
51static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000052static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070053static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010054
Mark Brown1130e5b2010-12-21 23:49:31 +000055#ifdef CONFIG_DEBUG_FS
56static struct dentry *debugfs_root;
57#endif
58
Mark Brown8dc53902008-12-31 12:52:40 +000059/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010060 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010066 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010067 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010068 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010069};
70
Liam Girdwood414c70c2008-04-30 15:59:04 +010071/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
79 int uA_load;
80 int min_uV;
81 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010082 char *supply_name;
83 struct device_attribute dev_attr;
84 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010085#ifdef CONFIG_DEBUG_FS
86 struct dentry *debugfs;
87#endif
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
135/* Platform voltage constraint check */
136static int regulator_check_voltage(struct regulator_dev *rdev,
137 int *min_uV, int *max_uV)
138{
139 BUG_ON(*min_uV > *max_uV);
140
141 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800142 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100143 return -ENODEV;
144 }
145 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800146 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100147 return -EPERM;
148 }
149
150 if (*max_uV > rdev->constraints->max_uV)
151 *max_uV = rdev->constraints->max_uV;
152 if (*min_uV < rdev->constraints->min_uV)
153 *min_uV = rdev->constraints->min_uV;
154
Mark Brown89f425e2011-07-12 11:20:37 +0900155 if (*min_uV > *max_uV) {
156 rdev_err(rdev, "unsupportable voltage range: %d-%duV\n",
Mark Brown54abd332011-07-21 15:07:37 +0100157 *min_uV, *max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100158 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900159 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100160
161 return 0;
162}
163
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100164/* Make sure we select a voltage that suits the needs of all
165 * regulator consumers
166 */
167static int regulator_check_consumers(struct regulator_dev *rdev,
168 int *min_uV, int *max_uV)
169{
170 struct regulator *regulator;
171
172 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700173 /*
174 * Assume consumers that didn't say anything are OK
175 * with anything in the constraint range.
176 */
177 if (!regulator->min_uV && !regulator->max_uV)
178 continue;
179
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100180 if (*max_uV > regulator->max_uV)
181 *max_uV = regulator->max_uV;
182 if (*min_uV < regulator->min_uV)
183 *min_uV = regulator->min_uV;
184 }
185
186 if (*min_uV > *max_uV)
187 return -EINVAL;
188
189 return 0;
190}
191
Liam Girdwood414c70c2008-04-30 15:59:04 +0100192/* current constraint check */
193static int regulator_check_current_limit(struct regulator_dev *rdev,
194 int *min_uA, int *max_uA)
195{
196 BUG_ON(*min_uA > *max_uA);
197
198 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800199 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100200 return -ENODEV;
201 }
202 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800203 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100204 return -EPERM;
205 }
206
207 if (*max_uA > rdev->constraints->max_uA)
208 *max_uA = rdev->constraints->max_uA;
209 if (*min_uA < rdev->constraints->min_uA)
210 *min_uA = rdev->constraints->min_uA;
211
Mark Brown89f425e2011-07-12 11:20:37 +0900212 if (*min_uA > *max_uA) {
213 rdev_err(rdev, "unsupportable current range: %d-%duA\n",
Mark Brown54abd332011-07-21 15:07:37 +0100214 *min_uA, *max_uA);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215 return -EINVAL;
Mark Brown89f425e2011-07-12 11:20:37 +0900216 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100217
218 return 0;
219}
220
221/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900222static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100223{
Mark Brown2c608232011-03-30 06:29:12 +0900224 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800225 case REGULATOR_MODE_FAST:
226 case REGULATOR_MODE_NORMAL:
227 case REGULATOR_MODE_IDLE:
228 case REGULATOR_MODE_STANDBY:
229 break;
230 default:
Mark Brown89f425e2011-07-12 11:20:37 +0900231 rdev_err(rdev, "invalid mode %x specified\n", *mode);
David Brownelle5735202008-11-16 11:46:56 -0800232 return -EINVAL;
233 }
234
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800236 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100237 return -ENODEV;
238 }
239 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800240 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241 return -EPERM;
242 }
Mark Brown2c608232011-03-30 06:29:12 +0900243
244 /* The modes are bitmasks, the most power hungry modes having
245 * the lowest values. If the requested mode isn't supported
246 * try higher modes. */
247 while (*mode) {
248 if (rdev->constraints->valid_modes_mask & *mode)
249 return 0;
250 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100251 }
Mark Brown2c608232011-03-30 06:29:12 +0900252
253 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254}
255
256/* dynamic regulator mode switching constraint check */
257static int regulator_check_drms(struct regulator_dev *rdev)
258{
259 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800260 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100261 return -ENODEV;
262 }
263 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800264 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100265 return -EPERM;
266 }
267 return 0;
268}
269
270static ssize_t device_requested_uA_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
273 struct regulator *regulator;
274
275 regulator = get_device_regulator(dev);
276 if (regulator == NULL)
277 return 0;
278
279 return sprintf(buf, "%d\n", regulator->uA_load);
280}
281
282static ssize_t regulator_uV_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
284{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100285 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100286 ssize_t ret;
287
288 mutex_lock(&rdev->mutex);
289 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
290 mutex_unlock(&rdev->mutex);
291
292 return ret;
293}
David Brownell7ad68e22008-11-11 17:39:02 -0800294static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100295
296static ssize_t regulator_uA_show(struct device *dev,
297 struct device_attribute *attr, char *buf)
298{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100299 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100300
301 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
302}
David Brownell7ad68e22008-11-11 17:39:02 -0800303static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100304
Mark Brownbc558a62008-10-10 15:33:20 +0100305static ssize_t regulator_name_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
307{
308 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100309
Mark Brown1083c392009-10-22 16:31:32 +0100310 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100311}
312
David Brownell4fca9542008-11-11 17:38:53 -0800313static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100314{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100315 switch (mode) {
316 case REGULATOR_MODE_FAST:
317 return sprintf(buf, "fast\n");
318 case REGULATOR_MODE_NORMAL:
319 return sprintf(buf, "normal\n");
320 case REGULATOR_MODE_IDLE:
321 return sprintf(buf, "idle\n");
322 case REGULATOR_MODE_STANDBY:
323 return sprintf(buf, "standby\n");
324 }
325 return sprintf(buf, "unknown\n");
326}
327
David Brownell4fca9542008-11-11 17:38:53 -0800328static ssize_t regulator_opmode_show(struct device *dev,
329 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100330{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100331 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100332
David Brownell4fca9542008-11-11 17:38:53 -0800333 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
334}
David Brownell7ad68e22008-11-11 17:39:02 -0800335static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800336
337static ssize_t regulator_print_state(char *buf, int state)
338{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100339 if (state > 0)
340 return sprintf(buf, "enabled\n");
341 else if (state == 0)
342 return sprintf(buf, "disabled\n");
343 else
344 return sprintf(buf, "unknown\n");
345}
346
David Brownell4fca9542008-11-11 17:38:53 -0800347static ssize_t regulator_state_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
350 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100351 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800352
Mark Brown93325462009-08-03 18:49:56 +0100353 mutex_lock(&rdev->mutex);
354 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
355 mutex_unlock(&rdev->mutex);
356
357 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800358}
David Brownell7ad68e22008-11-11 17:39:02 -0800359static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800360
David Brownell853116a2009-01-14 23:03:17 -0800361static ssize_t regulator_status_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct regulator_dev *rdev = dev_get_drvdata(dev);
365 int status;
366 char *label;
367
368 status = rdev->desc->ops->get_status(rdev);
369 if (status < 0)
370 return status;
371
372 switch (status) {
373 case REGULATOR_STATUS_OFF:
374 label = "off";
375 break;
376 case REGULATOR_STATUS_ON:
377 label = "on";
378 break;
379 case REGULATOR_STATUS_ERROR:
380 label = "error";
381 break;
382 case REGULATOR_STATUS_FAST:
383 label = "fast";
384 break;
385 case REGULATOR_STATUS_NORMAL:
386 label = "normal";
387 break;
388 case REGULATOR_STATUS_IDLE:
389 label = "idle";
390 break;
391 case REGULATOR_STATUS_STANDBY:
392 label = "standby";
393 break;
394 default:
395 return -ERANGE;
396 }
397
398 return sprintf(buf, "%s\n", label);
399}
400static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
401
Liam Girdwood414c70c2008-04-30 15:59:04 +0100402static ssize_t regulator_min_uA_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
404{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100405 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100406
407 if (!rdev->constraints)
408 return sprintf(buf, "constraint not defined\n");
409
410 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
411}
David Brownell7ad68e22008-11-11 17:39:02 -0800412static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100413
414static ssize_t regulator_max_uA_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
416{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100417 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418
419 if (!rdev->constraints)
420 return sprintf(buf, "constraint not defined\n");
421
422 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
423}
David Brownell7ad68e22008-11-11 17:39:02 -0800424static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100425
426static ssize_t regulator_min_uV_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100429 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
431 if (!rdev->constraints)
432 return sprintf(buf, "constraint not defined\n");
433
434 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
435}
David Brownell7ad68e22008-11-11 17:39:02 -0800436static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
438static ssize_t regulator_max_uV_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
443 if (!rdev->constraints)
444 return sprintf(buf, "constraint not defined\n");
445
446 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449
450static ssize_t regulator_total_uA_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454 struct regulator *regulator;
455 int uA = 0;
456
457 mutex_lock(&rdev->mutex);
458 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100459 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460 mutex_unlock(&rdev->mutex);
461 return sprintf(buf, "%d\n", uA);
462}
David Brownell7ad68e22008-11-11 17:39:02 -0800463static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465static ssize_t regulator_num_users_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100468 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469 return sprintf(buf, "%d\n", rdev->use_count);
470}
471
472static ssize_t regulator_type_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
477 switch (rdev->desc->type) {
478 case REGULATOR_VOLTAGE:
479 return sprintf(buf, "voltage\n");
480 case REGULATOR_CURRENT:
481 return sprintf(buf, "current\n");
482 }
483 return sprintf(buf, "unknown\n");
484}
485
486static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
487 struct device_attribute *attr, char *buf)
488{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100489 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
492}
David Brownell7ad68e22008-11-11 17:39:02 -0800493static DEVICE_ATTR(suspend_mem_microvolts, 0444,
494 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495
496static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
497 struct device_attribute *attr, char *buf)
498{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100499 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
502}
David Brownell7ad68e22008-11-11 17:39:02 -0800503static DEVICE_ATTR(suspend_disk_microvolts, 0444,
504 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505
506static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
507 struct device_attribute *attr, char *buf)
508{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100509 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
512}
David Brownell7ad68e22008-11-11 17:39:02 -0800513static DEVICE_ATTR(suspend_standby_microvolts, 0444,
514 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100519 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520
David Brownell4fca9542008-11-11 17:38:53 -0800521 return regulator_print_opmode(buf,
522 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523}
David Brownell7ad68e22008-11-11 17:39:02 -0800524static DEVICE_ATTR(suspend_mem_mode, 0444,
525 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
527static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
528 struct device_attribute *attr, char *buf)
529{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100530 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531
David Brownell4fca9542008-11-11 17:38:53 -0800532 return regulator_print_opmode(buf,
533 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534}
David Brownell7ad68e22008-11-11 17:39:02 -0800535static DEVICE_ATTR(suspend_disk_mode, 0444,
536 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
538static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
539 struct device_attribute *attr, char *buf)
540{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100541 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542
David Brownell4fca9542008-11-11 17:38:53 -0800543 return regulator_print_opmode(buf,
544 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545}
David Brownell7ad68e22008-11-11 17:39:02 -0800546static DEVICE_ATTR(suspend_standby_mode, 0444,
547 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548
549static ssize_t regulator_suspend_mem_state_show(struct device *dev,
550 struct device_attribute *attr, char *buf)
551{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100552 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553
David Brownell4fca9542008-11-11 17:38:53 -0800554 return regulator_print_state(buf,
555 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556}
David Brownell7ad68e22008-11-11 17:39:02 -0800557static DEVICE_ATTR(suspend_mem_state, 0444,
558 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559
560static ssize_t regulator_suspend_disk_state_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100563 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100564
David Brownell4fca9542008-11-11 17:38:53 -0800565 return regulator_print_state(buf,
566 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567}
David Brownell7ad68e22008-11-11 17:39:02 -0800568static DEVICE_ATTR(suspend_disk_state, 0444,
569 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100570
571static ssize_t regulator_suspend_standby_state_show(struct device *dev,
572 struct device_attribute *attr, char *buf)
573{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100574 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575
David Brownell4fca9542008-11-11 17:38:53 -0800576 return regulator_print_state(buf,
577 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578}
David Brownell7ad68e22008-11-11 17:39:02 -0800579static DEVICE_ATTR(suspend_standby_state, 0444,
580 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100581
David Brownell7ad68e22008-11-11 17:39:02 -0800582
583/*
584 * These are the only attributes are present for all regulators.
585 * Other attributes are a function of regulator functionality.
586 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100587static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100588 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
590 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591 __ATTR_NULL,
592};
593
594static void regulator_dev_release(struct device *dev)
595{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100596 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597 kfree(rdev);
598}
599
600static struct class regulator_class = {
601 .name = "regulator",
602 .dev_release = regulator_dev_release,
603 .dev_attrs = regulator_dev_attrs,
604};
605
606/* Calculate the new optimum regulator operating mode based on the new total
607 * consumer load. All locks held by caller */
608static void drms_uA_update(struct regulator_dev *rdev)
609{
610 struct regulator *sibling;
611 int current_uA = 0, output_uV, input_uV, err;
612 unsigned int mode;
613
614 err = regulator_check_drms(rdev);
615 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000616 (!rdev->desc->ops->get_voltage &&
617 !rdev->desc->ops->get_voltage_sel) ||
618 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300619 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
621 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000622 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623 if (output_uV <= 0)
624 return;
625
626 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000627 input_uV = 0;
628 if (rdev->supply)
629 input_uV = _regulator_get_voltage(rdev);
630 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631 input_uV = rdev->constraints->input_uV;
632 if (input_uV <= 0)
633 return;
634
635 /* calc total requested load */
636 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100637 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638
639 /* now get the optimum mode for our new total regulator load */
640 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
641 output_uV, current_uA);
642
643 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900644 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645 if (err == 0)
646 rdev->desc->ops->set_mode(rdev, mode);
647}
648
649static int suspend_set_state(struct regulator_dev *rdev,
650 struct regulator_state *rstate)
651{
652 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100653 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654
Mark Brown638f85c2009-10-22 16:31:33 +0100655 can_set_state = rdev->desc->ops->set_suspend_enable &&
656 rdev->desc->ops->set_suspend_disable;
657
658 /* If we have no suspend mode configration don't set anything;
659 * only warn if the driver actually makes the suspend mode
660 * configurable.
661 */
662 if (!rstate->enabled && !rstate->disabled) {
663 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800664 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100665 return 0;
666 }
667
668 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800669 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100670 return -EINVAL;
671 }
672
673 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800674 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100675 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100676 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100677
678 if (rstate->enabled)
679 ret = rdev->desc->ops->set_suspend_enable(rdev);
680 else
681 ret = rdev->desc->ops->set_suspend_disable(rdev);
682 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800683 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100684 return ret;
685 }
686
687 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
688 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
689 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800690 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100691 return ret;
692 }
693 }
694
695 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
696 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
697 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800698 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100699 return ret;
700 }
701 }
702 return ret;
703}
704
705/* locks held by caller */
706static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
707{
708 if (!rdev->constraints)
709 return -EINVAL;
710
711 switch (state) {
712 case PM_SUSPEND_STANDBY:
713 return suspend_set_state(rdev,
714 &rdev->constraints->state_standby);
715 case PM_SUSPEND_MEM:
716 return suspend_set_state(rdev,
717 &rdev->constraints->state_mem);
718 case PM_SUSPEND_MAX:
719 return suspend_set_state(rdev,
720 &rdev->constraints->state_disk);
721 default:
722 return -EINVAL;
723 }
724}
725
726static void print_constraints(struct regulator_dev *rdev)
727{
728 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000729 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100730 int count = 0;
731 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100732
Mark Brown8f031b42009-10-22 16:31:31 +0100733 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100734 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100735 count += sprintf(buf + count, "%d mV ",
736 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100737 else
Mark Brown8f031b42009-10-22 16:31:31 +0100738 count += sprintf(buf + count, "%d <--> %d mV ",
739 constraints->min_uV / 1000,
740 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100741 }
Mark Brown8f031b42009-10-22 16:31:31 +0100742
743 if (!constraints->min_uV ||
744 constraints->min_uV != constraints->max_uV) {
745 ret = _regulator_get_voltage(rdev);
746 if (ret > 0)
747 count += sprintf(buf + count, "at %d mV ", ret / 1000);
748 }
749
Mark Brownbf5892a2011-05-08 22:13:37 +0100750 if (constraints->uV_offset)
751 count += sprintf(buf, "%dmV offset ",
752 constraints->uV_offset / 1000);
753
Mark Brown8f031b42009-10-22 16:31:31 +0100754 if (constraints->min_uA && constraints->max_uA) {
755 if (constraints->min_uA == constraints->max_uA)
756 count += sprintf(buf + count, "%d mA ",
757 constraints->min_uA / 1000);
758 else
759 count += sprintf(buf + count, "%d <--> %d mA ",
760 constraints->min_uA / 1000,
761 constraints->max_uA / 1000);
762 }
763
764 if (!constraints->min_uA ||
765 constraints->min_uA != constraints->max_uA) {
766 ret = _regulator_get_current_limit(rdev);
767 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400768 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100769 }
770
Liam Girdwood414c70c2008-04-30 15:59:04 +0100771 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
772 count += sprintf(buf + count, "fast ");
773 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
774 count += sprintf(buf + count, "normal ");
775 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
776 count += sprintf(buf + count, "idle ");
777 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
778 count += sprintf(buf + count, "standby");
779
Mark Brown13ce29f2010-12-17 16:04:12 +0000780 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100781}
782
Mark Browne79055d2009-10-19 15:53:50 +0100783static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100784 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100785{
786 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100787 int ret;
788
789 /* do we need to apply the constraint voltage */
790 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000791 rdev->constraints->min_uV == rdev->constraints->max_uV) {
792 ret = _regulator_do_set_voltage(rdev,
793 rdev->constraints->min_uV,
794 rdev->constraints->max_uV);
795 if (ret < 0) {
796 rdev_err(rdev, "failed to apply %duV constraint\n",
797 rdev->constraints->min_uV);
Mark Brown75790252010-12-12 14:25:50 +0000798 return ret;
799 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100800 }
Mark Browne79055d2009-10-19 15:53:50 +0100801
802 /* constrain machine-level voltage specs to fit
803 * the actual range supported by this regulator.
804 */
805 if (ops->list_voltage && rdev->desc->n_voltages) {
806 int count = rdev->desc->n_voltages;
807 int i;
808 int min_uV = INT_MAX;
809 int max_uV = INT_MIN;
810 int cmin = constraints->min_uV;
811 int cmax = constraints->max_uV;
812
813 /* it's safe to autoconfigure fixed-voltage supplies
814 and the constraints are used by list_voltage. */
815 if (count == 1 && !cmin) {
816 cmin = 1;
817 cmax = INT_MAX;
818 constraints->min_uV = cmin;
819 constraints->max_uV = cmax;
820 }
821
822 /* voltage constraints are optional */
823 if ((cmin == 0) && (cmax == 0))
824 return 0;
825
826 /* else require explicit machine-level constraints */
827 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800828 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100829 return -EINVAL;
830 }
831
832 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
833 for (i = 0; i < count; i++) {
834 int value;
835
836 value = ops->list_voltage(rdev, i);
837 if (value <= 0)
838 continue;
839
840 /* maybe adjust [min_uV..max_uV] */
841 if (value >= cmin && value < min_uV)
842 min_uV = value;
843 if (value <= cmax && value > max_uV)
844 max_uV = value;
845 }
846
847 /* final: [min_uV..max_uV] valid iff constraints valid */
848 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800849 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100850 return -EINVAL;
851 }
852
853 /* use regulator's subset of machine constraints */
854 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800855 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
856 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100857 constraints->min_uV = min_uV;
858 }
859 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800860 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
861 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100862 constraints->max_uV = max_uV;
863 }
864 }
865
866 return 0;
867}
868
Liam Girdwooda5766f12008-10-10 13:22:20 +0100869/**
870 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000871 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000872 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100873 *
874 * Allows platform initialisation code to define and constrain
875 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
876 * Constraints *must* be set by platform code in order for some
877 * regulator operations to proceed i.e. set_voltage, set_current_limit,
878 * set_mode.
879 */
880static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000881 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100882{
883 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100884 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100885
Mark Brownf8c12fe2010-11-29 15:55:17 +0000886 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
887 GFP_KERNEL);
888 if (!rdev->constraints)
889 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100890
Mark Brownf8c12fe2010-11-29 15:55:17 +0000891 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100892 if (ret != 0)
893 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800894
Liam Girdwooda5766f12008-10-10 13:22:20 +0100895 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100896 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000897 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100898 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800899 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100900 goto out;
901 }
902 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100903
Mark Browna3084662009-02-26 19:24:19 +0000904 if (constraints->initial_mode) {
905 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800906 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000907 ret = -EINVAL;
908 goto out;
909 }
910
Mark Brownf8c12fe2010-11-29 15:55:17 +0000911 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000912 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800913 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000914 goto out;
915 }
916 }
917
Mark Browncacf90f2009-03-02 16:32:46 +0000918 /* If the constraints say the regulator should be on at this point
919 * and we have control then make sure it is enabled.
920 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000921 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
922 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100923 ret = ops->enable(rdev);
924 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800925 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100926 goto out;
927 }
928 }
929
Liam Girdwooda5766f12008-10-10 13:22:20 +0100930 print_constraints(rdev);
Axel Lin1a6958e72011-07-15 10:50:43 +0800931 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100932out:
Axel Lin1a6958e72011-07-15 10:50:43 +0800933 kfree(rdev->constraints);
934 rdev->constraints = NULL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100935 return ret;
936}
937
938/**
939 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000940 * @rdev: regulator name
941 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100942 *
943 * Called by platform initialisation code to set the supply regulator for this
944 * regulator. This ensures that a regulators supply will also be enabled by the
945 * core if it's child is enabled.
946 */
947static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100948 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100949{
950 int err;
951
Mark Brown3801b862011-06-09 16:22:22 +0100952 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
953
954 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
955 if (IS_ERR(rdev->supply)) {
956 err = PTR_ERR(rdev->supply);
957 rdev->supply = NULL;
958 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 }
Mark Brown3801b862011-06-09 16:22:22 +0100960
961 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962}
963
964/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800965 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000966 * @rdev: regulator source
967 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100968 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000969 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100970 *
971 * Allows platform initialisation code to map physical regulator
972 * sources to symbolic names for supplies for use by devices. Devices
973 * should use these symbolic names to request regulators, avoiding the
974 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100975 *
976 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100977 */
978static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100979 struct device *consumer_dev, const char *consumer_dev_name,
980 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981{
982 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100983 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100984
Mark Brown40f92442009-06-17 17:56:39 +0100985 if (consumer_dev && consumer_dev_name)
986 return -EINVAL;
987
988 if (!consumer_dev_name && consumer_dev)
989 consumer_dev_name = dev_name(consumer_dev);
990
Liam Girdwooda5766f12008-10-10 13:22:20 +0100991 if (supply == NULL)
992 return -EINVAL;
993
Mark Brown9ed20992009-07-21 16:00:26 +0100994 if (consumer_dev_name != NULL)
995 has_dev = 1;
996 else
997 has_dev = 0;
998
David Brownell6001e132008-12-31 12:54:19 +0000999 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +03001000 if (node->dev_name && consumer_dev_name) {
1001 if (strcmp(node->dev_name, consumer_dev_name) != 0)
1002 continue;
1003 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +00001004 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +03001005 }
1006
David Brownell6001e132008-12-31 12:54:19 +00001007 if (strcmp(node->supply, supply) != 0)
1008 continue;
1009
1010 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001011 dev_name(&node->regulator->dev),
1012 node->regulator->desc->name,
1013 supply,
1014 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001015 return -EBUSY;
1016 }
1017
Mark Brown9ed20992009-07-21 16:00:26 +01001018 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001019 if (node == NULL)
1020 return -ENOMEM;
1021
1022 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001023 node->supply = supply;
1024
Mark Brown9ed20992009-07-21 16:00:26 +01001025 if (has_dev) {
1026 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1027 if (node->dev_name == NULL) {
1028 kfree(node);
1029 return -ENOMEM;
1030 }
Mark Brown40f92442009-06-17 17:56:39 +01001031 }
1032
Liam Girdwooda5766f12008-10-10 13:22:20 +01001033 list_add(&node->list, &regulator_map_list);
1034 return 0;
1035}
1036
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001037static void unset_regulator_supplies(struct regulator_dev *rdev)
1038{
1039 struct regulator_map *node, *n;
1040
1041 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1042 if (rdev == node->regulator) {
1043 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001044 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001045 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001046 }
1047 }
1048}
1049
Mark Brownf5726ae2011-06-09 16:22:20 +01001050#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001051
1052static struct regulator *create_regulator(struct regulator_dev *rdev,
1053 struct device *dev,
1054 const char *supply_name)
1055{
1056 struct regulator *regulator;
1057 char buf[REG_STR_SIZE];
1058 int err, size;
1059
1060 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1061 if (regulator == NULL)
1062 return NULL;
1063
1064 mutex_lock(&rdev->mutex);
1065 regulator->rdev = rdev;
1066 list_add(&regulator->list, &rdev->consumer_list);
1067
1068 if (dev) {
1069 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001070 size = scnprintf(buf, REG_STR_SIZE,
1071 "microamps_requested_%s-%s",
1072 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001073 if (size >= REG_STR_SIZE)
1074 goto overflow_err;
1075
1076 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001077 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001078 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1079 if (regulator->dev_attr.attr.name == NULL)
1080 goto attr_name_err;
1081
Liam Girdwood414c70c2008-04-30 15:59:04 +01001082 regulator->dev_attr.attr.mode = 0444;
1083 regulator->dev_attr.show = device_requested_uA_show;
1084 err = device_create_file(dev, &regulator->dev_attr);
1085 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001086 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001087 goto attr_name_err;
1088 }
1089
1090 /* also add a link to the device sysfs entry */
1091 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1092 dev->kobj.name, supply_name);
1093 if (size >= REG_STR_SIZE)
1094 goto attr_err;
1095
1096 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1097 if (regulator->supply_name == NULL)
1098 goto attr_err;
1099
1100 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1101 buf);
1102 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001103 rdev_warn(rdev, "could not add device link %s err %d\n",
1104 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001105 goto link_name_err;
1106 }
Mark Brown5de70512011-06-19 13:33:16 +01001107 } else {
1108 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1109 if (regulator->supply_name == NULL)
1110 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001111 }
Mark Brown5de70512011-06-19 13:33:16 +01001112
1113#ifdef CONFIG_DEBUG_FS
1114 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1115 rdev->debugfs);
1116 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1117 rdev_warn(rdev, "Failed to create debugfs directory\n");
1118 regulator->debugfs = NULL;
1119 } else {
1120 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1121 &regulator->uA_load);
1122 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1123 &regulator->min_uV);
1124 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1125 &regulator->max_uV);
1126 }
1127#endif
1128
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129 mutex_unlock(&rdev->mutex);
1130 return regulator;
1131link_name_err:
1132 kfree(regulator->supply_name);
1133attr_err:
1134 device_remove_file(regulator->dev, &regulator->dev_attr);
1135attr_name_err:
1136 kfree(regulator->dev_attr.attr.name);
1137overflow_err:
1138 list_del(&regulator->list);
1139 kfree(regulator);
1140 mutex_unlock(&rdev->mutex);
1141 return NULL;
1142}
1143
Mark Brown31aae2b2009-12-21 12:21:52 +00001144static int _regulator_get_enable_time(struct regulator_dev *rdev)
1145{
1146 if (!rdev->desc->ops->enable_time)
1147 return 0;
1148 return rdev->desc->ops->enable_time(rdev);
1149}
1150
Mark Brown5ffbd132009-07-21 16:00:23 +01001151/* Internal regulator request function */
1152static struct regulator *_regulator_get(struct device *dev, const char *id,
1153 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001154{
1155 struct regulator_dev *rdev;
1156 struct regulator_map *map;
1157 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001158 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001159 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160
1161 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001162 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001163 return regulator;
1164 }
1165
Mark Brown40f92442009-06-17 17:56:39 +01001166 if (dev)
1167 devname = dev_name(dev);
1168
Liam Girdwood414c70c2008-04-30 15:59:04 +01001169 mutex_lock(&regulator_list_mutex);
1170
1171 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001172 /* If the mapping has a device set up it must match */
1173 if (map->dev_name &&
1174 (!devname || strcmp(map->dev_name, devname)))
1175 continue;
1176
1177 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001178 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001179 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001180 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001181 }
Mark Brown34abbd62010-02-12 10:18:08 +00001182
Mark Brown688fe992010-10-05 19:18:32 -07001183 if (board_wants_dummy_regulator) {
1184 rdev = dummy_regulator_rdev;
1185 goto found;
1186 }
1187
Mark Brown34abbd62010-02-12 10:18:08 +00001188#ifdef CONFIG_REGULATOR_DUMMY
1189 if (!devname)
1190 devname = "deviceless";
1191
1192 /* If the board didn't flag that it was fully constrained then
1193 * substitute in a dummy regulator so consumers can continue.
1194 */
1195 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001196 pr_warn("%s supply %s not found, using dummy regulator\n",
1197 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001198 rdev = dummy_regulator_rdev;
1199 goto found;
1200 }
1201#endif
1202
Liam Girdwood414c70c2008-04-30 15:59:04 +01001203 mutex_unlock(&regulator_list_mutex);
1204 return regulator;
1205
1206found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001207 if (rdev->exclusive) {
1208 regulator = ERR_PTR(-EPERM);
1209 goto out;
1210 }
1211
1212 if (exclusive && rdev->open_count) {
1213 regulator = ERR_PTR(-EBUSY);
1214 goto out;
1215 }
1216
Liam Girdwooda5766f12008-10-10 13:22:20 +01001217 if (!try_module_get(rdev->owner))
1218 goto out;
1219
Liam Girdwood414c70c2008-04-30 15:59:04 +01001220 regulator = create_regulator(rdev, dev, id);
1221 if (regulator == NULL) {
1222 regulator = ERR_PTR(-ENOMEM);
1223 module_put(rdev->owner);
1224 }
1225
Mark Brown5ffbd132009-07-21 16:00:23 +01001226 rdev->open_count++;
1227 if (exclusive) {
1228 rdev->exclusive = 1;
1229
1230 ret = _regulator_is_enabled(rdev);
1231 if (ret > 0)
1232 rdev->use_count = 1;
1233 else
1234 rdev->use_count = 0;
1235 }
1236
Liam Girdwooda5766f12008-10-10 13:22:20 +01001237out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001238 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001239
Liam Girdwood414c70c2008-04-30 15:59:04 +01001240 return regulator;
1241}
Mark Brown5ffbd132009-07-21 16:00:23 +01001242
1243/**
1244 * regulator_get - lookup and obtain a reference to a regulator.
1245 * @dev: device for regulator "consumer"
1246 * @id: Supply name or regulator ID.
1247 *
1248 * Returns a struct regulator corresponding to the regulator producer,
1249 * or IS_ERR() condition containing errno.
1250 *
1251 * Use of supply names configured via regulator_set_device_supply() is
1252 * strongly encouraged. It is recommended that the supply name used
1253 * should match the name used for the supply and/or the relevant
1254 * device pins in the datasheet.
1255 */
1256struct regulator *regulator_get(struct device *dev, const char *id)
1257{
1258 return _regulator_get(dev, id, 0);
1259}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260EXPORT_SYMBOL_GPL(regulator_get);
1261
1262/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001263 * regulator_get_exclusive - obtain exclusive access to a regulator.
1264 * @dev: device for regulator "consumer"
1265 * @id: Supply name or regulator ID.
1266 *
1267 * Returns a struct regulator corresponding to the regulator producer,
1268 * or IS_ERR() condition containing errno. Other consumers will be
1269 * unable to obtain this reference is held and the use count for the
1270 * regulator will be initialised to reflect the current state of the
1271 * regulator.
1272 *
1273 * This is intended for use by consumers which cannot tolerate shared
1274 * use of the regulator such as those which need to force the
1275 * regulator off for correct operation of the hardware they are
1276 * controlling.
1277 *
1278 * Use of supply names configured via regulator_set_device_supply() is
1279 * strongly encouraged. It is recommended that the supply name used
1280 * should match the name used for the supply and/or the relevant
1281 * device pins in the datasheet.
1282 */
1283struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1284{
1285 return _regulator_get(dev, id, 1);
1286}
1287EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1288
1289/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001290 * regulator_put - "free" the regulator source
1291 * @regulator: regulator source
1292 *
1293 * Note: drivers must ensure that all regulator_enable calls made on this
1294 * regulator source are balanced by regulator_disable calls prior to calling
1295 * this function.
1296 */
1297void regulator_put(struct regulator *regulator)
1298{
1299 struct regulator_dev *rdev;
1300
1301 if (regulator == NULL || IS_ERR(regulator))
1302 return;
1303
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304 mutex_lock(&regulator_list_mutex);
1305 rdev = regulator->rdev;
1306
Mark Brown5de70512011-06-19 13:33:16 +01001307#ifdef CONFIG_DEBUG_FS
1308 debugfs_remove_recursive(regulator->debugfs);
1309#endif
1310
Liam Girdwood414c70c2008-04-30 15:59:04 +01001311 /* remove any sysfs entries */
1312 if (regulator->dev) {
1313 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001314 device_remove_file(regulator->dev, &regulator->dev_attr);
1315 kfree(regulator->dev_attr.attr.name);
1316 }
Mark Brown5de70512011-06-19 13:33:16 +01001317 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001318 list_del(&regulator->list);
1319 kfree(regulator);
1320
Mark Brown5ffbd132009-07-21 16:00:23 +01001321 rdev->open_count--;
1322 rdev->exclusive = 0;
1323
Liam Girdwood414c70c2008-04-30 15:59:04 +01001324 module_put(rdev->owner);
1325 mutex_unlock(&regulator_list_mutex);
1326}
1327EXPORT_SYMBOL_GPL(regulator_put);
1328
Mark Brown9a2372f2009-08-03 18:49:57 +01001329static int _regulator_can_change_status(struct regulator_dev *rdev)
1330{
1331 if (!rdev->constraints)
1332 return 0;
1333
1334 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1335 return 1;
1336 else
1337 return 0;
1338}
1339
Liam Girdwood414c70c2008-04-30 15:59:04 +01001340/* locks held by regulator_enable() */
1341static int _regulator_enable(struct regulator_dev *rdev)
1342{
Mark Brown31aae2b2009-12-21 12:21:52 +00001343 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001344
Liam Girdwood414c70c2008-04-30 15:59:04 +01001345 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001346 if (rdev->constraints &&
1347 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1348 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349
Mark Brown9a2372f2009-08-03 18:49:57 +01001350 if (rdev->use_count == 0) {
1351 /* The regulator may on if it's not switchable or left on */
1352 ret = _regulator_is_enabled(rdev);
1353 if (ret == -EINVAL || ret == 0) {
1354 if (!_regulator_can_change_status(rdev))
1355 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001356
Mark Brown31aae2b2009-12-21 12:21:52 +00001357 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001358 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001359
1360 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001361 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001362 ret = _regulator_get_enable_time(rdev);
1363 if (ret >= 0) {
1364 delay = ret;
1365 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001366 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001367 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001368 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001369 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001370
Mark Brown02fa3ec2010-11-10 14:38:30 +00001371 trace_regulator_enable(rdev_get_name(rdev));
1372
Mark Brown31aae2b2009-12-21 12:21:52 +00001373 /* Allow the regulator to ramp; it would be useful
1374 * to extend this for bulk operations so that the
1375 * regulators can ramp together. */
1376 ret = rdev->desc->ops->enable(rdev);
1377 if (ret < 0)
1378 return ret;
1379
Mark Brown02fa3ec2010-11-10 14:38:30 +00001380 trace_regulator_enable_delay(rdev_get_name(rdev));
1381
Axel Line36c1df2010-11-05 21:51:32 +08001382 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001383 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001384 udelay(delay % 1000);
1385 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001386 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001387 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001388
Mark Brown02fa3ec2010-11-10 14:38:30 +00001389 trace_regulator_enable_complete(rdev_get_name(rdev));
1390
Linus Walleija7433cf2009-08-26 12:54:04 +02001391 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001392 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393 return ret;
1394 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001395 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 }
1397
Mark Brown9a2372f2009-08-03 18:49:57 +01001398 rdev->use_count++;
1399
1400 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401}
1402
1403/**
1404 * regulator_enable - enable regulator output
1405 * @regulator: regulator source
1406 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001407 * Request that the regulator be enabled with the regulator output at
1408 * the predefined voltage or current value. Calls to regulator_enable()
1409 * must be balanced with calls to regulator_disable().
1410 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001412 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001413 */
1414int regulator_enable(struct regulator *regulator)
1415{
David Brownell412aec62008-11-16 11:44:46 -08001416 struct regulator_dev *rdev = regulator->rdev;
1417 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418
Mark Brown3801b862011-06-09 16:22:22 +01001419 if (rdev->supply) {
1420 ret = regulator_enable(rdev->supply);
1421 if (ret != 0)
1422 return ret;
1423 }
1424
David Brownell412aec62008-11-16 11:44:46 -08001425 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001426 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001427 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001428
Heiko Stübnerd1685e42011-10-14 18:00:29 +02001429 if (ret != 0 && rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01001430 regulator_disable(rdev->supply);
1431
Liam Girdwood414c70c2008-04-30 15:59:04 +01001432 return ret;
1433}
1434EXPORT_SYMBOL_GPL(regulator_enable);
1435
1436/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001437static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001438{
1439 int ret = 0;
1440
David Brownellcd94b502009-03-11 16:43:34 -08001441 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001442 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001443 return -EIO;
1444
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001446 if (rdev->use_count == 1 &&
1447 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001448
1449 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001450 if (_regulator_can_change_status(rdev) &&
1451 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001452 trace_regulator_disable(rdev_get_name(rdev));
1453
Liam Girdwood414c70c2008-04-30 15:59:04 +01001454 ret = rdev->desc->ops->disable(rdev);
1455 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001456 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001457 return ret;
1458 }
Mark Brown84b68262009-12-01 21:12:27 +00001459
Mark Brown02fa3ec2010-11-10 14:38:30 +00001460 trace_regulator_disable_complete(rdev_get_name(rdev));
1461
Mark Brown84b68262009-12-01 21:12:27 +00001462 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1463 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001464 }
1465
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466 rdev->use_count = 0;
1467 } else if (rdev->use_count > 1) {
1468
1469 if (rdev->constraints &&
1470 (rdev->constraints->valid_ops_mask &
1471 REGULATOR_CHANGE_DRMS))
1472 drms_uA_update(rdev);
1473
1474 rdev->use_count--;
1475 }
Mark Brown3801b862011-06-09 16:22:22 +01001476
Liam Girdwood414c70c2008-04-30 15:59:04 +01001477 return ret;
1478}
1479
1480/**
1481 * regulator_disable - disable regulator output
1482 * @regulator: regulator source
1483 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001484 * Disable the regulator output voltage or current. Calls to
1485 * regulator_enable() must be balanced with calls to
1486 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001487 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001489 * devices have it enabled, the regulator device supports disabling and
1490 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491 */
1492int regulator_disable(struct regulator *regulator)
1493{
David Brownell412aec62008-11-16 11:44:46 -08001494 struct regulator_dev *rdev = regulator->rdev;
1495 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496
David Brownell412aec62008-11-16 11:44:46 -08001497 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001498 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001499 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001500
Mark Brown3801b862011-06-09 16:22:22 +01001501 if (ret == 0 && rdev->supply)
1502 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001503
Liam Girdwood414c70c2008-04-30 15:59:04 +01001504 return ret;
1505}
1506EXPORT_SYMBOL_GPL(regulator_disable);
1507
1508/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001509static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001510{
1511 int ret = 0;
1512
1513 /* force disable */
1514 if (rdev->desc->ops->disable) {
1515 /* ah well, who wants to live forever... */
1516 ret = rdev->desc->ops->disable(rdev);
1517 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001518 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519 return ret;
1520 }
1521 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001522 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1523 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524 }
1525
Liam Girdwood414c70c2008-04-30 15:59:04 +01001526 return ret;
1527}
1528
1529/**
1530 * regulator_force_disable - force disable regulator output
1531 * @regulator: regulator source
1532 *
1533 * Forcibly disable the regulator output voltage or current.
1534 * NOTE: this *will* disable the regulator output even if other consumer
1535 * devices have it enabled. This should be used for situations when device
1536 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1537 */
1538int regulator_force_disable(struct regulator *regulator)
1539{
Mark Brown82d15832011-05-09 11:41:02 +02001540 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541 int ret;
1542
Mark Brown82d15832011-05-09 11:41:02 +02001543 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001545 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001546 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001547
Mark Brown3801b862011-06-09 16:22:22 +01001548 if (rdev->supply)
1549 while (rdev->open_count--)
1550 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001551
Liam Girdwood414c70c2008-04-30 15:59:04 +01001552 return ret;
1553}
1554EXPORT_SYMBOL_GPL(regulator_force_disable);
1555
Mark Brownda07ecd2011-09-11 09:53:50 +01001556static void regulator_disable_work(struct work_struct *work)
1557{
1558 struct regulator_dev *rdev = container_of(work, struct regulator_dev,
1559 disable_work.work);
1560 int count, i, ret;
1561
1562 mutex_lock(&rdev->mutex);
1563
1564 BUG_ON(!rdev->deferred_disables);
1565
1566 count = rdev->deferred_disables;
1567 rdev->deferred_disables = 0;
1568
1569 for (i = 0; i < count; i++) {
1570 ret = _regulator_disable(rdev);
1571 if (ret != 0)
1572 rdev_err(rdev, "Deferred disable failed: %d\n", ret);
1573 }
1574
1575 mutex_unlock(&rdev->mutex);
1576
1577 if (rdev->supply) {
1578 for (i = 0; i < count; i++) {
1579 ret = regulator_disable(rdev->supply);
1580 if (ret != 0) {
1581 rdev_err(rdev,
1582 "Supply disable failed: %d\n", ret);
1583 }
1584 }
1585 }
1586}
1587
1588/**
1589 * regulator_disable_deferred - disable regulator output with delay
1590 * @regulator: regulator source
1591 * @ms: miliseconds until the regulator is disabled
1592 *
1593 * Execute regulator_disable() on the regulator after a delay. This
1594 * is intended for use with devices that require some time to quiesce.
1595 *
1596 * NOTE: this will only disable the regulator output if no other consumer
1597 * devices have it enabled, the regulator device supports disabling and
1598 * machine constraints permit this operation.
1599 */
1600int regulator_disable_deferred(struct regulator *regulator, int ms)
1601{
1602 struct regulator_dev *rdev = regulator->rdev;
Mark Brownaa598022011-10-03 22:42:43 +01001603 int ret;
Mark Brownda07ecd2011-09-11 09:53:50 +01001604
1605 mutex_lock(&rdev->mutex);
1606 rdev->deferred_disables++;
1607 mutex_unlock(&rdev->mutex);
1608
Mark Brownaa598022011-10-03 22:42:43 +01001609 ret = schedule_delayed_work(&rdev->disable_work,
1610 msecs_to_jiffies(ms));
1611 if (ret < 0)
1612 return ret;
1613 else
1614 return 0;
Mark Brownda07ecd2011-09-11 09:53:50 +01001615}
1616EXPORT_SYMBOL_GPL(regulator_disable_deferred);
1617
Liam Girdwood414c70c2008-04-30 15:59:04 +01001618static int _regulator_is_enabled(struct regulator_dev *rdev)
1619{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001620 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001621 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001622 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001623
Mark Brown93325462009-08-03 18:49:56 +01001624 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625}
1626
1627/**
1628 * regulator_is_enabled - is the regulator output enabled
1629 * @regulator: regulator source
1630 *
David Brownell412aec62008-11-16 11:44:46 -08001631 * Returns positive if the regulator driver backing the source/client
1632 * has requested that the device be enabled, zero if it hasn't, else a
1633 * negative errno code.
1634 *
1635 * Note that the device backing this regulator handle can have multiple
1636 * users, so it might be enabled even if regulator_enable() was never
1637 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 */
1639int regulator_is_enabled(struct regulator *regulator)
1640{
Mark Brown93325462009-08-03 18:49:56 +01001641 int ret;
1642
1643 mutex_lock(&regulator->rdev->mutex);
1644 ret = _regulator_is_enabled(regulator->rdev);
1645 mutex_unlock(&regulator->rdev->mutex);
1646
1647 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001648}
1649EXPORT_SYMBOL_GPL(regulator_is_enabled);
1650
1651/**
David Brownell4367cfd2009-02-26 11:48:36 -08001652 * regulator_count_voltages - count regulator_list_voltage() selectors
1653 * @regulator: regulator source
1654 *
1655 * Returns number of selectors, or negative errno. Selectors are
1656 * numbered starting at zero, and typically correspond to bitfields
1657 * in hardware registers.
1658 */
1659int regulator_count_voltages(struct regulator *regulator)
1660{
1661 struct regulator_dev *rdev = regulator->rdev;
1662
1663 return rdev->desc->n_voltages ? : -EINVAL;
1664}
1665EXPORT_SYMBOL_GPL(regulator_count_voltages);
1666
1667/**
1668 * regulator_list_voltage - enumerate supported voltages
1669 * @regulator: regulator source
1670 * @selector: identify voltage to list
1671 * Context: can sleep
1672 *
1673 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001674 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001675 * negative errno.
1676 */
1677int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1678{
1679 struct regulator_dev *rdev = regulator->rdev;
1680 struct regulator_ops *ops = rdev->desc->ops;
1681 int ret;
1682
1683 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1684 return -EINVAL;
1685
1686 mutex_lock(&rdev->mutex);
1687 ret = ops->list_voltage(rdev, selector);
1688 mutex_unlock(&rdev->mutex);
1689
1690 if (ret > 0) {
1691 if (ret < rdev->constraints->min_uV)
1692 ret = 0;
1693 else if (ret > rdev->constraints->max_uV)
1694 ret = 0;
1695 }
1696
1697 return ret;
1698}
1699EXPORT_SYMBOL_GPL(regulator_list_voltage);
1700
1701/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001702 * regulator_is_supported_voltage - check if a voltage range can be supported
1703 *
1704 * @regulator: Regulator to check.
1705 * @min_uV: Minimum required voltage in uV.
1706 * @max_uV: Maximum required voltage in uV.
1707 *
1708 * Returns a boolean or a negative error code.
1709 */
1710int regulator_is_supported_voltage(struct regulator *regulator,
1711 int min_uV, int max_uV)
1712{
1713 int i, voltages, ret;
1714
1715 ret = regulator_count_voltages(regulator);
1716 if (ret < 0)
1717 return ret;
1718 voltages = ret;
1719
1720 for (i = 0; i < voltages; i++) {
1721 ret = regulator_list_voltage(regulator, i);
1722
1723 if (ret >= min_uV && ret <= max_uV)
1724 return 1;
1725 }
1726
1727 return 0;
1728}
1729
Mark Brown75790252010-12-12 14:25:50 +00001730static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1731 int min_uV, int max_uV)
1732{
1733 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001734 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001735 unsigned int selector;
1736
1737 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1738
Mark Brownbf5892a2011-05-08 22:13:37 +01001739 min_uV += rdev->constraints->uV_offset;
1740 max_uV += rdev->constraints->uV_offset;
1741
Mark Brown75790252010-12-12 14:25:50 +00001742 if (rdev->desc->ops->set_voltage) {
1743 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1744 &selector);
1745
1746 if (rdev->desc->ops->list_voltage)
1747 selector = rdev->desc->ops->list_voltage(rdev,
1748 selector);
1749 else
1750 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001751 } else if (rdev->desc->ops->set_voltage_sel) {
1752 int best_val = INT_MAX;
1753 int i;
1754
1755 selector = 0;
1756
1757 /* Find the smallest voltage that falls within the specified
1758 * range.
1759 */
1760 for (i = 0; i < rdev->desc->n_voltages; i++) {
1761 ret = rdev->desc->ops->list_voltage(rdev, i);
1762 if (ret < 0)
1763 continue;
1764
1765 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1766 best_val = ret;
1767 selector = i;
1768 }
1769 }
1770
Linus Walleij77af1b2642011-03-17 13:24:36 +01001771 /*
1772 * If we can't obtain the old selector there is not enough
1773 * info to call set_voltage_time_sel().
1774 */
1775 if (rdev->desc->ops->set_voltage_time_sel &&
1776 rdev->desc->ops->get_voltage_sel) {
1777 unsigned int old_selector = 0;
1778
1779 ret = rdev->desc->ops->get_voltage_sel(rdev);
1780 if (ret < 0)
1781 return ret;
1782 old_selector = ret;
1783 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1784 old_selector, selector);
1785 }
1786
Mark Browne8eef822010-12-12 14:36:17 +00001787 if (best_val != INT_MAX) {
1788 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1789 selector = best_val;
1790 } else {
1791 ret = -EINVAL;
1792 }
Mark Brown75790252010-12-12 14:25:50 +00001793 } else {
1794 ret = -EINVAL;
1795 }
1796
Linus Walleij77af1b2642011-03-17 13:24:36 +01001797 /* Insert any necessary delays */
1798 if (delay >= 1000) {
1799 mdelay(delay / 1000);
1800 udelay(delay % 1000);
1801 } else if (delay) {
1802 udelay(delay);
1803 }
1804
Mark Brownded06a52010-12-16 13:59:10 +00001805 if (ret == 0)
1806 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1807 NULL);
1808
Mark Brown75790252010-12-12 14:25:50 +00001809 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1810
1811 return ret;
1812}
1813
Mark Browna7a1ad92009-07-21 16:00:24 +01001814/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001815 * regulator_set_voltage - set regulator output voltage
1816 * @regulator: regulator source
1817 * @min_uV: Minimum required voltage in uV
1818 * @max_uV: Maximum acceptable voltage in uV
1819 *
1820 * Sets a voltage regulator to the desired output voltage. This can be set
1821 * during any regulator state. IOW, regulator can be disabled or enabled.
1822 *
1823 * If the regulator is enabled then the voltage will change to the new value
1824 * immediately otherwise if the regulator is disabled the regulator will
1825 * output at the new voltage when enabled.
1826 *
1827 * NOTE: If the regulator is shared between several devices then the lowest
1828 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001829 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001830 * calling this function otherwise this call will fail.
1831 */
1832int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1833{
1834 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001835 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001836
1837 mutex_lock(&rdev->mutex);
1838
Mark Brown95a3c232010-12-16 15:49:37 +00001839 /* If we're setting the same range as last time the change
1840 * should be a noop (some cpufreq implementations use the same
1841 * voltage for multiple frequencies, for example).
1842 */
1843 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1844 goto out;
1845
Liam Girdwood414c70c2008-04-30 15:59:04 +01001846 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001847 if (!rdev->desc->ops->set_voltage &&
1848 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001849 ret = -EINVAL;
1850 goto out;
1851 }
1852
1853 /* constraints check */
1854 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1855 if (ret < 0)
1856 goto out;
1857 regulator->min_uV = min_uV;
1858 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001859
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001860 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1861 if (ret < 0)
1862 goto out;
1863
Mark Brown75790252010-12-12 14:25:50 +00001864 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001865
Liam Girdwood414c70c2008-04-30 15:59:04 +01001866out:
1867 mutex_unlock(&rdev->mutex);
1868 return ret;
1869}
1870EXPORT_SYMBOL_GPL(regulator_set_voltage);
1871
Mark Brown606a2562010-12-16 15:49:36 +00001872/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001873 * regulator_set_voltage_time - get raise/fall time
1874 * @regulator: regulator source
1875 * @old_uV: starting voltage in microvolts
1876 * @new_uV: target voltage in microvolts
1877 *
1878 * Provided with the starting and ending voltage, this function attempts to
1879 * calculate the time in microseconds required to rise or fall to this new
1880 * voltage.
1881 */
1882int regulator_set_voltage_time(struct regulator *regulator,
1883 int old_uV, int new_uV)
1884{
1885 struct regulator_dev *rdev = regulator->rdev;
1886 struct regulator_ops *ops = rdev->desc->ops;
1887 int old_sel = -1;
1888 int new_sel = -1;
1889 int voltage;
1890 int i;
1891
1892 /* Currently requires operations to do this */
1893 if (!ops->list_voltage || !ops->set_voltage_time_sel
1894 || !rdev->desc->n_voltages)
1895 return -EINVAL;
1896
1897 for (i = 0; i < rdev->desc->n_voltages; i++) {
1898 /* We only look for exact voltage matches here */
1899 voltage = regulator_list_voltage(regulator, i);
1900 if (voltage < 0)
1901 return -EINVAL;
1902 if (voltage == 0)
1903 continue;
1904 if (voltage == old_uV)
1905 old_sel = i;
1906 if (voltage == new_uV)
1907 new_sel = i;
1908 }
1909
1910 if (old_sel < 0 || new_sel < 0)
1911 return -EINVAL;
1912
1913 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1914}
1915EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1916
1917/**
Mark Brown606a2562010-12-16 15:49:36 +00001918 * regulator_sync_voltage - re-apply last regulator output voltage
1919 * @regulator: regulator source
1920 *
1921 * Re-apply the last configured voltage. This is intended to be used
1922 * where some external control source the consumer is cooperating with
1923 * has caused the configured voltage to change.
1924 */
1925int regulator_sync_voltage(struct regulator *regulator)
1926{
1927 struct regulator_dev *rdev = regulator->rdev;
1928 int ret, min_uV, max_uV;
1929
1930 mutex_lock(&rdev->mutex);
1931
1932 if (!rdev->desc->ops->set_voltage &&
1933 !rdev->desc->ops->set_voltage_sel) {
1934 ret = -EINVAL;
1935 goto out;
1936 }
1937
1938 /* This is only going to work if we've had a voltage configured. */
1939 if (!regulator->min_uV && !regulator->max_uV) {
1940 ret = -EINVAL;
1941 goto out;
1942 }
1943
1944 min_uV = regulator->min_uV;
1945 max_uV = regulator->max_uV;
1946
1947 /* This should be a paranoia check... */
1948 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1949 if (ret < 0)
1950 goto out;
1951
1952 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1953 if (ret < 0)
1954 goto out;
1955
1956 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1957
1958out:
1959 mutex_unlock(&rdev->mutex);
1960 return ret;
1961}
1962EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1963
Liam Girdwood414c70c2008-04-30 15:59:04 +01001964static int _regulator_get_voltage(struct regulator_dev *rdev)
1965{
Mark Brownbf5892a2011-05-08 22:13:37 +01001966 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001967
1968 if (rdev->desc->ops->get_voltage_sel) {
1969 sel = rdev->desc->ops->get_voltage_sel(rdev);
1970 if (sel < 0)
1971 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001972 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08001973 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01001974 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08001975 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001976 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08001977 }
Mark Brownbf5892a2011-05-08 22:13:37 +01001978
Axel Lincb220d12011-05-23 20:08:10 +08001979 if (ret < 0)
1980 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01001981 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001982}
1983
1984/**
1985 * regulator_get_voltage - get regulator output voltage
1986 * @regulator: regulator source
1987 *
1988 * This returns the current regulator voltage in uV.
1989 *
1990 * NOTE: If the regulator is disabled it will return the voltage value. This
1991 * function should not be used to determine regulator state.
1992 */
1993int regulator_get_voltage(struct regulator *regulator)
1994{
1995 int ret;
1996
1997 mutex_lock(&regulator->rdev->mutex);
1998
1999 ret = _regulator_get_voltage(regulator->rdev);
2000
2001 mutex_unlock(&regulator->rdev->mutex);
2002
2003 return ret;
2004}
2005EXPORT_SYMBOL_GPL(regulator_get_voltage);
2006
2007/**
2008 * regulator_set_current_limit - set regulator output current limit
2009 * @regulator: regulator source
2010 * @min_uA: Minimuum supported current in uA
2011 * @max_uA: Maximum supported current in uA
2012 *
2013 * Sets current sink to the desired output current. This can be set during
2014 * any regulator state. IOW, regulator can be disabled or enabled.
2015 *
2016 * If the regulator is enabled then the current will change to the new value
2017 * immediately otherwise if the regulator is disabled the regulator will
2018 * output at the new current when enabled.
2019 *
2020 * NOTE: Regulator system constraints must be set for this regulator before
2021 * calling this function otherwise this call will fail.
2022 */
2023int regulator_set_current_limit(struct regulator *regulator,
2024 int min_uA, int max_uA)
2025{
2026 struct regulator_dev *rdev = regulator->rdev;
2027 int ret;
2028
2029 mutex_lock(&rdev->mutex);
2030
2031 /* sanity check */
2032 if (!rdev->desc->ops->set_current_limit) {
2033 ret = -EINVAL;
2034 goto out;
2035 }
2036
2037 /* constraints check */
2038 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
2039 if (ret < 0)
2040 goto out;
2041
2042 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
2043out:
2044 mutex_unlock(&rdev->mutex);
2045 return ret;
2046}
2047EXPORT_SYMBOL_GPL(regulator_set_current_limit);
2048
2049static int _regulator_get_current_limit(struct regulator_dev *rdev)
2050{
2051 int ret;
2052
2053 mutex_lock(&rdev->mutex);
2054
2055 /* sanity check */
2056 if (!rdev->desc->ops->get_current_limit) {
2057 ret = -EINVAL;
2058 goto out;
2059 }
2060
2061 ret = rdev->desc->ops->get_current_limit(rdev);
2062out:
2063 mutex_unlock(&rdev->mutex);
2064 return ret;
2065}
2066
2067/**
2068 * regulator_get_current_limit - get regulator output current
2069 * @regulator: regulator source
2070 *
2071 * This returns the current supplied by the specified current sink in uA.
2072 *
2073 * NOTE: If the regulator is disabled it will return the current value. This
2074 * function should not be used to determine regulator state.
2075 */
2076int regulator_get_current_limit(struct regulator *regulator)
2077{
2078 return _regulator_get_current_limit(regulator->rdev);
2079}
2080EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2081
2082/**
2083 * regulator_set_mode - set regulator operating mode
2084 * @regulator: regulator source
2085 * @mode: operating mode - one of the REGULATOR_MODE constants
2086 *
2087 * Set regulator operating mode to increase regulator efficiency or improve
2088 * regulation performance.
2089 *
2090 * NOTE: Regulator system constraints must be set for this regulator before
2091 * calling this function otherwise this call will fail.
2092 */
2093int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2094{
2095 struct regulator_dev *rdev = regulator->rdev;
2096 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302097 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002098
2099 mutex_lock(&rdev->mutex);
2100
2101 /* sanity check */
2102 if (!rdev->desc->ops->set_mode) {
2103 ret = -EINVAL;
2104 goto out;
2105 }
2106
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302107 /* return if the same mode is requested */
2108 if (rdev->desc->ops->get_mode) {
2109 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2110 if (regulator_curr_mode == mode) {
2111 ret = 0;
2112 goto out;
2113 }
2114 }
2115
Liam Girdwood414c70c2008-04-30 15:59:04 +01002116 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002117 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002118 if (ret < 0)
2119 goto out;
2120
2121 ret = rdev->desc->ops->set_mode(rdev, mode);
2122out:
2123 mutex_unlock(&rdev->mutex);
2124 return ret;
2125}
2126EXPORT_SYMBOL_GPL(regulator_set_mode);
2127
2128static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2129{
2130 int ret;
2131
2132 mutex_lock(&rdev->mutex);
2133
2134 /* sanity check */
2135 if (!rdev->desc->ops->get_mode) {
2136 ret = -EINVAL;
2137 goto out;
2138 }
2139
2140 ret = rdev->desc->ops->get_mode(rdev);
2141out:
2142 mutex_unlock(&rdev->mutex);
2143 return ret;
2144}
2145
2146/**
2147 * regulator_get_mode - get regulator operating mode
2148 * @regulator: regulator source
2149 *
2150 * Get the current regulator operating mode.
2151 */
2152unsigned int regulator_get_mode(struct regulator *regulator)
2153{
2154 return _regulator_get_mode(regulator->rdev);
2155}
2156EXPORT_SYMBOL_GPL(regulator_get_mode);
2157
2158/**
2159 * regulator_set_optimum_mode - set regulator optimum operating mode
2160 * @regulator: regulator source
2161 * @uA_load: load current
2162 *
2163 * Notifies the regulator core of a new device load. This is then used by
2164 * DRMS (if enabled by constraints) to set the most efficient regulator
2165 * operating mode for the new regulator loading.
2166 *
2167 * Consumer devices notify their supply regulator of the maximum power
2168 * they will require (can be taken from device datasheet in the power
2169 * consumption tables) when they change operational status and hence power
2170 * state. Examples of operational state changes that can affect power
2171 * consumption are :-
2172 *
2173 * o Device is opened / closed.
2174 * o Device I/O is about to begin or has just finished.
2175 * o Device is idling in between work.
2176 *
2177 * This information is also exported via sysfs to userspace.
2178 *
2179 * DRMS will sum the total requested load on the regulator and change
2180 * to the most efficient operating mode if platform constraints allow.
2181 *
2182 * Returns the new regulator mode or error.
2183 */
2184int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2185{
2186 struct regulator_dev *rdev = regulator->rdev;
2187 struct regulator *consumer;
2188 int ret, output_uV, input_uV, total_uA_load = 0;
2189 unsigned int mode;
2190
2191 mutex_lock(&rdev->mutex);
2192
Mark Browna4b41482011-05-14 11:19:45 -07002193 /*
2194 * first check to see if we can set modes at all, otherwise just
2195 * tell the consumer everything is OK.
2196 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002197 regulator->uA_load = uA_load;
2198 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002199 if (ret < 0) {
2200 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002201 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002202 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002203
Liam Girdwood414c70c2008-04-30 15:59:04 +01002204 if (!rdev->desc->ops->get_optimum_mode)
2205 goto out;
2206
Mark Browna4b41482011-05-14 11:19:45 -07002207 /*
2208 * we can actually do this so any errors are indicators of
2209 * potential real failure.
2210 */
2211 ret = -EINVAL;
2212
Liam Girdwood414c70c2008-04-30 15:59:04 +01002213 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002214 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002215 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002216 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002217 goto out;
2218 }
2219
2220 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002221 input_uV = 0;
2222 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002223 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002224 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002225 input_uV = rdev->constraints->input_uV;
2226 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002227 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002228 goto out;
2229 }
2230
2231 /* calc total requested load for this regulator */
2232 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002233 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002234
2235 mode = rdev->desc->ops->get_optimum_mode(rdev,
2236 input_uV, output_uV,
2237 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002238 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002239 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002240 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2241 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002242 goto out;
2243 }
2244
2245 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002246 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002247 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002248 goto out;
2249 }
2250 ret = mode;
2251out:
2252 mutex_unlock(&rdev->mutex);
2253 return ret;
2254}
2255EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2256
2257/**
2258 * regulator_register_notifier - register regulator event notifier
2259 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002260 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002261 *
2262 * Register notifier block to receive regulator events.
2263 */
2264int regulator_register_notifier(struct regulator *regulator,
2265 struct notifier_block *nb)
2266{
2267 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2268 nb);
2269}
2270EXPORT_SYMBOL_GPL(regulator_register_notifier);
2271
2272/**
2273 * regulator_unregister_notifier - unregister regulator event notifier
2274 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002275 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002276 *
2277 * Unregister regulator event notifier block.
2278 */
2279int regulator_unregister_notifier(struct regulator *regulator,
2280 struct notifier_block *nb)
2281{
2282 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2283 nb);
2284}
2285EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2286
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002287/* notify regulator consumers and downstream regulator consumers.
2288 * Note mutex must be held by caller.
2289 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002290static void _notifier_call_chain(struct regulator_dev *rdev,
2291 unsigned long event, void *data)
2292{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002293 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002294 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002295}
2296
2297/**
2298 * regulator_bulk_get - get multiple regulator consumers
2299 *
2300 * @dev: Device to supply
2301 * @num_consumers: Number of consumers to register
2302 * @consumers: Configuration of consumers; clients are stored here.
2303 *
2304 * @return 0 on success, an errno on failure.
2305 *
2306 * This helper function allows drivers to get several regulator
2307 * consumers in one operation. If any of the regulators cannot be
2308 * acquired then any regulators that were allocated will be freed
2309 * before returning to the caller.
2310 */
2311int regulator_bulk_get(struct device *dev, int num_consumers,
2312 struct regulator_bulk_data *consumers)
2313{
2314 int i;
2315 int ret;
2316
2317 for (i = 0; i < num_consumers; i++)
2318 consumers[i].consumer = NULL;
2319
2320 for (i = 0; i < num_consumers; i++) {
2321 consumers[i].consumer = regulator_get(dev,
2322 consumers[i].supply);
2323 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002324 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002325 dev_err(dev, "Failed to get supply '%s': %d\n",
2326 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327 consumers[i].consumer = NULL;
2328 goto err;
2329 }
2330 }
2331
2332 return 0;
2333
2334err:
2335 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2336 regulator_put(consumers[i].consumer);
2337
2338 return ret;
2339}
2340EXPORT_SYMBOL_GPL(regulator_bulk_get);
2341
Mark Brownf21e0e82011-05-24 08:12:40 +08002342static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2343{
2344 struct regulator_bulk_data *bulk = data;
2345
2346 bulk->ret = regulator_enable(bulk->consumer);
2347}
2348
Liam Girdwood414c70c2008-04-30 15:59:04 +01002349/**
2350 * regulator_bulk_enable - enable multiple regulator consumers
2351 *
2352 * @num_consumers: Number of consumers
2353 * @consumers: Consumer data; clients are stored here.
2354 * @return 0 on success, an errno on failure
2355 *
2356 * This convenience API allows consumers to enable multiple regulator
2357 * clients in a single API call. If any consumers cannot be enabled
2358 * then any others that were enabled will be disabled again prior to
2359 * return.
2360 */
2361int regulator_bulk_enable(int num_consumers,
2362 struct regulator_bulk_data *consumers)
2363{
Mark Brownf21e0e82011-05-24 08:12:40 +08002364 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002365 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002366 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002367
Mark Brownf21e0e82011-05-24 08:12:40 +08002368 for (i = 0; i < num_consumers; i++)
2369 async_schedule_domain(regulator_bulk_enable_async,
2370 &consumers[i], &async_domain);
2371
2372 async_synchronize_full_domain(&async_domain);
2373
2374 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002375 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002376 if (consumers[i].ret != 0) {
2377 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002378 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002379 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002380 }
2381
2382 return 0;
2383
2384err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002385 for (i = 0; i < num_consumers; i++)
2386 if (consumers[i].ret == 0)
2387 regulator_disable(consumers[i].consumer);
2388 else
2389 pr_err("Failed to enable %s: %d\n",
2390 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002391
2392 return ret;
2393}
2394EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2395
2396/**
2397 * regulator_bulk_disable - disable multiple regulator consumers
2398 *
2399 * @num_consumers: Number of consumers
2400 * @consumers: Consumer data; clients are stored here.
2401 * @return 0 on success, an errno on failure
2402 *
2403 * This convenience API allows consumers to disable multiple regulator
2404 * clients in a single API call. If any consumers cannot be enabled
2405 * then any others that were disabled will be disabled again prior to
2406 * return.
2407 */
2408int regulator_bulk_disable(int num_consumers,
2409 struct regulator_bulk_data *consumers)
2410{
2411 int i;
2412 int ret;
2413
2414 for (i = 0; i < num_consumers; i++) {
2415 ret = regulator_disable(consumers[i].consumer);
2416 if (ret != 0)
2417 goto err;
2418 }
2419
2420 return 0;
2421
2422err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002423 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002424 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002425 regulator_enable(consumers[i].consumer);
2426
2427 return ret;
2428}
2429EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2430
2431/**
2432 * regulator_bulk_free - free multiple regulator consumers
2433 *
2434 * @num_consumers: Number of consumers
2435 * @consumers: Consumer data; clients are stored here.
2436 *
2437 * This convenience API allows consumers to free multiple regulator
2438 * clients in a single API call.
2439 */
2440void regulator_bulk_free(int num_consumers,
2441 struct regulator_bulk_data *consumers)
2442{
2443 int i;
2444
2445 for (i = 0; i < num_consumers; i++) {
2446 regulator_put(consumers[i].consumer);
2447 consumers[i].consumer = NULL;
2448 }
2449}
2450EXPORT_SYMBOL_GPL(regulator_bulk_free);
2451
2452/**
2453 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002454 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002455 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002456 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002457 *
2458 * Called by regulator drivers to notify clients a regulator event has
2459 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002460 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002461 */
2462int regulator_notifier_call_chain(struct regulator_dev *rdev,
2463 unsigned long event, void *data)
2464{
2465 _notifier_call_chain(rdev, event, data);
2466 return NOTIFY_DONE;
2467
2468}
2469EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2470
Mark Brownbe721972009-08-04 20:09:52 +02002471/**
2472 * regulator_mode_to_status - convert a regulator mode into a status
2473 *
2474 * @mode: Mode to convert
2475 *
2476 * Convert a regulator mode into a status.
2477 */
2478int regulator_mode_to_status(unsigned int mode)
2479{
2480 switch (mode) {
2481 case REGULATOR_MODE_FAST:
2482 return REGULATOR_STATUS_FAST;
2483 case REGULATOR_MODE_NORMAL:
2484 return REGULATOR_STATUS_NORMAL;
2485 case REGULATOR_MODE_IDLE:
2486 return REGULATOR_STATUS_IDLE;
2487 case REGULATOR_STATUS_STANDBY:
2488 return REGULATOR_STATUS_STANDBY;
2489 default:
2490 return 0;
2491 }
2492}
2493EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2494
David Brownell7ad68e22008-11-11 17:39:02 -08002495/*
2496 * To avoid cluttering sysfs (and memory) with useless state, only
2497 * create attributes that can be meaningfully displayed.
2498 */
2499static int add_regulator_attributes(struct regulator_dev *rdev)
2500{
2501 struct device *dev = &rdev->dev;
2502 struct regulator_ops *ops = rdev->desc->ops;
2503 int status = 0;
2504
2505 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002506 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002507 status = device_create_file(dev, &dev_attr_microvolts);
2508 if (status < 0)
2509 return status;
2510 }
2511 if (ops->get_current_limit) {
2512 status = device_create_file(dev, &dev_attr_microamps);
2513 if (status < 0)
2514 return status;
2515 }
2516 if (ops->get_mode) {
2517 status = device_create_file(dev, &dev_attr_opmode);
2518 if (status < 0)
2519 return status;
2520 }
2521 if (ops->is_enabled) {
2522 status = device_create_file(dev, &dev_attr_state);
2523 if (status < 0)
2524 return status;
2525 }
David Brownell853116a2009-01-14 23:03:17 -08002526 if (ops->get_status) {
2527 status = device_create_file(dev, &dev_attr_status);
2528 if (status < 0)
2529 return status;
2530 }
David Brownell7ad68e22008-11-11 17:39:02 -08002531
2532 /* some attributes are type-specific */
2533 if (rdev->desc->type == REGULATOR_CURRENT) {
2534 status = device_create_file(dev, &dev_attr_requested_microamps);
2535 if (status < 0)
2536 return status;
2537 }
2538
2539 /* all the other attributes exist to support constraints;
2540 * don't show them if there are no constraints, or if the
2541 * relevant supporting methods are missing.
2542 */
2543 if (!rdev->constraints)
2544 return status;
2545
2546 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002547 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002548 status = device_create_file(dev, &dev_attr_min_microvolts);
2549 if (status < 0)
2550 return status;
2551 status = device_create_file(dev, &dev_attr_max_microvolts);
2552 if (status < 0)
2553 return status;
2554 }
2555 if (ops->set_current_limit) {
2556 status = device_create_file(dev, &dev_attr_min_microamps);
2557 if (status < 0)
2558 return status;
2559 status = device_create_file(dev, &dev_attr_max_microamps);
2560 if (status < 0)
2561 return status;
2562 }
2563
2564 /* suspend mode constraints need multiple supporting methods */
2565 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2566 return status;
2567
2568 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2569 if (status < 0)
2570 return status;
2571 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2572 if (status < 0)
2573 return status;
2574 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2575 if (status < 0)
2576 return status;
2577
2578 if (ops->set_suspend_voltage) {
2579 status = device_create_file(dev,
2580 &dev_attr_suspend_standby_microvolts);
2581 if (status < 0)
2582 return status;
2583 status = device_create_file(dev,
2584 &dev_attr_suspend_mem_microvolts);
2585 if (status < 0)
2586 return status;
2587 status = device_create_file(dev,
2588 &dev_attr_suspend_disk_microvolts);
2589 if (status < 0)
2590 return status;
2591 }
2592
2593 if (ops->set_suspend_mode) {
2594 status = device_create_file(dev,
2595 &dev_attr_suspend_standby_mode);
2596 if (status < 0)
2597 return status;
2598 status = device_create_file(dev,
2599 &dev_attr_suspend_mem_mode);
2600 if (status < 0)
2601 return status;
2602 status = device_create_file(dev,
2603 &dev_attr_suspend_disk_mode);
2604 if (status < 0)
2605 return status;
2606 }
2607
2608 return status;
2609}
2610
Mark Brown1130e5b2010-12-21 23:49:31 +00002611static void rdev_init_debugfs(struct regulator_dev *rdev)
2612{
2613#ifdef CONFIG_DEBUG_FS
2614 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2615 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2616 rdev_warn(rdev, "Failed to create debugfs directory\n");
2617 rdev->debugfs = NULL;
2618 return;
2619 }
2620
2621 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2622 &rdev->use_count);
2623 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2624 &rdev->open_count);
2625#endif
2626}
2627
Liam Girdwood414c70c2008-04-30 15:59:04 +01002628/**
2629 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002630 * @regulator_desc: regulator to register
2631 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002632 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002633 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002634 *
2635 * Called by regulator drivers to register a regulator.
2636 * Returns 0 on success.
2637 */
2638struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002639 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002640 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002641{
2642 static atomic_t regulator_no = ATOMIC_INIT(0);
2643 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002644 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002645
2646 if (regulator_desc == NULL)
2647 return ERR_PTR(-EINVAL);
2648
2649 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2650 return ERR_PTR(-EINVAL);
2651
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002652 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2653 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002654 return ERR_PTR(-EINVAL);
2655
Mark Brown46fabe1e2008-09-09 16:21:18 +01002656 if (!init_data)
2657 return ERR_PTR(-EINVAL);
2658
Mark Brown476c2d82010-12-10 17:28:07 +00002659 /* Only one of each should be implemented */
2660 WARN_ON(regulator_desc->ops->get_voltage &&
2661 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002662 WARN_ON(regulator_desc->ops->set_voltage &&
2663 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002664
2665 /* If we're using selectors we must implement list_voltage. */
2666 if (regulator_desc->ops->get_voltage_sel &&
2667 !regulator_desc->ops->list_voltage) {
2668 return ERR_PTR(-EINVAL);
2669 }
Mark Browne8eef822010-12-12 14:36:17 +00002670 if (regulator_desc->ops->set_voltage_sel &&
2671 !regulator_desc->ops->list_voltage) {
2672 return ERR_PTR(-EINVAL);
2673 }
Mark Brown476c2d82010-12-10 17:28:07 +00002674
Liam Girdwood414c70c2008-04-30 15:59:04 +01002675 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2676 if (rdev == NULL)
2677 return ERR_PTR(-ENOMEM);
2678
2679 mutex_lock(&regulator_list_mutex);
2680
2681 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002682 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002683 rdev->owner = regulator_desc->owner;
2684 rdev->desc = regulator_desc;
2685 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002686 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002687 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
Mark Brownda07ecd2011-09-11 09:53:50 +01002688 INIT_DELAYED_WORK(&rdev->disable_work, regulator_disable_work);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002689
Liam Girdwooda5766f12008-10-10 13:22:20 +01002690 /* preform any regulator specific init */
2691 if (init_data->regulator_init) {
2692 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002693 if (ret < 0)
2694 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002695 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002696
Liam Girdwooda5766f12008-10-10 13:22:20 +01002697 /* register with sysfs */
2698 rdev->dev.class = &regulator_class;
2699 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002700 dev_set_name(&rdev->dev, "regulator.%d",
2701 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002702 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002703 if (ret != 0) {
2704 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002705 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002706 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002707
2708 dev_set_drvdata(&rdev->dev, rdev);
2709
Mike Rapoport74f544c2008-11-25 14:53:53 +02002710 /* set regulator constraints */
2711 ret = set_machine_constraints(rdev, &init_data->constraints);
2712 if (ret < 0)
2713 goto scrub;
2714
David Brownell7ad68e22008-11-11 17:39:02 -08002715 /* add attributes supported by this regulator */
2716 ret = add_regulator_attributes(rdev);
2717 if (ret < 0)
2718 goto scrub;
2719
Mark Brown0178f3e2010-04-26 15:18:14 +01002720 if (init_data->supply_regulator) {
2721 struct regulator_dev *r;
2722 int found = 0;
2723
2724 list_for_each_entry(r, &regulator_list, list) {
2725 if (strcmp(rdev_get_name(r),
2726 init_data->supply_regulator) == 0) {
2727 found = 1;
2728 break;
2729 }
2730 }
2731
2732 if (!found) {
2733 dev_err(dev, "Failed to find supply %s\n",
2734 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002735 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002736 goto scrub;
2737 }
2738
2739 ret = set_supply(rdev, r);
2740 if (ret < 0)
2741 goto scrub;
2742 }
2743
Liam Girdwooda5766f12008-10-10 13:22:20 +01002744 /* add consumers devices */
2745 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2746 ret = set_consumer_device_supply(rdev,
2747 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002748 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002749 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002750 if (ret < 0) {
2751 dev_err(dev, "Failed to set supply %s\n",
2752 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002753 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002754 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002755 }
2756
2757 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002758
2759 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002760out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002761 mutex_unlock(&regulator_list_mutex);
2762 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002763
Jani Nikulad4033b52010-04-29 10:55:11 +03002764unset_supplies:
2765 unset_regulator_supplies(rdev);
2766
David Brownell4fca9542008-11-11 17:38:53 -08002767scrub:
Axel Lin1a6958e72011-07-15 10:50:43 +08002768 kfree(rdev->constraints);
David Brownell4fca9542008-11-11 17:38:53 -08002769 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002770 /* device core frees rdev */
2771 rdev = ERR_PTR(ret);
2772 goto out;
2773
David Brownell4fca9542008-11-11 17:38:53 -08002774clean:
2775 kfree(rdev);
2776 rdev = ERR_PTR(ret);
2777 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002778}
2779EXPORT_SYMBOL_GPL(regulator_register);
2780
2781/**
2782 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002783 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002784 *
2785 * Called by regulator drivers to unregister a regulator.
2786 */
2787void regulator_unregister(struct regulator_dev *rdev)
2788{
2789 if (rdev == NULL)
2790 return;
2791
2792 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002793#ifdef CONFIG_DEBUG_FS
2794 debugfs_remove_recursive(rdev->debugfs);
2795#endif
Mark Brownda07ecd2011-09-11 09:53:50 +01002796 flush_work_sync(&rdev->disable_work.work);
Mark Brown6bf87d12009-07-21 16:00:25 +01002797 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002798 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002799 list_del(&rdev->list);
2800 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002801 regulator_put(rdev->supply);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002802 kfree(rdev->constraints);
Lothar Waßmann58fb5cf2011-11-28 15:38:37 +01002803 device_unregister(&rdev->dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002804 mutex_unlock(&regulator_list_mutex);
2805}
2806EXPORT_SYMBOL_GPL(regulator_unregister);
2807
2808/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002809 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002810 * @state: system suspend state
2811 *
2812 * Configure each regulator with it's suspend operating parameters for state.
2813 * This will usually be called by machine suspend code prior to supending.
2814 */
2815int regulator_suspend_prepare(suspend_state_t state)
2816{
2817 struct regulator_dev *rdev;
2818 int ret = 0;
2819
2820 /* ON is handled by regulator active state */
2821 if (state == PM_SUSPEND_ON)
2822 return -EINVAL;
2823
2824 mutex_lock(&regulator_list_mutex);
2825 list_for_each_entry(rdev, &regulator_list, list) {
2826
2827 mutex_lock(&rdev->mutex);
2828 ret = suspend_prepare(rdev, state);
2829 mutex_unlock(&rdev->mutex);
2830
2831 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002832 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002833 goto out;
2834 }
2835 }
2836out:
2837 mutex_unlock(&regulator_list_mutex);
2838 return ret;
2839}
2840EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2841
2842/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002843 * regulator_suspend_finish - resume regulators from system wide suspend
2844 *
2845 * Turn on regulators that might be turned off by regulator_suspend_prepare
2846 * and that should be turned on according to the regulators properties.
2847 */
2848int regulator_suspend_finish(void)
2849{
2850 struct regulator_dev *rdev;
2851 int ret = 0, error;
2852
2853 mutex_lock(&regulator_list_mutex);
2854 list_for_each_entry(rdev, &regulator_list, list) {
2855 struct regulator_ops *ops = rdev->desc->ops;
2856
2857 mutex_lock(&rdev->mutex);
2858 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2859 ops->enable) {
2860 error = ops->enable(rdev);
2861 if (error)
2862 ret = error;
2863 } else {
2864 if (!has_full_constraints)
2865 goto unlock;
2866 if (!ops->disable)
2867 goto unlock;
2868 if (ops->is_enabled && !ops->is_enabled(rdev))
2869 goto unlock;
2870
2871 error = ops->disable(rdev);
2872 if (error)
2873 ret = error;
2874 }
2875unlock:
2876 mutex_unlock(&rdev->mutex);
2877 }
2878 mutex_unlock(&regulator_list_mutex);
2879 return ret;
2880}
2881EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2882
2883/**
Mark Brownca725562009-03-16 19:36:34 +00002884 * regulator_has_full_constraints - the system has fully specified constraints
2885 *
2886 * Calling this function will cause the regulator API to disable all
2887 * regulators which have a zero use count and don't have an always_on
2888 * constraint in a late_initcall.
2889 *
2890 * The intention is that this will become the default behaviour in a
2891 * future kernel release so users are encouraged to use this facility
2892 * now.
2893 */
2894void regulator_has_full_constraints(void)
2895{
2896 has_full_constraints = 1;
2897}
2898EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2899
2900/**
Mark Brown688fe992010-10-05 19:18:32 -07002901 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2902 *
2903 * Calling this function will cause the regulator API to provide a
2904 * dummy regulator to consumers if no physical regulator is found,
2905 * allowing most consumers to proceed as though a regulator were
2906 * configured. This allows systems such as those with software
2907 * controllable regulators for the CPU core only to be brought up more
2908 * readily.
2909 */
2910void regulator_use_dummy_regulator(void)
2911{
2912 board_wants_dummy_regulator = true;
2913}
2914EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2915
2916/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002917 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002918 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002919 *
2920 * Get rdev regulator driver private data. This call can be used in the
2921 * regulator driver context.
2922 */
2923void *rdev_get_drvdata(struct regulator_dev *rdev)
2924{
2925 return rdev->reg_data;
2926}
2927EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2928
2929/**
2930 * regulator_get_drvdata - get regulator driver data
2931 * @regulator: regulator
2932 *
2933 * Get regulator driver private data. This call can be used in the consumer
2934 * driver context when non API regulator specific functions need to be called.
2935 */
2936void *regulator_get_drvdata(struct regulator *regulator)
2937{
2938 return regulator->rdev->reg_data;
2939}
2940EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2941
2942/**
2943 * regulator_set_drvdata - set regulator driver data
2944 * @regulator: regulator
2945 * @data: data
2946 */
2947void regulator_set_drvdata(struct regulator *regulator, void *data)
2948{
2949 regulator->rdev->reg_data = data;
2950}
2951EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2952
2953/**
2954 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002955 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002956 */
2957int rdev_get_id(struct regulator_dev *rdev)
2958{
2959 return rdev->desc->id;
2960}
2961EXPORT_SYMBOL_GPL(rdev_get_id);
2962
Liam Girdwooda5766f12008-10-10 13:22:20 +01002963struct device *rdev_get_dev(struct regulator_dev *rdev)
2964{
2965 return &rdev->dev;
2966}
2967EXPORT_SYMBOL_GPL(rdev_get_dev);
2968
2969void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2970{
2971 return reg_init_data->driver_data;
2972}
2973EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2974
Mark Brownba55a972011-08-23 17:39:10 +01002975#ifdef CONFIG_DEBUG_FS
2976static ssize_t supply_map_read_file(struct file *file, char __user *user_buf,
2977 size_t count, loff_t *ppos)
2978{
2979 char *buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
2980 ssize_t len, ret = 0;
2981 struct regulator_map *map;
2982
2983 if (!buf)
2984 return -ENOMEM;
2985
2986 list_for_each_entry(map, &regulator_map_list, list) {
2987 len = snprintf(buf + ret, PAGE_SIZE - ret,
2988 "%s -> %s.%s\n",
2989 rdev_get_name(map->regulator), map->dev_name,
2990 map->supply);
2991 if (len >= 0)
2992 ret += len;
2993 if (ret > PAGE_SIZE) {
2994 ret = PAGE_SIZE;
2995 break;
2996 }
2997 }
2998
2999 ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
3000
3001 kfree(buf);
3002
3003 return ret;
3004}
3005
3006static const struct file_operations supply_map_fops = {
3007 .read = supply_map_read_file,
3008 .llseek = default_llseek,
3009};
3010#endif
3011
Liam Girdwood414c70c2008-04-30 15:59:04 +01003012static int __init regulator_init(void)
3013{
Mark Brown34abbd62010-02-12 10:18:08 +00003014 int ret;
3015
Mark Brown34abbd62010-02-12 10:18:08 +00003016 ret = class_register(&regulator_class);
3017
Mark Brown1130e5b2010-12-21 23:49:31 +00003018#ifdef CONFIG_DEBUG_FS
3019 debugfs_root = debugfs_create_dir("regulator", NULL);
3020 if (IS_ERR(debugfs_root) || !debugfs_root) {
3021 pr_warn("regulator: Failed to create debugfs directory\n");
3022 debugfs_root = NULL;
3023 }
Mark Brownba55a972011-08-23 17:39:10 +01003024
3025 if (IS_ERR(debugfs_create_file("supply_map", 0444, debugfs_root,
3026 NULL, &supply_map_fops)))
3027 pr_warn("regulator: Failed to create supplies debugfs\n");
Mark Brown1130e5b2010-12-21 23:49:31 +00003028#endif
3029
Mark Brown34abbd62010-02-12 10:18:08 +00003030 regulator_dummy_init();
3031
3032 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01003033}
3034
3035/* init early to allow our consumers to complete system booting */
3036core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00003037
3038static int __init regulator_init_complete(void)
3039{
3040 struct regulator_dev *rdev;
3041 struct regulator_ops *ops;
3042 struct regulation_constraints *c;
3043 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00003044
3045 mutex_lock(&regulator_list_mutex);
3046
3047 /* If we have a full configuration then disable any regulators
3048 * which are not in use or always_on. This will become the
3049 * default behaviour in the future.
3050 */
3051 list_for_each_entry(rdev, &regulator_list, list) {
3052 ops = rdev->desc->ops;
3053 c = rdev->constraints;
3054
Mark Brownf25e0b42009-08-03 18:49:55 +01003055 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00003056 continue;
3057
3058 mutex_lock(&rdev->mutex);
3059
3060 if (rdev->use_count)
3061 goto unlock;
3062
3063 /* If we can't read the status assume it's on. */
3064 if (ops->is_enabled)
3065 enabled = ops->is_enabled(rdev);
3066 else
3067 enabled = 1;
3068
3069 if (!enabled)
3070 goto unlock;
3071
3072 if (has_full_constraints) {
3073 /* We log since this may kill the system if it
3074 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08003075 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00003076 ret = ops->disable(rdev);
3077 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08003078 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00003079 }
3080 } else {
3081 /* The intention is that in future we will
3082 * assume that full constraints are provided
3083 * so warn even if we aren't going to do
3084 * anything here.
3085 */
Joe Perches5da84fd2010-11-30 05:53:48 -08003086 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00003087 }
3088
3089unlock:
3090 mutex_unlock(&rdev->mutex);
3091 }
3092
3093 mutex_unlock(&regulator_list_mutex);
3094
3095 return 0;
3096}
3097late_initcall(regulator_init_complete);