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