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