]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/regulator/core.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/bp/bp
[net-next-2.6.git] / drivers / regulator / core.c
CommitLineData
414c70cb
LG
1/*
2 * core.c -- Voltage/Current Regulator framework.
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
a5766f11 5 * Copyright 2008 SlimLogic Ltd.
414c70cb 6 *
a5766f11 7 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
414c70cb
LG
8 *
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);
ca725561 31static int has_full_constraints;
414c70cb 32
8dc5390d 33/*
414c70cb
LG
34 * struct regulator_map
35 *
36 * Used to provide symbolic supply names to devices.
37 */
38struct regulator_map {
39 struct list_head list;
40f9244f 40 const char *dev_name; /* The dev_name() for the consumer */
414c70cb 41 const char *supply;
a5766f11 42 struct regulator_dev *regulator;
414c70cb
LG
43};
44
414c70cb
LG
45/*
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;
414c70cb
LG
56 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
1083c393
MB
69static const char *rdev_get_name(struct regulator_dev *rdev)
70{
71 if (rdev->constraints && rdev->constraints->name)
72 return rdev->constraints->name;
73 else if (rdev->desc->name)
74 return rdev->desc->name;
75 else
76 return "";
77}
78
414c70cb
LG
79/* gets the regulator for a given consumer device */
80static struct regulator *get_device_regulator(struct device *dev)
81{
82 struct regulator *regulator = NULL;
83 struct regulator_dev *rdev;
84
85 mutex_lock(&regulator_list_mutex);
86 list_for_each_entry(rdev, &regulator_list, list) {
87 mutex_lock(&rdev->mutex);
88 list_for_each_entry(regulator, &rdev->consumer_list, list) {
89 if (regulator->dev == dev) {
90 mutex_unlock(&rdev->mutex);
91 mutex_unlock(&regulator_list_mutex);
92 return regulator;
93 }
94 }
95 mutex_unlock(&rdev->mutex);
96 }
97 mutex_unlock(&regulator_list_mutex);
98 return NULL;
99}
100
101/* Platform voltage constraint check */
102static int regulator_check_voltage(struct regulator_dev *rdev,
103 int *min_uV, int *max_uV)
104{
105 BUG_ON(*min_uV > *max_uV);
106
107 if (!rdev->constraints) {
108 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
1083c393 109 rdev_get_name(rdev));
414c70cb
LG
110 return -ENODEV;
111 }
112 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
113 printk(KERN_ERR "%s: operation not allowed for %s\n",
1083c393 114 __func__, rdev_get_name(rdev));
414c70cb
LG
115 return -EPERM;
116 }
117
118 if (*max_uV > rdev->constraints->max_uV)
119 *max_uV = rdev->constraints->max_uV;
120 if (*min_uV < rdev->constraints->min_uV)
121 *min_uV = rdev->constraints->min_uV;
122
123 if (*min_uV > *max_uV)
124 return -EINVAL;
125
126 return 0;
127}
128
129/* current constraint check */
130static int regulator_check_current_limit(struct regulator_dev *rdev,
131 int *min_uA, int *max_uA)
132{
133 BUG_ON(*min_uA > *max_uA);
134
135 if (!rdev->constraints) {
136 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
1083c393 137 rdev_get_name(rdev));
414c70cb
LG
138 return -ENODEV;
139 }
140 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
141 printk(KERN_ERR "%s: operation not allowed for %s\n",
1083c393 142 __func__, rdev_get_name(rdev));
414c70cb
LG
143 return -EPERM;
144 }
145
146 if (*max_uA > rdev->constraints->max_uA)
147 *max_uA = rdev->constraints->max_uA;
148 if (*min_uA < rdev->constraints->min_uA)
149 *min_uA = rdev->constraints->min_uA;
150
151 if (*min_uA > *max_uA)
152 return -EINVAL;
153
154 return 0;
155}
156
157/* operating mode constraint check */
158static int regulator_check_mode(struct regulator_dev *rdev, int mode)
159{
e573520b
DB
160 switch (mode) {
161 case REGULATOR_MODE_FAST:
162 case REGULATOR_MODE_NORMAL:
163 case REGULATOR_MODE_IDLE:
164 case REGULATOR_MODE_STANDBY:
165 break;
166 default:
167 return -EINVAL;
168 }
169
414c70cb
LG
170 if (!rdev->constraints) {
171 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
1083c393 172 rdev_get_name(rdev));
414c70cb
LG
173 return -ENODEV;
174 }
175 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
176 printk(KERN_ERR "%s: operation not allowed for %s\n",
1083c393 177 __func__, rdev_get_name(rdev));
414c70cb
LG
178 return -EPERM;
179 }
180 if (!(rdev->constraints->valid_modes_mask & mode)) {
181 printk(KERN_ERR "%s: invalid mode %x for %s\n",
1083c393 182 __func__, mode, rdev_get_name(rdev));
414c70cb
LG
183 return -EINVAL;
184 }
185 return 0;
186}
187
188/* dynamic regulator mode switching constraint check */
189static int regulator_check_drms(struct regulator_dev *rdev)
190{
191 if (!rdev->constraints) {
192 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
1083c393 193 rdev_get_name(rdev));
414c70cb
LG
194 return -ENODEV;
195 }
196 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
197 printk(KERN_ERR "%s: operation not allowed for %s\n",
1083c393 198 __func__, rdev_get_name(rdev));
414c70cb
LG
199 return -EPERM;
200 }
201 return 0;
202}
203
204static ssize_t device_requested_uA_show(struct device *dev,
205 struct device_attribute *attr, char *buf)
206{
207 struct regulator *regulator;
208
209 regulator = get_device_regulator(dev);
210 if (regulator == NULL)
211 return 0;
212
213 return sprintf(buf, "%d\n", regulator->uA_load);
214}
215
216static ssize_t regulator_uV_show(struct device *dev,
217 struct device_attribute *attr, char *buf)
218{
a5766f11 219 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
220 ssize_t ret;
221
222 mutex_lock(&rdev->mutex);
223 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
224 mutex_unlock(&rdev->mutex);
225
226 return ret;
227}
7ad68e2f 228static DEVICE_ATTR(microvolts, 0444, regulator_uV_show, NULL);
414c70cb
LG
229
230static ssize_t regulator_uA_show(struct device *dev,
231 struct device_attribute *attr, char *buf)
232{
a5766f11 233 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
234
235 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
236}
7ad68e2f 237static DEVICE_ATTR(microamps, 0444, regulator_uA_show, NULL);
414c70cb 238
bc558a60
MB
239static ssize_t regulator_name_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 struct regulator_dev *rdev = dev_get_drvdata(dev);
bc558a60 243
1083c393 244 return sprintf(buf, "%s\n", rdev_get_name(rdev));
bc558a60
MB
245}
246
4fca9545 247static ssize_t regulator_print_opmode(char *buf, int mode)
414c70cb 248{
414c70cb
LG
249 switch (mode) {
250 case REGULATOR_MODE_FAST:
251 return sprintf(buf, "fast\n");
252 case REGULATOR_MODE_NORMAL:
253 return sprintf(buf, "normal\n");
254 case REGULATOR_MODE_IDLE:
255 return sprintf(buf, "idle\n");
256 case REGULATOR_MODE_STANDBY:
257 return sprintf(buf, "standby\n");
258 }
259 return sprintf(buf, "unknown\n");
260}
261
4fca9545
DB
262static ssize_t regulator_opmode_show(struct device *dev,
263 struct device_attribute *attr, char *buf)
414c70cb 264{
a5766f11 265 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 266
4fca9545
DB
267 return regulator_print_opmode(buf, _regulator_get_mode(rdev));
268}
7ad68e2f 269static DEVICE_ATTR(opmode, 0444, regulator_opmode_show, NULL);
4fca9545
DB
270
271static ssize_t regulator_print_state(char *buf, int state)
272{
414c70cb
LG
273 if (state > 0)
274 return sprintf(buf, "enabled\n");
275 else if (state == 0)
276 return sprintf(buf, "disabled\n");
277 else
278 return sprintf(buf, "unknown\n");
279}
280
4fca9545
DB
281static ssize_t regulator_state_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 struct regulator_dev *rdev = dev_get_drvdata(dev);
9332546f
MB
285 ssize_t ret;
286
287 mutex_lock(&rdev->mutex);
288 ret = regulator_print_state(buf, _regulator_is_enabled(rdev));
289 mutex_unlock(&rdev->mutex);
4fca9545 290
9332546f 291 return ret;
4fca9545 292}
7ad68e2f 293static DEVICE_ATTR(state, 0444, regulator_state_show, NULL);
4fca9545 294
853116a1
DB
295static ssize_t regulator_status_show(struct device *dev,
296 struct device_attribute *attr, char *buf)
297{
298 struct regulator_dev *rdev = dev_get_drvdata(dev);
299 int status;
300 char *label;
301
302 status = rdev->desc->ops->get_status(rdev);
303 if (status < 0)
304 return status;
305
306 switch (status) {
307 case REGULATOR_STATUS_OFF:
308 label = "off";
309 break;
310 case REGULATOR_STATUS_ON:
311 label = "on";
312 break;
313 case REGULATOR_STATUS_ERROR:
314 label = "error";
315 break;
316 case REGULATOR_STATUS_FAST:
317 label = "fast";
318 break;
319 case REGULATOR_STATUS_NORMAL:
320 label = "normal";
321 break;
322 case REGULATOR_STATUS_IDLE:
323 label = "idle";
324 break;
325 case REGULATOR_STATUS_STANDBY:
326 label = "standby";
327 break;
328 default:
329 return -ERANGE;
330 }
331
332 return sprintf(buf, "%s\n", label);
333}
334static DEVICE_ATTR(status, 0444, regulator_status_show, NULL);
335
414c70cb
LG
336static ssize_t regulator_min_uA_show(struct device *dev,
337 struct device_attribute *attr, char *buf)
338{
a5766f11 339 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
340
341 if (!rdev->constraints)
342 return sprintf(buf, "constraint not defined\n");
343
344 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
345}
7ad68e2f 346static DEVICE_ATTR(min_microamps, 0444, regulator_min_uA_show, NULL);
414c70cb
LG
347
348static ssize_t regulator_max_uA_show(struct device *dev,
349 struct device_attribute *attr, char *buf)
350{
a5766f11 351 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
352
353 if (!rdev->constraints)
354 return sprintf(buf, "constraint not defined\n");
355
356 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
357}
7ad68e2f 358static DEVICE_ATTR(max_microamps, 0444, regulator_max_uA_show, NULL);
414c70cb
LG
359
360static ssize_t regulator_min_uV_show(struct device *dev,
361 struct device_attribute *attr, char *buf)
362{
a5766f11 363 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
364
365 if (!rdev->constraints)
366 return sprintf(buf, "constraint not defined\n");
367
368 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
369}
7ad68e2f 370static DEVICE_ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL);
414c70cb
LG
371
372static ssize_t regulator_max_uV_show(struct device *dev,
373 struct device_attribute *attr, char *buf)
374{
a5766f11 375 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
376
377 if (!rdev->constraints)
378 return sprintf(buf, "constraint not defined\n");
379
380 return sprintf(buf, "%d\n", rdev->constraints->max_uV);
381}
7ad68e2f 382static DEVICE_ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL);
414c70cb
LG
383
384static ssize_t regulator_total_uA_show(struct device *dev,
385 struct device_attribute *attr, char *buf)
386{
a5766f11 387 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
388 struct regulator *regulator;
389 int uA = 0;
390
391 mutex_lock(&rdev->mutex);
392 list_for_each_entry(regulator, &rdev->consumer_list, list)
fa2984d4 393 uA += regulator->uA_load;
414c70cb
LG
394 mutex_unlock(&rdev->mutex);
395 return sprintf(buf, "%d\n", uA);
396}
7ad68e2f 397static DEVICE_ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL);
414c70cb
LG
398
399static ssize_t regulator_num_users_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
401{
a5766f11 402 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
403 return sprintf(buf, "%d\n", rdev->use_count);
404}
405
406static ssize_t regulator_type_show(struct device *dev,
407 struct device_attribute *attr, char *buf)
408{
a5766f11 409 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
410
411 switch (rdev->desc->type) {
412 case REGULATOR_VOLTAGE:
413 return sprintf(buf, "voltage\n");
414 case REGULATOR_CURRENT:
415 return sprintf(buf, "current\n");
416 }
417 return sprintf(buf, "unknown\n");
418}
419
420static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
421 struct device_attribute *attr, char *buf)
422{
a5766f11 423 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 424
414c70cb
LG
425 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
426}
7ad68e2f
DB
427static DEVICE_ATTR(suspend_mem_microvolts, 0444,
428 regulator_suspend_mem_uV_show, NULL);
414c70cb
LG
429
430static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
431 struct device_attribute *attr, char *buf)
432{
a5766f11 433 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 434
414c70cb
LG
435 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
436}
7ad68e2f
DB
437static DEVICE_ATTR(suspend_disk_microvolts, 0444,
438 regulator_suspend_disk_uV_show, NULL);
414c70cb
LG
439
440static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
441 struct device_attribute *attr, char *buf)
442{
a5766f11 443 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 444
414c70cb
LG
445 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
446}
7ad68e2f
DB
447static DEVICE_ATTR(suspend_standby_microvolts, 0444,
448 regulator_suspend_standby_uV_show, NULL);
414c70cb 449
414c70cb
LG
450static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
a5766f11 453 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 454
4fca9545
DB
455 return regulator_print_opmode(buf,
456 rdev->constraints->state_mem.mode);
414c70cb 457}
7ad68e2f
DB
458static DEVICE_ATTR(suspend_mem_mode, 0444,
459 regulator_suspend_mem_mode_show, NULL);
414c70cb
LG
460
461static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
a5766f11 464 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 465
4fca9545
DB
466 return regulator_print_opmode(buf,
467 rdev->constraints->state_disk.mode);
414c70cb 468}
7ad68e2f
DB
469static DEVICE_ATTR(suspend_disk_mode, 0444,
470 regulator_suspend_disk_mode_show, NULL);
414c70cb
LG
471
472static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
473 struct device_attribute *attr, char *buf)
474{
a5766f11 475 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 476
4fca9545
DB
477 return regulator_print_opmode(buf,
478 rdev->constraints->state_standby.mode);
414c70cb 479}
7ad68e2f
DB
480static DEVICE_ATTR(suspend_standby_mode, 0444,
481 regulator_suspend_standby_mode_show, NULL);
414c70cb
LG
482
483static ssize_t regulator_suspend_mem_state_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
a5766f11 486 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 487
4fca9545
DB
488 return regulator_print_state(buf,
489 rdev->constraints->state_mem.enabled);
414c70cb 490}
7ad68e2f
DB
491static DEVICE_ATTR(suspend_mem_state, 0444,
492 regulator_suspend_mem_state_show, NULL);
414c70cb
LG
493
494static ssize_t regulator_suspend_disk_state_show(struct device *dev,
495 struct device_attribute *attr, char *buf)
496{
a5766f11 497 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 498
4fca9545
DB
499 return regulator_print_state(buf,
500 rdev->constraints->state_disk.enabled);
414c70cb 501}
7ad68e2f
DB
502static DEVICE_ATTR(suspend_disk_state, 0444,
503 regulator_suspend_disk_state_show, NULL);
414c70cb
LG
504
505static ssize_t regulator_suspend_standby_state_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507{
a5766f11 508 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb 509
4fca9545
DB
510 return regulator_print_state(buf,
511 rdev->constraints->state_standby.enabled);
414c70cb 512}
7ad68e2f
DB
513static DEVICE_ATTR(suspend_standby_state, 0444,
514 regulator_suspend_standby_state_show, NULL);
515
bc558a60 516
7ad68e2f
DB
517/*
518 * These are the only attributes are present for all regulators.
519 * Other attributes are a function of regulator functionality.
520 */
414c70cb 521static struct device_attribute regulator_dev_attrs[] = {
bc558a60 522 __ATTR(name, 0444, regulator_name_show, NULL),
414c70cb
LG
523 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
524 __ATTR(type, 0444, regulator_type_show, NULL),
414c70cb
LG
525 __ATTR_NULL,
526};
527
528static void regulator_dev_release(struct device *dev)
529{
a5766f11 530 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
531 kfree(rdev);
532}
533
534static struct class regulator_class = {
535 .name = "regulator",
536 .dev_release = regulator_dev_release,
537 .dev_attrs = regulator_dev_attrs,
538};
539
540/* Calculate the new optimum regulator operating mode based on the new total
541 * consumer load. All locks held by caller */
542static void drms_uA_update(struct regulator_dev *rdev)
543{
544 struct regulator *sibling;
545 int current_uA = 0, output_uV, input_uV, err;
546 unsigned int mode;
547
548 err = regulator_check_drms(rdev);
549 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
036de8ef
DC
550 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode)
551 return;
414c70cb
LG
552
553 /* get output voltage */
554 output_uV = rdev->desc->ops->get_voltage(rdev);
555 if (output_uV <= 0)
556 return;
557
558 /* get input voltage */
559 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
560 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
561 else
562 input_uV = rdev->constraints->input_uV;
563 if (input_uV <= 0)
564 return;
565
566 /* calc total requested load */
567 list_for_each_entry(sibling, &rdev->consumer_list, list)
fa2984d4 568 current_uA += sibling->uA_load;
414c70cb
LG
569
570 /* now get the optimum mode for our new total regulator load */
571 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
572 output_uV, current_uA);
573
574 /* check the new mode is allowed */
575 err = regulator_check_mode(rdev, mode);
576 if (err == 0)
577 rdev->desc->ops->set_mode(rdev, mode);
578}
579
580static int suspend_set_state(struct regulator_dev *rdev,
581 struct regulator_state *rstate)
582{
583 int ret = 0;
638f85c5
MB
584 bool can_set_state;
585
586 can_set_state = rdev->desc->ops->set_suspend_enable &&
587 rdev->desc->ops->set_suspend_disable;
588
589 /* If we have no suspend mode configration don't set anything;
590 * only warn if the driver actually makes the suspend mode
591 * configurable.
592 */
593 if (!rstate->enabled && !rstate->disabled) {
594 if (can_set_state)
595 printk(KERN_WARNING "%s: No configuration for %s\n",
596 __func__, rdev_get_name(rdev));
597 return 0;
598 }
599
600 if (rstate->enabled && rstate->disabled) {
601 printk(KERN_ERR "%s: invalid configuration for %s\n",
602 __func__, rdev_get_name(rdev));
603 return -EINVAL;
604 }
414c70cb 605
638f85c5 606 if (!can_set_state) {
a5766f11
LG
607 printk(KERN_ERR "%s: no way to set suspend state\n",
608 __func__);
414c70cb 609 return -EINVAL;
a5766f11 610 }
414c70cb
LG
611
612 if (rstate->enabled)
613 ret = rdev->desc->ops->set_suspend_enable(rdev);
614 else
615 ret = rdev->desc->ops->set_suspend_disable(rdev);
616 if (ret < 0) {
617 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
618 return ret;
619 }
620
621 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
622 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
623 if (ret < 0) {
624 printk(KERN_ERR "%s: failed to set voltage\n",
625 __func__);
626 return ret;
627 }
628 }
629
630 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
631 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
632 if (ret < 0) {
633 printk(KERN_ERR "%s: failed to set mode\n", __func__);
634 return ret;
635 }
636 }
637 return ret;
638}
639
640/* locks held by caller */
641static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
642{
643 if (!rdev->constraints)
644 return -EINVAL;
645
646 switch (state) {
647 case PM_SUSPEND_STANDBY:
648 return suspend_set_state(rdev,
649 &rdev->constraints->state_standby);
650 case PM_SUSPEND_MEM:
651 return suspend_set_state(rdev,
652 &rdev->constraints->state_mem);
653 case PM_SUSPEND_MAX:
654 return suspend_set_state(rdev,
655 &rdev->constraints->state_disk);
656 default:
657 return -EINVAL;
658 }
659}
660
661static void print_constraints(struct regulator_dev *rdev)
662{
663 struct regulation_constraints *constraints = rdev->constraints;
664 char buf[80];
8f031b48
MB
665 int count = 0;
666 int ret;
414c70cb 667
8f031b48 668 if (constraints->min_uV && constraints->max_uV) {
414c70cb 669 if (constraints->min_uV == constraints->max_uV)
8f031b48
MB
670 count += sprintf(buf + count, "%d mV ",
671 constraints->min_uV / 1000);
414c70cb 672 else
8f031b48
MB
673 count += sprintf(buf + count, "%d <--> %d mV ",
674 constraints->min_uV / 1000,
675 constraints->max_uV / 1000);
676 }
677
678 if (!constraints->min_uV ||
679 constraints->min_uV != constraints->max_uV) {
680 ret = _regulator_get_voltage(rdev);
681 if (ret > 0)
682 count += sprintf(buf + count, "at %d mV ", ret / 1000);
683 }
684
685 if (constraints->min_uA && constraints->max_uA) {
414c70cb 686 if (constraints->min_uA == constraints->max_uA)
8f031b48
MB
687 count += sprintf(buf + count, "%d mA ",
688 constraints->min_uA / 1000);
414c70cb 689 else
8f031b48
MB
690 count += sprintf(buf + count, "%d <--> %d mA ",
691 constraints->min_uA / 1000,
692 constraints->max_uA / 1000);
693 }
694
695 if (!constraints->min_uA ||
696 constraints->min_uA != constraints->max_uA) {
697 ret = _regulator_get_current_limit(rdev);
698 if (ret > 0)
699 count += sprintf(buf + count, "at %d uA ", ret / 1000);
414c70cb 700 }
8f031b48 701
414c70cb
LG
702 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
703 count += sprintf(buf + count, "fast ");
704 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
705 count += sprintf(buf + count, "normal ");
706 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
707 count += sprintf(buf + count, "idle ");
708 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
709 count += sprintf(buf + count, "standby");
710
1083c393 711 printk(KERN_INFO "regulator: %s: %s\n", rdev_get_name(rdev), buf);
414c70cb
LG
712}
713
e79055d6 714static int machine_constraints_voltage(struct regulator_dev *rdev,
1083c393 715 struct regulation_constraints *constraints)
a5766f11 716{
e5fda26c 717 struct regulator_ops *ops = rdev->desc->ops;
1083c393 718 const char *name = rdev_get_name(rdev);
af5866c9
MB
719 int ret;
720
721 /* do we need to apply the constraint voltage */
722 if (rdev->constraints->apply_uV &&
723 rdev->constraints->min_uV == rdev->constraints->max_uV &&
724 ops->set_voltage) {
725 ret = ops->set_voltage(rdev,
726 rdev->constraints->min_uV, rdev->constraints->max_uV);
727 if (ret < 0) {
728 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
729 __func__,
730 rdev->constraints->min_uV, name);
731 rdev->constraints = NULL;
732 return ret;
733 }
734 }
e06f5b4f 735
4367cfdc
DB
736 /* constrain machine-level voltage specs to fit
737 * the actual range supported by this regulator.
738 */
739 if (ops->list_voltage && rdev->desc->n_voltages) {
740 int count = rdev->desc->n_voltages;
741 int i;
742 int min_uV = INT_MAX;
743 int max_uV = INT_MIN;
744 int cmin = constraints->min_uV;
745 int cmax = constraints->max_uV;
746
3e590918
MB
747 /* it's safe to autoconfigure fixed-voltage supplies
748 and the constraints are used by list_voltage. */
4367cfdc 749 if (count == 1 && !cmin) {
3e590918 750 cmin = 1;
4367cfdc 751 cmax = INT_MAX;
3e590918
MB
752 constraints->min_uV = cmin;
753 constraints->max_uV = cmax;
4367cfdc
DB
754 }
755
3e2b9abd
MB
756 /* voltage constraints are optional */
757 if ((cmin == 0) && (cmax == 0))
e79055d6 758 return 0;
3e2b9abd 759
4367cfdc 760 /* else require explicit machine-level constraints */
3e2b9abd 761 if (cmin <= 0 || cmax <= 0 || cmax < cmin) {
4367cfdc
DB
762 pr_err("%s: %s '%s' voltage constraints\n",
763 __func__, "invalid", name);
e79055d6 764 return -EINVAL;
4367cfdc
DB
765 }
766
767 /* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
768 for (i = 0; i < count; i++) {
769 int value;
770
771 value = ops->list_voltage(rdev, i);
772 if (value <= 0)
773 continue;
774
775 /* maybe adjust [min_uV..max_uV] */
776 if (value >= cmin && value < min_uV)
777 min_uV = value;
778 if (value <= cmax && value > max_uV)
779 max_uV = value;
780 }
781
782 /* final: [min_uV..max_uV] valid iff constraints valid */
783 if (max_uV < min_uV) {
784 pr_err("%s: %s '%s' voltage constraints\n",
785 __func__, "unsupportable", name);
e79055d6 786 return -EINVAL;
4367cfdc
DB
787 }
788
789 /* use regulator's subset of machine constraints */
790 if (constraints->min_uV < min_uV) {
791 pr_debug("%s: override '%s' %s, %d -> %d\n",
792 __func__, name, "min_uV",
793 constraints->min_uV, min_uV);
794 constraints->min_uV = min_uV;
795 }
796 if (constraints->max_uV > max_uV) {
797 pr_debug("%s: override '%s' %s, %d -> %d\n",
798 __func__, name, "max_uV",
799 constraints->max_uV, max_uV);
800 constraints->max_uV = max_uV;
801 }
802 }
803
e79055d6
MB
804 return 0;
805}
806
807/**
808 * set_machine_constraints - sets regulator constraints
809 * @rdev: regulator source
810 * @constraints: constraints to apply
811 *
812 * Allows platform initialisation code to define and constrain
813 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
814 * Constraints *must* be set by platform code in order for some
815 * regulator operations to proceed i.e. set_voltage, set_current_limit,
816 * set_mode.
817 */
818static int set_machine_constraints(struct regulator_dev *rdev,
819 struct regulation_constraints *constraints)
820{
821 int ret = 0;
822 const char *name;
823 struct regulator_ops *ops = rdev->desc->ops;
824
af5866c9
MB
825 rdev->constraints = constraints;
826
1083c393
MB
827 name = rdev_get_name(rdev);
828
829 ret = machine_constraints_voltage(rdev, constraints);
e79055d6
MB
830 if (ret != 0)
831 goto out;
832
a5766f11 833 /* do we need to setup our suspend state */
e06f5b4f 834 if (constraints->initial_state) {
a5766f11 835 ret = suspend_prepare(rdev, constraints->initial_state);
e06f5b4f
MB
836 if (ret < 0) {
837 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
838 __func__, name);
839 rdev->constraints = NULL;
840 goto out;
841 }
842 }
a5766f11 843
a308466c
MB
844 if (constraints->initial_mode) {
845 if (!ops->set_mode) {
846 printk(KERN_ERR "%s: no set_mode operation for %s\n",
847 __func__, name);
848 ret = -EINVAL;
849 goto out;
850 }
851
852 ret = ops->set_mode(rdev, constraints->initial_mode);
853 if (ret < 0) {
854 printk(KERN_ERR
855 "%s: failed to set initial mode for %s: %d\n",
856 __func__, name, ret);
857 goto out;
858 }
859 }
860
cacf90f2
MB
861 /* If the constraints say the regulator should be on at this point
862 * and we have control then make sure it is enabled.
863 */
864 if ((constraints->always_on || constraints->boot_on) && ops->enable) {
e5fda26c
MB
865 ret = ops->enable(rdev);
866 if (ret < 0) {
867 printk(KERN_ERR "%s: failed to enable %s\n",
868 __func__, name);
869 rdev->constraints = NULL;
870 goto out;
871 }
872 }
873
a5766f11
LG
874 print_constraints(rdev);
875out:
876 return ret;
877}
878
879/**
880 * set_supply - set regulator supply regulator
69279fb9
MB
881 * @rdev: regulator name
882 * @supply_rdev: supply regulator name
a5766f11
LG
883 *
884 * Called by platform initialisation code to set the supply regulator for this
885 * regulator. This ensures that a regulators supply will also be enabled by the
886 * core if it's child is enabled.
887 */
888static int set_supply(struct regulator_dev *rdev,
889 struct regulator_dev *supply_rdev)
890{
891 int err;
892
893 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
894 "supply");
895 if (err) {
896 printk(KERN_ERR
897 "%s: could not add device link %s err %d\n",
898 __func__, supply_rdev->dev.kobj.name, err);
899 goto out;
900 }
901 rdev->supply = supply_rdev;
902 list_add(&rdev->slist, &supply_rdev->supply_list);
903out:
904 return err;
905}
906
907/**
908 * set_consumer_device_supply: Bind a regulator to a symbolic supply
69279fb9
MB
909 * @rdev: regulator source
910 * @consumer_dev: device the supply applies to
40f9244f 911 * @consumer_dev_name: dev_name() string for device supply applies to
69279fb9 912 * @supply: symbolic name for supply
a5766f11
LG
913 *
914 * Allows platform initialisation code to map physical regulator
915 * sources to symbolic names for supplies for use by devices. Devices
916 * should use these symbolic names to request regulators, avoiding the
917 * need to provide board-specific regulator names as platform data.
40f9244f
MB
918 *
919 * Only one of consumer_dev and consumer_dev_name may be specified.
a5766f11
LG
920 */
921static int set_consumer_device_supply(struct regulator_dev *rdev,
40f9244f
MB
922 struct device *consumer_dev, const char *consumer_dev_name,
923 const char *supply)
a5766f11
LG
924{
925 struct regulator_map *node;
9ed2099e 926 int has_dev;
a5766f11 927
40f9244f
MB
928 if (consumer_dev && consumer_dev_name)
929 return -EINVAL;
930
931 if (!consumer_dev_name && consumer_dev)
932 consumer_dev_name = dev_name(consumer_dev);
933
a5766f11
LG
934 if (supply == NULL)
935 return -EINVAL;
936
9ed2099e
MB
937 if (consumer_dev_name != NULL)
938 has_dev = 1;
939 else
940 has_dev = 0;
941
6001e13c 942 list_for_each_entry(node, &regulator_map_list, list) {
40f9244f 943 if (consumer_dev_name != node->dev_name)
6001e13c
DB
944 continue;
945 if (strcmp(node->supply, supply) != 0)
946 continue;
947
948 dev_dbg(consumer_dev, "%s/%s is '%s' supply; fail %s/%s\n",
949 dev_name(&node->regulator->dev),
950 node->regulator->desc->name,
951 supply,
1083c393 952 dev_name(&rdev->dev), rdev_get_name(rdev));
6001e13c
DB
953 return -EBUSY;
954 }
955
9ed2099e 956 node = kzalloc(sizeof(struct regulator_map), GFP_KERNEL);
a5766f11
LG
957 if (node == NULL)
958 return -ENOMEM;
959
960 node->regulator = rdev;
a5766f11
LG
961 node->supply = supply;
962
9ed2099e
MB
963 if (has_dev) {
964 node->dev_name = kstrdup(consumer_dev_name, GFP_KERNEL);
965 if (node->dev_name == NULL) {
966 kfree(node);
967 return -ENOMEM;
968 }
40f9244f
MB
969 }
970
a5766f11
LG
971 list_add(&node->list, &regulator_map_list);
972 return 0;
973}
974
975static void unset_consumer_device_supply(struct regulator_dev *rdev,
40f9244f 976 const char *consumer_dev_name, struct device *consumer_dev)
a5766f11
LG
977{
978 struct regulator_map *node, *n;
979
40f9244f
MB
980 if (consumer_dev && !consumer_dev_name)
981 consumer_dev_name = dev_name(consumer_dev);
982
a5766f11 983 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
40f9244f
MB
984 if (rdev != node->regulator)
985 continue;
986
987 if (consumer_dev_name && node->dev_name &&
988 strcmp(consumer_dev_name, node->dev_name))
989 continue;
990
991 list_del(&node->list);
992 kfree(node->dev_name);
993 kfree(node);
994 return;
a5766f11
LG
995 }
996}
997
0f1d747b
MR
998static void unset_regulator_supplies(struct regulator_dev *rdev)
999{
1000 struct regulator_map *node, *n;
1001
1002 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
1003 if (rdev == node->regulator) {
1004 list_del(&node->list);
40f9244f 1005 kfree(node->dev_name);
0f1d747b
MR
1006 kfree(node);
1007 return;
1008 }
1009 }
1010}
1011
414c70cb
LG
1012#define REG_STR_SIZE 32
1013
1014static struct regulator *create_regulator(struct regulator_dev *rdev,
1015 struct device *dev,
1016 const char *supply_name)
1017{
1018 struct regulator *regulator;
1019 char buf[REG_STR_SIZE];
1020 int err, size;
1021
1022 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
1023 if (regulator == NULL)
1024 return NULL;
1025
1026 mutex_lock(&rdev->mutex);
1027 regulator->rdev = rdev;
1028 list_add(&regulator->list, &rdev->consumer_list);
1029
1030 if (dev) {
1031 /* create a 'requested_microamps_name' sysfs entry */
1032 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
1033 supply_name);
1034 if (size >= REG_STR_SIZE)
1035 goto overflow_err;
1036
1037 regulator->dev = dev;
1038 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
1039 if (regulator->dev_attr.attr.name == NULL)
1040 goto attr_name_err;
1041
1042 regulator->dev_attr.attr.owner = THIS_MODULE;
1043 regulator->dev_attr.attr.mode = 0444;
1044 regulator->dev_attr.show = device_requested_uA_show;
1045 err = device_create_file(dev, &regulator->dev_attr);
1046 if (err < 0) {
1047 printk(KERN_WARNING "%s: could not add regulator_dev"
1048 " load sysfs\n", __func__);
1049 goto attr_name_err;
1050 }
1051
1052 /* also add a link to the device sysfs entry */
1053 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
1054 dev->kobj.name, supply_name);
1055 if (size >= REG_STR_SIZE)
1056 goto attr_err;
1057
1058 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
1059 if (regulator->supply_name == NULL)
1060 goto attr_err;
1061
1062 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
1063 buf);
1064 if (err) {
1065 printk(KERN_WARNING
1066 "%s: could not add device link %s err %d\n",
1067 __func__, dev->kobj.name, err);
1068 device_remove_file(dev, &regulator->dev_attr);
1069 goto link_name_err;
1070 }
1071 }
1072 mutex_unlock(&rdev->mutex);
1073 return regulator;
1074link_name_err:
1075 kfree(regulator->supply_name);
1076attr_err:
1077 device_remove_file(regulator->dev, &regulator->dev_attr);
1078attr_name_err:
1079 kfree(regulator->dev_attr.attr.name);
1080overflow_err:
1081 list_del(&regulator->list);
1082 kfree(regulator);
1083 mutex_unlock(&rdev->mutex);
1084 return NULL;
1085}
1086
5ffbd136
MB
1087/* Internal regulator request function */
1088static struct regulator *_regulator_get(struct device *dev, const char *id,
1089 int exclusive)
414c70cb
LG
1090{
1091 struct regulator_dev *rdev;
1092 struct regulator_map *map;
1093 struct regulator *regulator = ERR_PTR(-ENODEV);
40f9244f 1094 const char *devname = NULL;
5ffbd136 1095 int ret;
414c70cb
LG
1096
1097 if (id == NULL) {
1098 printk(KERN_ERR "regulator: get() with no identifier\n");
1099 return regulator;
1100 }
1101
40f9244f
MB
1102 if (dev)
1103 devname = dev_name(dev);
1104
414c70cb
LG
1105 mutex_lock(&regulator_list_mutex);
1106
1107 list_for_each_entry(map, &regulator_map_list, list) {
40f9244f
MB
1108 /* If the mapping has a device set up it must match */
1109 if (map->dev_name &&
1110 (!devname || strcmp(map->dev_name, devname)))
1111 continue;
1112
1113 if (strcmp(map->supply, id) == 0) {
a5766f11 1114 rdev = map->regulator;
414c70cb 1115 goto found;
a5766f11 1116 }
414c70cb 1117 }
414c70cb
LG
1118 mutex_unlock(&regulator_list_mutex);
1119 return regulator;
1120
1121found:
5ffbd136
MB
1122 if (rdev->exclusive) {
1123 regulator = ERR_PTR(-EPERM);
1124 goto out;
1125 }
1126
1127 if (exclusive && rdev->open_count) {
1128 regulator = ERR_PTR(-EBUSY);
1129 goto out;
1130 }
1131
a5766f11
LG
1132 if (!try_module_get(rdev->owner))
1133 goto out;
1134
414c70cb
LG
1135 regulator = create_regulator(rdev, dev, id);
1136 if (regulator == NULL) {
1137 regulator = ERR_PTR(-ENOMEM);
1138 module_put(rdev->owner);
1139 }
1140
5ffbd136
MB
1141 rdev->open_count++;
1142 if (exclusive) {
1143 rdev->exclusive = 1;
1144
1145 ret = _regulator_is_enabled(rdev);
1146 if (ret > 0)
1147 rdev->use_count = 1;
1148 else
1149 rdev->use_count = 0;
1150 }
1151
a5766f11 1152out:
414c70cb 1153 mutex_unlock(&regulator_list_mutex);
5ffbd136 1154
414c70cb
LG
1155 return regulator;
1156}
5ffbd136
MB
1157
1158/**
1159 * regulator_get - lookup and obtain a reference to a regulator.
1160 * @dev: device for regulator "consumer"
1161 * @id: Supply name or regulator ID.
1162 *
1163 * Returns a struct regulator corresponding to the regulator producer,
1164 * or IS_ERR() condition containing errno.
1165 *
1166 * Use of supply names configured via regulator_set_device_supply() is
1167 * strongly encouraged. It is recommended that the supply name used
1168 * should match the name used for the supply and/or the relevant
1169 * device pins in the datasheet.
1170 */
1171struct regulator *regulator_get(struct device *dev, const char *id)
1172{
1173 return _regulator_get(dev, id, 0);
1174}
414c70cb
LG
1175EXPORT_SYMBOL_GPL(regulator_get);
1176
5ffbd136
MB
1177/**
1178 * regulator_get_exclusive - obtain exclusive access to a regulator.
1179 * @dev: device for regulator "consumer"
1180 * @id: Supply name or regulator ID.
1181 *
1182 * Returns a struct regulator corresponding to the regulator producer,
1183 * or IS_ERR() condition containing errno. Other consumers will be
1184 * unable to obtain this reference is held and the use count for the
1185 * regulator will be initialised to reflect the current state of the
1186 * regulator.
1187 *
1188 * This is intended for use by consumers which cannot tolerate shared
1189 * use of the regulator such as those which need to force the
1190 * regulator off for correct operation of the hardware they are
1191 * controlling.
1192 *
1193 * Use of supply names configured via regulator_set_device_supply() is
1194 * strongly encouraged. It is recommended that the supply name used
1195 * should match the name used for the supply and/or the relevant
1196 * device pins in the datasheet.
1197 */
1198struct regulator *regulator_get_exclusive(struct device *dev, const char *id)
1199{
1200 return _regulator_get(dev, id, 1);
1201}
1202EXPORT_SYMBOL_GPL(regulator_get_exclusive);
1203
414c70cb
LG
1204/**
1205 * regulator_put - "free" the regulator source
1206 * @regulator: regulator source
1207 *
1208 * Note: drivers must ensure that all regulator_enable calls made on this
1209 * regulator source are balanced by regulator_disable calls prior to calling
1210 * this function.
1211 */
1212void regulator_put(struct regulator *regulator)
1213{
1214 struct regulator_dev *rdev;
1215
1216 if (regulator == NULL || IS_ERR(regulator))
1217 return;
1218
414c70cb
LG
1219 mutex_lock(&regulator_list_mutex);
1220 rdev = regulator->rdev;
1221
1222 /* remove any sysfs entries */
1223 if (regulator->dev) {
1224 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
1225 kfree(regulator->supply_name);
1226 device_remove_file(regulator->dev, &regulator->dev_attr);
1227 kfree(regulator->dev_attr.attr.name);
1228 }
1229 list_del(&regulator->list);
1230 kfree(regulator);
1231
5ffbd136
MB
1232 rdev->open_count--;
1233 rdev->exclusive = 0;
1234
414c70cb
LG
1235 module_put(rdev->owner);
1236 mutex_unlock(&regulator_list_mutex);
1237}
1238EXPORT_SYMBOL_GPL(regulator_put);
1239
9a2372fa
MB
1240static int _regulator_can_change_status(struct regulator_dev *rdev)
1241{
1242 if (!rdev->constraints)
1243 return 0;
1244
1245 if (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_STATUS)
1246 return 1;
1247 else
1248 return 0;
1249}
1250
414c70cb
LG
1251/* locks held by regulator_enable() */
1252static int _regulator_enable(struct regulator_dev *rdev)
1253{
9a2372fa 1254 int ret;
414c70cb
LG
1255
1256 /* do we need to enable the supply regulator first */
1257 if (rdev->supply) {
1258 ret = _regulator_enable(rdev->supply);
1259 if (ret < 0) {
1260 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1083c393 1261 __func__, rdev_get_name(rdev), ret);
414c70cb
LG
1262 return ret;
1263 }
1264 }
1265
1266 /* check voltage and requested load before enabling */
9a2372fa
MB
1267 if (rdev->constraints &&
1268 (rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS))
1269 drms_uA_update(rdev);
414c70cb 1270
9a2372fa
MB
1271 if (rdev->use_count == 0) {
1272 /* The regulator may on if it's not switchable or left on */
1273 ret = _regulator_is_enabled(rdev);
1274 if (ret == -EINVAL || ret == 0) {
1275 if (!_regulator_can_change_status(rdev))
1276 return -EPERM;
1277
1278 if (rdev->desc->ops->enable) {
1279 ret = rdev->desc->ops->enable(rdev);
1280 if (ret < 0)
1281 return ret;
1282 } else {
1283 return -EINVAL;
1284 }
a7433cff 1285 } else if (ret < 0) {
9a2372fa 1286 printk(KERN_ERR "%s: is_enabled() failed for %s: %d\n",
1083c393 1287 __func__, rdev_get_name(rdev), ret);
414c70cb
LG
1288 return ret;
1289 }
a7433cff 1290 /* Fallthrough on positive return values - already enabled */
414c70cb
LG
1291 }
1292
9a2372fa
MB
1293 rdev->use_count++;
1294
1295 return 0;
414c70cb
LG
1296}
1297
1298/**
1299 * regulator_enable - enable regulator output
1300 * @regulator: regulator source
1301 *
cf7bbcdf
MB
1302 * Request that the regulator be enabled with the regulator output at
1303 * the predefined voltage or current value. Calls to regulator_enable()
1304 * must be balanced with calls to regulator_disable().
1305 *
414c70cb 1306 * NOTE: the output value can be set by other drivers, boot loader or may be
cf7bbcdf 1307 * hardwired in the regulator.
414c70cb
LG
1308 */
1309int regulator_enable(struct regulator *regulator)
1310{
412aec61
DB
1311 struct regulator_dev *rdev = regulator->rdev;
1312 int ret = 0;
414c70cb 1313
412aec61 1314 mutex_lock(&rdev->mutex);
cd94b505 1315 ret = _regulator_enable(rdev);
412aec61 1316 mutex_unlock(&rdev->mutex);
414c70cb
LG
1317 return ret;
1318}
1319EXPORT_SYMBOL_GPL(regulator_enable);
1320
1321/* locks held by regulator_disable() */
1322static int _regulator_disable(struct regulator_dev *rdev)
1323{
1324 int ret = 0;
1325
cd94b505
DB
1326 if (WARN(rdev->use_count <= 0,
1327 "unbalanced disables for %s\n",
1083c393 1328 rdev_get_name(rdev)))
cd94b505
DB
1329 return -EIO;
1330
414c70cb 1331 /* are we the last user and permitted to disable ? */
60ef66fc
MB
1332 if (rdev->use_count == 1 &&
1333 (rdev->constraints && !rdev->constraints->always_on)) {
414c70cb
LG
1334
1335 /* we are last user */
9a2372fa
MB
1336 if (_regulator_can_change_status(rdev) &&
1337 rdev->desc->ops->disable) {
414c70cb
LG
1338 ret = rdev->desc->ops->disable(rdev);
1339 if (ret < 0) {
1340 printk(KERN_ERR "%s: failed to disable %s\n",
1083c393 1341 __func__, rdev_get_name(rdev));
414c70cb
LG
1342 return ret;
1343 }
1344 }
1345
1346 /* decrease our supplies ref count and disable if required */
1347 if (rdev->supply)
1348 _regulator_disable(rdev->supply);
1349
1350 rdev->use_count = 0;
1351 } else if (rdev->use_count > 1) {
1352
1353 if (rdev->constraints &&
1354 (rdev->constraints->valid_ops_mask &
1355 REGULATOR_CHANGE_DRMS))
1356 drms_uA_update(rdev);
1357
1358 rdev->use_count--;
1359 }
1360 return ret;
1361}
1362
1363/**
1364 * regulator_disable - disable regulator output
1365 * @regulator: regulator source
1366 *
cf7bbcdf
MB
1367 * Disable the regulator output voltage or current. Calls to
1368 * regulator_enable() must be balanced with calls to
1369 * regulator_disable().
69279fb9 1370 *
414c70cb 1371 * NOTE: this will only disable the regulator output if no other consumer
cf7bbcdf
MB
1372 * devices have it enabled, the regulator device supports disabling and
1373 * machine constraints permit this operation.
414c70cb
LG
1374 */
1375int regulator_disable(struct regulator *regulator)
1376{
412aec61
DB
1377 struct regulator_dev *rdev = regulator->rdev;
1378 int ret = 0;
414c70cb 1379
412aec61 1380 mutex_lock(&rdev->mutex);
cd94b505 1381 ret = _regulator_disable(rdev);
412aec61 1382 mutex_unlock(&rdev->mutex);
414c70cb
LG
1383 return ret;
1384}
1385EXPORT_SYMBOL_GPL(regulator_disable);
1386
1387/* locks held by regulator_force_disable() */
1388static int _regulator_force_disable(struct regulator_dev *rdev)
1389{
1390 int ret = 0;
1391
1392 /* force disable */
1393 if (rdev->desc->ops->disable) {
1394 /* ah well, who wants to live forever... */
1395 ret = rdev->desc->ops->disable(rdev);
1396 if (ret < 0) {
1397 printk(KERN_ERR "%s: failed to force disable %s\n",
1083c393 1398 __func__, rdev_get_name(rdev));
414c70cb
LG
1399 return ret;
1400 }
1401 /* notify other consumers that power has been forced off */
1402 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1403 NULL);
1404 }
1405
1406 /* decrease our supplies ref count and disable if required */
1407 if (rdev->supply)
1408 _regulator_disable(rdev->supply);
1409
1410 rdev->use_count = 0;
1411 return ret;
1412}
1413
1414/**
1415 * regulator_force_disable - force disable regulator output
1416 * @regulator: regulator source
1417 *
1418 * Forcibly disable the regulator output voltage or current.
1419 * NOTE: this *will* disable the regulator output even if other consumer
1420 * devices have it enabled. This should be used for situations when device
1421 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1422 */
1423int regulator_force_disable(struct regulator *regulator)
1424{
1425 int ret;
1426
1427 mutex_lock(&regulator->rdev->mutex);
414c70cb
LG
1428 regulator->uA_load = 0;
1429 ret = _regulator_force_disable(regulator->rdev);
1430 mutex_unlock(&regulator->rdev->mutex);
1431 return ret;
1432}
1433EXPORT_SYMBOL_GPL(regulator_force_disable);
1434
1435static int _regulator_is_enabled(struct regulator_dev *rdev)
1436{
414c70cb 1437 /* sanity check */
9332546f
MB
1438 if (!rdev->desc->ops->is_enabled)
1439 return -EINVAL;
414c70cb 1440
9332546f 1441 return rdev->desc->ops->is_enabled(rdev);
414c70cb
LG
1442}
1443
1444/**
1445 * regulator_is_enabled - is the regulator output enabled
1446 * @regulator: regulator source
1447 *
412aec61
DB
1448 * Returns positive if the regulator driver backing the source/client
1449 * has requested that the device be enabled, zero if it hasn't, else a
1450 * negative errno code.
1451 *
1452 * Note that the device backing this regulator handle can have multiple
1453 * users, so it might be enabled even if regulator_enable() was never
1454 * called for this particular source.
414c70cb
LG
1455 */
1456int regulator_is_enabled(struct regulator *regulator)
1457{
9332546f
MB
1458 int ret;
1459
1460 mutex_lock(&regulator->rdev->mutex);
1461 ret = _regulator_is_enabled(regulator->rdev);
1462 mutex_unlock(&regulator->rdev->mutex);
1463
1464 return ret;
414c70cb
LG
1465}
1466EXPORT_SYMBOL_GPL(regulator_is_enabled);
1467
4367cfdc
DB
1468/**
1469 * regulator_count_voltages - count regulator_list_voltage() selectors
1470 * @regulator: regulator source
1471 *
1472 * Returns number of selectors, or negative errno. Selectors are
1473 * numbered starting at zero, and typically correspond to bitfields
1474 * in hardware registers.
1475 */
1476int regulator_count_voltages(struct regulator *regulator)
1477{
1478 struct regulator_dev *rdev = regulator->rdev;
1479
1480 return rdev->desc->n_voltages ? : -EINVAL;
1481}
1482EXPORT_SYMBOL_GPL(regulator_count_voltages);
1483
1484/**
1485 * regulator_list_voltage - enumerate supported voltages
1486 * @regulator: regulator source
1487 * @selector: identify voltage to list
1488 * Context: can sleep
1489 *
1490 * Returns a voltage that can be passed to @regulator_set_voltage(),
1491 * zero if this selector code can't be used on this sytem, or a
1492 * negative errno.
1493 */
1494int regulator_list_voltage(struct regulator *regulator, unsigned selector)
1495{
1496 struct regulator_dev *rdev = regulator->rdev;
1497 struct regulator_ops *ops = rdev->desc->ops;
1498 int ret;
1499
1500 if (!ops->list_voltage || selector >= rdev->desc->n_voltages)
1501 return -EINVAL;
1502
1503 mutex_lock(&rdev->mutex);
1504 ret = ops->list_voltage(rdev, selector);
1505 mutex_unlock(&rdev->mutex);
1506
1507 if (ret > 0) {
1508 if (ret < rdev->constraints->min_uV)
1509 ret = 0;
1510 else if (ret > rdev->constraints->max_uV)
1511 ret = 0;
1512 }
1513
1514 return ret;
1515}
1516EXPORT_SYMBOL_GPL(regulator_list_voltage);
1517
a7a1ad90
MB
1518/**
1519 * regulator_is_supported_voltage - check if a voltage range can be supported
1520 *
1521 * @regulator: Regulator to check.
1522 * @min_uV: Minimum required voltage in uV.
1523 * @max_uV: Maximum required voltage in uV.
1524 *
1525 * Returns a boolean or a negative error code.
1526 */
1527int regulator_is_supported_voltage(struct regulator *regulator,
1528 int min_uV, int max_uV)
1529{
1530 int i, voltages, ret;
1531
1532 ret = regulator_count_voltages(regulator);
1533 if (ret < 0)
1534 return ret;
1535 voltages = ret;
1536
1537 for (i = 0; i < voltages; i++) {
1538 ret = regulator_list_voltage(regulator, i);
1539
1540 if (ret >= min_uV && ret <= max_uV)
1541 return 1;
1542 }
1543
1544 return 0;
1545}
1546
414c70cb
LG
1547/**
1548 * regulator_set_voltage - set regulator output voltage
1549 * @regulator: regulator source
1550 * @min_uV: Minimum required voltage in uV
1551 * @max_uV: Maximum acceptable voltage in uV
1552 *
1553 * Sets a voltage regulator to the desired output voltage. This can be set
1554 * during any regulator state. IOW, regulator can be disabled or enabled.
1555 *
1556 * If the regulator is enabled then the voltage will change to the new value
1557 * immediately otherwise if the regulator is disabled the regulator will
1558 * output at the new voltage when enabled.
1559 *
1560 * NOTE: If the regulator is shared between several devices then the lowest
1561 * request voltage that meets the system constraints will be used.
69279fb9 1562 * Regulator system constraints must be set for this regulator before
414c70cb
LG
1563 * calling this function otherwise this call will fail.
1564 */
1565int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1566{
1567 struct regulator_dev *rdev = regulator->rdev;
1568 int ret;
1569
1570 mutex_lock(&rdev->mutex);
1571
1572 /* sanity check */
1573 if (!rdev->desc->ops->set_voltage) {
1574 ret = -EINVAL;
1575 goto out;
1576 }
1577
1578 /* constraints check */
1579 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1580 if (ret < 0)
1581 goto out;
1582 regulator->min_uV = min_uV;
1583 regulator->max_uV = max_uV;
1584 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1585
1586out:
b136fb44 1587 _notifier_call_chain(rdev, REGULATOR_EVENT_VOLTAGE_CHANGE, NULL);
414c70cb
LG
1588 mutex_unlock(&rdev->mutex);
1589 return ret;
1590}
1591EXPORT_SYMBOL_GPL(regulator_set_voltage);
1592
1593static int _regulator_get_voltage(struct regulator_dev *rdev)
1594{
1595 /* sanity check */
1596 if (rdev->desc->ops->get_voltage)
1597 return rdev->desc->ops->get_voltage(rdev);
1598 else
1599 return -EINVAL;
1600}
1601
1602/**
1603 * regulator_get_voltage - get regulator output voltage
1604 * @regulator: regulator source
1605 *
1606 * This returns the current regulator voltage in uV.
1607 *
1608 * NOTE: If the regulator is disabled it will return the voltage value. This
1609 * function should not be used to determine regulator state.
1610 */
1611int regulator_get_voltage(struct regulator *regulator)
1612{
1613 int ret;
1614
1615 mutex_lock(&regulator->rdev->mutex);
1616
1617 ret = _regulator_get_voltage(regulator->rdev);
1618
1619 mutex_unlock(&regulator->rdev->mutex);
1620
1621 return ret;
1622}
1623EXPORT_SYMBOL_GPL(regulator_get_voltage);
1624
1625/**
1626 * regulator_set_current_limit - set regulator output current limit
1627 * @regulator: regulator source
1628 * @min_uA: Minimuum supported current in uA
1629 * @max_uA: Maximum supported current in uA
1630 *
1631 * Sets current sink to the desired output current. This can be set during
1632 * any regulator state. IOW, regulator can be disabled or enabled.
1633 *
1634 * If the regulator is enabled then the current will change to the new value
1635 * immediately otherwise if the regulator is disabled the regulator will
1636 * output at the new current when enabled.
1637 *
1638 * NOTE: Regulator system constraints must be set for this regulator before
1639 * calling this function otherwise this call will fail.
1640 */
1641int regulator_set_current_limit(struct regulator *regulator,
1642 int min_uA, int max_uA)
1643{
1644 struct regulator_dev *rdev = regulator->rdev;
1645 int ret;
1646
1647 mutex_lock(&rdev->mutex);
1648
1649 /* sanity check */
1650 if (!rdev->desc->ops->set_current_limit) {
1651 ret = -EINVAL;
1652 goto out;
1653 }
1654
1655 /* constraints check */
1656 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1657 if (ret < 0)
1658 goto out;
1659
1660 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1661out:
1662 mutex_unlock(&rdev->mutex);
1663 return ret;
1664}
1665EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1666
1667static int _regulator_get_current_limit(struct regulator_dev *rdev)
1668{
1669 int ret;
1670
1671 mutex_lock(&rdev->mutex);
1672
1673 /* sanity check */
1674 if (!rdev->desc->ops->get_current_limit) {
1675 ret = -EINVAL;
1676 goto out;
1677 }
1678
1679 ret = rdev->desc->ops->get_current_limit(rdev);
1680out:
1681 mutex_unlock(&rdev->mutex);
1682 return ret;
1683}
1684
1685/**
1686 * regulator_get_current_limit - get regulator output current
1687 * @regulator: regulator source
1688 *
1689 * This returns the current supplied by the specified current sink in uA.
1690 *
1691 * NOTE: If the regulator is disabled it will return the current value. This
1692 * function should not be used to determine regulator state.
1693 */
1694int regulator_get_current_limit(struct regulator *regulator)
1695{
1696 return _regulator_get_current_limit(regulator->rdev);
1697}
1698EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1699
1700/**
1701 * regulator_set_mode - set regulator operating mode
1702 * @regulator: regulator source
1703 * @mode: operating mode - one of the REGULATOR_MODE constants
1704 *
1705 * Set regulator operating mode to increase regulator efficiency or improve
1706 * regulation performance.
1707 *
1708 * NOTE: Regulator system constraints must be set for this regulator before
1709 * calling this function otherwise this call will fail.
1710 */
1711int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1712{
1713 struct regulator_dev *rdev = regulator->rdev;
1714 int ret;
1715
1716 mutex_lock(&rdev->mutex);
1717
1718 /* sanity check */
1719 if (!rdev->desc->ops->set_mode) {
1720 ret = -EINVAL;
1721 goto out;
1722 }
1723
1724 /* constraints check */
1725 ret = regulator_check_mode(rdev, mode);
1726 if (ret < 0)
1727 goto out;
1728
1729 ret = rdev->desc->ops->set_mode(rdev, mode);
1730out:
1731 mutex_unlock(&rdev->mutex);
1732 return ret;
1733}
1734EXPORT_SYMBOL_GPL(regulator_set_mode);
1735
1736static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1737{
1738 int ret;
1739
1740 mutex_lock(&rdev->mutex);
1741
1742 /* sanity check */
1743 if (!rdev->desc->ops->get_mode) {
1744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 ret = rdev->desc->ops->get_mode(rdev);
1749out:
1750 mutex_unlock(&rdev->mutex);
1751 return ret;
1752}
1753
1754/**
1755 * regulator_get_mode - get regulator operating mode
1756 * @regulator: regulator source
1757 *
1758 * Get the current regulator operating mode.
1759 */
1760unsigned int regulator_get_mode(struct regulator *regulator)
1761{
1762 return _regulator_get_mode(regulator->rdev);
1763}
1764EXPORT_SYMBOL_GPL(regulator_get_mode);
1765
1766/**
1767 * regulator_set_optimum_mode - set regulator optimum operating mode
1768 * @regulator: regulator source
1769 * @uA_load: load current
1770 *
1771 * Notifies the regulator core of a new device load. This is then used by
1772 * DRMS (if enabled by constraints) to set the most efficient regulator
1773 * operating mode for the new regulator loading.
1774 *
1775 * Consumer devices notify their supply regulator of the maximum power
1776 * they will require (can be taken from device datasheet in the power
1777 * consumption tables) when they change operational status and hence power
1778 * state. Examples of operational state changes that can affect power
1779 * consumption are :-
1780 *
1781 * o Device is opened / closed.
1782 * o Device I/O is about to begin or has just finished.
1783 * o Device is idling in between work.
1784 *
1785 * This information is also exported via sysfs to userspace.
1786 *
1787 * DRMS will sum the total requested load on the regulator and change
1788 * to the most efficient operating mode if platform constraints allow.
1789 *
1790 * Returns the new regulator mode or error.
1791 */
1792int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1793{
1794 struct regulator_dev *rdev = regulator->rdev;
1795 struct regulator *consumer;
1796 int ret, output_uV, input_uV, total_uA_load = 0;
1797 unsigned int mode;
1798
1799 mutex_lock(&rdev->mutex);
1800
1801 regulator->uA_load = uA_load;
1802 ret = regulator_check_drms(rdev);
1803 if (ret < 0)
1804 goto out;
1805 ret = -EINVAL;
1806
1807 /* sanity check */
1808 if (!rdev->desc->ops->get_optimum_mode)
1809 goto out;
1810
1811 /* get output voltage */
1812 output_uV = rdev->desc->ops->get_voltage(rdev);
1813 if (output_uV <= 0) {
1814 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1083c393 1815 __func__, rdev_get_name(rdev));
414c70cb
LG
1816 goto out;
1817 }
1818
1819 /* get input voltage */
1820 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1821 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1822 else
1823 input_uV = rdev->constraints->input_uV;
1824 if (input_uV <= 0) {
1825 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1083c393 1826 __func__, rdev_get_name(rdev));
414c70cb
LG
1827 goto out;
1828 }
1829
1830 /* calc total requested load for this regulator */
1831 list_for_each_entry(consumer, &rdev->consumer_list, list)
fa2984d4 1832 total_uA_load += consumer->uA_load;
414c70cb
LG
1833
1834 mode = rdev->desc->ops->get_optimum_mode(rdev,
1835 input_uV, output_uV,
1836 total_uA_load);
e573520b
DB
1837 ret = regulator_check_mode(rdev, mode);
1838 if (ret < 0) {
414c70cb 1839 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1083c393 1840 " %d uA %d -> %d uV\n", __func__, rdev_get_name(rdev),
414c70cb
LG
1841 total_uA_load, input_uV, output_uV);
1842 goto out;
1843 }
1844
1845 ret = rdev->desc->ops->set_mode(rdev, mode);
e573520b 1846 if (ret < 0) {
414c70cb 1847 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1083c393 1848 __func__, mode, rdev_get_name(rdev));
414c70cb
LG
1849 goto out;
1850 }
1851 ret = mode;
1852out:
1853 mutex_unlock(&rdev->mutex);
1854 return ret;
1855}
1856EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1857
1858/**
1859 * regulator_register_notifier - register regulator event notifier
1860 * @regulator: regulator source
69279fb9 1861 * @nb: notifier block
414c70cb
LG
1862 *
1863 * Register notifier block to receive regulator events.
1864 */
1865int regulator_register_notifier(struct regulator *regulator,
1866 struct notifier_block *nb)
1867{
1868 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1869 nb);
1870}
1871EXPORT_SYMBOL_GPL(regulator_register_notifier);
1872
1873/**
1874 * regulator_unregister_notifier - unregister regulator event notifier
1875 * @regulator: regulator source
69279fb9 1876 * @nb: notifier block
414c70cb
LG
1877 *
1878 * Unregister regulator event notifier block.
1879 */
1880int regulator_unregister_notifier(struct regulator *regulator,
1881 struct notifier_block *nb)
1882{
1883 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1884 nb);
1885}
1886EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1887
b136fb44
JC
1888/* notify regulator consumers and downstream regulator consumers.
1889 * Note mutex must be held by caller.
1890 */
414c70cb
LG
1891static void _notifier_call_chain(struct regulator_dev *rdev,
1892 unsigned long event, void *data)
1893{
1894 struct regulator_dev *_rdev;
1895
1896 /* call rdev chain first */
414c70cb 1897 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
414c70cb
LG
1898
1899 /* now notify regulator we supply */
b136fb44 1900 list_for_each_entry(_rdev, &rdev->supply_list, slist) {
fa2984d4
SR
1901 mutex_lock(&_rdev->mutex);
1902 _notifier_call_chain(_rdev, event, data);
1903 mutex_unlock(&_rdev->mutex);
b136fb44 1904 }
414c70cb
LG
1905}
1906
1907/**
1908 * regulator_bulk_get - get multiple regulator consumers
1909 *
1910 * @dev: Device to supply
1911 * @num_consumers: Number of consumers to register
1912 * @consumers: Configuration of consumers; clients are stored here.
1913 *
1914 * @return 0 on success, an errno on failure.
1915 *
1916 * This helper function allows drivers to get several regulator
1917 * consumers in one operation. If any of the regulators cannot be
1918 * acquired then any regulators that were allocated will be freed
1919 * before returning to the caller.
1920 */
1921int regulator_bulk_get(struct device *dev, int num_consumers,
1922 struct regulator_bulk_data *consumers)
1923{
1924 int i;
1925 int ret;
1926
1927 for (i = 0; i < num_consumers; i++)
1928 consumers[i].consumer = NULL;
1929
1930 for (i = 0; i < num_consumers; i++) {
1931 consumers[i].consumer = regulator_get(dev,
1932 consumers[i].supply);
1933 if (IS_ERR(consumers[i].consumer)) {
414c70cb 1934 ret = PTR_ERR(consumers[i].consumer);
5b307627
MB
1935 dev_err(dev, "Failed to get supply '%s': %d\n",
1936 consumers[i].supply, ret);
414c70cb
LG
1937 consumers[i].consumer = NULL;
1938 goto err;
1939 }
1940 }
1941
1942 return 0;
1943
1944err:
1945 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1946 regulator_put(consumers[i].consumer);
1947
1948 return ret;
1949}
1950EXPORT_SYMBOL_GPL(regulator_bulk_get);
1951
1952/**
1953 * regulator_bulk_enable - enable multiple regulator consumers
1954 *
1955 * @num_consumers: Number of consumers
1956 * @consumers: Consumer data; clients are stored here.
1957 * @return 0 on success, an errno on failure
1958 *
1959 * This convenience API allows consumers to enable multiple regulator
1960 * clients in a single API call. If any consumers cannot be enabled
1961 * then any others that were enabled will be disabled again prior to
1962 * return.
1963 */
1964int regulator_bulk_enable(int num_consumers,
1965 struct regulator_bulk_data *consumers)
1966{
1967 int i;
1968 int ret;
1969
1970 for (i = 0; i < num_consumers; i++) {
1971 ret = regulator_enable(consumers[i].consumer);
1972 if (ret != 0)
1973 goto err;
1974 }
1975
1976 return 0;
1977
1978err:
5b307627 1979 printk(KERN_ERR "Failed to enable %s: %d\n", consumers[i].supply, ret);
eb143ac1 1980 for (--i; i >= 0; --i)
414c70cb
LG
1981 regulator_disable(consumers[i].consumer);
1982
1983 return ret;
1984}
1985EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1986
1987/**
1988 * regulator_bulk_disable - disable multiple regulator consumers
1989 *
1990 * @num_consumers: Number of consumers
1991 * @consumers: Consumer data; clients are stored here.
1992 * @return 0 on success, an errno on failure
1993 *
1994 * This convenience API allows consumers to disable multiple regulator
1995 * clients in a single API call. If any consumers cannot be enabled
1996 * then any others that were disabled will be disabled again prior to
1997 * return.
1998 */
1999int regulator_bulk_disable(int num_consumers,
2000 struct regulator_bulk_data *consumers)
2001{
2002 int i;
2003 int ret;
2004
2005 for (i = 0; i < num_consumers; i++) {
2006 ret = regulator_disable(consumers[i].consumer);
2007 if (ret != 0)
2008 goto err;
2009 }
2010
2011 return 0;
2012
2013err:
5b307627
MB
2014 printk(KERN_ERR "Failed to disable %s: %d\n", consumers[i].supply,
2015 ret);
eb143ac1 2016 for (--i; i >= 0; --i)
414c70cb
LG
2017 regulator_enable(consumers[i].consumer);
2018
2019 return ret;
2020}
2021EXPORT_SYMBOL_GPL(regulator_bulk_disable);
2022
2023/**
2024 * regulator_bulk_free - free multiple regulator consumers
2025 *
2026 * @num_consumers: Number of consumers
2027 * @consumers: Consumer data; clients are stored here.
2028 *
2029 * This convenience API allows consumers to free multiple regulator
2030 * clients in a single API call.
2031 */
2032void regulator_bulk_free(int num_consumers,
2033 struct regulator_bulk_data *consumers)
2034{
2035 int i;
2036
2037 for (i = 0; i < num_consumers; i++) {
2038 regulator_put(consumers[i].consumer);
2039 consumers[i].consumer = NULL;
2040 }
2041}
2042EXPORT_SYMBOL_GPL(regulator_bulk_free);
2043
2044/**
2045 * regulator_notifier_call_chain - call regulator event notifier
69279fb9 2046 * @rdev: regulator source
414c70cb 2047 * @event: notifier block
69279fb9 2048 * @data: callback-specific data.
414c70cb
LG
2049 *
2050 * Called by regulator drivers to notify clients a regulator event has
2051 * occurred. We also notify regulator clients downstream.
b136fb44 2052 * Note lock must be held by caller.
414c70cb
LG
2053 */
2054int regulator_notifier_call_chain(struct regulator_dev *rdev,
2055 unsigned long event, void *data)
2056{
2057 _notifier_call_chain(rdev, event, data);
2058 return NOTIFY_DONE;
2059
2060}
2061EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
2062
be721979
MB
2063/**
2064 * regulator_mode_to_status - convert a regulator mode into a status
2065 *
2066 * @mode: Mode to convert
2067 *
2068 * Convert a regulator mode into a status.
2069 */
2070int regulator_mode_to_status(unsigned int mode)
2071{
2072 switch (mode) {
2073 case REGULATOR_MODE_FAST:
2074 return REGULATOR_STATUS_FAST;
2075 case REGULATOR_MODE_NORMAL:
2076 return REGULATOR_STATUS_NORMAL;
2077 case REGULATOR_MODE_IDLE:
2078 return REGULATOR_STATUS_IDLE;
2079 case REGULATOR_STATUS_STANDBY:
2080 return REGULATOR_STATUS_STANDBY;
2081 default:
2082 return 0;
2083 }
2084}
2085EXPORT_SYMBOL_GPL(regulator_mode_to_status);
2086
7ad68e2f
DB
2087/*
2088 * To avoid cluttering sysfs (and memory) with useless state, only
2089 * create attributes that can be meaningfully displayed.
2090 */
2091static int add_regulator_attributes(struct regulator_dev *rdev)
2092{
2093 struct device *dev = &rdev->dev;
2094 struct regulator_ops *ops = rdev->desc->ops;
2095 int status = 0;
2096
2097 /* some attributes need specific methods to be displayed */
2098 if (ops->get_voltage) {
2099 status = device_create_file(dev, &dev_attr_microvolts);
2100 if (status < 0)
2101 return status;
2102 }
2103 if (ops->get_current_limit) {
2104 status = device_create_file(dev, &dev_attr_microamps);
2105 if (status < 0)
2106 return status;
2107 }
2108 if (ops->get_mode) {
2109 status = device_create_file(dev, &dev_attr_opmode);
2110 if (status < 0)
2111 return status;
2112 }
2113 if (ops->is_enabled) {
2114 status = device_create_file(dev, &dev_attr_state);
2115 if (status < 0)
2116 return status;
2117 }
853116a1
DB
2118 if (ops->get_status) {
2119 status = device_create_file(dev, &dev_attr_status);
2120 if (status < 0)
2121 return status;
2122 }
7ad68e2f
DB
2123
2124 /* some attributes are type-specific */
2125 if (rdev->desc->type == REGULATOR_CURRENT) {
2126 status = device_create_file(dev, &dev_attr_requested_microamps);
2127 if (status < 0)
2128 return status;
2129 }
2130
2131 /* all the other attributes exist to support constraints;
2132 * don't show them if there are no constraints, or if the
2133 * relevant supporting methods are missing.
2134 */
2135 if (!rdev->constraints)
2136 return status;
2137
2138 /* constraints need specific supporting methods */
2139 if (ops->set_voltage) {
2140 status = device_create_file(dev, &dev_attr_min_microvolts);
2141 if (status < 0)
2142 return status;
2143 status = device_create_file(dev, &dev_attr_max_microvolts);
2144 if (status < 0)
2145 return status;
2146 }
2147 if (ops->set_current_limit) {
2148 status = device_create_file(dev, &dev_attr_min_microamps);
2149 if (status < 0)
2150 return status;
2151 status = device_create_file(dev, &dev_attr_max_microamps);
2152 if (status < 0)
2153 return status;
2154 }
2155
2156 /* suspend mode constraints need multiple supporting methods */
2157 if (!(ops->set_suspend_enable && ops->set_suspend_disable))
2158 return status;
2159
2160 status = device_create_file(dev, &dev_attr_suspend_standby_state);
2161 if (status < 0)
2162 return status;
2163 status = device_create_file(dev, &dev_attr_suspend_mem_state);
2164 if (status < 0)
2165 return status;
2166 status = device_create_file(dev, &dev_attr_suspend_disk_state);
2167 if (status < 0)
2168 return status;
2169
2170 if (ops->set_suspend_voltage) {
2171 status = device_create_file(dev,
2172 &dev_attr_suspend_standby_microvolts);
2173 if (status < 0)
2174 return status;
2175 status = device_create_file(dev,
2176 &dev_attr_suspend_mem_microvolts);
2177 if (status < 0)
2178 return status;
2179 status = device_create_file(dev,
2180 &dev_attr_suspend_disk_microvolts);
2181 if (status < 0)
2182 return status;
2183 }
2184
2185 if (ops->set_suspend_mode) {
2186 status = device_create_file(dev,
2187 &dev_attr_suspend_standby_mode);
2188 if (status < 0)
2189 return status;
2190 status = device_create_file(dev,
2191 &dev_attr_suspend_mem_mode);
2192 if (status < 0)
2193 return status;
2194 status = device_create_file(dev,
2195 &dev_attr_suspend_disk_mode);
2196 if (status < 0)
2197 return status;
2198 }
2199
2200 return status;
2201}
2202
414c70cb
LG
2203/**
2204 * regulator_register - register regulator
69279fb9
MB
2205 * @regulator_desc: regulator to register
2206 * @dev: struct device for the regulator
0527100f 2207 * @init_data: platform provided init data, passed through by driver
69279fb9 2208 * @driver_data: private regulator data
414c70cb
LG
2209 *
2210 * Called by regulator drivers to register a regulator.
2211 * Returns 0 on success.
2212 */
2213struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
0527100f
MB
2214 struct device *dev, struct regulator_init_data *init_data,
2215 void *driver_data)
414c70cb
LG
2216{
2217 static atomic_t regulator_no = ATOMIC_INIT(0);
2218 struct regulator_dev *rdev;
a5766f11 2219 int ret, i;
414c70cb
LG
2220
2221 if (regulator_desc == NULL)
2222 return ERR_PTR(-EINVAL);
2223
2224 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
2225 return ERR_PTR(-EINVAL);
2226
cd78dfc6
DL
2227 if (regulator_desc->type != REGULATOR_VOLTAGE &&
2228 regulator_desc->type != REGULATOR_CURRENT)
414c70cb
LG
2229 return ERR_PTR(-EINVAL);
2230
46fabe1e
MB
2231 if (!init_data)
2232 return ERR_PTR(-EINVAL);
2233
414c70cb
LG
2234 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
2235 if (rdev == NULL)
2236 return ERR_PTR(-ENOMEM);
2237
2238 mutex_lock(&regulator_list_mutex);
2239
2240 mutex_init(&rdev->mutex);
a5766f11 2241 rdev->reg_data = driver_data;
414c70cb
LG
2242 rdev->owner = regulator_desc->owner;
2243 rdev->desc = regulator_desc;
2244 INIT_LIST_HEAD(&rdev->consumer_list);
2245 INIT_LIST_HEAD(&rdev->supply_list);
2246 INIT_LIST_HEAD(&rdev->list);
2247 INIT_LIST_HEAD(&rdev->slist);
2248 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
2249
a5766f11
LG
2250 /* preform any regulator specific init */
2251 if (init_data->regulator_init) {
2252 ret = init_data->regulator_init(rdev->reg_data);
4fca9545
DB
2253 if (ret < 0)
2254 goto clean;
a5766f11
LG
2255 }
2256
a5766f11 2257 /* register with sysfs */
414c70cb 2258 rdev->dev.class = &regulator_class;
a5766f11 2259 rdev->dev.parent = dev;
812460a9
KS
2260 dev_set_name(&rdev->dev, "regulator.%d",
2261 atomic_inc_return(&regulator_no) - 1);
a5766f11 2262 ret = device_register(&rdev->dev);
4fca9545
DB
2263 if (ret != 0)
2264 goto clean;
a5766f11
LG
2265
2266 dev_set_drvdata(&rdev->dev, rdev);
2267
74f544c1
MR
2268 /* set regulator constraints */
2269 ret = set_machine_constraints(rdev, &init_data->constraints);
2270 if (ret < 0)
2271 goto scrub;
2272
7ad68e2f
DB
2273 /* add attributes supported by this regulator */
2274 ret = add_regulator_attributes(rdev);
2275 if (ret < 0)
2276 goto scrub;
2277
a5766f11
LG
2278 /* set supply regulator if it exists */
2279 if (init_data->supply_regulator_dev) {
2280 ret = set_supply(rdev,
2281 dev_get_drvdata(init_data->supply_regulator_dev));
4fca9545
DB
2282 if (ret < 0)
2283 goto scrub;
a5766f11
LG
2284 }
2285
2286 /* add consumers devices */
2287 for (i = 0; i < init_data->num_consumer_supplies; i++) {
2288 ret = set_consumer_device_supply(rdev,
2289 init_data->consumer_supplies[i].dev,
40f9244f 2290 init_data->consumer_supplies[i].dev_name,
a5766f11
LG
2291 init_data->consumer_supplies[i].supply);
2292 if (ret < 0) {
2293 for (--i; i >= 0; i--)
2294 unset_consumer_device_supply(rdev,
40f9244f
MB
2295 init_data->consumer_supplies[i].dev_name,
2296 init_data->consumer_supplies[i].dev);
4fca9545 2297 goto scrub;
a5766f11 2298 }
414c70cb 2299 }
a5766f11
LG
2300
2301 list_add(&rdev->list, &regulator_list);
2302out:
414c70cb
LG
2303 mutex_unlock(&regulator_list_mutex);
2304 return rdev;
4fca9545
DB
2305
2306scrub:
2307 device_unregister(&rdev->dev);
53032daf
PW
2308 /* device core frees rdev */
2309 rdev = ERR_PTR(ret);
2310 goto out;
2311
4fca9545
DB
2312clean:
2313 kfree(rdev);
2314 rdev = ERR_PTR(ret);
2315 goto out;
414c70cb
LG
2316}
2317EXPORT_SYMBOL_GPL(regulator_register);
2318
2319/**
2320 * regulator_unregister - unregister regulator
69279fb9 2321 * @rdev: regulator to unregister
414c70cb
LG
2322 *
2323 * Called by regulator drivers to unregister a regulator.
2324 */
2325void regulator_unregister(struct regulator_dev *rdev)
2326{
2327 if (rdev == NULL)
2328 return;
2329
2330 mutex_lock(&regulator_list_mutex);
6bf87d17 2331 WARN_ON(rdev->open_count);
0f1d747b 2332 unset_regulator_supplies(rdev);
414c70cb
LG
2333 list_del(&rdev->list);
2334 if (rdev->supply)
2335 sysfs_remove_link(&rdev->dev.kobj, "supply");
2336 device_unregister(&rdev->dev);
2337 mutex_unlock(&regulator_list_mutex);
2338}
2339EXPORT_SYMBOL_GPL(regulator_unregister);
2340
414c70cb 2341/**
cf7bbcdf 2342 * regulator_suspend_prepare - prepare regulators for system wide suspend
414c70cb
LG
2343 * @state: system suspend state
2344 *
2345 * Configure each regulator with it's suspend operating parameters for state.
2346 * This will usually be called by machine suspend code prior to supending.
2347 */
2348int regulator_suspend_prepare(suspend_state_t state)
2349{
2350 struct regulator_dev *rdev;
2351 int ret = 0;
2352
2353 /* ON is handled by regulator active state */
2354 if (state == PM_SUSPEND_ON)
2355 return -EINVAL;
2356
2357 mutex_lock(&regulator_list_mutex);
2358 list_for_each_entry(rdev, &regulator_list, list) {
2359
2360 mutex_lock(&rdev->mutex);
2361 ret = suspend_prepare(rdev, state);
2362 mutex_unlock(&rdev->mutex);
2363
2364 if (ret < 0) {
2365 printk(KERN_ERR "%s: failed to prepare %s\n",
1083c393 2366 __func__, rdev_get_name(rdev));
414c70cb
LG
2367 goto out;
2368 }
2369 }
2370out:
2371 mutex_unlock(&regulator_list_mutex);
2372 return ret;
2373}
2374EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
2375
ca725561
MB
2376/**
2377 * regulator_has_full_constraints - the system has fully specified constraints
2378 *
2379 * Calling this function will cause the regulator API to disable all
2380 * regulators which have a zero use count and don't have an always_on
2381 * constraint in a late_initcall.
2382 *
2383 * The intention is that this will become the default behaviour in a
2384 * future kernel release so users are encouraged to use this facility
2385 * now.
2386 */
2387void regulator_has_full_constraints(void)
2388{
2389 has_full_constraints = 1;
2390}
2391EXPORT_SYMBOL_GPL(regulator_has_full_constraints);
2392
414c70cb
LG
2393/**
2394 * rdev_get_drvdata - get rdev regulator driver data
69279fb9 2395 * @rdev: regulator
414c70cb
LG
2396 *
2397 * Get rdev regulator driver private data. This call can be used in the
2398 * regulator driver context.
2399 */
2400void *rdev_get_drvdata(struct regulator_dev *rdev)
2401{
2402 return rdev->reg_data;
2403}
2404EXPORT_SYMBOL_GPL(rdev_get_drvdata);
2405
2406/**
2407 * regulator_get_drvdata - get regulator driver data
2408 * @regulator: regulator
2409 *
2410 * Get regulator driver private data. This call can be used in the consumer
2411 * driver context when non API regulator specific functions need to be called.
2412 */
2413void *regulator_get_drvdata(struct regulator *regulator)
2414{
2415 return regulator->rdev->reg_data;
2416}
2417EXPORT_SYMBOL_GPL(regulator_get_drvdata);
2418
2419/**
2420 * regulator_set_drvdata - set regulator driver data
2421 * @regulator: regulator
2422 * @data: data
2423 */
2424void regulator_set_drvdata(struct regulator *regulator, void *data)
2425{
2426 regulator->rdev->reg_data = data;
2427}
2428EXPORT_SYMBOL_GPL(regulator_set_drvdata);
2429
2430/**
2431 * regulator_get_id - get regulator ID
69279fb9 2432 * @rdev: regulator
414c70cb
LG
2433 */
2434int rdev_get_id(struct regulator_dev *rdev)
2435{
2436 return rdev->desc->id;
2437}
2438EXPORT_SYMBOL_GPL(rdev_get_id);
2439
a5766f11
LG
2440struct device *rdev_get_dev(struct regulator_dev *rdev)
2441{
2442 return &rdev->dev;
2443}
2444EXPORT_SYMBOL_GPL(rdev_get_dev);
2445
2446void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
2447{
2448 return reg_init_data->driver_data;
2449}
2450EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
2451
414c70cb
LG
2452static int __init regulator_init(void)
2453{
2454 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
2455 return class_register(&regulator_class);
2456}
2457
2458/* init early to allow our consumers to complete system booting */
2459core_initcall(regulator_init);
ca725561
MB
2460
2461static int __init regulator_init_complete(void)
2462{
2463 struct regulator_dev *rdev;
2464 struct regulator_ops *ops;
2465 struct regulation_constraints *c;
2466 int enabled, ret;
2467 const char *name;
2468
2469 mutex_lock(&regulator_list_mutex);
2470
2471 /* If we have a full configuration then disable any regulators
2472 * which are not in use or always_on. This will become the
2473 * default behaviour in the future.
2474 */
2475 list_for_each_entry(rdev, &regulator_list, list) {
2476 ops = rdev->desc->ops;
2477 c = rdev->constraints;
2478
1083c393 2479 name = rdev_get_name(rdev);
ca725561 2480
f25e0b4f 2481 if (!ops->disable || (c && c->always_on))
ca725561
MB
2482 continue;
2483
2484 mutex_lock(&rdev->mutex);
2485
2486 if (rdev->use_count)
2487 goto unlock;
2488
2489 /* If we can't read the status assume it's on. */
2490 if (ops->is_enabled)
2491 enabled = ops->is_enabled(rdev);
2492 else
2493 enabled = 1;
2494
2495 if (!enabled)
2496 goto unlock;
2497
2498 if (has_full_constraints) {
2499 /* We log since this may kill the system if it
2500 * goes wrong. */
2501 printk(KERN_INFO "%s: disabling %s\n",
2502 __func__, name);
2503 ret = ops->disable(rdev);
2504 if (ret != 0) {
2505 printk(KERN_ERR
2506 "%s: couldn't disable %s: %d\n",
2507 __func__, name, ret);
2508 }
2509 } else {
2510 /* The intention is that in future we will
2511 * assume that full constraints are provided
2512 * so warn even if we aren't going to do
2513 * anything here.
2514 */
2515 printk(KERN_WARNING
2516 "%s: incomplete constraints, leaving %s on\n",
2517 __func__, name);
2518 }
2519
2520unlock:
2521 mutex_unlock(&rdev->mutex);
2522 }
2523
2524 mutex_unlock(&regulator_list_mutex);
2525
2526 return 0;
2527}
2528late_initcall(regulator_init_complete);