blob: 9493f6111a38d33b3cd8fc0542908b180416b05a [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>
Liam Girdwood414c70c2008-04-30 15:59:04 +010023#include <linux/err.h>
24#include <linux/mutex.h>
25#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000026#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010027#include <linux/regulator/consumer.h>
28#include <linux/regulator/driver.h>
29#include <linux/regulator/machine.h>
30
Mark Brown02fa3ec2010-11-10 14:38:30 +000031#define CREATE_TRACE_POINTS
32#include <trace/events/regulator.h>
33
Mark Brown34abbd62010-02-12 10:18:08 +000034#include "dummy.h"
35
Joe Perches5da84fd2010-11-30 05:53:48 -080036#define rdev_err(rdev, fmt, ...) \
37 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
38#define rdev_warn(rdev, fmt, ...) \
39 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
40#define rdev_info(rdev, fmt, ...) \
41 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
42#define rdev_dbg(rdev, fmt, ...) \
43 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
44
Liam Girdwood414c70c2008-04-30 15:59:04 +010045static DEFINE_MUTEX(regulator_list_mutex);
46static LIST_HEAD(regulator_list);
47static LIST_HEAD(regulator_map_list);
Mark Brown21cf8912010-12-21 23:30:07 +000048static bool has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070049static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010050
Mark Brown1130e5b2010-12-21 23:49:31 +000051#ifdef CONFIG_DEBUG_FS
52static struct dentry *debugfs_root;
53#endif
54
Mark Brown8dc53902008-12-31 12:52:40 +000055/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010056 * struct regulator_map
57 *
58 * Used to provide symbolic supply names to devices.
59 */
60struct regulator_map {
61 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010062 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010063 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010064 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010065};
66
Liam Girdwood414c70c2008-04-30 15:59:04 +010067/*
68 * struct regulator
69 *
70 * One for each consumer device.
71 */
72struct regulator {
73 struct device *dev;
74 struct list_head list;
75 int uA_load;
76 int min_uV;
77 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010078 char *supply_name;
79 struct device_attribute dev_attr;
80 struct regulator_dev *rdev;
81};
82
83static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050084static int _regulator_disable(struct regulator_dev *rdev,
85 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010086static int _regulator_get_voltage(struct regulator_dev *rdev);
87static int _regulator_get_current_limit(struct regulator_dev *rdev);
88static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
89static void _notifier_call_chain(struct regulator_dev *rdev,
90 unsigned long event, void *data);
Mark Brown75790252010-12-12 14:25:50 +000091static int _regulator_do_set_voltage(struct regulator_dev *rdev,
92 int min_uV, int max_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +010093
Mark Brown1083c392009-10-22 16:31:32 +010094static const char *rdev_get_name(struct regulator_dev *rdev)
95{
96 if (rdev->constraints && rdev->constraints->name)
97 return rdev->constraints->name;
98 else if (rdev->desc->name)
99 return rdev->desc->name;
100 else
101 return "";
102}
103
Liam Girdwood414c70c2008-04-30 15:59:04 +0100104/* gets the regulator for a given consumer device */
105static struct regulator *get_device_regulator(struct device *dev)
106{
107 struct regulator *regulator = NULL;
108 struct regulator_dev *rdev;
109
110 mutex_lock(&regulator_list_mutex);
111 list_for_each_entry(rdev, &regulator_list, list) {
112 mutex_lock(&rdev->mutex);
113 list_for_each_entry(regulator, &rdev->consumer_list, list) {
114 if (regulator->dev == dev) {
115 mutex_unlock(&rdev->mutex);
116 mutex_unlock(&regulator_list_mutex);
117 return regulator;
118 }
119 }
120 mutex_unlock(&rdev->mutex);
121 }
122 mutex_unlock(&regulator_list_mutex);
123 return NULL;
124}
125
126/* Platform voltage constraint check */
127static int regulator_check_voltage(struct regulator_dev *rdev,
128 int *min_uV, int *max_uV)
129{
130 BUG_ON(*min_uV > *max_uV);
131
132 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800133 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100134 return -ENODEV;
135 }
136 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800137 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100138 return -EPERM;
139 }
140
141 if (*max_uV > rdev->constraints->max_uV)
142 *max_uV = rdev->constraints->max_uV;
143 if (*min_uV < rdev->constraints->min_uV)
144 *min_uV = rdev->constraints->min_uV;
145
146 if (*min_uV > *max_uV)
147 return -EINVAL;
148
149 return 0;
150}
151
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100152/* Make sure we select a voltage that suits the needs of all
153 * regulator consumers
154 */
155static int regulator_check_consumers(struct regulator_dev *rdev,
156 int *min_uV, int *max_uV)
157{
158 struct regulator *regulator;
159
160 list_for_each_entry(regulator, &rdev->consumer_list, list) {
Mark Brown4aa922c2011-05-14 13:42:34 -0700161 /*
162 * Assume consumers that didn't say anything are OK
163 * with anything in the constraint range.
164 */
165 if (!regulator->min_uV && !regulator->max_uV)
166 continue;
167
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100168 if (*max_uV > regulator->max_uV)
169 *max_uV = regulator->max_uV;
170 if (*min_uV < regulator->min_uV)
171 *min_uV = regulator->min_uV;
172 }
173
174 if (*min_uV > *max_uV)
175 return -EINVAL;
176
177 return 0;
178}
179
Liam Girdwood414c70c2008-04-30 15:59:04 +0100180/* current constraint check */
181static int regulator_check_current_limit(struct regulator_dev *rdev,
182 int *min_uA, int *max_uA)
183{
184 BUG_ON(*min_uA > *max_uA);
185
186 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800187 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100188 return -ENODEV;
189 }
190 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800191 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100192 return -EPERM;
193 }
194
195 if (*max_uA > rdev->constraints->max_uA)
196 *max_uA = rdev->constraints->max_uA;
197 if (*min_uA < rdev->constraints->min_uA)
198 *min_uA = rdev->constraints->min_uA;
199
200 if (*min_uA > *max_uA)
201 return -EINVAL;
202
203 return 0;
204}
205
206/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900207static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100208{
Mark Brown2c608232011-03-30 06:29:12 +0900209 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800210 case REGULATOR_MODE_FAST:
211 case REGULATOR_MODE_NORMAL:
212 case REGULATOR_MODE_IDLE:
213 case REGULATOR_MODE_STANDBY:
214 break;
215 default:
216 return -EINVAL;
217 }
218
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800220 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100221 return -ENODEV;
222 }
223 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800224 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100225 return -EPERM;
226 }
Mark Brown2c608232011-03-30 06:29:12 +0900227
228 /* The modes are bitmasks, the most power hungry modes having
229 * the lowest values. If the requested mode isn't supported
230 * try higher modes. */
231 while (*mode) {
232 if (rdev->constraints->valid_modes_mask & *mode)
233 return 0;
234 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235 }
Mark Brown2c608232011-03-30 06:29:12 +0900236
237 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238}
239
240/* dynamic regulator mode switching constraint check */
241static int regulator_check_drms(struct regulator_dev *rdev)
242{
243 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800244 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245 return -ENODEV;
246 }
247 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800248 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249 return -EPERM;
250 }
251 return 0;
252}
253
254static ssize_t device_requested_uA_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
257 struct regulator *regulator;
258
259 regulator = get_device_regulator(dev);
260 if (regulator == NULL)
261 return 0;
262
263 return sprintf(buf, "%d\n", regulator->uA_load);
264}
265
266static ssize_t regulator_uV_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
268{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100269 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270 ssize_t ret;
271
272 mutex_lock(&rdev->mutex);
273 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
274 mutex_unlock(&rdev->mutex);
275
276 return ret;
277}
David Brownell7ad68e22008-11-11 17:39:02 -0800278static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100279
280static ssize_t regulator_uA_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100283 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284
285 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
286}
David Brownell7ad68e22008-11-11 17:39:02 -0800287static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100288
Mark Brownbc558a62008-10-10 15:33:20 +0100289static ssize_t regulator_name_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
292 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100293
Mark Brown1083c392009-10-22 16:31:32 +0100294 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100295}
296
David Brownell4fca9542008-11-11 17:38:53 -0800297static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100298{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100299 switch (mode) {
300 case REGULATOR_MODE_FAST:
301 return sprintf(buf, "fast\n");
302 case REGULATOR_MODE_NORMAL:
303 return sprintf(buf, "normal\n");
304 case REGULATOR_MODE_IDLE:
305 return sprintf(buf, "idle\n");
306 case REGULATOR_MODE_STANDBY:
307 return sprintf(buf, "standby\n");
308 }
309 return sprintf(buf, "unknown\n");
310}
311
David Brownell4fca9542008-11-11 17:38:53 -0800312static ssize_t regulator_opmode_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100314{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100315 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100316
David Brownell4fca9542008-11-11 17:38:53 -0800317 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
318}
David Brownell7ad68e22008-11-11 17:39:02 -0800319static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800320
321static ssize_t regulator_print_state(char *buf, int state)
322{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100323 if (state > 0)
324 return sprintf(buf, "enabled\n");
325 else if (state == 0)
326 return sprintf(buf, "disabled\n");
327 else
328 return sprintf(buf, "unknown\n");
329}
330
David Brownell4fca9542008-11-11 17:38:53 -0800331static ssize_t regulator_state_show(struct device *dev,
332 struct device_attribute *attr, char *buf)
333{
334 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100335 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800336
Mark Brown93325462009-08-03 18:49:56 +0100337 mutex_lock(&rdev->mutex);
338 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
339 mutex_unlock(&rdev->mutex);
340
341 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800342}
David Brownell7ad68e22008-11-11 17:39:02 -0800343static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800344
David Brownell853116a2009-01-14 23:03:17 -0800345static ssize_t regulator_status_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
348 struct regulator_dev *rdev = dev_get_drvdata(dev);
349 int status;
350 char *label;
351
352 status = rdev->desc->ops->get_status(rdev);
353 if (status < 0)
354 return status;
355
356 switch (status) {
357 case REGULATOR_STATUS_OFF:
358 label = "off";
359 break;
360 case REGULATOR_STATUS_ON:
361 label = "on";
362 break;
363 case REGULATOR_STATUS_ERROR:
364 label = "error";
365 break;
366 case REGULATOR_STATUS_FAST:
367 label = "fast";
368 break;
369 case REGULATOR_STATUS_NORMAL:
370 label = "normal";
371 break;
372 case REGULATOR_STATUS_IDLE:
373 label = "idle";
374 break;
375 case REGULATOR_STATUS_STANDBY:
376 label = "standby";
377 break;
378 default:
379 return -ERANGE;
380 }
381
382 return sprintf(buf, "%s\n", label);
383}
384static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
385
Liam Girdwood414c70c2008-04-30 15:59:04 +0100386static ssize_t regulator_min_uA_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100389 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100390
391 if (!rdev->constraints)
392 return sprintf(buf, "constraint not defined\n");
393
394 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
395}
David Brownell7ad68e22008-11-11 17:39:02 -0800396static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100397
398static ssize_t regulator_max_uA_show(struct device *dev,
399 struct device_attribute *attr, char *buf)
400{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100401 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100402
403 if (!rdev->constraints)
404 return sprintf(buf, "constraint not defined\n");
405
406 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
407}
David Brownell7ad68e22008-11-11 17:39:02 -0800408static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100409
410static ssize_t regulator_min_uV_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100413 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414
415 if (!rdev->constraints)
416 return sprintf(buf, "constraint not defined\n");
417
418 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
419}
David Brownell7ad68e22008-11-11 17:39:02 -0800420static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100421
422static ssize_t regulator_max_uV_show(struct device *dev,
423 struct device_attribute *attr, char *buf)
424{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100425 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427 if (!rdev->constraints)
428 return sprintf(buf, "constraint not defined\n");
429
430 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
431}
David Brownell7ad68e22008-11-11 17:39:02 -0800432static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
434static ssize_t regulator_total_uA_show(struct device *dev,
435 struct device_attribute *attr, char *buf)
436{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100437 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438 struct regulator *regulator;
439 int uA = 0;
440
441 mutex_lock(&rdev->mutex);
442 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100443 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100444 mutex_unlock(&rdev->mutex);
445 return sprintf(buf, "%d\n", uA);
446}
David Brownell7ad68e22008-11-11 17:39:02 -0800447static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_num_users_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453 return sprintf(buf, "%d\n", rdev->use_count);
454}
455
456static ssize_t regulator_type_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100459 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
461 switch (rdev->desc->type) {
462 case REGULATOR_VOLTAGE:
463 return sprintf(buf, "voltage\n");
464 case REGULATOR_CURRENT:
465 return sprintf(buf, "current\n");
466 }
467 return sprintf(buf, "unknown\n");
468}
469
470static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
476}
David Brownell7ad68e22008-11-11 17:39:02 -0800477static DEVICE_ATTR(suspend_mem_microvolts, 0444,
478 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
480static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
482{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100483 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
486}
David Brownell7ad68e22008-11-11 17:39:02 -0800487static DEVICE_ATTR(suspend_disk_microvolts, 0444,
488 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100489
490static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
491 struct device_attribute *attr, char *buf)
492{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100493 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
496}
David Brownell7ad68e22008-11-11 17:39:02 -0800497static DEVICE_ATTR(suspend_standby_microvolts, 0444,
498 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100503 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
David Brownell4fca9542008-11-11 17:38:53 -0800505 return regulator_print_opmode(buf,
506 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507}
David Brownell7ad68e22008-11-11 17:39:02 -0800508static DEVICE_ATTR(suspend_mem_mode, 0444,
509 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510
511static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100514 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
David Brownell4fca9542008-11-11 17:38:53 -0800516 return regulator_print_opmode(buf,
517 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518}
David Brownell7ad68e22008-11-11 17:39:02 -0800519static DEVICE_ATTR(suspend_disk_mode, 0444,
520 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521
522static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100525 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100526
David Brownell4fca9542008-11-11 17:38:53 -0800527 return regulator_print_opmode(buf,
528 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529}
David Brownell7ad68e22008-11-11 17:39:02 -0800530static DEVICE_ATTR(suspend_standby_mode, 0444,
531 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532
533static ssize_t regulator_suspend_mem_state_show(struct device *dev,
534 struct device_attribute *attr, char *buf)
535{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100536 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537
David Brownell4fca9542008-11-11 17:38:53 -0800538 return regulator_print_state(buf,
539 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540}
David Brownell7ad68e22008-11-11 17:39:02 -0800541static DEVICE_ATTR(suspend_mem_state, 0444,
542 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100543
544static ssize_t regulator_suspend_disk_state_show(struct device *dev,
545 struct device_attribute *attr, char *buf)
546{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100547 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100548
David Brownell4fca9542008-11-11 17:38:53 -0800549 return regulator_print_state(buf,
550 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551}
David Brownell7ad68e22008-11-11 17:39:02 -0800552static DEVICE_ATTR(suspend_disk_state, 0444,
553 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100554
555static ssize_t regulator_suspend_standby_state_show(struct device *dev,
556 struct device_attribute *attr, char *buf)
557{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100558 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100559
David Brownell4fca9542008-11-11 17:38:53 -0800560 return regulator_print_state(buf,
561 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100562}
David Brownell7ad68e22008-11-11 17:39:02 -0800563static DEVICE_ATTR(suspend_standby_state, 0444,
564 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100565
David Brownell7ad68e22008-11-11 17:39:02 -0800566
567/*
568 * These are the only attributes are present for all regulators.
569 * Other attributes are a function of regulator functionality.
570 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100571static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100572 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100573 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
574 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575 __ATTR_NULL,
576};
577
578static void regulator_dev_release(struct device *dev)
579{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100580 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100581 kfree(rdev);
582}
583
584static struct class regulator_class = {
585 .name = "regulator",
586 .dev_release = regulator_dev_release,
587 .dev_attrs = regulator_dev_attrs,
588};
589
590/* Calculate the new optimum regulator operating mode based on the new total
591 * consumer load. All locks held by caller */
592static void drms_uA_update(struct regulator_dev *rdev)
593{
594 struct regulator *sibling;
595 int current_uA = 0, output_uV, input_uV, err;
596 unsigned int mode;
597
598 err = regulator_check_drms(rdev);
599 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000600 (!rdev->desc->ops->get_voltage &&
601 !rdev->desc->ops->get_voltage_sel) ||
602 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300603 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100604
605 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000606 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100607 if (output_uV <= 0)
608 return;
609
610 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000611 input_uV = 0;
612 if (rdev->supply)
613 input_uV = _regulator_get_voltage(rdev);
614 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615 input_uV = rdev->constraints->input_uV;
616 if (input_uV <= 0)
617 return;
618
619 /* calc total requested load */
620 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100621 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622
623 /* now get the optimum mode for our new total regulator load */
624 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
625 output_uV, current_uA);
626
627 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900628 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100629 if (err == 0)
630 rdev->desc->ops->set_mode(rdev, mode);
631}
632
633static int suspend_set_state(struct regulator_dev *rdev,
634 struct regulator_state *rstate)
635{
636 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100637 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638
Mark Brown638f85c2009-10-22 16:31:33 +0100639 can_set_state = rdev->desc->ops->set_suspend_enable &&
640 rdev->desc->ops->set_suspend_disable;
641
642 /* If we have no suspend mode configration don't set anything;
643 * only warn if the driver actually makes the suspend mode
644 * configurable.
645 */
646 if (!rstate->enabled && !rstate->disabled) {
647 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800648 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100649 return 0;
650 }
651
652 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800653 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100654 return -EINVAL;
655 }
656
657 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800658 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100659 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100660 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661
662 if (rstate->enabled)
663 ret = rdev->desc->ops->set_suspend_enable(rdev);
664 else
665 ret = rdev->desc->ops->set_suspend_disable(rdev);
666 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800667 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 return ret;
669 }
670
671 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
672 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
673 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800674 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100675 return ret;
676 }
677 }
678
679 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
680 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
681 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800682 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683 return ret;
684 }
685 }
686 return ret;
687}
688
689/* locks held by caller */
690static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
691{
692 if (!rdev->constraints)
693 return -EINVAL;
694
695 switch (state) {
696 case PM_SUSPEND_STANDBY:
697 return suspend_set_state(rdev,
698 &rdev->constraints->state_standby);
699 case PM_SUSPEND_MEM:
700 return suspend_set_state(rdev,
701 &rdev->constraints->state_mem);
702 case PM_SUSPEND_MAX:
703 return suspend_set_state(rdev,
704 &rdev->constraints->state_disk);
705 default:
706 return -EINVAL;
707 }
708}
709
710static void print_constraints(struct regulator_dev *rdev)
711{
712 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000713 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100714 int count = 0;
715 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100716
Mark Brown8f031b42009-10-22 16:31:31 +0100717 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100718 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100719 count += sprintf(buf + count, "%d mV ",
720 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721 else
Mark Brown8f031b42009-10-22 16:31:31 +0100722 count += sprintf(buf + count, "%d <--> %d mV ",
723 constraints->min_uV / 1000,
724 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100725 }
Mark Brown8f031b42009-10-22 16:31:31 +0100726
727 if (!constraints->min_uV ||
728 constraints->min_uV != constraints->max_uV) {
729 ret = _regulator_get_voltage(rdev);
730 if (ret > 0)
731 count += sprintf(buf + count, "at %d mV ", ret / 1000);
732 }
733
Mark Brownbf5892a2011-05-08 22:13:37 +0100734 if (constraints->uV_offset)
735 count += sprintf(buf, "%dmV offset ",
736 constraints->uV_offset / 1000);
737
Mark Brown8f031b42009-10-22 16:31:31 +0100738 if (constraints->min_uA && constraints->max_uA) {
739 if (constraints->min_uA == constraints->max_uA)
740 count += sprintf(buf + count, "%d mA ",
741 constraints->min_uA / 1000);
742 else
743 count += sprintf(buf + count, "%d <--> %d mA ",
744 constraints->min_uA / 1000,
745 constraints->max_uA / 1000);
746 }
747
748 if (!constraints->min_uA ||
749 constraints->min_uA != constraints->max_uA) {
750 ret = _regulator_get_current_limit(rdev);
751 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400752 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100753 }
754
Liam Girdwood414c70c2008-04-30 15:59:04 +0100755 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
756 count += sprintf(buf + count, "fast ");
757 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
758 count += sprintf(buf + count, "normal ");
759 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
760 count += sprintf(buf + count, "idle ");
761 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
762 count += sprintf(buf + count, "standby");
763
Mark Brown13ce29f2010-12-17 16:04:12 +0000764 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100765}
766
Mark Browne79055d2009-10-19 15:53:50 +0100767static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100768 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100769{
770 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100771 int ret;
772
773 /* do we need to apply the constraint voltage */
774 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000775 rdev->constraints->min_uV == rdev->constraints->max_uV) {
776 ret = _regulator_do_set_voltage(rdev,
777 rdev->constraints->min_uV,
778 rdev->constraints->max_uV);
779 if (ret < 0) {
780 rdev_err(rdev, "failed to apply %duV constraint\n",
781 rdev->constraints->min_uV);
782 rdev->constraints = NULL;
783 return ret;
784 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100785 }
Mark Browne79055d2009-10-19 15:53:50 +0100786
787 /* constrain machine-level voltage specs to fit
788 * the actual range supported by this regulator.
789 */
790 if (ops->list_voltage && rdev->desc->n_voltages) {
791 int count = rdev->desc->n_voltages;
792 int i;
793 int min_uV = INT_MAX;
794 int max_uV = INT_MIN;
795 int cmin = constraints->min_uV;
796 int cmax = constraints->max_uV;
797
798 /* it's safe to autoconfigure fixed-voltage supplies
799 and the constraints are used by list_voltage. */
800 if (count == 1 && !cmin) {
801 cmin = 1;
802 cmax = INT_MAX;
803 constraints->min_uV = cmin;
804 constraints->max_uV = cmax;
805 }
806
807 /* voltage constraints are optional */
808 if ((cmin == 0) && (cmax == 0))
809 return 0;
810
811 /* else require explicit machine-level constraints */
812 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800813 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100814 return -EINVAL;
815 }
816
817 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
818 for (i = 0; i < count; i++) {
819 int value;
820
821 value = ops->list_voltage(rdev, i);
822 if (value <= 0)
823 continue;
824
825 /* maybe adjust [min_uV..max_uV] */
826 if (value >= cmin && value < min_uV)
827 min_uV = value;
828 if (value <= cmax && value > max_uV)
829 max_uV = value;
830 }
831
832 /* final: [min_uV..max_uV] valid iff constraints valid */
833 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800834 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100835 return -EINVAL;
836 }
837
838 /* use regulator's subset of machine constraints */
839 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800840 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
841 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100842 constraints->min_uV = min_uV;
843 }
844 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800845 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
846 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100847 constraints->max_uV = max_uV;
848 }
849 }
850
851 return 0;
852}
853
Liam Girdwooda5766f12008-10-10 13:22:20 +0100854/**
855 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000856 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000857 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100858 *
859 * Allows platform initialisation code to define and constrain
860 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
861 * Constraints *must* be set by platform code in order for some
862 * regulator operations to proceed i.e. set_voltage, set_current_limit,
863 * set_mode.
864 */
865static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000866 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100867{
868 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100869 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100870
Mark Brownf8c12fe2010-11-29 15:55:17 +0000871 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
872 GFP_KERNEL);
873 if (!rdev->constraints)
874 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100875
Mark Brownf8c12fe2010-11-29 15:55:17 +0000876 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100877 if (ret != 0)
878 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800879
Liam Girdwooda5766f12008-10-10 13:22:20 +0100880 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100881 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000882 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100883 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800884 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100885 rdev->constraints = NULL;
886 goto out;
887 }
888 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100889
Mark Browna3084662009-02-26 19:24:19 +0000890 if (constraints->initial_mode) {
891 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800892 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000893 ret = -EINVAL;
894 goto out;
895 }
896
Mark Brownf8c12fe2010-11-29 15:55:17 +0000897 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000898 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800899 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000900 goto out;
901 }
902 }
903
Mark Browncacf90f2009-03-02 16:32:46 +0000904 /* If the constraints say the regulator should be on at this point
905 * and we have control then make sure it is enabled.
906 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000907 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
908 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100909 ret = ops->enable(rdev);
910 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800911 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100912 rdev->constraints = NULL;
913 goto out;
914 }
915 }
916
Liam Girdwooda5766f12008-10-10 13:22:20 +0100917 print_constraints(rdev);
918out:
919 return ret;
920}
921
922/**
923 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000924 * @rdev: regulator name
925 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100926 *
927 * Called by platform initialisation code to set the supply regulator for this
928 * regulator. This ensures that a regulators supply will also be enabled by the
929 * core if it's child is enabled.
930 */
931static int set_supply(struct regulator_dev *rdev,
932 struct regulator_dev *supply_rdev)
933{
934 int err;
935
936 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
937 "supply");
938 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800939 rdev_err(rdev, "could not add device link %s err %d\n",
940 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100941 goto out;
942 }
943 rdev->supply = supply_rdev;
944 list_add(&rdev->slist, &supply_rdev->supply_list);
945out:
946 return err;
947}
948
949/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800950 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000951 * @rdev: regulator source
952 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100953 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000954 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100955 *
956 * Allows platform initialisation code to map physical regulator
957 * sources to symbolic names for supplies for use by devices. Devices
958 * should use these symbolic names to request regulators, avoiding the
959 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100960 *
961 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962 */
963static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100964 struct device *consumer_dev, const char *consumer_dev_name,
965 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100966{
967 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100968 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100969
Mark Brown40f92442009-06-17 17:56:39 +0100970 if (consumer_dev && consumer_dev_name)
971 return -EINVAL;
972
973 if (!consumer_dev_name && consumer_dev)
974 consumer_dev_name = dev_name(consumer_dev);
975
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976 if (supply == NULL)
977 return -EINVAL;
978
Mark Brown9ed20992009-07-21 16:00:26 +0100979 if (consumer_dev_name != NULL)
980 has_dev = 1;
981 else
982 has_dev = 0;
983
David Brownell6001e132008-12-31 12:54:19 +0000984 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300985 if (node->dev_name && consumer_dev_name) {
986 if (strcmp(node->dev_name, consumer_dev_name) != 0)
987 continue;
988 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000989 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300990 }
991
David Brownell6001e132008-12-31 12:54:19 +0000992 if (strcmp(node->supply, supply) != 0)
993 continue;
994
995 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800996 dev_name(&node->regulator->dev),
997 node->regulator->desc->name,
998 supply,
999 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +00001000 return -EBUSY;
1001 }
1002
Mark Brown9ed20992009-07-21 16:00:26 +01001003 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +01001004 if (node == NULL)
1005 return -ENOMEM;
1006
1007 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001008 node->supply = supply;
1009
Mark Brown9ed20992009-07-21 16:00:26 +01001010 if (has_dev) {
1011 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1012 if (node->dev_name == NULL) {
1013 kfree(node);
1014 return -ENOMEM;
1015 }
Mark Brown40f92442009-06-17 17:56:39 +01001016 }
1017
Liam Girdwooda5766f12008-10-10 13:22:20 +01001018 list_add(&node->list, &regulator_map_list);
1019 return 0;
1020}
1021
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001022static void unset_regulator_supplies(struct regulator_dev *rdev)
1023{
1024 struct regulator_map *node, *n;
1025
1026 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1027 if (rdev == node->regulator) {
1028 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001029 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001030 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001031 }
1032 }
1033}
1034
Liam Girdwood414c70c2008-04-30 15:59:04 +01001035#define REG_STR_SIZE 32
1036
1037static struct regulator *create_regulator(struct regulator_dev *rdev,
1038 struct device *dev,
1039 const char *supply_name)
1040{
1041 struct regulator *regulator;
1042 char buf[REG_STR_SIZE];
1043 int err, size;
1044
1045 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1046 if (regulator == NULL)
1047 return NULL;
1048
1049 mutex_lock(&rdev->mutex);
1050 regulator->rdev = rdev;
1051 list_add(&regulator->list, &rdev->consumer_list);
1052
1053 if (dev) {
1054 /* create a 'requested_microamps_name' sysfs entry */
1055 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1056 supply_name);
1057 if (size >= REG_STR_SIZE)
1058 goto overflow_err;
1059
1060 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001061 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001062 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1063 if (regulator->dev_attr.attr.name == NULL)
1064 goto attr_name_err;
1065
Liam Girdwood414c70c2008-04-30 15:59:04 +01001066 regulator->dev_attr.attr.mode = 0444;
1067 regulator->dev_attr.show = device_requested_uA_show;
1068 err = device_create_file(dev, &regulator->dev_attr);
1069 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001070 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001071 goto attr_name_err;
1072 }
1073
1074 /* also add a link to the device sysfs entry */
1075 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1076 dev->kobj.name, supply_name);
1077 if (size >= REG_STR_SIZE)
1078 goto attr_err;
1079
1080 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1081 if (regulator->supply_name == NULL)
1082 goto attr_err;
1083
1084 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1085 buf);
1086 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001087 rdev_warn(rdev, "could not add device link %s err %d\n",
1088 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089 goto link_name_err;
1090 }
1091 }
1092 mutex_unlock(&rdev->mutex);
1093 return regulator;
1094link_name_err:
1095 kfree(regulator->supply_name);
1096attr_err:
1097 device_remove_file(regulator->dev, &regulator->dev_attr);
1098attr_name_err:
1099 kfree(regulator->dev_attr.attr.name);
1100overflow_err:
1101 list_del(&regulator->list);
1102 kfree(regulator);
1103 mutex_unlock(&rdev->mutex);
1104 return NULL;
1105}
1106
Mark Brown31aae2b2009-12-21 12:21:52 +00001107static int _regulator_get_enable_time(struct regulator_dev *rdev)
1108{
1109 if (!rdev->desc->ops->enable_time)
1110 return 0;
1111 return rdev->desc->ops->enable_time(rdev);
1112}
1113
Mark Brown5ffbd132009-07-21 16:00:23 +01001114/* Internal regulator request function */
1115static struct regulator *_regulator_get(struct device *dev, const char *id,
1116 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001117{
1118 struct regulator_dev *rdev;
1119 struct regulator_map *map;
1120 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001121 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001122 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001123
1124 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001125 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001126 return regulator;
1127 }
1128
Mark Brown40f92442009-06-17 17:56:39 +01001129 if (dev)
1130 devname = dev_name(dev);
1131
Liam Girdwood414c70c2008-04-30 15:59:04 +01001132 mutex_lock(&regulator_list_mutex);
1133
1134 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001135 /* If the mapping has a device set up it must match */
1136 if (map->dev_name &&
1137 (!devname || strcmp(map->dev_name, devname)))
1138 continue;
1139
1140 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001141 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001142 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001143 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001144 }
Mark Brown34abbd62010-02-12 10:18:08 +00001145
Mark Brown688fe992010-10-05 19:18:32 -07001146 if (board_wants_dummy_regulator) {
1147 rdev = dummy_regulator_rdev;
1148 goto found;
1149 }
1150
Mark Brown34abbd62010-02-12 10:18:08 +00001151#ifdef CONFIG_REGULATOR_DUMMY
1152 if (!devname)
1153 devname = "deviceless";
1154
1155 /* If the board didn't flag that it was fully constrained then
1156 * substitute in a dummy regulator so consumers can continue.
1157 */
1158 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001159 pr_warn("%s supply %s not found, using dummy regulator\n",
1160 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001161 rdev = dummy_regulator_rdev;
1162 goto found;
1163 }
1164#endif
1165
Liam Girdwood414c70c2008-04-30 15:59:04 +01001166 mutex_unlock(&regulator_list_mutex);
1167 return regulator;
1168
1169found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001170 if (rdev->exclusive) {
1171 regulator = ERR_PTR(-EPERM);
1172 goto out;
1173 }
1174
1175 if (exclusive && rdev->open_count) {
1176 regulator = ERR_PTR(-EBUSY);
1177 goto out;
1178 }
1179
Liam Girdwooda5766f12008-10-10 13:22:20 +01001180 if (!try_module_get(rdev->owner))
1181 goto out;
1182
Liam Girdwood414c70c2008-04-30 15:59:04 +01001183 regulator = create_regulator(rdev, dev, id);
1184 if (regulator == NULL) {
1185 regulator = ERR_PTR(-ENOMEM);
1186 module_put(rdev->owner);
1187 }
1188
Mark Brown5ffbd132009-07-21 16:00:23 +01001189 rdev->open_count++;
1190 if (exclusive) {
1191 rdev->exclusive = 1;
1192
1193 ret = _regulator_is_enabled(rdev);
1194 if (ret > 0)
1195 rdev->use_count = 1;
1196 else
1197 rdev->use_count = 0;
1198 }
1199
Liam Girdwooda5766f12008-10-10 13:22:20 +01001200out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001201 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001202
Liam Girdwood414c70c2008-04-30 15:59:04 +01001203 return regulator;
1204}
Mark Brown5ffbd132009-07-21 16:00:23 +01001205
1206/**
1207 * regulator_get - lookup and obtain a reference to a regulator.
1208 * @dev: device for regulator "consumer"
1209 * @id: Supply name or regulator ID.
1210 *
1211 * Returns a struct regulator corresponding to the regulator producer,
1212 * or IS_ERR() condition containing errno.
1213 *
1214 * Use of supply names configured via regulator_set_device_supply() is
1215 * strongly encouraged. It is recommended that the supply name used
1216 * should match the name used for the supply and/or the relevant
1217 * device pins in the datasheet.
1218 */
1219struct regulator *regulator_get(struct device *dev, const char *id)
1220{
1221 return _regulator_get(dev, id, 0);
1222}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001223EXPORT_SYMBOL_GPL(regulator_get);
1224
1225/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001226 * regulator_get_exclusive - obtain exclusive access to a regulator.
1227 * @dev: device for regulator "consumer"
1228 * @id: Supply name or regulator ID.
1229 *
1230 * Returns a struct regulator corresponding to the regulator producer,
1231 * or IS_ERR() condition containing errno. Other consumers will be
1232 * unable to obtain this reference is held and the use count for the
1233 * regulator will be initialised to reflect the current state of the
1234 * regulator.
1235 *
1236 * This is intended for use by consumers which cannot tolerate shared
1237 * use of the regulator such as those which need to force the
1238 * regulator off for correct operation of the hardware they are
1239 * controlling.
1240 *
1241 * Use of supply names configured via regulator_set_device_supply() is
1242 * strongly encouraged. It is recommended that the supply name used
1243 * should match the name used for the supply and/or the relevant
1244 * device pins in the datasheet.
1245 */
1246struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1247{
1248 return _regulator_get(dev, id, 1);
1249}
1250EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1251
1252/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001253 * regulator_put - "free" the regulator source
1254 * @regulator: regulator source
1255 *
1256 * Note: drivers must ensure that all regulator_enable calls made on this
1257 * regulator source are balanced by regulator_disable calls prior to calling
1258 * this function.
1259 */
1260void regulator_put(struct regulator *regulator)
1261{
1262 struct regulator_dev *rdev;
1263
1264 if (regulator == NULL || IS_ERR(regulator))
1265 return;
1266
Liam Girdwood414c70c2008-04-30 15:59:04 +01001267 mutex_lock(&regulator_list_mutex);
1268 rdev = regulator->rdev;
1269
1270 /* remove any sysfs entries */
1271 if (regulator->dev) {
1272 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1273 kfree(regulator->supply_name);
1274 device_remove_file(regulator->dev, &regulator->dev_attr);
1275 kfree(regulator->dev_attr.attr.name);
1276 }
1277 list_del(&regulator->list);
1278 kfree(regulator);
1279
Mark Brown5ffbd132009-07-21 16:00:23 +01001280 rdev->open_count--;
1281 rdev->exclusive = 0;
1282
Liam Girdwood414c70c2008-04-30 15:59:04 +01001283 module_put(rdev->owner);
1284 mutex_unlock(&regulator_list_mutex);
1285}
1286EXPORT_SYMBOL_GPL(regulator_put);
1287
Mark Brown9a2372f2009-08-03 18:49:57 +01001288static int _regulator_can_change_status(struct regulator_dev *rdev)
1289{
1290 if (!rdev->constraints)
1291 return 0;
1292
1293 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1294 return 1;
1295 else
1296 return 0;
1297}
1298
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299/* locks held by regulator_enable() */
1300static int _regulator_enable(struct regulator_dev *rdev)
1301{
Mark Brown31aae2b2009-12-21 12:21:52 +00001302 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001303
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001304 if (rdev->use_count == 0) {
1305 /* do we need to enable the supply regulator first */
1306 if (rdev->supply) {
1307 mutex_lock(&rdev->supply->mutex);
1308 ret = _regulator_enable(rdev->supply);
1309 mutex_unlock(&rdev->supply->mutex);
1310 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001311 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001312 return ret;
1313 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001314 }
1315 }
1316
1317 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001318 if (rdev->constraints &&
1319 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1320 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321
Mark Brown9a2372f2009-08-03 18:49:57 +01001322 if (rdev->use_count == 0) {
1323 /* The regulator may on if it's not switchable or left on */
1324 ret = _regulator_is_enabled(rdev);
1325 if (ret == -EINVAL || ret == 0) {
1326 if (!_regulator_can_change_status(rdev))
1327 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001328
Mark Brown31aae2b2009-12-21 12:21:52 +00001329 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001330 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001331
1332 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001333 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001334 ret = _regulator_get_enable_time(rdev);
1335 if (ret >= 0) {
1336 delay = ret;
1337 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001338 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001339 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001340 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001341 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001342
Mark Brown02fa3ec2010-11-10 14:38:30 +00001343 trace_regulator_enable(rdev_get_name(rdev));
1344
Mark Brown31aae2b2009-12-21 12:21:52 +00001345 /* Allow the regulator to ramp; it would be useful
1346 * to extend this for bulk operations so that the
1347 * regulators can ramp together. */
1348 ret = rdev->desc->ops->enable(rdev);
1349 if (ret < 0)
1350 return ret;
1351
Mark Brown02fa3ec2010-11-10 14:38:30 +00001352 trace_regulator_enable_delay(rdev_get_name(rdev));
1353
Axel Line36c1df2010-11-05 21:51:32 +08001354 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001355 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001356 udelay(delay % 1000);
1357 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001358 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001359 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001360
Mark Brown02fa3ec2010-11-10 14:38:30 +00001361 trace_regulator_enable_complete(rdev_get_name(rdev));
1362
Linus Walleija7433cf2009-08-26 12:54:04 +02001363 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001364 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365 return ret;
1366 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001367 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368 }
1369
Mark Brown9a2372f2009-08-03 18:49:57 +01001370 rdev->use_count++;
1371
1372 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373}
1374
1375/**
1376 * regulator_enable - enable regulator output
1377 * @regulator: regulator source
1378 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001379 * Request that the regulator be enabled with the regulator output at
1380 * the predefined voltage or current value. Calls to regulator_enable()
1381 * must be balanced with calls to regulator_disable().
1382 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001383 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001384 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385 */
1386int regulator_enable(struct regulator *regulator)
1387{
David Brownell412aec62008-11-16 11:44:46 -08001388 struct regulator_dev *rdev = regulator->rdev;
1389 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390
David Brownell412aec62008-11-16 11:44:46 -08001391 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001392 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001393 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001394 return ret;
1395}
1396EXPORT_SYMBOL_GPL(regulator_enable);
1397
1398/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001399static int _regulator_disable(struct regulator_dev *rdev,
1400 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401{
1402 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001403 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404
David Brownellcd94b502009-03-11 16:43:34 -08001405 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001406 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001407 return -EIO;
1408
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001410 if (rdev->use_count == 1 &&
1411 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412
1413 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001414 if (_regulator_can_change_status(rdev) &&
1415 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001416 trace_regulator_disable(rdev_get_name(rdev));
1417
Liam Girdwood414c70c2008-04-30 15:59:04 +01001418 ret = rdev->desc->ops->disable(rdev);
1419 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001420 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 return ret;
1422 }
Mark Brown84b68262009-12-01 21:12:27 +00001423
Mark Brown02fa3ec2010-11-10 14:38:30 +00001424 trace_regulator_disable_complete(rdev_get_name(rdev));
1425
Mark Brown84b68262009-12-01 21:12:27 +00001426 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1427 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001428 }
1429
1430 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001431 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001432
1433 rdev->use_count = 0;
1434 } else if (rdev->use_count > 1) {
1435
1436 if (rdev->constraints &&
1437 (rdev->constraints->valid_ops_mask &
1438 REGULATOR_CHANGE_DRMS))
1439 drms_uA_update(rdev);
1440
1441 rdev->use_count--;
1442 }
1443 return ret;
1444}
1445
1446/**
1447 * regulator_disable - disable regulator output
1448 * @regulator: regulator source
1449 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001450 * Disable the regulator output voltage or current. Calls to
1451 * regulator_enable() must be balanced with calls to
1452 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001453 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001454 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001455 * devices have it enabled, the regulator device supports disabling and
1456 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001457 */
1458int regulator_disable(struct regulator *regulator)
1459{
David Brownell412aec62008-11-16 11:44:46 -08001460 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001461 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001462 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001463
David Brownell412aec62008-11-16 11:44:46 -08001464 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001465 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001466 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001467
1468 /* decrease our supplies ref count and disable if required */
1469 while (supply_rdev != NULL) {
1470 rdev = supply_rdev;
1471
1472 mutex_lock(&rdev->mutex);
1473 _regulator_disable(rdev, &supply_rdev);
1474 mutex_unlock(&rdev->mutex);
1475 }
1476
Liam Girdwood414c70c2008-04-30 15:59:04 +01001477 return ret;
1478}
1479EXPORT_SYMBOL_GPL(regulator_disable);
1480
1481/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001482static int _regulator_force_disable(struct regulator_dev *rdev,
1483 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001484{
1485 int ret = 0;
1486
1487 /* force disable */
1488 if (rdev->desc->ops->disable) {
1489 /* ah well, who wants to live forever... */
1490 ret = rdev->desc->ops->disable(rdev);
1491 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001492 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001493 return ret;
1494 }
1495 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001496 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1497 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498 }
1499
1500 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001501 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001502
1503 rdev->use_count = 0;
1504 return ret;
1505}
1506
1507/**
1508 * regulator_force_disable - force disable regulator output
1509 * @regulator: regulator source
1510 *
1511 * Forcibly disable the regulator output voltage or current.
1512 * NOTE: this *will* disable the regulator output even if other consumer
1513 * devices have it enabled. This should be used for situations when device
1514 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1515 */
1516int regulator_force_disable(struct regulator *regulator)
1517{
Mark Brown82d15832011-05-09 11:41:02 +02001518 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001519 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001520 int ret;
1521
Mark Brown82d15832011-05-09 11:41:02 +02001522 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 regulator->uA_load = 0;
Mark Brown82d15832011-05-09 11:41:02 +02001524 ret = _regulator_force_disable(rdev, &supply_rdev);
1525 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001526
1527 if (supply_rdev)
1528 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1529
Liam Girdwood414c70c2008-04-30 15:59:04 +01001530 return ret;
1531}
1532EXPORT_SYMBOL_GPL(regulator_force_disable);
1533
1534static int _regulator_is_enabled(struct regulator_dev *rdev)
1535{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001536 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001537 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001538 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001539
Mark Brown93325462009-08-03 18:49:56 +01001540 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001541}
1542
1543/**
1544 * regulator_is_enabled - is the regulator output enabled
1545 * @regulator: regulator source
1546 *
David Brownell412aec62008-11-16 11:44:46 -08001547 * Returns positive if the regulator driver backing the source/client
1548 * has requested that the device be enabled, zero if it hasn't, else a
1549 * negative errno code.
1550 *
1551 * Note that the device backing this regulator handle can have multiple
1552 * users, so it might be enabled even if regulator_enable() was never
1553 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001554 */
1555int regulator_is_enabled(struct regulator *regulator)
1556{
Mark Brown93325462009-08-03 18:49:56 +01001557 int ret;
1558
1559 mutex_lock(&regulator->rdev->mutex);
1560 ret = _regulator_is_enabled(regulator->rdev);
1561 mutex_unlock(&regulator->rdev->mutex);
1562
1563 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001564}
1565EXPORT_SYMBOL_GPL(regulator_is_enabled);
1566
1567/**
David Brownell4367cfd2009-02-26 11:48:36 -08001568 * regulator_count_voltages - count regulator_list_voltage() selectors
1569 * @regulator: regulator source
1570 *
1571 * Returns number of selectors, or negative errno. Selectors are
1572 * numbered starting at zero, and typically correspond to bitfields
1573 * in hardware registers.
1574 */
1575int regulator_count_voltages(struct regulator *regulator)
1576{
1577 struct regulator_dev *rdev = regulator->rdev;
1578
1579 return rdev->desc->n_voltages ? : -EINVAL;
1580}
1581EXPORT_SYMBOL_GPL(regulator_count_voltages);
1582
1583/**
1584 * regulator_list_voltage - enumerate supported voltages
1585 * @regulator: regulator source
1586 * @selector: identify voltage to list
1587 * Context: can sleep
1588 *
1589 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001590 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001591 * negative errno.
1592 */
1593int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1594{
1595 struct regulator_dev *rdev = regulator->rdev;
1596 struct regulator_ops *ops = rdev->desc->ops;
1597 int ret;
1598
1599 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1600 return -EINVAL;
1601
1602 mutex_lock(&rdev->mutex);
1603 ret = ops->list_voltage(rdev, selector);
1604 mutex_unlock(&rdev->mutex);
1605
1606 if (ret > 0) {
1607 if (ret < rdev->constraints->min_uV)
1608 ret = 0;
1609 else if (ret > rdev->constraints->max_uV)
1610 ret = 0;
1611 }
1612
1613 return ret;
1614}
1615EXPORT_SYMBOL_GPL(regulator_list_voltage);
1616
1617/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001618 * regulator_is_supported_voltage - check if a voltage range can be supported
1619 *
1620 * @regulator: Regulator to check.
1621 * @min_uV: Minimum required voltage in uV.
1622 * @max_uV: Maximum required voltage in uV.
1623 *
1624 * Returns a boolean or a negative error code.
1625 */
1626int regulator_is_supported_voltage(struct regulator *regulator,
1627 int min_uV, int max_uV)
1628{
1629 int i, voltages, ret;
1630
1631 ret = regulator_count_voltages(regulator);
1632 if (ret < 0)
1633 return ret;
1634 voltages = ret;
1635
1636 for (i = 0; i < voltages; i++) {
1637 ret = regulator_list_voltage(regulator, i);
1638
1639 if (ret >= min_uV && ret <= max_uV)
1640 return 1;
1641 }
1642
1643 return 0;
1644}
1645
Mark Brown75790252010-12-12 14:25:50 +00001646static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1647 int min_uV, int max_uV)
1648{
1649 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001650 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001651 unsigned int selector;
1652
1653 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1654
Mark Brownbf5892a2011-05-08 22:13:37 +01001655 min_uV += rdev->constraints->uV_offset;
1656 max_uV += rdev->constraints->uV_offset;
1657
Mark Brown75790252010-12-12 14:25:50 +00001658 if (rdev->desc->ops->set_voltage) {
1659 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1660 &selector);
1661
1662 if (rdev->desc->ops->list_voltage)
1663 selector = rdev->desc->ops->list_voltage(rdev,
1664 selector);
1665 else
1666 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001667 } else if (rdev->desc->ops->set_voltage_sel) {
1668 int best_val = INT_MAX;
1669 int i;
1670
1671 selector = 0;
1672
1673 /* Find the smallest voltage that falls within the specified
1674 * range.
1675 */
1676 for (i = 0; i < rdev->desc->n_voltages; i++) {
1677 ret = rdev->desc->ops->list_voltage(rdev, i);
1678 if (ret < 0)
1679 continue;
1680
1681 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1682 best_val = ret;
1683 selector = i;
1684 }
1685 }
1686
Linus Walleij77af1b2642011-03-17 13:24:36 +01001687 /*
1688 * If we can't obtain the old selector there is not enough
1689 * info to call set_voltage_time_sel().
1690 */
1691 if (rdev->desc->ops->set_voltage_time_sel &&
1692 rdev->desc->ops->get_voltage_sel) {
1693 unsigned int old_selector = 0;
1694
1695 ret = rdev->desc->ops->get_voltage_sel(rdev);
1696 if (ret < 0)
1697 return ret;
1698 old_selector = ret;
1699 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1700 old_selector, selector);
1701 }
1702
Mark Browne8eef822010-12-12 14:36:17 +00001703 if (best_val != INT_MAX) {
1704 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1705 selector = best_val;
1706 } else {
1707 ret = -EINVAL;
1708 }
Mark Brown75790252010-12-12 14:25:50 +00001709 } else {
1710 ret = -EINVAL;
1711 }
1712
Linus Walleij77af1b2642011-03-17 13:24:36 +01001713 /* Insert any necessary delays */
1714 if (delay >= 1000) {
1715 mdelay(delay / 1000);
1716 udelay(delay % 1000);
1717 } else if (delay) {
1718 udelay(delay);
1719 }
1720
Mark Brownded06a52010-12-16 13:59:10 +00001721 if (ret == 0)
1722 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1723 NULL);
1724
Mark Brown75790252010-12-12 14:25:50 +00001725 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1726
1727 return ret;
1728}
1729
Mark Browna7a1ad92009-07-21 16:00:24 +01001730/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001731 * regulator_set_voltage - set regulator output voltage
1732 * @regulator: regulator source
1733 * @min_uV: Minimum required voltage in uV
1734 * @max_uV: Maximum acceptable voltage in uV
1735 *
1736 * Sets a voltage regulator to the desired output voltage. This can be set
1737 * during any regulator state. IOW, regulator can be disabled or enabled.
1738 *
1739 * If the regulator is enabled then the voltage will change to the new value
1740 * immediately otherwise if the regulator is disabled the regulator will
1741 * output at the new voltage when enabled.
1742 *
1743 * NOTE: If the regulator is shared between several devices then the lowest
1744 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001745 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001746 * calling this function otherwise this call will fail.
1747 */
1748int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1749{
1750 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001751 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001752
1753 mutex_lock(&rdev->mutex);
1754
Mark Brown95a3c232010-12-16 15:49:37 +00001755 /* If we're setting the same range as last time the change
1756 * should be a noop (some cpufreq implementations use the same
1757 * voltage for multiple frequencies, for example).
1758 */
1759 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1760 goto out;
1761
Liam Girdwood414c70c2008-04-30 15:59:04 +01001762 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001763 if (!rdev->desc->ops->set_voltage &&
1764 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 /* constraints check */
1770 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1771 if (ret < 0)
1772 goto out;
1773 regulator->min_uV = min_uV;
1774 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001775
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001776 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1777 if (ret < 0)
1778 goto out;
1779
Mark Brown75790252010-12-12 14:25:50 +00001780 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001781
Liam Girdwood414c70c2008-04-30 15:59:04 +01001782out:
1783 mutex_unlock(&rdev->mutex);
1784 return ret;
1785}
1786EXPORT_SYMBOL_GPL(regulator_set_voltage);
1787
Mark Brown606a2562010-12-16 15:49:36 +00001788/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001789 * regulator_set_voltage_time - get raise/fall time
1790 * @regulator: regulator source
1791 * @old_uV: starting voltage in microvolts
1792 * @new_uV: target voltage in microvolts
1793 *
1794 * Provided with the starting and ending voltage, this function attempts to
1795 * calculate the time in microseconds required to rise or fall to this new
1796 * voltage.
1797 */
1798int regulator_set_voltage_time(struct regulator *regulator,
1799 int old_uV, int new_uV)
1800{
1801 struct regulator_dev *rdev = regulator->rdev;
1802 struct regulator_ops *ops = rdev->desc->ops;
1803 int old_sel = -1;
1804 int new_sel = -1;
1805 int voltage;
1806 int i;
1807
1808 /* Currently requires operations to do this */
1809 if (!ops->list_voltage || !ops->set_voltage_time_sel
1810 || !rdev->desc->n_voltages)
1811 return -EINVAL;
1812
1813 for (i = 0; i < rdev->desc->n_voltages; i++) {
1814 /* We only look for exact voltage matches here */
1815 voltage = regulator_list_voltage(regulator, i);
1816 if (voltage < 0)
1817 return -EINVAL;
1818 if (voltage == 0)
1819 continue;
1820 if (voltage == old_uV)
1821 old_sel = i;
1822 if (voltage == new_uV)
1823 new_sel = i;
1824 }
1825
1826 if (old_sel < 0 || new_sel < 0)
1827 return -EINVAL;
1828
1829 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1830}
1831EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1832
1833/**
Mark Brown606a2562010-12-16 15:49:36 +00001834 * regulator_sync_voltage - re-apply last regulator output voltage
1835 * @regulator: regulator source
1836 *
1837 * Re-apply the last configured voltage. This is intended to be used
1838 * where some external control source the consumer is cooperating with
1839 * has caused the configured voltage to change.
1840 */
1841int regulator_sync_voltage(struct regulator *regulator)
1842{
1843 struct regulator_dev *rdev = regulator->rdev;
1844 int ret, min_uV, max_uV;
1845
1846 mutex_lock(&rdev->mutex);
1847
1848 if (!rdev->desc->ops->set_voltage &&
1849 !rdev->desc->ops->set_voltage_sel) {
1850 ret = -EINVAL;
1851 goto out;
1852 }
1853
1854 /* This is only going to work if we've had a voltage configured. */
1855 if (!regulator->min_uV && !regulator->max_uV) {
1856 ret = -EINVAL;
1857 goto out;
1858 }
1859
1860 min_uV = regulator->min_uV;
1861 max_uV = regulator->max_uV;
1862
1863 /* This should be a paranoia check... */
1864 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1865 if (ret < 0)
1866 goto out;
1867
1868 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1869 if (ret < 0)
1870 goto out;
1871
1872 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1873
1874out:
1875 mutex_unlock(&rdev->mutex);
1876 return ret;
1877}
1878EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1879
Liam Girdwood414c70c2008-04-30 15:59:04 +01001880static int _regulator_get_voltage(struct regulator_dev *rdev)
1881{
Mark Brownbf5892a2011-05-08 22:13:37 +01001882 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001883
1884 if (rdev->desc->ops->get_voltage_sel) {
1885 sel = rdev->desc->ops->get_voltage_sel(rdev);
1886 if (sel < 0)
1887 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001888 ret = rdev->desc->ops->list_voltage(rdev, sel);
Mark Brown476c2d82010-12-10 17:28:07 +00001889 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001890 if (rdev->desc->ops->get_voltage)
Mark Brownbf5892a2011-05-08 22:13:37 +01001891 ret = rdev->desc->ops->get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001892 else
1893 return -EINVAL;
Mark Brownbf5892a2011-05-08 22:13:37 +01001894
1895 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001896}
1897
1898/**
1899 * regulator_get_voltage - get regulator output voltage
1900 * @regulator: regulator source
1901 *
1902 * This returns the current regulator voltage in uV.
1903 *
1904 * NOTE: If the regulator is disabled it will return the voltage value. This
1905 * function should not be used to determine regulator state.
1906 */
1907int regulator_get_voltage(struct regulator *regulator)
1908{
1909 int ret;
1910
1911 mutex_lock(&regulator->rdev->mutex);
1912
1913 ret = _regulator_get_voltage(regulator->rdev);
1914
1915 mutex_unlock(&regulator->rdev->mutex);
1916
1917 return ret;
1918}
1919EXPORT_SYMBOL_GPL(regulator_get_voltage);
1920
1921/**
1922 * regulator_set_current_limit - set regulator output current limit
1923 * @regulator: regulator source
1924 * @min_uA: Minimuum supported current in uA
1925 * @max_uA: Maximum supported current in uA
1926 *
1927 * Sets current sink to the desired output current. This can be set during
1928 * any regulator state. IOW, regulator can be disabled or enabled.
1929 *
1930 * If the regulator is enabled then the current will change to the new value
1931 * immediately otherwise if the regulator is disabled the regulator will
1932 * output at the new current when enabled.
1933 *
1934 * NOTE: Regulator system constraints must be set for this regulator before
1935 * calling this function otherwise this call will fail.
1936 */
1937int regulator_set_current_limit(struct regulator *regulator,
1938 int min_uA, int max_uA)
1939{
1940 struct regulator_dev *rdev = regulator->rdev;
1941 int ret;
1942
1943 mutex_lock(&rdev->mutex);
1944
1945 /* sanity check */
1946 if (!rdev->desc->ops->set_current_limit) {
1947 ret = -EINVAL;
1948 goto out;
1949 }
1950
1951 /* constraints check */
1952 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1953 if (ret < 0)
1954 goto out;
1955
1956 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1957out:
1958 mutex_unlock(&rdev->mutex);
1959 return ret;
1960}
1961EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1962
1963static int _regulator_get_current_limit(struct regulator_dev *rdev)
1964{
1965 int ret;
1966
1967 mutex_lock(&rdev->mutex);
1968
1969 /* sanity check */
1970 if (!rdev->desc->ops->get_current_limit) {
1971 ret = -EINVAL;
1972 goto out;
1973 }
1974
1975 ret = rdev->desc->ops->get_current_limit(rdev);
1976out:
1977 mutex_unlock(&rdev->mutex);
1978 return ret;
1979}
1980
1981/**
1982 * regulator_get_current_limit - get regulator output current
1983 * @regulator: regulator source
1984 *
1985 * This returns the current supplied by the specified current sink in uA.
1986 *
1987 * NOTE: If the regulator is disabled it will return the current value. This
1988 * function should not be used to determine regulator state.
1989 */
1990int regulator_get_current_limit(struct regulator *regulator)
1991{
1992 return _regulator_get_current_limit(regulator->rdev);
1993}
1994EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1995
1996/**
1997 * regulator_set_mode - set regulator operating mode
1998 * @regulator: regulator source
1999 * @mode: operating mode - one of the REGULATOR_MODE constants
2000 *
2001 * Set regulator operating mode to increase regulator efficiency or improve
2002 * regulation performance.
2003 *
2004 * NOTE: Regulator system constraints must be set for this regulator before
2005 * calling this function otherwise this call will fail.
2006 */
2007int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2008{
2009 struct regulator_dev *rdev = regulator->rdev;
2010 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302011 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002012
2013 mutex_lock(&rdev->mutex);
2014
2015 /* sanity check */
2016 if (!rdev->desc->ops->set_mode) {
2017 ret = -EINVAL;
2018 goto out;
2019 }
2020
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302021 /* return if the same mode is requested */
2022 if (rdev->desc->ops->get_mode) {
2023 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2024 if (regulator_curr_mode == mode) {
2025 ret = 0;
2026 goto out;
2027 }
2028 }
2029
Liam Girdwood414c70c2008-04-30 15:59:04 +01002030 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002031 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002032 if (ret < 0)
2033 goto out;
2034
2035 ret = rdev->desc->ops->set_mode(rdev, mode);
2036out:
2037 mutex_unlock(&rdev->mutex);
2038 return ret;
2039}
2040EXPORT_SYMBOL_GPL(regulator_set_mode);
2041
2042static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2043{
2044 int ret;
2045
2046 mutex_lock(&rdev->mutex);
2047
2048 /* sanity check */
2049 if (!rdev->desc->ops->get_mode) {
2050 ret = -EINVAL;
2051 goto out;
2052 }
2053
2054 ret = rdev->desc->ops->get_mode(rdev);
2055out:
2056 mutex_unlock(&rdev->mutex);
2057 return ret;
2058}
2059
2060/**
2061 * regulator_get_mode - get regulator operating mode
2062 * @regulator: regulator source
2063 *
2064 * Get the current regulator operating mode.
2065 */
2066unsigned int regulator_get_mode(struct regulator *regulator)
2067{
2068 return _regulator_get_mode(regulator->rdev);
2069}
2070EXPORT_SYMBOL_GPL(regulator_get_mode);
2071
2072/**
2073 * regulator_set_optimum_mode - set regulator optimum operating mode
2074 * @regulator: regulator source
2075 * @uA_load: load current
2076 *
2077 * Notifies the regulator core of a new device load. This is then used by
2078 * DRMS (if enabled by constraints) to set the most efficient regulator
2079 * operating mode for the new regulator loading.
2080 *
2081 * Consumer devices notify their supply regulator of the maximum power
2082 * they will require (can be taken from device datasheet in the power
2083 * consumption tables) when they change operational status and hence power
2084 * state. Examples of operational state changes that can affect power
2085 * consumption are :-
2086 *
2087 * o Device is opened / closed.
2088 * o Device I/O is about to begin or has just finished.
2089 * o Device is idling in between work.
2090 *
2091 * This information is also exported via sysfs to userspace.
2092 *
2093 * DRMS will sum the total requested load on the regulator and change
2094 * to the most efficient operating mode if platform constraints allow.
2095 *
2096 * Returns the new regulator mode or error.
2097 */
2098int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2099{
2100 struct regulator_dev *rdev = regulator->rdev;
2101 struct regulator *consumer;
2102 int ret, output_uV, input_uV, total_uA_load = 0;
2103 unsigned int mode;
2104
2105 mutex_lock(&rdev->mutex);
2106
Mark Browna4b41482011-05-14 11:19:45 -07002107 /*
2108 * first check to see if we can set modes at all, otherwise just
2109 * tell the consumer everything is OK.
2110 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111 regulator->uA_load = uA_load;
2112 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002113 if (ret < 0) {
2114 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002115 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002116 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002117
Liam Girdwood414c70c2008-04-30 15:59:04 +01002118 if (!rdev->desc->ops->get_optimum_mode)
2119 goto out;
2120
Mark Browna4b41482011-05-14 11:19:45 -07002121 /*
2122 * we can actually do this so any errors are indicators of
2123 * potential real failure.
2124 */
2125 ret = -EINVAL;
2126
Liam Girdwood414c70c2008-04-30 15:59:04 +01002127 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002128 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002129 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002130 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002131 goto out;
2132 }
2133
2134 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002135 input_uV = 0;
2136 if (rdev->supply)
2137 input_uV = _regulator_get_voltage(rdev->supply);
2138 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002139 input_uV = rdev->constraints->input_uV;
2140 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002141 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002142 goto out;
2143 }
2144
2145 /* calc total requested load for this regulator */
2146 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002147 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002148
2149 mode = rdev->desc->ops->get_optimum_mode(rdev,
2150 input_uV, output_uV,
2151 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002152 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002153 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002154 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2155 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002156 goto out;
2157 }
2158
2159 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08002160 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002161 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002162 goto out;
2163 }
2164 ret = mode;
2165out:
2166 mutex_unlock(&rdev->mutex);
2167 return ret;
2168}
2169EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2170
2171/**
2172 * regulator_register_notifier - register regulator event notifier
2173 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002174 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002175 *
2176 * Register notifier block to receive regulator events.
2177 */
2178int regulator_register_notifier(struct regulator *regulator,
2179 struct notifier_block *nb)
2180{
2181 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2182 nb);
2183}
2184EXPORT_SYMBOL_GPL(regulator_register_notifier);
2185
2186/**
2187 * regulator_unregister_notifier - unregister regulator event notifier
2188 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002189 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002190 *
2191 * Unregister regulator event notifier block.
2192 */
2193int regulator_unregister_notifier(struct regulator *regulator,
2194 struct notifier_block *nb)
2195{
2196 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2197 nb);
2198}
2199EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2200
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002201/* notify regulator consumers and downstream regulator consumers.
2202 * Note mutex must be held by caller.
2203 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002204static void _notifier_call_chain(struct regulator_dev *rdev,
2205 unsigned long event, void *data)
2206{
2207 struct regulator_dev *_rdev;
2208
2209 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002210 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002211
2212 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002213 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002214 mutex_lock(&_rdev->mutex);
2215 _notifier_call_chain(_rdev, event, data);
2216 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002217 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002218}
2219
2220/**
2221 * regulator_bulk_get - get multiple regulator consumers
2222 *
2223 * @dev: Device to supply
2224 * @num_consumers: Number of consumers to register
2225 * @consumers: Configuration of consumers; clients are stored here.
2226 *
2227 * @return 0 on success, an errno on failure.
2228 *
2229 * This helper function allows drivers to get several regulator
2230 * consumers in one operation. If any of the regulators cannot be
2231 * acquired then any regulators that were allocated will be freed
2232 * before returning to the caller.
2233 */
2234int regulator_bulk_get(struct device *dev, int num_consumers,
2235 struct regulator_bulk_data *consumers)
2236{
2237 int i;
2238 int ret;
2239
2240 for (i = 0; i < num_consumers; i++)
2241 consumers[i].consumer = NULL;
2242
2243 for (i = 0; i < num_consumers; i++) {
2244 consumers[i].consumer = regulator_get(dev,
2245 consumers[i].supply);
2246 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002247 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002248 dev_err(dev, "Failed to get supply '%s': %d\n",
2249 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002250 consumers[i].consumer = NULL;
2251 goto err;
2252 }
2253 }
2254
2255 return 0;
2256
2257err:
2258 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2259 regulator_put(consumers[i].consumer);
2260
2261 return ret;
2262}
2263EXPORT_SYMBOL_GPL(regulator_bulk_get);
2264
2265/**
2266 * regulator_bulk_enable - enable multiple regulator consumers
2267 *
2268 * @num_consumers: Number of consumers
2269 * @consumers: Consumer data; clients are stored here.
2270 * @return 0 on success, an errno on failure
2271 *
2272 * This convenience API allows consumers to enable multiple regulator
2273 * clients in a single API call. If any consumers cannot be enabled
2274 * then any others that were enabled will be disabled again prior to
2275 * return.
2276 */
2277int regulator_bulk_enable(int num_consumers,
2278 struct regulator_bulk_data *consumers)
2279{
2280 int i;
2281 int ret;
2282
2283 for (i = 0; i < num_consumers; i++) {
2284 ret = regulator_enable(consumers[i].consumer);
2285 if (ret != 0)
2286 goto err;
2287 }
2288
2289 return 0;
2290
2291err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002292 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002293 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002294 regulator_disable(consumers[i].consumer);
2295
2296 return ret;
2297}
2298EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2299
2300/**
2301 * regulator_bulk_disable - disable multiple regulator consumers
2302 *
2303 * @num_consumers: Number of consumers
2304 * @consumers: Consumer data; clients are stored here.
2305 * @return 0 on success, an errno on failure
2306 *
2307 * This convenience API allows consumers to disable multiple regulator
2308 * clients in a single API call. If any consumers cannot be enabled
2309 * then any others that were disabled will be disabled again prior to
2310 * return.
2311 */
2312int regulator_bulk_disable(int num_consumers,
2313 struct regulator_bulk_data *consumers)
2314{
2315 int i;
2316 int ret;
2317
2318 for (i = 0; i < num_consumers; i++) {
2319 ret = regulator_disable(consumers[i].consumer);
2320 if (ret != 0)
2321 goto err;
2322 }
2323
2324 return 0;
2325
2326err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002327 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002328 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002329 regulator_enable(consumers[i].consumer);
2330
2331 return ret;
2332}
2333EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2334
2335/**
2336 * regulator_bulk_free - free multiple regulator consumers
2337 *
2338 * @num_consumers: Number of consumers
2339 * @consumers: Consumer data; clients are stored here.
2340 *
2341 * This convenience API allows consumers to free multiple regulator
2342 * clients in a single API call.
2343 */
2344void regulator_bulk_free(int num_consumers,
2345 struct regulator_bulk_data *consumers)
2346{
2347 int i;
2348
2349 for (i = 0; i < num_consumers; i++) {
2350 regulator_put(consumers[i].consumer);
2351 consumers[i].consumer = NULL;
2352 }
2353}
2354EXPORT_SYMBOL_GPL(regulator_bulk_free);
2355
2356/**
2357 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002358 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002359 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002360 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002361 *
2362 * Called by regulator drivers to notify clients a regulator event has
2363 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002364 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002365 */
2366int regulator_notifier_call_chain(struct regulator_dev *rdev,
2367 unsigned long event, void *data)
2368{
2369 _notifier_call_chain(rdev, event, data);
2370 return NOTIFY_DONE;
2371
2372}
2373EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2374
Mark Brownbe721972009-08-04 20:09:52 +02002375/**
2376 * regulator_mode_to_status - convert a regulator mode into a status
2377 *
2378 * @mode: Mode to convert
2379 *
2380 * Convert a regulator mode into a status.
2381 */
2382int regulator_mode_to_status(unsigned int mode)
2383{
2384 switch (mode) {
2385 case REGULATOR_MODE_FAST:
2386 return REGULATOR_STATUS_FAST;
2387 case REGULATOR_MODE_NORMAL:
2388 return REGULATOR_STATUS_NORMAL;
2389 case REGULATOR_MODE_IDLE:
2390 return REGULATOR_STATUS_IDLE;
2391 case REGULATOR_STATUS_STANDBY:
2392 return REGULATOR_STATUS_STANDBY;
2393 default:
2394 return 0;
2395 }
2396}
2397EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2398
David Brownell7ad68e22008-11-11 17:39:02 -08002399/*
2400 * To avoid cluttering sysfs (and memory) with useless state, only
2401 * create attributes that can be meaningfully displayed.
2402 */
2403static int add_regulator_attributes(struct regulator_dev *rdev)
2404{
2405 struct device *dev = &rdev->dev;
2406 struct regulator_ops *ops = rdev->desc->ops;
2407 int status = 0;
2408
2409 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002410 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002411 status = device_create_file(dev, &dev_attr_microvolts);
2412 if (status < 0)
2413 return status;
2414 }
2415 if (ops->get_current_limit) {
2416 status = device_create_file(dev, &dev_attr_microamps);
2417 if (status < 0)
2418 return status;
2419 }
2420 if (ops->get_mode) {
2421 status = device_create_file(dev, &dev_attr_opmode);
2422 if (status < 0)
2423 return status;
2424 }
2425 if (ops->is_enabled) {
2426 status = device_create_file(dev, &dev_attr_state);
2427 if (status < 0)
2428 return status;
2429 }
David Brownell853116a2009-01-14 23:03:17 -08002430 if (ops->get_status) {
2431 status = device_create_file(dev, &dev_attr_status);
2432 if (status < 0)
2433 return status;
2434 }
David Brownell7ad68e22008-11-11 17:39:02 -08002435
2436 /* some attributes are type-specific */
2437 if (rdev->desc->type == REGULATOR_CURRENT) {
2438 status = device_create_file(dev, &dev_attr_requested_microamps);
2439 if (status < 0)
2440 return status;
2441 }
2442
2443 /* all the other attributes exist to support constraints;
2444 * don't show them if there are no constraints, or if the
2445 * relevant supporting methods are missing.
2446 */
2447 if (!rdev->constraints)
2448 return status;
2449
2450 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002451 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002452 status = device_create_file(dev, &dev_attr_min_microvolts);
2453 if (status < 0)
2454 return status;
2455 status = device_create_file(dev, &dev_attr_max_microvolts);
2456 if (status < 0)
2457 return status;
2458 }
2459 if (ops->set_current_limit) {
2460 status = device_create_file(dev, &dev_attr_min_microamps);
2461 if (status < 0)
2462 return status;
2463 status = device_create_file(dev, &dev_attr_max_microamps);
2464 if (status < 0)
2465 return status;
2466 }
2467
2468 /* suspend mode constraints need multiple supporting methods */
2469 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2470 return status;
2471
2472 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2473 if (status < 0)
2474 return status;
2475 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2476 if (status < 0)
2477 return status;
2478 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2479 if (status < 0)
2480 return status;
2481
2482 if (ops->set_suspend_voltage) {
2483 status = device_create_file(dev,
2484 &dev_attr_suspend_standby_microvolts);
2485 if (status < 0)
2486 return status;
2487 status = device_create_file(dev,
2488 &dev_attr_suspend_mem_microvolts);
2489 if (status < 0)
2490 return status;
2491 status = device_create_file(dev,
2492 &dev_attr_suspend_disk_microvolts);
2493 if (status < 0)
2494 return status;
2495 }
2496
2497 if (ops->set_suspend_mode) {
2498 status = device_create_file(dev,
2499 &dev_attr_suspend_standby_mode);
2500 if (status < 0)
2501 return status;
2502 status = device_create_file(dev,
2503 &dev_attr_suspend_mem_mode);
2504 if (status < 0)
2505 return status;
2506 status = device_create_file(dev,
2507 &dev_attr_suspend_disk_mode);
2508 if (status < 0)
2509 return status;
2510 }
2511
2512 return status;
2513}
2514
Mark Brown1130e5b2010-12-21 23:49:31 +00002515static void rdev_init_debugfs(struct regulator_dev *rdev)
2516{
2517#ifdef CONFIG_DEBUG_FS
2518 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2519 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2520 rdev_warn(rdev, "Failed to create debugfs directory\n");
2521 rdev->debugfs = NULL;
2522 return;
2523 }
2524
2525 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2526 &rdev->use_count);
2527 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2528 &rdev->open_count);
2529#endif
2530}
2531
Liam Girdwood414c70c2008-04-30 15:59:04 +01002532/**
2533 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002534 * @regulator_desc: regulator to register
2535 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002536 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002537 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 *
2539 * Called by regulator drivers to register a regulator.
2540 * Returns 0 on success.
2541 */
2542struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002543 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002544 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002545{
2546 static atomic_t regulator_no = ATOMIC_INIT(0);
2547 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002548 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002549
2550 if (regulator_desc == NULL)
2551 return ERR_PTR(-EINVAL);
2552
2553 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2554 return ERR_PTR(-EINVAL);
2555
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002556 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2557 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002558 return ERR_PTR(-EINVAL);
2559
Mark Brown46fabe1e2008-09-09 16:21:18 +01002560 if (!init_data)
2561 return ERR_PTR(-EINVAL);
2562
Mark Brown476c2d82010-12-10 17:28:07 +00002563 /* Only one of each should be implemented */
2564 WARN_ON(regulator_desc->ops->get_voltage &&
2565 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002566 WARN_ON(regulator_desc->ops->set_voltage &&
2567 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002568
2569 /* If we're using selectors we must implement list_voltage. */
2570 if (regulator_desc->ops->get_voltage_sel &&
2571 !regulator_desc->ops->list_voltage) {
2572 return ERR_PTR(-EINVAL);
2573 }
Mark Browne8eef822010-12-12 14:36:17 +00002574 if (regulator_desc->ops->set_voltage_sel &&
2575 !regulator_desc->ops->list_voltage) {
2576 return ERR_PTR(-EINVAL);
2577 }
Mark Brown476c2d82010-12-10 17:28:07 +00002578
Liam Girdwood414c70c2008-04-30 15:59:04 +01002579 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2580 if (rdev == NULL)
2581 return ERR_PTR(-ENOMEM);
2582
2583 mutex_lock(&regulator_list_mutex);
2584
2585 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002586 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002587 rdev->owner = regulator_desc->owner;
2588 rdev->desc = regulator_desc;
2589 INIT_LIST_HEAD(&rdev->consumer_list);
2590 INIT_LIST_HEAD(&rdev->supply_list);
2591 INIT_LIST_HEAD(&rdev->list);
2592 INIT_LIST_HEAD(&rdev->slist);
2593 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2594
Liam Girdwooda5766f12008-10-10 13:22:20 +01002595 /* preform any regulator specific init */
2596 if (init_data->regulator_init) {
2597 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002598 if (ret < 0)
2599 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002600 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002601
Liam Girdwooda5766f12008-10-10 13:22:20 +01002602 /* register with sysfs */
2603 rdev->dev.class = &regulator_class;
2604 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002605 dev_set_name(&rdev->dev, "regulator.%d",
2606 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002607 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002608 if (ret != 0) {
2609 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002610 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002611 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002612
2613 dev_set_drvdata(&rdev->dev, rdev);
2614
Mike Rapoport74f544c2008-11-25 14:53:53 +02002615 /* set regulator constraints */
2616 ret = set_machine_constraints(rdev, &init_data->constraints);
2617 if (ret < 0)
2618 goto scrub;
2619
David Brownell7ad68e22008-11-11 17:39:02 -08002620 /* add attributes supported by this regulator */
2621 ret = add_regulator_attributes(rdev);
2622 if (ret < 0)
2623 goto scrub;
2624
Mark Brown0178f3e2010-04-26 15:18:14 +01002625 if (init_data->supply_regulator) {
2626 struct regulator_dev *r;
2627 int found = 0;
2628
2629 list_for_each_entry(r, &regulator_list, list) {
2630 if (strcmp(rdev_get_name(r),
2631 init_data->supply_regulator) == 0) {
2632 found = 1;
2633 break;
2634 }
2635 }
2636
2637 if (!found) {
2638 dev_err(dev, "Failed to find supply %s\n",
2639 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002640 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002641 goto scrub;
2642 }
2643
2644 ret = set_supply(rdev, r);
2645 if (ret < 0)
2646 goto scrub;
2647 }
2648
Liam Girdwooda5766f12008-10-10 13:22:20 +01002649 /* add consumers devices */
2650 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2651 ret = set_consumer_device_supply(rdev,
2652 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002653 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002654 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002655 if (ret < 0) {
2656 dev_err(dev, "Failed to set supply %s\n",
2657 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002658 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002659 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002660 }
2661
2662 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002663
2664 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002665out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002666 mutex_unlock(&regulator_list_mutex);
2667 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002668
Jani Nikulad4033b52010-04-29 10:55:11 +03002669unset_supplies:
2670 unset_regulator_supplies(rdev);
2671
David Brownell4fca9542008-11-11 17:38:53 -08002672scrub:
2673 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002674 /* device core frees rdev */
2675 rdev = ERR_PTR(ret);
2676 goto out;
2677
David Brownell4fca9542008-11-11 17:38:53 -08002678clean:
2679 kfree(rdev);
2680 rdev = ERR_PTR(ret);
2681 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002682}
2683EXPORT_SYMBOL_GPL(regulator_register);
2684
2685/**
2686 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002687 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002688 *
2689 * Called by regulator drivers to unregister a regulator.
2690 */
2691void regulator_unregister(struct regulator_dev *rdev)
2692{
2693 if (rdev == NULL)
2694 return;
2695
2696 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002697#ifdef CONFIG_DEBUG_FS
2698 debugfs_remove_recursive(rdev->debugfs);
2699#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002700 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002701 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002702 list_del(&rdev->list);
2703 if (rdev->supply)
2704 sysfs_remove_link(&rdev->dev.kobj, "supply");
2705 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002706 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002707 mutex_unlock(&regulator_list_mutex);
2708}
2709EXPORT_SYMBOL_GPL(regulator_unregister);
2710
2711/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002712 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002713 * @state: system suspend state
2714 *
2715 * Configure each regulator with it's suspend operating parameters for state.
2716 * This will usually be called by machine suspend code prior to supending.
2717 */
2718int regulator_suspend_prepare(suspend_state_t state)
2719{
2720 struct regulator_dev *rdev;
2721 int ret = 0;
2722
2723 /* ON is handled by regulator active state */
2724 if (state == PM_SUSPEND_ON)
2725 return -EINVAL;
2726
2727 mutex_lock(&regulator_list_mutex);
2728 list_for_each_entry(rdev, &regulator_list, list) {
2729
2730 mutex_lock(&rdev->mutex);
2731 ret = suspend_prepare(rdev, state);
2732 mutex_unlock(&rdev->mutex);
2733
2734 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002735 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002736 goto out;
2737 }
2738 }
2739out:
2740 mutex_unlock(&regulator_list_mutex);
2741 return ret;
2742}
2743EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2744
2745/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002746 * regulator_suspend_finish - resume regulators from system wide suspend
2747 *
2748 * Turn on regulators that might be turned off by regulator_suspend_prepare
2749 * and that should be turned on according to the regulators properties.
2750 */
2751int regulator_suspend_finish(void)
2752{
2753 struct regulator_dev *rdev;
2754 int ret = 0, error;
2755
2756 mutex_lock(&regulator_list_mutex);
2757 list_for_each_entry(rdev, &regulator_list, list) {
2758 struct regulator_ops *ops = rdev->desc->ops;
2759
2760 mutex_lock(&rdev->mutex);
2761 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2762 ops->enable) {
2763 error = ops->enable(rdev);
2764 if (error)
2765 ret = error;
2766 } else {
2767 if (!has_full_constraints)
2768 goto unlock;
2769 if (!ops->disable)
2770 goto unlock;
2771 if (ops->is_enabled && !ops->is_enabled(rdev))
2772 goto unlock;
2773
2774 error = ops->disable(rdev);
2775 if (error)
2776 ret = error;
2777 }
2778unlock:
2779 mutex_unlock(&rdev->mutex);
2780 }
2781 mutex_unlock(&regulator_list_mutex);
2782 return ret;
2783}
2784EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2785
2786/**
Mark Brownca725562009-03-16 19:36:34 +00002787 * regulator_has_full_constraints - the system has fully specified constraints
2788 *
2789 * Calling this function will cause the regulator API to disable all
2790 * regulators which have a zero use count and don't have an always_on
2791 * constraint in a late_initcall.
2792 *
2793 * The intention is that this will become the default behaviour in a
2794 * future kernel release so users are encouraged to use this facility
2795 * now.
2796 */
2797void regulator_has_full_constraints(void)
2798{
2799 has_full_constraints = 1;
2800}
2801EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2802
2803/**
Mark Brown688fe992010-10-05 19:18:32 -07002804 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2805 *
2806 * Calling this function will cause the regulator API to provide a
2807 * dummy regulator to consumers if no physical regulator is found,
2808 * allowing most consumers to proceed as though a regulator were
2809 * configured. This allows systems such as those with software
2810 * controllable regulators for the CPU core only to be brought up more
2811 * readily.
2812 */
2813void regulator_use_dummy_regulator(void)
2814{
2815 board_wants_dummy_regulator = true;
2816}
2817EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2818
2819/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002820 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002821 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002822 *
2823 * Get rdev regulator driver private data. This call can be used in the
2824 * regulator driver context.
2825 */
2826void *rdev_get_drvdata(struct regulator_dev *rdev)
2827{
2828 return rdev->reg_data;
2829}
2830EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2831
2832/**
2833 * regulator_get_drvdata - get regulator driver data
2834 * @regulator: regulator
2835 *
2836 * Get regulator driver private data. This call can be used in the consumer
2837 * driver context when non API regulator specific functions need to be called.
2838 */
2839void *regulator_get_drvdata(struct regulator *regulator)
2840{
2841 return regulator->rdev->reg_data;
2842}
2843EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2844
2845/**
2846 * regulator_set_drvdata - set regulator driver data
2847 * @regulator: regulator
2848 * @data: data
2849 */
2850void regulator_set_drvdata(struct regulator *regulator, void *data)
2851{
2852 regulator->rdev->reg_data = data;
2853}
2854EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2855
2856/**
2857 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002858 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002859 */
2860int rdev_get_id(struct regulator_dev *rdev)
2861{
2862 return rdev->desc->id;
2863}
2864EXPORT_SYMBOL_GPL(rdev_get_id);
2865
Liam Girdwooda5766f12008-10-10 13:22:20 +01002866struct device *rdev_get_dev(struct regulator_dev *rdev)
2867{
2868 return &rdev->dev;
2869}
2870EXPORT_SYMBOL_GPL(rdev_get_dev);
2871
2872void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2873{
2874 return reg_init_data->driver_data;
2875}
2876EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2877
Liam Girdwood414c70c2008-04-30 15:59:04 +01002878static int __init regulator_init(void)
2879{
Mark Brown34abbd62010-02-12 10:18:08 +00002880 int ret;
2881
Mark Brown34abbd62010-02-12 10:18:08 +00002882 ret = class_register(&regulator_class);
2883
Mark Brown1130e5b2010-12-21 23:49:31 +00002884#ifdef CONFIG_DEBUG_FS
2885 debugfs_root = debugfs_create_dir("regulator", NULL);
2886 if (IS_ERR(debugfs_root) || !debugfs_root) {
2887 pr_warn("regulator: Failed to create debugfs directory\n");
2888 debugfs_root = NULL;
2889 }
2890#endif
2891
Mark Brown34abbd62010-02-12 10:18:08 +00002892 regulator_dummy_init();
2893
2894 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002895}
2896
2897/* init early to allow our consumers to complete system booting */
2898core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002899
2900static int __init regulator_init_complete(void)
2901{
2902 struct regulator_dev *rdev;
2903 struct regulator_ops *ops;
2904 struct regulation_constraints *c;
2905 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002906
2907 mutex_lock(&regulator_list_mutex);
2908
2909 /* If we have a full configuration then disable any regulators
2910 * which are not in use or always_on. This will become the
2911 * default behaviour in the future.
2912 */
2913 list_for_each_entry(rdev, &regulator_list, list) {
2914 ops = rdev->desc->ops;
2915 c = rdev->constraints;
2916
Mark Brownf25e0b42009-08-03 18:49:55 +01002917 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002918 continue;
2919
2920 mutex_lock(&rdev->mutex);
2921
2922 if (rdev->use_count)
2923 goto unlock;
2924
2925 /* If we can't read the status assume it's on. */
2926 if (ops->is_enabled)
2927 enabled = ops->is_enabled(rdev);
2928 else
2929 enabled = 1;
2930
2931 if (!enabled)
2932 goto unlock;
2933
2934 if (has_full_constraints) {
2935 /* We log since this may kill the system if it
2936 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002937 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002938 ret = ops->disable(rdev);
2939 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002940 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002941 }
2942 } else {
2943 /* The intention is that in future we will
2944 * assume that full constraints are provided
2945 * so warn even if we aren't going to do
2946 * anything here.
2947 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002948 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002949 }
2950
2951unlock:
2952 mutex_unlock(&rdev->mutex);
2953 }
2954
2955 mutex_unlock(&regulator_list_mutex);
2956
2957 return 0;
2958}
2959late_initcall(regulator_init_complete);