blob: 5d07e5dd417f858f121524b1666004737c0cc20f [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>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010026#include <linux/regulator/consumer.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
29
Mark Brown02fa3ec2010-11-10 14:38:30 +000030#define CREATE_TRACE_POINTS
31#include <trace/events/regulator.h>
32
Mark Brown34abbd62010-02-12 10:18:08 +000033#include "dummy.h"
34
Joe Perches5da84fd2010-11-30 05:53:48 -080035#define rdev_err(rdev, fmt, ...) \
36 pr_err("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
37#define rdev_warn(rdev, fmt, ...) \
38 pr_warn("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
39#define rdev_info(rdev, fmt, ...) \
40 pr_info("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
41#define rdev_dbg(rdev, fmt, ...) \
42 pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__)
43
Liam Girdwood414c70c2008-04-30 15:59:04 +010044static DEFINE_MUTEX(regulator_list_mutex);
45static LIST_HEAD(regulator_list);
46static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000047static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070048static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010049
Mark Brown8dc53902008-12-31 12:52:40 +000050/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010051 * struct regulator_map
52 *
53 * Used to provide symbolic supply names to devices.
54 */
55struct regulator_map {
56 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010057 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010058 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010059 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010060};
61
Liam Girdwood414c70c2008-04-30 15:59:04 +010062/*
63 * struct regulator
64 *
65 * One for each consumer device.
66 */
67struct regulator {
68 struct device *dev;
69 struct list_head list;
70 int uA_load;
71 int min_uV;
72 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010073 char *supply_name;
74 struct device_attribute dev_attr;
75 struct regulator_dev *rdev;
76};
77
78static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050079static int _regulator_disable(struct regulator_dev *rdev,
80 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010081static int _regulator_get_voltage(struct regulator_dev *rdev);
82static int _regulator_get_current_limit(struct regulator_dev *rdev);
83static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
84static void _notifier_call_chain(struct regulator_dev *rdev,
85 unsigned long event, void *data);
86
Mark Brown1083c392009-10-22 16:31:32 +010087static const char *rdev_get_name(struct regulator_dev *rdev)
88{
89 if (rdev->constraints && rdev->constraints->name)
90 return rdev->constraints->name;
91 else if (rdev->desc->name)
92 return rdev->desc->name;
93 else
94 return "";
95}
96
Liam Girdwood414c70c2008-04-30 15:59:04 +010097/* gets the regulator for a given consumer device */
98static struct regulator *get_device_regulator(struct device *dev)
99{
100 struct regulator *regulator = NULL;
101 struct regulator_dev *rdev;
102
103 mutex_lock(&regulator_list_mutex);
104 list_for_each_entry(rdev, &regulator_list, list) {
105 mutex_lock(&rdev->mutex);
106 list_for_each_entry(regulator, &rdev->consumer_list, list) {
107 if (regulator->dev == dev) {
108 mutex_unlock(&rdev->mutex);
109 mutex_unlock(&regulator_list_mutex);
110 return regulator;
111 }
112 }
113 mutex_unlock(&rdev->mutex);
114 }
115 mutex_unlock(&regulator_list_mutex);
116 return NULL;
117}
118
119/* Platform voltage constraint check */
120static int regulator_check_voltage(struct regulator_dev *rdev,
121 int *min_uV, int *max_uV)
122{
123 BUG_ON(*min_uV > *max_uV);
124
125 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800126 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100127 return -ENODEV;
128 }
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800130 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100131 return -EPERM;
132 }
133
134 if (*max_uV > rdev->constraints->max_uV)
135 *max_uV = rdev->constraints->max_uV;
136 if (*min_uV < rdev->constraints->min_uV)
137 *min_uV = rdev->constraints->min_uV;
138
139 if (*min_uV > *max_uV)
140 return -EINVAL;
141
142 return 0;
143}
144
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +0100145/* Make sure we select a voltage that suits the needs of all
146 * regulator consumers
147 */
148static int regulator_check_consumers(struct regulator_dev *rdev,
149 int *min_uV, int *max_uV)
150{
151 struct regulator *regulator;
152
153 list_for_each_entry(regulator, &rdev->consumer_list, list) {
154 if (*max_uV > regulator->max_uV)
155 *max_uV = regulator->max_uV;
156 if (*min_uV < regulator->min_uV)
157 *min_uV = regulator->min_uV;
158 }
159
160 if (*min_uV > *max_uV)
161 return -EINVAL;
162
163 return 0;
164}
165
Liam Girdwood414c70c2008-04-30 15:59:04 +0100166/* current constraint check */
167static int regulator_check_current_limit(struct regulator_dev *rdev,
168 int *min_uA, int *max_uA)
169{
170 BUG_ON(*min_uA > *max_uA);
171
172 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800173 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100174 return -ENODEV;
175 }
176 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800177 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100178 return -EPERM;
179 }
180
181 if (*max_uA > rdev->constraints->max_uA)
182 *max_uA = rdev->constraints->max_uA;
183 if (*min_uA < rdev->constraints->min_uA)
184 *min_uA = rdev->constraints->min_uA;
185
186 if (*min_uA > *max_uA)
187 return -EINVAL;
188
189 return 0;
190}
191
192/* operating mode constraint check */
193static int regulator_check_mode(struct regulator_dev *rdev, int mode)
194{
David Brownelle5735202008-11-16 11:46:56 -0800195 switch (mode) {
196 case REGULATOR_MODE_FAST:
197 case REGULATOR_MODE_NORMAL:
198 case REGULATOR_MODE_IDLE:
199 case REGULATOR_MODE_STANDBY:
200 break;
201 default:
202 return -EINVAL;
203 }
204
Liam Girdwood414c70c2008-04-30 15:59:04 +0100205 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800206 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100207 return -ENODEV;
208 }
209 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800210 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100211 return -EPERM;
212 }
213 if (!(rdev->constraints->valid_modes_mask & mode)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800214 rdev_err(rdev, "invalid mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100215 return -EINVAL;
216 }
217 return 0;
218}
219
220/* dynamic regulator mode switching constraint check */
221static int regulator_check_drms(struct regulator_dev *rdev)
222{
223 if (!rdev->constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800224 rdev_err(rdev, "no constraints\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100225 return -ENODEV;
226 }
227 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800228 rdev_err(rdev, "operation not allowed\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 return -EPERM;
230 }
231 return 0;
232}
233
234static ssize_t device_requested_uA_show(struct device *dev,
235 struct device_attribute *attr, char *buf)
236{
237 struct regulator *regulator;
238
239 regulator = get_device_regulator(dev);
240 if (regulator == NULL)
241 return 0;
242
243 return sprintf(buf, "%d\n", regulator->uA_load);
244}
245
246static ssize_t regulator_uV_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
248{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100249 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100250 ssize_t ret;
251
252 mutex_lock(&rdev->mutex);
253 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
254 mutex_unlock(&rdev->mutex);
255
256 return ret;
257}
David Brownell7ad68e22008-11-11 17:39:02 -0800258static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100259
260static ssize_t regulator_uA_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
262{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100263 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264
265 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
266}
David Brownell7ad68e22008-11-11 17:39:02 -0800267static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100268
Mark Brownbc558a62008-10-10 15:33:20 +0100269static ssize_t regulator_name_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
272 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100273
Mark Brown1083c392009-10-22 16:31:32 +0100274 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100275}
276
David Brownell4fca9542008-11-11 17:38:53 -0800277static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100278{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100279 switch (mode) {
280 case REGULATOR_MODE_FAST:
281 return sprintf(buf, "fast\n");
282 case REGULATOR_MODE_NORMAL:
283 return sprintf(buf, "normal\n");
284 case REGULATOR_MODE_IDLE:
285 return sprintf(buf, "idle\n");
286 case REGULATOR_MODE_STANDBY:
287 return sprintf(buf, "standby\n");
288 }
289 return sprintf(buf, "unknown\n");
290}
291
David Brownell4fca9542008-11-11 17:38:53 -0800292static ssize_t regulator_opmode_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100294{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100295 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100296
David Brownell4fca9542008-11-11 17:38:53 -0800297 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
298}
David Brownell7ad68e22008-11-11 17:39:02 -0800299static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800300
301static ssize_t regulator_print_state(char *buf, int state)
302{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100303 if (state > 0)
304 return sprintf(buf, "enabled\n");
305 else if (state == 0)
306 return sprintf(buf, "disabled\n");
307 else
308 return sprintf(buf, "unknown\n");
309}
310
David Brownell4fca9542008-11-11 17:38:53 -0800311static ssize_t regulator_state_show(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
314 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100315 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800316
Mark Brown93325462009-08-03 18:49:56 +0100317 mutex_lock(&rdev->mutex);
318 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
319 mutex_unlock(&rdev->mutex);
320
321 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800322}
David Brownell7ad68e22008-11-11 17:39:02 -0800323static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800324
David Brownell853116a2009-01-14 23:03:17 -0800325static ssize_t regulator_status_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
328 struct regulator_dev *rdev = dev_get_drvdata(dev);
329 int status;
330 char *label;
331
332 status = rdev->desc->ops->get_status(rdev);
333 if (status < 0)
334 return status;
335
336 switch (status) {
337 case REGULATOR_STATUS_OFF:
338 label = "off";
339 break;
340 case REGULATOR_STATUS_ON:
341 label = "on";
342 break;
343 case REGULATOR_STATUS_ERROR:
344 label = "error";
345 break;
346 case REGULATOR_STATUS_FAST:
347 label = "fast";
348 break;
349 case REGULATOR_STATUS_NORMAL:
350 label = "normal";
351 break;
352 case REGULATOR_STATUS_IDLE:
353 label = "idle";
354 break;
355 case REGULATOR_STATUS_STANDBY:
356 label = "standby";
357 break;
358 default:
359 return -ERANGE;
360 }
361
362 return sprintf(buf, "%s\n", label);
363}
364static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
365
Liam Girdwood414c70c2008-04-30 15:59:04 +0100366static ssize_t regulator_min_uA_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
368{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100369 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100370
371 if (!rdev->constraints)
372 return sprintf(buf, "constraint not defined\n");
373
374 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
375}
David Brownell7ad68e22008-11-11 17:39:02 -0800376static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
378static ssize_t regulator_max_uA_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
380{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100381 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100382
383 if (!rdev->constraints)
384 return sprintf(buf, "constraint not defined\n");
385
386 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
387}
David Brownell7ad68e22008-11-11 17:39:02 -0800388static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100389
390static ssize_t regulator_min_uV_show(struct device *dev,
391 struct device_attribute *attr, char *buf)
392{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100393 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100394
395 if (!rdev->constraints)
396 return sprintf(buf, "constraint not defined\n");
397
398 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
399}
David Brownell7ad68e22008-11-11 17:39:02 -0800400static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100401
402static ssize_t regulator_max_uV_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
404{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100405 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100406
407 if (!rdev->constraints)
408 return sprintf(buf, "constraint not defined\n");
409
410 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
411}
David Brownell7ad68e22008-11-11 17:39:02 -0800412static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100413
414static ssize_t regulator_total_uA_show(struct device *dev,
415 struct device_attribute *attr, char *buf)
416{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100417 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418 struct regulator *regulator;
419 int uA = 0;
420
421 mutex_lock(&rdev->mutex);
422 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100423 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100424 mutex_unlock(&rdev->mutex);
425 return sprintf(buf, "%d\n", uA);
426}
David Brownell7ad68e22008-11-11 17:39:02 -0800427static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428
429static ssize_t regulator_num_users_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100432 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433 return sprintf(buf, "%d\n", rdev->use_count);
434}
435
436static ssize_t regulator_type_show(struct device *dev,
437 struct device_attribute *attr, char *buf)
438{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100439 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441 switch (rdev->desc->type) {
442 case REGULATOR_VOLTAGE:
443 return sprintf(buf, "voltage\n");
444 case REGULATOR_CURRENT:
445 return sprintf(buf, "current\n");
446 }
447 return sprintf(buf, "unknown\n");
448}
449
450static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100453 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
456}
David Brownell7ad68e22008-11-11 17:39:02 -0800457static DEVICE_ATTR(suspend_mem_microvolts, 0444,
458 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459
460static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100463 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(suspend_disk_microvolts, 0444,
468 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
470static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
476}
David Brownell7ad68e22008-11-11 17:39:02 -0800477static DEVICE_ATTR(suspend_standby_microvolts, 0444,
478 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
481 struct device_attribute *attr, char *buf)
482{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100483 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100484
David Brownell4fca9542008-11-11 17:38:53 -0800485 return regulator_print_opmode(buf,
486 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487}
David Brownell7ad68e22008-11-11 17:39:02 -0800488static DEVICE_ATTR(suspend_mem_mode, 0444,
489 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490
491static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
492 struct device_attribute *attr, char *buf)
493{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100494 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100495
David Brownell4fca9542008-11-11 17:38:53 -0800496 return regulator_print_opmode(buf,
497 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498}
David Brownell7ad68e22008-11-11 17:39:02 -0800499static DEVICE_ATTR(suspend_disk_mode, 0444,
500 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501
502static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
503 struct device_attribute *attr, char *buf)
504{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100505 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100506
David Brownell4fca9542008-11-11 17:38:53 -0800507 return regulator_print_opmode(buf,
508 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509}
David Brownell7ad68e22008-11-11 17:39:02 -0800510static DEVICE_ATTR(suspend_standby_mode, 0444,
511 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512
513static ssize_t regulator_suspend_mem_state_show(struct device *dev,
514 struct device_attribute *attr, char *buf)
515{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100516 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100517
David Brownell4fca9542008-11-11 17:38:53 -0800518 return regulator_print_state(buf,
519 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520}
David Brownell7ad68e22008-11-11 17:39:02 -0800521static DEVICE_ATTR(suspend_mem_state, 0444,
522 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523
524static ssize_t regulator_suspend_disk_state_show(struct device *dev,
525 struct device_attribute *attr, char *buf)
526{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100527 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100528
David Brownell4fca9542008-11-11 17:38:53 -0800529 return regulator_print_state(buf,
530 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531}
David Brownell7ad68e22008-11-11 17:39:02 -0800532static DEVICE_ATTR(suspend_disk_state, 0444,
533 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534
535static ssize_t regulator_suspend_standby_state_show(struct device *dev,
536 struct device_attribute *attr, char *buf)
537{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100538 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100539
David Brownell4fca9542008-11-11 17:38:53 -0800540 return regulator_print_state(buf,
541 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542}
David Brownell7ad68e22008-11-11 17:39:02 -0800543static DEVICE_ATTR(suspend_standby_state, 0444,
544 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100545
David Brownell7ad68e22008-11-11 17:39:02 -0800546
547/*
548 * These are the only attributes are present for all regulators.
549 * Other attributes are a function of regulator functionality.
550 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100551static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100552 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100553 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
554 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100555 __ATTR_NULL,
556};
557
558static void regulator_dev_release(struct device *dev)
559{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100560 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561 kfree(rdev);
562}
563
564static struct class regulator_class = {
565 .name = "regulator",
566 .dev_release = regulator_dev_release,
567 .dev_attrs = regulator_dev_attrs,
568};
569
570/* Calculate the new optimum regulator operating mode based on the new total
571 * consumer load. All locks held by caller */
572static void drms_uA_update(struct regulator_dev *rdev)
573{
574 struct regulator *sibling;
575 int current_uA = 0, output_uV, input_uV, err;
576 unsigned int mode;
577
578 err = regulator_check_drms(rdev);
579 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300580 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
581 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100582
583 /* get output voltage */
584 output_uV = rdev->desc->ops->get_voltage(rdev);
585 if (output_uV <= 0)
586 return;
587
588 /* get input voltage */
589 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
590 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
591 else
592 input_uV = rdev->constraints->input_uV;
593 if (input_uV <= 0)
594 return;
595
596 /* calc total requested load */
597 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100598 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100599
600 /* now get the optimum mode for our new total regulator load */
601 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
602 output_uV, current_uA);
603
604 /* check the new mode is allowed */
605 err = regulator_check_mode(rdev, mode);
606 if (err == 0)
607 rdev->desc->ops->set_mode(rdev, mode);
608}
609
610static int suspend_set_state(struct regulator_dev *rdev,
611 struct regulator_state *rstate)
612{
613 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100614 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615
Mark Brown638f85c2009-10-22 16:31:33 +0100616 can_set_state = rdev->desc->ops->set_suspend_enable &&
617 rdev->desc->ops->set_suspend_disable;
618
619 /* If we have no suspend mode configration don't set anything;
620 * only warn if the driver actually makes the suspend mode
621 * configurable.
622 */
623 if (!rstate->enabled && !rstate->disabled) {
624 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800625 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100626 return 0;
627 }
628
629 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800630 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100631 return -EINVAL;
632 }
633
634 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800635 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100636 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100637 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100638
639 if (rstate->enabled)
640 ret = rdev->desc->ops->set_suspend_enable(rdev);
641 else
642 ret = rdev->desc->ops->set_suspend_disable(rdev);
643 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800644 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100645 return ret;
646 }
647
648 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
649 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
650 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800651 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100652 return ret;
653 }
654 }
655
656 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
657 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
658 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800659 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100660 return ret;
661 }
662 }
663 return ret;
664}
665
666/* locks held by caller */
667static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
668{
669 if (!rdev->constraints)
670 return -EINVAL;
671
672 switch (state) {
673 case PM_SUSPEND_STANDBY:
674 return suspend_set_state(rdev,
675 &rdev->constraints->state_standby);
676 case PM_SUSPEND_MEM:
677 return suspend_set_state(rdev,
678 &rdev->constraints->state_mem);
679 case PM_SUSPEND_MAX:
680 return suspend_set_state(rdev,
681 &rdev->constraints->state_disk);
682 default:
683 return -EINVAL;
684 }
685}
686
687static void print_constraints(struct regulator_dev *rdev)
688{
689 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000690 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100691 int count = 0;
692 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100693
Mark Brown8f031b42009-10-22 16:31:31 +0100694 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100695 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100696 count += sprintf(buf + count, "%d mV ",
697 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100698 else
Mark Brown8f031b42009-10-22 16:31:31 +0100699 count += sprintf(buf + count, "%d <--> %d mV ",
700 constraints->min_uV / 1000,
701 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100702 }
Mark Brown8f031b42009-10-22 16:31:31 +0100703
704 if (!constraints->min_uV ||
705 constraints->min_uV != constraints->max_uV) {
706 ret = _regulator_get_voltage(rdev);
707 if (ret > 0)
708 count += sprintf(buf + count, "at %d mV ", ret / 1000);
709 }
710
711 if (constraints->min_uA && constraints->max_uA) {
712 if (constraints->min_uA == constraints->max_uA)
713 count += sprintf(buf + count, "%d mA ",
714 constraints->min_uA / 1000);
715 else
716 count += sprintf(buf + count, "%d <--> %d mA ",
717 constraints->min_uA / 1000,
718 constraints->max_uA / 1000);
719 }
720
721 if (!constraints->min_uA ||
722 constraints->min_uA != constraints->max_uA) {
723 ret = _regulator_get_current_limit(rdev);
724 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400725 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100726 }
727
Liam Girdwood414c70c2008-04-30 15:59:04 +0100728 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
729 count += sprintf(buf + count, "fast ");
730 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
731 count += sprintf(buf + count, "normal ");
732 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
733 count += sprintf(buf + count, "idle ");
734 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
735 count += sprintf(buf + count, "standby");
736
Joe Perches5da84fd2010-11-30 05:53:48 -0800737 rdev_info(rdev, "regulator: %s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100738}
739
Mark Browne79055d2009-10-19 15:53:50 +0100740static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100741 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100742{
743 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100744 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000745 unsigned selector;
Mark Brownaf5866c2009-10-22 16:31:30 +0100746
747 /* do we need to apply the constraint voltage */
748 if (rdev->constraints->apply_uV &&
749 rdev->constraints->min_uV == rdev->constraints->max_uV &&
750 ops->set_voltage) {
751 ret = ops->set_voltage(rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000752 rdev->constraints->min_uV,
753 rdev->constraints->max_uV,
754 &selector);
Mark Brownaf5866c2009-10-22 16:31:30 +0100755 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800756 rdev_err(rdev, "failed to apply %duV constraint\n",
757 rdev->constraints->min_uV);
Mark Brownaf5866c2009-10-22 16:31:30 +0100758 rdev->constraints = NULL;
759 return ret;
760 }
761 }
Mark Browne79055d2009-10-19 15:53:50 +0100762
763 /* constrain machine-level voltage specs to fit
764 * the actual range supported by this regulator.
765 */
766 if (ops->list_voltage && rdev->desc->n_voltages) {
767 int count = rdev->desc->n_voltages;
768 int i;
769 int min_uV = INT_MAX;
770 int max_uV = INT_MIN;
771 int cmin = constraints->min_uV;
772 int cmax = constraints->max_uV;
773
774 /* it's safe to autoconfigure fixed-voltage supplies
775 and the constraints are used by list_voltage. */
776 if (count == 1 && !cmin) {
777 cmin = 1;
778 cmax = INT_MAX;
779 constraints->min_uV = cmin;
780 constraints->max_uV = cmax;
781 }
782
783 /* voltage constraints are optional */
784 if ((cmin == 0) && (cmax == 0))
785 return 0;
786
787 /* else require explicit machine-level constraints */
788 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800789 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100790 return -EINVAL;
791 }
792
793 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
794 for (i = 0; i < count; i++) {
795 int value;
796
797 value = ops->list_voltage(rdev, i);
798 if (value <= 0)
799 continue;
800
801 /* maybe adjust [min_uV..max_uV] */
802 if (value >= cmin && value < min_uV)
803 min_uV = value;
804 if (value <= cmax && value > max_uV)
805 max_uV = value;
806 }
807
808 /* final: [min_uV..max_uV] valid iff constraints valid */
809 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800810 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100811 return -EINVAL;
812 }
813
814 /* use regulator's subset of machine constraints */
815 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800816 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
817 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100818 constraints->min_uV = min_uV;
819 }
820 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800821 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
822 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100823 constraints->max_uV = max_uV;
824 }
825 }
826
827 return 0;
828}
829
Liam Girdwooda5766f12008-10-10 13:22:20 +0100830/**
831 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000832 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000833 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100834 *
835 * Allows platform initialisation code to define and constrain
836 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
837 * Constraints *must* be set by platform code in order for some
838 * regulator operations to proceed i.e. set_voltage, set_current_limit,
839 * set_mode.
840 */
841static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000842 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100843{
844 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100845 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100846
Mark Brownf8c12fe2010-11-29 15:55:17 +0000847 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
848 GFP_KERNEL);
849 if (!rdev->constraints)
850 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100851
Mark Brownf8c12fe2010-11-29 15:55:17 +0000852 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100853 if (ret != 0)
854 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800855
Liam Girdwooda5766f12008-10-10 13:22:20 +0100856 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100857 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000858 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100859 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800860 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100861 rdev->constraints = NULL;
862 goto out;
863 }
864 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100865
Mark Browna3084662009-02-26 19:24:19 +0000866 if (constraints->initial_mode) {
867 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800868 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000869 ret = -EINVAL;
870 goto out;
871 }
872
Mark Brownf8c12fe2010-11-29 15:55:17 +0000873 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000874 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800875 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000876 goto out;
877 }
878 }
879
Mark Browncacf90f2009-03-02 16:32:46 +0000880 /* If the constraints say the regulator should be on at this point
881 * and we have control then make sure it is enabled.
882 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000883 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
884 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100885 ret = ops->enable(rdev);
886 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800887 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100888 rdev->constraints = NULL;
889 goto out;
890 }
891 }
892
Liam Girdwooda5766f12008-10-10 13:22:20 +0100893 print_constraints(rdev);
894out:
895 return ret;
896}
897
898/**
899 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000900 * @rdev: regulator name
901 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100902 *
903 * Called by platform initialisation code to set the supply regulator for this
904 * regulator. This ensures that a regulators supply will also be enabled by the
905 * core if it's child is enabled.
906 */
907static int set_supply(struct regulator_dev *rdev,
908 struct regulator_dev *supply_rdev)
909{
910 int err;
911
912 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
913 "supply");
914 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800915 rdev_err(rdev, "could not add device link %s err %d\n",
916 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100917 goto out;
918 }
919 rdev->supply = supply_rdev;
920 list_add(&rdev->slist, &supply_rdev->supply_list);
921out:
922 return err;
923}
924
925/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800926 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000927 * @rdev: regulator source
928 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100929 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000930 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931 *
932 * Allows platform initialisation code to map physical regulator
933 * sources to symbolic names for supplies for use by devices. Devices
934 * should use these symbolic names to request regulators, avoiding the
935 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100936 *
937 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938 */
939static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100940 struct device *consumer_dev, const char *consumer_dev_name,
941 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100942{
943 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100944 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100945
Mark Brown40f92442009-06-17 17:56:39 +0100946 if (consumer_dev && consumer_dev_name)
947 return -EINVAL;
948
949 if (!consumer_dev_name && consumer_dev)
950 consumer_dev_name = dev_name(consumer_dev);
951
Liam Girdwooda5766f12008-10-10 13:22:20 +0100952 if (supply == NULL)
953 return -EINVAL;
954
Mark Brown9ed20992009-07-21 16:00:26 +0100955 if (consumer_dev_name != NULL)
956 has_dev = 1;
957 else
958 has_dev = 0;
959
David Brownell6001e132008-12-31 12:54:19 +0000960 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300961 if (node->dev_name && consumer_dev_name) {
962 if (strcmp(node->dev_name, consumer_dev_name) != 0)
963 continue;
964 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000965 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300966 }
967
David Brownell6001e132008-12-31 12:54:19 +0000968 if (strcmp(node->supply, supply) != 0)
969 continue;
970
971 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800972 dev_name(&node->regulator->dev),
973 node->regulator->desc->name,
974 supply,
975 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000976 return -EBUSY;
977 }
978
Mark Brown9ed20992009-07-21 16:00:26 +0100979 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100980 if (node == NULL)
981 return -ENOMEM;
982
983 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100984 node->supply = supply;
985
Mark Brown9ed20992009-07-21 16:00:26 +0100986 if (has_dev) {
987 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
988 if (node->dev_name == NULL) {
989 kfree(node);
990 return -ENOMEM;
991 }
Mark Brown40f92442009-06-17 17:56:39 +0100992 }
993
Liam Girdwooda5766f12008-10-10 13:22:20 +0100994 list_add(&node->list, &regulator_map_list);
995 return 0;
996}
997
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200998static void unset_regulator_supplies(struct regulator_dev *rdev)
999{
1000 struct regulator_map *node, *n;
1001
1002 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1003 if (rdev == node->regulator) {
1004 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001005 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001006 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001007 }
1008 }
1009}
1010
Liam Girdwood414c70c2008-04-30 15:59:04 +01001011#define REG_STR_SIZE 32
1012
1013static struct regulator *create_regulator(struct regulator_dev *rdev,
1014 struct device *dev,
1015 const char *supply_name)
1016{
1017 struct regulator *regulator;
1018 char buf[REG_STR_SIZE];
1019 int err, size;
1020
1021 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1022 if (regulator == NULL)
1023 return NULL;
1024
1025 mutex_lock(&rdev->mutex);
1026 regulator->rdev = rdev;
1027 list_add(&regulator->list, &rdev->consumer_list);
1028
1029 if (dev) {
1030 /* create a 'requested_microamps_name' sysfs entry */
1031 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1032 supply_name);
1033 if (size >= REG_STR_SIZE)
1034 goto overflow_err;
1035
1036 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001037 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001038 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1039 if (regulator->dev_attr.attr.name == NULL)
1040 goto attr_name_err;
1041
Liam Girdwood414c70c2008-04-30 15:59:04 +01001042 regulator->dev_attr.attr.mode = 0444;
1043 regulator->dev_attr.show = device_requested_uA_show;
1044 err = device_create_file(dev, &regulator->dev_attr);
1045 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001046 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001047 goto attr_name_err;
1048 }
1049
1050 /* also add a link to the device sysfs entry */
1051 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1052 dev->kobj.name, supply_name);
1053 if (size >= REG_STR_SIZE)
1054 goto attr_err;
1055
1056 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1057 if (regulator->supply_name == NULL)
1058 goto attr_err;
1059
1060 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1061 buf);
1062 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001063 rdev_warn(rdev, "could not add device link %s err %d\n",
1064 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001065 goto link_name_err;
1066 }
1067 }
1068 mutex_unlock(&rdev->mutex);
1069 return regulator;
1070link_name_err:
1071 kfree(regulator->supply_name);
1072attr_err:
1073 device_remove_file(regulator->dev, &regulator->dev_attr);
1074attr_name_err:
1075 kfree(regulator->dev_attr.attr.name);
1076overflow_err:
1077 list_del(&regulator->list);
1078 kfree(regulator);
1079 mutex_unlock(&rdev->mutex);
1080 return NULL;
1081}
1082
Mark Brown31aae2b2009-12-21 12:21:52 +00001083static int _regulator_get_enable_time(struct regulator_dev *rdev)
1084{
1085 if (!rdev->desc->ops->enable_time)
1086 return 0;
1087 return rdev->desc->ops->enable_time(rdev);
1088}
1089
Mark Brown5ffbd132009-07-21 16:00:23 +01001090/* Internal regulator request function */
1091static struct regulator *_regulator_get(struct device *dev, const char *id,
1092 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001093{
1094 struct regulator_dev *rdev;
1095 struct regulator_map *map;
1096 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001097 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001098 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001099
1100 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001101 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001102 return regulator;
1103 }
1104
Mark Brown40f92442009-06-17 17:56:39 +01001105 if (dev)
1106 devname = dev_name(dev);
1107
Liam Girdwood414c70c2008-04-30 15:59:04 +01001108 mutex_lock(&regulator_list_mutex);
1109
1110 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001111 /* If the mapping has a device set up it must match */
1112 if (map->dev_name &&
1113 (!devname || strcmp(map->dev_name, devname)))
1114 continue;
1115
1116 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001117 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001118 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001119 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001120 }
Mark Brown34abbd62010-02-12 10:18:08 +00001121
Mark Brown688fe992010-10-05 19:18:32 -07001122 if (board_wants_dummy_regulator) {
1123 rdev = dummy_regulator_rdev;
1124 goto found;
1125 }
1126
Mark Brown34abbd62010-02-12 10:18:08 +00001127#ifdef CONFIG_REGULATOR_DUMMY
1128 if (!devname)
1129 devname = "deviceless";
1130
1131 /* If the board didn't flag that it was fully constrained then
1132 * substitute in a dummy regulator so consumers can continue.
1133 */
1134 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001135 pr_warn("%s supply %s not found, using dummy regulator\n",
1136 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001137 rdev = dummy_regulator_rdev;
1138 goto found;
1139 }
1140#endif
1141
Liam Girdwood414c70c2008-04-30 15:59:04 +01001142 mutex_unlock(&regulator_list_mutex);
1143 return regulator;
1144
1145found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001146 if (rdev->exclusive) {
1147 regulator = ERR_PTR(-EPERM);
1148 goto out;
1149 }
1150
1151 if (exclusive && rdev->open_count) {
1152 regulator = ERR_PTR(-EBUSY);
1153 goto out;
1154 }
1155
Liam Girdwooda5766f12008-10-10 13:22:20 +01001156 if (!try_module_get(rdev->owner))
1157 goto out;
1158
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 regulator = create_regulator(rdev, dev, id);
1160 if (regulator == NULL) {
1161 regulator = ERR_PTR(-ENOMEM);
1162 module_put(rdev->owner);
1163 }
1164
Mark Brown5ffbd132009-07-21 16:00:23 +01001165 rdev->open_count++;
1166 if (exclusive) {
1167 rdev->exclusive = 1;
1168
1169 ret = _regulator_is_enabled(rdev);
1170 if (ret > 0)
1171 rdev->use_count = 1;
1172 else
1173 rdev->use_count = 0;
1174 }
1175
Liam Girdwooda5766f12008-10-10 13:22:20 +01001176out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001177 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001178
Liam Girdwood414c70c2008-04-30 15:59:04 +01001179 return regulator;
1180}
Mark Brown5ffbd132009-07-21 16:00:23 +01001181
1182/**
1183 * regulator_get - lookup and obtain a reference to a regulator.
1184 * @dev: device for regulator "consumer"
1185 * @id: Supply name or regulator ID.
1186 *
1187 * Returns a struct regulator corresponding to the regulator producer,
1188 * or IS_ERR() condition containing errno.
1189 *
1190 * Use of supply names configured via regulator_set_device_supply() is
1191 * strongly encouraged. It is recommended that the supply name used
1192 * should match the name used for the supply and/or the relevant
1193 * device pins in the datasheet.
1194 */
1195struct regulator *regulator_get(struct device *dev, const char *id)
1196{
1197 return _regulator_get(dev, id, 0);
1198}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001199EXPORT_SYMBOL_GPL(regulator_get);
1200
1201/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001202 * regulator_get_exclusive - obtain exclusive access to a regulator.
1203 * @dev: device for regulator "consumer"
1204 * @id: Supply name or regulator ID.
1205 *
1206 * Returns a struct regulator corresponding to the regulator producer,
1207 * or IS_ERR() condition containing errno. Other consumers will be
1208 * unable to obtain this reference is held and the use count for the
1209 * regulator will be initialised to reflect the current state of the
1210 * regulator.
1211 *
1212 * This is intended for use by consumers which cannot tolerate shared
1213 * use of the regulator such as those which need to force the
1214 * regulator off for correct operation of the hardware they are
1215 * controlling.
1216 *
1217 * Use of supply names configured via regulator_set_device_supply() is
1218 * strongly encouraged. It is recommended that the supply name used
1219 * should match the name used for the supply and/or the relevant
1220 * device pins in the datasheet.
1221 */
1222struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1223{
1224 return _regulator_get(dev, id, 1);
1225}
1226EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1227
1228/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001229 * regulator_put - "free" the regulator source
1230 * @regulator: regulator source
1231 *
1232 * Note: drivers must ensure that all regulator_enable calls made on this
1233 * regulator source are balanced by regulator_disable calls prior to calling
1234 * this function.
1235 */
1236void regulator_put(struct regulator *regulator)
1237{
1238 struct regulator_dev *rdev;
1239
1240 if (regulator == NULL || IS_ERR(regulator))
1241 return;
1242
Liam Girdwood414c70c2008-04-30 15:59:04 +01001243 mutex_lock(&regulator_list_mutex);
1244 rdev = regulator->rdev;
1245
1246 /* remove any sysfs entries */
1247 if (regulator->dev) {
1248 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1249 kfree(regulator->supply_name);
1250 device_remove_file(regulator->dev, &regulator->dev_attr);
1251 kfree(regulator->dev_attr.attr.name);
1252 }
1253 list_del(&regulator->list);
1254 kfree(regulator);
1255
Mark Brown5ffbd132009-07-21 16:00:23 +01001256 rdev->open_count--;
1257 rdev->exclusive = 0;
1258
Liam Girdwood414c70c2008-04-30 15:59:04 +01001259 module_put(rdev->owner);
1260 mutex_unlock(&regulator_list_mutex);
1261}
1262EXPORT_SYMBOL_GPL(regulator_put);
1263
Mark Brown9a2372f2009-08-03 18:49:57 +01001264static int _regulator_can_change_status(struct regulator_dev *rdev)
1265{
1266 if (!rdev->constraints)
1267 return 0;
1268
1269 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1270 return 1;
1271 else
1272 return 0;
1273}
1274
Liam Girdwood414c70c2008-04-30 15:59:04 +01001275/* locks held by regulator_enable() */
1276static int _regulator_enable(struct regulator_dev *rdev)
1277{
Mark Brown31aae2b2009-12-21 12:21:52 +00001278 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001279
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001280 if (rdev->use_count == 0) {
1281 /* do we need to enable the supply regulator first */
1282 if (rdev->supply) {
1283 mutex_lock(&rdev->supply->mutex);
1284 ret = _regulator_enable(rdev->supply);
1285 mutex_unlock(&rdev->supply->mutex);
1286 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001287 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001288 return ret;
1289 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001290 }
1291 }
1292
1293 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001294 if (rdev->constraints &&
1295 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1296 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297
Mark Brown9a2372f2009-08-03 18:49:57 +01001298 if (rdev->use_count == 0) {
1299 /* The regulator may on if it's not switchable or left on */
1300 ret = _regulator_is_enabled(rdev);
1301 if (ret == -EINVAL || ret == 0) {
1302 if (!_regulator_can_change_status(rdev))
1303 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001304
Mark Brown31aae2b2009-12-21 12:21:52 +00001305 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001306 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001307
1308 /* Query before enabling in case configuration
1309 * dependant. */
1310 ret = _regulator_get_enable_time(rdev);
1311 if (ret >= 0) {
1312 delay = ret;
1313 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001314 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001315 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001316 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001317 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001318
Mark Brown02fa3ec2010-11-10 14:38:30 +00001319 trace_regulator_enable(rdev_get_name(rdev));
1320
Mark Brown31aae2b2009-12-21 12:21:52 +00001321 /* Allow the regulator to ramp; it would be useful
1322 * to extend this for bulk operations so that the
1323 * regulators can ramp together. */
1324 ret = rdev->desc->ops->enable(rdev);
1325 if (ret < 0)
1326 return ret;
1327
Mark Brown02fa3ec2010-11-10 14:38:30 +00001328 trace_regulator_enable_delay(rdev_get_name(rdev));
1329
Axel Line36c1df2010-11-05 21:51:32 +08001330 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001331 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001332 udelay(delay % 1000);
1333 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001334 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001335 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001336
Mark Brown02fa3ec2010-11-10 14:38:30 +00001337 trace_regulator_enable_complete(rdev_get_name(rdev));
1338
Linus Walleija7433cf2009-08-26 12:54:04 +02001339 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001340 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341 return ret;
1342 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001343 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001344 }
1345
Mark Brown9a2372f2009-08-03 18:49:57 +01001346 rdev->use_count++;
1347
1348 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349}
1350
1351/**
1352 * regulator_enable - enable regulator output
1353 * @regulator: regulator source
1354 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001355 * Request that the regulator be enabled with the regulator output at
1356 * the predefined voltage or current value. Calls to regulator_enable()
1357 * must be balanced with calls to regulator_disable().
1358 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001360 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 */
1362int regulator_enable(struct regulator *regulator)
1363{
David Brownell412aec62008-11-16 11:44:46 -08001364 struct regulator_dev *rdev = regulator->rdev;
1365 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366
David Brownell412aec62008-11-16 11:44:46 -08001367 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001368 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001369 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370 return ret;
1371}
1372EXPORT_SYMBOL_GPL(regulator_enable);
1373
1374/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001375static int _regulator_disable(struct regulator_dev *rdev,
1376 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377{
1378 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001379 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001380
David Brownellcd94b502009-03-11 16:43:34 -08001381 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001382 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001383 return -EIO;
1384
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001386 if (rdev->use_count == 1 &&
1387 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388
1389 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001390 if (_regulator_can_change_status(rdev) &&
1391 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001392 trace_regulator_disable(rdev_get_name(rdev));
1393
Liam Girdwood414c70c2008-04-30 15:59:04 +01001394 ret = rdev->desc->ops->disable(rdev);
1395 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001396 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397 return ret;
1398 }
Mark Brown84b68262009-12-01 21:12:27 +00001399
Mark Brown02fa3ec2010-11-10 14:38:30 +00001400 trace_regulator_disable_complete(rdev_get_name(rdev));
1401
Mark Brown84b68262009-12-01 21:12:27 +00001402 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1403 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404 }
1405
1406 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001407 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001408
1409 rdev->use_count = 0;
1410 } else if (rdev->use_count > 1) {
1411
1412 if (rdev->constraints &&
1413 (rdev->constraints->valid_ops_mask &
1414 REGULATOR_CHANGE_DRMS))
1415 drms_uA_update(rdev);
1416
1417 rdev->use_count--;
1418 }
1419 return ret;
1420}
1421
1422/**
1423 * regulator_disable - disable regulator output
1424 * @regulator: regulator source
1425 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001426 * Disable the regulator output voltage or current. Calls to
1427 * regulator_enable() must be balanced with calls to
1428 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001429 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001431 * devices have it enabled, the regulator device supports disabling and
1432 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001433 */
1434int regulator_disable(struct regulator *regulator)
1435{
David Brownell412aec62008-11-16 11:44:46 -08001436 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001437 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001438 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439
David Brownell412aec62008-11-16 11:44:46 -08001440 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001441 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001442 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001443
1444 /* decrease our supplies ref count and disable if required */
1445 while (supply_rdev != NULL) {
1446 rdev = supply_rdev;
1447
1448 mutex_lock(&rdev->mutex);
1449 _regulator_disable(rdev, &supply_rdev);
1450 mutex_unlock(&rdev->mutex);
1451 }
1452
Liam Girdwood414c70c2008-04-30 15:59:04 +01001453 return ret;
1454}
1455EXPORT_SYMBOL_GPL(regulator_disable);
1456
1457/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001458static int _regulator_force_disable(struct regulator_dev *rdev,
1459 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460{
1461 int ret = 0;
1462
1463 /* force disable */
1464 if (rdev->desc->ops->disable) {
1465 /* ah well, who wants to live forever... */
1466 ret = rdev->desc->ops->disable(rdev);
1467 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001468 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001469 return ret;
1470 }
1471 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001472 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1473 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001474 }
1475
1476 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001477 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001478
1479 rdev->use_count = 0;
1480 return ret;
1481}
1482
1483/**
1484 * regulator_force_disable - force disable regulator output
1485 * @regulator: regulator source
1486 *
1487 * Forcibly disable the regulator output voltage or current.
1488 * NOTE: this *will* disable the regulator output even if other consumer
1489 * devices have it enabled. This should be used for situations when device
1490 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1491 */
1492int regulator_force_disable(struct regulator *regulator)
1493{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001494 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001495 int ret;
1496
1497 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001499 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001501
1502 if (supply_rdev)
1503 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1504
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505 return ret;
1506}
1507EXPORT_SYMBOL_GPL(regulator_force_disable);
1508
1509static int _regulator_is_enabled(struct regulator_dev *rdev)
1510{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001511 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001512 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001513 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001514
Mark Brown93325462009-08-03 18:49:56 +01001515 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516}
1517
1518/**
1519 * regulator_is_enabled - is the regulator output enabled
1520 * @regulator: regulator source
1521 *
David Brownell412aec62008-11-16 11:44:46 -08001522 * Returns positive if the regulator driver backing the source/client
1523 * has requested that the device be enabled, zero if it hasn't, else a
1524 * negative errno code.
1525 *
1526 * Note that the device backing this regulator handle can have multiple
1527 * users, so it might be enabled even if regulator_enable() was never
1528 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001529 */
1530int regulator_is_enabled(struct regulator *regulator)
1531{
Mark Brown93325462009-08-03 18:49:56 +01001532 int ret;
1533
1534 mutex_lock(&regulator->rdev->mutex);
1535 ret = _regulator_is_enabled(regulator->rdev);
1536 mutex_unlock(&regulator->rdev->mutex);
1537
1538 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001539}
1540EXPORT_SYMBOL_GPL(regulator_is_enabled);
1541
1542/**
David Brownell4367cfd2009-02-26 11:48:36 -08001543 * regulator_count_voltages - count regulator_list_voltage() selectors
1544 * @regulator: regulator source
1545 *
1546 * Returns number of selectors, or negative errno. Selectors are
1547 * numbered starting at zero, and typically correspond to bitfields
1548 * in hardware registers.
1549 */
1550int regulator_count_voltages(struct regulator *regulator)
1551{
1552 struct regulator_dev *rdev = regulator->rdev;
1553
1554 return rdev->desc->n_voltages ? : -EINVAL;
1555}
1556EXPORT_SYMBOL_GPL(regulator_count_voltages);
1557
1558/**
1559 * regulator_list_voltage - enumerate supported voltages
1560 * @regulator: regulator source
1561 * @selector: identify voltage to list
1562 * Context: can sleep
1563 *
1564 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001565 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001566 * negative errno.
1567 */
1568int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1569{
1570 struct regulator_dev *rdev = regulator->rdev;
1571 struct regulator_ops *ops = rdev->desc->ops;
1572 int ret;
1573
1574 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1575 return -EINVAL;
1576
1577 mutex_lock(&rdev->mutex);
1578 ret = ops->list_voltage(rdev, selector);
1579 mutex_unlock(&rdev->mutex);
1580
1581 if (ret > 0) {
1582 if (ret < rdev->constraints->min_uV)
1583 ret = 0;
1584 else if (ret > rdev->constraints->max_uV)
1585 ret = 0;
1586 }
1587
1588 return ret;
1589}
1590EXPORT_SYMBOL_GPL(regulator_list_voltage);
1591
1592/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001593 * regulator_is_supported_voltage - check if a voltage range can be supported
1594 *
1595 * @regulator: Regulator to check.
1596 * @min_uV: Minimum required voltage in uV.
1597 * @max_uV: Maximum required voltage in uV.
1598 *
1599 * Returns a boolean or a negative error code.
1600 */
1601int regulator_is_supported_voltage(struct regulator *regulator,
1602 int min_uV, int max_uV)
1603{
1604 int i, voltages, ret;
1605
1606 ret = regulator_count_voltages(regulator);
1607 if (ret < 0)
1608 return ret;
1609 voltages = ret;
1610
1611 for (i = 0; i < voltages; i++) {
1612 ret = regulator_list_voltage(regulator, i);
1613
1614 if (ret >= min_uV && ret <= max_uV)
1615 return 1;
1616 }
1617
1618 return 0;
1619}
1620
1621/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001622 * regulator_set_voltage - set regulator output voltage
1623 * @regulator: regulator source
1624 * @min_uV: Minimum required voltage in uV
1625 * @max_uV: Maximum acceptable voltage in uV
1626 *
1627 * Sets a voltage regulator to the desired output voltage. This can be set
1628 * during any regulator state. IOW, regulator can be disabled or enabled.
1629 *
1630 * If the regulator is enabled then the voltage will change to the new value
1631 * immediately otherwise if the regulator is disabled the regulator will
1632 * output at the new voltage when enabled.
1633 *
1634 * NOTE: If the regulator is shared between several devices then the lowest
1635 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001636 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001637 * calling this function otherwise this call will fail.
1638 */
1639int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1640{
1641 struct regulator_dev *rdev = regulator->rdev;
1642 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001643 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001644
1645 mutex_lock(&rdev->mutex);
1646
1647 /* sanity check */
1648 if (!rdev->desc->ops->set_voltage) {
1649 ret = -EINVAL;
1650 goto out;
1651 }
1652
1653 /* constraints check */
1654 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1655 if (ret < 0)
1656 goto out;
1657 regulator->min_uV = min_uV;
1658 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001659
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001660 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1661 if (ret < 0)
1662 goto out;
1663
Mark Brown02fa3ec2010-11-10 14:38:30 +00001664 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1665
Mark Brown3a93f2a2010-11-10 14:38:29 +00001666 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1667
1668 if (rdev->desc->ops->list_voltage)
1669 selector = rdev->desc->ops->list_voltage(rdev, selector);
1670 else
1671 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001672
Mark Brown02fa3ec2010-11-10 14:38:30 +00001673 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1674
Liam Girdwood414c70c2008-04-30 15:59:04 +01001675out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001676 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001677 mutex_unlock(&rdev->mutex);
1678 return ret;
1679}
1680EXPORT_SYMBOL_GPL(regulator_set_voltage);
1681
1682static int _regulator_get_voltage(struct regulator_dev *rdev)
1683{
1684 /* sanity check */
1685 if (rdev->desc->ops->get_voltage)
1686 return rdev->desc->ops->get_voltage(rdev);
1687 else
1688 return -EINVAL;
1689}
1690
1691/**
1692 * regulator_get_voltage - get regulator output voltage
1693 * @regulator: regulator source
1694 *
1695 * This returns the current regulator voltage in uV.
1696 *
1697 * NOTE: If the regulator is disabled it will return the voltage value. This
1698 * function should not be used to determine regulator state.
1699 */
1700int regulator_get_voltage(struct regulator *regulator)
1701{
1702 int ret;
1703
1704 mutex_lock(&regulator->rdev->mutex);
1705
1706 ret = _regulator_get_voltage(regulator->rdev);
1707
1708 mutex_unlock(&regulator->rdev->mutex);
1709
1710 return ret;
1711}
1712EXPORT_SYMBOL_GPL(regulator_get_voltage);
1713
1714/**
1715 * regulator_set_current_limit - set regulator output current limit
1716 * @regulator: regulator source
1717 * @min_uA: Minimuum supported current in uA
1718 * @max_uA: Maximum supported current in uA
1719 *
1720 * Sets current sink to the desired output current. This can be set during
1721 * any regulator state. IOW, regulator can be disabled or enabled.
1722 *
1723 * If the regulator is enabled then the current will change to the new value
1724 * immediately otherwise if the regulator is disabled the regulator will
1725 * output at the new current when enabled.
1726 *
1727 * NOTE: Regulator system constraints must be set for this regulator before
1728 * calling this function otherwise this call will fail.
1729 */
1730int regulator_set_current_limit(struct regulator *regulator,
1731 int min_uA, int max_uA)
1732{
1733 struct regulator_dev *rdev = regulator->rdev;
1734 int ret;
1735
1736 mutex_lock(&rdev->mutex);
1737
1738 /* sanity check */
1739 if (!rdev->desc->ops->set_current_limit) {
1740 ret = -EINVAL;
1741 goto out;
1742 }
1743
1744 /* constraints check */
1745 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1746 if (ret < 0)
1747 goto out;
1748
1749 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1750out:
1751 mutex_unlock(&rdev->mutex);
1752 return ret;
1753}
1754EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1755
1756static int _regulator_get_current_limit(struct regulator_dev *rdev)
1757{
1758 int ret;
1759
1760 mutex_lock(&rdev->mutex);
1761
1762 /* sanity check */
1763 if (!rdev->desc->ops->get_current_limit) {
1764 ret = -EINVAL;
1765 goto out;
1766 }
1767
1768 ret = rdev->desc->ops->get_current_limit(rdev);
1769out:
1770 mutex_unlock(&rdev->mutex);
1771 return ret;
1772}
1773
1774/**
1775 * regulator_get_current_limit - get regulator output current
1776 * @regulator: regulator source
1777 *
1778 * This returns the current supplied by the specified current sink in uA.
1779 *
1780 * NOTE: If the regulator is disabled it will return the current value. This
1781 * function should not be used to determine regulator state.
1782 */
1783int regulator_get_current_limit(struct regulator *regulator)
1784{
1785 return _regulator_get_current_limit(regulator->rdev);
1786}
1787EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1788
1789/**
1790 * regulator_set_mode - set regulator operating mode
1791 * @regulator: regulator source
1792 * @mode: operating mode - one of the REGULATOR_MODE constants
1793 *
1794 * Set regulator operating mode to increase regulator efficiency or improve
1795 * regulation performance.
1796 *
1797 * NOTE: Regulator system constraints must be set for this regulator before
1798 * calling this function otherwise this call will fail.
1799 */
1800int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1801{
1802 struct regulator_dev *rdev = regulator->rdev;
1803 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301804 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001805
1806 mutex_lock(&rdev->mutex);
1807
1808 /* sanity check */
1809 if (!rdev->desc->ops->set_mode) {
1810 ret = -EINVAL;
1811 goto out;
1812 }
1813
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301814 /* return if the same mode is requested */
1815 if (rdev->desc->ops->get_mode) {
1816 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1817 if (regulator_curr_mode == mode) {
1818 ret = 0;
1819 goto out;
1820 }
1821 }
1822
Liam Girdwood414c70c2008-04-30 15:59:04 +01001823 /* constraints check */
1824 ret = regulator_check_mode(rdev, mode);
1825 if (ret < 0)
1826 goto out;
1827
1828 ret = rdev->desc->ops->set_mode(rdev, mode);
1829out:
1830 mutex_unlock(&rdev->mutex);
1831 return ret;
1832}
1833EXPORT_SYMBOL_GPL(regulator_set_mode);
1834
1835static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1836{
1837 int ret;
1838
1839 mutex_lock(&rdev->mutex);
1840
1841 /* sanity check */
1842 if (!rdev->desc->ops->get_mode) {
1843 ret = -EINVAL;
1844 goto out;
1845 }
1846
1847 ret = rdev->desc->ops->get_mode(rdev);
1848out:
1849 mutex_unlock(&rdev->mutex);
1850 return ret;
1851}
1852
1853/**
1854 * regulator_get_mode - get regulator operating mode
1855 * @regulator: regulator source
1856 *
1857 * Get the current regulator operating mode.
1858 */
1859unsigned int regulator_get_mode(struct regulator *regulator)
1860{
1861 return _regulator_get_mode(regulator->rdev);
1862}
1863EXPORT_SYMBOL_GPL(regulator_get_mode);
1864
1865/**
1866 * regulator_set_optimum_mode - set regulator optimum operating mode
1867 * @regulator: regulator source
1868 * @uA_load: load current
1869 *
1870 * Notifies the regulator core of a new device load. This is then used by
1871 * DRMS (if enabled by constraints) to set the most efficient regulator
1872 * operating mode for the new regulator loading.
1873 *
1874 * Consumer devices notify their supply regulator of the maximum power
1875 * they will require (can be taken from device datasheet in the power
1876 * consumption tables) when they change operational status and hence power
1877 * state. Examples of operational state changes that can affect power
1878 * consumption are :-
1879 *
1880 * o Device is opened / closed.
1881 * o Device I/O is about to begin or has just finished.
1882 * o Device is idling in between work.
1883 *
1884 * This information is also exported via sysfs to userspace.
1885 *
1886 * DRMS will sum the total requested load on the regulator and change
1887 * to the most efficient operating mode if platform constraints allow.
1888 *
1889 * Returns the new regulator mode or error.
1890 */
1891int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1892{
1893 struct regulator_dev *rdev = regulator->rdev;
1894 struct regulator *consumer;
1895 int ret, output_uV, input_uV, total_uA_load = 0;
1896 unsigned int mode;
1897
1898 mutex_lock(&rdev->mutex);
1899
1900 regulator->uA_load = uA_load;
1901 ret = regulator_check_drms(rdev);
1902 if (ret < 0)
1903 goto out;
1904 ret = -EINVAL;
1905
1906 /* sanity check */
1907 if (!rdev->desc->ops->get_optimum_mode)
1908 goto out;
1909
1910 /* get output voltage */
1911 output_uV = rdev->desc->ops->get_voltage(rdev);
1912 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001913 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001914 goto out;
1915 }
1916
1917 /* get input voltage */
1918 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1919 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1920 else
1921 input_uV = rdev->constraints->input_uV;
1922 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001923 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001924 goto out;
1925 }
1926
1927 /* calc total requested load for this regulator */
1928 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001929 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001930
1931 mode = rdev->desc->ops->get_optimum_mode(rdev,
1932 input_uV, output_uV,
1933 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001934 ret = regulator_check_mode(rdev, mode);
1935 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001936 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
1937 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001938 goto out;
1939 }
1940
1941 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001942 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001943 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001944 goto out;
1945 }
1946 ret = mode;
1947out:
1948 mutex_unlock(&rdev->mutex);
1949 return ret;
1950}
1951EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1952
1953/**
1954 * regulator_register_notifier - register regulator event notifier
1955 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001956 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001957 *
1958 * Register notifier block to receive regulator events.
1959 */
1960int regulator_register_notifier(struct regulator *regulator,
1961 struct notifier_block *nb)
1962{
1963 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1964 nb);
1965}
1966EXPORT_SYMBOL_GPL(regulator_register_notifier);
1967
1968/**
1969 * regulator_unregister_notifier - unregister regulator event notifier
1970 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001971 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001972 *
1973 * Unregister regulator event notifier block.
1974 */
1975int regulator_unregister_notifier(struct regulator *regulator,
1976 struct notifier_block *nb)
1977{
1978 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1979 nb);
1980}
1981EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1982
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001983/* notify regulator consumers and downstream regulator consumers.
1984 * Note mutex must be held by caller.
1985 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001986static void _notifier_call_chain(struct regulator_dev *rdev,
1987 unsigned long event, void *data)
1988{
1989 struct regulator_dev *_rdev;
1990
1991 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001992 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993
1994 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001995 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001996 mutex_lock(&_rdev->mutex);
1997 _notifier_call_chain(_rdev, event, data);
1998 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001999 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002000}
2001
2002/**
2003 * regulator_bulk_get - get multiple regulator consumers
2004 *
2005 * @dev: Device to supply
2006 * @num_consumers: Number of consumers to register
2007 * @consumers: Configuration of consumers; clients are stored here.
2008 *
2009 * @return 0 on success, an errno on failure.
2010 *
2011 * This helper function allows drivers to get several regulator
2012 * consumers in one operation. If any of the regulators cannot be
2013 * acquired then any regulators that were allocated will be freed
2014 * before returning to the caller.
2015 */
2016int regulator_bulk_get(struct device *dev, int num_consumers,
2017 struct regulator_bulk_data *consumers)
2018{
2019 int i;
2020 int ret;
2021
2022 for (i = 0; i < num_consumers; i++)
2023 consumers[i].consumer = NULL;
2024
2025 for (i = 0; i < num_consumers; i++) {
2026 consumers[i].consumer = regulator_get(dev,
2027 consumers[i].supply);
2028 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002029 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002030 dev_err(dev, "Failed to get supply '%s': %d\n",
2031 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002032 consumers[i].consumer = NULL;
2033 goto err;
2034 }
2035 }
2036
2037 return 0;
2038
2039err:
2040 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2041 regulator_put(consumers[i].consumer);
2042
2043 return ret;
2044}
2045EXPORT_SYMBOL_GPL(regulator_bulk_get);
2046
2047/**
2048 * regulator_bulk_enable - enable multiple regulator consumers
2049 *
2050 * @num_consumers: Number of consumers
2051 * @consumers: Consumer data; clients are stored here.
2052 * @return 0 on success, an errno on failure
2053 *
2054 * This convenience API allows consumers to enable multiple regulator
2055 * clients in a single API call. If any consumers cannot be enabled
2056 * then any others that were enabled will be disabled again prior to
2057 * return.
2058 */
2059int regulator_bulk_enable(int num_consumers,
2060 struct regulator_bulk_data *consumers)
2061{
2062 int i;
2063 int ret;
2064
2065 for (i = 0; i < num_consumers; i++) {
2066 ret = regulator_enable(consumers[i].consumer);
2067 if (ret != 0)
2068 goto err;
2069 }
2070
2071 return 0;
2072
2073err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002074 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002075 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002076 regulator_disable(consumers[i].consumer);
2077
2078 return ret;
2079}
2080EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2081
2082/**
2083 * regulator_bulk_disable - disable multiple regulator consumers
2084 *
2085 * @num_consumers: Number of consumers
2086 * @consumers: Consumer data; clients are stored here.
2087 * @return 0 on success, an errno on failure
2088 *
2089 * This convenience API allows consumers to disable multiple regulator
2090 * clients in a single API call. If any consumers cannot be enabled
2091 * then any others that were disabled will be disabled again prior to
2092 * return.
2093 */
2094int regulator_bulk_disable(int num_consumers,
2095 struct regulator_bulk_data *consumers)
2096{
2097 int i;
2098 int ret;
2099
2100 for (i = 0; i < num_consumers; i++) {
2101 ret = regulator_disable(consumers[i].consumer);
2102 if (ret != 0)
2103 goto err;
2104 }
2105
2106 return 0;
2107
2108err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002109 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002110 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002111 regulator_enable(consumers[i].consumer);
2112
2113 return ret;
2114}
2115EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2116
2117/**
2118 * regulator_bulk_free - free multiple regulator consumers
2119 *
2120 * @num_consumers: Number of consumers
2121 * @consumers: Consumer data; clients are stored here.
2122 *
2123 * This convenience API allows consumers to free multiple regulator
2124 * clients in a single API call.
2125 */
2126void regulator_bulk_free(int num_consumers,
2127 struct regulator_bulk_data *consumers)
2128{
2129 int i;
2130
2131 for (i = 0; i < num_consumers; i++) {
2132 regulator_put(consumers[i].consumer);
2133 consumers[i].consumer = NULL;
2134 }
2135}
2136EXPORT_SYMBOL_GPL(regulator_bulk_free);
2137
2138/**
2139 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002140 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002141 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002142 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 *
2144 * Called by regulator drivers to notify clients a regulator event has
2145 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002146 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147 */
2148int regulator_notifier_call_chain(struct regulator_dev *rdev,
2149 unsigned long event, void *data)
2150{
2151 _notifier_call_chain(rdev, event, data);
2152 return NOTIFY_DONE;
2153
2154}
2155EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2156
Mark Brownbe721972009-08-04 20:09:52 +02002157/**
2158 * regulator_mode_to_status - convert a regulator mode into a status
2159 *
2160 * @mode: Mode to convert
2161 *
2162 * Convert a regulator mode into a status.
2163 */
2164int regulator_mode_to_status(unsigned int mode)
2165{
2166 switch (mode) {
2167 case REGULATOR_MODE_FAST:
2168 return REGULATOR_STATUS_FAST;
2169 case REGULATOR_MODE_NORMAL:
2170 return REGULATOR_STATUS_NORMAL;
2171 case REGULATOR_MODE_IDLE:
2172 return REGULATOR_STATUS_IDLE;
2173 case REGULATOR_STATUS_STANDBY:
2174 return REGULATOR_STATUS_STANDBY;
2175 default:
2176 return 0;
2177 }
2178}
2179EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2180
David Brownell7ad68e22008-11-11 17:39:02 -08002181/*
2182 * To avoid cluttering sysfs (and memory) with useless state, only
2183 * create attributes that can be meaningfully displayed.
2184 */
2185static int add_regulator_attributes(struct regulator_dev *rdev)
2186{
2187 struct device *dev = &rdev->dev;
2188 struct regulator_ops *ops = rdev->desc->ops;
2189 int status = 0;
2190
2191 /* some attributes need specific methods to be displayed */
2192 if (ops->get_voltage) {
2193 status = device_create_file(dev, &dev_attr_microvolts);
2194 if (status < 0)
2195 return status;
2196 }
2197 if (ops->get_current_limit) {
2198 status = device_create_file(dev, &dev_attr_microamps);
2199 if (status < 0)
2200 return status;
2201 }
2202 if (ops->get_mode) {
2203 status = device_create_file(dev, &dev_attr_opmode);
2204 if (status < 0)
2205 return status;
2206 }
2207 if (ops->is_enabled) {
2208 status = device_create_file(dev, &dev_attr_state);
2209 if (status < 0)
2210 return status;
2211 }
David Brownell853116a2009-01-14 23:03:17 -08002212 if (ops->get_status) {
2213 status = device_create_file(dev, &dev_attr_status);
2214 if (status < 0)
2215 return status;
2216 }
David Brownell7ad68e22008-11-11 17:39:02 -08002217
2218 /* some attributes are type-specific */
2219 if (rdev->desc->type == REGULATOR_CURRENT) {
2220 status = device_create_file(dev, &dev_attr_requested_microamps);
2221 if (status < 0)
2222 return status;
2223 }
2224
2225 /* all the other attributes exist to support constraints;
2226 * don't show them if there are no constraints, or if the
2227 * relevant supporting methods are missing.
2228 */
2229 if (!rdev->constraints)
2230 return status;
2231
2232 /* constraints need specific supporting methods */
2233 if (ops->set_voltage) {
2234 status = device_create_file(dev, &dev_attr_min_microvolts);
2235 if (status < 0)
2236 return status;
2237 status = device_create_file(dev, &dev_attr_max_microvolts);
2238 if (status < 0)
2239 return status;
2240 }
2241 if (ops->set_current_limit) {
2242 status = device_create_file(dev, &dev_attr_min_microamps);
2243 if (status < 0)
2244 return status;
2245 status = device_create_file(dev, &dev_attr_max_microamps);
2246 if (status < 0)
2247 return status;
2248 }
2249
2250 /* suspend mode constraints need multiple supporting methods */
2251 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2252 return status;
2253
2254 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2255 if (status < 0)
2256 return status;
2257 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2258 if (status < 0)
2259 return status;
2260 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2261 if (status < 0)
2262 return status;
2263
2264 if (ops->set_suspend_voltage) {
2265 status = device_create_file(dev,
2266 &dev_attr_suspend_standby_microvolts);
2267 if (status < 0)
2268 return status;
2269 status = device_create_file(dev,
2270 &dev_attr_suspend_mem_microvolts);
2271 if (status < 0)
2272 return status;
2273 status = device_create_file(dev,
2274 &dev_attr_suspend_disk_microvolts);
2275 if (status < 0)
2276 return status;
2277 }
2278
2279 if (ops->set_suspend_mode) {
2280 status = device_create_file(dev,
2281 &dev_attr_suspend_standby_mode);
2282 if (status < 0)
2283 return status;
2284 status = device_create_file(dev,
2285 &dev_attr_suspend_mem_mode);
2286 if (status < 0)
2287 return status;
2288 status = device_create_file(dev,
2289 &dev_attr_suspend_disk_mode);
2290 if (status < 0)
2291 return status;
2292 }
2293
2294 return status;
2295}
2296
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297/**
2298 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002299 * @regulator_desc: regulator to register
2300 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002301 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002302 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002303 *
2304 * Called by regulator drivers to register a regulator.
2305 * Returns 0 on success.
2306 */
2307struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002308 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002309 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002310{
2311 static atomic_t regulator_no = ATOMIC_INIT(0);
2312 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002313 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002314
2315 if (regulator_desc == NULL)
2316 return ERR_PTR(-EINVAL);
2317
2318 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2319 return ERR_PTR(-EINVAL);
2320
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002321 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2322 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002323 return ERR_PTR(-EINVAL);
2324
Mark Brown46fabe1e2008-09-09 16:21:18 +01002325 if (!init_data)
2326 return ERR_PTR(-EINVAL);
2327
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2329 if (rdev == NULL)
2330 return ERR_PTR(-ENOMEM);
2331
2332 mutex_lock(&regulator_list_mutex);
2333
2334 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002335 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002336 rdev->owner = regulator_desc->owner;
2337 rdev->desc = regulator_desc;
2338 INIT_LIST_HEAD(&rdev->consumer_list);
2339 INIT_LIST_HEAD(&rdev->supply_list);
2340 INIT_LIST_HEAD(&rdev->list);
2341 INIT_LIST_HEAD(&rdev->slist);
2342 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2343
Liam Girdwooda5766f12008-10-10 13:22:20 +01002344 /* preform any regulator specific init */
2345 if (init_data->regulator_init) {
2346 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002347 if (ret < 0)
2348 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002349 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002350
Liam Girdwooda5766f12008-10-10 13:22:20 +01002351 /* register with sysfs */
2352 rdev->dev.class = &regulator_class;
2353 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002354 dev_set_name(&rdev->dev, "regulator.%d",
2355 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002356 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002357 if (ret != 0) {
2358 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002359 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002360 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002361
2362 dev_set_drvdata(&rdev->dev, rdev);
2363
Mike Rapoport74f544c2008-11-25 14:53:53 +02002364 /* set regulator constraints */
2365 ret = set_machine_constraints(rdev, &init_data->constraints);
2366 if (ret < 0)
2367 goto scrub;
2368
David Brownell7ad68e22008-11-11 17:39:02 -08002369 /* add attributes supported by this regulator */
2370 ret = add_regulator_attributes(rdev);
2371 if (ret < 0)
2372 goto scrub;
2373
Liam Girdwooda5766f12008-10-10 13:22:20 +01002374 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002375 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2376 dev_err(dev,
2377 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002378 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002379 goto scrub;
2380 }
2381
2382 if (init_data->supply_regulator) {
2383 struct regulator_dev *r;
2384 int found = 0;
2385
2386 list_for_each_entry(r, &regulator_list, list) {
2387 if (strcmp(rdev_get_name(r),
2388 init_data->supply_regulator) == 0) {
2389 found = 1;
2390 break;
2391 }
2392 }
2393
2394 if (!found) {
2395 dev_err(dev, "Failed to find supply %s\n",
2396 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002397 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002398 goto scrub;
2399 }
2400
2401 ret = set_supply(rdev, r);
2402 if (ret < 0)
2403 goto scrub;
2404 }
2405
Liam Girdwooda5766f12008-10-10 13:22:20 +01002406 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002407 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002408 ret = set_supply(rdev,
2409 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002410 if (ret < 0)
2411 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002412 }
2413
2414 /* add consumers devices */
2415 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2416 ret = set_consumer_device_supply(rdev,
2417 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002418 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002419 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002420 if (ret < 0)
2421 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002422 }
2423
2424 list_add(&rdev->list, &regulator_list);
2425out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002426 mutex_unlock(&regulator_list_mutex);
2427 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002428
Jani Nikulad4033b52010-04-29 10:55:11 +03002429unset_supplies:
2430 unset_regulator_supplies(rdev);
2431
David Brownell4fca9542008-11-11 17:38:53 -08002432scrub:
2433 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002434 /* device core frees rdev */
2435 rdev = ERR_PTR(ret);
2436 goto out;
2437
David Brownell4fca9542008-11-11 17:38:53 -08002438clean:
2439 kfree(rdev);
2440 rdev = ERR_PTR(ret);
2441 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002442}
2443EXPORT_SYMBOL_GPL(regulator_register);
2444
2445/**
2446 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002447 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002448 *
2449 * Called by regulator drivers to unregister a regulator.
2450 */
2451void regulator_unregister(struct regulator_dev *rdev)
2452{
2453 if (rdev == NULL)
2454 return;
2455
2456 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002457 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002458 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002459 list_del(&rdev->list);
2460 if (rdev->supply)
2461 sysfs_remove_link(&rdev->dev.kobj, "supply");
2462 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002463 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002464 mutex_unlock(&regulator_list_mutex);
2465}
2466EXPORT_SYMBOL_GPL(regulator_unregister);
2467
2468/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002469 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002470 * @state: system suspend state
2471 *
2472 * Configure each regulator with it's suspend operating parameters for state.
2473 * This will usually be called by machine suspend code prior to supending.
2474 */
2475int regulator_suspend_prepare(suspend_state_t state)
2476{
2477 struct regulator_dev *rdev;
2478 int ret = 0;
2479
2480 /* ON is handled by regulator active state */
2481 if (state == PM_SUSPEND_ON)
2482 return -EINVAL;
2483
2484 mutex_lock(&regulator_list_mutex);
2485 list_for_each_entry(rdev, &regulator_list, list) {
2486
2487 mutex_lock(&rdev->mutex);
2488 ret = suspend_prepare(rdev, state);
2489 mutex_unlock(&rdev->mutex);
2490
2491 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002492 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002493 goto out;
2494 }
2495 }
2496out:
2497 mutex_unlock(&regulator_list_mutex);
2498 return ret;
2499}
2500EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2501
2502/**
Mark Brownca725562009-03-16 19:36:34 +00002503 * regulator_has_full_constraints - the system has fully specified constraints
2504 *
2505 * Calling this function will cause the regulator API to disable all
2506 * regulators which have a zero use count and don't have an always_on
2507 * constraint in a late_initcall.
2508 *
2509 * The intention is that this will become the default behaviour in a
2510 * future kernel release so users are encouraged to use this facility
2511 * now.
2512 */
2513void regulator_has_full_constraints(void)
2514{
2515 has_full_constraints = 1;
2516}
2517EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2518
2519/**
Mark Brown688fe992010-10-05 19:18:32 -07002520 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2521 *
2522 * Calling this function will cause the regulator API to provide a
2523 * dummy regulator to consumers if no physical regulator is found,
2524 * allowing most consumers to proceed as though a regulator were
2525 * configured. This allows systems such as those with software
2526 * controllable regulators for the CPU core only to be brought up more
2527 * readily.
2528 */
2529void regulator_use_dummy_regulator(void)
2530{
2531 board_wants_dummy_regulator = true;
2532}
2533EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2534
2535/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002536 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002537 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 *
2539 * Get rdev regulator driver private data. This call can be used in the
2540 * regulator driver context.
2541 */
2542void *rdev_get_drvdata(struct regulator_dev *rdev)
2543{
2544 return rdev->reg_data;
2545}
2546EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2547
2548/**
2549 * regulator_get_drvdata - get regulator driver data
2550 * @regulator: regulator
2551 *
2552 * Get regulator driver private data. This call can be used in the consumer
2553 * driver context when non API regulator specific functions need to be called.
2554 */
2555void *regulator_get_drvdata(struct regulator *regulator)
2556{
2557 return regulator->rdev->reg_data;
2558}
2559EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2560
2561/**
2562 * regulator_set_drvdata - set regulator driver data
2563 * @regulator: regulator
2564 * @data: data
2565 */
2566void regulator_set_drvdata(struct regulator *regulator, void *data)
2567{
2568 regulator->rdev->reg_data = data;
2569}
2570EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2571
2572/**
2573 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002574 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002575 */
2576int rdev_get_id(struct regulator_dev *rdev)
2577{
2578 return rdev->desc->id;
2579}
2580EXPORT_SYMBOL_GPL(rdev_get_id);
2581
Liam Girdwooda5766f12008-10-10 13:22:20 +01002582struct device *rdev_get_dev(struct regulator_dev *rdev)
2583{
2584 return &rdev->dev;
2585}
2586EXPORT_SYMBOL_GPL(rdev_get_dev);
2587
2588void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2589{
2590 return reg_init_data->driver_data;
2591}
2592EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2593
Liam Girdwood414c70c2008-04-30 15:59:04 +01002594static int __init regulator_init(void)
2595{
Mark Brown34abbd62010-02-12 10:18:08 +00002596 int ret;
2597
Mark Brown34abbd62010-02-12 10:18:08 +00002598 ret = class_register(&regulator_class);
2599
2600 regulator_dummy_init();
2601
2602 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002603}
2604
2605/* init early to allow our consumers to complete system booting */
2606core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002607
2608static int __init regulator_init_complete(void)
2609{
2610 struct regulator_dev *rdev;
2611 struct regulator_ops *ops;
2612 struct regulation_constraints *c;
2613 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002614
2615 mutex_lock(&regulator_list_mutex);
2616
2617 /* If we have a full configuration then disable any regulators
2618 * which are not in use or always_on. This will become the
2619 * default behaviour in the future.
2620 */
2621 list_for_each_entry(rdev, &regulator_list, list) {
2622 ops = rdev->desc->ops;
2623 c = rdev->constraints;
2624
Mark Brownf25e0b42009-08-03 18:49:55 +01002625 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002626 continue;
2627
2628 mutex_lock(&rdev->mutex);
2629
2630 if (rdev->use_count)
2631 goto unlock;
2632
2633 /* If we can't read the status assume it's on. */
2634 if (ops->is_enabled)
2635 enabled = ops->is_enabled(rdev);
2636 else
2637 enabled = 1;
2638
2639 if (!enabled)
2640 goto unlock;
2641
2642 if (has_full_constraints) {
2643 /* We log since this may kill the system if it
2644 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002645 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002646 ret = ops->disable(rdev);
2647 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002648 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002649 }
2650 } else {
2651 /* The intention is that in future we will
2652 * assume that full constraints are provided
2653 * so warn even if we aren't going to do
2654 * anything here.
2655 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002656 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002657 }
2658
2659unlock:
2660 mutex_unlock(&rdev->mutex);
2661 }
2662
2663 mutex_unlock(&regulator_list_mutex);
2664
2665 return 0;
2666}
2667late_initcall(regulator_init_complete);