blob: ba521f0f0fac634753cc79afe84c27c451a6bda7 [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
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010020#include <linux/err.h>
21#include <linux/mutex.h>
22#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000023#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/regulator/consumer.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27
Mark Brown34abbd62010-02-12 10:18:08 +000028#include "dummy.h"
29
Liam Girdwood414c70c2008-04-30 15:59:04 +010030#define REGULATOR_VERSION "0.5"
31
32static DEFINE_MUTEX(regulator_list_mutex);
33static LIST_HEAD(regulator_list);
34static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000035static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070036static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010037
Mark Brown8dc53902008-12-31 12:52:40 +000038/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010039 * struct regulator_map
40 *
41 * Used to provide symbolic supply names to devices.
42 */
43struct regulator_map {
44 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010045 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010046 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010047 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010048};
49
Liam Girdwood414c70c2008-04-30 15:59:04 +010050/*
51 * struct regulator
52 *
53 * One for each consumer device.
54 */
55struct regulator {
56 struct device *dev;
57 struct list_head list;
58 int uA_load;
59 int min_uV;
60 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010061 char *supply_name;
62 struct device_attribute dev_attr;
63 struct regulator_dev *rdev;
64};
65
66static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050067static int _regulator_disable(struct regulator_dev *rdev,
68 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010069static int _regulator_get_voltage(struct regulator_dev *rdev);
70static int _regulator_get_current_limit(struct regulator_dev *rdev);
71static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
72static void _notifier_call_chain(struct regulator_dev *rdev,
73 unsigned long event, void *data);
74
Mark Brown1083c392009-10-22 16:31:32 +010075static const char *rdev_get_name(struct regulator_dev *rdev)
76{
77 if (rdev->constraints && rdev->constraints->name)
78 return rdev->constraints->name;
79 else if (rdev->desc->name)
80 return rdev->desc->name;
81 else
82 return "";
83}
84
Liam Girdwood414c70c2008-04-30 15:59:04 +010085/* gets the regulator for a given consumer device */
86static struct regulator *get_device_regulator(struct device *dev)
87{
88 struct regulator *regulator = NULL;
89 struct regulator_dev *rdev;
90
91 mutex_lock(&regulator_list_mutex);
92 list_for_each_entry(rdev, &regulator_list, list) {
93 mutex_lock(&rdev->mutex);
94 list_for_each_entry(regulator, &rdev->consumer_list, list) {
95 if (regulator->dev == dev) {
96 mutex_unlock(&rdev->mutex);
97 mutex_unlock(&regulator_list_mutex);
98 return regulator;
99 }
100 }
101 mutex_unlock(&rdev->mutex);
102 }
103 mutex_unlock(&regulator_list_mutex);
104 return NULL;
105}
106
107/* Platform voltage constraint check */
108static int regulator_check_voltage(struct regulator_dev *rdev,
109 int *min_uV, int *max_uV)
110{
111 BUG_ON(*min_uV > *max_uV);
112
113 if (!rdev->constraints) {
114 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100115 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100116 return -ENODEV;
117 }
118 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
119 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100120 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100121 return -EPERM;
122 }
123
124 if (*max_uV > rdev->constraints->max_uV)
125 *max_uV = rdev->constraints->max_uV;
126 if (*min_uV < rdev->constraints->min_uV)
127 *min_uV = rdev->constraints->min_uV;
128
129 if (*min_uV > *max_uV)
130 return -EINVAL;
131
132 return 0;
133}
134
135/* current constraint check */
136static int regulator_check_current_limit(struct regulator_dev *rdev,
137 int *min_uA, int *max_uA)
138{
139 BUG_ON(*min_uA > *max_uA);
140
141 if (!rdev->constraints) {
142 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100143 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100144 return -ENODEV;
145 }
146 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
147 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100148 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100149 return -EPERM;
150 }
151
152 if (*max_uA > rdev->constraints->max_uA)
153 *max_uA = rdev->constraints->max_uA;
154 if (*min_uA < rdev->constraints->min_uA)
155 *min_uA = rdev->constraints->min_uA;
156
157 if (*min_uA > *max_uA)
158 return -EINVAL;
159
160 return 0;
161}
162
163/* operating mode constraint check */
164static int regulator_check_mode(struct regulator_dev *rdev, int mode)
165{
David Brownelle5735202008-11-16 11:46:56 -0800166 switch (mode) {
167 case REGULATOR_MODE_FAST:
168 case REGULATOR_MODE_NORMAL:
169 case REGULATOR_MODE_IDLE:
170 case REGULATOR_MODE_STANDBY:
171 break;
172 default:
173 return -EINVAL;
174 }
175
Liam Girdwood414c70c2008-04-30 15:59:04 +0100176 if (!rdev->constraints) {
177 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100178 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100179 return -ENODEV;
180 }
181 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
182 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100183 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 return -EPERM;
185 }
186 if (!(rdev->constraints->valid_modes_mask & mode)) {
187 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100188 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100189 return -EINVAL;
190 }
191 return 0;
192}
193
194/* dynamic regulator mode switching constraint check */
195static int regulator_check_drms(struct regulator_dev *rdev)
196{
197 if (!rdev->constraints) {
198 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100199 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100200 return -ENODEV;
201 }
202 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
203 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100204 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100205 return -EPERM;
206 }
207 return 0;
208}
209
210static ssize_t device_requested_uA_show(struct device *dev,
211 struct device_attribute *attr, char *buf)
212{
213 struct regulator *regulator;
214
215 regulator = get_device_regulator(dev);
216 if (regulator == NULL)
217 return 0;
218
219 return sprintf(buf, "%d\n", regulator->uA_load);
220}
221
222static ssize_t regulator_uV_show(struct device *dev,
223 struct device_attribute *attr, char *buf)
224{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100225 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100226 ssize_t ret;
227
228 mutex_lock(&rdev->mutex);
229 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
230 mutex_unlock(&rdev->mutex);
231
232 return ret;
233}
David Brownell7ad68e22008-11-11 17:39:02 -0800234static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100235
236static ssize_t regulator_uA_show(struct device *dev,
237 struct device_attribute *attr, char *buf)
238{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100239 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240
241 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
242}
David Brownell7ad68e22008-11-11 17:39:02 -0800243static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100244
Mark Brownbc558a62008-10-10 15:33:20 +0100245static ssize_t regulator_name_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
248 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100249
Mark Brown1083c392009-10-22 16:31:32 +0100250 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100251}
252
David Brownell4fca9542008-11-11 17:38:53 -0800253static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100254{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100255 switch (mode) {
256 case REGULATOR_MODE_FAST:
257 return sprintf(buf, "fast\n");
258 case REGULATOR_MODE_NORMAL:
259 return sprintf(buf, "normal\n");
260 case REGULATOR_MODE_IDLE:
261 return sprintf(buf, "idle\n");
262 case REGULATOR_MODE_STANDBY:
263 return sprintf(buf, "standby\n");
264 }
265 return sprintf(buf, "unknown\n");
266}
267
David Brownell4fca9542008-11-11 17:38:53 -0800268static ssize_t regulator_opmode_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100270{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100271 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100272
David Brownell4fca9542008-11-11 17:38:53 -0800273 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
274}
David Brownell7ad68e22008-11-11 17:39:02 -0800275static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800276
277static ssize_t regulator_print_state(char *buf, int state)
278{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100279 if (state > 0)
280 return sprintf(buf, "enabled\n");
281 else if (state == 0)
282 return sprintf(buf, "disabled\n");
283 else
284 return sprintf(buf, "unknown\n");
285}
286
David Brownell4fca9542008-11-11 17:38:53 -0800287static ssize_t regulator_state_show(struct device *dev,
288 struct device_attribute *attr, char *buf)
289{
290 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100291 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800292
Mark Brown93325462009-08-03 18:49:56 +0100293 mutex_lock(&rdev->mutex);
294 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
295 mutex_unlock(&rdev->mutex);
296
297 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800298}
David Brownell7ad68e22008-11-11 17:39:02 -0800299static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800300
David Brownell853116a2009-01-14 23:03:17 -0800301static ssize_t regulator_status_show(struct device *dev,
302 struct device_attribute *attr, char *buf)
303{
304 struct regulator_dev *rdev = dev_get_drvdata(dev);
305 int status;
306 char *label;
307
308 status = rdev->desc->ops->get_status(rdev);
309 if (status < 0)
310 return status;
311
312 switch (status) {
313 case REGULATOR_STATUS_OFF:
314 label = "off";
315 break;
316 case REGULATOR_STATUS_ON:
317 label = "on";
318 break;
319 case REGULATOR_STATUS_ERROR:
320 label = "error";
321 break;
322 case REGULATOR_STATUS_FAST:
323 label = "fast";
324 break;
325 case REGULATOR_STATUS_NORMAL:
326 label = "normal";
327 break;
328 case REGULATOR_STATUS_IDLE:
329 label = "idle";
330 break;
331 case REGULATOR_STATUS_STANDBY:
332 label = "standby";
333 break;
334 default:
335 return -ERANGE;
336 }
337
338 return sprintf(buf, "%s\n", label);
339}
340static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
341
Liam Girdwood414c70c2008-04-30 15:59:04 +0100342static ssize_t regulator_min_uA_show(struct device *dev,
343 struct device_attribute *attr, char *buf)
344{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100345 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100346
347 if (!rdev->constraints)
348 return sprintf(buf, "constraint not defined\n");
349
350 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
351}
David Brownell7ad68e22008-11-11 17:39:02 -0800352static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100353
354static ssize_t regulator_max_uA_show(struct device *dev,
355 struct device_attribute *attr, char *buf)
356{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100357 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100358
359 if (!rdev->constraints)
360 return sprintf(buf, "constraint not defined\n");
361
362 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
363}
David Brownell7ad68e22008-11-11 17:39:02 -0800364static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100365
366static ssize_t regulator_min_uV_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_uV);
375}
David Brownell7ad68e22008-11-11 17:39:02 -0800376static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100377
378static ssize_t regulator_max_uV_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_uV);
387}
David Brownell7ad68e22008-11-11 17:39:02 -0800388static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100389
390static ssize_t regulator_total_uA_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 struct regulator *regulator;
395 int uA = 0;
396
397 mutex_lock(&rdev->mutex);
398 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100399 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100400 mutex_unlock(&rdev->mutex);
401 return sprintf(buf, "%d\n", uA);
402}
David Brownell7ad68e22008-11-11 17:39:02 -0800403static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100404
405static ssize_t regulator_num_users_show(struct device *dev,
406 struct device_attribute *attr, char *buf)
407{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100408 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100409 return sprintf(buf, "%d\n", rdev->use_count);
410}
411
412static ssize_t regulator_type_show(struct device *dev,
413 struct device_attribute *attr, char *buf)
414{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100415 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100416
417 switch (rdev->desc->type) {
418 case REGULATOR_VOLTAGE:
419 return sprintf(buf, "voltage\n");
420 case REGULATOR_CURRENT:
421 return sprintf(buf, "current\n");
422 }
423 return sprintf(buf, "unknown\n");
424}
425
426static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
427 struct device_attribute *attr, char *buf)
428{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100429 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100430
Liam Girdwood414c70c2008-04-30 15:59:04 +0100431 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
432}
David Brownell7ad68e22008-11-11 17:39:02 -0800433static DEVICE_ATTR(suspend_mem_microvolts, 0444,
434 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
436static ssize_t regulator_suspend_disk_uV_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
Liam Girdwood414c70c2008-04-30 15:59:04 +0100441 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
442}
David Brownell7ad68e22008-11-11 17:39:02 -0800443static DEVICE_ATTR(suspend_disk_microvolts, 0444,
444 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100445
446static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
447 struct device_attribute *attr, char *buf)
448{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100449 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
Liam Girdwood414c70c2008-04-30 15:59:04 +0100451 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
452}
David Brownell7ad68e22008-11-11 17:39:02 -0800453static DEVICE_ATTR(suspend_standby_microvolts, 0444,
454 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
Liam Girdwood414c70c2008-04-30 15:59:04 +0100456static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
457 struct device_attribute *attr, char *buf)
458{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100459 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
David Brownell4fca9542008-11-11 17:38:53 -0800461 return regulator_print_opmode(buf,
462 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463}
David Brownell7ad68e22008-11-11 17:39:02 -0800464static DEVICE_ATTR(suspend_mem_mode, 0444,
465 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466
467static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
468 struct device_attribute *attr, char *buf)
469{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100470 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
David Brownell4fca9542008-11-11 17:38:53 -0800472 return regulator_print_opmode(buf,
473 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474}
David Brownell7ad68e22008-11-11 17:39:02 -0800475static DEVICE_ATTR(suspend_disk_mode, 0444,
476 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477
478static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
479 struct device_attribute *attr, char *buf)
480{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100481 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
David Brownell4fca9542008-11-11 17:38:53 -0800483 return regulator_print_opmode(buf,
484 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485}
David Brownell7ad68e22008-11-11 17:39:02 -0800486static DEVICE_ATTR(suspend_standby_mode, 0444,
487 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488
489static ssize_t regulator_suspend_mem_state_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100492 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
David Brownell4fca9542008-11-11 17:38:53 -0800494 return regulator_print_state(buf,
495 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496}
David Brownell7ad68e22008-11-11 17:39:02 -0800497static DEVICE_ATTR(suspend_mem_state, 0444,
498 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499
500static ssize_t regulator_suspend_disk_state_show(struct device *dev,
501 struct device_attribute *attr, char *buf)
502{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100503 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
David Brownell4fca9542008-11-11 17:38:53 -0800505 return regulator_print_state(buf,
506 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507}
David Brownell7ad68e22008-11-11 17:39:02 -0800508static DEVICE_ATTR(suspend_disk_state, 0444,
509 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510
511static ssize_t regulator_suspend_standby_state_show(struct device *dev,
512 struct device_attribute *attr, char *buf)
513{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100514 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
David Brownell4fca9542008-11-11 17:38:53 -0800516 return regulator_print_state(buf,
517 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518}
David Brownell7ad68e22008-11-11 17:39:02 -0800519static DEVICE_ATTR(suspend_standby_state, 0444,
520 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100521
David Brownell7ad68e22008-11-11 17:39:02 -0800522
523/*
524 * These are the only attributes are present for all regulators.
525 * Other attributes are a function of regulator functionality.
526 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100527static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100528 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
530 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100531 __ATTR_NULL,
532};
533
534static void regulator_dev_release(struct device *dev)
535{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100536 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100537 kfree(rdev);
538}
539
540static struct class regulator_class = {
541 .name = "regulator",
542 .dev_release = regulator_dev_release,
543 .dev_attrs = regulator_dev_attrs,
544};
545
546/* Calculate the new optimum regulator operating mode based on the new total
547 * consumer load. All locks held by caller */
548static void drms_uA_update(struct regulator_dev *rdev)
549{
550 struct regulator *sibling;
551 int current_uA = 0, output_uV, input_uV, err;
552 unsigned int mode;
553
554 err = regulator_check_drms(rdev);
555 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300556 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
557 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100558
559 /* get output voltage */
560 output_uV = rdev->desc->ops->get_voltage(rdev);
561 if (output_uV <= 0)
562 return;
563
564 /* get input voltage */
565 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
566 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
567 else
568 input_uV = rdev->constraints->input_uV;
569 if (input_uV <= 0)
570 return;
571
572 /* calc total requested load */
573 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100574 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100575
576 /* now get the optimum mode for our new total regulator load */
577 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
578 output_uV, current_uA);
579
580 /* check the new mode is allowed */
581 err = regulator_check_mode(rdev, mode);
582 if (err == 0)
583 rdev->desc->ops->set_mode(rdev, mode);
584}
585
586static int suspend_set_state(struct regulator_dev *rdev,
587 struct regulator_state *rstate)
588{
589 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100590 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100591
Mark Brown638f85c2009-10-22 16:31:33 +0100592 can_set_state = rdev->desc->ops->set_suspend_enable &&
593 rdev->desc->ops->set_suspend_disable;
594
595 /* If we have no suspend mode configration don't set anything;
596 * only warn if the driver actually makes the suspend mode
597 * configurable.
598 */
599 if (!rstate->enabled && !rstate->disabled) {
600 if (can_set_state)
601 printk(KERN_WARNING "%s: No configuration for %s\n",
602 __func__, rdev_get_name(rdev));
603 return 0;
604 }
605
606 if (rstate->enabled && rstate->disabled) {
607 printk(KERN_ERR "%s: invalid configuration for %s\n",
608 __func__, rdev_get_name(rdev));
609 return -EINVAL;
610 }
611
612 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100613 printk(KERN_ERR "%s: no way to set suspend state\n",
614 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100615 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100616 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100617
618 if (rstate->enabled)
619 ret = rdev->desc->ops->set_suspend_enable(rdev);
620 else
621 ret = rdev->desc->ops->set_suspend_disable(rdev);
622 if (ret < 0) {
623 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
624 return ret;
625 }
626
627 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
628 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
629 if (ret < 0) {
630 printk(KERN_ERR "%s: failed to set voltage\n",
631 __func__);
632 return ret;
633 }
634 }
635
636 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
637 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
638 if (ret < 0) {
639 printk(KERN_ERR "%s: failed to set mode\n", __func__);
640 return ret;
641 }
642 }
643 return ret;
644}
645
646/* locks held by caller */
647static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
648{
649 if (!rdev->constraints)
650 return -EINVAL;
651
652 switch (state) {
653 case PM_SUSPEND_STANDBY:
654 return suspend_set_state(rdev,
655 &rdev->constraints->state_standby);
656 case PM_SUSPEND_MEM:
657 return suspend_set_state(rdev,
658 &rdev->constraints->state_mem);
659 case PM_SUSPEND_MAX:
660 return suspend_set_state(rdev,
661 &rdev->constraints->state_disk);
662 default:
663 return -EINVAL;
664 }
665}
666
667static void print_constraints(struct regulator_dev *rdev)
668{
669 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000670 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100671 int count = 0;
672 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100673
Mark Brown8f031b42009-10-22 16:31:31 +0100674 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100675 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100676 count += sprintf(buf + count, "%d mV ",
677 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678 else
Mark Brown8f031b42009-10-22 16:31:31 +0100679 count += sprintf(buf + count, "%d <--> %d mV ",
680 constraints->min_uV / 1000,
681 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100682 }
Mark Brown8f031b42009-10-22 16:31:31 +0100683
684 if (!constraints->min_uV ||
685 constraints->min_uV != constraints->max_uV) {
686 ret = _regulator_get_voltage(rdev);
687 if (ret > 0)
688 count += sprintf(buf + count, "at %d mV ", ret / 1000);
689 }
690
691 if (constraints->min_uA && constraints->max_uA) {
692 if (constraints->min_uA == constraints->max_uA)
693 count += sprintf(buf + count, "%d mA ",
694 constraints->min_uA / 1000);
695 else
696 count += sprintf(buf + count, "%d <--> %d mA ",
697 constraints->min_uA / 1000,
698 constraints->max_uA / 1000);
699 }
700
701 if (!constraints->min_uA ||
702 constraints->min_uA != constraints->max_uA) {
703 ret = _regulator_get_current_limit(rdev);
704 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400705 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100706 }
707
Liam Girdwood414c70c2008-04-30 15:59:04 +0100708 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
709 count += sprintf(buf + count, "fast ");
710 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
711 count += sprintf(buf + count, "normal ");
712 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
713 count += sprintf(buf + count, "idle ");
714 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
715 count += sprintf(buf + count, "standby");
716
Mark Brown1083c392009-10-22 16:31:32 +0100717 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100718}
719
Mark Browne79055d2009-10-19 15:53:50 +0100720static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100721 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100722{
723 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100724 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100725 int ret;
726
727 /* do we need to apply the constraint voltage */
728 if (rdev->constraints->apply_uV &&
729 rdev->constraints->min_uV == rdev->constraints->max_uV &&
730 ops->set_voltage) {
731 ret = ops->set_voltage(rdev,
732 rdev->constraints->min_uV, rdev->constraints->max_uV);
733 if (ret < 0) {
734 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
735 __func__,
736 rdev->constraints->min_uV, name);
737 rdev->constraints = NULL;
738 return ret;
739 }
740 }
Mark Browne79055d2009-10-19 15:53:50 +0100741
742 /* constrain machine-level voltage specs to fit
743 * the actual range supported by this regulator.
744 */
745 if (ops->list_voltage && rdev->desc->n_voltages) {
746 int count = rdev->desc->n_voltages;
747 int i;
748 int min_uV = INT_MAX;
749 int max_uV = INT_MIN;
750 int cmin = constraints->min_uV;
751 int cmax = constraints->max_uV;
752
753 /* it's safe to autoconfigure fixed-voltage supplies
754 and the constraints are used by list_voltage. */
755 if (count == 1 && !cmin) {
756 cmin = 1;
757 cmax = INT_MAX;
758 constraints->min_uV = cmin;
759 constraints->max_uV = cmax;
760 }
761
762 /* voltage constraints are optional */
763 if ((cmin == 0) && (cmax == 0))
764 return 0;
765
766 /* else require explicit machine-level constraints */
767 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
768 pr_err("%s: %s '%s' voltage constraints\n",
769 __func__, "invalid", name);
770 return -EINVAL;
771 }
772
773 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
774 for (i = 0; i < count; i++) {
775 int value;
776
777 value = ops->list_voltage(rdev, i);
778 if (value <= 0)
779 continue;
780
781 /* maybe adjust [min_uV..max_uV] */
782 if (value >= cmin && value < min_uV)
783 min_uV = value;
784 if (value <= cmax && value > max_uV)
785 max_uV = value;
786 }
787
788 /* final: [min_uV..max_uV] valid iff constraints valid */
789 if (max_uV < min_uV) {
790 pr_err("%s: %s '%s' voltage constraints\n",
791 __func__, "unsupportable", name);
792 return -EINVAL;
793 }
794
795 /* use regulator's subset of machine constraints */
796 if (constraints->min_uV < min_uV) {
797 pr_debug("%s: override '%s' %s, %d -> %d\n",
798 __func__, name, "min_uV",
799 constraints->min_uV, min_uV);
800 constraints->min_uV = min_uV;
801 }
802 if (constraints->max_uV > max_uV) {
803 pr_debug("%s: override '%s' %s, %d -> %d\n",
804 __func__, name, "max_uV",
805 constraints->max_uV, max_uV);
806 constraints->max_uV = max_uV;
807 }
808 }
809
810 return 0;
811}
812
Liam Girdwooda5766f12008-10-10 13:22:20 +0100813/**
814 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000815 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000816 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100817 *
818 * Allows platform initialisation code to define and constrain
819 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
820 * Constraints *must* be set by platform code in order for some
821 * regulator operations to proceed i.e. set_voltage, set_current_limit,
822 * set_mode.
823 */
824static int set_machine_constraints(struct regulator_dev *rdev,
825 struct regulation_constraints *constraints)
826{
827 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100828 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100829 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100830
Mark Brownaf5866c2009-10-22 16:31:30 +0100831 rdev->constraints = constraints;
832
Mark Brown1083c392009-10-22 16:31:32 +0100833 name = rdev_get_name(rdev);
834
835 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100836 if (ret != 0)
837 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800838
Liam Girdwooda5766f12008-10-10 13:22:20 +0100839 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100840 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100841 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100842 if (ret < 0) {
843 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
844 __func__, name);
845 rdev->constraints = NULL;
846 goto out;
847 }
848 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100849
Mark Browna3084662009-02-26 19:24:19 +0000850 if (constraints->initial_mode) {
851 if (!ops->set_mode) {
852 printk(KERN_ERR "%s: no set_mode operation for %s\n",
853 __func__, name);
854 ret = -EINVAL;
855 goto out;
856 }
857
858 ret = ops->set_mode(rdev, constraints->initial_mode);
859 if (ret < 0) {
860 printk(KERN_ERR
861 "%s: failed to set initial mode for %s: %d\n",
862 __func__, name, ret);
863 goto out;
864 }
865 }
866
Mark Browncacf90f2009-03-02 16:32:46 +0000867 /* If the constraints say the regulator should be on at this point
868 * and we have control then make sure it is enabled.
869 */
870 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100871 ret = ops->enable(rdev);
872 if (ret < 0) {
873 printk(KERN_ERR "%s: failed to enable %s\n",
874 __func__, name);
875 rdev->constraints = NULL;
876 goto out;
877 }
878 }
879
Liam Girdwooda5766f12008-10-10 13:22:20 +0100880 print_constraints(rdev);
881out:
882 return ret;
883}
884
885/**
886 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000887 * @rdev: regulator name
888 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100889 *
890 * Called by platform initialisation code to set the supply regulator for this
891 * regulator. This ensures that a regulators supply will also be enabled by the
892 * core if it's child is enabled.
893 */
894static int set_supply(struct regulator_dev *rdev,
895 struct regulator_dev *supply_rdev)
896{
897 int err;
898
899 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
900 "supply");
901 if (err) {
902 printk(KERN_ERR
903 "%s: could not add device link %s err %d\n",
904 __func__, supply_rdev->dev.kobj.name, err);
905 goto out;
906 }
907 rdev->supply = supply_rdev;
908 list_add(&rdev->slist, &supply_rdev->supply_list);
909out:
910 return err;
911}
912
913/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800914 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000915 * @rdev: regulator source
916 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100917 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000918 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100919 *
920 * Allows platform initialisation code to map physical regulator
921 * sources to symbolic names for supplies for use by devices. Devices
922 * should use these symbolic names to request regulators, avoiding the
923 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100924 *
925 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100926 */
927static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100928 struct device *consumer_dev, const char *consumer_dev_name,
929 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100930{
931 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100932 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100933
Mark Brown40f92442009-06-17 17:56:39 +0100934 if (consumer_dev && consumer_dev_name)
935 return -EINVAL;
936
937 if (!consumer_dev_name && consumer_dev)
938 consumer_dev_name = dev_name(consumer_dev);
939
Liam Girdwooda5766f12008-10-10 13:22:20 +0100940 if (supply == NULL)
941 return -EINVAL;
942
Mark Brown9ed20992009-07-21 16:00:26 +0100943 if (consumer_dev_name != NULL)
944 has_dev = 1;
945 else
946 has_dev = 0;
947
David Brownell6001e132008-12-31 12:54:19 +0000948 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300949 if (node->dev_name && consumer_dev_name) {
950 if (strcmp(node->dev_name, consumer_dev_name) != 0)
951 continue;
952 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000953 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300954 }
955
David Brownell6001e132008-12-31 12:54:19 +0000956 if (strcmp(node->supply, supply) != 0)
957 continue;
958
959 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
960 dev_name(&node->regulator->dev),
961 node->regulator->desc->name,
962 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100963 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000964 return -EBUSY;
965 }
966
Mark Brown9ed20992009-07-21 16:00:26 +0100967 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100968 if (node == NULL)
969 return -ENOMEM;
970
971 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100972 node->supply = supply;
973
Mark Brown9ed20992009-07-21 16:00:26 +0100974 if (has_dev) {
975 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
976 if (node->dev_name == NULL) {
977 kfree(node);
978 return -ENOMEM;
979 }
Mark Brown40f92442009-06-17 17:56:39 +0100980 }
981
Liam Girdwooda5766f12008-10-10 13:22:20 +0100982 list_add(&node->list, &regulator_map_list);
983 return 0;
984}
985
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200986static void unset_regulator_supplies(struct regulator_dev *rdev)
987{
988 struct regulator_map *node, *n;
989
990 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
991 if (rdev == node->regulator) {
992 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100993 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200994 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200995 }
996 }
997}
998
Liam Girdwood414c70c2008-04-30 15:59:04 +0100999#define REG_STR_SIZE 32
1000
1001static struct regulator *create_regulator(struct regulator_dev *rdev,
1002 struct device *dev,
1003 const char *supply_name)
1004{
1005 struct regulator *regulator;
1006 char buf[REG_STR_SIZE];
1007 int err, size;
1008
1009 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1010 if (regulator == NULL)
1011 return NULL;
1012
1013 mutex_lock(&rdev->mutex);
1014 regulator->rdev = rdev;
1015 list_add(&regulator->list, &rdev->consumer_list);
1016
1017 if (dev) {
1018 /* create a 'requested_microamps_name' sysfs entry */
1019 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1020 supply_name);
1021 if (size >= REG_STR_SIZE)
1022 goto overflow_err;
1023
1024 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001025 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001026 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1027 if (regulator->dev_attr.attr.name == NULL)
1028 goto attr_name_err;
1029
Liam Girdwood414c70c2008-04-30 15:59:04 +01001030 regulator->dev_attr.attr.mode = 0444;
1031 regulator->dev_attr.show = device_requested_uA_show;
1032 err = device_create_file(dev, &regulator->dev_attr);
1033 if (err < 0) {
1034 printk(KERN_WARNING "%s: could not add regulator_dev"
1035 " load sysfs\n", __func__);
1036 goto attr_name_err;
1037 }
1038
1039 /* also add a link to the device sysfs entry */
1040 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1041 dev->kobj.name, supply_name);
1042 if (size >= REG_STR_SIZE)
1043 goto attr_err;
1044
1045 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1046 if (regulator->supply_name == NULL)
1047 goto attr_err;
1048
1049 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1050 buf);
1051 if (err) {
1052 printk(KERN_WARNING
1053 "%s: could not add device link %s err %d\n",
1054 __func__, dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001055 goto link_name_err;
1056 }
1057 }
1058 mutex_unlock(&rdev->mutex);
1059 return regulator;
1060link_name_err:
1061 kfree(regulator->supply_name);
1062attr_err:
1063 device_remove_file(regulator->dev, &regulator->dev_attr);
1064attr_name_err:
1065 kfree(regulator->dev_attr.attr.name);
1066overflow_err:
1067 list_del(&regulator->list);
1068 kfree(regulator);
1069 mutex_unlock(&rdev->mutex);
1070 return NULL;
1071}
1072
Mark Brown31aae2b2009-12-21 12:21:52 +00001073static int _regulator_get_enable_time(struct regulator_dev *rdev)
1074{
1075 if (!rdev->desc->ops->enable_time)
1076 return 0;
1077 return rdev->desc->ops->enable_time(rdev);
1078}
1079
Mark Brown5ffbd132009-07-21 16:00:23 +01001080/* Internal regulator request function */
1081static struct regulator *_regulator_get(struct device *dev, const char *id,
1082 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001083{
1084 struct regulator_dev *rdev;
1085 struct regulator_map *map;
1086 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001087 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001088 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089
1090 if (id == NULL) {
1091 printk(KERN_ERR "regulator: get() with no identifier\n");
1092 return regulator;
1093 }
1094
Mark Brown40f92442009-06-17 17:56:39 +01001095 if (dev)
1096 devname = dev_name(dev);
1097
Liam Girdwood414c70c2008-04-30 15:59:04 +01001098 mutex_lock(&regulator_list_mutex);
1099
1100 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001101 /* If the mapping has a device set up it must match */
1102 if (map->dev_name &&
1103 (!devname || strcmp(map->dev_name, devname)))
1104 continue;
1105
1106 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001107 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001108 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001109 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001110 }
Mark Brown34abbd62010-02-12 10:18:08 +00001111
Mark Brown688fe992010-10-05 19:18:32 -07001112 if (board_wants_dummy_regulator) {
1113 rdev = dummy_regulator_rdev;
1114 goto found;
1115 }
1116
Mark Brown34abbd62010-02-12 10:18:08 +00001117#ifdef CONFIG_REGULATOR_DUMMY
1118 if (!devname)
1119 devname = "deviceless";
1120
1121 /* If the board didn't flag that it was fully constrained then
1122 * substitute in a dummy regulator so consumers can continue.
1123 */
1124 if (!has_full_constraints) {
1125 pr_warning("%s supply %s not found, using dummy regulator\n",
1126 devname, id);
1127 rdev = dummy_regulator_rdev;
1128 goto found;
1129 }
1130#endif
1131
Liam Girdwood414c70c2008-04-30 15:59:04 +01001132 mutex_unlock(&regulator_list_mutex);
1133 return regulator;
1134
1135found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001136 if (rdev->exclusive) {
1137 regulator = ERR_PTR(-EPERM);
1138 goto out;
1139 }
1140
1141 if (exclusive && rdev->open_count) {
1142 regulator = ERR_PTR(-EBUSY);
1143 goto out;
1144 }
1145
Liam Girdwooda5766f12008-10-10 13:22:20 +01001146 if (!try_module_get(rdev->owner))
1147 goto out;
1148
Liam Girdwood414c70c2008-04-30 15:59:04 +01001149 regulator = create_regulator(rdev, dev, id);
1150 if (regulator == NULL) {
1151 regulator = ERR_PTR(-ENOMEM);
1152 module_put(rdev->owner);
1153 }
1154
Mark Brown5ffbd132009-07-21 16:00:23 +01001155 rdev->open_count++;
1156 if (exclusive) {
1157 rdev->exclusive = 1;
1158
1159 ret = _regulator_is_enabled(rdev);
1160 if (ret > 0)
1161 rdev->use_count = 1;
1162 else
1163 rdev->use_count = 0;
1164 }
1165
Liam Girdwooda5766f12008-10-10 13:22:20 +01001166out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001167 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001168
Liam Girdwood414c70c2008-04-30 15:59:04 +01001169 return regulator;
1170}
Mark Brown5ffbd132009-07-21 16:00:23 +01001171
1172/**
1173 * regulator_get - lookup and obtain a reference to a regulator.
1174 * @dev: device for regulator "consumer"
1175 * @id: Supply name or regulator ID.
1176 *
1177 * Returns a struct regulator corresponding to the regulator producer,
1178 * or IS_ERR() condition containing errno.
1179 *
1180 * Use of supply names configured via regulator_set_device_supply() is
1181 * strongly encouraged. It is recommended that the supply name used
1182 * should match the name used for the supply and/or the relevant
1183 * device pins in the datasheet.
1184 */
1185struct regulator *regulator_get(struct device *dev, const char *id)
1186{
1187 return _regulator_get(dev, id, 0);
1188}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001189EXPORT_SYMBOL_GPL(regulator_get);
1190
1191/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001192 * regulator_get_exclusive - obtain exclusive access to a regulator.
1193 * @dev: device for regulator "consumer"
1194 * @id: Supply name or regulator ID.
1195 *
1196 * Returns a struct regulator corresponding to the regulator producer,
1197 * or IS_ERR() condition containing errno. Other consumers will be
1198 * unable to obtain this reference is held and the use count for the
1199 * regulator will be initialised to reflect the current state of the
1200 * regulator.
1201 *
1202 * This is intended for use by consumers which cannot tolerate shared
1203 * use of the regulator such as those which need to force the
1204 * regulator off for correct operation of the hardware they are
1205 * controlling.
1206 *
1207 * Use of supply names configured via regulator_set_device_supply() is
1208 * strongly encouraged. It is recommended that the supply name used
1209 * should match the name used for the supply and/or the relevant
1210 * device pins in the datasheet.
1211 */
1212struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1213{
1214 return _regulator_get(dev, id, 1);
1215}
1216EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1217
1218/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219 * regulator_put - "free" the regulator source
1220 * @regulator: regulator source
1221 *
1222 * Note: drivers must ensure that all regulator_enable calls made on this
1223 * regulator source are balanced by regulator_disable calls prior to calling
1224 * this function.
1225 */
1226void regulator_put(struct regulator *regulator)
1227{
1228 struct regulator_dev *rdev;
1229
1230 if (regulator == NULL || IS_ERR(regulator))
1231 return;
1232
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233 mutex_lock(&regulator_list_mutex);
1234 rdev = regulator->rdev;
1235
1236 /* remove any sysfs entries */
1237 if (regulator->dev) {
1238 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1239 kfree(regulator->supply_name);
1240 device_remove_file(regulator->dev, &regulator->dev_attr);
1241 kfree(regulator->dev_attr.attr.name);
1242 }
1243 list_del(&regulator->list);
1244 kfree(regulator);
1245
Mark Brown5ffbd132009-07-21 16:00:23 +01001246 rdev->open_count--;
1247 rdev->exclusive = 0;
1248
Liam Girdwood414c70c2008-04-30 15:59:04 +01001249 module_put(rdev->owner);
1250 mutex_unlock(&regulator_list_mutex);
1251}
1252EXPORT_SYMBOL_GPL(regulator_put);
1253
Mark Brown9a2372f2009-08-03 18:49:57 +01001254static int _regulator_can_change_status(struct regulator_dev *rdev)
1255{
1256 if (!rdev->constraints)
1257 return 0;
1258
1259 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1260 return 1;
1261 else
1262 return 0;
1263}
1264
Liam Girdwood414c70c2008-04-30 15:59:04 +01001265/* locks held by regulator_enable() */
1266static int _regulator_enable(struct regulator_dev *rdev)
1267{
Mark Brown31aae2b2009-12-21 12:21:52 +00001268 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001269
Bengt Jonssonf3c18a82010-11-10 11:06:22 +01001270 if (rdev->use_count == 0) {
1271 /* do we need to enable the supply regulator first */
1272 if (rdev->supply) {
1273 mutex_lock(&rdev->supply->mutex);
1274 ret = _regulator_enable(rdev->supply);
1275 mutex_unlock(&rdev->supply->mutex);
1276 if (ret < 0) {
1277 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1278 __func__, rdev_get_name(rdev), ret);
1279 return ret;
1280 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001281 }
1282 }
1283
1284 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001285 if (rdev->constraints &&
1286 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1287 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001288
Mark Brown9a2372f2009-08-03 18:49:57 +01001289 if (rdev->use_count == 0) {
1290 /* The regulator may on if it's not switchable or left on */
1291 ret = _regulator_is_enabled(rdev);
1292 if (ret == -EINVAL || ret == 0) {
1293 if (!_regulator_can_change_status(rdev))
1294 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295
Mark Brown31aae2b2009-12-21 12:21:52 +00001296 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001297 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001298
1299 /* Query before enabling in case configuration
1300 * dependant. */
1301 ret = _regulator_get_enable_time(rdev);
1302 if (ret >= 0) {
1303 delay = ret;
1304 } else {
1305 printk(KERN_WARNING
1306 "%s: enable_time() failed for %s: %d\n",
1307 __func__, rdev_get_name(rdev),
1308 ret);
1309 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001310 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001311
1312 /* Allow the regulator to ramp; it would be useful
1313 * to extend this for bulk operations so that the
1314 * regulators can ramp together. */
1315 ret = rdev->desc->ops->enable(rdev);
1316 if (ret < 0)
1317 return ret;
1318
Axel Line36c1df2010-11-05 21:51:32 +08001319 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001320 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001321 udelay(delay % 1000);
1322 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001323 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001324 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001325
Linus Walleija7433cf2009-08-26 12:54:04 +02001326 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001327 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001328 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001329 return ret;
1330 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001331 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001332 }
1333
Mark Brown9a2372f2009-08-03 18:49:57 +01001334 rdev->use_count++;
1335
1336 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001337}
1338
1339/**
1340 * regulator_enable - enable regulator output
1341 * @regulator: regulator source
1342 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001343 * Request that the regulator be enabled with the regulator output at
1344 * the predefined voltage or current value. Calls to regulator_enable()
1345 * must be balanced with calls to regulator_disable().
1346 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001348 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349 */
1350int regulator_enable(struct regulator *regulator)
1351{
David Brownell412aec62008-11-16 11:44:46 -08001352 struct regulator_dev *rdev = regulator->rdev;
1353 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001354
David Brownell412aec62008-11-16 11:44:46 -08001355 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001356 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001357 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001358 return ret;
1359}
1360EXPORT_SYMBOL_GPL(regulator_enable);
1361
1362/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001363static int _regulator_disable(struct regulator_dev *rdev,
1364 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001365{
1366 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001367 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368
David Brownellcd94b502009-03-11 16:43:34 -08001369 if (WARN(rdev->use_count <= 0,
1370 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001371 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001372 return -EIO;
1373
Liam Girdwood414c70c2008-04-30 15:59:04 +01001374 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001375 if (rdev->use_count == 1 &&
1376 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377
1378 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001379 if (_regulator_can_change_status(rdev) &&
1380 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001381 ret = rdev->desc->ops->disable(rdev);
1382 if (ret < 0) {
1383 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001384 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001385 return ret;
1386 }
Mark Brown84b68262009-12-01 21:12:27 +00001387
1388 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1389 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001390 }
1391
1392 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001393 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001394
1395 rdev->use_count = 0;
1396 } else if (rdev->use_count > 1) {
1397
1398 if (rdev->constraints &&
1399 (rdev->constraints->valid_ops_mask &
1400 REGULATOR_CHANGE_DRMS))
1401 drms_uA_update(rdev);
1402
1403 rdev->use_count--;
1404 }
1405 return ret;
1406}
1407
1408/**
1409 * regulator_disable - disable regulator output
1410 * @regulator: regulator source
1411 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001412 * Disable the regulator output voltage or current. Calls to
1413 * regulator_enable() must be balanced with calls to
1414 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001415 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001416 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001417 * devices have it enabled, the regulator device supports disabling and
1418 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 */
1420int regulator_disable(struct regulator *regulator)
1421{
David Brownell412aec62008-11-16 11:44:46 -08001422 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001423 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001424 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001425
David Brownell412aec62008-11-16 11:44:46 -08001426 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001427 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001428 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001429
1430 /* decrease our supplies ref count and disable if required */
1431 while (supply_rdev != NULL) {
1432 rdev = supply_rdev;
1433
1434 mutex_lock(&rdev->mutex);
1435 _regulator_disable(rdev, &supply_rdev);
1436 mutex_unlock(&rdev->mutex);
1437 }
1438
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439 return ret;
1440}
1441EXPORT_SYMBOL_GPL(regulator_disable);
1442
1443/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001444static int _regulator_force_disable(struct regulator_dev *rdev,
1445 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001446{
1447 int ret = 0;
1448
1449 /* force disable */
1450 if (rdev->desc->ops->disable) {
1451 /* ah well, who wants to live forever... */
1452 ret = rdev->desc->ops->disable(rdev);
1453 if (ret < 0) {
1454 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001455 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001456 return ret;
1457 }
1458 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001459 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1460 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001461 }
1462
1463 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001464 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001465
1466 rdev->use_count = 0;
1467 return ret;
1468}
1469
1470/**
1471 * regulator_force_disable - force disable regulator output
1472 * @regulator: regulator source
1473 *
1474 * Forcibly disable the regulator output voltage or current.
1475 * NOTE: this *will* disable the regulator output even if other consumer
1476 * devices have it enabled. This should be used for situations when device
1477 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1478 */
1479int regulator_force_disable(struct regulator *regulator)
1480{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001481 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001482 int ret;
1483
1484 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001486 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001487 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001488
1489 if (supply_rdev)
1490 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1491
Liam Girdwood414c70c2008-04-30 15:59:04 +01001492 return ret;
1493}
1494EXPORT_SYMBOL_GPL(regulator_force_disable);
1495
1496static int _regulator_is_enabled(struct regulator_dev *rdev)
1497{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001498 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001499 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001500 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501
Mark Brown93325462009-08-03 18:49:56 +01001502 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503}
1504
1505/**
1506 * regulator_is_enabled - is the regulator output enabled
1507 * @regulator: regulator source
1508 *
David Brownell412aec62008-11-16 11:44:46 -08001509 * Returns positive if the regulator driver backing the source/client
1510 * has requested that the device be enabled, zero if it hasn't, else a
1511 * negative errno code.
1512 *
1513 * Note that the device backing this regulator handle can have multiple
1514 * users, so it might be enabled even if regulator_enable() was never
1515 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001516 */
1517int regulator_is_enabled(struct regulator *regulator)
1518{
Mark Brown93325462009-08-03 18:49:56 +01001519 int ret;
1520
1521 mutex_lock(&regulator->rdev->mutex);
1522 ret = _regulator_is_enabled(regulator->rdev);
1523 mutex_unlock(&regulator->rdev->mutex);
1524
1525 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001526}
1527EXPORT_SYMBOL_GPL(regulator_is_enabled);
1528
1529/**
David Brownell4367cfd2009-02-26 11:48:36 -08001530 * regulator_count_voltages - count regulator_list_voltage() selectors
1531 * @regulator: regulator source
1532 *
1533 * Returns number of selectors, or negative errno. Selectors are
1534 * numbered starting at zero, and typically correspond to bitfields
1535 * in hardware registers.
1536 */
1537int regulator_count_voltages(struct regulator *regulator)
1538{
1539 struct regulator_dev *rdev = regulator->rdev;
1540
1541 return rdev->desc->n_voltages ? : -EINVAL;
1542}
1543EXPORT_SYMBOL_GPL(regulator_count_voltages);
1544
1545/**
1546 * regulator_list_voltage - enumerate supported voltages
1547 * @regulator: regulator source
1548 * @selector: identify voltage to list
1549 * Context: can sleep
1550 *
1551 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001552 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001553 * negative errno.
1554 */
1555int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1556{
1557 struct regulator_dev *rdev = regulator->rdev;
1558 struct regulator_ops *ops = rdev->desc->ops;
1559 int ret;
1560
1561 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1562 return -EINVAL;
1563
1564 mutex_lock(&rdev->mutex);
1565 ret = ops->list_voltage(rdev, selector);
1566 mutex_unlock(&rdev->mutex);
1567
1568 if (ret > 0) {
1569 if (ret < rdev->constraints->min_uV)
1570 ret = 0;
1571 else if (ret > rdev->constraints->max_uV)
1572 ret = 0;
1573 }
1574
1575 return ret;
1576}
1577EXPORT_SYMBOL_GPL(regulator_list_voltage);
1578
1579/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001580 * regulator_is_supported_voltage - check if a voltage range can be supported
1581 *
1582 * @regulator: Regulator to check.
1583 * @min_uV: Minimum required voltage in uV.
1584 * @max_uV: Maximum required voltage in uV.
1585 *
1586 * Returns a boolean or a negative error code.
1587 */
1588int regulator_is_supported_voltage(struct regulator *regulator,
1589 int min_uV, int max_uV)
1590{
1591 int i, voltages, ret;
1592
1593 ret = regulator_count_voltages(regulator);
1594 if (ret < 0)
1595 return ret;
1596 voltages = ret;
1597
1598 for (i = 0; i < voltages; i++) {
1599 ret = regulator_list_voltage(regulator, i);
1600
1601 if (ret >= min_uV && ret <= max_uV)
1602 return 1;
1603 }
1604
1605 return 0;
1606}
1607
1608/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001609 * regulator_set_voltage - set regulator output voltage
1610 * @regulator: regulator source
1611 * @min_uV: Minimum required voltage in uV
1612 * @max_uV: Maximum acceptable voltage in uV
1613 *
1614 * Sets a voltage regulator to the desired output voltage. This can be set
1615 * during any regulator state. IOW, regulator can be disabled or enabled.
1616 *
1617 * If the regulator is enabled then the voltage will change to the new value
1618 * immediately otherwise if the regulator is disabled the regulator will
1619 * output at the new voltage when enabled.
1620 *
1621 * NOTE: If the regulator is shared between several devices then the lowest
1622 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001623 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001624 * calling this function otherwise this call will fail.
1625 */
1626int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1627{
1628 struct regulator_dev *rdev = regulator->rdev;
1629 int ret;
1630
1631 mutex_lock(&rdev->mutex);
1632
1633 /* sanity check */
1634 if (!rdev->desc->ops->set_voltage) {
1635 ret = -EINVAL;
1636 goto out;
1637 }
1638
1639 /* constraints check */
1640 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1641 if (ret < 0)
1642 goto out;
1643 regulator->min_uV = min_uV;
1644 regulator->max_uV = max_uV;
1645 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1646
1647out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001648 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001649 mutex_unlock(&rdev->mutex);
1650 return ret;
1651}
1652EXPORT_SYMBOL_GPL(regulator_set_voltage);
1653
1654static int _regulator_get_voltage(struct regulator_dev *rdev)
1655{
1656 /* sanity check */
1657 if (rdev->desc->ops->get_voltage)
1658 return rdev->desc->ops->get_voltage(rdev);
1659 else
1660 return -EINVAL;
1661}
1662
1663/**
1664 * regulator_get_voltage - get regulator output voltage
1665 * @regulator: regulator source
1666 *
1667 * This returns the current regulator voltage in uV.
1668 *
1669 * NOTE: If the regulator is disabled it will return the voltage value. This
1670 * function should not be used to determine regulator state.
1671 */
1672int regulator_get_voltage(struct regulator *regulator)
1673{
1674 int ret;
1675
1676 mutex_lock(&regulator->rdev->mutex);
1677
1678 ret = _regulator_get_voltage(regulator->rdev);
1679
1680 mutex_unlock(&regulator->rdev->mutex);
1681
1682 return ret;
1683}
1684EXPORT_SYMBOL_GPL(regulator_get_voltage);
1685
1686/**
1687 * regulator_set_current_limit - set regulator output current limit
1688 * @regulator: regulator source
1689 * @min_uA: Minimuum supported current in uA
1690 * @max_uA: Maximum supported current in uA
1691 *
1692 * Sets current sink to the desired output current. This can be set during
1693 * any regulator state. IOW, regulator can be disabled or enabled.
1694 *
1695 * If the regulator is enabled then the current will change to the new value
1696 * immediately otherwise if the regulator is disabled the regulator will
1697 * output at the new current when enabled.
1698 *
1699 * NOTE: Regulator system constraints must be set for this regulator before
1700 * calling this function otherwise this call will fail.
1701 */
1702int regulator_set_current_limit(struct regulator *regulator,
1703 int min_uA, int max_uA)
1704{
1705 struct regulator_dev *rdev = regulator->rdev;
1706 int ret;
1707
1708 mutex_lock(&rdev->mutex);
1709
1710 /* sanity check */
1711 if (!rdev->desc->ops->set_current_limit) {
1712 ret = -EINVAL;
1713 goto out;
1714 }
1715
1716 /* constraints check */
1717 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1718 if (ret < 0)
1719 goto out;
1720
1721 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1722out:
1723 mutex_unlock(&rdev->mutex);
1724 return ret;
1725}
1726EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1727
1728static int _regulator_get_current_limit(struct regulator_dev *rdev)
1729{
1730 int ret;
1731
1732 mutex_lock(&rdev->mutex);
1733
1734 /* sanity check */
1735 if (!rdev->desc->ops->get_current_limit) {
1736 ret = -EINVAL;
1737 goto out;
1738 }
1739
1740 ret = rdev->desc->ops->get_current_limit(rdev);
1741out:
1742 mutex_unlock(&rdev->mutex);
1743 return ret;
1744}
1745
1746/**
1747 * regulator_get_current_limit - get regulator output current
1748 * @regulator: regulator source
1749 *
1750 * This returns the current supplied by the specified current sink in uA.
1751 *
1752 * NOTE: If the regulator is disabled it will return the current value. This
1753 * function should not be used to determine regulator state.
1754 */
1755int regulator_get_current_limit(struct regulator *regulator)
1756{
1757 return _regulator_get_current_limit(regulator->rdev);
1758}
1759EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1760
1761/**
1762 * regulator_set_mode - set regulator operating mode
1763 * @regulator: regulator source
1764 * @mode: operating mode - one of the REGULATOR_MODE constants
1765 *
1766 * Set regulator operating mode to increase regulator efficiency or improve
1767 * regulation performance.
1768 *
1769 * NOTE: Regulator system constraints must be set for this regulator before
1770 * calling this function otherwise this call will fail.
1771 */
1772int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1773{
1774 struct regulator_dev *rdev = regulator->rdev;
1775 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301776 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001777
1778 mutex_lock(&rdev->mutex);
1779
1780 /* sanity check */
1781 if (!rdev->desc->ops->set_mode) {
1782 ret = -EINVAL;
1783 goto out;
1784 }
1785
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301786 /* return if the same mode is requested */
1787 if (rdev->desc->ops->get_mode) {
1788 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1789 if (regulator_curr_mode == mode) {
1790 ret = 0;
1791 goto out;
1792 }
1793 }
1794
Liam Girdwood414c70c2008-04-30 15:59:04 +01001795 /* constraints check */
1796 ret = regulator_check_mode(rdev, mode);
1797 if (ret < 0)
1798 goto out;
1799
1800 ret = rdev->desc->ops->set_mode(rdev, mode);
1801out:
1802 mutex_unlock(&rdev->mutex);
1803 return ret;
1804}
1805EXPORT_SYMBOL_GPL(regulator_set_mode);
1806
1807static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1808{
1809 int ret;
1810
1811 mutex_lock(&rdev->mutex);
1812
1813 /* sanity check */
1814 if (!rdev->desc->ops->get_mode) {
1815 ret = -EINVAL;
1816 goto out;
1817 }
1818
1819 ret = rdev->desc->ops->get_mode(rdev);
1820out:
1821 mutex_unlock(&rdev->mutex);
1822 return ret;
1823}
1824
1825/**
1826 * regulator_get_mode - get regulator operating mode
1827 * @regulator: regulator source
1828 *
1829 * Get the current regulator operating mode.
1830 */
1831unsigned int regulator_get_mode(struct regulator *regulator)
1832{
1833 return _regulator_get_mode(regulator->rdev);
1834}
1835EXPORT_SYMBOL_GPL(regulator_get_mode);
1836
1837/**
1838 * regulator_set_optimum_mode - set regulator optimum operating mode
1839 * @regulator: regulator source
1840 * @uA_load: load current
1841 *
1842 * Notifies the regulator core of a new device load. This is then used by
1843 * DRMS (if enabled by constraints) to set the most efficient regulator
1844 * operating mode for the new regulator loading.
1845 *
1846 * Consumer devices notify their supply regulator of the maximum power
1847 * they will require (can be taken from device datasheet in the power
1848 * consumption tables) when they change operational status and hence power
1849 * state. Examples of operational state changes that can affect power
1850 * consumption are :-
1851 *
1852 * o Device is opened / closed.
1853 * o Device I/O is about to begin or has just finished.
1854 * o Device is idling in between work.
1855 *
1856 * This information is also exported via sysfs to userspace.
1857 *
1858 * DRMS will sum the total requested load on the regulator and change
1859 * to the most efficient operating mode if platform constraints allow.
1860 *
1861 * Returns the new regulator mode or error.
1862 */
1863int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1864{
1865 struct regulator_dev *rdev = regulator->rdev;
1866 struct regulator *consumer;
1867 int ret, output_uV, input_uV, total_uA_load = 0;
1868 unsigned int mode;
1869
1870 mutex_lock(&rdev->mutex);
1871
1872 regulator->uA_load = uA_load;
1873 ret = regulator_check_drms(rdev);
1874 if (ret < 0)
1875 goto out;
1876 ret = -EINVAL;
1877
1878 /* sanity check */
1879 if (!rdev->desc->ops->get_optimum_mode)
1880 goto out;
1881
1882 /* get output voltage */
1883 output_uV = rdev->desc->ops->get_voltage(rdev);
1884 if (output_uV <= 0) {
1885 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001886 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001887 goto out;
1888 }
1889
1890 /* get input voltage */
1891 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1892 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1893 else
1894 input_uV = rdev->constraints->input_uV;
1895 if (input_uV <= 0) {
1896 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001897 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001898 goto out;
1899 }
1900
1901 /* calc total requested load for this regulator */
1902 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001903 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001904
1905 mode = rdev->desc->ops->get_optimum_mode(rdev,
1906 input_uV, output_uV,
1907 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001908 ret = regulator_check_mode(rdev, mode);
1909 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001910 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001911 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 total_uA_load, input_uV, output_uV);
1913 goto out;
1914 }
1915
1916 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001917 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001918 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001919 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001920 goto out;
1921 }
1922 ret = mode;
1923out:
1924 mutex_unlock(&rdev->mutex);
1925 return ret;
1926}
1927EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1928
1929/**
1930 * regulator_register_notifier - register regulator event notifier
1931 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001932 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001933 *
1934 * Register notifier block to receive regulator events.
1935 */
1936int regulator_register_notifier(struct regulator *regulator,
1937 struct notifier_block *nb)
1938{
1939 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1940 nb);
1941}
1942EXPORT_SYMBOL_GPL(regulator_register_notifier);
1943
1944/**
1945 * regulator_unregister_notifier - unregister regulator event notifier
1946 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001947 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001948 *
1949 * Unregister regulator event notifier block.
1950 */
1951int regulator_unregister_notifier(struct regulator *regulator,
1952 struct notifier_block *nb)
1953{
1954 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1955 nb);
1956}
1957EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1958
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001959/* notify regulator consumers and downstream regulator consumers.
1960 * Note mutex must be held by caller.
1961 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962static void _notifier_call_chain(struct regulator_dev *rdev,
1963 unsigned long event, void *data)
1964{
1965 struct regulator_dev *_rdev;
1966
1967 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001968 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001969
1970 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001971 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001972 mutex_lock(&_rdev->mutex);
1973 _notifier_call_chain(_rdev, event, data);
1974 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001975 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001976}
1977
1978/**
1979 * regulator_bulk_get - get multiple regulator consumers
1980 *
1981 * @dev: Device to supply
1982 * @num_consumers: Number of consumers to register
1983 * @consumers: Configuration of consumers; clients are stored here.
1984 *
1985 * @return 0 on success, an errno on failure.
1986 *
1987 * This helper function allows drivers to get several regulator
1988 * consumers in one operation. If any of the regulators cannot be
1989 * acquired then any regulators that were allocated will be freed
1990 * before returning to the caller.
1991 */
1992int regulator_bulk_get(struct device *dev, int num_consumers,
1993 struct regulator_bulk_data *consumers)
1994{
1995 int i;
1996 int ret;
1997
1998 for (i = 0; i < num_consumers; i++)
1999 consumers[i].consumer = NULL;
2000
2001 for (i = 0; i < num_consumers; i++) {
2002 consumers[i].consumer = regulator_get(dev,
2003 consumers[i].supply);
2004 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002006 dev_err(dev, "Failed to get supply '%s': %d\n",
2007 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002008 consumers[i].consumer = NULL;
2009 goto err;
2010 }
2011 }
2012
2013 return 0;
2014
2015err:
2016 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2017 regulator_put(consumers[i].consumer);
2018
2019 return ret;
2020}
2021EXPORT_SYMBOL_GPL(regulator_bulk_get);
2022
2023/**
2024 * regulator_bulk_enable - enable multiple regulator consumers
2025 *
2026 * @num_consumers: Number of consumers
2027 * @consumers: Consumer data; clients are stored here.
2028 * @return 0 on success, an errno on failure
2029 *
2030 * This convenience API allows consumers to enable multiple regulator
2031 * clients in a single API call. If any consumers cannot be enabled
2032 * then any others that were enabled will be disabled again prior to
2033 * return.
2034 */
2035int regulator_bulk_enable(int num_consumers,
2036 struct regulator_bulk_data *consumers)
2037{
2038 int i;
2039 int ret;
2040
2041 for (i = 0; i < num_consumers; i++) {
2042 ret = regulator_enable(consumers[i].consumer);
2043 if (ret != 0)
2044 goto err;
2045 }
2046
2047 return 0;
2048
2049err:
Mark Brown5b307622009-10-13 13:06:49 +01002050 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002051 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002052 regulator_disable(consumers[i].consumer);
2053
2054 return ret;
2055}
2056EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2057
2058/**
2059 * regulator_bulk_disable - disable multiple regulator consumers
2060 *
2061 * @num_consumers: Number of consumers
2062 * @consumers: Consumer data; clients are stored here.
2063 * @return 0 on success, an errno on failure
2064 *
2065 * This convenience API allows consumers to disable multiple regulator
2066 * clients in a single API call. If any consumers cannot be enabled
2067 * then any others that were disabled will be disabled again prior to
2068 * return.
2069 */
2070int regulator_bulk_disable(int num_consumers,
2071 struct regulator_bulk_data *consumers)
2072{
2073 int i;
2074 int ret;
2075
2076 for (i = 0; i < num_consumers; i++) {
2077 ret = regulator_disable(consumers[i].consumer);
2078 if (ret != 0)
2079 goto err;
2080 }
2081
2082 return 0;
2083
2084err:
Mark Brown5b307622009-10-13 13:06:49 +01002085 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2086 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002087 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002088 regulator_enable(consumers[i].consumer);
2089
2090 return ret;
2091}
2092EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2093
2094/**
2095 * regulator_bulk_free - free multiple regulator consumers
2096 *
2097 * @num_consumers: Number of consumers
2098 * @consumers: Consumer data; clients are stored here.
2099 *
2100 * This convenience API allows consumers to free multiple regulator
2101 * clients in a single API call.
2102 */
2103void regulator_bulk_free(int num_consumers,
2104 struct regulator_bulk_data *consumers)
2105{
2106 int i;
2107
2108 for (i = 0; i < num_consumers; i++) {
2109 regulator_put(consumers[i].consumer);
2110 consumers[i].consumer = NULL;
2111 }
2112}
2113EXPORT_SYMBOL_GPL(regulator_bulk_free);
2114
2115/**
2116 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002117 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002118 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002119 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002120 *
2121 * Called by regulator drivers to notify clients a regulator event has
2122 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002123 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002124 */
2125int regulator_notifier_call_chain(struct regulator_dev *rdev,
2126 unsigned long event, void *data)
2127{
2128 _notifier_call_chain(rdev, event, data);
2129 return NOTIFY_DONE;
2130
2131}
2132EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2133
Mark Brownbe721972009-08-04 20:09:52 +02002134/**
2135 * regulator_mode_to_status - convert a regulator mode into a status
2136 *
2137 * @mode: Mode to convert
2138 *
2139 * Convert a regulator mode into a status.
2140 */
2141int regulator_mode_to_status(unsigned int mode)
2142{
2143 switch (mode) {
2144 case REGULATOR_MODE_FAST:
2145 return REGULATOR_STATUS_FAST;
2146 case REGULATOR_MODE_NORMAL:
2147 return REGULATOR_STATUS_NORMAL;
2148 case REGULATOR_MODE_IDLE:
2149 return REGULATOR_STATUS_IDLE;
2150 case REGULATOR_STATUS_STANDBY:
2151 return REGULATOR_STATUS_STANDBY;
2152 default:
2153 return 0;
2154 }
2155}
2156EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2157
David Brownell7ad68e22008-11-11 17:39:02 -08002158/*
2159 * To avoid cluttering sysfs (and memory) with useless state, only
2160 * create attributes that can be meaningfully displayed.
2161 */
2162static int add_regulator_attributes(struct regulator_dev *rdev)
2163{
2164 struct device *dev = &rdev->dev;
2165 struct regulator_ops *ops = rdev->desc->ops;
2166 int status = 0;
2167
2168 /* some attributes need specific methods to be displayed */
2169 if (ops->get_voltage) {
2170 status = device_create_file(dev, &dev_attr_microvolts);
2171 if (status < 0)
2172 return status;
2173 }
2174 if (ops->get_current_limit) {
2175 status = device_create_file(dev, &dev_attr_microamps);
2176 if (status < 0)
2177 return status;
2178 }
2179 if (ops->get_mode) {
2180 status = device_create_file(dev, &dev_attr_opmode);
2181 if (status < 0)
2182 return status;
2183 }
2184 if (ops->is_enabled) {
2185 status = device_create_file(dev, &dev_attr_state);
2186 if (status < 0)
2187 return status;
2188 }
David Brownell853116a2009-01-14 23:03:17 -08002189 if (ops->get_status) {
2190 status = device_create_file(dev, &dev_attr_status);
2191 if (status < 0)
2192 return status;
2193 }
David Brownell7ad68e22008-11-11 17:39:02 -08002194
2195 /* some attributes are type-specific */
2196 if (rdev->desc->type == REGULATOR_CURRENT) {
2197 status = device_create_file(dev, &dev_attr_requested_microamps);
2198 if (status < 0)
2199 return status;
2200 }
2201
2202 /* all the other attributes exist to support constraints;
2203 * don't show them if there are no constraints, or if the
2204 * relevant supporting methods are missing.
2205 */
2206 if (!rdev->constraints)
2207 return status;
2208
2209 /* constraints need specific supporting methods */
2210 if (ops->set_voltage) {
2211 status = device_create_file(dev, &dev_attr_min_microvolts);
2212 if (status < 0)
2213 return status;
2214 status = device_create_file(dev, &dev_attr_max_microvolts);
2215 if (status < 0)
2216 return status;
2217 }
2218 if (ops->set_current_limit) {
2219 status = device_create_file(dev, &dev_attr_min_microamps);
2220 if (status < 0)
2221 return status;
2222 status = device_create_file(dev, &dev_attr_max_microamps);
2223 if (status < 0)
2224 return status;
2225 }
2226
2227 /* suspend mode constraints need multiple supporting methods */
2228 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2229 return status;
2230
2231 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2232 if (status < 0)
2233 return status;
2234 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2235 if (status < 0)
2236 return status;
2237 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2238 if (status < 0)
2239 return status;
2240
2241 if (ops->set_suspend_voltage) {
2242 status = device_create_file(dev,
2243 &dev_attr_suspend_standby_microvolts);
2244 if (status < 0)
2245 return status;
2246 status = device_create_file(dev,
2247 &dev_attr_suspend_mem_microvolts);
2248 if (status < 0)
2249 return status;
2250 status = device_create_file(dev,
2251 &dev_attr_suspend_disk_microvolts);
2252 if (status < 0)
2253 return status;
2254 }
2255
2256 if (ops->set_suspend_mode) {
2257 status = device_create_file(dev,
2258 &dev_attr_suspend_standby_mode);
2259 if (status < 0)
2260 return status;
2261 status = device_create_file(dev,
2262 &dev_attr_suspend_mem_mode);
2263 if (status < 0)
2264 return status;
2265 status = device_create_file(dev,
2266 &dev_attr_suspend_disk_mode);
2267 if (status < 0)
2268 return status;
2269 }
2270
2271 return status;
2272}
2273
Liam Girdwood414c70c2008-04-30 15:59:04 +01002274/**
2275 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002276 * @regulator_desc: regulator to register
2277 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002278 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002279 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280 *
2281 * Called by regulator drivers to register a regulator.
2282 * Returns 0 on success.
2283 */
2284struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002285 struct device *dev, struct regulator_init_data *init_data,
2286 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002287{
2288 static atomic_t regulator_no = ATOMIC_INIT(0);
2289 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002290 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291
2292 if (regulator_desc == NULL)
2293 return ERR_PTR(-EINVAL);
2294
2295 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2296 return ERR_PTR(-EINVAL);
2297
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002298 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2299 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002300 return ERR_PTR(-EINVAL);
2301
Mark Brown46fabe1e2008-09-09 16:21:18 +01002302 if (!init_data)
2303 return ERR_PTR(-EINVAL);
2304
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2306 if (rdev == NULL)
2307 return ERR_PTR(-ENOMEM);
2308
2309 mutex_lock(&regulator_list_mutex);
2310
2311 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002312 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002313 rdev->owner = regulator_desc->owner;
2314 rdev->desc = regulator_desc;
2315 INIT_LIST_HEAD(&rdev->consumer_list);
2316 INIT_LIST_HEAD(&rdev->supply_list);
2317 INIT_LIST_HEAD(&rdev->list);
2318 INIT_LIST_HEAD(&rdev->slist);
2319 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2320
Liam Girdwooda5766f12008-10-10 13:22:20 +01002321 /* preform any regulator specific init */
2322 if (init_data->regulator_init) {
2323 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002324 if (ret < 0)
2325 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002326 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327
Liam Girdwooda5766f12008-10-10 13:22:20 +01002328 /* register with sysfs */
2329 rdev->dev.class = &regulator_class;
2330 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002331 dev_set_name(&rdev->dev, "regulator.%d",
2332 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002333 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002334 if (ret != 0) {
2335 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002336 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002337 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002338
2339 dev_set_drvdata(&rdev->dev, rdev);
2340
Mike Rapoport74f544c2008-11-25 14:53:53 +02002341 /* set regulator constraints */
2342 ret = set_machine_constraints(rdev, &init_data->constraints);
2343 if (ret < 0)
2344 goto scrub;
2345
David Brownell7ad68e22008-11-11 17:39:02 -08002346 /* add attributes supported by this regulator */
2347 ret = add_regulator_attributes(rdev);
2348 if (ret < 0)
2349 goto scrub;
2350
Liam Girdwooda5766f12008-10-10 13:22:20 +01002351 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002352 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2353 dev_err(dev,
2354 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002355 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002356 goto scrub;
2357 }
2358
2359 if (init_data->supply_regulator) {
2360 struct regulator_dev *r;
2361 int found = 0;
2362
2363 list_for_each_entry(r, &regulator_list, list) {
2364 if (strcmp(rdev_get_name(r),
2365 init_data->supply_regulator) == 0) {
2366 found = 1;
2367 break;
2368 }
2369 }
2370
2371 if (!found) {
2372 dev_err(dev, "Failed to find supply %s\n",
2373 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002374 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002375 goto scrub;
2376 }
2377
2378 ret = set_supply(rdev, r);
2379 if (ret < 0)
2380 goto scrub;
2381 }
2382
Liam Girdwooda5766f12008-10-10 13:22:20 +01002383 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002384 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002385 ret = set_supply(rdev,
2386 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002387 if (ret < 0)
2388 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002389 }
2390
2391 /* add consumers devices */
2392 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2393 ret = set_consumer_device_supply(rdev,
2394 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002395 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002396 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002397 if (ret < 0)
2398 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002399 }
2400
2401 list_add(&rdev->list, &regulator_list);
2402out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002403 mutex_unlock(&regulator_list_mutex);
2404 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002405
Jani Nikulad4033b52010-04-29 10:55:11 +03002406unset_supplies:
2407 unset_regulator_supplies(rdev);
2408
David Brownell4fca9542008-11-11 17:38:53 -08002409scrub:
2410 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002411 /* device core frees rdev */
2412 rdev = ERR_PTR(ret);
2413 goto out;
2414
David Brownell4fca9542008-11-11 17:38:53 -08002415clean:
2416 kfree(rdev);
2417 rdev = ERR_PTR(ret);
2418 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002419}
2420EXPORT_SYMBOL_GPL(regulator_register);
2421
2422/**
2423 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002424 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002425 *
2426 * Called by regulator drivers to unregister a regulator.
2427 */
2428void regulator_unregister(struct regulator_dev *rdev)
2429{
2430 if (rdev == NULL)
2431 return;
2432
2433 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002434 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002435 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002436 list_del(&rdev->list);
2437 if (rdev->supply)
2438 sysfs_remove_link(&rdev->dev.kobj, "supply");
2439 device_unregister(&rdev->dev);
2440 mutex_unlock(&regulator_list_mutex);
2441}
2442EXPORT_SYMBOL_GPL(regulator_unregister);
2443
2444/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002445 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002446 * @state: system suspend state
2447 *
2448 * Configure each regulator with it's suspend operating parameters for state.
2449 * This will usually be called by machine suspend code prior to supending.
2450 */
2451int regulator_suspend_prepare(suspend_state_t state)
2452{
2453 struct regulator_dev *rdev;
2454 int ret = 0;
2455
2456 /* ON is handled by regulator active state */
2457 if (state == PM_SUSPEND_ON)
2458 return -EINVAL;
2459
2460 mutex_lock(&regulator_list_mutex);
2461 list_for_each_entry(rdev, &regulator_list, list) {
2462
2463 mutex_lock(&rdev->mutex);
2464 ret = suspend_prepare(rdev, state);
2465 mutex_unlock(&rdev->mutex);
2466
2467 if (ret < 0) {
2468 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002469 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002470 goto out;
2471 }
2472 }
2473out:
2474 mutex_unlock(&regulator_list_mutex);
2475 return ret;
2476}
2477EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2478
2479/**
Mark Brownca725562009-03-16 19:36:34 +00002480 * regulator_has_full_constraints - the system has fully specified constraints
2481 *
2482 * Calling this function will cause the regulator API to disable all
2483 * regulators which have a zero use count and don't have an always_on
2484 * constraint in a late_initcall.
2485 *
2486 * The intention is that this will become the default behaviour in a
2487 * future kernel release so users are encouraged to use this facility
2488 * now.
2489 */
2490void regulator_has_full_constraints(void)
2491{
2492 has_full_constraints = 1;
2493}
2494EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2495
2496/**
Mark Brown688fe992010-10-05 19:18:32 -07002497 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2498 *
2499 * Calling this function will cause the regulator API to provide a
2500 * dummy regulator to consumers if no physical regulator is found,
2501 * allowing most consumers to proceed as though a regulator were
2502 * configured. This allows systems such as those with software
2503 * controllable regulators for the CPU core only to be brought up more
2504 * readily.
2505 */
2506void regulator_use_dummy_regulator(void)
2507{
2508 board_wants_dummy_regulator = true;
2509}
2510EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2511
2512/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002513 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002514 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002515 *
2516 * Get rdev regulator driver private data. This call can be used in the
2517 * regulator driver context.
2518 */
2519void *rdev_get_drvdata(struct regulator_dev *rdev)
2520{
2521 return rdev->reg_data;
2522}
2523EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2524
2525/**
2526 * regulator_get_drvdata - get regulator driver data
2527 * @regulator: regulator
2528 *
2529 * Get regulator driver private data. This call can be used in the consumer
2530 * driver context when non API regulator specific functions need to be called.
2531 */
2532void *regulator_get_drvdata(struct regulator *regulator)
2533{
2534 return regulator->rdev->reg_data;
2535}
2536EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2537
2538/**
2539 * regulator_set_drvdata - set regulator driver data
2540 * @regulator: regulator
2541 * @data: data
2542 */
2543void regulator_set_drvdata(struct regulator *regulator, void *data)
2544{
2545 regulator->rdev->reg_data = data;
2546}
2547EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2548
2549/**
2550 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002551 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002552 */
2553int rdev_get_id(struct regulator_dev *rdev)
2554{
2555 return rdev->desc->id;
2556}
2557EXPORT_SYMBOL_GPL(rdev_get_id);
2558
Liam Girdwooda5766f12008-10-10 13:22:20 +01002559struct device *rdev_get_dev(struct regulator_dev *rdev)
2560{
2561 return &rdev->dev;
2562}
2563EXPORT_SYMBOL_GPL(rdev_get_dev);
2564
2565void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2566{
2567 return reg_init_data->driver_data;
2568}
2569EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2570
Liam Girdwood414c70c2008-04-30 15:59:04 +01002571static int __init regulator_init(void)
2572{
Mark Brown34abbd62010-02-12 10:18:08 +00002573 int ret;
2574
Liam Girdwood414c70c2008-04-30 15:59:04 +01002575 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002576
2577 ret = class_register(&regulator_class);
2578
2579 regulator_dummy_init();
2580
2581 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002582}
2583
2584/* init early to allow our consumers to complete system booting */
2585core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002586
2587static int __init regulator_init_complete(void)
2588{
2589 struct regulator_dev *rdev;
2590 struct regulator_ops *ops;
2591 struct regulation_constraints *c;
2592 int enabled, ret;
2593 const char *name;
2594
2595 mutex_lock(&regulator_list_mutex);
2596
2597 /* If we have a full configuration then disable any regulators
2598 * which are not in use or always_on. This will become the
2599 * default behaviour in the future.
2600 */
2601 list_for_each_entry(rdev, &regulator_list, list) {
2602 ops = rdev->desc->ops;
2603 c = rdev->constraints;
2604
Mark Brown1083c392009-10-22 16:31:32 +01002605 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002606
Mark Brownf25e0b42009-08-03 18:49:55 +01002607 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002608 continue;
2609
2610 mutex_lock(&rdev->mutex);
2611
2612 if (rdev->use_count)
2613 goto unlock;
2614
2615 /* If we can't read the status assume it's on. */
2616 if (ops->is_enabled)
2617 enabled = ops->is_enabled(rdev);
2618 else
2619 enabled = 1;
2620
2621 if (!enabled)
2622 goto unlock;
2623
2624 if (has_full_constraints) {
2625 /* We log since this may kill the system if it
2626 * goes wrong. */
2627 printk(KERN_INFO "%s: disabling %s\n",
2628 __func__, name);
2629 ret = ops->disable(rdev);
2630 if (ret != 0) {
2631 printk(KERN_ERR
2632 "%s: couldn't disable %s: %d\n",
2633 __func__, name, ret);
2634 }
2635 } else {
2636 /* The intention is that in future we will
2637 * assume that full constraints are provided
2638 * so warn even if we aren't going to do
2639 * anything here.
2640 */
2641 printk(KERN_WARNING
2642 "%s: incomplete constraints, leaving %s on\n",
2643 __func__, name);
2644 }
2645
2646unlock:
2647 mutex_unlock(&rdev->mutex);
2648 }
2649
2650 mutex_unlock(&regulator_list_mutex);
2651
2652 return 0;
2653}
2654late_initcall(regulator_init_complete);