blob: 60fcd986ff3f06de92e3054e0b4c2a541ff35ea9 [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
Liam Girdwooda5766f12008-10-10 13:22:20 +0100675/**
676 * set_machine_constraints - sets regulator constraints
Mark Brown69279fb2008-12-31 12:52:41 +0000677 * @rdev: regulator source
Mark Brownc8e7e462008-12-31 12:52:42 +0000678 * @constraints: constraints to apply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100679 *
680 * Allows platform initialisation code to define and constrain
681 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
682 * Constraints *must* be set by platform code in order for some
683 * regulator operations to proceed i.e. set_voltage, set_current_limit,
684 * set_mode.
685 */
686static int set_machine_constraints(struct regulator_dev *rdev,
687 struct regulation_constraints *constraints)
688{
689 int ret = 0;
Mark Browne06f5b42008-09-09 16:21:19 +0100690 const char *name;
Mark Browne5fda262008-09-09 16:21:20 +0100691 struct regulator_ops *ops = rdev->desc->ops;
Mark Browne06f5b42008-09-09 16:21:19 +0100692
693 if (constraints->name)
694 name = constraints->name;
695 else if (rdev->desc->name)
696 name = rdev->desc->name;
697 else
698 name = "regulator";
Liam Girdwooda5766f12008-10-10 13:22:20 +0100699
David Brownell4367cfd2009-02-26 11:48:36 -0800700 /* constrain machine-level voltage specs to fit
701 * the actual range supported by this regulator.
702 */
703 if (ops->list_voltage && rdev->desc->n_voltages) {
704 int count = rdev->desc->n_voltages;
705 int i;
706 int min_uV = INT_MAX;
707 int max_uV = INT_MIN;
708 int cmin = constraints->min_uV;
709 int cmax = constraints->max_uV;
710
Mark Brown3e590912009-04-28 11:09:38 +0100711 /* it's safe to autoconfigure fixed-voltage supplies
712 and the constraints are used by list_voltage. */
David Brownell4367cfd2009-02-26 11:48:36 -0800713 if (count == 1 && !cmin) {
Mark Brown3e590912009-04-28 11:09:38 +0100714 cmin = 1;
David Brownell4367cfd2009-02-26 11:48:36 -0800715 cmax = INT_MAX;
Mark Brown3e590912009-04-28 11:09:38 +0100716 constraints->min_uV = cmin;
717 constraints->max_uV = cmax;
David Brownell4367cfd2009-02-26 11:48:36 -0800718 }
719
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000720 /* voltage constraints are optional */
721 if ((cmin == 0) && (cmax == 0))
722 goto out;
723
David Brownell4367cfd2009-02-26 11:48:36 -0800724 /* else require explicit machine-level constraints */
Mark Brown3e2b9ab2009-03-10 16:28:36 +0000725 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
David Brownell4367cfd2009-02-26 11:48:36 -0800726 pr_err("%s: %s '%s' voltage constraints\n",
727 __func__, "invalid", name);
728 ret = -EINVAL;
729 goto out;
730 }
731
732 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
733 for (i = 0; i < count; i++) {
734 int value;
735
736 value = ops->list_voltage(rdev, i);
737 if (value <= 0)
738 continue;
739
740 /* maybe adjust [min_uV..max_uV] */
741 if (value >= cmin && value < min_uV)
742 min_uV = value;
743 if (value <= cmax && value > max_uV)
744 max_uV = value;
745 }
746
747 /* final: [min_uV..max_uV] valid iff constraints valid */
748 if (max_uV < min_uV) {
749 pr_err("%s: %s '%s' voltage constraints\n",
750 __func__, "unsupportable", name);
751 ret = -EINVAL;
752 goto out;
753 }
754
755 /* use regulator's subset of machine constraints */
756 if (constraints->min_uV < min_uV) {
757 pr_debug("%s: override '%s' %s, %d -> %d\n",
758 __func__, name, "min_uV",
759 constraints->min_uV, min_uV);
760 constraints->min_uV = min_uV;
761 }
762 if (constraints->max_uV > max_uV) {
763 pr_debug("%s: override '%s' %s, %d -> %d\n",
764 __func__, name, "max_uV",
765 constraints->max_uV, max_uV);
766 constraints->max_uV = max_uV;
767 }
768 }
769
Liam Girdwooda5766f12008-10-10 13:22:20 +0100770 rdev->constraints = constraints;
771
772 /* do we need to apply the constraint voltage */
773 if (rdev->constraints->apply_uV &&
774 rdev->constraints->min_uV == rdev->constraints->max_uV &&
Mark Browne5fda262008-09-09 16:21:20 +0100775 ops->set_voltage) {
776 ret = ops->set_voltage(rdev,
Liam Girdwooda5766f12008-10-10 13:22:20 +0100777 rdev->constraints->min_uV, rdev->constraints->max_uV);
778 if (ret < 0) {
Mark Browne06f5b42008-09-09 16:21:19 +0100779 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
780 __func__,
781 rdev->constraints->min_uV, name);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100782 rdev->constraints = NULL;
783 goto out;
784 }
785 }
786
Liam Girdwooda5766f12008-10-10 13:22:20 +0100787 /* do we need to setup our suspend state */
Mark Browne06f5b42008-09-09 16:21:19 +0100788 if (constraints->initial_state) {
Liam Girdwooda5766f12008-10-10 13:22:20 +0100789 ret = suspend_prepare(rdev, constraints->initial_state);
Mark Browne06f5b42008-09-09 16:21:19 +0100790 if (ret < 0) {
791 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
792 __func__, name);
793 rdev->constraints = NULL;
794 goto out;
795 }
796 }
Liam Girdwooda5766f12008-10-10 13:22:20 +0100797
Mark Browna3084662009-02-26 19:24:19 +0000798 if (constraints->initial_mode) {
799 if (!ops->set_mode) {
800 printk(KERN_ERR "%s: no set_mode operation for %s\n",
801 __func__, name);
802 ret = -EINVAL;
803 goto out;
804 }
805
806 ret = ops->set_mode(rdev, constraints->initial_mode);
807 if (ret < 0) {
808 printk(KERN_ERR
809 "%s: failed to set initial mode for %s: %d\n",
810 __func__, name, ret);
811 goto out;
812 }
813 }
814
Mark Browncacf90f2009-03-02 16:32:46 +0000815 /* If the constraints say the regulator should be on at this point
816 * and we have control then make sure it is enabled.
817 */
818 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
Mark Browne5fda262008-09-09 16:21:20 +0100819 ret = ops->enable(rdev);
820 if (ret < 0) {
821 printk(KERN_ERR "%s: failed to enable %s\n",
822 __func__, name);
823 rdev->constraints = NULL;
824 goto out;
825 }
826 }
827
Liam Girdwooda5766f12008-10-10 13:22:20 +0100828 print_constraints(rdev);
829out:
830 return ret;
831}
832
833/**
834 * set_supply - set regulator supply regulator
Mark Brown69279fb2008-12-31 12:52:41 +0000835 * @rdev: regulator name
836 * @supply_rdev: supply regulator name
Liam Girdwooda5766f12008-10-10 13:22:20 +0100837 *
838 * Called by platform initialisation code to set the supply regulator for this
839 * regulator. This ensures that a regulators supply will also be enabled by the
840 * core if it's child is enabled.
841 */
842static int set_supply(struct regulator_dev *rdev,
843 struct regulator_dev *supply_rdev)
844{
845 int err;
846
847 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
848 "supply");
849 if (err) {
850 printk(KERN_ERR
851 "%s: could not add device link %s err %d\n",
852 __func__, supply_rdev->dev.kobj.name, err);
853 goto out;
854 }
855 rdev->supply = supply_rdev;
856 list_add(&rdev->slist, &supply_rdev->supply_list);
857out:
858 return err;
859}
860
861/**
862 * set_consumer_device_supply: Bind a regulator to a symbolic supply
Mark Brown69279fb2008-12-31 12:52:41 +0000863 * @rdev: regulator source
864 * @consumer_dev: device the supply applies to
Mark Brown40f92442009-06-17 17:56:39 +0100865 * @consumer_dev_name: dev_name() string for device supply applies to
Mark Brown69279fb2008-12-31 12:52:41 +0000866 * @supply: symbolic name for supply
Liam Girdwooda5766f12008-10-10 13:22:20 +0100867 *
868 * Allows platform initialisation code to map physical regulator
869 * sources to symbolic names for supplies for use by devices. Devices
870 * should use these symbolic names to request regulators, avoiding the
871 * need to provide board-specific regulator names as platform data.
Mark Brown40f92442009-06-17 17:56:39 +0100872 *
873 * Only one of consumer_dev and consumer_dev_name may be specified.
Liam Girdwooda5766f12008-10-10 13:22:20 +0100874 */
875static int set_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100876 struct device *consumer_dev, const char *consumer_dev_name,
877 const char *supply)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100878{
879 struct regulator_map *node;
Mark Brown9ed20992009-07-21 16:00:26 +0100880 int has_dev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100881
Mark Brown40f92442009-06-17 17:56:39 +0100882 if (consumer_dev && consumer_dev_name)
883 return -EINVAL;
884
885 if (!consumer_dev_name && consumer_dev)
886 consumer_dev_name = dev_name(consumer_dev);
887
Liam Girdwooda5766f12008-10-10 13:22:20 +0100888 if (supply == NULL)
889 return -EINVAL;
890
Mark Brown9ed20992009-07-21 16:00:26 +0100891 if (consumer_dev_name != NULL)
892 has_dev = 1;
893 else
894 has_dev = 0;
895
David Brownell6001e132008-12-31 12:54:19 +0000896 list_for_each_entry(node, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100897 if (consumer_dev_name != node->dev_name)
David Brownell6001e132008-12-31 12:54:19 +0000898 continue;
899 if (strcmp(node->supply, supply) != 0)
900 continue;
901
902 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
903 dev_name(&node->regulator->dev),
904 node->regulator->desc->name,
905 supply,
906 dev_name(&rdev->dev), rdev->desc->name);
907 return -EBUSY;
908 }
909
Mark Brown9ed20992009-07-21 16:00:26 +0100910 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
Liam Girdwooda5766f12008-10-10 13:22:20 +0100911 if (node == NULL)
912 return -ENOMEM;
913
914 node->regulator = rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100915 node->supply = supply;
916
Mark Brown9ed20992009-07-21 16:00:26 +0100917 if (has_dev) {
918 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
919 if (node->dev_name == NULL) {
920 kfree(node);
921 return -ENOMEM;
922 }
Mark Brown40f92442009-06-17 17:56:39 +0100923 }
924
Liam Girdwooda5766f12008-10-10 13:22:20 +0100925 list_add(&node->list, &regulator_map_list);
926 return 0;
927}
928
929static void unset_consumer_device_supply(struct regulator_dev *rdev,
Mark Brown40f92442009-06-17 17:56:39 +0100930 const char *consumer_dev_name, struct device *consumer_dev)
Liam Girdwooda5766f12008-10-10 13:22:20 +0100931{
932 struct regulator_map *node, *n;
933
Mark Brown40f92442009-06-17 17:56:39 +0100934 if (consumer_dev && !consumer_dev_name)
935 consumer_dev_name = dev_name(consumer_dev);
936
Liam Girdwooda5766f12008-10-10 13:22:20 +0100937 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +0100938 if (rdev != node->regulator)
939 continue;
940
941 if (consumer_dev_name && node->dev_name &&
942 strcmp(consumer_dev_name, node->dev_name))
943 continue;
944
945 list_del(&node->list);
946 kfree(node->dev_name);
947 kfree(node);
948 return;
Liam Girdwooda5766f12008-10-10 13:22:20 +0100949 }
950}
951
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200952static void unset_regulator_supplies(struct regulator_dev *rdev)
953{
954 struct regulator_map *node, *n;
955
956 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
957 if (rdev == node->regulator) {
958 list_del(&node->list);
Mark Brown40f92442009-06-17 17:56:39 +0100959 kfree(node->dev_name);
Mike Rapoport0f1d7472009-01-22 16:00:29 +0200960 kfree(node);
961 return;
962 }
963 }
964}
965
Liam Girdwood414c70c2008-04-30 15:59:04 +0100966#define REG_STR_SIZE 32
967
968static struct regulator *create_regulator(struct regulator_dev *rdev,
969 struct device *dev,
970 const char *supply_name)
971{
972 struct regulator *regulator;
973 char buf[REG_STR_SIZE];
974 int err, size;
975
976 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
977 if (regulator == NULL)
978 return NULL;
979
980 mutex_lock(&rdev->mutex);
981 regulator->rdev = rdev;
982 list_add(&regulator->list, &rdev->consumer_list);
983
984 if (dev) {
985 /* create a 'requested_microamps_name' sysfs entry */
986 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
987 supply_name);
988 if (size >= REG_STR_SIZE)
989 goto overflow_err;
990
991 regulator->dev = dev;
992 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
993 if (regulator->dev_attr.attr.name == NULL)
994 goto attr_name_err;
995
996 regulator->dev_attr.attr.owner = THIS_MODULE;
997 regulator->dev_attr.attr.mode = 0444;
998 regulator->dev_attr.show = device_requested_uA_show;
999 err = device_create_file(dev, &regulator->dev_attr);
1000 if (err < 0) {
1001 printk(KERN_WARNING "%s: could not add regulator_dev"
1002 " load sysfs\n", __func__);
1003 goto attr_name_err;
1004 }
1005
1006 /* also add a link to the device sysfs entry */
1007 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1008 dev->kobj.name, supply_name);
1009 if (size >= REG_STR_SIZE)
1010 goto attr_err;
1011
1012 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1013 if (regulator->supply_name == NULL)
1014 goto attr_err;
1015
1016 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1017 buf);
1018 if (err) {
1019 printk(KERN_WARNING
1020 "%s: could not add device link %s err %d\n",
1021 __func__, dev->kobj.name, err);
1022 device_remove_file(dev, &regulator->dev_attr);
1023 goto link_name_err;
1024 }
1025 }
1026 mutex_unlock(&rdev->mutex);
1027 return regulator;
1028link_name_err:
1029 kfree(regulator->supply_name);
1030attr_err:
1031 device_remove_file(regulator->dev, &regulator->dev_attr);
1032attr_name_err:
1033 kfree(regulator->dev_attr.attr.name);
1034overflow_err:
1035 list_del(&regulator->list);
1036 kfree(regulator);
1037 mutex_unlock(&rdev->mutex);
1038 return NULL;
1039}
1040
Mark Brown5ffbd132009-07-21 16:00:23 +01001041/* Internal regulator request function */
1042static struct regulator *_regulator_get(struct device *dev, const char *id,
1043 int exclusive)
Liam Girdwood414c70c2008-04-30 15:59:04 +01001044{
1045 struct regulator_dev *rdev;
1046 struct regulator_map *map;
1047 struct regulator *regulator = ERR_PTR(-ENODEV);
Mark Brown40f92442009-06-17 17:56:39 +01001048 const char *devname = NULL;
Mark Brown5ffbd132009-07-21 16:00:23 +01001049 int ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001050
1051 if (id == NULL) {
1052 printk(KERN_ERR "regulator: get() with no identifier\n");
1053 return regulator;
1054 }
1055
Mark Brown40f92442009-06-17 17:56:39 +01001056 if (dev)
1057 devname = dev_name(dev);
1058
Liam Girdwood414c70c2008-04-30 15:59:04 +01001059 mutex_lock(&regulator_list_mutex);
1060
1061 list_for_each_entry(map, &regulator_map_list, list) {
Mark Brown40f92442009-06-17 17:56:39 +01001062 /* If the mapping has a device set up it must match */
1063 if (map->dev_name &&
1064 (!devname || strcmp(map->dev_name, devname)))
1065 continue;
1066
1067 if (strcmp(map->supply, id) == 0) {
Liam Girdwooda5766f12008-10-10 13:22:20 +01001068 rdev = map->regulator;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001069 goto found;
Liam Girdwooda5766f12008-10-10 13:22:20 +01001070 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001071 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001072 mutex_unlock(&regulator_list_mutex);
1073 return regulator;
1074
1075found:
Mark Brown5ffbd132009-07-21 16:00:23 +01001076 if (rdev->exclusive) {
1077 regulator = ERR_PTR(-EPERM);
1078 goto out;
1079 }
1080
1081 if (exclusive && rdev->open_count) {
1082 regulator = ERR_PTR(-EBUSY);
1083 goto out;
1084 }
1085
Liam Girdwooda5766f12008-10-10 13:22:20 +01001086 if (!try_module_get(rdev->owner))
1087 goto out;
1088
Liam Girdwood414c70c2008-04-30 15:59:04 +01001089 regulator = create_regulator(rdev, dev, id);
1090 if (regulator == NULL) {
1091 regulator = ERR_PTR(-ENOMEM);
1092 module_put(rdev->owner);
1093 }
1094
Mark Brown5ffbd132009-07-21 16:00:23 +01001095 rdev->open_count++;
1096 if (exclusive) {
1097 rdev->exclusive = 1;
1098
1099 ret = _regulator_is_enabled(rdev);
1100 if (ret > 0)
1101 rdev->use_count = 1;
1102 else
1103 rdev->use_count = 0;
1104 }
1105
Liam Girdwooda5766f12008-10-10 13:22:20 +01001106out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01001107 mutex_unlock(&regulator_list_mutex);
Mark Brown5ffbd132009-07-21 16:00:23 +01001108
Liam Girdwood414c70c2008-04-30 15:59:04 +01001109 return regulator;
1110}
Mark Brown5ffbd132009-07-21 16:00:23 +01001111
1112/**
1113 * regulator_get - lookup and obtain a reference to a regulator.
1114 * @dev: device for regulator "consumer"
1115 * @id: Supply name or regulator ID.
1116 *
1117 * Returns a struct regulator corresponding to the regulator producer,
1118 * or IS_ERR() condition containing errno.
1119 *
1120 * Use of supply names configured via regulator_set_device_supply() is
1121 * strongly encouraged. It is recommended that the supply name used
1122 * should match the name used for the supply and/or the relevant
1123 * device pins in the datasheet.
1124 */
1125struct regulator *regulator_get(struct device *dev, const char *id)
1126{
1127 return _regulator_get(dev, id, 0);
1128}
Liam Girdwood414c70c2008-04-30 15:59:04 +01001129EXPORT_SYMBOL_GPL(regulator_get);
1130
1131/**
Mark Brown5ffbd132009-07-21 16:00:23 +01001132 * regulator_get_exclusive - obtain exclusive access to a regulator.
1133 * @dev: device for regulator "consumer"
1134 * @id: Supply name or regulator ID.
1135 *
1136 * Returns a struct regulator corresponding to the regulator producer,
1137 * or IS_ERR() condition containing errno. Other consumers will be
1138 * unable to obtain this reference is held and the use count for the
1139 * regulator will be initialised to reflect the current state of the
1140 * regulator.
1141 *
1142 * This is intended for use by consumers which cannot tolerate shared
1143 * use of the regulator such as those which need to force the
1144 * regulator off for correct operation of the hardware they are
1145 * controlling.
1146 *
1147 * Use of supply names configured via regulator_set_device_supply() is
1148 * strongly encouraged. It is recommended that the supply name used
1149 * should match the name used for the supply and/or the relevant
1150 * device pins in the datasheet.
1151 */
1152struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1153{
1154 return _regulator_get(dev, id, 1);
1155}
1156EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1157
1158/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001159 * regulator_put - "free" the regulator source
1160 * @regulator: regulator source
1161 *
1162 * Note: drivers must ensure that all regulator_enable calls made on this
1163 * regulator source are balanced by regulator_disable calls prior to calling
1164 * this function.
1165 */
1166void regulator_put(struct regulator *regulator)
1167{
1168 struct regulator_dev *rdev;
1169
1170 if (regulator == NULL || IS_ERR(regulator))
1171 return;
1172
Liam Girdwood414c70c2008-04-30 15:59:04 +01001173 mutex_lock(&regulator_list_mutex);
1174 rdev = regulator->rdev;
1175
1176 /* remove any sysfs entries */
1177 if (regulator->dev) {
1178 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1179 kfree(regulator->supply_name);
1180 device_remove_file(regulator->dev, &regulator->dev_attr);
1181 kfree(regulator->dev_attr.attr.name);
1182 }
1183 list_del(&regulator->list);
1184 kfree(regulator);
1185
Mark Brown5ffbd132009-07-21 16:00:23 +01001186 rdev->open_count--;
1187 rdev->exclusive = 0;
1188
Liam Girdwood414c70c2008-04-30 15:59:04 +01001189 module_put(rdev->owner);
1190 mutex_unlock(&regulator_list_mutex);
1191}
1192EXPORT_SYMBOL_GPL(regulator_put);
1193
1194/* locks held by regulator_enable() */
1195static int _regulator_enable(struct regulator_dev *rdev)
1196{
1197 int ret = -EINVAL;
1198
1199 if (!rdev->constraints) {
1200 printk(KERN_ERR "%s: %s has no constraints\n",
1201 __func__, rdev->desc->name);
1202 return ret;
1203 }
1204
1205 /* do we need to enable the supply regulator first */
1206 if (rdev->supply) {
1207 ret = _regulator_enable(rdev->supply);
1208 if (ret < 0) {
1209 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1210 __func__, rdev->desc->name, ret);
1211 return ret;
1212 }
1213 }
1214
1215 /* check voltage and requested load before enabling */
1216 if (rdev->desc->ops->enable) {
1217
1218 if (rdev->constraints &&
1219 (rdev->constraints->valid_ops_mask &
1220 REGULATOR_CHANGE_DRMS))
1221 drms_uA_update(rdev);
1222
1223 ret = rdev->desc->ops->enable(rdev);
1224 if (ret < 0) {
1225 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1226 __func__, rdev->desc->name, ret);
1227 return ret;
1228 }
1229 rdev->use_count++;
1230 return ret;
1231 }
1232
1233 return ret;
1234}
1235
1236/**
1237 * regulator_enable - enable regulator output
1238 * @regulator: regulator source
1239 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001240 * Request that the regulator be enabled with the regulator output at
1241 * the predefined voltage or current value. Calls to regulator_enable()
1242 * must be balanced with calls to regulator_disable().
1243 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001244 * NOTE: the output value can be set by other drivers, boot loader or may be
Mark Browncf7bbcd2008-12-31 12:52:43 +00001245 * hardwired in the regulator.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001246 */
1247int regulator_enable(struct regulator *regulator)
1248{
David Brownell412aec62008-11-16 11:44:46 -08001249 struct regulator_dev *rdev = regulator->rdev;
1250 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001251
David Brownell412aec62008-11-16 11:44:46 -08001252 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001253 ret = _regulator_enable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001254 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001255 return ret;
1256}
1257EXPORT_SYMBOL_GPL(regulator_enable);
1258
1259/* locks held by regulator_disable() */
1260static int _regulator_disable(struct regulator_dev *rdev)
1261{
1262 int ret = 0;
1263
David Brownellcd94b502009-03-11 16:43:34 -08001264 if (WARN(rdev->use_count <= 0,
1265 "unbalanced disables for %s\n",
1266 rdev->desc->name))
1267 return -EIO;
1268
Liam Girdwood414c70c2008-04-30 15:59:04 +01001269 /* are we the last user and permitted to disable ? */
1270 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1271
1272 /* we are last user */
1273 if (rdev->desc->ops->disable) {
1274 ret = rdev->desc->ops->disable(rdev);
1275 if (ret < 0) {
1276 printk(KERN_ERR "%s: failed to disable %s\n",
1277 __func__, rdev->desc->name);
1278 return ret;
1279 }
1280 }
1281
1282 /* decrease our supplies ref count and disable if required */
1283 if (rdev->supply)
1284 _regulator_disable(rdev->supply);
1285
1286 rdev->use_count = 0;
1287 } else if (rdev->use_count > 1) {
1288
1289 if (rdev->constraints &&
1290 (rdev->constraints->valid_ops_mask &
1291 REGULATOR_CHANGE_DRMS))
1292 drms_uA_update(rdev);
1293
1294 rdev->use_count--;
1295 }
1296 return ret;
1297}
1298
1299/**
1300 * regulator_disable - disable regulator output
1301 * @regulator: regulator source
1302 *
Mark Browncf7bbcd2008-12-31 12:52:43 +00001303 * Disable the regulator output voltage or current. Calls to
1304 * regulator_enable() must be balanced with calls to
1305 * regulator_disable().
Mark Brown69279fb2008-12-31 12:52:41 +00001306 *
Liam Girdwood414c70c2008-04-30 15:59:04 +01001307 * NOTE: this will only disable the regulator output if no other consumer
Mark Browncf7bbcd2008-12-31 12:52:43 +00001308 * devices have it enabled, the regulator device supports disabling and
1309 * machine constraints permit this operation.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001310 */
1311int regulator_disable(struct regulator *regulator)
1312{
David Brownell412aec62008-11-16 11:44:46 -08001313 struct regulator_dev *rdev = regulator->rdev;
1314 int ret = 0;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001315
David Brownell412aec62008-11-16 11:44:46 -08001316 mutex_lock(&rdev->mutex);
David Brownellcd94b502009-03-11 16:43:34 -08001317 ret = _regulator_disable(rdev);
David Brownell412aec62008-11-16 11:44:46 -08001318 mutex_unlock(&rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001319 return ret;
1320}
1321EXPORT_SYMBOL_GPL(regulator_disable);
1322
1323/* locks held by regulator_force_disable() */
1324static int _regulator_force_disable(struct regulator_dev *rdev)
1325{
1326 int ret = 0;
1327
1328 /* force disable */
1329 if (rdev->desc->ops->disable) {
1330 /* ah well, who wants to live forever... */
1331 ret = rdev->desc->ops->disable(rdev);
1332 if (ret < 0) {
1333 printk(KERN_ERR "%s: failed to force disable %s\n",
1334 __func__, rdev->desc->name);
1335 return ret;
1336 }
1337 /* notify other consumers that power has been forced off */
1338 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1339 NULL);
1340 }
1341
1342 /* decrease our supplies ref count and disable if required */
1343 if (rdev->supply)
1344 _regulator_disable(rdev->supply);
1345
1346 rdev->use_count = 0;
1347 return ret;
1348}
1349
1350/**
1351 * regulator_force_disable - force disable regulator output
1352 * @regulator: regulator source
1353 *
1354 * Forcibly disable the regulator output voltage or current.
1355 * NOTE: this *will* disable the regulator output even if other consumer
1356 * devices have it enabled. This should be used for situations when device
1357 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1358 */
1359int regulator_force_disable(struct regulator *regulator)
1360{
1361 int ret;
1362
1363 mutex_lock(&regulator->rdev->mutex);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001364 regulator->uA_load = 0;
1365 ret = _regulator_force_disable(regulator->rdev);
1366 mutex_unlock(&regulator->rdev->mutex);
1367 return ret;
1368}
1369EXPORT_SYMBOL_GPL(regulator_force_disable);
1370
1371static int _regulator_is_enabled(struct regulator_dev *rdev)
1372{
Liam Girdwood414c70c2008-04-30 15:59:04 +01001373 /* sanity check */
Mark Brown93325462009-08-03 18:49:56 +01001374 if (!rdev->desc->ops->is_enabled)
1375 return -EINVAL;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001376
Mark Brown93325462009-08-03 18:49:56 +01001377 return rdev->desc->ops->is_enabled(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001378}
1379
1380/**
1381 * regulator_is_enabled - is the regulator output enabled
1382 * @regulator: regulator source
1383 *
David Brownell412aec62008-11-16 11:44:46 -08001384 * Returns positive if the regulator driver backing the source/client
1385 * has requested that the device be enabled, zero if it hasn't, else a
1386 * negative errno code.
1387 *
1388 * Note that the device backing this regulator handle can have multiple
1389 * users, so it might be enabled even if regulator_enable() was never
1390 * called for this particular source.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001391 */
1392int regulator_is_enabled(struct regulator *regulator)
1393{
Mark Brown93325462009-08-03 18:49:56 +01001394 int ret;
1395
1396 mutex_lock(&regulator->rdev->mutex);
1397 ret = _regulator_is_enabled(regulator->rdev);
1398 mutex_unlock(&regulator->rdev->mutex);
1399
1400 return ret;
Liam Girdwood414c70c2008-04-30 15:59:04 +01001401}
1402EXPORT_SYMBOL_GPL(regulator_is_enabled);
1403
1404/**
David Brownell4367cfd2009-02-26 11:48:36 -08001405 * regulator_count_voltages - count regulator_list_voltage() selectors
1406 * @regulator: regulator source
1407 *
1408 * Returns number of selectors, or negative errno. Selectors are
1409 * numbered starting at zero, and typically correspond to bitfields
1410 * in hardware registers.
1411 */
1412int regulator_count_voltages(struct regulator *regulator)
1413{
1414 struct regulator_dev *rdev = regulator->rdev;
1415
1416 return rdev->desc->n_voltages ? : -EINVAL;
1417}
1418EXPORT_SYMBOL_GPL(regulator_count_voltages);
1419
1420/**
1421 * regulator_list_voltage - enumerate supported voltages
1422 * @regulator: regulator source
1423 * @selector: identify voltage to list
1424 * Context: can sleep
1425 *
1426 * Returns a voltage that can be passed to @regulator_set_voltage(),
1427 * zero if this selector code can't be used on this sytem, or a
1428 * negative errno.
1429 */
1430int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1431{
1432 struct regulator_dev *rdev = regulator->rdev;
1433 struct regulator_ops *ops = rdev->desc->ops;
1434 int ret;
1435
1436 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1437 return -EINVAL;
1438
1439 mutex_lock(&rdev->mutex);
1440 ret = ops->list_voltage(rdev, selector);
1441 mutex_unlock(&rdev->mutex);
1442
1443 if (ret > 0) {
1444 if (ret < rdev->constraints->min_uV)
1445 ret = 0;
1446 else if (ret > rdev->constraints->max_uV)
1447 ret = 0;
1448 }
1449
1450 return ret;
1451}
1452EXPORT_SYMBOL_GPL(regulator_list_voltage);
1453
1454/**
Mark Browna7a1ad92009-07-21 16:00:24 +01001455 * regulator_is_supported_voltage - check if a voltage range can be supported
1456 *
1457 * @regulator: Regulator to check.
1458 * @min_uV: Minimum required voltage in uV.
1459 * @max_uV: Maximum required voltage in uV.
1460 *
1461 * Returns a boolean or a negative error code.
1462 */
1463int regulator_is_supported_voltage(struct regulator *regulator,
1464 int min_uV, int max_uV)
1465{
1466 int i, voltages, ret;
1467
1468 ret = regulator_count_voltages(regulator);
1469 if (ret < 0)
1470 return ret;
1471 voltages = ret;
1472
1473 for (i = 0; i < voltages; i++) {
1474 ret = regulator_list_voltage(regulator, i);
1475
1476 if (ret >= min_uV && ret <= max_uV)
1477 return 1;
1478 }
1479
1480 return 0;
1481}
1482
1483/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01001484 * regulator_set_voltage - set regulator output voltage
1485 * @regulator: regulator source
1486 * @min_uV: Minimum required voltage in uV
1487 * @max_uV: Maximum acceptable voltage in uV
1488 *
1489 * Sets a voltage regulator to the desired output voltage. This can be set
1490 * during any regulator state. IOW, regulator can be disabled or enabled.
1491 *
1492 * If the regulator is enabled then the voltage will change to the new value
1493 * immediately otherwise if the regulator is disabled the regulator will
1494 * output at the new voltage when enabled.
1495 *
1496 * NOTE: If the regulator is shared between several devices then the lowest
1497 * request voltage that meets the system constraints will be used.
Mark Brown69279fb2008-12-31 12:52:41 +00001498 * Regulator system constraints must be set for this regulator before
Liam Girdwood414c70c2008-04-30 15:59:04 +01001499 * calling this function otherwise this call will fail.
1500 */
1501int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1502{
1503 struct regulator_dev *rdev = regulator->rdev;
1504 int ret;
1505
1506 mutex_lock(&rdev->mutex);
1507
1508 /* sanity check */
1509 if (!rdev->desc->ops->set_voltage) {
1510 ret = -EINVAL;
1511 goto out;
1512 }
1513
1514 /* constraints check */
1515 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1516 if (ret < 0)
1517 goto out;
1518 regulator->min_uV = min_uV;
1519 regulator->max_uV = max_uV;
1520 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1521
1522out:
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001523 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001524 mutex_unlock(&rdev->mutex);
1525 return ret;
1526}
1527EXPORT_SYMBOL_GPL(regulator_set_voltage);
1528
1529static int _regulator_get_voltage(struct regulator_dev *rdev)
1530{
1531 /* sanity check */
1532 if (rdev->desc->ops->get_voltage)
1533 return rdev->desc->ops->get_voltage(rdev);
1534 else
1535 return -EINVAL;
1536}
1537
1538/**
1539 * regulator_get_voltage - get regulator output voltage
1540 * @regulator: regulator source
1541 *
1542 * This returns the current regulator voltage in uV.
1543 *
1544 * NOTE: If the regulator is disabled it will return the voltage value. This
1545 * function should not be used to determine regulator state.
1546 */
1547int regulator_get_voltage(struct regulator *regulator)
1548{
1549 int ret;
1550
1551 mutex_lock(&regulator->rdev->mutex);
1552
1553 ret = _regulator_get_voltage(regulator->rdev);
1554
1555 mutex_unlock(&regulator->rdev->mutex);
1556
1557 return ret;
1558}
1559EXPORT_SYMBOL_GPL(regulator_get_voltage);
1560
1561/**
1562 * regulator_set_current_limit - set regulator output current limit
1563 * @regulator: regulator source
1564 * @min_uA: Minimuum supported current in uA
1565 * @max_uA: Maximum supported current in uA
1566 *
1567 * Sets current sink to the desired output current. This can be set during
1568 * any regulator state. IOW, regulator can be disabled or enabled.
1569 *
1570 * If the regulator is enabled then the current will change to the new value
1571 * immediately otherwise if the regulator is disabled the regulator will
1572 * output at the new current when enabled.
1573 *
1574 * NOTE: Regulator system constraints must be set for this regulator before
1575 * calling this function otherwise this call will fail.
1576 */
1577int regulator_set_current_limit(struct regulator *regulator,
1578 int min_uA, int max_uA)
1579{
1580 struct regulator_dev *rdev = regulator->rdev;
1581 int ret;
1582
1583 mutex_lock(&rdev->mutex);
1584
1585 /* sanity check */
1586 if (!rdev->desc->ops->set_current_limit) {
1587 ret = -EINVAL;
1588 goto out;
1589 }
1590
1591 /* constraints check */
1592 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1593 if (ret < 0)
1594 goto out;
1595
1596 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1597out:
1598 mutex_unlock(&rdev->mutex);
1599 return ret;
1600}
1601EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1602
1603static int _regulator_get_current_limit(struct regulator_dev *rdev)
1604{
1605 int ret;
1606
1607 mutex_lock(&rdev->mutex);
1608
1609 /* sanity check */
1610 if (!rdev->desc->ops->get_current_limit) {
1611 ret = -EINVAL;
1612 goto out;
1613 }
1614
1615 ret = rdev->desc->ops->get_current_limit(rdev);
1616out:
1617 mutex_unlock(&rdev->mutex);
1618 return ret;
1619}
1620
1621/**
1622 * regulator_get_current_limit - get regulator output current
1623 * @regulator: regulator source
1624 *
1625 * This returns the current supplied by the specified current sink in uA.
1626 *
1627 * NOTE: If the regulator is disabled it will return the current value. This
1628 * function should not be used to determine regulator state.
1629 */
1630int regulator_get_current_limit(struct regulator *regulator)
1631{
1632 return _regulator_get_current_limit(regulator->rdev);
1633}
1634EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1635
1636/**
1637 * regulator_set_mode - set regulator operating mode
1638 * @regulator: regulator source
1639 * @mode: operating mode - one of the REGULATOR_MODE constants
1640 *
1641 * Set regulator operating mode to increase regulator efficiency or improve
1642 * regulation performance.
1643 *
1644 * NOTE: Regulator system constraints must be set for this regulator before
1645 * calling this function otherwise this call will fail.
1646 */
1647int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1648{
1649 struct regulator_dev *rdev = regulator->rdev;
1650 int ret;
1651
1652 mutex_lock(&rdev->mutex);
1653
1654 /* sanity check */
1655 if (!rdev->desc->ops->set_mode) {
1656 ret = -EINVAL;
1657 goto out;
1658 }
1659
1660 /* constraints check */
1661 ret = regulator_check_mode(rdev, mode);
1662 if (ret < 0)
1663 goto out;
1664
1665 ret = rdev->desc->ops->set_mode(rdev, mode);
1666out:
1667 mutex_unlock(&rdev->mutex);
1668 return ret;
1669}
1670EXPORT_SYMBOL_GPL(regulator_set_mode);
1671
1672static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1673{
1674 int ret;
1675
1676 mutex_lock(&rdev->mutex);
1677
1678 /* sanity check */
1679 if (!rdev->desc->ops->get_mode) {
1680 ret = -EINVAL;
1681 goto out;
1682 }
1683
1684 ret = rdev->desc->ops->get_mode(rdev);
1685out:
1686 mutex_unlock(&rdev->mutex);
1687 return ret;
1688}
1689
1690/**
1691 * regulator_get_mode - get regulator operating mode
1692 * @regulator: regulator source
1693 *
1694 * Get the current regulator operating mode.
1695 */
1696unsigned int regulator_get_mode(struct regulator *regulator)
1697{
1698 return _regulator_get_mode(regulator->rdev);
1699}
1700EXPORT_SYMBOL_GPL(regulator_get_mode);
1701
1702/**
1703 * regulator_set_optimum_mode - set regulator optimum operating mode
1704 * @regulator: regulator source
1705 * @uA_load: load current
1706 *
1707 * Notifies the regulator core of a new device load. This is then used by
1708 * DRMS (if enabled by constraints) to set the most efficient regulator
1709 * operating mode for the new regulator loading.
1710 *
1711 * Consumer devices notify their supply regulator of the maximum power
1712 * they will require (can be taken from device datasheet in the power
1713 * consumption tables) when they change operational status and hence power
1714 * state. Examples of operational state changes that can affect power
1715 * consumption are :-
1716 *
1717 * o Device is opened / closed.
1718 * o Device I/O is about to begin or has just finished.
1719 * o Device is idling in between work.
1720 *
1721 * This information is also exported via sysfs to userspace.
1722 *
1723 * DRMS will sum the total requested load on the regulator and change
1724 * to the most efficient operating mode if platform constraints allow.
1725 *
1726 * Returns the new regulator mode or error.
1727 */
1728int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1729{
1730 struct regulator_dev *rdev = regulator->rdev;
1731 struct regulator *consumer;
1732 int ret, output_uV, input_uV, total_uA_load = 0;
1733 unsigned int mode;
1734
1735 mutex_lock(&rdev->mutex);
1736
1737 regulator->uA_load = uA_load;
1738 ret = regulator_check_drms(rdev);
1739 if (ret < 0)
1740 goto out;
1741 ret = -EINVAL;
1742
1743 /* sanity check */
1744 if (!rdev->desc->ops->get_optimum_mode)
1745 goto out;
1746
1747 /* get output voltage */
1748 output_uV = rdev->desc->ops->get_voltage(rdev);
1749 if (output_uV <= 0) {
1750 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1751 __func__, rdev->desc->name);
1752 goto out;
1753 }
1754
1755 /* get input voltage */
1756 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1757 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1758 else
1759 input_uV = rdev->constraints->input_uV;
1760 if (input_uV <= 0) {
1761 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1762 __func__, rdev->desc->name);
1763 goto out;
1764 }
1765
1766 /* calc total requested load for this regulator */
1767 list_for_each_entry(consumer, &rdev->consumer_list, list)
1768 total_uA_load += consumer->uA_load;
1769
1770 mode = rdev->desc->ops->get_optimum_mode(rdev,
1771 input_uV, output_uV,
1772 total_uA_load);
David Brownelle5735202008-11-16 11:46:56 -08001773 ret = regulator_check_mode(rdev, mode);
1774 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001775 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1776 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1777 total_uA_load, input_uV, output_uV);
1778 goto out;
1779 }
1780
1781 ret = rdev->desc->ops->set_mode(rdev, mode);
David Brownelle5735202008-11-16 11:46:56 -08001782 if (ret < 0) {
Liam Girdwood414c70c2008-04-30 15:59:04 +01001783 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1784 __func__, mode, rdev->desc->name);
1785 goto out;
1786 }
1787 ret = mode;
1788out:
1789 mutex_unlock(&rdev->mutex);
1790 return ret;
1791}
1792EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1793
1794/**
1795 * regulator_register_notifier - register regulator event notifier
1796 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001797 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001798 *
1799 * Register notifier block to receive regulator events.
1800 */
1801int regulator_register_notifier(struct regulator *regulator,
1802 struct notifier_block *nb)
1803{
1804 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1805 nb);
1806}
1807EXPORT_SYMBOL_GPL(regulator_register_notifier);
1808
1809/**
1810 * regulator_unregister_notifier - unregister regulator event notifier
1811 * @regulator: regulator source
Mark Brown69279fb2008-12-31 12:52:41 +00001812 * @nb: notifier block
Liam Girdwood414c70c2008-04-30 15:59:04 +01001813 *
1814 * Unregister regulator event notifier block.
1815 */
1816int regulator_unregister_notifier(struct regulator *regulator,
1817 struct notifier_block *nb)
1818{
1819 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1820 nb);
1821}
1822EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1823
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001824/* notify regulator consumers and downstream regulator consumers.
1825 * Note mutex must be held by caller.
1826 */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001827static void _notifier_call_chain(struct regulator_dev *rdev,
1828 unsigned long event, void *data)
1829{
1830 struct regulator_dev *_rdev;
1831
1832 /* call rdev chain first */
Liam Girdwood414c70c2008-04-30 15:59:04 +01001833 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
Liam Girdwood414c70c2008-04-30 15:59:04 +01001834
1835 /* now notify regulator we supply */
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001836 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
1837 mutex_lock(&_rdev->mutex);
1838 _notifier_call_chain(_rdev, event, data);
1839 mutex_unlock(&_rdev->mutex);
1840 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01001841}
1842
1843/**
1844 * regulator_bulk_get - get multiple regulator consumers
1845 *
1846 * @dev: Device to supply
1847 * @num_consumers: Number of consumers to register
1848 * @consumers: Configuration of consumers; clients are stored here.
1849 *
1850 * @return 0 on success, an errno on failure.
1851 *
1852 * This helper function allows drivers to get several regulator
1853 * consumers in one operation. If any of the regulators cannot be
1854 * acquired then any regulators that were allocated will be freed
1855 * before returning to the caller.
1856 */
1857int regulator_bulk_get(struct device *dev, int num_consumers,
1858 struct regulator_bulk_data *consumers)
1859{
1860 int i;
1861 int ret;
1862
1863 for (i = 0; i < num_consumers; i++)
1864 consumers[i].consumer = NULL;
1865
1866 for (i = 0; i < num_consumers; i++) {
1867 consumers[i].consumer = regulator_get(dev,
1868 consumers[i].supply);
1869 if (IS_ERR(consumers[i].consumer)) {
1870 dev_err(dev, "Failed to get supply '%s'\n",
1871 consumers[i].supply);
1872 ret = PTR_ERR(consumers[i].consumer);
1873 consumers[i].consumer = NULL;
1874 goto err;
1875 }
1876 }
1877
1878 return 0;
1879
1880err:
1881 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1882 regulator_put(consumers[i].consumer);
1883
1884 return ret;
1885}
1886EXPORT_SYMBOL_GPL(regulator_bulk_get);
1887
1888/**
1889 * regulator_bulk_enable - enable multiple regulator consumers
1890 *
1891 * @num_consumers: Number of consumers
1892 * @consumers: Consumer data; clients are stored here.
1893 * @return 0 on success, an errno on failure
1894 *
1895 * This convenience API allows consumers to enable multiple regulator
1896 * clients in a single API call. If any consumers cannot be enabled
1897 * then any others that were enabled will be disabled again prior to
1898 * return.
1899 */
1900int regulator_bulk_enable(int num_consumers,
1901 struct regulator_bulk_data *consumers)
1902{
1903 int i;
1904 int ret;
1905
1906 for (i = 0; i < num_consumers; i++) {
1907 ret = regulator_enable(consumers[i].consumer);
1908 if (ret != 0)
1909 goto err;
1910 }
1911
1912 return 0;
1913
1914err:
1915 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1916 for (i = 0; i < num_consumers; i++)
1917 regulator_disable(consumers[i].consumer);
1918
1919 return ret;
1920}
1921EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1922
1923/**
1924 * regulator_bulk_disable - disable multiple regulator consumers
1925 *
1926 * @num_consumers: Number of consumers
1927 * @consumers: Consumer data; clients are stored here.
1928 * @return 0 on success, an errno on failure
1929 *
1930 * This convenience API allows consumers to disable multiple regulator
1931 * clients in a single API call. If any consumers cannot be enabled
1932 * then any others that were disabled will be disabled again prior to
1933 * return.
1934 */
1935int regulator_bulk_disable(int num_consumers,
1936 struct regulator_bulk_data *consumers)
1937{
1938 int i;
1939 int ret;
1940
1941 for (i = 0; i < num_consumers; i++) {
1942 ret = regulator_disable(consumers[i].consumer);
1943 if (ret != 0)
1944 goto err;
1945 }
1946
1947 return 0;
1948
1949err:
1950 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1951 for (i = 0; i < num_consumers; i++)
1952 regulator_enable(consumers[i].consumer);
1953
1954 return ret;
1955}
1956EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1957
1958/**
1959 * regulator_bulk_free - free multiple regulator consumers
1960 *
1961 * @num_consumers: Number of consumers
1962 * @consumers: Consumer data; clients are stored here.
1963 *
1964 * This convenience API allows consumers to free multiple regulator
1965 * clients in a single API call.
1966 */
1967void regulator_bulk_free(int num_consumers,
1968 struct regulator_bulk_data *consumers)
1969{
1970 int i;
1971
1972 for (i = 0; i < num_consumers; i++) {
1973 regulator_put(consumers[i].consumer);
1974 consumers[i].consumer = NULL;
1975 }
1976}
1977EXPORT_SYMBOL_GPL(regulator_bulk_free);
1978
1979/**
1980 * regulator_notifier_call_chain - call regulator event notifier
Mark Brown69279fb2008-12-31 12:52:41 +00001981 * @rdev: regulator source
Liam Girdwood414c70c2008-04-30 15:59:04 +01001982 * @event: notifier block
Mark Brown69279fb2008-12-31 12:52:41 +00001983 * @data: callback-specific data.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001984 *
1985 * Called by regulator drivers to notify clients a regulator event has
1986 * occurred. We also notify regulator clients downstream.
Jonathan Cameronb136fb42009-01-19 18:20:58 +00001987 * Note lock must be held by caller.
Liam Girdwood414c70c2008-04-30 15:59:04 +01001988 */
1989int regulator_notifier_call_chain(struct regulator_dev *rdev,
1990 unsigned long event, void *data)
1991{
1992 _notifier_call_chain(rdev, event, data);
1993 return NOTIFY_DONE;
1994
1995}
1996EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1997
Mark Brownbe721972009-08-04 20:09:52 +02001998/**
1999 * regulator_mode_to_status - convert a regulator mode into a status
2000 *
2001 * @mode: Mode to convert
2002 *
2003 * Convert a regulator mode into a status.
2004 */
2005int regulator_mode_to_status(unsigned int mode)
2006{
2007 switch (mode) {
2008 case REGULATOR_MODE_FAST:
2009 return REGULATOR_STATUS_FAST;
2010 case REGULATOR_MODE_NORMAL:
2011 return REGULATOR_STATUS_NORMAL;
2012 case REGULATOR_MODE_IDLE:
2013 return REGULATOR_STATUS_IDLE;
2014 case REGULATOR_STATUS_STANDBY:
2015 return REGULATOR_STATUS_STANDBY;
2016 default:
2017 return 0;
2018 }
2019}
2020EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2021
David Brownell7ad68e22008-11-11 17:39:02 -08002022/*
2023 * To avoid cluttering sysfs (and memory) with useless state, only
2024 * create attributes that can be meaningfully displayed.
2025 */
2026static int add_regulator_attributes(struct regulator_dev *rdev)
2027{
2028 struct device *dev = &rdev->dev;
2029 struct regulator_ops *ops = rdev->desc->ops;
2030 int status = 0;
2031
2032 /* some attributes need specific methods to be displayed */
2033 if (ops->get_voltage) {
2034 status = device_create_file(dev, &dev_attr_microvolts);
2035 if (status < 0)
2036 return status;
2037 }
2038 if (ops->get_current_limit) {
2039 status = device_create_file(dev, &dev_attr_microamps);
2040 if (status < 0)
2041 return status;
2042 }
2043 if (ops->get_mode) {
2044 status = device_create_file(dev, &dev_attr_opmode);
2045 if (status < 0)
2046 return status;
2047 }
2048 if (ops->is_enabled) {
2049 status = device_create_file(dev, &dev_attr_state);
2050 if (status < 0)
2051 return status;
2052 }
David Brownell853116a2009-01-14 23:03:17 -08002053 if (ops->get_status) {
2054 status = device_create_file(dev, &dev_attr_status);
2055 if (status < 0)
2056 return status;
2057 }
David Brownell7ad68e22008-11-11 17:39:02 -08002058
2059 /* some attributes are type-specific */
2060 if (rdev->desc->type == REGULATOR_CURRENT) {
2061 status = device_create_file(dev, &dev_attr_requested_microamps);
2062 if (status < 0)
2063 return status;
2064 }
2065
2066 /* all the other attributes exist to support constraints;
2067 * don't show them if there are no constraints, or if the
2068 * relevant supporting methods are missing.
2069 */
2070 if (!rdev->constraints)
2071 return status;
2072
2073 /* constraints need specific supporting methods */
2074 if (ops->set_voltage) {
2075 status = device_create_file(dev, &dev_attr_min_microvolts);
2076 if (status < 0)
2077 return status;
2078 status = device_create_file(dev, &dev_attr_max_microvolts);
2079 if (status < 0)
2080 return status;
2081 }
2082 if (ops->set_current_limit) {
2083 status = device_create_file(dev, &dev_attr_min_microamps);
2084 if (status < 0)
2085 return status;
2086 status = device_create_file(dev, &dev_attr_max_microamps);
2087 if (status < 0)
2088 return status;
2089 }
2090
2091 /* suspend mode constraints need multiple supporting methods */
2092 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2093 return status;
2094
2095 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2096 if (status < 0)
2097 return status;
2098 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2099 if (status < 0)
2100 return status;
2101 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2102 if (status < 0)
2103 return status;
2104
2105 if (ops->set_suspend_voltage) {
2106 status = device_create_file(dev,
2107 &dev_attr_suspend_standby_microvolts);
2108 if (status < 0)
2109 return status;
2110 status = device_create_file(dev,
2111 &dev_attr_suspend_mem_microvolts);
2112 if (status < 0)
2113 return status;
2114 status = device_create_file(dev,
2115 &dev_attr_suspend_disk_microvolts);
2116 if (status < 0)
2117 return status;
2118 }
2119
2120 if (ops->set_suspend_mode) {
2121 status = device_create_file(dev,
2122 &dev_attr_suspend_standby_mode);
2123 if (status < 0)
2124 return status;
2125 status = device_create_file(dev,
2126 &dev_attr_suspend_mem_mode);
2127 if (status < 0)
2128 return status;
2129 status = device_create_file(dev,
2130 &dev_attr_suspend_disk_mode);
2131 if (status < 0)
2132 return status;
2133 }
2134
2135 return status;
2136}
2137
Liam Girdwood414c70c2008-04-30 15:59:04 +01002138/**
2139 * regulator_register - register regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002140 * @regulator_desc: regulator to register
2141 * @dev: struct device for the regulator
Mark Brown05271002009-01-19 13:37:02 +00002142 * @init_data: platform provided init data, passed through by driver
Mark Brown69279fb2008-12-31 12:52:41 +00002143 * @driver_data: private regulator data
Liam Girdwood414c70c2008-04-30 15:59:04 +01002144 *
2145 * Called by regulator drivers to register a regulator.
2146 * Returns 0 on success.
2147 */
2148struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
Mark Brown05271002009-01-19 13:37:02 +00002149 struct device *dev, struct regulator_init_data *init_data,
2150 void *driver_data)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002151{
2152 static atomic_t regulator_no = ATOMIC_INIT(0);
2153 struct regulator_dev *rdev;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002154 int ret, i;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002155
2156 if (regulator_desc == NULL)
2157 return ERR_PTR(-EINVAL);
2158
2159 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2160 return ERR_PTR(-EINVAL);
2161
Diego Lizierocd78dfc2009-04-14 03:04:47 +02002162 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2163 regulator_desc->type != REGULATOR_CURRENT)
Liam Girdwood414c70c2008-04-30 15:59:04 +01002164 return ERR_PTR(-EINVAL);
2165
Mark Brown46fabe1e2008-09-09 16:21:18 +01002166 if (!init_data)
2167 return ERR_PTR(-EINVAL);
2168
Liam Girdwood414c70c2008-04-30 15:59:04 +01002169 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2170 if (rdev == NULL)
2171 return ERR_PTR(-ENOMEM);
2172
2173 mutex_lock(&regulator_list_mutex);
2174
2175 mutex_init(&rdev->mutex);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002176 rdev->reg_data = driver_data;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002177 rdev->owner = regulator_desc->owner;
2178 rdev->desc = regulator_desc;
2179 INIT_LIST_HEAD(&rdev->consumer_list);
2180 INIT_LIST_HEAD(&rdev->supply_list);
2181 INIT_LIST_HEAD(&rdev->list);
2182 INIT_LIST_HEAD(&rdev->slist);
2183 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2184
Liam Girdwooda5766f12008-10-10 13:22:20 +01002185 /* preform any regulator specific init */
2186 if (init_data->regulator_init) {
2187 ret = init_data->regulator_init(rdev->reg_data);
David Brownell4fca9542008-11-11 17:38:53 -08002188 if (ret < 0)
2189 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002190 }
Liam Girdwood414c70c2008-04-30 15:59:04 +01002191
Liam Girdwooda5766f12008-10-10 13:22:20 +01002192 /* register with sysfs */
2193 rdev->dev.class = &regulator_class;
2194 rdev->dev.parent = dev;
Kay Sievers812460a2008-11-02 03:55:10 +01002195 dev_set_name(&rdev->dev, "regulator.%d",
2196 atomic_inc_return(&regulator_no) - 1);
Liam Girdwooda5766f12008-10-10 13:22:20 +01002197 ret = device_register(&rdev->dev);
David Brownell4fca9542008-11-11 17:38:53 -08002198 if (ret != 0)
2199 goto clean;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002200
2201 dev_set_drvdata(&rdev->dev, rdev);
2202
Mike Rapoport74f544c2008-11-25 14:53:53 +02002203 /* set regulator constraints */
2204 ret = set_machine_constraints(rdev, &init_data->constraints);
2205 if (ret < 0)
2206 goto scrub;
2207
David Brownell7ad68e22008-11-11 17:39:02 -08002208 /* add attributes supported by this regulator */
2209 ret = add_regulator_attributes(rdev);
2210 if (ret < 0)
2211 goto scrub;
2212
Liam Girdwooda5766f12008-10-10 13:22:20 +01002213 /* set supply regulator if it exists */
2214 if (init_data->supply_regulator_dev) {
2215 ret = set_supply(rdev,
2216 dev_get_drvdata(init_data->supply_regulator_dev));
David Brownell4fca9542008-11-11 17:38:53 -08002217 if (ret < 0)
2218 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002219 }
2220
2221 /* add consumers devices */
2222 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2223 ret = set_consumer_device_supply(rdev,
2224 init_data->consumer_supplies[i].dev,
Mark Brown40f92442009-06-17 17:56:39 +01002225 init_data->consumer_supplies[i].dev_name,
Liam Girdwooda5766f12008-10-10 13:22:20 +01002226 init_data->consumer_supplies[i].supply);
2227 if (ret < 0) {
2228 for (--i; i >= 0; i--)
2229 unset_consumer_device_supply(rdev,
Mark Brown40f92442009-06-17 17:56:39 +01002230 init_data->consumer_supplies[i].dev_name,
2231 init_data->consumer_supplies[i].dev);
David Brownell4fca9542008-11-11 17:38:53 -08002232 goto scrub;
Liam Girdwooda5766f12008-10-10 13:22:20 +01002233 }
2234 }
2235
2236 list_add(&rdev->list, &regulator_list);
2237out:
Liam Girdwood414c70c2008-04-30 15:59:04 +01002238 mutex_unlock(&regulator_list_mutex);
2239 return rdev;
David Brownell4fca9542008-11-11 17:38:53 -08002240
2241scrub:
2242 device_unregister(&rdev->dev);
Paul Walmsley53032da2009-04-25 05:28:36 -06002243 /* device core frees rdev */
2244 rdev = ERR_PTR(ret);
2245 goto out;
2246
David Brownell4fca9542008-11-11 17:38:53 -08002247clean:
2248 kfree(rdev);
2249 rdev = ERR_PTR(ret);
2250 goto out;
Liam Girdwood414c70c2008-04-30 15:59:04 +01002251}
2252EXPORT_SYMBOL_GPL(regulator_register);
2253
2254/**
2255 * regulator_unregister - unregister regulator
Mark Brown69279fb2008-12-31 12:52:41 +00002256 * @rdev: regulator to unregister
Liam Girdwood414c70c2008-04-30 15:59:04 +01002257 *
2258 * Called by regulator drivers to unregister a regulator.
2259 */
2260void regulator_unregister(struct regulator_dev *rdev)
2261{
2262 if (rdev == NULL)
2263 return;
2264
2265 mutex_lock(&regulator_list_mutex);
Mark Brown6bf87d12009-07-21 16:00:25 +01002266 WARN_ON(rdev->open_count);
Mike Rapoport0f1d7472009-01-22 16:00:29 +02002267 unset_regulator_supplies(rdev);
Liam Girdwood414c70c2008-04-30 15:59:04 +01002268 list_del(&rdev->list);
2269 if (rdev->supply)
2270 sysfs_remove_link(&rdev->dev.kobj, "supply");
2271 device_unregister(&rdev->dev);
2272 mutex_unlock(&regulator_list_mutex);
2273}
2274EXPORT_SYMBOL_GPL(regulator_unregister);
2275
2276/**
Mark Browncf7bbcd2008-12-31 12:52:43 +00002277 * regulator_suspend_prepare - prepare regulators for system wide suspend
Liam Girdwood414c70c2008-04-30 15:59:04 +01002278 * @state: system suspend state
2279 *
2280 * Configure each regulator with it's suspend operating parameters for state.
2281 * This will usually be called by machine suspend code prior to supending.
2282 */
2283int regulator_suspend_prepare(suspend_state_t state)
2284{
2285 struct regulator_dev *rdev;
2286 int ret = 0;
2287
2288 /* ON is handled by regulator active state */
2289 if (state == PM_SUSPEND_ON)
2290 return -EINVAL;
2291
2292 mutex_lock(&regulator_list_mutex);
2293 list_for_each_entry(rdev, &regulator_list, list) {
2294
2295 mutex_lock(&rdev->mutex);
2296 ret = suspend_prepare(rdev, state);
2297 mutex_unlock(&rdev->mutex);
2298
2299 if (ret < 0) {
2300 printk(KERN_ERR "%s: failed to prepare %s\n",
2301 __func__, rdev->desc->name);
2302 goto out;
2303 }
2304 }
2305out:
2306 mutex_unlock(&regulator_list_mutex);
2307 return ret;
2308}
2309EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2310
2311/**
Mark Brownca725562009-03-16 19:36:34 +00002312 * regulator_has_full_constraints - the system has fully specified constraints
2313 *
2314 * Calling this function will cause the regulator API to disable all
2315 * regulators which have a zero use count and don't have an always_on
2316 * constraint in a late_initcall.
2317 *
2318 * The intention is that this will become the default behaviour in a
2319 * future kernel release so users are encouraged to use this facility
2320 * now.
2321 */
2322void regulator_has_full_constraints(void)
2323{
2324 has_full_constraints = 1;
2325}
2326EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2327
2328/**
Liam Girdwood414c70c2008-04-30 15:59:04 +01002329 * rdev_get_drvdata - get rdev regulator driver data
Mark Brown69279fb2008-12-31 12:52:41 +00002330 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002331 *
2332 * Get rdev regulator driver private data. This call can be used in the
2333 * regulator driver context.
2334 */
2335void *rdev_get_drvdata(struct regulator_dev *rdev)
2336{
2337 return rdev->reg_data;
2338}
2339EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2340
2341/**
2342 * regulator_get_drvdata - get regulator driver data
2343 * @regulator: regulator
2344 *
2345 * Get regulator driver private data. This call can be used in the consumer
2346 * driver context when non API regulator specific functions need to be called.
2347 */
2348void *regulator_get_drvdata(struct regulator *regulator)
2349{
2350 return regulator->rdev->reg_data;
2351}
2352EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2353
2354/**
2355 * regulator_set_drvdata - set regulator driver data
2356 * @regulator: regulator
2357 * @data: data
2358 */
2359void regulator_set_drvdata(struct regulator *regulator, void *data)
2360{
2361 regulator->rdev->reg_data = data;
2362}
2363EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2364
2365/**
2366 * regulator_get_id - get regulator ID
Mark Brown69279fb2008-12-31 12:52:41 +00002367 * @rdev: regulator
Liam Girdwood414c70c2008-04-30 15:59:04 +01002368 */
2369int rdev_get_id(struct regulator_dev *rdev)
2370{
2371 return rdev->desc->id;
2372}
2373EXPORT_SYMBOL_GPL(rdev_get_id);
2374
Liam Girdwooda5766f12008-10-10 13:22:20 +01002375struct device *rdev_get_dev(struct regulator_dev *rdev)
2376{
2377 return &rdev->dev;
2378}
2379EXPORT_SYMBOL_GPL(rdev_get_dev);
2380
2381void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2382{
2383 return reg_init_data->driver_data;
2384}
2385EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2386
Liam Girdwood414c70c2008-04-30 15:59:04 +01002387static int __init regulator_init(void)
2388{
2389 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2390 return class_register(&regulator_class);
2391}
2392
2393/* init early to allow our consumers to complete system booting */
2394core_initcall(regulator_init);
Mark Brownca725562009-03-16 19:36:34 +00002395
2396static int __init regulator_init_complete(void)
2397{
2398 struct regulator_dev *rdev;
2399 struct regulator_ops *ops;
2400 struct regulation_constraints *c;
2401 int enabled, ret;
2402 const char *name;
2403
2404 mutex_lock(&regulator_list_mutex);
2405
2406 /* If we have a full configuration then disable any regulators
2407 * which are not in use or always_on. This will become the
2408 * default behaviour in the future.
2409 */
2410 list_for_each_entry(rdev, &regulator_list, list) {
2411 ops = rdev->desc->ops;
2412 c = rdev->constraints;
2413
Mark Brownf25e0b42009-08-03 18:49:55 +01002414 if (c && c->name)
Mark Brownca725562009-03-16 19:36:34 +00002415 name = c->name;
2416 else if (rdev->desc->name)
2417 name = rdev->desc->name;
2418 else
2419 name = "regulator";
2420
Mark Brownf25e0b42009-08-03 18:49:55 +01002421 if (!ops->disable || (c && c->always_on))
Mark Brownca725562009-03-16 19:36:34 +00002422 continue;
2423
2424 mutex_lock(&rdev->mutex);
2425
2426 if (rdev->use_count)
2427 goto unlock;
2428
2429 /* If we can't read the status assume it's on. */
2430 if (ops->is_enabled)
2431 enabled = ops->is_enabled(rdev);
2432 else
2433 enabled = 1;
2434
2435 if (!enabled)
2436 goto unlock;
2437
2438 if (has_full_constraints) {
2439 /* We log since this may kill the system if it
2440 * goes wrong. */
2441 printk(KERN_INFO "%s: disabling %s\n",
2442 __func__, name);
2443 ret = ops->disable(rdev);
2444 if (ret != 0) {
2445 printk(KERN_ERR
2446 "%s: couldn't disable %s: %d\n",
2447 __func__, name, ret);
2448 }
2449 } else {
2450 /* The intention is that in future we will
2451 * assume that full constraints are provided
2452 * so warn even if we aren't going to do
2453 * anything here.
2454 */
2455 printk(KERN_WARNING
2456 "%s: incomplete constraints, leaving %s on\n",
2457 __func__, name);
2458 }
2459
2460unlock:
2461 mutex_unlock(&rdev->mutex);
2462 }
2463
2464 mutex_unlock(&regulator_list_mutex);
2465
2466 return 0;
2467}
2468late_initcall(regulator_init_complete);