blob: 8a5d0ae191b3cbe5c513415ce3697def45755571 [file] [log] [blame]
Liam Girdwood414c70c2008-04-30 15:59:04 +01001/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
Liam Girdwooda5766f12008-10-10 13:22:20 +01005 * Copyright 2008 SlimLogic Ltd.
Liam Girdwood414c70c2008-04-30 15:59:04 +01006 *
Liam Girdwooda5766f12008-10-10 13:22:20 +01007 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
Liam Girdwood414c70c2008-04-30 15:59:04 +01008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010020#include <linux/err.h>
21#include <linux/mutex.h>
22#include <linux/suspend.h>
Mark Brown31aae2b2009-12-21 12:21:52 +000023#include <linux/delay.h>
Liam Girdwood414c70c2008-04-30 15:59:04 +010024#include <linux/regulator/consumer.h>
25#include <linux/regulator/driver.h>
26#include <linux/regulator/machine.h>
27
Mark Brown02fa3ec2010-11-10 14:38:30 +000028#define CREATE_TRACE_POINTS
29#include <trace/events/regulator.h>
30
Mark Brown34abbd62010-02-12 10:18:08 +000031#include "dummy.h"
32
Liam Girdwood414c70c2008-04-30 15:59:04 +010033#define REGULATOR_VERSION "0.5"
34
35static DEFINE_MUTEX(regulator_list_mutex);
36static LIST_HEAD(regulator_list);
37static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000038static int has_full_constraints;
Mark Brown688fe992010-10-05 19:18:32 -070039static bool board_wants_dummy_regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010040
Mark Brown8dc53902008-12-31 12:52:40 +000041/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010042 * struct regulator_map
43 *
44 * Used to provide symbolic supply names to devices.
45 */
46struct regulator_map {
47 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010048 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010049 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010050 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010051};
52
Liam Girdwood414c70c2008-04-30 15:59:04 +010053/*
54 * struct regulator
55 *
56 * One for each consumer device.
57 */
58struct regulator {
59 struct device *dev;
60 struct list_head list;
61 int uA_load;
62 int min_uV;
63 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010064 char *supply_name;
65 struct device_attribute dev_attr;
66 struct regulator_dev *rdev;
67};
68
69static int _regulator_is_enabled(struct regulator_dev *rdev);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -050070static int _regulator_disable(struct regulator_dev *rdev,
71 struct regulator_dev **supply_rdev_ptr);
Liam Girdwood414c70c2008-04-30 15:59:04 +010072static int _regulator_get_voltage(struct regulator_dev *rdev);
73static int _regulator_get_current_limit(struct regulator_dev *rdev);
74static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
75static void _notifier_call_chain(struct regulator_dev *rdev,
76 unsigned long event, void *data);
77
Mark Brown1083c392009-10-22 16:31:32 +010078static const char *rdev_get_name(struct regulator_dev *rdev)
79{
80 if (rdev->constraints && rdev->constraints->name)
81 return rdev->constraints->name;
82 else if (rdev->desc->name)
83 return rdev->desc->name;
84 else
85 return "";
86}
87
Liam Girdwood414c70c2008-04-30 15:59:04 +010088/* gets the regulator for a given consumer device */
89static struct regulator *get_device_regulator(struct device *dev)
90{
91 struct regulator *regulator = NULL;
92 struct regulator_dev *rdev;
93
94 mutex_lock(&regulator_list_mutex);
95 list_for_each_entry(rdev, &regulator_list, list) {
96 mutex_lock(&rdev->mutex);
97 list_for_each_entry(regulator, &rdev->consumer_list, list) {
98 if (regulator->dev == dev) {
99 mutex_unlock(&rdev->mutex);
100 mutex_unlock(&regulator_list_mutex);
101 return regulator;
102 }
103 }
104 mutex_unlock(&rdev->mutex);
105 }
106 mutex_unlock(&regulator_list_mutex);
107 return NULL;
108}
109
110/* Platform voltage constraint check */
111static int regulator_check_voltage(struct regulator_dev *rdev,
112 int *min_uV, int *max_uV)
113{
114 BUG_ON(*min_uV > *max_uV);
115
116 if (!rdev->constraints) {
117 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100118 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100119 return -ENODEV;
120 }
121 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
122 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100123 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100124 return -EPERM;
125 }
126
127 if (*max_uV > rdev->constraints->max_uV)
128 *max_uV = rdev->constraints->max_uV;
129 if (*min_uV < rdev->constraints->min_uV)
130 *min_uV = rdev->constraints->min_uV;
131
132 if (*min_uV > *max_uV)
133 return -EINVAL;
134
135 return 0;
136}
137
138/* current constraint check */
139static int regulator_check_current_limit(struct regulator_dev *rdev,
140 int *min_uA, int *max_uA)
141{
142 BUG_ON(*min_uA > *max_uA);
143
144 if (!rdev->constraints) {
145 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100146 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100147 return -ENODEV;
148 }
149 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
150 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100151 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100152 return -EPERM;
153 }
154
155 if (*max_uA > rdev->constraints->max_uA)
156 *max_uA = rdev->constraints->max_uA;
157 if (*min_uA < rdev->constraints->min_uA)
158 *min_uA = rdev->constraints->min_uA;
159
160 if (*min_uA > *max_uA)
161 return -EINVAL;
162
163 return 0;
164}
165
166/* operating mode constraint check */
167static int regulator_check_mode(struct regulator_dev *rdev, int mode)
168{
David Brownelle5735202008-11-16 11:46:56 -0800169 switch (mode) {
170 case REGULATOR_MODE_FAST:
171 case REGULATOR_MODE_NORMAL:
172 case REGULATOR_MODE_IDLE:
173 case REGULATOR_MODE_STANDBY:
174 break;
175 default:
176 return -EINVAL;
177 }
178
Liam Girdwood414c70c2008-04-30 15:59:04 +0100179 if (!rdev->constraints) {
180 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100181 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100182 return -ENODEV;
183 }
184 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
185 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100186 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100187 return -EPERM;
188 }
189 if (!(rdev->constraints->valid_modes_mask & mode)) {
190 printk(KERN_ERR "%s: invalid mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100191 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100192 return -EINVAL;
193 }
194 return 0;
195}
196
197/* dynamic regulator mode switching constraint check */
198static int regulator_check_drms(struct regulator_dev *rdev)
199{
200 if (!rdev->constraints) {
201 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
Mark Brown1083c392009-10-22 16:31:32 +0100202 rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100203 return -ENODEV;
204 }
205 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
206 printk(KERN_ERR "%s: operation not allowed for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +0100207 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +0100208 return -EPERM;
209 }
210 return 0;
211}
212
213static ssize_t device_requested_uA_show(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct regulator *regulator;
217
218 regulator = get_device_regulator(dev);
219 if (regulator == NULL)
220 return 0;
221
222 return sprintf(buf, "%d\n", regulator->uA_load);
223}
224
225static ssize_t regulator_uV_show(struct device *dev,
226 struct device_attribute *attr, char *buf)
227{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100228 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100229 ssize_t ret;
230
231 mutex_lock(&rdev->mutex);
232 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
233 mutex_unlock(&rdev->mutex);
234
235 return ret;
236}
David Brownell7ad68e22008-11-11 17:39:02 -0800237static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100238
239static ssize_t regulator_uA_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100242 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100243
244 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
245}
David Brownell7ad68e22008-11-11 17:39:02 -0800246static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100247
Mark Brownbc558a62008-10-10 15:33:20 +0100248static ssize_t regulator_name_show(struct device *dev,
249 struct device_attribute *attr, char *buf)
250{
251 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brownbc558a62008-10-10 15:33:20 +0100252
Mark Brown1083c392009-10-22 16:31:32 +0100253 return sprintf(buf, "%s\n", rdev_get_name(rdev));
Mark Brownbc558a62008-10-10 15:33:20 +0100254}
255
David Brownell4fca9542008-11-11 17:38:53 -0800256static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100257{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100258 switch (mode) {
259 case REGULATOR_MODE_FAST:
260 return sprintf(buf, "fast\n");
261 case REGULATOR_MODE_NORMAL:
262 return sprintf(buf, "normal\n");
263 case REGULATOR_MODE_IDLE:
264 return sprintf(buf, "idle\n");
265 case REGULATOR_MODE_STANDBY:
266 return sprintf(buf, "standby\n");
267 }
268 return sprintf(buf, "unknown\n");
269}
270
David Brownell4fca9542008-11-11 17:38:53 -0800271static ssize_t regulator_opmode_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100273{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100274 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100275
David Brownell4fca9542008-11-11 17:38:53 -0800276 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
277}
David Brownell7ad68e22008-11-11 17:39:02 -0800278static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800279
280static ssize_t regulator_print_state(char *buf, int state)
281{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100282 if (state > 0)
283 return sprintf(buf, "enabled\n");
284 else if (state == 0)
285 return sprintf(buf, "disabled\n");
286 else
287 return sprintf(buf, "unknown\n");
288}
289
David Brownell4fca9542008-11-11 17:38:53 -0800290static ssize_t regulator_state_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
292{
293 struct regulator_dev *rdev = dev_get_drvdata(dev);
Mark Brown93325462009-08-03 18:49:56 +0100294 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800295
Mark Brown93325462009-08-03 18:49:56 +0100296 mutex_lock(&rdev->mutex);
297 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
298 mutex_unlock(&rdev->mutex);
299
300 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800301}
David Brownell7ad68e22008-11-11 17:39:02 -0800302static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800303
David Brownell853116a2009-01-14 23:03:17 -0800304static ssize_t regulator_status_show(struct device *dev,
305 struct device_attribute *attr, char *buf)
306{
307 struct regulator_dev *rdev = dev_get_drvdata(dev);
308 int status;
309 char *label;
310
311 status = rdev->desc->ops->get_status(rdev);
312 if (status < 0)
313 return status;
314
315 switch (status) {
316 case REGULATOR_STATUS_OFF:
317 label = "off";
318 break;
319 case REGULATOR_STATUS_ON:
320 label = "on";
321 break;
322 case REGULATOR_STATUS_ERROR:
323 label = "error";
324 break;
325 case REGULATOR_STATUS_FAST:
326 label = "fast";
327 break;
328 case REGULATOR_STATUS_NORMAL:
329 label = "normal";
330 break;
331 case REGULATOR_STATUS_IDLE:
332 label = "idle";
333 break;
334 case REGULATOR_STATUS_STANDBY:
335 label = "standby";
336 break;
337 default:
338 return -ERANGE;
339 }
340
341 return sprintf(buf, "%s\n", label);
342}
343static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
344
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345static ssize_t regulator_min_uA_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
347{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100348 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100349
350 if (!rdev->constraints)
351 return sprintf(buf, "constraint not defined\n");
352
353 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
354}
David Brownell7ad68e22008-11-11 17:39:02 -0800355static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100356
357static ssize_t regulator_max_uA_show(struct device *dev,
358 struct device_attribute *attr, char *buf)
359{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100360 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100361
362 if (!rdev->constraints)
363 return sprintf(buf, "constraint not defined\n");
364
365 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
366}
David Brownell7ad68e22008-11-11 17:39:02 -0800367static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100368
369static ssize_t regulator_min_uV_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
371{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100372 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100373
374 if (!rdev->constraints)
375 return sprintf(buf, "constraint not defined\n");
376
377 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
378}
David Brownell7ad68e22008-11-11 17:39:02 -0800379static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100380
381static ssize_t regulator_max_uV_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
383{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100384 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100385
386 if (!rdev->constraints)
387 return sprintf(buf, "constraint not defined\n");
388
389 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
390}
David Brownell7ad68e22008-11-11 17:39:02 -0800391static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100392
393static ssize_t regulator_total_uA_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
395{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100396 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100397 struct regulator *regulator;
398 int uA = 0;
399
400 mutex_lock(&rdev->mutex);
401 list_for_each_entry(regulator, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100402 uA += regulator->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403 mutex_unlock(&rdev->mutex);
404 return sprintf(buf, "%d\n", uA);
405}
David Brownell7ad68e22008-11-11 17:39:02 -0800406static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100407
408static ssize_t regulator_num_users_show(struct device *dev,
409 struct device_attribute *attr, char *buf)
410{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100411 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100412 return sprintf(buf, "%d\n", rdev->use_count);
413}
414
415static ssize_t regulator_type_show(struct device *dev,
416 struct device_attribute *attr, char *buf)
417{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100418 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100419
420 switch (rdev->desc->type) {
421 case REGULATOR_VOLTAGE:
422 return sprintf(buf, "voltage\n");
423 case REGULATOR_CURRENT:
424 return sprintf(buf, "current\n");
425 }
426 return sprintf(buf, "unknown\n");
427}
428
429static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
430 struct device_attribute *attr, char *buf)
431{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100432 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433
Liam Girdwood414c70c2008-04-30 15:59:04 +0100434 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
435}
David Brownell7ad68e22008-11-11 17:39:02 -0800436static DEVICE_ATTR(suspend_mem_microvolts, 0444,
437 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438
439static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100442 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443
Liam Girdwood414c70c2008-04-30 15:59:04 +0100444 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
445}
David Brownell7ad68e22008-11-11 17:39:02 -0800446static DEVICE_ATTR(suspend_disk_microvolts, 0444,
447 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448
449static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
450 struct device_attribute *attr, char *buf)
451{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100452 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
Liam Girdwood414c70c2008-04-30 15:59:04 +0100454 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
455}
David Brownell7ad68e22008-11-11 17:39:02 -0800456static DEVICE_ATTR(suspend_standby_microvolts, 0444,
457 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
Liam Girdwood414c70c2008-04-30 15:59:04 +0100459static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
460 struct device_attribute *attr, char *buf)
461{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100462 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100463
David Brownell4fca9542008-11-11 17:38:53 -0800464 return regulator_print_opmode(buf,
465 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(suspend_mem_mode, 0444,
468 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
470static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
471 struct device_attribute *attr, char *buf)
472{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100473 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100474
David Brownell4fca9542008-11-11 17:38:53 -0800475 return regulator_print_opmode(buf,
476 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477}
David Brownell7ad68e22008-11-11 17:39:02 -0800478static DEVICE_ATTR(suspend_disk_mode, 0444,
479 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
481static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
482 struct device_attribute *attr, char *buf)
483{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100484 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100485
David Brownell4fca9542008-11-11 17:38:53 -0800486 return regulator_print_opmode(buf,
487 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488}
David Brownell7ad68e22008-11-11 17:39:02 -0800489static DEVICE_ATTR(suspend_standby_mode, 0444,
490 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
492static ssize_t regulator_suspend_mem_state_show(struct device *dev,
493 struct device_attribute *attr, char *buf)
494{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100495 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100496
David Brownell4fca9542008-11-11 17:38:53 -0800497 return regulator_print_state(buf,
498 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(suspend_mem_state, 0444,
501 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
503static ssize_t regulator_suspend_disk_state_show(struct device *dev,
504 struct device_attribute *attr, char *buf)
505{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100506 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100507
David Brownell4fca9542008-11-11 17:38:53 -0800508 return regulator_print_state(buf,
509 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_disk_state, 0444,
512 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100513
514static ssize_t regulator_suspend_standby_state_show(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100517 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518
David Brownell4fca9542008-11-11 17:38:53 -0800519 return regulator_print_state(buf,
520 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521}
David Brownell7ad68e22008-11-11 17:39:02 -0800522static DEVICE_ATTR(suspend_standby_state, 0444,
523 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100524
David Brownell7ad68e22008-11-11 17:39:02 -0800525
526/*
527 * These are the only attributes are present for all regulators.
528 * Other attributes are a function of regulator functionality.
529 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100530static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100531 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100532 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
533 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100534 __ATTR_NULL,
535};
536
537static void regulator_dev_release(struct device *dev)
538{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100539 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100540 kfree(rdev);
541}
542
543static struct class regulator_class = {
544 .name = "regulator",
545 .dev_release = regulator_dev_release,
546 .dev_attrs = regulator_dev_attrs,
547};
548
549/* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551static void drms_uA_update(struct regulator_dev *rdev)
552{
553 struct regulator *sibling;
554 int current_uA = 0, output_uV, input_uV, err;
555 unsigned int mode;
556
557 err = regulator_check_drms(rdev);
558 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300559 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
560 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100561
562 /* get output voltage */
563 output_uV = rdev->desc->ops->get_voltage(rdev);
564 if (output_uV <= 0)
565 return;
566
567 /* get input voltage */
568 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
570 else
571 input_uV = rdev->constraints->input_uV;
572 if (input_uV <= 0)
573 return;
574
575 /* calc total requested load */
576 list_for_each_entry(sibling, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +0100577 current_uA += sibling->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100578
579 /* now get the optimum mode for our new total regulator load */
580 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581 output_uV, current_uA);
582
583 /* check the new mode is allowed */
584 err = regulator_check_mode(rdev, mode);
585 if (err == 0)
586 rdev->desc->ops->set_mode(rdev, mode);
587}
588
589static int suspend_set_state(struct regulator_dev *rdev,
590 struct regulator_state *rstate)
591{
592 int ret = 0;
Mark Brown638f85c2009-10-22 16:31:33 +0100593 bool can_set_state;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100594
Mark Brown638f85c2009-10-22 16:31:33 +0100595 can_set_state = rdev->desc->ops->set_suspend_enable &&
596 rdev->desc->ops->set_suspend_disable;
597
598 /* If we have no suspend mode configration don't set anything;
599 * only warn if the driver actually makes the suspend mode
600 * configurable.
601 */
602 if (!rstate->enabled && !rstate->disabled) {
603 if (can_set_state)
604 printk(KERN_WARNING "%s: No configuration for %s\n",
605 __func__, rdev_get_name(rdev));
606 return 0;
607 }
608
609 if (rstate->enabled && rstate->disabled) {
610 printk(KERN_ERR "%s: invalid configuration for %s\n",
611 __func__, rdev_get_name(rdev));
612 return -EINVAL;
613 }
614
615 if (!can_set_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100616 printk(KERN_ERR "%s: no way to set suspend state\n",
617 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100618 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100619 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100620
621 if (rstate->enabled)
622 ret = rdev->desc->ops->set_suspend_enable(rdev);
623 else
624 ret = rdev->desc->ops->set_suspend_disable(rdev);
625 if (ret < 0) {
626 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
627 return ret;
628 }
629
630 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
631 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
632 if (ret < 0) {
633 printk(KERN_ERR "%s: failed to set voltage\n",
634 __func__);
635 return ret;
636 }
637 }
638
639 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
640 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
641 if (ret < 0) {
642 printk(KERN_ERR "%s: failed to set mode\n", __func__);
643 return ret;
644 }
645 }
646 return ret;
647}
648
649/* locks held by caller */
650static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
651{
652 if (!rdev->constraints)
653 return -EINVAL;
654
655 switch (state) {
656 case PM_SUSPEND_STANDBY:
657 return suspend_set_state(rdev,
658 &rdev->constraints->state_standby);
659 case PM_SUSPEND_MEM:
660 return suspend_set_state(rdev,
661 &rdev->constraints->state_mem);
662 case PM_SUSPEND_MAX:
663 return suspend_set_state(rdev,
664 &rdev->constraints->state_disk);
665 default:
666 return -EINVAL;
667 }
668}
669
670static void print_constraints(struct regulator_dev *rdev)
671{
672 struct regulation_constraints *constraints = rdev->constraints;
Mark Brown973e9a22010-02-11 19:20:48 +0000673 char buf[80] = "";
Mark Brown8f031b42009-10-22 16:31:31 +0100674 int count = 0;
675 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100676
Mark Brown8f031b42009-10-22 16:31:31 +0100677 if (constraints->min_uV && constraints->max_uV) {
Liam Girdwood414c70c2008-04-30 15:59:04 +0100678 if (constraints->min_uV == constraints->max_uV)
Mark Brown8f031b42009-10-22 16:31:31 +0100679 count += sprintf(buf + count, "%d mV ",
680 constraints->min_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100681 else
Mark Brown8f031b42009-10-22 16:31:31 +0100682 count += sprintf(buf + count, "%d <--> %d mV ",
683 constraints->min_uV / 1000,
684 constraints->max_uV / 1000);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100685 }
Mark Brown8f031b42009-10-22 16:31:31 +0100686
687 if (!constraints->min_uV ||
688 constraints->min_uV != constraints->max_uV) {
689 ret = _regulator_get_voltage(rdev);
690 if (ret > 0)
691 count += sprintf(buf + count, "at %d mV ", ret / 1000);
692 }
693
694 if (constraints->min_uA && constraints->max_uA) {
695 if (constraints->min_uA == constraints->max_uA)
696 count += sprintf(buf + count, "%d mA ",
697 constraints->min_uA / 1000);
698 else
699 count += sprintf(buf + count, "%d <--> %d mA ",
700 constraints->min_uA / 1000,
701 constraints->max_uA / 1000);
702 }
703
704 if (!constraints->min_uA ||
705 constraints->min_uA != constraints->max_uA) {
706 ret = _regulator_get_current_limit(rdev);
707 if (ret > 0)
Cyril Chemparathye4a63762010-09-22 12:30:15 -0400708 count += sprintf(buf + count, "at %d mA ", ret / 1000);
Mark Brown8f031b42009-10-22 16:31:31 +0100709 }
710
Liam Girdwood414c70c2008-04-30 15:59:04 +0100711 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
712 count += sprintf(buf + count, "fast ");
713 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
714 count += sprintf(buf + count, "normal ");
715 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
716 count += sprintf(buf + count, "idle ");
717 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
718 count += sprintf(buf + count, "standby");
719
Mark Brown1083c392009-10-22 16:31:32 +0100720 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100721}
722
Mark Browne79055d2009-10-19 15:53:50 +0100723static int machine_constraints_voltage(struct regulator_dev *rdev,
Mark Brown1083c392009-10-22 16:31:32 +0100724 struct regulation_constraints *constraints)
Mark Browne79055d2009-10-19 15:53:50 +0100725{
726 struct regulator_ops *ops = rdev->desc->ops;
Mark Brown1083c392009-10-22 16:31:32 +0100727 const char *name = rdev_get_name(rdev);
Mark Brownaf5866c2009-10-22 16:31:30 +0100728 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +0000729 unsigned selector;
Mark Brownaf5866c2009-10-22 16:31:30 +0100730
731 /* do we need to apply the constraint voltage */
732 if (rdev->constraints->apply_uV &&
733 rdev->constraints->min_uV == rdev->constraints->max_uV &&
734 ops->set_voltage) {
735 ret = ops->set_voltage(rdev,
Mark Brown3a93f2a2010-11-10 14:38:29 +0000736 rdev->constraints->min_uV,
737 rdev->constraints->max_uV,
738 &selector);
Mark Brownaf5866c2009-10-22 16:31:30 +0100739 if (ret < 0) {
740 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
741 __func__,
742 rdev->constraints->min_uV, name);
743 rdev->constraints = NULL;
744 return ret;
745 }
746 }
Mark Browne79055d2009-10-19 15:53:50 +0100747
748 /* constrain machine-level voltage specs to fit
749 * the actual range supported by this regulator.
750 */
751 if (ops->list_voltage && rdev->desc->n_voltages) {
752 int count = rdev->desc->n_voltages;
753 int i;
754 int min_uV = INT_MAX;
755 int max_uV = INT_MIN;
756 int cmin = constraints->min_uV;
757 int cmax = constraints->max_uV;
758
759 /* it's safe to autoconfigure fixed-voltage supplies
760 and the constraints are used by list_voltage. */
761 if (count == 1 && !cmin) {
762 cmin = 1;
763 cmax = INT_MAX;
764 constraints->min_uV = cmin;
765 constraints->max_uV = cmax;
766 }
767
768 /* voltage constraints are optional */
769 if ((cmin == 0) && (cmax == 0))
770 return 0;
771
772 /* else require explicit machine-level constraints */
773 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
774 pr_err("%s: %s '%s' voltage constraints\n",
775 __func__, "invalid", name);
776 return -EINVAL;
777 }
778
779 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
780 for (i = 0; i < count; i++) {
781 int value;
782
783 value = ops->list_voltage(rdev, i);
784 if (value <= 0)
785 continue;
786
787 /* maybe adjust [min_uV..max_uV] */
788 if (value >= cmin && value < min_uV)
789 min_uV = value;
790 if (value <= cmax && value > max_uV)
791 max_uV = value;
792 }
793
794 /* final: [min_uV..max_uV] valid iff constraints valid */
795 if (max_uV < min_uV) {
796 pr_err("%s: %s '%s' voltage constraints\n",
797 __func__, "unsupportable", name);
798 return -EINVAL;
799 }
800
801 /* use regulator's subset of machine constraints */
802 if (constraints->min_uV < min_uV) {
803 pr_debug("%s: override '%s' %s, %d -> %d\n",
804 __func__, name, "min_uV",
805 constraints->min_uV, min_uV);
806 constraints->min_uV = min_uV;
807 }
808 if (constraints->max_uV > max_uV) {
809 pr_debug("%s: override '%s' %s, %d -> %d\n",
810 __func__, name, "max_uV",
811 constraints->max_uV, max_uV);
812 constraints->max_uV = max_uV;
813 }
814 }
815
816 return 0;
817}
818
Liam Girdwooda5766f12008-10-10 13:22:20 +0100819/**
820 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000821 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000822 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100823 *
824 * Allows platform initialisation code to define and constrain
825 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
826 * Constraints *must* be set by platform code in order for some
827 * regulator operations to proceed i.e. set_voltage, set_current_limit,
828 * set_mode.
829 */
830static int set_machine_constraints(struct regulator_dev *rdev,
831 struct regulation_constraints *constraints)
832{
833 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100834 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100835 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100836
Mark Brownaf5866c2009-10-22 16:31:30 +0100837 rdev->constraints = constraints;
838
Mark Brown1083c392009-10-22 16:31:32 +0100839 name = rdev_get_name(rdev);
840
841 ret = machine_constraints_voltage(rdev, constraints);
Mark Browne79055d2009-10-19 15:53:50 +0100842 if (ret != 0)
843 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800844
Liam Girdwooda5766f12008-10-10 13:22:20 +0100845 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100846 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100847 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100848 if (ret < 0) {
849 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
850 __func__, name);
851 rdev->constraints = NULL;
852 goto out;
853 }
854 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100855
Mark Browna3084662009-02-26 19:24:19 +0000856 if (constraints->initial_mode) {
857 if (!ops->set_mode) {
858 printk(KERN_ERR "%s: no set_mode operation for %s\n",
859 __func__, name);
860 ret = -EINVAL;
861 goto out;
862 }
863
864 ret = ops->set_mode(rdev, constraints->initial_mode);
865 if (ret < 0) {
866 printk(KERN_ERR
867 "%s: failed to set initial mode for %s: %d\n",
868 __func__, name, ret);
869 goto out;
870 }
871 }
872
Mark Browncacf90f2009-03-02 16:32:46 +0000873 /* If the constraints say the regulator should be on at this point
874 * and we have control then make sure it is enabled.
875 */
876 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100877 ret = ops->enable(rdev);
878 if (ret < 0) {
879 printk(KERN_ERR "%s: failed to enable %s\n",
880 __func__, name);
881 rdev->constraints = NULL;
882 goto out;
883 }
884 }
885
Liam Girdwooda5766f12008-10-10 13:22:20 +0100886 print_constraints(rdev);
887out:
888 return ret;
889}
890
891/**
892 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000893 * @rdev: regulator name
894 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100895 *
896 * Called by platform initialisation code to set the supply regulator for this
897 * regulator. This ensures that a regulators supply will also be enabled by the
898 * core if it's child is enabled.
899 */
900static int set_supply(struct regulator_dev *rdev,
901 struct regulator_dev *supply_rdev)
902{
903 int err;
904
905 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
906 "supply");
907 if (err) {
908 printk(KERN_ERR
909 "%s: could not add device link %s err %d\n",
910 __func__, supply_rdev->dev.kobj.name, err);
911 goto out;
912 }
913 rdev->supply = supply_rdev;
914 list_add(&rdev->slist, &supply_rdev->supply_list);
915out:
916 return err;
917}
918
919/**
Randy Dunlap06c63f92010-11-18 15:02:26 -0800920 * set_consumer_device_supply - Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000921 * @rdev: regulator source
922 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100923 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000924 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925 *
926 * Allows platform initialisation code to map physical regulator
927 * sources to symbolic names for supplies for use by devices. Devices
928 * should use these symbolic names to request regulators, avoiding the
929 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100930 *
931 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100932 */
933static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100934 struct device *consumer_dev, const char *consumer_dev_name,
935 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936{
937 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100938 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100939
Mark Brown40f92442009-06-17 17:56:39 +0100940 if (consumer_dev && consumer_dev_name)
941 return -EINVAL;
942
943 if (!consumer_dev_name && consumer_dev)
944 consumer_dev_name = dev_name(consumer_dev);
945
Liam Girdwooda5766f12008-10-10 13:22:20 +0100946 if (supply == NULL)
947 return -EINVAL;
948
Mark Brown9ed20992009-07-21 16:00:26 +0100949 if (consumer_dev_name != NULL)
950 has_dev = 1;
951 else
952 has_dev = 0;
953
David Brownell6001e132008-12-31 12:54:19 +0000954 list_for_each_entry(node, &regulator_map_list, list) {
Jani Nikula23b5cc22010-04-29 10:55:09 +0300955 if (node->dev_name && consumer_dev_name) {
956 if (strcmp(node->dev_name, consumer_dev_name) != 0)
957 continue;
958 } else if (node->dev_name || consumer_dev_name) {
David Brownell6001e132008-12-31 12:54:19 +0000959 continue;
Jani Nikula23b5cc22010-04-29 10:55:09 +0300960 }
961
David Brownell6001e132008-12-31 12:54:19 +0000962 if (strcmp(node->supply, supply) != 0)
963 continue;
964
965 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
966 dev_name(&node->regulator->dev),
967 node->regulator->desc->name,
968 supply,
Mark Brown1083c392009-10-22 16:31:32 +0100969 dev_name(&rdev->dev), rdev_get_name(rdev));
David Brownell6001e132008-12-31 12:54:19 +0000970 return -EBUSY;
971 }
972
Mark Brown9ed20992009-07-21 16:00:26 +0100973 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100974 if (node == NULL)
975 return -ENOMEM;
976
977 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100978 node->supply = supply;
979
Mark Brown9ed20992009-07-21 16:00:26 +0100980 if (has_dev) {
981 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
982 if (node->dev_name == NULL) {
983 kfree(node);
984 return -ENOMEM;
985 }
Mark Brown40f92442009-06-17 17:56:39 +0100986 }
987
Liam Girdwooda5766f12008-10-10 13:22:20 +0100988 list_add(&node->list, &regulator_map_list);
989 return 0;
990}
991
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200992static void unset_regulator_supplies(struct regulator_dev *rdev)
993{
994 struct regulator_map *node, *n;
995
996 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
997 if (rdev == node->regulator) {
998 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100999 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001000 kfree(node);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02001001 }
1002 }
1003}
1004
Liam Girdwood414c70c2008-04-30 15:59:04 +01001005#define REG_STR_SIZE 32
1006
1007static struct regulator *create_regulator(struct regulator_dev *rdev,
1008 struct device *dev,
1009 const char *supply_name)
1010{
1011 struct regulator *regulator;
1012 char buf[REG_STR_SIZE];
1013 int err, size;
1014
1015 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1016 if (regulator == NULL)
1017 return NULL;
1018
1019 mutex_lock(&rdev->mutex);
1020 regulator->rdev = rdev;
1021 list_add(&regulator->list, &rdev->consumer_list);
1022
1023 if (dev) {
1024 /* create a 'requested_microamps_name' sysfs entry */
1025 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1026 supply_name);
1027 if (size >= REG_STR_SIZE)
1028 goto overflow_err;
1029
1030 regulator->dev = dev;
Ameya Palande4f26a2a2010-03-12 20:09:01 +02001031 sysfs_attr_init(&regulator->dev_attr.attr);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001032 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1033 if (regulator->dev_attr.attr.name == NULL)
1034 goto attr_name_err;
1035
Liam Girdwood414c70c2008-04-30 15:59:04 +01001036 regulator->dev_attr.attr.mode = 0444;
1037 regulator->dev_attr.show = device_requested_uA_show;
1038 err = device_create_file(dev, &regulator->dev_attr);
1039 if (err < 0) {
1040 printk(KERN_WARNING "%s: could not add regulator_dev"
1041 " load sysfs\n", __func__);
1042 goto attr_name_err;
1043 }
1044
1045 /* also add a link to the device sysfs entry */
1046 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1047 dev->kobj.name, supply_name);
1048 if (size >= REG_STR_SIZE)
1049 goto attr_err;
1050
1051 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1052 if (regulator->supply_name == NULL)
1053 goto attr_err;
1054
1055 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1056 buf);
1057 if (err) {
1058 printk(KERN_WARNING
1059 "%s: could not add device link %s err %d\n",
1060 __func__, dev->kobj.name, err);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001061 goto link_name_err;
1062 }
1063 }
1064 mutex_unlock(&rdev->mutex);
1065 return regulator;
1066link_name_err:
1067 kfree(regulator->supply_name);
1068attr_err:
1069 device_remove_file(regulator->dev, &regulator->dev_attr);
1070attr_name_err:
1071 kfree(regulator->dev_attr.attr.name);
1072overflow_err:
1073 list_del(&regulator->list);
1074 kfree(regulator);
1075 mutex_unlock(&rdev->mutex);
1076 return NULL;
1077}
1078
Mark Brown31aae2b2009-12-21 12:21:52 +00001079static int _regulator_get_enable_time(struct regulator_dev *rdev)
1080{
1081 if (!rdev->desc->ops->enable_time)
1082 return 0;
1083 return rdev->desc->ops->enable_time(rdev);
1084}
1085
Mark Brown5ffbd132009-07-21 16:00:23 +01001086/* Internal regulator request function */
1087static struct regulator *_regulator_get(struct device *dev, const char *id,
1088 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089{
1090 struct regulator_dev *rdev;
1091 struct regulator_map *map;
1092 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001093 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001094 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001095
1096 if (id == NULL) {
1097 printk(KERN_ERR "regulator: get() with no identifier\n");
1098 return regulator;
1099 }
1100
Mark Brown40f92442009-06-17 17:56:39 +01001101 if (dev)
1102 devname = dev_name(dev);
1103
Liam Girdwood414c70c2008-04-30 15:59:04 +01001104 mutex_lock(&regulator_list_mutex);
1105
1106 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001107 /* If the mapping has a device set up it must match */
1108 if (map->dev_name &&
1109 (!devname || strcmp(map->dev_name, devname)))
1110 continue;
1111
1112 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001113 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001114 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001115 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116 }
Mark Brown34abbd62010-02-12 10:18:08 +00001117
Mark Brown688fe992010-10-05 19:18:32 -07001118 if (board_wants_dummy_regulator) {
1119 rdev = dummy_regulator_rdev;
1120 goto found;
1121 }
1122
Mark Brown34abbd62010-02-12 10:18:08 +00001123#ifdef CONFIG_REGULATOR_DUMMY
1124 if (!devname)
1125 devname = "deviceless";
1126
1127 /* If the board didn't flag that it was fully constrained then
1128 * substitute in a dummy regulator so consumers can continue.
1129 */
1130 if (!has_full_constraints) {
1131 pr_warning("%s supply %s not found, using dummy regulator\n",
1132 devname, id);
1133 rdev = dummy_regulator_rdev;
1134 goto found;
1135 }
1136#endif
1137
Liam Girdwood414c70c2008-04-30 15:59:04 +01001138 mutex_unlock(&regulator_list_mutex);
1139 return regulator;
1140
1141found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001142 if (rdev->exclusive) {
1143 regulator = ERR_PTR(-EPERM);
1144 goto out;
1145 }
1146
1147 if (exclusive && rdev->open_count) {
1148 regulator = ERR_PTR(-EBUSY);
1149 goto out;
1150 }
1151
Liam Girdwooda5766f12008-10-10 13:22:20 +01001152 if (!try_module_get(rdev->owner))
1153 goto out;
1154
Liam Girdwood414c70c2008-04-30 15:59:04 +01001155 regulator = create_regulator(rdev, dev, id);
1156 if (regulator == NULL) {
1157 regulator = ERR_PTR(-ENOMEM);
1158 module_put(rdev->owner);
1159 }
1160
Mark Brown5ffbd132009-07-21 16:00:23 +01001161 rdev->open_count++;
1162 if (exclusive) {
1163 rdev->exclusive = 1;
1164
1165 ret = _regulator_is_enabled(rdev);
1166 if (ret > 0)
1167 rdev->use_count = 1;
1168 else
1169 rdev->use_count = 0;
1170 }
1171
Liam Girdwooda5766f12008-10-10 13:22:20 +01001172out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001173 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001174
Liam Girdwood414c70c2008-04-30 15:59:04 +01001175 return regulator;
1176}
Mark Brown5ffbd132009-07-21 16:00:23 +01001177
1178/**
1179 * regulator_get - lookup and obtain a reference to a regulator.
1180 * @dev: device for regulator "consumer"
1181 * @id: Supply name or regulator ID.
1182 *
1183 * Returns a struct regulator corresponding to the regulator producer,
1184 * or IS_ERR() condition containing errno.
1185 *
1186 * Use of supply names configured via regulator_set_device_supply() is
1187 * strongly encouraged. It is recommended that the supply name used
1188 * should match the name used for the supply and/or the relevant
1189 * device pins in the datasheet.
1190 */
1191struct regulator *regulator_get(struct device *dev, const char *id)
1192{
1193 return _regulator_get(dev, id, 0);
1194}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001195EXPORT_SYMBOL_GPL(regulator_get);
1196
1197/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001198 * regulator_get_exclusive - obtain exclusive access to a regulator.
1199 * @dev: device for regulator "consumer"
1200 * @id: Supply name or regulator ID.
1201 *
1202 * Returns a struct regulator corresponding to the regulator producer,
1203 * or IS_ERR() condition containing errno. Other consumers will be
1204 * unable to obtain this reference is held and the use count for the
1205 * regulator will be initialised to reflect the current state of the
1206 * regulator.
1207 *
1208 * This is intended for use by consumers which cannot tolerate shared
1209 * use of the regulator such as those which need to force the
1210 * regulator off for correct operation of the hardware they are
1211 * controlling.
1212 *
1213 * Use of supply names configured via regulator_set_device_supply() is
1214 * strongly encouraged. It is recommended that the supply name used
1215 * should match the name used for the supply and/or the relevant
1216 * device pins in the datasheet.
1217 */
1218struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1219{
1220 return _regulator_get(dev, id, 1);
1221}
1222EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1223
1224/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001225 * regulator_put - "free" the regulator source
1226 * @regulator: regulator source
1227 *
1228 * Note: drivers must ensure that all regulator_enable calls made on this
1229 * regulator source are balanced by regulator_disable calls prior to calling
1230 * this function.
1231 */
1232void regulator_put(struct regulator *regulator)
1233{
1234 struct regulator_dev *rdev;
1235
1236 if (regulator == NULL || IS_ERR(regulator))
1237 return;
1238
Liam Girdwood414c70c2008-04-30 15:59:04 +01001239 mutex_lock(&regulator_list_mutex);
1240 rdev = regulator->rdev;
1241
1242 /* remove any sysfs entries */
1243 if (regulator->dev) {
1244 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1245 kfree(regulator->supply_name);
1246 device_remove_file(regulator->dev, &regulator->dev_attr);
1247 kfree(regulator->dev_attr.attr.name);
1248 }
1249 list_del(&regulator->list);
1250 kfree(regulator);
1251
Mark Brown5ffbd132009-07-21 16:00:23 +01001252 rdev->open_count--;
1253 rdev->exclusive = 0;
1254
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255 module_put(rdev->owner);
1256 mutex_unlock(&regulator_list_mutex);
1257}
1258EXPORT_SYMBOL_GPL(regulator_put);
1259
Mark Brown9a2372f2009-08-03 18:49:57 +01001260static int _regulator_can_change_status(struct regulator_dev *rdev)
1261{
1262 if (!rdev->constraints)
1263 return 0;
1264
1265 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1266 return 1;
1267 else
1268 return 0;
1269}
1270
Liam Girdwood414c70c2008-04-30 15:59:04 +01001271/* locks held by regulator_enable() */
1272static int _regulator_enable(struct regulator_dev *rdev)
1273{
Mark Brown31aae2b2009-12-21 12:21:52 +00001274 int ret, delay;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001275
Mattias Wallin63cee942010-11-04 11:01:31 +01001276 /* do we need to enable the supply regulator first */
1277 if (rdev->supply) {
1278 mutex_lock(&rdev->supply->mutex);
1279 ret = _regulator_enable(rdev->supply);
1280 mutex_unlock(&rdev->supply->mutex);
1281 if (ret < 0) {
1282 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1283 __func__, rdev_get_name(rdev), ret);
1284 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001285 }
1286 }
1287
1288 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001289 if (rdev->constraints &&
1290 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1291 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001292
Mark Brown9a2372f2009-08-03 18:49:57 +01001293 if (rdev->use_count == 0) {
1294 /* The regulator may on if it's not switchable or left on */
1295 ret = _regulator_is_enabled(rdev);
1296 if (ret == -EINVAL || ret == 0) {
1297 if (!_regulator_can_change_status(rdev))
1298 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001299
Mark Brown31aae2b2009-12-21 12:21:52 +00001300 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001301 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001302
1303 /* Query before enabling in case configuration
1304 * dependant. */
1305 ret = _regulator_get_enable_time(rdev);
1306 if (ret >= 0) {
1307 delay = ret;
1308 } else {
1309 printk(KERN_WARNING
1310 "%s: enable_time() failed for %s: %d\n",
1311 __func__, rdev_get_name(rdev),
1312 ret);
1313 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001314 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001315
Mark Brown02fa3ec2010-11-10 14:38:30 +00001316 trace_regulator_enable(rdev_get_name(rdev));
1317
Mark Brown31aae2b2009-12-21 12:21:52 +00001318 /* Allow the regulator to ramp; it would be useful
1319 * to extend this for bulk operations so that the
1320 * regulators can ramp together. */
1321 ret = rdev->desc->ops->enable(rdev);
1322 if (ret < 0)
1323 return ret;
1324
Mark Brown02fa3ec2010-11-10 14:38:30 +00001325 trace_regulator_enable_delay(rdev_get_name(rdev));
1326
Axel Line36c1df2010-11-05 21:51:32 +08001327 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001328 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001329 udelay(delay % 1000);
1330 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001331 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001332 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001333
Mark Brown02fa3ec2010-11-10 14:38:30 +00001334 trace_regulator_enable_complete(rdev_get_name(rdev));
1335
Linus Walleija7433cf2009-08-26 12:54:04 +02001336 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001337 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001338 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001339 return ret;
1340 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001341 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001342 }
1343
Mark Brown9a2372f2009-08-03 18:49:57 +01001344 rdev->use_count++;
1345
1346 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347}
1348
1349/**
1350 * regulator_enable - enable regulator output
1351 * @regulator: regulator source
1352 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001353 * Request that the regulator be enabled with the regulator output at
1354 * the predefined voltage or current value. Calls to regulator_enable()
1355 * must be balanced with calls to regulator_disable().
1356 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001357 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001358 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 */
1360int regulator_enable(struct regulator *regulator)
1361{
David Brownell412aec62008-11-16 11:44:46 -08001362 struct regulator_dev *rdev = regulator->rdev;
1363 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001364
David Brownell412aec62008-11-16 11:44:46 -08001365 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001366 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001367 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001368 return ret;
1369}
1370EXPORT_SYMBOL_GPL(regulator_enable);
1371
1372/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001373static int _regulator_disable(struct regulator_dev *rdev,
1374 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001375{
1376 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001377 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378
David Brownellcd94b502009-03-11 16:43:34 -08001379 if (WARN(rdev->use_count <= 0,
1380 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001381 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001382 return -EIO;
1383
Liam Girdwood414c70c2008-04-30 15:59:04 +01001384 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001385 if (rdev->use_count == 1 &&
1386 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387
1388 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001389 if (_regulator_can_change_status(rdev) &&
1390 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001391 trace_regulator_disable(rdev_get_name(rdev));
1392
Liam Girdwood414c70c2008-04-30 15:59:04 +01001393 ret = rdev->desc->ops->disable(rdev);
1394 if (ret < 0) {
1395 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001396 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001397 return ret;
1398 }
Mark Brown84b68262009-12-01 21:12:27 +00001399
Mark Brown02fa3ec2010-11-10 14:38:30 +00001400 trace_regulator_disable_complete(rdev_get_name(rdev));
1401
Mark Brown84b68262009-12-01 21:12:27 +00001402 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1403 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404 }
1405
1406 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001407 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001408
1409 rdev->use_count = 0;
1410 } else if (rdev->use_count > 1) {
1411
1412 if (rdev->constraints &&
1413 (rdev->constraints->valid_ops_mask &
1414 REGULATOR_CHANGE_DRMS))
1415 drms_uA_update(rdev);
1416
1417 rdev->use_count--;
1418 }
1419 return ret;
1420}
1421
1422/**
1423 * regulator_disable - disable regulator output
1424 * @regulator: regulator source
1425 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001426 * Disable the regulator output voltage or current. Calls to
1427 * regulator_enable() must be balanced with calls to
1428 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001429 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001430 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001431 * devices have it enabled, the regulator device supports disabling and
1432 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001433 */
1434int regulator_disable(struct regulator *regulator)
1435{
David Brownell412aec62008-11-16 11:44:46 -08001436 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001437 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001438 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001439
David Brownell412aec62008-11-16 11:44:46 -08001440 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001441 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001442 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001443
1444 /* decrease our supplies ref count and disable if required */
1445 while (supply_rdev != NULL) {
1446 rdev = supply_rdev;
1447
1448 mutex_lock(&rdev->mutex);
1449 _regulator_disable(rdev, &supply_rdev);
1450 mutex_unlock(&rdev->mutex);
1451 }
1452
Liam Girdwood414c70c2008-04-30 15:59:04 +01001453 return ret;
1454}
1455EXPORT_SYMBOL_GPL(regulator_disable);
1456
1457/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001458static int _regulator_force_disable(struct regulator_dev *rdev,
1459 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460{
1461 int ret = 0;
1462
1463 /* force disable */
1464 if (rdev->desc->ops->disable) {
1465 /* ah well, who wants to live forever... */
1466 ret = rdev->desc->ops->disable(rdev);
1467 if (ret < 0) {
1468 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001469 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001470 return ret;
1471 }
1472 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001473 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1474 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001475 }
1476
1477 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001478 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001479
1480 rdev->use_count = 0;
1481 return ret;
1482}
1483
1484/**
1485 * regulator_force_disable - force disable regulator output
1486 * @regulator: regulator source
1487 *
1488 * Forcibly disable the regulator output voltage or current.
1489 * NOTE: this *will* disable the regulator output even if other consumer
1490 * devices have it enabled. This should be used for situations when device
1491 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1492 */
1493int regulator_force_disable(struct regulator *regulator)
1494{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001495 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001496 int ret;
1497
1498 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001499 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001500 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001502
1503 if (supply_rdev)
1504 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1505
Liam Girdwood414c70c2008-04-30 15:59:04 +01001506 return ret;
1507}
1508EXPORT_SYMBOL_GPL(regulator_force_disable);
1509
1510static int _regulator_is_enabled(struct regulator_dev *rdev)
1511{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001512 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001513 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001514 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001515
Mark Brown93325462009-08-03 18:49:56 +01001516 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517}
1518
1519/**
1520 * regulator_is_enabled - is the regulator output enabled
1521 * @regulator: regulator source
1522 *
David Brownell412aec62008-11-16 11:44:46 -08001523 * Returns positive if the regulator driver backing the source/client
1524 * has requested that the device be enabled, zero if it hasn't, else a
1525 * negative errno code.
1526 *
1527 * Note that the device backing this regulator handle can have multiple
1528 * users, so it might be enabled even if regulator_enable() was never
1529 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001530 */
1531int regulator_is_enabled(struct regulator *regulator)
1532{
Mark Brown93325462009-08-03 18:49:56 +01001533 int ret;
1534
1535 mutex_lock(&regulator->rdev->mutex);
1536 ret = _regulator_is_enabled(regulator->rdev);
1537 mutex_unlock(&regulator->rdev->mutex);
1538
1539 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001540}
1541EXPORT_SYMBOL_GPL(regulator_is_enabled);
1542
1543/**
David Brownell4367cfd2009-02-26 11:48:36 -08001544 * regulator_count_voltages - count regulator_list_voltage() selectors
1545 * @regulator: regulator source
1546 *
1547 * Returns number of selectors, or negative errno. Selectors are
1548 * numbered starting at zero, and typically correspond to bitfields
1549 * in hardware registers.
1550 */
1551int regulator_count_voltages(struct regulator *regulator)
1552{
1553 struct regulator_dev *rdev = regulator->rdev;
1554
1555 return rdev->desc->n_voltages ? : -EINVAL;
1556}
1557EXPORT_SYMBOL_GPL(regulator_count_voltages);
1558
1559/**
1560 * regulator_list_voltage - enumerate supported voltages
1561 * @regulator: regulator source
1562 * @selector: identify voltage to list
1563 * Context: can sleep
1564 *
1565 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001566 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001567 * negative errno.
1568 */
1569int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1570{
1571 struct regulator_dev *rdev = regulator->rdev;
1572 struct regulator_ops *ops = rdev->desc->ops;
1573 int ret;
1574
1575 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1576 return -EINVAL;
1577
1578 mutex_lock(&rdev->mutex);
1579 ret = ops->list_voltage(rdev, selector);
1580 mutex_unlock(&rdev->mutex);
1581
1582 if (ret > 0) {
1583 if (ret < rdev->constraints->min_uV)
1584 ret = 0;
1585 else if (ret > rdev->constraints->max_uV)
1586 ret = 0;
1587 }
1588
1589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(regulator_list_voltage);
1592
1593/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001594 * regulator_is_supported_voltage - check if a voltage range can be supported
1595 *
1596 * @regulator: Regulator to check.
1597 * @min_uV: Minimum required voltage in uV.
1598 * @max_uV: Maximum required voltage in uV.
1599 *
1600 * Returns a boolean or a negative error code.
1601 */
1602int regulator_is_supported_voltage(struct regulator *regulator,
1603 int min_uV, int max_uV)
1604{
1605 int i, voltages, ret;
1606
1607 ret = regulator_count_voltages(regulator);
1608 if (ret < 0)
1609 return ret;
1610 voltages = ret;
1611
1612 for (i = 0; i < voltages; i++) {
1613 ret = regulator_list_voltage(regulator, i);
1614
1615 if (ret >= min_uV && ret <= max_uV)
1616 return 1;
1617 }
1618
1619 return 0;
1620}
1621
1622/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001623 * regulator_set_voltage - set regulator output voltage
1624 * @regulator: regulator source
1625 * @min_uV: Minimum required voltage in uV
1626 * @max_uV: Maximum acceptable voltage in uV
1627 *
1628 * Sets a voltage regulator to the desired output voltage. This can be set
1629 * during any regulator state. IOW, regulator can be disabled or enabled.
1630 *
1631 * If the regulator is enabled then the voltage will change to the new value
1632 * immediately otherwise if the regulator is disabled the regulator will
1633 * output at the new voltage when enabled.
1634 *
1635 * NOTE: If the regulator is shared between several devices then the lowest
1636 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001637 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001638 * calling this function otherwise this call will fail.
1639 */
1640int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1641{
1642 struct regulator_dev *rdev = regulator->rdev;
1643 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001644 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001645
1646 mutex_lock(&rdev->mutex);
1647
1648 /* sanity check */
1649 if (!rdev->desc->ops->set_voltage) {
1650 ret = -EINVAL;
1651 goto out;
1652 }
1653
1654 /* constraints check */
1655 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1656 if (ret < 0)
1657 goto out;
1658 regulator->min_uV = min_uV;
1659 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001660
Mark Brown02fa3ec2010-11-10 14:38:30 +00001661 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1662
Mark Brown3a93f2a2010-11-10 14:38:29 +00001663 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1664
1665 if (rdev->desc->ops->list_voltage)
1666 selector = rdev->desc->ops->list_voltage(rdev, selector);
1667 else
1668 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001669
Mark Brown02fa3ec2010-11-10 14:38:30 +00001670 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1671
Liam Girdwood414c70c2008-04-30 15:59:04 +01001672out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001673 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674 mutex_unlock(&rdev->mutex);
1675 return ret;
1676}
1677EXPORT_SYMBOL_GPL(regulator_set_voltage);
1678
1679static int _regulator_get_voltage(struct regulator_dev *rdev)
1680{
1681 /* sanity check */
1682 if (rdev->desc->ops->get_voltage)
1683 return rdev->desc->ops->get_voltage(rdev);
1684 else
1685 return -EINVAL;
1686}
1687
1688/**
1689 * regulator_get_voltage - get regulator output voltage
1690 * @regulator: regulator source
1691 *
1692 * This returns the current regulator voltage in uV.
1693 *
1694 * NOTE: If the regulator is disabled it will return the voltage value. This
1695 * function should not be used to determine regulator state.
1696 */
1697int regulator_get_voltage(struct regulator *regulator)
1698{
1699 int ret;
1700
1701 mutex_lock(&regulator->rdev->mutex);
1702
1703 ret = _regulator_get_voltage(regulator->rdev);
1704
1705 mutex_unlock(&regulator->rdev->mutex);
1706
1707 return ret;
1708}
1709EXPORT_SYMBOL_GPL(regulator_get_voltage);
1710
1711/**
1712 * regulator_set_current_limit - set regulator output current limit
1713 * @regulator: regulator source
1714 * @min_uA: Minimuum supported current in uA
1715 * @max_uA: Maximum supported current in uA
1716 *
1717 * Sets current sink to the desired output current. This can be set during
1718 * any regulator state. IOW, regulator can be disabled or enabled.
1719 *
1720 * If the regulator is enabled then the current will change to the new value
1721 * immediately otherwise if the regulator is disabled the regulator will
1722 * output at the new current when enabled.
1723 *
1724 * NOTE: Regulator system constraints must be set for this regulator before
1725 * calling this function otherwise this call will fail.
1726 */
1727int regulator_set_current_limit(struct regulator *regulator,
1728 int min_uA, int max_uA)
1729{
1730 struct regulator_dev *rdev = regulator->rdev;
1731 int ret;
1732
1733 mutex_lock(&rdev->mutex);
1734
1735 /* sanity check */
1736 if (!rdev->desc->ops->set_current_limit) {
1737 ret = -EINVAL;
1738 goto out;
1739 }
1740
1741 /* constraints check */
1742 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1743 if (ret < 0)
1744 goto out;
1745
1746 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1747out:
1748 mutex_unlock(&rdev->mutex);
1749 return ret;
1750}
1751EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1752
1753static int _regulator_get_current_limit(struct regulator_dev *rdev)
1754{
1755 int ret;
1756
1757 mutex_lock(&rdev->mutex);
1758
1759 /* sanity check */
1760 if (!rdev->desc->ops->get_current_limit) {
1761 ret = -EINVAL;
1762 goto out;
1763 }
1764
1765 ret = rdev->desc->ops->get_current_limit(rdev);
1766out:
1767 mutex_unlock(&rdev->mutex);
1768 return ret;
1769}
1770
1771/**
1772 * regulator_get_current_limit - get regulator output current
1773 * @regulator: regulator source
1774 *
1775 * This returns the current supplied by the specified current sink in uA.
1776 *
1777 * NOTE: If the regulator is disabled it will return the current value. This
1778 * function should not be used to determine regulator state.
1779 */
1780int regulator_get_current_limit(struct regulator *regulator)
1781{
1782 return _regulator_get_current_limit(regulator->rdev);
1783}
1784EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1785
1786/**
1787 * regulator_set_mode - set regulator operating mode
1788 * @regulator: regulator source
1789 * @mode: operating mode - one of the REGULATOR_MODE constants
1790 *
1791 * Set regulator operating mode to increase regulator efficiency or improve
1792 * regulation performance.
1793 *
1794 * NOTE: Regulator system constraints must be set for this regulator before
1795 * calling this function otherwise this call will fail.
1796 */
1797int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1798{
1799 struct regulator_dev *rdev = regulator->rdev;
1800 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301801 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001802
1803 mutex_lock(&rdev->mutex);
1804
1805 /* sanity check */
1806 if (!rdev->desc->ops->set_mode) {
1807 ret = -EINVAL;
1808 goto out;
1809 }
1810
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301811 /* return if the same mode is requested */
1812 if (rdev->desc->ops->get_mode) {
1813 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1814 if (regulator_curr_mode == mode) {
1815 ret = 0;
1816 goto out;
1817 }
1818 }
1819
Liam Girdwood414c70c2008-04-30 15:59:04 +01001820 /* constraints check */
1821 ret = regulator_check_mode(rdev, mode);
1822 if (ret < 0)
1823 goto out;
1824
1825 ret = rdev->desc->ops->set_mode(rdev, mode);
1826out:
1827 mutex_unlock(&rdev->mutex);
1828 return ret;
1829}
1830EXPORT_SYMBOL_GPL(regulator_set_mode);
1831
1832static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1833{
1834 int ret;
1835
1836 mutex_lock(&rdev->mutex);
1837
1838 /* sanity check */
1839 if (!rdev->desc->ops->get_mode) {
1840 ret = -EINVAL;
1841 goto out;
1842 }
1843
1844 ret = rdev->desc->ops->get_mode(rdev);
1845out:
1846 mutex_unlock(&rdev->mutex);
1847 return ret;
1848}
1849
1850/**
1851 * regulator_get_mode - get regulator operating mode
1852 * @regulator: regulator source
1853 *
1854 * Get the current regulator operating mode.
1855 */
1856unsigned int regulator_get_mode(struct regulator *regulator)
1857{
1858 return _regulator_get_mode(regulator->rdev);
1859}
1860EXPORT_SYMBOL_GPL(regulator_get_mode);
1861
1862/**
1863 * regulator_set_optimum_mode - set regulator optimum operating mode
1864 * @regulator: regulator source
1865 * @uA_load: load current
1866 *
1867 * Notifies the regulator core of a new device load. This is then used by
1868 * DRMS (if enabled by constraints) to set the most efficient regulator
1869 * operating mode for the new regulator loading.
1870 *
1871 * Consumer devices notify their supply regulator of the maximum power
1872 * they will require (can be taken from device datasheet in the power
1873 * consumption tables) when they change operational status and hence power
1874 * state. Examples of operational state changes that can affect power
1875 * consumption are :-
1876 *
1877 * o Device is opened / closed.
1878 * o Device I/O is about to begin or has just finished.
1879 * o Device is idling in between work.
1880 *
1881 * This information is also exported via sysfs to userspace.
1882 *
1883 * DRMS will sum the total requested load on the regulator and change
1884 * to the most efficient operating mode if platform constraints allow.
1885 *
1886 * Returns the new regulator mode or error.
1887 */
1888int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1889{
1890 struct regulator_dev *rdev = regulator->rdev;
1891 struct regulator *consumer;
1892 int ret, output_uV, input_uV, total_uA_load = 0;
1893 unsigned int mode;
1894
1895 mutex_lock(&rdev->mutex);
1896
1897 regulator->uA_load = uA_load;
1898 ret = regulator_check_drms(rdev);
1899 if (ret < 0)
1900 goto out;
1901 ret = -EINVAL;
1902
1903 /* sanity check */
1904 if (!rdev->desc->ops->get_optimum_mode)
1905 goto out;
1906
1907 /* get output voltage */
1908 output_uV = rdev->desc->ops->get_voltage(rdev);
1909 if (output_uV <= 0) {
1910 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001911 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001912 goto out;
1913 }
1914
1915 /* get input voltage */
1916 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1917 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1918 else
1919 input_uV = rdev->constraints->input_uV;
1920 if (input_uV <= 0) {
1921 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001922 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001923 goto out;
1924 }
1925
1926 /* calc total requested load for this regulator */
1927 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001928 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001929
1930 mode = rdev->desc->ops->get_optimum_mode(rdev,
1931 input_uV, output_uV,
1932 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001933 ret = regulator_check_mode(rdev, mode);
1934 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001935 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001936 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001937 total_uA_load, input_uV, output_uV);
1938 goto out;
1939 }
1940
1941 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001942 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001943 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001944 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 goto out;
1946 }
1947 ret = mode;
1948out:
1949 mutex_unlock(&rdev->mutex);
1950 return ret;
1951}
1952EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1953
1954/**
1955 * regulator_register_notifier - register regulator event notifier
1956 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001957 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001958 *
1959 * Register notifier block to receive regulator events.
1960 */
1961int regulator_register_notifier(struct regulator *regulator,
1962 struct notifier_block *nb)
1963{
1964 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1965 nb);
1966}
1967EXPORT_SYMBOL_GPL(regulator_register_notifier);
1968
1969/**
1970 * regulator_unregister_notifier - unregister regulator event notifier
1971 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001972 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001973 *
1974 * Unregister regulator event notifier block.
1975 */
1976int regulator_unregister_notifier(struct regulator *regulator,
1977 struct notifier_block *nb)
1978{
1979 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1980 nb);
1981}
1982EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1983
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001984/* notify regulator consumers and downstream regulator consumers.
1985 * Note mutex must be held by caller.
1986 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001987static void _notifier_call_chain(struct regulator_dev *rdev,
1988 unsigned long event, void *data)
1989{
1990 struct regulator_dev *_rdev;
1991
1992 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001993 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001994
1995 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001996 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001997 mutex_lock(&_rdev->mutex);
1998 _notifier_call_chain(_rdev, event, data);
1999 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002000 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002001}
2002
2003/**
2004 * regulator_bulk_get - get multiple regulator consumers
2005 *
2006 * @dev: Device to supply
2007 * @num_consumers: Number of consumers to register
2008 * @consumers: Configuration of consumers; clients are stored here.
2009 *
2010 * @return 0 on success, an errno on failure.
2011 *
2012 * This helper function allows drivers to get several regulator
2013 * consumers in one operation. If any of the regulators cannot be
2014 * acquired then any regulators that were allocated will be freed
2015 * before returning to the caller.
2016 */
2017int regulator_bulk_get(struct device *dev, int num_consumers,
2018 struct regulator_bulk_data *consumers)
2019{
2020 int i;
2021 int ret;
2022
2023 for (i = 0; i < num_consumers; i++)
2024 consumers[i].consumer = NULL;
2025
2026 for (i = 0; i < num_consumers; i++) {
2027 consumers[i].consumer = regulator_get(dev,
2028 consumers[i].supply);
2029 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002030 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002031 dev_err(dev, "Failed to get supply '%s': %d\n",
2032 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002033 consumers[i].consumer = NULL;
2034 goto err;
2035 }
2036 }
2037
2038 return 0;
2039
2040err:
2041 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2042 regulator_put(consumers[i].consumer);
2043
2044 return ret;
2045}
2046EXPORT_SYMBOL_GPL(regulator_bulk_get);
2047
2048/**
2049 * regulator_bulk_enable - enable multiple regulator consumers
2050 *
2051 * @num_consumers: Number of consumers
2052 * @consumers: Consumer data; clients are stored here.
2053 * @return 0 on success, an errno on failure
2054 *
2055 * This convenience API allows consumers to enable multiple regulator
2056 * clients in a single API call. If any consumers cannot be enabled
2057 * then any others that were enabled will be disabled again prior to
2058 * return.
2059 */
2060int regulator_bulk_enable(int num_consumers,
2061 struct regulator_bulk_data *consumers)
2062{
2063 int i;
2064 int ret;
2065
2066 for (i = 0; i < num_consumers; i++) {
2067 ret = regulator_enable(consumers[i].consumer);
2068 if (ret != 0)
2069 goto err;
2070 }
2071
2072 return 0;
2073
2074err:
Mark Brown5b307622009-10-13 13:06:49 +01002075 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002076 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002077 regulator_disable(consumers[i].consumer);
2078
2079 return ret;
2080}
2081EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2082
2083/**
2084 * regulator_bulk_disable - disable multiple regulator consumers
2085 *
2086 * @num_consumers: Number of consumers
2087 * @consumers: Consumer data; clients are stored here.
2088 * @return 0 on success, an errno on failure
2089 *
2090 * This convenience API allows consumers to disable multiple regulator
2091 * clients in a single API call. If any consumers cannot be enabled
2092 * then any others that were disabled will be disabled again prior to
2093 * return.
2094 */
2095int regulator_bulk_disable(int num_consumers,
2096 struct regulator_bulk_data *consumers)
2097{
2098 int i;
2099 int ret;
2100
2101 for (i = 0; i < num_consumers; i++) {
2102 ret = regulator_disable(consumers[i].consumer);
2103 if (ret != 0)
2104 goto err;
2105 }
2106
2107 return 0;
2108
2109err:
Mark Brown5b307622009-10-13 13:06:49 +01002110 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2111 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002112 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002113 regulator_enable(consumers[i].consumer);
2114
2115 return ret;
2116}
2117EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2118
2119/**
2120 * regulator_bulk_free - free multiple regulator consumers
2121 *
2122 * @num_consumers: Number of consumers
2123 * @consumers: Consumer data; clients are stored here.
2124 *
2125 * This convenience API allows consumers to free multiple regulator
2126 * clients in a single API call.
2127 */
2128void regulator_bulk_free(int num_consumers,
2129 struct regulator_bulk_data *consumers)
2130{
2131 int i;
2132
2133 for (i = 0; i < num_consumers; i++) {
2134 regulator_put(consumers[i].consumer);
2135 consumers[i].consumer = NULL;
2136 }
2137}
2138EXPORT_SYMBOL_GPL(regulator_bulk_free);
2139
2140/**
2141 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002142 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002143 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002144 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145 *
2146 * Called by regulator drivers to notify clients a regulator event has
2147 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002148 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002149 */
2150int regulator_notifier_call_chain(struct regulator_dev *rdev,
2151 unsigned long event, void *data)
2152{
2153 _notifier_call_chain(rdev, event, data);
2154 return NOTIFY_DONE;
2155
2156}
2157EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2158
Mark Brownbe721972009-08-04 20:09:52 +02002159/**
2160 * regulator_mode_to_status - convert a regulator mode into a status
2161 *
2162 * @mode: Mode to convert
2163 *
2164 * Convert a regulator mode into a status.
2165 */
2166int regulator_mode_to_status(unsigned int mode)
2167{
2168 switch (mode) {
2169 case REGULATOR_MODE_FAST:
2170 return REGULATOR_STATUS_FAST;
2171 case REGULATOR_MODE_NORMAL:
2172 return REGULATOR_STATUS_NORMAL;
2173 case REGULATOR_MODE_IDLE:
2174 return REGULATOR_STATUS_IDLE;
2175 case REGULATOR_STATUS_STANDBY:
2176 return REGULATOR_STATUS_STANDBY;
2177 default:
2178 return 0;
2179 }
2180}
2181EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2182
David Brownell7ad68e22008-11-11 17:39:02 -08002183/*
2184 * To avoid cluttering sysfs (and memory) with useless state, only
2185 * create attributes that can be meaningfully displayed.
2186 */
2187static int add_regulator_attributes(struct regulator_dev *rdev)
2188{
2189 struct device *dev = &rdev->dev;
2190 struct regulator_ops *ops = rdev->desc->ops;
2191 int status = 0;
2192
2193 /* some attributes need specific methods to be displayed */
2194 if (ops->get_voltage) {
2195 status = device_create_file(dev, &dev_attr_microvolts);
2196 if (status < 0)
2197 return status;
2198 }
2199 if (ops->get_current_limit) {
2200 status = device_create_file(dev, &dev_attr_microamps);
2201 if (status < 0)
2202 return status;
2203 }
2204 if (ops->get_mode) {
2205 status = device_create_file(dev, &dev_attr_opmode);
2206 if (status < 0)
2207 return status;
2208 }
2209 if (ops->is_enabled) {
2210 status = device_create_file(dev, &dev_attr_state);
2211 if (status < 0)
2212 return status;
2213 }
David Brownell853116a2009-01-14 23:03:17 -08002214 if (ops->get_status) {
2215 status = device_create_file(dev, &dev_attr_status);
2216 if (status < 0)
2217 return status;
2218 }
David Brownell7ad68e22008-11-11 17:39:02 -08002219
2220 /* some attributes are type-specific */
2221 if (rdev->desc->type == REGULATOR_CURRENT) {
2222 status = device_create_file(dev, &dev_attr_requested_microamps);
2223 if (status < 0)
2224 return status;
2225 }
2226
2227 /* all the other attributes exist to support constraints;
2228 * don't show them if there are no constraints, or if the
2229 * relevant supporting methods are missing.
2230 */
2231 if (!rdev->constraints)
2232 return status;
2233
2234 /* constraints need specific supporting methods */
2235 if (ops->set_voltage) {
2236 status = device_create_file(dev, &dev_attr_min_microvolts);
2237 if (status < 0)
2238 return status;
2239 status = device_create_file(dev, &dev_attr_max_microvolts);
2240 if (status < 0)
2241 return status;
2242 }
2243 if (ops->set_current_limit) {
2244 status = device_create_file(dev, &dev_attr_min_microamps);
2245 if (status < 0)
2246 return status;
2247 status = device_create_file(dev, &dev_attr_max_microamps);
2248 if (status < 0)
2249 return status;
2250 }
2251
2252 /* suspend mode constraints need multiple supporting methods */
2253 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2254 return status;
2255
2256 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2257 if (status < 0)
2258 return status;
2259 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2260 if (status < 0)
2261 return status;
2262 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2263 if (status < 0)
2264 return status;
2265
2266 if (ops->set_suspend_voltage) {
2267 status = device_create_file(dev,
2268 &dev_attr_suspend_standby_microvolts);
2269 if (status < 0)
2270 return status;
2271 status = device_create_file(dev,
2272 &dev_attr_suspend_mem_microvolts);
2273 if (status < 0)
2274 return status;
2275 status = device_create_file(dev,
2276 &dev_attr_suspend_disk_microvolts);
2277 if (status < 0)
2278 return status;
2279 }
2280
2281 if (ops->set_suspend_mode) {
2282 status = device_create_file(dev,
2283 &dev_attr_suspend_standby_mode);
2284 if (status < 0)
2285 return status;
2286 status = device_create_file(dev,
2287 &dev_attr_suspend_mem_mode);
2288 if (status < 0)
2289 return status;
2290 status = device_create_file(dev,
2291 &dev_attr_suspend_disk_mode);
2292 if (status < 0)
2293 return status;
2294 }
2295
2296 return status;
2297}
2298
Liam Girdwood414c70c2008-04-30 15:59:04 +01002299/**
2300 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002301 * @regulator_desc: regulator to register
2302 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002303 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002304 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002305 *
2306 * Called by regulator drivers to register a regulator.
2307 * Returns 0 on success.
2308 */
2309struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002310 struct device *dev, struct regulator_init_data *init_data,
2311 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002312{
2313 static atomic_t regulator_no = ATOMIC_INIT(0);
2314 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002315 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002316
2317 if (regulator_desc == NULL)
2318 return ERR_PTR(-EINVAL);
2319
2320 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2321 return ERR_PTR(-EINVAL);
2322
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002323 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2324 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002325 return ERR_PTR(-EINVAL);
2326
Mark Brown46fabe1e2008-09-09 16:21:18 +01002327 if (!init_data)
2328 return ERR_PTR(-EINVAL);
2329
Liam Girdwood414c70c2008-04-30 15:59:04 +01002330 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2331 if (rdev == NULL)
2332 return ERR_PTR(-ENOMEM);
2333
2334 mutex_lock(&regulator_list_mutex);
2335
2336 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002337 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002338 rdev->owner = regulator_desc->owner;
2339 rdev->desc = regulator_desc;
2340 INIT_LIST_HEAD(&rdev->consumer_list);
2341 INIT_LIST_HEAD(&rdev->supply_list);
2342 INIT_LIST_HEAD(&rdev->list);
2343 INIT_LIST_HEAD(&rdev->slist);
2344 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2345
Liam Girdwooda5766f12008-10-10 13:22:20 +01002346 /* preform any regulator specific init */
2347 if (init_data->regulator_init) {
2348 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002349 if (ret < 0)
2350 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002351 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002352
Liam Girdwooda5766f12008-10-10 13:22:20 +01002353 /* register with sysfs */
2354 rdev->dev.class = &regulator_class;
2355 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002356 dev_set_name(&rdev->dev, "regulator.%d",
2357 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002358 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002359 if (ret != 0) {
2360 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002361 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002362 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002363
2364 dev_set_drvdata(&rdev->dev, rdev);
2365
Mike Rapoport74f544c2008-11-25 14:53:53 +02002366 /* set regulator constraints */
2367 ret = set_machine_constraints(rdev, &init_data->constraints);
2368 if (ret < 0)
2369 goto scrub;
2370
David Brownell7ad68e22008-11-11 17:39:02 -08002371 /* add attributes supported by this regulator */
2372 ret = add_regulator_attributes(rdev);
2373 if (ret < 0)
2374 goto scrub;
2375
Liam Girdwooda5766f12008-10-10 13:22:20 +01002376 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002377 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2378 dev_err(dev,
2379 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002380 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002381 goto scrub;
2382 }
2383
2384 if (init_data->supply_regulator) {
2385 struct regulator_dev *r;
2386 int found = 0;
2387
2388 list_for_each_entry(r, &regulator_list, list) {
2389 if (strcmp(rdev_get_name(r),
2390 init_data->supply_regulator) == 0) {
2391 found = 1;
2392 break;
2393 }
2394 }
2395
2396 if (!found) {
2397 dev_err(dev, "Failed to find supply %s\n",
2398 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002399 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002400 goto scrub;
2401 }
2402
2403 ret = set_supply(rdev, r);
2404 if (ret < 0)
2405 goto scrub;
2406 }
2407
Liam Girdwooda5766f12008-10-10 13:22:20 +01002408 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002409 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002410 ret = set_supply(rdev,
2411 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002412 if (ret < 0)
2413 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002414 }
2415
2416 /* add consumers devices */
2417 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2418 ret = set_consumer_device_supply(rdev,
2419 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002420 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002421 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002422 if (ret < 0)
2423 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002424 }
2425
2426 list_add(&rdev->list, &regulator_list);
2427out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002428 mutex_unlock(&regulator_list_mutex);
2429 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002430
Jani Nikulad4033b52010-04-29 10:55:11 +03002431unset_supplies:
2432 unset_regulator_supplies(rdev);
2433
David Brownell4fca9542008-11-11 17:38:53 -08002434scrub:
2435 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002436 /* device core frees rdev */
2437 rdev = ERR_PTR(ret);
2438 goto out;
2439
David Brownell4fca9542008-11-11 17:38:53 -08002440clean:
2441 kfree(rdev);
2442 rdev = ERR_PTR(ret);
2443 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002444}
2445EXPORT_SYMBOL_GPL(regulator_register);
2446
2447/**
2448 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002449 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002450 *
2451 * Called by regulator drivers to unregister a regulator.
2452 */
2453void regulator_unregister(struct regulator_dev *rdev)
2454{
2455 if (rdev == NULL)
2456 return;
2457
2458 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002459 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002460 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002461 list_del(&rdev->list);
2462 if (rdev->supply)
2463 sysfs_remove_link(&rdev->dev.kobj, "supply");
2464 device_unregister(&rdev->dev);
2465 mutex_unlock(&regulator_list_mutex);
2466}
2467EXPORT_SYMBOL_GPL(regulator_unregister);
2468
2469/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002470 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002471 * @state: system suspend state
2472 *
2473 * Configure each regulator with it's suspend operating parameters for state.
2474 * This will usually be called by machine suspend code prior to supending.
2475 */
2476int regulator_suspend_prepare(suspend_state_t state)
2477{
2478 struct regulator_dev *rdev;
2479 int ret = 0;
2480
2481 /* ON is handled by regulator active state */
2482 if (state == PM_SUSPEND_ON)
2483 return -EINVAL;
2484
2485 mutex_lock(&regulator_list_mutex);
2486 list_for_each_entry(rdev, &regulator_list, list) {
2487
2488 mutex_lock(&rdev->mutex);
2489 ret = suspend_prepare(rdev, state);
2490 mutex_unlock(&rdev->mutex);
2491
2492 if (ret < 0) {
2493 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002494 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002495 goto out;
2496 }
2497 }
2498out:
2499 mutex_unlock(&regulator_list_mutex);
2500 return ret;
2501}
2502EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2503
2504/**
Mark Brownca725562009-03-16 19:36:34 +00002505 * regulator_has_full_constraints - the system has fully specified constraints
2506 *
2507 * Calling this function will cause the regulator API to disable all
2508 * regulators which have a zero use count and don't have an always_on
2509 * constraint in a late_initcall.
2510 *
2511 * The intention is that this will become the default behaviour in a
2512 * future kernel release so users are encouraged to use this facility
2513 * now.
2514 */
2515void regulator_has_full_constraints(void)
2516{
2517 has_full_constraints = 1;
2518}
2519EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2520
2521/**
Mark Brown688fe992010-10-05 19:18:32 -07002522 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2523 *
2524 * Calling this function will cause the regulator API to provide a
2525 * dummy regulator to consumers if no physical regulator is found,
2526 * allowing most consumers to proceed as though a regulator were
2527 * configured. This allows systems such as those with software
2528 * controllable regulators for the CPU core only to be brought up more
2529 * readily.
2530 */
2531void regulator_use_dummy_regulator(void)
2532{
2533 board_wants_dummy_regulator = true;
2534}
2535EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2536
2537/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002538 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002539 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002540 *
2541 * Get rdev regulator driver private data. This call can be used in the
2542 * regulator driver context.
2543 */
2544void *rdev_get_drvdata(struct regulator_dev *rdev)
2545{
2546 return rdev->reg_data;
2547}
2548EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2549
2550/**
2551 * regulator_get_drvdata - get regulator driver data
2552 * @regulator: regulator
2553 *
2554 * Get regulator driver private data. This call can be used in the consumer
2555 * driver context when non API regulator specific functions need to be called.
2556 */
2557void *regulator_get_drvdata(struct regulator *regulator)
2558{
2559 return regulator->rdev->reg_data;
2560}
2561EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2562
2563/**
2564 * regulator_set_drvdata - set regulator driver data
2565 * @regulator: regulator
2566 * @data: data
2567 */
2568void regulator_set_drvdata(struct regulator *regulator, void *data)
2569{
2570 regulator->rdev->reg_data = data;
2571}
2572EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2573
2574/**
2575 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002576 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002577 */
2578int rdev_get_id(struct regulator_dev *rdev)
2579{
2580 return rdev->desc->id;
2581}
2582EXPORT_SYMBOL_GPL(rdev_get_id);
2583
Liam Girdwooda5766f12008-10-10 13:22:20 +01002584struct device *rdev_get_dev(struct regulator_dev *rdev)
2585{
2586 return &rdev->dev;
2587}
2588EXPORT_SYMBOL_GPL(rdev_get_dev);
2589
2590void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2591{
2592 return reg_init_data->driver_data;
2593}
2594EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2595
Liam Girdwood414c70c2008-04-30 15:59:04 +01002596static int __init regulator_init(void)
2597{
Mark Brown34abbd62010-02-12 10:18:08 +00002598 int ret;
2599
Liam Girdwood414c70c2008-04-30 15:59:04 +01002600 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002601
2602 ret = class_register(&regulator_class);
2603
2604 regulator_dummy_init();
2605
2606 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002607}
2608
2609/* init early to allow our consumers to complete system booting */
2610core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002611
2612static int __init regulator_init_complete(void)
2613{
2614 struct regulator_dev *rdev;
2615 struct regulator_ops *ops;
2616 struct regulation_constraints *c;
2617 int enabled, ret;
2618 const char *name;
2619
2620 mutex_lock(&regulator_list_mutex);
2621
2622 /* If we have a full configuration then disable any regulators
2623 * which are not in use or always_on. This will become the
2624 * default behaviour in the future.
2625 */
2626 list_for_each_entry(rdev, &regulator_list, list) {
2627 ops = rdev->desc->ops;
2628 c = rdev->constraints;
2629
Mark Brown1083c392009-10-22 16:31:32 +01002630 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002631
Mark Brownf25e0b42009-08-03 18:49:55 +01002632 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002633 continue;
2634
2635 mutex_lock(&rdev->mutex);
2636
2637 if (rdev->use_count)
2638 goto unlock;
2639
2640 /* If we can't read the status assume it's on. */
2641 if (ops->is_enabled)
2642 enabled = ops->is_enabled(rdev);
2643 else
2644 enabled = 1;
2645
2646 if (!enabled)
2647 goto unlock;
2648
2649 if (has_full_constraints) {
2650 /* We log since this may kill the system if it
2651 * goes wrong. */
2652 printk(KERN_INFO "%s: disabling %s\n",
2653 __func__, name);
2654 ret = ops->disable(rdev);
2655 if (ret != 0) {
2656 printk(KERN_ERR
2657 "%s: couldn't disable %s: %d\n",
2658 __func__, name, ret);
2659 }
2660 } else {
2661 /* The intention is that in future we will
2662 * assume that full constraints are provided
2663 * so warn even if we aren't going to do
2664 * anything here.
2665 */
2666 printk(KERN_WARNING
2667 "%s: incomplete constraints, leaving %s on\n",
2668 __func__, name);
2669 }
2670
2671unlock:
2672 mutex_unlock(&rdev->mutex);
2673 }
2674
2675 mutex_unlock(&regulator_list_mutex);
2676
2677 return 0;
2678}
2679late_initcall(regulator_init_complete);