blob: 68549008582cea31a0a705f98d64b36030d77d37 [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>
19#include <linux/err.h>
20#include <linux/mutex.h>
21#include <linux/suspend.h>
22#include <linux/regulator/consumer.h>
23#include <linux/regulator/driver.h>
24#include <linux/regulator/machine.h>
25
26#define REGULATOR_VERSION "0.5"
27
28static DEFINE_MUTEX(regulator_list_mutex);
29static LIST_HEAD(regulator_list);
30static LIST_HEAD(regulator_map_list);
Mark Brownca725562009-03-16 19:36:34 +000031static int has_full_constraints;
Liam Girdwood414c70c2008-04-30 15:59:04 +010032
Mark Brown8dc53902008-12-31 12:52:40 +000033/*
Liam Girdwood414c70c2008-04-30 15:59:04 +010034 * struct regulator_map
35 *
36 * Used to provide symbolic supply names to devices.
37 */
38struct regulator_map {
39 struct list_head list;
Mark Brown40f92442009-06-17 17:56:39 +010040 const char *dev_name; /* The dev_name() for the consumer */
Liam Girdwood414c70c2008-04-30 15:59:04 +010041 const char *supply;
Liam Girdwooda5766f12008-10-10 13:22:20 +010042 struct regulator_dev *regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +010043};
44
Liam Girdwood414c70c2008-04-30 15:59:04 +010045/*
46 * struct regulator
47 *
48 * One for each consumer device.
49 */
50struct regulator {
51 struct device *dev;
52 struct list_head list;
53 int uA_load;
54 int min_uV;
55 int max_uV;
Liam Girdwood414c70c2008-04-30 15:59:04 +010056 char *supply_name;
57 struct device_attribute dev_attr;
58 struct regulator_dev *rdev;
59};
60
61static int _regulator_is_enabled(struct regulator_dev *rdev);
62static int _regulator_disable(struct regulator_dev *rdev);
63static int _regulator_get_voltage(struct regulator_dev *rdev);
64static int _regulator_get_current_limit(struct regulator_dev *rdev);
65static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
66static void _notifier_call_chain(struct regulator_dev *rdev,
67 unsigned long event, void *data);
68
69/* gets the regulator for a given consumer device */
70static struct regulator *get_device_regulator(struct device *dev)
71{
72 struct regulator *regulator = NULL;
73 struct regulator_dev *rdev;
74
75 mutex_lock(&regulator_list_mutex);
76 list_for_each_entry(rdev, &regulator_list, list) {
77 mutex_lock(&rdev->mutex);
78 list_for_each_entry(regulator, &rdev->consumer_list, list) {
79 if (regulator->dev == dev) {
80 mutex_unlock(&rdev->mutex);
81 mutex_unlock(&regulator_list_mutex);
82 return regulator;
83 }
84 }
85 mutex_unlock(&rdev->mutex);
86 }
87 mutex_unlock(&regulator_list_mutex);
88 return NULL;
89}
90
91/* Platform voltage constraint check */
92static int regulator_check_voltage(struct regulator_dev *rdev,
93 int *min_uV, int *max_uV)
94{
95 BUG_ON(*min_uV > *max_uV);
96
97 if (!rdev->constraints) {
98 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
99 rdev->desc->name);
100 return -ENODEV;
101 }
102 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
103 printk(KERN_ERR "%s: operation not allowed for %s\n",
104 __func__, rdev->desc->name);
105 return -EPERM;
106 }
107
108 if (*max_uV > rdev->constraints->max_uV)
109 *max_uV = rdev->constraints->max_uV;
110 if (*min_uV < rdev->constraints->min_uV)
111 *min_uV = rdev->constraints->min_uV;
112
113 if (*min_uV > *max_uV)
114 return -EINVAL;
115
116 return 0;
117}
118
119/* current constraint check */
120static int regulator_check_current_limit(struct regulator_dev *rdev,
121 int *min_uA, int *max_uA)
122{
123 BUG_ON(*min_uA > *max_uA);
124
125 if (!rdev->constraints) {
126 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
127 rdev->desc->name);
128 return -ENODEV;
129 }
130 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
131 printk(KERN_ERR "%s: operation not allowed for %s\n",
132 __func__, rdev->desc->name);
133 return -EPERM;
134 }
135
136 if (*max_uA > rdev->constraints->max_uA)
137 *max_uA = rdev->constraints->max_uA;
138 if (*min_uA < rdev->constraints->min_uA)
139 *min_uA = rdev->constraints->min_uA;
140
141 if (*min_uA > *max_uA)
142 return -EINVAL;
143
144 return 0;
145}
146
147/* operating mode constraint check */
148static int regulator_check_mode(struct regulator_dev *rdev, int mode)
149{
David Brownelle5735202008-11-16 11:46:56 -0800150 switch (mode) {
151 case REGULATOR_MODE_FAST:
152 case REGULATOR_MODE_NORMAL:
153 case REGULATOR_MODE_IDLE:
154 case REGULATOR_MODE_STANDBY:
155 break;
156 default:
157 return -EINVAL;
158 }
159
Liam Girdwood414c70c2008-04-30 15:59:04 +0100160 if (!rdev->constraints) {
161 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
162 rdev->desc->name);
163 return -ENODEV;
164 }
165 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
166 printk(KERN_ERR "%s: operation not allowed for %s\n",
167 __func__, rdev->desc->name);
168 return -EPERM;
169 }
170 if (!(rdev->constraints->valid_modes_mask & mode)) {
171 printk(KERN_ERR "%s: invalid mode %x for %s\n",
172 __func__, mode, rdev->desc->name);
173 return -EINVAL;
174 }
175 return 0;
176}
177
178/* dynamic regulator mode switching constraint check */
179static int regulator_check_drms(struct regulator_dev *rdev)
180{
181 if (!rdev->constraints) {
182 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
183 rdev->desc->name);
184 return -ENODEV;
185 }
186 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
187 printk(KERN_ERR "%s: operation not allowed for %s\n",
188 __func__, rdev->desc->name);
189 return -EPERM;
190 }
191 return 0;
192}
193
194static ssize_t device_requested_uA_show(struct device *dev,
195 struct device_attribute *attr, char *buf)
196{
197 struct regulator *regulator;
198
199 regulator = get_device_regulator(dev);
200 if (regulator == NULL)
201 return 0;
202
203 return sprintf(buf, "%d\n", regulator->uA_load);
204}
205
206static ssize_t regulator_uV_show(struct device *dev,
207 struct device_attribute *attr, char *buf)
208{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100209 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100210 ssize_t ret;
211
212 mutex_lock(&rdev->mutex);
213 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
214 mutex_unlock(&rdev->mutex);
215
216 return ret;
217}
David Brownell7ad68e22008-11-11 17:39:02 -0800218static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100219
220static ssize_t regulator_uA_show(struct device *dev,
221 struct device_attribute *attr, char *buf)
222{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100223 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100224
225 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
226}
David Brownell7ad68e22008-11-11 17:39:02 -0800227static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100228
Mark Brownbc558a62008-10-10 15:33:20 +0100229static ssize_t regulator_name_show(struct device *dev,
230 struct device_attribute *attr, char *buf)
231{
232 struct regulator_dev *rdev = dev_get_drvdata(dev);
233 const char *name;
234
235 if (rdev->constraints->name)
236 name = rdev->constraints->name;
237 else if (rdev->desc->name)
238 name = rdev->desc->name;
239 else
240 name = "";
241
242 return sprintf(buf, "%s\n", name);
243}
244
David Brownell4fca9542008-11-11 17:38:53 -0800245static ssize_t regulator_print_opmode(char *buf, int mode)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100246{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100247 switch (mode) {
248 case REGULATOR_MODE_FAST:
249 return sprintf(buf, "fast\n");
250 case REGULATOR_MODE_NORMAL:
251 return sprintf(buf, "normal\n");
252 case REGULATOR_MODE_IDLE:
253 return sprintf(buf, "idle\n");
254 case REGULATOR_MODE_STANDBY:
255 return sprintf(buf, "standby\n");
256 }
257 return sprintf(buf, "unknown\n");
258}
259
David Brownell4fca9542008-11-11 17:38:53 -0800260static ssize_t regulator_opmode_show(struct device *dev,
261 struct device_attribute *attr, char *buf)
Liam Girdwood414c70c2008-04-30 15:59:04 +0100262{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100263 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100264
David Brownell4fca9542008-11-11 17:38:53 -0800265 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
266}
David Brownell7ad68e22008-11-11 17:39:02 -0800267static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800268
269static ssize_t regulator_print_state(char *buf, int state)
270{
Liam Girdwood414c70c2008-04-30 15:59:04 +0100271 if (state > 0)
272 return sprintf(buf, "enabled\n");
273 else if (state == 0)
274 return sprintf(buf, "disabled\n");
275 else
276 return sprintf(buf, "unknown\n");
277}
278
David Brownell4fca9542008-11-11 17:38:53 -0800279static ssize_t regulator_state_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
281{
282 struct regulator_dev *rdev = dev_get_drvdata(dev);
283
284 return regulator_print_state(buf, _regulator_is_enabled(rdev));
285}
David Brownell7ad68e22008-11-11 17:39:02 -0800286static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800287
David Brownell853116a2009-01-14 23:03:17 -0800288static ssize_t regulator_status_show(struct device *dev,
289 struct device_attribute *attr, char *buf)
290{
291 struct regulator_dev *rdev = dev_get_drvdata(dev);
292 int status;
293 char *label;
294
295 status = rdev->desc->ops->get_status(rdev);
296 if (status < 0)
297 return status;
298
299 switch (status) {
300 case REGULATOR_STATUS_OFF:
301 label = "off";
302 break;
303 case REGULATOR_STATUS_ON:
304 label = "on";
305 break;
306 case REGULATOR_STATUS_ERROR:
307 label = "error";
308 break;
309 case REGULATOR_STATUS_FAST:
310 label = "fast";
311 break;
312 case REGULATOR_STATUS_NORMAL:
313 label = "normal";
314 break;
315 case REGULATOR_STATUS_IDLE:
316 label = "idle";
317 break;
318 case REGULATOR_STATUS_STANDBY:
319 label = "standby";
320 break;
321 default:
322 return -ERANGE;
323 }
324
325 return sprintf(buf, "%s\n", label);
326}
327static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
328
Liam Girdwood414c70c2008-04-30 15:59:04 +0100329static ssize_t regulator_min_uA_show(struct device *dev,
330 struct device_attribute *attr, char *buf)
331{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100332 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100333
334 if (!rdev->constraints)
335 return sprintf(buf, "constraint not defined\n");
336
337 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
338}
David Brownell7ad68e22008-11-11 17:39:02 -0800339static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100340
341static ssize_t regulator_max_uA_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100344 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346 if (!rdev->constraints)
347 return sprintf(buf, "constraint not defined\n");
348
349 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
350}
David Brownell7ad68e22008-11-11 17:39:02 -0800351static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100352
353static ssize_t regulator_min_uV_show(struct device *dev,
354 struct device_attribute *attr, char *buf)
355{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100356 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100357
358 if (!rdev->constraints)
359 return sprintf(buf, "constraint not defined\n");
360
361 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
362}
David Brownell7ad68e22008-11-11 17:39:02 -0800363static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100364
365static ssize_t regulator_max_uV_show(struct device *dev,
366 struct device_attribute *attr, char *buf)
367{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100368 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
370 if (!rdev->constraints)
371 return sprintf(buf, "constraint not defined\n");
372
373 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
374}
David Brownell7ad68e22008-11-11 17:39:02 -0800375static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100376
377static ssize_t regulator_total_uA_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
379{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100380 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381 struct regulator *regulator;
382 int uA = 0;
383
384 mutex_lock(&rdev->mutex);
385 list_for_each_entry(regulator, &rdev->consumer_list, list)
386 uA += regulator->uA_load;
387 mutex_unlock(&rdev->mutex);
388 return sprintf(buf, "%d\n", uA);
389}
David Brownell7ad68e22008-11-11 17:39:02 -0800390static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100391
392static ssize_t regulator_num_users_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100395 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396 return sprintf(buf, "%d\n", rdev->use_count);
397}
398
399static ssize_t regulator_type_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100402 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100403
404 switch (rdev->desc->type) {
405 case REGULATOR_VOLTAGE:
406 return sprintf(buf, "voltage\n");
407 case REGULATOR_CURRENT:
408 return sprintf(buf, "current\n");
409 }
410 return sprintf(buf, "unknown\n");
411}
412
413static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100416 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100417
Liam Girdwood414c70c2008-04-30 15:59:04 +0100418 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
419}
David Brownell7ad68e22008-11-11 17:39:02 -0800420static DEVICE_ATTR(suspend_mem_microvolts, 0444,
421 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100422
423static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
424 struct device_attribute *attr, char *buf)
425{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100426 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100427
Liam Girdwood414c70c2008-04-30 15:59:04 +0100428 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
429}
David Brownell7ad68e22008-11-11 17:39:02 -0800430static DEVICE_ATTR(suspend_disk_microvolts, 0444,
431 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100432
433static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
435{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100436 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
Liam Girdwood414c70c2008-04-30 15:59:04 +0100438 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
439}
David Brownell7ad68e22008-11-11 17:39:02 -0800440static DEVICE_ATTR(suspend_standby_microvolts, 0444,
441 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
444 struct device_attribute *attr, char *buf)
445{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100446 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
David Brownell4fca9542008-11-11 17:38:53 -0800448 return regulator_print_opmode(buf,
449 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100450}
David Brownell7ad68e22008-11-11 17:39:02 -0800451static DEVICE_ATTR(suspend_mem_mode, 0444,
452 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100453
454static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
455 struct device_attribute *attr, char *buf)
456{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100457 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
David Brownell4fca9542008-11-11 17:38:53 -0800459 return regulator_print_opmode(buf,
460 rdev->constraints->state_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100461}
David Brownell7ad68e22008-11-11 17:39:02 -0800462static DEVICE_ATTR(suspend_disk_mode, 0444,
463 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100464
465static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
466 struct device_attribute *attr, char *buf)
467{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100468 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
David Brownell4fca9542008-11-11 17:38:53 -0800470 return regulator_print_opmode(buf,
471 rdev->constraints->state_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100472}
David Brownell7ad68e22008-11-11 17:39:02 -0800473static DEVICE_ATTR(suspend_standby_mode, 0444,
474 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100475
476static ssize_t regulator_suspend_mem_state_show(struct device *dev,
477 struct device_attribute *attr, char *buf)
478{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100479 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
David Brownell4fca9542008-11-11 17:38:53 -0800481 return regulator_print_state(buf,
482 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100483}
David Brownell7ad68e22008-11-11 17:39:02 -0800484static DEVICE_ATTR(suspend_mem_state, 0444,
485 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100486
487static ssize_t regulator_suspend_disk_state_show(struct device *dev,
488 struct device_attribute *attr, char *buf)
489{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100490 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
David Brownell4fca9542008-11-11 17:38:53 -0800492 return regulator_print_state(buf,
493 rdev->constraints->state_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100494}
David Brownell7ad68e22008-11-11 17:39:02 -0800495static DEVICE_ATTR(suspend_disk_state, 0444,
496 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100497
498static ssize_t regulator_suspend_standby_state_show(struct device *dev,
499 struct device_attribute *attr, char *buf)
500{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100501 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
David Brownell4fca9542008-11-11 17:38:53 -0800503 return regulator_print_state(buf,
504 rdev->constraints->state_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100505}
David Brownell7ad68e22008-11-11 17:39:02 -0800506static DEVICE_ATTR(suspend_standby_state, 0444,
507 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100508
David Brownell7ad68e22008-11-11 17:39:02 -0800509
510/*
511 * These are the only attributes are present for all regulators.
512 * Other attributes are a function of regulator functionality.
513 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100514static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100515 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100516 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
517 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100518 __ATTR_NULL,
519};
520
521static void regulator_dev_release(struct device *dev)
522{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100523 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100524 kfree(rdev);
525}
526
527static struct class regulator_class = {
528 .name = "regulator",
529 .dev_release = regulator_dev_release,
530 .dev_attrs = regulator_dev_attrs,
531};
532
533/* Calculate the new optimum regulator operating mode based on the new total
534 * consumer load. All locks held by caller */
535static void drms_uA_update(struct regulator_dev *rdev)
536{
537 struct regulator *sibling;
538 int current_uA = 0, output_uV, input_uV, err;
539 unsigned int mode;
540
541 err = regulator_check_drms(rdev);
542 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300543 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
544 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100545
546 /* get output voltage */
547 output_uV = rdev->desc->ops->get_voltage(rdev);
548 if (output_uV <= 0)
549 return;
550
551 /* get input voltage */
552 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
553 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
554 else
555 input_uV = rdev->constraints->input_uV;
556 if (input_uV <= 0)
557 return;
558
559 /* calc total requested load */
560 list_for_each_entry(sibling, &rdev->consumer_list, list)
561 current_uA += sibling->uA_load;
562
563 /* now get the optimum mode for our new total regulator load */
564 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
565 output_uV, current_uA);
566
567 /* check the new mode is allowed */
568 err = regulator_check_mode(rdev, mode);
569 if (err == 0)
570 rdev->desc->ops->set_mode(rdev, mode);
571}
572
573static int suspend_set_state(struct regulator_dev *rdev,
574 struct regulator_state *rstate)
575{
576 int ret = 0;
577
578 /* enable & disable are mandatory for suspend control */
579 if (!rdev->desc->ops->set_suspend_enable ||
Liam Girdwooda5766f12008-10-10 13:22:20 +0100580 !rdev->desc->ops->set_suspend_disable) {
581 printk(KERN_ERR "%s: no way to set suspend state\n",
582 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100583 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100584 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100585
586 if (rstate->enabled)
587 ret = rdev->desc->ops->set_suspend_enable(rdev);
588 else
589 ret = rdev->desc->ops->set_suspend_disable(rdev);
590 if (ret < 0) {
591 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
592 return ret;
593 }
594
595 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
596 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
597 if (ret < 0) {
598 printk(KERN_ERR "%s: failed to set voltage\n",
599 __func__);
600 return ret;
601 }
602 }
603
604 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
605 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
606 if (ret < 0) {
607 printk(KERN_ERR "%s: failed to set mode\n", __func__);
608 return ret;
609 }
610 }
611 return ret;
612}
613
614/* locks held by caller */
615static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
616{
617 if (!rdev->constraints)
618 return -EINVAL;
619
620 switch (state) {
621 case PM_SUSPEND_STANDBY:
622 return suspend_set_state(rdev,
623 &rdev->constraints->state_standby);
624 case PM_SUSPEND_MEM:
625 return suspend_set_state(rdev,
626 &rdev->constraints->state_mem);
627 case PM_SUSPEND_MAX:
628 return suspend_set_state(rdev,
629 &rdev->constraints->state_disk);
630 default:
631 return -EINVAL;
632 }
633}
634
635static void print_constraints(struct regulator_dev *rdev)
636{
637 struct regulation_constraints *constraints = rdev->constraints;
638 char buf[80];
639 int count;
640
641 if (rdev->desc->type == REGULATOR_VOLTAGE) {
642 if (constraints->min_uV == constraints->max_uV)
643 count = sprintf(buf, "%d mV ",
644 constraints->min_uV / 1000);
645 else
646 count = sprintf(buf, "%d <--> %d mV ",
647 constraints->min_uV / 1000,
648 constraints->max_uV / 1000);
649 } else {
650 if (constraints->min_uA == constraints->max_uA)
651 count = sprintf(buf, "%d mA ",
652 constraints->min_uA / 1000);
653 else
654 count = sprintf(buf, "%d <--> %d mA ",
655 constraints->min_uA / 1000,
656 constraints->max_uA / 1000);
657 }
658 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
659 count += sprintf(buf + count, "fast ");
660 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
661 count += sprintf(buf + count, "normal ");
662 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
663 count += sprintf(buf + count, "idle ");
664 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
665 count += sprintf(buf + count, "standby");
666
667 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
668}
669
Liam Girdwooda5766f12008-10-10 13:22:20 +0100670/**
671 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000672 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000673 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100674 *
675 * Allows platform initialisation code to define and constrain
676 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
677 * Constraints *must* be set by platform code in order for some
678 * regulator operations to proceed i.e. set_voltage, set_current_limit,
679 * set_mode.
680 */
681static int set_machine_constraints(struct regulator_dev *rdev,
682 struct regulation_constraints *constraints)
683{
684 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100685 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100686 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100687
688 if (constraints->name)
689 name = constraints->name;
690 else if (rdev->desc->name)
691 name = rdev->desc->name;
692 else
693 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100694
David Brownell4367cfd2009-02-26 11:48:36 -0800695 /* constrain machine-level voltage specs to fit
696 * the actual range supported by this regulator.
697 */
698 if (ops->list_voltage && rdev->desc->n_voltages) {
699 int count = rdev->desc->n_voltages;
700 int i;
701 int min_uV = INT_MAX;
702 int max_uV = INT_MIN;
703 int cmin = constraints->min_uV;
704 int cmax = constraints->max_uV;
705
Mark Brown3e590912009-04-28 11:09:38 +0100706 /* it's safe to autoconfigure fixed-voltage supplies
707 and the constraints are used by list_voltage. */
David Brownell4367cfd2009-02-26 11:48:36 -0800708 if (count == 1 && !cmin) {
Mark Brown3e590912009-04-28 11:09:38 +0100709 cmin = 1;
David Brownell4367cfd2009-02-26 11:48:36 -0800710 cmax = INT_MAX;
Mark Brown3e590912009-04-28 11:09:38 +0100711 constraints->min_uV = cmin;
712 constraints->max_uV = cmax;
David Brownell4367cfd2009-02-26 11:48:36 -0800713 }
714
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000715 /* voltage constraints are optional */
716 if ((cmin == 0) && (cmax == 0))
717 goto out;
718
David Brownell4367cfd2009-02-26 11:48:36 -0800719 /* else require explicit machine-level constraints */
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000720 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
David Brownell4367cfd2009-02-26 11:48:36 -0800721 pr_err("%s: %s '%s' voltage constraints\n",
722 __func__, "invalid", name);
723 ret = -EINVAL;
724 goto out;
725 }
726
727 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
728 for (i = 0; i < count; i++) {
729 int value;
730
731 value = ops->list_voltage(rdev, i);
732 if (value <= 0)
733 continue;
734
735 /* maybe adjust [min_uV..max_uV] */
736 if (value >= cmin && value < min_uV)
737 min_uV = value;
738 if (value <= cmax && value > max_uV)
739 max_uV = value;
740 }
741
742 /* final: [min_uV..max_uV] valid iff constraints valid */
743 if (max_uV < min_uV) {
744 pr_err("%s: %s '%s' voltage constraints\n",
745 __func__, "unsupportable", name);
746 ret = -EINVAL;
747 goto out;
748 }
749
750 /* use regulator's subset of machine constraints */
751 if (constraints->min_uV < min_uV) {
752 pr_debug("%s: override '%s' %s, %d -> %d\n",
753 __func__, name, "min_uV",
754 constraints->min_uV, min_uV);
755 constraints->min_uV = min_uV;
756 }
757 if (constraints->max_uV > max_uV) {
758 pr_debug("%s: override '%s' %s, %d -> %d\n",
759 __func__, name, "max_uV",
760 constraints->max_uV, max_uV);
761 constraints->max_uV = max_uV;
762 }
763 }
764
Liam Girdwooda5766f12008-10-10 13:22:20 +0100765 rdev->constraints = constraints;
766
767 /* do we need to apply the constraint voltage */
768 if (rdev->constraints->apply_uV &&
769 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100770 ops->set_voltage) {
771 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100772 rdev->constraints->min_uV, rdev->constraints->max_uV);
773 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100774 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
775 __func__,
776 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100777 rdev->constraints = NULL;
778 goto out;
779 }
780 }
781
Liam Girdwooda5766f12008-10-10 13:22:20 +0100782 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100783 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100784 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100785 if (ret < 0) {
786 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
787 __func__, name);
788 rdev->constraints = NULL;
789 goto out;
790 }
791 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100792
Mark Browna3084662009-02-26 19:24:19 +0000793 if (constraints->initial_mode) {
794 if (!ops->set_mode) {
795 printk(KERN_ERR "%s: no set_mode operation for %s\n",
796 __func__, name);
797 ret = -EINVAL;
798 goto out;
799 }
800
801 ret = ops->set_mode(rdev, constraints->initial_mode);
802 if (ret < 0) {
803 printk(KERN_ERR
804 "%s: failed to set initial mode for %s: %d\n",
805 __func__, name, ret);
806 goto out;
807 }
808 }
809
Mark Browncacf90f2009-03-02 16:32:46 +0000810 /* If the constraints say the regulator should be on at this point
811 * and we have control then make sure it is enabled.
812 */
813 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100814 ret = ops->enable(rdev);
815 if (ret < 0) {
816 printk(KERN_ERR "%s: failed to enable %s\n",
817 __func__, name);
818 rdev->constraints = NULL;
819 goto out;
820 }
821 }
822
Liam Girdwooda5766f12008-10-10 13:22:20 +0100823 print_constraints(rdev);
824out:
825 return ret;
826}
827
828/**
829 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000830 * @rdev: regulator name
831 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100832 *
833 * Called by platform initialisation code to set the supply regulator for this
834 * regulator. This ensures that a regulators supply will also be enabled by the
835 * core if it's child is enabled.
836 */
837static int set_supply(struct regulator_dev *rdev,
838 struct regulator_dev *supply_rdev)
839{
840 int err;
841
842 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
843 "supply");
844 if (err) {
845 printk(KERN_ERR
846 "%s: could not add device link %s err %d\n",
847 __func__, supply_rdev->dev.kobj.name, err);
848 goto out;
849 }
850 rdev->supply = supply_rdev;
851 list_add(&rdev->slist, &supply_rdev->supply_list);
852out:
853 return err;
854}
855
856/**
857 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000858 * @rdev: regulator source
859 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100860 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000861 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100862 *
863 * Allows platform initialisation code to map physical regulator
864 * sources to symbolic names for supplies for use by devices. Devices
865 * should use these symbolic names to request regulators, avoiding the
866 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100867 *
868 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100869 */
870static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100871 struct device *consumer_dev, const char *consumer_dev_name,
872 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100873{
874 struct regulator_map *node;
875
Mark Brown40f92442009-06-17 17:56:39 +0100876 if (consumer_dev && consumer_dev_name)
877 return -EINVAL;
878
879 if (!consumer_dev_name && consumer_dev)
880 consumer_dev_name = dev_name(consumer_dev);
881
Liam Girdwooda5766f12008-10-10 13:22:20 +0100882 if (supply == NULL)
883 return -EINVAL;
884
David Brownell6001e132008-12-31 12:54:19 +0000885 list_for_each_entry(node, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100886 if (consumer_dev_name != node->dev_name)
David Brownell6001e132008-12-31 12:54:19 +0000887 continue;
888 if (strcmp(node->supply, supply) != 0)
889 continue;
890
891 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
892 dev_name(&node->regulator->dev),
893 node->regulator->desc->name,
894 supply,
895 dev_name(&rdev->dev), rdev->desc->name);
896 return -EBUSY;
897 }
898
Liam Girdwooda5766f12008-10-10 13:22:20 +0100899 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
900 if (node == NULL)
901 return -ENOMEM;
902
903 node->regulator = rdev;
Mark Brown40f92442009-06-17 17:56:39 +0100904 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100905 node->supply = supply;
906
Mark Brown40f92442009-06-17 17:56:39 +0100907 if (node->dev_name == NULL) {
908 kfree(node);
909 return -ENOMEM;
910 }
911
Liam Girdwooda5766f12008-10-10 13:22:20 +0100912 list_add(&node->list, &regulator_map_list);
913 return 0;
914}
915
916static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100917 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100918{
919 struct regulator_map *node, *n;
920
Mark Brown40f92442009-06-17 17:56:39 +0100921 if (consumer_dev && !consumer_dev_name)
922 consumer_dev_name = dev_name(consumer_dev);
923
Liam Girdwooda5766f12008-10-10 13:22:20 +0100924 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100925 if (rdev != node->regulator)
926 continue;
927
928 if (consumer_dev_name && node->dev_name &&
929 strcmp(consumer_dev_name, node->dev_name))
930 continue;
931
932 list_del(&node->list);
933 kfree(node->dev_name);
934 kfree(node);
935 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100936 }
937}
938
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200939static void unset_regulator_supplies(struct regulator_dev *rdev)
940{
941 struct regulator_map *node, *n;
942
943 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
944 if (rdev == node->regulator) {
945 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100946 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200947 kfree(node);
948 return;
949 }
950 }
951}
952
Liam Girdwood414c70c2008-04-30 15:59:04 +0100953#define REG_STR_SIZE 32
954
955static struct regulator *create_regulator(struct regulator_dev *rdev,
956 struct device *dev,
957 const char *supply_name)
958{
959 struct regulator *regulator;
960 char buf[REG_STR_SIZE];
961 int err, size;
962
963 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
964 if (regulator == NULL)
965 return NULL;
966
967 mutex_lock(&rdev->mutex);
968 regulator->rdev = rdev;
969 list_add(&regulator->list, &rdev->consumer_list);
970
971 if (dev) {
972 /* create a 'requested_microamps_name' sysfs entry */
973 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
974 supply_name);
975 if (size >= REG_STR_SIZE)
976 goto overflow_err;
977
978 regulator->dev = dev;
979 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
980 if (regulator->dev_attr.attr.name == NULL)
981 goto attr_name_err;
982
983 regulator->dev_attr.attr.owner = THIS_MODULE;
984 regulator->dev_attr.attr.mode = 0444;
985 regulator->dev_attr.show = device_requested_uA_show;
986 err = device_create_file(dev, &regulator->dev_attr);
987 if (err < 0) {
988 printk(KERN_WARNING "%s: could not add regulator_dev"
989 " load sysfs\n", __func__);
990 goto attr_name_err;
991 }
992
993 /* also add a link to the device sysfs entry */
994 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
995 dev->kobj.name, supply_name);
996 if (size >= REG_STR_SIZE)
997 goto attr_err;
998
999 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1000 if (regulator->supply_name == NULL)
1001 goto attr_err;
1002
1003 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1004 buf);
1005 if (err) {
1006 printk(KERN_WARNING
1007 "%s: could not add device link %s err %d\n",
1008 __func__, dev->kobj.name, err);
1009 device_remove_file(dev, &regulator->dev_attr);
1010 goto link_name_err;
1011 }
1012 }
1013 mutex_unlock(&rdev->mutex);
1014 return regulator;
1015link_name_err:
1016 kfree(regulator->supply_name);
1017attr_err:
1018 device_remove_file(regulator->dev, &regulator->dev_attr);
1019attr_name_err:
1020 kfree(regulator->dev_attr.attr.name);
1021overflow_err:
1022 list_del(&regulator->list);
1023 kfree(regulator);
1024 mutex_unlock(&rdev->mutex);
1025 return NULL;
1026}
1027
Mark Brown5ffbd132009-07-21 16:00:23 +01001028/* Internal regulator request function */
1029static struct regulator *_regulator_get(struct device *dev, const char *id,
1030 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001031{
1032 struct regulator_dev *rdev;
1033 struct regulator_map *map;
1034 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001035 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001036 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001037
1038 if (id == NULL) {
1039 printk(KERN_ERR "regulator: get() with no identifier\n");
1040 return regulator;
1041 }
1042
Mark Brown40f92442009-06-17 17:56:39 +01001043 if (dev)
1044 devname = dev_name(dev);
1045
Liam Girdwood414c70c2008-04-30 15:59:04 +01001046 mutex_lock(&regulator_list_mutex);
1047
1048 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001049 /* If the mapping has a device set up it must match */
1050 if (map->dev_name &&
1051 (!devname || strcmp(map->dev_name, devname)))
1052 continue;
1053
1054 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001055 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001056 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001057 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001058 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001059 mutex_unlock(&regulator_list_mutex);
1060 return regulator;
1061
1062found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001063 if (rdev->exclusive) {
1064 regulator = ERR_PTR(-EPERM);
1065 goto out;
1066 }
1067
1068 if (exclusive && rdev->open_count) {
1069 regulator = ERR_PTR(-EBUSY);
1070 goto out;
1071 }
1072
Liam Girdwooda5766f12008-10-10 13:22:20 +01001073 if (!try_module_get(rdev->owner))
1074 goto out;
1075
Liam Girdwood414c70c2008-04-30 15:59:04 +01001076 regulator = create_regulator(rdev, dev, id);
1077 if (regulator == NULL) {
1078 regulator = ERR_PTR(-ENOMEM);
1079 module_put(rdev->owner);
1080 }
1081
Mark Brown5ffbd132009-07-21 16:00:23 +01001082 rdev->open_count++;
1083 if (exclusive) {
1084 rdev->exclusive = 1;
1085
1086 ret = _regulator_is_enabled(rdev);
1087 if (ret > 0)
1088 rdev->use_count = 1;
1089 else
1090 rdev->use_count = 0;
1091 }
1092
Liam Girdwooda5766f12008-10-10 13:22:20 +01001093out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001094 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001095
Liam Girdwood414c70c2008-04-30 15:59:04 +01001096 return regulator;
1097}
Mark Brown5ffbd132009-07-21 16:00:23 +01001098
1099/**
1100 * regulator_get - lookup and obtain a reference to a regulator.
1101 * @dev: device for regulator "consumer"
1102 * @id: Supply name or regulator ID.
1103 *
1104 * Returns a struct regulator corresponding to the regulator producer,
1105 * or IS_ERR() condition containing errno.
1106 *
1107 * Use of supply names configured via regulator_set_device_supply() is
1108 * strongly encouraged. It is recommended that the supply name used
1109 * should match the name used for the supply and/or the relevant
1110 * device pins in the datasheet.
1111 */
1112struct regulator *regulator_get(struct device *dev, const char *id)
1113{
1114 return _regulator_get(dev, id, 0);
1115}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001116EXPORT_SYMBOL_GPL(regulator_get);
1117
1118/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001119 * regulator_get_exclusive - obtain exclusive access to a regulator.
1120 * @dev: device for regulator "consumer"
1121 * @id: Supply name or regulator ID.
1122 *
1123 * Returns a struct regulator corresponding to the regulator producer,
1124 * or IS_ERR() condition containing errno. Other consumers will be
1125 * unable to obtain this reference is held and the use count for the
1126 * regulator will be initialised to reflect the current state of the
1127 * regulator.
1128 *
1129 * This is intended for use by consumers which cannot tolerate shared
1130 * use of the regulator such as those which need to force the
1131 * regulator off for correct operation of the hardware they are
1132 * controlling.
1133 *
1134 * Use of supply names configured via regulator_set_device_supply() is
1135 * strongly encouraged. It is recommended that the supply name used
1136 * should match the name used for the supply and/or the relevant
1137 * device pins in the datasheet.
1138 */
1139struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1140{
1141 return _regulator_get(dev, id, 1);
1142}
1143EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1144
1145/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001146 * regulator_put - "free" the regulator source
1147 * @regulator: regulator source
1148 *
1149 * Note: drivers must ensure that all regulator_enable calls made on this
1150 * regulator source are balanced by regulator_disable calls prior to calling
1151 * this function.
1152 */
1153void regulator_put(struct regulator *regulator)
1154{
1155 struct regulator_dev *rdev;
1156
1157 if (regulator == NULL || IS_ERR(regulator))
1158 return;
1159
Liam Girdwood414c70c2008-04-30 15:59:04 +01001160 mutex_lock(&regulator_list_mutex);
1161 rdev = regulator->rdev;
1162
1163 /* remove any sysfs entries */
1164 if (regulator->dev) {
1165 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1166 kfree(regulator->supply_name);
1167 device_remove_file(regulator->dev, &regulator->dev_attr);
1168 kfree(regulator->dev_attr.attr.name);
1169 }
1170 list_del(&regulator->list);
1171 kfree(regulator);
1172
Mark Brown5ffbd132009-07-21 16:00:23 +01001173 rdev->open_count--;
1174 rdev->exclusive = 0;
1175
Liam Girdwood414c70c2008-04-30 15:59:04 +01001176 module_put(rdev->owner);
1177 mutex_unlock(&regulator_list_mutex);
1178}
1179EXPORT_SYMBOL_GPL(regulator_put);
1180
1181/* locks held by regulator_enable() */
1182static int _regulator_enable(struct regulator_dev *rdev)
1183{
1184 int ret = -EINVAL;
1185
1186 if (!rdev->constraints) {
1187 printk(KERN_ERR "%s: %s has no constraints\n",
1188 __func__, rdev->desc->name);
1189 return ret;
1190 }
1191
1192 /* do we need to enable the supply regulator first */
1193 if (rdev->supply) {
1194 ret = _regulator_enable(rdev->supply);
1195 if (ret < 0) {
1196 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1197 __func__, rdev->desc->name, ret);
1198 return ret;
1199 }
1200 }
1201
1202 /* check voltage and requested load before enabling */
1203 if (rdev->desc->ops->enable) {
1204
1205 if (rdev->constraints &&
1206 (rdev->constraints->valid_ops_mask &
1207 REGULATOR_CHANGE_DRMS))
1208 drms_uA_update(rdev);
1209
1210 ret = rdev->desc->ops->enable(rdev);
1211 if (ret < 0) {
1212 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1213 __func__, rdev->desc->name, ret);
1214 return ret;
1215 }
1216 rdev->use_count++;
1217 return ret;
1218 }
1219
1220 return ret;
1221}
1222
1223/**
1224 * regulator_enable - enable regulator output
1225 * @regulator: regulator source
1226 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001227 * Request that the regulator be enabled with the regulator output at
1228 * the predefined voltage or current value. Calls to regulator_enable()
1229 * must be balanced with calls to regulator_disable().
1230 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001231 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001232 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001233 */
1234int regulator_enable(struct regulator *regulator)
1235{
David Brownell412aec62008-11-16 11:44:46 -08001236 struct regulator_dev *rdev = regulator->rdev;
1237 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001238
David Brownell412aec62008-11-16 11:44:46 -08001239 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001240 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001241 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001242 return ret;
1243}
1244EXPORT_SYMBOL_GPL(regulator_enable);
1245
1246/* locks held by regulator_disable() */
1247static int _regulator_disable(struct regulator_dev *rdev)
1248{
1249 int ret = 0;
1250
David Brownellcd94b502009-03-11 16:43:34 -08001251 if (WARN(rdev->use_count <= 0,
1252 "unbalanced disables for %s\n",
1253 rdev->desc->name))
1254 return -EIO;
1255
Liam Girdwood414c70c2008-04-30 15:59:04 +01001256 /* are we the last user and permitted to disable ? */
1257 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1258
1259 /* we are last user */
1260 if (rdev->desc->ops->disable) {
1261 ret = rdev->desc->ops->disable(rdev);
1262 if (ret < 0) {
1263 printk(KERN_ERR "%s: failed to disable %s\n",
1264 __func__, rdev->desc->name);
1265 return ret;
1266 }
1267 }
1268
1269 /* decrease our supplies ref count and disable if required */
1270 if (rdev->supply)
1271 _regulator_disable(rdev->supply);
1272
1273 rdev->use_count = 0;
1274 } else if (rdev->use_count > 1) {
1275
1276 if (rdev->constraints &&
1277 (rdev->constraints->valid_ops_mask &
1278 REGULATOR_CHANGE_DRMS))
1279 drms_uA_update(rdev);
1280
1281 rdev->use_count--;
1282 }
1283 return ret;
1284}
1285
1286/**
1287 * regulator_disable - disable regulator output
1288 * @regulator: regulator source
1289 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001290 * Disable the regulator output voltage or current. Calls to
1291 * regulator_enable() must be balanced with calls to
1292 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001293 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001294 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001295 * devices have it enabled, the regulator device supports disabling and
1296 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001297 */
1298int regulator_disable(struct regulator *regulator)
1299{
David Brownell412aec62008-11-16 11:44:46 -08001300 struct regulator_dev *rdev = regulator->rdev;
1301 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302
David Brownell412aec62008-11-16 11:44:46 -08001303 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001304 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001305 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001306 return ret;
1307}
1308EXPORT_SYMBOL_GPL(regulator_disable);
1309
1310/* locks held by regulator_force_disable() */
1311static int _regulator_force_disable(struct regulator_dev *rdev)
1312{
1313 int ret = 0;
1314
1315 /* force disable */
1316 if (rdev->desc->ops->disable) {
1317 /* ah well, who wants to live forever... */
1318 ret = rdev->desc->ops->disable(rdev);
1319 if (ret < 0) {
1320 printk(KERN_ERR "%s: failed to force disable %s\n",
1321 __func__, rdev->desc->name);
1322 return ret;
1323 }
1324 /* notify other consumers that power has been forced off */
1325 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1326 NULL);
1327 }
1328
1329 /* decrease our supplies ref count and disable if required */
1330 if (rdev->supply)
1331 _regulator_disable(rdev->supply);
1332
1333 rdev->use_count = 0;
1334 return ret;
1335}
1336
1337/**
1338 * regulator_force_disable - force disable regulator output
1339 * @regulator: regulator source
1340 *
1341 * Forcibly disable the regulator output voltage or current.
1342 * NOTE: this *will* disable the regulator output even if other consumer
1343 * devices have it enabled. This should be used for situations when device
1344 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1345 */
1346int regulator_force_disable(struct regulator *regulator)
1347{
1348 int ret;
1349
1350 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001351 regulator->uA_load = 0;
1352 ret = _regulator_force_disable(regulator->rdev);
1353 mutex_unlock(&regulator->rdev->mutex);
1354 return ret;
1355}
1356EXPORT_SYMBOL_GPL(regulator_force_disable);
1357
1358static int _regulator_is_enabled(struct regulator_dev *rdev)
1359{
1360 int ret;
1361
1362 mutex_lock(&rdev->mutex);
1363
1364 /* sanity check */
1365 if (!rdev->desc->ops->is_enabled) {
1366 ret = -EINVAL;
1367 goto out;
1368 }
1369
1370 ret = rdev->desc->ops->is_enabled(rdev);
1371out:
1372 mutex_unlock(&rdev->mutex);
1373 return ret;
1374}
1375
1376/**
1377 * regulator_is_enabled - is the regulator output enabled
1378 * @regulator: regulator source
1379 *
David Brownell412aec62008-11-16 11:44:46 -08001380 * Returns positive if the regulator driver backing the source/client
1381 * has requested that the device be enabled, zero if it hasn't, else a
1382 * negative errno code.
1383 *
1384 * Note that the device backing this regulator handle can have multiple
1385 * users, so it might be enabled even if regulator_enable() was never
1386 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001387 */
1388int regulator_is_enabled(struct regulator *regulator)
1389{
1390 return _regulator_is_enabled(regulator->rdev);
1391}
1392EXPORT_SYMBOL_GPL(regulator_is_enabled);
1393
1394/**
David Brownell4367cfd2009-02-26 11:48:36 -08001395 * regulator_count_voltages - count regulator_list_voltage() selectors
1396 * @regulator: regulator source
1397 *
1398 * Returns number of selectors, or negative errno. Selectors are
1399 * numbered starting at zero, and typically correspond to bitfields
1400 * in hardware registers.
1401 */
1402int regulator_count_voltages(struct regulator *regulator)
1403{
1404 struct regulator_dev *rdev = regulator->rdev;
1405
1406 return rdev->desc->n_voltages ? : -EINVAL;
1407}
1408EXPORT_SYMBOL_GPL(regulator_count_voltages);
1409
1410/**
1411 * regulator_list_voltage - enumerate supported voltages
1412 * @regulator: regulator source
1413 * @selector: identify voltage to list
1414 * Context: can sleep
1415 *
1416 * Returns a voltage that can be passed to @regulator_set_voltage(),
1417 * zero if this selector code can't be used on this sytem, or a
1418 * negative errno.
1419 */
1420int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1421{
1422 struct regulator_dev *rdev = regulator->rdev;
1423 struct regulator_ops *ops = rdev->desc->ops;
1424 int ret;
1425
1426 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1427 return -EINVAL;
1428
1429 mutex_lock(&rdev->mutex);
1430 ret = ops->list_voltage(rdev, selector);
1431 mutex_unlock(&rdev->mutex);
1432
1433 if (ret > 0) {
1434 if (ret < rdev->constraints->min_uV)
1435 ret = 0;
1436 else if (ret > rdev->constraints->max_uV)
1437 ret = 0;
1438 }
1439
1440 return ret;
1441}
1442EXPORT_SYMBOL_GPL(regulator_list_voltage);
1443
1444/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001445 * regulator_set_voltage - set regulator output voltage
1446 * @regulator: regulator source
1447 * @min_uV: Minimum required voltage in uV
1448 * @max_uV: Maximum acceptable voltage in uV
1449 *
1450 * Sets a voltage regulator to the desired output voltage. This can be set
1451 * during any regulator state. IOW, regulator can be disabled or enabled.
1452 *
1453 * If the regulator is enabled then the voltage will change to the new value
1454 * immediately otherwise if the regulator is disabled the regulator will
1455 * output at the new voltage when enabled.
1456 *
1457 * NOTE: If the regulator is shared between several devices then the lowest
1458 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001459 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001460 * calling this function otherwise this call will fail.
1461 */
1462int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1463{
1464 struct regulator_dev *rdev = regulator->rdev;
1465 int ret;
1466
1467 mutex_lock(&rdev->mutex);
1468
1469 /* sanity check */
1470 if (!rdev->desc->ops->set_voltage) {
1471 ret = -EINVAL;
1472 goto out;
1473 }
1474
1475 /* constraints check */
1476 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1477 if (ret < 0)
1478 goto out;
1479 regulator->min_uV = min_uV;
1480 regulator->max_uV = max_uV;
1481 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1482
1483out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001484 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001485 mutex_unlock(&rdev->mutex);
1486 return ret;
1487}
1488EXPORT_SYMBOL_GPL(regulator_set_voltage);
1489
1490static int _regulator_get_voltage(struct regulator_dev *rdev)
1491{
1492 /* sanity check */
1493 if (rdev->desc->ops->get_voltage)
1494 return rdev->desc->ops->get_voltage(rdev);
1495 else
1496 return -EINVAL;
1497}
1498
1499/**
1500 * regulator_get_voltage - get regulator output voltage
1501 * @regulator: regulator source
1502 *
1503 * This returns the current regulator voltage in uV.
1504 *
1505 * NOTE: If the regulator is disabled it will return the voltage value. This
1506 * function should not be used to determine regulator state.
1507 */
1508int regulator_get_voltage(struct regulator *regulator)
1509{
1510 int ret;
1511
1512 mutex_lock(&regulator->rdev->mutex);
1513
1514 ret = _regulator_get_voltage(regulator->rdev);
1515
1516 mutex_unlock(&regulator->rdev->mutex);
1517
1518 return ret;
1519}
1520EXPORT_SYMBOL_GPL(regulator_get_voltage);
1521
1522/**
1523 * regulator_set_current_limit - set regulator output current limit
1524 * @regulator: regulator source
1525 * @min_uA: Minimuum supported current in uA
1526 * @max_uA: Maximum supported current in uA
1527 *
1528 * Sets current sink to the desired output current. This can be set during
1529 * any regulator state. IOW, regulator can be disabled or enabled.
1530 *
1531 * If the regulator is enabled then the current will change to the new value
1532 * immediately otherwise if the regulator is disabled the regulator will
1533 * output at the new current when enabled.
1534 *
1535 * NOTE: Regulator system constraints must be set for this regulator before
1536 * calling this function otherwise this call will fail.
1537 */
1538int regulator_set_current_limit(struct regulator *regulator,
1539 int min_uA, int max_uA)
1540{
1541 struct regulator_dev *rdev = regulator->rdev;
1542 int ret;
1543
1544 mutex_lock(&rdev->mutex);
1545
1546 /* sanity check */
1547 if (!rdev->desc->ops->set_current_limit) {
1548 ret = -EINVAL;
1549 goto out;
1550 }
1551
1552 /* constraints check */
1553 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1554 if (ret < 0)
1555 goto out;
1556
1557 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1558out:
1559 mutex_unlock(&rdev->mutex);
1560 return ret;
1561}
1562EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1563
1564static int _regulator_get_current_limit(struct regulator_dev *rdev)
1565{
1566 int ret;
1567
1568 mutex_lock(&rdev->mutex);
1569
1570 /* sanity check */
1571 if (!rdev->desc->ops->get_current_limit) {
1572 ret = -EINVAL;
1573 goto out;
1574 }
1575
1576 ret = rdev->desc->ops->get_current_limit(rdev);
1577out:
1578 mutex_unlock(&rdev->mutex);
1579 return ret;
1580}
1581
1582/**
1583 * regulator_get_current_limit - get regulator output current
1584 * @regulator: regulator source
1585 *
1586 * This returns the current supplied by the specified current sink in uA.
1587 *
1588 * NOTE: If the regulator is disabled it will return the current value. This
1589 * function should not be used to determine regulator state.
1590 */
1591int regulator_get_current_limit(struct regulator *regulator)
1592{
1593 return _regulator_get_current_limit(regulator->rdev);
1594}
1595EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1596
1597/**
1598 * regulator_set_mode - set regulator operating mode
1599 * @regulator: regulator source
1600 * @mode: operating mode - one of the REGULATOR_MODE constants
1601 *
1602 * Set regulator operating mode to increase regulator efficiency or improve
1603 * regulation performance.
1604 *
1605 * NOTE: Regulator system constraints must be set for this regulator before
1606 * calling this function otherwise this call will fail.
1607 */
1608int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1609{
1610 struct regulator_dev *rdev = regulator->rdev;
1611 int ret;
1612
1613 mutex_lock(&rdev->mutex);
1614
1615 /* sanity check */
1616 if (!rdev->desc->ops->set_mode) {
1617 ret = -EINVAL;
1618 goto out;
1619 }
1620
1621 /* constraints check */
1622 ret = regulator_check_mode(rdev, mode);
1623 if (ret < 0)
1624 goto out;
1625
1626 ret = rdev->desc->ops->set_mode(rdev, mode);
1627out:
1628 mutex_unlock(&rdev->mutex);
1629 return ret;
1630}
1631EXPORT_SYMBOL_GPL(regulator_set_mode);
1632
1633static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1634{
1635 int ret;
1636
1637 mutex_lock(&rdev->mutex);
1638
1639 /* sanity check */
1640 if (!rdev->desc->ops->get_mode) {
1641 ret = -EINVAL;
1642 goto out;
1643 }
1644
1645 ret = rdev->desc->ops->get_mode(rdev);
1646out:
1647 mutex_unlock(&rdev->mutex);
1648 return ret;
1649}
1650
1651/**
1652 * regulator_get_mode - get regulator operating mode
1653 * @regulator: regulator source
1654 *
1655 * Get the current regulator operating mode.
1656 */
1657unsigned int regulator_get_mode(struct regulator *regulator)
1658{
1659 return _regulator_get_mode(regulator->rdev);
1660}
1661EXPORT_SYMBOL_GPL(regulator_get_mode);
1662
1663/**
1664 * regulator_set_optimum_mode - set regulator optimum operating mode
1665 * @regulator: regulator source
1666 * @uA_load: load current
1667 *
1668 * Notifies the regulator core of a new device load. This is then used by
1669 * DRMS (if enabled by constraints) to set the most efficient regulator
1670 * operating mode for the new regulator loading.
1671 *
1672 * Consumer devices notify their supply regulator of the maximum power
1673 * they will require (can be taken from device datasheet in the power
1674 * consumption tables) when they change operational status and hence power
1675 * state. Examples of operational state changes that can affect power
1676 * consumption are :-
1677 *
1678 * o Device is opened / closed.
1679 * o Device I/O is about to begin or has just finished.
1680 * o Device is idling in between work.
1681 *
1682 * This information is also exported via sysfs to userspace.
1683 *
1684 * DRMS will sum the total requested load on the regulator and change
1685 * to the most efficient operating mode if platform constraints allow.
1686 *
1687 * Returns the new regulator mode or error.
1688 */
1689int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1690{
1691 struct regulator_dev *rdev = regulator->rdev;
1692 struct regulator *consumer;
1693 int ret, output_uV, input_uV, total_uA_load = 0;
1694 unsigned int mode;
1695
1696 mutex_lock(&rdev->mutex);
1697
1698 regulator->uA_load = uA_load;
1699 ret = regulator_check_drms(rdev);
1700 if (ret < 0)
1701 goto out;
1702 ret = -EINVAL;
1703
1704 /* sanity check */
1705 if (!rdev->desc->ops->get_optimum_mode)
1706 goto out;
1707
1708 /* get output voltage */
1709 output_uV = rdev->desc->ops->get_voltage(rdev);
1710 if (output_uV <= 0) {
1711 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1712 __func__, rdev->desc->name);
1713 goto out;
1714 }
1715
1716 /* get input voltage */
1717 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1718 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1719 else
1720 input_uV = rdev->constraints->input_uV;
1721 if (input_uV <= 0) {
1722 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1723 __func__, rdev->desc->name);
1724 goto out;
1725 }
1726
1727 /* calc total requested load for this regulator */
1728 list_for_each_entry(consumer, &rdev->consumer_list, list)
1729 total_uA_load += consumer->uA_load;
1730
1731 mode = rdev->desc->ops->get_optimum_mode(rdev,
1732 input_uV, output_uV,
1733 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001734 ret = regulator_check_mode(rdev, mode);
1735 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001736 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1737 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1738 total_uA_load, input_uV, output_uV);
1739 goto out;
1740 }
1741
1742 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001743 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001744 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1745 __func__, mode, rdev->desc->name);
1746 goto out;
1747 }
1748 ret = mode;
1749out:
1750 mutex_unlock(&rdev->mutex);
1751 return ret;
1752}
1753EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1754
1755/**
1756 * regulator_register_notifier - register regulator event notifier
1757 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001758 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001759 *
1760 * Register notifier block to receive regulator events.
1761 */
1762int regulator_register_notifier(struct regulator *regulator,
1763 struct notifier_block *nb)
1764{
1765 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1766 nb);
1767}
1768EXPORT_SYMBOL_GPL(regulator_register_notifier);
1769
1770/**
1771 * regulator_unregister_notifier - unregister regulator event notifier
1772 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001773 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001774 *
1775 * Unregister regulator event notifier block.
1776 */
1777int regulator_unregister_notifier(struct regulator *regulator,
1778 struct notifier_block *nb)
1779{
1780 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1781 nb);
1782}
1783EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1784
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001785/* notify regulator consumers and downstream regulator consumers.
1786 * Note mutex must be held by caller.
1787 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001788static void _notifier_call_chain(struct regulator_dev *rdev,
1789 unsigned long event, void *data)
1790{
1791 struct regulator_dev *_rdev;
1792
1793 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001794 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001795
1796 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001797 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1798 mutex_lock(&_rdev->mutex);
1799 _notifier_call_chain(_rdev, event, data);
1800 mutex_unlock(&_rdev->mutex);
1801 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001802}
1803
1804/**
1805 * regulator_bulk_get - get multiple regulator consumers
1806 *
1807 * @dev: Device to supply
1808 * @num_consumers: Number of consumers to register
1809 * @consumers: Configuration of consumers; clients are stored here.
1810 *
1811 * @return 0 on success, an errno on failure.
1812 *
1813 * This helper function allows drivers to get several regulator
1814 * consumers in one operation. If any of the regulators cannot be
1815 * acquired then any regulators that were allocated will be freed
1816 * before returning to the caller.
1817 */
1818int regulator_bulk_get(struct device *dev, int num_consumers,
1819 struct regulator_bulk_data *consumers)
1820{
1821 int i;
1822 int ret;
1823
1824 for (i = 0; i < num_consumers; i++)
1825 consumers[i].consumer = NULL;
1826
1827 for (i = 0; i < num_consumers; i++) {
1828 consumers[i].consumer = regulator_get(dev,
1829 consumers[i].supply);
1830 if (IS_ERR(consumers[i].consumer)) {
1831 dev_err(dev, "Failed to get supply '%s'\n",
1832 consumers[i].supply);
1833 ret = PTR_ERR(consumers[i].consumer);
1834 consumers[i].consumer = NULL;
1835 goto err;
1836 }
1837 }
1838
1839 return 0;
1840
1841err:
1842 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1843 regulator_put(consumers[i].consumer);
1844
1845 return ret;
1846}
1847EXPORT_SYMBOL_GPL(regulator_bulk_get);
1848
1849/**
1850 * regulator_bulk_enable - enable multiple regulator consumers
1851 *
1852 * @num_consumers: Number of consumers
1853 * @consumers: Consumer data; clients are stored here.
1854 * @return 0 on success, an errno on failure
1855 *
1856 * This convenience API allows consumers to enable multiple regulator
1857 * clients in a single API call. If any consumers cannot be enabled
1858 * then any others that were enabled will be disabled again prior to
1859 * return.
1860 */
1861int regulator_bulk_enable(int num_consumers,
1862 struct regulator_bulk_data *consumers)
1863{
1864 int i;
1865 int ret;
1866
1867 for (i = 0; i < num_consumers; i++) {
1868 ret = regulator_enable(consumers[i].consumer);
1869 if (ret != 0)
1870 goto err;
1871 }
1872
1873 return 0;
1874
1875err:
1876 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1877 for (i = 0; i < num_consumers; i++)
1878 regulator_disable(consumers[i].consumer);
1879
1880 return ret;
1881}
1882EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1883
1884/**
1885 * regulator_bulk_disable - disable multiple regulator consumers
1886 *
1887 * @num_consumers: Number of consumers
1888 * @consumers: Consumer data; clients are stored here.
1889 * @return 0 on success, an errno on failure
1890 *
1891 * This convenience API allows consumers to disable multiple regulator
1892 * clients in a single API call. If any consumers cannot be enabled
1893 * then any others that were disabled will be disabled again prior to
1894 * return.
1895 */
1896int regulator_bulk_disable(int num_consumers,
1897 struct regulator_bulk_data *consumers)
1898{
1899 int i;
1900 int ret;
1901
1902 for (i = 0; i < num_consumers; i++) {
1903 ret = regulator_disable(consumers[i].consumer);
1904 if (ret != 0)
1905 goto err;
1906 }
1907
1908 return 0;
1909
1910err:
1911 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1912 for (i = 0; i < num_consumers; i++)
1913 regulator_enable(consumers[i].consumer);
1914
1915 return ret;
1916}
1917EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1918
1919/**
1920 * regulator_bulk_free - free multiple regulator consumers
1921 *
1922 * @num_consumers: Number of consumers
1923 * @consumers: Consumer data; clients are stored here.
1924 *
1925 * This convenience API allows consumers to free multiple regulator
1926 * clients in a single API call.
1927 */
1928void regulator_bulk_free(int num_consumers,
1929 struct regulator_bulk_data *consumers)
1930{
1931 int i;
1932
1933 for (i = 0; i < num_consumers; i++) {
1934 regulator_put(consumers[i].consumer);
1935 consumers[i].consumer = NULL;
1936 }
1937}
1938EXPORT_SYMBOL_GPL(regulator_bulk_free);
1939
1940/**
1941 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001942 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001943 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001944 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001945 *
1946 * Called by regulator drivers to notify clients a regulator event has
1947 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001948 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001949 */
1950int regulator_notifier_call_chain(struct regulator_dev *rdev,
1951 unsigned long event, void *data)
1952{
1953 _notifier_call_chain(rdev, event, data);
1954 return NOTIFY_DONE;
1955
1956}
1957EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1958
Mark Brownbe721972009-08-04 20:09:52 +02001959/**
1960 * regulator_mode_to_status - convert a regulator mode into a status
1961 *
1962 * @mode: Mode to convert
1963 *
1964 * Convert a regulator mode into a status.
1965 */
1966int regulator_mode_to_status(unsigned int mode)
1967{
1968 switch (mode) {
1969 case REGULATOR_MODE_FAST:
1970 return REGULATOR_STATUS_FAST;
1971 case REGULATOR_MODE_NORMAL:
1972 return REGULATOR_STATUS_NORMAL;
1973 case REGULATOR_MODE_IDLE:
1974 return REGULATOR_STATUS_IDLE;
1975 case REGULATOR_STATUS_STANDBY:
1976 return REGULATOR_STATUS_STANDBY;
1977 default:
1978 return 0;
1979 }
1980}
1981EXPORT_SYMBOL_GPL(regulator_mode_to_status);
1982
David Brownell7ad68e22008-11-11 17:39:02 -08001983/*
1984 * To avoid cluttering sysfs (and memory) with useless state, only
1985 * create attributes that can be meaningfully displayed.
1986 */
1987static int add_regulator_attributes(struct regulator_dev *rdev)
1988{
1989 struct device *dev = &rdev->dev;
1990 struct regulator_ops *ops = rdev->desc->ops;
1991 int status = 0;
1992
1993 /* some attributes need specific methods to be displayed */
1994 if (ops->get_voltage) {
1995 status = device_create_file(dev, &dev_attr_microvolts);
1996 if (status < 0)
1997 return status;
1998 }
1999 if (ops->get_current_limit) {
2000 status = device_create_file(dev, &dev_attr_microamps);
2001 if (status < 0)
2002 return status;
2003 }
2004 if (ops->get_mode) {
2005 status = device_create_file(dev, &dev_attr_opmode);
2006 if (status < 0)
2007 return status;
2008 }
2009 if (ops->is_enabled) {
2010 status = device_create_file(dev, &dev_attr_state);
2011 if (status < 0)
2012 return status;
2013 }
David Brownell853116a2009-01-14 23:03:17 -08002014 if (ops->get_status) {
2015 status = device_create_file(dev, &dev_attr_status);
2016 if (status < 0)
2017 return status;
2018 }
David Brownell7ad68e22008-11-11 17:39:02 -08002019
2020 /* some attributes are type-specific */
2021 if (rdev->desc->type == REGULATOR_CURRENT) {
2022 status = device_create_file(dev, &dev_attr_requested_microamps);
2023 if (status < 0)
2024 return status;
2025 }
2026
2027 /* all the other attributes exist to support constraints;
2028 * don't show them if there are no constraints, or if the
2029 * relevant supporting methods are missing.
2030 */
2031 if (!rdev->constraints)
2032 return status;
2033
2034 /* constraints need specific supporting methods */
2035 if (ops->set_voltage) {
2036 status = device_create_file(dev, &dev_attr_min_microvolts);
2037 if (status < 0)
2038 return status;
2039 status = device_create_file(dev, &dev_attr_max_microvolts);
2040 if (status < 0)
2041 return status;
2042 }
2043 if (ops->set_current_limit) {
2044 status = device_create_file(dev, &dev_attr_min_microamps);
2045 if (status < 0)
2046 return status;
2047 status = device_create_file(dev, &dev_attr_max_microamps);
2048 if (status < 0)
2049 return status;
2050 }
2051
2052 /* suspend mode constraints need multiple supporting methods */
2053 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2054 return status;
2055
2056 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2057 if (status < 0)
2058 return status;
2059 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2060 if (status < 0)
2061 return status;
2062 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2063 if (status < 0)
2064 return status;
2065
2066 if (ops->set_suspend_voltage) {
2067 status = device_create_file(dev,
2068 &dev_attr_suspend_standby_microvolts);
2069 if (status < 0)
2070 return status;
2071 status = device_create_file(dev,
2072 &dev_attr_suspend_mem_microvolts);
2073 if (status < 0)
2074 return status;
2075 status = device_create_file(dev,
2076 &dev_attr_suspend_disk_microvolts);
2077 if (status < 0)
2078 return status;
2079 }
2080
2081 if (ops->set_suspend_mode) {
2082 status = device_create_file(dev,
2083 &dev_attr_suspend_standby_mode);
2084 if (status < 0)
2085 return status;
2086 status = device_create_file(dev,
2087 &dev_attr_suspend_mem_mode);
2088 if (status < 0)
2089 return status;
2090 status = device_create_file(dev,
2091 &dev_attr_suspend_disk_mode);
2092 if (status < 0)
2093 return status;
2094 }
2095
2096 return status;
2097}
2098
Liam Girdwood414c70c2008-04-30 15:59:04 +01002099/**
2100 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002101 * @regulator_desc: regulator to register
2102 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002103 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002104 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002105 *
2106 * Called by regulator drivers to register a regulator.
2107 * Returns 0 on success.
2108 */
2109struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002110 struct device *dev, struct regulator_init_data *init_data,
2111 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002112{
2113 static atomic_t regulator_no = ATOMIC_INIT(0);
2114 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002115 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002116
2117 if (regulator_desc == NULL)
2118 return ERR_PTR(-EINVAL);
2119
2120 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2121 return ERR_PTR(-EINVAL);
2122
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002123 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2124 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002125 return ERR_PTR(-EINVAL);
2126
Mark Brown46fabe1e2008-09-09 16:21:18 +01002127 if (!init_data)
2128 return ERR_PTR(-EINVAL);
2129
Liam Girdwood414c70c2008-04-30 15:59:04 +01002130 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2131 if (rdev == NULL)
2132 return ERR_PTR(-ENOMEM);
2133
2134 mutex_lock(&regulator_list_mutex);
2135
2136 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002137 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002138 rdev->owner = regulator_desc->owner;
2139 rdev->desc = regulator_desc;
2140 INIT_LIST_HEAD(&rdev->consumer_list);
2141 INIT_LIST_HEAD(&rdev->supply_list);
2142 INIT_LIST_HEAD(&rdev->list);
2143 INIT_LIST_HEAD(&rdev->slist);
2144 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2145
Liam Girdwooda5766f12008-10-10 13:22:20 +01002146 /* preform any regulator specific init */
2147 if (init_data->regulator_init) {
2148 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002149 if (ret < 0)
2150 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002151 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002152
Liam Girdwooda5766f12008-10-10 13:22:20 +01002153 /* register with sysfs */
2154 rdev->dev.class = &regulator_class;
2155 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002156 dev_set_name(&rdev->dev, "regulator.%d",
2157 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002158 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002159 if (ret != 0)
2160 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002161
2162 dev_set_drvdata(&rdev->dev, rdev);
2163
Mike Rapoport74f544c2008-11-25 14:53:53 +02002164 /* set regulator constraints */
2165 ret = set_machine_constraints(rdev, &init_data->constraints);
2166 if (ret < 0)
2167 goto scrub;
2168
David Brownell7ad68e22008-11-11 17:39:02 -08002169 /* add attributes supported by this regulator */
2170 ret = add_regulator_attributes(rdev);
2171 if (ret < 0)
2172 goto scrub;
2173
Liam Girdwooda5766f12008-10-10 13:22:20 +01002174 /* set supply regulator if it exists */
2175 if (init_data->supply_regulator_dev) {
2176 ret = set_supply(rdev,
2177 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002178 if (ret < 0)
2179 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002180 }
2181
2182 /* add consumers devices */
2183 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2184 ret = set_consumer_device_supply(rdev,
2185 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002186 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002187 init_data->consumer_supplies[i].supply);
2188 if (ret < 0) {
2189 for (--i; i >= 0; i--)
2190 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002191 init_data->consumer_supplies[i].dev_name,
2192 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002193 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002194 }
2195 }
2196
2197 list_add(&rdev->list, &regulator_list);
2198out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002199 mutex_unlock(&regulator_list_mutex);
2200 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002201
2202scrub:
2203 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002204 /* device core frees rdev */
2205 rdev = ERR_PTR(ret);
2206 goto out;
2207
David Brownell4fca9542008-11-11 17:38:53 -08002208clean:
2209 kfree(rdev);
2210 rdev = ERR_PTR(ret);
2211 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002212}
2213EXPORT_SYMBOL_GPL(regulator_register);
2214
2215/**
2216 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002217 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002218 *
2219 * Called by regulator drivers to unregister a regulator.
2220 */
2221void regulator_unregister(struct regulator_dev *rdev)
2222{
2223 if (rdev == NULL)
2224 return;
2225
2226 mutex_lock(&regulator_list_mutex);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002227 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002228 list_del(&rdev->list);
2229 if (rdev->supply)
2230 sysfs_remove_link(&rdev->dev.kobj, "supply");
2231 device_unregister(&rdev->dev);
2232 mutex_unlock(&regulator_list_mutex);
2233}
2234EXPORT_SYMBOL_GPL(regulator_unregister);
2235
2236/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002237 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002238 * @state: system suspend state
2239 *
2240 * Configure each regulator with it's suspend operating parameters for state.
2241 * This will usually be called by machine suspend code prior to supending.
2242 */
2243int regulator_suspend_prepare(suspend_state_t state)
2244{
2245 struct regulator_dev *rdev;
2246 int ret = 0;
2247
2248 /* ON is handled by regulator active state */
2249 if (state == PM_SUSPEND_ON)
2250 return -EINVAL;
2251
2252 mutex_lock(&regulator_list_mutex);
2253 list_for_each_entry(rdev, &regulator_list, list) {
2254
2255 mutex_lock(&rdev->mutex);
2256 ret = suspend_prepare(rdev, state);
2257 mutex_unlock(&rdev->mutex);
2258
2259 if (ret < 0) {
2260 printk(KERN_ERR "%s: failed to prepare %s\n",
2261 __func__, rdev->desc->name);
2262 goto out;
2263 }
2264 }
2265out:
2266 mutex_unlock(&regulator_list_mutex);
2267 return ret;
2268}
2269EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2270
2271/**
Mark Brownca725562009-03-16 19:36:34 +00002272 * regulator_has_full_constraints - the system has fully specified constraints
2273 *
2274 * Calling this function will cause the regulator API to disable all
2275 * regulators which have a zero use count and don't have an always_on
2276 * constraint in a late_initcall.
2277 *
2278 * The intention is that this will become the default behaviour in a
2279 * future kernel release so users are encouraged to use this facility
2280 * now.
2281 */
2282void regulator_has_full_constraints(void)
2283{
2284 has_full_constraints = 1;
2285}
2286EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2287
2288/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002289 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002290 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002291 *
2292 * Get rdev regulator driver private data. This call can be used in the
2293 * regulator driver context.
2294 */
2295void *rdev_get_drvdata(struct regulator_dev *rdev)
2296{
2297 return rdev->reg_data;
2298}
2299EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2300
2301/**
2302 * regulator_get_drvdata - get regulator driver data
2303 * @regulator: regulator
2304 *
2305 * Get regulator driver private data. This call can be used in the consumer
2306 * driver context when non API regulator specific functions need to be called.
2307 */
2308void *regulator_get_drvdata(struct regulator *regulator)
2309{
2310 return regulator->rdev->reg_data;
2311}
2312EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2313
2314/**
2315 * regulator_set_drvdata - set regulator driver data
2316 * @regulator: regulator
2317 * @data: data
2318 */
2319void regulator_set_drvdata(struct regulator *regulator, void *data)
2320{
2321 regulator->rdev->reg_data = data;
2322}
2323EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2324
2325/**
2326 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002327 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002328 */
2329int rdev_get_id(struct regulator_dev *rdev)
2330{
2331 return rdev->desc->id;
2332}
2333EXPORT_SYMBOL_GPL(rdev_get_id);
2334
Liam Girdwooda5766f12008-10-10 13:22:20 +01002335struct device *rdev_get_dev(struct regulator_dev *rdev)
2336{
2337 return &rdev->dev;
2338}
2339EXPORT_SYMBOL_GPL(rdev_get_dev);
2340
2341void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2342{
2343 return reg_init_data->driver_data;
2344}
2345EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2346
Liam Girdwood414c70c2008-04-30 15:59:04 +01002347static int __init regulator_init(void)
2348{
2349 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2350 return class_register(&regulator_class);
2351}
2352
2353/* init early to allow our consumers to complete system booting */
2354core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002355
2356static int __init regulator_init_complete(void)
2357{
2358 struct regulator_dev *rdev;
2359 struct regulator_ops *ops;
2360 struct regulation_constraints *c;
2361 int enabled, ret;
2362 const char *name;
2363
2364 mutex_lock(&regulator_list_mutex);
2365
2366 /* If we have a full configuration then disable any regulators
2367 * which are not in use or always_on. This will become the
2368 * default behaviour in the future.
2369 */
2370 list_for_each_entry(rdev, &regulator_list, list) {
2371 ops = rdev->desc->ops;
2372 c = rdev->constraints;
2373
2374 if (c->name)
2375 name = c->name;
2376 else if (rdev->desc->name)
2377 name = rdev->desc->name;
2378 else
2379 name = "regulator";
2380
2381 if (!ops->disable || c->always_on)
2382 continue;
2383
2384 mutex_lock(&rdev->mutex);
2385
2386 if (rdev->use_count)
2387 goto unlock;
2388
2389 /* If we can't read the status assume it's on. */
2390 if (ops->is_enabled)
2391 enabled = ops->is_enabled(rdev);
2392 else
2393 enabled = 1;
2394
2395 if (!enabled)
2396 goto unlock;
2397
2398 if (has_full_constraints) {
2399 /* We log since this may kill the system if it
2400 * goes wrong. */
2401 printk(KERN_INFO "%s: disabling %s\n",
2402 __func__, name);
2403 ret = ops->disable(rdev);
2404 if (ret != 0) {
2405 printk(KERN_ERR
2406 "%s: couldn't disable %s: %d\n",
2407 __func__, name, ret);
2408 }
2409 } else {
2410 /* The intention is that in future we will
2411 * assume that full constraints are provided
2412 * so warn even if we aren't going to do
2413 * anything here.
2414 */
2415 printk(KERN_WARNING
2416 "%s: incomplete constraints, leaving %s on\n",
2417 __func__, name);
2418 }
2419
2420unlock:
2421 mutex_unlock(&rdev->mutex);
2422 }
2423
2424 mutex_unlock(&regulator_list_mutex);
2425
2426 return 0;
2427}
2428late_initcall(regulator_init_complete);