blob: f0cc3983ffeeb738a30d516dc22f96bc892c68b9 [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
Joe Perches5da84fd2010-11-30 05:53:48 -080037#define rdev_err(rdev, fmt, ...) \
38 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39#define rdev_warn(rdev, fmt, ...) \
40 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_info(rdev, fmt, ...) \
42 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_dbg(rdev, fmt, ...) \
44 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45
Liam Girdwood414c70c2008-04-30 15:59:04 +010046static DEFINE_MUTEX(regulator_list_mutex);
47static LIST_HEAD(regulator_list);
48static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000049static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070050static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010051
Mark Brown1130e5b2010-12-21 23:49:31 +000052#ifdef CONFIG_DEBUG_FS
53static struct dentry *debugfs_root;
54#endif
55
Mark Brown8dc53902008-12-31 12:52:40 +000056/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010057 * struct regulator_map
58 *
59 * Used to provide symbolic supply names to devices.
60 */
61struct regulator_map {
62 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010063 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010065 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010066};
67
Liam Girdwood414c70c2008-04-30 15:59:04 +010068/*
69 * struct regulator
70 *
71 * One for each consumer device.
72 */
73struct regulator {
74 struct device *dev;
75 struct list_head list;
76 int uA_load;
77 int min_uV;
78 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010079 char *supply_name;
80 struct device_attribute dev_attr;
81 struct regulator_dev *rdev;
82};
83
84static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010085static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010086static int _regulator_get_voltage(struct regulator_dev *rdev);
87static int _regulator_get_current_limit(struct regulator_dev *rdev);
88static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89static void _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000091static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010093static struct regulator *create_regulator(struct regulator_dev *rdev,
94 struct device *dev,
95 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +010096
Mark Brown1083c392009-10-22 16:31:32 +010097static const char *rdev_get_name(struct regulator_dev *rdev)
98{
99 if (rdev->constraints && rdev->constraints->name)
100 return rdev->constraints->name;
101 else if (rdev->desc->name)
102 return rdev->desc->name;
103 else
104 return "";
105}
106
Liam Girdwood414c70c2008-04-30 15:59:04 +0100107/* gets the regulator for a given consumer device */
108static struct regulator *get_device_regulator(struct device *dev)
109{
110 struct regulator *regulator = NULL;
111 struct regulator_dev *rdev;
112
113 mutex_lock(&regulator_list_mutex);
114 list_for_each_entry(rdev, &regulator_list, list) {
115 mutex_lock(&rdev->mutex);
116 list_for_each_entry(regulator, &rdev->consumer_list, list) {
117 if (regulator->dev == dev) {
118 mutex_unlock(&rdev->mutex);
119 mutex_unlock(&regulator_list_mutex);
120 return regulator;
121 }
122 }
123 mutex_unlock(&rdev->mutex);
124 }
125 mutex_unlock(&regulator_list_mutex);
126 return NULL;
127}
128
129/* Platform voltage constraint check */
130static int regulator_check_voltage(struct regulator_dev *rdev,
131 int *min_uV, int *max_uV)
132{
133 BUG_ON(*min_uV > *max_uV);
134
135 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800136 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100137 return -ENODEV;
138 }
139 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800140 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100141 return -EPERM;
142 }
143
144 if (*max_uV > rdev->constraints->max_uV)
145 *max_uV = rdev->constraints->max_uV;
146 if (*min_uV < rdev->constraints->min_uV)
147 *min_uV = rdev->constraints->min_uV;
148
149 if (*min_uV > *max_uV)
150 return -EINVAL;
151
152 return 0;
153}
154
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100155/* Make sure we select a voltage that suits the needs of all
156 * regulator consumers
157 */
158static int regulator_check_consumers(struct regulator_dev *rdev,
159 int *min_uV, int *max_uV)
160{
161 struct regulator *regulator;
162
163 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700164 /*
165 * Assume consumers that didn't say anything are OK
166 * with anything in the constraint range.
167 */
168 if (!regulator->min_uV && !regulator->max_uV)
169 continue;
170
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100171 if (*max_uV > regulator->max_uV)
172 *max_uV = regulator->max_uV;
173 if (*min_uV < regulator->min_uV)
174 *min_uV = regulator->min_uV;
175 }
176
177 if (*min_uV > *max_uV)
178 return -EINVAL;
179
180 return 0;
181}
182
Liam Girdwood414c70c2008-04-30 15:59:04 +0100183/* current constraint check */
184static int regulator_check_current_limit(struct regulator_dev *rdev,
185 int *min_uA, int *max_uA)
186{
187 BUG_ON(*min_uA > *max_uA);
188
189 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800190 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100191 return -ENODEV;
192 }
193 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800194 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100195 return -EPERM;
196 }
197
198 if (*max_uA > rdev->constraints->max_uA)
199 *max_uA = rdev->constraints->max_uA;
200 if (*min_uA < rdev->constraints->min_uA)
201 *min_uA = rdev->constraints->min_uA;
202
203 if (*min_uA > *max_uA)
204 return -EINVAL;
205
206 return 0;
207}
208
209/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900210static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100211{
Mark Brown2c608232011-03-30 06:29:12 +0900212 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800213 case REGULATOR_MODE_FAST:
214 case REGULATOR_MODE_NORMAL:
215 case REGULATOR_MODE_IDLE:
216 case REGULATOR_MODE_STANDBY:
217 break;
218 default:
219 return -EINVAL;
220 }
221
Liam Girdwood414c70c2008-04-30 15:59:04 +0100222 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800223 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224 return -ENODEV;
225 }
226 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800227 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228 return -EPERM;
229 }
Mark Brown2c608232011-03-30 06:29:12 +0900230
231 /* The modes are bitmasks, the most power hungry modes having
232 * the lowest values. If the requested mode isn't supported
233 * try higher modes. */
234 while (*mode) {
235 if (rdev->constraints->valid_modes_mask & *mode)
236 return 0;
237 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238 }
Mark Brown2c608232011-03-30 06:29:12 +0900239
240 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100241}
242
243/* dynamic regulator mode switching constraint check */
244static int regulator_check_drms(struct regulator_dev *rdev)
245{
246 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800247 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100248 return -ENODEV;
249 }
250 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800251 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100252 return -EPERM;
253 }
254 return 0;
255}
256
257static ssize_t device_requested_uA_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
259{
260 struct regulator *regulator;
261
262 regulator = get_device_regulator(dev);
263 if (regulator == NULL)
264 return 0;
265
266 return sprintf(buf, "%d\n", regulator->uA_load);
267}
268
269static ssize_t regulator_uV_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100272 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273 ssize_t ret;
274
275 mutex_lock(&rdev->mutex);
276 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
277 mutex_unlock(&rdev->mutex);
278
279 return ret;
280}
David Brownell7ad68e22008-11-11 17:39:02 -0800281static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282
283static ssize_t regulator_uA_show(struct device *dev,
284 struct device_attribute *attr, char *buf)
285{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100286 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100287
288 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
289}
David Brownell7ad68e22008-11-11 17:39:02 -0800290static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291
Mark Brownbc558a62008-10-10 15:33:20 +0100292static ssize_t regulator_name_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100296
Mark Brown1083c392009-10-22 16:31:32 +0100297 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100298}
299
David Brownell4fca9542008-11-11 17:38:53 -0800300static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100301{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100302 switch (mode) {
303 case REGULATOR_MODE_FAST:
304 return sprintf(buf, "fast\n");
305 case REGULATOR_MODE_NORMAL:
306 return sprintf(buf, "normal\n");
307 case REGULATOR_MODE_IDLE:
308 return sprintf(buf, "idle\n");
309 case REGULATOR_MODE_STANDBY:
310 return sprintf(buf, "standby\n");
311 }
312 return sprintf(buf, "unknown\n");
313}
314
David Brownell4fca9542008-11-11 17:38:53 -0800315static ssize_t regulator_opmode_show(struct device *dev,
316 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100317{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100318 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100319
David Brownell4fca9542008-11-11 17:38:53 -0800320 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
321}
David Brownell7ad68e22008-11-11 17:39:02 -0800322static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800323
324static ssize_t regulator_print_state(char *buf, int state)
325{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100326 if (state > 0)
327 return sprintf(buf, "enabled\n");
328 else if (state == 0)
329 return sprintf(buf, "disabled\n");
330 else
331 return sprintf(buf, "unknown\n");
332}
333
David Brownell4fca9542008-11-11 17:38:53 -0800334static ssize_t regulator_state_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100338 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800339
Mark Brown93325462009-08-03 18:49:56 +0100340 mutex_lock(&rdev->mutex);
341 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
342 mutex_unlock(&rdev->mutex);
343
344 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800345}
David Brownell7ad68e22008-11-11 17:39:02 -0800346static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800347
David Brownell853116a2009-01-14 23:03:17 -0800348static ssize_t regulator_status_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
351 struct regulator_dev *rdev = dev_get_drvdata(dev);
352 int status;
353 char *label;
354
355 status = rdev->desc->ops->get_status(rdev);
356 if (status < 0)
357 return status;
358
359 switch (status) {
360 case REGULATOR_STATUS_OFF:
361 label = "off";
362 break;
363 case REGULATOR_STATUS_ON:
364 label = "on";
365 break;
366 case REGULATOR_STATUS_ERROR:
367 label = "error";
368 break;
369 case REGULATOR_STATUS_FAST:
370 label = "fast";
371 break;
372 case REGULATOR_STATUS_NORMAL:
373 label = "normal";
374 break;
375 case REGULATOR_STATUS_IDLE:
376 label = "idle";
377 break;
378 case REGULATOR_STATUS_STANDBY:
379 label = "standby";
380 break;
381 default:
382 return -ERANGE;
383 }
384
385 return sprintf(buf, "%s\n", label);
386}
387static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
388
Liam Girdwood414c70c2008-04-30 15:59:04 +0100389static ssize_t regulator_min_uA_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
391{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100392 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100393
394 if (!rdev->constraints)
395 return sprintf(buf, "constraint not defined\n");
396
397 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
398}
David Brownell7ad68e22008-11-11 17:39:02 -0800399static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100400
401static ssize_t regulator_max_uA_show(struct device *dev,
402 struct device_attribute *attr, char *buf)
403{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100404 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100405
406 if (!rdev->constraints)
407 return sprintf(buf, "constraint not defined\n");
408
409 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
410}
David Brownell7ad68e22008-11-11 17:39:02 -0800411static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412
413static ssize_t regulator_min_uV_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100416 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100417
418 if (!rdev->constraints)
419 return sprintf(buf, "constraint not defined\n");
420
421 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
422}
David Brownell7ad68e22008-11-11 17:39:02 -0800423static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100424
425static ssize_t regulator_max_uV_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100428 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430 if (!rdev->constraints)
431 return sprintf(buf, "constraint not defined\n");
432
433 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
434}
David Brownell7ad68e22008-11-11 17:39:02 -0800435static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436
437static ssize_t regulator_total_uA_show(struct device *dev,
438 struct device_attribute *attr, char *buf)
439{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100440 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441 struct regulator *regulator;
442 int uA = 0;
443
444 mutex_lock(&rdev->mutex);
445 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100446 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447 mutex_unlock(&rdev->mutex);
448 return sprintf(buf, "%d\n", uA);
449}
David Brownell7ad68e22008-11-11 17:39:02 -0800450static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100451
452static ssize_t regulator_num_users_show(struct device *dev,
453 struct device_attribute *attr, char *buf)
454{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100455 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100456 return sprintf(buf, "%d\n", rdev->use_count);
457}
458
459static ssize_t regulator_type_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100462 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
464 switch (rdev->desc->type) {
465 case REGULATOR_VOLTAGE:
466 return sprintf(buf, "voltage\n");
467 case REGULATOR_CURRENT:
468 return sprintf(buf, "current\n");
469 }
470 return sprintf(buf, "unknown\n");
471}
472
473static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
479}
David Brownell7ad68e22008-11-11 17:39:02 -0800480static DEVICE_ATTR(suspend_mem_microvolts, 0444,
481 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
489}
David Brownell7ad68e22008-11-11 17:39:02 -0800490static DEVICE_ATTR(suspend_disk_microvolts, 0444,
491 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
493static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100496 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(suspend_standby_microvolts, 0444,
501 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100506 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
David Brownell4fca9542008-11-11 17:38:53 -0800508 return regulator_print_opmode(buf,
509 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_mem_mode, 0444,
512 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100517 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
David Brownell4fca9542008-11-11 17:38:53 -0800519 return regulator_print_opmode(buf,
520 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_disk_mode, 0444,
523 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524
525static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
526 struct device_attribute *attr, char *buf)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
David Brownell4fca9542008-11-11 17:38:53 -0800530 return regulator_print_opmode(buf,
531 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532}
David Brownell7ad68e22008-11-11 17:39:02 -0800533static DEVICE_ATTR(suspend_standby_mode, 0444,
534 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100535
536static ssize_t regulator_suspend_mem_state_show(struct device *dev,
537 struct device_attribute *attr, char *buf)
538{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100539 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
David Brownell4fca9542008-11-11 17:38:53 -0800541 return regulator_print_state(buf,
542 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543}
David Brownell7ad68e22008-11-11 17:39:02 -0800544static DEVICE_ATTR(suspend_mem_state, 0444,
545 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100546
547static ssize_t regulator_suspend_disk_state_show(struct device *dev,
548 struct device_attribute *attr, char *buf)
549{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100550 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
David Brownell4fca9542008-11-11 17:38:53 -0800552 return regulator_print_state(buf,
553 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554}
David Brownell7ad68e22008-11-11 17:39:02 -0800555static DEVICE_ATTR(suspend_disk_state, 0444,
556 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100557
558static ssize_t regulator_suspend_standby_state_show(struct device *dev,
559 struct device_attribute *attr, char *buf)
560{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100561 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
David Brownell4fca9542008-11-11 17:38:53 -0800563 return regulator_print_state(buf,
564 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100565}
David Brownell7ad68e22008-11-11 17:39:02 -0800566static DEVICE_ATTR(suspend_standby_state, 0444,
567 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100568
David Brownell7ad68e22008-11-11 17:39:02 -0800569
570/*
571 * These are the only attributes are present for all regulators.
572 * Other attributes are a function of regulator functionality.
573 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100575 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100576 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
577 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578 __ATTR_NULL,
579};
580
581static void regulator_dev_release(struct device *dev)
582{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100583 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100584 kfree(rdev);
585}
586
587static struct class regulator_class = {
588 .name = "regulator",
589 .dev_release = regulator_dev_release,
590 .dev_attrs = regulator_dev_attrs,
591};
592
593/* Calculate the new optimum regulator operating mode based on the new total
594 * consumer load. All locks held by caller */
595static void drms_uA_update(struct regulator_dev *rdev)
596{
597 struct regulator *sibling;
598 int current_uA = 0, output_uV, input_uV, err;
599 unsigned int mode;
600
601 err = regulator_check_drms(rdev);
602 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000603 (!rdev->desc->ops->get_voltage &&
604 !rdev->desc->ops->get_voltage_sel) ||
605 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300606 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607
608 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000609 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100610 if (output_uV <= 0)
611 return;
612
613 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000614 input_uV = 0;
615 if (rdev->supply)
616 input_uV = _regulator_get_voltage(rdev);
617 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618 input_uV = rdev->constraints->input_uV;
619 if (input_uV <= 0)
620 return;
621
622 /* calc total requested load */
623 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100624 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100625
626 /* now get the optimum mode for our new total regulator load */
627 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
628 output_uV, current_uA);
629
630 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900631 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100632 if (err == 0)
633 rdev->desc->ops->set_mode(rdev, mode);
634}
635
636static int suspend_set_state(struct regulator_dev *rdev,
637 struct regulator_state *rstate)
638{
639 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100640 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100641
Mark Brown638f85c2009-10-22 16:31:33 +0100642 can_set_state = rdev->desc->ops->set_suspend_enable &&
643 rdev->desc->ops->set_suspend_disable;
644
645 /* If we have no suspend mode configration don't set anything;
646 * only warn if the driver actually makes the suspend mode
647 * configurable.
648 */
649 if (!rstate->enabled && !rstate->disabled) {
650 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800651 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100652 return 0;
653 }
654
655 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800656 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100657 return -EINVAL;
658 }
659
660 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800661 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100662 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100663 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100664
665 if (rstate->enabled)
666 ret = rdev->desc->ops->set_suspend_enable(rdev);
667 else
668 ret = rdev->desc->ops->set_suspend_disable(rdev);
669 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800670 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100671 return ret;
672 }
673
674 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
675 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
676 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800677 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678 return ret;
679 }
680 }
681
682 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
683 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
684 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800685 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100686 return ret;
687 }
688 }
689 return ret;
690}
691
692/* locks held by caller */
693static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
694{
695 if (!rdev->constraints)
696 return -EINVAL;
697
698 switch (state) {
699 case PM_SUSPEND_STANDBY:
700 return suspend_set_state(rdev,
701 &rdev->constraints->state_standby);
702 case PM_SUSPEND_MEM:
703 return suspend_set_state(rdev,
704 &rdev->constraints->state_mem);
705 case PM_SUSPEND_MAX:
706 return suspend_set_state(rdev,
707 &rdev->constraints->state_disk);
708 default:
709 return -EINVAL;
710 }
711}
712
713static void print_constraints(struct regulator_dev *rdev)
714{
715 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000716 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100717 int count = 0;
718 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100719
Mark Brown8f031b42009-10-22 16:31:31 +0100720 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100722 count += sprintf(buf + count, "%d mV ",
723 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100724 else
Mark Brown8f031b42009-10-22 16:31:31 +0100725 count += sprintf(buf + count, "%d <--> %d mV ",
726 constraints->min_uV / 1000,
727 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 }
Mark Brown8f031b42009-10-22 16:31:31 +0100729
730 if (!constraints->min_uV ||
731 constraints->min_uV != constraints->max_uV) {
732 ret = _regulator_get_voltage(rdev);
733 if (ret > 0)
734 count += sprintf(buf + count, "at %d mV ", ret / 1000);
735 }
736
Mark Brownbf5892a2011-05-08 22:13:37 +0100737 if (constraints->uV_offset)
738 count += sprintf(buf, "%dmV offset ",
739 constraints->uV_offset / 1000);
740
Mark Brown8f031b42009-10-22 16:31:31 +0100741 if (constraints->min_uA && constraints->max_uA) {
742 if (constraints->min_uA == constraints->max_uA)
743 count += sprintf(buf + count, "%d mA ",
744 constraints->min_uA / 1000);
745 else
746 count += sprintf(buf + count, "%d <--> %d mA ",
747 constraints->min_uA / 1000,
748 constraints->max_uA / 1000);
749 }
750
751 if (!constraints->min_uA ||
752 constraints->min_uA != constraints->max_uA) {
753 ret = _regulator_get_current_limit(rdev);
754 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400755 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100756 }
757
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
759 count += sprintf(buf + count, "fast ");
760 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
761 count += sprintf(buf + count, "normal ");
762 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
763 count += sprintf(buf + count, "idle ");
764 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
765 count += sprintf(buf + count, "standby");
766
Mark Brown13ce29f2010-12-17 16:04:12 +0000767 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100768}
769
Mark Browne79055d2009-10-19 15:53:50 +0100770static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100771 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100772{
773 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100774 int ret;
775
776 /* do we need to apply the constraint voltage */
777 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000778 rdev->constraints->min_uV == rdev->constraints->max_uV) {
779 ret = _regulator_do_set_voltage(rdev,
780 rdev->constraints->min_uV,
781 rdev->constraints->max_uV);
782 if (ret < 0) {
783 rdev_err(rdev, "failed to apply %duV constraint\n",
784 rdev->constraints->min_uV);
785 rdev->constraints = NULL;
786 return ret;
787 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100788 }
Mark Browne79055d2009-10-19 15:53:50 +0100789
790 /* constrain machine-level voltage specs to fit
791 * the actual range supported by this regulator.
792 */
793 if (ops->list_voltage && rdev->desc->n_voltages) {
794 int count = rdev->desc->n_voltages;
795 int i;
796 int min_uV = INT_MAX;
797 int max_uV = INT_MIN;
798 int cmin = constraints->min_uV;
799 int cmax = constraints->max_uV;
800
801 /* it's safe to autoconfigure fixed-voltage supplies
802 and the constraints are used by list_voltage. */
803 if (count == 1 && !cmin) {
804 cmin = 1;
805 cmax = INT_MAX;
806 constraints->min_uV = cmin;
807 constraints->max_uV = cmax;
808 }
809
810 /* voltage constraints are optional */
811 if ((cmin == 0) && (cmax == 0))
812 return 0;
813
814 /* else require explicit machine-level constraints */
815 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800816 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100817 return -EINVAL;
818 }
819
820 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
821 for (i = 0; i < count; i++) {
822 int value;
823
824 value = ops->list_voltage(rdev, i);
825 if (value <= 0)
826 continue;
827
828 /* maybe adjust [min_uV..max_uV] */
829 if (value >= cmin && value < min_uV)
830 min_uV = value;
831 if (value <= cmax && value > max_uV)
832 max_uV = value;
833 }
834
835 /* final: [min_uV..max_uV] valid iff constraints valid */
836 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800837 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100838 return -EINVAL;
839 }
840
841 /* use regulator's subset of machine constraints */
842 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800843 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
844 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100845 constraints->min_uV = min_uV;
846 }
847 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800848 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
849 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100850 constraints->max_uV = max_uV;
851 }
852 }
853
854 return 0;
855}
856
Liam Girdwooda5766f12008-10-10 13:22:20 +0100857/**
858 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000859 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000860 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100861 *
862 * Allows platform initialisation code to define and constrain
863 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
864 * Constraints *must* be set by platform code in order for some
865 * regulator operations to proceed i.e. set_voltage, set_current_limit,
866 * set_mode.
867 */
868static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000869 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100870{
871 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100872 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100873
Mark Brownf8c12fe2010-11-29 15:55:17 +0000874 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
875 GFP_KERNEL);
876 if (!rdev->constraints)
877 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100878
Mark Brownf8c12fe2010-11-29 15:55:17 +0000879 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100880 if (ret != 0)
881 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800882
Liam Girdwooda5766f12008-10-10 13:22:20 +0100883 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100884 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000885 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100886 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800887 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100888 rdev->constraints = NULL;
889 goto out;
890 }
891 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100892
Mark Browna3084662009-02-26 19:24:19 +0000893 if (constraints->initial_mode) {
894 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800895 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000896 ret = -EINVAL;
897 goto out;
898 }
899
Mark Brownf8c12fe2010-11-29 15:55:17 +0000900 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000901 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800902 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000903 goto out;
904 }
905 }
906
Mark Browncacf90f2009-03-02 16:32:46 +0000907 /* If the constraints say the regulator should be on at this point
908 * and we have control then make sure it is enabled.
909 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000910 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
911 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100912 ret = ops->enable(rdev);
913 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800914 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100915 rdev->constraints = NULL;
916 goto out;
917 }
918 }
919
Liam Girdwooda5766f12008-10-10 13:22:20 +0100920 print_constraints(rdev);
921out:
922 return ret;
923}
924
925/**
926 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000927 * @rdev: regulator name
928 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100929 *
930 * Called by platform initialisation code to set the supply regulator for this
931 * regulator. This ensures that a regulators supply will also be enabled by the
932 * core if it's child is enabled.
933 */
934static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100935 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936{
937 int err;
938
Mark Brown3801b862011-06-09 16:22:22 +0100939 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
940
941 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
942 if (IS_ERR(rdev->supply)) {
943 err = PTR_ERR(rdev->supply);
944 rdev->supply = NULL;
945 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100946 }
Mark Brown3801b862011-06-09 16:22:22 +0100947
948 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100949}
950
951/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800952 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000953 * @rdev: regulator source
954 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100955 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000956 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100957 *
958 * Allows platform initialisation code to map physical regulator
959 * sources to symbolic names for supplies for use by devices. Devices
960 * should use these symbolic names to request regulators, avoiding the
961 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100962 *
963 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100964 */
965static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100966 struct device *consumer_dev, const char *consumer_dev_name,
967 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100968{
969 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100970 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100971
Mark Brown40f92442009-06-17 17:56:39 +0100972 if (consumer_dev && consumer_dev_name)
973 return -EINVAL;
974
975 if (!consumer_dev_name && consumer_dev)
976 consumer_dev_name = dev_name(consumer_dev);
977
Liam Girdwooda5766f12008-10-10 13:22:20 +0100978 if (supply == NULL)
979 return -EINVAL;
980
Mark Brown9ed20992009-07-21 16:00:26 +0100981 if (consumer_dev_name != NULL)
982 has_dev = 1;
983 else
984 has_dev = 0;
985
David Brownell6001e132008-12-31 12:54:19 +0000986 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300987 if (node->dev_name && consumer_dev_name) {
988 if (strcmp(node->dev_name, consumer_dev_name) != 0)
989 continue;
990 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000991 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300992 }
993
David Brownell6001e132008-12-31 12:54:19 +0000994 if (strcmp(node->supply, supply) != 0)
995 continue;
996
997 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800998 dev_name(&node->regulator->dev),
999 node->regulator->desc->name,
1000 supply,
1001 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001002 return -EBUSY;
1003 }
1004
Mark Brown9ed20992009-07-21 16:00:26 +01001005 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001006 if (node == NULL)
1007 return -ENOMEM;
1008
1009 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001010 node->supply = supply;
1011
Mark Brown9ed20992009-07-21 16:00:26 +01001012 if (has_dev) {
1013 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1014 if (node->dev_name == NULL) {
1015 kfree(node);
1016 return -ENOMEM;
1017 }
Mark Brown40f92442009-06-17 17:56:39 +01001018 }
1019
Liam Girdwooda5766f12008-10-10 13:22:20 +01001020 list_add(&node->list, &regulator_map_list);
1021 return 0;
1022}
1023
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001024static void unset_regulator_supplies(struct regulator_dev *rdev)
1025{
1026 struct regulator_map *node, *n;
1027
1028 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1029 if (rdev == node->regulator) {
1030 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001031 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001032 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001033 }
1034 }
1035}
1036
Mark Brownf5726ae2011-06-09 16:22:20 +01001037#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001038
1039static struct regulator *create_regulator(struct regulator_dev *rdev,
1040 struct device *dev,
1041 const char *supply_name)
1042{
1043 struct regulator *regulator;
1044 char buf[REG_STR_SIZE];
1045 int err, size;
1046
1047 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1048 if (regulator == NULL)
1049 return NULL;
1050
1051 mutex_lock(&rdev->mutex);
1052 regulator->rdev = rdev;
1053 list_add(&regulator->list, &rdev->consumer_list);
1054
1055 if (dev) {
1056 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001057 size = scnprintf(buf, REG_STR_SIZE,
1058 "microamps_requested_%s-%s",
1059 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001060 if (size >= REG_STR_SIZE)
1061 goto overflow_err;
1062
1063 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001064 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001065 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1066 if (regulator->dev_attr.attr.name == NULL)
1067 goto attr_name_err;
1068
Liam Girdwood414c70c2008-04-30 15:59:04 +01001069 regulator->dev_attr.attr.mode = 0444;
1070 regulator->dev_attr.show = device_requested_uA_show;
1071 err = device_create_file(dev, &regulator->dev_attr);
1072 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001073 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001074 goto attr_name_err;
1075 }
1076
1077 /* also add a link to the device sysfs entry */
1078 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1079 dev->kobj.name, supply_name);
1080 if (size >= REG_STR_SIZE)
1081 goto attr_err;
1082
1083 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1084 if (regulator->supply_name == NULL)
1085 goto attr_err;
1086
1087 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1088 buf);
1089 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001090 rdev_warn(rdev, "could not add device link %s err %d\n",
1091 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001092 goto link_name_err;
1093 }
1094 }
1095 mutex_unlock(&rdev->mutex);
1096 return regulator;
1097link_name_err:
1098 kfree(regulator->supply_name);
1099attr_err:
1100 device_remove_file(regulator->dev, &regulator->dev_attr);
1101attr_name_err:
1102 kfree(regulator->dev_attr.attr.name);
1103overflow_err:
1104 list_del(&regulator->list);
1105 kfree(regulator);
1106 mutex_unlock(&rdev->mutex);
1107 return NULL;
1108}
1109
Mark Brown31aae2b2009-12-21 12:21:52 +00001110static int _regulator_get_enable_time(struct regulator_dev *rdev)
1111{
1112 if (!rdev->desc->ops->enable_time)
1113 return 0;
1114 return rdev->desc->ops->enable_time(rdev);
1115}
1116
Mark Brown5ffbd132009-07-21 16:00:23 +01001117/* Internal regulator request function */
1118static struct regulator *_regulator_get(struct device *dev, const char *id,
1119 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001120{
1121 struct regulator_dev *rdev;
1122 struct regulator_map *map;
1123 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001124 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001125 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001126
1127 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001128 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129 return regulator;
1130 }
1131
Mark Brown40f92442009-06-17 17:56:39 +01001132 if (dev)
1133 devname = dev_name(dev);
1134
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 mutex_lock(&regulator_list_mutex);
1136
1137 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001138 /* If the mapping has a device set up it must match */
1139 if (map->dev_name &&
1140 (!devname || strcmp(map->dev_name, devname)))
1141 continue;
1142
1143 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001144 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001145 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001146 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001147 }
Mark Brown34abbd62010-02-12 10:18:08 +00001148
Mark Brown688fe992010-10-05 19:18:32 -07001149 if (board_wants_dummy_regulator) {
1150 rdev = dummy_regulator_rdev;
1151 goto found;
1152 }
1153
Mark Brown34abbd62010-02-12 10:18:08 +00001154#ifdef CONFIG_REGULATOR_DUMMY
1155 if (!devname)
1156 devname = "deviceless";
1157
1158 /* If the board didn't flag that it was fully constrained then
1159 * substitute in a dummy regulator so consumers can continue.
1160 */
1161 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001162 pr_warn("%s supply %s not found, using dummy regulator\n",
1163 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001164 rdev = dummy_regulator_rdev;
1165 goto found;
1166 }
1167#endif
1168
Liam Girdwood414c70c2008-04-30 15:59:04 +01001169 mutex_unlock(&regulator_list_mutex);
1170 return regulator;
1171
1172found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001173 if (rdev->exclusive) {
1174 regulator = ERR_PTR(-EPERM);
1175 goto out;
1176 }
1177
1178 if (exclusive && rdev->open_count) {
1179 regulator = ERR_PTR(-EBUSY);
1180 goto out;
1181 }
1182
Liam Girdwooda5766f12008-10-10 13:22:20 +01001183 if (!try_module_get(rdev->owner))
1184 goto out;
1185
Liam Girdwood414c70c2008-04-30 15:59:04 +01001186 regulator = create_regulator(rdev, dev, id);
1187 if (regulator == NULL) {
1188 regulator = ERR_PTR(-ENOMEM);
1189 module_put(rdev->owner);
1190 }
1191
Mark Brown5ffbd132009-07-21 16:00:23 +01001192 rdev->open_count++;
1193 if (exclusive) {
1194 rdev->exclusive = 1;
1195
1196 ret = _regulator_is_enabled(rdev);
1197 if (ret > 0)
1198 rdev->use_count = 1;
1199 else
1200 rdev->use_count = 0;
1201 }
1202
Liam Girdwooda5766f12008-10-10 13:22:20 +01001203out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001204 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001205
Liam Girdwood414c70c2008-04-30 15:59:04 +01001206 return regulator;
1207}
Mark Brown5ffbd132009-07-21 16:00:23 +01001208
1209/**
1210 * regulator_get - lookup and obtain a reference to a regulator.
1211 * @dev: device for regulator "consumer"
1212 * @id: Supply name or regulator ID.
1213 *
1214 * Returns a struct regulator corresponding to the regulator producer,
1215 * or IS_ERR() condition containing errno.
1216 *
1217 * Use of supply names configured via regulator_set_device_supply() is
1218 * strongly encouraged. It is recommended that the supply name used
1219 * should match the name used for the supply and/or the relevant
1220 * device pins in the datasheet.
1221 */
1222struct regulator *regulator_get(struct device *dev, const char *id)
1223{
1224 return _regulator_get(dev, id, 0);
1225}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001226EXPORT_SYMBOL_GPL(regulator_get);
1227
1228/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001229 * regulator_get_exclusive - obtain exclusive access to a regulator.
1230 * @dev: device for regulator "consumer"
1231 * @id: Supply name or regulator ID.
1232 *
1233 * Returns a struct regulator corresponding to the regulator producer,
1234 * or IS_ERR() condition containing errno. Other consumers will be
1235 * unable to obtain this reference is held and the use count for the
1236 * regulator will be initialised to reflect the current state of the
1237 * regulator.
1238 *
1239 * This is intended for use by consumers which cannot tolerate shared
1240 * use of the regulator such as those which need to force the
1241 * regulator off for correct operation of the hardware they are
1242 * controlling.
1243 *
1244 * Use of supply names configured via regulator_set_device_supply() is
1245 * strongly encouraged. It is recommended that the supply name used
1246 * should match the name used for the supply and/or the relevant
1247 * device pins in the datasheet.
1248 */
1249struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1250{
1251 return _regulator_get(dev, id, 1);
1252}
1253EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1254
1255/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001256 * regulator_put - "free" the regulator source
1257 * @regulator: regulator source
1258 *
1259 * Note: drivers must ensure that all regulator_enable calls made on this
1260 * regulator source are balanced by regulator_disable calls prior to calling
1261 * this function.
1262 */
1263void regulator_put(struct regulator *regulator)
1264{
1265 struct regulator_dev *rdev;
1266
1267 if (regulator == NULL || IS_ERR(regulator))
1268 return;
1269
Liam Girdwood414c70c2008-04-30 15:59:04 +01001270 mutex_lock(&regulator_list_mutex);
1271 rdev = regulator->rdev;
1272
1273 /* remove any sysfs entries */
1274 if (regulator->dev) {
1275 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1276 kfree(regulator->supply_name);
1277 device_remove_file(regulator->dev, &regulator->dev_attr);
1278 kfree(regulator->dev_attr.attr.name);
1279 }
1280 list_del(&regulator->list);
1281 kfree(regulator);
1282
Mark Brown5ffbd132009-07-21 16:00:23 +01001283 rdev->open_count--;
1284 rdev->exclusive = 0;
1285
Liam Girdwood414c70c2008-04-30 15:59:04 +01001286 module_put(rdev->owner);
1287 mutex_unlock(&regulator_list_mutex);
1288}
1289EXPORT_SYMBOL_GPL(regulator_put);
1290
Mark Brown9a2372f2009-08-03 18:49:57 +01001291static int _regulator_can_change_status(struct regulator_dev *rdev)
1292{
1293 if (!rdev->constraints)
1294 return 0;
1295
1296 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1297 return 1;
1298 else
1299 return 0;
1300}
1301
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302/* locks held by regulator_enable() */
1303static int _regulator_enable(struct regulator_dev *rdev)
1304{
Mark Brown31aae2b2009-12-21 12:21:52 +00001305 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306
Liam Girdwood414c70c2008-04-30 15:59:04 +01001307 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001308 if (rdev->constraints &&
1309 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1310 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001311
Mark Brown9a2372f2009-08-03 18:49:57 +01001312 if (rdev->use_count == 0) {
1313 /* The regulator may on if it's not switchable or left on */
1314 ret = _regulator_is_enabled(rdev);
1315 if (ret == -EINVAL || ret == 0) {
1316 if (!_regulator_can_change_status(rdev))
1317 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001318
Mark Brown31aae2b2009-12-21 12:21:52 +00001319 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001320 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001321
1322 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001323 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001324 ret = _regulator_get_enable_time(rdev);
1325 if (ret >= 0) {
1326 delay = ret;
1327 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001328 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001329 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001330 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001331 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001332
Mark Brown02fa3ec2010-11-10 14:38:30 +00001333 trace_regulator_enable(rdev_get_name(rdev));
1334
Mark Brown31aae2b2009-12-21 12:21:52 +00001335 /* Allow the regulator to ramp; it would be useful
1336 * to extend this for bulk operations so that the
1337 * regulators can ramp together. */
1338 ret = rdev->desc->ops->enable(rdev);
1339 if (ret < 0)
1340 return ret;
1341
Mark Brown02fa3ec2010-11-10 14:38:30 +00001342 trace_regulator_enable_delay(rdev_get_name(rdev));
1343
Axel Line36c1df2010-11-05 21:51:32 +08001344 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001345 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001346 udelay(delay % 1000);
1347 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001348 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001349 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001350
Mark Brown02fa3ec2010-11-10 14:38:30 +00001351 trace_regulator_enable_complete(rdev_get_name(rdev));
1352
Linus Walleija7433cf2009-08-26 12:54:04 +02001353 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001354 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001355 return ret;
1356 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001357 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001358 }
1359
Mark Brown9a2372f2009-08-03 18:49:57 +01001360 rdev->use_count++;
1361
1362 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001363}
1364
1365/**
1366 * regulator_enable - enable regulator output
1367 * @regulator: regulator source
1368 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001369 * Request that the regulator be enabled with the regulator output at
1370 * the predefined voltage or current value. Calls to regulator_enable()
1371 * must be balanced with calls to regulator_disable().
1372 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001374 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375 */
1376int regulator_enable(struct regulator *regulator)
1377{
David Brownell412aec62008-11-16 11:44:46 -08001378 struct regulator_dev *rdev = regulator->rdev;
1379 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001380
Mark Brown3801b862011-06-09 16:22:22 +01001381 if (rdev->supply) {
1382 ret = regulator_enable(rdev->supply);
1383 if (ret != 0)
1384 return ret;
1385 }
1386
David Brownell412aec62008-11-16 11:44:46 -08001387 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001388 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001389 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001390
1391 if (ret != 0)
1392 regulator_disable(rdev->supply);
1393
Liam Girdwood414c70c2008-04-30 15:59:04 +01001394 return ret;
1395}
1396EXPORT_SYMBOL_GPL(regulator_enable);
1397
1398/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001399static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001400{
1401 int ret = 0;
1402
David Brownellcd94b502009-03-11 16:43:34 -08001403 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001404 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001405 return -EIO;
1406
Liam Girdwood414c70c2008-04-30 15:59:04 +01001407 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001408 if (rdev->use_count == 1 &&
1409 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410
1411 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001412 if (_regulator_can_change_status(rdev) &&
1413 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001414 trace_regulator_disable(rdev_get_name(rdev));
1415
Liam Girdwood414c70c2008-04-30 15:59:04 +01001416 ret = rdev->desc->ops->disable(rdev);
1417 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001418 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 return ret;
1420 }
Mark Brown84b68262009-12-01 21:12:27 +00001421
Mark Brown02fa3ec2010-11-10 14:38:30 +00001422 trace_regulator_disable_complete(rdev_get_name(rdev));
1423
Mark Brown84b68262009-12-01 21:12:27 +00001424 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1425 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001426 }
1427
Liam Girdwood414c70c2008-04-30 15:59:04 +01001428 rdev->use_count = 0;
1429 } else if (rdev->use_count > 1) {
1430
1431 if (rdev->constraints &&
1432 (rdev->constraints->valid_ops_mask &
1433 REGULATOR_CHANGE_DRMS))
1434 drms_uA_update(rdev);
1435
1436 rdev->use_count--;
1437 }
Mark Brown3801b862011-06-09 16:22:22 +01001438
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439 return ret;
1440}
1441
1442/**
1443 * regulator_disable - disable regulator output
1444 * @regulator: regulator source
1445 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001446 * Disable the regulator output voltage or current. Calls to
1447 * regulator_enable() must be balanced with calls to
1448 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001449 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001451 * devices have it enabled, the regulator device supports disabling and
1452 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001453 */
1454int regulator_disable(struct regulator *regulator)
1455{
David Brownell412aec62008-11-16 11:44:46 -08001456 struct regulator_dev *rdev = regulator->rdev;
1457 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458
David Brownell412aec62008-11-16 11:44:46 -08001459 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001460 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001461 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001462
Mark Brown3801b862011-06-09 16:22:22 +01001463 if (ret == 0 && rdev->supply)
1464 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001465
Liam Girdwood414c70c2008-04-30 15:59:04 +01001466 return ret;
1467}
1468EXPORT_SYMBOL_GPL(regulator_disable);
1469
1470/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001471static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001472{
1473 int ret = 0;
1474
1475 /* force disable */
1476 if (rdev->desc->ops->disable) {
1477 /* ah well, who wants to live forever... */
1478 ret = rdev->desc->ops->disable(rdev);
1479 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001480 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001481 return ret;
1482 }
1483 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001484 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1485 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001486 }
1487
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488 return ret;
1489}
1490
1491/**
1492 * regulator_force_disable - force disable regulator output
1493 * @regulator: regulator source
1494 *
1495 * Forcibly disable the regulator output voltage or current.
1496 * NOTE: this *will* disable the regulator output even if other consumer
1497 * devices have it enabled. This should be used for situations when device
1498 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1499 */
1500int regulator_force_disable(struct regulator *regulator)
1501{
Mark Brown82d15832011-05-09 11:41:02 +02001502 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 int ret;
1504
Mark Brown82d15832011-05-09 11:41:02 +02001505 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001507 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001508 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001509
Mark Brown3801b862011-06-09 16:22:22 +01001510 if (rdev->supply)
1511 while (rdev->open_count--)
1512 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001513
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514 return ret;
1515}
1516EXPORT_SYMBOL_GPL(regulator_force_disable);
1517
1518static int _regulator_is_enabled(struct regulator_dev *rdev)
1519{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001520 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001521 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001522 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523
Mark Brown93325462009-08-03 18:49:56 +01001524 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001525}
1526
1527/**
1528 * regulator_is_enabled - is the regulator output enabled
1529 * @regulator: regulator source
1530 *
David Brownell412aec62008-11-16 11:44:46 -08001531 * Returns positive if the regulator driver backing the source/client
1532 * has requested that the device be enabled, zero if it hasn't, else a
1533 * negative errno code.
1534 *
1535 * Note that the device backing this regulator handle can have multiple
1536 * users, so it might be enabled even if regulator_enable() was never
1537 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001538 */
1539int regulator_is_enabled(struct regulator *regulator)
1540{
Mark Brown93325462009-08-03 18:49:56 +01001541 int ret;
1542
1543 mutex_lock(&regulator->rdev->mutex);
1544 ret = _regulator_is_enabled(regulator->rdev);
1545 mutex_unlock(&regulator->rdev->mutex);
1546
1547 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001548}
1549EXPORT_SYMBOL_GPL(regulator_is_enabled);
1550
1551/**
David Brownell4367cfd2009-02-26 11:48:36 -08001552 * regulator_count_voltages - count regulator_list_voltage() selectors
1553 * @regulator: regulator source
1554 *
1555 * Returns number of selectors, or negative errno. Selectors are
1556 * numbered starting at zero, and typically correspond to bitfields
1557 * in hardware registers.
1558 */
1559int regulator_count_voltages(struct regulator *regulator)
1560{
1561 struct regulator_dev *rdev = regulator->rdev;
1562
1563 return rdev->desc->n_voltages ? : -EINVAL;
1564}
1565EXPORT_SYMBOL_GPL(regulator_count_voltages);
1566
1567/**
1568 * regulator_list_voltage - enumerate supported voltages
1569 * @regulator: regulator source
1570 * @selector: identify voltage to list
1571 * Context: can sleep
1572 *
1573 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001574 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001575 * negative errno.
1576 */
1577int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1578{
1579 struct regulator_dev *rdev = regulator->rdev;
1580 struct regulator_ops *ops = rdev->desc->ops;
1581 int ret;
1582
1583 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1584 return -EINVAL;
1585
1586 mutex_lock(&rdev->mutex);
1587 ret = ops->list_voltage(rdev, selector);
1588 mutex_unlock(&rdev->mutex);
1589
1590 if (ret > 0) {
1591 if (ret < rdev->constraints->min_uV)
1592 ret = 0;
1593 else if (ret > rdev->constraints->max_uV)
1594 ret = 0;
1595 }
1596
1597 return ret;
1598}
1599EXPORT_SYMBOL_GPL(regulator_list_voltage);
1600
1601/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001602 * regulator_is_supported_voltage - check if a voltage range can be supported
1603 *
1604 * @regulator: Regulator to check.
1605 * @min_uV: Minimum required voltage in uV.
1606 * @max_uV: Maximum required voltage in uV.
1607 *
1608 * Returns a boolean or a negative error code.
1609 */
1610int regulator_is_supported_voltage(struct regulator *regulator,
1611 int min_uV, int max_uV)
1612{
1613 int i, voltages, ret;
1614
1615 ret = regulator_count_voltages(regulator);
1616 if (ret < 0)
1617 return ret;
1618 voltages = ret;
1619
1620 for (i = 0; i < voltages; i++) {
1621 ret = regulator_list_voltage(regulator, i);
1622
1623 if (ret >= min_uV && ret <= max_uV)
1624 return 1;
1625 }
1626
1627 return 0;
1628}
1629
Mark Brown75790252010-12-12 14:25:50 +00001630static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1631 int min_uV, int max_uV)
1632{
1633 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001634 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001635 unsigned int selector;
1636
1637 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1638
Mark Brownbf5892a2011-05-08 22:13:37 +01001639 min_uV += rdev->constraints->uV_offset;
1640 max_uV += rdev->constraints->uV_offset;
1641
Mark Brown75790252010-12-12 14:25:50 +00001642 if (rdev->desc->ops->set_voltage) {
1643 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1644 &selector);
1645
1646 if (rdev->desc->ops->list_voltage)
1647 selector = rdev->desc->ops->list_voltage(rdev,
1648 selector);
1649 else
1650 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001651 } else if (rdev->desc->ops->set_voltage_sel) {
1652 int best_val = INT_MAX;
1653 int i;
1654
1655 selector = 0;
1656
1657 /* Find the smallest voltage that falls within the specified
1658 * range.
1659 */
1660 for (i = 0; i < rdev->desc->n_voltages; i++) {
1661 ret = rdev->desc->ops->list_voltage(rdev, i);
1662 if (ret < 0)
1663 continue;
1664
1665 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1666 best_val = ret;
1667 selector = i;
1668 }
1669 }
1670
Linus Walleij77af1b2642011-03-17 13:24:36 +01001671 /*
1672 * If we can't obtain the old selector there is not enough
1673 * info to call set_voltage_time_sel().
1674 */
1675 if (rdev->desc->ops->set_voltage_time_sel &&
1676 rdev->desc->ops->get_voltage_sel) {
1677 unsigned int old_selector = 0;
1678
1679 ret = rdev->desc->ops->get_voltage_sel(rdev);
1680 if (ret < 0)
1681 return ret;
1682 old_selector = ret;
1683 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1684 old_selector, selector);
1685 }
1686
Mark Browne8eef822010-12-12 14:36:17 +00001687 if (best_val != INT_MAX) {
1688 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1689 selector = best_val;
1690 } else {
1691 ret = -EINVAL;
1692 }
Mark Brown75790252010-12-12 14:25:50 +00001693 } else {
1694 ret = -EINVAL;
1695 }
1696
Linus Walleij77af1b2642011-03-17 13:24:36 +01001697 /* Insert any necessary delays */
1698 if (delay >= 1000) {
1699 mdelay(delay / 1000);
1700 udelay(delay % 1000);
1701 } else if (delay) {
1702 udelay(delay);
1703 }
1704
Mark Brownded06a52010-12-16 13:59:10 +00001705 if (ret == 0)
1706 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1707 NULL);
1708
Mark Brown75790252010-12-12 14:25:50 +00001709 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1710
1711 return ret;
1712}
1713
Mark Browna7a1ad92009-07-21 16:00:24 +01001714/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001715 * regulator_set_voltage - set regulator output voltage
1716 * @regulator: regulator source
1717 * @min_uV: Minimum required voltage in uV
1718 * @max_uV: Maximum acceptable voltage in uV
1719 *
1720 * Sets a voltage regulator to the desired output voltage. This can be set
1721 * during any regulator state. IOW, regulator can be disabled or enabled.
1722 *
1723 * If the regulator is enabled then the voltage will change to the new value
1724 * immediately otherwise if the regulator is disabled the regulator will
1725 * output at the new voltage when enabled.
1726 *
1727 * NOTE: If the regulator is shared between several devices then the lowest
1728 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001729 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001730 * calling this function otherwise this call will fail.
1731 */
1732int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1733{
1734 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001735 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001736
1737 mutex_lock(&rdev->mutex);
1738
Mark Brown95a3c232010-12-16 15:49:37 +00001739 /* If we're setting the same range as last time the change
1740 * should be a noop (some cpufreq implementations use the same
1741 * voltage for multiple frequencies, for example).
1742 */
1743 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1744 goto out;
1745
Liam Girdwood414c70c2008-04-30 15:59:04 +01001746 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001747 if (!rdev->desc->ops->set_voltage &&
1748 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001749 ret = -EINVAL;
1750 goto out;
1751 }
1752
1753 /* constraints check */
1754 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1755 if (ret < 0)
1756 goto out;
1757 regulator->min_uV = min_uV;
1758 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001759
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001760 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1761 if (ret < 0)
1762 goto out;
1763
Mark Brown75790252010-12-12 14:25:50 +00001764 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001765
Liam Girdwood414c70c2008-04-30 15:59:04 +01001766out:
1767 mutex_unlock(&rdev->mutex);
1768 return ret;
1769}
1770EXPORT_SYMBOL_GPL(regulator_set_voltage);
1771
Mark Brown606a2562010-12-16 15:49:36 +00001772/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001773 * regulator_set_voltage_time - get raise/fall time
1774 * @regulator: regulator source
1775 * @old_uV: starting voltage in microvolts
1776 * @new_uV: target voltage in microvolts
1777 *
1778 * Provided with the starting and ending voltage, this function attempts to
1779 * calculate the time in microseconds required to rise or fall to this new
1780 * voltage.
1781 */
1782int regulator_set_voltage_time(struct regulator *regulator,
1783 int old_uV, int new_uV)
1784{
1785 struct regulator_dev *rdev = regulator->rdev;
1786 struct regulator_ops *ops = rdev->desc->ops;
1787 int old_sel = -1;
1788 int new_sel = -1;
1789 int voltage;
1790 int i;
1791
1792 /* Currently requires operations to do this */
1793 if (!ops->list_voltage || !ops->set_voltage_time_sel
1794 || !rdev->desc->n_voltages)
1795 return -EINVAL;
1796
1797 for (i = 0; i < rdev->desc->n_voltages; i++) {
1798 /* We only look for exact voltage matches here */
1799 voltage = regulator_list_voltage(regulator, i);
1800 if (voltage < 0)
1801 return -EINVAL;
1802 if (voltage == 0)
1803 continue;
1804 if (voltage == old_uV)
1805 old_sel = i;
1806 if (voltage == new_uV)
1807 new_sel = i;
1808 }
1809
1810 if (old_sel < 0 || new_sel < 0)
1811 return -EINVAL;
1812
1813 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1814}
1815EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1816
1817/**
Mark Brown606a2562010-12-16 15:49:36 +00001818 * regulator_sync_voltage - re-apply last regulator output voltage
1819 * @regulator: regulator source
1820 *
1821 * Re-apply the last configured voltage. This is intended to be used
1822 * where some external control source the consumer is cooperating with
1823 * has caused the configured voltage to change.
1824 */
1825int regulator_sync_voltage(struct regulator *regulator)
1826{
1827 struct regulator_dev *rdev = regulator->rdev;
1828 int ret, min_uV, max_uV;
1829
1830 mutex_lock(&rdev->mutex);
1831
1832 if (!rdev->desc->ops->set_voltage &&
1833 !rdev->desc->ops->set_voltage_sel) {
1834 ret = -EINVAL;
1835 goto out;
1836 }
1837
1838 /* This is only going to work if we've had a voltage configured. */
1839 if (!regulator->min_uV && !regulator->max_uV) {
1840 ret = -EINVAL;
1841 goto out;
1842 }
1843
1844 min_uV = regulator->min_uV;
1845 max_uV = regulator->max_uV;
1846
1847 /* This should be a paranoia check... */
1848 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1849 if (ret < 0)
1850 goto out;
1851
1852 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1853 if (ret < 0)
1854 goto out;
1855
1856 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1857
1858out:
1859 mutex_unlock(&rdev->mutex);
1860 return ret;
1861}
1862EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1863
Liam Girdwood414c70c2008-04-30 15:59:04 +01001864static int _regulator_get_voltage(struct regulator_dev *rdev)
1865{
Mark Brownbf5892a2011-05-08 22:13:37 +01001866 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001867
1868 if (rdev->desc->ops->get_voltage_sel) {
1869 sel = rdev->desc->ops->get_voltage_sel(rdev);
1870 if (sel < 0)
1871 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001872 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08001873 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01001874 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08001875 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001876 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08001877 }
Mark Brownbf5892a2011-05-08 22:13:37 +01001878
Axel Lincb220d12011-05-23 20:08:10 +08001879 if (ret < 0)
1880 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01001881 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001882}
1883
1884/**
1885 * regulator_get_voltage - get regulator output voltage
1886 * @regulator: regulator source
1887 *
1888 * This returns the current regulator voltage in uV.
1889 *
1890 * NOTE: If the regulator is disabled it will return the voltage value. This
1891 * function should not be used to determine regulator state.
1892 */
1893int regulator_get_voltage(struct regulator *regulator)
1894{
1895 int ret;
1896
1897 mutex_lock(&regulator->rdev->mutex);
1898
1899 ret = _regulator_get_voltage(regulator->rdev);
1900
1901 mutex_unlock(&regulator->rdev->mutex);
1902
1903 return ret;
1904}
1905EXPORT_SYMBOL_GPL(regulator_get_voltage);
1906
1907/**
1908 * regulator_set_current_limit - set regulator output current limit
1909 * @regulator: regulator source
1910 * @min_uA: Minimuum supported current in uA
1911 * @max_uA: Maximum supported current in uA
1912 *
1913 * Sets current sink to the desired output current. This can be set during
1914 * any regulator state. IOW, regulator can be disabled or enabled.
1915 *
1916 * If the regulator is enabled then the current will change to the new value
1917 * immediately otherwise if the regulator is disabled the regulator will
1918 * output at the new current when enabled.
1919 *
1920 * NOTE: Regulator system constraints must be set for this regulator before
1921 * calling this function otherwise this call will fail.
1922 */
1923int regulator_set_current_limit(struct regulator *regulator,
1924 int min_uA, int max_uA)
1925{
1926 struct regulator_dev *rdev = regulator->rdev;
1927 int ret;
1928
1929 mutex_lock(&rdev->mutex);
1930
1931 /* sanity check */
1932 if (!rdev->desc->ops->set_current_limit) {
1933 ret = -EINVAL;
1934 goto out;
1935 }
1936
1937 /* constraints check */
1938 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1939 if (ret < 0)
1940 goto out;
1941
1942 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1943out:
1944 mutex_unlock(&rdev->mutex);
1945 return ret;
1946}
1947EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1948
1949static int _regulator_get_current_limit(struct regulator_dev *rdev)
1950{
1951 int ret;
1952
1953 mutex_lock(&rdev->mutex);
1954
1955 /* sanity check */
1956 if (!rdev->desc->ops->get_current_limit) {
1957 ret = -EINVAL;
1958 goto out;
1959 }
1960
1961 ret = rdev->desc->ops->get_current_limit(rdev);
1962out:
1963 mutex_unlock(&rdev->mutex);
1964 return ret;
1965}
1966
1967/**
1968 * regulator_get_current_limit - get regulator output current
1969 * @regulator: regulator source
1970 *
1971 * This returns the current supplied by the specified current sink in uA.
1972 *
1973 * NOTE: If the regulator is disabled it will return the current value. This
1974 * function should not be used to determine regulator state.
1975 */
1976int regulator_get_current_limit(struct regulator *regulator)
1977{
1978 return _regulator_get_current_limit(regulator->rdev);
1979}
1980EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1981
1982/**
1983 * regulator_set_mode - set regulator operating mode
1984 * @regulator: regulator source
1985 * @mode: operating mode - one of the REGULATOR_MODE constants
1986 *
1987 * Set regulator operating mode to increase regulator efficiency or improve
1988 * regulation performance.
1989 *
1990 * NOTE: Regulator system constraints must be set for this regulator before
1991 * calling this function otherwise this call will fail.
1992 */
1993int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1994{
1995 struct regulator_dev *rdev = regulator->rdev;
1996 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301997 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001998
1999 mutex_lock(&rdev->mutex);
2000
2001 /* sanity check */
2002 if (!rdev->desc->ops->set_mode) {
2003 ret = -EINVAL;
2004 goto out;
2005 }
2006
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302007 /* return if the same mode is requested */
2008 if (rdev->desc->ops->get_mode) {
2009 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2010 if (regulator_curr_mode == mode) {
2011 ret = 0;
2012 goto out;
2013 }
2014 }
2015
Liam Girdwood414c70c2008-04-30 15:59:04 +01002016 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002017 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002018 if (ret < 0)
2019 goto out;
2020
2021 ret = rdev->desc->ops->set_mode(rdev, mode);
2022out:
2023 mutex_unlock(&rdev->mutex);
2024 return ret;
2025}
2026EXPORT_SYMBOL_GPL(regulator_set_mode);
2027
2028static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2029{
2030 int ret;
2031
2032 mutex_lock(&rdev->mutex);
2033
2034 /* sanity check */
2035 if (!rdev->desc->ops->get_mode) {
2036 ret = -EINVAL;
2037 goto out;
2038 }
2039
2040 ret = rdev->desc->ops->get_mode(rdev);
2041out:
2042 mutex_unlock(&rdev->mutex);
2043 return ret;
2044}
2045
2046/**
2047 * regulator_get_mode - get regulator operating mode
2048 * @regulator: regulator source
2049 *
2050 * Get the current regulator operating mode.
2051 */
2052unsigned int regulator_get_mode(struct regulator *regulator)
2053{
2054 return _regulator_get_mode(regulator->rdev);
2055}
2056EXPORT_SYMBOL_GPL(regulator_get_mode);
2057
2058/**
2059 * regulator_set_optimum_mode - set regulator optimum operating mode
2060 * @regulator: regulator source
2061 * @uA_load: load current
2062 *
2063 * Notifies the regulator core of a new device load. This is then used by
2064 * DRMS (if enabled by constraints) to set the most efficient regulator
2065 * operating mode for the new regulator loading.
2066 *
2067 * Consumer devices notify their supply regulator of the maximum power
2068 * they will require (can be taken from device datasheet in the power
2069 * consumption tables) when they change operational status and hence power
2070 * state. Examples of operational state changes that can affect power
2071 * consumption are :-
2072 *
2073 * o Device is opened / closed.
2074 * o Device I/O is about to begin or has just finished.
2075 * o Device is idling in between work.
2076 *
2077 * This information is also exported via sysfs to userspace.
2078 *
2079 * DRMS will sum the total requested load on the regulator and change
2080 * to the most efficient operating mode if platform constraints allow.
2081 *
2082 * Returns the new regulator mode or error.
2083 */
2084int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2085{
2086 struct regulator_dev *rdev = regulator->rdev;
2087 struct regulator *consumer;
2088 int ret, output_uV, input_uV, total_uA_load = 0;
2089 unsigned int mode;
2090
2091 mutex_lock(&rdev->mutex);
2092
Mark Browna4b41482011-05-14 11:19:45 -07002093 /*
2094 * first check to see if we can set modes at all, otherwise just
2095 * tell the consumer everything is OK.
2096 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002097 regulator->uA_load = uA_load;
2098 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002099 if (ret < 0) {
2100 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002101 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002102 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002103
Liam Girdwood414c70c2008-04-30 15:59:04 +01002104 if (!rdev->desc->ops->get_optimum_mode)
2105 goto out;
2106
Mark Browna4b41482011-05-14 11:19:45 -07002107 /*
2108 * we can actually do this so any errors are indicators of
2109 * potential real failure.
2110 */
2111 ret = -EINVAL;
2112
Liam Girdwood414c70c2008-04-30 15:59:04 +01002113 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002114 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002115 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002116 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002117 goto out;
2118 }
2119
2120 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002121 input_uV = 0;
2122 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002123 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002124 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002125 input_uV = rdev->constraints->input_uV;
2126 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002127 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002128 goto out;
2129 }
2130
2131 /* calc total requested load for this regulator */
2132 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002133 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002134
2135 mode = rdev->desc->ops->get_optimum_mode(rdev,
2136 input_uV, output_uV,
2137 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002138 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002139 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002140 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2141 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002142 goto out;
2143 }
2144
2145 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002146 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002147 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002148 goto out;
2149 }
2150 ret = mode;
2151out:
2152 mutex_unlock(&rdev->mutex);
2153 return ret;
2154}
2155EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2156
2157/**
2158 * regulator_register_notifier - register regulator event notifier
2159 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002160 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002161 *
2162 * Register notifier block to receive regulator events.
2163 */
2164int regulator_register_notifier(struct regulator *regulator,
2165 struct notifier_block *nb)
2166{
2167 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2168 nb);
2169}
2170EXPORT_SYMBOL_GPL(regulator_register_notifier);
2171
2172/**
2173 * regulator_unregister_notifier - unregister regulator event notifier
2174 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002175 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002176 *
2177 * Unregister regulator event notifier block.
2178 */
2179int regulator_unregister_notifier(struct regulator *regulator,
2180 struct notifier_block *nb)
2181{
2182 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2183 nb);
2184}
2185EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2186
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002187/* notify regulator consumers and downstream regulator consumers.
2188 * Note mutex must be held by caller.
2189 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002190static void _notifier_call_chain(struct regulator_dev *rdev,
2191 unsigned long event, void *data)
2192{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002193 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002194 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002195}
2196
2197/**
2198 * regulator_bulk_get - get multiple regulator consumers
2199 *
2200 * @dev: Device to supply
2201 * @num_consumers: Number of consumers to register
2202 * @consumers: Configuration of consumers; clients are stored here.
2203 *
2204 * @return 0 on success, an errno on failure.
2205 *
2206 * This helper function allows drivers to get several regulator
2207 * consumers in one operation. If any of the regulators cannot be
2208 * acquired then any regulators that were allocated will be freed
2209 * before returning to the caller.
2210 */
2211int regulator_bulk_get(struct device *dev, int num_consumers,
2212 struct regulator_bulk_data *consumers)
2213{
2214 int i;
2215 int ret;
2216
2217 for (i = 0; i < num_consumers; i++)
2218 consumers[i].consumer = NULL;
2219
2220 for (i = 0; i < num_consumers; i++) {
2221 consumers[i].consumer = regulator_get(dev,
2222 consumers[i].supply);
2223 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002224 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002225 dev_err(dev, "Failed to get supply '%s': %d\n",
2226 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002227 consumers[i].consumer = NULL;
2228 goto err;
2229 }
2230 }
2231
2232 return 0;
2233
2234err:
2235 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2236 regulator_put(consumers[i].consumer);
2237
2238 return ret;
2239}
2240EXPORT_SYMBOL_GPL(regulator_bulk_get);
2241
Mark Brownf21e0e82011-05-24 08:12:40 +08002242static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2243{
2244 struct regulator_bulk_data *bulk = data;
2245
2246 bulk->ret = regulator_enable(bulk->consumer);
2247}
2248
Liam Girdwood414c70c2008-04-30 15:59:04 +01002249/**
2250 * regulator_bulk_enable - enable multiple regulator consumers
2251 *
2252 * @num_consumers: Number of consumers
2253 * @consumers: Consumer data; clients are stored here.
2254 * @return 0 on success, an errno on failure
2255 *
2256 * This convenience API allows consumers to enable multiple regulator
2257 * clients in a single API call. If any consumers cannot be enabled
2258 * then any others that were enabled will be disabled again prior to
2259 * return.
2260 */
2261int regulator_bulk_enable(int num_consumers,
2262 struct regulator_bulk_data *consumers)
2263{
Mark Brownf21e0e82011-05-24 08:12:40 +08002264 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002265 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002266 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267
Mark Brownf21e0e82011-05-24 08:12:40 +08002268 for (i = 0; i < num_consumers; i++)
2269 async_schedule_domain(regulator_bulk_enable_async,
2270 &consumers[i], &async_domain);
2271
2272 async_synchronize_full_domain(&async_domain);
2273
2274 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002275 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002276 if (consumers[i].ret != 0) {
2277 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002278 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002279 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 }
2281
2282 return 0;
2283
2284err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002285 for (i = 0; i < num_consumers; i++)
2286 if (consumers[i].ret == 0)
2287 regulator_disable(consumers[i].consumer);
2288 else
2289 pr_err("Failed to enable %s: %d\n",
2290 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291
2292 return ret;
2293}
2294EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2295
2296/**
2297 * regulator_bulk_disable - disable multiple regulator consumers
2298 *
2299 * @num_consumers: Number of consumers
2300 * @consumers: Consumer data; clients are stored here.
2301 * @return 0 on success, an errno on failure
2302 *
2303 * This convenience API allows consumers to disable multiple regulator
2304 * clients in a single API call. If any consumers cannot be enabled
2305 * then any others that were disabled will be disabled again prior to
2306 * return.
2307 */
2308int regulator_bulk_disable(int num_consumers,
2309 struct regulator_bulk_data *consumers)
2310{
2311 int i;
2312 int ret;
2313
2314 for (i = 0; i < num_consumers; i++) {
2315 ret = regulator_disable(consumers[i].consumer);
2316 if (ret != 0)
2317 goto err;
2318 }
2319
2320 return 0;
2321
2322err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002323 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002324 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 regulator_enable(consumers[i].consumer);
2326
2327 return ret;
2328}
2329EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2330
2331/**
2332 * regulator_bulk_free - free multiple regulator consumers
2333 *
2334 * @num_consumers: Number of consumers
2335 * @consumers: Consumer data; clients are stored here.
2336 *
2337 * This convenience API allows consumers to free multiple regulator
2338 * clients in a single API call.
2339 */
2340void regulator_bulk_free(int num_consumers,
2341 struct regulator_bulk_data *consumers)
2342{
2343 int i;
2344
2345 for (i = 0; i < num_consumers; i++) {
2346 regulator_put(consumers[i].consumer);
2347 consumers[i].consumer = NULL;
2348 }
2349}
2350EXPORT_SYMBOL_GPL(regulator_bulk_free);
2351
2352/**
2353 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002354 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002355 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002356 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002357 *
2358 * Called by regulator drivers to notify clients a regulator event has
2359 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002360 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002361 */
2362int regulator_notifier_call_chain(struct regulator_dev *rdev,
2363 unsigned long event, void *data)
2364{
2365 _notifier_call_chain(rdev, event, data);
2366 return NOTIFY_DONE;
2367
2368}
2369EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2370
Mark Brownbe721972009-08-04 20:09:52 +02002371/**
2372 * regulator_mode_to_status - convert a regulator mode into a status
2373 *
2374 * @mode: Mode to convert
2375 *
2376 * Convert a regulator mode into a status.
2377 */
2378int regulator_mode_to_status(unsigned int mode)
2379{
2380 switch (mode) {
2381 case REGULATOR_MODE_FAST:
2382 return REGULATOR_STATUS_FAST;
2383 case REGULATOR_MODE_NORMAL:
2384 return REGULATOR_STATUS_NORMAL;
2385 case REGULATOR_MODE_IDLE:
2386 return REGULATOR_STATUS_IDLE;
2387 case REGULATOR_STATUS_STANDBY:
2388 return REGULATOR_STATUS_STANDBY;
2389 default:
2390 return 0;
2391 }
2392}
2393EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2394
David Brownell7ad68e22008-11-11 17:39:02 -08002395/*
2396 * To avoid cluttering sysfs (and memory) with useless state, only
2397 * create attributes that can be meaningfully displayed.
2398 */
2399static int add_regulator_attributes(struct regulator_dev *rdev)
2400{
2401 struct device *dev = &rdev->dev;
2402 struct regulator_ops *ops = rdev->desc->ops;
2403 int status = 0;
2404
2405 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002406 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002407 status = device_create_file(dev, &dev_attr_microvolts);
2408 if (status < 0)
2409 return status;
2410 }
2411 if (ops->get_current_limit) {
2412 status = device_create_file(dev, &dev_attr_microamps);
2413 if (status < 0)
2414 return status;
2415 }
2416 if (ops->get_mode) {
2417 status = device_create_file(dev, &dev_attr_opmode);
2418 if (status < 0)
2419 return status;
2420 }
2421 if (ops->is_enabled) {
2422 status = device_create_file(dev, &dev_attr_state);
2423 if (status < 0)
2424 return status;
2425 }
David Brownell853116a2009-01-14 23:03:17 -08002426 if (ops->get_status) {
2427 status = device_create_file(dev, &dev_attr_status);
2428 if (status < 0)
2429 return status;
2430 }
David Brownell7ad68e22008-11-11 17:39:02 -08002431
2432 /* some attributes are type-specific */
2433 if (rdev->desc->type == REGULATOR_CURRENT) {
2434 status = device_create_file(dev, &dev_attr_requested_microamps);
2435 if (status < 0)
2436 return status;
2437 }
2438
2439 /* all the other attributes exist to support constraints;
2440 * don't show them if there are no constraints, or if the
2441 * relevant supporting methods are missing.
2442 */
2443 if (!rdev->constraints)
2444 return status;
2445
2446 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002447 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002448 status = device_create_file(dev, &dev_attr_min_microvolts);
2449 if (status < 0)
2450 return status;
2451 status = device_create_file(dev, &dev_attr_max_microvolts);
2452 if (status < 0)
2453 return status;
2454 }
2455 if (ops->set_current_limit) {
2456 status = device_create_file(dev, &dev_attr_min_microamps);
2457 if (status < 0)
2458 return status;
2459 status = device_create_file(dev, &dev_attr_max_microamps);
2460 if (status < 0)
2461 return status;
2462 }
2463
2464 /* suspend mode constraints need multiple supporting methods */
2465 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2466 return status;
2467
2468 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2469 if (status < 0)
2470 return status;
2471 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2472 if (status < 0)
2473 return status;
2474 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2475 if (status < 0)
2476 return status;
2477
2478 if (ops->set_suspend_voltage) {
2479 status = device_create_file(dev,
2480 &dev_attr_suspend_standby_microvolts);
2481 if (status < 0)
2482 return status;
2483 status = device_create_file(dev,
2484 &dev_attr_suspend_mem_microvolts);
2485 if (status < 0)
2486 return status;
2487 status = device_create_file(dev,
2488 &dev_attr_suspend_disk_microvolts);
2489 if (status < 0)
2490 return status;
2491 }
2492
2493 if (ops->set_suspend_mode) {
2494 status = device_create_file(dev,
2495 &dev_attr_suspend_standby_mode);
2496 if (status < 0)
2497 return status;
2498 status = device_create_file(dev,
2499 &dev_attr_suspend_mem_mode);
2500 if (status < 0)
2501 return status;
2502 status = device_create_file(dev,
2503 &dev_attr_suspend_disk_mode);
2504 if (status < 0)
2505 return status;
2506 }
2507
2508 return status;
2509}
2510
Mark Brown1130e5b2010-12-21 23:49:31 +00002511static void rdev_init_debugfs(struct regulator_dev *rdev)
2512{
2513#ifdef CONFIG_DEBUG_FS
2514 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2515 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2516 rdev_warn(rdev, "Failed to create debugfs directory\n");
2517 rdev->debugfs = NULL;
2518 return;
2519 }
2520
2521 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2522 &rdev->use_count);
2523 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2524 &rdev->open_count);
2525#endif
2526}
2527
Liam Girdwood414c70c2008-04-30 15:59:04 +01002528/**
2529 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002530 * @regulator_desc: regulator to register
2531 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002532 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002533 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002534 *
2535 * Called by regulator drivers to register a regulator.
2536 * Returns 0 on success.
2537 */
2538struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002539 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002540 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002541{
2542 static atomic_t regulator_no = ATOMIC_INIT(0);
2543 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002544 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002545
2546 if (regulator_desc == NULL)
2547 return ERR_PTR(-EINVAL);
2548
2549 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2550 return ERR_PTR(-EINVAL);
2551
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002552 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2553 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002554 return ERR_PTR(-EINVAL);
2555
Mark Brown46fabe1e2008-09-09 16:21:18 +01002556 if (!init_data)
2557 return ERR_PTR(-EINVAL);
2558
Mark Brown476c2d82010-12-10 17:28:07 +00002559 /* Only one of each should be implemented */
2560 WARN_ON(regulator_desc->ops->get_voltage &&
2561 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002562 WARN_ON(regulator_desc->ops->set_voltage &&
2563 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002564
2565 /* If we're using selectors we must implement list_voltage. */
2566 if (regulator_desc->ops->get_voltage_sel &&
2567 !regulator_desc->ops->list_voltage) {
2568 return ERR_PTR(-EINVAL);
2569 }
Mark Browne8eef822010-12-12 14:36:17 +00002570 if (regulator_desc->ops->set_voltage_sel &&
2571 !regulator_desc->ops->list_voltage) {
2572 return ERR_PTR(-EINVAL);
2573 }
Mark Brown476c2d82010-12-10 17:28:07 +00002574
Liam Girdwood414c70c2008-04-30 15:59:04 +01002575 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2576 if (rdev == NULL)
2577 return ERR_PTR(-ENOMEM);
2578
2579 mutex_lock(&regulator_list_mutex);
2580
2581 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002582 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002583 rdev->owner = regulator_desc->owner;
2584 rdev->desc = regulator_desc;
2585 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002586 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002587 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2588
Liam Girdwooda5766f12008-10-10 13:22:20 +01002589 /* preform any regulator specific init */
2590 if (init_data->regulator_init) {
2591 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002592 if (ret < 0)
2593 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002594 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002595
Liam Girdwooda5766f12008-10-10 13:22:20 +01002596 /* register with sysfs */
2597 rdev->dev.class = &regulator_class;
2598 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002599 dev_set_name(&rdev->dev, "regulator.%d",
2600 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002601 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002602 if (ret != 0) {
2603 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002604 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002605 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002606
2607 dev_set_drvdata(&rdev->dev, rdev);
2608
Mike Rapoport74f544c2008-11-25 14:53:53 +02002609 /* set regulator constraints */
2610 ret = set_machine_constraints(rdev, &init_data->constraints);
2611 if (ret < 0)
2612 goto scrub;
2613
David Brownell7ad68e22008-11-11 17:39:02 -08002614 /* add attributes supported by this regulator */
2615 ret = add_regulator_attributes(rdev);
2616 if (ret < 0)
2617 goto scrub;
2618
Mark Brown0178f3e2010-04-26 15:18:14 +01002619 if (init_data->supply_regulator) {
2620 struct regulator_dev *r;
2621 int found = 0;
2622
2623 list_for_each_entry(r, &regulator_list, list) {
2624 if (strcmp(rdev_get_name(r),
2625 init_data->supply_regulator) == 0) {
2626 found = 1;
2627 break;
2628 }
2629 }
2630
2631 if (!found) {
2632 dev_err(dev, "Failed to find supply %s\n",
2633 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002634 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002635 goto scrub;
2636 }
2637
2638 ret = set_supply(rdev, r);
2639 if (ret < 0)
2640 goto scrub;
2641 }
2642
Liam Girdwooda5766f12008-10-10 13:22:20 +01002643 /* add consumers devices */
2644 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2645 ret = set_consumer_device_supply(rdev,
2646 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002647 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002648 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002649 if (ret < 0) {
2650 dev_err(dev, "Failed to set supply %s\n",
2651 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002652 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002653 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002654 }
2655
2656 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002657
2658 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002659out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002660 mutex_unlock(&regulator_list_mutex);
2661 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002662
Jani Nikulad4033b52010-04-29 10:55:11 +03002663unset_supplies:
2664 unset_regulator_supplies(rdev);
2665
David Brownell4fca9542008-11-11 17:38:53 -08002666scrub:
2667 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002668 /* device core frees rdev */
2669 rdev = ERR_PTR(ret);
2670 goto out;
2671
David Brownell4fca9542008-11-11 17:38:53 -08002672clean:
2673 kfree(rdev);
2674 rdev = ERR_PTR(ret);
2675 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002676}
2677EXPORT_SYMBOL_GPL(regulator_register);
2678
2679/**
2680 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002681 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002682 *
2683 * Called by regulator drivers to unregister a regulator.
2684 */
2685void regulator_unregister(struct regulator_dev *rdev)
2686{
2687 if (rdev == NULL)
2688 return;
2689
2690 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002691#ifdef CONFIG_DEBUG_FS
2692 debugfs_remove_recursive(rdev->debugfs);
2693#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002694 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002695 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002696 list_del(&rdev->list);
2697 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002698 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002699 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002700 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002701 mutex_unlock(&regulator_list_mutex);
2702}
2703EXPORT_SYMBOL_GPL(regulator_unregister);
2704
2705/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002706 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707 * @state: system suspend state
2708 *
2709 * Configure each regulator with it's suspend operating parameters for state.
2710 * This will usually be called by machine suspend code prior to supending.
2711 */
2712int regulator_suspend_prepare(suspend_state_t state)
2713{
2714 struct regulator_dev *rdev;
2715 int ret = 0;
2716
2717 /* ON is handled by regulator active state */
2718 if (state == PM_SUSPEND_ON)
2719 return -EINVAL;
2720
2721 mutex_lock(&regulator_list_mutex);
2722 list_for_each_entry(rdev, &regulator_list, list) {
2723
2724 mutex_lock(&rdev->mutex);
2725 ret = suspend_prepare(rdev, state);
2726 mutex_unlock(&rdev->mutex);
2727
2728 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002729 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002730 goto out;
2731 }
2732 }
2733out:
2734 mutex_unlock(&regulator_list_mutex);
2735 return ret;
2736}
2737EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2738
2739/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002740 * regulator_suspend_finish - resume regulators from system wide suspend
2741 *
2742 * Turn on regulators that might be turned off by regulator_suspend_prepare
2743 * and that should be turned on according to the regulators properties.
2744 */
2745int regulator_suspend_finish(void)
2746{
2747 struct regulator_dev *rdev;
2748 int ret = 0, error;
2749
2750 mutex_lock(&regulator_list_mutex);
2751 list_for_each_entry(rdev, &regulator_list, list) {
2752 struct regulator_ops *ops = rdev->desc->ops;
2753
2754 mutex_lock(&rdev->mutex);
2755 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2756 ops->enable) {
2757 error = ops->enable(rdev);
2758 if (error)
2759 ret = error;
2760 } else {
2761 if (!has_full_constraints)
2762 goto unlock;
2763 if (!ops->disable)
2764 goto unlock;
2765 if (ops->is_enabled && !ops->is_enabled(rdev))
2766 goto unlock;
2767
2768 error = ops->disable(rdev);
2769 if (error)
2770 ret = error;
2771 }
2772unlock:
2773 mutex_unlock(&rdev->mutex);
2774 }
2775 mutex_unlock(&regulator_list_mutex);
2776 return ret;
2777}
2778EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2779
2780/**
Mark Brownca725562009-03-16 19:36:34 +00002781 * regulator_has_full_constraints - the system has fully specified constraints
2782 *
2783 * Calling this function will cause the regulator API to disable all
2784 * regulators which have a zero use count and don't have an always_on
2785 * constraint in a late_initcall.
2786 *
2787 * The intention is that this will become the default behaviour in a
2788 * future kernel release so users are encouraged to use this facility
2789 * now.
2790 */
2791void regulator_has_full_constraints(void)
2792{
2793 has_full_constraints = 1;
2794}
2795EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2796
2797/**
Mark Brown688fe992010-10-05 19:18:32 -07002798 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2799 *
2800 * Calling this function will cause the regulator API to provide a
2801 * dummy regulator to consumers if no physical regulator is found,
2802 * allowing most consumers to proceed as though a regulator were
2803 * configured. This allows systems such as those with software
2804 * controllable regulators for the CPU core only to be brought up more
2805 * readily.
2806 */
2807void regulator_use_dummy_regulator(void)
2808{
2809 board_wants_dummy_regulator = true;
2810}
2811EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2812
2813/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002814 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002815 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002816 *
2817 * Get rdev regulator driver private data. This call can be used in the
2818 * regulator driver context.
2819 */
2820void *rdev_get_drvdata(struct regulator_dev *rdev)
2821{
2822 return rdev->reg_data;
2823}
2824EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2825
2826/**
2827 * regulator_get_drvdata - get regulator driver data
2828 * @regulator: regulator
2829 *
2830 * Get regulator driver private data. This call can be used in the consumer
2831 * driver context when non API regulator specific functions need to be called.
2832 */
2833void *regulator_get_drvdata(struct regulator *regulator)
2834{
2835 return regulator->rdev->reg_data;
2836}
2837EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2838
2839/**
2840 * regulator_set_drvdata - set regulator driver data
2841 * @regulator: regulator
2842 * @data: data
2843 */
2844void regulator_set_drvdata(struct regulator *regulator, void *data)
2845{
2846 regulator->rdev->reg_data = data;
2847}
2848EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2849
2850/**
2851 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002852 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002853 */
2854int rdev_get_id(struct regulator_dev *rdev)
2855{
2856 return rdev->desc->id;
2857}
2858EXPORT_SYMBOL_GPL(rdev_get_id);
2859
Liam Girdwooda5766f12008-10-10 13:22:20 +01002860struct device *rdev_get_dev(struct regulator_dev *rdev)
2861{
2862 return &rdev->dev;
2863}
2864EXPORT_SYMBOL_GPL(rdev_get_dev);
2865
2866void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2867{
2868 return reg_init_data->driver_data;
2869}
2870EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2871
Liam Girdwood414c70c2008-04-30 15:59:04 +01002872static int __init regulator_init(void)
2873{
Mark Brown34abbd62010-02-12 10:18:08 +00002874 int ret;
2875
Mark Brown34abbd62010-02-12 10:18:08 +00002876 ret = class_register(&regulator_class);
2877
Mark Brown1130e5b2010-12-21 23:49:31 +00002878#ifdef CONFIG_DEBUG_FS
2879 debugfs_root = debugfs_create_dir("regulator", NULL);
2880 if (IS_ERR(debugfs_root) || !debugfs_root) {
2881 pr_warn("regulator: Failed to create debugfs directory\n");
2882 debugfs_root = NULL;
2883 }
2884#endif
2885
Mark Brown34abbd62010-02-12 10:18:08 +00002886 regulator_dummy_init();
2887
2888 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002889}
2890
2891/* init early to allow our consumers to complete system booting */
2892core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002893
2894static int __init regulator_init_complete(void)
2895{
2896 struct regulator_dev *rdev;
2897 struct regulator_ops *ops;
2898 struct regulation_constraints *c;
2899 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002900
2901 mutex_lock(&regulator_list_mutex);
2902
2903 /* If we have a full configuration then disable any regulators
2904 * which are not in use or always_on. This will become the
2905 * default behaviour in the future.
2906 */
2907 list_for_each_entry(rdev, &regulator_list, list) {
2908 ops = rdev->desc->ops;
2909 c = rdev->constraints;
2910
Mark Brownf25e0b42009-08-03 18:49:55 +01002911 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002912 continue;
2913
2914 mutex_lock(&rdev->mutex);
2915
2916 if (rdev->use_count)
2917 goto unlock;
2918
2919 /* If we can't read the status assume it's on. */
2920 if (ops->is_enabled)
2921 enabled = ops->is_enabled(rdev);
2922 else
2923 enabled = 1;
2924
2925 if (!enabled)
2926 goto unlock;
2927
2928 if (has_full_constraints) {
2929 /* We log since this may kill the system if it
2930 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002931 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002932 ret = ops->disable(rdev);
2933 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002934 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002935 }
2936 } else {
2937 /* The intention is that in future we will
2938 * assume that full constraints are provided
2939 * so warn even if we aren't going to do
2940 * anything here.
2941 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002942 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002943 }
2944
2945unlock:
2946 mutex_unlock(&rdev->mutex);
2947 }
2948
2949 mutex_unlock(&regulator_list_mutex);
2950
2951 return 0;
2952}
2953late_initcall(regulator_init_complete);