blob: 7c0906367239a1ea1fa5c2d9702d2ff9f9e7d85b [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
Daniel Walkerc5e28ed72010-11-17 15:30:27 -080016#define pr_fmt(fmt) "%s:" fmt, __func__
17
Liam Girdwood414c70c2008-04-30 15:59:04 +010018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010022#include <linux/err.h>
23#include <linux/mutex.h>
24#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000025#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010026#include <linux/regulator/consumer.h>
27#include <linux/regulator/driver.h>
28#include <linux/regulator/machine.h>
29
Mark Brown02fa3ec2010-11-10 14:38:30 +000030#define CREATE_TRACE_POINTS
31#include <trace/events/regulator.h>
32
Mark Brown34abbd62010-02-12 10:18:08 +000033#include "dummy.h"
34
Liam Girdwood414c70c2008-04-30 15:59:04 +010035#define REGULATOR_VERSION "0.5"
36
37static DEFINE_MUTEX(regulator_list_mutex);
38static LIST_HEAD(regulator_list);
39static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000040static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070041static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010042
Mark Brown8dc53902008-12-31 12:52:40 +000043/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010044 * struct regulator_map
45 *
46 * Used to provide symbolic supply names to devices.
47 */
48struct regulator_map {
49 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010050 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010051 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010052 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010053};
54
Liam Girdwood414c70c2008-04-30 15:59:04 +010055/*
56 * struct regulator
57 *
58 * One for each consumer device.
59 */
60struct regulator {
61 struct device *dev;
62 struct list_head list;
63 int uA_load;
64 int min_uV;
65 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010066 char *supply_name;
67 struct device_attribute dev_attr;
68 struct regulator_dev *rdev;
69};
70
71static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050072static int _regulator_disable(struct regulator_dev *rdev,
73 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010074static int _regulator_get_voltage(struct regulator_dev *rdev);
75static int _regulator_get_current_limit(struct regulator_dev *rdev);
76static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
77static void _notifier_call_chain(struct regulator_dev *rdev,
78 unsigned long event, void *data);
79
Mark Brown1083c392009-10-22 16:31:32 +010080static const char *rdev_get_name(struct regulator_dev *rdev)
81{
82 if (rdev->constraints && rdev->constraints->name)
83 return rdev->constraints->name;
84 else if (rdev->desc->name)
85 return rdev->desc->name;
86 else
87 return "";
88}
89
Liam Girdwood414c70c2008-04-30 15:59:04 +010090/* gets the regulator for a given consumer device */
91static struct regulator *get_device_regulator(struct device *dev)
92{
93 struct regulator *regulator = NULL;
94 struct regulator_dev *rdev;
95
96 mutex_lock(&regulator_list_mutex);
97 list_for_each_entry(rdev, &regulator_list, list) {
98 mutex_lock(&rdev->mutex);
99 list_for_each_entry(regulator, &rdev->consumer_list, list) {
100 if (regulator->dev == dev) {
101 mutex_unlock(&rdev->mutex);
102 mutex_unlock(&regulator_list_mutex);
103 return regulator;
104 }
105 }
106 mutex_unlock(&rdev->mutex);
107 }
108 mutex_unlock(&regulator_list_mutex);
109 return NULL;
110}
111
112/* Platform voltage constraint check */
113static int regulator_check_voltage(struct regulator_dev *rdev,
114 int *min_uV, int *max_uV)
115{
116 BUG_ON(*min_uV > *max_uV);
117
118 if (!rdev->constraints) {
119 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100120 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100121 return -ENODEV;
122 }
123 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
124 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100125 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100126 return -EPERM;
127 }
128
129 if (*max_uV > rdev->constraints->max_uV)
130 *max_uV = rdev->constraints->max_uV;
131 if (*min_uV < rdev->constraints->min_uV)
132 *min_uV = rdev->constraints->min_uV;
133
134 if (*min_uV > *max_uV)
135 return -EINVAL;
136
137 return 0;
138}
139
140/* current constraint check */
141static int regulator_check_current_limit(struct regulator_dev *rdev,
142 int *min_uA, int *max_uA)
143{
144 BUG_ON(*min_uA > *max_uA);
145
146 if (!rdev->constraints) {
147 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100148 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100149 return -ENODEV;
150 }
151 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
152 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100153 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100154 return -EPERM;
155 }
156
157 if (*max_uA > rdev->constraints->max_uA)
158 *max_uA = rdev->constraints->max_uA;
159 if (*min_uA < rdev->constraints->min_uA)
160 *min_uA = rdev->constraints->min_uA;
161
162 if (*min_uA > *max_uA)
163 return -EINVAL;
164
165 return 0;
166}
167
168/* operating mode constraint check */
169static int regulator_check_mode(struct regulator_dev *rdev, int mode)
170{
David Brownelle5735202008-11-16 11:46:56 -0800171 switch (mode) {
172 case REGULATOR_MODE_FAST:
173 case REGULATOR_MODE_NORMAL:
174 case REGULATOR_MODE_IDLE:
175 case REGULATOR_MODE_STANDBY:
176 break;
177 default:
178 return -EINVAL;
179 }
180
Liam Girdwood414c70c2008-04-30 15:59:04 +0100181 if (!rdev->constraints) {
182 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100183 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100184 return -ENODEV;
185 }
186 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
187 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100188 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100189 return -EPERM;
190 }
191 if (!(rdev->constraints->valid_modes_mask & mode)) {
192 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100193 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100194 return -EINVAL;
195 }
196 return 0;
197}
198
199/* dynamic regulator mode switching constraint check */
200static int regulator_check_drms(struct regulator_dev *rdev)
201{
202 if (!rdev->constraints) {
203 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100204 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100205 return -ENODEV;
206 }
207 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
208 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100209 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100210 return -EPERM;
211 }
212 return 0;
213}
214
215static ssize_t device_requested_uA_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
217{
218 struct regulator *regulator;
219
220 regulator = get_device_regulator(dev);
221 if (regulator == NULL)
222 return 0;
223
224 return sprintf(buf, "%d\n", regulator->uA_load);
225}
226
227static ssize_t regulator_uV_show(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100230 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100231 ssize_t ret;
232
233 mutex_lock(&rdev->mutex);
234 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
235 mutex_unlock(&rdev->mutex);
236
237 return ret;
238}
David Brownell7ad68e22008-11-11 17:39:02 -0800239static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100240
241static ssize_t regulator_uA_show(struct device *dev,
242 struct device_attribute *attr, char *buf)
243{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100244 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100245
246 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
247}
David Brownell7ad68e22008-11-11 17:39:02 -0800248static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100249
Mark Brownbc558a62008-10-10 15:33:20 +0100250static ssize_t regulator_name_show(struct device *dev,
251 struct device_attribute *attr, char *buf)
252{
253 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100254
Mark Brown1083c392009-10-22 16:31:32 +0100255 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100256}
257
David Brownell4fca9542008-11-11 17:38:53 -0800258static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100259{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100260 switch (mode) {
261 case REGULATOR_MODE_FAST:
262 return sprintf(buf, "fast\n");
263 case REGULATOR_MODE_NORMAL:
264 return sprintf(buf, "normal\n");
265 case REGULATOR_MODE_IDLE:
266 return sprintf(buf, "idle\n");
267 case REGULATOR_MODE_STANDBY:
268 return sprintf(buf, "standby\n");
269 }
270 return sprintf(buf, "unknown\n");
271}
272
David Brownell4fca9542008-11-11 17:38:53 -0800273static ssize_t regulator_opmode_show(struct device *dev,
274 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100276 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100277
David Brownell4fca9542008-11-11 17:38:53 -0800278 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
279}
David Brownell7ad68e22008-11-11 17:39:02 -0800280static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800281
282static ssize_t regulator_print_state(char *buf, int state)
283{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100284 if (state > 0)
285 return sprintf(buf, "enabled\n");
286 else if (state == 0)
287 return sprintf(buf, "disabled\n");
288 else
289 return sprintf(buf, "unknown\n");
290}
291
David Brownell4fca9542008-11-11 17:38:53 -0800292static ssize_t regulator_state_show(struct device *dev,
293 struct device_attribute *attr, char *buf)
294{
295 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100296 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800297
Mark Brown93325462009-08-03 18:49:56 +0100298 mutex_lock(&rdev->mutex);
299 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
300 mutex_unlock(&rdev->mutex);
301
302 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800303}
David Brownell7ad68e22008-11-11 17:39:02 -0800304static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800305
David Brownell853116a2009-01-14 23:03:17 -0800306static ssize_t regulator_status_show(struct device *dev,
307 struct device_attribute *attr, char *buf)
308{
309 struct regulator_dev *rdev = dev_get_drvdata(dev);
310 int status;
311 char *label;
312
313 status = rdev->desc->ops->get_status(rdev);
314 if (status < 0)
315 return status;
316
317 switch (status) {
318 case REGULATOR_STATUS_OFF:
319 label = "off";
320 break;
321 case REGULATOR_STATUS_ON:
322 label = "on";
323 break;
324 case REGULATOR_STATUS_ERROR:
325 label = "error";
326 break;
327 case REGULATOR_STATUS_FAST:
328 label = "fast";
329 break;
330 case REGULATOR_STATUS_NORMAL:
331 label = "normal";
332 break;
333 case REGULATOR_STATUS_IDLE:
334 label = "idle";
335 break;
336 case REGULATOR_STATUS_STANDBY:
337 label = "standby";
338 break;
339 default:
340 return -ERANGE;
341 }
342
343 return sprintf(buf, "%s\n", label);
344}
345static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
346
Liam Girdwood414c70c2008-04-30 15:59:04 +0100347static ssize_t regulator_min_uA_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100350 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100351
352 if (!rdev->constraints)
353 return sprintf(buf, "constraint not defined\n");
354
355 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
356}
David Brownell7ad68e22008-11-11 17:39:02 -0800357static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100358
359static ssize_t regulator_max_uA_show(struct device *dev,
360 struct device_attribute *attr, char *buf)
361{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100362 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100363
364 if (!rdev->constraints)
365 return sprintf(buf, "constraint not defined\n");
366
367 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
368}
David Brownell7ad68e22008-11-11 17:39:02 -0800369static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100370
371static ssize_t regulator_min_uV_show(struct device *dev,
372 struct device_attribute *attr, char *buf)
373{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100374 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100375
376 if (!rdev->constraints)
377 return sprintf(buf, "constraint not defined\n");
378
379 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
380}
David Brownell7ad68e22008-11-11 17:39:02 -0800381static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100382
383static ssize_t regulator_max_uV_show(struct device *dev,
384 struct device_attribute *attr, char *buf)
385{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100386 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100387
388 if (!rdev->constraints)
389 return sprintf(buf, "constraint not defined\n");
390
391 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
392}
David Brownell7ad68e22008-11-11 17:39:02 -0800393static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100394
395static ssize_t regulator_total_uA_show(struct device *dev,
396 struct device_attribute *attr, char *buf)
397{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100398 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100399 struct regulator *regulator;
400 int uA = 0;
401
402 mutex_lock(&rdev->mutex);
403 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100404 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100405 mutex_unlock(&rdev->mutex);
406 return sprintf(buf, "%d\n", uA);
407}
David Brownell7ad68e22008-11-11 17:39:02 -0800408static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100409
410static ssize_t regulator_num_users_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100413 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100414 return sprintf(buf, "%d\n", rdev->use_count);
415}
416
417static ssize_t regulator_type_show(struct device *dev,
418 struct device_attribute *attr, char *buf)
419{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100420 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100421
422 switch (rdev->desc->type) {
423 case REGULATOR_VOLTAGE:
424 return sprintf(buf, "voltage\n");
425 case REGULATOR_CURRENT:
426 return sprintf(buf, "current\n");
427 }
428 return sprintf(buf, "unknown\n");
429}
430
431static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
432 struct device_attribute *attr, char *buf)
433{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100434 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100435
Liam Girdwood414c70c2008-04-30 15:59:04 +0100436 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
437}
David Brownell7ad68e22008-11-11 17:39:02 -0800438static DEVICE_ATTR(suspend_mem_microvolts, 0444,
439 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100440
441static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
442 struct device_attribute *attr, char *buf)
443{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100444 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100445
Liam Girdwood414c70c2008-04-30 15:59:04 +0100446 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
447}
David Brownell7ad68e22008-11-11 17:39:02 -0800448static DEVICE_ATTR(suspend_disk_microvolts, 0444,
449 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450
451static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
452 struct device_attribute *attr, char *buf)
453{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100454 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455
Liam Girdwood414c70c2008-04-30 15:59:04 +0100456 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
457}
David Brownell7ad68e22008-11-11 17:39:02 -0800458static DEVICE_ATTR(suspend_standby_microvolts, 0444,
459 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100460
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100464 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100465
David Brownell4fca9542008-11-11 17:38:53 -0800466 return regulator_print_opmode(buf,
467 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100468}
David Brownell7ad68e22008-11-11 17:39:02 -0800469static DEVICE_ATTR(suspend_mem_mode, 0444,
470 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100471
472static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100475 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100476
David Brownell4fca9542008-11-11 17:38:53 -0800477 return regulator_print_opmode(buf,
478 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100479}
David Brownell7ad68e22008-11-11 17:39:02 -0800480static DEVICE_ATTR(suspend_disk_mode, 0444,
481 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100482
483static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100486 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100487
David Brownell4fca9542008-11-11 17:38:53 -0800488 return regulator_print_opmode(buf,
489 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100490}
David Brownell7ad68e22008-11-11 17:39:02 -0800491static DEVICE_ATTR(suspend_standby_mode, 0444,
492 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100493
494static ssize_t regulator_suspend_mem_state_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100497 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100498
David Brownell4fca9542008-11-11 17:38:53 -0800499 return regulator_print_state(buf,
500 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100501}
David Brownell7ad68e22008-11-11 17:39:02 -0800502static DEVICE_ATTR(suspend_mem_state, 0444,
503 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100504
505static ssize_t regulator_suspend_disk_state_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100508 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100509
David Brownell4fca9542008-11-11 17:38:53 -0800510 return regulator_print_state(buf,
511 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100512}
David Brownell7ad68e22008-11-11 17:39:02 -0800513static DEVICE_ATTR(suspend_disk_state, 0444,
514 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100515
516static ssize_t regulator_suspend_standby_state_show(struct device *dev,
517 struct device_attribute *attr, char *buf)
518{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100519 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100520
David Brownell4fca9542008-11-11 17:38:53 -0800521 return regulator_print_state(buf,
522 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523}
David Brownell7ad68e22008-11-11 17:39:02 -0800524static DEVICE_ATTR(suspend_standby_state, 0444,
525 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100526
David Brownell7ad68e22008-11-11 17:39:02 -0800527
528/*
529 * These are the only attributes are present for all regulators.
530 * Other attributes are a function of regulator functionality.
531 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100533 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
535 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100536 __ATTR_NULL,
537};
538
539static void regulator_dev_release(struct device *dev)
540{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100541 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100542 kfree(rdev);
543}
544
545static struct class regulator_class = {
546 .name = "regulator",
547 .dev_release = regulator_dev_release,
548 .dev_attrs = regulator_dev_attrs,
549};
550
551/* Calculate the new optimum regulator operating mode based on the new total
552 * consumer load. All locks held by caller */
553static void drms_uA_update(struct regulator_dev *rdev)
554{
555 struct regulator *sibling;
556 int current_uA = 0, output_uV, input_uV, err;
557 unsigned int mode;
558
559 err = regulator_check_drms(rdev);
560 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300561 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
562 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100563
564 /* get output voltage */
565 output_uV = rdev->desc->ops->get_voltage(rdev);
566 if (output_uV <= 0)
567 return;
568
569 /* get input voltage */
570 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
571 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
572 else
573 input_uV = rdev->constraints->input_uV;
574 if (input_uV <= 0)
575 return;
576
577 /* calc total requested load */
578 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100579 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100580
581 /* now get the optimum mode for our new total regulator load */
582 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
583 output_uV, current_uA);
584
585 /* check the new mode is allowed */
586 err = regulator_check_mode(rdev, mode);
587 if (err == 0)
588 rdev->desc->ops->set_mode(rdev, mode);
589}
590
591static int suspend_set_state(struct regulator_dev *rdev,
592 struct regulator_state *rstate)
593{
594 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100595 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100596
Mark Brown638f85c2009-10-22 16:31:33 +0100597 can_set_state = rdev->desc->ops->set_suspend_enable &&
598 rdev->desc->ops->set_suspend_disable;
599
600 /* If we have no suspend mode configration don't set anything;
601 * only warn if the driver actually makes the suspend mode
602 * configurable.
603 */
604 if (!rstate->enabled && !rstate->disabled) {
605 if (can_set_state)
606 printk(KERN_WARNING "%s: No configuration for %s\n",
607 __func__, rdev_get_name(rdev));
608 return 0;
609 }
610
611 if (rstate->enabled && rstate->disabled) {
612 printk(KERN_ERR "%s: invalid configuration for %s\n",
613 __func__, rdev_get_name(rdev));
614 return -EINVAL;
615 }
616
617 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100618 printk(KERN_ERR "%s: no way to set suspend state\n",
619 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100621 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100622
623 if (rstate->enabled)
624 ret = rdev->desc->ops->set_suspend_enable(rdev);
625 else
626 ret = rdev->desc->ops->set_suspend_disable(rdev);
627 if (ret < 0) {
628 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
629 return ret;
630 }
631
632 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
633 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
634 if (ret < 0) {
635 printk(KERN_ERR "%s: failed to set voltage\n",
636 __func__);
637 return ret;
638 }
639 }
640
641 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
642 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
643 if (ret < 0) {
644 printk(KERN_ERR "%s: failed to set mode\n", __func__);
645 return ret;
646 }
647 }
648 return ret;
649}
650
651/* locks held by caller */
652static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
653{
654 if (!rdev->constraints)
655 return -EINVAL;
656
657 switch (state) {
658 case PM_SUSPEND_STANDBY:
659 return suspend_set_state(rdev,
660 &rdev->constraints->state_standby);
661 case PM_SUSPEND_MEM:
662 return suspend_set_state(rdev,
663 &rdev->constraints->state_mem);
664 case PM_SUSPEND_MAX:
665 return suspend_set_state(rdev,
666 &rdev->constraints->state_disk);
667 default:
668 return -EINVAL;
669 }
670}
671
672static void print_constraints(struct regulator_dev *rdev)
673{
674 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000675 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100676 int count = 0;
677 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678
Mark Brown8f031b42009-10-22 16:31:31 +0100679 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100680 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100681 count += sprintf(buf + count, "%d mV ",
682 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100683 else
Mark Brown8f031b42009-10-22 16:31:31 +0100684 count += sprintf(buf + count, "%d <--> %d mV ",
685 constraints->min_uV / 1000,
686 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100687 }
Mark Brown8f031b42009-10-22 16:31:31 +0100688
689 if (!constraints->min_uV ||
690 constraints->min_uV != constraints->max_uV) {
691 ret = _regulator_get_voltage(rdev);
692 if (ret > 0)
693 count += sprintf(buf + count, "at %d mV ", ret / 1000);
694 }
695
696 if (constraints->min_uA && constraints->max_uA) {
697 if (constraints->min_uA == constraints->max_uA)
698 count += sprintf(buf + count, "%d mA ",
699 constraints->min_uA / 1000);
700 else
701 count += sprintf(buf + count, "%d <--> %d mA ",
702 constraints->min_uA / 1000,
703 constraints->max_uA / 1000);
704 }
705
706 if (!constraints->min_uA ||
707 constraints->min_uA != constraints->max_uA) {
708 ret = _regulator_get_current_limit(rdev);
709 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400710 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100711 }
712
Liam Girdwood414c70c2008-04-30 15:59:04 +0100713 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
714 count += sprintf(buf + count, "fast ");
715 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
716 count += sprintf(buf + count, "normal ");
717 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
718 count += sprintf(buf + count, "idle ");
719 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
720 count += sprintf(buf + count, "standby");
721
Mark Brown1083c392009-10-22 16:31:32 +0100722 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100723}
724
Mark Browne79055d2009-10-19 15:53:50 +0100725static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100726 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100727{
728 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100729 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100730 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000731 unsigned selector;
Mark Brownaf5866c2009-10-22 16:31:30 +0100732
733 /* do we need to apply the constraint voltage */
734 if (rdev->constraints->apply_uV &&
735 rdev->constraints->min_uV == rdev->constraints->max_uV &&
736 ops->set_voltage) {
737 ret = ops->set_voltage(rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000738 rdev->constraints->min_uV,
739 rdev->constraints->max_uV,
740 &selector);
Mark Brownaf5866c2009-10-22 16:31:30 +0100741 if (ret < 0) {
742 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
743 __func__,
744 rdev->constraints->min_uV, name);
745 rdev->constraints = NULL;
746 return ret;
747 }
748 }
Mark Browne79055d2009-10-19 15:53:50 +0100749
750 /* constrain machine-level voltage specs to fit
751 * the actual range supported by this regulator.
752 */
753 if (ops->list_voltage && rdev->desc->n_voltages) {
754 int count = rdev->desc->n_voltages;
755 int i;
756 int min_uV = INT_MAX;
757 int max_uV = INT_MIN;
758 int cmin = constraints->min_uV;
759 int cmax = constraints->max_uV;
760
761 /* it's safe to autoconfigure fixed-voltage supplies
762 and the constraints are used by list_voltage. */
763 if (count == 1 && !cmin) {
764 cmin = 1;
765 cmax = INT_MAX;
766 constraints->min_uV = cmin;
767 constraints->max_uV = cmax;
768 }
769
770 /* voltage constraints are optional */
771 if ((cmin == 0) && (cmax == 0))
772 return 0;
773
774 /* else require explicit machine-level constraints */
775 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
Daniel Walkerc5e28ed72010-11-17 15:30:27 -0800776 pr_err("%s '%s' voltage constraints\n", "invalid",
777 name);
Mark Browne79055d2009-10-19 15:53:50 +0100778 return -EINVAL;
779 }
780
781 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
782 for (i = 0; i < count; i++) {
783 int value;
784
785 value = ops->list_voltage(rdev, i);
786 if (value <= 0)
787 continue;
788
789 /* maybe adjust [min_uV..max_uV] */
790 if (value >= cmin && value < min_uV)
791 min_uV = value;
792 if (value <= cmax && value > max_uV)
793 max_uV = value;
794 }
795
796 /* final: [min_uV..max_uV] valid iff constraints valid */
797 if (max_uV < min_uV) {
Daniel Walkerc5e28ed72010-11-17 15:30:27 -0800798 pr_err("%s '%s' voltage constraints\n", "unsupportable",
799 name);
Mark Browne79055d2009-10-19 15:53:50 +0100800 return -EINVAL;
801 }
802
803 /* use regulator's subset of machine constraints */
804 if (constraints->min_uV < min_uV) {
Daniel Walkerc5e28ed72010-11-17 15:30:27 -0800805 pr_debug("override '%s' %s, %d -> %d\n",
806 name, "min_uV",
807 constraints->min_uV, min_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100808 constraints->min_uV = min_uV;
809 }
810 if (constraints->max_uV > max_uV) {
Daniel Walkerc5e28ed72010-11-17 15:30:27 -0800811 pr_debug("override '%s' %s, %d -> %d\n",
812 name, "max_uV",
813 constraints->max_uV, max_uV);
Mark Browne79055d2009-10-19 15:53:50 +0100814 constraints->max_uV = max_uV;
815 }
816 }
817
818 return 0;
819}
820
Liam Girdwooda5766f12008-10-10 13:22:20 +0100821/**
822 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000823 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000824 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100825 *
826 * Allows platform initialisation code to define and constrain
827 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
828 * Constraints *must* be set by platform code in order for some
829 * regulator operations to proceed i.e. set_voltage, set_current_limit,
830 * set_mode.
831 */
832static int set_machine_constraints(struct regulator_dev *rdev,
833 struct regulation_constraints *constraints)
834{
835 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100836 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100837 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100838
Mark Brownaf5866c2009-10-22 16:31:30 +0100839 rdev->constraints = constraints;
840
Mark Brown1083c392009-10-22 16:31:32 +0100841 name = rdev_get_name(rdev);
842
843 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100844 if (ret != 0)
845 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800846
Liam Girdwooda5766f12008-10-10 13:22:20 +0100847 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100848 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100849 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100850 if (ret < 0) {
851 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
852 __func__, name);
853 rdev->constraints = NULL;
854 goto out;
855 }
856 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100857
Mark Browna3084662009-02-26 19:24:19 +0000858 if (constraints->initial_mode) {
859 if (!ops->set_mode) {
860 printk(KERN_ERR "%s: no set_mode operation for %s\n",
861 __func__, name);
862 ret = -EINVAL;
863 goto out;
864 }
865
866 ret = ops->set_mode(rdev, constraints->initial_mode);
867 if (ret < 0) {
868 printk(KERN_ERR
869 "%s: failed to set initial mode for %s: %d\n",
870 __func__, name, ret);
871 goto out;
872 }
873 }
874
Mark Browncacf90f2009-03-02 16:32:46 +0000875 /* If the constraints say the regulator should be on at this point
876 * and we have control then make sure it is enabled.
877 */
878 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100879 ret = ops->enable(rdev);
880 if (ret < 0) {
881 printk(KERN_ERR "%s: failed to enable %s\n",
882 __func__, name);
883 rdev->constraints = NULL;
884 goto out;
885 }
886 }
887
Liam Girdwooda5766f12008-10-10 13:22:20 +0100888 print_constraints(rdev);
889out:
890 return ret;
891}
892
893/**
894 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000895 * @rdev: regulator name
896 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100897 *
898 * Called by platform initialisation code to set the supply regulator for this
899 * regulator. This ensures that a regulators supply will also be enabled by the
900 * core if it's child is enabled.
901 */
902static int set_supply(struct regulator_dev *rdev,
903 struct regulator_dev *supply_rdev)
904{
905 int err;
906
907 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
908 "supply");
909 if (err) {
910 printk(KERN_ERR
911 "%s: could not add device link %s err %d\n",
912 __func__, supply_rdev->dev.kobj.name, err);
913 goto out;
914 }
915 rdev->supply = supply_rdev;
916 list_add(&rdev->slist, &supply_rdev->supply_list);
917out:
918 return err;
919}
920
921/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800922 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000923 * @rdev: regulator source
924 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100925 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000926 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100927 *
928 * Allows platform initialisation code to map physical regulator
929 * sources to symbolic names for supplies for use by devices. Devices
930 * should use these symbolic names to request regulators, avoiding the
931 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100932 *
933 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100934 */
935static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100936 struct device *consumer_dev, const char *consumer_dev_name,
937 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100938{
939 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100940 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100941
Mark Brown40f92442009-06-17 17:56:39 +0100942 if (consumer_dev && consumer_dev_name)
943 return -EINVAL;
944
945 if (!consumer_dev_name && consumer_dev)
946 consumer_dev_name = dev_name(consumer_dev);
947
Liam Girdwooda5766f12008-10-10 13:22:20 +0100948 if (supply == NULL)
949 return -EINVAL;
950
Mark Brown9ed20992009-07-21 16:00:26 +0100951 if (consumer_dev_name != NULL)
952 has_dev = 1;
953 else
954 has_dev = 0;
955
David Brownell6001e132008-12-31 12:54:19 +0000956 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300957 if (node->dev_name && consumer_dev_name) {
958 if (strcmp(node->dev_name, consumer_dev_name) != 0)
959 continue;
960 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000961 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300962 }
963
David Brownell6001e132008-12-31 12:54:19 +0000964 if (strcmp(node->supply, supply) != 0)
965 continue;
966
967 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
968 dev_name(&node->regulator->dev),
969 node->regulator->desc->name,
970 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100971 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000972 return -EBUSY;
973 }
974
Mark Brown9ed20992009-07-21 16:00:26 +0100975 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100976 if (node == NULL)
977 return -ENOMEM;
978
979 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100980 node->supply = supply;
981
Mark Brown9ed20992009-07-21 16:00:26 +0100982 if (has_dev) {
983 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
984 if (node->dev_name == NULL) {
985 kfree(node);
986 return -ENOMEM;
987 }
Mark Brown40f92442009-06-17 17:56:39 +0100988 }
989
Liam Girdwooda5766f12008-10-10 13:22:20 +0100990 list_add(&node->list, &regulator_map_list);
991 return 0;
992}
993
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200994static void unset_regulator_supplies(struct regulator_dev *rdev)
995{
996 struct regulator_map *node, *n;
997
998 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
999 if (rdev == node->regulator) {
1000 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +01001001 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001002 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001003 }
1004 }
1005}
1006
Liam Girdwood414c70c2008-04-30 15:59:04 +01001007#define REG_STR_SIZE 32
1008
1009static struct regulator *create_regulator(struct regulator_dev *rdev,
1010 struct device *dev,
1011 const char *supply_name)
1012{
1013 struct regulator *regulator;
1014 char buf[REG_STR_SIZE];
1015 int err, size;
1016
1017 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1018 if (regulator == NULL)
1019 return NULL;
1020
1021 mutex_lock(&rdev->mutex);
1022 regulator->rdev = rdev;
1023 list_add(&regulator->list, &rdev->consumer_list);
1024
1025 if (dev) {
1026 /* create a 'requested_microamps_name' sysfs entry */
1027 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1028 supply_name);
1029 if (size >= REG_STR_SIZE)
1030 goto overflow_err;
1031
1032 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001033 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001034 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1035 if (regulator->dev_attr.attr.name == NULL)
1036 goto attr_name_err;
1037
Liam Girdwood414c70c2008-04-30 15:59:04 +01001038 regulator->dev_attr.attr.mode = 0444;
1039 regulator->dev_attr.show = device_requested_uA_show;
1040 err = device_create_file(dev, &regulator->dev_attr);
1041 if (err < 0) {
1042 printk(KERN_WARNING "%s: could not add regulator_dev"
1043 " load sysfs\n", __func__);
1044 goto attr_name_err;
1045 }
1046
1047 /* also add a link to the device sysfs entry */
1048 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1049 dev->kobj.name, supply_name);
1050 if (size >= REG_STR_SIZE)
1051 goto attr_err;
1052
1053 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1054 if (regulator->supply_name == NULL)
1055 goto attr_err;
1056
1057 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1058 buf);
1059 if (err) {
1060 printk(KERN_WARNING
1061 "%s: could not add device link %s err %d\n",
1062 __func__, dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001063 goto link_name_err;
1064 }
1065 }
1066 mutex_unlock(&rdev->mutex);
1067 return regulator;
1068link_name_err:
1069 kfree(regulator->supply_name);
1070attr_err:
1071 device_remove_file(regulator->dev, &regulator->dev_attr);
1072attr_name_err:
1073 kfree(regulator->dev_attr.attr.name);
1074overflow_err:
1075 list_del(&regulator->list);
1076 kfree(regulator);
1077 mutex_unlock(&rdev->mutex);
1078 return NULL;
1079}
1080
Mark Brown31aae2b2009-12-21 12:21:52 +00001081static int _regulator_get_enable_time(struct regulator_dev *rdev)
1082{
1083 if (!rdev->desc->ops->enable_time)
1084 return 0;
1085 return rdev->desc->ops->enable_time(rdev);
1086}
1087
Mark Brown5ffbd132009-07-21 16:00:23 +01001088/* Internal regulator request function */
1089static struct regulator *_regulator_get(struct device *dev, const char *id,
1090 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001091{
1092 struct regulator_dev *rdev;
1093 struct regulator_map *map;
1094 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001095 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001096 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001097
1098 if (id == NULL) {
1099 printk(KERN_ERR "regulator: get() with no identifier\n");
1100 return regulator;
1101 }
1102
Mark Brown40f92442009-06-17 17:56:39 +01001103 if (dev)
1104 devname = dev_name(dev);
1105
Liam Girdwood414c70c2008-04-30 15:59:04 +01001106 mutex_lock(&regulator_list_mutex);
1107
1108 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001109 /* If the mapping has a device set up it must match */
1110 if (map->dev_name &&
1111 (!devname || strcmp(map->dev_name, devname)))
1112 continue;
1113
1114 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001115 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001117 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001118 }
Mark Brown34abbd62010-02-12 10:18:08 +00001119
Mark Brown688fe992010-10-05 19:18:32 -07001120 if (board_wants_dummy_regulator) {
1121 rdev = dummy_regulator_rdev;
1122 goto found;
1123 }
1124
Mark Brown34abbd62010-02-12 10:18:08 +00001125#ifdef CONFIG_REGULATOR_DUMMY
1126 if (!devname)
1127 devname = "deviceless";
1128
1129 /* If the board didn't flag that it was fully constrained then
1130 * substitute in a dummy regulator so consumers can continue.
1131 */
1132 if (!has_full_constraints) {
1133 pr_warning("%s supply %s not found, using dummy regulator\n",
1134 devname, id);
1135 rdev = dummy_regulator_rdev;
1136 goto found;
1137 }
1138#endif
1139
Liam Girdwood414c70c2008-04-30 15:59:04 +01001140 mutex_unlock(&regulator_list_mutex);
1141 return regulator;
1142
1143found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001144 if (rdev->exclusive) {
1145 regulator = ERR_PTR(-EPERM);
1146 goto out;
1147 }
1148
1149 if (exclusive && rdev->open_count) {
1150 regulator = ERR_PTR(-EBUSY);
1151 goto out;
1152 }
1153
Liam Girdwooda5766f12008-10-10 13:22:20 +01001154 if (!try_module_get(rdev->owner))
1155 goto out;
1156
Liam Girdwood414c70c2008-04-30 15:59:04 +01001157 regulator = create_regulator(rdev, dev, id);
1158 if (regulator == NULL) {
1159 regulator = ERR_PTR(-ENOMEM);
1160 module_put(rdev->owner);
1161 }
1162
Mark Brown5ffbd132009-07-21 16:00:23 +01001163 rdev->open_count++;
1164 if (exclusive) {
1165 rdev->exclusive = 1;
1166
1167 ret = _regulator_is_enabled(rdev);
1168 if (ret > 0)
1169 rdev->use_count = 1;
1170 else
1171 rdev->use_count = 0;
1172 }
1173
Liam Girdwooda5766f12008-10-10 13:22:20 +01001174out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001175 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001176
Liam Girdwood414c70c2008-04-30 15:59:04 +01001177 return regulator;
1178}
Mark Brown5ffbd132009-07-21 16:00:23 +01001179
1180/**
1181 * regulator_get - lookup and obtain a reference to a regulator.
1182 * @dev: device for regulator "consumer"
1183 * @id: Supply name or regulator ID.
1184 *
1185 * Returns a struct regulator corresponding to the regulator producer,
1186 * or IS_ERR() condition containing errno.
1187 *
1188 * Use of supply names configured via regulator_set_device_supply() is
1189 * strongly encouraged. It is recommended that the supply name used
1190 * should match the name used for the supply and/or the relevant
1191 * device pins in the datasheet.
1192 */
1193struct regulator *regulator_get(struct device *dev, const char *id)
1194{
1195 return _regulator_get(dev, id, 0);
1196}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001197EXPORT_SYMBOL_GPL(regulator_get);
1198
1199/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001200 * regulator_get_exclusive - obtain exclusive access to a regulator.
1201 * @dev: device for regulator "consumer"
1202 * @id: Supply name or regulator ID.
1203 *
1204 * Returns a struct regulator corresponding to the regulator producer,
1205 * or IS_ERR() condition containing errno. Other consumers will be
1206 * unable to obtain this reference is held and the use count for the
1207 * regulator will be initialised to reflect the current state of the
1208 * regulator.
1209 *
1210 * This is intended for use by consumers which cannot tolerate shared
1211 * use of the regulator such as those which need to force the
1212 * regulator off for correct operation of the hardware they are
1213 * controlling.
1214 *
1215 * Use of supply names configured via regulator_set_device_supply() is
1216 * strongly encouraged. It is recommended that the supply name used
1217 * should match the name used for the supply and/or the relevant
1218 * device pins in the datasheet.
1219 */
1220struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1221{
1222 return _regulator_get(dev, id, 1);
1223}
1224EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1225
1226/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001227 * regulator_put - "free" the regulator source
1228 * @regulator: regulator source
1229 *
1230 * Note: drivers must ensure that all regulator_enable calls made on this
1231 * regulator source are balanced by regulator_disable calls prior to calling
1232 * this function.
1233 */
1234void regulator_put(struct regulator *regulator)
1235{
1236 struct regulator_dev *rdev;
1237
1238 if (regulator == NULL || IS_ERR(regulator))
1239 return;
1240
Liam Girdwood414c70c2008-04-30 15:59:04 +01001241 mutex_lock(&regulator_list_mutex);
1242 rdev = regulator->rdev;
1243
1244 /* remove any sysfs entries */
1245 if (regulator->dev) {
1246 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1247 kfree(regulator->supply_name);
1248 device_remove_file(regulator->dev, &regulator->dev_attr);
1249 kfree(regulator->dev_attr.attr.name);
1250 }
1251 list_del(&regulator->list);
1252 kfree(regulator);
1253
Mark Brown5ffbd132009-07-21 16:00:23 +01001254 rdev->open_count--;
1255 rdev->exclusive = 0;
1256
Liam Girdwood414c70c2008-04-30 15:59:04 +01001257 module_put(rdev->owner);
1258 mutex_unlock(&regulator_list_mutex);
1259}
1260EXPORT_SYMBOL_GPL(regulator_put);
1261
Mark Brown9a2372f2009-08-03 18:49:57 +01001262static int _regulator_can_change_status(struct regulator_dev *rdev)
1263{
1264 if (!rdev->constraints)
1265 return 0;
1266
1267 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1268 return 1;
1269 else
1270 return 0;
1271}
1272
Liam Girdwood414c70c2008-04-30 15:59:04 +01001273/* locks held by regulator_enable() */
1274static int _regulator_enable(struct regulator_dev *rdev)
1275{
Mark Brown31aae2b2009-12-21 12:21:52 +00001276 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001278 if (rdev->use_count == 0) {
1279 /* do we need to enable the supply regulator first */
1280 if (rdev->supply) {
1281 mutex_lock(&rdev->supply->mutex);
1282 ret = _regulator_enable(rdev->supply);
1283 mutex_unlock(&rdev->supply->mutex);
1284 if (ret < 0) {
1285 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1286 __func__, rdev_get_name(rdev), ret);
1287 return ret;
1288 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001289 }
1290 }
1291
1292 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001293 if (rdev->constraints &&
1294 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1295 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001296
Mark Brown9a2372f2009-08-03 18:49:57 +01001297 if (rdev->use_count == 0) {
1298 /* The regulator may on if it's not switchable or left on */
1299 ret = _regulator_is_enabled(rdev);
1300 if (ret == -EINVAL || ret == 0) {
1301 if (!_regulator_can_change_status(rdev))
1302 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001303
Mark Brown31aae2b2009-12-21 12:21:52 +00001304 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001305 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001306
1307 /* Query before enabling in case configuration
1308 * dependant. */
1309 ret = _regulator_get_enable_time(rdev);
1310 if (ret >= 0) {
1311 delay = ret;
1312 } else {
1313 printk(KERN_WARNING
1314 "%s: enable_time() failed for %s: %d\n",
1315 __func__, rdev_get_name(rdev),
1316 ret);
1317 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001318 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001319
Mark Brown02fa3ec2010-11-10 14:38:30 +00001320 trace_regulator_enable(rdev_get_name(rdev));
1321
Mark Brown31aae2b2009-12-21 12:21:52 +00001322 /* Allow the regulator to ramp; it would be useful
1323 * to extend this for bulk operations so that the
1324 * regulators can ramp together. */
1325 ret = rdev->desc->ops->enable(rdev);
1326 if (ret < 0)
1327 return ret;
1328
Mark Brown02fa3ec2010-11-10 14:38:30 +00001329 trace_regulator_enable_delay(rdev_get_name(rdev));
1330
Axel Line36c1df2010-11-05 21:51:32 +08001331 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001332 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001333 udelay(delay % 1000);
1334 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001335 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001336 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001337
Mark Brown02fa3ec2010-11-10 14:38:30 +00001338 trace_regulator_enable_complete(rdev_get_name(rdev));
1339
Linus Walleija7433cf2009-08-26 12:54:04 +02001340 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001341 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001342 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001343 return ret;
1344 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001345 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001346 }
1347
Mark Brown9a2372f2009-08-03 18:49:57 +01001348 rdev->use_count++;
1349
1350 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001351}
1352
1353/**
1354 * regulator_enable - enable regulator output
1355 * @regulator: regulator source
1356 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001357 * Request that the regulator be enabled with the regulator output at
1358 * the predefined voltage or current value. Calls to regulator_enable()
1359 * must be balanced with calls to regulator_disable().
1360 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001362 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001363 */
1364int regulator_enable(struct regulator *regulator)
1365{
David Brownell412aec62008-11-16 11:44:46 -08001366 struct regulator_dev *rdev = regulator->rdev;
1367 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368
David Brownell412aec62008-11-16 11:44:46 -08001369 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001370 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001371 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001372 return ret;
1373}
1374EXPORT_SYMBOL_GPL(regulator_enable);
1375
1376/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001377static int _regulator_disable(struct regulator_dev *rdev,
1378 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001379{
1380 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001381 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001382
David Brownellcd94b502009-03-11 16:43:34 -08001383 if (WARN(rdev->use_count <= 0,
1384 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001385 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001386 return -EIO;
1387
Liam Girdwood414c70c2008-04-30 15:59:04 +01001388 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001389 if (rdev->use_count == 1 &&
1390 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391
1392 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001393 if (_regulator_can_change_status(rdev) &&
1394 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001395 trace_regulator_disable(rdev_get_name(rdev));
1396
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397 ret = rdev->desc->ops->disable(rdev);
1398 if (ret < 0) {
1399 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001400 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401 return ret;
1402 }
Mark Brown84b68262009-12-01 21:12:27 +00001403
Mark Brown02fa3ec2010-11-10 14:38:30 +00001404 trace_regulator_disable_complete(rdev_get_name(rdev));
1405
Mark Brown84b68262009-12-01 21:12:27 +00001406 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1407 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001408 }
1409
1410 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001411 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001412
1413 rdev->use_count = 0;
1414 } else if (rdev->use_count > 1) {
1415
1416 if (rdev->constraints &&
1417 (rdev->constraints->valid_ops_mask &
1418 REGULATOR_CHANGE_DRMS))
1419 drms_uA_update(rdev);
1420
1421 rdev->use_count--;
1422 }
1423 return ret;
1424}
1425
1426/**
1427 * regulator_disable - disable regulator output
1428 * @regulator: regulator source
1429 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001430 * Disable the regulator output voltage or current. Calls to
1431 * regulator_enable() must be balanced with calls to
1432 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001433 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001434 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001435 * devices have it enabled, the regulator device supports disabling and
1436 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001437 */
1438int regulator_disable(struct regulator *regulator)
1439{
David Brownell412aec62008-11-16 11:44:46 -08001440 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001441 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001442 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001443
David Brownell412aec62008-11-16 11:44:46 -08001444 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001445 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001446 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001447
1448 /* decrease our supplies ref count and disable if required */
1449 while (supply_rdev != NULL) {
1450 rdev = supply_rdev;
1451
1452 mutex_lock(&rdev->mutex);
1453 _regulator_disable(rdev, &supply_rdev);
1454 mutex_unlock(&rdev->mutex);
1455 }
1456
Liam Girdwood414c70c2008-04-30 15:59:04 +01001457 return ret;
1458}
1459EXPORT_SYMBOL_GPL(regulator_disable);
1460
1461/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001462static int _regulator_force_disable(struct regulator_dev *rdev,
1463 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001464{
1465 int ret = 0;
1466
1467 /* force disable */
1468 if (rdev->desc->ops->disable) {
1469 /* ah well, who wants to live forever... */
1470 ret = rdev->desc->ops->disable(rdev);
1471 if (ret < 0) {
1472 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001473 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001474 return ret;
1475 }
1476 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001477 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1478 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001479 }
1480
1481 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001482 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001483
1484 rdev->use_count = 0;
1485 return ret;
1486}
1487
1488/**
1489 * regulator_force_disable - force disable regulator output
1490 * @regulator: regulator source
1491 *
1492 * Forcibly disable the regulator output voltage or current.
1493 * NOTE: this *will* disable the regulator output even if other consumer
1494 * devices have it enabled. This should be used for situations when device
1495 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1496 */
1497int regulator_force_disable(struct regulator *regulator)
1498{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001499 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001500 int ret;
1501
1502 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001504 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001505 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001506
1507 if (supply_rdev)
1508 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1509
Liam Girdwood414c70c2008-04-30 15:59:04 +01001510 return ret;
1511}
1512EXPORT_SYMBOL_GPL(regulator_force_disable);
1513
1514static int _regulator_is_enabled(struct regulator_dev *rdev)
1515{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001516 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001517 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001518 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519
Mark Brown93325462009-08-03 18:49:56 +01001520 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001521}
1522
1523/**
1524 * regulator_is_enabled - is the regulator output enabled
1525 * @regulator: regulator source
1526 *
David Brownell412aec62008-11-16 11:44:46 -08001527 * Returns positive if the regulator driver backing the source/client
1528 * has requested that the device be enabled, zero if it hasn't, else a
1529 * negative errno code.
1530 *
1531 * Note that the device backing this regulator handle can have multiple
1532 * users, so it might be enabled even if regulator_enable() was never
1533 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001534 */
1535int regulator_is_enabled(struct regulator *regulator)
1536{
Mark Brown93325462009-08-03 18:49:56 +01001537 int ret;
1538
1539 mutex_lock(&regulator->rdev->mutex);
1540 ret = _regulator_is_enabled(regulator->rdev);
1541 mutex_unlock(&regulator->rdev->mutex);
1542
1543 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001544}
1545EXPORT_SYMBOL_GPL(regulator_is_enabled);
1546
1547/**
David Brownell4367cfd2009-02-26 11:48:36 -08001548 * regulator_count_voltages - count regulator_list_voltage() selectors
1549 * @regulator: regulator source
1550 *
1551 * Returns number of selectors, or negative errno. Selectors are
1552 * numbered starting at zero, and typically correspond to bitfields
1553 * in hardware registers.
1554 */
1555int regulator_count_voltages(struct regulator *regulator)
1556{
1557 struct regulator_dev *rdev = regulator->rdev;
1558
1559 return rdev->desc->n_voltages ? : -EINVAL;
1560}
1561EXPORT_SYMBOL_GPL(regulator_count_voltages);
1562
1563/**
1564 * regulator_list_voltage - enumerate supported voltages
1565 * @regulator: regulator source
1566 * @selector: identify voltage to list
1567 * Context: can sleep
1568 *
1569 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001570 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001571 * negative errno.
1572 */
1573int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1574{
1575 struct regulator_dev *rdev = regulator->rdev;
1576 struct regulator_ops *ops = rdev->desc->ops;
1577 int ret;
1578
1579 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1580 return -EINVAL;
1581
1582 mutex_lock(&rdev->mutex);
1583 ret = ops->list_voltage(rdev, selector);
1584 mutex_unlock(&rdev->mutex);
1585
1586 if (ret > 0) {
1587 if (ret < rdev->constraints->min_uV)
1588 ret = 0;
1589 else if (ret > rdev->constraints->max_uV)
1590 ret = 0;
1591 }
1592
1593 return ret;
1594}
1595EXPORT_SYMBOL_GPL(regulator_list_voltage);
1596
1597/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001598 * regulator_is_supported_voltage - check if a voltage range can be supported
1599 *
1600 * @regulator: Regulator to check.
1601 * @min_uV: Minimum required voltage in uV.
1602 * @max_uV: Maximum required voltage in uV.
1603 *
1604 * Returns a boolean or a negative error code.
1605 */
1606int regulator_is_supported_voltage(struct regulator *regulator,
1607 int min_uV, int max_uV)
1608{
1609 int i, voltages, ret;
1610
1611 ret = regulator_count_voltages(regulator);
1612 if (ret < 0)
1613 return ret;
1614 voltages = ret;
1615
1616 for (i = 0; i < voltages; i++) {
1617 ret = regulator_list_voltage(regulator, i);
1618
1619 if (ret >= min_uV && ret <= max_uV)
1620 return 1;
1621 }
1622
1623 return 0;
1624}
1625
1626/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001627 * regulator_set_voltage - set regulator output voltage
1628 * @regulator: regulator source
1629 * @min_uV: Minimum required voltage in uV
1630 * @max_uV: Maximum acceptable voltage in uV
1631 *
1632 * Sets a voltage regulator to the desired output voltage. This can be set
1633 * during any regulator state. IOW, regulator can be disabled or enabled.
1634 *
1635 * If the regulator is enabled then the voltage will change to the new value
1636 * immediately otherwise if the regulator is disabled the regulator will
1637 * output at the new voltage when enabled.
1638 *
1639 * NOTE: If the regulator is shared between several devices then the lowest
1640 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001641 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001642 * calling this function otherwise this call will fail.
1643 */
1644int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1645{
1646 struct regulator_dev *rdev = regulator->rdev;
1647 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001648 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001649
1650 mutex_lock(&rdev->mutex);
1651
1652 /* sanity check */
1653 if (!rdev->desc->ops->set_voltage) {
1654 ret = -EINVAL;
1655 goto out;
1656 }
1657
1658 /* constraints check */
1659 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1660 if (ret < 0)
1661 goto out;
1662 regulator->min_uV = min_uV;
1663 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001664
Mark Brown02fa3ec2010-11-10 14:38:30 +00001665 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1666
Mark Brown3a93f2a2010-11-10 14:38:29 +00001667 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1668
1669 if (rdev->desc->ops->list_voltage)
1670 selector = rdev->desc->ops->list_voltage(rdev, selector);
1671 else
1672 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001673
Mark Brown02fa3ec2010-11-10 14:38:30 +00001674 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1675
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001677 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001678 mutex_unlock(&rdev->mutex);
1679 return ret;
1680}
1681EXPORT_SYMBOL_GPL(regulator_set_voltage);
1682
1683static int _regulator_get_voltage(struct regulator_dev *rdev)
1684{
1685 /* sanity check */
1686 if (rdev->desc->ops->get_voltage)
1687 return rdev->desc->ops->get_voltage(rdev);
1688 else
1689 return -EINVAL;
1690}
1691
1692/**
1693 * regulator_get_voltage - get regulator output voltage
1694 * @regulator: regulator source
1695 *
1696 * This returns the current regulator voltage in uV.
1697 *
1698 * NOTE: If the regulator is disabled it will return the voltage value. This
1699 * function should not be used to determine regulator state.
1700 */
1701int regulator_get_voltage(struct regulator *regulator)
1702{
1703 int ret;
1704
1705 mutex_lock(&regulator->rdev->mutex);
1706
1707 ret = _regulator_get_voltage(regulator->rdev);
1708
1709 mutex_unlock(&regulator->rdev->mutex);
1710
1711 return ret;
1712}
1713EXPORT_SYMBOL_GPL(regulator_get_voltage);
1714
1715/**
1716 * regulator_set_current_limit - set regulator output current limit
1717 * @regulator: regulator source
1718 * @min_uA: Minimuum supported current in uA
1719 * @max_uA: Maximum supported current in uA
1720 *
1721 * Sets current sink to the desired output current. This can be set during
1722 * any regulator state. IOW, regulator can be disabled or enabled.
1723 *
1724 * If the regulator is enabled then the current will change to the new value
1725 * immediately otherwise if the regulator is disabled the regulator will
1726 * output at the new current when enabled.
1727 *
1728 * NOTE: Regulator system constraints must be set for this regulator before
1729 * calling this function otherwise this call will fail.
1730 */
1731int regulator_set_current_limit(struct regulator *regulator,
1732 int min_uA, int max_uA)
1733{
1734 struct regulator_dev *rdev = regulator->rdev;
1735 int ret;
1736
1737 mutex_lock(&rdev->mutex);
1738
1739 /* sanity check */
1740 if (!rdev->desc->ops->set_current_limit) {
1741 ret = -EINVAL;
1742 goto out;
1743 }
1744
1745 /* constraints check */
1746 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1747 if (ret < 0)
1748 goto out;
1749
1750 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1751out:
1752 mutex_unlock(&rdev->mutex);
1753 return ret;
1754}
1755EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1756
1757static int _regulator_get_current_limit(struct regulator_dev *rdev)
1758{
1759 int ret;
1760
1761 mutex_lock(&rdev->mutex);
1762
1763 /* sanity check */
1764 if (!rdev->desc->ops->get_current_limit) {
1765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 ret = rdev->desc->ops->get_current_limit(rdev);
1770out:
1771 mutex_unlock(&rdev->mutex);
1772 return ret;
1773}
1774
1775/**
1776 * regulator_get_current_limit - get regulator output current
1777 * @regulator: regulator source
1778 *
1779 * This returns the current supplied by the specified current sink in uA.
1780 *
1781 * NOTE: If the regulator is disabled it will return the current value. This
1782 * function should not be used to determine regulator state.
1783 */
1784int regulator_get_current_limit(struct regulator *regulator)
1785{
1786 return _regulator_get_current_limit(regulator->rdev);
1787}
1788EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1789
1790/**
1791 * regulator_set_mode - set regulator operating mode
1792 * @regulator: regulator source
1793 * @mode: operating mode - one of the REGULATOR_MODE constants
1794 *
1795 * Set regulator operating mode to increase regulator efficiency or improve
1796 * regulation performance.
1797 *
1798 * NOTE: Regulator system constraints must be set for this regulator before
1799 * calling this function otherwise this call will fail.
1800 */
1801int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1802{
1803 struct regulator_dev *rdev = regulator->rdev;
1804 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301805 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001806
1807 mutex_lock(&rdev->mutex);
1808
1809 /* sanity check */
1810 if (!rdev->desc->ops->set_mode) {
1811 ret = -EINVAL;
1812 goto out;
1813 }
1814
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301815 /* return if the same mode is requested */
1816 if (rdev->desc->ops->get_mode) {
1817 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1818 if (regulator_curr_mode == mode) {
1819 ret = 0;
1820 goto out;
1821 }
1822 }
1823
Liam Girdwood414c70c2008-04-30 15:59:04 +01001824 /* constraints check */
1825 ret = regulator_check_mode(rdev, mode);
1826 if (ret < 0)
1827 goto out;
1828
1829 ret = rdev->desc->ops->set_mode(rdev, mode);
1830out:
1831 mutex_unlock(&rdev->mutex);
1832 return ret;
1833}
1834EXPORT_SYMBOL_GPL(regulator_set_mode);
1835
1836static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1837{
1838 int ret;
1839
1840 mutex_lock(&rdev->mutex);
1841
1842 /* sanity check */
1843 if (!rdev->desc->ops->get_mode) {
1844 ret = -EINVAL;
1845 goto out;
1846 }
1847
1848 ret = rdev->desc->ops->get_mode(rdev);
1849out:
1850 mutex_unlock(&rdev->mutex);
1851 return ret;
1852}
1853
1854/**
1855 * regulator_get_mode - get regulator operating mode
1856 * @regulator: regulator source
1857 *
1858 * Get the current regulator operating mode.
1859 */
1860unsigned int regulator_get_mode(struct regulator *regulator)
1861{
1862 return _regulator_get_mode(regulator->rdev);
1863}
1864EXPORT_SYMBOL_GPL(regulator_get_mode);
1865
1866/**
1867 * regulator_set_optimum_mode - set regulator optimum operating mode
1868 * @regulator: regulator source
1869 * @uA_load: load current
1870 *
1871 * Notifies the regulator core of a new device load. This is then used by
1872 * DRMS (if enabled by constraints) to set the most efficient regulator
1873 * operating mode for the new regulator loading.
1874 *
1875 * Consumer devices notify their supply regulator of the maximum power
1876 * they will require (can be taken from device datasheet in the power
1877 * consumption tables) when they change operational status and hence power
1878 * state. Examples of operational state changes that can affect power
1879 * consumption are :-
1880 *
1881 * o Device is opened / closed.
1882 * o Device I/O is about to begin or has just finished.
1883 * o Device is idling in between work.
1884 *
1885 * This information is also exported via sysfs to userspace.
1886 *
1887 * DRMS will sum the total requested load on the regulator and change
1888 * to the most efficient operating mode if platform constraints allow.
1889 *
1890 * Returns the new regulator mode or error.
1891 */
1892int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1893{
1894 struct regulator_dev *rdev = regulator->rdev;
1895 struct regulator *consumer;
1896 int ret, output_uV, input_uV, total_uA_load = 0;
1897 unsigned int mode;
1898
1899 mutex_lock(&rdev->mutex);
1900
1901 regulator->uA_load = uA_load;
1902 ret = regulator_check_drms(rdev);
1903 if (ret < 0)
1904 goto out;
1905 ret = -EINVAL;
1906
1907 /* sanity check */
1908 if (!rdev->desc->ops->get_optimum_mode)
1909 goto out;
1910
1911 /* get output voltage */
1912 output_uV = rdev->desc->ops->get_voltage(rdev);
1913 if (output_uV <= 0) {
1914 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001915 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001916 goto out;
1917 }
1918
1919 /* get input voltage */
1920 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1921 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1922 else
1923 input_uV = rdev->constraints->input_uV;
1924 if (input_uV <= 0) {
1925 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001926 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001927 goto out;
1928 }
1929
1930 /* calc total requested load for this regulator */
1931 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001932 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001933
1934 mode = rdev->desc->ops->get_optimum_mode(rdev,
1935 input_uV, output_uV,
1936 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001937 ret = regulator_check_mode(rdev, mode);
1938 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001939 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001940 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001941 total_uA_load, input_uV, output_uV);
1942 goto out;
1943 }
1944
1945 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001946 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001947 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001948 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001949 goto out;
1950 }
1951 ret = mode;
1952out:
1953 mutex_unlock(&rdev->mutex);
1954 return ret;
1955}
1956EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1957
1958/**
1959 * regulator_register_notifier - register regulator event notifier
1960 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001961 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001962 *
1963 * Register notifier block to receive regulator events.
1964 */
1965int regulator_register_notifier(struct regulator *regulator,
1966 struct notifier_block *nb)
1967{
1968 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1969 nb);
1970}
1971EXPORT_SYMBOL_GPL(regulator_register_notifier);
1972
1973/**
1974 * regulator_unregister_notifier - unregister regulator event notifier
1975 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001976 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001977 *
1978 * Unregister regulator event notifier block.
1979 */
1980int regulator_unregister_notifier(struct regulator *regulator,
1981 struct notifier_block *nb)
1982{
1983 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1984 nb);
1985}
1986EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1987
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001988/* notify regulator consumers and downstream regulator consumers.
1989 * Note mutex must be held by caller.
1990 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001991static void _notifier_call_chain(struct regulator_dev *rdev,
1992 unsigned long event, void *data)
1993{
1994 struct regulator_dev *_rdev;
1995
1996 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001997 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001998
1999 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002000 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01002001 mutex_lock(&_rdev->mutex);
2002 _notifier_call_chain(_rdev, event, data);
2003 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002004 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002005}
2006
2007/**
2008 * regulator_bulk_get - get multiple regulator consumers
2009 *
2010 * @dev: Device to supply
2011 * @num_consumers: Number of consumers to register
2012 * @consumers: Configuration of consumers; clients are stored here.
2013 *
2014 * @return 0 on success, an errno on failure.
2015 *
2016 * This helper function allows drivers to get several regulator
2017 * consumers in one operation. If any of the regulators cannot be
2018 * acquired then any regulators that were allocated will be freed
2019 * before returning to the caller.
2020 */
2021int regulator_bulk_get(struct device *dev, int num_consumers,
2022 struct regulator_bulk_data *consumers)
2023{
2024 int i;
2025 int ret;
2026
2027 for (i = 0; i < num_consumers; i++)
2028 consumers[i].consumer = NULL;
2029
2030 for (i = 0; i < num_consumers; i++) {
2031 consumers[i].consumer = regulator_get(dev,
2032 consumers[i].supply);
2033 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002034 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002035 dev_err(dev, "Failed to get supply '%s': %d\n",
2036 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002037 consumers[i].consumer = NULL;
2038 goto err;
2039 }
2040 }
2041
2042 return 0;
2043
2044err:
2045 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2046 regulator_put(consumers[i].consumer);
2047
2048 return ret;
2049}
2050EXPORT_SYMBOL_GPL(regulator_bulk_get);
2051
2052/**
2053 * regulator_bulk_enable - enable multiple regulator consumers
2054 *
2055 * @num_consumers: Number of consumers
2056 * @consumers: Consumer data; clients are stored here.
2057 * @return 0 on success, an errno on failure
2058 *
2059 * This convenience API allows consumers to enable multiple regulator
2060 * clients in a single API call. If any consumers cannot be enabled
2061 * then any others that were enabled will be disabled again prior to
2062 * return.
2063 */
2064int regulator_bulk_enable(int num_consumers,
2065 struct regulator_bulk_data *consumers)
2066{
2067 int i;
2068 int ret;
2069
2070 for (i = 0; i < num_consumers; i++) {
2071 ret = regulator_enable(consumers[i].consumer);
2072 if (ret != 0)
2073 goto err;
2074 }
2075
2076 return 0;
2077
2078err:
Mark Brown5b307622009-10-13 13:06:49 +01002079 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002080 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002081 regulator_disable(consumers[i].consumer);
2082
2083 return ret;
2084}
2085EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2086
2087/**
2088 * regulator_bulk_disable - disable multiple regulator consumers
2089 *
2090 * @num_consumers: Number of consumers
2091 * @consumers: Consumer data; clients are stored here.
2092 * @return 0 on success, an errno on failure
2093 *
2094 * This convenience API allows consumers to disable multiple regulator
2095 * clients in a single API call. If any consumers cannot be enabled
2096 * then any others that were disabled will be disabled again prior to
2097 * return.
2098 */
2099int regulator_bulk_disable(int num_consumers,
2100 struct regulator_bulk_data *consumers)
2101{
2102 int i;
2103 int ret;
2104
2105 for (i = 0; i < num_consumers; i++) {
2106 ret = regulator_disable(consumers[i].consumer);
2107 if (ret != 0)
2108 goto err;
2109 }
2110
2111 return 0;
2112
2113err:
Mark Brown5b307622009-10-13 13:06:49 +01002114 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2115 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002116 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002117 regulator_enable(consumers[i].consumer);
2118
2119 return ret;
2120}
2121EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2122
2123/**
2124 * regulator_bulk_free - free multiple regulator consumers
2125 *
2126 * @num_consumers: Number of consumers
2127 * @consumers: Consumer data; clients are stored here.
2128 *
2129 * This convenience API allows consumers to free multiple regulator
2130 * clients in a single API call.
2131 */
2132void regulator_bulk_free(int num_consumers,
2133 struct regulator_bulk_data *consumers)
2134{
2135 int i;
2136
2137 for (i = 0; i < num_consumers; i++) {
2138 regulator_put(consumers[i].consumer);
2139 consumers[i].consumer = NULL;
2140 }
2141}
2142EXPORT_SYMBOL_GPL(regulator_bulk_free);
2143
2144/**
2145 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002146 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002148 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149 *
2150 * Called by regulator drivers to notify clients a regulator event has
2151 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002152 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002153 */
2154int regulator_notifier_call_chain(struct regulator_dev *rdev,
2155 unsigned long event, void *data)
2156{
2157 _notifier_call_chain(rdev, event, data);
2158 return NOTIFY_DONE;
2159
2160}
2161EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2162
Mark Brownbe721972009-08-04 20:09:52 +02002163/**
2164 * regulator_mode_to_status - convert a regulator mode into a status
2165 *
2166 * @mode: Mode to convert
2167 *
2168 * Convert a regulator mode into a status.
2169 */
2170int regulator_mode_to_status(unsigned int mode)
2171{
2172 switch (mode) {
2173 case REGULATOR_MODE_FAST:
2174 return REGULATOR_STATUS_FAST;
2175 case REGULATOR_MODE_NORMAL:
2176 return REGULATOR_STATUS_NORMAL;
2177 case REGULATOR_MODE_IDLE:
2178 return REGULATOR_STATUS_IDLE;
2179 case REGULATOR_STATUS_STANDBY:
2180 return REGULATOR_STATUS_STANDBY;
2181 default:
2182 return 0;
2183 }
2184}
2185EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2186
David Brownell7ad68e22008-11-11 17:39:02 -08002187/*
2188 * To avoid cluttering sysfs (and memory) with useless state, only
2189 * create attributes that can be meaningfully displayed.
2190 */
2191static int add_regulator_attributes(struct regulator_dev *rdev)
2192{
2193 struct device *dev = &rdev->dev;
2194 struct regulator_ops *ops = rdev->desc->ops;
2195 int status = 0;
2196
2197 /* some attributes need specific methods to be displayed */
2198 if (ops->get_voltage) {
2199 status = device_create_file(dev, &dev_attr_microvolts);
2200 if (status < 0)
2201 return status;
2202 }
2203 if (ops->get_current_limit) {
2204 status = device_create_file(dev, &dev_attr_microamps);
2205 if (status < 0)
2206 return status;
2207 }
2208 if (ops->get_mode) {
2209 status = device_create_file(dev, &dev_attr_opmode);
2210 if (status < 0)
2211 return status;
2212 }
2213 if (ops->is_enabled) {
2214 status = device_create_file(dev, &dev_attr_state);
2215 if (status < 0)
2216 return status;
2217 }
David Brownell853116a2009-01-14 23:03:17 -08002218 if (ops->get_status) {
2219 status = device_create_file(dev, &dev_attr_status);
2220 if (status < 0)
2221 return status;
2222 }
David Brownell7ad68e22008-11-11 17:39:02 -08002223
2224 /* some attributes are type-specific */
2225 if (rdev->desc->type == REGULATOR_CURRENT) {
2226 status = device_create_file(dev, &dev_attr_requested_microamps);
2227 if (status < 0)
2228 return status;
2229 }
2230
2231 /* all the other attributes exist to support constraints;
2232 * don't show them if there are no constraints, or if the
2233 * relevant supporting methods are missing.
2234 */
2235 if (!rdev->constraints)
2236 return status;
2237
2238 /* constraints need specific supporting methods */
2239 if (ops->set_voltage) {
2240 status = device_create_file(dev, &dev_attr_min_microvolts);
2241 if (status < 0)
2242 return status;
2243 status = device_create_file(dev, &dev_attr_max_microvolts);
2244 if (status < 0)
2245 return status;
2246 }
2247 if (ops->set_current_limit) {
2248 status = device_create_file(dev, &dev_attr_min_microamps);
2249 if (status < 0)
2250 return status;
2251 status = device_create_file(dev, &dev_attr_max_microamps);
2252 if (status < 0)
2253 return status;
2254 }
2255
2256 /* suspend mode constraints need multiple supporting methods */
2257 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2258 return status;
2259
2260 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2261 if (status < 0)
2262 return status;
2263 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2264 if (status < 0)
2265 return status;
2266 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2267 if (status < 0)
2268 return status;
2269
2270 if (ops->set_suspend_voltage) {
2271 status = device_create_file(dev,
2272 &dev_attr_suspend_standby_microvolts);
2273 if (status < 0)
2274 return status;
2275 status = device_create_file(dev,
2276 &dev_attr_suspend_mem_microvolts);
2277 if (status < 0)
2278 return status;
2279 status = device_create_file(dev,
2280 &dev_attr_suspend_disk_microvolts);
2281 if (status < 0)
2282 return status;
2283 }
2284
2285 if (ops->set_suspend_mode) {
2286 status = device_create_file(dev,
2287 &dev_attr_suspend_standby_mode);
2288 if (status < 0)
2289 return status;
2290 status = device_create_file(dev,
2291 &dev_attr_suspend_mem_mode);
2292 if (status < 0)
2293 return status;
2294 status = device_create_file(dev,
2295 &dev_attr_suspend_disk_mode);
2296 if (status < 0)
2297 return status;
2298 }
2299
2300 return status;
2301}
2302
Liam Girdwood414c70c2008-04-30 15:59:04 +01002303/**
2304 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002305 * @regulator_desc: regulator to register
2306 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002307 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002308 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002309 *
2310 * Called by regulator drivers to register a regulator.
2311 * Returns 0 on success.
2312 */
2313struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002314 struct device *dev, struct regulator_init_data *init_data,
2315 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002316{
2317 static atomic_t regulator_no = ATOMIC_INIT(0);
2318 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002319 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002320
2321 if (regulator_desc == NULL)
2322 return ERR_PTR(-EINVAL);
2323
2324 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2325 return ERR_PTR(-EINVAL);
2326
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002327 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2328 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002329 return ERR_PTR(-EINVAL);
2330
Mark Brown46fabe1e2008-09-09 16:21:18 +01002331 if (!init_data)
2332 return ERR_PTR(-EINVAL);
2333
Liam Girdwood414c70c2008-04-30 15:59:04 +01002334 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2335 if (rdev == NULL)
2336 return ERR_PTR(-ENOMEM);
2337
2338 mutex_lock(&regulator_list_mutex);
2339
2340 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002341 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002342 rdev->owner = regulator_desc->owner;
2343 rdev->desc = regulator_desc;
2344 INIT_LIST_HEAD(&rdev->consumer_list);
2345 INIT_LIST_HEAD(&rdev->supply_list);
2346 INIT_LIST_HEAD(&rdev->list);
2347 INIT_LIST_HEAD(&rdev->slist);
2348 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2349
Liam Girdwooda5766f12008-10-10 13:22:20 +01002350 /* preform any regulator specific init */
2351 if (init_data->regulator_init) {
2352 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002353 if (ret < 0)
2354 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002355 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002356
Liam Girdwooda5766f12008-10-10 13:22:20 +01002357 /* register with sysfs */
2358 rdev->dev.class = &regulator_class;
2359 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002360 dev_set_name(&rdev->dev, "regulator.%d",
2361 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002362 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002363 if (ret != 0) {
2364 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002365 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002366 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002367
2368 dev_set_drvdata(&rdev->dev, rdev);
2369
Mike Rapoport74f544c2008-11-25 14:53:53 +02002370 /* set regulator constraints */
2371 ret = set_machine_constraints(rdev, &init_data->constraints);
2372 if (ret < 0)
2373 goto scrub;
2374
David Brownell7ad68e22008-11-11 17:39:02 -08002375 /* add attributes supported by this regulator */
2376 ret = add_regulator_attributes(rdev);
2377 if (ret < 0)
2378 goto scrub;
2379
Liam Girdwooda5766f12008-10-10 13:22:20 +01002380 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002381 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2382 dev_err(dev,
2383 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002384 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002385 goto scrub;
2386 }
2387
2388 if (init_data->supply_regulator) {
2389 struct regulator_dev *r;
2390 int found = 0;
2391
2392 list_for_each_entry(r, &regulator_list, list) {
2393 if (strcmp(rdev_get_name(r),
2394 init_data->supply_regulator) == 0) {
2395 found = 1;
2396 break;
2397 }
2398 }
2399
2400 if (!found) {
2401 dev_err(dev, "Failed to find supply %s\n",
2402 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002403 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002404 goto scrub;
2405 }
2406
2407 ret = set_supply(rdev, r);
2408 if (ret < 0)
2409 goto scrub;
2410 }
2411
Liam Girdwooda5766f12008-10-10 13:22:20 +01002412 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002413 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002414 ret = set_supply(rdev,
2415 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002416 if (ret < 0)
2417 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002418 }
2419
2420 /* add consumers devices */
2421 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2422 ret = set_consumer_device_supply(rdev,
2423 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002424 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002425 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002426 if (ret < 0)
2427 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002428 }
2429
2430 list_add(&rdev->list, &regulator_list);
2431out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002432 mutex_unlock(&regulator_list_mutex);
2433 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002434
Jani Nikulad4033b52010-04-29 10:55:11 +03002435unset_supplies:
2436 unset_regulator_supplies(rdev);
2437
David Brownell4fca9542008-11-11 17:38:53 -08002438scrub:
2439 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002440 /* device core frees rdev */
2441 rdev = ERR_PTR(ret);
2442 goto out;
2443
David Brownell4fca9542008-11-11 17:38:53 -08002444clean:
2445 kfree(rdev);
2446 rdev = ERR_PTR(ret);
2447 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002448}
2449EXPORT_SYMBOL_GPL(regulator_register);
2450
2451/**
2452 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002453 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002454 *
2455 * Called by regulator drivers to unregister a regulator.
2456 */
2457void regulator_unregister(struct regulator_dev *rdev)
2458{
2459 if (rdev == NULL)
2460 return;
2461
2462 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002463 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002464 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002465 list_del(&rdev->list);
2466 if (rdev->supply)
2467 sysfs_remove_link(&rdev->dev.kobj, "supply");
2468 device_unregister(&rdev->dev);
2469 mutex_unlock(&regulator_list_mutex);
2470}
2471EXPORT_SYMBOL_GPL(regulator_unregister);
2472
2473/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002474 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002475 * @state: system suspend state
2476 *
2477 * Configure each regulator with it's suspend operating parameters for state.
2478 * This will usually be called by machine suspend code prior to supending.
2479 */
2480int regulator_suspend_prepare(suspend_state_t state)
2481{
2482 struct regulator_dev *rdev;
2483 int ret = 0;
2484
2485 /* ON is handled by regulator active state */
2486 if (state == PM_SUSPEND_ON)
2487 return -EINVAL;
2488
2489 mutex_lock(&regulator_list_mutex);
2490 list_for_each_entry(rdev, &regulator_list, list) {
2491
2492 mutex_lock(&rdev->mutex);
2493 ret = suspend_prepare(rdev, state);
2494 mutex_unlock(&rdev->mutex);
2495
2496 if (ret < 0) {
2497 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002498 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002499 goto out;
2500 }
2501 }
2502out:
2503 mutex_unlock(&regulator_list_mutex);
2504 return ret;
2505}
2506EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2507
2508/**
Mark Brownca725562009-03-16 19:36:34 +00002509 * regulator_has_full_constraints - the system has fully specified constraints
2510 *
2511 * Calling this function will cause the regulator API to disable all
2512 * regulators which have a zero use count and don't have an always_on
2513 * constraint in a late_initcall.
2514 *
2515 * The intention is that this will become the default behaviour in a
2516 * future kernel release so users are encouraged to use this facility
2517 * now.
2518 */
2519void regulator_has_full_constraints(void)
2520{
2521 has_full_constraints = 1;
2522}
2523EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2524
2525/**
Mark Brown688fe992010-10-05 19:18:32 -07002526 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2527 *
2528 * Calling this function will cause the regulator API to provide a
2529 * dummy regulator to consumers if no physical regulator is found,
2530 * allowing most consumers to proceed as though a regulator were
2531 * configured. This allows systems such as those with software
2532 * controllable regulators for the CPU core only to be brought up more
2533 * readily.
2534 */
2535void regulator_use_dummy_regulator(void)
2536{
2537 board_wants_dummy_regulator = true;
2538}
2539EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2540
2541/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002542 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002543 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002544 *
2545 * Get rdev regulator driver private data. This call can be used in the
2546 * regulator driver context.
2547 */
2548void *rdev_get_drvdata(struct regulator_dev *rdev)
2549{
2550 return rdev->reg_data;
2551}
2552EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2553
2554/**
2555 * regulator_get_drvdata - get regulator driver data
2556 * @regulator: regulator
2557 *
2558 * Get regulator driver private data. This call can be used in the consumer
2559 * driver context when non API regulator specific functions need to be called.
2560 */
2561void *regulator_get_drvdata(struct regulator *regulator)
2562{
2563 return regulator->rdev->reg_data;
2564}
2565EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2566
2567/**
2568 * regulator_set_drvdata - set regulator driver data
2569 * @regulator: regulator
2570 * @data: data
2571 */
2572void regulator_set_drvdata(struct regulator *regulator, void *data)
2573{
2574 regulator->rdev->reg_data = data;
2575}
2576EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2577
2578/**
2579 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002580 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002581 */
2582int rdev_get_id(struct regulator_dev *rdev)
2583{
2584 return rdev->desc->id;
2585}
2586EXPORT_SYMBOL_GPL(rdev_get_id);
2587
Liam Girdwooda5766f12008-10-10 13:22:20 +01002588struct device *rdev_get_dev(struct regulator_dev *rdev)
2589{
2590 return &rdev->dev;
2591}
2592EXPORT_SYMBOL_GPL(rdev_get_dev);
2593
2594void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2595{
2596 return reg_init_data->driver_data;
2597}
2598EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2599
Liam Girdwood414c70c2008-04-30 15:59:04 +01002600static int __init regulator_init(void)
2601{
Mark Brown34abbd62010-02-12 10:18:08 +00002602 int ret;
2603
Liam Girdwood414c70c2008-04-30 15:59:04 +01002604 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002605
2606 ret = class_register(&regulator_class);
2607
2608 regulator_dummy_init();
2609
2610 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002611}
2612
2613/* init early to allow our consumers to complete system booting */
2614core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002615
2616static int __init regulator_init_complete(void)
2617{
2618 struct regulator_dev *rdev;
2619 struct regulator_ops *ops;
2620 struct regulation_constraints *c;
2621 int enabled, ret;
2622 const char *name;
2623
2624 mutex_lock(&regulator_list_mutex);
2625
2626 /* If we have a full configuration then disable any regulators
2627 * which are not in use or always_on. This will become the
2628 * default behaviour in the future.
2629 */
2630 list_for_each_entry(rdev, &regulator_list, list) {
2631 ops = rdev->desc->ops;
2632 c = rdev->constraints;
2633
Mark Brown1083c392009-10-22 16:31:32 +01002634 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002635
Mark Brownf25e0b42009-08-03 18:49:55 +01002636 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002637 continue;
2638
2639 mutex_lock(&rdev->mutex);
2640
2641 if (rdev->use_count)
2642 goto unlock;
2643
2644 /* If we can't read the status assume it's on. */
2645 if (ops->is_enabled)
2646 enabled = ops->is_enabled(rdev);
2647 else
2648 enabled = 1;
2649
2650 if (!enabled)
2651 goto unlock;
2652
2653 if (has_full_constraints) {
2654 /* We log since this may kill the system if it
2655 * goes wrong. */
2656 printk(KERN_INFO "%s: disabling %s\n",
2657 __func__, name);
2658 ret = ops->disable(rdev);
2659 if (ret != 0) {
2660 printk(KERN_ERR
2661 "%s: couldn't disable %s: %d\n",
2662 __func__, name, ret);
2663 }
2664 } else {
2665 /* The intention is that in future we will
2666 * assume that full constraints are provided
2667 * so warn even if we aren't going to do
2668 * anything here.
2669 */
2670 printk(KERN_WARNING
2671 "%s: incomplete constraints, leaving %s on\n",
2672 __func__, name);
2673 }
2674
2675unlock:
2676 mutex_unlock(&rdev->mutex);
2677 }
2678
2679 mutex_unlock(&regulator_list_mutex);
2680
2681 return 0;
2682}
2683late_initcall(regulator_init_complete);