blob: cc3dfd66f395e515e39c76453ad848f5eddf1f1f [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>
31
Mark Brown02fa3ec2010-11-10 14:38:30 +000032#define CREATE_TRACE_POINTS
33#include <trace/events/regulator.h>
34
Mark Brown34abbd62010-02-12 10:18:08 +000035#include "dummy.h"
36
Mark Brown7d51a0d2011-06-09 16:06:37 +010037#define rdev_crit(rdev, fmt, ...) \
38 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080039#define rdev_err(rdev, fmt, ...) \
40 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_warn(rdev, fmt, ...) \
42 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_info(rdev, fmt, ...) \
44 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_dbg(rdev, fmt, ...) \
46 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47
Liam Girdwood414c70c2008-04-30 15:59:04 +010048static DEFINE_MUTEX(regulator_list_mutex);
49static LIST_HEAD(regulator_list);
50static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000051static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070052static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010053
Mark Brown1130e5b2010-12-21 23:49:31 +000054#ifdef CONFIG_DEBUG_FS
55static struct dentry *debugfs_root;
56#endif
57
Mark Brown8dc53902008-12-31 12:52:40 +000058/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010059 * struct regulator_map
60 *
61 * Used to provide symbolic supply names to devices.
62 */
63struct regulator_map {
64 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010065 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010066 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010067 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010068};
69
Liam Girdwood414c70c2008-04-30 15:59:04 +010070/*
71 * struct regulator
72 *
73 * One for each consumer device.
74 */
75struct regulator {
76 struct device *dev;
77 struct list_head list;
78 int uA_load;
79 int min_uV;
80 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 char *supply_name;
82 struct device_attribute dev_attr;
83 struct regulator_dev *rdev;
84};
85
86static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010087static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010088static int _regulator_get_voltage(struct regulator_dev *rdev);
89static int _regulator_get_current_limit(struct regulator_dev *rdev);
90static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
91static void _notifier_call_chain(struct regulator_dev *rdev,
92 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000093static int _regulator_do_set_voltage(struct regulator_dev *rdev,
94 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010095static struct regulator *create_regulator(struct regulator_dev *rdev,
96 struct device *dev,
97 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010098
Mark Brown1083c392009-10-22 16:31:32 +010099static const char *rdev_get_name(struct regulator_dev *rdev)
100{
101 if (rdev->constraints && rdev->constraints->name)
102 return rdev->constraints->name;
103 else if (rdev->desc->name)
104 return rdev->desc->name;
105 else
106 return "";
107}
108
Liam Girdwood414c70c2008-04-30 15:59:04 +0100109/* gets the regulator for a given consumer device */
110static struct regulator *get_device_regulator(struct device *dev)
111{
112 struct regulator *regulator = NULL;
113 struct regulator_dev *rdev;
114
115 mutex_lock(&regulator_list_mutex);
116 list_for_each_entry(rdev, &regulator_list, list) {
117 mutex_lock(&rdev->mutex);
118 list_for_each_entry(regulator, &rdev->consumer_list, list) {
119 if (regulator->dev == dev) {
120 mutex_unlock(&rdev->mutex);
121 mutex_unlock(&regulator_list_mutex);
122 return regulator;
123 }
124 }
125 mutex_unlock(&rdev->mutex);
126 }
127 mutex_unlock(&regulator_list_mutex);
128 return NULL;
129}
130
131/* Platform voltage constraint check */
132static int regulator_check_voltage(struct regulator_dev *rdev,
133 int *min_uV, int *max_uV)
134{
135 BUG_ON(*min_uV > *max_uV);
136
137 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800138 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100139 return -ENODEV;
140 }
141 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800142 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100143 return -EPERM;
144 }
145
146 if (*max_uV > rdev->constraints->max_uV)
147 *max_uV = rdev->constraints->max_uV;
148 if (*min_uV < rdev->constraints->min_uV)
149 *min_uV = rdev->constraints->min_uV;
150
151 if (*min_uV > *max_uV)
152 return -EINVAL;
153
154 return 0;
155}
156
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100157/* Make sure we select a voltage that suits the needs of all
158 * regulator consumers
159 */
160static int regulator_check_consumers(struct regulator_dev *rdev,
161 int *min_uV, int *max_uV)
162{
163 struct regulator *regulator;
164
165 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700166 /*
167 * Assume consumers that didn't say anything are OK
168 * with anything in the constraint range.
169 */
170 if (!regulator->min_uV && !regulator->max_uV)
171 continue;
172
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100173 if (*max_uV > regulator->max_uV)
174 *max_uV = regulator->max_uV;
175 if (*min_uV < regulator->min_uV)
176 *min_uV = regulator->min_uV;
177 }
178
179 if (*min_uV > *max_uV)
180 return -EINVAL;
181
182 return 0;
183}
184
Liam Girdwood414c70c2008-04-30 15:59:04 +0100185/* current constraint check */
186static int regulator_check_current_limit(struct regulator_dev *rdev,
187 int *min_uA, int *max_uA)
188{
189 BUG_ON(*min_uA > *max_uA);
190
191 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800192 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100193 return -ENODEV;
194 }
195 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800196 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100197 return -EPERM;
198 }
199
200 if (*max_uA > rdev->constraints->max_uA)
201 *max_uA = rdev->constraints->max_uA;
202 if (*min_uA < rdev->constraints->min_uA)
203 *min_uA = rdev->constraints->min_uA;
204
205 if (*min_uA > *max_uA)
206 return -EINVAL;
207
208 return 0;
209}
210
211/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900212static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100213{
Mark Brown2c608232011-03-30 06:29:12 +0900214 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800215 case REGULATOR_MODE_FAST:
216 case REGULATOR_MODE_NORMAL:
217 case REGULATOR_MODE_IDLE:
218 case REGULATOR_MODE_STANDBY:
219 break;
220 default:
221 return -EINVAL;
222 }
223
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800225 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100226 return -ENODEV;
227 }
228 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800229 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100230 return -EPERM;
231 }
Mark Brown2c608232011-03-30 06:29:12 +0900232
233 /* The modes are bitmasks, the most power hungry modes having
234 * the lowest values. If the requested mode isn't supported
235 * try higher modes. */
236 while (*mode) {
237 if (rdev->constraints->valid_modes_mask & *mode)
238 return 0;
239 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240 }
Mark Brown2c608232011-03-30 06:29:12 +0900241
242 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243}
244
245/* dynamic regulator mode switching constraint check */
246static int regulator_check_drms(struct regulator_dev *rdev)
247{
248 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800249 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250 return -ENODEV;
251 }
252 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800253 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254 return -EPERM;
255 }
256 return 0;
257}
258
259static ssize_t device_requested_uA_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261{
262 struct regulator *regulator;
263
264 regulator = get_device_regulator(dev);
265 if (regulator == NULL)
266 return 0;
267
268 return sprintf(buf, "%d\n", regulator->uA_load);
269}
270
271static ssize_t regulator_uV_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
273{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100274 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275 ssize_t ret;
276
277 mutex_lock(&rdev->mutex);
278 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
279 mutex_unlock(&rdev->mutex);
280
281 return ret;
282}
David Brownell7ad68e22008-11-11 17:39:02 -0800283static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284
285static ssize_t regulator_uA_show(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100288 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100289
290 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
291}
David Brownell7ad68e22008-11-11 17:39:02 -0800292static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100293
Mark Brownbc558a62008-10-10 15:33:20 +0100294static ssize_t regulator_name_show(struct device *dev,
295 struct device_attribute *attr, char *buf)
296{
297 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100298
Mark Brown1083c392009-10-22 16:31:32 +0100299 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100300}
301
David Brownell4fca9542008-11-11 17:38:53 -0800302static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100304 switch (mode) {
305 case REGULATOR_MODE_FAST:
306 return sprintf(buf, "fast\n");
307 case REGULATOR_MODE_NORMAL:
308 return sprintf(buf, "normal\n");
309 case REGULATOR_MODE_IDLE:
310 return sprintf(buf, "idle\n");
311 case REGULATOR_MODE_STANDBY:
312 return sprintf(buf, "standby\n");
313 }
314 return sprintf(buf, "unknown\n");
315}
316
David Brownell4fca9542008-11-11 17:38:53 -0800317static ssize_t regulator_opmode_show(struct device *dev,
318 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100320 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100321
David Brownell4fca9542008-11-11 17:38:53 -0800322 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
323}
David Brownell7ad68e22008-11-11 17:39:02 -0800324static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800325
326static ssize_t regulator_print_state(char *buf, int state)
327{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100328 if (state > 0)
329 return sprintf(buf, "enabled\n");
330 else if (state == 0)
331 return sprintf(buf, "disabled\n");
332 else
333 return sprintf(buf, "unknown\n");
334}
335
David Brownell4fca9542008-11-11 17:38:53 -0800336static ssize_t regulator_state_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
339 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100340 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800341
Mark Brown93325462009-08-03 18:49:56 +0100342 mutex_lock(&rdev->mutex);
343 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
344 mutex_unlock(&rdev->mutex);
345
346 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800347}
David Brownell7ad68e22008-11-11 17:39:02 -0800348static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800349
David Brownell853116a2009-01-14 23:03:17 -0800350static ssize_t regulator_status_show(struct device *dev,
351 struct device_attribute *attr, char *buf)
352{
353 struct regulator_dev *rdev = dev_get_drvdata(dev);
354 int status;
355 char *label;
356
357 status = rdev->desc->ops->get_status(rdev);
358 if (status < 0)
359 return status;
360
361 switch (status) {
362 case REGULATOR_STATUS_OFF:
363 label = "off";
364 break;
365 case REGULATOR_STATUS_ON:
366 label = "on";
367 break;
368 case REGULATOR_STATUS_ERROR:
369 label = "error";
370 break;
371 case REGULATOR_STATUS_FAST:
372 label = "fast";
373 break;
374 case REGULATOR_STATUS_NORMAL:
375 label = "normal";
376 break;
377 case REGULATOR_STATUS_IDLE:
378 label = "idle";
379 break;
380 case REGULATOR_STATUS_STANDBY:
381 label = "standby";
382 break;
383 default:
384 return -ERANGE;
385 }
386
387 return sprintf(buf, "%s\n", label);
388}
389static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
390
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391static ssize_t regulator_min_uA_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
393{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100394 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100395
396 if (!rdev->constraints)
397 return sprintf(buf, "constraint not defined\n");
398
399 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
400}
David Brownell7ad68e22008-11-11 17:39:02 -0800401static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100402
403static ssize_t regulator_max_uA_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100406 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100407
408 if (!rdev->constraints)
409 return sprintf(buf, "constraint not defined\n");
410
411 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
412}
David Brownell7ad68e22008-11-11 17:39:02 -0800413static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414
415static ssize_t regulator_min_uV_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
422
423 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427static ssize_t regulator_max_uV_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100430 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431
432 if (!rdev->constraints)
433 return sprintf(buf, "constraint not defined\n");
434
435 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
436}
David Brownell7ad68e22008-11-11 17:39:02 -0800437static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_total_uA_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443 struct regulator *regulator;
444 int uA = 0;
445
446 mutex_lock(&rdev->mutex);
447 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100448 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100449 mutex_unlock(&rdev->mutex);
450 return sprintf(buf, "%d\n", uA);
451}
David Brownell7ad68e22008-11-11 17:39:02 -0800452static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454static ssize_t regulator_num_users_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100457 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458 return sprintf(buf, "%d\n", rdev->use_count);
459}
460
461static ssize_t regulator_type_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
466 switch (rdev->desc->type) {
467 case REGULATOR_VOLTAGE:
468 return sprintf(buf, "voltage\n");
469 case REGULATOR_CURRENT:
470 return sprintf(buf, "current\n");
471 }
472 return sprintf(buf, "unknown\n");
473}
474
475static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100478 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
481}
David Brownell7ad68e22008-11-11 17:39:02 -0800482static DEVICE_ATTR(suspend_mem_microvolts, 0444,
483 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484
485static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
486 struct device_attribute *attr, char *buf)
487{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100488 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
491}
David Brownell7ad68e22008-11-11 17:39:02 -0800492static DEVICE_ATTR(suspend_disk_microvolts, 0444,
493 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494
495static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
496 struct device_attribute *attr, char *buf)
497{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100498 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
501}
David Brownell7ad68e22008-11-11 17:39:02 -0800502static DEVICE_ATTR(suspend_standby_microvolts, 0444,
503 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100508 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
David Brownell4fca9542008-11-11 17:38:53 -0800510 return regulator_print_opmode(buf,
511 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512}
David Brownell7ad68e22008-11-11 17:39:02 -0800513static DEVICE_ATTR(suspend_mem_mode, 0444,
514 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
516static ssize_t regulator_suspend_disk_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_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523}
David Brownell7ad68e22008-11-11 17:39:02 -0800524static DEVICE_ATTR(suspend_disk_mode, 0444,
525 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
527static ssize_t regulator_suspend_standby_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_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534}
David Brownell7ad68e22008-11-11 17:39:02 -0800535static DEVICE_ATTR(suspend_standby_mode, 0444,
536 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
538static ssize_t regulator_suspend_mem_state_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_state(buf,
544 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545}
David Brownell7ad68e22008-11-11 17:39:02 -0800546static DEVICE_ATTR(suspend_mem_state, 0444,
547 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548
549static ssize_t regulator_suspend_disk_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_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556}
David Brownell7ad68e22008-11-11 17:39:02 -0800557static DEVICE_ATTR(suspend_disk_state, 0444,
558 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559
560static ssize_t regulator_suspend_standby_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_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567}
David Brownell7ad68e22008-11-11 17:39:02 -0800568static DEVICE_ATTR(suspend_standby_state, 0444,
569 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100570
David Brownell7ad68e22008-11-11 17:39:02 -0800571
572/*
573 * These are the only attributes are present for all regulators.
574 * Other attributes are a function of regulator functionality.
575 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100577 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
579 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580 __ATTR_NULL,
581};
582
583static void regulator_dev_release(struct device *dev)
584{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100585 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100586 kfree(rdev);
587}
588
589static struct class regulator_class = {
590 .name = "regulator",
591 .dev_release = regulator_dev_release,
592 .dev_attrs = regulator_dev_attrs,
593};
594
595/* Calculate the new optimum regulator operating mode based on the new total
596 * consumer load. All locks held by caller */
597static void drms_uA_update(struct regulator_dev *rdev)
598{
599 struct regulator *sibling;
600 int current_uA = 0, output_uV, input_uV, err;
601 unsigned int mode;
602
603 err = regulator_check_drms(rdev);
604 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000605 (!rdev->desc->ops->get_voltage &&
606 !rdev->desc->ops->get_voltage_sel) ||
607 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300608 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100609
610 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000611 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612 if (output_uV <= 0)
613 return;
614
615 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000616 input_uV = 0;
617 if (rdev->supply)
618 input_uV = _regulator_get_voltage(rdev);
619 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 input_uV = rdev->constraints->input_uV;
621 if (input_uV <= 0)
622 return;
623
624 /* calc total requested load */
625 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100626 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100627
628 /* now get the optimum mode for our new total regulator load */
629 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
630 output_uV, current_uA);
631
632 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900633 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100634 if (err == 0)
635 rdev->desc->ops->set_mode(rdev, mode);
636}
637
638static int suspend_set_state(struct regulator_dev *rdev,
639 struct regulator_state *rstate)
640{
641 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100642 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100643
Mark Brown638f85c2009-10-22 16:31:33 +0100644 can_set_state = rdev->desc->ops->set_suspend_enable &&
645 rdev->desc->ops->set_suspend_disable;
646
647 /* If we have no suspend mode configration don't set anything;
648 * only warn if the driver actually makes the suspend mode
649 * configurable.
650 */
651 if (!rstate->enabled && !rstate->disabled) {
652 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800653 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100654 return 0;
655 }
656
657 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800658 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100659 return -EINVAL;
660 }
661
662 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800663 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100664 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100665 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100666
667 if (rstate->enabled)
668 ret = rdev->desc->ops->set_suspend_enable(rdev);
669 else
670 ret = rdev->desc->ops->set_suspend_disable(rdev);
671 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800672 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100673 return ret;
674 }
675
676 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
677 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
678 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800679 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680 return ret;
681 }
682 }
683
684 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
685 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
686 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800687 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100688 return ret;
689 }
690 }
691 return ret;
692}
693
694/* locks held by caller */
695static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
696{
697 if (!rdev->constraints)
698 return -EINVAL;
699
700 switch (state) {
701 case PM_SUSPEND_STANDBY:
702 return suspend_set_state(rdev,
703 &rdev->constraints->state_standby);
704 case PM_SUSPEND_MEM:
705 return suspend_set_state(rdev,
706 &rdev->constraints->state_mem);
707 case PM_SUSPEND_MAX:
708 return suspend_set_state(rdev,
709 &rdev->constraints->state_disk);
710 default:
711 return -EINVAL;
712 }
713}
714
715static void print_constraints(struct regulator_dev *rdev)
716{
717 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000718 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100719 int count = 0;
720 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721
Mark Brown8f031b42009-10-22 16:31:31 +0100722 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100723 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100724 count += sprintf(buf + count, "%d mV ",
725 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100726 else
Mark Brown8f031b42009-10-22 16:31:31 +0100727 count += sprintf(buf + count, "%d <--> %d mV ",
728 constraints->min_uV / 1000,
729 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100730 }
Mark Brown8f031b42009-10-22 16:31:31 +0100731
732 if (!constraints->min_uV ||
733 constraints->min_uV != constraints->max_uV) {
734 ret = _regulator_get_voltage(rdev);
735 if (ret > 0)
736 count += sprintf(buf + count, "at %d mV ", ret / 1000);
737 }
738
Mark Brownbf5892a2011-05-08 22:13:37 +0100739 if (constraints->uV_offset)
740 count += sprintf(buf, "%dmV offset ",
741 constraints->uV_offset / 1000);
742
Mark Brown8f031b42009-10-22 16:31:31 +0100743 if (constraints->min_uA && constraints->max_uA) {
744 if (constraints->min_uA == constraints->max_uA)
745 count += sprintf(buf + count, "%d mA ",
746 constraints->min_uA / 1000);
747 else
748 count += sprintf(buf + count, "%d <--> %d mA ",
749 constraints->min_uA / 1000,
750 constraints->max_uA / 1000);
751 }
752
753 if (!constraints->min_uA ||
754 constraints->min_uA != constraints->max_uA) {
755 ret = _regulator_get_current_limit(rdev);
756 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400757 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100758 }
759
Liam Girdwood414c70c2008-04-30 15:59:04 +0100760 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
761 count += sprintf(buf + count, "fast ");
762 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
763 count += sprintf(buf + count, "normal ");
764 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
765 count += sprintf(buf + count, "idle ");
766 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
767 count += sprintf(buf + count, "standby");
768
Mark Brown13ce29f2010-12-17 16:04:12 +0000769 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100770}
771
Mark Browne79055d2009-10-19 15:53:50 +0100772static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100773 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100774{
775 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100776 int ret;
777
778 /* do we need to apply the constraint voltage */
779 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000780 rdev->constraints->min_uV == rdev->constraints->max_uV) {
781 ret = _regulator_do_set_voltage(rdev,
782 rdev->constraints->min_uV,
783 rdev->constraints->max_uV);
784 if (ret < 0) {
785 rdev_err(rdev, "failed to apply %duV constraint\n",
786 rdev->constraints->min_uV);
787 rdev->constraints = NULL;
788 return ret;
789 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100790 }
Mark Browne79055d2009-10-19 15:53:50 +0100791
792 /* constrain machine-level voltage specs to fit
793 * the actual range supported by this regulator.
794 */
795 if (ops->list_voltage && rdev->desc->n_voltages) {
796 int count = rdev->desc->n_voltages;
797 int i;
798 int min_uV = INT_MAX;
799 int max_uV = INT_MIN;
800 int cmin = constraints->min_uV;
801 int cmax = constraints->max_uV;
802
803 /* it's safe to autoconfigure fixed-voltage supplies
804 and the constraints are used by list_voltage. */
805 if (count == 1 && !cmin) {
806 cmin = 1;
807 cmax = INT_MAX;
808 constraints->min_uV = cmin;
809 constraints->max_uV = cmax;
810 }
811
812 /* voltage constraints are optional */
813 if ((cmin == 0) && (cmax == 0))
814 return 0;
815
816 /* else require explicit machine-level constraints */
817 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800818 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100819 return -EINVAL;
820 }
821
822 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
823 for (i = 0; i < count; i++) {
824 int value;
825
826 value = ops->list_voltage(rdev, i);
827 if (value <= 0)
828 continue;
829
830 /* maybe adjust [min_uV..max_uV] */
831 if (value >= cmin && value < min_uV)
832 min_uV = value;
833 if (value <= cmax && value > max_uV)
834 max_uV = value;
835 }
836
837 /* final: [min_uV..max_uV] valid iff constraints valid */
838 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800839 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100840 return -EINVAL;
841 }
842
843 /* use regulator's subset of machine constraints */
844 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800845 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
846 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100847 constraints->min_uV = min_uV;
848 }
849 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800850 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
851 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100852 constraints->max_uV = max_uV;
853 }
854 }
855
856 return 0;
857}
858
Liam Girdwooda5766f12008-10-10 13:22:20 +0100859/**
860 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000861 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000862 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100863 *
864 * Allows platform initialisation code to define and constrain
865 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
866 * Constraints *must* be set by platform code in order for some
867 * regulator operations to proceed i.e. set_voltage, set_current_limit,
868 * set_mode.
869 */
870static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000871 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100872{
873 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100874 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100875
Mark Brownf8c12fe2010-11-29 15:55:17 +0000876 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
877 GFP_KERNEL);
878 if (!rdev->constraints)
879 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100880
Mark Brownf8c12fe2010-11-29 15:55:17 +0000881 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100882 if (ret != 0)
883 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800884
Liam Girdwooda5766f12008-10-10 13:22:20 +0100885 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100886 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000887 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100888 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800889 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100890 rdev->constraints = NULL;
891 goto out;
892 }
893 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100894
Mark Browna3084662009-02-26 19:24:19 +0000895 if (constraints->initial_mode) {
896 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800897 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000898 ret = -EINVAL;
899 goto out;
900 }
901
Mark Brownf8c12fe2010-11-29 15:55:17 +0000902 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000903 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800904 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000905 goto out;
906 }
907 }
908
Mark Browncacf90f2009-03-02 16:32:46 +0000909 /* If the constraints say the regulator should be on at this point
910 * and we have control then make sure it is enabled.
911 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000912 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
913 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100914 ret = ops->enable(rdev);
915 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800916 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100917 rdev->constraints = NULL;
918 goto out;
919 }
920 }
921
Liam Girdwooda5766f12008-10-10 13:22:20 +0100922 print_constraints(rdev);
923out:
924 return ret;
925}
926
927/**
928 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000929 * @rdev: regulator name
930 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931 *
932 * Called by platform initialisation code to set the supply regulator for this
933 * regulator. This ensures that a regulators supply will also be enabled by the
934 * core if it's child is enabled.
935 */
936static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100937 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938{
939 int err;
940
Mark Brown3801b862011-06-09 16:22:22 +0100941 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
942
943 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
944 if (IS_ERR(rdev->supply)) {
945 err = PTR_ERR(rdev->supply);
946 rdev->supply = NULL;
947 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100948 }
Mark Brown3801b862011-06-09 16:22:22 +0100949
950 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100951}
952
953/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800954 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000955 * @rdev: regulator source
956 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100957 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000958 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 *
960 * Allows platform initialisation code to map physical regulator
961 * sources to symbolic names for supplies for use by devices. Devices
962 * should use these symbolic names to request regulators, avoiding the
963 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100964 *
965 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100966 */
967static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100968 struct device *consumer_dev, const char *consumer_dev_name,
969 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100970{
971 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100972 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100973
Mark Brown40f92442009-06-17 17:56:39 +0100974 if (consumer_dev && consumer_dev_name)
975 return -EINVAL;
976
977 if (!consumer_dev_name && consumer_dev)
978 consumer_dev_name = dev_name(consumer_dev);
979
Liam Girdwooda5766f12008-10-10 13:22:20 +0100980 if (supply == NULL)
981 return -EINVAL;
982
Mark Brown9ed20992009-07-21 16:00:26 +0100983 if (consumer_dev_name != NULL)
984 has_dev = 1;
985 else
986 has_dev = 0;
987
David Brownell6001e132008-12-31 12:54:19 +0000988 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300989 if (node->dev_name && consumer_dev_name) {
990 if (strcmp(node->dev_name, consumer_dev_name) != 0)
991 continue;
992 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000993 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300994 }
995
David Brownell6001e132008-12-31 12:54:19 +0000996 if (strcmp(node->supply, supply) != 0)
997 continue;
998
999 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001000 dev_name(&node->regulator->dev),
1001 node->regulator->desc->name,
1002 supply,
1003 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001004 return -EBUSY;
1005 }
1006
Mark Brown9ed20992009-07-21 16:00:26 +01001007 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008 if (node == NULL)
1009 return -ENOMEM;
1010
1011 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001012 node->supply = supply;
1013
Mark Brown9ed20992009-07-21 16:00:26 +01001014 if (has_dev) {
1015 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1016 if (node->dev_name == NULL) {
1017 kfree(node);
1018 return -ENOMEM;
1019 }
Mark Brown40f92442009-06-17 17:56:39 +01001020 }
1021
Liam Girdwooda5766f12008-10-10 13:22:20 +01001022 list_add(&node->list, &regulator_map_list);
1023 return 0;
1024}
1025
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001026static void unset_regulator_supplies(struct regulator_dev *rdev)
1027{
1028 struct regulator_map *node, *n;
1029
1030 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1031 if (rdev == node->regulator) {
1032 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001033 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001034 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001035 }
1036 }
1037}
1038
Mark Brownf5726ae2011-06-09 16:22:20 +01001039#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001040
1041static struct regulator *create_regulator(struct regulator_dev *rdev,
1042 struct device *dev,
1043 const char *supply_name)
1044{
1045 struct regulator *regulator;
1046 char buf[REG_STR_SIZE];
1047 int err, size;
1048
1049 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1050 if (regulator == NULL)
1051 return NULL;
1052
1053 mutex_lock(&rdev->mutex);
1054 regulator->rdev = rdev;
1055 list_add(&regulator->list, &rdev->consumer_list);
1056
1057 if (dev) {
1058 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001059 size = scnprintf(buf, REG_STR_SIZE,
1060 "microamps_requested_%s-%s",
1061 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001062 if (size >= REG_STR_SIZE)
1063 goto overflow_err;
1064
1065 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001066 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001067 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1068 if (regulator->dev_attr.attr.name == NULL)
1069 goto attr_name_err;
1070
Liam Girdwood414c70c2008-04-30 15:59:04 +01001071 regulator->dev_attr.attr.mode = 0444;
1072 regulator->dev_attr.show = device_requested_uA_show;
1073 err = device_create_file(dev, &regulator->dev_attr);
1074 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001075 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001076 goto attr_name_err;
1077 }
1078
1079 /* also add a link to the device sysfs entry */
1080 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1081 dev->kobj.name, supply_name);
1082 if (size >= REG_STR_SIZE)
1083 goto attr_err;
1084
1085 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1086 if (regulator->supply_name == NULL)
1087 goto attr_err;
1088
1089 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1090 buf);
1091 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001092 rdev_warn(rdev, "could not add device link %s err %d\n",
1093 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001094 goto link_name_err;
1095 }
1096 }
1097 mutex_unlock(&rdev->mutex);
1098 return regulator;
1099link_name_err:
1100 kfree(regulator->supply_name);
1101attr_err:
1102 device_remove_file(regulator->dev, &regulator->dev_attr);
1103attr_name_err:
1104 kfree(regulator->dev_attr.attr.name);
1105overflow_err:
1106 list_del(&regulator->list);
1107 kfree(regulator);
1108 mutex_unlock(&rdev->mutex);
1109 return NULL;
1110}
1111
Mark Brown31aae2b2009-12-21 12:21:52 +00001112static int _regulator_get_enable_time(struct regulator_dev *rdev)
1113{
1114 if (!rdev->desc->ops->enable_time)
1115 return 0;
1116 return rdev->desc->ops->enable_time(rdev);
1117}
1118
Mark Brown5ffbd132009-07-21 16:00:23 +01001119/* Internal regulator request function */
1120static struct regulator *_regulator_get(struct device *dev, const char *id,
1121 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001122{
1123 struct regulator_dev *rdev;
1124 struct regulator_map *map;
1125 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001126 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001127 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001128
1129 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001130 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001131 return regulator;
1132 }
1133
Mark Brown40f92442009-06-17 17:56:39 +01001134 if (dev)
1135 devname = dev_name(dev);
1136
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 mutex_lock(&regulator_list_mutex);
1138
1139 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001140 /* If the mapping has a device set up it must match */
1141 if (map->dev_name &&
1142 (!devname || strcmp(map->dev_name, devname)))
1143 continue;
1144
1145 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001146 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001147 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001148 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 }
Mark Brown34abbd62010-02-12 10:18:08 +00001150
Mark Brown688fe992010-10-05 19:18:32 -07001151 if (board_wants_dummy_regulator) {
1152 rdev = dummy_regulator_rdev;
1153 goto found;
1154 }
1155
Mark Brown34abbd62010-02-12 10:18:08 +00001156#ifdef CONFIG_REGULATOR_DUMMY
1157 if (!devname)
1158 devname = "deviceless";
1159
1160 /* If the board didn't flag that it was fully constrained then
1161 * substitute in a dummy regulator so consumers can continue.
1162 */
1163 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001164 pr_warn("%s supply %s not found, using dummy regulator\n",
1165 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001166 rdev = dummy_regulator_rdev;
1167 goto found;
1168 }
1169#endif
1170
Liam Girdwood414c70c2008-04-30 15:59:04 +01001171 mutex_unlock(&regulator_list_mutex);
1172 return regulator;
1173
1174found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001175 if (rdev->exclusive) {
1176 regulator = ERR_PTR(-EPERM);
1177 goto out;
1178 }
1179
1180 if (exclusive && rdev->open_count) {
1181 regulator = ERR_PTR(-EBUSY);
1182 goto out;
1183 }
1184
Liam Girdwooda5766f12008-10-10 13:22:20 +01001185 if (!try_module_get(rdev->owner))
1186 goto out;
1187
Liam Girdwood414c70c2008-04-30 15:59:04 +01001188 regulator = create_regulator(rdev, dev, id);
1189 if (regulator == NULL) {
1190 regulator = ERR_PTR(-ENOMEM);
1191 module_put(rdev->owner);
1192 }
1193
Mark Brown5ffbd132009-07-21 16:00:23 +01001194 rdev->open_count++;
1195 if (exclusive) {
1196 rdev->exclusive = 1;
1197
1198 ret = _regulator_is_enabled(rdev);
1199 if (ret > 0)
1200 rdev->use_count = 1;
1201 else
1202 rdev->use_count = 0;
1203 }
1204
Liam Girdwooda5766f12008-10-10 13:22:20 +01001205out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001206 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001207
Liam Girdwood414c70c2008-04-30 15:59:04 +01001208 return regulator;
1209}
Mark Brown5ffbd132009-07-21 16:00:23 +01001210
1211/**
1212 * regulator_get - lookup and obtain a reference to a regulator.
1213 * @dev: device for regulator "consumer"
1214 * @id: Supply name or regulator ID.
1215 *
1216 * Returns a struct regulator corresponding to the regulator producer,
1217 * or IS_ERR() condition containing errno.
1218 *
1219 * Use of supply names configured via regulator_set_device_supply() is
1220 * strongly encouraged. It is recommended that the supply name used
1221 * should match the name used for the supply and/or the relevant
1222 * device pins in the datasheet.
1223 */
1224struct regulator *regulator_get(struct device *dev, const char *id)
1225{
1226 return _regulator_get(dev, id, 0);
1227}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001228EXPORT_SYMBOL_GPL(regulator_get);
1229
1230/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001231 * regulator_get_exclusive - obtain exclusive access to a regulator.
1232 * @dev: device for regulator "consumer"
1233 * @id: Supply name or regulator ID.
1234 *
1235 * Returns a struct regulator corresponding to the regulator producer,
1236 * or IS_ERR() condition containing errno. Other consumers will be
1237 * unable to obtain this reference is held and the use count for the
1238 * regulator will be initialised to reflect the current state of the
1239 * regulator.
1240 *
1241 * This is intended for use by consumers which cannot tolerate shared
1242 * use of the regulator such as those which need to force the
1243 * regulator off for correct operation of the hardware they are
1244 * controlling.
1245 *
1246 * Use of supply names configured via regulator_set_device_supply() is
1247 * strongly encouraged. It is recommended that the supply name used
1248 * should match the name used for the supply and/or the relevant
1249 * device pins in the datasheet.
1250 */
1251struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1252{
1253 return _regulator_get(dev, id, 1);
1254}
1255EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1256
1257/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001258 * regulator_put - "free" the regulator source
1259 * @regulator: regulator source
1260 *
1261 * Note: drivers must ensure that all regulator_enable calls made on this
1262 * regulator source are balanced by regulator_disable calls prior to calling
1263 * this function.
1264 */
1265void regulator_put(struct regulator *regulator)
1266{
1267 struct regulator_dev *rdev;
1268
1269 if (regulator == NULL || IS_ERR(regulator))
1270 return;
1271
Liam Girdwood414c70c2008-04-30 15:59:04 +01001272 mutex_lock(&regulator_list_mutex);
1273 rdev = regulator->rdev;
1274
1275 /* remove any sysfs entries */
1276 if (regulator->dev) {
1277 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1278 kfree(regulator->supply_name);
1279 device_remove_file(regulator->dev, &regulator->dev_attr);
1280 kfree(regulator->dev_attr.attr.name);
1281 }
1282 list_del(&regulator->list);
1283 kfree(regulator);
1284
Mark Brown5ffbd132009-07-21 16:00:23 +01001285 rdev->open_count--;
1286 rdev->exclusive = 0;
1287
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288 module_put(rdev->owner);
1289 mutex_unlock(&regulator_list_mutex);
1290}
1291EXPORT_SYMBOL_GPL(regulator_put);
1292
Mark Brown9a2372f2009-08-03 18:49:57 +01001293static int _regulator_can_change_status(struct regulator_dev *rdev)
1294{
1295 if (!rdev->constraints)
1296 return 0;
1297
1298 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1299 return 1;
1300 else
1301 return 0;
1302}
1303
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304/* locks held by regulator_enable() */
1305static int _regulator_enable(struct regulator_dev *rdev)
1306{
Mark Brown31aae2b2009-12-21 12:21:52 +00001307 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001308
Liam Girdwood414c70c2008-04-30 15:59:04 +01001309 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001310 if (rdev->constraints &&
1311 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1312 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001313
Mark Brown9a2372f2009-08-03 18:49:57 +01001314 if (rdev->use_count == 0) {
1315 /* The regulator may on if it's not switchable or left on */
1316 ret = _regulator_is_enabled(rdev);
1317 if (ret == -EINVAL || ret == 0) {
1318 if (!_regulator_can_change_status(rdev))
1319 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001320
Mark Brown31aae2b2009-12-21 12:21:52 +00001321 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001322 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001323
1324 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001325 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001326 ret = _regulator_get_enable_time(rdev);
1327 if (ret >= 0) {
1328 delay = ret;
1329 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001330 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001331 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001332 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001333 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001334
Mark Brown02fa3ec2010-11-10 14:38:30 +00001335 trace_regulator_enable(rdev_get_name(rdev));
1336
Mark Brown31aae2b2009-12-21 12:21:52 +00001337 /* Allow the regulator to ramp; it would be useful
1338 * to extend this for bulk operations so that the
1339 * regulators can ramp together. */
1340 ret = rdev->desc->ops->enable(rdev);
1341 if (ret < 0)
1342 return ret;
1343
Mark Brown02fa3ec2010-11-10 14:38:30 +00001344 trace_regulator_enable_delay(rdev_get_name(rdev));
1345
Axel Line36c1df2010-11-05 21:51:32 +08001346 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001347 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001348 udelay(delay % 1000);
1349 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001350 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001351 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001352
Mark Brown02fa3ec2010-11-10 14:38:30 +00001353 trace_regulator_enable_complete(rdev_get_name(rdev));
1354
Linus Walleija7433cf2009-08-26 12:54:04 +02001355 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001356 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001357 return ret;
1358 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001359 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001360 }
1361
Mark Brown9a2372f2009-08-03 18:49:57 +01001362 rdev->use_count++;
1363
1364 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365}
1366
1367/**
1368 * regulator_enable - enable regulator output
1369 * @regulator: regulator source
1370 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001371 * Request that the regulator be enabled with the regulator output at
1372 * the predefined voltage or current value. Calls to regulator_enable()
1373 * must be balanced with calls to regulator_disable().
1374 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001376 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377 */
1378int regulator_enable(struct regulator *regulator)
1379{
David Brownell412aec62008-11-16 11:44:46 -08001380 struct regulator_dev *rdev = regulator->rdev;
1381 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382
Mark Brown3801b862011-06-09 16:22:22 +01001383 if (rdev->supply) {
1384 ret = regulator_enable(rdev->supply);
1385 if (ret != 0)
1386 return ret;
1387 }
1388
David Brownell412aec62008-11-16 11:44:46 -08001389 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001390 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001391 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001392
1393 if (ret != 0)
1394 regulator_disable(rdev->supply);
1395
Liam Girdwood414c70c2008-04-30 15:59:04 +01001396 return ret;
1397}
1398EXPORT_SYMBOL_GPL(regulator_enable);
1399
1400/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001401static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402{
1403 int ret = 0;
1404
David Brownellcd94b502009-03-11 16:43:34 -08001405 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001406 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001407 return -EIO;
1408
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001410 if (rdev->use_count == 1 &&
1411 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412
1413 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001414 if (_regulator_can_change_status(rdev) &&
1415 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001416 trace_regulator_disable(rdev_get_name(rdev));
1417
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418 ret = rdev->desc->ops->disable(rdev);
1419 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001420 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 return ret;
1422 }
Mark Brown84b68262009-12-01 21:12:27 +00001423
Mark Brown02fa3ec2010-11-10 14:38:30 +00001424 trace_regulator_disable_complete(rdev_get_name(rdev));
1425
Mark Brown84b68262009-12-01 21:12:27 +00001426 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1427 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001428 }
1429
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430 rdev->use_count = 0;
1431 } else if (rdev->use_count > 1) {
1432
1433 if (rdev->constraints &&
1434 (rdev->constraints->valid_ops_mask &
1435 REGULATOR_CHANGE_DRMS))
1436 drms_uA_update(rdev);
1437
1438 rdev->use_count--;
1439 }
Mark Brown3801b862011-06-09 16:22:22 +01001440
Liam Girdwood414c70c2008-04-30 15:59:04 +01001441 return ret;
1442}
1443
1444/**
1445 * regulator_disable - disable regulator output
1446 * @regulator: regulator source
1447 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001448 * Disable the regulator output voltage or current. Calls to
1449 * regulator_enable() must be balanced with calls to
1450 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001451 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001452 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001453 * devices have it enabled, the regulator device supports disabling and
1454 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 */
1456int regulator_disable(struct regulator *regulator)
1457{
David Brownell412aec62008-11-16 11:44:46 -08001458 struct regulator_dev *rdev = regulator->rdev;
1459 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460
David Brownell412aec62008-11-16 11:44:46 -08001461 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001462 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001463 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001464
Mark Brown3801b862011-06-09 16:22:22 +01001465 if (ret == 0 && rdev->supply)
1466 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001467
Liam Girdwood414c70c2008-04-30 15:59:04 +01001468 return ret;
1469}
1470EXPORT_SYMBOL_GPL(regulator_disable);
1471
1472/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001473static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001474{
1475 int ret = 0;
1476
1477 /* force disable */
1478 if (rdev->desc->ops->disable) {
1479 /* ah well, who wants to live forever... */
1480 ret = rdev->desc->ops->disable(rdev);
1481 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001482 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483 return ret;
1484 }
1485 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001486 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1487 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488 }
1489
Liam Girdwood414c70c2008-04-30 15:59:04 +01001490 return ret;
1491}
1492
1493/**
1494 * regulator_force_disable - force disable regulator output
1495 * @regulator: regulator source
1496 *
1497 * Forcibly disable the regulator output voltage or current.
1498 * NOTE: this *will* disable the regulator output even if other consumer
1499 * devices have it enabled. This should be used for situations when device
1500 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1501 */
1502int regulator_force_disable(struct regulator *regulator)
1503{
Mark Brown82d15832011-05-09 11:41:02 +02001504 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505 int ret;
1506
Mark Brown82d15832011-05-09 11:41:02 +02001507 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001509 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001510 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001511
Mark Brown3801b862011-06-09 16:22:22 +01001512 if (rdev->supply)
1513 while (rdev->open_count--)
1514 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001515
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 return ret;
1517}
1518EXPORT_SYMBOL_GPL(regulator_force_disable);
1519
1520static int _regulator_is_enabled(struct regulator_dev *rdev)
1521{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001522 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001523 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001524 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001525
Mark Brown93325462009-08-03 18:49:56 +01001526 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527}
1528
1529/**
1530 * regulator_is_enabled - is the regulator output enabled
1531 * @regulator: regulator source
1532 *
David Brownell412aec62008-11-16 11:44:46 -08001533 * Returns positive if the regulator driver backing the source/client
1534 * has requested that the device be enabled, zero if it hasn't, else a
1535 * negative errno code.
1536 *
1537 * Note that the device backing this regulator handle can have multiple
1538 * users, so it might be enabled even if regulator_enable() was never
1539 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540 */
1541int regulator_is_enabled(struct regulator *regulator)
1542{
Mark Brown93325462009-08-03 18:49:56 +01001543 int ret;
1544
1545 mutex_lock(&regulator->rdev->mutex);
1546 ret = _regulator_is_enabled(regulator->rdev);
1547 mutex_unlock(&regulator->rdev->mutex);
1548
1549 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001550}
1551EXPORT_SYMBOL_GPL(regulator_is_enabled);
1552
1553/**
David Brownell4367cfd2009-02-26 11:48:36 -08001554 * regulator_count_voltages - count regulator_list_voltage() selectors
1555 * @regulator: regulator source
1556 *
1557 * Returns number of selectors, or negative errno. Selectors are
1558 * numbered starting at zero, and typically correspond to bitfields
1559 * in hardware registers.
1560 */
1561int regulator_count_voltages(struct regulator *regulator)
1562{
1563 struct regulator_dev *rdev = regulator->rdev;
1564
1565 return rdev->desc->n_voltages ? : -EINVAL;
1566}
1567EXPORT_SYMBOL_GPL(regulator_count_voltages);
1568
1569/**
1570 * regulator_list_voltage - enumerate supported voltages
1571 * @regulator: regulator source
1572 * @selector: identify voltage to list
1573 * Context: can sleep
1574 *
1575 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001576 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001577 * negative errno.
1578 */
1579int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1580{
1581 struct regulator_dev *rdev = regulator->rdev;
1582 struct regulator_ops *ops = rdev->desc->ops;
1583 int ret;
1584
1585 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1586 return -EINVAL;
1587
1588 mutex_lock(&rdev->mutex);
1589 ret = ops->list_voltage(rdev, selector);
1590 mutex_unlock(&rdev->mutex);
1591
1592 if (ret > 0) {
1593 if (ret < rdev->constraints->min_uV)
1594 ret = 0;
1595 else if (ret > rdev->constraints->max_uV)
1596 ret = 0;
1597 }
1598
1599 return ret;
1600}
1601EXPORT_SYMBOL_GPL(regulator_list_voltage);
1602
1603/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001604 * regulator_is_supported_voltage - check if a voltage range can be supported
1605 *
1606 * @regulator: Regulator to check.
1607 * @min_uV: Minimum required voltage in uV.
1608 * @max_uV: Maximum required voltage in uV.
1609 *
1610 * Returns a boolean or a negative error code.
1611 */
1612int regulator_is_supported_voltage(struct regulator *regulator,
1613 int min_uV, int max_uV)
1614{
1615 int i, voltages, ret;
1616
1617 ret = regulator_count_voltages(regulator);
1618 if (ret < 0)
1619 return ret;
1620 voltages = ret;
1621
1622 for (i = 0; i < voltages; i++) {
1623 ret = regulator_list_voltage(regulator, i);
1624
1625 if (ret >= min_uV && ret <= max_uV)
1626 return 1;
1627 }
1628
1629 return 0;
1630}
1631
Mark Brown75790252010-12-12 14:25:50 +00001632static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1633 int min_uV, int max_uV)
1634{
1635 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001636 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001637 unsigned int selector;
1638
1639 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1640
Mark Brownbf5892a2011-05-08 22:13:37 +01001641 min_uV += rdev->constraints->uV_offset;
1642 max_uV += rdev->constraints->uV_offset;
1643
Mark Brown75790252010-12-12 14:25:50 +00001644 if (rdev->desc->ops->set_voltage) {
1645 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1646 &selector);
1647
1648 if (rdev->desc->ops->list_voltage)
1649 selector = rdev->desc->ops->list_voltage(rdev,
1650 selector);
1651 else
1652 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001653 } else if (rdev->desc->ops->set_voltage_sel) {
1654 int best_val = INT_MAX;
1655 int i;
1656
1657 selector = 0;
1658
1659 /* Find the smallest voltage that falls within the specified
1660 * range.
1661 */
1662 for (i = 0; i < rdev->desc->n_voltages; i++) {
1663 ret = rdev->desc->ops->list_voltage(rdev, i);
1664 if (ret < 0)
1665 continue;
1666
1667 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1668 best_val = ret;
1669 selector = i;
1670 }
1671 }
1672
Linus Walleij77af1b2642011-03-17 13:24:36 +01001673 /*
1674 * If we can't obtain the old selector there is not enough
1675 * info to call set_voltage_time_sel().
1676 */
1677 if (rdev->desc->ops->set_voltage_time_sel &&
1678 rdev->desc->ops->get_voltage_sel) {
1679 unsigned int old_selector = 0;
1680
1681 ret = rdev->desc->ops->get_voltage_sel(rdev);
1682 if (ret < 0)
1683 return ret;
1684 old_selector = ret;
1685 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1686 old_selector, selector);
1687 }
1688
Mark Browne8eef822010-12-12 14:36:17 +00001689 if (best_val != INT_MAX) {
1690 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1691 selector = best_val;
1692 } else {
1693 ret = -EINVAL;
1694 }
Mark Brown75790252010-12-12 14:25:50 +00001695 } else {
1696 ret = -EINVAL;
1697 }
1698
Linus Walleij77af1b2642011-03-17 13:24:36 +01001699 /* Insert any necessary delays */
1700 if (delay >= 1000) {
1701 mdelay(delay / 1000);
1702 udelay(delay % 1000);
1703 } else if (delay) {
1704 udelay(delay);
1705 }
1706
Mark Brownded06a52010-12-16 13:59:10 +00001707 if (ret == 0)
1708 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1709 NULL);
1710
Mark Brown75790252010-12-12 14:25:50 +00001711 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1712
1713 return ret;
1714}
1715
Mark Browna7a1ad92009-07-21 16:00:24 +01001716/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001717 * regulator_set_voltage - set regulator output voltage
1718 * @regulator: regulator source
1719 * @min_uV: Minimum required voltage in uV
1720 * @max_uV: Maximum acceptable voltage in uV
1721 *
1722 * Sets a voltage regulator to the desired output voltage. This can be set
1723 * during any regulator state. IOW, regulator can be disabled or enabled.
1724 *
1725 * If the regulator is enabled then the voltage will change to the new value
1726 * immediately otherwise if the regulator is disabled the regulator will
1727 * output at the new voltage when enabled.
1728 *
1729 * NOTE: If the regulator is shared between several devices then the lowest
1730 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001731 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001732 * calling this function otherwise this call will fail.
1733 */
1734int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1735{
1736 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001737 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001738
1739 mutex_lock(&rdev->mutex);
1740
Mark Brown95a3c232010-12-16 15:49:37 +00001741 /* If we're setting the same range as last time the change
1742 * should be a noop (some cpufreq implementations use the same
1743 * voltage for multiple frequencies, for example).
1744 */
1745 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1746 goto out;
1747
Liam Girdwood414c70c2008-04-30 15:59:04 +01001748 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001749 if (!rdev->desc->ops->set_voltage &&
1750 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001751 ret = -EINVAL;
1752 goto out;
1753 }
1754
1755 /* constraints check */
1756 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1757 if (ret < 0)
1758 goto out;
1759 regulator->min_uV = min_uV;
1760 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001761
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001762 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1763 if (ret < 0)
1764 goto out;
1765
Mark Brown75790252010-12-12 14:25:50 +00001766 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001767
Liam Girdwood414c70c2008-04-30 15:59:04 +01001768out:
1769 mutex_unlock(&rdev->mutex);
1770 return ret;
1771}
1772EXPORT_SYMBOL_GPL(regulator_set_voltage);
1773
Mark Brown606a2562010-12-16 15:49:36 +00001774/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001775 * regulator_set_voltage_time - get raise/fall time
1776 * @regulator: regulator source
1777 * @old_uV: starting voltage in microvolts
1778 * @new_uV: target voltage in microvolts
1779 *
1780 * Provided with the starting and ending voltage, this function attempts to
1781 * calculate the time in microseconds required to rise or fall to this new
1782 * voltage.
1783 */
1784int regulator_set_voltage_time(struct regulator *regulator,
1785 int old_uV, int new_uV)
1786{
1787 struct regulator_dev *rdev = regulator->rdev;
1788 struct regulator_ops *ops = rdev->desc->ops;
1789 int old_sel = -1;
1790 int new_sel = -1;
1791 int voltage;
1792 int i;
1793
1794 /* Currently requires operations to do this */
1795 if (!ops->list_voltage || !ops->set_voltage_time_sel
1796 || !rdev->desc->n_voltages)
1797 return -EINVAL;
1798
1799 for (i = 0; i < rdev->desc->n_voltages; i++) {
1800 /* We only look for exact voltage matches here */
1801 voltage = regulator_list_voltage(regulator, i);
1802 if (voltage < 0)
1803 return -EINVAL;
1804 if (voltage == 0)
1805 continue;
1806 if (voltage == old_uV)
1807 old_sel = i;
1808 if (voltage == new_uV)
1809 new_sel = i;
1810 }
1811
1812 if (old_sel < 0 || new_sel < 0)
1813 return -EINVAL;
1814
1815 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1816}
1817EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1818
1819/**
Mark Brown606a2562010-12-16 15:49:36 +00001820 * regulator_sync_voltage - re-apply last regulator output voltage
1821 * @regulator: regulator source
1822 *
1823 * Re-apply the last configured voltage. This is intended to be used
1824 * where some external control source the consumer is cooperating with
1825 * has caused the configured voltage to change.
1826 */
1827int regulator_sync_voltage(struct regulator *regulator)
1828{
1829 struct regulator_dev *rdev = regulator->rdev;
1830 int ret, min_uV, max_uV;
1831
1832 mutex_lock(&rdev->mutex);
1833
1834 if (!rdev->desc->ops->set_voltage &&
1835 !rdev->desc->ops->set_voltage_sel) {
1836 ret = -EINVAL;
1837 goto out;
1838 }
1839
1840 /* This is only going to work if we've had a voltage configured. */
1841 if (!regulator->min_uV && !regulator->max_uV) {
1842 ret = -EINVAL;
1843 goto out;
1844 }
1845
1846 min_uV = regulator->min_uV;
1847 max_uV = regulator->max_uV;
1848
1849 /* This should be a paranoia check... */
1850 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1851 if (ret < 0)
1852 goto out;
1853
1854 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1855 if (ret < 0)
1856 goto out;
1857
1858 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1859
1860out:
1861 mutex_unlock(&rdev->mutex);
1862 return ret;
1863}
1864EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1865
Liam Girdwood414c70c2008-04-30 15:59:04 +01001866static int _regulator_get_voltage(struct regulator_dev *rdev)
1867{
Mark Brownbf5892a2011-05-08 22:13:37 +01001868 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001869
1870 if (rdev->desc->ops->get_voltage_sel) {
1871 sel = rdev->desc->ops->get_voltage_sel(rdev);
1872 if (sel < 0)
1873 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001874 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08001875 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01001876 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08001877 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001878 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08001879 }
Mark Brownbf5892a2011-05-08 22:13:37 +01001880
Axel Lincb220d12011-05-23 20:08:10 +08001881 if (ret < 0)
1882 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01001883 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001884}
1885
1886/**
1887 * regulator_get_voltage - get regulator output voltage
1888 * @regulator: regulator source
1889 *
1890 * This returns the current regulator voltage in uV.
1891 *
1892 * NOTE: If the regulator is disabled it will return the voltage value. This
1893 * function should not be used to determine regulator state.
1894 */
1895int regulator_get_voltage(struct regulator *regulator)
1896{
1897 int ret;
1898
1899 mutex_lock(&regulator->rdev->mutex);
1900
1901 ret = _regulator_get_voltage(regulator->rdev);
1902
1903 mutex_unlock(&regulator->rdev->mutex);
1904
1905 return ret;
1906}
1907EXPORT_SYMBOL_GPL(regulator_get_voltage);
1908
1909/**
1910 * regulator_set_current_limit - set regulator output current limit
1911 * @regulator: regulator source
1912 * @min_uA: Minimuum supported current in uA
1913 * @max_uA: Maximum supported current in uA
1914 *
1915 * Sets current sink to the desired output current. This can be set during
1916 * any regulator state. IOW, regulator can be disabled or enabled.
1917 *
1918 * If the regulator is enabled then the current will change to the new value
1919 * immediately otherwise if the regulator is disabled the regulator will
1920 * output at the new current when enabled.
1921 *
1922 * NOTE: Regulator system constraints must be set for this regulator before
1923 * calling this function otherwise this call will fail.
1924 */
1925int regulator_set_current_limit(struct regulator *regulator,
1926 int min_uA, int max_uA)
1927{
1928 struct regulator_dev *rdev = regulator->rdev;
1929 int ret;
1930
1931 mutex_lock(&rdev->mutex);
1932
1933 /* sanity check */
1934 if (!rdev->desc->ops->set_current_limit) {
1935 ret = -EINVAL;
1936 goto out;
1937 }
1938
1939 /* constraints check */
1940 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1941 if (ret < 0)
1942 goto out;
1943
1944 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1945out:
1946 mutex_unlock(&rdev->mutex);
1947 return ret;
1948}
1949EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1950
1951static int _regulator_get_current_limit(struct regulator_dev *rdev)
1952{
1953 int ret;
1954
1955 mutex_lock(&rdev->mutex);
1956
1957 /* sanity check */
1958 if (!rdev->desc->ops->get_current_limit) {
1959 ret = -EINVAL;
1960 goto out;
1961 }
1962
1963 ret = rdev->desc->ops->get_current_limit(rdev);
1964out:
1965 mutex_unlock(&rdev->mutex);
1966 return ret;
1967}
1968
1969/**
1970 * regulator_get_current_limit - get regulator output current
1971 * @regulator: regulator source
1972 *
1973 * This returns the current supplied by the specified current sink in uA.
1974 *
1975 * NOTE: If the regulator is disabled it will return the current value. This
1976 * function should not be used to determine regulator state.
1977 */
1978int regulator_get_current_limit(struct regulator *regulator)
1979{
1980 return _regulator_get_current_limit(regulator->rdev);
1981}
1982EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1983
1984/**
1985 * regulator_set_mode - set regulator operating mode
1986 * @regulator: regulator source
1987 * @mode: operating mode - one of the REGULATOR_MODE constants
1988 *
1989 * Set regulator operating mode to increase regulator efficiency or improve
1990 * regulation performance.
1991 *
1992 * NOTE: Regulator system constraints must be set for this regulator before
1993 * calling this function otherwise this call will fail.
1994 */
1995int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1996{
1997 struct regulator_dev *rdev = regulator->rdev;
1998 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301999 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002000
2001 mutex_lock(&rdev->mutex);
2002
2003 /* sanity check */
2004 if (!rdev->desc->ops->set_mode) {
2005 ret = -EINVAL;
2006 goto out;
2007 }
2008
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302009 /* return if the same mode is requested */
2010 if (rdev->desc->ops->get_mode) {
2011 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2012 if (regulator_curr_mode == mode) {
2013 ret = 0;
2014 goto out;
2015 }
2016 }
2017
Liam Girdwood414c70c2008-04-30 15:59:04 +01002018 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002019 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002020 if (ret < 0)
2021 goto out;
2022
2023 ret = rdev->desc->ops->set_mode(rdev, mode);
2024out:
2025 mutex_unlock(&rdev->mutex);
2026 return ret;
2027}
2028EXPORT_SYMBOL_GPL(regulator_set_mode);
2029
2030static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2031{
2032 int ret;
2033
2034 mutex_lock(&rdev->mutex);
2035
2036 /* sanity check */
2037 if (!rdev->desc->ops->get_mode) {
2038 ret = -EINVAL;
2039 goto out;
2040 }
2041
2042 ret = rdev->desc->ops->get_mode(rdev);
2043out:
2044 mutex_unlock(&rdev->mutex);
2045 return ret;
2046}
2047
2048/**
2049 * regulator_get_mode - get regulator operating mode
2050 * @regulator: regulator source
2051 *
2052 * Get the current regulator operating mode.
2053 */
2054unsigned int regulator_get_mode(struct regulator *regulator)
2055{
2056 return _regulator_get_mode(regulator->rdev);
2057}
2058EXPORT_SYMBOL_GPL(regulator_get_mode);
2059
2060/**
2061 * regulator_set_optimum_mode - set regulator optimum operating mode
2062 * @regulator: regulator source
2063 * @uA_load: load current
2064 *
2065 * Notifies the regulator core of a new device load. This is then used by
2066 * DRMS (if enabled by constraints) to set the most efficient regulator
2067 * operating mode for the new regulator loading.
2068 *
2069 * Consumer devices notify their supply regulator of the maximum power
2070 * they will require (can be taken from device datasheet in the power
2071 * consumption tables) when they change operational status and hence power
2072 * state. Examples of operational state changes that can affect power
2073 * consumption are :-
2074 *
2075 * o Device is opened / closed.
2076 * o Device I/O is about to begin or has just finished.
2077 * o Device is idling in between work.
2078 *
2079 * This information is also exported via sysfs to userspace.
2080 *
2081 * DRMS will sum the total requested load on the regulator and change
2082 * to the most efficient operating mode if platform constraints allow.
2083 *
2084 * Returns the new regulator mode or error.
2085 */
2086int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2087{
2088 struct regulator_dev *rdev = regulator->rdev;
2089 struct regulator *consumer;
2090 int ret, output_uV, input_uV, total_uA_load = 0;
2091 unsigned int mode;
2092
2093 mutex_lock(&rdev->mutex);
2094
Mark Browna4b41482011-05-14 11:19:45 -07002095 /*
2096 * first check to see if we can set modes at all, otherwise just
2097 * tell the consumer everything is OK.
2098 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002099 regulator->uA_load = uA_load;
2100 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002101 if (ret < 0) {
2102 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002104 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002105
Liam Girdwood414c70c2008-04-30 15:59:04 +01002106 if (!rdev->desc->ops->get_optimum_mode)
2107 goto out;
2108
Mark Browna4b41482011-05-14 11:19:45 -07002109 /*
2110 * we can actually do this so any errors are indicators of
2111 * potential real failure.
2112 */
2113 ret = -EINVAL;
2114
Liam Girdwood414c70c2008-04-30 15:59:04 +01002115 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002116 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002117 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002118 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002119 goto out;
2120 }
2121
2122 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002123 input_uV = 0;
2124 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002125 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002126 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002127 input_uV = rdev->constraints->input_uV;
2128 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002129 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002130 goto out;
2131 }
2132
2133 /* calc total requested load for this regulator */
2134 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002135 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002136
2137 mode = rdev->desc->ops->get_optimum_mode(rdev,
2138 input_uV, output_uV,
2139 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002140 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002141 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002142 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2143 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002144 goto out;
2145 }
2146
2147 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002148 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002149 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002150 goto out;
2151 }
2152 ret = mode;
2153out:
2154 mutex_unlock(&rdev->mutex);
2155 return ret;
2156}
2157EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2158
2159/**
2160 * regulator_register_notifier - register regulator event notifier
2161 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002162 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002163 *
2164 * Register notifier block to receive regulator events.
2165 */
2166int regulator_register_notifier(struct regulator *regulator,
2167 struct notifier_block *nb)
2168{
2169 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2170 nb);
2171}
2172EXPORT_SYMBOL_GPL(regulator_register_notifier);
2173
2174/**
2175 * regulator_unregister_notifier - unregister regulator event notifier
2176 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002177 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002178 *
2179 * Unregister regulator event notifier block.
2180 */
2181int regulator_unregister_notifier(struct regulator *regulator,
2182 struct notifier_block *nb)
2183{
2184 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2185 nb);
2186}
2187EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2188
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002189/* notify regulator consumers and downstream regulator consumers.
2190 * Note mutex must be held by caller.
2191 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002192static void _notifier_call_chain(struct regulator_dev *rdev,
2193 unsigned long event, void *data)
2194{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002196 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002197}
2198
2199/**
2200 * regulator_bulk_get - get multiple regulator consumers
2201 *
2202 * @dev: Device to supply
2203 * @num_consumers: Number of consumers to register
2204 * @consumers: Configuration of consumers; clients are stored here.
2205 *
2206 * @return 0 on success, an errno on failure.
2207 *
2208 * This helper function allows drivers to get several regulator
2209 * consumers in one operation. If any of the regulators cannot be
2210 * acquired then any regulators that were allocated will be freed
2211 * before returning to the caller.
2212 */
2213int regulator_bulk_get(struct device *dev, int num_consumers,
2214 struct regulator_bulk_data *consumers)
2215{
2216 int i;
2217 int ret;
2218
2219 for (i = 0; i < num_consumers; i++)
2220 consumers[i].consumer = NULL;
2221
2222 for (i = 0; i < num_consumers; i++) {
2223 consumers[i].consumer = regulator_get(dev,
2224 consumers[i].supply);
2225 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002226 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002227 dev_err(dev, "Failed to get supply '%s': %d\n",
2228 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002229 consumers[i].consumer = NULL;
2230 goto err;
2231 }
2232 }
2233
2234 return 0;
2235
2236err:
2237 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2238 regulator_put(consumers[i].consumer);
2239
2240 return ret;
2241}
2242EXPORT_SYMBOL_GPL(regulator_bulk_get);
2243
Mark Brownf21e0e82011-05-24 08:12:40 +08002244static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2245{
2246 struct regulator_bulk_data *bulk = data;
2247
2248 bulk->ret = regulator_enable(bulk->consumer);
2249}
2250
Liam Girdwood414c70c2008-04-30 15:59:04 +01002251/**
2252 * regulator_bulk_enable - enable multiple regulator consumers
2253 *
2254 * @num_consumers: Number of consumers
2255 * @consumers: Consumer data; clients are stored here.
2256 * @return 0 on success, an errno on failure
2257 *
2258 * This convenience API allows consumers to enable multiple regulator
2259 * clients in a single API call. If any consumers cannot be enabled
2260 * then any others that were enabled will be disabled again prior to
2261 * return.
2262 */
2263int regulator_bulk_enable(int num_consumers,
2264 struct regulator_bulk_data *consumers)
2265{
Mark Brownf21e0e82011-05-24 08:12:40 +08002266 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002268 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002269
Mark Brownf21e0e82011-05-24 08:12:40 +08002270 for (i = 0; i < num_consumers; i++)
2271 async_schedule_domain(regulator_bulk_enable_async,
2272 &consumers[i], &async_domain);
2273
2274 async_synchronize_full_domain(&async_domain);
2275
2276 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002277 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002278 if (consumers[i].ret != 0) {
2279 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002281 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002282 }
2283
2284 return 0;
2285
2286err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002287 for (i = 0; i < num_consumers; i++)
2288 if (consumers[i].ret == 0)
2289 regulator_disable(consumers[i].consumer);
2290 else
2291 pr_err("Failed to enable %s: %d\n",
2292 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002293
2294 return ret;
2295}
2296EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2297
2298/**
2299 * regulator_bulk_disable - disable multiple regulator consumers
2300 *
2301 * @num_consumers: Number of consumers
2302 * @consumers: Consumer data; clients are stored here.
2303 * @return 0 on success, an errno on failure
2304 *
2305 * This convenience API allows consumers to disable multiple regulator
2306 * clients in a single API call. If any consumers cannot be enabled
2307 * then any others that were disabled will be disabled again prior to
2308 * return.
2309 */
2310int regulator_bulk_disable(int num_consumers,
2311 struct regulator_bulk_data *consumers)
2312{
2313 int i;
2314 int ret;
2315
2316 for (i = 0; i < num_consumers; i++) {
2317 ret = regulator_disable(consumers[i].consumer);
2318 if (ret != 0)
2319 goto err;
2320 }
2321
2322 return 0;
2323
2324err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002325 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002326 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327 regulator_enable(consumers[i].consumer);
2328
2329 return ret;
2330}
2331EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2332
2333/**
2334 * regulator_bulk_free - free multiple regulator consumers
2335 *
2336 * @num_consumers: Number of consumers
2337 * @consumers: Consumer data; clients are stored here.
2338 *
2339 * This convenience API allows consumers to free multiple regulator
2340 * clients in a single API call.
2341 */
2342void regulator_bulk_free(int num_consumers,
2343 struct regulator_bulk_data *consumers)
2344{
2345 int i;
2346
2347 for (i = 0; i < num_consumers; i++) {
2348 regulator_put(consumers[i].consumer);
2349 consumers[i].consumer = NULL;
2350 }
2351}
2352EXPORT_SYMBOL_GPL(regulator_bulk_free);
2353
2354/**
2355 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002356 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002358 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002359 *
2360 * Called by regulator drivers to notify clients a regulator event has
2361 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002362 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002363 */
2364int regulator_notifier_call_chain(struct regulator_dev *rdev,
2365 unsigned long event, void *data)
2366{
2367 _notifier_call_chain(rdev, event, data);
2368 return NOTIFY_DONE;
2369
2370}
2371EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2372
Mark Brownbe721972009-08-04 20:09:52 +02002373/**
2374 * regulator_mode_to_status - convert a regulator mode into a status
2375 *
2376 * @mode: Mode to convert
2377 *
2378 * Convert a regulator mode into a status.
2379 */
2380int regulator_mode_to_status(unsigned int mode)
2381{
2382 switch (mode) {
2383 case REGULATOR_MODE_FAST:
2384 return REGULATOR_STATUS_FAST;
2385 case REGULATOR_MODE_NORMAL:
2386 return REGULATOR_STATUS_NORMAL;
2387 case REGULATOR_MODE_IDLE:
2388 return REGULATOR_STATUS_IDLE;
2389 case REGULATOR_STATUS_STANDBY:
2390 return REGULATOR_STATUS_STANDBY;
2391 default:
2392 return 0;
2393 }
2394}
2395EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2396
David Brownell7ad68e22008-11-11 17:39:02 -08002397/*
2398 * To avoid cluttering sysfs (and memory) with useless state, only
2399 * create attributes that can be meaningfully displayed.
2400 */
2401static int add_regulator_attributes(struct regulator_dev *rdev)
2402{
2403 struct device *dev = &rdev->dev;
2404 struct regulator_ops *ops = rdev->desc->ops;
2405 int status = 0;
2406
2407 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002408 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002409 status = device_create_file(dev, &dev_attr_microvolts);
2410 if (status < 0)
2411 return status;
2412 }
2413 if (ops->get_current_limit) {
2414 status = device_create_file(dev, &dev_attr_microamps);
2415 if (status < 0)
2416 return status;
2417 }
2418 if (ops->get_mode) {
2419 status = device_create_file(dev, &dev_attr_opmode);
2420 if (status < 0)
2421 return status;
2422 }
2423 if (ops->is_enabled) {
2424 status = device_create_file(dev, &dev_attr_state);
2425 if (status < 0)
2426 return status;
2427 }
David Brownell853116a2009-01-14 23:03:17 -08002428 if (ops->get_status) {
2429 status = device_create_file(dev, &dev_attr_status);
2430 if (status < 0)
2431 return status;
2432 }
David Brownell7ad68e22008-11-11 17:39:02 -08002433
2434 /* some attributes are type-specific */
2435 if (rdev->desc->type == REGULATOR_CURRENT) {
2436 status = device_create_file(dev, &dev_attr_requested_microamps);
2437 if (status < 0)
2438 return status;
2439 }
2440
2441 /* all the other attributes exist to support constraints;
2442 * don't show them if there are no constraints, or if the
2443 * relevant supporting methods are missing.
2444 */
2445 if (!rdev->constraints)
2446 return status;
2447
2448 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002449 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002450 status = device_create_file(dev, &dev_attr_min_microvolts);
2451 if (status < 0)
2452 return status;
2453 status = device_create_file(dev, &dev_attr_max_microvolts);
2454 if (status < 0)
2455 return status;
2456 }
2457 if (ops->set_current_limit) {
2458 status = device_create_file(dev, &dev_attr_min_microamps);
2459 if (status < 0)
2460 return status;
2461 status = device_create_file(dev, &dev_attr_max_microamps);
2462 if (status < 0)
2463 return status;
2464 }
2465
2466 /* suspend mode constraints need multiple supporting methods */
2467 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2468 return status;
2469
2470 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2471 if (status < 0)
2472 return status;
2473 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2474 if (status < 0)
2475 return status;
2476 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2477 if (status < 0)
2478 return status;
2479
2480 if (ops->set_suspend_voltage) {
2481 status = device_create_file(dev,
2482 &dev_attr_suspend_standby_microvolts);
2483 if (status < 0)
2484 return status;
2485 status = device_create_file(dev,
2486 &dev_attr_suspend_mem_microvolts);
2487 if (status < 0)
2488 return status;
2489 status = device_create_file(dev,
2490 &dev_attr_suspend_disk_microvolts);
2491 if (status < 0)
2492 return status;
2493 }
2494
2495 if (ops->set_suspend_mode) {
2496 status = device_create_file(dev,
2497 &dev_attr_suspend_standby_mode);
2498 if (status < 0)
2499 return status;
2500 status = device_create_file(dev,
2501 &dev_attr_suspend_mem_mode);
2502 if (status < 0)
2503 return status;
2504 status = device_create_file(dev,
2505 &dev_attr_suspend_disk_mode);
2506 if (status < 0)
2507 return status;
2508 }
2509
2510 return status;
2511}
2512
Mark Brown1130e5b2010-12-21 23:49:31 +00002513static void rdev_init_debugfs(struct regulator_dev *rdev)
2514{
2515#ifdef CONFIG_DEBUG_FS
2516 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2517 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2518 rdev_warn(rdev, "Failed to create debugfs directory\n");
2519 rdev->debugfs = NULL;
2520 return;
2521 }
2522
2523 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2524 &rdev->use_count);
2525 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2526 &rdev->open_count);
2527#endif
2528}
2529
Liam Girdwood414c70c2008-04-30 15:59:04 +01002530/**
2531 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002532 * @regulator_desc: regulator to register
2533 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002534 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002535 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536 *
2537 * Called by regulator drivers to register a regulator.
2538 * Returns 0 on success.
2539 */
2540struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002541 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002542 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002543{
2544 static atomic_t regulator_no = ATOMIC_INIT(0);
2545 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002546 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002547
2548 if (regulator_desc == NULL)
2549 return ERR_PTR(-EINVAL);
2550
2551 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2552 return ERR_PTR(-EINVAL);
2553
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002554 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2555 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002556 return ERR_PTR(-EINVAL);
2557
Mark Brown46fabe1e2008-09-09 16:21:18 +01002558 if (!init_data)
2559 return ERR_PTR(-EINVAL);
2560
Mark Brown476c2d82010-12-10 17:28:07 +00002561 /* Only one of each should be implemented */
2562 WARN_ON(regulator_desc->ops->get_voltage &&
2563 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002564 WARN_ON(regulator_desc->ops->set_voltage &&
2565 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002566
2567 /* If we're using selectors we must implement list_voltage. */
2568 if (regulator_desc->ops->get_voltage_sel &&
2569 !regulator_desc->ops->list_voltage) {
2570 return ERR_PTR(-EINVAL);
2571 }
Mark Browne8eef822010-12-12 14:36:17 +00002572 if (regulator_desc->ops->set_voltage_sel &&
2573 !regulator_desc->ops->list_voltage) {
2574 return ERR_PTR(-EINVAL);
2575 }
Mark Brown476c2d82010-12-10 17:28:07 +00002576
Liam Girdwood414c70c2008-04-30 15:59:04 +01002577 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2578 if (rdev == NULL)
2579 return ERR_PTR(-ENOMEM);
2580
2581 mutex_lock(&regulator_list_mutex);
2582
2583 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002584 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002585 rdev->owner = regulator_desc->owner;
2586 rdev->desc = regulator_desc;
2587 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002588 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002589 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2590
Liam Girdwooda5766f12008-10-10 13:22:20 +01002591 /* preform any regulator specific init */
2592 if (init_data->regulator_init) {
2593 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002594 if (ret < 0)
2595 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002596 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002597
Liam Girdwooda5766f12008-10-10 13:22:20 +01002598 /* register with sysfs */
2599 rdev->dev.class = &regulator_class;
2600 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002601 dev_set_name(&rdev->dev, "regulator.%d",
2602 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002603 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002604 if (ret != 0) {
2605 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002606 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002607 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002608
2609 dev_set_drvdata(&rdev->dev, rdev);
2610
Mike Rapoport74f544c2008-11-25 14:53:53 +02002611 /* set regulator constraints */
2612 ret = set_machine_constraints(rdev, &init_data->constraints);
2613 if (ret < 0)
2614 goto scrub;
2615
David Brownell7ad68e22008-11-11 17:39:02 -08002616 /* add attributes supported by this regulator */
2617 ret = add_regulator_attributes(rdev);
2618 if (ret < 0)
2619 goto scrub;
2620
Mark Brown0178f3e2010-04-26 15:18:14 +01002621 if (init_data->supply_regulator) {
2622 struct regulator_dev *r;
2623 int found = 0;
2624
2625 list_for_each_entry(r, &regulator_list, list) {
2626 if (strcmp(rdev_get_name(r),
2627 init_data->supply_regulator) == 0) {
2628 found = 1;
2629 break;
2630 }
2631 }
2632
2633 if (!found) {
2634 dev_err(dev, "Failed to find supply %s\n",
2635 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002636 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002637 goto scrub;
2638 }
2639
2640 ret = set_supply(rdev, r);
2641 if (ret < 0)
2642 goto scrub;
2643 }
2644
Liam Girdwooda5766f12008-10-10 13:22:20 +01002645 /* add consumers devices */
2646 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2647 ret = set_consumer_device_supply(rdev,
2648 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002649 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002650 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002651 if (ret < 0) {
2652 dev_err(dev, "Failed to set supply %s\n",
2653 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002654 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002655 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002656 }
2657
2658 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002659
2660 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002661out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002662 mutex_unlock(&regulator_list_mutex);
2663 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002664
Jani Nikulad4033b52010-04-29 10:55:11 +03002665unset_supplies:
2666 unset_regulator_supplies(rdev);
2667
David Brownell4fca9542008-11-11 17:38:53 -08002668scrub:
2669 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002670 /* device core frees rdev */
2671 rdev = ERR_PTR(ret);
2672 goto out;
2673
David Brownell4fca9542008-11-11 17:38:53 -08002674clean:
2675 kfree(rdev);
2676 rdev = ERR_PTR(ret);
2677 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002678}
2679EXPORT_SYMBOL_GPL(regulator_register);
2680
2681/**
2682 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002683 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002684 *
2685 * Called by regulator drivers to unregister a regulator.
2686 */
2687void regulator_unregister(struct regulator_dev *rdev)
2688{
2689 if (rdev == NULL)
2690 return;
2691
2692 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002693#ifdef CONFIG_DEBUG_FS
2694 debugfs_remove_recursive(rdev->debugfs);
2695#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002696 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002697 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002698 list_del(&rdev->list);
2699 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002700 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002701 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002702 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002703 mutex_unlock(&regulator_list_mutex);
2704}
2705EXPORT_SYMBOL_GPL(regulator_unregister);
2706
2707/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002708 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002709 * @state: system suspend state
2710 *
2711 * Configure each regulator with it's suspend operating parameters for state.
2712 * This will usually be called by machine suspend code prior to supending.
2713 */
2714int regulator_suspend_prepare(suspend_state_t state)
2715{
2716 struct regulator_dev *rdev;
2717 int ret = 0;
2718
2719 /* ON is handled by regulator active state */
2720 if (state == PM_SUSPEND_ON)
2721 return -EINVAL;
2722
2723 mutex_lock(&regulator_list_mutex);
2724 list_for_each_entry(rdev, &regulator_list, list) {
2725
2726 mutex_lock(&rdev->mutex);
2727 ret = suspend_prepare(rdev, state);
2728 mutex_unlock(&rdev->mutex);
2729
2730 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002731 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002732 goto out;
2733 }
2734 }
2735out:
2736 mutex_unlock(&regulator_list_mutex);
2737 return ret;
2738}
2739EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2740
2741/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002742 * regulator_suspend_finish - resume regulators from system wide suspend
2743 *
2744 * Turn on regulators that might be turned off by regulator_suspend_prepare
2745 * and that should be turned on according to the regulators properties.
2746 */
2747int regulator_suspend_finish(void)
2748{
2749 struct regulator_dev *rdev;
2750 int ret = 0, error;
2751
2752 mutex_lock(&regulator_list_mutex);
2753 list_for_each_entry(rdev, &regulator_list, list) {
2754 struct regulator_ops *ops = rdev->desc->ops;
2755
2756 mutex_lock(&rdev->mutex);
2757 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2758 ops->enable) {
2759 error = ops->enable(rdev);
2760 if (error)
2761 ret = error;
2762 } else {
2763 if (!has_full_constraints)
2764 goto unlock;
2765 if (!ops->disable)
2766 goto unlock;
2767 if (ops->is_enabled && !ops->is_enabled(rdev))
2768 goto unlock;
2769
2770 error = ops->disable(rdev);
2771 if (error)
2772 ret = error;
2773 }
2774unlock:
2775 mutex_unlock(&rdev->mutex);
2776 }
2777 mutex_unlock(&regulator_list_mutex);
2778 return ret;
2779}
2780EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2781
2782/**
Mark Brownca725562009-03-16 19:36:34 +00002783 * regulator_has_full_constraints - the system has fully specified constraints
2784 *
2785 * Calling this function will cause the regulator API to disable all
2786 * regulators which have a zero use count and don't have an always_on
2787 * constraint in a late_initcall.
2788 *
2789 * The intention is that this will become the default behaviour in a
2790 * future kernel release so users are encouraged to use this facility
2791 * now.
2792 */
2793void regulator_has_full_constraints(void)
2794{
2795 has_full_constraints = 1;
2796}
2797EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2798
2799/**
Mark Brown688fe992010-10-05 19:18:32 -07002800 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2801 *
2802 * Calling this function will cause the regulator API to provide a
2803 * dummy regulator to consumers if no physical regulator is found,
2804 * allowing most consumers to proceed as though a regulator were
2805 * configured. This allows systems such as those with software
2806 * controllable regulators for the CPU core only to be brought up more
2807 * readily.
2808 */
2809void regulator_use_dummy_regulator(void)
2810{
2811 board_wants_dummy_regulator = true;
2812}
2813EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2814
2815/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002817 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002818 *
2819 * Get rdev regulator driver private data. This call can be used in the
2820 * regulator driver context.
2821 */
2822void *rdev_get_drvdata(struct regulator_dev *rdev)
2823{
2824 return rdev->reg_data;
2825}
2826EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2827
2828/**
2829 * regulator_get_drvdata - get regulator driver data
2830 * @regulator: regulator
2831 *
2832 * Get regulator driver private data. This call can be used in the consumer
2833 * driver context when non API regulator specific functions need to be called.
2834 */
2835void *regulator_get_drvdata(struct regulator *regulator)
2836{
2837 return regulator->rdev->reg_data;
2838}
2839EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2840
2841/**
2842 * regulator_set_drvdata - set regulator driver data
2843 * @regulator: regulator
2844 * @data: data
2845 */
2846void regulator_set_drvdata(struct regulator *regulator, void *data)
2847{
2848 regulator->rdev->reg_data = data;
2849}
2850EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2851
2852/**
2853 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002854 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002855 */
2856int rdev_get_id(struct regulator_dev *rdev)
2857{
2858 return rdev->desc->id;
2859}
2860EXPORT_SYMBOL_GPL(rdev_get_id);
2861
Liam Girdwooda5766f12008-10-10 13:22:20 +01002862struct device *rdev_get_dev(struct regulator_dev *rdev)
2863{
2864 return &rdev->dev;
2865}
2866EXPORT_SYMBOL_GPL(rdev_get_dev);
2867
2868void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2869{
2870 return reg_init_data->driver_data;
2871}
2872EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2873
Liam Girdwood414c70c2008-04-30 15:59:04 +01002874static int __init regulator_init(void)
2875{
Mark Brown34abbd62010-02-12 10:18:08 +00002876 int ret;
2877
Mark Brown34abbd62010-02-12 10:18:08 +00002878 ret = class_register(&regulator_class);
2879
Mark Brown1130e5b2010-12-21 23:49:31 +00002880#ifdef CONFIG_DEBUG_FS
2881 debugfs_root = debugfs_create_dir("regulator", NULL);
2882 if (IS_ERR(debugfs_root) || !debugfs_root) {
2883 pr_warn("regulator: Failed to create debugfs directory\n");
2884 debugfs_root = NULL;
2885 }
2886#endif
2887
Mark Brown34abbd62010-02-12 10:18:08 +00002888 regulator_dummy_init();
2889
2890 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002891}
2892
2893/* init early to allow our consumers to complete system booting */
2894core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002895
2896static int __init regulator_init_complete(void)
2897{
2898 struct regulator_dev *rdev;
2899 struct regulator_ops *ops;
2900 struct regulation_constraints *c;
2901 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002902
2903 mutex_lock(&regulator_list_mutex);
2904
2905 /* If we have a full configuration then disable any regulators
2906 * which are not in use or always_on. This will become the
2907 * default behaviour in the future.
2908 */
2909 list_for_each_entry(rdev, &regulator_list, list) {
2910 ops = rdev->desc->ops;
2911 c = rdev->constraints;
2912
Mark Brownf25e0b42009-08-03 18:49:55 +01002913 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002914 continue;
2915
2916 mutex_lock(&rdev->mutex);
2917
2918 if (rdev->use_count)
2919 goto unlock;
2920
2921 /* If we can't read the status assume it's on. */
2922 if (ops->is_enabled)
2923 enabled = ops->is_enabled(rdev);
2924 else
2925 enabled = 1;
2926
2927 if (!enabled)
2928 goto unlock;
2929
2930 if (has_full_constraints) {
2931 /* We log since this may kill the system if it
2932 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002933 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002934 ret = ops->disable(rdev);
2935 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002936 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002937 }
2938 } else {
2939 /* The intention is that in future we will
2940 * assume that full constraints are provided
2941 * so warn even if we aren't going to do
2942 * anything here.
2943 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002944 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002945 }
2946
2947unlock:
2948 mutex_unlock(&rdev->mutex);
2949 }
2950
2951 mutex_unlock(&regulator_list_mutex);
2952
2953 return 0;
2954}
2955late_initcall(regulator_init_complete);