blob: 7287000595e503b29880d64131dfee25b088dd3f [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) {
161 if (*max_uV > regulator->max_uV)
162 *max_uV = regulator->max_uV;
163 if (*min_uV < regulator->min_uV)
164 *min_uV = regulator->min_uV;
165 }
166
167 if (*min_uV > *max_uV)
168 return -EINVAL;
169
170 return 0;
171}
172
Liam Girdwood414c70c2008-04-30 15:59:04 +0100173/* current constraint check */
174static int regulator_check_current_limit(struct regulator_dev *rdev,
175 int *min_uA, int *max_uA)
176{
177 BUG_ON(*min_uA > *max_uA);
178
179 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800180 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 return -ENODEV;
182 }
183 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800184 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100185 return -EPERM;
186 }
187
188 if (*max_uA > rdev->constraints->max_uA)
189 *max_uA = rdev->constraints->max_uA;
190 if (*min_uA < rdev->constraints->min_uA)
191 *min_uA = rdev->constraints->min_uA;
192
193 if (*min_uA > *max_uA)
194 return -EINVAL;
195
196 return 0;
197}
198
199/* operating mode constraint check */
Mark Brown2c608232011-03-30 06:29:12 +0900200static int regulator_mode_constrain(struct regulator_dev *rdev, int *mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100201{
Mark Brown2c608232011-03-30 06:29:12 +0900202 switch (*mode) {
David Brownelle5735202008-11-16 11:46:56 -0800203 case REGULATOR_MODE_FAST:
204 case REGULATOR_MODE_NORMAL:
205 case REGULATOR_MODE_IDLE:
206 case REGULATOR_MODE_STANDBY:
207 break;
208 default:
209 return -EINVAL;
210 }
211
Liam Girdwood414c70c2008-04-30 15:59:04 +0100212 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800213 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100214 return -ENODEV;
215 }
216 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800217 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100218 return -EPERM;
219 }
Mark Brown2c608232011-03-30 06:29:12 +0900220
221 /* The modes are bitmasks, the most power hungry modes having
222 * the lowest values. If the requested mode isn't supported
223 * try higher modes. */
224 while (*mode) {
225 if (rdev->constraints->valid_modes_mask & *mode)
226 return 0;
227 *mode /= 2;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228 }
Mark Brown2c608232011-03-30 06:29:12 +0900229
230 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100231}
232
233/* dynamic regulator mode switching constraint check */
234static int regulator_check_drms(struct regulator_dev *rdev)
235{
236 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800237 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238 return -ENODEV;
239 }
240 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800241 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100242 return -EPERM;
243 }
244 return 0;
245}
246
247static ssize_t device_requested_uA_show(struct device *dev,
248 struct device_attribute *attr, char *buf)
249{
250 struct regulator *regulator;
251
252 regulator = get_device_regulator(dev);
253 if (regulator == NULL)
254 return 0;
255
256 return sprintf(buf, "%d\n", regulator->uA_load);
257}
258
259static ssize_t regulator_uV_show(struct device *dev,
260 struct device_attribute *attr, char *buf)
261{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100262 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100263 ssize_t ret;
264
265 mutex_lock(&rdev->mutex);
266 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
267 mutex_unlock(&rdev->mutex);
268
269 return ret;
270}
David Brownell7ad68e22008-11-11 17:39:02 -0800271static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272
273static ssize_t regulator_uA_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
275{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100276 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277
278 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
279}
David Brownell7ad68e22008-11-11 17:39:02 -0800280static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100281
Mark Brownbc558a62008-10-10 15:33:20 +0100282static ssize_t regulator_name_show(struct device *dev,
283 struct device_attribute *attr, char *buf)
284{
285 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100286
Mark Brown1083c392009-10-22 16:31:32 +0100287 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100288}
289
David Brownell4fca9542008-11-11 17:38:53 -0800290static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100291{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100292 switch (mode) {
293 case REGULATOR_MODE_FAST:
294 return sprintf(buf, "fast\n");
295 case REGULATOR_MODE_NORMAL:
296 return sprintf(buf, "normal\n");
297 case REGULATOR_MODE_IDLE:
298 return sprintf(buf, "idle\n");
299 case REGULATOR_MODE_STANDBY:
300 return sprintf(buf, "standby\n");
301 }
302 return sprintf(buf, "unknown\n");
303}
304
David Brownell4fca9542008-11-11 17:38:53 -0800305static ssize_t regulator_opmode_show(struct device *dev,
306 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100307{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100308 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100309
David Brownell4fca9542008-11-11 17:38:53 -0800310 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
311}
David Brownell7ad68e22008-11-11 17:39:02 -0800312static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800313
314static ssize_t regulator_print_state(char *buf, int state)
315{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100316 if (state > 0)
317 return sprintf(buf, "enabled\n");
318 else if (state == 0)
319 return sprintf(buf, "disabled\n");
320 else
321 return sprintf(buf, "unknown\n");
322}
323
David Brownell4fca9542008-11-11 17:38:53 -0800324static ssize_t regulator_state_show(struct device *dev,
325 struct device_attribute *attr, char *buf)
326{
327 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100328 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800329
Mark Brown93325462009-08-03 18:49:56 +0100330 mutex_lock(&rdev->mutex);
331 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
332 mutex_unlock(&rdev->mutex);
333
334 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800335}
David Brownell7ad68e22008-11-11 17:39:02 -0800336static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800337
David Brownell853116a2009-01-14 23:03:17 -0800338static ssize_t regulator_status_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
340{
341 struct regulator_dev *rdev = dev_get_drvdata(dev);
342 int status;
343 char *label;
344
345 status = rdev->desc->ops->get_status(rdev);
346 if (status < 0)
347 return status;
348
349 switch (status) {
350 case REGULATOR_STATUS_OFF:
351 label = "off";
352 break;
353 case REGULATOR_STATUS_ON:
354 label = "on";
355 break;
356 case REGULATOR_STATUS_ERROR:
357 label = "error";
358 break;
359 case REGULATOR_STATUS_FAST:
360 label = "fast";
361 break;
362 case REGULATOR_STATUS_NORMAL:
363 label = "normal";
364 break;
365 case REGULATOR_STATUS_IDLE:
366 label = "idle";
367 break;
368 case REGULATOR_STATUS_STANDBY:
369 label = "standby";
370 break;
371 default:
372 return -ERANGE;
373 }
374
375 return sprintf(buf, "%s\n", label);
376}
377static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
378
Liam Girdwood414c70c2008-04-30 15:59:04 +0100379static ssize_t regulator_min_uA_show(struct device *dev,
380 struct device_attribute *attr, char *buf)
381{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100382 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100383
384 if (!rdev->constraints)
385 return sprintf(buf, "constraint not defined\n");
386
387 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
388}
David Brownell7ad68e22008-11-11 17:39:02 -0800389static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100390
391static ssize_t regulator_max_uA_show(struct device *dev,
392 struct device_attribute *attr, char *buf)
393{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100394 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100395
396 if (!rdev->constraints)
397 return sprintf(buf, "constraint not defined\n");
398
399 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
400}
David Brownell7ad68e22008-11-11 17:39:02 -0800401static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100402
403static ssize_t regulator_min_uV_show(struct device *dev,
404 struct device_attribute *attr, char *buf)
405{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100406 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100407
408 if (!rdev->constraints)
409 return sprintf(buf, "constraint not defined\n");
410
411 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
412}
David Brownell7ad68e22008-11-11 17:39:02 -0800413static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414
415static ssize_t regulator_max_uV_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 if (!rdev->constraints)
421 return sprintf(buf, "constraint not defined\n");
422
423 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100426
427static ssize_t regulator_total_uA_show(struct device *dev,
428 struct device_attribute *attr, char *buf)
429{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100430 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431 struct regulator *regulator;
432 int uA = 0;
433
434 mutex_lock(&rdev->mutex);
435 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100436 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437 mutex_unlock(&rdev->mutex);
438 return sprintf(buf, "%d\n", uA);
439}
David Brownell7ad68e22008-11-11 17:39:02 -0800440static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441
442static ssize_t regulator_num_users_show(struct device *dev,
443 struct device_attribute *attr, char *buf)
444{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100445 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100446 return sprintf(buf, "%d\n", rdev->use_count);
447}
448
449static ssize_t regulator_type_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
454 switch (rdev->desc->type) {
455 case REGULATOR_VOLTAGE:
456 return sprintf(buf, "voltage\n");
457 case REGULATOR_CURRENT:
458 return sprintf(buf, "current\n");
459 }
460 return sprintf(buf, "unknown\n");
461}
462
463static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
464 struct device_attribute *attr, char *buf)
465{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100466 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100467
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
469}
David Brownell7ad68e22008-11-11 17:39:02 -0800470static DEVICE_ATTR(suspend_mem_microvolts, 0444,
471 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472
473static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
474 struct device_attribute *attr, char *buf)
475{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100476 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
Liam Girdwood414c70c2008-04-30 15:59:04 +0100478 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
479}
David Brownell7ad68e22008-11-11 17:39:02 -0800480static DEVICE_ATTR(suspend_disk_microvolts, 0444,
481 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
489}
David Brownell7ad68e22008-11-11 17:39:02 -0800490static DEVICE_ATTR(suspend_standby_microvolts, 0444,
491 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100492
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100496 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
David Brownell4fca9542008-11-11 17:38:53 -0800498 return regulator_print_opmode(buf,
499 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100500}
David Brownell7ad68e22008-11-11 17:39:02 -0800501static DEVICE_ATTR(suspend_mem_mode, 0444,
502 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100503
504static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
505 struct device_attribute *attr, char *buf)
506{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100507 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100508
David Brownell4fca9542008-11-11 17:38:53 -0800509 return regulator_print_opmode(buf,
510 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100511}
David Brownell7ad68e22008-11-11 17:39:02 -0800512static DEVICE_ATTR(suspend_disk_mode, 0444,
513 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514
515static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
516 struct device_attribute *attr, char *buf)
517{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100518 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519
David Brownell4fca9542008-11-11 17:38:53 -0800520 return regulator_print_opmode(buf,
521 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100522}
David Brownell7ad68e22008-11-11 17:39:02 -0800523static DEVICE_ATTR(suspend_standby_mode, 0444,
524 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100525
526static ssize_t regulator_suspend_mem_state_show(struct device *dev,
527 struct device_attribute *attr, char *buf)
528{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100529 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530
David Brownell4fca9542008-11-11 17:38:53 -0800531 return regulator_print_state(buf,
532 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100533}
David Brownell7ad68e22008-11-11 17:39:02 -0800534static DEVICE_ATTR(suspend_mem_state, 0444,
535 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536
537static ssize_t regulator_suspend_disk_state_show(struct device *dev,
538 struct device_attribute *attr, char *buf)
539{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100540 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100541
David Brownell4fca9542008-11-11 17:38:53 -0800542 return regulator_print_state(buf,
543 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100544}
David Brownell7ad68e22008-11-11 17:39:02 -0800545static DEVICE_ATTR(suspend_disk_state, 0444,
546 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100547
548static ssize_t regulator_suspend_standby_state_show(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100551 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100552
David Brownell4fca9542008-11-11 17:38:53 -0800553 return regulator_print_state(buf,
554 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555}
David Brownell7ad68e22008-11-11 17:39:02 -0800556static DEVICE_ATTR(suspend_standby_state, 0444,
557 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100558
David Brownell7ad68e22008-11-11 17:39:02 -0800559
560/*
561 * These are the only attributes are present for all regulators.
562 * Other attributes are a function of regulator functionality.
563 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100564static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100565 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100566 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
567 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100568 __ATTR_NULL,
569};
570
571static void regulator_dev_release(struct device *dev)
572{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100573 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100574 kfree(rdev);
575}
576
577static struct class regulator_class = {
578 .name = "regulator",
579 .dev_release = regulator_dev_release,
580 .dev_attrs = regulator_dev_attrs,
581};
582
583/* Calculate the new optimum regulator operating mode based on the new total
584 * consumer load. All locks held by caller */
585static void drms_uA_update(struct regulator_dev *rdev)
586{
587 struct regulator *sibling;
588 int current_uA = 0, output_uV, input_uV, err;
589 unsigned int mode;
590
591 err = regulator_check_drms(rdev);
592 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Mark Brown476c2d82010-12-10 17:28:07 +0000593 (!rdev->desc->ops->get_voltage &&
594 !rdev->desc->ops->get_voltage_sel) ||
595 !rdev->desc->ops->set_mode)
Dan Carpenter036de8e2009-04-08 13:52:39 +0300596 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100597
598 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000599 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600 if (output_uV <= 0)
601 return;
602
603 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000604 input_uV = 0;
605 if (rdev->supply)
606 input_uV = _regulator_get_voltage(rdev);
607 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100608 input_uV = rdev->constraints->input_uV;
609 if (input_uV <= 0)
610 return;
611
612 /* calc total requested load */
613 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100614 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615
616 /* now get the optimum mode for our new total regulator load */
617 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
618 output_uV, current_uA);
619
620 /* check the new mode is allowed */
Mark Brown2c608232011-03-30 06:29:12 +0900621 err = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622 if (err == 0)
623 rdev->desc->ops->set_mode(rdev, mode);
624}
625
626static int suspend_set_state(struct regulator_dev *rdev,
627 struct regulator_state *rstate)
628{
629 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100630 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100631
Mark Brown638f85c2009-10-22 16:31:33 +0100632 can_set_state = rdev->desc->ops->set_suspend_enable &&
633 rdev->desc->ops->set_suspend_disable;
634
635 /* If we have no suspend mode configration don't set anything;
636 * only warn if the driver actually makes the suspend mode
637 * configurable.
638 */
639 if (!rstate->enabled && !rstate->disabled) {
640 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800641 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100642 return 0;
643 }
644
645 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800646 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100647 return -EINVAL;
648 }
649
650 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800651 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100653 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100654
655 if (rstate->enabled)
656 ret = rdev->desc->ops->set_suspend_enable(rdev);
657 else
658 ret = rdev->desc->ops->set_suspend_disable(rdev);
659 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800660 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661 return ret;
662 }
663
664 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
665 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
666 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800667 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100668 return ret;
669 }
670 }
671
672 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
673 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
674 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800675 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676 return ret;
677 }
678 }
679 return ret;
680}
681
682/* locks held by caller */
683static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
684{
685 if (!rdev->constraints)
686 return -EINVAL;
687
688 switch (state) {
689 case PM_SUSPEND_STANDBY:
690 return suspend_set_state(rdev,
691 &rdev->constraints->state_standby);
692 case PM_SUSPEND_MEM:
693 return suspend_set_state(rdev,
694 &rdev->constraints->state_mem);
695 case PM_SUSPEND_MAX:
696 return suspend_set_state(rdev,
697 &rdev->constraints->state_disk);
698 default:
699 return -EINVAL;
700 }
701}
702
703static void print_constraints(struct regulator_dev *rdev)
704{
705 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000706 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100707 int count = 0;
708 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100709
Mark Brown8f031b42009-10-22 16:31:31 +0100710 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100711 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100712 count += sprintf(buf + count, "%d mV ",
713 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100714 else
Mark Brown8f031b42009-10-22 16:31:31 +0100715 count += sprintf(buf + count, "%d <--> %d mV ",
716 constraints->min_uV / 1000,
717 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100718 }
Mark Brown8f031b42009-10-22 16:31:31 +0100719
720 if (!constraints->min_uV ||
721 constraints->min_uV != constraints->max_uV) {
722 ret = _regulator_get_voltage(rdev);
723 if (ret > 0)
724 count += sprintf(buf + count, "at %d mV ", ret / 1000);
725 }
726
Mark Brownbf5892a2011-05-08 22:13:37 +0100727 if (constraints->uV_offset)
728 count += sprintf(buf, "%dmV offset ",
729 constraints->uV_offset / 1000);
730
Mark Brown8f031b42009-10-22 16:31:31 +0100731 if (constraints->min_uA && constraints->max_uA) {
732 if (constraints->min_uA == constraints->max_uA)
733 count += sprintf(buf + count, "%d mA ",
734 constraints->min_uA / 1000);
735 else
736 count += sprintf(buf + count, "%d <--> %d mA ",
737 constraints->min_uA / 1000,
738 constraints->max_uA / 1000);
739 }
740
741 if (!constraints->min_uA ||
742 constraints->min_uA != constraints->max_uA) {
743 ret = _regulator_get_current_limit(rdev);
744 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400745 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100746 }
747
Liam Girdwood414c70c2008-04-30 15:59:04 +0100748 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
749 count += sprintf(buf + count, "fast ");
750 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
751 count += sprintf(buf + count, "normal ");
752 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
753 count += sprintf(buf + count, "idle ");
754 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
755 count += sprintf(buf + count, "standby");
756
Mark Brown13ce29f2010-12-17 16:04:12 +0000757 rdev_info(rdev, "%s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100758}
759
Mark Browne79055d2009-10-19 15:53:50 +0100760static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100761 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100762{
763 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100764 int ret;
765
766 /* do we need to apply the constraint voltage */
767 if (rdev->constraints->apply_uV &&
Mark Brown75790252010-12-12 14:25:50 +0000768 rdev->constraints->min_uV == rdev->constraints->max_uV) {
769 ret = _regulator_do_set_voltage(rdev,
770 rdev->constraints->min_uV,
771 rdev->constraints->max_uV);
772 if (ret < 0) {
773 rdev_err(rdev, "failed to apply %duV constraint\n",
774 rdev->constraints->min_uV);
775 rdev->constraints = NULL;
776 return ret;
777 }
Mark Brownaf5866c2009-10-22 16:31:30 +0100778 }
Mark Browne79055d2009-10-19 15:53:50 +0100779
780 /* constrain machine-level voltage specs to fit
781 * the actual range supported by this regulator.
782 */
783 if (ops->list_voltage && rdev->desc->n_voltages) {
784 int count = rdev->desc->n_voltages;
785 int i;
786 int min_uV = INT_MAX;
787 int max_uV = INT_MIN;
788 int cmin = constraints->min_uV;
789 int cmax = constraints->max_uV;
790
791 /* it's safe to autoconfigure fixed-voltage supplies
792 and the constraints are used by list_voltage. */
793 if (count == 1 && !cmin) {
794 cmin = 1;
795 cmax = INT_MAX;
796 constraints->min_uV = cmin;
797 constraints->max_uV = cmax;
798 }
799
800 /* voltage constraints are optional */
801 if ((cmin == 0) && (cmax == 0))
802 return 0;
803
804 /* else require explicit machine-level constraints */
805 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800806 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100807 return -EINVAL;
808 }
809
810 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
811 for (i = 0; i < count; i++) {
812 int value;
813
814 value = ops->list_voltage(rdev, i);
815 if (value <= 0)
816 continue;
817
818 /* maybe adjust [min_uV..max_uV] */
819 if (value >= cmin && value < min_uV)
820 min_uV = value;
821 if (value <= cmax && value > max_uV)
822 max_uV = value;
823 }
824
825 /* final: [min_uV..max_uV] valid iff constraints valid */
826 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800827 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100828 return -EINVAL;
829 }
830
831 /* use regulator's subset of machine constraints */
832 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800833 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
834 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100835 constraints->min_uV = min_uV;
836 }
837 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800838 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
839 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100840 constraints->max_uV = max_uV;
841 }
842 }
843
844 return 0;
845}
846
Liam Girdwooda5766f12008-10-10 13:22:20 +0100847/**
848 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000849 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000850 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100851 *
852 * Allows platform initialisation code to define and constrain
853 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
854 * Constraints *must* be set by platform code in order for some
855 * regulator operations to proceed i.e. set_voltage, set_current_limit,
856 * set_mode.
857 */
858static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000859 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100860{
861 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100862 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100863
Mark Brownf8c12fe2010-11-29 15:55:17 +0000864 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
865 GFP_KERNEL);
866 if (!rdev->constraints)
867 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100868
Mark Brownf8c12fe2010-11-29 15:55:17 +0000869 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100870 if (ret != 0)
871 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800872
Liam Girdwooda5766f12008-10-10 13:22:20 +0100873 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100874 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000875 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100876 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800877 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100878 rdev->constraints = NULL;
879 goto out;
880 }
881 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100882
Mark Browna3084662009-02-26 19:24:19 +0000883 if (constraints->initial_mode) {
884 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800885 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000886 ret = -EINVAL;
887 goto out;
888 }
889
Mark Brownf8c12fe2010-11-29 15:55:17 +0000890 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000891 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800892 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000893 goto out;
894 }
895 }
896
Mark Browncacf90f2009-03-02 16:32:46 +0000897 /* If the constraints say the regulator should be on at this point
898 * and we have control then make sure it is enabled.
899 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000900 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
901 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100902 ret = ops->enable(rdev);
903 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800904 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100905 rdev->constraints = NULL;
906 goto out;
907 }
908 }
909
Liam Girdwooda5766f12008-10-10 13:22:20 +0100910 print_constraints(rdev);
911out:
912 return ret;
913}
914
915/**
916 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000917 * @rdev: regulator name
918 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100919 *
920 * Called by platform initialisation code to set the supply regulator for this
921 * regulator. This ensures that a regulators supply will also be enabled by the
922 * core if it's child is enabled.
923 */
924static int set_supply(struct regulator_dev *rdev,
925 struct regulator_dev *supply_rdev)
926{
927 int err;
928
929 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
930 "supply");
931 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800932 rdev_err(rdev, "could not add device link %s err %d\n",
933 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100934 goto out;
935 }
936 rdev->supply = supply_rdev;
937 list_add(&rdev->slist, &supply_rdev->supply_list);
938out:
939 return err;
940}
941
942/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800943 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000944 * @rdev: regulator source
945 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100946 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000947 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100948 *
949 * Allows platform initialisation code to map physical regulator
950 * sources to symbolic names for supplies for use by devices. Devices
951 * should use these symbolic names to request regulators, avoiding the
952 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100953 *
954 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100955 */
956static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100957 struct device *consumer_dev, const char *consumer_dev_name,
958 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959{
960 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100961 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100962
Mark Brown40f92442009-06-17 17:56:39 +0100963 if (consumer_dev && consumer_dev_name)
964 return -EINVAL;
965
966 if (!consumer_dev_name && consumer_dev)
967 consumer_dev_name = dev_name(consumer_dev);
968
Liam Girdwooda5766f12008-10-10 13:22:20 +0100969 if (supply == NULL)
970 return -EINVAL;
971
Mark Brown9ed20992009-07-21 16:00:26 +0100972 if (consumer_dev_name != NULL)
973 has_dev = 1;
974 else
975 has_dev = 0;
976
David Brownell6001e132008-12-31 12:54:19 +0000977 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300978 if (node->dev_name && consumer_dev_name) {
979 if (strcmp(node->dev_name, consumer_dev_name) != 0)
980 continue;
981 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000982 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300983 }
984
David Brownell6001e132008-12-31 12:54:19 +0000985 if (strcmp(node->supply, supply) != 0)
986 continue;
987
988 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800989 dev_name(&node->regulator->dev),
990 node->regulator->desc->name,
991 supply,
992 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000993 return -EBUSY;
994 }
995
Mark Brown9ed20992009-07-21 16:00:26 +0100996 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100997 if (node == NULL)
998 return -ENOMEM;
999
1000 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001001 node->supply = supply;
1002
Mark Brown9ed20992009-07-21 16:00:26 +01001003 if (has_dev) {
1004 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
1005 if (node->dev_name == NULL) {
1006 kfree(node);
1007 return -ENOMEM;
1008 }
Mark Brown40f92442009-06-17 17:56:39 +01001009 }
1010
Liam Girdwooda5766f12008-10-10 13:22:20 +01001011 list_add(&node->list, &regulator_map_list);
1012 return 0;
1013}
1014
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001015static void unset_regulator_supplies(struct regulator_dev *rdev)
1016{
1017 struct regulator_map *node, *n;
1018
1019 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1020 if (rdev == node->regulator) {
1021 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001022 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001023 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001024 }
1025 }
1026}
1027
Liam Girdwood414c70c2008-04-30 15:59:04 +01001028#define REG_STR_SIZE 32
1029
1030static struct regulator *create_regulator(struct regulator_dev *rdev,
1031 struct device *dev,
1032 const char *supply_name)
1033{
1034 struct regulator *regulator;
1035 char buf[REG_STR_SIZE];
1036 int err, size;
1037
1038 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1039 if (regulator == NULL)
1040 return NULL;
1041
1042 mutex_lock(&rdev->mutex);
1043 regulator->rdev = rdev;
1044 list_add(&regulator->list, &rdev->consumer_list);
1045
1046 if (dev) {
1047 /* create a 'requested_microamps_name' sysfs entry */
1048 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1049 supply_name);
1050 if (size >= REG_STR_SIZE)
1051 goto overflow_err;
1052
1053 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001054 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001055 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1056 if (regulator->dev_attr.attr.name == NULL)
1057 goto attr_name_err;
1058
Liam Girdwood414c70c2008-04-30 15:59:04 +01001059 regulator->dev_attr.attr.mode = 0444;
1060 regulator->dev_attr.show = device_requested_uA_show;
1061 err = device_create_file(dev, &regulator->dev_attr);
1062 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001063 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001064 goto attr_name_err;
1065 }
1066
1067 /* also add a link to the device sysfs entry */
1068 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1069 dev->kobj.name, supply_name);
1070 if (size >= REG_STR_SIZE)
1071 goto attr_err;
1072
1073 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1074 if (regulator->supply_name == NULL)
1075 goto attr_err;
1076
1077 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1078 buf);
1079 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001080 rdev_warn(rdev, "could not add device link %s err %d\n",
1081 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001082 goto link_name_err;
1083 }
1084 }
1085 mutex_unlock(&rdev->mutex);
1086 return regulator;
1087link_name_err:
1088 kfree(regulator->supply_name);
1089attr_err:
1090 device_remove_file(regulator->dev, &regulator->dev_attr);
1091attr_name_err:
1092 kfree(regulator->dev_attr.attr.name);
1093overflow_err:
1094 list_del(&regulator->list);
1095 kfree(regulator);
1096 mutex_unlock(&rdev->mutex);
1097 return NULL;
1098}
1099
Mark Brown31aae2b2009-12-21 12:21:52 +00001100static int _regulator_get_enable_time(struct regulator_dev *rdev)
1101{
1102 if (!rdev->desc->ops->enable_time)
1103 return 0;
1104 return rdev->desc->ops->enable_time(rdev);
1105}
1106
Mark Brown5ffbd132009-07-21 16:00:23 +01001107/* Internal regulator request function */
1108static struct regulator *_regulator_get(struct device *dev, const char *id,
1109 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110{
1111 struct regulator_dev *rdev;
1112 struct regulator_map *map;
1113 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001114 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001115 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116
1117 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001118 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119 return regulator;
1120 }
1121
Mark Brown40f92442009-06-17 17:56:39 +01001122 if (dev)
1123 devname = dev_name(dev);
1124
Liam Girdwood414c70c2008-04-30 15:59:04 +01001125 mutex_lock(&regulator_list_mutex);
1126
1127 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001128 /* If the mapping has a device set up it must match */
1129 if (map->dev_name &&
1130 (!devname || strcmp(map->dev_name, devname)))
1131 continue;
1132
1133 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001134 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001135 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001136 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001137 }
Mark Brown34abbd62010-02-12 10:18:08 +00001138
Mark Brown688fe992010-10-05 19:18:32 -07001139 if (board_wants_dummy_regulator) {
1140 rdev = dummy_regulator_rdev;
1141 goto found;
1142 }
1143
Mark Brown34abbd62010-02-12 10:18:08 +00001144#ifdef CONFIG_REGULATOR_DUMMY
1145 if (!devname)
1146 devname = "deviceless";
1147
1148 /* If the board didn't flag that it was fully constrained then
1149 * substitute in a dummy regulator so consumers can continue.
1150 */
1151 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001152 pr_warn("%s supply %s not found, using dummy regulator\n",
1153 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001154 rdev = dummy_regulator_rdev;
1155 goto found;
1156 }
1157#endif
1158
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 mutex_unlock(&regulator_list_mutex);
1160 return regulator;
1161
1162found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001163 if (rdev->exclusive) {
1164 regulator = ERR_PTR(-EPERM);
1165 goto out;
1166 }
1167
1168 if (exclusive && rdev->open_count) {
1169 regulator = ERR_PTR(-EBUSY);
1170 goto out;
1171 }
1172
Liam Girdwooda5766f12008-10-10 13:22:20 +01001173 if (!try_module_get(rdev->owner))
1174 goto out;
1175
Liam Girdwood414c70c2008-04-30 15:59:04 +01001176 regulator = create_regulator(rdev, dev, id);
1177 if (regulator == NULL) {
1178 regulator = ERR_PTR(-ENOMEM);
1179 module_put(rdev->owner);
1180 }
1181
Mark Brown5ffbd132009-07-21 16:00:23 +01001182 rdev->open_count++;
1183 if (exclusive) {
1184 rdev->exclusive = 1;
1185
1186 ret = _regulator_is_enabled(rdev);
1187 if (ret > 0)
1188 rdev->use_count = 1;
1189 else
1190 rdev->use_count = 0;
1191 }
1192
Liam Girdwooda5766f12008-10-10 13:22:20 +01001193out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001194 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001195
Liam Girdwood414c70c2008-04-30 15:59:04 +01001196 return regulator;
1197}
Mark Brown5ffbd132009-07-21 16:00:23 +01001198
1199/**
1200 * regulator_get - lookup and obtain a reference to a regulator.
1201 * @dev: device for regulator "consumer"
1202 * @id: Supply name or regulator ID.
1203 *
1204 * Returns a struct regulator corresponding to the regulator producer,
1205 * or IS_ERR() condition containing errno.
1206 *
1207 * Use of supply names configured via regulator_set_device_supply() is
1208 * strongly encouraged. It is recommended that the supply name used
1209 * should match the name used for the supply and/or the relevant
1210 * device pins in the datasheet.
1211 */
1212struct regulator *regulator_get(struct device *dev, const char *id)
1213{
1214 return _regulator_get(dev, id, 0);
1215}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001216EXPORT_SYMBOL_GPL(regulator_get);
1217
1218/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001219 * regulator_get_exclusive - obtain exclusive access to a regulator.
1220 * @dev: device for regulator "consumer"
1221 * @id: Supply name or regulator ID.
1222 *
1223 * Returns a struct regulator corresponding to the regulator producer,
1224 * or IS_ERR() condition containing errno. Other consumers will be
1225 * unable to obtain this reference is held and the use count for the
1226 * regulator will be initialised to reflect the current state of the
1227 * regulator.
1228 *
1229 * This is intended for use by consumers which cannot tolerate shared
1230 * use of the regulator such as those which need to force the
1231 * regulator off for correct operation of the hardware they are
1232 * controlling.
1233 *
1234 * Use of supply names configured via regulator_set_device_supply() is
1235 * strongly encouraged. It is recommended that the supply name used
1236 * should match the name used for the supply and/or the relevant
1237 * device pins in the datasheet.
1238 */
1239struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1240{
1241 return _regulator_get(dev, id, 1);
1242}
1243EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1244
1245/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001246 * regulator_put - "free" the regulator source
1247 * @regulator: regulator source
1248 *
1249 * Note: drivers must ensure that all regulator_enable calls made on this
1250 * regulator source are balanced by regulator_disable calls prior to calling
1251 * this function.
1252 */
1253void regulator_put(struct regulator *regulator)
1254{
1255 struct regulator_dev *rdev;
1256
1257 if (regulator == NULL || IS_ERR(regulator))
1258 return;
1259
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260 mutex_lock(&regulator_list_mutex);
1261 rdev = regulator->rdev;
1262
1263 /* remove any sysfs entries */
1264 if (regulator->dev) {
1265 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1266 kfree(regulator->supply_name);
1267 device_remove_file(regulator->dev, &regulator->dev_attr);
1268 kfree(regulator->dev_attr.attr.name);
1269 }
1270 list_del(&regulator->list);
1271 kfree(regulator);
1272
Mark Brown5ffbd132009-07-21 16:00:23 +01001273 rdev->open_count--;
1274 rdev->exclusive = 0;
1275
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276 module_put(rdev->owner);
1277 mutex_unlock(&regulator_list_mutex);
1278}
1279EXPORT_SYMBOL_GPL(regulator_put);
1280
Mark Brown9a2372f2009-08-03 18:49:57 +01001281static int _regulator_can_change_status(struct regulator_dev *rdev)
1282{
1283 if (!rdev->constraints)
1284 return 0;
1285
1286 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1287 return 1;
1288 else
1289 return 0;
1290}
1291
Liam Girdwood414c70c2008-04-30 15:59:04 +01001292/* locks held by regulator_enable() */
1293static int _regulator_enable(struct regulator_dev *rdev)
1294{
Mark Brown31aae2b2009-12-21 12:21:52 +00001295 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001296
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001297 if (rdev->use_count == 0) {
1298 /* do we need to enable the supply regulator first */
1299 if (rdev->supply) {
1300 mutex_lock(&rdev->supply->mutex);
1301 ret = _regulator_enable(rdev->supply);
1302 mutex_unlock(&rdev->supply->mutex);
1303 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001304 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001305 return ret;
1306 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001307 }
1308 }
1309
1310 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001311 if (rdev->constraints &&
1312 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1313 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001314
Mark Brown9a2372f2009-08-03 18:49:57 +01001315 if (rdev->use_count == 0) {
1316 /* The regulator may on if it's not switchable or left on */
1317 ret = _regulator_is_enabled(rdev);
1318 if (ret == -EINVAL || ret == 0) {
1319 if (!_regulator_can_change_status(rdev))
1320 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001321
Mark Brown31aae2b2009-12-21 12:21:52 +00001322 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001323 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001324
1325 /* Query before enabling in case configuration
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001326 * dependent. */
Mark Brown31aae2b2009-12-21 12:21:52 +00001327 ret = _regulator_get_enable_time(rdev);
1328 if (ret >= 0) {
1329 delay = ret;
1330 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001331 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001332 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001333 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001334 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001335
Mark Brown02fa3ec2010-11-10 14:38:30 +00001336 trace_regulator_enable(rdev_get_name(rdev));
1337
Mark Brown31aae2b2009-12-21 12:21:52 +00001338 /* Allow the regulator to ramp; it would be useful
1339 * to extend this for bulk operations so that the
1340 * regulators can ramp together. */
1341 ret = rdev->desc->ops->enable(rdev);
1342 if (ret < 0)
1343 return ret;
1344
Mark Brown02fa3ec2010-11-10 14:38:30 +00001345 trace_regulator_enable_delay(rdev_get_name(rdev));
1346
Axel Line36c1df2010-11-05 21:51:32 +08001347 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001348 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001349 udelay(delay % 1000);
1350 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001351 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001352 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001353
Mark Brown02fa3ec2010-11-10 14:38:30 +00001354 trace_regulator_enable_complete(rdev_get_name(rdev));
1355
Linus Walleija7433cf2009-08-26 12:54:04 +02001356 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001357 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001358 return ret;
1359 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001360 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 }
1362
Mark Brown9a2372f2009-08-03 18:49:57 +01001363 rdev->use_count++;
1364
1365 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366}
1367
1368/**
1369 * regulator_enable - enable regulator output
1370 * @regulator: regulator source
1371 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001372 * Request that the regulator be enabled with the regulator output at
1373 * the predefined voltage or current value. Calls to regulator_enable()
1374 * must be balanced with calls to regulator_disable().
1375 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001376 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001377 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378 */
1379int regulator_enable(struct regulator *regulator)
1380{
David Brownell412aec62008-11-16 11:44:46 -08001381 struct regulator_dev *rdev = regulator->rdev;
1382 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001383
David Brownell412aec62008-11-16 11:44:46 -08001384 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001385 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001386 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387 return ret;
1388}
1389EXPORT_SYMBOL_GPL(regulator_enable);
1390
1391/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001392static int _regulator_disable(struct regulator_dev *rdev,
1393 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001394{
1395 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001396 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397
David Brownellcd94b502009-03-11 16:43:34 -08001398 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001399 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001400 return -EIO;
1401
Liam Girdwood414c70c2008-04-30 15:59:04 +01001402 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001403 if (rdev->use_count == 1 &&
1404 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405
1406 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001407 if (_regulator_can_change_status(rdev) &&
1408 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001409 trace_regulator_disable(rdev_get_name(rdev));
1410
Liam Girdwood414c70c2008-04-30 15:59:04 +01001411 ret = rdev->desc->ops->disable(rdev);
1412 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001413 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001414 return ret;
1415 }
Mark Brown84b68262009-12-01 21:12:27 +00001416
Mark Brown02fa3ec2010-11-10 14:38:30 +00001417 trace_regulator_disable_complete(rdev_get_name(rdev));
1418
Mark Brown84b68262009-12-01 21:12:27 +00001419 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1420 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001421 }
1422
1423 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001424 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425
1426 rdev->use_count = 0;
1427 } else if (rdev->use_count > 1) {
1428
1429 if (rdev->constraints &&
1430 (rdev->constraints->valid_ops_mask &
1431 REGULATOR_CHANGE_DRMS))
1432 drms_uA_update(rdev);
1433
1434 rdev->use_count--;
1435 }
1436 return ret;
1437}
1438
1439/**
1440 * regulator_disable - disable regulator output
1441 * @regulator: regulator source
1442 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001443 * Disable the regulator output voltage or current. Calls to
1444 * regulator_enable() must be balanced with calls to
1445 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001446 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001447 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001448 * devices have it enabled, the regulator device supports disabling and
1449 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001450 */
1451int regulator_disable(struct regulator *regulator)
1452{
David Brownell412aec62008-11-16 11:44:46 -08001453 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001454 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001455 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001456
David Brownell412aec62008-11-16 11:44:46 -08001457 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001458 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001459 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001460
1461 /* decrease our supplies ref count and disable if required */
1462 while (supply_rdev != NULL) {
1463 rdev = supply_rdev;
1464
1465 mutex_lock(&rdev->mutex);
1466 _regulator_disable(rdev, &supply_rdev);
1467 mutex_unlock(&rdev->mutex);
1468 }
1469
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470 return ret;
1471}
1472EXPORT_SYMBOL_GPL(regulator_disable);
1473
1474/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001475static int _regulator_force_disable(struct regulator_dev *rdev,
1476 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001477{
1478 int ret = 0;
1479
1480 /* force disable */
1481 if (rdev->desc->ops->disable) {
1482 /* ah well, who wants to live forever... */
1483 ret = rdev->desc->ops->disable(rdev);
1484 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001485 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001486 return ret;
1487 }
1488 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001489 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1490 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001491 }
1492
1493 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001494 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001495
1496 rdev->use_count = 0;
1497 return ret;
1498}
1499
1500/**
1501 * regulator_force_disable - force disable regulator output
1502 * @regulator: regulator source
1503 *
1504 * Forcibly disable the regulator output voltage or current.
1505 * NOTE: this *will* disable the regulator output even if other consumer
1506 * devices have it enabled. This should be used for situations when device
1507 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1508 */
1509int regulator_force_disable(struct regulator *regulator)
1510{
Mark Brown82d15832011-05-09 11:41:02 +02001511 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001512 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001513 int ret;
1514
Mark Brown82d15832011-05-09 11:41:02 +02001515 mutex_lock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 regulator->uA_load = 0;
Mark Brown82d15832011-05-09 11:41:02 +02001517 ret = _regulator_force_disable(rdev, &supply_rdev);
1518 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001519
1520 if (supply_rdev)
1521 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1522
Liam Girdwood414c70c2008-04-30 15:59:04 +01001523 return ret;
1524}
1525EXPORT_SYMBOL_GPL(regulator_force_disable);
1526
1527static int _regulator_is_enabled(struct regulator_dev *rdev)
1528{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001529 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001530 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001531 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001532
Mark Brown93325462009-08-03 18:49:56 +01001533 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534}
1535
1536/**
1537 * regulator_is_enabled - is the regulator output enabled
1538 * @regulator: regulator source
1539 *
David Brownell412aec62008-11-16 11:44:46 -08001540 * Returns positive if the regulator driver backing the source/client
1541 * has requested that the device be enabled, zero if it hasn't, else a
1542 * negative errno code.
1543 *
1544 * Note that the device backing this regulator handle can have multiple
1545 * users, so it might be enabled even if regulator_enable() was never
1546 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001547 */
1548int regulator_is_enabled(struct regulator *regulator)
1549{
Mark Brown93325462009-08-03 18:49:56 +01001550 int ret;
1551
1552 mutex_lock(&regulator->rdev->mutex);
1553 ret = _regulator_is_enabled(regulator->rdev);
1554 mutex_unlock(&regulator->rdev->mutex);
1555
1556 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001557}
1558EXPORT_SYMBOL_GPL(regulator_is_enabled);
1559
1560/**
David Brownell4367cfd2009-02-26 11:48:36 -08001561 * regulator_count_voltages - count regulator_list_voltage() selectors
1562 * @regulator: regulator source
1563 *
1564 * Returns number of selectors, or negative errno. Selectors are
1565 * numbered starting at zero, and typically correspond to bitfields
1566 * in hardware registers.
1567 */
1568int regulator_count_voltages(struct regulator *regulator)
1569{
1570 struct regulator_dev *rdev = regulator->rdev;
1571
1572 return rdev->desc->n_voltages ? : -EINVAL;
1573}
1574EXPORT_SYMBOL_GPL(regulator_count_voltages);
1575
1576/**
1577 * regulator_list_voltage - enumerate supported voltages
1578 * @regulator: regulator source
1579 * @selector: identify voltage to list
1580 * Context: can sleep
1581 *
1582 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001583 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001584 * negative errno.
1585 */
1586int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1587{
1588 struct regulator_dev *rdev = regulator->rdev;
1589 struct regulator_ops *ops = rdev->desc->ops;
1590 int ret;
1591
1592 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1593 return -EINVAL;
1594
1595 mutex_lock(&rdev->mutex);
1596 ret = ops->list_voltage(rdev, selector);
1597 mutex_unlock(&rdev->mutex);
1598
1599 if (ret > 0) {
1600 if (ret < rdev->constraints->min_uV)
1601 ret = 0;
1602 else if (ret > rdev->constraints->max_uV)
1603 ret = 0;
1604 }
1605
1606 return ret;
1607}
1608EXPORT_SYMBOL_GPL(regulator_list_voltage);
1609
1610/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001611 * regulator_is_supported_voltage - check if a voltage range can be supported
1612 *
1613 * @regulator: Regulator to check.
1614 * @min_uV: Minimum required voltage in uV.
1615 * @max_uV: Maximum required voltage in uV.
1616 *
1617 * Returns a boolean or a negative error code.
1618 */
1619int regulator_is_supported_voltage(struct regulator *regulator,
1620 int min_uV, int max_uV)
1621{
1622 int i, voltages, ret;
1623
1624 ret = regulator_count_voltages(regulator);
1625 if (ret < 0)
1626 return ret;
1627 voltages = ret;
1628
1629 for (i = 0; i < voltages; i++) {
1630 ret = regulator_list_voltage(regulator, i);
1631
1632 if (ret >= min_uV && ret <= max_uV)
1633 return 1;
1634 }
1635
1636 return 0;
1637}
1638
Mark Brown75790252010-12-12 14:25:50 +00001639static int _regulator_do_set_voltage(struct regulator_dev *rdev,
1640 int min_uV, int max_uV)
1641{
1642 int ret;
Linus Walleij77af1b2642011-03-17 13:24:36 +01001643 int delay = 0;
Mark Brown75790252010-12-12 14:25:50 +00001644 unsigned int selector;
1645
1646 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1647
Mark Brownbf5892a2011-05-08 22:13:37 +01001648 min_uV += rdev->constraints->uV_offset;
1649 max_uV += rdev->constraints->uV_offset;
1650
Mark Brown75790252010-12-12 14:25:50 +00001651 if (rdev->desc->ops->set_voltage) {
1652 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV,
1653 &selector);
1654
1655 if (rdev->desc->ops->list_voltage)
1656 selector = rdev->desc->ops->list_voltage(rdev,
1657 selector);
1658 else
1659 selector = -1;
Mark Browne8eef822010-12-12 14:36:17 +00001660 } else if (rdev->desc->ops->set_voltage_sel) {
1661 int best_val = INT_MAX;
1662 int i;
1663
1664 selector = 0;
1665
1666 /* Find the smallest voltage that falls within the specified
1667 * range.
1668 */
1669 for (i = 0; i < rdev->desc->n_voltages; i++) {
1670 ret = rdev->desc->ops->list_voltage(rdev, i);
1671 if (ret < 0)
1672 continue;
1673
1674 if (ret < best_val && ret >= min_uV && ret <= max_uV) {
1675 best_val = ret;
1676 selector = i;
1677 }
1678 }
1679
Linus Walleij77af1b2642011-03-17 13:24:36 +01001680 /*
1681 * If we can't obtain the old selector there is not enough
1682 * info to call set_voltage_time_sel().
1683 */
1684 if (rdev->desc->ops->set_voltage_time_sel &&
1685 rdev->desc->ops->get_voltage_sel) {
1686 unsigned int old_selector = 0;
1687
1688 ret = rdev->desc->ops->get_voltage_sel(rdev);
1689 if (ret < 0)
1690 return ret;
1691 old_selector = ret;
1692 delay = rdev->desc->ops->set_voltage_time_sel(rdev,
1693 old_selector, selector);
1694 }
1695
Mark Browne8eef822010-12-12 14:36:17 +00001696 if (best_val != INT_MAX) {
1697 ret = rdev->desc->ops->set_voltage_sel(rdev, selector);
1698 selector = best_val;
1699 } else {
1700 ret = -EINVAL;
1701 }
Mark Brown75790252010-12-12 14:25:50 +00001702 } else {
1703 ret = -EINVAL;
1704 }
1705
Linus Walleij77af1b2642011-03-17 13:24:36 +01001706 /* Insert any necessary delays */
1707 if (delay >= 1000) {
1708 mdelay(delay / 1000);
1709 udelay(delay % 1000);
1710 } else if (delay) {
1711 udelay(delay);
1712 }
1713
Mark Brownded06a52010-12-16 13:59:10 +00001714 if (ret == 0)
1715 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE,
1716 NULL);
1717
Mark Brown75790252010-12-12 14:25:50 +00001718 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1719
1720 return ret;
1721}
1722
Mark Browna7a1ad92009-07-21 16:00:24 +01001723/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001724 * regulator_set_voltage - set regulator output voltage
1725 * @regulator: regulator source
1726 * @min_uV: Minimum required voltage in uV
1727 * @max_uV: Maximum acceptable voltage in uV
1728 *
1729 * Sets a voltage regulator to the desired output voltage. This can be set
1730 * during any regulator state. IOW, regulator can be disabled or enabled.
1731 *
1732 * If the regulator is enabled then the voltage will change to the new value
1733 * immediately otherwise if the regulator is disabled the regulator will
1734 * output at the new voltage when enabled.
1735 *
1736 * NOTE: If the regulator is shared between several devices then the lowest
1737 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001738 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001739 * calling this function otherwise this call will fail.
1740 */
1741int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1742{
1743 struct regulator_dev *rdev = regulator->rdev;
Mark Brown95a3c232010-12-16 15:49:37 +00001744 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001745
1746 mutex_lock(&rdev->mutex);
1747
Mark Brown95a3c232010-12-16 15:49:37 +00001748 /* If we're setting the same range as last time the change
1749 * should be a noop (some cpufreq implementations use the same
1750 * voltage for multiple frequencies, for example).
1751 */
1752 if (regulator->min_uV == min_uV && regulator->max_uV == max_uV)
1753 goto out;
1754
Liam Girdwood414c70c2008-04-30 15:59:04 +01001755 /* sanity check */
Mark Browne8eef822010-12-12 14:36:17 +00001756 if (!rdev->desc->ops->set_voltage &&
1757 !rdev->desc->ops->set_voltage_sel) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001758 ret = -EINVAL;
1759 goto out;
1760 }
1761
1762 /* constraints check */
1763 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1764 if (ret < 0)
1765 goto out;
1766 regulator->min_uV = min_uV;
1767 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001768
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001769 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1770 if (ret < 0)
1771 goto out;
1772
Mark Brown75790252010-12-12 14:25:50 +00001773 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
Mark Brown02fa3ec2010-11-10 14:38:30 +00001774
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775out:
1776 mutex_unlock(&rdev->mutex);
1777 return ret;
1778}
1779EXPORT_SYMBOL_GPL(regulator_set_voltage);
1780
Mark Brown606a2562010-12-16 15:49:36 +00001781/**
Linus Walleij88cd222b2011-03-17 13:24:52 +01001782 * regulator_set_voltage_time - get raise/fall time
1783 * @regulator: regulator source
1784 * @old_uV: starting voltage in microvolts
1785 * @new_uV: target voltage in microvolts
1786 *
1787 * Provided with the starting and ending voltage, this function attempts to
1788 * calculate the time in microseconds required to rise or fall to this new
1789 * voltage.
1790 */
1791int regulator_set_voltage_time(struct regulator *regulator,
1792 int old_uV, int new_uV)
1793{
1794 struct regulator_dev *rdev = regulator->rdev;
1795 struct regulator_ops *ops = rdev->desc->ops;
1796 int old_sel = -1;
1797 int new_sel = -1;
1798 int voltage;
1799 int i;
1800
1801 /* Currently requires operations to do this */
1802 if (!ops->list_voltage || !ops->set_voltage_time_sel
1803 || !rdev->desc->n_voltages)
1804 return -EINVAL;
1805
1806 for (i = 0; i < rdev->desc->n_voltages; i++) {
1807 /* We only look for exact voltage matches here */
1808 voltage = regulator_list_voltage(regulator, i);
1809 if (voltage < 0)
1810 return -EINVAL;
1811 if (voltage == 0)
1812 continue;
1813 if (voltage == old_uV)
1814 old_sel = i;
1815 if (voltage == new_uV)
1816 new_sel = i;
1817 }
1818
1819 if (old_sel < 0 || new_sel < 0)
1820 return -EINVAL;
1821
1822 return ops->set_voltage_time_sel(rdev, old_sel, new_sel);
1823}
1824EXPORT_SYMBOL_GPL(regulator_set_voltage_time);
1825
1826/**
Mark Brown606a2562010-12-16 15:49:36 +00001827 * regulator_sync_voltage - re-apply last regulator output voltage
1828 * @regulator: regulator source
1829 *
1830 * Re-apply the last configured voltage. This is intended to be used
1831 * where some external control source the consumer is cooperating with
1832 * has caused the configured voltage to change.
1833 */
1834int regulator_sync_voltage(struct regulator *regulator)
1835{
1836 struct regulator_dev *rdev = regulator->rdev;
1837 int ret, min_uV, max_uV;
1838
1839 mutex_lock(&rdev->mutex);
1840
1841 if (!rdev->desc->ops->set_voltage &&
1842 !rdev->desc->ops->set_voltage_sel) {
1843 ret = -EINVAL;
1844 goto out;
1845 }
1846
1847 /* This is only going to work if we've had a voltage configured. */
1848 if (!regulator->min_uV && !regulator->max_uV) {
1849 ret = -EINVAL;
1850 goto out;
1851 }
1852
1853 min_uV = regulator->min_uV;
1854 max_uV = regulator->max_uV;
1855
1856 /* This should be a paranoia check... */
1857 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1858 if (ret < 0)
1859 goto out;
1860
1861 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1862 if (ret < 0)
1863 goto out;
1864
1865 ret = _regulator_do_set_voltage(rdev, min_uV, max_uV);
1866
1867out:
1868 mutex_unlock(&rdev->mutex);
1869 return ret;
1870}
1871EXPORT_SYMBOL_GPL(regulator_sync_voltage);
1872
Liam Girdwood414c70c2008-04-30 15:59:04 +01001873static int _regulator_get_voltage(struct regulator_dev *rdev)
1874{
Mark Brownbf5892a2011-05-08 22:13:37 +01001875 int sel, ret;
Mark Brown476c2d82010-12-10 17:28:07 +00001876
1877 if (rdev->desc->ops->get_voltage_sel) {
1878 sel = rdev->desc->ops->get_voltage_sel(rdev);
1879 if (sel < 0)
1880 return sel;
Mark Brownbf5892a2011-05-08 22:13:37 +01001881 ret = rdev->desc->ops->list_voltage(rdev, sel);
Mark Brown476c2d82010-12-10 17:28:07 +00001882 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001883 if (rdev->desc->ops->get_voltage)
Mark Brownbf5892a2011-05-08 22:13:37 +01001884 ret = rdev->desc->ops->get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001885 else
1886 return -EINVAL;
Mark Brownbf5892a2011-05-08 22:13:37 +01001887
1888 return ret - rdev->constraints->uV_offset;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001889}
1890
1891/**
1892 * regulator_get_voltage - get regulator output voltage
1893 * @regulator: regulator source
1894 *
1895 * This returns the current regulator voltage in uV.
1896 *
1897 * NOTE: If the regulator is disabled it will return the voltage value. This
1898 * function should not be used to determine regulator state.
1899 */
1900int regulator_get_voltage(struct regulator *regulator)
1901{
1902 int ret;
1903
1904 mutex_lock(&regulator->rdev->mutex);
1905
1906 ret = _regulator_get_voltage(regulator->rdev);
1907
1908 mutex_unlock(&regulator->rdev->mutex);
1909
1910 return ret;
1911}
1912EXPORT_SYMBOL_GPL(regulator_get_voltage);
1913
1914/**
1915 * regulator_set_current_limit - set regulator output current limit
1916 * @regulator: regulator source
1917 * @min_uA: Minimuum supported current in uA
1918 * @max_uA: Maximum supported current in uA
1919 *
1920 * Sets current sink to the desired output current. This can be set during
1921 * any regulator state. IOW, regulator can be disabled or enabled.
1922 *
1923 * If the regulator is enabled then the current will change to the new value
1924 * immediately otherwise if the regulator is disabled the regulator will
1925 * output at the new current when enabled.
1926 *
1927 * NOTE: Regulator system constraints must be set for this regulator before
1928 * calling this function otherwise this call will fail.
1929 */
1930int regulator_set_current_limit(struct regulator *regulator,
1931 int min_uA, int max_uA)
1932{
1933 struct regulator_dev *rdev = regulator->rdev;
1934 int ret;
1935
1936 mutex_lock(&rdev->mutex);
1937
1938 /* sanity check */
1939 if (!rdev->desc->ops->set_current_limit) {
1940 ret = -EINVAL;
1941 goto out;
1942 }
1943
1944 /* constraints check */
1945 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1946 if (ret < 0)
1947 goto out;
1948
1949 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1950out:
1951 mutex_unlock(&rdev->mutex);
1952 return ret;
1953}
1954EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1955
1956static int _regulator_get_current_limit(struct regulator_dev *rdev)
1957{
1958 int ret;
1959
1960 mutex_lock(&rdev->mutex);
1961
1962 /* sanity check */
1963 if (!rdev->desc->ops->get_current_limit) {
1964 ret = -EINVAL;
1965 goto out;
1966 }
1967
1968 ret = rdev->desc->ops->get_current_limit(rdev);
1969out:
1970 mutex_unlock(&rdev->mutex);
1971 return ret;
1972}
1973
1974/**
1975 * regulator_get_current_limit - get regulator output current
1976 * @regulator: regulator source
1977 *
1978 * This returns the current supplied by the specified current sink in uA.
1979 *
1980 * NOTE: If the regulator is disabled it will return the current value. This
1981 * function should not be used to determine regulator state.
1982 */
1983int regulator_get_current_limit(struct regulator *regulator)
1984{
1985 return _regulator_get_current_limit(regulator->rdev);
1986}
1987EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1988
1989/**
1990 * regulator_set_mode - set regulator operating mode
1991 * @regulator: regulator source
1992 * @mode: operating mode - one of the REGULATOR_MODE constants
1993 *
1994 * Set regulator operating mode to increase regulator efficiency or improve
1995 * regulation performance.
1996 *
1997 * NOTE: Regulator system constraints must be set for this regulator before
1998 * calling this function otherwise this call will fail.
1999 */
2000int regulator_set_mode(struct regulator *regulator, unsigned int mode)
2001{
2002 struct regulator_dev *rdev = regulator->rdev;
2003 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302004 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005
2006 mutex_lock(&rdev->mutex);
2007
2008 /* sanity check */
2009 if (!rdev->desc->ops->set_mode) {
2010 ret = -EINVAL;
2011 goto out;
2012 }
2013
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05302014 /* return if the same mode is requested */
2015 if (rdev->desc->ops->get_mode) {
2016 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
2017 if (regulator_curr_mode == mode) {
2018 ret = 0;
2019 goto out;
2020 }
2021 }
2022
Liam Girdwood414c70c2008-04-30 15:59:04 +01002023 /* constraints check */
Axel Lin22c51b42011-04-01 18:25:25 +08002024 ret = regulator_mode_constrain(rdev, &mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002025 if (ret < 0)
2026 goto out;
2027
2028 ret = rdev->desc->ops->set_mode(rdev, mode);
2029out:
2030 mutex_unlock(&rdev->mutex);
2031 return ret;
2032}
2033EXPORT_SYMBOL_GPL(regulator_set_mode);
2034
2035static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
2036{
2037 int ret;
2038
2039 mutex_lock(&rdev->mutex);
2040
2041 /* sanity check */
2042 if (!rdev->desc->ops->get_mode) {
2043 ret = -EINVAL;
2044 goto out;
2045 }
2046
2047 ret = rdev->desc->ops->get_mode(rdev);
2048out:
2049 mutex_unlock(&rdev->mutex);
2050 return ret;
2051}
2052
2053/**
2054 * regulator_get_mode - get regulator operating mode
2055 * @regulator: regulator source
2056 *
2057 * Get the current regulator operating mode.
2058 */
2059unsigned int regulator_get_mode(struct regulator *regulator)
2060{
2061 return _regulator_get_mode(regulator->rdev);
2062}
2063EXPORT_SYMBOL_GPL(regulator_get_mode);
2064
2065/**
2066 * regulator_set_optimum_mode - set regulator optimum operating mode
2067 * @regulator: regulator source
2068 * @uA_load: load current
2069 *
2070 * Notifies the regulator core of a new device load. This is then used by
2071 * DRMS (if enabled by constraints) to set the most efficient regulator
2072 * operating mode for the new regulator loading.
2073 *
2074 * Consumer devices notify their supply regulator of the maximum power
2075 * they will require (can be taken from device datasheet in the power
2076 * consumption tables) when they change operational status and hence power
2077 * state. Examples of operational state changes that can affect power
2078 * consumption are :-
2079 *
2080 * o Device is opened / closed.
2081 * o Device I/O is about to begin or has just finished.
2082 * o Device is idling in between work.
2083 *
2084 * This information is also exported via sysfs to userspace.
2085 *
2086 * DRMS will sum the total requested load on the regulator and change
2087 * to the most efficient operating mode if platform constraints allow.
2088 *
2089 * Returns the new regulator mode or error.
2090 */
2091int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
2092{
2093 struct regulator_dev *rdev = regulator->rdev;
2094 struct regulator *consumer;
2095 int ret, output_uV, input_uV, total_uA_load = 0;
2096 unsigned int mode;
2097
2098 mutex_lock(&rdev->mutex);
2099
Mark Browna4b41482011-05-14 11:19:45 -07002100 /*
2101 * first check to see if we can set modes at all, otherwise just
2102 * tell the consumer everything is OK.
2103 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002104 regulator->uA_load = uA_load;
2105 ret = regulator_check_drms(rdev);
Mark Browna4b41482011-05-14 11:19:45 -07002106 if (ret < 0) {
2107 ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002108 goto out;
Mark Browna4b41482011-05-14 11:19:45 -07002109 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002110
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111 if (!rdev->desc->ops->get_optimum_mode)
2112 goto out;
2113
Mark Browna4b41482011-05-14 11:19:45 -07002114 /*
2115 * we can actually do this so any errors are indicators of
2116 * potential real failure.
2117 */
2118 ret = -EINVAL;
2119
Liam Girdwood414c70c2008-04-30 15:59:04 +01002120 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002121 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002122 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002123 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002124 goto out;
2125 }
2126
2127 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00002128 input_uV = 0;
2129 if (rdev->supply)
2130 input_uV = _regulator_get_voltage(rdev->supply);
2131 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002132 input_uV = rdev->constraints->input_uV;
2133 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002134 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002135 goto out;
2136 }
2137
2138 /* calc total requested load for this regulator */
2139 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01002140 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002141
2142 mode = rdev->desc->ops->get_optimum_mode(rdev,
2143 input_uV, output_uV,
2144 total_uA_load);
Mark Brown2c608232011-03-30 06:29:12 +09002145 ret = regulator_mode_constrain(rdev, &mode);
David Brownelle5735202008-11-16 11:46:56 -08002146 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002147 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
2148 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149 goto out;
2150 }
2151
2152 ret = rdev->desc->ops->set_mode(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 set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002155 goto out;
2156 }
2157 ret = mode;
2158out:
2159 mutex_unlock(&rdev->mutex);
2160 return ret;
2161}
2162EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
2163
2164/**
2165 * regulator_register_notifier - register regulator event notifier
2166 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002167 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002168 *
2169 * Register notifier block to receive regulator events.
2170 */
2171int regulator_register_notifier(struct regulator *regulator,
2172 struct notifier_block *nb)
2173{
2174 return blocking_notifier_chain_register(&regulator->rdev->notifier,
2175 nb);
2176}
2177EXPORT_SYMBOL_GPL(regulator_register_notifier);
2178
2179/**
2180 * regulator_unregister_notifier - unregister regulator event notifier
2181 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00002182 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01002183 *
2184 * Unregister regulator event notifier block.
2185 */
2186int regulator_unregister_notifier(struct regulator *regulator,
2187 struct notifier_block *nb)
2188{
2189 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
2190 nb);
2191}
2192EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
2193
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002194/* notify regulator consumers and downstream regulator consumers.
2195 * Note mutex must be held by caller.
2196 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002197static void _notifier_call_chain(struct regulator_dev *rdev,
2198 unsigned long event, void *data)
2199{
2200 struct regulator_dev *_rdev;
2201
2202 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01002203 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002204
2205 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002206 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002207 mutex_lock(&_rdev->mutex);
2208 _notifier_call_chain(_rdev, event, data);
2209 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002210 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002211}
2212
2213/**
2214 * regulator_bulk_get - get multiple regulator consumers
2215 *
2216 * @dev: Device to supply
2217 * @num_consumers: Number of consumers to register
2218 * @consumers: Configuration of consumers; clients are stored here.
2219 *
2220 * @return 0 on success, an errno on failure.
2221 *
2222 * This helper function allows drivers to get several regulator
2223 * consumers in one operation. If any of the regulators cannot be
2224 * acquired then any regulators that were allocated will be freed
2225 * before returning to the caller.
2226 */
2227int regulator_bulk_get(struct device *dev, int num_consumers,
2228 struct regulator_bulk_data *consumers)
2229{
2230 int i;
2231 int ret;
2232
2233 for (i = 0; i < num_consumers; i++)
2234 consumers[i].consumer = NULL;
2235
2236 for (i = 0; i < num_consumers; i++) {
2237 consumers[i].consumer = regulator_get(dev,
2238 consumers[i].supply);
2239 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002240 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002241 dev_err(dev, "Failed to get supply '%s': %d\n",
2242 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002243 consumers[i].consumer = NULL;
2244 goto err;
2245 }
2246 }
2247
2248 return 0;
2249
2250err:
2251 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2252 regulator_put(consumers[i].consumer);
2253
2254 return ret;
2255}
2256EXPORT_SYMBOL_GPL(regulator_bulk_get);
2257
2258/**
2259 * regulator_bulk_enable - enable multiple regulator consumers
2260 *
2261 * @num_consumers: Number of consumers
2262 * @consumers: Consumer data; clients are stored here.
2263 * @return 0 on success, an errno on failure
2264 *
2265 * This convenience API allows consumers to enable multiple regulator
2266 * clients in a single API call. If any consumers cannot be enabled
2267 * then any others that were enabled will be disabled again prior to
2268 * return.
2269 */
2270int regulator_bulk_enable(int num_consumers,
2271 struct regulator_bulk_data *consumers)
2272{
2273 int i;
2274 int ret;
2275
2276 for (i = 0; i < num_consumers; i++) {
2277 ret = regulator_enable(consumers[i].consumer);
2278 if (ret != 0)
2279 goto err;
2280 }
2281
2282 return 0;
2283
2284err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002285 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002286 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287 regulator_disable(consumers[i].consumer);
2288
2289 return ret;
2290}
2291EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2292
2293/**
2294 * regulator_bulk_disable - disable multiple regulator consumers
2295 *
2296 * @num_consumers: Number of consumers
2297 * @consumers: Consumer data; clients are stored here.
2298 * @return 0 on success, an errno on failure
2299 *
2300 * This convenience API allows consumers to disable multiple regulator
2301 * clients in a single API call. If any consumers cannot be enabled
2302 * then any others that were disabled will be disabled again prior to
2303 * return.
2304 */
2305int regulator_bulk_disable(int num_consumers,
2306 struct regulator_bulk_data *consumers)
2307{
2308 int i;
2309 int ret;
2310
2311 for (i = 0; i < num_consumers; i++) {
2312 ret = regulator_disable(consumers[i].consumer);
2313 if (ret != 0)
2314 goto err;
2315 }
2316
2317 return 0;
2318
2319err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002320 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002321 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002322 regulator_enable(consumers[i].consumer);
2323
2324 return ret;
2325}
2326EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2327
2328/**
2329 * regulator_bulk_free - free multiple regulator consumers
2330 *
2331 * @num_consumers: Number of consumers
2332 * @consumers: Consumer data; clients are stored here.
2333 *
2334 * This convenience API allows consumers to free multiple regulator
2335 * clients in a single API call.
2336 */
2337void regulator_bulk_free(int num_consumers,
2338 struct regulator_bulk_data *consumers)
2339{
2340 int i;
2341
2342 for (i = 0; i < num_consumers; i++) {
2343 regulator_put(consumers[i].consumer);
2344 consumers[i].consumer = NULL;
2345 }
2346}
2347EXPORT_SYMBOL_GPL(regulator_bulk_free);
2348
2349/**
2350 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002351 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002353 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002354 *
2355 * Called by regulator drivers to notify clients a regulator event has
2356 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002357 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358 */
2359int regulator_notifier_call_chain(struct regulator_dev *rdev,
2360 unsigned long event, void *data)
2361{
2362 _notifier_call_chain(rdev, event, data);
2363 return NOTIFY_DONE;
2364
2365}
2366EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2367
Mark Brownbe721972009-08-04 20:09:52 +02002368/**
2369 * regulator_mode_to_status - convert a regulator mode into a status
2370 *
2371 * @mode: Mode to convert
2372 *
2373 * Convert a regulator mode into a status.
2374 */
2375int regulator_mode_to_status(unsigned int mode)
2376{
2377 switch (mode) {
2378 case REGULATOR_MODE_FAST:
2379 return REGULATOR_STATUS_FAST;
2380 case REGULATOR_MODE_NORMAL:
2381 return REGULATOR_STATUS_NORMAL;
2382 case REGULATOR_MODE_IDLE:
2383 return REGULATOR_STATUS_IDLE;
2384 case REGULATOR_STATUS_STANDBY:
2385 return REGULATOR_STATUS_STANDBY;
2386 default:
2387 return 0;
2388 }
2389}
2390EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2391
David Brownell7ad68e22008-11-11 17:39:02 -08002392/*
2393 * To avoid cluttering sysfs (and memory) with useless state, only
2394 * create attributes that can be meaningfully displayed.
2395 */
2396static int add_regulator_attributes(struct regulator_dev *rdev)
2397{
2398 struct device *dev = &rdev->dev;
2399 struct regulator_ops *ops = rdev->desc->ops;
2400 int status = 0;
2401
2402 /* some attributes need specific methods to be displayed */
Mark Brown476c2d82010-12-10 17:28:07 +00002403 if (ops->get_voltage || ops->get_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002404 status = device_create_file(dev, &dev_attr_microvolts);
2405 if (status < 0)
2406 return status;
2407 }
2408 if (ops->get_current_limit) {
2409 status = device_create_file(dev, &dev_attr_microamps);
2410 if (status < 0)
2411 return status;
2412 }
2413 if (ops->get_mode) {
2414 status = device_create_file(dev, &dev_attr_opmode);
2415 if (status < 0)
2416 return status;
2417 }
2418 if (ops->is_enabled) {
2419 status = device_create_file(dev, &dev_attr_state);
2420 if (status < 0)
2421 return status;
2422 }
David Brownell853116a2009-01-14 23:03:17 -08002423 if (ops->get_status) {
2424 status = device_create_file(dev, &dev_attr_status);
2425 if (status < 0)
2426 return status;
2427 }
David Brownell7ad68e22008-11-11 17:39:02 -08002428
2429 /* some attributes are type-specific */
2430 if (rdev->desc->type == REGULATOR_CURRENT) {
2431 status = device_create_file(dev, &dev_attr_requested_microamps);
2432 if (status < 0)
2433 return status;
2434 }
2435
2436 /* all the other attributes exist to support constraints;
2437 * don't show them if there are no constraints, or if the
2438 * relevant supporting methods are missing.
2439 */
2440 if (!rdev->constraints)
2441 return status;
2442
2443 /* constraints need specific supporting methods */
Mark Browne8eef822010-12-12 14:36:17 +00002444 if (ops->set_voltage || ops->set_voltage_sel) {
David Brownell7ad68e22008-11-11 17:39:02 -08002445 status = device_create_file(dev, &dev_attr_min_microvolts);
2446 if (status < 0)
2447 return status;
2448 status = device_create_file(dev, &dev_attr_max_microvolts);
2449 if (status < 0)
2450 return status;
2451 }
2452 if (ops->set_current_limit) {
2453 status = device_create_file(dev, &dev_attr_min_microamps);
2454 if (status < 0)
2455 return status;
2456 status = device_create_file(dev, &dev_attr_max_microamps);
2457 if (status < 0)
2458 return status;
2459 }
2460
2461 /* suspend mode constraints need multiple supporting methods */
2462 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2463 return status;
2464
2465 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2466 if (status < 0)
2467 return status;
2468 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2469 if (status < 0)
2470 return status;
2471 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2472 if (status < 0)
2473 return status;
2474
2475 if (ops->set_suspend_voltage) {
2476 status = device_create_file(dev,
2477 &dev_attr_suspend_standby_microvolts);
2478 if (status < 0)
2479 return status;
2480 status = device_create_file(dev,
2481 &dev_attr_suspend_mem_microvolts);
2482 if (status < 0)
2483 return status;
2484 status = device_create_file(dev,
2485 &dev_attr_suspend_disk_microvolts);
2486 if (status < 0)
2487 return status;
2488 }
2489
2490 if (ops->set_suspend_mode) {
2491 status = device_create_file(dev,
2492 &dev_attr_suspend_standby_mode);
2493 if (status < 0)
2494 return status;
2495 status = device_create_file(dev,
2496 &dev_attr_suspend_mem_mode);
2497 if (status < 0)
2498 return status;
2499 status = device_create_file(dev,
2500 &dev_attr_suspend_disk_mode);
2501 if (status < 0)
2502 return status;
2503 }
2504
2505 return status;
2506}
2507
Mark Brown1130e5b2010-12-21 23:49:31 +00002508static void rdev_init_debugfs(struct regulator_dev *rdev)
2509{
2510#ifdef CONFIG_DEBUG_FS
2511 rdev->debugfs = debugfs_create_dir(rdev_get_name(rdev), debugfs_root);
2512 if (IS_ERR(rdev->debugfs) || !rdev->debugfs) {
2513 rdev_warn(rdev, "Failed to create debugfs directory\n");
2514 rdev->debugfs = NULL;
2515 return;
2516 }
2517
2518 debugfs_create_u32("use_count", 0444, rdev->debugfs,
2519 &rdev->use_count);
2520 debugfs_create_u32("open_count", 0444, rdev->debugfs,
2521 &rdev->open_count);
2522#endif
2523}
2524
Liam Girdwood414c70c2008-04-30 15:59:04 +01002525/**
2526 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002527 * @regulator_desc: regulator to register
2528 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002529 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002530 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002531 *
2532 * Called by regulator drivers to register a regulator.
2533 * Returns 0 on success.
2534 */
2535struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002536 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002537 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538{
2539 static atomic_t regulator_no = ATOMIC_INIT(0);
2540 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002541 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002542
2543 if (regulator_desc == NULL)
2544 return ERR_PTR(-EINVAL);
2545
2546 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2547 return ERR_PTR(-EINVAL);
2548
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002549 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2550 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002551 return ERR_PTR(-EINVAL);
2552
Mark Brown46fabe1e2008-09-09 16:21:18 +01002553 if (!init_data)
2554 return ERR_PTR(-EINVAL);
2555
Mark Brown476c2d82010-12-10 17:28:07 +00002556 /* Only one of each should be implemented */
2557 WARN_ON(regulator_desc->ops->get_voltage &&
2558 regulator_desc->ops->get_voltage_sel);
Mark Browne8eef822010-12-12 14:36:17 +00002559 WARN_ON(regulator_desc->ops->set_voltage &&
2560 regulator_desc->ops->set_voltage_sel);
Mark Brown476c2d82010-12-10 17:28:07 +00002561
2562 /* If we're using selectors we must implement list_voltage. */
2563 if (regulator_desc->ops->get_voltage_sel &&
2564 !regulator_desc->ops->list_voltage) {
2565 return ERR_PTR(-EINVAL);
2566 }
Mark Browne8eef822010-12-12 14:36:17 +00002567 if (regulator_desc->ops->set_voltage_sel &&
2568 !regulator_desc->ops->list_voltage) {
2569 return ERR_PTR(-EINVAL);
2570 }
Mark Brown476c2d82010-12-10 17:28:07 +00002571
Liam Girdwood414c70c2008-04-30 15:59:04 +01002572 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2573 if (rdev == NULL)
2574 return ERR_PTR(-ENOMEM);
2575
2576 mutex_lock(&regulator_list_mutex);
2577
2578 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002579 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002580 rdev->owner = regulator_desc->owner;
2581 rdev->desc = regulator_desc;
2582 INIT_LIST_HEAD(&rdev->consumer_list);
2583 INIT_LIST_HEAD(&rdev->supply_list);
2584 INIT_LIST_HEAD(&rdev->list);
2585 INIT_LIST_HEAD(&rdev->slist);
2586 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2587
Liam Girdwooda5766f12008-10-10 13:22:20 +01002588 /* preform any regulator specific init */
2589 if (init_data->regulator_init) {
2590 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002591 if (ret < 0)
2592 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002593 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002594
Liam Girdwooda5766f12008-10-10 13:22:20 +01002595 /* register with sysfs */
2596 rdev->dev.class = &regulator_class;
2597 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002598 dev_set_name(&rdev->dev, "regulator.%d",
2599 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002600 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002601 if (ret != 0) {
2602 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002603 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002604 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002605
2606 dev_set_drvdata(&rdev->dev, rdev);
2607
Mike Rapoport74f544c2008-11-25 14:53:53 +02002608 /* set regulator constraints */
2609 ret = set_machine_constraints(rdev, &init_data->constraints);
2610 if (ret < 0)
2611 goto scrub;
2612
David Brownell7ad68e22008-11-11 17:39:02 -08002613 /* add attributes supported by this regulator */
2614 ret = add_regulator_attributes(rdev);
2615 if (ret < 0)
2616 goto scrub;
2617
Mark Brown0178f3e2010-04-26 15:18:14 +01002618 if (init_data->supply_regulator) {
2619 struct regulator_dev *r;
2620 int found = 0;
2621
2622 list_for_each_entry(r, &regulator_list, list) {
2623 if (strcmp(rdev_get_name(r),
2624 init_data->supply_regulator) == 0) {
2625 found = 1;
2626 break;
2627 }
2628 }
2629
2630 if (!found) {
2631 dev_err(dev, "Failed to find supply %s\n",
2632 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002633 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002634 goto scrub;
2635 }
2636
2637 ret = set_supply(rdev, r);
2638 if (ret < 0)
2639 goto scrub;
2640 }
2641
Liam Girdwooda5766f12008-10-10 13:22:20 +01002642 /* add consumers devices */
2643 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2644 ret = set_consumer_device_supply(rdev,
2645 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002646 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002647 init_data->consumer_supplies[i].supply);
Mark Brown23c2f042011-02-24 17:39:09 +00002648 if (ret < 0) {
2649 dev_err(dev, "Failed to set supply %s\n",
2650 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002651 goto unset_supplies;
Mark Brown23c2f042011-02-24 17:39:09 +00002652 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002653 }
2654
2655 list_add(&rdev->list, &regulator_list);
Mark Brown1130e5b2010-12-21 23:49:31 +00002656
2657 rdev_init_debugfs(rdev);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002658out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002659 mutex_unlock(&regulator_list_mutex);
2660 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002661
Jani Nikulad4033b52010-04-29 10:55:11 +03002662unset_supplies:
2663 unset_regulator_supplies(rdev);
2664
David Brownell4fca9542008-11-11 17:38:53 -08002665scrub:
2666 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002667 /* device core frees rdev */
2668 rdev = ERR_PTR(ret);
2669 goto out;
2670
David Brownell4fca9542008-11-11 17:38:53 -08002671clean:
2672 kfree(rdev);
2673 rdev = ERR_PTR(ret);
2674 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002675}
2676EXPORT_SYMBOL_GPL(regulator_register);
2677
2678/**
2679 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002680 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002681 *
2682 * Called by regulator drivers to unregister a regulator.
2683 */
2684void regulator_unregister(struct regulator_dev *rdev)
2685{
2686 if (rdev == NULL)
2687 return;
2688
2689 mutex_lock(&regulator_list_mutex);
Mark Brown1130e5b2010-12-21 23:49:31 +00002690#ifdef CONFIG_DEBUG_FS
2691 debugfs_remove_recursive(rdev->debugfs);
2692#endif
Mark Brown6bf87d12009-07-21 16:00:25 +01002693 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002694 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002695 list_del(&rdev->list);
2696 if (rdev->supply)
2697 sysfs_remove_link(&rdev->dev.kobj, "supply");
2698 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002699 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002700 mutex_unlock(&regulator_list_mutex);
2701}
2702EXPORT_SYMBOL_GPL(regulator_unregister);
2703
2704/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002705 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002706 * @state: system suspend state
2707 *
2708 * Configure each regulator with it's suspend operating parameters for state.
2709 * This will usually be called by machine suspend code prior to supending.
2710 */
2711int regulator_suspend_prepare(suspend_state_t state)
2712{
2713 struct regulator_dev *rdev;
2714 int ret = 0;
2715
2716 /* ON is handled by regulator active state */
2717 if (state == PM_SUSPEND_ON)
2718 return -EINVAL;
2719
2720 mutex_lock(&regulator_list_mutex);
2721 list_for_each_entry(rdev, &regulator_list, list) {
2722
2723 mutex_lock(&rdev->mutex);
2724 ret = suspend_prepare(rdev, state);
2725 mutex_unlock(&rdev->mutex);
2726
2727 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002728 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002729 goto out;
2730 }
2731 }
2732out:
2733 mutex_unlock(&regulator_list_mutex);
2734 return ret;
2735}
2736EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2737
2738/**
MyungJoo Ham7a32b582011-03-11 10:13:59 +09002739 * regulator_suspend_finish - resume regulators from system wide suspend
2740 *
2741 * Turn on regulators that might be turned off by regulator_suspend_prepare
2742 * and that should be turned on according to the regulators properties.
2743 */
2744int regulator_suspend_finish(void)
2745{
2746 struct regulator_dev *rdev;
2747 int ret = 0, error;
2748
2749 mutex_lock(&regulator_list_mutex);
2750 list_for_each_entry(rdev, &regulator_list, list) {
2751 struct regulator_ops *ops = rdev->desc->ops;
2752
2753 mutex_lock(&rdev->mutex);
2754 if ((rdev->use_count > 0 || rdev->constraints->always_on) &&
2755 ops->enable) {
2756 error = ops->enable(rdev);
2757 if (error)
2758 ret = error;
2759 } else {
2760 if (!has_full_constraints)
2761 goto unlock;
2762 if (!ops->disable)
2763 goto unlock;
2764 if (ops->is_enabled && !ops->is_enabled(rdev))
2765 goto unlock;
2766
2767 error = ops->disable(rdev);
2768 if (error)
2769 ret = error;
2770 }
2771unlock:
2772 mutex_unlock(&rdev->mutex);
2773 }
2774 mutex_unlock(&regulator_list_mutex);
2775 return ret;
2776}
2777EXPORT_SYMBOL_GPL(regulator_suspend_finish);
2778
2779/**
Mark Brownca725562009-03-16 19:36:34 +00002780 * regulator_has_full_constraints - the system has fully specified constraints
2781 *
2782 * Calling this function will cause the regulator API to disable all
2783 * regulators which have a zero use count and don't have an always_on
2784 * constraint in a late_initcall.
2785 *
2786 * The intention is that this will become the default behaviour in a
2787 * future kernel release so users are encouraged to use this facility
2788 * now.
2789 */
2790void regulator_has_full_constraints(void)
2791{
2792 has_full_constraints = 1;
2793}
2794EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2795
2796/**
Mark Brown688fe992010-10-05 19:18:32 -07002797 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2798 *
2799 * Calling this function will cause the regulator API to provide a
2800 * dummy regulator to consumers if no physical regulator is found,
2801 * allowing most consumers to proceed as though a regulator were
2802 * configured. This allows systems such as those with software
2803 * controllable regulators for the CPU core only to be brought up more
2804 * readily.
2805 */
2806void regulator_use_dummy_regulator(void)
2807{
2808 board_wants_dummy_regulator = true;
2809}
2810EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2811
2812/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002813 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002814 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002815 *
2816 * Get rdev regulator driver private data. This call can be used in the
2817 * regulator driver context.
2818 */
2819void *rdev_get_drvdata(struct regulator_dev *rdev)
2820{
2821 return rdev->reg_data;
2822}
2823EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2824
2825/**
2826 * regulator_get_drvdata - get regulator driver data
2827 * @regulator: regulator
2828 *
2829 * Get regulator driver private data. This call can be used in the consumer
2830 * driver context when non API regulator specific functions need to be called.
2831 */
2832void *regulator_get_drvdata(struct regulator *regulator)
2833{
2834 return regulator->rdev->reg_data;
2835}
2836EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2837
2838/**
2839 * regulator_set_drvdata - set regulator driver data
2840 * @regulator: regulator
2841 * @data: data
2842 */
2843void regulator_set_drvdata(struct regulator *regulator, void *data)
2844{
2845 regulator->rdev->reg_data = data;
2846}
2847EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2848
2849/**
2850 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002851 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002852 */
2853int rdev_get_id(struct regulator_dev *rdev)
2854{
2855 return rdev->desc->id;
2856}
2857EXPORT_SYMBOL_GPL(rdev_get_id);
2858
Liam Girdwooda5766f12008-10-10 13:22:20 +01002859struct device *rdev_get_dev(struct regulator_dev *rdev)
2860{
2861 return &rdev->dev;
2862}
2863EXPORT_SYMBOL_GPL(rdev_get_dev);
2864
2865void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2866{
2867 return reg_init_data->driver_data;
2868}
2869EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2870
Liam Girdwood414c70c2008-04-30 15:59:04 +01002871static int __init regulator_init(void)
2872{
Mark Brown34abbd62010-02-12 10:18:08 +00002873 int ret;
2874
Mark Brown34abbd62010-02-12 10:18:08 +00002875 ret = class_register(&regulator_class);
2876
Mark Brown1130e5b2010-12-21 23:49:31 +00002877#ifdef CONFIG_DEBUG_FS
2878 debugfs_root = debugfs_create_dir("regulator", NULL);
2879 if (IS_ERR(debugfs_root) || !debugfs_root) {
2880 pr_warn("regulator: Failed to create debugfs directory\n");
2881 debugfs_root = NULL;
2882 }
2883#endif
2884
Mark Brown34abbd62010-02-12 10:18:08 +00002885 regulator_dummy_init();
2886
2887 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002888}
2889
2890/* init early to allow our consumers to complete system booting */
2891core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002892
2893static int __init regulator_init_complete(void)
2894{
2895 struct regulator_dev *rdev;
2896 struct regulator_ops *ops;
2897 struct regulation_constraints *c;
2898 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002899
2900 mutex_lock(&regulator_list_mutex);
2901
2902 /* If we have a full configuration then disable any regulators
2903 * which are not in use or always_on. This will become the
2904 * default behaviour in the future.
2905 */
2906 list_for_each_entry(rdev, &regulator_list, list) {
2907 ops = rdev->desc->ops;
2908 c = rdev->constraints;
2909
Mark Brownf25e0b42009-08-03 18:49:55 +01002910 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002911 continue;
2912
2913 mutex_lock(&rdev->mutex);
2914
2915 if (rdev->use_count)
2916 goto unlock;
2917
2918 /* If we can't read the status assume it's on. */
2919 if (ops->is_enabled)
2920 enabled = ops->is_enabled(rdev);
2921 else
2922 enabled = 1;
2923
2924 if (!enabled)
2925 goto unlock;
2926
2927 if (has_full_constraints) {
2928 /* We log since this may kill the system if it
2929 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002930 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002931 ret = ops->disable(rdev);
2932 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002933 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002934 }
2935 } else {
2936 /* The intention is that in future we will
2937 * assume that full constraints are provided
2938 * so warn even if we aren't going to do
2939 * anything here.
2940 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002941 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002942 }
2943
2944unlock:
2945 mutex_unlock(&rdev->mutex);
2946 }
2947
2948 mutex_unlock(&regulator_list_mutex);
2949
2950 return 0;
2951}
2952late_initcall(regulator_init_complete);