blob: f59821f10fdcabb9ed713976958603b1e21621e9 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
Daniel Walker1d7372e2010-11-17 15:30:28 -080016#define pr_fmt(fmt) "%s: " fmt, __func__
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080017
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
Mark Brown1130e5b2010-12-21 23:49:31 +000020#include <linux/debugfs.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010021#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Mark Brownf21e0e82011-05-24 08:12:40 +080023#include <linux/async.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/err.h>
25#include <linux/mutex.h>
26#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000027#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010028#include <linux/regulator/consumer.h>
29#include <linux/regulator/driver.h>
30#include <linux/regulator/machine.h>
31
Mark Brown02fa3ec2010-11-10 14:38:30 +000032#define CREATE_TRACE_POINTS
33#include <trace/events/regulator.h>
34
Mark Brown34abbd62010-02-12 10:18:08 +000035#include "dummy.h"
36
Mark Brown7d51a0d2011-06-09 16:06:37 +010037#define rdev_crit(rdev, fmt, ...) \
38 pr_crit("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
Joe Perches5da84fd2010-11-30 05:53:48 -080039#define rdev_err(rdev, fmt, ...) \
40 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_warn(rdev, fmt, ...) \
42 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43#define rdev_info(rdev, fmt, ...) \
44 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
45#define rdev_dbg(rdev, fmt, ...) \
46 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
47
Liam Girdwood414c70c2008-04-30 15:59:04 +010048static DEFINE_MUTEX(regulator_list_mutex);
49static LIST_HEAD(regulator_list);
50static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000051static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070052static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010053
Mark Brown1130e5b2010-12-21 23:49:31 +000054#ifdef CONFIG_DEBUG_FS
55static struct dentry *debugfs_root;
56#endif
57
Mark Brown8dc53902008-12-31 12:52:40 +000058/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010059 * struct regulator_map
60 *
61 * Used to provide symbolic supply names to devices.
62 */
63struct regulator_map {
64 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010065 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010066 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010067 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010068};
69
Liam Girdwood414c70c2008-04-30 15:59:04 +010070/*
71 * struct regulator
72 *
73 * One for each consumer device.
74 */
75struct regulator {
76 struct device *dev;
77 struct list_head list;
78 int uA_load;
79 int min_uV;
80 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010081 char *supply_name;
82 struct device_attribute dev_attr;
83 struct regulator_dev *rdev;
Mark Brown5de70512011-06-19 13:33:16 +010084#ifdef CONFIG_DEBUG_FS
85 struct dentry *debugfs;
86#endif
Liam Girdwood414c70c2008-04-30 15:59:04 +010087};
88
89static int _regulator_is_enabled(struct regulator_dev *rdev);
Mark Brown3801b862011-06-09 16:22:22 +010090static int _regulator_disable(struct regulator_dev *rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +010091static int _regulator_get_voltage(struct regulator_dev *rdev);
92static int _regulator_get_current_limit(struct regulator_dev *rdev);
93static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
94static void _notifier_call_chain(struct regulator_dev *rdev,
95 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000096static int _regulator_do_set_voltage(struct regulator_dev *rdev,
97 int min_uV, int max_uV);
Mark Brown3801b862011-06-09 16:22:22 +010098static struct regulator *create_regulator(struct regulator_dev *rdev,
99 struct device *dev,
100 const char *supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100101
Mark Brown1083c392009-10-22 16:31:32 +0100102static const char *rdev_get_name(struct regulator_dev *rdev)
103{
104 if (rdev->constraints && rdev->constraints->name)
105 return rdev->constraints->name;
106 else if (rdev->desc->name)
107 return rdev->desc->name;
108 else
109 return "";
110}
111
Liam Girdwood414c70c2008-04-30 15:59:04 +0100112/* gets the regulator for a given consumer device */
113static struct regulator *get_device_regulator(struct device *dev)
114{
115 struct regulator *regulator = NULL;
116 struct regulator_dev *rdev;
117
118 mutex_lock(&regulator_list_mutex);
119 list_for_each_entry(rdev, &regulator_list, list) {
120 mutex_lock(&rdev->mutex);
121 list_for_each_entry(regulator, &rdev->consumer_list, list) {
122 if (regulator->dev == dev) {
123 mutex_unlock(&rdev->mutex);
124 mutex_unlock(&regulator_list_mutex);
125 return regulator;
126 }
127 }
128 mutex_unlock(&rdev->mutex);
129 }
130 mutex_unlock(&regulator_list_mutex);
131 return NULL;
132}
133
134/* Platform voltage constraint check */
135static int regulator_check_voltage(struct regulator_dev *rdev,
136 int *min_uV, int *max_uV)
137{
138 BUG_ON(*min_uV > *max_uV);
139
140 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800141 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100142 return -ENODEV;
143 }
144 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800145 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100146 return -EPERM;
147 }
148
149 if (*max_uV > rdev->constraints->max_uV)
150 *max_uV = rdev->constraints->max_uV;
151 if (*min_uV < rdev->constraints->min_uV)
152 *min_uV = rdev->constraints->min_uV;
153
154 if (*min_uV > *max_uV)
155 return -EINVAL;
156
157 return 0;
158}
159
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100160/* Make sure we select a voltage that suits the needs of all
161 * regulator consumers
162 */
163static int regulator_check_consumers(struct regulator_dev *rdev,
164 int *min_uV, int *max_uV)
165{
166 struct regulator *regulator;
167
168 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700169 /*
170 * Assume consumers that didn't say anything are OK
171 * with anything in the constraint range.
172 */
173 if (!regulator->min_uV && !regulator->max_uV)
174 continue;
175
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100176 if (*max_uV > regulator->max_uV)
177 *max_uV = regulator->max_uV;
178 if (*min_uV < regulator->min_uV)
179 *min_uV = regulator->min_uV;
180 }
181
182 if (*min_uV > *max_uV)
183 return -EINVAL;
184
185 return 0;
186}
187
Liam Girdwood414c70c2008-04-30 15:59:04 +0100188/* current constraint check */
189static int regulator_check_current_limit(struct regulator_dev *rdev,
190 int *min_uA, int *max_uA)
191{
192 BUG_ON(*min_uA > *max_uA);
193
194 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800195 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100196 return -ENODEV;
197 }
198 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800199 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100200 return -EPERM;
201 }
202
203 if (*max_uA > rdev->constraints->max_uA)
204 *max_uA = rdev->constraints->max_uA;
205 if (*min_uA < rdev->constraints->min_uA)
206 *min_uA = rdev->constraints->min_uA;
207
208 if (*min_uA > *max_uA)
209 return -EINVAL;
210
211 return 0;
212}
213
214/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900215static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100216{
Mark Brown2c608232011-03-30 06:29:12 +0900217 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800218 case REGULATOR_MODE_FAST:
219 case REGULATOR_MODE_NORMAL:
220 case REGULATOR_MODE_IDLE:
221 case REGULATOR_MODE_STANDBY:
222 break;
223 default:
224 return -EINVAL;
225 }
226
Liam Girdwood414c70c2008-04-30 15:59:04 +0100227 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800228 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 return -ENODEV;
230 }
231 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800232 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100233 return -EPERM;
234 }
Mark Brown2c608232011-03-30 06:29:12 +0900235
236 /* The modes are bitmasks, the most power hungry modes having
237 * the lowest values. If the requested mode isn't supported
238 * try higher modes. */
239 while (*mode) {
240 if (rdev->constraints->valid_modes_mask & *mode)
241 return 0;
242 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243 }
Mark Brown2c608232011-03-30 06:29:12 +0900244
245 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246}
247
248/* dynamic regulator mode switching constraint check */
249static int regulator_check_drms(struct regulator_dev *rdev)
250{
251 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800252 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100253 return -ENODEV;
254 }
255 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800256 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257 return -EPERM;
258 }
259 return 0;
260}
261
262static ssize_t device_requested_uA_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
264{
265 struct regulator *regulator;
266
267 regulator = get_device_regulator(dev);
268 if (regulator == NULL)
269 return 0;
270
271 return sprintf(buf, "%d\n", regulator->uA_load);
272}
273
274static ssize_t regulator_uV_show(struct device *dev,
275 struct device_attribute *attr, char *buf)
276{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100277 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278 ssize_t ret;
279
280 mutex_lock(&rdev->mutex);
281 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
282 mutex_unlock(&rdev->mutex);
283
284 return ret;
285}
David Brownell7ad68e22008-11-11 17:39:02 -0800286static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100287
288static ssize_t regulator_uA_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100291 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100292
293 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
294}
David Brownell7ad68e22008-11-11 17:39:02 -0800295static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296
Mark Brownbc558a62008-10-10 15:33:20 +0100297static ssize_t regulator_name_show(struct device *dev,
298 struct device_attribute *attr, char *buf)
299{
300 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100301
Mark Brown1083c392009-10-22 16:31:32 +0100302 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100303}
304
David Brownell4fca9542008-11-11 17:38:53 -0800305static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100306{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307 switch (mode) {
308 case REGULATOR_MODE_FAST:
309 return sprintf(buf, "fast\n");
310 case REGULATOR_MODE_NORMAL:
311 return sprintf(buf, "normal\n");
312 case REGULATOR_MODE_IDLE:
313 return sprintf(buf, "idle\n");
314 case REGULATOR_MODE_STANDBY:
315 return sprintf(buf, "standby\n");
316 }
317 return sprintf(buf, "unknown\n");
318}
319
David Brownell4fca9542008-11-11 17:38:53 -0800320static ssize_t regulator_opmode_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100322{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100323 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100324
David Brownell4fca9542008-11-11 17:38:53 -0800325 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
326}
David Brownell7ad68e22008-11-11 17:39:02 -0800327static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800328
329static ssize_t regulator_print_state(char *buf, int state)
330{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100331 if (state > 0)
332 return sprintf(buf, "enabled\n");
333 else if (state == 0)
334 return sprintf(buf, "disabled\n");
335 else
336 return sprintf(buf, "unknown\n");
337}
338
David Brownell4fca9542008-11-11 17:38:53 -0800339static ssize_t regulator_state_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
342 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100343 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800344
Mark Brown93325462009-08-03 18:49:56 +0100345 mutex_lock(&rdev->mutex);
346 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
347 mutex_unlock(&rdev->mutex);
348
349 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800350}
David Brownell7ad68e22008-11-11 17:39:02 -0800351static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800352
David Brownell853116a2009-01-14 23:03:17 -0800353static ssize_t regulator_status_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
356 struct regulator_dev *rdev = dev_get_drvdata(dev);
357 int status;
358 char *label;
359
360 status = rdev->desc->ops->get_status(rdev);
361 if (status < 0)
362 return status;
363
364 switch (status) {
365 case REGULATOR_STATUS_OFF:
366 label = "off";
367 break;
368 case REGULATOR_STATUS_ON:
369 label = "on";
370 break;
371 case REGULATOR_STATUS_ERROR:
372 label = "error";
373 break;
374 case REGULATOR_STATUS_FAST:
375 label = "fast";
376 break;
377 case REGULATOR_STATUS_NORMAL:
378 label = "normal";
379 break;
380 case REGULATOR_STATUS_IDLE:
381 label = "idle";
382 break;
383 case REGULATOR_STATUS_STANDBY:
384 label = "standby";
385 break;
386 default:
387 return -ERANGE;
388 }
389
390 return sprintf(buf, "%s\n", label);
391}
392static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
393
Liam Girdwood414c70c2008-04-30 15:59:04 +0100394static ssize_t regulator_min_uA_show(struct device *dev,
395 struct device_attribute *attr, char *buf)
396{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100397 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100398
399 if (!rdev->constraints)
400 return sprintf(buf, "constraint not defined\n");
401
402 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
403}
David Brownell7ad68e22008-11-11 17:39:02 -0800404static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100405
406static ssize_t regulator_max_uA_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100409 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100410
411 if (!rdev->constraints)
412 return sprintf(buf, "constraint not defined\n");
413
414 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
415}
David Brownell7ad68e22008-11-11 17:39:02 -0800416static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100417
418static ssize_t regulator_min_uV_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
420{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100421 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100422
423 if (!rdev->constraints)
424 return sprintf(buf, "constraint not defined\n");
425
426 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
427}
David Brownell7ad68e22008-11-11 17:39:02 -0800428static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100429
430static ssize_t regulator_max_uV_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100433 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100434
435 if (!rdev->constraints)
436 return sprintf(buf, "constraint not defined\n");
437
438 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
439}
David Brownell7ad68e22008-11-11 17:39:02 -0800440static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442static ssize_t regulator_total_uA_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
444{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100445 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100446 struct regulator *regulator;
447 int uA = 0;
448
449 mutex_lock(&rdev->mutex);
450 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100451 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452 mutex_unlock(&rdev->mutex);
453 return sprintf(buf, "%d\n", uA);
454}
David Brownell7ad68e22008-11-11 17:39:02 -0800455static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100456
457static ssize_t regulator_num_users_show(struct device *dev,
458 struct device_attribute *attr, char *buf)
459{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100460 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461 return sprintf(buf, "%d\n", rdev->use_count);
462}
463
464static ssize_t regulator_type_show(struct device *dev,
465 struct device_attribute *attr, char *buf)
466{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100467 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468
469 switch (rdev->desc->type) {
470 case REGULATOR_VOLTAGE:
471 return sprintf(buf, "voltage\n");
472 case REGULATOR_CURRENT:
473 return sprintf(buf, "current\n");
474 }
475 return sprintf(buf, "unknown\n");
476}
477
478static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100481 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
484}
David Brownell7ad68e22008-11-11 17:39:02 -0800485static DEVICE_ATTR(suspend_mem_microvolts, 0444,
486 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
488static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
489 struct device_attribute *attr, char *buf)
490{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100491 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
494}
David Brownell7ad68e22008-11-11 17:39:02 -0800495static DEVICE_ATTR(suspend_disk_microvolts, 0444,
496 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
504}
David Brownell7ad68e22008-11-11 17:39:02 -0800505static DEVICE_ATTR(suspend_standby_microvolts, 0444,
506 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
509 struct device_attribute *attr, char *buf)
510{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100511 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
David Brownell4fca9542008-11-11 17:38:53 -0800513 return regulator_print_opmode(buf,
514 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515}
David Brownell7ad68e22008-11-11 17:39:02 -0800516static DEVICE_ATTR(suspend_mem_mode, 0444,
517 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
519static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
520 struct device_attribute *attr, char *buf)
521{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100522 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
David Brownell4fca9542008-11-11 17:38:53 -0800524 return regulator_print_opmode(buf,
525 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526}
David Brownell7ad68e22008-11-11 17:39:02 -0800527static DEVICE_ATTR(suspend_disk_mode, 0444,
528 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529
530static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
531 struct device_attribute *attr, char *buf)
532{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100533 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
David Brownell4fca9542008-11-11 17:38:53 -0800535 return regulator_print_opmode(buf,
536 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537}
David Brownell7ad68e22008-11-11 17:39:02 -0800538static DEVICE_ATTR(suspend_standby_mode, 0444,
539 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540
541static ssize_t regulator_suspend_mem_state_show(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100544 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
David Brownell4fca9542008-11-11 17:38:53 -0800546 return regulator_print_state(buf,
547 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548}
David Brownell7ad68e22008-11-11 17:39:02 -0800549static DEVICE_ATTR(suspend_mem_state, 0444,
550 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551
552static ssize_t regulator_suspend_disk_state_show(struct device *dev,
553 struct device_attribute *attr, char *buf)
554{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100555 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100556
David Brownell4fca9542008-11-11 17:38:53 -0800557 return regulator_print_state(buf,
558 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559}
David Brownell7ad68e22008-11-11 17:39:02 -0800560static DEVICE_ATTR(suspend_disk_state, 0444,
561 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562
563static ssize_t regulator_suspend_standby_state_show(struct device *dev,
564 struct device_attribute *attr, char *buf)
565{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100566 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100567
David Brownell4fca9542008-11-11 17:38:53 -0800568 return regulator_print_state(buf,
569 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100570}
David Brownell7ad68e22008-11-11 17:39:02 -0800571static DEVICE_ATTR(suspend_standby_state, 0444,
572 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100573
David Brownell7ad68e22008-11-11 17:39:02 -0800574
575/*
576 * These are the only attributes are present for all regulators.
577 * Other attributes are a function of regulator functionality.
578 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100579static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100580 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100581 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
582 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583 __ATTR_NULL,
584};
585
586static void regulator_dev_release(struct device *dev)
587{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100588 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100589 kfree(rdev);
590}
591
592static struct class regulator_class = {
593 .name = "regulator",
594 .dev_release = regulator_dev_release,
595 .dev_attrs = regulator_dev_attrs,
596};
597
598/* Calculate the new optimum regulator operating mode based on the new total
599 * consumer load. All locks held by caller */
600static void drms_uA_update(struct regulator_dev *rdev)
601{
602 struct regulator *sibling;
603 int current_uA = 0, output_uV, input_uV, err;
604 unsigned int mode;
605
606 err = regulator_check_drms(rdev);
607 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000608 (!rdev->desc->ops->get_voltage &&
609 !rdev->desc->ops->get_voltage_sel) ||
610 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300611 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100612
613 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000614 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615 if (output_uV <= 0)
616 return;
617
618 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000619 input_uV = 0;
620 if (rdev->supply)
621 input_uV = _regulator_get_voltage(rdev);
622 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100623 input_uV = rdev->constraints->input_uV;
624 if (input_uV <= 0)
625 return;
626
627 /* calc total requested load */
628 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100629 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100630
631 /* now get the optimum mode for our new total regulator load */
632 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
633 output_uV, current_uA);
634
635 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900636 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100637 if (err == 0)
638 rdev->desc->ops->set_mode(rdev, mode);
639}
640
641static int suspend_set_state(struct regulator_dev *rdev,
642 struct regulator_state *rstate)
643{
644 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100645 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646
Mark Brown638f85c2009-10-22 16:31:33 +0100647 can_set_state = rdev->desc->ops->set_suspend_enable &&
648 rdev->desc->ops->set_suspend_disable;
649
650 /* If we have no suspend mode configration don't set anything;
651 * only warn if the driver actually makes the suspend mode
652 * configurable.
653 */
654 if (!rstate->enabled && !rstate->disabled) {
655 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800656 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100657 return 0;
658 }
659
660 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800661 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100662 return -EINVAL;
663 }
664
665 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800666 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100667 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100668 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100669
670 if (rstate->enabled)
671 ret = rdev->desc->ops->set_suspend_enable(rdev);
672 else
673 ret = rdev->desc->ops->set_suspend_disable(rdev);
674 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800675 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 return ret;
677 }
678
679 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
680 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
681 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800682 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683 return ret;
684 }
685 }
686
687 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
688 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
689 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800690 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100691 return ret;
692 }
693 }
694 return ret;
695}
696
697/* locks held by caller */
698static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
699{
700 if (!rdev->constraints)
701 return -EINVAL;
702
703 switch (state) {
704 case PM_SUSPEND_STANDBY:
705 return suspend_set_state(rdev,
706 &rdev->constraints->state_standby);
707 case PM_SUSPEND_MEM:
708 return suspend_set_state(rdev,
709 &rdev->constraints->state_mem);
710 case PM_SUSPEND_MAX:
711 return suspend_set_state(rdev,
712 &rdev->constraints->state_disk);
713 default:
714 return -EINVAL;
715 }
716}
717
718static void print_constraints(struct regulator_dev *rdev)
719{
720 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000721 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100722 int count = 0;
723 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100724
Mark Brown8f031b42009-10-22 16:31:31 +0100725 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100726 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100727 count += sprintf(buf + count, "%d mV ",
728 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100729 else
Mark Brown8f031b42009-10-22 16:31:31 +0100730 count += sprintf(buf + count, "%d <--> %d mV ",
731 constraints->min_uV / 1000,
732 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100733 }
Mark Brown8f031b42009-10-22 16:31:31 +0100734
735 if (!constraints->min_uV ||
736 constraints->min_uV != constraints->max_uV) {
737 ret = _regulator_get_voltage(rdev);
738 if (ret > 0)
739 count += sprintf(buf + count, "at %d mV ", ret / 1000);
740 }
741
Mark Brownbf5892a2011-05-08 22:13:37 +0100742 if (constraints->uV_offset)
743 count += sprintf(buf, "%dmV offset ",
744 constraints->uV_offset / 1000);
745
Mark Brown8f031b42009-10-22 16:31:31 +0100746 if (constraints->min_uA && constraints->max_uA) {
747 if (constraints->min_uA == constraints->max_uA)
748 count += sprintf(buf + count, "%d mA ",
749 constraints->min_uA / 1000);
750 else
751 count += sprintf(buf + count, "%d <--> %d mA ",
752 constraints->min_uA / 1000,
753 constraints->max_uA / 1000);
754 }
755
756 if (!constraints->min_uA ||
757 constraints->min_uA != constraints->max_uA) {
758 ret = _regulator_get_current_limit(rdev);
759 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400760 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100761 }
762
Liam Girdwood414c70c2008-04-30 15:59:04 +0100763 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
764 count += sprintf(buf + count, "fast ");
765 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
766 count += sprintf(buf + count, "normal ");
767 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
768 count += sprintf(buf + count, "idle ");
769 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
770 count += sprintf(buf + count, "standby");
771
Mark Brown13ce29f2010-12-17 16:04:12 +0000772 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100773}
774
Mark Browne79055d2009-10-19 15:53:50 +0100775static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100776 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100777{
778 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100779 int ret;
780
781 /* do we need to apply the constraint voltage */
782 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000783 rdev->constraints->min_uV == rdev->constraints->max_uV) {
784 ret = _regulator_do_set_voltage(rdev,
785 rdev->constraints->min_uV,
786 rdev->constraints->max_uV);
787 if (ret < 0) {
788 rdev_err(rdev, "failed to apply %duV constraint\n",
789 rdev->constraints->min_uV);
790 rdev->constraints = NULL;
791 return ret;
792 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100793 }
Mark Browne79055d2009-10-19 15:53:50 +0100794
795 /* constrain machine-level voltage specs to fit
796 * the actual range supported by this regulator.
797 */
798 if (ops->list_voltage && rdev->desc->n_voltages) {
799 int count = rdev->desc->n_voltages;
800 int i;
801 int min_uV = INT_MAX;
802 int max_uV = INT_MIN;
803 int cmin = constraints->min_uV;
804 int cmax = constraints->max_uV;
805
806 /* it's safe to autoconfigure fixed-voltage supplies
807 and the constraints are used by list_voltage. */
808 if (count == 1 && !cmin) {
809 cmin = 1;
810 cmax = INT_MAX;
811 constraints->min_uV = cmin;
812 constraints->max_uV = cmax;
813 }
814
815 /* voltage constraints are optional */
816 if ((cmin == 0) && (cmax == 0))
817 return 0;
818
819 /* else require explicit machine-level constraints */
820 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800821 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100822 return -EINVAL;
823 }
824
825 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
826 for (i = 0; i < count; i++) {
827 int value;
828
829 value = ops->list_voltage(rdev, i);
830 if (value <= 0)
831 continue;
832
833 /* maybe adjust [min_uV..max_uV] */
834 if (value >= cmin && value < min_uV)
835 min_uV = value;
836 if (value <= cmax && value > max_uV)
837 max_uV = value;
838 }
839
840 /* final: [min_uV..max_uV] valid iff constraints valid */
841 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800842 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100843 return -EINVAL;
844 }
845
846 /* use regulator's subset of machine constraints */
847 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800848 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
849 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100850 constraints->min_uV = min_uV;
851 }
852 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800853 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
854 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100855 constraints->max_uV = max_uV;
856 }
857 }
858
859 return 0;
860}
861
Liam Girdwooda5766f12008-10-10 13:22:20 +0100862/**
863 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000864 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000865 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100866 *
867 * Allows platform initialisation code to define and constrain
868 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
869 * Constraints *must* be set by platform code in order for some
870 * regulator operations to proceed i.e. set_voltage, set_current_limit,
871 * set_mode.
872 */
873static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000874 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100875{
876 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100877 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100878
Mark Brownf8c12fe2010-11-29 15:55:17 +0000879 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
880 GFP_KERNEL);
881 if (!rdev->constraints)
882 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100883
Mark Brownf8c12fe2010-11-29 15:55:17 +0000884 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100885 if (ret != 0)
886 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800887
Liam Girdwooda5766f12008-10-10 13:22:20 +0100888 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100889 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000890 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100891 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800892 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100893 rdev->constraints = NULL;
894 goto out;
895 }
896 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100897
Mark Browna3084662009-02-26 19:24:19 +0000898 if (constraints->initial_mode) {
899 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800900 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000901 ret = -EINVAL;
902 goto out;
903 }
904
Mark Brownf8c12fe2010-11-29 15:55:17 +0000905 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000906 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800907 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000908 goto out;
909 }
910 }
911
Mark Browncacf90f2009-03-02 16:32:46 +0000912 /* If the constraints say the regulator should be on at this point
913 * and we have control then make sure it is enabled.
914 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000915 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
916 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100917 ret = ops->enable(rdev);
918 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800919 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100920 rdev->constraints = NULL;
921 goto out;
922 }
923 }
924
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925 print_constraints(rdev);
926out:
927 return ret;
928}
929
930/**
931 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000932 * @rdev: regulator name
933 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100934 *
935 * Called by platform initialisation code to set the supply regulator for this
936 * regulator. This ensures that a regulators supply will also be enabled by the
937 * core if it's child is enabled.
938 */
939static int set_supply(struct regulator_dev *rdev,
Mark Brown3801b862011-06-09 16:22:22 +0100940 struct regulator_dev *supply_rdev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100941{
942 int err;
943
Mark Brown3801b862011-06-09 16:22:22 +0100944 rdev_info(rdev, "supplied by %s\n", rdev_get_name(supply_rdev));
945
946 rdev->supply = create_regulator(supply_rdev, &rdev->dev, "SUPPLY");
947 if (IS_ERR(rdev->supply)) {
948 err = PTR_ERR(rdev->supply);
949 rdev->supply = NULL;
950 return err;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100951 }
Mark Brown3801b862011-06-09 16:22:22 +0100952
953 return 0;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100954}
955
956/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800957 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000958 * @rdev: regulator source
959 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100960 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000961 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962 *
963 * Allows platform initialisation code to map physical regulator
964 * sources to symbolic names for supplies for use by devices. Devices
965 * should use these symbolic names to request regulators, avoiding the
966 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100967 *
968 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100969 */
970static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100971 struct device *consumer_dev, const char *consumer_dev_name,
972 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100973{
974 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100975 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976
Mark Brown40f92442009-06-17 17:56:39 +0100977 if (consumer_dev && consumer_dev_name)
978 return -EINVAL;
979
980 if (!consumer_dev_name && consumer_dev)
981 consumer_dev_name = dev_name(consumer_dev);
982
Liam Girdwooda5766f12008-10-10 13:22:20 +0100983 if (supply == NULL)
984 return -EINVAL;
985
Mark Brown9ed20992009-07-21 16:00:26 +0100986 if (consumer_dev_name != NULL)
987 has_dev = 1;
988 else
989 has_dev = 0;
990
David Brownell6001e132008-12-31 12:54:19 +0000991 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300992 if (node->dev_name && consumer_dev_name) {
993 if (strcmp(node->dev_name, consumer_dev_name) != 0)
994 continue;
995 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000996 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300997 }
998
David Brownell6001e132008-12-31 12:54:19 +0000999 if (strcmp(node->supply, supply) != 0)
1000 continue;
1001
1002 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -08001003 dev_name(&node->regulator->dev),
1004 node->regulator->desc->name,
1005 supply,
1006 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001007 return -EBUSY;
1008 }
1009
Mark Brown9ed20992009-07-21 16:00:26 +01001010 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001011 if (node == NULL)
1012 return -ENOMEM;
1013
1014 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001015 node->supply = supply;
1016
Mark Brown9ed20992009-07-21 16:00:26 +01001017 if (has_dev) {
1018 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1019 if (node->dev_name == NULL) {
1020 kfree(node);
1021 return -ENOMEM;
1022 }
Mark Brown40f92442009-06-17 17:56:39 +01001023 }
1024
Liam Girdwooda5766f12008-10-10 13:22:20 +01001025 list_add(&node->list, &regulator_map_list);
1026 return 0;
1027}
1028
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001029static void unset_regulator_supplies(struct regulator_dev *rdev)
1030{
1031 struct regulator_map *node, *n;
1032
1033 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1034 if (rdev == node->regulator) {
1035 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001036 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001037 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001038 }
1039 }
1040}
1041
Mark Brownf5726ae2011-06-09 16:22:20 +01001042#define REG_STR_SIZE 64
Liam Girdwood414c70c2008-04-30 15:59:04 +01001043
1044static struct regulator *create_regulator(struct regulator_dev *rdev,
1045 struct device *dev,
1046 const char *supply_name)
1047{
1048 struct regulator *regulator;
1049 char buf[REG_STR_SIZE];
1050 int err, size;
1051
1052 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1053 if (regulator == NULL)
1054 return NULL;
1055
1056 mutex_lock(&rdev->mutex);
1057 regulator->rdev = rdev;
1058 list_add(&regulator->list, &rdev->consumer_list);
1059
1060 if (dev) {
1061 /* create a 'requested_microamps_name' sysfs entry */
Mark Browne0eaede2011-06-09 16:22:21 +01001062 size = scnprintf(buf, REG_STR_SIZE,
1063 "microamps_requested_%s-%s",
1064 dev_name(dev), supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001065 if (size >= REG_STR_SIZE)
1066 goto overflow_err;
1067
1068 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001069 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001070 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1071 if (regulator->dev_attr.attr.name == NULL)
1072 goto attr_name_err;
1073
Liam Girdwood414c70c2008-04-30 15:59:04 +01001074 regulator->dev_attr.attr.mode = 0444;
1075 regulator->dev_attr.show = device_requested_uA_show;
1076 err = device_create_file(dev, &regulator->dev_attr);
1077 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001078 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001079 goto attr_name_err;
1080 }
1081
1082 /* also add a link to the device sysfs entry */
1083 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1084 dev->kobj.name, supply_name);
1085 if (size >= REG_STR_SIZE)
1086 goto attr_err;
1087
1088 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1089 if (regulator->supply_name == NULL)
1090 goto attr_err;
1091
1092 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1093 buf);
1094 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001095 rdev_warn(rdev, "could not add device link %s err %d\n",
1096 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001097 goto link_name_err;
1098 }
Mark Brown5de70512011-06-19 13:33:16 +01001099 } else {
1100 regulator->supply_name = kstrdup(supply_name, GFP_KERNEL);
1101 if (regulator->supply_name == NULL)
1102 goto attr_err;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001103 }
Mark Brown5de70512011-06-19 13:33:16 +01001104
1105#ifdef CONFIG_DEBUG_FS
1106 regulator->debugfs = debugfs_create_dir(regulator->supply_name,
1107 rdev->debugfs);
1108 if (IS_ERR_OR_NULL(regulator->debugfs)) {
1109 rdev_warn(rdev, "Failed to create debugfs directory\n");
1110 regulator->debugfs = NULL;
1111 } else {
1112 debugfs_create_u32("uA_load", 0444, regulator->debugfs,
1113 &regulator->uA_load);
1114 debugfs_create_u32("min_uV", 0444, regulator->debugfs,
1115 &regulator->min_uV);
1116 debugfs_create_u32("max_uV", 0444, regulator->debugfs,
1117 &regulator->max_uV);
1118 }
1119#endif
1120
Liam Girdwood414c70c2008-04-30 15:59:04 +01001121 mutex_unlock(&rdev->mutex);
1122 return regulator;
1123link_name_err:
1124 kfree(regulator->supply_name);
1125attr_err:
1126 device_remove_file(regulator->dev, &regulator->dev_attr);
1127attr_name_err:
1128 kfree(regulator->dev_attr.attr.name);
1129overflow_err:
1130 list_del(&regulator->list);
1131 kfree(regulator);
1132 mutex_unlock(&rdev->mutex);
1133 return NULL;
1134}
1135
Mark Brown31aae2b2009-12-21 12:21:52 +00001136static int _regulator_get_enable_time(struct regulator_dev *rdev)
1137{
1138 if (!rdev->desc->ops->enable_time)
1139 return 0;
1140 return rdev->desc->ops->enable_time(rdev);
1141}
1142
Mark Brown5ffbd132009-07-21 16:00:23 +01001143/* Internal regulator request function */
1144static struct regulator *_regulator_get(struct device *dev, const char *id,
1145 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001146{
1147 struct regulator_dev *rdev;
1148 struct regulator_map *map;
1149 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001150 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001151 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001152
1153 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001154 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001155 return regulator;
1156 }
1157
Mark Brown40f92442009-06-17 17:56:39 +01001158 if (dev)
1159 devname = dev_name(dev);
1160
Liam Girdwood414c70c2008-04-30 15:59:04 +01001161 mutex_lock(&regulator_list_mutex);
1162
1163 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001164 /* If the mapping has a device set up it must match */
1165 if (map->dev_name &&
1166 (!devname || strcmp(map->dev_name, devname)))
1167 continue;
1168
1169 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001170 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001171 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001172 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001173 }
Mark Brown34abbd62010-02-12 10:18:08 +00001174
Mark Brown688fe992010-10-05 19:18:32 -07001175 if (board_wants_dummy_regulator) {
1176 rdev = dummy_regulator_rdev;
1177 goto found;
1178 }
1179
Mark Brown34abbd62010-02-12 10:18:08 +00001180#ifdef CONFIG_REGULATOR_DUMMY
1181 if (!devname)
1182 devname = "deviceless";
1183
1184 /* If the board didn't flag that it was fully constrained then
1185 * substitute in a dummy regulator so consumers can continue.
1186 */
1187 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001188 pr_warn("%s supply %s not found, using dummy regulator\n",
1189 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001190 rdev = dummy_regulator_rdev;
1191 goto found;
1192 }
1193#endif
1194
Liam Girdwood414c70c2008-04-30 15:59:04 +01001195 mutex_unlock(&regulator_list_mutex);
1196 return regulator;
1197
1198found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001199 if (rdev->exclusive) {
1200 regulator = ERR_PTR(-EPERM);
1201 goto out;
1202 }
1203
1204 if (exclusive && rdev->open_count) {
1205 regulator = ERR_PTR(-EBUSY);
1206 goto out;
1207 }
1208
Liam Girdwooda5766f12008-10-10 13:22:20 +01001209 if (!try_module_get(rdev->owner))
1210 goto out;
1211
Liam Girdwood414c70c2008-04-30 15:59:04 +01001212 regulator = create_regulator(rdev, dev, id);
1213 if (regulator == NULL) {
1214 regulator = ERR_PTR(-ENOMEM);
1215 module_put(rdev->owner);
1216 }
1217
Mark Brown5ffbd132009-07-21 16:00:23 +01001218 rdev->open_count++;
1219 if (exclusive) {
1220 rdev->exclusive = 1;
1221
1222 ret = _regulator_is_enabled(rdev);
1223 if (ret > 0)
1224 rdev->use_count = 1;
1225 else
1226 rdev->use_count = 0;
1227 }
1228
Liam Girdwooda5766f12008-10-10 13:22:20 +01001229out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001231
Liam Girdwood414c70c2008-04-30 15:59:04 +01001232 return regulator;
1233}
Mark Brown5ffbd132009-07-21 16:00:23 +01001234
1235/**
1236 * regulator_get - lookup and obtain a reference to a regulator.
1237 * @dev: device for regulator "consumer"
1238 * @id: Supply name or regulator ID.
1239 *
1240 * Returns a struct regulator corresponding to the regulator producer,
1241 * or IS_ERR() condition containing errno.
1242 *
1243 * Use of supply names configured via regulator_set_device_supply() is
1244 * strongly encouraged. It is recommended that the supply name used
1245 * should match the name used for the supply and/or the relevant
1246 * device pins in the datasheet.
1247 */
1248struct regulator *regulator_get(struct device *dev, const char *id)
1249{
1250 return _regulator_get(dev, id, 0);
1251}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001252EXPORT_SYMBOL_GPL(regulator_get);
1253
1254/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001255 * regulator_get_exclusive - obtain exclusive access to a regulator.
1256 * @dev: device for regulator "consumer"
1257 * @id: Supply name or regulator ID.
1258 *
1259 * Returns a struct regulator corresponding to the regulator producer,
1260 * or IS_ERR() condition containing errno. Other consumers will be
1261 * unable to obtain this reference is held and the use count for the
1262 * regulator will be initialised to reflect the current state of the
1263 * regulator.
1264 *
1265 * This is intended for use by consumers which cannot tolerate shared
1266 * use of the regulator such as those which need to force the
1267 * regulator off for correct operation of the hardware they are
1268 * controlling.
1269 *
1270 * Use of supply names configured via regulator_set_device_supply() is
1271 * strongly encouraged. It is recommended that the supply name used
1272 * should match the name used for the supply and/or the relevant
1273 * device pins in the datasheet.
1274 */
1275struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1276{
1277 return _regulator_get(dev, id, 1);
1278}
1279EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1280
1281/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001282 * regulator_put - "free" the regulator source
1283 * @regulator: regulator source
1284 *
1285 * Note: drivers must ensure that all regulator_enable calls made on this
1286 * regulator source are balanced by regulator_disable calls prior to calling
1287 * this function.
1288 */
1289void regulator_put(struct regulator *regulator)
1290{
1291 struct regulator_dev *rdev;
1292
1293 if (regulator == NULL || IS_ERR(regulator))
1294 return;
1295
Liam Girdwood414c70c2008-04-30 15:59:04 +01001296 mutex_lock(&regulator_list_mutex);
1297 rdev = regulator->rdev;
1298
Mark Brown5de70512011-06-19 13:33:16 +01001299#ifdef CONFIG_DEBUG_FS
1300 debugfs_remove_recursive(regulator->debugfs);
1301#endif
1302
Liam Girdwood414c70c2008-04-30 15:59:04 +01001303 /* remove any sysfs entries */
1304 if (regulator->dev) {
1305 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306 device_remove_file(regulator->dev, &regulator->dev_attr);
1307 kfree(regulator->dev_attr.attr.name);
1308 }
Mark Brown5de70512011-06-19 13:33:16 +01001309 kfree(regulator->supply_name);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001310 list_del(&regulator->list);
1311 kfree(regulator);
1312
Mark Brown5ffbd132009-07-21 16:00:23 +01001313 rdev->open_count--;
1314 rdev->exclusive = 0;
1315
Liam Girdwood414c70c2008-04-30 15:59:04 +01001316 module_put(rdev->owner);
1317 mutex_unlock(&regulator_list_mutex);
1318}
1319EXPORT_SYMBOL_GPL(regulator_put);
1320
Mark Brown9a2372f2009-08-03 18:49:57 +01001321static int _regulator_can_change_status(struct regulator_dev *rdev)
1322{
1323 if (!rdev->constraints)
1324 return 0;
1325
1326 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1327 return 1;
1328 else
1329 return 0;
1330}
1331
Liam Girdwood414c70c2008-04-30 15:59:04 +01001332/* locks held by regulator_enable() */
1333static int _regulator_enable(struct regulator_dev *rdev)
1334{
Mark Brown31aae2b2009-12-21 12:21:52 +00001335 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001336
Liam Girdwood414c70c2008-04-30 15:59:04 +01001337 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001338 if (rdev->constraints &&
1339 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1340 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341
Mark Brown9a2372f2009-08-03 18:49:57 +01001342 if (rdev->use_count == 0) {
1343 /* The regulator may on if it's not switchable or left on */
1344 ret = _regulator_is_enabled(rdev);
1345 if (ret == -EINVAL || ret == 0) {
1346 if (!_regulator_can_change_status(rdev))
1347 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001348
Mark Brown31aae2b2009-12-21 12:21:52 +00001349 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001350 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001351
1352 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001353 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001354 ret = _regulator_get_enable_time(rdev);
1355 if (ret >= 0) {
1356 delay = ret;
1357 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001358 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001359 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001360 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001361 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001362
Mark Brown02fa3ec2010-11-10 14:38:30 +00001363 trace_regulator_enable(rdev_get_name(rdev));
1364
Mark Brown31aae2b2009-12-21 12:21:52 +00001365 /* Allow the regulator to ramp; it would be useful
1366 * to extend this for bulk operations so that the
1367 * regulators can ramp together. */
1368 ret = rdev->desc->ops->enable(rdev);
1369 if (ret < 0)
1370 return ret;
1371
Mark Brown02fa3ec2010-11-10 14:38:30 +00001372 trace_regulator_enable_delay(rdev_get_name(rdev));
1373
Axel Line36c1df2010-11-05 21:51:32 +08001374 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001375 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001376 udelay(delay % 1000);
1377 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001378 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001379 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001380
Mark Brown02fa3ec2010-11-10 14:38:30 +00001381 trace_regulator_enable_complete(rdev_get_name(rdev));
1382
Linus Walleija7433cf2009-08-26 12:54:04 +02001383 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001384 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385 return ret;
1386 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001387 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388 }
1389
Mark Brown9a2372f2009-08-03 18:49:57 +01001390 rdev->use_count++;
1391
1392 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393}
1394
1395/**
1396 * regulator_enable - enable regulator output
1397 * @regulator: regulator source
1398 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001399 * Request that the regulator be enabled with the regulator output at
1400 * the predefined voltage or current value. Calls to regulator_enable()
1401 * must be balanced with calls to regulator_disable().
1402 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001403 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001404 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 */
1406int regulator_enable(struct regulator *regulator)
1407{
David Brownell412aec62008-11-16 11:44:46 -08001408 struct regulator_dev *rdev = regulator->rdev;
1409 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410
Mark Brown3801b862011-06-09 16:22:22 +01001411 if (rdev->supply) {
1412 ret = regulator_enable(rdev->supply);
1413 if (ret != 0)
1414 return ret;
1415 }
1416
David Brownell412aec62008-11-16 11:44:46 -08001417 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001418 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001419 mutex_unlock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001420
1421 if (ret != 0)
1422 regulator_disable(rdev->supply);
1423
Liam Girdwood414c70c2008-04-30 15:59:04 +01001424 return ret;
1425}
1426EXPORT_SYMBOL_GPL(regulator_enable);
1427
1428/* locks held by regulator_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001429static int _regulator_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430{
1431 int ret = 0;
1432
David Brownellcd94b502009-03-11 16:43:34 -08001433 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001434 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001435 return -EIO;
1436
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001438 if (rdev->use_count == 1 &&
1439 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440
1441 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001442 if (_regulator_can_change_status(rdev) &&
1443 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001444 trace_regulator_disable(rdev_get_name(rdev));
1445
Liam Girdwood414c70c2008-04-30 15:59:04 +01001446 ret = rdev->desc->ops->disable(rdev);
1447 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001448 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001449 return ret;
1450 }
Mark Brown84b68262009-12-01 21:12:27 +00001451
Mark Brown02fa3ec2010-11-10 14:38:30 +00001452 trace_regulator_disable_complete(rdev_get_name(rdev));
1453
Mark Brown84b68262009-12-01 21:12:27 +00001454 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1455 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001456 }
1457
Liam Girdwood414c70c2008-04-30 15:59:04 +01001458 rdev->use_count = 0;
1459 } else if (rdev->use_count > 1) {
1460
1461 if (rdev->constraints &&
1462 (rdev->constraints->valid_ops_mask &
1463 REGULATOR_CHANGE_DRMS))
1464 drms_uA_update(rdev);
1465
1466 rdev->use_count--;
1467 }
Mark Brown3801b862011-06-09 16:22:22 +01001468
Liam Girdwood414c70c2008-04-30 15:59:04 +01001469 return ret;
1470}
1471
1472/**
1473 * regulator_disable - disable regulator output
1474 * @regulator: regulator source
1475 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001476 * Disable the regulator output voltage or current. Calls to
1477 * regulator_enable() must be balanced with calls to
1478 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001479 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001480 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001481 * devices have it enabled, the regulator device supports disabling and
1482 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483 */
1484int regulator_disable(struct regulator *regulator)
1485{
David Brownell412aec62008-11-16 11:44:46 -08001486 struct regulator_dev *rdev = regulator->rdev;
1487 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001488
David Brownell412aec62008-11-16 11:44:46 -08001489 mutex_lock(&rdev->mutex);
Mark Brown3801b862011-06-09 16:22:22 +01001490 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001491 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001492
Mark Brown3801b862011-06-09 16:22:22 +01001493 if (ret == 0 && rdev->supply)
1494 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001495
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496 return ret;
1497}
1498EXPORT_SYMBOL_GPL(regulator_disable);
1499
1500/* locks held by regulator_force_disable() */
Mark Brown3801b862011-06-09 16:22:22 +01001501static int _regulator_force_disable(struct regulator_dev *rdev)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502{
1503 int ret = 0;
1504
1505 /* force disable */
1506 if (rdev->desc->ops->disable) {
1507 /* ah well, who wants to live forever... */
1508 ret = rdev->desc->ops->disable(rdev);
1509 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001510 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001511 return ret;
1512 }
1513 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001514 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1515 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 }
1517
Liam Girdwood414c70c2008-04-30 15:59:04 +01001518 return ret;
1519}
1520
1521/**
1522 * regulator_force_disable - force disable regulator output
1523 * @regulator: regulator source
1524 *
1525 * Forcibly disable the regulator output voltage or current.
1526 * NOTE: this *will* disable the regulator output even if other consumer
1527 * devices have it enabled. This should be used for situations when device
1528 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1529 */
1530int regulator_force_disable(struct regulator *regulator)
1531{
Mark Brown82d15832011-05-09 11:41:02 +02001532 struct regulator_dev *rdev = regulator->rdev;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001533 int ret;
1534
Mark Brown82d15832011-05-09 11:41:02 +02001535 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001536 regulator->uA_load = 0;
Mark Brown3801b862011-06-09 16:22:22 +01001537 ret = _regulator_force_disable(regulator->rdev);
Mark Brown82d15832011-05-09 11:41:02 +02001538 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001539
Mark Brown3801b862011-06-09 16:22:22 +01001540 if (rdev->supply)
1541 while (rdev->open_count--)
1542 regulator_disable(rdev->supply);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001543
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544 return ret;
1545}
1546EXPORT_SYMBOL_GPL(regulator_force_disable);
1547
1548static int _regulator_is_enabled(struct regulator_dev *rdev)
1549{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001550 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001551 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001552 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001553
Mark Brown93325462009-08-03 18:49:56 +01001554 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001555}
1556
1557/**
1558 * regulator_is_enabled - is the regulator output enabled
1559 * @regulator: regulator source
1560 *
David Brownell412aec62008-11-16 11:44:46 -08001561 * Returns positive if the regulator driver backing the source/client
1562 * has requested that the device be enabled, zero if it hasn't, else a
1563 * negative errno code.
1564 *
1565 * Note that the device backing this regulator handle can have multiple
1566 * users, so it might be enabled even if regulator_enable() was never
1567 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001568 */
1569int regulator_is_enabled(struct regulator *regulator)
1570{
Mark Brown93325462009-08-03 18:49:56 +01001571 int ret;
1572
1573 mutex_lock(&regulator->rdev->mutex);
1574 ret = _regulator_is_enabled(regulator->rdev);
1575 mutex_unlock(&regulator->rdev->mutex);
1576
1577 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001578}
1579EXPORT_SYMBOL_GPL(regulator_is_enabled);
1580
1581/**
David Brownell4367cfd2009-02-26 11:48:36 -08001582 * regulator_count_voltages - count regulator_list_voltage() selectors
1583 * @regulator: regulator source
1584 *
1585 * Returns number of selectors, or negative errno. Selectors are
1586 * numbered starting at zero, and typically correspond to bitfields
1587 * in hardware registers.
1588 */
1589int regulator_count_voltages(struct regulator *regulator)
1590{
1591 struct regulator_dev *rdev = regulator->rdev;
1592
1593 return rdev->desc->n_voltages ? : -EINVAL;
1594}
1595EXPORT_SYMBOL_GPL(regulator_count_voltages);
1596
1597/**
1598 * regulator_list_voltage - enumerate supported voltages
1599 * @regulator: regulator source
1600 * @selector: identify voltage to list
1601 * Context: can sleep
1602 *
1603 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001604 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001605 * negative errno.
1606 */
1607int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1608{
1609 struct regulator_dev *rdev = regulator->rdev;
1610 struct regulator_ops *ops = rdev->desc->ops;
1611 int ret;
1612
1613 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1614 return -EINVAL;
1615
1616 mutex_lock(&rdev->mutex);
1617 ret = ops->list_voltage(rdev, selector);
1618 mutex_unlock(&rdev->mutex);
1619
1620 if (ret > 0) {
1621 if (ret < rdev->constraints->min_uV)
1622 ret = 0;
1623 else if (ret > rdev->constraints->max_uV)
1624 ret = 0;
1625 }
1626
1627 return ret;
1628}
1629EXPORT_SYMBOL_GPL(regulator_list_voltage);
1630
1631/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001632 * regulator_is_supported_voltage - check if a voltage range can be supported
1633 *
1634 * @regulator: Regulator to check.
1635 * @min_uV: Minimum required voltage in uV.
1636 * @max_uV: Maximum required voltage in uV.
1637 *
1638 * Returns a boolean or a negative error code.
1639 */
1640int regulator_is_supported_voltage(struct regulator *regulator,
1641 int min_uV, int max_uV)
1642{
1643 int i, voltages, ret;
1644
1645 ret = regulator_count_voltages(regulator);
1646 if (ret < 0)
1647 return ret;
1648 voltages = ret;
1649
1650 for (i = 0; i < voltages; i++) {
1651 ret = regulator_list_voltage(regulator, i);
1652
1653 if (ret >= min_uV && ret <= max_uV)
1654 return 1;
1655 }
1656
1657 return 0;
1658}
1659
Mark Brown75790252010-12-12 14:25:50 +00001660static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1661 int min_uV, int max_uV)
1662{
1663 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001664 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001665 unsigned int selector;
1666
1667 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1668
Mark Brownbf5892a2011-05-08 22:13:37 +01001669 min_uV += rdev->constraints->uV_offset;
1670 max_uV += rdev->constraints->uV_offset;
1671
Mark Brown75790252010-12-12 14:25:50 +00001672 if (rdev->desc->ops->set_voltage) {
1673 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1674 &selector);
1675
1676 if (rdev->desc->ops->list_voltage)
1677 selector = rdev->desc->ops->list_voltage(rdev,
1678 selector);
1679 else
1680 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001681 } else if (rdev->desc->ops->set_voltage_sel) {
1682 int best_val = INT_MAX;
1683 int i;
1684
1685 selector = 0;
1686
1687 /* Find the smallest voltage that falls within the specified
1688 * range.
1689 */
1690 for (i = 0; i < rdev->desc->n_voltages; i++) {
1691 ret = rdev->desc->ops->list_voltage(rdev, i);
1692 if (ret < 0)
1693 continue;
1694
1695 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1696 best_val = ret;
1697 selector = i;
1698 }
1699 }
1700
Linus Walleij77af1b2642011-03-17 13:24:36 +01001701 /*
1702 * If we can't obtain the old selector there is not enough
1703 * info to call set_voltage_time_sel().
1704 */
1705 if (rdev->desc->ops->set_voltage_time_sel &&
1706 rdev->desc->ops->get_voltage_sel) {
1707 unsigned int old_selector = 0;
1708
1709 ret = rdev->desc->ops->get_voltage_sel(rdev);
1710 if (ret < 0)
1711 return ret;
1712 old_selector = ret;
1713 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1714 old_selector, selector);
1715 }
1716
Mark Browne8eef822010-12-12 14:36:17 +00001717 if (best_val != INT_MAX) {
1718 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1719 selector = best_val;
1720 } else {
1721 ret = -EINVAL;
1722 }
Mark Brown75790252010-12-12 14:25:50 +00001723 } else {
1724 ret = -EINVAL;
1725 }
1726
Linus Walleij77af1b2642011-03-17 13:24:36 +01001727 /* Insert any necessary delays */
1728 if (delay >= 1000) {
1729 mdelay(delay / 1000);
1730 udelay(delay % 1000);
1731 } else if (delay) {
1732 udelay(delay);
1733 }
1734
Mark Brownded06a52010-12-16 13:59:10 +00001735 if (ret == 0)
1736 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1737 NULL);
1738
Mark Brown75790252010-12-12 14:25:50 +00001739 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1740
1741 return ret;
1742}
1743
Mark Browna7a1ad92009-07-21 16:00:24 +01001744/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001745 * regulator_set_voltage - set regulator output voltage
1746 * @regulator: regulator source
1747 * @min_uV: Minimum required voltage in uV
1748 * @max_uV: Maximum acceptable voltage in uV
1749 *
1750 * Sets a voltage regulator to the desired output voltage. This can be set
1751 * during any regulator state. IOW, regulator can be disabled or enabled.
1752 *
1753 * If the regulator is enabled then the voltage will change to the new value
1754 * immediately otherwise if the regulator is disabled the regulator will
1755 * output at the new voltage when enabled.
1756 *
1757 * NOTE: If the regulator is shared between several devices then the lowest
1758 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001759 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001760 * calling this function otherwise this call will fail.
1761 */
1762int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1763{
1764 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001765 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001766
1767 mutex_lock(&rdev->mutex);
1768
Mark Brown95a3c232010-12-16 15:49:37 +00001769 /* If we're setting the same range as last time the change
1770 * should be a noop (some cpufreq implementations use the same
1771 * voltage for multiple frequencies, for example).
1772 */
1773 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1774 goto out;
1775
Liam Girdwood414c70c2008-04-30 15:59:04 +01001776 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001777 if (!rdev->desc->ops->set_voltage &&
1778 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001779 ret = -EINVAL;
1780 goto out;
1781 }
1782
1783 /* constraints check */
1784 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1785 if (ret < 0)
1786 goto out;
1787 regulator->min_uV = min_uV;
1788 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001789
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001790 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1791 if (ret < 0)
1792 goto out;
1793
Mark Brown75790252010-12-12 14:25:50 +00001794 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001795
Liam Girdwood414c70c2008-04-30 15:59:04 +01001796out:
1797 mutex_unlock(&rdev->mutex);
1798 return ret;
1799}
1800EXPORT_SYMBOL_GPL(regulator_set_voltage);
1801
Mark Brown606a2562010-12-16 15:49:36 +00001802/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001803 * regulator_set_voltage_time - get raise/fall time
1804 * @regulator: regulator source
1805 * @old_uV: starting voltage in microvolts
1806 * @new_uV: target voltage in microvolts
1807 *
1808 * Provided with the starting and ending voltage, this function attempts to
1809 * calculate the time in microseconds required to rise or fall to this new
1810 * voltage.
1811 */
1812int regulator_set_voltage_time(struct regulator *regulator,
1813 int old_uV, int new_uV)
1814{
1815 struct regulator_dev *rdev = regulator->rdev;
1816 struct regulator_ops *ops = rdev->desc->ops;
1817 int old_sel = -1;
1818 int new_sel = -1;
1819 int voltage;
1820 int i;
1821
1822 /* Currently requires operations to do this */
1823 if (!ops->list_voltage || !ops->set_voltage_time_sel
1824 || !rdev->desc->n_voltages)
1825 return -EINVAL;
1826
1827 for (i = 0; i < rdev->desc->n_voltages; i++) {
1828 /* We only look for exact voltage matches here */
1829 voltage = regulator_list_voltage(regulator, i);
1830 if (voltage < 0)
1831 return -EINVAL;
1832 if (voltage == 0)
1833 continue;
1834 if (voltage == old_uV)
1835 old_sel = i;
1836 if (voltage == new_uV)
1837 new_sel = i;
1838 }
1839
1840 if (old_sel < 0 || new_sel < 0)
1841 return -EINVAL;
1842
1843 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1844}
1845EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1846
1847/**
Mark Brown606a2562010-12-16 15:49:36 +00001848 * regulator_sync_voltage - re-apply last regulator output voltage
1849 * @regulator: regulator source
1850 *
1851 * Re-apply the last configured voltage. This is intended to be used
1852 * where some external control source the consumer is cooperating with
1853 * has caused the configured voltage to change.
1854 */
1855int regulator_sync_voltage(struct regulator *regulator)
1856{
1857 struct regulator_dev *rdev = regulator->rdev;
1858 int ret, min_uV, max_uV;
1859
1860 mutex_lock(&rdev->mutex);
1861
1862 if (!rdev->desc->ops->set_voltage &&
1863 !rdev->desc->ops->set_voltage_sel) {
1864 ret = -EINVAL;
1865 goto out;
1866 }
1867
1868 /* This is only going to work if we've had a voltage configured. */
1869 if (!regulator->min_uV && !regulator->max_uV) {
1870 ret = -EINVAL;
1871 goto out;
1872 }
1873
1874 min_uV = regulator->min_uV;
1875 max_uV = regulator->max_uV;
1876
1877 /* This should be a paranoia check... */
1878 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1879 if (ret < 0)
1880 goto out;
1881
1882 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1883 if (ret < 0)
1884 goto out;
1885
1886 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1887
1888out:
1889 mutex_unlock(&rdev->mutex);
1890 return ret;
1891}
1892EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1893
Liam Girdwood414c70c2008-04-30 15:59:04 +01001894static int _regulator_get_voltage(struct regulator_dev *rdev)
1895{
Mark Brownbf5892a2011-05-08 22:13:37 +01001896 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001897
1898 if (rdev->desc->ops->get_voltage_sel) {
1899 sel = rdev->desc->ops->get_voltage_sel(rdev);
1900 if (sel < 0)
1901 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001902 ret = rdev->desc->ops->list_voltage(rdev, sel);
Axel Lincb220d12011-05-23 20:08:10 +08001903 } else if (rdev->desc->ops->get_voltage) {
Mark Brownbf5892a2011-05-08 22:13:37 +01001904 ret = rdev->desc->ops->get_voltage(rdev);
Axel Lincb220d12011-05-23 20:08:10 +08001905 } else {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001906 return -EINVAL;
Axel Lincb220d12011-05-23 20:08:10 +08001907 }
Mark Brownbf5892a2011-05-08 22:13:37 +01001908
Axel Lincb220d12011-05-23 20:08:10 +08001909 if (ret < 0)
1910 return ret;
Mark Brownbf5892a2011-05-08 22:13:37 +01001911 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912}
1913
1914/**
1915 * regulator_get_voltage - get regulator output voltage
1916 * @regulator: regulator source
1917 *
1918 * This returns the current regulator voltage in uV.
1919 *
1920 * NOTE: If the regulator is disabled it will return the voltage value. This
1921 * function should not be used to determine regulator state.
1922 */
1923int regulator_get_voltage(struct regulator *regulator)
1924{
1925 int ret;
1926
1927 mutex_lock(&regulator->rdev->mutex);
1928
1929 ret = _regulator_get_voltage(regulator->rdev);
1930
1931 mutex_unlock(&regulator->rdev->mutex);
1932
1933 return ret;
1934}
1935EXPORT_SYMBOL_GPL(regulator_get_voltage);
1936
1937/**
1938 * regulator_set_current_limit - set regulator output current limit
1939 * @regulator: regulator source
1940 * @min_uA: Minimuum supported current in uA
1941 * @max_uA: Maximum supported current in uA
1942 *
1943 * Sets current sink to the desired output current. This can be set during
1944 * any regulator state. IOW, regulator can be disabled or enabled.
1945 *
1946 * If the regulator is enabled then the current will change to the new value
1947 * immediately otherwise if the regulator is disabled the regulator will
1948 * output at the new current when enabled.
1949 *
1950 * NOTE: Regulator system constraints must be set for this regulator before
1951 * calling this function otherwise this call will fail.
1952 */
1953int regulator_set_current_limit(struct regulator *regulator,
1954 int min_uA, int max_uA)
1955{
1956 struct regulator_dev *rdev = regulator->rdev;
1957 int ret;
1958
1959 mutex_lock(&rdev->mutex);
1960
1961 /* sanity check */
1962 if (!rdev->desc->ops->set_current_limit) {
1963 ret = -EINVAL;
1964 goto out;
1965 }
1966
1967 /* constraints check */
1968 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1969 if (ret < 0)
1970 goto out;
1971
1972 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1973out:
1974 mutex_unlock(&rdev->mutex);
1975 return ret;
1976}
1977EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1978
1979static int _regulator_get_current_limit(struct regulator_dev *rdev)
1980{
1981 int ret;
1982
1983 mutex_lock(&rdev->mutex);
1984
1985 /* sanity check */
1986 if (!rdev->desc->ops->get_current_limit) {
1987 ret = -EINVAL;
1988 goto out;
1989 }
1990
1991 ret = rdev->desc->ops->get_current_limit(rdev);
1992out:
1993 mutex_unlock(&rdev->mutex);
1994 return ret;
1995}
1996
1997/**
1998 * regulator_get_current_limit - get regulator output current
1999 * @regulator: regulator source
2000 *
2001 * This returns the current supplied by the specified current sink in uA.
2002 *
2003 * NOTE: If the regulator is disabled it will return the current value. This
2004 * function should not be used to determine regulator state.
2005 */
2006int regulator_get_current_limit(struct regulator *regulator)
2007{
2008 return _regulator_get_current_limit(regulator->rdev);
2009}
2010EXPORT_SYMBOL_GPL(regulator_get_current_limit);
2011
2012/**
2013 * regulator_set_mode - set regulator operating mode
2014 * @regulator: regulator source
2015 * @mode: operating mode - one of the REGULATOR_MODE constants
2016 *
2017 * Set regulator operating mode to increase regulator efficiency or improve
2018 * regulation performance.
2019 *
2020 * NOTE: Regulator system constraints must be set for this regulator before
2021 * calling this function otherwise this call will fail.
2022 */
2023int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2024{
2025 struct regulator_dev *rdev = regulator->rdev;
2026 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302027 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002028
2029 mutex_lock(&rdev->mutex);
2030
2031 /* sanity check */
2032 if (!rdev->desc->ops->set_mode) {
2033 ret = -EINVAL;
2034 goto out;
2035 }
2036
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302037 /* return if the same mode is requested */
2038 if (rdev->desc->ops->get_mode) {
2039 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2040 if (regulator_curr_mode == mode) {
2041 ret = 0;
2042 goto out;
2043 }
2044 }
2045
Liam Girdwood414c70c2008-04-30 15:59:04 +01002046 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002047 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002048 if (ret < 0)
2049 goto out;
2050
2051 ret = rdev->desc->ops->set_mode(rdev, mode);
2052out:
2053 mutex_unlock(&rdev->mutex);
2054 return ret;
2055}
2056EXPORT_SYMBOL_GPL(regulator_set_mode);
2057
2058static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2059{
2060 int ret;
2061
2062 mutex_lock(&rdev->mutex);
2063
2064 /* sanity check */
2065 if (!rdev->desc->ops->get_mode) {
2066 ret = -EINVAL;
2067 goto out;
2068 }
2069
2070 ret = rdev->desc->ops->get_mode(rdev);
2071out:
2072 mutex_unlock(&rdev->mutex);
2073 return ret;
2074}
2075
2076/**
2077 * regulator_get_mode - get regulator operating mode
2078 * @regulator: regulator source
2079 *
2080 * Get the current regulator operating mode.
2081 */
2082unsigned int regulator_get_mode(struct regulator *regulator)
2083{
2084 return _regulator_get_mode(regulator->rdev);
2085}
2086EXPORT_SYMBOL_GPL(regulator_get_mode);
2087
2088/**
2089 * regulator_set_optimum_mode - set regulator optimum operating mode
2090 * @regulator: regulator source
2091 * @uA_load: load current
2092 *
2093 * Notifies the regulator core of a new device load. This is then used by
2094 * DRMS (if enabled by constraints) to set the most efficient regulator
2095 * operating mode for the new regulator loading.
2096 *
2097 * Consumer devices notify their supply regulator of the maximum power
2098 * they will require (can be taken from device datasheet in the power
2099 * consumption tables) when they change operational status and hence power
2100 * state. Examples of operational state changes that can affect power
2101 * consumption are :-
2102 *
2103 * o Device is opened / closed.
2104 * o Device I/O is about to begin or has just finished.
2105 * o Device is idling in between work.
2106 *
2107 * This information is also exported via sysfs to userspace.
2108 *
2109 * DRMS will sum the total requested load on the regulator and change
2110 * to the most efficient operating mode if platform constraints allow.
2111 *
2112 * Returns the new regulator mode or error.
2113 */
2114int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2115{
2116 struct regulator_dev *rdev = regulator->rdev;
2117 struct regulator *consumer;
2118 int ret, output_uV, input_uV, total_uA_load = 0;
2119 unsigned int mode;
2120
2121 mutex_lock(&rdev->mutex);
2122
Mark Browna4b41482011-05-14 11:19:45 -07002123 /*
2124 * first check to see if we can set modes at all, otherwise just
2125 * tell the consumer everything is OK.
2126 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002127 regulator->uA_load = uA_load;
2128 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002129 if (ret < 0) {
2130 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002131 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002132 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002133
Liam Girdwood414c70c2008-04-30 15:59:04 +01002134 if (!rdev->desc->ops->get_optimum_mode)
2135 goto out;
2136
Mark Browna4b41482011-05-14 11:19:45 -07002137 /*
2138 * we can actually do this so any errors are indicators of
2139 * potential real failure.
2140 */
2141 ret = -EINVAL;
2142
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002144 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002146 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147 goto out;
2148 }
2149
2150 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002151 input_uV = 0;
2152 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002153 input_uV = regulator_get_voltage(rdev->supply);
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002154 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002155 input_uV = rdev->constraints->input_uV;
2156 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002157 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002158 goto out;
2159 }
2160
2161 /* calc total requested load for this regulator */
2162 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002163 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002164
2165 mode = rdev->desc->ops->get_optimum_mode(rdev,
2166 input_uV, output_uV,
2167 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002168 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002169 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002170 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2171 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002172 goto out;
2173 }
2174
2175 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002176 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002177 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002178 goto out;
2179 }
2180 ret = mode;
2181out:
2182 mutex_unlock(&rdev->mutex);
2183 return ret;
2184}
2185EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2186
2187/**
2188 * regulator_register_notifier - register regulator event notifier
2189 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002190 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002191 *
2192 * Register notifier block to receive regulator events.
2193 */
2194int regulator_register_notifier(struct regulator *regulator,
2195 struct notifier_block *nb)
2196{
2197 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2198 nb);
2199}
2200EXPORT_SYMBOL_GPL(regulator_register_notifier);
2201
2202/**
2203 * regulator_unregister_notifier - unregister regulator event notifier
2204 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002205 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206 *
2207 * Unregister regulator event notifier block.
2208 */
2209int regulator_unregister_notifier(struct regulator *regulator,
2210 struct notifier_block *nb)
2211{
2212 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2213 nb);
2214}
2215EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2216
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002217/* notify regulator consumers and downstream regulator consumers.
2218 * Note mutex must be held by caller.
2219 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002220static void _notifier_call_chain(struct regulator_dev *rdev,
2221 unsigned long event, void *data)
2222{
Liam Girdwood414c70c2008-04-30 15:59:04 +01002223 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002224 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002225}
2226
2227/**
2228 * regulator_bulk_get - get multiple regulator consumers
2229 *
2230 * @dev: Device to supply
2231 * @num_consumers: Number of consumers to register
2232 * @consumers: Configuration of consumers; clients are stored here.
2233 *
2234 * @return 0 on success, an errno on failure.
2235 *
2236 * This helper function allows drivers to get several regulator
2237 * consumers in one operation. If any of the regulators cannot be
2238 * acquired then any regulators that were allocated will be freed
2239 * before returning to the caller.
2240 */
2241int regulator_bulk_get(struct device *dev, int num_consumers,
2242 struct regulator_bulk_data *consumers)
2243{
2244 int i;
2245 int ret;
2246
2247 for (i = 0; i < num_consumers; i++)
2248 consumers[i].consumer = NULL;
2249
2250 for (i = 0; i < num_consumers; i++) {
2251 consumers[i].consumer = regulator_get(dev,
2252 consumers[i].supply);
2253 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002254 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002255 dev_err(dev, "Failed to get supply '%s': %d\n",
2256 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257 consumers[i].consumer = NULL;
2258 goto err;
2259 }
2260 }
2261
2262 return 0;
2263
2264err:
2265 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2266 regulator_put(consumers[i].consumer);
2267
2268 return ret;
2269}
2270EXPORT_SYMBOL_GPL(regulator_bulk_get);
2271
Mark Brownf21e0e82011-05-24 08:12:40 +08002272static void regulator_bulk_enable_async(void *data, async_cookie_t cookie)
2273{
2274 struct regulator_bulk_data *bulk = data;
2275
2276 bulk->ret = regulator_enable(bulk->consumer);
2277}
2278
Liam Girdwood414c70c2008-04-30 15:59:04 +01002279/**
2280 * regulator_bulk_enable - enable multiple regulator consumers
2281 *
2282 * @num_consumers: Number of consumers
2283 * @consumers: Consumer data; clients are stored here.
2284 * @return 0 on success, an errno on failure
2285 *
2286 * This convenience API allows consumers to enable multiple regulator
2287 * clients in a single API call. If any consumers cannot be enabled
2288 * then any others that were enabled will be disabled again prior to
2289 * return.
2290 */
2291int regulator_bulk_enable(int num_consumers,
2292 struct regulator_bulk_data *consumers)
2293{
Mark Brownf21e0e82011-05-24 08:12:40 +08002294 LIST_HEAD(async_domain);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002295 int i;
Mark Brownf21e0e82011-05-24 08:12:40 +08002296 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297
Mark Brownf21e0e82011-05-24 08:12:40 +08002298 for (i = 0; i < num_consumers; i++)
2299 async_schedule_domain(regulator_bulk_enable_async,
2300 &consumers[i], &async_domain);
2301
2302 async_synchronize_full_domain(&async_domain);
2303
2304 /* If any consumer failed we need to unwind any that succeeded */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 for (i = 0; i < num_consumers; i++) {
Mark Brownf21e0e82011-05-24 08:12:40 +08002306 if (consumers[i].ret != 0) {
2307 ret = consumers[i].ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002308 goto err;
Mark Brownf21e0e82011-05-24 08:12:40 +08002309 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002310 }
2311
2312 return 0;
2313
2314err:
Mark Brownf21e0e82011-05-24 08:12:40 +08002315 for (i = 0; i < num_consumers; i++)
2316 if (consumers[i].ret == 0)
2317 regulator_disable(consumers[i].consumer);
2318 else
2319 pr_err("Failed to enable %s: %d\n",
2320 consumers[i].supply, consumers[i].ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002321
2322 return ret;
2323}
2324EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2325
2326/**
2327 * regulator_bulk_disable - disable multiple regulator consumers
2328 *
2329 * @num_consumers: Number of consumers
2330 * @consumers: Consumer data; clients are stored here.
2331 * @return 0 on success, an errno on failure
2332 *
2333 * This convenience API allows consumers to disable multiple regulator
2334 * clients in a single API call. If any consumers cannot be enabled
2335 * then any others that were disabled will be disabled again prior to
2336 * return.
2337 */
2338int regulator_bulk_disable(int num_consumers,
2339 struct regulator_bulk_data *consumers)
2340{
2341 int i;
2342 int ret;
2343
2344 for (i = 0; i < num_consumers; i++) {
2345 ret = regulator_disable(consumers[i].consumer);
2346 if (ret != 0)
2347 goto err;
2348 }
2349
2350 return 0;
2351
2352err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002353 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002354 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002355 regulator_enable(consumers[i].consumer);
2356
2357 return ret;
2358}
2359EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2360
2361/**
2362 * regulator_bulk_free - free multiple regulator consumers
2363 *
2364 * @num_consumers: Number of consumers
2365 * @consumers: Consumer data; clients are stored here.
2366 *
2367 * This convenience API allows consumers to free multiple regulator
2368 * clients in a single API call.
2369 */
2370void regulator_bulk_free(int num_consumers,
2371 struct regulator_bulk_data *consumers)
2372{
2373 int i;
2374
2375 for (i = 0; i < num_consumers; i++) {
2376 regulator_put(consumers[i].consumer);
2377 consumers[i].consumer = NULL;
2378 }
2379}
2380EXPORT_SYMBOL_GPL(regulator_bulk_free);
2381
2382/**
2383 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002384 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002385 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002386 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387 *
2388 * Called by regulator drivers to notify clients a regulator event has
2389 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002390 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002391 */
2392int regulator_notifier_call_chain(struct regulator_dev *rdev,
2393 unsigned long event, void *data)
2394{
2395 _notifier_call_chain(rdev, event, data);
2396 return NOTIFY_DONE;
2397
2398}
2399EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2400
Mark Brownbe721972009-08-04 20:09:52 +02002401/**
2402 * regulator_mode_to_status - convert a regulator mode into a status
2403 *
2404 * @mode: Mode to convert
2405 *
2406 * Convert a regulator mode into a status.
2407 */
2408int regulator_mode_to_status(unsigned int mode)
2409{
2410 switch (mode) {
2411 case REGULATOR_MODE_FAST:
2412 return REGULATOR_STATUS_FAST;
2413 case REGULATOR_MODE_NORMAL:
2414 return REGULATOR_STATUS_NORMAL;
2415 case REGULATOR_MODE_IDLE:
2416 return REGULATOR_STATUS_IDLE;
2417 case REGULATOR_STATUS_STANDBY:
2418 return REGULATOR_STATUS_STANDBY;
2419 default:
2420 return 0;
2421 }
2422}
2423EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2424
David Brownell7ad68e22008-11-11 17:39:02 -08002425/*
2426 * To avoid cluttering sysfs (and memory) with useless state, only
2427 * create attributes that can be meaningfully displayed.
2428 */
2429static int add_regulator_attributes(struct regulator_dev *rdev)
2430{
2431 struct device *dev = &rdev->dev;
2432 struct regulator_ops *ops = rdev->desc->ops;
2433 int status = 0;
2434
2435 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002436 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002437 status = device_create_file(dev, &dev_attr_microvolts);
2438 if (status < 0)
2439 return status;
2440 }
2441 if (ops->get_current_limit) {
2442 status = device_create_file(dev, &dev_attr_microamps);
2443 if (status < 0)
2444 return status;
2445 }
2446 if (ops->get_mode) {
2447 status = device_create_file(dev, &dev_attr_opmode);
2448 if (status < 0)
2449 return status;
2450 }
2451 if (ops->is_enabled) {
2452 status = device_create_file(dev, &dev_attr_state);
2453 if (status < 0)
2454 return status;
2455 }
David Brownell853116a2009-01-14 23:03:17 -08002456 if (ops->get_status) {
2457 status = device_create_file(dev, &dev_attr_status);
2458 if (status < 0)
2459 return status;
2460 }
David Brownell7ad68e22008-11-11 17:39:02 -08002461
2462 /* some attributes are type-specific */
2463 if (rdev->desc->type == REGULATOR_CURRENT) {
2464 status = device_create_file(dev, &dev_attr_requested_microamps);
2465 if (status < 0)
2466 return status;
2467 }
2468
2469 /* all the other attributes exist to support constraints;
2470 * don't show them if there are no constraints, or if the
2471 * relevant supporting methods are missing.
2472 */
2473 if (!rdev->constraints)
2474 return status;
2475
2476 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002477 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002478 status = device_create_file(dev, &dev_attr_min_microvolts);
2479 if (status < 0)
2480 return status;
2481 status = device_create_file(dev, &dev_attr_max_microvolts);
2482 if (status < 0)
2483 return status;
2484 }
2485 if (ops->set_current_limit) {
2486 status = device_create_file(dev, &dev_attr_min_microamps);
2487 if (status < 0)
2488 return status;
2489 status = device_create_file(dev, &dev_attr_max_microamps);
2490 if (status < 0)
2491 return status;
2492 }
2493
2494 /* suspend mode constraints need multiple supporting methods */
2495 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2496 return status;
2497
2498 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2499 if (status < 0)
2500 return status;
2501 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2502 if (status < 0)
2503 return status;
2504 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2505 if (status < 0)
2506 return status;
2507
2508 if (ops->set_suspend_voltage) {
2509 status = device_create_file(dev,
2510 &dev_attr_suspend_standby_microvolts);
2511 if (status < 0)
2512 return status;
2513 status = device_create_file(dev,
2514 &dev_attr_suspend_mem_microvolts);
2515 if (status < 0)
2516 return status;
2517 status = device_create_file(dev,
2518 &dev_attr_suspend_disk_microvolts);
2519 if (status < 0)
2520 return status;
2521 }
2522
2523 if (ops->set_suspend_mode) {
2524 status = device_create_file(dev,
2525 &dev_attr_suspend_standby_mode);
2526 if (status < 0)
2527 return status;
2528 status = device_create_file(dev,
2529 &dev_attr_suspend_mem_mode);
2530 if (status < 0)
2531 return status;
2532 status = device_create_file(dev,
2533 &dev_attr_suspend_disk_mode);
2534 if (status < 0)
2535 return status;
2536 }
2537
2538 return status;
2539}
2540
Mark Brown1130e5b2010-12-21 23:49:31 +00002541static void rdev_init_debugfs(struct regulator_dev *rdev)
2542{
2543#ifdef CONFIG_DEBUG_FS
2544 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2545 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2546 rdev_warn(rdev, "Failed to create debugfs directory\n");
2547 rdev->debugfs = NULL;
2548 return;
2549 }
2550
2551 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2552 &rdev->use_count);
2553 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2554 &rdev->open_count);
2555#endif
2556}
2557
Liam Girdwood414c70c2008-04-30 15:59:04 +01002558/**
2559 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002560 * @regulator_desc: regulator to register
2561 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002562 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002563 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002564 *
2565 * Called by regulator drivers to register a regulator.
2566 * Returns 0 on success.
2567 */
2568struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002569 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002570 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002571{
2572 static atomic_t regulator_no = ATOMIC_INIT(0);
2573 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002574 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002575
2576 if (regulator_desc == NULL)
2577 return ERR_PTR(-EINVAL);
2578
2579 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2580 return ERR_PTR(-EINVAL);
2581
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002582 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2583 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002584 return ERR_PTR(-EINVAL);
2585
Mark Brown46fabe1e2008-09-09 16:21:18 +01002586 if (!init_data)
2587 return ERR_PTR(-EINVAL);
2588
Mark Brown476c2d82010-12-10 17:28:07 +00002589 /* Only one of each should be implemented */
2590 WARN_ON(regulator_desc->ops->get_voltage &&
2591 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002592 WARN_ON(regulator_desc->ops->set_voltage &&
2593 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002594
2595 /* If we're using selectors we must implement list_voltage. */
2596 if (regulator_desc->ops->get_voltage_sel &&
2597 !regulator_desc->ops->list_voltage) {
2598 return ERR_PTR(-EINVAL);
2599 }
Mark Browne8eef822010-12-12 14:36:17 +00002600 if (regulator_desc->ops->set_voltage_sel &&
2601 !regulator_desc->ops->list_voltage) {
2602 return ERR_PTR(-EINVAL);
2603 }
Mark Brown476c2d82010-12-10 17:28:07 +00002604
Liam Girdwood414c70c2008-04-30 15:59:04 +01002605 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2606 if (rdev == NULL)
2607 return ERR_PTR(-ENOMEM);
2608
2609 mutex_lock(&regulator_list_mutex);
2610
2611 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002612 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002613 rdev->owner = regulator_desc->owner;
2614 rdev->desc = regulator_desc;
2615 INIT_LIST_HEAD(&rdev->consumer_list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002616 INIT_LIST_HEAD(&rdev->list);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002617 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2618
Liam Girdwooda5766f12008-10-10 13:22:20 +01002619 /* preform any regulator specific init */
2620 if (init_data->regulator_init) {
2621 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002622 if (ret < 0)
2623 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002624 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002625
Liam Girdwooda5766f12008-10-10 13:22:20 +01002626 /* register with sysfs */
2627 rdev->dev.class = &regulator_class;
2628 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002629 dev_set_name(&rdev->dev, "regulator.%d",
2630 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002631 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002632 if (ret != 0) {
2633 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002634 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002635 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002636
2637 dev_set_drvdata(&rdev->dev, rdev);
2638
Mike Rapoport74f544c2008-11-25 14:53:53 +02002639 /* set regulator constraints */
2640 ret = set_machine_constraints(rdev, &init_data->constraints);
2641 if (ret < 0)
2642 goto scrub;
2643
David Brownell7ad68e22008-11-11 17:39:02 -08002644 /* add attributes supported by this regulator */
2645 ret = add_regulator_attributes(rdev);
2646 if (ret < 0)
2647 goto scrub;
2648
Mark Brown0178f3e2010-04-26 15:18:14 +01002649 if (init_data->supply_regulator) {
2650 struct regulator_dev *r;
2651 int found = 0;
2652
2653 list_for_each_entry(r, &regulator_list, list) {
2654 if (strcmp(rdev_get_name(r),
2655 init_data->supply_regulator) == 0) {
2656 found = 1;
2657 break;
2658 }
2659 }
2660
2661 if (!found) {
2662 dev_err(dev, "Failed to find supply %s\n",
2663 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002664 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002665 goto scrub;
2666 }
2667
2668 ret = set_supply(rdev, r);
2669 if (ret < 0)
2670 goto scrub;
2671 }
2672
Liam Girdwooda5766f12008-10-10 13:22:20 +01002673 /* add consumers devices */
2674 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2675 ret = set_consumer_device_supply(rdev,
2676 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002677 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002678 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002679 if (ret < 0) {
2680 dev_err(dev, "Failed to set supply %s\n",
2681 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002682 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002683 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002684 }
2685
2686 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002687
2688 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002689out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002690 mutex_unlock(&regulator_list_mutex);
2691 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002692
Jani Nikulad4033b52010-04-29 10:55:11 +03002693unset_supplies:
2694 unset_regulator_supplies(rdev);
2695
David Brownell4fca9542008-11-11 17:38:53 -08002696scrub:
2697 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002698 /* device core frees rdev */
2699 rdev = ERR_PTR(ret);
2700 goto out;
2701
David Brownell4fca9542008-11-11 17:38:53 -08002702clean:
2703 kfree(rdev);
2704 rdev = ERR_PTR(ret);
2705 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002706}
2707EXPORT_SYMBOL_GPL(regulator_register);
2708
2709/**
2710 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002711 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002712 *
2713 * Called by regulator drivers to unregister a regulator.
2714 */
2715void regulator_unregister(struct regulator_dev *rdev)
2716{
2717 if (rdev == NULL)
2718 return;
2719
2720 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002721#ifdef CONFIG_DEBUG_FS
2722 debugfs_remove_recursive(rdev->debugfs);
2723#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002724 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002725 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002726 list_del(&rdev->list);
2727 if (rdev->supply)
Mark Brown3801b862011-06-09 16:22:22 +01002728 regulator_put(rdev->supply);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002729 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002730 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002731 mutex_unlock(&regulator_list_mutex);
2732}
2733EXPORT_SYMBOL_GPL(regulator_unregister);
2734
2735/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002736 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002737 * @state: system suspend state
2738 *
2739 * Configure each regulator with it's suspend operating parameters for state.
2740 * This will usually be called by machine suspend code prior to supending.
2741 */
2742int regulator_suspend_prepare(suspend_state_t state)
2743{
2744 struct regulator_dev *rdev;
2745 int ret = 0;
2746
2747 /* ON is handled by regulator active state */
2748 if (state == PM_SUSPEND_ON)
2749 return -EINVAL;
2750
2751 mutex_lock(&regulator_list_mutex);
2752 list_for_each_entry(rdev, &regulator_list, list) {
2753
2754 mutex_lock(&rdev->mutex);
2755 ret = suspend_prepare(rdev, state);
2756 mutex_unlock(&rdev->mutex);
2757
2758 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002759 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002760 goto out;
2761 }
2762 }
2763out:
2764 mutex_unlock(&regulator_list_mutex);
2765 return ret;
2766}
2767EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2768
2769/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002770 * regulator_suspend_finish - resume regulators from system wide suspend
2771 *
2772 * Turn on regulators that might be turned off by regulator_suspend_prepare
2773 * and that should be turned on according to the regulators properties.
2774 */
2775int regulator_suspend_finish(void)
2776{
2777 struct regulator_dev *rdev;
2778 int ret = 0, error;
2779
2780 mutex_lock(&regulator_list_mutex);
2781 list_for_each_entry(rdev, &regulator_list, list) {
2782 struct regulator_ops *ops = rdev->desc->ops;
2783
2784 mutex_lock(&rdev->mutex);
2785 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2786 ops->enable) {
2787 error = ops->enable(rdev);
2788 if (error)
2789 ret = error;
2790 } else {
2791 if (!has_full_constraints)
2792 goto unlock;
2793 if (!ops->disable)
2794 goto unlock;
2795 if (ops->is_enabled && !ops->is_enabled(rdev))
2796 goto unlock;
2797
2798 error = ops->disable(rdev);
2799 if (error)
2800 ret = error;
2801 }
2802unlock:
2803 mutex_unlock(&rdev->mutex);
2804 }
2805 mutex_unlock(&regulator_list_mutex);
2806 return ret;
2807}
2808EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2809
2810/**
Mark Brownca725562009-03-16 19:36:34 +00002811 * regulator_has_full_constraints - the system has fully specified constraints
2812 *
2813 * Calling this function will cause the regulator API to disable all
2814 * regulators which have a zero use count and don't have an always_on
2815 * constraint in a late_initcall.
2816 *
2817 * The intention is that this will become the default behaviour in a
2818 * future kernel release so users are encouraged to use this facility
2819 * now.
2820 */
2821void regulator_has_full_constraints(void)
2822{
2823 has_full_constraints = 1;
2824}
2825EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2826
2827/**
Mark Brown688fe992010-10-05 19:18:32 -07002828 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2829 *
2830 * Calling this function will cause the regulator API to provide a
2831 * dummy regulator to consumers if no physical regulator is found,
2832 * allowing most consumers to proceed as though a regulator were
2833 * configured. This allows systems such as those with software
2834 * controllable regulators for the CPU core only to be brought up more
2835 * readily.
2836 */
2837void regulator_use_dummy_regulator(void)
2838{
2839 board_wants_dummy_regulator = true;
2840}
2841EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2842
2843/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002844 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002845 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002846 *
2847 * Get rdev regulator driver private data. This call can be used in the
2848 * regulator driver context.
2849 */
2850void *rdev_get_drvdata(struct regulator_dev *rdev)
2851{
2852 return rdev->reg_data;
2853}
2854EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2855
2856/**
2857 * regulator_get_drvdata - get regulator driver data
2858 * @regulator: regulator
2859 *
2860 * Get regulator driver private data. This call can be used in the consumer
2861 * driver context when non API regulator specific functions need to be called.
2862 */
2863void *regulator_get_drvdata(struct regulator *regulator)
2864{
2865 return regulator->rdev->reg_data;
2866}
2867EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2868
2869/**
2870 * regulator_set_drvdata - set regulator driver data
2871 * @regulator: regulator
2872 * @data: data
2873 */
2874void regulator_set_drvdata(struct regulator *regulator, void *data)
2875{
2876 regulator->rdev->reg_data = data;
2877}
2878EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2879
2880/**
2881 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002882 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002883 */
2884int rdev_get_id(struct regulator_dev *rdev)
2885{
2886 return rdev->desc->id;
2887}
2888EXPORT_SYMBOL_GPL(rdev_get_id);
2889
Liam Girdwooda5766f12008-10-10 13:22:20 +01002890struct device *rdev_get_dev(struct regulator_dev *rdev)
2891{
2892 return &rdev->dev;
2893}
2894EXPORT_SYMBOL_GPL(rdev_get_dev);
2895
2896void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2897{
2898 return reg_init_data->driver_data;
2899}
2900EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2901
Liam Girdwood414c70c2008-04-30 15:59:04 +01002902static int __init regulator_init(void)
2903{
Mark Brown34abbd62010-02-12 10:18:08 +00002904 int ret;
2905
Mark Brown34abbd62010-02-12 10:18:08 +00002906 ret = class_register(&regulator_class);
2907
Mark Brown1130e5b2010-12-21 23:49:31 +00002908#ifdef CONFIG_DEBUG_FS
2909 debugfs_root = debugfs_create_dir("regulator", NULL);
2910 if (IS_ERR(debugfs_root) || !debugfs_root) {
2911 pr_warn("regulator: Failed to create debugfs directory\n");
2912 debugfs_root = NULL;
2913 }
2914#endif
2915
Mark Brown34abbd62010-02-12 10:18:08 +00002916 regulator_dummy_init();
2917
2918 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002919}
2920
2921/* init early to allow our consumers to complete system booting */
2922core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002923
2924static int __init regulator_init_complete(void)
2925{
2926 struct regulator_dev *rdev;
2927 struct regulator_ops *ops;
2928 struct regulation_constraints *c;
2929 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002930
2931 mutex_lock(&regulator_list_mutex);
2932
2933 /* If we have a full configuration then disable any regulators
2934 * which are not in use or always_on. This will become the
2935 * default behaviour in the future.
2936 */
2937 list_for_each_entry(rdev, &regulator_list, list) {
2938 ops = rdev->desc->ops;
2939 c = rdev->constraints;
2940
Mark Brownf25e0b42009-08-03 18:49:55 +01002941 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002942 continue;
2943
2944 mutex_lock(&rdev->mutex);
2945
2946 if (rdev->use_count)
2947 goto unlock;
2948
2949 /* If we can't read the status assume it's on. */
2950 if (ops->is_enabled)
2951 enabled = ops->is_enabled(rdev);
2952 else
2953 enabled = 1;
2954
2955 if (!enabled)
2956 goto unlock;
2957
2958 if (has_full_constraints) {
2959 /* We log since this may kill the system if it
2960 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002961 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002962 ret = ops->disable(rdev);
2963 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002964 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002965 }
2966 } else {
2967 /* The intention is that in future we will
2968 * assume that full constraints are provided
2969 * so warn even if we aren't going to do
2970 * anything here.
2971 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002972 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002973 }
2974
2975unlock:
2976 mutex_unlock(&rdev->mutex);
2977 }
2978
2979 mutex_unlock(&regulator_list_mutex);
2980
2981 return 0;
2982}
2983late_initcall(regulator_init_complete);