blob: a48cf582dcd00bcc8a9bfd92a3dad5aede3fb708 [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
Bengt Jonssonacaf6ff2010-11-10 11:06:22 +01001276 if (rdev->use_count == 0) {
1277 /* do we need to enable the supply regulator first */
1278 if (rdev->supply) {
1279 mutex_lock(&rdev->supply->mutex);
1280 ret = _regulator_enable(rdev->supply);
1281 mutex_unlock(&rdev->supply->mutex);
1282 if (ret < 0) {
1283 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1284 __func__, rdev_get_name(rdev), ret);
1285 return ret;
1286 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001287 }
1288 }
1289
1290 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001291 if (rdev->constraints &&
1292 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1293 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001294
Mark Brown9a2372f2009-08-03 18:49:57 +01001295 if (rdev->use_count == 0) {
1296 /* The regulator may on if it's not switchable or left on */
1297 ret = _regulator_is_enabled(rdev);
1298 if (ret == -EINVAL || ret == 0) {
1299 if (!_regulator_can_change_status(rdev))
1300 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001301
Mark Brown31aae2b2009-12-21 12:21:52 +00001302 if (!rdev->desc->ops->enable)
Mark Brown9a2372f2009-08-03 18:49:57 +01001303 return -EINVAL;
Mark Brown31aae2b2009-12-21 12:21:52 +00001304
1305 /* Query before enabling in case configuration
1306 * dependant. */
1307 ret = _regulator_get_enable_time(rdev);
1308 if (ret >= 0) {
1309 delay = ret;
1310 } else {
1311 printk(KERN_WARNING
1312 "%s: enable_time() failed for %s: %d\n",
1313 __func__, rdev_get_name(rdev),
1314 ret);
1315 delay = 0;
Mark Brown9a2372f2009-08-03 18:49:57 +01001316 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001317
Mark Brown02fa3ec2010-11-10 14:38:30 +00001318 trace_regulator_enable(rdev_get_name(rdev));
1319
Mark Brown31aae2b2009-12-21 12:21:52 +00001320 /* Allow the regulator to ramp; it would be useful
1321 * to extend this for bulk operations so that the
1322 * regulators can ramp together. */
1323 ret = rdev->desc->ops->enable(rdev);
1324 if (ret < 0)
1325 return ret;
1326
Mark Brown02fa3ec2010-11-10 14:38:30 +00001327 trace_regulator_enable_delay(rdev_get_name(rdev));
1328
Axel Line36c1df2010-11-05 21:51:32 +08001329 if (delay >= 1000) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001330 mdelay(delay / 1000);
Axel Line36c1df2010-11-05 21:51:32 +08001331 udelay(delay % 1000);
1332 } else if (delay) {
Mark Brown31aae2b2009-12-21 12:21:52 +00001333 udelay(delay);
Axel Line36c1df2010-11-05 21:51:32 +08001334 }
Mark Brown31aae2b2009-12-21 12:21:52 +00001335
Mark Brown02fa3ec2010-11-10 14:38:30 +00001336 trace_regulator_enable_complete(rdev_get_name(rdev));
1337
Linus Walleija7433cf2009-08-26 12:54:04 +02001338 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001339 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Mark Brown1083c392009-10-22 16:31:32 +01001340 __func__, rdev_get_name(rdev), ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001341 return ret;
1342 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001343 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001344 }
1345
Mark Brown9a2372f2009-08-03 18:49:57 +01001346 rdev->use_count++;
1347
1348 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001349}
1350
1351/**
1352 * regulator_enable - enable regulator output
1353 * @regulator: regulator source
1354 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001355 * Request that the regulator be enabled with the regulator output at
1356 * the predefined voltage or current value. Calls to regulator_enable()
1357 * must be balanced with calls to regulator_disable().
1358 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001359 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001360 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001361 */
1362int regulator_enable(struct regulator *regulator)
1363{
David Brownell412aec62008-11-16 11:44:46 -08001364 struct regulator_dev *rdev = regulator->rdev;
1365 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001366
David Brownell412aec62008-11-16 11:44:46 -08001367 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001368 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001369 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001370 return ret;
1371}
1372EXPORT_SYMBOL_GPL(regulator_enable);
1373
1374/* locks held by regulator_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001375static int _regulator_disable(struct regulator_dev *rdev,
1376 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001377{
1378 int ret = 0;
Mattias Wallinb12a1e22010-11-02 14:55:34 +01001379 *supply_rdev_ptr = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001380
David Brownellcd94b502009-03-11 16:43:34 -08001381 if (WARN(rdev->use_count <= 0,
1382 "unbalanced disables for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001383 rdev_get_name(rdev)))
David Brownellcd94b502009-03-11 16:43:34 -08001384 return -EIO;
1385
Liam Girdwood414c70c2008-04-30 15:59:04 +01001386 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001387 if (rdev->use_count == 1 &&
1388 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001389
1390 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001391 if (_regulator_can_change_status(rdev) &&
1392 rdev->desc->ops->disable) {
Mark Brown02fa3ec2010-11-10 14:38:30 +00001393 trace_regulator_disable(rdev_get_name(rdev));
1394
Liam Girdwood414c70c2008-04-30 15:59:04 +01001395 ret = rdev->desc->ops->disable(rdev);
1396 if (ret < 0) {
1397 printk(KERN_ERR "%s: failed to disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001398 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001399 return ret;
1400 }
Mark Brown84b68262009-12-01 21:12:27 +00001401
Mark Brown02fa3ec2010-11-10 14:38:30 +00001402 trace_regulator_disable_complete(rdev_get_name(rdev));
1403
Mark Brown84b68262009-12-01 21:12:27 +00001404 _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE,
1405 NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406 }
1407
1408 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001409 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001410
1411 rdev->use_count = 0;
1412 } else if (rdev->use_count > 1) {
1413
1414 if (rdev->constraints &&
1415 (rdev->constraints->valid_ops_mask &
1416 REGULATOR_CHANGE_DRMS))
1417 drms_uA_update(rdev);
1418
1419 rdev->use_count--;
1420 }
1421 return ret;
1422}
1423
1424/**
1425 * regulator_disable - disable regulator output
1426 * @regulator: regulator source
1427 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001428 * Disable the regulator output voltage or current. Calls to
1429 * regulator_enable() must be balanced with calls to
1430 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001431 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001432 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001433 * devices have it enabled, the regulator device supports disabling and
1434 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001435 */
1436int regulator_disable(struct regulator *regulator)
1437{
David Brownell412aec62008-11-16 11:44:46 -08001438 struct regulator_dev *rdev = regulator->rdev;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001439 struct regulator_dev *supply_rdev = NULL;
David Brownell412aec62008-11-16 11:44:46 -08001440 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001441
David Brownell412aec62008-11-16 11:44:46 -08001442 mutex_lock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001443 ret = _regulator_disable(rdev, &supply_rdev);
David Brownell412aec62008-11-16 11:44:46 -08001444 mutex_unlock(&rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001445
1446 /* decrease our supplies ref count and disable if required */
1447 while (supply_rdev != NULL) {
1448 rdev = supply_rdev;
1449
1450 mutex_lock(&rdev->mutex);
1451 _regulator_disable(rdev, &supply_rdev);
1452 mutex_unlock(&rdev->mutex);
1453 }
1454
Liam Girdwood414c70c2008-04-30 15:59:04 +01001455 return ret;
1456}
1457EXPORT_SYMBOL_GPL(regulator_disable);
1458
1459/* locks held by regulator_force_disable() */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001460static int _regulator_force_disable(struct regulator_dev *rdev,
1461 struct regulator_dev **supply_rdev_ptr)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001462{
1463 int ret = 0;
1464
1465 /* force disable */
1466 if (rdev->desc->ops->disable) {
1467 /* ah well, who wants to live forever... */
1468 ret = rdev->desc->ops->disable(rdev);
1469 if (ret < 0) {
1470 printk(KERN_ERR "%s: failed to force disable %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001471 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001472 return ret;
1473 }
1474 /* notify other consumers that power has been forced off */
Mark Brown84b68262009-12-01 21:12:27 +00001475 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE |
1476 REGULATOR_EVENT_DISABLE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001477 }
1478
1479 /* decrease our supplies ref count and disable if required */
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001480 *supply_rdev_ptr = rdev->supply;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001481
1482 rdev->use_count = 0;
1483 return ret;
1484}
1485
1486/**
1487 * regulator_force_disable - force disable regulator output
1488 * @regulator: regulator source
1489 *
1490 * Forcibly disable the regulator output voltage or current.
1491 * NOTE: this *will* disable the regulator output even if other consumer
1492 * devices have it enabled. This should be used for situations when device
1493 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1494 */
1495int regulator_force_disable(struct regulator *regulator)
1496{
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001497 struct regulator_dev *supply_rdev = NULL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001498 int ret;
1499
1500 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001501 regulator->uA_load = 0;
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001502 ret = _regulator_force_disable(regulator->rdev, &supply_rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001503 mutex_unlock(&regulator->rdev->mutex);
Jeffrey Carlyle8cbf8112010-10-08 14:49:19 -05001504
1505 if (supply_rdev)
1506 regulator_disable(get_device_regulator(rdev_get_dev(supply_rdev)));
1507
Liam Girdwood414c70c2008-04-30 15:59:04 +01001508 return ret;
1509}
1510EXPORT_SYMBOL_GPL(regulator_force_disable);
1511
1512static int _regulator_is_enabled(struct regulator_dev *rdev)
1513{
Mark Brown9a7f6a42010-02-11 17:22:45 +00001514 /* If we don't know then assume that the regulator is always on */
Mark Brown93325462009-08-03 18:49:56 +01001515 if (!rdev->desc->ops->is_enabled)
Mark Brown9a7f6a42010-02-11 17:22:45 +00001516 return 1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001517
Mark Brown93325462009-08-03 18:49:56 +01001518 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001519}
1520
1521/**
1522 * regulator_is_enabled - is the regulator output enabled
1523 * @regulator: regulator source
1524 *
David Brownell412aec62008-11-16 11:44:46 -08001525 * Returns positive if the regulator driver backing the source/client
1526 * has requested that the device be enabled, zero if it hasn't, else a
1527 * negative errno code.
1528 *
1529 * Note that the device backing this regulator handle can have multiple
1530 * users, so it might be enabled even if regulator_enable() was never
1531 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001532 */
1533int regulator_is_enabled(struct regulator *regulator)
1534{
Mark Brown93325462009-08-03 18:49:56 +01001535 int ret;
1536
1537 mutex_lock(&regulator->rdev->mutex);
1538 ret = _regulator_is_enabled(regulator->rdev);
1539 mutex_unlock(&regulator->rdev->mutex);
1540
1541 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001542}
1543EXPORT_SYMBOL_GPL(regulator_is_enabled);
1544
1545/**
David Brownell4367cfd2009-02-26 11:48:36 -08001546 * regulator_count_voltages - count regulator_list_voltage() selectors
1547 * @regulator: regulator source
1548 *
1549 * Returns number of selectors, or negative errno. Selectors are
1550 * numbered starting at zero, and typically correspond to bitfields
1551 * in hardware registers.
1552 */
1553int regulator_count_voltages(struct regulator *regulator)
1554{
1555 struct regulator_dev *rdev = regulator->rdev;
1556
1557 return rdev->desc->n_voltages ? : -EINVAL;
1558}
1559EXPORT_SYMBOL_GPL(regulator_count_voltages);
1560
1561/**
1562 * regulator_list_voltage - enumerate supported voltages
1563 * @regulator: regulator source
1564 * @selector: identify voltage to list
1565 * Context: can sleep
1566 *
1567 * Returns a voltage that can be passed to @regulator_set_voltage(),
Thomas Weber88393162010-03-16 11:47:56 +01001568 * zero if this selector code can't be used on this system, or a
David Brownell4367cfd2009-02-26 11:48:36 -08001569 * negative errno.
1570 */
1571int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1572{
1573 struct regulator_dev *rdev = regulator->rdev;
1574 struct regulator_ops *ops = rdev->desc->ops;
1575 int ret;
1576
1577 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1578 return -EINVAL;
1579
1580 mutex_lock(&rdev->mutex);
1581 ret = ops->list_voltage(rdev, selector);
1582 mutex_unlock(&rdev->mutex);
1583
1584 if (ret > 0) {
1585 if (ret < rdev->constraints->min_uV)
1586 ret = 0;
1587 else if (ret > rdev->constraints->max_uV)
1588 ret = 0;
1589 }
1590
1591 return ret;
1592}
1593EXPORT_SYMBOL_GPL(regulator_list_voltage);
1594
1595/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001596 * regulator_is_supported_voltage - check if a voltage range can be supported
1597 *
1598 * @regulator: Regulator to check.
1599 * @min_uV: Minimum required voltage in uV.
1600 * @max_uV: Maximum required voltage in uV.
1601 *
1602 * Returns a boolean or a negative error code.
1603 */
1604int regulator_is_supported_voltage(struct regulator *regulator,
1605 int min_uV, int max_uV)
1606{
1607 int i, voltages, ret;
1608
1609 ret = regulator_count_voltages(regulator);
1610 if (ret < 0)
1611 return ret;
1612 voltages = ret;
1613
1614 for (i = 0; i < voltages; i++) {
1615 ret = regulator_list_voltage(regulator, i);
1616
1617 if (ret >= min_uV && ret <= max_uV)
1618 return 1;
1619 }
1620
1621 return 0;
1622}
1623
1624/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001625 * regulator_set_voltage - set regulator output voltage
1626 * @regulator: regulator source
1627 * @min_uV: Minimum required voltage in uV
1628 * @max_uV: Maximum acceptable voltage in uV
1629 *
1630 * Sets a voltage regulator to the desired output voltage. This can be set
1631 * during any regulator state. IOW, regulator can be disabled or enabled.
1632 *
1633 * If the regulator is enabled then the voltage will change to the new value
1634 * immediately otherwise if the regulator is disabled the regulator will
1635 * output at the new voltage when enabled.
1636 *
1637 * NOTE: If the regulator is shared between several devices then the lowest
1638 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001639 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001640 * calling this function otherwise this call will fail.
1641 */
1642int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1643{
1644 struct regulator_dev *rdev = regulator->rdev;
1645 int ret;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001646 unsigned selector;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001647
1648 mutex_lock(&rdev->mutex);
1649
1650 /* sanity check */
1651 if (!rdev->desc->ops->set_voltage) {
1652 ret = -EINVAL;
1653 goto out;
1654 }
1655
1656 /* constraints check */
1657 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1658 if (ret < 0)
1659 goto out;
1660 regulator->min_uV = min_uV;
1661 regulator->max_uV = max_uV;
Mark Brown3a93f2a2010-11-10 14:38:29 +00001662
Mark Brown02fa3ec2010-11-10 14:38:30 +00001663 trace_regulator_set_voltage(rdev_get_name(rdev), min_uV, max_uV);
1664
Mark Brown3a93f2a2010-11-10 14:38:29 +00001665 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV, &selector);
1666
1667 if (rdev->desc->ops->list_voltage)
1668 selector = rdev->desc->ops->list_voltage(rdev, selector);
1669 else
1670 selector = -1;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001671
Mark Brown02fa3ec2010-11-10 14:38:30 +00001672 trace_regulator_set_voltage_complete(rdev_get_name(rdev), selector);
1673
Liam Girdwood414c70c2008-04-30 15:59:04 +01001674out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001675 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001676 mutex_unlock(&rdev->mutex);
1677 return ret;
1678}
1679EXPORT_SYMBOL_GPL(regulator_set_voltage);
1680
1681static int _regulator_get_voltage(struct regulator_dev *rdev)
1682{
1683 /* sanity check */
1684 if (rdev->desc->ops->get_voltage)
1685 return rdev->desc->ops->get_voltage(rdev);
1686 else
1687 return -EINVAL;
1688}
1689
1690/**
1691 * regulator_get_voltage - get regulator output voltage
1692 * @regulator: regulator source
1693 *
1694 * This returns the current regulator voltage in uV.
1695 *
1696 * NOTE: If the regulator is disabled it will return the voltage value. This
1697 * function should not be used to determine regulator state.
1698 */
1699int regulator_get_voltage(struct regulator *regulator)
1700{
1701 int ret;
1702
1703 mutex_lock(&regulator->rdev->mutex);
1704
1705 ret = _regulator_get_voltage(regulator->rdev);
1706
1707 mutex_unlock(&regulator->rdev->mutex);
1708
1709 return ret;
1710}
1711EXPORT_SYMBOL_GPL(regulator_get_voltage);
1712
1713/**
1714 * regulator_set_current_limit - set regulator output current limit
1715 * @regulator: regulator source
1716 * @min_uA: Minimuum supported current in uA
1717 * @max_uA: Maximum supported current in uA
1718 *
1719 * Sets current sink to the desired output current. This can be set during
1720 * any regulator state. IOW, regulator can be disabled or enabled.
1721 *
1722 * If the regulator is enabled then the current will change to the new value
1723 * immediately otherwise if the regulator is disabled the regulator will
1724 * output at the new current when enabled.
1725 *
1726 * NOTE: Regulator system constraints must be set for this regulator before
1727 * calling this function otherwise this call will fail.
1728 */
1729int regulator_set_current_limit(struct regulator *regulator,
1730 int min_uA, int max_uA)
1731{
1732 struct regulator_dev *rdev = regulator->rdev;
1733 int ret;
1734
1735 mutex_lock(&rdev->mutex);
1736
1737 /* sanity check */
1738 if (!rdev->desc->ops->set_current_limit) {
1739 ret = -EINVAL;
1740 goto out;
1741 }
1742
1743 /* constraints check */
1744 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1745 if (ret < 0)
1746 goto out;
1747
1748 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1749out:
1750 mutex_unlock(&rdev->mutex);
1751 return ret;
1752}
1753EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1754
1755static int _regulator_get_current_limit(struct regulator_dev *rdev)
1756{
1757 int ret;
1758
1759 mutex_lock(&rdev->mutex);
1760
1761 /* sanity check */
1762 if (!rdev->desc->ops->get_current_limit) {
1763 ret = -EINVAL;
1764 goto out;
1765 }
1766
1767 ret = rdev->desc->ops->get_current_limit(rdev);
1768out:
1769 mutex_unlock(&rdev->mutex);
1770 return ret;
1771}
1772
1773/**
1774 * regulator_get_current_limit - get regulator output current
1775 * @regulator: regulator source
1776 *
1777 * This returns the current supplied by the specified current sink in uA.
1778 *
1779 * NOTE: If the regulator is disabled it will return the current value. This
1780 * function should not be used to determine regulator state.
1781 */
1782int regulator_get_current_limit(struct regulator *regulator)
1783{
1784 return _regulator_get_current_limit(regulator->rdev);
1785}
1786EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1787
1788/**
1789 * regulator_set_mode - set regulator operating mode
1790 * @regulator: regulator source
1791 * @mode: operating mode - one of the REGULATOR_MODE constants
1792 *
1793 * Set regulator operating mode to increase regulator efficiency or improve
1794 * regulation performance.
1795 *
1796 * NOTE: Regulator system constraints must be set for this regulator before
1797 * calling this function otherwise this call will fail.
1798 */
1799int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1800{
1801 struct regulator_dev *rdev = regulator->rdev;
1802 int ret;
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301803 int regulator_curr_mode;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001804
1805 mutex_lock(&rdev->mutex);
1806
1807 /* sanity check */
1808 if (!rdev->desc->ops->set_mode) {
1809 ret = -EINVAL;
1810 goto out;
1811 }
1812
Sundar R Iyer500b4ac2010-05-17 21:24:48 +05301813 /* return if the same mode is requested */
1814 if (rdev->desc->ops->get_mode) {
1815 regulator_curr_mode = rdev->desc->ops->get_mode(rdev);
1816 if (regulator_curr_mode == mode) {
1817 ret = 0;
1818 goto out;
1819 }
1820 }
1821
Liam Girdwood414c70c2008-04-30 15:59:04 +01001822 /* constraints check */
1823 ret = regulator_check_mode(rdev, mode);
1824 if (ret < 0)
1825 goto out;
1826
1827 ret = rdev->desc->ops->set_mode(rdev, mode);
1828out:
1829 mutex_unlock(&rdev->mutex);
1830 return ret;
1831}
1832EXPORT_SYMBOL_GPL(regulator_set_mode);
1833
1834static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1835{
1836 int ret;
1837
1838 mutex_lock(&rdev->mutex);
1839
1840 /* sanity check */
1841 if (!rdev->desc->ops->get_mode) {
1842 ret = -EINVAL;
1843 goto out;
1844 }
1845
1846 ret = rdev->desc->ops->get_mode(rdev);
1847out:
1848 mutex_unlock(&rdev->mutex);
1849 return ret;
1850}
1851
1852/**
1853 * regulator_get_mode - get regulator operating mode
1854 * @regulator: regulator source
1855 *
1856 * Get the current regulator operating mode.
1857 */
1858unsigned int regulator_get_mode(struct regulator *regulator)
1859{
1860 return _regulator_get_mode(regulator->rdev);
1861}
1862EXPORT_SYMBOL_GPL(regulator_get_mode);
1863
1864/**
1865 * regulator_set_optimum_mode - set regulator optimum operating mode
1866 * @regulator: regulator source
1867 * @uA_load: load current
1868 *
1869 * Notifies the regulator core of a new device load. This is then used by
1870 * DRMS (if enabled by constraints) to set the most efficient regulator
1871 * operating mode for the new regulator loading.
1872 *
1873 * Consumer devices notify their supply regulator of the maximum power
1874 * they will require (can be taken from device datasheet in the power
1875 * consumption tables) when they change operational status and hence power
1876 * state. Examples of operational state changes that can affect power
1877 * consumption are :-
1878 *
1879 * o Device is opened / closed.
1880 * o Device I/O is about to begin or has just finished.
1881 * o Device is idling in between work.
1882 *
1883 * This information is also exported via sysfs to userspace.
1884 *
1885 * DRMS will sum the total requested load on the regulator and change
1886 * to the most efficient operating mode if platform constraints allow.
1887 *
1888 * Returns the new regulator mode or error.
1889 */
1890int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1891{
1892 struct regulator_dev *rdev = regulator->rdev;
1893 struct regulator *consumer;
1894 int ret, output_uV, input_uV, total_uA_load = 0;
1895 unsigned int mode;
1896
1897 mutex_lock(&rdev->mutex);
1898
1899 regulator->uA_load = uA_load;
1900 ret = regulator_check_drms(rdev);
1901 if (ret < 0)
1902 goto out;
1903 ret = -EINVAL;
1904
1905 /* sanity check */
1906 if (!rdev->desc->ops->get_optimum_mode)
1907 goto out;
1908
1909 /* get output voltage */
1910 output_uV = rdev->desc->ops->get_voltage(rdev);
1911 if (output_uV <= 0) {
1912 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001913 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001914 goto out;
1915 }
1916
1917 /* get input voltage */
1918 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1919 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1920 else
1921 input_uV = rdev->constraints->input_uV;
1922 if (input_uV <= 0) {
1923 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001924 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001925 goto out;
1926 }
1927
1928 /* calc total requested load for this regulator */
1929 list_for_each_entry(consumer, &rdev->consumer_list, list)
Stefan Roesefa2984d2009-11-27 15:56:34 +01001930 total_uA_load += consumer->uA_load;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001931
1932 mode = rdev->desc->ops->get_optimum_mode(rdev,
1933 input_uV, output_uV,
1934 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001935 ret = regulator_check_mode(rdev, mode);
1936 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001937 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
Mark Brown1083c392009-10-22 16:31:32 +01001938 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
Liam Girdwood414c70c2008-04-30 15:59:04 +01001939 total_uA_load, input_uV, output_uV);
1940 goto out;
1941 }
1942
1943 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001944 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01001946 __func__, mode, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01001947 goto out;
1948 }
1949 ret = mode;
1950out:
1951 mutex_unlock(&rdev->mutex);
1952 return ret;
1953}
1954EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1955
1956/**
1957 * regulator_register_notifier - register regulator event notifier
1958 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001959 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001960 *
1961 * Register notifier block to receive regulator events.
1962 */
1963int regulator_register_notifier(struct regulator *regulator,
1964 struct notifier_block *nb)
1965{
1966 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1967 nb);
1968}
1969EXPORT_SYMBOL_GPL(regulator_register_notifier);
1970
1971/**
1972 * regulator_unregister_notifier - unregister regulator event notifier
1973 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001974 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001975 *
1976 * Unregister regulator event notifier block.
1977 */
1978int regulator_unregister_notifier(struct regulator *regulator,
1979 struct notifier_block *nb)
1980{
1981 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1982 nb);
1983}
1984EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1985
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001986/* notify regulator consumers and downstream regulator consumers.
1987 * Note mutex must be held by caller.
1988 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001989static void _notifier_call_chain(struct regulator_dev *rdev,
1990 unsigned long event, void *data)
1991{
1992 struct regulator_dev *_rdev;
1993
1994 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001995 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001996
1997 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001998 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
Stefan Roesefa2984d2009-11-27 15:56:34 +01001999 mutex_lock(&_rdev->mutex);
2000 _notifier_call_chain(_rdev, event, data);
2001 mutex_unlock(&_rdev->mutex);
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002002 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002003}
2004
2005/**
2006 * regulator_bulk_get - get multiple regulator consumers
2007 *
2008 * @dev: Device to supply
2009 * @num_consumers: Number of consumers to register
2010 * @consumers: Configuration of consumers; clients are stored here.
2011 *
2012 * @return 0 on success, an errno on failure.
2013 *
2014 * This helper function allows drivers to get several regulator
2015 * consumers in one operation. If any of the regulators cannot be
2016 * acquired then any regulators that were allocated will be freed
2017 * before returning to the caller.
2018 */
2019int regulator_bulk_get(struct device *dev, int num_consumers,
2020 struct regulator_bulk_data *consumers)
2021{
2022 int i;
2023 int ret;
2024
2025 for (i = 0; i < num_consumers; i++)
2026 consumers[i].consumer = NULL;
2027
2028 for (i = 0; i < num_consumers; i++) {
2029 consumers[i].consumer = regulator_get(dev,
2030 consumers[i].supply);
2031 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01002032 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01002033 dev_err(dev, "Failed to get supply '%s': %d\n",
2034 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002035 consumers[i].consumer = NULL;
2036 goto err;
2037 }
2038 }
2039
2040 return 0;
2041
2042err:
2043 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
2044 regulator_put(consumers[i].consumer);
2045
2046 return ret;
2047}
2048EXPORT_SYMBOL_GPL(regulator_bulk_get);
2049
2050/**
2051 * regulator_bulk_enable - enable multiple regulator consumers
2052 *
2053 * @num_consumers: Number of consumers
2054 * @consumers: Consumer data; clients are stored here.
2055 * @return 0 on success, an errno on failure
2056 *
2057 * This convenience API allows consumers to enable multiple regulator
2058 * clients in a single API call. If any consumers cannot be enabled
2059 * then any others that were enabled will be disabled again prior to
2060 * return.
2061 */
2062int regulator_bulk_enable(int num_consumers,
2063 struct regulator_bulk_data *consumers)
2064{
2065 int i;
2066 int ret;
2067
2068 for (i = 0; i < num_consumers; i++) {
2069 ret = regulator_enable(consumers[i].consumer);
2070 if (ret != 0)
2071 goto err;
2072 }
2073
2074 return 0;
2075
2076err:
Mark Brown5b307622009-10-13 13:06:49 +01002077 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002078 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002079 regulator_disable(consumers[i].consumer);
2080
2081 return ret;
2082}
2083EXPORT_SYMBOL_GPL(regulator_bulk_enable);
2084
2085/**
2086 * regulator_bulk_disable - disable multiple regulator consumers
2087 *
2088 * @num_consumers: Number of consumers
2089 * @consumers: Consumer data; clients are stored here.
2090 * @return 0 on success, an errno on failure
2091 *
2092 * This convenience API allows consumers to disable multiple regulator
2093 * clients in a single API call. If any consumers cannot be enabled
2094 * then any others that were disabled will be disabled again prior to
2095 * return.
2096 */
2097int regulator_bulk_disable(int num_consumers,
2098 struct regulator_bulk_data *consumers)
2099{
2100 int i;
2101 int ret;
2102
2103 for (i = 0; i < num_consumers; i++) {
2104 ret = regulator_disable(consumers[i].consumer);
2105 if (ret != 0)
2106 goto err;
2107 }
2108
2109 return 0;
2110
2111err:
Mark Brown5b307622009-10-13 13:06:49 +01002112 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2113 ret);
Lars-Peter Clauseneb143ac2009-12-15 14:30:01 +01002114 for (--i; i >= 0; --i)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002115 regulator_enable(consumers[i].consumer);
2116
2117 return ret;
2118}
2119EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2120
2121/**
2122 * regulator_bulk_free - free multiple regulator consumers
2123 *
2124 * @num_consumers: Number of consumers
2125 * @consumers: Consumer data; clients are stored here.
2126 *
2127 * This convenience API allows consumers to free multiple regulator
2128 * clients in a single API call.
2129 */
2130void regulator_bulk_free(int num_consumers,
2131 struct regulator_bulk_data *consumers)
2132{
2133 int i;
2134
2135 for (i = 0; i < num_consumers; i++) {
2136 regulator_put(consumers[i].consumer);
2137 consumers[i].consumer = NULL;
2138 }
2139}
2140EXPORT_SYMBOL_GPL(regulator_bulk_free);
2141
2142/**
2143 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002144 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002145 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002146 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002147 *
2148 * Called by regulator drivers to notify clients a regulator event has
2149 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002150 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002151 */
2152int regulator_notifier_call_chain(struct regulator_dev *rdev,
2153 unsigned long event, void *data)
2154{
2155 _notifier_call_chain(rdev, event, data);
2156 return NOTIFY_DONE;
2157
2158}
2159EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2160
Mark Brownbe721972009-08-04 20:09:52 +02002161/**
2162 * regulator_mode_to_status - convert a regulator mode into a status
2163 *
2164 * @mode: Mode to convert
2165 *
2166 * Convert a regulator mode into a status.
2167 */
2168int regulator_mode_to_status(unsigned int mode)
2169{
2170 switch (mode) {
2171 case REGULATOR_MODE_FAST:
2172 return REGULATOR_STATUS_FAST;
2173 case REGULATOR_MODE_NORMAL:
2174 return REGULATOR_STATUS_NORMAL;
2175 case REGULATOR_MODE_IDLE:
2176 return REGULATOR_STATUS_IDLE;
2177 case REGULATOR_STATUS_STANDBY:
2178 return REGULATOR_STATUS_STANDBY;
2179 default:
2180 return 0;
2181 }
2182}
2183EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2184
David Brownell7ad68e22008-11-11 17:39:02 -08002185/*
2186 * To avoid cluttering sysfs (and memory) with useless state, only
2187 * create attributes that can be meaningfully displayed.
2188 */
2189static int add_regulator_attributes(struct regulator_dev *rdev)
2190{
2191 struct device *dev = &rdev->dev;
2192 struct regulator_ops *ops = rdev->desc->ops;
2193 int status = 0;
2194
2195 /* some attributes need specific methods to be displayed */
2196 if (ops->get_voltage) {
2197 status = device_create_file(dev, &dev_attr_microvolts);
2198 if (status < 0)
2199 return status;
2200 }
2201 if (ops->get_current_limit) {
2202 status = device_create_file(dev, &dev_attr_microamps);
2203 if (status < 0)
2204 return status;
2205 }
2206 if (ops->get_mode) {
2207 status = device_create_file(dev, &dev_attr_opmode);
2208 if (status < 0)
2209 return status;
2210 }
2211 if (ops->is_enabled) {
2212 status = device_create_file(dev, &dev_attr_state);
2213 if (status < 0)
2214 return status;
2215 }
David Brownell853116a2009-01-14 23:03:17 -08002216 if (ops->get_status) {
2217 status = device_create_file(dev, &dev_attr_status);
2218 if (status < 0)
2219 return status;
2220 }
David Brownell7ad68e22008-11-11 17:39:02 -08002221
2222 /* some attributes are type-specific */
2223 if (rdev->desc->type == REGULATOR_CURRENT) {
2224 status = device_create_file(dev, &dev_attr_requested_microamps);
2225 if (status < 0)
2226 return status;
2227 }
2228
2229 /* all the other attributes exist to support constraints;
2230 * don't show them if there are no constraints, or if the
2231 * relevant supporting methods are missing.
2232 */
2233 if (!rdev->constraints)
2234 return status;
2235
2236 /* constraints need specific supporting methods */
2237 if (ops->set_voltage) {
2238 status = device_create_file(dev, &dev_attr_min_microvolts);
2239 if (status < 0)
2240 return status;
2241 status = device_create_file(dev, &dev_attr_max_microvolts);
2242 if (status < 0)
2243 return status;
2244 }
2245 if (ops->set_current_limit) {
2246 status = device_create_file(dev, &dev_attr_min_microamps);
2247 if (status < 0)
2248 return status;
2249 status = device_create_file(dev, &dev_attr_max_microamps);
2250 if (status < 0)
2251 return status;
2252 }
2253
2254 /* suspend mode constraints need multiple supporting methods */
2255 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2256 return status;
2257
2258 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2259 if (status < 0)
2260 return status;
2261 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2262 if (status < 0)
2263 return status;
2264 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2265 if (status < 0)
2266 return status;
2267
2268 if (ops->set_suspend_voltage) {
2269 status = device_create_file(dev,
2270 &dev_attr_suspend_standby_microvolts);
2271 if (status < 0)
2272 return status;
2273 status = device_create_file(dev,
2274 &dev_attr_suspend_mem_microvolts);
2275 if (status < 0)
2276 return status;
2277 status = device_create_file(dev,
2278 &dev_attr_suspend_disk_microvolts);
2279 if (status < 0)
2280 return status;
2281 }
2282
2283 if (ops->set_suspend_mode) {
2284 status = device_create_file(dev,
2285 &dev_attr_suspend_standby_mode);
2286 if (status < 0)
2287 return status;
2288 status = device_create_file(dev,
2289 &dev_attr_suspend_mem_mode);
2290 if (status < 0)
2291 return status;
2292 status = device_create_file(dev,
2293 &dev_attr_suspend_disk_mode);
2294 if (status < 0)
2295 return status;
2296 }
2297
2298 return status;
2299}
2300
Liam Girdwood414c70c2008-04-30 15:59:04 +01002301/**
2302 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002303 * @regulator_desc: regulator to register
2304 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002305 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002306 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002307 *
2308 * Called by regulator drivers to register a regulator.
2309 * Returns 0 on success.
2310 */
2311struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002312 struct device *dev, struct regulator_init_data *init_data,
2313 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002314{
2315 static atomic_t regulator_no = ATOMIC_INIT(0);
2316 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002317 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002318
2319 if (regulator_desc == NULL)
2320 return ERR_PTR(-EINVAL);
2321
2322 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2323 return ERR_PTR(-EINVAL);
2324
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002325 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2326 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002327 return ERR_PTR(-EINVAL);
2328
Mark Brown46fabe1e2008-09-09 16:21:18 +01002329 if (!init_data)
2330 return ERR_PTR(-EINVAL);
2331
Liam Girdwood414c70c2008-04-30 15:59:04 +01002332 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2333 if (rdev == NULL)
2334 return ERR_PTR(-ENOMEM);
2335
2336 mutex_lock(&regulator_list_mutex);
2337
2338 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002339 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002340 rdev->owner = regulator_desc->owner;
2341 rdev->desc = regulator_desc;
2342 INIT_LIST_HEAD(&rdev->consumer_list);
2343 INIT_LIST_HEAD(&rdev->supply_list);
2344 INIT_LIST_HEAD(&rdev->list);
2345 INIT_LIST_HEAD(&rdev->slist);
2346 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2347
Liam Girdwooda5766f12008-10-10 13:22:20 +01002348 /* preform any regulator specific init */
2349 if (init_data->regulator_init) {
2350 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002351 if (ret < 0)
2352 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002353 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002354
Liam Girdwooda5766f12008-10-10 13:22:20 +01002355 /* register with sysfs */
2356 rdev->dev.class = &regulator_class;
2357 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002358 dev_set_name(&rdev->dev, "regulator.%d",
2359 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002360 ret = device_register(&rdev->dev);
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002361 if (ret != 0) {
2362 put_device(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002363 goto clean;
Vasiliy Kulikovad7725c2010-09-19 16:55:01 +04002364 }
Liam Girdwooda5766f12008-10-10 13:22:20 +01002365
2366 dev_set_drvdata(&rdev->dev, rdev);
2367
Mike Rapoport74f544c2008-11-25 14:53:53 +02002368 /* set regulator constraints */
2369 ret = set_machine_constraints(rdev, &init_data->constraints);
2370 if (ret < 0)
2371 goto scrub;
2372
David Brownell7ad68e22008-11-11 17:39:02 -08002373 /* add attributes supported by this regulator */
2374 ret = add_regulator_attributes(rdev);
2375 if (ret < 0)
2376 goto scrub;
2377
Liam Girdwooda5766f12008-10-10 13:22:20 +01002378 /* set supply regulator if it exists */
Mark Brown0178f3e2010-04-26 15:18:14 +01002379 if (init_data->supply_regulator && init_data->supply_regulator_dev) {
2380 dev_err(dev,
2381 "Supply regulator specified by both name and dev\n");
Axel Lin7727da22010-11-05 15:27:17 +08002382 ret = -EINVAL;
Mark Brown0178f3e2010-04-26 15:18:14 +01002383 goto scrub;
2384 }
2385
2386 if (init_data->supply_regulator) {
2387 struct regulator_dev *r;
2388 int found = 0;
2389
2390 list_for_each_entry(r, &regulator_list, list) {
2391 if (strcmp(rdev_get_name(r),
2392 init_data->supply_regulator) == 0) {
2393 found = 1;
2394 break;
2395 }
2396 }
2397
2398 if (!found) {
2399 dev_err(dev, "Failed to find supply %s\n",
2400 init_data->supply_regulator);
Axel Lin7727da22010-11-05 15:27:17 +08002401 ret = -ENODEV;
Mark Brown0178f3e2010-04-26 15:18:14 +01002402 goto scrub;
2403 }
2404
2405 ret = set_supply(rdev, r);
2406 if (ret < 0)
2407 goto scrub;
2408 }
2409
Liam Girdwooda5766f12008-10-10 13:22:20 +01002410 if (init_data->supply_regulator_dev) {
Mark Brown0178f3e2010-04-26 15:18:14 +01002411 dev_warn(dev, "Uses supply_regulator_dev instead of regulator_supply\n");
Liam Girdwooda5766f12008-10-10 13:22:20 +01002412 ret = set_supply(rdev,
2413 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002414 if (ret < 0)
2415 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002416 }
2417
2418 /* add consumers devices */
2419 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2420 ret = set_consumer_device_supply(rdev,
2421 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002422 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002423 init_data->consumer_supplies[i].supply);
Jani Nikulad4033b52010-04-29 10:55:11 +03002424 if (ret < 0)
2425 goto unset_supplies;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002426 }
2427
2428 list_add(&rdev->list, &regulator_list);
2429out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002430 mutex_unlock(&regulator_list_mutex);
2431 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002432
Jani Nikulad4033b52010-04-29 10:55:11 +03002433unset_supplies:
2434 unset_regulator_supplies(rdev);
2435
David Brownell4fca9542008-11-11 17:38:53 -08002436scrub:
2437 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002438 /* device core frees rdev */
2439 rdev = ERR_PTR(ret);
2440 goto out;
2441
David Brownell4fca9542008-11-11 17:38:53 -08002442clean:
2443 kfree(rdev);
2444 rdev = ERR_PTR(ret);
2445 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002446}
2447EXPORT_SYMBOL_GPL(regulator_register);
2448
2449/**
2450 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002451 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002452 *
2453 * Called by regulator drivers to unregister a regulator.
2454 */
2455void regulator_unregister(struct regulator_dev *rdev)
2456{
2457 if (rdev == NULL)
2458 return;
2459
2460 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002461 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002462 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002463 list_del(&rdev->list);
2464 if (rdev->supply)
2465 sysfs_remove_link(&rdev->dev.kobj, "supply");
2466 device_unregister(&rdev->dev);
2467 mutex_unlock(&regulator_list_mutex);
2468}
2469EXPORT_SYMBOL_GPL(regulator_unregister);
2470
2471/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002472 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002473 * @state: system suspend state
2474 *
2475 * Configure each regulator with it's suspend operating parameters for state.
2476 * This will usually be called by machine suspend code prior to supending.
2477 */
2478int regulator_suspend_prepare(suspend_state_t state)
2479{
2480 struct regulator_dev *rdev;
2481 int ret = 0;
2482
2483 /* ON is handled by regulator active state */
2484 if (state == PM_SUSPEND_ON)
2485 return -EINVAL;
2486
2487 mutex_lock(&regulator_list_mutex);
2488 list_for_each_entry(rdev, &regulator_list, list) {
2489
2490 mutex_lock(&rdev->mutex);
2491 ret = suspend_prepare(rdev, state);
2492 mutex_unlock(&rdev->mutex);
2493
2494 if (ret < 0) {
2495 printk(KERN_ERR "%s: failed to prepare %s\n",
Mark Brown1083c392009-10-22 16:31:32 +01002496 __func__, rdev_get_name(rdev));
Liam Girdwood414c70c2008-04-30 15:59:04 +01002497 goto out;
2498 }
2499 }
2500out:
2501 mutex_unlock(&regulator_list_mutex);
2502 return ret;
2503}
2504EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2505
2506/**
Mark Brownca725562009-03-16 19:36:34 +00002507 * regulator_has_full_constraints - the system has fully specified constraints
2508 *
2509 * Calling this function will cause the regulator API to disable all
2510 * regulators which have a zero use count and don't have an always_on
2511 * constraint in a late_initcall.
2512 *
2513 * The intention is that this will become the default behaviour in a
2514 * future kernel release so users are encouraged to use this facility
2515 * now.
2516 */
2517void regulator_has_full_constraints(void)
2518{
2519 has_full_constraints = 1;
2520}
2521EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2522
2523/**
Mark Brown688fe992010-10-05 19:18:32 -07002524 * regulator_use_dummy_regulator - Provide a dummy regulator when none is found
2525 *
2526 * Calling this function will cause the regulator API to provide a
2527 * dummy regulator to consumers if no physical regulator is found,
2528 * allowing most consumers to proceed as though a regulator were
2529 * configured. This allows systems such as those with software
2530 * controllable regulators for the CPU core only to be brought up more
2531 * readily.
2532 */
2533void regulator_use_dummy_regulator(void)
2534{
2535 board_wants_dummy_regulator = true;
2536}
2537EXPORT_SYMBOL_GPL(regulator_use_dummy_regulator);
2538
2539/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002540 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002541 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002542 *
2543 * Get rdev regulator driver private data. This call can be used in the
2544 * regulator driver context.
2545 */
2546void *rdev_get_drvdata(struct regulator_dev *rdev)
2547{
2548 return rdev->reg_data;
2549}
2550EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2551
2552/**
2553 * regulator_get_drvdata - get regulator driver data
2554 * @regulator: regulator
2555 *
2556 * Get regulator driver private data. This call can be used in the consumer
2557 * driver context when non API regulator specific functions need to be called.
2558 */
2559void *regulator_get_drvdata(struct regulator *regulator)
2560{
2561 return regulator->rdev->reg_data;
2562}
2563EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2564
2565/**
2566 * regulator_set_drvdata - set regulator driver data
2567 * @regulator: regulator
2568 * @data: data
2569 */
2570void regulator_set_drvdata(struct regulator *regulator, void *data)
2571{
2572 regulator->rdev->reg_data = data;
2573}
2574EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2575
2576/**
2577 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002578 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002579 */
2580int rdev_get_id(struct regulator_dev *rdev)
2581{
2582 return rdev->desc->id;
2583}
2584EXPORT_SYMBOL_GPL(rdev_get_id);
2585
Liam Girdwooda5766f12008-10-10 13:22:20 +01002586struct device *rdev_get_dev(struct regulator_dev *rdev)
2587{
2588 return &rdev->dev;
2589}
2590EXPORT_SYMBOL_GPL(rdev_get_dev);
2591
2592void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2593{
2594 return reg_init_data->driver_data;
2595}
2596EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2597
Liam Girdwood414c70c2008-04-30 15:59:04 +01002598static int __init regulator_init(void)
2599{
Mark Brown34abbd62010-02-12 10:18:08 +00002600 int ret;
2601
Liam Girdwood414c70c2008-04-30 15:59:04 +01002602 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
Mark Brown34abbd62010-02-12 10:18:08 +00002603
2604 ret = class_register(&regulator_class);
2605
2606 regulator_dummy_init();
2607
2608 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002609}
2610
2611/* init early to allow our consumers to complete system booting */
2612core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002613
2614static int __init regulator_init_complete(void)
2615{
2616 struct regulator_dev *rdev;
2617 struct regulator_ops *ops;
2618 struct regulation_constraints *c;
2619 int enabled, ret;
2620 const char *name;
2621
2622 mutex_lock(&regulator_list_mutex);
2623
2624 /* If we have a full configuration then disable any regulators
2625 * which are not in use or always_on. This will become the
2626 * default behaviour in the future.
2627 */
2628 list_for_each_entry(rdev, &regulator_list, list) {
2629 ops = rdev->desc->ops;
2630 c = rdev->constraints;
2631
Mark Brown1083c392009-10-22 16:31:32 +01002632 name = rdev_get_name(rdev);
Mark Brownca725562009-03-16 19:36:34 +00002633
Mark Brownf25e0b42009-08-03 18:49:55 +01002634 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002635 continue;
2636
2637 mutex_lock(&rdev->mutex);
2638
2639 if (rdev->use_count)
2640 goto unlock;
2641
2642 /* If we can't read the status assume it's on. */
2643 if (ops->is_enabled)
2644 enabled = ops->is_enabled(rdev);
2645 else
2646 enabled = 1;
2647
2648 if (!enabled)
2649 goto unlock;
2650
2651 if (has_full_constraints) {
2652 /* We log since this may kill the system if it
2653 * goes wrong. */
2654 printk(KERN_INFO "%s: disabling %s\n",
2655 __func__, name);
2656 ret = ops->disable(rdev);
2657 if (ret != 0) {
2658 printk(KERN_ERR
2659 "%s: couldn't disable %s: %d\n",
2660 __func__, name, ret);
2661 }
2662 } else {
2663 /* The intention is that in future we will
2664 * assume that full constraints are provided
2665 * so warn even if we aren't going to do
2666 * anything here.
2667 */
2668 printk(KERN_WARNING
2669 "%s: incomplete constraints, leaving %s on\n",
2670 __func__, name);
2671 }
2672
2673unlock:
2674 mutex_unlock(&rdev->mutex);
2675 }
2676
2677 mutex_unlock(&regulator_list_mutex);
2678
2679 return 0;
2680}
2681late_initcall(regulator_init_complete);