regulator: improved mode error checks
[linux-2.6-block.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);
31
32/**
33 * struct regulator_dev
34 *
35 * Voltage / Current regulator class device. One for each regulator.
36 */
37struct regulator_dev {
38 struct regulator_desc *desc;
39 int use_count;
40
41 /* lists we belong to */
42 struct list_head list; /* list of all regulators */
43 struct list_head slist; /* list of supplied regulators */
44
45 /* lists we own */
46 struct list_head consumer_list; /* consumers we supply */
47 struct list_head supply_list; /* regulators we supply */
48
49 struct blocking_notifier_head notifier;
50 struct mutex mutex; /* consumer lock */
51 struct module *owner;
52 struct device dev;
53 struct regulation_constraints *constraints;
54 struct regulator_dev *supply; /* for tree */
55
56 void *reg_data; /* regulator_dev data */
57};
58
59/**
60 * struct regulator_map
61 *
62 * Used to provide symbolic supply names to devices.
63 */
64struct regulator_map {
65 struct list_head list;
66 struct device *dev;
67 const char *supply;
a5766f11 68 struct regulator_dev *regulator;
414c70cb
LG
69};
70
414c70cb
LG
71/*
72 * struct regulator
73 *
74 * One for each consumer device.
75 */
76struct regulator {
77 struct device *dev;
78 struct list_head list;
79 int uA_load;
80 int min_uV;
81 int max_uV;
412aec61 82 int enabled; /* count of client enables */
414c70cb
LG
83 char *supply_name;
84 struct device_attribute dev_attr;
85 struct regulator_dev *rdev;
86};
87
88static int _regulator_is_enabled(struct regulator_dev *rdev);
89static int _regulator_disable(struct regulator_dev *rdev);
90static int _regulator_get_voltage(struct regulator_dev *rdev);
91static int _regulator_get_current_limit(struct regulator_dev *rdev);
92static unsigned int _regulator_get_mode(struct regulator_dev *rdev);
93static void _notifier_call_chain(struct regulator_dev *rdev,
94 unsigned long event, void *data);
95
96/* gets the regulator for a given consumer device */
97static struct regulator *get_device_regulator(struct device *dev)
98{
99 struct regulator *regulator = NULL;
100 struct regulator_dev *rdev;
101
102 mutex_lock(&regulator_list_mutex);
103 list_for_each_entry(rdev, &regulator_list, list) {
104 mutex_lock(&rdev->mutex);
105 list_for_each_entry(regulator, &rdev->consumer_list, list) {
106 if (regulator->dev == dev) {
107 mutex_unlock(&rdev->mutex);
108 mutex_unlock(&regulator_list_mutex);
109 return regulator;
110 }
111 }
112 mutex_unlock(&rdev->mutex);
113 }
114 mutex_unlock(&regulator_list_mutex);
115 return NULL;
116}
117
118/* Platform voltage constraint check */
119static int regulator_check_voltage(struct regulator_dev *rdev,
120 int *min_uV, int *max_uV)
121{
122 BUG_ON(*min_uV > *max_uV);
123
124 if (!rdev->constraints) {
125 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
126 rdev->desc->name);
127 return -ENODEV;
128 }
129 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_VOLTAGE)) {
130 printk(KERN_ERR "%s: operation not allowed for %s\n",
131 __func__, rdev->desc->name);
132 return -EPERM;
133 }
134
135 if (*max_uV > rdev->constraints->max_uV)
136 *max_uV = rdev->constraints->max_uV;
137 if (*min_uV < rdev->constraints->min_uV)
138 *min_uV = rdev->constraints->min_uV;
139
140 if (*min_uV > *max_uV)
141 return -EINVAL;
142
143 return 0;
144}
145
146/* current constraint check */
147static int regulator_check_current_limit(struct regulator_dev *rdev,
148 int *min_uA, int *max_uA)
149{
150 BUG_ON(*min_uA > *max_uA);
151
152 if (!rdev->constraints) {
153 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
154 rdev->desc->name);
155 return -ENODEV;
156 }
157 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_CURRENT)) {
158 printk(KERN_ERR "%s: operation not allowed for %s\n",
159 __func__, rdev->desc->name);
160 return -EPERM;
161 }
162
163 if (*max_uA > rdev->constraints->max_uA)
164 *max_uA = rdev->constraints->max_uA;
165 if (*min_uA < rdev->constraints->min_uA)
166 *min_uA = rdev->constraints->min_uA;
167
168 if (*min_uA > *max_uA)
169 return -EINVAL;
170
171 return 0;
172}
173
174/* operating mode constraint check */
175static int regulator_check_mode(struct regulator_dev *rdev, int mode)
176{
e573520b
DB
177 switch (mode) {
178 case REGULATOR_MODE_FAST:
179 case REGULATOR_MODE_NORMAL:
180 case REGULATOR_MODE_IDLE:
181 case REGULATOR_MODE_STANDBY:
182 break;
183 default:
184 return -EINVAL;
185 }
186
414c70cb
LG
187 if (!rdev->constraints) {
188 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
189 rdev->desc->name);
190 return -ENODEV;
191 }
192 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_MODE)) {
193 printk(KERN_ERR "%s: operation not allowed for %s\n",
194 __func__, rdev->desc->name);
195 return -EPERM;
196 }
197 if (!(rdev->constraints->valid_modes_mask & mode)) {
198 printk(KERN_ERR "%s: invalid mode %x for %s\n",
199 __func__, mode, rdev->desc->name);
200 return -EINVAL;
201 }
202 return 0;
203}
204
205/* dynamic regulator mode switching constraint check */
206static int regulator_check_drms(struct regulator_dev *rdev)
207{
208 if (!rdev->constraints) {
209 printk(KERN_ERR "%s: no constraints for %s\n", __func__,
210 rdev->desc->name);
211 return -ENODEV;
212 }
213 if (!(rdev->constraints->valid_ops_mask & REGULATOR_CHANGE_DRMS)) {
214 printk(KERN_ERR "%s: operation not allowed for %s\n",
215 __func__, rdev->desc->name);
216 return -EPERM;
217 }
218 return 0;
219}
220
221static ssize_t device_requested_uA_show(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 struct regulator *regulator;
225
226 regulator = get_device_regulator(dev);
227 if (regulator == NULL)
228 return 0;
229
230 return sprintf(buf, "%d\n", regulator->uA_load);
231}
232
233static ssize_t regulator_uV_show(struct device *dev,
234 struct device_attribute *attr, char *buf)
235{
a5766f11 236 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
237 ssize_t ret;
238
239 mutex_lock(&rdev->mutex);
240 ret = sprintf(buf, "%d\n", _regulator_get_voltage(rdev));
241 mutex_unlock(&rdev->mutex);
242
243 return ret;
244}
245
246static ssize_t regulator_uA_show(struct device *dev,
247 struct device_attribute *attr, char *buf)
248{
a5766f11 249 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
250
251 return sprintf(buf, "%d\n", _regulator_get_current_limit(rdev));
252}
253
bc558a60
MB
254static ssize_t regulator_name_show(struct device *dev,
255 struct device_attribute *attr, char *buf)
256{
257 struct regulator_dev *rdev = dev_get_drvdata(dev);
258 const char *name;
259
260 if (rdev->constraints->name)
261 name = rdev->constraints->name;
262 else if (rdev->desc->name)
263 name = rdev->desc->name;
264 else
265 name = "";
266
267 return sprintf(buf, "%s\n", name);
268}
269
414c70cb
LG
270static ssize_t regulator_opmode_show(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
a5766f11 273 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
274 int mode = _regulator_get_mode(rdev);
275
276 switch (mode) {
277 case REGULATOR_MODE_FAST:
278 return sprintf(buf, "fast\n");
279 case REGULATOR_MODE_NORMAL:
280 return sprintf(buf, "normal\n");
281 case REGULATOR_MODE_IDLE:
282 return sprintf(buf, "idle\n");
283 case REGULATOR_MODE_STANDBY:
284 return sprintf(buf, "standby\n");
285 }
286 return sprintf(buf, "unknown\n");
287}
288
289static ssize_t regulator_state_show(struct device *dev,
290 struct device_attribute *attr, char *buf)
291{
a5766f11 292 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
293 int state = _regulator_is_enabled(rdev);
294
295 if (state > 0)
296 return sprintf(buf, "enabled\n");
297 else if (state == 0)
298 return sprintf(buf, "disabled\n");
299 else
300 return sprintf(buf, "unknown\n");
301}
302
303static ssize_t regulator_min_uA_show(struct device *dev,
304 struct device_attribute *attr, char *buf)
305{
a5766f11 306 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
307
308 if (!rdev->constraints)
309 return sprintf(buf, "constraint not defined\n");
310
311 return sprintf(buf, "%d\n", rdev->constraints->min_uA);
312}
313
314static ssize_t regulator_max_uA_show(struct device *dev,
315 struct device_attribute *attr, char *buf)
316{
a5766f11 317 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
318
319 if (!rdev->constraints)
320 return sprintf(buf, "constraint not defined\n");
321
322 return sprintf(buf, "%d\n", rdev->constraints->max_uA);
323}
324
325static ssize_t regulator_min_uV_show(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
a5766f11 328 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
329
330 if (!rdev->constraints)
331 return sprintf(buf, "constraint not defined\n");
332
333 return sprintf(buf, "%d\n", rdev->constraints->min_uV);
334}
335
336static ssize_t regulator_max_uV_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->max_uV);
345}
346
347static ssize_t regulator_total_uA_show(struct device *dev,
348 struct device_attribute *attr, char *buf)
349{
a5766f11 350 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
351 struct regulator *regulator;
352 int uA = 0;
353
354 mutex_lock(&rdev->mutex);
355 list_for_each_entry(regulator, &rdev->consumer_list, list)
356 uA += regulator->uA_load;
357 mutex_unlock(&rdev->mutex);
358 return sprintf(buf, "%d\n", uA);
359}
360
361static ssize_t regulator_num_users_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
a5766f11 364 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
365 return sprintf(buf, "%d\n", rdev->use_count);
366}
367
368static ssize_t regulator_type_show(struct device *dev,
369 struct device_attribute *attr, char *buf)
370{
a5766f11 371 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
372
373 switch (rdev->desc->type) {
374 case REGULATOR_VOLTAGE:
375 return sprintf(buf, "voltage\n");
376 case REGULATOR_CURRENT:
377 return sprintf(buf, "current\n");
378 }
379 return sprintf(buf, "unknown\n");
380}
381
382static ssize_t regulator_suspend_mem_uV_show(struct device *dev,
383 struct device_attribute *attr, char *buf)
384{
a5766f11 385 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
386
387 if (!rdev->constraints)
388 return sprintf(buf, "not defined\n");
389 return sprintf(buf, "%d\n", rdev->constraints->state_mem.uV);
390}
391
392static ssize_t regulator_suspend_disk_uV_show(struct device *dev,
393 struct device_attribute *attr, char *buf)
394{
a5766f11 395 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
396
397 if (!rdev->constraints)
398 return sprintf(buf, "not defined\n");
399 return sprintf(buf, "%d\n", rdev->constraints->state_disk.uV);
400}
401
402static ssize_t regulator_suspend_standby_uV_show(struct device *dev,
403 struct device_attribute *attr, char *buf)
404{
a5766f11 405 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
406
407 if (!rdev->constraints)
408 return sprintf(buf, "not defined\n");
409 return sprintf(buf, "%d\n", rdev->constraints->state_standby.uV);
410}
411
412static ssize_t suspend_opmode_show(struct regulator_dev *rdev,
413 unsigned int mode, char *buf)
414{
415 switch (mode) {
416 case REGULATOR_MODE_FAST:
417 return sprintf(buf, "fast\n");
418 case REGULATOR_MODE_NORMAL:
419 return sprintf(buf, "normal\n");
420 case REGULATOR_MODE_IDLE:
421 return sprintf(buf, "idle\n");
422 case REGULATOR_MODE_STANDBY:
423 return sprintf(buf, "standby\n");
424 }
425 return sprintf(buf, "unknown\n");
426}
427
428static ssize_t regulator_suspend_mem_mode_show(struct device *dev,
429 struct device_attribute *attr, char *buf)
430{
a5766f11 431 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
432
433 if (!rdev->constraints)
434 return sprintf(buf, "not defined\n");
435 return suspend_opmode_show(rdev,
436 rdev->constraints->state_mem.mode, buf);
437}
438
439static ssize_t regulator_suspend_disk_mode_show(struct device *dev,
440 struct device_attribute *attr, char *buf)
441{
a5766f11 442 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
443
444 if (!rdev->constraints)
445 return sprintf(buf, "not defined\n");
446 return suspend_opmode_show(rdev,
447 rdev->constraints->state_disk.mode, buf);
448}
449
450static ssize_t regulator_suspend_standby_mode_show(struct device *dev,
451 struct device_attribute *attr, char *buf)
452{
a5766f11 453 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
454
455 if (!rdev->constraints)
456 return sprintf(buf, "not defined\n");
457 return suspend_opmode_show(rdev,
458 rdev->constraints->state_standby.mode, buf);
459}
460
461static ssize_t regulator_suspend_mem_state_show(struct device *dev,
462 struct device_attribute *attr, char *buf)
463{
a5766f11 464 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
465
466 if (!rdev->constraints)
467 return sprintf(buf, "not defined\n");
468
469 if (rdev->constraints->state_mem.enabled)
470 return sprintf(buf, "enabled\n");
471 else
472 return sprintf(buf, "disabled\n");
473}
474
475static ssize_t regulator_suspend_disk_state_show(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
a5766f11 478 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
479
480 if (!rdev->constraints)
481 return sprintf(buf, "not defined\n");
482
483 if (rdev->constraints->state_disk.enabled)
484 return sprintf(buf, "enabled\n");
485 else
486 return sprintf(buf, "disabled\n");
487}
488
489static ssize_t regulator_suspend_standby_state_show(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
a5766f11 492 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
493
494 if (!rdev->constraints)
495 return sprintf(buf, "not defined\n");
496
497 if (rdev->constraints->state_standby.enabled)
498 return sprintf(buf, "enabled\n");
499 else
500 return sprintf(buf, "disabled\n");
501}
bc558a60 502
414c70cb 503static struct device_attribute regulator_dev_attrs[] = {
bc558a60 504 __ATTR(name, 0444, regulator_name_show, NULL),
414c70cb
LG
505 __ATTR(microvolts, 0444, regulator_uV_show, NULL),
506 __ATTR(microamps, 0444, regulator_uA_show, NULL),
507 __ATTR(opmode, 0444, regulator_opmode_show, NULL),
508 __ATTR(state, 0444, regulator_state_show, NULL),
509 __ATTR(min_microvolts, 0444, regulator_min_uV_show, NULL),
510 __ATTR(min_microamps, 0444, regulator_min_uA_show, NULL),
511 __ATTR(max_microvolts, 0444, regulator_max_uV_show, NULL),
512 __ATTR(max_microamps, 0444, regulator_max_uA_show, NULL),
513 __ATTR(requested_microamps, 0444, regulator_total_uA_show, NULL),
514 __ATTR(num_users, 0444, regulator_num_users_show, NULL),
515 __ATTR(type, 0444, regulator_type_show, NULL),
516 __ATTR(suspend_mem_microvolts, 0444,
517 regulator_suspend_mem_uV_show, NULL),
518 __ATTR(suspend_disk_microvolts, 0444,
519 regulator_suspend_disk_uV_show, NULL),
520 __ATTR(suspend_standby_microvolts, 0444,
521 regulator_suspend_standby_uV_show, NULL),
522 __ATTR(suspend_mem_mode, 0444,
523 regulator_suspend_mem_mode_show, NULL),
524 __ATTR(suspend_disk_mode, 0444,
525 regulator_suspend_disk_mode_show, NULL),
526 __ATTR(suspend_standby_mode, 0444,
527 regulator_suspend_standby_mode_show, NULL),
528 __ATTR(suspend_mem_state, 0444,
529 regulator_suspend_mem_state_show, NULL),
530 __ATTR(suspend_disk_state, 0444,
531 regulator_suspend_disk_state_show, NULL),
532 __ATTR(suspend_standby_state, 0444,
533 regulator_suspend_standby_state_show, NULL),
534 __ATTR_NULL,
535};
536
537static void regulator_dev_release(struct device *dev)
538{
a5766f11 539 struct regulator_dev *rdev = dev_get_drvdata(dev);
414c70cb
LG
540 kfree(rdev);
541}
542
543static struct class regulator_class = {
544 .name = "regulator",
545 .dev_release = regulator_dev_release,
546 .dev_attrs = regulator_dev_attrs,
547};
548
549/* Calculate the new optimum regulator operating mode based on the new total
550 * consumer load. All locks held by caller */
551static void drms_uA_update(struct regulator_dev *rdev)
552{
553 struct regulator *sibling;
554 int current_uA = 0, output_uV, input_uV, err;
555 unsigned int mode;
556
557 err = regulator_check_drms(rdev);
558 if (err < 0 || !rdev->desc->ops->get_optimum_mode ||
559 !rdev->desc->ops->get_voltage || !rdev->desc->ops->set_mode);
560 return;
561
562 /* get output voltage */
563 output_uV = rdev->desc->ops->get_voltage(rdev);
564 if (output_uV <= 0)
565 return;
566
567 /* get input voltage */
568 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
569 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
570 else
571 input_uV = rdev->constraints->input_uV;
572 if (input_uV <= 0)
573 return;
574
575 /* calc total requested load */
576 list_for_each_entry(sibling, &rdev->consumer_list, list)
577 current_uA += sibling->uA_load;
578
579 /* now get the optimum mode for our new total regulator load */
580 mode = rdev->desc->ops->get_optimum_mode(rdev, input_uV,
581 output_uV, current_uA);
582
583 /* check the new mode is allowed */
584 err = regulator_check_mode(rdev, mode);
585 if (err == 0)
586 rdev->desc->ops->set_mode(rdev, mode);
587}
588
589static int suspend_set_state(struct regulator_dev *rdev,
590 struct regulator_state *rstate)
591{
592 int ret = 0;
593
594 /* enable & disable are mandatory for suspend control */
595 if (!rdev->desc->ops->set_suspend_enable ||
a5766f11
LG
596 !rdev->desc->ops->set_suspend_disable) {
597 printk(KERN_ERR "%s: no way to set suspend state\n",
598 __func__);
414c70cb 599 return -EINVAL;
a5766f11 600 }
414c70cb
LG
601
602 if (rstate->enabled)
603 ret = rdev->desc->ops->set_suspend_enable(rdev);
604 else
605 ret = rdev->desc->ops->set_suspend_disable(rdev);
606 if (ret < 0) {
607 printk(KERN_ERR "%s: failed to enabled/disable\n", __func__);
608 return ret;
609 }
610
611 if (rdev->desc->ops->set_suspend_voltage && rstate->uV > 0) {
612 ret = rdev->desc->ops->set_suspend_voltage(rdev, rstate->uV);
613 if (ret < 0) {
614 printk(KERN_ERR "%s: failed to set voltage\n",
615 __func__);
616 return ret;
617 }
618 }
619
620 if (rdev->desc->ops->set_suspend_mode && rstate->mode > 0) {
621 ret = rdev->desc->ops->set_suspend_mode(rdev, rstate->mode);
622 if (ret < 0) {
623 printk(KERN_ERR "%s: failed to set mode\n", __func__);
624 return ret;
625 }
626 }
627 return ret;
628}
629
630/* locks held by caller */
631static int suspend_prepare(struct regulator_dev *rdev, suspend_state_t state)
632{
633 if (!rdev->constraints)
634 return -EINVAL;
635
636 switch (state) {
637 case PM_SUSPEND_STANDBY:
638 return suspend_set_state(rdev,
639 &rdev->constraints->state_standby);
640 case PM_SUSPEND_MEM:
641 return suspend_set_state(rdev,
642 &rdev->constraints->state_mem);
643 case PM_SUSPEND_MAX:
644 return suspend_set_state(rdev,
645 &rdev->constraints->state_disk);
646 default:
647 return -EINVAL;
648 }
649}
650
651static void print_constraints(struct regulator_dev *rdev)
652{
653 struct regulation_constraints *constraints = rdev->constraints;
654 char buf[80];
655 int count;
656
657 if (rdev->desc->type == REGULATOR_VOLTAGE) {
658 if (constraints->min_uV == constraints->max_uV)
659 count = sprintf(buf, "%d mV ",
660 constraints->min_uV / 1000);
661 else
662 count = sprintf(buf, "%d <--> %d mV ",
663 constraints->min_uV / 1000,
664 constraints->max_uV / 1000);
665 } else {
666 if (constraints->min_uA == constraints->max_uA)
667 count = sprintf(buf, "%d mA ",
668 constraints->min_uA / 1000);
669 else
670 count = sprintf(buf, "%d <--> %d mA ",
671 constraints->min_uA / 1000,
672 constraints->max_uA / 1000);
673 }
674 if (constraints->valid_modes_mask & REGULATOR_MODE_FAST)
675 count += sprintf(buf + count, "fast ");
676 if (constraints->valid_modes_mask & REGULATOR_MODE_NORMAL)
677 count += sprintf(buf + count, "normal ");
678 if (constraints->valid_modes_mask & REGULATOR_MODE_IDLE)
679 count += sprintf(buf + count, "idle ");
680 if (constraints->valid_modes_mask & REGULATOR_MODE_STANDBY)
681 count += sprintf(buf + count, "standby");
682
683 printk(KERN_INFO "regulator: %s: %s\n", rdev->desc->name, buf);
684}
685
a5766f11
LG
686/**
687 * set_machine_constraints - sets regulator constraints
688 * @regulator: regulator source
689 *
690 * Allows platform initialisation code to define and constrain
691 * regulator circuits e.g. valid voltage/current ranges, etc. NOTE:
692 * Constraints *must* be set by platform code in order for some
693 * regulator operations to proceed i.e. set_voltage, set_current_limit,
694 * set_mode.
695 */
696static int set_machine_constraints(struct regulator_dev *rdev,
697 struct regulation_constraints *constraints)
698{
699 int ret = 0;
e06f5b4f 700 const char *name;
e5fda26c 701 struct regulator_ops *ops = rdev->desc->ops;
e06f5b4f
MB
702
703 if (constraints->name)
704 name = constraints->name;
705 else if (rdev->desc->name)
706 name = rdev->desc->name;
707 else
708 name = "regulator";
a5766f11
LG
709
710 rdev->constraints = constraints;
711
712 /* do we need to apply the constraint voltage */
713 if (rdev->constraints->apply_uV &&
714 rdev->constraints->min_uV == rdev->constraints->max_uV &&
e5fda26c
MB
715 ops->set_voltage) {
716 ret = ops->set_voltage(rdev,
a5766f11
LG
717 rdev->constraints->min_uV, rdev->constraints->max_uV);
718 if (ret < 0) {
e06f5b4f
MB
719 printk(KERN_ERR "%s: failed to apply %duV constraint to %s\n",
720 __func__,
721 rdev->constraints->min_uV, name);
a5766f11
LG
722 rdev->constraints = NULL;
723 goto out;
724 }
725 }
726
727 /* are we enabled at boot time by firmware / bootloader */
728 if (rdev->constraints->boot_on)
729 rdev->use_count = 1;
730
731 /* do we need to setup our suspend state */
e06f5b4f 732 if (constraints->initial_state) {
a5766f11 733 ret = suspend_prepare(rdev, constraints->initial_state);
e06f5b4f
MB
734 if (ret < 0) {
735 printk(KERN_ERR "%s: failed to set suspend state for %s\n",
736 __func__, name);
737 rdev->constraints = NULL;
738 goto out;
739 }
740 }
a5766f11 741
e5fda26c
MB
742 /* if always_on is set then turn the regulator on if it's not
743 * already on. */
744 if (constraints->always_on && ops->enable &&
745 ((ops->is_enabled && !ops->is_enabled(rdev)) ||
746 (!ops->is_enabled && !constraints->boot_on))) {
747 ret = ops->enable(rdev);
748 if (ret < 0) {
749 printk(KERN_ERR "%s: failed to enable %s\n",
750 __func__, name);
751 rdev->constraints = NULL;
752 goto out;
753 }
754 }
755
a5766f11
LG
756 print_constraints(rdev);
757out:
758 return ret;
759}
760
761/**
762 * set_supply - set regulator supply regulator
763 * @regulator: regulator name
764 * @supply: supply regulator name
765 *
766 * Called by platform initialisation code to set the supply regulator for this
767 * regulator. This ensures that a regulators supply will also be enabled by the
768 * core if it's child is enabled.
769 */
770static int set_supply(struct regulator_dev *rdev,
771 struct regulator_dev *supply_rdev)
772{
773 int err;
774
775 err = sysfs_create_link(&rdev->dev.kobj, &supply_rdev->dev.kobj,
776 "supply");
777 if (err) {
778 printk(KERN_ERR
779 "%s: could not add device link %s err %d\n",
780 __func__, supply_rdev->dev.kobj.name, err);
781 goto out;
782 }
783 rdev->supply = supply_rdev;
784 list_add(&rdev->slist, &supply_rdev->supply_list);
785out:
786 return err;
787}
788
789/**
790 * set_consumer_device_supply: Bind a regulator to a symbolic supply
791 * @regulator: regulator source
792 * @dev: device the supply applies to
793 * @supply: symbolic name for supply
794 *
795 * Allows platform initialisation code to map physical regulator
796 * sources to symbolic names for supplies for use by devices. Devices
797 * should use these symbolic names to request regulators, avoiding the
798 * need to provide board-specific regulator names as platform data.
799 */
800static int set_consumer_device_supply(struct regulator_dev *rdev,
801 struct device *consumer_dev, const char *supply)
802{
803 struct regulator_map *node;
804
805 if (supply == NULL)
806 return -EINVAL;
807
808 node = kmalloc(sizeof(struct regulator_map), GFP_KERNEL);
809 if (node == NULL)
810 return -ENOMEM;
811
812 node->regulator = rdev;
813 node->dev = consumer_dev;
814 node->supply = supply;
815
816 list_add(&node->list, &regulator_map_list);
817 return 0;
818}
819
820static void unset_consumer_device_supply(struct regulator_dev *rdev,
821 struct device *consumer_dev)
822{
823 struct regulator_map *node, *n;
824
825 list_for_each_entry_safe(node, n, &regulator_map_list, list) {
826 if (rdev == node->regulator &&
827 consumer_dev == node->dev) {
828 list_del(&node->list);
829 kfree(node);
830 return;
831 }
832 }
833}
834
414c70cb
LG
835#define REG_STR_SIZE 32
836
837static struct regulator *create_regulator(struct regulator_dev *rdev,
838 struct device *dev,
839 const char *supply_name)
840{
841 struct regulator *regulator;
842 char buf[REG_STR_SIZE];
843 int err, size;
844
845 regulator = kzalloc(sizeof(*regulator), GFP_KERNEL);
846 if (regulator == NULL)
847 return NULL;
848
849 mutex_lock(&rdev->mutex);
850 regulator->rdev = rdev;
851 list_add(&regulator->list, &rdev->consumer_list);
852
853 if (dev) {
854 /* create a 'requested_microamps_name' sysfs entry */
855 size = scnprintf(buf, REG_STR_SIZE, "microamps_requested_%s",
856 supply_name);
857 if (size >= REG_STR_SIZE)
858 goto overflow_err;
859
860 regulator->dev = dev;
861 regulator->dev_attr.attr.name = kstrdup(buf, GFP_KERNEL);
862 if (regulator->dev_attr.attr.name == NULL)
863 goto attr_name_err;
864
865 regulator->dev_attr.attr.owner = THIS_MODULE;
866 regulator->dev_attr.attr.mode = 0444;
867 regulator->dev_attr.show = device_requested_uA_show;
868 err = device_create_file(dev, &regulator->dev_attr);
869 if (err < 0) {
870 printk(KERN_WARNING "%s: could not add regulator_dev"
871 " load sysfs\n", __func__);
872 goto attr_name_err;
873 }
874
875 /* also add a link to the device sysfs entry */
876 size = scnprintf(buf, REG_STR_SIZE, "%s-%s",
877 dev->kobj.name, supply_name);
878 if (size >= REG_STR_SIZE)
879 goto attr_err;
880
881 regulator->supply_name = kstrdup(buf, GFP_KERNEL);
882 if (regulator->supply_name == NULL)
883 goto attr_err;
884
885 err = sysfs_create_link(&rdev->dev.kobj, &dev->kobj,
886 buf);
887 if (err) {
888 printk(KERN_WARNING
889 "%s: could not add device link %s err %d\n",
890 __func__, dev->kobj.name, err);
891 device_remove_file(dev, &regulator->dev_attr);
892 goto link_name_err;
893 }
894 }
895 mutex_unlock(&rdev->mutex);
896 return regulator;
897link_name_err:
898 kfree(regulator->supply_name);
899attr_err:
900 device_remove_file(regulator->dev, &regulator->dev_attr);
901attr_name_err:
902 kfree(regulator->dev_attr.attr.name);
903overflow_err:
904 list_del(&regulator->list);
905 kfree(regulator);
906 mutex_unlock(&rdev->mutex);
907 return NULL;
908}
909
910/**
911 * regulator_get - lookup and obtain a reference to a regulator.
912 * @dev: device for regulator "consumer"
913 * @id: Supply name or regulator ID.
914 *
915 * Returns a struct regulator corresponding to the regulator producer,
916 * or IS_ERR() condition containing errno. Use of supply names
917 * configured via regulator_set_device_supply() is strongly
918 * encouraged.
919 */
920struct regulator *regulator_get(struct device *dev, const char *id)
921{
922 struct regulator_dev *rdev;
923 struct regulator_map *map;
924 struct regulator *regulator = ERR_PTR(-ENODEV);
414c70cb
LG
925
926 if (id == NULL) {
927 printk(KERN_ERR "regulator: get() with no identifier\n");
928 return regulator;
929 }
930
931 mutex_lock(&regulator_list_mutex);
932
933 list_for_each_entry(map, &regulator_map_list, list) {
934 if (dev == map->dev &&
935 strcmp(map->supply, id) == 0) {
a5766f11 936 rdev = map->regulator;
414c70cb 937 goto found;
a5766f11 938 }
414c70cb
LG
939 }
940 printk(KERN_ERR "regulator: Unable to get requested regulator: %s\n",
941 id);
942 mutex_unlock(&regulator_list_mutex);
943 return regulator;
944
945found:
a5766f11
LG
946 if (!try_module_get(rdev->owner))
947 goto out;
948
414c70cb
LG
949 regulator = create_regulator(rdev, dev, id);
950 if (regulator == NULL) {
951 regulator = ERR_PTR(-ENOMEM);
952 module_put(rdev->owner);
953 }
954
a5766f11 955out:
414c70cb
LG
956 mutex_unlock(&regulator_list_mutex);
957 return regulator;
958}
959EXPORT_SYMBOL_GPL(regulator_get);
960
961/**
962 * regulator_put - "free" the regulator source
963 * @regulator: regulator source
964 *
965 * Note: drivers must ensure that all regulator_enable calls made on this
966 * regulator source are balanced by regulator_disable calls prior to calling
967 * this function.
968 */
969void regulator_put(struct regulator *regulator)
970{
971 struct regulator_dev *rdev;
972
973 if (regulator == NULL || IS_ERR(regulator))
974 return;
975
414c70cb
LG
976 mutex_lock(&regulator_list_mutex);
977 rdev = regulator->rdev;
978
412aec61
DB
979 if (WARN(regulator->enabled, "Releasing supply %s while enabled\n",
980 regulator->supply_name))
981 _regulator_disable(rdev);
982
414c70cb
LG
983 /* remove any sysfs entries */
984 if (regulator->dev) {
985 sysfs_remove_link(&rdev->dev.kobj, regulator->supply_name);
986 kfree(regulator->supply_name);
987 device_remove_file(regulator->dev, &regulator->dev_attr);
988 kfree(regulator->dev_attr.attr.name);
989 }
990 list_del(&regulator->list);
991 kfree(regulator);
992
993 module_put(rdev->owner);
994 mutex_unlock(&regulator_list_mutex);
995}
996EXPORT_SYMBOL_GPL(regulator_put);
997
998/* locks held by regulator_enable() */
999static int _regulator_enable(struct regulator_dev *rdev)
1000{
1001 int ret = -EINVAL;
1002
1003 if (!rdev->constraints) {
1004 printk(KERN_ERR "%s: %s has no constraints\n",
1005 __func__, rdev->desc->name);
1006 return ret;
1007 }
1008
1009 /* do we need to enable the supply regulator first */
1010 if (rdev->supply) {
1011 ret = _regulator_enable(rdev->supply);
1012 if (ret < 0) {
1013 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1014 __func__, rdev->desc->name, ret);
1015 return ret;
1016 }
1017 }
1018
1019 /* check voltage and requested load before enabling */
1020 if (rdev->desc->ops->enable) {
1021
1022 if (rdev->constraints &&
1023 (rdev->constraints->valid_ops_mask &
1024 REGULATOR_CHANGE_DRMS))
1025 drms_uA_update(rdev);
1026
1027 ret = rdev->desc->ops->enable(rdev);
1028 if (ret < 0) {
1029 printk(KERN_ERR "%s: failed to enable %s: %d\n",
1030 __func__, rdev->desc->name, ret);
1031 return ret;
1032 }
1033 rdev->use_count++;
1034 return ret;
1035 }
1036
1037 return ret;
1038}
1039
1040/**
1041 * regulator_enable - enable regulator output
1042 * @regulator: regulator source
1043 *
1044 * Enable the regulator output at the predefined voltage or current value.
1045 * NOTE: the output value can be set by other drivers, boot loader or may be
1046 * hardwired in the regulator.
1047 * NOTE: calls to regulator_enable() must be balanced with calls to
1048 * regulator_disable().
1049 */
1050int regulator_enable(struct regulator *regulator)
1051{
412aec61
DB
1052 struct regulator_dev *rdev = regulator->rdev;
1053 int ret = 0;
414c70cb 1054
412aec61
DB
1055 mutex_lock(&rdev->mutex);
1056 if (regulator->enabled == 0)
1057 ret = _regulator_enable(rdev);
1058 else if (regulator->enabled < 0)
1059 ret = -EIO;
1060 if (ret == 0)
1061 regulator->enabled++;
1062 mutex_unlock(&rdev->mutex);
414c70cb
LG
1063 return ret;
1064}
1065EXPORT_SYMBOL_GPL(regulator_enable);
1066
1067/* locks held by regulator_disable() */
1068static int _regulator_disable(struct regulator_dev *rdev)
1069{
1070 int ret = 0;
1071
1072 /* are we the last user and permitted to disable ? */
1073 if (rdev->use_count == 1 && !rdev->constraints->always_on) {
1074
1075 /* we are last user */
1076 if (rdev->desc->ops->disable) {
1077 ret = rdev->desc->ops->disable(rdev);
1078 if (ret < 0) {
1079 printk(KERN_ERR "%s: failed to disable %s\n",
1080 __func__, rdev->desc->name);
1081 return ret;
1082 }
1083 }
1084
1085 /* decrease our supplies ref count and disable if required */
1086 if (rdev->supply)
1087 _regulator_disable(rdev->supply);
1088
1089 rdev->use_count = 0;
1090 } else if (rdev->use_count > 1) {
1091
1092 if (rdev->constraints &&
1093 (rdev->constraints->valid_ops_mask &
1094 REGULATOR_CHANGE_DRMS))
1095 drms_uA_update(rdev);
1096
1097 rdev->use_count--;
1098 }
1099 return ret;
1100}
1101
1102/**
1103 * regulator_disable - disable regulator output
1104 * @regulator: regulator source
1105 *
1106 * Disable the regulator output voltage or current.
1107 * NOTE: this will only disable the regulator output if no other consumer
1108 * devices have it enabled.
1109 * NOTE: calls to regulator_enable() must be balanced with calls to
1110 * regulator_disable().
1111 */
1112int regulator_disable(struct regulator *regulator)
1113{
412aec61
DB
1114 struct regulator_dev *rdev = regulator->rdev;
1115 int ret = 0;
414c70cb 1116
412aec61
DB
1117 mutex_lock(&rdev->mutex);
1118 if (regulator->enabled == 1) {
1119 ret = _regulator_disable(rdev);
1120 if (ret == 0)
1121 regulator->uA_load = 0;
1122 } else if (WARN(regulator->enabled <= 0,
1123 "unbalanced disables for supply %s\n",
1124 regulator->supply_name))
1125 ret = -EIO;
1126 if (ret == 0)
1127 regulator->enabled--;
1128 mutex_unlock(&rdev->mutex);
414c70cb
LG
1129 return ret;
1130}
1131EXPORT_SYMBOL_GPL(regulator_disable);
1132
1133/* locks held by regulator_force_disable() */
1134static int _regulator_force_disable(struct regulator_dev *rdev)
1135{
1136 int ret = 0;
1137
1138 /* force disable */
1139 if (rdev->desc->ops->disable) {
1140 /* ah well, who wants to live forever... */
1141 ret = rdev->desc->ops->disable(rdev);
1142 if (ret < 0) {
1143 printk(KERN_ERR "%s: failed to force disable %s\n",
1144 __func__, rdev->desc->name);
1145 return ret;
1146 }
1147 /* notify other consumers that power has been forced off */
1148 _notifier_call_chain(rdev, REGULATOR_EVENT_FORCE_DISABLE,
1149 NULL);
1150 }
1151
1152 /* decrease our supplies ref count and disable if required */
1153 if (rdev->supply)
1154 _regulator_disable(rdev->supply);
1155
1156 rdev->use_count = 0;
1157 return ret;
1158}
1159
1160/**
1161 * regulator_force_disable - force disable regulator output
1162 * @regulator: regulator source
1163 *
1164 * Forcibly disable the regulator output voltage or current.
1165 * NOTE: this *will* disable the regulator output even if other consumer
1166 * devices have it enabled. This should be used for situations when device
1167 * damage will likely occur if the regulator is not disabled (e.g. over temp).
1168 */
1169int regulator_force_disable(struct regulator *regulator)
1170{
1171 int ret;
1172
1173 mutex_lock(&regulator->rdev->mutex);
1174 regulator->enabled = 0;
1175 regulator->uA_load = 0;
1176 ret = _regulator_force_disable(regulator->rdev);
1177 mutex_unlock(&regulator->rdev->mutex);
1178 return ret;
1179}
1180EXPORT_SYMBOL_GPL(regulator_force_disable);
1181
1182static int _regulator_is_enabled(struct regulator_dev *rdev)
1183{
1184 int ret;
1185
1186 mutex_lock(&rdev->mutex);
1187
1188 /* sanity check */
1189 if (!rdev->desc->ops->is_enabled) {
1190 ret = -EINVAL;
1191 goto out;
1192 }
1193
1194 ret = rdev->desc->ops->is_enabled(rdev);
1195out:
1196 mutex_unlock(&rdev->mutex);
1197 return ret;
1198}
1199
1200/**
1201 * regulator_is_enabled - is the regulator output enabled
1202 * @regulator: regulator source
1203 *
412aec61
DB
1204 * Returns positive if the regulator driver backing the source/client
1205 * has requested that the device be enabled, zero if it hasn't, else a
1206 * negative errno code.
1207 *
1208 * Note that the device backing this regulator handle can have multiple
1209 * users, so it might be enabled even if regulator_enable() was never
1210 * called for this particular source.
414c70cb
LG
1211 */
1212int regulator_is_enabled(struct regulator *regulator)
1213{
1214 return _regulator_is_enabled(regulator->rdev);
1215}
1216EXPORT_SYMBOL_GPL(regulator_is_enabled);
1217
1218/**
1219 * regulator_set_voltage - set regulator output voltage
1220 * @regulator: regulator source
1221 * @min_uV: Minimum required voltage in uV
1222 * @max_uV: Maximum acceptable voltage in uV
1223 *
1224 * Sets a voltage regulator to the desired output voltage. This can be set
1225 * during any regulator state. IOW, regulator can be disabled or enabled.
1226 *
1227 * If the regulator is enabled then the voltage will change to the new value
1228 * immediately otherwise if the regulator is disabled the regulator will
1229 * output at the new voltage when enabled.
1230 *
1231 * NOTE: If the regulator is shared between several devices then the lowest
1232 * request voltage that meets the system constraints will be used.
1233 * NOTE: Regulator system constraints must be set for this regulator before
1234 * calling this function otherwise this call will fail.
1235 */
1236int regulator_set_voltage(struct regulator *regulator, int min_uV, int max_uV)
1237{
1238 struct regulator_dev *rdev = regulator->rdev;
1239 int ret;
1240
1241 mutex_lock(&rdev->mutex);
1242
1243 /* sanity check */
1244 if (!rdev->desc->ops->set_voltage) {
1245 ret = -EINVAL;
1246 goto out;
1247 }
1248
1249 /* constraints check */
1250 ret = regulator_check_voltage(rdev, &min_uV, &max_uV);
1251 if (ret < 0)
1252 goto out;
1253 regulator->min_uV = min_uV;
1254 regulator->max_uV = max_uV;
1255 ret = rdev->desc->ops->set_voltage(rdev, min_uV, max_uV);
1256
1257out:
1258 mutex_unlock(&rdev->mutex);
1259 return ret;
1260}
1261EXPORT_SYMBOL_GPL(regulator_set_voltage);
1262
1263static int _regulator_get_voltage(struct regulator_dev *rdev)
1264{
1265 /* sanity check */
1266 if (rdev->desc->ops->get_voltage)
1267 return rdev->desc->ops->get_voltage(rdev);
1268 else
1269 return -EINVAL;
1270}
1271
1272/**
1273 * regulator_get_voltage - get regulator output voltage
1274 * @regulator: regulator source
1275 *
1276 * This returns the current regulator voltage in uV.
1277 *
1278 * NOTE: If the regulator is disabled it will return the voltage value. This
1279 * function should not be used to determine regulator state.
1280 */
1281int regulator_get_voltage(struct regulator *regulator)
1282{
1283 int ret;
1284
1285 mutex_lock(&regulator->rdev->mutex);
1286
1287 ret = _regulator_get_voltage(regulator->rdev);
1288
1289 mutex_unlock(&regulator->rdev->mutex);
1290
1291 return ret;
1292}
1293EXPORT_SYMBOL_GPL(regulator_get_voltage);
1294
1295/**
1296 * regulator_set_current_limit - set regulator output current limit
1297 * @regulator: regulator source
1298 * @min_uA: Minimuum supported current in uA
1299 * @max_uA: Maximum supported current in uA
1300 *
1301 * Sets current sink to the desired output current. This can be set during
1302 * any regulator state. IOW, regulator can be disabled or enabled.
1303 *
1304 * If the regulator is enabled then the current will change to the new value
1305 * immediately otherwise if the regulator is disabled the regulator will
1306 * output at the new current when enabled.
1307 *
1308 * NOTE: Regulator system constraints must be set for this regulator before
1309 * calling this function otherwise this call will fail.
1310 */
1311int regulator_set_current_limit(struct regulator *regulator,
1312 int min_uA, int max_uA)
1313{
1314 struct regulator_dev *rdev = regulator->rdev;
1315 int ret;
1316
1317 mutex_lock(&rdev->mutex);
1318
1319 /* sanity check */
1320 if (!rdev->desc->ops->set_current_limit) {
1321 ret = -EINVAL;
1322 goto out;
1323 }
1324
1325 /* constraints check */
1326 ret = regulator_check_current_limit(rdev, &min_uA, &max_uA);
1327 if (ret < 0)
1328 goto out;
1329
1330 ret = rdev->desc->ops->set_current_limit(rdev, min_uA, max_uA);
1331out:
1332 mutex_unlock(&rdev->mutex);
1333 return ret;
1334}
1335EXPORT_SYMBOL_GPL(regulator_set_current_limit);
1336
1337static int _regulator_get_current_limit(struct regulator_dev *rdev)
1338{
1339 int ret;
1340
1341 mutex_lock(&rdev->mutex);
1342
1343 /* sanity check */
1344 if (!rdev->desc->ops->get_current_limit) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
1348
1349 ret = rdev->desc->ops->get_current_limit(rdev);
1350out:
1351 mutex_unlock(&rdev->mutex);
1352 return ret;
1353}
1354
1355/**
1356 * regulator_get_current_limit - get regulator output current
1357 * @regulator: regulator source
1358 *
1359 * This returns the current supplied by the specified current sink in uA.
1360 *
1361 * NOTE: If the regulator is disabled it will return the current value. This
1362 * function should not be used to determine regulator state.
1363 */
1364int regulator_get_current_limit(struct regulator *regulator)
1365{
1366 return _regulator_get_current_limit(regulator->rdev);
1367}
1368EXPORT_SYMBOL_GPL(regulator_get_current_limit);
1369
1370/**
1371 * regulator_set_mode - set regulator operating mode
1372 * @regulator: regulator source
1373 * @mode: operating mode - one of the REGULATOR_MODE constants
1374 *
1375 * Set regulator operating mode to increase regulator efficiency or improve
1376 * regulation performance.
1377 *
1378 * NOTE: Regulator system constraints must be set for this regulator before
1379 * calling this function otherwise this call will fail.
1380 */
1381int regulator_set_mode(struct regulator *regulator, unsigned int mode)
1382{
1383 struct regulator_dev *rdev = regulator->rdev;
1384 int ret;
1385
1386 mutex_lock(&rdev->mutex);
1387
1388 /* sanity check */
1389 if (!rdev->desc->ops->set_mode) {
1390 ret = -EINVAL;
1391 goto out;
1392 }
1393
1394 /* constraints check */
1395 ret = regulator_check_mode(rdev, mode);
1396 if (ret < 0)
1397 goto out;
1398
1399 ret = rdev->desc->ops->set_mode(rdev, mode);
1400out:
1401 mutex_unlock(&rdev->mutex);
1402 return ret;
1403}
1404EXPORT_SYMBOL_GPL(regulator_set_mode);
1405
1406static unsigned int _regulator_get_mode(struct regulator_dev *rdev)
1407{
1408 int ret;
1409
1410 mutex_lock(&rdev->mutex);
1411
1412 /* sanity check */
1413 if (!rdev->desc->ops->get_mode) {
1414 ret = -EINVAL;
1415 goto out;
1416 }
1417
1418 ret = rdev->desc->ops->get_mode(rdev);
1419out:
1420 mutex_unlock(&rdev->mutex);
1421 return ret;
1422}
1423
1424/**
1425 * regulator_get_mode - get regulator operating mode
1426 * @regulator: regulator source
1427 *
1428 * Get the current regulator operating mode.
1429 */
1430unsigned int regulator_get_mode(struct regulator *regulator)
1431{
1432 return _regulator_get_mode(regulator->rdev);
1433}
1434EXPORT_SYMBOL_GPL(regulator_get_mode);
1435
1436/**
1437 * regulator_set_optimum_mode - set regulator optimum operating mode
1438 * @regulator: regulator source
1439 * @uA_load: load current
1440 *
1441 * Notifies the regulator core of a new device load. This is then used by
1442 * DRMS (if enabled by constraints) to set the most efficient regulator
1443 * operating mode for the new regulator loading.
1444 *
1445 * Consumer devices notify their supply regulator of the maximum power
1446 * they will require (can be taken from device datasheet in the power
1447 * consumption tables) when they change operational status and hence power
1448 * state. Examples of operational state changes that can affect power
1449 * consumption are :-
1450 *
1451 * o Device is opened / closed.
1452 * o Device I/O is about to begin or has just finished.
1453 * o Device is idling in between work.
1454 *
1455 * This information is also exported via sysfs to userspace.
1456 *
1457 * DRMS will sum the total requested load on the regulator and change
1458 * to the most efficient operating mode if platform constraints allow.
1459 *
1460 * Returns the new regulator mode or error.
1461 */
1462int regulator_set_optimum_mode(struct regulator *regulator, int uA_load)
1463{
1464 struct regulator_dev *rdev = regulator->rdev;
1465 struct regulator *consumer;
1466 int ret, output_uV, input_uV, total_uA_load = 0;
1467 unsigned int mode;
1468
1469 mutex_lock(&rdev->mutex);
1470
1471 regulator->uA_load = uA_load;
1472 ret = regulator_check_drms(rdev);
1473 if (ret < 0)
1474 goto out;
1475 ret = -EINVAL;
1476
1477 /* sanity check */
1478 if (!rdev->desc->ops->get_optimum_mode)
1479 goto out;
1480
1481 /* get output voltage */
1482 output_uV = rdev->desc->ops->get_voltage(rdev);
1483 if (output_uV <= 0) {
1484 printk(KERN_ERR "%s: invalid output voltage found for %s\n",
1485 __func__, rdev->desc->name);
1486 goto out;
1487 }
1488
1489 /* get input voltage */
1490 if (rdev->supply && rdev->supply->desc->ops->get_voltage)
1491 input_uV = rdev->supply->desc->ops->get_voltage(rdev->supply);
1492 else
1493 input_uV = rdev->constraints->input_uV;
1494 if (input_uV <= 0) {
1495 printk(KERN_ERR "%s: invalid input voltage found for %s\n",
1496 __func__, rdev->desc->name);
1497 goto out;
1498 }
1499
1500 /* calc total requested load for this regulator */
1501 list_for_each_entry(consumer, &rdev->consumer_list, list)
1502 total_uA_load += consumer->uA_load;
1503
1504 mode = rdev->desc->ops->get_optimum_mode(rdev,
1505 input_uV, output_uV,
1506 total_uA_load);
e573520b
DB
1507 ret = regulator_check_mode(rdev, mode);
1508 if (ret < 0) {
414c70cb
LG
1509 printk(KERN_ERR "%s: failed to get optimum mode for %s @"
1510 " %d uA %d -> %d uV\n", __func__, rdev->desc->name,
1511 total_uA_load, input_uV, output_uV);
1512 goto out;
1513 }
1514
1515 ret = rdev->desc->ops->set_mode(rdev, mode);
e573520b 1516 if (ret < 0) {
414c70cb
LG
1517 printk(KERN_ERR "%s: failed to set optimum mode %x for %s\n",
1518 __func__, mode, rdev->desc->name);
1519 goto out;
1520 }
1521 ret = mode;
1522out:
1523 mutex_unlock(&rdev->mutex);
1524 return ret;
1525}
1526EXPORT_SYMBOL_GPL(regulator_set_optimum_mode);
1527
1528/**
1529 * regulator_register_notifier - register regulator event notifier
1530 * @regulator: regulator source
1531 * @notifier_block: notifier block
1532 *
1533 * Register notifier block to receive regulator events.
1534 */
1535int regulator_register_notifier(struct regulator *regulator,
1536 struct notifier_block *nb)
1537{
1538 return blocking_notifier_chain_register(&regulator->rdev->notifier,
1539 nb);
1540}
1541EXPORT_SYMBOL_GPL(regulator_register_notifier);
1542
1543/**
1544 * regulator_unregister_notifier - unregister regulator event notifier
1545 * @regulator: regulator source
1546 * @notifier_block: notifier block
1547 *
1548 * Unregister regulator event notifier block.
1549 */
1550int regulator_unregister_notifier(struct regulator *regulator,
1551 struct notifier_block *nb)
1552{
1553 return blocking_notifier_chain_unregister(&regulator->rdev->notifier,
1554 nb);
1555}
1556EXPORT_SYMBOL_GPL(regulator_unregister_notifier);
1557
1558/* notify regulator consumers and downstream regulator consumers */
1559static void _notifier_call_chain(struct regulator_dev *rdev,
1560 unsigned long event, void *data)
1561{
1562 struct regulator_dev *_rdev;
1563
1564 /* call rdev chain first */
1565 mutex_lock(&rdev->mutex);
1566 blocking_notifier_call_chain(&rdev->notifier, event, NULL);
1567 mutex_unlock(&rdev->mutex);
1568
1569 /* now notify regulator we supply */
1570 list_for_each_entry(_rdev, &rdev->supply_list, slist)
1571 _notifier_call_chain(_rdev, event, data);
1572}
1573
1574/**
1575 * regulator_bulk_get - get multiple regulator consumers
1576 *
1577 * @dev: Device to supply
1578 * @num_consumers: Number of consumers to register
1579 * @consumers: Configuration of consumers; clients are stored here.
1580 *
1581 * @return 0 on success, an errno on failure.
1582 *
1583 * This helper function allows drivers to get several regulator
1584 * consumers in one operation. If any of the regulators cannot be
1585 * acquired then any regulators that were allocated will be freed
1586 * before returning to the caller.
1587 */
1588int regulator_bulk_get(struct device *dev, int num_consumers,
1589 struct regulator_bulk_data *consumers)
1590{
1591 int i;
1592 int ret;
1593
1594 for (i = 0; i < num_consumers; i++)
1595 consumers[i].consumer = NULL;
1596
1597 for (i = 0; i < num_consumers; i++) {
1598 consumers[i].consumer = regulator_get(dev,
1599 consumers[i].supply);
1600 if (IS_ERR(consumers[i].consumer)) {
1601 dev_err(dev, "Failed to get supply '%s'\n",
1602 consumers[i].supply);
1603 ret = PTR_ERR(consumers[i].consumer);
1604 consumers[i].consumer = NULL;
1605 goto err;
1606 }
1607 }
1608
1609 return 0;
1610
1611err:
1612 for (i = 0; i < num_consumers && consumers[i].consumer; i++)
1613 regulator_put(consumers[i].consumer);
1614
1615 return ret;
1616}
1617EXPORT_SYMBOL_GPL(regulator_bulk_get);
1618
1619/**
1620 * regulator_bulk_enable - enable multiple regulator consumers
1621 *
1622 * @num_consumers: Number of consumers
1623 * @consumers: Consumer data; clients are stored here.
1624 * @return 0 on success, an errno on failure
1625 *
1626 * This convenience API allows consumers to enable multiple regulator
1627 * clients in a single API call. If any consumers cannot be enabled
1628 * then any others that were enabled will be disabled again prior to
1629 * return.
1630 */
1631int regulator_bulk_enable(int num_consumers,
1632 struct regulator_bulk_data *consumers)
1633{
1634 int i;
1635 int ret;
1636
1637 for (i = 0; i < num_consumers; i++) {
1638 ret = regulator_enable(consumers[i].consumer);
1639 if (ret != 0)
1640 goto err;
1641 }
1642
1643 return 0;
1644
1645err:
1646 printk(KERN_ERR "Failed to enable %s\n", consumers[i].supply);
1647 for (i = 0; i < num_consumers; i++)
1648 regulator_disable(consumers[i].consumer);
1649
1650 return ret;
1651}
1652EXPORT_SYMBOL_GPL(regulator_bulk_enable);
1653
1654/**
1655 * regulator_bulk_disable - disable multiple regulator consumers
1656 *
1657 * @num_consumers: Number of consumers
1658 * @consumers: Consumer data; clients are stored here.
1659 * @return 0 on success, an errno on failure
1660 *
1661 * This convenience API allows consumers to disable multiple regulator
1662 * clients in a single API call. If any consumers cannot be enabled
1663 * then any others that were disabled will be disabled again prior to
1664 * return.
1665 */
1666int regulator_bulk_disable(int num_consumers,
1667 struct regulator_bulk_data *consumers)
1668{
1669 int i;
1670 int ret;
1671
1672 for (i = 0; i < num_consumers; i++) {
1673 ret = regulator_disable(consumers[i].consumer);
1674 if (ret != 0)
1675 goto err;
1676 }
1677
1678 return 0;
1679
1680err:
1681 printk(KERN_ERR "Failed to disable %s\n", consumers[i].supply);
1682 for (i = 0; i < num_consumers; i++)
1683 regulator_enable(consumers[i].consumer);
1684
1685 return ret;
1686}
1687EXPORT_SYMBOL_GPL(regulator_bulk_disable);
1688
1689/**
1690 * regulator_bulk_free - free multiple regulator consumers
1691 *
1692 * @num_consumers: Number of consumers
1693 * @consumers: Consumer data; clients are stored here.
1694 *
1695 * This convenience API allows consumers to free multiple regulator
1696 * clients in a single API call.
1697 */
1698void regulator_bulk_free(int num_consumers,
1699 struct regulator_bulk_data *consumers)
1700{
1701 int i;
1702
1703 for (i = 0; i < num_consumers; i++) {
1704 regulator_put(consumers[i].consumer);
1705 consumers[i].consumer = NULL;
1706 }
1707}
1708EXPORT_SYMBOL_GPL(regulator_bulk_free);
1709
1710/**
1711 * regulator_notifier_call_chain - call regulator event notifier
1712 * @regulator: regulator source
1713 * @event: notifier block
1714 * @data:
1715 *
1716 * Called by regulator drivers to notify clients a regulator event has
1717 * occurred. We also notify regulator clients downstream.
1718 */
1719int regulator_notifier_call_chain(struct regulator_dev *rdev,
1720 unsigned long event, void *data)
1721{
1722 _notifier_call_chain(rdev, event, data);
1723 return NOTIFY_DONE;
1724
1725}
1726EXPORT_SYMBOL_GPL(regulator_notifier_call_chain);
1727
1728/**
1729 * regulator_register - register regulator
1730 * @regulator: regulator source
1731 * @reg_data: private regulator data
1732 *
1733 * Called by regulator drivers to register a regulator.
1734 * Returns 0 on success.
1735 */
1736struct regulator_dev *regulator_register(struct regulator_desc *regulator_desc,
a5766f11 1737 struct device *dev, void *driver_data)
414c70cb
LG
1738{
1739 static atomic_t regulator_no = ATOMIC_INIT(0);
1740 struct regulator_dev *rdev;
a5766f11
LG
1741 struct regulator_init_data *init_data = dev->platform_data;
1742 int ret, i;
414c70cb
LG
1743
1744 if (regulator_desc == NULL)
1745 return ERR_PTR(-EINVAL);
1746
1747 if (regulator_desc->name == NULL || regulator_desc->ops == NULL)
1748 return ERR_PTR(-EINVAL);
1749
1750 if (!regulator_desc->type == REGULATOR_VOLTAGE &&
1751 !regulator_desc->type == REGULATOR_CURRENT)
1752 return ERR_PTR(-EINVAL);
1753
46fabe1e
MB
1754 if (!init_data)
1755 return ERR_PTR(-EINVAL);
1756
414c70cb
LG
1757 rdev = kzalloc(sizeof(struct regulator_dev), GFP_KERNEL);
1758 if (rdev == NULL)
1759 return ERR_PTR(-ENOMEM);
1760
1761 mutex_lock(&regulator_list_mutex);
1762
1763 mutex_init(&rdev->mutex);
a5766f11 1764 rdev->reg_data = driver_data;
414c70cb
LG
1765 rdev->owner = regulator_desc->owner;
1766 rdev->desc = regulator_desc;
1767 INIT_LIST_HEAD(&rdev->consumer_list);
1768 INIT_LIST_HEAD(&rdev->supply_list);
1769 INIT_LIST_HEAD(&rdev->list);
1770 INIT_LIST_HEAD(&rdev->slist);
1771 BLOCKING_INIT_NOTIFIER_HEAD(&rdev->notifier);
1772
a5766f11
LG
1773 /* preform any regulator specific init */
1774 if (init_data->regulator_init) {
1775 ret = init_data->regulator_init(rdev->reg_data);
1776 if (ret < 0) {
1777 kfree(rdev);
1778 rdev = ERR_PTR(ret);
1779 goto out;
1780 }
1781 }
1782
1783 /* set regulator constraints */
1784 ret = set_machine_constraints(rdev, &init_data->constraints);
1785 if (ret < 0) {
1786 kfree(rdev);
1787 rdev = ERR_PTR(ret);
1788 goto out;
1789 }
1790
1791 /* register with sysfs */
414c70cb 1792 rdev->dev.class = &regulator_class;
a5766f11 1793 rdev->dev.parent = dev;
812460a9
KS
1794 dev_set_name(&rdev->dev, "regulator.%d",
1795 atomic_inc_return(&regulator_no) - 1);
a5766f11
LG
1796 ret = device_register(&rdev->dev);
1797 if (ret != 0) {
414c70cb
LG
1798 kfree(rdev);
1799 rdev = ERR_PTR(ret);
a5766f11
LG
1800 goto out;
1801 }
1802
1803 dev_set_drvdata(&rdev->dev, rdev);
1804
1805 /* set supply regulator if it exists */
1806 if (init_data->supply_regulator_dev) {
1807 ret = set_supply(rdev,
1808 dev_get_drvdata(init_data->supply_regulator_dev));
1809 if (ret < 0) {
1810 device_unregister(&rdev->dev);
1811 kfree(rdev);
1812 rdev = ERR_PTR(ret);
1813 goto out;
1814 }
1815 }
1816
1817 /* add consumers devices */
1818 for (i = 0; i < init_data->num_consumer_supplies; i++) {
1819 ret = set_consumer_device_supply(rdev,
1820 init_data->consumer_supplies[i].dev,
1821 init_data->consumer_supplies[i].supply);
1822 if (ret < 0) {
1823 for (--i; i >= 0; i--)
1824 unset_consumer_device_supply(rdev,
1825 init_data->consumer_supplies[i].dev);
1826 device_unregister(&rdev->dev);
1827 kfree(rdev);
1828 rdev = ERR_PTR(ret);
1829 goto out;
1830 }
414c70cb 1831 }
a5766f11
LG
1832
1833 list_add(&rdev->list, &regulator_list);
1834out:
414c70cb
LG
1835 mutex_unlock(&regulator_list_mutex);
1836 return rdev;
1837}
1838EXPORT_SYMBOL_GPL(regulator_register);
1839
1840/**
1841 * regulator_unregister - unregister regulator
1842 * @regulator: regulator source
1843 *
1844 * Called by regulator drivers to unregister a regulator.
1845 */
1846void regulator_unregister(struct regulator_dev *rdev)
1847{
1848 if (rdev == NULL)
1849 return;
1850
1851 mutex_lock(&regulator_list_mutex);
1852 list_del(&rdev->list);
1853 if (rdev->supply)
1854 sysfs_remove_link(&rdev->dev.kobj, "supply");
1855 device_unregister(&rdev->dev);
1856 mutex_unlock(&regulator_list_mutex);
1857}
1858EXPORT_SYMBOL_GPL(regulator_unregister);
1859
414c70cb
LG
1860/**
1861 * regulator_suspend_prepare: prepare regulators for system wide suspend
1862 * @state: system suspend state
1863 *
1864 * Configure each regulator with it's suspend operating parameters for state.
1865 * This will usually be called by machine suspend code prior to supending.
1866 */
1867int regulator_suspend_prepare(suspend_state_t state)
1868{
1869 struct regulator_dev *rdev;
1870 int ret = 0;
1871
1872 /* ON is handled by regulator active state */
1873 if (state == PM_SUSPEND_ON)
1874 return -EINVAL;
1875
1876 mutex_lock(&regulator_list_mutex);
1877 list_for_each_entry(rdev, &regulator_list, list) {
1878
1879 mutex_lock(&rdev->mutex);
1880 ret = suspend_prepare(rdev, state);
1881 mutex_unlock(&rdev->mutex);
1882
1883 if (ret < 0) {
1884 printk(KERN_ERR "%s: failed to prepare %s\n",
1885 __func__, rdev->desc->name);
1886 goto out;
1887 }
1888 }
1889out:
1890 mutex_unlock(&regulator_list_mutex);
1891 return ret;
1892}
1893EXPORT_SYMBOL_GPL(regulator_suspend_prepare);
1894
1895/**
1896 * rdev_get_drvdata - get rdev regulator driver data
1897 * @regulator: regulator
1898 *
1899 * Get rdev regulator driver private data. This call can be used in the
1900 * regulator driver context.
1901 */
1902void *rdev_get_drvdata(struct regulator_dev *rdev)
1903{
1904 return rdev->reg_data;
1905}
1906EXPORT_SYMBOL_GPL(rdev_get_drvdata);
1907
1908/**
1909 * regulator_get_drvdata - get regulator driver data
1910 * @regulator: regulator
1911 *
1912 * Get regulator driver private data. This call can be used in the consumer
1913 * driver context when non API regulator specific functions need to be called.
1914 */
1915void *regulator_get_drvdata(struct regulator *regulator)
1916{
1917 return regulator->rdev->reg_data;
1918}
1919EXPORT_SYMBOL_GPL(regulator_get_drvdata);
1920
1921/**
1922 * regulator_set_drvdata - set regulator driver data
1923 * @regulator: regulator
1924 * @data: data
1925 */
1926void regulator_set_drvdata(struct regulator *regulator, void *data)
1927{
1928 regulator->rdev->reg_data = data;
1929}
1930EXPORT_SYMBOL_GPL(regulator_set_drvdata);
1931
1932/**
1933 * regulator_get_id - get regulator ID
1934 * @regulator: regulator
1935 */
1936int rdev_get_id(struct regulator_dev *rdev)
1937{
1938 return rdev->desc->id;
1939}
1940EXPORT_SYMBOL_GPL(rdev_get_id);
1941
a5766f11
LG
1942struct device *rdev_get_dev(struct regulator_dev *rdev)
1943{
1944 return &rdev->dev;
1945}
1946EXPORT_SYMBOL_GPL(rdev_get_dev);
1947
1948void *regulator_get_init_drvdata(struct regulator_init_data *reg_init_data)
1949{
1950 return reg_init_data->driver_data;
1951}
1952EXPORT_SYMBOL_GPL(regulator_get_init_drvdata);
1953
414c70cb
LG
1954static int __init regulator_init(void)
1955{
1956 printk(KERN_INFO "regulator: core version %s\n", REGULATOR_VERSION);
1957 return class_register(&regulator_class);
1958}
1959
1960/* init early to allow our consumers to complete system booting */
1961core_initcall(regulator_init);