blob: c1a49917af24cc1546edbbfb14834af250fb7f1d [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
Mark Brownb39480a2009-08-03 18:49:54 +0100235 if (rdev->constraints && rdev->constraints->name)
Mark Brownbc558a62008-10-10 15:33:20 +0100236 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);
Mark Brown93325462009-08-03 18:49:56 +0100283 ssize_t ret;
David Brownell4fca9542008-11-11 17:38:53 -0800284
Mark Brown93325462009-08-03 18:49:56 +0100285 mutex_lock(&rdev->mutex);
286 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
287 mutex_unlock(&rdev->mutex);
288
289 return ret;
David Brownell4fca9542008-11-11 17:38:53 -0800290}
David Brownell7ad68e22008-11-11 17:39:02 -0800291static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
David Brownell4fca9542008-11-11 17:38:53 -0800292
David Brownell853116a2009-01-14 23:03:17 -0800293static ssize_t regulator_status_show(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
296 struct regulator_dev *rdev = dev_get_drvdata(dev);
297 int status;
298 char *label;
299
300 status = rdev->desc->ops->get_status(rdev);
301 if (status < 0)
302 return status;
303
304 switch (status) {
305 case REGULATOR_STATUS_OFF:
306 label = "off";
307 break;
308 case REGULATOR_STATUS_ON:
309 label = "on";
310 break;
311 case REGULATOR_STATUS_ERROR:
312 label = "error";
313 break;
314 case REGULATOR_STATUS_FAST:
315 label = "fast";
316 break;
317 case REGULATOR_STATUS_NORMAL:
318 label = "normal";
319 break;
320 case REGULATOR_STATUS_IDLE:
321 label = "idle";
322 break;
323 case REGULATOR_STATUS_STANDBY:
324 label = "standby";
325 break;
326 default:
327 return -ERANGE;
328 }
329
330 return sprintf(buf, "%s\n", label);
331}
332static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
333
Liam Girdwood414c70c2008-04-30 15:59:04 +0100334static ssize_t regulator_min_uA_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100337 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100338
339 if (!rdev->constraints)
340 return sprintf(buf, "constraint not defined\n");
341
342 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
343}
David Brownell7ad68e22008-11-11 17:39:02 -0800344static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100345
346static ssize_t regulator_max_uA_show(struct device *dev,
347 struct device_attribute *attr, char *buf)
348{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100349 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100350
351 if (!rdev->constraints)
352 return sprintf(buf, "constraint not defined\n");
353
354 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
355}
David Brownell7ad68e22008-11-11 17:39:02 -0800356static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100357
358static ssize_t regulator_min_uV_show(struct device *dev,
359 struct device_attribute *attr, char *buf)
360{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100361 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100362
363 if (!rdev->constraints)
364 return sprintf(buf, "constraint not defined\n");
365
366 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
367}
David Brownell7ad68e22008-11-11 17:39:02 -0800368static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100369
370static ssize_t regulator_max_uV_show(struct device *dev,
371 struct device_attribute *attr, char *buf)
372{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100373 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100374
375 if (!rdev->constraints)
376 return sprintf(buf, "constraint not defined\n");
377
378 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
379}
David Brownell7ad68e22008-11-11 17:39:02 -0800380static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100381
382static ssize_t regulator_total_uA_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100385 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100386 struct regulator *regulator;
387 int uA = 0;
388
389 mutex_lock(&rdev->mutex);
390 list_for_each_entry(regulator, &rdev->consumer_list, list)
391 uA += regulator->uA_load;
392 mutex_unlock(&rdev->mutex);
393 return sprintf(buf, "%d\n", uA);
394}
David Brownell7ad68e22008-11-11 17:39:02 -0800395static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100396
397static ssize_t regulator_num_users_show(struct device *dev,
398 struct device_attribute *attr, char *buf)
399{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100400 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100401 return sprintf(buf, "%d\n", rdev->use_count);
402}
403
404static ssize_t regulator_type_show(struct device *dev,
405 struct device_attribute *attr, char *buf)
406{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100407 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100408
409 switch (rdev->desc->type) {
410 case REGULATOR_VOLTAGE:
411 return sprintf(buf, "voltage\n");
412 case REGULATOR_CURRENT:
413 return sprintf(buf, "current\n");
414 }
415 return sprintf(buf, "unknown\n");
416}
417
418static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
419 struct device_attribute *attr, char *buf)
420{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100421 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100422
Liam Girdwood414c70c2008-04-30 15:59:04 +0100423 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
424}
David Brownell7ad68e22008-11-11 17:39:02 -0800425static DEVICE_ATTR(suspend_mem_microvolts, 0444,
426 regulator_suspend_mem_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100427
428static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100431 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100432
Liam Girdwood414c70c2008-04-30 15:59:04 +0100433 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
434}
David Brownell7ad68e22008-11-11 17:39:02 -0800435static DEVICE_ATTR(suspend_disk_microvolts, 0444,
436 regulator_suspend_disk_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100437
438static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
439 struct device_attribute *attr, char *buf)
440{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100441 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100442
Liam Girdwood414c70c2008-04-30 15:59:04 +0100443 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
444}
David Brownell7ad68e22008-11-11 17:39:02 -0800445static DEVICE_ATTR(suspend_standby_microvolts, 0444,
446 regulator_suspend_standby_uV_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100447
Liam Girdwood414c70c2008-04-30 15:59:04 +0100448static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100451 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100452
David Brownell4fca9542008-11-11 17:38:53 -0800453 return regulator_print_opmode(buf,
454 rdev->constraints->state_mem.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100455}
David Brownell7ad68e22008-11-11 17:39:02 -0800456static DEVICE_ATTR(suspend_mem_mode, 0444,
457 regulator_suspend_mem_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100458
459static ssize_t regulator_suspend_disk_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_disk.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100466}
David Brownell7ad68e22008-11-11 17:39:02 -0800467static DEVICE_ATTR(suspend_disk_mode, 0444,
468 regulator_suspend_disk_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100469
470static ssize_t regulator_suspend_standby_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_standby.mode);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100477}
David Brownell7ad68e22008-11-11 17:39:02 -0800478static DEVICE_ATTR(suspend_standby_mode, 0444,
479 regulator_suspend_standby_mode_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100480
481static ssize_t regulator_suspend_mem_state_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_state(buf,
487 rdev->constraints->state_mem.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100488}
David Brownell7ad68e22008-11-11 17:39:02 -0800489static DEVICE_ATTR(suspend_mem_state, 0444,
490 regulator_suspend_mem_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100491
492static ssize_t regulator_suspend_disk_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_disk.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100499}
David Brownell7ad68e22008-11-11 17:39:02 -0800500static DEVICE_ATTR(suspend_disk_state, 0444,
501 regulator_suspend_disk_state_show, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100502
503static ssize_t regulator_suspend_standby_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_standby.enabled);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100510}
David Brownell7ad68e22008-11-11 17:39:02 -0800511static DEVICE_ATTR(suspend_standby_state, 0444,
512 regulator_suspend_standby_state_show, NULL);
Mark Brownbc558a62008-10-10 15:33:20 +0100513
David Brownell7ad68e22008-11-11 17:39:02 -0800514
515/*
516 * These are the only attributes are present for all regulators.
517 * Other attributes are a function of regulator functionality.
518 */
Liam Girdwood414c70c2008-04-30 15:59:04 +0100519static struct device_attribute regulator_dev_attrs[] = {
Mark Brownbc558a62008-10-10 15:33:20 +0100520 __ATTR(name, 0444, regulator_name_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100521 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
522 __ATTR(type, 0444, regulator_type_show, NULL),
Liam Girdwood414c70c2008-04-30 15:59:04 +0100523 __ATTR_NULL,
524};
525
526static void regulator_dev_release(struct device *dev)
527{
Liam Girdwooda5766f12008-10-10 13:22:20 +0100528 struct regulator_dev *rdev = dev_get_drvdata(dev);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100529 kfree(rdev);
530}
531
532static struct class regulator_class = {
533 .name = "regulator",
534 .dev_release = regulator_dev_release,
535 .dev_attrs = regulator_dev_attrs,
536};
537
538/* Calculate the new optimum regulator operating mode based on the new total
539 * consumer load. All locks held by caller */
540static void drms_uA_update(struct regulator_dev *rdev)
541{
542 struct regulator *sibling;
543 int current_uA = 0, output_uV, input_uV, err;
544 unsigned int mode;
545
546 err = regulator_check_drms(rdev);
547 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
Dan Carpenter036de8e2009-04-08 13:52:39 +0300548 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
549 return;
Liam Girdwood414c70c2008-04-30 15:59:04 +0100550
551 /* get output voltage */
552 output_uV = rdev->desc->ops->get_voltage(rdev);
553 if (output_uV <= 0)
554 return;
555
556 /* get input voltage */
557 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
558 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
559 else
560 input_uV = rdev->constraints->input_uV;
561 if (input_uV <= 0)
562 return;
563
564 /* calc total requested load */
565 list_for_each_entry(sibling, &rdev->consumer_list, list)
566 current_uA += sibling->uA_load;
567
568 /* now get the optimum mode for our new total regulator load */
569 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
570 output_uV, current_uA);
571
572 /* check the new mode is allowed */
573 err = regulator_check_mode(rdev, mode);
574 if (err == 0)
575 rdev->desc->ops->set_mode(rdev, mode);
576}
577
578static int suspend_set_state(struct regulator_dev *rdev,
579 struct regulator_state *rstate)
580{
581 int ret = 0;
582
583 /* enable & disable are mandatory for suspend control */
584 if (!rdev->desc->ops->set_suspend_enable ||
Liam Girdwooda5766f12008-10-10 13:22:20 +0100585 !rdev->desc->ops->set_suspend_disable) {
586 printk(KERN_ERR "%s: no way to set suspend state\n",
587 __func__);
Liam Girdwood414c70c2008-04-30 15:59:04 +0100588 return -EINVAL;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100589 }
Liam Girdwood414c70c2008-04-30 15:59:04 +0100590
591 if (rstate->enabled)
592 ret = rdev->desc->ops->set_suspend_enable(rdev);
593 else
594 ret = rdev->desc->ops->set_suspend_disable(rdev);
595 if (ret < 0) {
596 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
597 return ret;
598 }
599
600 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
601 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
602 if (ret < 0) {
603 printk(KERN_ERR "%s: failed to set voltage\n",
604 __func__);
605 return ret;
606 }
607 }
608
609 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
610 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
611 if (ret < 0) {
612 printk(KERN_ERR "%s: failed to set mode\n", __func__);
613 return ret;
614 }
615 }
616 return ret;
617}
618
619/* locks held by caller */
620static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
621{
622 if (!rdev->constraints)
623 return -EINVAL;
624
625 switch (state) {
626 case PM_SUSPEND_STANDBY:
627 return suspend_set_state(rdev,
628 &rdev->constraints->state_standby);
629 case PM_SUSPEND_MEM:
630 return suspend_set_state(rdev,
631 &rdev->constraints->state_mem);
632 case PM_SUSPEND_MAX:
633 return suspend_set_state(rdev,
634 &rdev->constraints->state_disk);
635 default:
636 return -EINVAL;
637 }
638}
639
640static void print_constraints(struct regulator_dev *rdev)
641{
642 struct regulation_constraints *constraints = rdev->constraints;
643 char buf[80];
644 int count;
645
646 if (rdev->desc->type == REGULATOR_VOLTAGE) {
647 if (constraints->min_uV == constraints->max_uV)
648 count = sprintf(buf, "%d mV ",
649 constraints->min_uV / 1000);
650 else
651 count = sprintf(buf, "%d <--> %d mV ",
652 constraints->min_uV / 1000,
653 constraints->max_uV / 1000);
654 } else {
655 if (constraints->min_uA == constraints->max_uA)
656 count = sprintf(buf, "%d mA ",
657 constraints->min_uA / 1000);
658 else
659 count = sprintf(buf, "%d <--> %d mA ",
660 constraints->min_uA / 1000,
661 constraints->max_uA / 1000);
662 }
663 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
664 count += sprintf(buf + count, "fast ");
665 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
666 count += sprintf(buf + count, "normal ");
667 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
668 count += sprintf(buf + count, "idle ");
669 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
670 count += sprintf(buf + count, "standby");
671
672 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
673}
674
Mark Browne79055d2009-10-19 15:53:50 +0100675static int machine_constraints_voltage(struct regulator_dev *rdev,
676 const char *name, struct regulation_constraints *constraints)
677{
678 struct regulator_ops *ops = rdev->desc->ops;
679
680 /* constrain machine-level voltage specs to fit
681 * the actual range supported by this regulator.
682 */
683 if (ops->list_voltage && rdev->desc->n_voltages) {
684 int count = rdev->desc->n_voltages;
685 int i;
686 int min_uV = INT_MAX;
687 int max_uV = INT_MIN;
688 int cmin = constraints->min_uV;
689 int cmax = constraints->max_uV;
690
691 /* it's safe to autoconfigure fixed-voltage supplies
692 and the constraints are used by list_voltage. */
693 if (count == 1 && !cmin) {
694 cmin = 1;
695 cmax = INT_MAX;
696 constraints->min_uV = cmin;
697 constraints->max_uV = cmax;
698 }
699
700 /* voltage constraints are optional */
701 if ((cmin == 0) && (cmax == 0))
702 return 0;
703
704 /* else require explicit machine-level constraints */
705 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
706 pr_err("%s: %s '%s' voltage constraints\n",
707 __func__, "invalid", name);
708 return -EINVAL;
709 }
710
711 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
712 for (i = 0; i < count; i++) {
713 int value;
714
715 value = ops->list_voltage(rdev, i);
716 if (value <= 0)
717 continue;
718
719 /* maybe adjust [min_uV..max_uV] */
720 if (value >= cmin && value < min_uV)
721 min_uV = value;
722 if (value <= cmax && value > max_uV)
723 max_uV = value;
724 }
725
726 /* final: [min_uV..max_uV] valid iff constraints valid */
727 if (max_uV < min_uV) {
728 pr_err("%s: %s '%s' voltage constraints\n",
729 __func__, "unsupportable", name);
730 return -EINVAL;
731 }
732
733 /* use regulator's subset of machine constraints */
734 if (constraints->min_uV < min_uV) {
735 pr_debug("%s: override '%s' %s, %d -> %d\n",
736 __func__, name, "min_uV",
737 constraints->min_uV, min_uV);
738 constraints->min_uV = min_uV;
739 }
740 if (constraints->max_uV > max_uV) {
741 pr_debug("%s: override '%s' %s, %d -> %d\n",
742 __func__, name, "max_uV",
743 constraints->max_uV, max_uV);
744 constraints->max_uV = max_uV;
745 }
746 }
747
748 return 0;
749}
750
Liam Girdwooda5766f12008-10-10 13:22:20 +0100751/**
752 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000753 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000754 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100755 *
756 * Allows platform initialisation code to define and constrain
757 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
758 * Constraints *must* be set by platform code in order for some
759 * regulator operations to proceed i.e. set_voltage, set_current_limit,
760 * set_mode.
761 */
762static int set_machine_constraints(struct regulator_dev *rdev,
763 struct regulation_constraints *constraints)
764{
765 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100766 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100767 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100768
769 if (constraints->name)
770 name = constraints->name;
771 else if (rdev->desc->name)
772 name = rdev->desc->name;
773 else
774 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100775
Mark Browne79055d2009-10-19 15:53:50 +0100776 ret = machine_constraints_voltage(rdev, name, constraints);
777 if (ret != 0)
778 goto out;
David Brownell4367cfd2009-02-26 11:48:36 -0800779
Liam Girdwooda5766f12008-10-10 13:22:20 +0100780 rdev->constraints = constraints;
781
782 /* do we need to apply the constraint voltage */
783 if (rdev->constraints->apply_uV &&
784 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100785 ops->set_voltage) {
786 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100787 rdev->constraints->min_uV, rdev->constraints->max_uV);
788 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100789 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
790 __func__,
791 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100792 rdev->constraints = NULL;
793 goto out;
794 }
795 }
796
Liam Girdwooda5766f12008-10-10 13:22:20 +0100797 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100798 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100799 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100800 if (ret < 0) {
801 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
802 __func__, name);
803 rdev->constraints = NULL;
804 goto out;
805 }
806 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100807
Mark Browna3084662009-02-26 19:24:19 +0000808 if (constraints->initial_mode) {
809 if (!ops->set_mode) {
810 printk(KERN_ERR "%s: no set_mode operation for %s\n",
811 __func__, name);
812 ret = -EINVAL;
813 goto out;
814 }
815
816 ret = ops->set_mode(rdev, constraints->initial_mode);
817 if (ret < 0) {
818 printk(KERN_ERR
819 "%s: failed to set initial mode for %s: %d\n",
820 __func__, name, ret);
821 goto out;
822 }
823 }
824
Mark Browncacf90f2009-03-02 16:32:46 +0000825 /* If the constraints say the regulator should be on at this point
826 * and we have control then make sure it is enabled.
827 */
828 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100829 ret = ops->enable(rdev);
830 if (ret < 0) {
831 printk(KERN_ERR "%s: failed to enable %s\n",
832 __func__, name);
833 rdev->constraints = NULL;
834 goto out;
835 }
836 }
837
Liam Girdwooda5766f12008-10-10 13:22:20 +0100838 print_constraints(rdev);
839out:
840 return ret;
841}
842
843/**
844 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000845 * @rdev: regulator name
846 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100847 *
848 * Called by platform initialisation code to set the supply regulator for this
849 * regulator. This ensures that a regulators supply will also be enabled by the
850 * core if it's child is enabled.
851 */
852static int set_supply(struct regulator_dev *rdev,
853 struct regulator_dev *supply_rdev)
854{
855 int err;
856
857 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
858 "supply");
859 if (err) {
860 printk(KERN_ERR
861 "%s: could not add device link %s err %d\n",
862 __func__, supply_rdev->dev.kobj.name, err);
863 goto out;
864 }
865 rdev->supply = supply_rdev;
866 list_add(&rdev->slist, &supply_rdev->supply_list);
867out:
868 return err;
869}
870
871/**
872 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000873 * @rdev: regulator source
874 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100875 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000876 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100877 *
878 * Allows platform initialisation code to map physical regulator
879 * sources to symbolic names for supplies for use by devices. Devices
880 * should use these symbolic names to request regulators, avoiding the
881 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100882 *
883 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100884 */
885static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100886 struct device *consumer_dev, const char *consumer_dev_name,
887 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100888{
889 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100890 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100891
Mark Brown40f92442009-06-17 17:56:39 +0100892 if (consumer_dev && consumer_dev_name)
893 return -EINVAL;
894
895 if (!consumer_dev_name && consumer_dev)
896 consumer_dev_name = dev_name(consumer_dev);
897
Liam Girdwooda5766f12008-10-10 13:22:20 +0100898 if (supply == NULL)
899 return -EINVAL;
900
Mark Brown9ed20992009-07-21 16:00:26 +0100901 if (consumer_dev_name != NULL)
902 has_dev = 1;
903 else
904 has_dev = 0;
905
David Brownell6001e132008-12-31 12:54:19 +0000906 list_for_each_entry(node, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100907 if (consumer_dev_name != node->dev_name)
David Brownell6001e132008-12-31 12:54:19 +0000908 continue;
909 if (strcmp(node->supply, supply) != 0)
910 continue;
911
912 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
913 dev_name(&node->regulator->dev),
914 node->regulator->desc->name,
915 supply,
916 dev_name(&rdev->dev), rdev->desc->name);
917 return -EBUSY;
918 }
919
Mark Brown9ed20992009-07-21 16:00:26 +0100920 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100921 if (node == NULL)
922 return -ENOMEM;
923
924 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925 node->supply = supply;
926
Mark Brown9ed20992009-07-21 16:00:26 +0100927 if (has_dev) {
928 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
929 if (node->dev_name == NULL) {
930 kfree(node);
931 return -ENOMEM;
932 }
Mark Brown40f92442009-06-17 17:56:39 +0100933 }
934
Liam Girdwooda5766f12008-10-10 13:22:20 +0100935 list_add(&node->list, &regulator_map_list);
936 return 0;
937}
938
939static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100940 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100941{
942 struct regulator_map *node, *n;
943
Mark Brown40f92442009-06-17 17:56:39 +0100944 if (consumer_dev && !consumer_dev_name)
945 consumer_dev_name = dev_name(consumer_dev);
946
Liam Girdwooda5766f12008-10-10 13:22:20 +0100947 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100948 if (rdev != node->regulator)
949 continue;
950
951 if (consumer_dev_name && node->dev_name &&
952 strcmp(consumer_dev_name, node->dev_name))
953 continue;
954
955 list_del(&node->list);
956 kfree(node->dev_name);
957 kfree(node);
958 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100959 }
960}
961
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200962static void unset_regulator_supplies(struct regulator_dev *rdev)
963{
964 struct regulator_map *node, *n;
965
966 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
967 if (rdev == node->regulator) {
968 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100969 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200970 kfree(node);
971 return;
972 }
973 }
974}
975
Liam Girdwood414c70c2008-04-30 15:59:04 +0100976#define REG_STR_SIZE 32
977
978static struct regulator *create_regulator(struct regulator_dev *rdev,
979 struct device *dev,
980 const char *supply_name)
981{
982 struct regulator *regulator;
983 char buf[REG_STR_SIZE];
984 int err, size;
985
986 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
987 if (regulator == NULL)
988 return NULL;
989
990 mutex_lock(&rdev->mutex);
991 regulator->rdev = rdev;
992 list_add(&regulator->list, &rdev->consumer_list);
993
994 if (dev) {
995 /* create a 'requested_microamps_name' sysfs entry */
996 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
997 supply_name);
998 if (size >= REG_STR_SIZE)
999 goto overflow_err;
1000
1001 regulator->dev = dev;
1002 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1003 if (regulator->dev_attr.attr.name == NULL)
1004 goto attr_name_err;
1005
1006 regulator->dev_attr.attr.owner = THIS_MODULE;
1007 regulator->dev_attr.attr.mode = 0444;
1008 regulator->dev_attr.show = device_requested_uA_show;
1009 err = device_create_file(dev, &regulator->dev_attr);
1010 if (err < 0) {
1011 printk(KERN_WARNING "%s: could not add regulator_dev"
1012 " load sysfs\n", __func__);
1013 goto attr_name_err;
1014 }
1015
1016 /* also add a link to the device sysfs entry */
1017 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1018 dev->kobj.name, supply_name);
1019 if (size >= REG_STR_SIZE)
1020 goto attr_err;
1021
1022 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1023 if (regulator->supply_name == NULL)
1024 goto attr_err;
1025
1026 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1027 buf);
1028 if (err) {
1029 printk(KERN_WARNING
1030 "%s: could not add device link %s err %d\n",
1031 __func__, dev->kobj.name, err);
1032 device_remove_file(dev, &regulator->dev_attr);
1033 goto link_name_err;
1034 }
1035 }
1036 mutex_unlock(&rdev->mutex);
1037 return regulator;
1038link_name_err:
1039 kfree(regulator->supply_name);
1040attr_err:
1041 device_remove_file(regulator->dev, &regulator->dev_attr);
1042attr_name_err:
1043 kfree(regulator->dev_attr.attr.name);
1044overflow_err:
1045 list_del(&regulator->list);
1046 kfree(regulator);
1047 mutex_unlock(&rdev->mutex);
1048 return NULL;
1049}
1050
Mark Brown5ffbd132009-07-21 16:00:23 +01001051/* Internal regulator request function */
1052static struct regulator *_regulator_get(struct device *dev, const char *id,
1053 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001054{
1055 struct regulator_dev *rdev;
1056 struct regulator_map *map;
1057 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001058 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001059 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001060
1061 if (id == NULL) {
1062 printk(KERN_ERR "regulator: get() with no identifier\n");
1063 return regulator;
1064 }
1065
Mark Brown40f92442009-06-17 17:56:39 +01001066 if (dev)
1067 devname = dev_name(dev);
1068
Liam Girdwood414c70c2008-04-30 15:59:04 +01001069 mutex_lock(&regulator_list_mutex);
1070
1071 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001072 /* If the mapping has a device set up it must match */
1073 if (map->dev_name &&
1074 (!devname || strcmp(map->dev_name, devname)))
1075 continue;
1076
1077 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001078 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001079 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001080 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001081 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001082 mutex_unlock(&regulator_list_mutex);
1083 return regulator;
1084
1085found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001086 if (rdev->exclusive) {
1087 regulator = ERR_PTR(-EPERM);
1088 goto out;
1089 }
1090
1091 if (exclusive && rdev->open_count) {
1092 regulator = ERR_PTR(-EBUSY);
1093 goto out;
1094 }
1095
Liam Girdwooda5766f12008-10-10 13:22:20 +01001096 if (!try_module_get(rdev->owner))
1097 goto out;
1098
Liam Girdwood414c70c2008-04-30 15:59:04 +01001099 regulator = create_regulator(rdev, dev, id);
1100 if (regulator == NULL) {
1101 regulator = ERR_PTR(-ENOMEM);
1102 module_put(rdev->owner);
1103 }
1104
Mark Brown5ffbd132009-07-21 16:00:23 +01001105 rdev->open_count++;
1106 if (exclusive) {
1107 rdev->exclusive = 1;
1108
1109 ret = _regulator_is_enabled(rdev);
1110 if (ret > 0)
1111 rdev->use_count = 1;
1112 else
1113 rdev->use_count = 0;
1114 }
1115
Liam Girdwooda5766f12008-10-10 13:22:20 +01001116out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001117 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001118
Liam Girdwood414c70c2008-04-30 15:59:04 +01001119 return regulator;
1120}
Mark Brown5ffbd132009-07-21 16:00:23 +01001121
1122/**
1123 * regulator_get - lookup and obtain a reference to a regulator.
1124 * @dev: device for regulator "consumer"
1125 * @id: Supply name or regulator ID.
1126 *
1127 * Returns a struct regulator corresponding to the regulator producer,
1128 * or IS_ERR() condition containing errno.
1129 *
1130 * Use of supply names configured via regulator_set_device_supply() is
1131 * strongly encouraged. It is recommended that the supply name used
1132 * should match the name used for the supply and/or the relevant
1133 * device pins in the datasheet.
1134 */
1135struct regulator *regulator_get(struct device *dev, const char *id)
1136{
1137 return _regulator_get(dev, id, 0);
1138}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001139EXPORT_SYMBOL_GPL(regulator_get);
1140
1141/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001142 * regulator_get_exclusive - obtain exclusive access to a regulator.
1143 * @dev: device for regulator "consumer"
1144 * @id: Supply name or regulator ID.
1145 *
1146 * Returns a struct regulator corresponding to the regulator producer,
1147 * or IS_ERR() condition containing errno. Other consumers will be
1148 * unable to obtain this reference is held and the use count for the
1149 * regulator will be initialised to reflect the current state of the
1150 * regulator.
1151 *
1152 * This is intended for use by consumers which cannot tolerate shared
1153 * use of the regulator such as those which need to force the
1154 * regulator off for correct operation of the hardware they are
1155 * controlling.
1156 *
1157 * Use of supply names configured via regulator_set_device_supply() is
1158 * strongly encouraged. It is recommended that the supply name used
1159 * should match the name used for the supply and/or the relevant
1160 * device pins in the datasheet.
1161 */
1162struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1163{
1164 return _regulator_get(dev, id, 1);
1165}
1166EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1167
1168/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001169 * regulator_put - "free" the regulator source
1170 * @regulator: regulator source
1171 *
1172 * Note: drivers must ensure that all regulator_enable calls made on this
1173 * regulator source are balanced by regulator_disable calls prior to calling
1174 * this function.
1175 */
1176void regulator_put(struct regulator *regulator)
1177{
1178 struct regulator_dev *rdev;
1179
1180 if (regulator == NULL || IS_ERR(regulator))
1181 return;
1182
Liam Girdwood414c70c2008-04-30 15:59:04 +01001183 mutex_lock(&regulator_list_mutex);
1184 rdev = regulator->rdev;
1185
1186 /* remove any sysfs entries */
1187 if (regulator->dev) {
1188 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1189 kfree(regulator->supply_name);
1190 device_remove_file(regulator->dev, &regulator->dev_attr);
1191 kfree(regulator->dev_attr.attr.name);
1192 }
1193 list_del(&regulator->list);
1194 kfree(regulator);
1195
Mark Brown5ffbd132009-07-21 16:00:23 +01001196 rdev->open_count--;
1197 rdev->exclusive = 0;
1198
Liam Girdwood414c70c2008-04-30 15:59:04 +01001199 module_put(rdev->owner);
1200 mutex_unlock(&regulator_list_mutex);
1201}
1202EXPORT_SYMBOL_GPL(regulator_put);
1203
Mark Brown9a2372f2009-08-03 18:49:57 +01001204static int _regulator_can_change_status(struct regulator_dev *rdev)
1205{
1206 if (!rdev->constraints)
1207 return 0;
1208
1209 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1210 return 1;
1211 else
1212 return 0;
1213}
1214
Liam Girdwood414c70c2008-04-30 15:59:04 +01001215/* locks held by regulator_enable() */
1216static int _regulator_enable(struct regulator_dev *rdev)
1217{
Mark Brown9a2372f2009-08-03 18:49:57 +01001218 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001219
1220 /* do we need to enable the supply regulator first */
1221 if (rdev->supply) {
1222 ret = _regulator_enable(rdev->supply);
1223 if (ret < 0) {
1224 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1225 __func__, rdev->desc->name, ret);
1226 return ret;
1227 }
1228 }
1229
1230 /* check voltage and requested load before enabling */
Mark Brown9a2372f2009-08-03 18:49:57 +01001231 if (rdev->constraints &&
1232 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1233 drms_uA_update(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001234
Mark Brown9a2372f2009-08-03 18:49:57 +01001235 if (rdev->use_count == 0) {
1236 /* The regulator may on if it's not switchable or left on */
1237 ret = _regulator_is_enabled(rdev);
1238 if (ret == -EINVAL || ret == 0) {
1239 if (!_regulator_can_change_status(rdev))
1240 return -EPERM;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001241
Mark Brown9a2372f2009-08-03 18:49:57 +01001242 if (rdev->desc->ops->enable) {
1243 ret = rdev->desc->ops->enable(rdev);
1244 if (ret < 0)
1245 return ret;
1246 } else {
1247 return -EINVAL;
1248 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001249 } else if (ret < 0) {
Mark Brown9a2372f2009-08-03 18:49:57 +01001250 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251 __func__, rdev->desc->name, ret);
1252 return ret;
1253 }
Linus Walleija7433cf2009-08-26 12:54:04 +02001254 /* Fallthrough on positive return values - already enabled */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255 }
1256
Mark Brown9a2372f2009-08-03 18:49:57 +01001257 rdev->use_count++;
1258
1259 return 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001260}
1261
1262/**
1263 * regulator_enable - enable regulator output
1264 * @regulator: regulator source
1265 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001266 * Request that the regulator be enabled with the regulator output at
1267 * the predefined voltage or current value. Calls to regulator_enable()
1268 * must be balanced with calls to regulator_disable().
1269 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001270 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001271 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001272 */
1273int regulator_enable(struct regulator *regulator)
1274{
David Brownell412aec62008-11-16 11:44:46 -08001275 struct regulator_dev *rdev = regulator->rdev;
1276 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001277
David Brownell412aec62008-11-16 11:44:46 -08001278 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001279 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001280 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001281 return ret;
1282}
1283EXPORT_SYMBOL_GPL(regulator_enable);
1284
1285/* locks held by regulator_disable() */
1286static int _regulator_disable(struct regulator_dev *rdev)
1287{
1288 int ret = 0;
1289
David Brownellcd94b502009-03-11 16:43:34 -08001290 if (WARN(rdev->use_count <= 0,
1291 "unbalanced disables for %s\n",
1292 rdev->desc->name))
1293 return -EIO;
1294
Liam Girdwood414c70c2008-04-30 15:59:04 +01001295 /* are we the last user and permitted to disable ? */
Mark Brown60ef66f2009-10-13 13:06:50 +01001296 if (rdev->use_count == 1 &&
1297 (rdev->constraints && !rdev->constraints->always_on)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001298
1299 /* we are last user */
Mark Brown9a2372f2009-08-03 18:49:57 +01001300 if (_regulator_can_change_status(rdev) &&
1301 rdev->desc->ops->disable) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001302 ret = rdev->desc->ops->disable(rdev);
1303 if (ret < 0) {
1304 printk(KERN_ERR "%s: failed to disable %s\n",
1305 __func__, rdev->desc->name);
1306 return ret;
1307 }
1308 }
1309
1310 /* decrease our supplies ref count and disable if required */
1311 if (rdev->supply)
1312 _regulator_disable(rdev->supply);
1313
1314 rdev->use_count = 0;
1315 } else if (rdev->use_count > 1) {
1316
1317 if (rdev->constraints &&
1318 (rdev->constraints->valid_ops_mask &
1319 REGULATOR_CHANGE_DRMS))
1320 drms_uA_update(rdev);
1321
1322 rdev->use_count--;
1323 }
1324 return ret;
1325}
1326
1327/**
1328 * regulator_disable - disable regulator output
1329 * @regulator: regulator source
1330 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001331 * Disable the regulator output voltage or current. Calls to
1332 * regulator_enable() must be balanced with calls to
1333 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001334 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001335 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001336 * devices have it enabled, the regulator device supports disabling and
1337 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001338 */
1339int regulator_disable(struct regulator *regulator)
1340{
David Brownell412aec62008-11-16 11:44:46 -08001341 struct regulator_dev *rdev = regulator->rdev;
1342 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001343
David Brownell412aec62008-11-16 11:44:46 -08001344 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001345 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001346 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001347 return ret;
1348}
1349EXPORT_SYMBOL_GPL(regulator_disable);
1350
1351/* locks held by regulator_force_disable() */
1352static int _regulator_force_disable(struct regulator_dev *rdev)
1353{
1354 int ret = 0;
1355
1356 /* force disable */
1357 if (rdev->desc->ops->disable) {
1358 /* ah well, who wants to live forever... */
1359 ret = rdev->desc->ops->disable(rdev);
1360 if (ret < 0) {
1361 printk(KERN_ERR "%s: failed to force disable %s\n",
1362 __func__, rdev->desc->name);
1363 return ret;
1364 }
1365 /* notify other consumers that power has been forced off */
1366 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1367 NULL);
1368 }
1369
1370 /* decrease our supplies ref count and disable if required */
1371 if (rdev->supply)
1372 _regulator_disable(rdev->supply);
1373
1374 rdev->use_count = 0;
1375 return ret;
1376}
1377
1378/**
1379 * regulator_force_disable - force disable regulator output
1380 * @regulator: regulator source
1381 *
1382 * Forcibly disable the regulator output voltage or current.
1383 * NOTE: this *will* disable the regulator output even if other consumer
1384 * devices have it enabled. This should be used for situations when device
1385 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1386 */
1387int regulator_force_disable(struct regulator *regulator)
1388{
1389 int ret;
1390
1391 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001392 regulator->uA_load = 0;
1393 ret = _regulator_force_disable(regulator->rdev);
1394 mutex_unlock(&regulator->rdev->mutex);
1395 return ret;
1396}
1397EXPORT_SYMBOL_GPL(regulator_force_disable);
1398
1399static int _regulator_is_enabled(struct regulator_dev *rdev)
1400{
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401 /* sanity check */
Mark Brown93325462009-08-03 18:49:56 +01001402 if (!rdev->desc->ops->is_enabled)
1403 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001404
Mark Brown93325462009-08-03 18:49:56 +01001405 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001406}
1407
1408/**
1409 * regulator_is_enabled - is the regulator output enabled
1410 * @regulator: regulator source
1411 *
David Brownell412aec62008-11-16 11:44:46 -08001412 * Returns positive if the regulator driver backing the source/client
1413 * has requested that the device be enabled, zero if it hasn't, else a
1414 * negative errno code.
1415 *
1416 * Note that the device backing this regulator handle can have multiple
1417 * users, so it might be enabled even if regulator_enable() was never
1418 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001419 */
1420int regulator_is_enabled(struct regulator *regulator)
1421{
Mark Brown93325462009-08-03 18:49:56 +01001422 int ret;
1423
1424 mutex_lock(&regulator->rdev->mutex);
1425 ret = _regulator_is_enabled(regulator->rdev);
1426 mutex_unlock(&regulator->rdev->mutex);
1427
1428 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001429}
1430EXPORT_SYMBOL_GPL(regulator_is_enabled);
1431
1432/**
David Brownell4367cfd2009-02-26 11:48:36 -08001433 * regulator_count_voltages - count regulator_list_voltage() selectors
1434 * @regulator: regulator source
1435 *
1436 * Returns number of selectors, or negative errno. Selectors are
1437 * numbered starting at zero, and typically correspond to bitfields
1438 * in hardware registers.
1439 */
1440int regulator_count_voltages(struct regulator *regulator)
1441{
1442 struct regulator_dev *rdev = regulator->rdev;
1443
1444 return rdev->desc->n_voltages ? : -EINVAL;
1445}
1446EXPORT_SYMBOL_GPL(regulator_count_voltages);
1447
1448/**
1449 * regulator_list_voltage - enumerate supported voltages
1450 * @regulator: regulator source
1451 * @selector: identify voltage to list
1452 * Context: can sleep
1453 *
1454 * Returns a voltage that can be passed to @regulator_set_voltage(),
1455 * zero if this selector code can't be used on this sytem, or a
1456 * negative errno.
1457 */
1458int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1459{
1460 struct regulator_dev *rdev = regulator->rdev;
1461 struct regulator_ops *ops = rdev->desc->ops;
1462 int ret;
1463
1464 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1465 return -EINVAL;
1466
1467 mutex_lock(&rdev->mutex);
1468 ret = ops->list_voltage(rdev, selector);
1469 mutex_unlock(&rdev->mutex);
1470
1471 if (ret > 0) {
1472 if (ret < rdev->constraints->min_uV)
1473 ret = 0;
1474 else if (ret > rdev->constraints->max_uV)
1475 ret = 0;
1476 }
1477
1478 return ret;
1479}
1480EXPORT_SYMBOL_GPL(regulator_list_voltage);
1481
1482/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001483 * regulator_is_supported_voltage - check if a voltage range can be supported
1484 *
1485 * @regulator: Regulator to check.
1486 * @min_uV: Minimum required voltage in uV.
1487 * @max_uV: Maximum required voltage in uV.
1488 *
1489 * Returns a boolean or a negative error code.
1490 */
1491int regulator_is_supported_voltage(struct regulator *regulator,
1492 int min_uV, int max_uV)
1493{
1494 int i, voltages, ret;
1495
1496 ret = regulator_count_voltages(regulator);
1497 if (ret < 0)
1498 return ret;
1499 voltages = ret;
1500
1501 for (i = 0; i < voltages; i++) {
1502 ret = regulator_list_voltage(regulator, i);
1503
1504 if (ret >= min_uV && ret <= max_uV)
1505 return 1;
1506 }
1507
1508 return 0;
1509}
1510
1511/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001512 * regulator_set_voltage - set regulator output voltage
1513 * @regulator: regulator source
1514 * @min_uV: Minimum required voltage in uV
1515 * @max_uV: Maximum acceptable voltage in uV
1516 *
1517 * Sets a voltage regulator to the desired output voltage. This can be set
1518 * during any regulator state. IOW, regulator can be disabled or enabled.
1519 *
1520 * If the regulator is enabled then the voltage will change to the new value
1521 * immediately otherwise if the regulator is disabled the regulator will
1522 * output at the new voltage when enabled.
1523 *
1524 * NOTE: If the regulator is shared between several devices then the lowest
1525 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001526 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001527 * calling this function otherwise this call will fail.
1528 */
1529int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1530{
1531 struct regulator_dev *rdev = regulator->rdev;
1532 int ret;
1533
1534 mutex_lock(&rdev->mutex);
1535
1536 /* sanity check */
1537 if (!rdev->desc->ops->set_voltage) {
1538 ret = -EINVAL;
1539 goto out;
1540 }
1541
1542 /* constraints check */
1543 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1544 if (ret < 0)
1545 goto out;
1546 regulator->min_uV = min_uV;
1547 regulator->max_uV = max_uV;
1548 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1549
1550out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001551 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001552 mutex_unlock(&rdev->mutex);
1553 return ret;
1554}
1555EXPORT_SYMBOL_GPL(regulator_set_voltage);
1556
1557static int _regulator_get_voltage(struct regulator_dev *rdev)
1558{
1559 /* sanity check */
1560 if (rdev->desc->ops->get_voltage)
1561 return rdev->desc->ops->get_voltage(rdev);
1562 else
1563 return -EINVAL;
1564}
1565
1566/**
1567 * regulator_get_voltage - get regulator output voltage
1568 * @regulator: regulator source
1569 *
1570 * This returns the current regulator voltage in uV.
1571 *
1572 * NOTE: If the regulator is disabled it will return the voltage value. This
1573 * function should not be used to determine regulator state.
1574 */
1575int regulator_get_voltage(struct regulator *regulator)
1576{
1577 int ret;
1578
1579 mutex_lock(&regulator->rdev->mutex);
1580
1581 ret = _regulator_get_voltage(regulator->rdev);
1582
1583 mutex_unlock(&regulator->rdev->mutex);
1584
1585 return ret;
1586}
1587EXPORT_SYMBOL_GPL(regulator_get_voltage);
1588
1589/**
1590 * regulator_set_current_limit - set regulator output current limit
1591 * @regulator: regulator source
1592 * @min_uA: Minimuum supported current in uA
1593 * @max_uA: Maximum supported current in uA
1594 *
1595 * Sets current sink to the desired output current. This can be set during
1596 * any regulator state. IOW, regulator can be disabled or enabled.
1597 *
1598 * If the regulator is enabled then the current will change to the new value
1599 * immediately otherwise if the regulator is disabled the regulator will
1600 * output at the new current when enabled.
1601 *
1602 * NOTE: Regulator system constraints must be set for this regulator before
1603 * calling this function otherwise this call will fail.
1604 */
1605int regulator_set_current_limit(struct regulator *regulator,
1606 int min_uA, int max_uA)
1607{
1608 struct regulator_dev *rdev = regulator->rdev;
1609 int ret;
1610
1611 mutex_lock(&rdev->mutex);
1612
1613 /* sanity check */
1614 if (!rdev->desc->ops->set_current_limit) {
1615 ret = -EINVAL;
1616 goto out;
1617 }
1618
1619 /* constraints check */
1620 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1621 if (ret < 0)
1622 goto out;
1623
1624 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1625out:
1626 mutex_unlock(&rdev->mutex);
1627 return ret;
1628}
1629EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1630
1631static int _regulator_get_current_limit(struct regulator_dev *rdev)
1632{
1633 int ret;
1634
1635 mutex_lock(&rdev->mutex);
1636
1637 /* sanity check */
1638 if (!rdev->desc->ops->get_current_limit) {
1639 ret = -EINVAL;
1640 goto out;
1641 }
1642
1643 ret = rdev->desc->ops->get_current_limit(rdev);
1644out:
1645 mutex_unlock(&rdev->mutex);
1646 return ret;
1647}
1648
1649/**
1650 * regulator_get_current_limit - get regulator output current
1651 * @regulator: regulator source
1652 *
1653 * This returns the current supplied by the specified current sink in uA.
1654 *
1655 * NOTE: If the regulator is disabled it will return the current value. This
1656 * function should not be used to determine regulator state.
1657 */
1658int regulator_get_current_limit(struct regulator *regulator)
1659{
1660 return _regulator_get_current_limit(regulator->rdev);
1661}
1662EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1663
1664/**
1665 * regulator_set_mode - set regulator operating mode
1666 * @regulator: regulator source
1667 * @mode: operating mode - one of the REGULATOR_MODE constants
1668 *
1669 * Set regulator operating mode to increase regulator efficiency or improve
1670 * regulation performance.
1671 *
1672 * NOTE: Regulator system constraints must be set for this regulator before
1673 * calling this function otherwise this call will fail.
1674 */
1675int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1676{
1677 struct regulator_dev *rdev = regulator->rdev;
1678 int ret;
1679
1680 mutex_lock(&rdev->mutex);
1681
1682 /* sanity check */
1683 if (!rdev->desc->ops->set_mode) {
1684 ret = -EINVAL;
1685 goto out;
1686 }
1687
1688 /* constraints check */
1689 ret = regulator_check_mode(rdev, mode);
1690 if (ret < 0)
1691 goto out;
1692
1693 ret = rdev->desc->ops->set_mode(rdev, mode);
1694out:
1695 mutex_unlock(&rdev->mutex);
1696 return ret;
1697}
1698EXPORT_SYMBOL_GPL(regulator_set_mode);
1699
1700static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1701{
1702 int ret;
1703
1704 mutex_lock(&rdev->mutex);
1705
1706 /* sanity check */
1707 if (!rdev->desc->ops->get_mode) {
1708 ret = -EINVAL;
1709 goto out;
1710 }
1711
1712 ret = rdev->desc->ops->get_mode(rdev);
1713out:
1714 mutex_unlock(&rdev->mutex);
1715 return ret;
1716}
1717
1718/**
1719 * regulator_get_mode - get regulator operating mode
1720 * @regulator: regulator source
1721 *
1722 * Get the current regulator operating mode.
1723 */
1724unsigned int regulator_get_mode(struct regulator *regulator)
1725{
1726 return _regulator_get_mode(regulator->rdev);
1727}
1728EXPORT_SYMBOL_GPL(regulator_get_mode);
1729
1730/**
1731 * regulator_set_optimum_mode - set regulator optimum operating mode
1732 * @regulator: regulator source
1733 * @uA_load: load current
1734 *
1735 * Notifies the regulator core of a new device load. This is then used by
1736 * DRMS (if enabled by constraints) to set the most efficient regulator
1737 * operating mode for the new regulator loading.
1738 *
1739 * Consumer devices notify their supply regulator of the maximum power
1740 * they will require (can be taken from device datasheet in the power
1741 * consumption tables) when they change operational status and hence power
1742 * state. Examples of operational state changes that can affect power
1743 * consumption are :-
1744 *
1745 * o Device is opened / closed.
1746 * o Device I/O is about to begin or has just finished.
1747 * o Device is idling in between work.
1748 *
1749 * This information is also exported via sysfs to userspace.
1750 *
1751 * DRMS will sum the total requested load on the regulator and change
1752 * to the most efficient operating mode if platform constraints allow.
1753 *
1754 * Returns the new regulator mode or error.
1755 */
1756int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1757{
1758 struct regulator_dev *rdev = regulator->rdev;
1759 struct regulator *consumer;
1760 int ret, output_uV, input_uV, total_uA_load = 0;
1761 unsigned int mode;
1762
1763 mutex_lock(&rdev->mutex);
1764
1765 regulator->uA_load = uA_load;
1766 ret = regulator_check_drms(rdev);
1767 if (ret < 0)
1768 goto out;
1769 ret = -EINVAL;
1770
1771 /* sanity check */
1772 if (!rdev->desc->ops->get_optimum_mode)
1773 goto out;
1774
1775 /* get output voltage */
1776 output_uV = rdev->desc->ops->get_voltage(rdev);
1777 if (output_uV <= 0) {
1778 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1779 __func__, rdev->desc->name);
1780 goto out;
1781 }
1782
1783 /* get input voltage */
1784 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1785 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1786 else
1787 input_uV = rdev->constraints->input_uV;
1788 if (input_uV <= 0) {
1789 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1790 __func__, rdev->desc->name);
1791 goto out;
1792 }
1793
1794 /* calc total requested load for this regulator */
1795 list_for_each_entry(consumer, &rdev->consumer_list, list)
1796 total_uA_load += consumer->uA_load;
1797
1798 mode = rdev->desc->ops->get_optimum_mode(rdev,
1799 input_uV, output_uV,
1800 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001801 ret = regulator_check_mode(rdev, mode);
1802 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001803 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1804 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1805 total_uA_load, input_uV, output_uV);
1806 goto out;
1807 }
1808
1809 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001810 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001811 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1812 __func__, mode, rdev->desc->name);
1813 goto out;
1814 }
1815 ret = mode;
1816out:
1817 mutex_unlock(&rdev->mutex);
1818 return ret;
1819}
1820EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1821
1822/**
1823 * regulator_register_notifier - register regulator event notifier
1824 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001825 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001826 *
1827 * Register notifier block to receive regulator events.
1828 */
1829int regulator_register_notifier(struct regulator *regulator,
1830 struct notifier_block *nb)
1831{
1832 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1833 nb);
1834}
1835EXPORT_SYMBOL_GPL(regulator_register_notifier);
1836
1837/**
1838 * regulator_unregister_notifier - unregister regulator event notifier
1839 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001840 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001841 *
1842 * Unregister regulator event notifier block.
1843 */
1844int regulator_unregister_notifier(struct regulator *regulator,
1845 struct notifier_block *nb)
1846{
1847 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1848 nb);
1849}
1850EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1851
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001852/* notify regulator consumers and downstream regulator consumers.
1853 * Note mutex must be held by caller.
1854 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001855static void _notifier_call_chain(struct regulator_dev *rdev,
1856 unsigned long event, void *data)
1857{
1858 struct regulator_dev *_rdev;
1859
1860 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001861 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001862
1863 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001864 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1865 mutex_lock(&_rdev->mutex);
1866 _notifier_call_chain(_rdev, event, data);
1867 mutex_unlock(&_rdev->mutex);
1868 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001869}
1870
1871/**
1872 * regulator_bulk_get - get multiple regulator consumers
1873 *
1874 * @dev: Device to supply
1875 * @num_consumers: Number of consumers to register
1876 * @consumers: Configuration of consumers; clients are stored here.
1877 *
1878 * @return 0 on success, an errno on failure.
1879 *
1880 * This helper function allows drivers to get several regulator
1881 * consumers in one operation. If any of the regulators cannot be
1882 * acquired then any regulators that were allocated will be freed
1883 * before returning to the caller.
1884 */
1885int regulator_bulk_get(struct device *dev, int num_consumers,
1886 struct regulator_bulk_data *consumers)
1887{
1888 int i;
1889 int ret;
1890
1891 for (i = 0; i < num_consumers; i++)
1892 consumers[i].consumer = NULL;
1893
1894 for (i = 0; i < num_consumers; i++) {
1895 consumers[i].consumer = regulator_get(dev,
1896 consumers[i].supply);
1897 if (IS_ERR(consumers[i].consumer)) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001898 ret = PTR_ERR(consumers[i].consumer);
Mark Brown5b307622009-10-13 13:06:49 +01001899 dev_err(dev, "Failed to get supply '%s': %d\n",
1900 consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001901 consumers[i].consumer = NULL;
1902 goto err;
1903 }
1904 }
1905
1906 return 0;
1907
1908err:
1909 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1910 regulator_put(consumers[i].consumer);
1911
1912 return ret;
1913}
1914EXPORT_SYMBOL_GPL(regulator_bulk_get);
1915
1916/**
1917 * regulator_bulk_enable - enable multiple regulator consumers
1918 *
1919 * @num_consumers: Number of consumers
1920 * @consumers: Consumer data; clients are stored here.
1921 * @return 0 on success, an errno on failure
1922 *
1923 * This convenience API allows consumers to enable multiple regulator
1924 * clients in a single API call. If any consumers cannot be enabled
1925 * then any others that were enabled will be disabled again prior to
1926 * return.
1927 */
1928int regulator_bulk_enable(int num_consumers,
1929 struct regulator_bulk_data *consumers)
1930{
1931 int i;
1932 int ret;
1933
1934 for (i = 0; i < num_consumers; i++) {
1935 ret = regulator_enable(consumers[i].consumer);
1936 if (ret != 0)
1937 goto err;
1938 }
1939
1940 return 0;
1941
1942err:
Mark Brown5b307622009-10-13 13:06:49 +01001943 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001944 for (i = 0; i < num_consumers; i++)
1945 regulator_disable(consumers[i].consumer);
1946
1947 return ret;
1948}
1949EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1950
1951/**
1952 * regulator_bulk_disable - disable multiple regulator consumers
1953 *
1954 * @num_consumers: Number of consumers
1955 * @consumers: Consumer data; clients are stored here.
1956 * @return 0 on success, an errno on failure
1957 *
1958 * This convenience API allows consumers to disable multiple regulator
1959 * clients in a single API call. If any consumers cannot be enabled
1960 * then any others that were disabled will be disabled again prior to
1961 * return.
1962 */
1963int regulator_bulk_disable(int num_consumers,
1964 struct regulator_bulk_data *consumers)
1965{
1966 int i;
1967 int ret;
1968
1969 for (i = 0; i < num_consumers; i++) {
1970 ret = regulator_disable(consumers[i].consumer);
1971 if (ret != 0)
1972 goto err;
1973 }
1974
1975 return 0;
1976
1977err:
Mark Brown5b307622009-10-13 13:06:49 +01001978 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
1979 ret);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001980 for (i = 0; i < num_consumers; i++)
1981 regulator_enable(consumers[i].consumer);
1982
1983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1986
1987/**
1988 * regulator_bulk_free - free multiple regulator consumers
1989 *
1990 * @num_consumers: Number of consumers
1991 * @consumers: Consumer data; clients are stored here.
1992 *
1993 * This convenience API allows consumers to free multiple regulator
1994 * clients in a single API call.
1995 */
1996void regulator_bulk_free(int num_consumers,
1997 struct regulator_bulk_data *consumers)
1998{
1999 int i;
2000
2001 for (i = 0; i < num_consumers; i++) {
2002 regulator_put(consumers[i].consumer);
2003 consumers[i].consumer = NULL;
2004 }
2005}
2006EXPORT_SYMBOL_GPL(regulator_bulk_free);
2007
2008/**
2009 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00002010 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01002011 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00002012 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002013 *
2014 * Called by regulator drivers to notify clients a regulator event has
2015 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00002016 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01002017 */
2018int regulator_notifier_call_chain(struct regulator_dev *rdev,
2019 unsigned long event, void *data)
2020{
2021 _notifier_call_chain(rdev, event, data);
2022 return NOTIFY_DONE;
2023
2024}
2025EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2026
Mark Brownbe721972009-08-04 20:09:52 +02002027/**
2028 * regulator_mode_to_status - convert a regulator mode into a status
2029 *
2030 * @mode: Mode to convert
2031 *
2032 * Convert a regulator mode into a status.
2033 */
2034int regulator_mode_to_status(unsigned int mode)
2035{
2036 switch (mode) {
2037 case REGULATOR_MODE_FAST:
2038 return REGULATOR_STATUS_FAST;
2039 case REGULATOR_MODE_NORMAL:
2040 return REGULATOR_STATUS_NORMAL;
2041 case REGULATOR_MODE_IDLE:
2042 return REGULATOR_STATUS_IDLE;
2043 case REGULATOR_STATUS_STANDBY:
2044 return REGULATOR_STATUS_STANDBY;
2045 default:
2046 return 0;
2047 }
2048}
2049EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2050
David Brownell7ad68e22008-11-11 17:39:02 -08002051/*
2052 * To avoid cluttering sysfs (and memory) with useless state, only
2053 * create attributes that can be meaningfully displayed.
2054 */
2055static int add_regulator_attributes(struct regulator_dev *rdev)
2056{
2057 struct device *dev = &rdev->dev;
2058 struct regulator_ops *ops = rdev->desc->ops;
2059 int status = 0;
2060
2061 /* some attributes need specific methods to be displayed */
2062 if (ops->get_voltage) {
2063 status = device_create_file(dev, &dev_attr_microvolts);
2064 if (status < 0)
2065 return status;
2066 }
2067 if (ops->get_current_limit) {
2068 status = device_create_file(dev, &dev_attr_microamps);
2069 if (status < 0)
2070 return status;
2071 }
2072 if (ops->get_mode) {
2073 status = device_create_file(dev, &dev_attr_opmode);
2074 if (status < 0)
2075 return status;
2076 }
2077 if (ops->is_enabled) {
2078 status = device_create_file(dev, &dev_attr_state);
2079 if (status < 0)
2080 return status;
2081 }
David Brownell853116a2009-01-14 23:03:17 -08002082 if (ops->get_status) {
2083 status = device_create_file(dev, &dev_attr_status);
2084 if (status < 0)
2085 return status;
2086 }
David Brownell7ad68e22008-11-11 17:39:02 -08002087
2088 /* some attributes are type-specific */
2089 if (rdev->desc->type == REGULATOR_CURRENT) {
2090 status = device_create_file(dev, &dev_attr_requested_microamps);
2091 if (status < 0)
2092 return status;
2093 }
2094
2095 /* all the other attributes exist to support constraints;
2096 * don't show them if there are no constraints, or if the
2097 * relevant supporting methods are missing.
2098 */
2099 if (!rdev->constraints)
2100 return status;
2101
2102 /* constraints need specific supporting methods */
2103 if (ops->set_voltage) {
2104 status = device_create_file(dev, &dev_attr_min_microvolts);
2105 if (status < 0)
2106 return status;
2107 status = device_create_file(dev, &dev_attr_max_microvolts);
2108 if (status < 0)
2109 return status;
2110 }
2111 if (ops->set_current_limit) {
2112 status = device_create_file(dev, &dev_attr_min_microamps);
2113 if (status < 0)
2114 return status;
2115 status = device_create_file(dev, &dev_attr_max_microamps);
2116 if (status < 0)
2117 return status;
2118 }
2119
2120 /* suspend mode constraints need multiple supporting methods */
2121 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2122 return status;
2123
2124 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2125 if (status < 0)
2126 return status;
2127 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2128 if (status < 0)
2129 return status;
2130 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2131 if (status < 0)
2132 return status;
2133
2134 if (ops->set_suspend_voltage) {
2135 status = device_create_file(dev,
2136 &dev_attr_suspend_standby_microvolts);
2137 if (status < 0)
2138 return status;
2139 status = device_create_file(dev,
2140 &dev_attr_suspend_mem_microvolts);
2141 if (status < 0)
2142 return status;
2143 status = device_create_file(dev,
2144 &dev_attr_suspend_disk_microvolts);
2145 if (status < 0)
2146 return status;
2147 }
2148
2149 if (ops->set_suspend_mode) {
2150 status = device_create_file(dev,
2151 &dev_attr_suspend_standby_mode);
2152 if (status < 0)
2153 return status;
2154 status = device_create_file(dev,
2155 &dev_attr_suspend_mem_mode);
2156 if (status < 0)
2157 return status;
2158 status = device_create_file(dev,
2159 &dev_attr_suspend_disk_mode);
2160 if (status < 0)
2161 return status;
2162 }
2163
2164 return status;
2165}
2166
Liam Girdwood414c70c2008-04-30 15:59:04 +01002167/**
2168 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002169 * @regulator_desc: regulator to register
2170 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002171 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002172 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002173 *
2174 * Called by regulator drivers to register a regulator.
2175 * Returns 0 on success.
2176 */
2177struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002178 struct device *dev, struct regulator_init_data *init_data,
2179 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002180{
2181 static atomic_t regulator_no = ATOMIC_INIT(0);
2182 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002183 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002184
2185 if (regulator_desc == NULL)
2186 return ERR_PTR(-EINVAL);
2187
2188 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2189 return ERR_PTR(-EINVAL);
2190
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002191 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2192 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002193 return ERR_PTR(-EINVAL);
2194
Mark Brown46fabe1e2008-09-09 16:21:18 +01002195 if (!init_data)
2196 return ERR_PTR(-EINVAL);
2197
Liam Girdwood414c70c2008-04-30 15:59:04 +01002198 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2199 if (rdev == NULL)
2200 return ERR_PTR(-ENOMEM);
2201
2202 mutex_lock(&regulator_list_mutex);
2203
2204 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002205 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002206 rdev->owner = regulator_desc->owner;
2207 rdev->desc = regulator_desc;
2208 INIT_LIST_HEAD(&rdev->consumer_list);
2209 INIT_LIST_HEAD(&rdev->supply_list);
2210 INIT_LIST_HEAD(&rdev->list);
2211 INIT_LIST_HEAD(&rdev->slist);
2212 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2213
Liam Girdwooda5766f12008-10-10 13:22:20 +01002214 /* preform any regulator specific init */
2215 if (init_data->regulator_init) {
2216 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002217 if (ret < 0)
2218 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002219 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002220
Liam Girdwooda5766f12008-10-10 13:22:20 +01002221 /* register with sysfs */
2222 rdev->dev.class = &regulator_class;
2223 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002224 dev_set_name(&rdev->dev, "regulator.%d",
2225 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002226 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002227 if (ret != 0)
2228 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002229
2230 dev_set_drvdata(&rdev->dev, rdev);
2231
Mike Rapoport74f544c2008-11-25 14:53:53 +02002232 /* set regulator constraints */
2233 ret = set_machine_constraints(rdev, &init_data->constraints);
2234 if (ret < 0)
2235 goto scrub;
2236
David Brownell7ad68e22008-11-11 17:39:02 -08002237 /* add attributes supported by this regulator */
2238 ret = add_regulator_attributes(rdev);
2239 if (ret < 0)
2240 goto scrub;
2241
Liam Girdwooda5766f12008-10-10 13:22:20 +01002242 /* set supply regulator if it exists */
2243 if (init_data->supply_regulator_dev) {
2244 ret = set_supply(rdev,
2245 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002246 if (ret < 0)
2247 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002248 }
2249
2250 /* add consumers devices */
2251 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2252 ret = set_consumer_device_supply(rdev,
2253 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002254 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002255 init_data->consumer_supplies[i].supply);
2256 if (ret < 0) {
2257 for (--i; i >= 0; i--)
2258 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002259 init_data->consumer_supplies[i].dev_name,
2260 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002261 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002262 }
2263 }
2264
2265 list_add(&rdev->list, &regulator_list);
2266out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002267 mutex_unlock(&regulator_list_mutex);
2268 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002269
2270scrub:
2271 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002272 /* device core frees rdev */
2273 rdev = ERR_PTR(ret);
2274 goto out;
2275
David Brownell4fca9542008-11-11 17:38:53 -08002276clean:
2277 kfree(rdev);
2278 rdev = ERR_PTR(ret);
2279 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002280}
2281EXPORT_SYMBOL_GPL(regulator_register);
2282
2283/**
2284 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002285 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002286 *
2287 * Called by regulator drivers to unregister a regulator.
2288 */
2289void regulator_unregister(struct regulator_dev *rdev)
2290{
2291 if (rdev == NULL)
2292 return;
2293
2294 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002295 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002296 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002297 list_del(&rdev->list);
2298 if (rdev->supply)
2299 sysfs_remove_link(&rdev->dev.kobj, "supply");
2300 device_unregister(&rdev->dev);
2301 mutex_unlock(&regulator_list_mutex);
2302}
2303EXPORT_SYMBOL_GPL(regulator_unregister);
2304
2305/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002306 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002307 * @state: system suspend state
2308 *
2309 * Configure each regulator with it's suspend operating parameters for state.
2310 * This will usually be called by machine suspend code prior to supending.
2311 */
2312int regulator_suspend_prepare(suspend_state_t state)
2313{
2314 struct regulator_dev *rdev;
2315 int ret = 0;
2316
2317 /* ON is handled by regulator active state */
2318 if (state == PM_SUSPEND_ON)
2319 return -EINVAL;
2320
2321 mutex_lock(&regulator_list_mutex);
2322 list_for_each_entry(rdev, &regulator_list, list) {
2323
2324 mutex_lock(&rdev->mutex);
2325 ret = suspend_prepare(rdev, state);
2326 mutex_unlock(&rdev->mutex);
2327
2328 if (ret < 0) {
2329 printk(KERN_ERR "%s: failed to prepare %s\n",
2330 __func__, rdev->desc->name);
2331 goto out;
2332 }
2333 }
2334out:
2335 mutex_unlock(&regulator_list_mutex);
2336 return ret;
2337}
2338EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2339
2340/**
Mark Brownca725562009-03-16 19:36:34 +00002341 * regulator_has_full_constraints - the system has fully specified constraints
2342 *
2343 * Calling this function will cause the regulator API to disable all
2344 * regulators which have a zero use count and don't have an always_on
2345 * constraint in a late_initcall.
2346 *
2347 * The intention is that this will become the default behaviour in a
2348 * future kernel release so users are encouraged to use this facility
2349 * now.
2350 */
2351void regulator_has_full_constraints(void)
2352{
2353 has_full_constraints = 1;
2354}
2355EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2356
2357/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002358 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002359 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002360 *
2361 * Get rdev regulator driver private data. This call can be used in the
2362 * regulator driver context.
2363 */
2364void *rdev_get_drvdata(struct regulator_dev *rdev)
2365{
2366 return rdev->reg_data;
2367}
2368EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2369
2370/**
2371 * regulator_get_drvdata - get regulator driver data
2372 * @regulator: regulator
2373 *
2374 * Get regulator driver private data. This call can be used in the consumer
2375 * driver context when non API regulator specific functions need to be called.
2376 */
2377void *regulator_get_drvdata(struct regulator *regulator)
2378{
2379 return regulator->rdev->reg_data;
2380}
2381EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2382
2383/**
2384 * regulator_set_drvdata - set regulator driver data
2385 * @regulator: regulator
2386 * @data: data
2387 */
2388void regulator_set_drvdata(struct regulator *regulator, void *data)
2389{
2390 regulator->rdev->reg_data = data;
2391}
2392EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2393
2394/**
2395 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002396 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002397 */
2398int rdev_get_id(struct regulator_dev *rdev)
2399{
2400 return rdev->desc->id;
2401}
2402EXPORT_SYMBOL_GPL(rdev_get_id);
2403
Liam Girdwooda5766f12008-10-10 13:22:20 +01002404struct device *rdev_get_dev(struct regulator_dev *rdev)
2405{
2406 return &rdev->dev;
2407}
2408EXPORT_SYMBOL_GPL(rdev_get_dev);
2409
2410void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2411{
2412 return reg_init_data->driver_data;
2413}
2414EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2415
Liam Girdwood414c70c2008-04-30 15:59:04 +01002416static int __init regulator_init(void)
2417{
2418 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2419 return class_register(&regulator_class);
2420}
2421
2422/* init early to allow our consumers to complete system booting */
2423core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002424
2425static int __init regulator_init_complete(void)
2426{
2427 struct regulator_dev *rdev;
2428 struct regulator_ops *ops;
2429 struct regulation_constraints *c;
2430 int enabled, ret;
2431 const char *name;
2432
2433 mutex_lock(&regulator_list_mutex);
2434
2435 /* If we have a full configuration then disable any regulators
2436 * which are not in use or always_on. This will become the
2437 * default behaviour in the future.
2438 */
2439 list_for_each_entry(rdev, &regulator_list, list) {
2440 ops = rdev->desc->ops;
2441 c = rdev->constraints;
2442
Mark Brownf25e0b42009-08-03 18:49:55 +01002443 if (c && c->name)
Mark Brownca725562009-03-16 19:36:34 +00002444 name = c->name;
2445 else if (rdev->desc->name)
2446 name = rdev->desc->name;
2447 else
2448 name = "regulator";
2449
Mark Brownf25e0b42009-08-03 18:49:55 +01002450 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002451 continue;
2452
2453 mutex_lock(&rdev->mutex);
2454
2455 if (rdev->use_count)
2456 goto unlock;
2457
2458 /* If we can't read the status assume it's on. */
2459 if (ops->is_enabled)
2460 enabled = ops->is_enabled(rdev);
2461 else
2462 enabled = 1;
2463
2464 if (!enabled)
2465 goto unlock;
2466
2467 if (has_full_constraints) {
2468 /* We log since this may kill the system if it
2469 * goes wrong. */
2470 printk(KERN_INFO "%s: disabling %s\n",
2471 __func__, name);
2472 ret = ops->disable(rdev);
2473 if (ret != 0) {
2474 printk(KERN_ERR
2475 "%s: couldn't disable %s: %d\n",
2476 __func__, name, ret);
2477 }
2478 } else {
2479 /* The intention is that in future we will
2480 * assume that full constraints are provided
2481 * so warn even if we aren't going to do
2482 * anything here.
2483 */
2484 printk(KERN_WARNING
2485 "%s: incomplete constraints, leaving %s on\n",
2486 __func__, name);
2487 }
2488
2489unlock:
2490 mutex_unlock(&rdev->mutex);
2491 }
2492
2493 mutex_unlock(&regulator_list_mutex);
2494
2495 return 0;
2496}
2497late_initcall(regulator_init_complete);