blob: 671eb53b5cdc1a1125abc36712923c56ee785bdb [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 */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000584 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585 if (output_uV <= 0)
586 return;
587
588 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +0000589 input_uV = 0;
590 if (rdev->supply)
591 input_uV = _regulator_get_voltage(rdev);
592 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100593 input_uV = rdev->constraints->input_uV;
594 if (input_uV <= 0)
595 return;
596
597 /* calc total requested load */
598 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100599 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100600
601 /* now get the optimum mode for our new total regulator load */
602 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
603 output_uV, current_uA);
604
605 /* check the new mode is allowed */
606 err = regulator_check_mode(rdev, mode);
607 if (err == 0)
608 rdev->desc->ops->set_mode(rdev, mode);
609}
610
611static int suspend_set_state(struct regulator_dev *rdev,
612 struct regulator_state *rstate)
613{
614 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100615 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100616
Mark Brown638f85c2009-10-22 16:31:33 +0100617 can_set_state = rdev->desc->ops->set_suspend_enable &&
618 rdev->desc->ops->set_suspend_disable;
619
620 /* If we have no suspend mode configration don't set anything;
621 * only warn if the driver actually makes the suspend mode
622 * configurable.
623 */
624 if (!rstate->enabled && !rstate->disabled) {
625 if (can_set_state)
Joe Perches5da84fd2010-11-30 05:53:48 -0800626 rdev_warn(rdev, "No configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100627 return 0;
628 }
629
630 if (rstate->enabled && rstate->disabled) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800631 rdev_err(rdev, "invalid configuration\n");
Mark Brown638f85c2009-10-22 16:31:33 +0100632 return -EINVAL;
633 }
634
635 if (!can_set_state) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800636 rdev_err(rdev, "no way to set suspend state\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100637 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100638 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100639
640 if (rstate->enabled)
641 ret = rdev->desc->ops->set_suspend_enable(rdev);
642 else
643 ret = rdev->desc->ops->set_suspend_disable(rdev);
644 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800645 rdev_err(rdev, "failed to enabled/disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100646 return ret;
647 }
648
649 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
650 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
651 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800652 rdev_err(rdev, "failed to set voltage\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100653 return ret;
654 }
655 }
656
657 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
658 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
659 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800660 rdev_err(rdev, "failed to set mode\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +0100661 return ret;
662 }
663 }
664 return ret;
665}
666
667/* locks held by caller */
668static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
669{
670 if (!rdev->constraints)
671 return -EINVAL;
672
673 switch (state) {
674 case PM_SUSPEND_STANDBY:
675 return suspend_set_state(rdev,
676 &rdev->constraints->state_standby);
677 case PM_SUSPEND_MEM:
678 return suspend_set_state(rdev,
679 &rdev->constraints->state_mem);
680 case PM_SUSPEND_MAX:
681 return suspend_set_state(rdev,
682 &rdev->constraints->state_disk);
683 default:
684 return -EINVAL;
685 }
686}
687
688static void print_constraints(struct regulator_dev *rdev)
689{
690 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000691 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100692 int count = 0;
693 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100694
Mark Brown8f031b42009-10-22 16:31:31 +0100695 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100696 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100697 count += sprintf(buf + count, "%d mV ",
698 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100699 else
Mark Brown8f031b42009-10-22 16:31:31 +0100700 count += sprintf(buf + count, "%d <--> %d mV ",
701 constraints->min_uV / 1000,
702 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100703 }
Mark Brown8f031b42009-10-22 16:31:31 +0100704
705 if (!constraints->min_uV ||
706 constraints->min_uV != constraints->max_uV) {
707 ret = _regulator_get_voltage(rdev);
708 if (ret > 0)
709 count += sprintf(buf + count, "at %d mV ", ret / 1000);
710 }
711
712 if (constraints->min_uA && constraints->max_uA) {
713 if (constraints->min_uA == constraints->max_uA)
714 count += sprintf(buf + count, "%d mA ",
715 constraints->min_uA / 1000);
716 else
717 count += sprintf(buf + count, "%d <--> %d mA ",
718 constraints->min_uA / 1000,
719 constraints->max_uA / 1000);
720 }
721
722 if (!constraints->min_uA ||
723 constraints->min_uA != constraints->max_uA) {
724 ret = _regulator_get_current_limit(rdev);
725 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400726 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100727 }
728
Liam Girdwood414c70c2008-04-30 15:59:04 +0100729 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
730 count += sprintf(buf + count, "fast ");
731 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
732 count += sprintf(buf + count, "normal ");
733 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
734 count += sprintf(buf + count, "idle ");
735 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
736 count += sprintf(buf + count, "standby");
737
Joe Perches5da84fd2010-11-30 05:53:48 -0800738 rdev_info(rdev, "regulator: %s\n", buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100739}
740
Mark Browne79055d2009-10-19 15:53:50 +0100741static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100742 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100743{
744 struct regulator_ops *ops = rdev->desc->ops;
Mark Brownaf5866c2009-10-22 16:31:30 +0100745 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000746 unsigned selector;
Mark Brownaf5866c2009-10-22 16:31:30 +0100747
748 /* do we need to apply the constraint voltage */
749 if (rdev->constraints->apply_uV &&
750 rdev->constraints->min_uV == rdev->constraints->max_uV &&
751 ops->set_voltage) {
752 ret = ops->set_voltage(rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000753 rdev->constraints->min_uV,
754 rdev->constraints->max_uV,
755 &selector);
Mark Brownaf5866c2009-10-22 16:31:30 +0100756 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800757 rdev_err(rdev, "failed to apply %duV constraint\n",
758 rdev->constraints->min_uV);
Mark Brownaf5866c2009-10-22 16:31:30 +0100759 rdev->constraints = NULL;
760 return ret;
761 }
762 }
Mark Browne79055d2009-10-19 15:53:50 +0100763
764 /* constrain machine-level voltage specs to fit
765 * the actual range supported by this regulator.
766 */
767 if (ops->list_voltage && rdev->desc->n_voltages) {
768 int count = rdev->desc->n_voltages;
769 int i;
770 int min_uV = INT_MAX;
771 int max_uV = INT_MIN;
772 int cmin = constraints->min_uV;
773 int cmax = constraints->max_uV;
774
775 /* it's safe to autoconfigure fixed-voltage supplies
776 and the constraints are used by list_voltage. */
777 if (count == 1 && !cmin) {
778 cmin = 1;
779 cmax = INT_MAX;
780 constraints->min_uV = cmin;
781 constraints->max_uV = cmax;
782 }
783
784 /* voltage constraints are optional */
785 if ((cmin == 0) && (cmax == 0))
786 return 0;
787
788 /* else require explicit machine-level constraints */
789 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800790 rdev_err(rdev, "invalid voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100791 return -EINVAL;
792 }
793
794 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
795 for (i = 0; i < count; i++) {
796 int value;
797
798 value = ops->list_voltage(rdev, i);
799 if (value <= 0)
800 continue;
801
802 /* maybe adjust [min_uV..max_uV] */
803 if (value >= cmin && value < min_uV)
804 min_uV = value;
805 if (value <= cmax && value > max_uV)
806 max_uV = value;
807 }
808
809 /* final: [min_uV..max_uV] valid iff constraints valid */
810 if (max_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800811 rdev_err(rdev, "unsupportable voltage constraints\n");
Mark Browne79055d2009-10-19 15:53:50 +0100812 return -EINVAL;
813 }
814
815 /* use regulator's subset of machine constraints */
816 if (constraints->min_uV < min_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800817 rdev_dbg(rdev, "override min_uV, %d -> %d\n",
818 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100819 constraints->min_uV = min_uV;
820 }
821 if (constraints->max_uV > max_uV) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800822 rdev_dbg(rdev, "override max_uV, %d -> %d\n",
823 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100824 constraints->max_uV = max_uV;
825 }
826 }
827
828 return 0;
829}
830
Liam Girdwooda5766f12008-10-10 13:22:20 +0100831/**
832 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000833 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000834 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100835 *
836 * Allows platform initialisation code to define and constrain
837 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
838 * Constraints *must* be set by platform code in order for some
839 * regulator operations to proceed i.e. set_voltage, set_current_limit,
840 * set_mode.
841 */
842static int set_machine_constraints(struct regulator_dev *rdev,
Mark Brownf8c12fe2010-11-29 15:55:17 +0000843 const struct regulation_constraints *constraints)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100844{
845 int ret = 0;
Mark Browne5fda262008-09-09 16:21:20 +0100846 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100847
Mark Brownf8c12fe2010-11-29 15:55:17 +0000848 rdev->constraints = kmemdup(constraints, sizeof(*constraints),
849 GFP_KERNEL);
850 if (!rdev->constraints)
851 return -ENOMEM;
Mark Brownaf5866c2009-10-22 16:31:30 +0100852
Mark Brownf8c12fe2010-11-29 15:55:17 +0000853 ret = machine_constraints_voltage(rdev, rdev->constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100854 if (ret != 0)
855 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800856
Liam Girdwooda5766f12008-10-10 13:22:20 +0100857 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100858 if (constraints->initial_state) {
Mark Brownf8c12fe2010-11-29 15:55:17 +0000859 ret = suspend_prepare(rdev, rdev->constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100860 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800861 rdev_err(rdev, "failed to set suspend state\n");
Mark Browne06f5b42008-09-09 16:21:19 +0100862 rdev->constraints = NULL;
863 goto out;
864 }
865 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100866
Mark Browna3084662009-02-26 19:24:19 +0000867 if (constraints->initial_mode) {
868 if (!ops->set_mode) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800869 rdev_err(rdev, "no set_mode operation\n");
Mark Browna3084662009-02-26 19:24:19 +0000870 ret = -EINVAL;
871 goto out;
872 }
873
Mark Brownf8c12fe2010-11-29 15:55:17 +0000874 ret = ops->set_mode(rdev, rdev->constraints->initial_mode);
Mark Browna3084662009-02-26 19:24:19 +0000875 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800876 rdev_err(rdev, "failed to set initial mode: %d\n", ret);
Mark Browna3084662009-02-26 19:24:19 +0000877 goto out;
878 }
879 }
880
Mark Browncacf90f2009-03-02 16:32:46 +0000881 /* If the constraints say the regulator should be on at this point
882 * and we have control then make sure it is enabled.
883 */
Mark Brownf8c12fe2010-11-29 15:55:17 +0000884 if ((rdev->constraints->always_on || rdev->constraints->boot_on) &&
885 ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100886 ret = ops->enable(rdev);
887 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800888 rdev_err(rdev, "failed to enable\n");
Mark Browne5fda262008-09-09 16:21:20 +0100889 rdev->constraints = NULL;
890 goto out;
891 }
892 }
893
Liam Girdwooda5766f12008-10-10 13:22:20 +0100894 print_constraints(rdev);
895out:
896 return ret;
897}
898
899/**
900 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000901 * @rdev: regulator name
902 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100903 *
904 * Called by platform initialisation code to set the supply regulator for this
905 * regulator. This ensures that a regulators supply will also be enabled by the
906 * core if it's child is enabled.
907 */
908static int set_supply(struct regulator_dev *rdev,
909 struct regulator_dev *supply_rdev)
910{
911 int err;
912
913 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
914 "supply");
915 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -0800916 rdev_err(rdev, "could not add device link %s err %d\n",
917 supply_rdev->dev.kobj.name, err);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100918 goto out;
919 }
920 rdev->supply = supply_rdev;
921 list_add(&rdev->slist, &supply_rdev->supply_list);
922out:
923 return err;
924}
925
926/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800927 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000928 * @rdev: regulator source
929 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100930 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000931 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100932 *
933 * Allows platform initialisation code to map physical regulator
934 * sources to symbolic names for supplies for use by devices. Devices
935 * should use these symbolic names to request regulators, avoiding the
936 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100937 *
938 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100939 */
940static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100941 struct device *consumer_dev, const char *consumer_dev_name,
942 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100943{
944 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100945 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100946
Mark Brown40f92442009-06-17 17:56:39 +0100947 if (consumer_dev && consumer_dev_name)
948 return -EINVAL;
949
950 if (!consumer_dev_name && consumer_dev)
951 consumer_dev_name = dev_name(consumer_dev);
952
Liam Girdwooda5766f12008-10-10 13:22:20 +0100953 if (supply == NULL)
954 return -EINVAL;
955
Mark Brown9ed20992009-07-21 16:00:26 +0100956 if (consumer_dev_name != NULL)
957 has_dev = 1;
958 else
959 has_dev = 0;
960
David Brownell6001e132008-12-31 12:54:19 +0000961 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300962 if (node->dev_name && consumer_dev_name) {
963 if (strcmp(node->dev_name, consumer_dev_name) != 0)
964 continue;
965 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000966 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300967 }
968
David Brownell6001e132008-12-31 12:54:19 +0000969 if (strcmp(node->supply, supply) != 0)
970 continue;
971
972 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
Joe Perches5da84fd2010-11-30 05:53:48 -0800973 dev_name(&node->regulator->dev),
974 node->regulator->desc->name,
975 supply,
976 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000977 return -EBUSY;
978 }
979
Mark Brown9ed20992009-07-21 16:00:26 +0100980 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100981 if (node == NULL)
982 return -ENOMEM;
983
984 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100985 node->supply = supply;
986
Mark Brown9ed20992009-07-21 16:00:26 +0100987 if (has_dev) {
988 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
989 if (node->dev_name == NULL) {
990 kfree(node);
991 return -ENOMEM;
992 }
Mark Brown40f92442009-06-17 17:56:39 +0100993 }
994
Liam Girdwooda5766f12008-10-10 13:22:20 +0100995 list_add(&node->list, &regulator_map_list);
996 return 0;
997}
998
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200999static void unset_regulator_supplies(struct regulator_dev *rdev)
1000{
1001 struct regulator_map *node, *n;
1002
1003 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1004 if (rdev == node->regulator) {
1005 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001006 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001007 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001008 }
1009 }
1010}
1011
Liam Girdwood414c70c2008-04-30 15:59:04 +01001012#define REG_STR_SIZE 32
1013
1014static struct regulator *create_regulator(struct regulator_dev *rdev,
1015 struct device *dev,
1016 const char *supply_name)
1017{
1018 struct regulator *regulator;
1019 char buf[REG_STR_SIZE];
1020 int err, size;
1021
1022 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1023 if (regulator == NULL)
1024 return NULL;
1025
1026 mutex_lock(&rdev->mutex);
1027 regulator->rdev = rdev;
1028 list_add(&regulator->list, &rdev->consumer_list);
1029
1030 if (dev) {
1031 /* create a 'requested_microamps_name' sysfs entry */
1032 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1033 supply_name);
1034 if (size >= REG_STR_SIZE)
1035 goto overflow_err;
1036
1037 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001038 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001039 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1040 if (regulator->dev_attr.attr.name == NULL)
1041 goto attr_name_err;
1042
Liam Girdwood414c70c2008-04-30 15:59:04 +01001043 regulator->dev_attr.attr.mode = 0444;
1044 regulator->dev_attr.show = device_requested_uA_show;
1045 err = device_create_file(dev, &regulator->dev_attr);
1046 if (err < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001047 rdev_warn(rdev, "could not add regulator_dev requested microamps sysfs entry\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001048 goto attr_name_err;
1049 }
1050
1051 /* also add a link to the device sysfs entry */
1052 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1053 dev->kobj.name, supply_name);
1054 if (size >= REG_STR_SIZE)
1055 goto attr_err;
1056
1057 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1058 if (regulator->supply_name == NULL)
1059 goto attr_err;
1060
1061 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1062 buf);
1063 if (err) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001064 rdev_warn(rdev, "could not add device link %s err %d\n",
1065 dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001066 goto link_name_err;
1067 }
1068 }
1069 mutex_unlock(&rdev->mutex);
1070 return regulator;
1071link_name_err:
1072 kfree(regulator->supply_name);
1073attr_err:
1074 device_remove_file(regulator->dev, &regulator->dev_attr);
1075attr_name_err:
1076 kfree(regulator->dev_attr.attr.name);
1077overflow_err:
1078 list_del(&regulator->list);
1079 kfree(regulator);
1080 mutex_unlock(&rdev->mutex);
1081 return NULL;
1082}
1083
Mark Brown31aae2b2009-12-21 12:21:52 +00001084static int _regulator_get_enable_time(struct regulator_dev *rdev)
1085{
1086 if (!rdev->desc->ops->enable_time)
1087 return 0;
1088 return rdev->desc->ops->enable_time(rdev);
1089}
1090
Mark Brown5ffbd132009-07-21 16:00:23 +01001091/* Internal regulator request function */
1092static struct regulator *_regulator_get(struct device *dev, const char *id,
1093 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001094{
1095 struct regulator_dev *rdev;
1096 struct regulator_map *map;
1097 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001098 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001099 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001100
1101 if (id == NULL) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001102 pr_err("get() with no identifier\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001103 return regulator;
1104 }
1105
Mark Brown40f92442009-06-17 17:56:39 +01001106 if (dev)
1107 devname = dev_name(dev);
1108
Liam Girdwood414c70c2008-04-30 15:59:04 +01001109 mutex_lock(&regulator_list_mutex);
1110
1111 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001112 /* If the mapping has a device set up it must match */
1113 if (map->dev_name &&
1114 (!devname || strcmp(map->dev_name, devname)))
1115 continue;
1116
1117 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001118 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001120 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001121 }
Mark Brown34abbd62010-02-12 10:18:08 +00001122
Mark Brown688fe992010-10-05 19:18:32 -07001123 if (board_wants_dummy_regulator) {
1124 rdev = dummy_regulator_rdev;
1125 goto found;
1126 }
1127
Mark Brown34abbd62010-02-12 10:18:08 +00001128#ifdef CONFIG_REGULATOR_DUMMY
1129 if (!devname)
1130 devname = "deviceless";
1131
1132 /* If the board didn't flag that it was fully constrained then
1133 * substitute in a dummy regulator so consumers can continue.
1134 */
1135 if (!has_full_constraints) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001136 pr_warn("%s supply %s not found, using dummy regulator\n",
1137 devname, id);
Mark Brown34abbd62010-02-12 10:18:08 +00001138 rdev = dummy_regulator_rdev;
1139 goto found;
1140 }
1141#endif
1142
Liam Girdwood414c70c2008-04-30 15:59:04 +01001143 mutex_unlock(&regulator_list_mutex);
1144 return regulator;
1145
1146found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001147 if (rdev->exclusive) {
1148 regulator = ERR_PTR(-EPERM);
1149 goto out;
1150 }
1151
1152 if (exclusive && rdev->open_count) {
1153 regulator = ERR_PTR(-EBUSY);
1154 goto out;
1155 }
1156
Liam Girdwooda5766f12008-10-10 13:22:20 +01001157 if (!try_module_get(rdev->owner))
1158 goto out;
1159
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160 regulator = create_regulator(rdev, dev, id);
1161 if (regulator == NULL) {
1162 regulator = ERR_PTR(-ENOMEM);
1163 module_put(rdev->owner);
1164 }
1165
Mark Brown5ffbd132009-07-21 16:00:23 +01001166 rdev->open_count++;
1167 if (exclusive) {
1168 rdev->exclusive = 1;
1169
1170 ret = _regulator_is_enabled(rdev);
1171 if (ret > 0)
1172 rdev->use_count = 1;
1173 else
1174 rdev->use_count = 0;
1175 }
1176
Liam Girdwooda5766f12008-10-10 13:22:20 +01001177out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001178 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001179
Liam Girdwood414c70c2008-04-30 15:59:04 +01001180 return regulator;
1181}
Mark Brown5ffbd132009-07-21 16:00:23 +01001182
1183/**
1184 * regulator_get - lookup and obtain a reference to a regulator.
1185 * @dev: device for regulator "consumer"
1186 * @id: Supply name or regulator ID.
1187 *
1188 * Returns a struct regulator corresponding to the regulator producer,
1189 * or IS_ERR() condition containing errno.
1190 *
1191 * Use of supply names configured via regulator_set_device_supply() is
1192 * strongly encouraged. It is recommended that the supply name used
1193 * should match the name used for the supply and/or the relevant
1194 * device pins in the datasheet.
1195 */
1196struct regulator *regulator_get(struct device *dev, const char *id)
1197{
1198 return _regulator_get(dev, id, 0);
1199}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001200EXPORT_SYMBOL_GPL(regulator_get);
1201
1202/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001203 * regulator_get_exclusive - obtain exclusive access to a regulator.
1204 * @dev: device for regulator "consumer"
1205 * @id: Supply name or regulator ID.
1206 *
1207 * Returns a struct regulator corresponding to the regulator producer,
1208 * or IS_ERR() condition containing errno. Other consumers will be
1209 * unable to obtain this reference is held and the use count for the
1210 * regulator will be initialised to reflect the current state of the
1211 * regulator.
1212 *
1213 * This is intended for use by consumers which cannot tolerate shared
1214 * use of the regulator such as those which need to force the
1215 * regulator off for correct operation of the hardware they are
1216 * controlling.
1217 *
1218 * Use of supply names configured via regulator_set_device_supply() is
1219 * strongly encouraged. It is recommended that the supply name used
1220 * should match the name used for the supply and/or the relevant
1221 * device pins in the datasheet.
1222 */
1223struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1224{
1225 return _regulator_get(dev, id, 1);
1226}
1227EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1228
1229/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001230 * regulator_put - "free" the regulator source
1231 * @regulator: regulator source
1232 *
1233 * Note: drivers must ensure that all regulator_enable calls made on this
1234 * regulator source are balanced by regulator_disable calls prior to calling
1235 * this function.
1236 */
1237void regulator_put(struct regulator *regulator)
1238{
1239 struct regulator_dev *rdev;
1240
1241 if (regulator == NULL || IS_ERR(regulator))
1242 return;
1243
Liam Girdwood414c70c2008-04-30 15:59:04 +01001244 mutex_lock(&regulator_list_mutex);
1245 rdev = regulator->rdev;
1246
1247 /* remove any sysfs entries */
1248 if (regulator->dev) {
1249 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1250 kfree(regulator->supply_name);
1251 device_remove_file(regulator->dev, &regulator->dev_attr);
1252 kfree(regulator->dev_attr.attr.name);
1253 }
1254 list_del(&regulator->list);
1255 kfree(regulator);
1256
Mark Brown5ffbd132009-07-21 16:00:23 +01001257 rdev->open_count--;
1258 rdev->exclusive = 0;
1259
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260 module_put(rdev->owner);
1261 mutex_unlock(&regulator_list_mutex);
1262}
1263EXPORT_SYMBOL_GPL(regulator_put);
1264
Mark Brown9a2372f2009-08-03 18:49:57 +01001265static int _regulator_can_change_status(struct regulator_dev *rdev)
1266{
1267 if (!rdev->constraints)
1268 return 0;
1269
1270 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1271 return 1;
1272 else
1273 return 0;
1274}
1275
Liam Girdwood414c70c2008-04-30 15:59:04 +01001276/* locks held by regulator_enable() */
1277static int _regulator_enable(struct regulator_dev *rdev)
1278{
Mark Brown31aae2b2009-12-21 12:21:52 +00001279 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001280
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001281 if (rdev->use_count == 0) {
1282 /* do we need to enable the supply regulator first */
1283 if (rdev->supply) {
1284 mutex_lock(&rdev->supply->mutex);
1285 ret = _regulator_enable(rdev->supply);
1286 mutex_unlock(&rdev->supply->mutex);
1287 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001288 rdev_err(rdev, "failed to enable: %d\n", ret);
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001289 return ret;
1290 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001291 }
1292 }
1293
1294 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001295 if (rdev->constraints &&
1296 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1297 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001298
Mark Brown9a2372f2009-08-03 18:49:57 +01001299 if (rdev->use_count == 0) {
1300 /* The regulator may on if it's not switchable or left on */
1301 ret = _regulator_is_enabled(rdev);
1302 if (ret == -EINVAL || ret == 0) {
1303 if (!_regulator_can_change_status(rdev))
1304 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001305
Mark Brown31aae2b2009-12-21 12:21:52 +00001306 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001307 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001308
1309 /* Query before enabling in case configuration
1310 * dependant. */
1311 ret = _regulator_get_enable_time(rdev);
1312 if (ret >= 0) {
1313 delay = ret;
1314 } else {
Joe Perches5da84fd2010-11-30 05:53:48 -08001315 rdev_warn(rdev, "enable_time() failed: %d\n",
Daniel Walker1d7372e2010-11-17 15:30:28 -08001316 ret);
Mark Brown31aae2b2009-12-21 12:21:52 +00001317 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001318 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001319
Mark Brown02fa3ec2010-11-10 14:38:30 +00001320 trace_regulator_enable(rdev_get_name(rdev));
1321
Mark Brown31aae2b2009-12-21 12:21:52 +00001322 /* Allow the regulator to ramp; it would be useful
1323 * to extend this for bulk operations so that the
1324 * regulators can ramp together. */
1325 ret = rdev->desc->ops->enable(rdev);
1326 if (ret < 0)
1327 return ret;
1328
Mark Brown02fa3ec2010-11-10 14:38:30 +00001329 trace_regulator_enable_delay(rdev_get_name(rdev));
1330
Axel Line36c1df2010-11-05 21:51:32 +08001331 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001332 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001333 udelay(delay % 1000);
1334 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001335 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001336 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001337
Mark Brown02fa3ec2010-11-10 14:38:30 +00001338 trace_regulator_enable_complete(rdev_get_name(rdev));
1339
Linus Walleija7433cf2009-08-26 12:54:04 +02001340 } else if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001341 rdev_err(rdev, "is_enabled() failed: %d\n", ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001342 return ret;
1343 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001344 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001345 }
1346
Mark Brown9a2372f2009-08-03 18:49:57 +01001347 rdev->use_count++;
1348
1349 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001350}
1351
1352/**
1353 * regulator_enable - enable regulator output
1354 * @regulator: regulator source
1355 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001356 * Request that the regulator be enabled with the regulator output at
1357 * the predefined voltage or current value. Calls to regulator_enable()
1358 * must be balanced with calls to regulator_disable().
1359 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001360 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001361 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001362 */
1363int regulator_enable(struct regulator *regulator)
1364{
David Brownell412aec62008-11-16 11:44:46 -08001365 struct regulator_dev *rdev = regulator->rdev;
1366 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001367
David Brownell412aec62008-11-16 11:44:46 -08001368 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001369 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001370 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001371 return ret;
1372}
1373EXPORT_SYMBOL_GPL(regulator_enable);
1374
1375/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001376static int _regulator_disable(struct regulator_dev *rdev,
1377 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378{
1379 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001380 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001381
David Brownellcd94b502009-03-11 16:43:34 -08001382 if (WARN(rdev->use_count <= 0,
Joe Perches43e7ee32010-12-06 14:05:19 -08001383 "unbalanced disables for %s\n", rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001384 return -EIO;
1385
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001387 if (rdev->use_count == 1 &&
1388 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001389
1390 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001391 if (_regulator_can_change_status(rdev) &&
1392 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001393 trace_regulator_disable(rdev_get_name(rdev));
1394
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 ret = rdev->desc->ops->disable(rdev);
1396 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001397 rdev_err(rdev, "failed to disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001398 return ret;
1399 }
Mark Brown84b68262009-12-01 21:12:27 +00001400
Mark Brown02fa3ec2010-11-10 14:38:30 +00001401 trace_regulator_disable_complete(rdev_get_name(rdev));
1402
Mark Brown84b68262009-12-01 21:12:27 +00001403 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1404 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001405 }
1406
1407 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001408 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001409
1410 rdev->use_count = 0;
1411 } else if (rdev->use_count > 1) {
1412
1413 if (rdev->constraints &&
1414 (rdev->constraints->valid_ops_mask &
1415 REGULATOR_CHANGE_DRMS))
1416 drms_uA_update(rdev);
1417
1418 rdev->use_count--;
1419 }
1420 return ret;
1421}
1422
1423/**
1424 * regulator_disable - disable regulator output
1425 * @regulator: regulator source
1426 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001427 * Disable the regulator output voltage or current. Calls to
1428 * regulator_enable() must be balanced with calls to
1429 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001430 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001431 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001432 * devices have it enabled, the regulator device supports disabling and
1433 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 */
1435int regulator_disable(struct regulator *regulator)
1436{
David Brownell412aec62008-11-16 11:44:46 -08001437 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001438 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001439 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001440
David Brownell412aec62008-11-16 11:44:46 -08001441 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001442 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001443 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001444
1445 /* decrease our supplies ref count and disable if required */
1446 while (supply_rdev != NULL) {
1447 rdev = supply_rdev;
1448
1449 mutex_lock(&rdev->mutex);
1450 _regulator_disable(rdev, &supply_rdev);
1451 mutex_unlock(&rdev->mutex);
1452 }
1453
Liam Girdwood414c70c2008-04-30 15:59:04 +01001454 return ret;
1455}
1456EXPORT_SYMBOL_GPL(regulator_disable);
1457
1458/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001459static int _regulator_force_disable(struct regulator_dev *rdev,
1460 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001461{
1462 int ret = 0;
1463
1464 /* force disable */
1465 if (rdev->desc->ops->disable) {
1466 /* ah well, who wants to live forever... */
1467 ret = rdev->desc->ops->disable(rdev);
1468 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001469 rdev_err(rdev, "failed to force disable\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470 return ret;
1471 }
1472 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001473 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1474 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475 }
1476
1477 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001478 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001479
1480 rdev->use_count = 0;
1481 return ret;
1482}
1483
1484/**
1485 * regulator_force_disable - force disable regulator output
1486 * @regulator: regulator source
1487 *
1488 * Forcibly disable the regulator output voltage or current.
1489 * NOTE: this *will* disable the regulator output even if other consumer
1490 * devices have it enabled. This should be used for situations when device
1491 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1492 */
1493int regulator_force_disable(struct regulator *regulator)
1494{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001495 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496 int ret;
1497
1498 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001499 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001500 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001502
1503 if (supply_rdev)
1504 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1505
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 return ret;
1507}
1508EXPORT_SYMBOL_GPL(regulator_force_disable);
1509
1510static int _regulator_is_enabled(struct regulator_dev *rdev)
1511{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001512 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001513 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001514 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001515
Mark Brown93325462009-08-03 18:49:56 +01001516 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517}
1518
1519/**
1520 * regulator_is_enabled - is the regulator output enabled
1521 * @regulator: regulator source
1522 *
David Brownell412aec62008-11-16 11:44:46 -08001523 * Returns positive if the regulator driver backing the source/client
1524 * has requested that the device be enabled, zero if it hasn't, else a
1525 * negative errno code.
1526 *
1527 * Note that the device backing this regulator handle can have multiple
1528 * users, so it might be enabled even if regulator_enable() was never
1529 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001530 */
1531int regulator_is_enabled(struct regulator *regulator)
1532{
Mark Brown93325462009-08-03 18:49:56 +01001533 int ret;
1534
1535 mutex_lock(&regulator->rdev->mutex);
1536 ret = _regulator_is_enabled(regulator->rdev);
1537 mutex_unlock(&regulator->rdev->mutex);
1538
1539 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540}
1541EXPORT_SYMBOL_GPL(regulator_is_enabled);
1542
1543/**
David Brownell4367cfd2009-02-26 11:48:36 -08001544 * regulator_count_voltages - count regulator_list_voltage() selectors
1545 * @regulator: regulator source
1546 *
1547 * Returns number of selectors, or negative errno. Selectors are
1548 * numbered starting at zero, and typically correspond to bitfields
1549 * in hardware registers.
1550 */
1551int regulator_count_voltages(struct regulator *regulator)
1552{
1553 struct regulator_dev *rdev = regulator->rdev;
1554
1555 return rdev->desc->n_voltages ? : -EINVAL;
1556}
1557EXPORT_SYMBOL_GPL(regulator_count_voltages);
1558
1559/**
1560 * regulator_list_voltage - enumerate supported voltages
1561 * @regulator: regulator source
1562 * @selector: identify voltage to list
1563 * Context: can sleep
1564 *
1565 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001566 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001567 * negative errno.
1568 */
1569int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1570{
1571 struct regulator_dev *rdev = regulator->rdev;
1572 struct regulator_ops *ops = rdev->desc->ops;
1573 int ret;
1574
1575 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1576 return -EINVAL;
1577
1578 mutex_lock(&rdev->mutex);
1579 ret = ops->list_voltage(rdev, selector);
1580 mutex_unlock(&rdev->mutex);
1581
1582 if (ret > 0) {
1583 if (ret < rdev->constraints->min_uV)
1584 ret = 0;
1585 else if (ret > rdev->constraints->max_uV)
1586 ret = 0;
1587 }
1588
1589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(regulator_list_voltage);
1592
1593/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001594 * regulator_is_supported_voltage - check if a voltage range can be supported
1595 *
1596 * @regulator: Regulator to check.
1597 * @min_uV: Minimum required voltage in uV.
1598 * @max_uV: Maximum required voltage in uV.
1599 *
1600 * Returns a boolean or a negative error code.
1601 */
1602int regulator_is_supported_voltage(struct regulator *regulator,
1603 int min_uV, int max_uV)
1604{
1605 int i, voltages, ret;
1606
1607 ret = regulator_count_voltages(regulator);
1608 if (ret < 0)
1609 return ret;
1610 voltages = ret;
1611
1612 for (i = 0; i < voltages; i++) {
1613 ret = regulator_list_voltage(regulator, i);
1614
1615 if (ret >= min_uV && ret <= max_uV)
1616 return 1;
1617 }
1618
1619 return 0;
1620}
1621
1622/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001623 * regulator_set_voltage - set regulator output voltage
1624 * @regulator: regulator source
1625 * @min_uV: Minimum required voltage in uV
1626 * @max_uV: Maximum acceptable voltage in uV
1627 *
1628 * Sets a voltage regulator to the desired output voltage. This can be set
1629 * during any regulator state. IOW, regulator can be disabled or enabled.
1630 *
1631 * If the regulator is enabled then the voltage will change to the new value
1632 * immediately otherwise if the regulator is disabled the regulator will
1633 * output at the new voltage when enabled.
1634 *
1635 * NOTE: If the regulator is shared between several devices then the lowest
1636 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001637 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 * calling this function otherwise this call will fail.
1639 */
1640int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1641{
1642 struct regulator_dev *rdev = regulator->rdev;
1643 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001644 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001645
1646 mutex_lock(&rdev->mutex);
1647
1648 /* sanity check */
1649 if (!rdev->desc->ops->set_voltage) {
1650 ret = -EINVAL;
1651 goto out;
1652 }
1653
1654 /* constraints check */
1655 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1656 if (ret < 0)
1657 goto out;
1658 regulator->min_uV = min_uV;
1659 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001660
Thomas Petazzoni05fda3b1a2010-12-03 11:31:07 +01001661 ret = regulator_check_consumers(rdev, &min_uV, &max_uV);
1662 if (ret < 0)
1663 goto out;
1664
Mark Brown02fa3ec2010-11-10 14:38:30 +00001665 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1666
Mark Brown3a93f2a2010-11-10 14:38:29 +00001667 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1668
1669 if (rdev->desc->ops->list_voltage)
1670 selector = rdev->desc->ops->list_voltage(rdev, selector);
1671 else
1672 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001673
Mark Brown02fa3ec2010-11-10 14:38:30 +00001674 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001677 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001678 mutex_unlock(&rdev->mutex);
1679 return ret;
1680}
1681EXPORT_SYMBOL_GPL(regulator_set_voltage);
1682
1683static int _regulator_get_voltage(struct regulator_dev *rdev)
1684{
1685 /* sanity check */
1686 if (rdev->desc->ops->get_voltage)
1687 return rdev->desc->ops->get_voltage(rdev);
1688 else
1689 return -EINVAL;
1690}
1691
1692/**
1693 * regulator_get_voltage - get regulator output voltage
1694 * @regulator: regulator source
1695 *
1696 * This returns the current regulator voltage in uV.
1697 *
1698 * NOTE: If the regulator is disabled it will return the voltage value. This
1699 * function should not be used to determine regulator state.
1700 */
1701int regulator_get_voltage(struct regulator *regulator)
1702{
1703 int ret;
1704
1705 mutex_lock(&regulator->rdev->mutex);
1706
1707 ret = _regulator_get_voltage(regulator->rdev);
1708
1709 mutex_unlock(&regulator->rdev->mutex);
1710
1711 return ret;
1712}
1713EXPORT_SYMBOL_GPL(regulator_get_voltage);
1714
1715/**
1716 * regulator_set_current_limit - set regulator output current limit
1717 * @regulator: regulator source
1718 * @min_uA: Minimuum supported current in uA
1719 * @max_uA: Maximum supported current in uA
1720 *
1721 * Sets current sink to the desired output current. This can be set during
1722 * any regulator state. IOW, regulator can be disabled or enabled.
1723 *
1724 * If the regulator is enabled then the current will change to the new value
1725 * immediately otherwise if the regulator is disabled the regulator will
1726 * output at the new current when enabled.
1727 *
1728 * NOTE: Regulator system constraints must be set for this regulator before
1729 * calling this function otherwise this call will fail.
1730 */
1731int regulator_set_current_limit(struct regulator *regulator,
1732 int min_uA, int max_uA)
1733{
1734 struct regulator_dev *rdev = regulator->rdev;
1735 int ret;
1736
1737 mutex_lock(&rdev->mutex);
1738
1739 /* sanity check */
1740 if (!rdev->desc->ops->set_current_limit) {
1741 ret = -EINVAL;
1742 goto out;
1743 }
1744
1745 /* constraints check */
1746 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1747 if (ret < 0)
1748 goto out;
1749
1750 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1751out:
1752 mutex_unlock(&rdev->mutex);
1753 return ret;
1754}
1755EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1756
1757static int _regulator_get_current_limit(struct regulator_dev *rdev)
1758{
1759 int ret;
1760
1761 mutex_lock(&rdev->mutex);
1762
1763 /* sanity check */
1764 if (!rdev->desc->ops->get_current_limit) {
1765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 ret = rdev->desc->ops->get_current_limit(rdev);
1770out:
1771 mutex_unlock(&rdev->mutex);
1772 return ret;
1773}
1774
1775/**
1776 * regulator_get_current_limit - get regulator output current
1777 * @regulator: regulator source
1778 *
1779 * This returns the current supplied by the specified current sink in uA.
1780 *
1781 * NOTE: If the regulator is disabled it will return the current value. This
1782 * function should not be used to determine regulator state.
1783 */
1784int regulator_get_current_limit(struct regulator *regulator)
1785{
1786 return _regulator_get_current_limit(regulator->rdev);
1787}
1788EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1789
1790/**
1791 * regulator_set_mode - set regulator operating mode
1792 * @regulator: regulator source
1793 * @mode: operating mode - one of the REGULATOR_MODE constants
1794 *
1795 * Set regulator operating mode to increase regulator efficiency or improve
1796 * regulation performance.
1797 *
1798 * NOTE: Regulator system constraints must be set for this regulator before
1799 * calling this function otherwise this call will fail.
1800 */
1801int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1802{
1803 struct regulator_dev *rdev = regulator->rdev;
1804 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301805 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001806
1807 mutex_lock(&rdev->mutex);
1808
1809 /* sanity check */
1810 if (!rdev->desc->ops->set_mode) {
1811 ret = -EINVAL;
1812 goto out;
1813 }
1814
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301815 /* return if the same mode is requested */
1816 if (rdev->desc->ops->get_mode) {
1817 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1818 if (regulator_curr_mode == mode) {
1819 ret = 0;
1820 goto out;
1821 }
1822 }
1823
Liam Girdwood414c70c2008-04-30 15:59:04 +01001824 /* constraints check */
1825 ret = regulator_check_mode(rdev, mode);
1826 if (ret < 0)
1827 goto out;
1828
1829 ret = rdev->desc->ops->set_mode(rdev, mode);
1830out:
1831 mutex_unlock(&rdev->mutex);
1832 return ret;
1833}
1834EXPORT_SYMBOL_GPL(regulator_set_mode);
1835
1836static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1837{
1838 int ret;
1839
1840 mutex_lock(&rdev->mutex);
1841
1842 /* sanity check */
1843 if (!rdev->desc->ops->get_mode) {
1844 ret = -EINVAL;
1845 goto out;
1846 }
1847
1848 ret = rdev->desc->ops->get_mode(rdev);
1849out:
1850 mutex_unlock(&rdev->mutex);
1851 return ret;
1852}
1853
1854/**
1855 * regulator_get_mode - get regulator operating mode
1856 * @regulator: regulator source
1857 *
1858 * Get the current regulator operating mode.
1859 */
1860unsigned int regulator_get_mode(struct regulator *regulator)
1861{
1862 return _regulator_get_mode(regulator->rdev);
1863}
1864EXPORT_SYMBOL_GPL(regulator_get_mode);
1865
1866/**
1867 * regulator_set_optimum_mode - set regulator optimum operating mode
1868 * @regulator: regulator source
1869 * @uA_load: load current
1870 *
1871 * Notifies the regulator core of a new device load. This is then used by
1872 * DRMS (if enabled by constraints) to set the most efficient regulator
1873 * operating mode for the new regulator loading.
1874 *
1875 * Consumer devices notify their supply regulator of the maximum power
1876 * they will require (can be taken from device datasheet in the power
1877 * consumption tables) when they change operational status and hence power
1878 * state. Examples of operational state changes that can affect power
1879 * consumption are :-
1880 *
1881 * o Device is opened / closed.
1882 * o Device I/O is about to begin or has just finished.
1883 * o Device is idling in between work.
1884 *
1885 * This information is also exported via sysfs to userspace.
1886 *
1887 * DRMS will sum the total requested load on the regulator and change
1888 * to the most efficient operating mode if platform constraints allow.
1889 *
1890 * Returns the new regulator mode or error.
1891 */
1892int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1893{
1894 struct regulator_dev *rdev = regulator->rdev;
1895 struct regulator *consumer;
1896 int ret, output_uV, input_uV, total_uA_load = 0;
1897 unsigned int mode;
1898
1899 mutex_lock(&rdev->mutex);
1900
1901 regulator->uA_load = uA_load;
1902 ret = regulator_check_drms(rdev);
1903 if (ret < 0)
1904 goto out;
1905 ret = -EINVAL;
1906
1907 /* sanity check */
1908 if (!rdev->desc->ops->get_optimum_mode)
1909 goto out;
1910
1911 /* get output voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00001912 output_uV = _regulator_get_voltage(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001913 if (output_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001914 rdev_err(rdev, "invalid output voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001915 goto out;
1916 }
1917
1918 /* get input voltage */
Mark Brown1bf5a1f2010-12-10 17:28:06 +00001919 input_uV = 0;
1920 if (rdev->supply)
1921 input_uV = _regulator_get_voltage(rdev->supply);
1922 if (input_uV <= 0)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001923 input_uV = rdev->constraints->input_uV;
1924 if (input_uV <= 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001925 rdev_err(rdev, "invalid input voltage found\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01001926 goto out;
1927 }
1928
1929 /* calc total requested load for this regulator */
1930 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001931 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001932
1933 mode = rdev->desc->ops->get_optimum_mode(rdev,
1934 input_uV, output_uV,
1935 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001936 ret = regulator_check_mode(rdev, mode);
1937 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001938 rdev_err(rdev, "failed to get optimum mode @ %d uA %d -> %d uV\n",
1939 total_uA_load, input_uV, output_uV);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001940 goto out;
1941 }
1942
1943 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001944 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08001945 rdev_err(rdev, "failed to set optimum mode %x\n", mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001946 goto out;
1947 }
1948 ret = mode;
1949out:
1950 mutex_unlock(&rdev->mutex);
1951 return ret;
1952}
1953EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1954
1955/**
1956 * regulator_register_notifier - register regulator event notifier
1957 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001958 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001959 *
1960 * Register notifier block to receive regulator events.
1961 */
1962int regulator_register_notifier(struct regulator *regulator,
1963 struct notifier_block *nb)
1964{
1965 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1966 nb);
1967}
1968EXPORT_SYMBOL_GPL(regulator_register_notifier);
1969
1970/**
1971 * regulator_unregister_notifier - unregister regulator event notifier
1972 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001973 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001974 *
1975 * Unregister regulator event notifier block.
1976 */
1977int regulator_unregister_notifier(struct regulator *regulator,
1978 struct notifier_block *nb)
1979{
1980 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1981 nb);
1982}
1983EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1984
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001985/* notify regulator consumers and downstream regulator consumers.
1986 * Note mutex must be held by caller.
1987 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001988static void _notifier_call_chain(struct regulator_dev *rdev,
1989 unsigned long event, void *data)
1990{
1991 struct regulator_dev *_rdev;
1992
1993 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001994 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001995
1996 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001997 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001998 mutex_lock(&_rdev->mutex);
1999 _notifier_call_chain(_rdev, event, data);
2000 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002001 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002002}
2003
2004/**
2005 * regulator_bulk_get - get multiple regulator consumers
2006 *
2007 * @dev: Device to supply
2008 * @num_consumers: Number of consumers to register
2009 * @consumers: Configuration of consumers; clients are stored here.
2010 *
2011 * @return 0 on success, an errno on failure.
2012 *
2013 * This helper function allows drivers to get several regulator
2014 * consumers in one operation. If any of the regulators cannot be
2015 * acquired then any regulators that were allocated will be freed
2016 * before returning to the caller.
2017 */
2018int regulator_bulk_get(struct device *dev, int num_consumers,
2019 struct regulator_bulk_data *consumers)
2020{
2021 int i;
2022 int ret;
2023
2024 for (i = 0; i < num_consumers; i++)
2025 consumers[i].consumer = NULL;
2026
2027 for (i = 0; i < num_consumers; i++) {
2028 consumers[i].consumer = regulator_get(dev,
2029 consumers[i].supply);
2030 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002031 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002032 dev_err(dev, "Failed to get supply '%s': %d\n",
2033 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002034 consumers[i].consumer = NULL;
2035 goto err;
2036 }
2037 }
2038
2039 return 0;
2040
2041err:
2042 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2043 regulator_put(consumers[i].consumer);
2044
2045 return ret;
2046}
2047EXPORT_SYMBOL_GPL(regulator_bulk_get);
2048
2049/**
2050 * regulator_bulk_enable - enable multiple regulator consumers
2051 *
2052 * @num_consumers: Number of consumers
2053 * @consumers: Consumer data; clients are stored here.
2054 * @return 0 on success, an errno on failure
2055 *
2056 * This convenience API allows consumers to enable multiple regulator
2057 * clients in a single API call. If any consumers cannot be enabled
2058 * then any others that were enabled will be disabled again prior to
2059 * return.
2060 */
2061int regulator_bulk_enable(int num_consumers,
2062 struct regulator_bulk_data *consumers)
2063{
2064 int i;
2065 int ret;
2066
2067 for (i = 0; i < num_consumers; i++) {
2068 ret = regulator_enable(consumers[i].consumer);
2069 if (ret != 0)
2070 goto err;
2071 }
2072
2073 return 0;
2074
2075err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002076 pr_err("Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002077 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002078 regulator_disable(consumers[i].consumer);
2079
2080 return ret;
2081}
2082EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2083
2084/**
2085 * regulator_bulk_disable - disable multiple regulator consumers
2086 *
2087 * @num_consumers: Number of consumers
2088 * @consumers: Consumer data; clients are stored here.
2089 * @return 0 on success, an errno on failure
2090 *
2091 * This convenience API allows consumers to disable multiple regulator
2092 * clients in a single API call. If any consumers cannot be enabled
2093 * then any others that were disabled will be disabled again prior to
2094 * return.
2095 */
2096int regulator_bulk_disable(int num_consumers,
2097 struct regulator_bulk_data *consumers)
2098{
2099 int i;
2100 int ret;
2101
2102 for (i = 0; i < num_consumers; i++) {
2103 ret = regulator_disable(consumers[i].consumer);
2104 if (ret != 0)
2105 goto err;
2106 }
2107
2108 return 0;
2109
2110err:
Joe Perches5da84fd2010-11-30 05:53:48 -08002111 pr_err("Failed to disable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002112 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002113 regulator_enable(consumers[i].consumer);
2114
2115 return ret;
2116}
2117EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2118
2119/**
2120 * regulator_bulk_free - free multiple regulator consumers
2121 *
2122 * @num_consumers: Number of consumers
2123 * @consumers: Consumer data; clients are stored here.
2124 *
2125 * This convenience API allows consumers to free multiple regulator
2126 * clients in a single API call.
2127 */
2128void regulator_bulk_free(int num_consumers,
2129 struct regulator_bulk_data *consumers)
2130{
2131 int i;
2132
2133 for (i = 0; i < num_consumers; i++) {
2134 regulator_put(consumers[i].consumer);
2135 consumers[i].consumer = NULL;
2136 }
2137}
2138EXPORT_SYMBOL_GPL(regulator_bulk_free);
2139
2140/**
2141 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002142 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002144 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145 *
2146 * Called by regulator drivers to notify clients a regulator event has
2147 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002148 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149 */
2150int regulator_notifier_call_chain(struct regulator_dev *rdev,
2151 unsigned long event, void *data)
2152{
2153 _notifier_call_chain(rdev, event, data);
2154 return NOTIFY_DONE;
2155
2156}
2157EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2158
Mark Brownbe721972009-08-04 20:09:52 +02002159/**
2160 * regulator_mode_to_status - convert a regulator mode into a status
2161 *
2162 * @mode: Mode to convert
2163 *
2164 * Convert a regulator mode into a status.
2165 */
2166int regulator_mode_to_status(unsigned int mode)
2167{
2168 switch (mode) {
2169 case REGULATOR_MODE_FAST:
2170 return REGULATOR_STATUS_FAST;
2171 case REGULATOR_MODE_NORMAL:
2172 return REGULATOR_STATUS_NORMAL;
2173 case REGULATOR_MODE_IDLE:
2174 return REGULATOR_STATUS_IDLE;
2175 case REGULATOR_STATUS_STANDBY:
2176 return REGULATOR_STATUS_STANDBY;
2177 default:
2178 return 0;
2179 }
2180}
2181EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2182
David Brownell7ad68e22008-11-11 17:39:02 -08002183/*
2184 * To avoid cluttering sysfs (and memory) with useless state, only
2185 * create attributes that can be meaningfully displayed.
2186 */
2187static int add_regulator_attributes(struct regulator_dev *rdev)
2188{
2189 struct device *dev = &rdev->dev;
2190 struct regulator_ops *ops = rdev->desc->ops;
2191 int status = 0;
2192
2193 /* some attributes need specific methods to be displayed */
2194 if (ops->get_voltage) {
2195 status = device_create_file(dev, &dev_attr_microvolts);
2196 if (status < 0)
2197 return status;
2198 }
2199 if (ops->get_current_limit) {
2200 status = device_create_file(dev, &dev_attr_microamps);
2201 if (status < 0)
2202 return status;
2203 }
2204 if (ops->get_mode) {
2205 status = device_create_file(dev, &dev_attr_opmode);
2206 if (status < 0)
2207 return status;
2208 }
2209 if (ops->is_enabled) {
2210 status = device_create_file(dev, &dev_attr_state);
2211 if (status < 0)
2212 return status;
2213 }
David Brownell853116a2009-01-14 23:03:17 -08002214 if (ops->get_status) {
2215 status = device_create_file(dev, &dev_attr_status);
2216 if (status < 0)
2217 return status;
2218 }
David Brownell7ad68e22008-11-11 17:39:02 -08002219
2220 /* some attributes are type-specific */
2221 if (rdev->desc->type == REGULATOR_CURRENT) {
2222 status = device_create_file(dev, &dev_attr_requested_microamps);
2223 if (status < 0)
2224 return status;
2225 }
2226
2227 /* all the other attributes exist to support constraints;
2228 * don't show them if there are no constraints, or if the
2229 * relevant supporting methods are missing.
2230 */
2231 if (!rdev->constraints)
2232 return status;
2233
2234 /* constraints need specific supporting methods */
2235 if (ops->set_voltage) {
2236 status = device_create_file(dev, &dev_attr_min_microvolts);
2237 if (status < 0)
2238 return status;
2239 status = device_create_file(dev, &dev_attr_max_microvolts);
2240 if (status < 0)
2241 return status;
2242 }
2243 if (ops->set_current_limit) {
2244 status = device_create_file(dev, &dev_attr_min_microamps);
2245 if (status < 0)
2246 return status;
2247 status = device_create_file(dev, &dev_attr_max_microamps);
2248 if (status < 0)
2249 return status;
2250 }
2251
2252 /* suspend mode constraints need multiple supporting methods */
2253 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2254 return status;
2255
2256 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2257 if (status < 0)
2258 return status;
2259 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2260 if (status < 0)
2261 return status;
2262 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2263 if (status < 0)
2264 return status;
2265
2266 if (ops->set_suspend_voltage) {
2267 status = device_create_file(dev,
2268 &dev_attr_suspend_standby_microvolts);
2269 if (status < 0)
2270 return status;
2271 status = device_create_file(dev,
2272 &dev_attr_suspend_mem_microvolts);
2273 if (status < 0)
2274 return status;
2275 status = device_create_file(dev,
2276 &dev_attr_suspend_disk_microvolts);
2277 if (status < 0)
2278 return status;
2279 }
2280
2281 if (ops->set_suspend_mode) {
2282 status = device_create_file(dev,
2283 &dev_attr_suspend_standby_mode);
2284 if (status < 0)
2285 return status;
2286 status = device_create_file(dev,
2287 &dev_attr_suspend_mem_mode);
2288 if (status < 0)
2289 return status;
2290 status = device_create_file(dev,
2291 &dev_attr_suspend_disk_mode);
2292 if (status < 0)
2293 return status;
2294 }
2295
2296 return status;
2297}
2298
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299/**
2300 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002301 * @regulator_desc: regulator to register
2302 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002303 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002304 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 *
2306 * Called by regulator drivers to register a regulator.
2307 * Returns 0 on success.
2308 */
2309struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brownf8c12fe2010-11-29 15:55:17 +00002310 struct device *dev, const struct regulator_init_data *init_data,
Mark Brown05271002009-01-19 13:37:02 +00002311 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312{
2313 static atomic_t regulator_no = ATOMIC_INIT(0);
2314 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002315 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002316
2317 if (regulator_desc == NULL)
2318 return ERR_PTR(-EINVAL);
2319
2320 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2321 return ERR_PTR(-EINVAL);
2322
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002323 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2324 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 return ERR_PTR(-EINVAL);
2326
Mark Brown46fabe1e2008-09-09 16:21:18 +01002327 if (!init_data)
2328 return ERR_PTR(-EINVAL);
2329
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2331 if (rdev == NULL)
2332 return ERR_PTR(-ENOMEM);
2333
2334 mutex_lock(&regulator_list_mutex);
2335
2336 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002337 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002338 rdev->owner = regulator_desc->owner;
2339 rdev->desc = regulator_desc;
2340 INIT_LIST_HEAD(&rdev->consumer_list);
2341 INIT_LIST_HEAD(&rdev->supply_list);
2342 INIT_LIST_HEAD(&rdev->list);
2343 INIT_LIST_HEAD(&rdev->slist);
2344 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2345
Liam Girdwooda5766f12008-10-10 13:22:20 +01002346 /* preform any regulator specific init */
2347 if (init_data->regulator_init) {
2348 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002349 if (ret < 0)
2350 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002351 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352
Liam Girdwooda5766f12008-10-10 13:22:20 +01002353 /* register with sysfs */
2354 rdev->dev.class = &regulator_class;
2355 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002356 dev_set_name(&rdev->dev, "regulator.%d",
2357 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002358 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002359 if (ret != 0) {
2360 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002361 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002362 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002363
2364 dev_set_drvdata(&rdev->dev, rdev);
2365
Mike Rapoport74f544c2008-11-25 14:53:53 +02002366 /* set regulator constraints */
2367 ret = set_machine_constraints(rdev, &init_data->constraints);
2368 if (ret < 0)
2369 goto scrub;
2370
David Brownell7ad68e22008-11-11 17:39:02 -08002371 /* add attributes supported by this regulator */
2372 ret = add_regulator_attributes(rdev);
2373 if (ret < 0)
2374 goto scrub;
2375
Liam Girdwooda5766f12008-10-10 13:22:20 +01002376 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002377 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2378 dev_err(dev,
2379 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002380 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002381 goto scrub;
2382 }
2383
2384 if (init_data->supply_regulator) {
2385 struct regulator_dev *r;
2386 int found = 0;
2387
2388 list_for_each_entry(r, &regulator_list, list) {
2389 if (strcmp(rdev_get_name(r),
2390 init_data->supply_regulator) == 0) {
2391 found = 1;
2392 break;
2393 }
2394 }
2395
2396 if (!found) {
2397 dev_err(dev, "Failed to find supply %s\n",
2398 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002399 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002400 goto scrub;
2401 }
2402
2403 ret = set_supply(rdev, r);
2404 if (ret < 0)
2405 goto scrub;
2406 }
2407
Liam Girdwooda5766f12008-10-10 13:22:20 +01002408 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002409 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002410 ret = set_supply(rdev,
2411 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002412 if (ret < 0)
2413 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002414 }
2415
2416 /* add consumers devices */
2417 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2418 ret = set_consumer_device_supply(rdev,
2419 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002420 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002421 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002422 if (ret < 0)
2423 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002424 }
2425
2426 list_add(&rdev->list, &regulator_list);
2427out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002428 mutex_unlock(&regulator_list_mutex);
2429 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002430
Jani Nikulad4033b52010-04-29 10:55:11 +03002431unset_supplies:
2432 unset_regulator_supplies(rdev);
2433
David Brownell4fca9542008-11-11 17:38:53 -08002434scrub:
2435 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002436 /* device core frees rdev */
2437 rdev = ERR_PTR(ret);
2438 goto out;
2439
David Brownell4fca9542008-11-11 17:38:53 -08002440clean:
2441 kfree(rdev);
2442 rdev = ERR_PTR(ret);
2443 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002444}
2445EXPORT_SYMBOL_GPL(regulator_register);
2446
2447/**
2448 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002449 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002450 *
2451 * Called by regulator drivers to unregister a regulator.
2452 */
2453void regulator_unregister(struct regulator_dev *rdev)
2454{
2455 if (rdev == NULL)
2456 return;
2457
2458 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002459 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002460 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002461 list_del(&rdev->list);
2462 if (rdev->supply)
2463 sysfs_remove_link(&rdev->dev.kobj, "supply");
2464 device_unregister(&rdev->dev);
Mark Brownf8c12fe2010-11-29 15:55:17 +00002465 kfree(rdev->constraints);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002466 mutex_unlock(&regulator_list_mutex);
2467}
2468EXPORT_SYMBOL_GPL(regulator_unregister);
2469
2470/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002471 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002472 * @state: system suspend state
2473 *
2474 * Configure each regulator with it's suspend operating parameters for state.
2475 * This will usually be called by machine suspend code prior to supending.
2476 */
2477int regulator_suspend_prepare(suspend_state_t state)
2478{
2479 struct regulator_dev *rdev;
2480 int ret = 0;
2481
2482 /* ON is handled by regulator active state */
2483 if (state == PM_SUSPEND_ON)
2484 return -EINVAL;
2485
2486 mutex_lock(&regulator_list_mutex);
2487 list_for_each_entry(rdev, &regulator_list, list) {
2488
2489 mutex_lock(&rdev->mutex);
2490 ret = suspend_prepare(rdev, state);
2491 mutex_unlock(&rdev->mutex);
2492
2493 if (ret < 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002494 rdev_err(rdev, "failed to prepare\n");
Liam Girdwood414c70c2008-04-30 15:59:04 +01002495 goto out;
2496 }
2497 }
2498out:
2499 mutex_unlock(&regulator_list_mutex);
2500 return ret;
2501}
2502EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2503
2504/**
Mark Brownca725562009-03-16 19:36:34 +00002505 * regulator_has_full_constraints - the system has fully specified constraints
2506 *
2507 * Calling this function will cause the regulator API to disable all
2508 * regulators which have a zero use count and don't have an always_on
2509 * constraint in a late_initcall.
2510 *
2511 * The intention is that this will become the default behaviour in a
2512 * future kernel release so users are encouraged to use this facility
2513 * now.
2514 */
2515void regulator_has_full_constraints(void)
2516{
2517 has_full_constraints = 1;
2518}
2519EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2520
2521/**
Mark Brown688fe992010-10-05 19:18:32 -07002522 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2523 *
2524 * Calling this function will cause the regulator API to provide a
2525 * dummy regulator to consumers if no physical regulator is found,
2526 * allowing most consumers to proceed as though a regulator were
2527 * configured. This allows systems such as those with software
2528 * controllable regulators for the CPU core only to be brought up more
2529 * readily.
2530 */
2531void regulator_use_dummy_regulator(void)
2532{
2533 board_wants_dummy_regulator = true;
2534}
2535EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2536
2537/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002539 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002540 *
2541 * Get rdev regulator driver private data. This call can be used in the
2542 * regulator driver context.
2543 */
2544void *rdev_get_drvdata(struct regulator_dev *rdev)
2545{
2546 return rdev->reg_data;
2547}
2548EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2549
2550/**
2551 * regulator_get_drvdata - get regulator driver data
2552 * @regulator: regulator
2553 *
2554 * Get regulator driver private data. This call can be used in the consumer
2555 * driver context when non API regulator specific functions need to be called.
2556 */
2557void *regulator_get_drvdata(struct regulator *regulator)
2558{
2559 return regulator->rdev->reg_data;
2560}
2561EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2562
2563/**
2564 * regulator_set_drvdata - set regulator driver data
2565 * @regulator: regulator
2566 * @data: data
2567 */
2568void regulator_set_drvdata(struct regulator *regulator, void *data)
2569{
2570 regulator->rdev->reg_data = data;
2571}
2572EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2573
2574/**
2575 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002576 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002577 */
2578int rdev_get_id(struct regulator_dev *rdev)
2579{
2580 return rdev->desc->id;
2581}
2582EXPORT_SYMBOL_GPL(rdev_get_id);
2583
Liam Girdwooda5766f12008-10-10 13:22:20 +01002584struct device *rdev_get_dev(struct regulator_dev *rdev)
2585{
2586 return &rdev->dev;
2587}
2588EXPORT_SYMBOL_GPL(rdev_get_dev);
2589
2590void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2591{
2592 return reg_init_data->driver_data;
2593}
2594EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2595
Liam Girdwood414c70c2008-04-30 15:59:04 +01002596static int __init regulator_init(void)
2597{
Mark Brown34abbd62010-02-12 10:18:08 +00002598 int ret;
2599
Mark Brown34abbd62010-02-12 10:18:08 +00002600 ret = class_register(&regulator_class);
2601
2602 regulator_dummy_init();
2603
2604 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002605}
2606
2607/* init early to allow our consumers to complete system booting */
2608core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002609
2610static int __init regulator_init_complete(void)
2611{
2612 struct regulator_dev *rdev;
2613 struct regulator_ops *ops;
2614 struct regulation_constraints *c;
2615 int enabled, ret;
Mark Brownca725562009-03-16 19:36:34 +00002616
2617 mutex_lock(&regulator_list_mutex);
2618
2619 /* If we have a full configuration then disable any regulators
2620 * which are not in use or always_on. This will become the
2621 * default behaviour in the future.
2622 */
2623 list_for_each_entry(rdev, &regulator_list, list) {
2624 ops = rdev->desc->ops;
2625 c = rdev->constraints;
2626
Mark Brownf25e0b42009-08-03 18:49:55 +01002627 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002628 continue;
2629
2630 mutex_lock(&rdev->mutex);
2631
2632 if (rdev->use_count)
2633 goto unlock;
2634
2635 /* If we can't read the status assume it's on. */
2636 if (ops->is_enabled)
2637 enabled = ops->is_enabled(rdev);
2638 else
2639 enabled = 1;
2640
2641 if (!enabled)
2642 goto unlock;
2643
2644 if (has_full_constraints) {
2645 /* We log since this may kill the system if it
2646 * goes wrong. */
Joe Perches5da84fd2010-11-30 05:53:48 -08002647 rdev_info(rdev, "disabling\n");
Mark Brownca725562009-03-16 19:36:34 +00002648 ret = ops->disable(rdev);
2649 if (ret != 0) {
Joe Perches5da84fd2010-11-30 05:53:48 -08002650 rdev_err(rdev, "couldn't disable: %d\n", ret);
Mark Brownca725562009-03-16 19:36:34 +00002651 }
2652 } else {
2653 /* The intention is that in future we will
2654 * assume that full constraints are provided
2655 * so warn even if we aren't going to do
2656 * anything here.
2657 */
Joe Perches5da84fd2010-11-30 05:53:48 -08002658 rdev_warn(rdev, "incomplete constraints, leaving on\n");
Mark Brownca725562009-03-16 19:36:34 +00002659 }
2660
2661unlock:
2662 mutex_unlock(&rdev->mutex);
2663 }
2664
2665 mutex_unlock(&regulator_list_mutex);
2666
2667 return 0;
2668}
2669late_initcall(regulator_init_complete);